Skip to main content

xidl_parser/hir/interface/
mod.rs

1//! Semantic interface, operation, and attribute declarations.
2
3mod convert;
4
5use super::*;
6use serde::{Deserialize, Serialize};
7
8/// An interface declaration with its contract annotations.
9#[derive(Debug, Serialize, Deserialize, Clone)]
10pub struct InterfaceDcl {
11    pub annotations: Vec<Annotation>,
12    pub decl: InterfaceDclInner,
13}
14
15/// A complete interface definition or forward declaration.
16#[derive(Debug, Serialize, Deserialize, Clone)]
17pub enum InterfaceDclInner {
18    InterfaceForwardDcl(InterfaceForwardDcl),
19    InterfaceDef(InterfaceDef),
20}
21
22/// An interface name declared before its definition.
23#[derive(Debug, Serialize, Deserialize, Clone)]
24pub struct InterfaceForwardDcl {
25    pub ident: String,
26}
27
28/// An interface header and its optional body.
29#[derive(Debug, Serialize, Deserialize, Clone)]
30pub struct InterfaceDef {
31    pub header: InterfaceHeader,
32    pub interface_body: Option<InterfaceBody>,
33}
34
35/// An interface name and inherited interfaces.
36#[derive(Debug, Serialize, Deserialize, Clone)]
37pub struct InterfaceHeader {
38    pub ident: String,
39    pub parent: Option<InterfaceInheritanceSpec>,
40}
41
42/// The parent interfaces named by an inheritance clause.
43#[derive(Debug, Serialize, Deserialize, Clone)]
44pub struct InterfaceInheritanceSpec(pub Vec<InterfaceName>);
45
46/// A scoped reference to an interface.
47#[derive(Debug, Serialize, Deserialize, Clone)]
48pub struct InterfaceName(pub ScopedName);
49
50/// Declarations exported by an interface.
51#[derive(Debug, Serialize, Deserialize, Clone)]
52pub struct InterfaceBody(pub Vec<Export>);
53
54/// An operation, attribute, type, constant, or exception in an interface.
55#[derive(Debug, Serialize, Deserialize, Clone)]
56pub enum Export {
57    OpDcl(OpDcl),
58    AttrDcl(AttrDcl),
59    TypeDcl(TypeDcl),
60    ConstDcl(ConstDcl),
61    ExceptDcl(ExceptDcl),
62}
63
64/// An operation signature with annotations, parameters, and exceptions.
65#[derive(Debug, Serialize, Deserialize, Clone)]
66pub struct OpDcl {
67    pub annotations: Vec<Annotation>,
68    pub ty: OpTypeSpec,
69    pub ident: String,
70    pub parameter: Option<ParameterDcls>,
71    pub raises: Option<RaisesExpr>,
72}
73
74/// An operation return type, including void.
75#[derive(Debug, Serialize, Deserialize, Clone)]
76#[allow(clippy::large_enum_variant)]
77pub enum OpTypeSpec {
78    Void,
79    TypeSpec(TypeSpec),
80}
81
82/// The ordered parameters of an operation.
83#[derive(Debug, Serialize, Deserialize, Clone)]
84pub struct ParameterDcls(pub Vec<ParamDcl>);
85
86/// An operation parameter and its binding annotations.
87#[derive(Debug, Serialize, Deserialize, Clone)]
88pub struct ParamDcl {
89    pub annotations: Vec<Annotation>,
90    pub attr: Option<ParamAttribute>,
91    pub ty: TypeSpec,
92    pub declarator: SimpleDeclarator,
93}
94
95/// A parameter direction such as in, out, or inout.
96#[derive(Debug, Serialize, Deserialize, Clone)]
97pub struct ParamAttribute(pub String);
98
99/// The ordered exceptions declared by an operation or getter.
100#[derive(Debug, Serialize, Deserialize, Clone)]
101pub struct RaisesExpr(pub Vec<ScopedName>);
102
103/// An interface attribute with contract annotations.
104#[derive(Debug, Serialize, Deserialize, Clone)]
105pub struct AttrDcl {
106    pub annotations: Vec<Annotation>,
107    pub decl: AttrDclInner,
108}
109
110/// A readonly or writable attribute specification.
111#[derive(Debug, Serialize, Deserialize, Clone)]
112pub enum AttrDclInner {
113    ReadonlyAttrSpec(ReadonlyAttrSpec),
114    AttrSpec(AttrSpec),
115}
116
117/// The type and names of a readonly attribute declaration.
118#[derive(Debug, Serialize, Deserialize, Clone)]
119pub struct ReadonlyAttrSpec {
120    pub ty: TypeSpec,
121    pub declarator: ReadonlyAttrDeclarator,
122}
123
124/// Readonly attribute names and the raises clause belonging to one name.
125#[derive(Debug, Serialize, Deserialize, Clone)]
126pub enum ReadonlyAttrDeclarator {
127    SimpleDeclarator(Vec<SimpleDeclarator>),
128    WithRaises {
129        declarator: SimpleDeclarator,
130        raises: RaisesExpr,
131    },
132}
133
134impl ReadonlyAttrDeclarator {
135    /// All attribute names, including the name with a raises clause.
136    pub fn declarators(&self) -> &[SimpleDeclarator] {
137        match self {
138            Self::SimpleDeclarator(names) => names,
139            Self::WithRaises { declarator, .. } => std::slice::from_ref(declarator),
140        }
141    }
142
143    /// Exceptions declared for the attribute getter.
144    pub fn raises(&self) -> Option<&RaisesExpr> {
145        match self {
146            Self::SimpleDeclarator(_) => None,
147            Self::WithRaises { raises, .. } => Some(raises),
148        }
149    }
150
151    /// Exceptions to rewrite when projecting the getter into a version.
152    pub fn raises_mut(&mut self) -> Option<&mut RaisesExpr> {
153        match self {
154            Self::SimpleDeclarator(_) => None,
155            Self::WithRaises { raises, .. } => Some(raises),
156        }
157    }
158}
159
160/// The type, names, and exception clauses of writable attributes.
161#[derive(Debug, Serialize, Deserialize, Clone)]
162pub struct AttrSpec {
163    pub ty: TypeSpec,
164    pub declarator: AttrDeclarator,
165}
166
167/// Writable attribute names and any getter or setter exceptions.
168#[derive(Debug, Serialize, Deserialize, Clone)]
169pub enum AttrDeclarator {
170    SimpleDeclarator(Vec<SimpleDeclarator>),
171    WithRaises {
172        declarator: SimpleDeclarator,
173        raises: AttrRaisesExpr,
174    },
175}
176
177/// Getter and setter exception clauses for one attribute.
178#[derive(Debug, Serialize, Deserialize, Clone)]
179pub enum AttrRaisesExpr {
180    Case1(GetExcepExpr, Option<SetExcepExpr>),
181    SetExcepExpr(SetExcepExpr),
182}
183
184/// The exception list of an attribute getter.
185#[derive(Debug, Serialize, Deserialize, Clone)]
186pub struct GetExcepExpr {
187    pub expr: ExceptionList,
188}
189
190/// The exception list of an attribute setter.
191#[derive(Debug, Serialize, Deserialize, Clone)]
192pub struct SetExcepExpr {
193    pub expr: ExceptionList,
194}
195
196/// The ordered scoped exception names in an attribute clause.
197#[derive(Debug, Serialize, Deserialize, Clone)]
198pub struct ExceptionList(pub Vec<ScopedName>);