parser-c 0.3.0

Macros for parser-c.
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
// Original file: "SemRep.hs"
// File auto-generated using Corollary.

#[macro_use]
use corollary_support::*;

// NOTE: These imports are advisory. You probably need to change them to support Rust.
// use Language::C::Data;
// use Language::C::Syntax;
// use Data::Map;
// use Map;
// use Data::Map;
// use Data::Maybe;
// use Data::Generics;

use data::ident::*;
use data::node::*;
use syntax::ast::*;

#[derive(Clone, Debug)]
pub enum TagDef {
    CompDef(CompType),
    EnumDef(EnumType),
}
pub use self::TagDef::*;

pub fn typeOfTagDef(_0: TagDef) -> TypeName {
    match (_0) {
        CompDef(comptype) => typeOfCompDef(comptype),
        EnumDef(enumtype) => typeOfEnumDef(enumtype),
    }
}

pub fn declOfDef(def: n) -> Decl {
    {
        let vd = getVarDecl(def);

        Decl(vd, (nodeInfo(def)))
    }
}

pub fn declIdent() -> Ident {
    identOfVarName(declName)
}

pub fn declName() -> VarName {
    (|VarDecl(n, _, _)| {
         n
     }(getVarDecl))
}

pub fn declType() -> Type {
    (|VarDecl(_, _, ty)| {
         ty
     }(getVarDecl))
}

pub fn declAttrs() -> DeclAttrs {
    (|VarDecl(_, specs, _)| {
         specs
     }(getVarDecl))
}

#[derive(Clone, Debug)]
pub enum IdentDecl {
    Declaration(Decl),
    ObjectDef(ObjDef),
    FunctionDef(FunDef),
    EnumeratorDef(Enumerator),
}
pub use self::IdentDecl::*;

pub fn objKindDescr(_0: IdentDecl) -> String {
    match (_0) {
        Declaration(_) => "declaration".to_string(),
        ObjectDef(_) => "object definition".to_string(),
        FunctionDef(_) => "function definition".to_string(),
        EnumeratorDef(_) => "enumerator definition".to_string(),
    }
}

pub fn splitIdentDecls
    (include_all: bool)
     -> (Map<Ident, Decl>, (Map<Ident, Enumerator>, Map<Ident, ObjDef>, Map<Ident, FunDef>)) {

    let deal = |ident, entry, (decls, defs)| {
        (Map::insert(ident, (declOfDef(entry)), decls), addDef(ident, entry, defs))
    };

    let deal_q = |_0, _1, _2| match (_0, _1, _2) {
        (ident, Declaration(d), (decls, defs)) => (Map::insert(ident, d, decls), defs),
        (ident, def, (decls, defs)) => (decls, addDef(ident, def, defs)),
    };

    let addDef = |ident, entry, (es, os, fs)| match entry {
        Declaration(_) => (es, os, fs),
        EnumeratorDef(e) => (Map::insert(ident, e, es), os, fs),
        ObjectDef(o) => (es, Map::insert(ident, o, os), fs),
        FunctionDef(f) => (es, os, Map::insert(ident, f, fs)),
    };

    Map::foldWithKey((if include_all { deal } else { deal_q }),
                     (Map::empty, (Map::empty, Map::empty, Map::empty)))
}

pub struct GlobalDecls {
    gObjs: Map<Ident, IdentDecl>,
    gTags: Map<SUERef, TagDef>,
    gTypeDefs: Map<Ident, TypeDef>,
}
fn gObjs(a: GlobalDecls) -> Map<Ident, IdentDecl> {
    a.gObjs
}
fn gTags(a: GlobalDecls) -> Map<SUERef, TagDef> {
    a.gTags
}
fn gTypeDefs(a: GlobalDecls) -> Map<Ident, TypeDef> {
    a.gTypeDefs
}

pub fn emptyGlobalDecls() -> GlobalDecls {
    GlobalDecls(Map::empty, Map::empty, Map::empty)
}

pub fn filterGlobalDecls(decl_filter: fn(DeclEvent) -> bool, gmap: GlobalDecls) -> GlobalDecls {
    GlobalDecls {
        gObjs: Map::filter((decl_filter(DeclEvent)), (gObjs(gmap))),
        gTags: Map::filter((decl_filter(TagEvent)), (gTags(gmap))),
        gTypeDefs: Map::filter((decl_filter(TypeDefEvent)), (gTypeDefs(gmap))),
    }
}

pub fn mergeGlobalDecls(gmap1: GlobalDecls, gmap2: GlobalDecls) -> GlobalDecls {
    GlobalDecls {
        gObjs: Map::union((gObjs(gmap1)), (gObjs(gmap2))),
        gTags: Map::union((gTags(gmap1)), (gTags(gmap2))),
        gTypeDefs: Map::union((gTypeDefs(gmap1)), (gTypeDefs(gmap2))),
    }
}

pub enum DeclEvent {
    TagEvent(TagDef),
    DeclEvent(IdentDecl),
    ParamEvent(ParamDecl),
    LocalEvent(IdentDecl),
    TypeDefEvent(TypeDef),
    AsmEvent(AsmBlock),
}
pub use self::DeclEvent::*;

#[derive(Clone, Debug)]
pub struct Decl(pub VarDecl, pub NodeInfo);


#[derive(Clone, Debug)]
pub struct ObjDef(pub VarDecl, pub Option<Initializer>, pub NodeInfo);


#[derive(Clone, Debug)]
pub struct FunDef(pub VarDecl, pub Stmt, pub NodeInfo);


#[derive(Clone, Debug)]
pub enum ParamDecl {
    ParamDecl(VarDecl, NodeInfo),
    AbstractParamDecl(VarDecl, NodeInfo),
}
pub use self::ParamDecl::*;

#[derive(Clone, Debug)]
pub enum MemberDecl {
    MemberDecl(VarDecl, Option<Expr>, NodeInfo),
    AnonBitField(Type, Expr, NodeInfo),
}
pub use self::MemberDecl::*;

#[derive(Clone, Debug)]
pub struct TypeDef(pub Ident, pub Type, pub Attributes, pub NodeInfo);


pub fn identOfTypeDef(TypeDef(ide, _, _, _): TypeDef) -> Ident {
    ide
}

#[derive(Clone, Debug)]
pub struct VarDecl();


pub fn isExtDecl() -> bool {
    hasLinkage(declStorage)
}

#[derive(Clone, Debug)]
pub struct DeclAttrs(pub FunctionAttrs, pub Storage, pub Attributes);


pub fn declStorage(d: d) -> Storage {
    match declAttrs(d) {
        DeclAttrs(_, st, _) => st,
    }
}

pub fn functionAttrs(d: d) -> FunctionAttrs {
    match declAttrs(d) {
        DeclAttrs(fa, _, _) => fa,
    }
}

#[derive(Clone, Debug, Eq, Ord)]
pub struct FunctionAttrs {
    isInline: bool,
    isNoreturn: bool,
}
fn isInline(a: FunctionAttrs) -> bool {
    a.isInline
}
fn isNoreturn(a: FunctionAttrs) -> bool {
    a.isNoreturn
}

pub fn noFunctionAttrs() -> FunctionAttrs {
    FunctionAttrs {
        isInline: false,
        isNoreturn: false,
    }
}

#[derive(Clone, Debug, Eq, Ord)]
pub enum Storage {
    NoStorage,
    Auto(Register),
    Static(Linkage, ThreadLocal),
    FunLinkage(Linkage),
}
pub use self::Storage::*;

pub type ThreadLocal = bool;

pub type Register = bool;

#[derive(Clone, Debug, Eq, Ord)]
pub enum Linkage {
    NoLinkage,
    InternalLinkage,
    ExternalLinkage,
}
pub use self::Linkage::*;

pub fn hasLinkage(_0: Storage) -> bool {
    match (_0) {
        Auto(_) => false,
        Static(NoLinkage, _) => false,
        _ => true,
    }
}

pub fn declLinkage(decl: d) -> Linkage {
    match declStorage(decl) {
        NoStorage => undefined,
        Auto(_) => NoLinkage,
        Static(linkage, _) => linkage,
        FunLinkage(linkage) => linkage,
    }
}

#[derive(Clone, Debug)]
pub enum Type {
    DirectType(TypeName, TypeQuals, Attributes),
    PtrType(Type, TypeQuals, Attributes),
    ArrayType(Type, ArraySize, TypeQuals, Attributes),
    FunctionType(FunType, Attributes),
    TypeDefType(TypeDefRef, TypeQuals, Attributes),
}
pub use self::Type::*;

#[derive(Clone, Debug)]
pub enum FunType {
    FunType(Type, Vec<ParamDecl>, bool),
    FunTypeIncomplete(Type),
}
pub use self::FunType::*;

#[derive(Clone, Debug)]
pub enum ArraySize {
    UnknownArraySize(bool),
    ArraySize(bool, Expr),
}
pub use self::ArraySize::*;

#[derive(Clone, Debug)]
pub enum TypeName {
    TyVoid,
    TyIntegral(IntType),
    TyFloating(FloatType),
    TyComplex(FloatType),
    TyComp(CompTypeRef),
    TyEnum(EnumTypeRef),
    TyBuiltin(BuiltinType),
}
pub use self::TypeName::*;

#[derive(Clone, Debug)]
pub enum BuiltinType {
    TyVaList,
    TyAny,
}
pub use self::BuiltinType::*;

#[derive(Clone, Debug)]
pub struct TypeDefRef(pub Ident, pub Type, pub NodeInfo);


#[derive(Clone, Debug, Eq, Ord)]
pub enum IntType {
    TyBool,
    TyChar,
    TySChar,
    TyUChar,
    TyShort,
    TyUShort,
    TyInt,
    TyUInt,
    TyInt128,
    TyUInt128,
    TyLong,
    TyULong,
    TyLLong,
    TyULLong,
}
pub use self::IntType::*;

#[derive(Clone, Debug, Eq, Ord)]
pub enum FloatType {
    TyFloat,
    TyDouble,
    TyLDouble,
}
pub use self::FloatType::*;

#[derive(Clone, Debug)]
pub struct CompTypeRef(pub SUERef, pub CompTyKind, pub NodeInfo);


#[derive(Clone, Debug)]
pub struct EnumTypeRef(pub SUERef, pub NodeInfo);


#[derive(Clone, Debug)]
pub struct CompType(pub SUERef, pub CompTyKind, pub Vec<MemberDecl>, pub Attributes, pub NodeInfo);


pub fn typeOfCompDef(CompType(__ref, tag, _, _, _): CompType) -> TypeName {
    TyComp((CompTypeRef(__ref, tag, undefNode)))
}

#[derive(Clone, Debug, Eq, Ord)]
pub enum CompTyKind {
    StructTag,
    UnionTag,
}
pub use self::CompTyKind::*;

#[derive(Clone, Debug)]
pub struct EnumType(pub SUERef, pub Vec<Enumerator>, pub Attributes, pub NodeInfo);


pub fn typeOfEnumDef(EnumType(__ref, _, _, _): EnumType) -> TypeName {
    TyEnum((EnumTypeRef(__ref, undefNode)))
}

#[derive(Clone, Debug)]
pub struct Enumerator(pub Ident, pub Expr, pub EnumType, pub NodeInfo);


#[derive(Clone, Debug)]
pub struct TypeQuals {
    constant: bool,
    volatile: bool,
    restrict: bool,
    atomic: bool,
    nullable: bool,
    nonnull: bool,
}
fn constant(a: TypeQuals) -> bool {
    a.constant
}
fn volatile(a: TypeQuals) -> bool {
    a.volatile
}
fn restrict(a: TypeQuals) -> bool {
    a.restrict
}
fn atomic(a: TypeQuals) -> bool {
    a.atomic
}
fn nullable(a: TypeQuals) -> bool {
    a.nullable
}
fn nonnull(a: TypeQuals) -> bool {
    a.nonnull
}

pub fn noTypeQuals() -> TypeQuals {
    TypeQuals(false, false, false, false, false, false)
}

pub fn mergeTypeQuals(TypeQuals(c1, v1, r1, a1, n1, nn1): TypeQuals,
                      TypeQuals(c2, v2, r2, a2, n2, nn2): TypeQuals)
                      -> TypeQuals {
    TypeQuals(((c1 && c2)),
              ((v1 && v2)),
              ((r1 && r2)),
              ((a1 && a2)),
              ((n1 && n2)),
              ((nn1 && nn2)))
}

pub type Initializer = CInit;

#[derive(Clone, Debug)]
pub enum VarName {
    VarName(Ident, Option<AsmName>),
    NoName,
}
pub use self::VarName::*;

pub fn identOfVarName(_0: VarName) -> Ident {
    match (_0) {
        NoName => panic!("identOfVarName: NoName"),
        VarName(ident, _) => ident,
    }
}

pub fn isNoName(_0: VarName) -> bool {
    match (_0) {
        NoName => true,
        _ => false,
    }
}

pub type AsmBlock = CStrLit;

pub type AsmName = CStrLit;

#[derive(Clone, Debug)]
pub struct Attr(pub Ident, pub Vec<Expr>, pub NodeInfo);


pub type Attributes = Vec<Attr>;

pub fn noAttributes() -> Attributes {
    vec![]
}

pub fn mergeAttributes() -> Attributes {
    (__op_addadd)
}

pub type Stmt = CStat;

pub type Expr = CExpr;