slang_solidity 1.3.5

A modular set of compiler APIs empowering the next generation of Solidity code analysis and developer tooling. Written in Rust and distributed in multiple languages.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use std::collections::{HashMap, HashSet};
use std::rc::Rc;

use super::definitions::Definition;
use super::ScopeId;
use crate::backend::types::TypeId;
use crate::cst::{NodeId, TerminalKind, TerminalNode};

//////////////////////////////////////////////////////////////////////////////
// Scopes - types

pub(crate) enum Scope {
    Block(BlockScope),
    Contract(ContractScope),
    Enum(EnumScope),
    File(FileScope),
    Function(FunctionScope),
    Modifier(ModifierScope),
    Parameters(ParametersScope),
    Struct(StructScope),
    Using(UsingScope),
    YulBlock(YulBlockScope),
    YulFunction(YulFunctionScope),
}

pub(crate) struct BlockScope {
    pub(crate) node_id: NodeId,
    pub(crate) parent_scope_id: ScopeId,
    pub(crate) definitions: HashMap<String, NodeId>,
}

pub(crate) struct ContractScope {
    pub(crate) node_id: NodeId,
    pub(crate) file_scope_id: ScopeId,
    pub(crate) definitions: HashMap<String, Vec<NodeId>>,
    pub(crate) using_directives: Vec<UsingDirective>,
}

pub(crate) struct EnumScope {
    pub(crate) node_id: NodeId,
    pub(crate) definitions: HashMap<String, NodeId>,
}

pub(crate) struct FileScope {
    pub(crate) node_id: NodeId,
    pub(crate) file_id: String,
    pub(crate) definitions: HashMap<String, Vec<NodeId>>,
    pub(crate) imported_files: HashSet<String>,
    pub(crate) using_directives: Vec<UsingDirective>,
}

pub(crate) struct FunctionScope {
    pub(crate) node_id: NodeId,
    pub(crate) parent_scope_id: ScopeId,
    pub(crate) parameters_scope_id: ScopeId,
    pub(crate) definitions: HashMap<String, NodeId>,
}

// TODO: this is similar to a function scope, but it doesn't have a separate
// parameters scope; it also should bind the special `_` symbol to the built-in.
// There are other functions that don't need a separate parameters scope (eg.
// receive/fallback/unnamed), but they don't need to bind `_`. Should we
// refactor? Or remove this and make the parameters optional in FunctionScope?
// Probably the latter as we can control resolution in the relevant pass.
pub(crate) struct ModifierScope {
    pub(crate) node_id: NodeId,
    pub(crate) parent_scope_id: ScopeId,
    pub(crate) definitions: HashMap<String, NodeId>,
}

/// This is stored in a vector in the `ParametersScope` below preserving order
/// for positional arguments and used by the binder, both to resolve named
/// arguments, and disambiguate overloads of functions and events.
pub(crate) struct ParameterDefinition {
    pub(crate) name: Option<String>,
    pub(crate) node_id: NodeId,
    /// The type of the parameter, or `None` if it's not yet computed or cannot
    /// be computed. For some parameter containers (eg. errors) it's never
    /// computed.
    pub(crate) type_id: Option<TypeId>,
}

pub(crate) struct ParametersScope {
    pub(crate) parameters: Vec<ParameterDefinition>,
}

pub(crate) struct StructScope {
    pub(crate) node_id: NodeId,
    pub(crate) definitions: HashMap<String, NodeId>,
}

pub(crate) struct UsingScope {
    pub(crate) node_id: NodeId,
    pub(crate) symbols: HashMap<String, Vec<NodeId>>,
}

pub(crate) struct YulBlockScope {
    pub(crate) node_id: NodeId,
    pub(crate) parent_scope_id: ScopeId,
    pub(crate) definitions: HashMap<String, NodeId>,
}

pub(crate) struct YulFunctionScope {
    pub(crate) node_id: NodeId,
    pub(crate) parent_scope_id: ScopeId,
    pub(crate) definitions: HashMap<String, NodeId>,
}

//////////////////////////////////////////////////////////////////////////////
// Scopes - implementations

impl Scope {
    pub(crate) fn node_id(&self) -> NodeId {
        match self {
            Self::Block(block_scope) => block_scope.node_id,
            Self::Contract(contract_scope) => contract_scope.node_id,
            Self::Enum(enum_scope) => enum_scope.node_id,
            Self::File(file_scope) => file_scope.node_id,
            Self::Function(function_scope) => function_scope.node_id,
            Self::Modifier(modifier_scope) => modifier_scope.node_id,
            Self::Parameters(_) => unreachable!("parameters scope don't have a node ID"),
            Self::Struct(struct_scope) => struct_scope.node_id,
            Self::Using(using_scope) => using_scope.node_id,
            Self::YulBlock(yul_block_scope) => yul_block_scope.node_id,
            Self::YulFunction(yul_function_scope) => yul_function_scope.node_id,
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        match self {
            Self::Block(block_scope) => block_scope.insert_definition(definition),
            Self::Contract(contract_scope) => contract_scope.insert_definition(definition),
            Self::Enum(enum_scope) => enum_scope.insert_definition(definition),
            Self::File(file_scope) => file_scope.insert_definition(definition),
            Self::Function(function_scope) => function_scope.insert_definition(definition),
            Self::Modifier(modifier_scope) => modifier_scope.insert_definition(definition),
            Self::Parameters(_) => {
                unreachable!("cannot insert a definition into parameters scope directly")
            }
            Self::Struct(struct_scope) => struct_scope.insert_definition(definition),
            Self::Using(_) => unreachable!("cannot insert a definition into a using clause scope"),
            Self::YulBlock(yul_block_scope) => yul_block_scope.insert_definition(definition),
            Self::YulFunction(function_scope) => function_scope.insert_definition(definition),
        }
    }

    pub(crate) fn get_using_directives(&self) -> impl Iterator<Item = &UsingDirective> {
        match self {
            Self::Contract(contract_scope) => {
                EitherIter::Left(contract_scope.using_directives.iter())
            }
            Self::File(file_scope) => EitherIter::Right(file_scope.using_directives.iter()),
            _ => EitherIter::Empty,
        }
    }

    pub(crate) fn new_block(node_id: NodeId, parent_scope_id: ScopeId) -> Self {
        Self::Block(BlockScope::new(node_id, parent_scope_id))
    }

    pub(crate) fn new_contract(node_id: NodeId, file_scope_id: ScopeId) -> Self {
        Self::Contract(ContractScope::new(node_id, file_scope_id))
    }

    pub(crate) fn new_enum(node_id: NodeId) -> Self {
        Self::Enum(EnumScope::new(node_id))
    }

    pub(crate) fn new_file(node_id: NodeId, file_id: &str) -> Self {
        Self::File(FileScope::new(node_id, file_id))
    }

    pub(crate) fn new_function(
        node_id: NodeId,
        parent_scope_id: ScopeId,
        parameters_scope_id: ScopeId,
    ) -> Self {
        Self::Function(FunctionScope::new(
            node_id,
            parent_scope_id,
            parameters_scope_id,
        ))
    }

    pub(crate) fn new_modifier(node_id: NodeId, parent_scope_id: ScopeId) -> Self {
        Self::Modifier(ModifierScope::new(node_id, parent_scope_id))
    }

    pub(crate) fn new_struct(node_id: NodeId) -> Self {
        Self::Struct(StructScope::new(node_id))
    }

    pub(crate) fn new_using(node_id: NodeId, symbols: HashMap<String, Vec<NodeId>>) -> Self {
        Self::Using(UsingScope::new(node_id, symbols))
    }

    pub(crate) fn new_yul_block(node_id: NodeId, parent_scope_id: ScopeId) -> Self {
        Self::YulBlock(YulBlockScope::new(node_id, parent_scope_id))
    }

    pub(crate) fn new_yul_function(node_id: NodeId, enclosing_scope_id: ScopeId) -> Self {
        Self::YulFunction(YulFunctionScope::new(node_id, enclosing_scope_id))
    }
}

impl BlockScope {
    fn new(node_id: NodeId, parent_scope_id: ScopeId) -> Self {
        Self {
            node_id,
            parent_scope_id,
            definitions: HashMap::new(),
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        let symbol = definition.identifier().unparse();
        let node_id = definition.node_id();
        self.definitions.insert(symbol, node_id);
    }
}

impl ContractScope {
    fn new(node_id: NodeId, file_scope_id: ScopeId) -> Self {
        Self {
            node_id,
            file_scope_id,
            definitions: HashMap::new(),
            using_directives: Vec::new(),
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        let symbol = definition.identifier().unparse();
        let node_id = definition.node_id();
        if let Some(definitions) = self.definitions.get_mut(&symbol) {
            definitions.push(node_id);
        } else {
            self.definitions.insert(symbol, vec![node_id]);
        }
    }
}

impl EnumScope {
    fn new(node_id: NodeId) -> Self {
        Self {
            node_id,
            definitions: HashMap::new(),
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        let symbol = definition.identifier().unparse();
        let node_id = definition.node_id();
        self.definitions.insert(symbol, node_id);
    }
}

impl FileScope {
    fn new(node_id: NodeId, file_id: &str) -> Self {
        Self {
            node_id,
            file_id: file_id.to_string(),
            definitions: HashMap::new(),
            imported_files: HashSet::new(),
            using_directives: Vec::new(),
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        let symbol = definition.identifier().unparse();
        let node_id = definition.node_id();
        if let Some(definitions) = self.definitions.get_mut(&symbol) {
            definitions.push(node_id);
        } else {
            self.definitions.insert(symbol, vec![node_id]);
        }
    }

    pub(crate) fn add_imported_file(&mut self, file_id: String) {
        self.imported_files.insert(file_id);
    }

    pub(super) fn lookup_symbol<'a>(&'a self, symbol: &str) -> impl Iterator<Item = NodeId> + 'a {
        match self.definitions.get(symbol) {
            Some(defs) => OptionIter::Some(defs.iter().copied()),
            None => OptionIter::Empty,
        }
    }
}

impl FunctionScope {
    fn new(node_id: NodeId, parent_scope_id: ScopeId, parameters_scope_id: ScopeId) -> Self {
        Self {
            node_id,
            parent_scope_id,
            parameters_scope_id,
            definitions: HashMap::new(),
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        let symbol = definition.identifier().unparse();
        let node_id = definition.node_id();
        self.definitions.insert(symbol, node_id);
    }
}

impl ModifierScope {
    fn new(node_id: NodeId, parent_scope_id: ScopeId) -> Self {
        Self {
            node_id,
            parent_scope_id,
            definitions: HashMap::new(),
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        let symbol = definition.identifier().unparse();
        let node_id = definition.node_id();
        self.definitions.insert(symbol, node_id);
    }
}

impl ParametersScope {
    pub(crate) fn new() -> Self {
        Self {
            parameters: Vec::new(),
        }
    }

    pub(crate) fn add_parameter(&mut self, identifier: Option<&Rc<TerminalNode>>, node_id: NodeId) {
        self.parameters.push(ParameterDefinition {
            name: identifier.map(|name| name.unparse()),
            node_id,
            type_id: None,
        });
    }

    pub(crate) fn set_parameter_types(&mut self, type_ids: &[Option<TypeId>]) {
        if type_ids.len() != self.parameters.len() {
            unreachable!("parameter count mismatch while setting types");
        }
        for (index, parameter) in self.parameters.iter_mut().enumerate() {
            parameter.type_id = type_ids[index];
        }
    }

    pub(crate) fn lookup_definition(&self, symbol: &str) -> Option<NodeId> {
        self.parameters
            .iter()
            .find(|parameter| parameter.name.as_ref().is_some_and(|name| name == symbol))
            .map(|parameter| parameter.node_id)
    }
}

impl StructScope {
    fn new(node_id: NodeId) -> Self {
        Self {
            node_id,
            definitions: HashMap::new(),
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        let symbol = definition.identifier().unparse();
        let node_id = definition.node_id();
        self.definitions.insert(symbol, node_id);
    }
}

impl UsingScope {
    fn new(node_id: NodeId, symbols: HashMap<String, Vec<NodeId>>) -> Self {
        Self { node_id, symbols }
    }
}

impl YulBlockScope {
    fn new(node_id: NodeId, parent_scope_id: ScopeId) -> Self {
        Self {
            node_id,
            parent_scope_id,
            definitions: HashMap::new(),
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        let symbol = definition.identifier().unparse();
        let node_id = definition.node_id();
        self.definitions.insert(symbol, node_id);
    }
}

impl YulFunctionScope {
    fn new(node_id: NodeId, parent_scope_id: ScopeId) -> Self {
        Self {
            node_id,
            parent_scope_id,
            definitions: HashMap::new(),
        }
    }

    pub(crate) fn insert_definition(&mut self, definition: &Definition) {
        let symbol = definition.identifier().unparse();
        let node_id = definition.node_id();
        self.definitions.insert(symbol, node_id);
    }
}

//////////////////////////////////////////////////////////////////////////////
// Using directives

#[allow(dead_code)]
pub(crate) enum UsingDirective {
    AllTypes {
        scope_id: ScopeId,
    },
    SingleType {
        scope_id: ScopeId,
        type_id: TypeId,
    },
    SingleTypeOperator {
        scope_id: ScopeId,
        operator_mapping: HashMap<TerminalKind, String>,
        type_id: TypeId,
    },
}

impl UsingDirective {
    pub(crate) fn new_all(scope_id: ScopeId) -> Self {
        Self::AllTypes { scope_id }
    }

    pub(crate) fn new_single_type(scope_id: ScopeId, type_id: TypeId) -> Self {
        Self::SingleType { scope_id, type_id }
    }

    pub(crate) fn new_single_type_with_operators(
        scope_id: ScopeId,
        type_id: TypeId,
        operator_mapping: HashMap<TerminalKind, String>,
    ) -> Self {
        Self::SingleTypeOperator {
            scope_id,
            operator_mapping,
            type_id,
        }
    }

    pub(crate) fn applies_to(&self, filter_type_id: TypeId) -> bool {
        match self {
            Self::AllTypes { .. } => true,
            Self::SingleType { type_id, .. } | Self::SingleTypeOperator { type_id, .. } => {
                *type_id == filter_type_id
            }
        }
    }

    pub(crate) fn get_scope_id(&self) -> ScopeId {
        match self {
            UsingDirective::AllTypes { scope_id }
            | UsingDirective::SingleType { scope_id, .. }
            | UsingDirective::SingleTypeOperator { scope_id, .. } => *scope_id,
        }
    }
}

pub(crate) enum OptionIter<T: Iterator> {
    Some(T),
    Empty,
}

impl<T> Iterator for OptionIter<T>
where
    T: Iterator,
{
    type Item = T::Item;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            OptionIter::Some(iter) => iter.next(),
            OptionIter::Empty => None,
        }
    }
}

pub(crate) enum EitherIter<L: Iterator, R: Iterator> {
    Left(L),
    Right(R),
    Empty,
}

impl<L, R> Iterator for EitherIter<L, R>
where
    L: Iterator,
    R: Iterator<Item = L::Item>,
{
    type Item = L::Item;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            EitherIter::Left(iter) => iter.next(),
            EitherIter::Right(iter) => iter.next(),
            EitherIter::Empty => None,
        }
    }
}