ink_analyzer_ir/
lib.rs

1//! [ink!] intermediate representations (IRs) and abstractions for [ink! analyzer].
2//!
3//! [ink!]: https://use.ink/
4//! [ink! analyzer]: https://docs.rs/ink-analyzer/latest/ink_analyzer/
5//!
6//! # Example
7//! Generate an IR of ink! smart contract code.
8//!
9//! ```
10//! use ink_analyzer_ir::InkFile;
11//!
12//! fn generate_ir() {
13//!         let file = InkFile::parse(r#"
14//!             #[ink::contract]
15//!             mod my_contract {
16//!
17//!                 #[ink(storage)]
18//!                 pub struct MyContract {
19//!                     value: bool,
20//!                 }
21//!
22//!                 // --snip--
23//!             }
24//!         "#);
25//!         dbg!(&file);
26//!
27//!         let contracts = file.contracts();
28//!         dbg!(&contracts);
29//!
30//!         if let Some(contract) = contracts.first() {
31//!             let storage = contract.storage();
32//!             dbg!(&storage);
33//!         }
34//!     }
35//! ```
36
37#[macro_use]
38mod macros;
39
40mod attrs;
41mod chain_extension;
42mod constructor;
43mod contract;
44mod contract_ref;
45mod error;
46mod event;
47mod event_v2;
48mod extension;
49mod file;
50mod function;
51mod ink_e2e_test;
52mod ink_impl;
53mod ink_test;
54mod message;
55mod scale_derive;
56mod storage;
57mod storage_item;
58mod topic;
59mod trait_definition;
60
61mod environment;
62mod selector;
63
64mod iter;
65mod traits;
66mod tree;
67
68mod test_utils;
69
70pub use self::{
71    attrs::{
72        meta, InkArg, InkArgKind, InkArgValueKind, InkArgValuePathKind, InkArgValueStringKind,
73        InkAttribute, InkAttributeKind, InkMacroKind,
74    },
75    chain_extension::ChainExtension,
76    constructor::Constructor,
77    contract::Contract,
78    contract_ref::ContractRef,
79    environment::{EnvArg, Environment},
80    error::Error,
81    event::Event,
82    event_v2::EventV2,
83    extension::Extension,
84    file::InkFile,
85    function::Function,
86    ink_e2e_test::InkE2ETest,
87    ink_impl::InkImpl,
88    ink_test::InkTest,
89    message::Message,
90    scale_derive::ScaleDerive,
91    selector::{Selector, SelectorArg, SelectorArgKind},
92    storage::Storage,
93    storage_item::StorageItem,
94    topic::Topic,
95    trait_definition::TraitDefinition,
96    traits::{
97        HasInkEnvironment, HasInkImplParent, InkEntity, IsChainExtensionFn, IsInkCallable,
98        IsInkEvent, IsInkFn, IsInkStruct, IsInkTrait, IsIntId, IsSyntax,
99    },
100    tree::ast_ext::{
101        closest_ancestor_ast_type, closest_item_which, closest_non_trivia_token, parent_ast_item,
102        path_from_str, path_from_type, path_to_string, resolve_current_module, resolve_item,
103        resolve_qualifier, simple_use_paths_and_aliases_in_scope,
104    },
105    tree::utils::{
106        attrs, ink_ancestors, ink_arg_by_kind, ink_args, ink_args_by_kind, ink_attr_to_entity,
107        ink_attrs, ink_attrs_ancestors, ink_attrs_closest_ancestors, ink_attrs_closest_descendants,
108        ink_attrs_descendants, ink_attrs_in_scope, ink_callable_closest_descendants,
109        ink_closest_ancestors, ink_closest_descendants, ink_descendants,
110        ink_impl_closest_descendants, ink_parent, ink_peekable_quasi_closest_descendants,
111    },
112    tree::{InkTree, ItemAtOffset},
113};
114
115/// Re-export `ra_ap_syntax` as syntax.
116pub use ra_ap_syntax as syntax;
117
118/// Re-export `ra_ap_syntax::ast` as `ast`.
119pub use ra_ap_syntax::ast;
120
121/// ink! language version.
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123#[non_exhaustive]
124pub enum Version {
125    /// version <= 4.x.x
126    /// NOTE: We only actually support v4
127    Legacy,
128    /// version == 5.x.x
129    V5(MinorVersion),
130    /// version == 6.x.x
131    V6,
132}
133
134/// ink! language minor version.
135#[derive(Debug, Clone, Copy, PartialEq, Eq)]
136#[non_exhaustive]
137pub enum MinorVersion {
138    /// Latest minor version.
139    Latest,
140    /// Base minor version (e.g. 5.0.x).
141    Base,
142}
143
144impl Version {
145    /// Returns true if `version <= 4.x.x`
146    pub fn is_legacy(&self) -> bool {
147        *self == Version::Legacy
148    }
149
150    /// Returns true if `version == 5.x.x`
151    pub fn is_v5(&self) -> bool {
152        matches!(self, Version::V5(..))
153    }
154
155    /// Returns true if `version == 6.x.x`
156    pub fn is_v6(&self) -> bool {
157        *self == Version::V6
158    }
159
160    /// Returns true if `version <= 5`
161    pub fn is_lte_v5(&self) -> bool {
162        matches!(self, Version::Legacy | Version::V5(..))
163    }
164
165    /// Returns true if `version >= 5`
166    pub fn is_gte_v5(&self) -> bool {
167        matches!(self, Version::V5(..) | Version::V6)
168    }
169
170    /// Returns true if `version == 5.0.x`
171    pub fn is_v5_0(&self) -> bool {
172        *self == Version::V5(MinorVersion::Base)
173    }
174
175    /// Returns true if `version >= 5.1`
176    pub fn is_gte_v5_1(&self) -> bool {
177        matches!(self, Version::V5(MinorVersion::Latest) | Version::V6)
178    }
179
180    /// Returns true if `version >= 6`
181    pub fn is_gte_v6(&self) -> bool {
182        self.is_v6()
183    }
184}