ink_analyzer_ir/
lib.rs

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