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
// SPDX-License-Identifier: GPL-3.0-or-later
use repc_impl::layout::{BuiltinType, FieldLayout, RecordKind, TypeLayout};

pub struct State {
    pub next_id: usize,
}

/// A type declaration.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Span(pub usize, pub usize);

/// A type declaration.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Declaration {
    pub name: String,
    pub span: Span,
    pub ty: DeclarationType,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DeclarationType {
    Type(Type),
    Const(Expr),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Annotation {
    PragmaPack(Box<Expr>),
    AttrPacked,
    Aligned(Option<Box<Expr>>),
}

/// A type.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Type {
    pub id: usize,
    pub lo: usize,
    pub layout: Option<TypeLayout>,
    pub layout_hi: usize,
    pub annotations: Vec<Annotation>,
    pub variant: TypeVariant,
}

/// A struct or union.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Record {
    pub kind: RecordKind,
    pub fields: Vec<RecordField>,
}

/// A struct or union field.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RecordField {
    pub parent_id: usize,
    pub pos: Option<usize>,
    pub lo: usize,
    pub layout: Option<FieldLayout>,
    pub layout_hi: usize,
    pub annotations: Vec<Annotation>,
    pub name: Option<String>,
    pub bit_width: Option<Box<Expr>>,
    pub ty: Type,
}

/// A type without its annotations.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TypeVariant {
    Builtin(BuiltinType),
    Record(Record),
    Typedef(Box<Type>),
    Array(Array),
    Opaque(OpaqueTypeLayout),
    Name(String, Span),
    Enum(Vec<Expr>),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OpaqueTypeLayout {
    pub size_bits: Box<Expr>,
    pub pointer_alignment_bits: Box<Expr>,
    pub field_alignment_bits: Box<Expr>,
    pub required_alignment_bits: Box<Expr>,
}

/// An array.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Array {
    pub element_type: Box<Type>,
    pub num_elements: Option<Box<Expr>>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Expr {
    pub span: Span,
    pub value: Option<i128>,
    pub value_hi: usize,
    pub ty: ExprType,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ExprType {
    Lit(i128),
    Unary(UnaryExprType, Box<Expr>),
    Binary(BinaryExprType, Box<Expr>, Box<Expr>),
    TypeExpr(TypeExprType, Box<Type>),
    Builtin(BuiltinExpr),
    Name(String),
    Offsetof(OffsetofType, Type, Vec<Index>),
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum BuiltinExpr {
    BitsPerByte,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum UnaryExprType {
    Neg,
    Not,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TypeExprType {
    Sizeof,
    SizeofBits,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum BinaryExprType {
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    LogicalAnd,
    LogicalOr,
    Eq,
    NotEq,
    Lt,
    Le,
    Gt,
    Ge,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Index {
    pub span: Span,
    pub ty: IndexType,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum IndexType {
    Field(String),
    Array(Box<Expr>),
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum OffsetofType {
    Bytes,
    Bits,
}