aidl-parser 0.12.3

Parse AIDL files, crate AST and diagnostics
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use core::fmt;
use std::collections::HashMap;

use serde_derive::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Aidl {
    pub package: Package,
    pub imports: Vec<Import>,
    pub declared_parcelables: Vec<Import>,
    pub item: Item,
}

pub type ItemKey = String;
pub type ItemKeyRef<'a> = &'a str;

impl Aidl {
    // TODO: cache it
    pub fn get_key(&self) -> ItemKey {
        format!("{}.{}", self.package.name, self.item.get_name())
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Position {
    pub offset: usize,

    /// 1-based line and column
    pub line_col: (usize, usize),
}

impl Position {
    pub(crate) fn new(lookup: &line_col::LineColLookup, offset: usize) -> Self {
        Position {
            offset,
            line_col: lookup.get_by_cluster(offset),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Range {
    pub start: Position,
    pub end: Position,
}

impl Range {
    pub(crate) fn new(lookup: &line_col::LineColLookup, start: usize, end: usize) -> Self {
        let start = Position::new(lookup, start);
        let end = Position::new(lookup, end);

        Range { start, end }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Package {
    pub name: String,
    pub symbol_range: Range,
    pub full_range: Range,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Import {
    pub path: String,
    pub name: String,
    pub symbol_range: Range,
    pub full_range: Range,
}

impl Import {
    // TODO: cache it?
    pub fn get_qualified_name(&self) -> String {
        if self.path.is_empty() {
            self.name.clone()
        } else {
            format!("{}.{}", self.path, self.name)
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum InterfaceElement {
    Const(Const),
    Method(Method),
}

impl InterfaceElement {
    pub fn as_method(&self) -> Option<&Method> {
        match &self {
            InterfaceElement::Method(m) => Some(m),
            _ => None,
        }
    }

    pub fn get_name(&self) -> &str {
        match self {
            InterfaceElement::Const(c) => &c.name,
            InterfaceElement::Method(m) => &m.name,
        }
    }

    pub fn get_symbol_range(&self) -> &Range {
        match self {
            InterfaceElement::Const(c) => &c.symbol_range,
            InterfaceElement::Method(m) => &m.symbol_range,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ResolvedItemKind {
    Interface,
    Parcelable,
    Enum,
    ForwardDeclaredParcelable,
    UnknownImport,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum Item {
    Interface(Interface),
    Parcelable(Parcelable),
    Enum(Enum),
}

impl Item {
    pub fn as_interface(&self) -> Option<&Interface> {
        match &self {
            Item::Interface(i) => Some(i),
            _ => None,
        }
    }

    pub fn as_parcelable(&self) -> Option<&Parcelable> {
        match &self {
            Item::Parcelable(p) => Some(p),
            _ => None,
        }
    }

    pub fn as_enum(&self) -> Option<&Enum> {
        match &self {
            Item::Enum(e) => Some(e),
            _ => None,
        }
    }

    pub fn get_kind(&self) -> ResolvedItemKind {
        match self {
            Item::Interface(_) => ResolvedItemKind::Interface,
            Item::Parcelable(_) => ResolvedItemKind::Parcelable,
            Item::Enum(_) => ResolvedItemKind::Enum,
        }
    }

    pub fn get_name(&self) -> &str {
        match self {
            Item::Interface(i) => &i.name,
            Item::Parcelable(p) => &p.name,
            Item::Enum(e) => &e.name,
        }
    }

    pub fn get_symbol_range(&self) -> &Range {
        match self {
            Item::Interface(i) => &i.symbol_range,
            Item::Parcelable(p) => &p.symbol_range,
            Item::Enum(e) => &e.symbol_range,
        }
    }

    pub fn get_full_range(&self) -> &Range {
        match self {
            Item::Interface(i) => &i.full_range,
            Item::Parcelable(p) => &p.full_range,
            Item::Enum(e) => &e.full_range,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Interface {
    pub oneway: bool,
    pub name: String,
    pub elements: Vec<InterfaceElement>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<Annotation>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc: Option<String>,
    pub full_range: Range,
    pub symbol_range: Range,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Parcelable {
    pub name: String,
    pub elements: Vec<ParcelableElement>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<Annotation>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc: Option<String>,
    pub full_range: Range,
    pub symbol_range: Range,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Enum {
    pub name: String,
    pub elements: Vec<EnumElement>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<Annotation>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc: Option<String>,
    pub full_range: Range,
    pub symbol_range: Range,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Const {
    pub name: String,
    #[serde(rename = "type")]
    pub const_type: Type,
    pub value: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<Annotation>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc: Option<String>,
    pub symbol_range: Range,
    pub full_range: Range,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Method {
    #[serde(default, skip_serializing_if = "BoolExt::is_true")]
    pub oneway: bool,
    pub name: String,
    pub return_type: Type,
    pub args: Vec<Arg>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<Annotation>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub transact_code: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc: Option<String>,
    pub symbol_range: Range,
    pub full_range: Range,
    pub transact_code_range: Range,
    pub oneway_range: Range,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Arg {
    #[serde(default, skip_serializing_if = "Direction::is_unspecified")]
    pub direction: Direction,
    pub name: Option<String>,
    #[serde(rename = "type")]
    pub arg_type: Type,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<Annotation>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc: Option<String>,
    pub symbol_range: Range,
    pub full_range: Range,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Direction {
    In(Range),
    Out(Range),
    InOut(Range),
    Unspecified,
}

impl Direction {
    fn is_unspecified(&self) -> bool {
        matches!(self, Self::Unspecified)
    }
}

impl Default for Direction {
    fn default() -> Self {
        Direction::Unspecified
    }
}

impl fmt::Display for Direction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Direction::In(_) => write!(f, "in"),
            Direction::Out(_) => write!(f, "out"),
            Direction::InOut(_) => write!(f, "inout"),
            Direction::Unspecified => Ok(()),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ParcelableElement {
    Const(Const),
    Field(Field),
}

impl ParcelableElement {
    pub fn as_field(&self) -> Option<&Field> {
        match &self {
            ParcelableElement::Field(f) => Some(f),
            _ => None,
        }
    }

    pub fn get_name(&self) -> &str {
        match self {
            ParcelableElement::Const(c) => &c.name,
            ParcelableElement::Field(f) => &f.name,
        }
    }

    pub fn get_symbol_range(&self) -> &Range {
        match self {
            ParcelableElement::Const(c) => &c.symbol_range,
            ParcelableElement::Field(f) => &f.symbol_range,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Field {
    pub name: String,
    #[serde(rename = "type")]
    pub field_type: Type,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<Annotation>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc: Option<String>,
    pub symbol_range: Range,
    pub full_range: Range,
}

impl Field {
    pub fn get_signature(&self) -> String {
        format!("{} {}", self.field_type.name, self.name,)
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct EnumElement {
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub doc: Option<String>,
    pub symbol_range: Range,
    pub full_range: Range,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Annotation {
    pub name: String,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub key_values: HashMap<String, Option<String>>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum TypeKind {
    Primitive,
    Void,
    Array,
    Map,
    List,
    String,
    CharSequence,
    AndroidType(AndroidTypeKind),
    ResolvedItem(String, ResolvedItemKind),
    Unresolved,
}

/// Android (or Java) built-in types which do not require explicit import
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AndroidTypeKind {
    IBinder,
    FileDescriptor,
    ParcelFileDescriptor,
    ParcelableHolder,
}

impl AndroidTypeKind {
    fn get_all() -> Vec<Self> {
        Vec::from([
            Self::IBinder,
            Self::FileDescriptor,
            Self::ParcelFileDescriptor,
            Self::ParcelableHolder,
        ])
    }

    pub fn from_type_name(name: &str) -> Option<Self> {
        Self::get_all()
            .into_iter()
            .find(|at| at.get_name() == name || (at.can_be_qualified() && at.get_qualified_name() == name))
    }

    pub fn from_name(name: &str) -> Option<Self> {
        Self::get_all()
            .into_iter()
            .find(|at| at.get_name() == name)
    }

    pub fn from_qualified_name(qualified_name: &str) -> Option<Self> {
        Self::get_all()
            .into_iter()
            .find(|at| at.get_qualified_name() == qualified_name)
    }

    pub fn get_name(&self) -> &str {
        match self {
            AndroidTypeKind::IBinder => "IBinder",
            AndroidTypeKind::FileDescriptor => "FileDescriptor",
            AndroidTypeKind::ParcelFileDescriptor => "ParcelFileDescriptor",
            AndroidTypeKind::ParcelableHolder => "ParcelableHolder",
        }
    }

    // If the type can be used with qualified name, e.g. MyMethod(in android.os.IBinder)
    pub fn can_be_qualified(&self) -> bool {
        match self {
            AndroidTypeKind::IBinder => false,
            AndroidTypeKind::FileDescriptor => false,
            AndroidTypeKind::ParcelFileDescriptor => true,
            AndroidTypeKind::ParcelableHolder => false,
        }
    }

    pub fn must_be_imported(&self) -> bool {
        match self {
            AndroidTypeKind::IBinder => true,
            AndroidTypeKind::FileDescriptor => true,
            AndroidTypeKind::ParcelFileDescriptor => true,
            AndroidTypeKind::ParcelableHolder => true,
        }
    }
    
    pub fn get_qualified_name(&self) -> &str {
        match self {
            AndroidTypeKind::IBinder => "android.os.IBinder",
            AndroidTypeKind::FileDescriptor => "java.os.FileDescriptor",
            AndroidTypeKind::ParcelFileDescriptor => "android.os.ParcelFileDescriptor",
            AndroidTypeKind::ParcelableHolder => "android.os.ParcelableHolder",
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Type {
    pub name: String,
    pub kind: TypeKind,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub generic_types: Vec<Type>,
    pub symbol_range: Range,
    pub full_range: Range,
}

impl Type {
    pub fn simple_type<S: Into<String>>(
        name: S,
        kind: TypeKind,
        lookup: &line_col::LineColLookup,
        start: usize,
        end: usize,
    ) -> Self {
        Type {
            name: name.into(),
            kind,
            generic_types: Vec::new(),
            symbol_range: Range::new(lookup, start, end),
            full_range: Range::new(lookup, start, end),
        }
    }

    pub fn array(
        param: Type,
        lookup: &line_col::LineColLookup,
        start: usize,
        end: usize,
        fr_start: usize,
        fr_end: usize,
    ) -> Self {
        Type {
            name: "Array".to_owned(),
            kind: TypeKind::Array,
            generic_types: Vec::from([param]),
            symbol_range: Range::new(lookup, start, end),
            full_range: Range::new(lookup, fr_start, fr_end),
        }
    }

    pub fn list(
        param: Type,
        lookup: &line_col::LineColLookup,
        start: usize,
        end: usize,
        fr_start: usize,
        fr_end: usize,
    ) -> Self {
        Type {
            name: "List".to_owned(),
            kind: TypeKind::List,
            generic_types: Vec::from([param]),
            symbol_range: Range::new(lookup, start, end),
            full_range: Range::new(lookup, fr_start, fr_end),
        }
    }

    pub fn non_generic_list(lookup: &line_col::LineColLookup, start: usize, end: usize) -> Self {
        Type {
            name: "List".to_owned(),
            kind: TypeKind::List,
            generic_types: Vec::new(),
            symbol_range: Range::new(lookup, start, end),
            full_range: Range::new(lookup, start, end),
        }
    }

    pub fn map(
        key_param: Type,
        value_param: Type,
        lookup: &line_col::LineColLookup,
        start: usize,
        end: usize,
        fr_start: usize,
        fr_end: usize,
    ) -> Self {
        Type {
            name: "Map".to_owned(),
            kind: TypeKind::Map,
            generic_types: Vec::from([key_param, value_param]),
            symbol_range: Range::new(lookup, start, end),
            full_range: Range::new(lookup, fr_start, fr_end),
        }
    }

    pub fn non_generic_map(lookup: &line_col::LineColLookup, start: usize, end: usize) -> Self {
        Type {
            name: "Map".to_owned(),
            kind: TypeKind::Map,
            generic_types: Vec::new(),
            symbol_range: Range::new(lookup, start, end),
            full_range: Range::new(lookup, start, end),
        }
    }
}

trait BoolExt {
    fn is_true(&self) -> bool;
}

impl BoolExt for bool {
    fn is_true(&self) -> bool {
        *self
    }
}