Skip to main content

oak_fortran/ast/
specification_nodes.rs

1use core::range::Range;
2use std::{boxed::Box, string::String, vec::Vec};
3
4use super::{Dimension, ExprNode, ProgramUnitKind};
5
6/// Specification statement.
7#[derive(Debug, Clone, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum SpecificationStmt {
10    /// Type declaration.
11    TypeDeclaration(TypeDeclarationNode),
12    /// Parameter declaration.
13    Parameter(ParameterNode),
14    /// Implicit declaration.
15    Implicit(ImplicitNode),
16    /// Use statement.
17    Use(UseNode),
18    /// Import statement.
19    Import(ImportNode),
20    /// Interface declaration.
21    Interface(InterfaceNode),
22    /// Procedure declaration.
23    Procedure(ProcedureNode),
24    /// Generic declaration.
25    Generic(GenericNode),
26}
27
28/// Type specification.
29#[derive(Debug, Clone, PartialEq)]
30#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
31pub enum TypeSpec {
32    /// Integer type.
33    Integer(Option<KindSelector>),
34    /// Real type.
35    Real(Option<KindSelector>),
36    /// Double precision type.
37    DoublePrecision,
38    /// Complex type.
39    Complex(Option<KindSelector>),
40    /// Character type.
41    Character(Option<CharacterSelector>),
42    /// Logical type.
43    Logical(Option<KindSelector>),
44    /// Derived type.
45    Derived(String),
46    /// Class type.
47    Class(String),
48    /// Type star.
49    TypeStar,
50}
51
52/// Kind selector.
53#[derive(Debug, Clone, PartialEq)]
54#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
55pub enum KindSelector {
56    /// Expression.
57    Expression(Box<ExprNode>),
58}
59
60/// Character selector.
61#[derive(Debug, Clone, PartialEq)]
62#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
63pub enum CharacterSelector {
64    /// Length.
65    Length(Box<ExprNode>),
66    /// Length and kind.
67    LengthAndKind(Box<ExprNode>, Box<ExprNode>),
68}
69
70/// Type declaration node.
71#[derive(Debug, Clone, PartialEq)]
72#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
73pub struct TypeDeclarationNode {
74    /// The type specification for the declared entities.
75    pub type_spec: TypeSpec,
76    /// The attributes applied to the declared entities.
77    pub attributes: Vec<Attribute>,
78    /// The list of entity declarations.
79    pub entities: Vec<EntityDecl>,
80    /// The byte range of this node in the source text.
81    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
82    pub span: Range<usize>,
83}
84
85/// Attribute.
86#[derive(Debug, Clone, PartialEq)]
87#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
88pub enum Attribute {
89    /// Allocatable.
90    Allocatable,
91    /// Asynchronous.
92    Asynchronous,
93    /// Bind.
94    Bind(String),
95    /// Dimension.
96    Dimension(Vec<Dimension>),
97    /// External.
98    External,
99    /// Intent.
100    Intent(Intent),
101    /// Intrinsic.
102    Intrinsic,
103    /// Optional.
104    Optional,
105    /// Parameter.
106    Parameter,
107    /// Pointer.
108    Pointer,
109    /// Protected.
110    Protected,
111    /// Private.
112    Private,
113    /// Public.
114    Public,
115    /// Save.
116    Save,
117    /// Target.
118    Target,
119    /// Value.
120    Value,
121    /// Volatile.
122    Volatile,
123}
124
125/// Intent.
126#[derive(Debug, Clone, PartialEq)]
127#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
128pub enum Intent {
129    /// In.
130    In,
131    /// Out.
132    Out,
133    /// InOut.
134    InOut,
135}
136
137/// Entity declaration.
138#[derive(Debug, Clone, PartialEq)]
139#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
140pub struct EntityDecl {
141    /// The name of the entity.
142    pub name: String,
143    /// The optional array specification.
144    pub array_spec: Option<Vec<Dimension>>,
145    /// The optional character length specification.
146    pub char_length: Option<Box<ExprNode>>,
147    /// The optional initialization expression.
148    pub initialization: Option<Box<ExprNode>>,
149    /// The byte range of this node in the source text.
150    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
151    pub span: Range<usize>,
152}
153
154/// Parameter node.
155#[derive(Debug, Clone, PartialEq)]
156#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
157pub struct ParameterNode {
158    /// The list of parameter entity declarations.
159    pub entities: Vec<EntityDecl>,
160    /// The byte range of this node in the source text.
161    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
162    pub span: Range<usize>,
163}
164
165/// Implicit node.
166#[derive(Debug, Clone, PartialEq)]
167#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
168pub enum ImplicitNode {
169    /// None.
170    None,
171    /// Spec.
172    Spec(Vec<ImplicitSpec>),
173}
174
175/// Implicit specification.
176#[derive(Debug, Clone, PartialEq)]
177#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
178pub struct ImplicitSpec {
179    /// The type specification for the implicit declaration.
180    pub type_spec: TypeSpec,
181    /// The letter ranges for the implicit declaration.
182    pub letter_ranges: Vec<LetterRange>,
183}
184
185/// Letter range.
186#[derive(Debug, Clone, PartialEq)]
187#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
188pub struct LetterRange {
189    /// The starting letter of the range.
190    pub start: char,
191    /// The optional ending letter of the range.
192    pub end: Option<char>,
193}
194
195/// Use node.
196#[derive(Debug, Clone, PartialEq)]
197#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
198pub struct UseNode {
199    /// The name of the module to use.
200    pub module_name: String,
201    /// The optional module nature (intrinsic or non-intrinsic).
202    pub nature: Option<ModuleNature>,
203    /// The list of rename specifications.
204    pub rename_list: Vec<Rename>,
205    /// The list of only specifications.
206    pub only_list: Vec<Only>,
207    /// The byte range of this node in the source text.
208    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
209    pub span: Range<usize>,
210}
211
212/// Module nature.
213#[derive(Debug, Clone, PartialEq)]
214#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
215pub enum ModuleNature {
216    /// Intrinsic.
217    Intrinsic,
218    /// Non-intrinsic.
219    NonIntrinsic,
220}
221
222/// Rename specification.
223#[derive(Debug, Clone, PartialEq)]
224#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
225pub struct Rename {
226    /// The local name to use.
227    pub local_name: String,
228    /// The original name in the module.
229    pub use_name: String,
230}
231
232/// Only specification.
233#[derive(Debug, Clone, PartialEq)]
234#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
235pub enum Only {
236    /// Generic.
237    Generic(String),
238    /// Rename.
239    Rename(Rename),
240}
241
242/// Import node.
243#[derive(Debug, Clone, PartialEq)]
244#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
245pub struct ImportNode {
246    /// The list of names to import.
247    pub import_names: Vec<String>,
248    /// The byte range of this node in the source text.
249    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
250    pub span: Range<usize>,
251}
252
253/// Interface node.
254#[derive(Debug, Clone, PartialEq)]
255#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
256pub struct InterfaceNode {
257    /// The optional generic specification for the interface.
258    pub generic_spec: Option<GenericSpec>,
259    /// The interface bodies (procedure interfaces).
260    pub interface_bodies: Vec<ProgramUnitKind>,
261    /// The byte range of this node in the source text.
262    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
263    pub span: Range<usize>,
264}
265
266/// Generic specification.
267#[derive(Debug, Clone, PartialEq)]
268#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
269pub enum GenericSpec {
270    /// Generic name.
271    GenericName(String),
272    /// Operator.
273    Operator(String),
274    /// Assignment.
275    Assignment,
276    /// Read defined.
277    ReadDefined,
278    /// Write defined.
279    WriteDefined,
280}
281
282/// Procedure node.
283#[derive(Debug, Clone, PartialEq)]
284#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
285pub struct ProcedureNode {
286    /// The optional interface name for the procedure.
287    pub interface_name: Option<String>,
288    /// The attributes applied to the procedure.
289    pub attributes: Vec<Attribute>,
290    /// The procedure entity declarations.
291    pub entities: Vec<ProcedureEntity>,
292    /// The byte range of this node in the source text.
293    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
294    pub span: Range<usize>,
295}
296
297/// Procedure entity.
298#[derive(Debug, Clone, PartialEq)]
299#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
300pub struct ProcedureEntity {
301    /// The name of the procedure.
302    pub name: String,
303    /// The optional binding name.
304    pub binding_name: Option<String>,
305}
306
307/// Generic node.
308#[derive(Debug, Clone, PartialEq)]
309#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
310pub struct GenericNode {
311    /// The generic specification.
312    pub generic_spec: GenericSpec,
313    /// The optional access specification.
314    pub access_spec: Option<Attribute>,
315    /// The list of procedure names bound to this generic.
316    pub procedure_names: Vec<String>,
317    /// The byte range of this node in the source text.
318    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
319    pub span: Range<usize>,
320}