Skip to main content

solidity_language_server/solc_ast/
enums.rs

1//! Enum types used across the Solidity AST.
2//!
3//! These mirror the enums defined in the official solc source at
4//! `libsolidity/ast/ASTEnums.h` and serialized by `ASTJsonExporter.cpp`.
5
6use std::fmt;
7
8use serde::{Deserialize, Serialize};
9
10// ── Contract ───────────────────────────────────────────────────────────────
11
12/// The kind of a contract declaration.
13///
14/// Matches `ContractKind` in `ASTEnums.h`.
15#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
16#[serde(rename_all = "lowercase")]
17pub enum ContractKind {
18    Contract,
19    Interface,
20    Library,
21}
22
23impl fmt::Display for ContractKind {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::Contract => write!(f, "contract"),
27            Self::Interface => write!(f, "interface"),
28            Self::Library => write!(f, "library"),
29        }
30    }
31}
32
33// ── Visibility ─────────────────────────────────────────────────────────────
34
35/// Visibility of a declaration.
36///
37/// Matches `Visibility` in `ASTEnums.h`, serialized as lowercase strings.
38#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
39#[serde(rename_all = "lowercase")]
40pub enum Visibility {
41    Default,
42    Private,
43    Internal,
44    Public,
45    External,
46}
47
48impl fmt::Display for Visibility {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            Self::Default => write!(f, ""),
52            Self::Private => write!(f, "private"),
53            Self::Internal => write!(f, "internal"),
54            Self::Public => write!(f, "public"),
55            Self::External => write!(f, "external"),
56        }
57    }
58}
59
60// ── State mutability ───────────────────────────────────────────────────────
61
62/// How a function or variable can mutate EVM state.
63///
64/// Matches `StateMutability` in `ASTEnums.h`.
65#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
66#[serde(rename_all = "lowercase")]
67pub enum StateMutability {
68    Pure,
69    View,
70    Nonpayable,
71    Payable,
72}
73
74impl fmt::Display for StateMutability {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        match self {
77            Self::Pure => write!(f, "pure"),
78            Self::View => write!(f, "view"),
79            Self::Nonpayable => write!(f, "nonpayable"),
80            Self::Payable => write!(f, "payable"),
81        }
82    }
83}
84
85// ── Variable mutability ────────────────────────────────────────────────────
86
87/// Mutability of a variable declaration.
88///
89/// Serialized by `ASTJsonExporter::visit(VariableDeclaration)`.
90#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
91#[serde(rename_all = "lowercase")]
92pub enum Mutability {
93    Mutable,
94    Immutable,
95    Constant,
96    /// Transient storage (EIP-1153, solc 0.8.28+).
97    Transient,
98}
99
100impl fmt::Display for Mutability {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        match self {
103            Self::Mutable => write!(f, "mutable"),
104            Self::Immutable => write!(f, "immutable"),
105            Self::Constant => write!(f, "constant"),
106            Self::Transient => write!(f, "transient"),
107        }
108    }
109}
110
111// ── Storage location ───────────────────────────────────────────────────────
112
113/// Storage location of a variable.
114///
115/// `"default"` means the compiler decides (state variables, value types).
116#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
117#[serde(rename_all = "lowercase")]
118pub enum StorageLocation {
119    Default,
120    Storage,
121    Memory,
122    Calldata,
123    /// Transient storage (EIP-1153, solc 0.8.28+).
124    Transient,
125}
126
127impl fmt::Display for StorageLocation {
128    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129        match self {
130            Self::Default => write!(f, "default"),
131            Self::Storage => write!(f, "storage"),
132            Self::Memory => write!(f, "memory"),
133            Self::Calldata => write!(f, "calldata"),
134            Self::Transient => write!(f, "transient"),
135        }
136    }
137}
138
139// ── Function kind ──────────────────────────────────────────────────────────
140
141/// The kind of a function definition.
142///
143/// Note: `freeFunction` is serialized with camelCase by `ASTJsonExporter`.
144#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
145#[serde(rename_all = "camelCase")]
146pub enum FunctionKind {
147    Function,
148    Receive,
149    Constructor,
150    Fallback,
151    FreeFunction,
152}
153
154impl fmt::Display for FunctionKind {
155    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156        match self {
157            Self::Function => write!(f, "function"),
158            Self::Receive => write!(f, "receive"),
159            Self::Constructor => write!(f, "constructor"),
160            Self::Fallback => write!(f, "fallback"),
161            Self::FreeFunction => write!(f, "function"),
162        }
163    }
164}
165
166// ── Function call kind ─────────────────────────────────────────────────────
167
168/// The kind of a function call expression.
169#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
170#[serde(rename_all = "camelCase")]
171pub enum FunctionCallKind {
172    FunctionCall,
173    TypeConversion,
174    StructConstructorCall,
175}
176
177// ── Literal kind ───────────────────────────────────────────────────────────
178
179/// The kind of a literal value.
180#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
181#[serde(rename_all = "camelCase")]
182pub enum LiteralKind {
183    Bool,
184    Number,
185    String,
186    HexString,
187    UnicodeString,
188}
189
190// ── Modifier invocation kind ───────────────────────────────────────────────
191
192/// Distinguishes modifier calls from base constructor specifiers.
193#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
194#[serde(rename_all = "camelCase")]
195pub enum ModifierInvocationKind {
196    ModifierInvocation,
197    BaseConstructorSpecifier,
198}
199
200// ── Yul literal kind ───────────────────────────────────────────────────────
201
202/// The kind of a Yul literal value.
203#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
204#[serde(rename_all = "camelCase")]
205pub enum YulLiteralKind {
206    Number,
207    String,
208    Bool,
209}