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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
use std::rc::Rc;

use super::Pass;
use crate::backend::binder::{Definition, Resolution, Typing};
use crate::backend::built_ins::BuiltIn;
use crate::backend::ir::ir2_flat_contracts::{self as input_ir};
use crate::backend::types::{ConstantValue, DataLocation, FunctionType, LiteralKind, Type, TypeId};
use crate::cst::{NodeId, TerminalKind, TerminalNode};
use crate::utils::versions::{VERSION_0_5_0, VERSION_0_8_0};

impl Pass<'_> {
    pub(super) fn typing_of_expression(&self, node: &input_ir::Expression) -> Typing {
        match node {
            input_ir::Expression::AssignmentExpression(assignment_expression) => {
                self.binder.node_typing(assignment_expression.node_id)
            }
            input_ir::Expression::ConditionalExpression(conditional_expression) => {
                self.binder.node_typing(conditional_expression.node_id)
            }
            input_ir::Expression::OrExpression(_)
            | input_ir::Expression::AndExpression(_)
            | input_ir::Expression::EqualityExpression(_)
            | input_ir::Expression::InequalityExpression(_)
            | input_ir::Expression::TrueKeyword
            | input_ir::Expression::FalseKeyword => Typing::Resolved(self.types.boolean()),
            input_ir::Expression::BitwiseOrExpression(bitwise_or_expression) => {
                self.binder.node_typing(bitwise_or_expression.node_id)
            }
            input_ir::Expression::BitwiseXorExpression(bitwise_xor_expression) => {
                self.binder.node_typing(bitwise_xor_expression.node_id)
            }
            input_ir::Expression::BitwiseAndExpression(bitwise_and_expression) => {
                self.binder.node_typing(bitwise_and_expression.node_id)
            }
            input_ir::Expression::ShiftExpression(shift_expression) => {
                self.binder.node_typing(shift_expression.node_id)
            }
            input_ir::Expression::AdditiveExpression(additive_expression) => {
                self.binder.node_typing(additive_expression.node_id)
            }
            input_ir::Expression::MultiplicativeExpression(multiplicative_expression) => {
                self.binder.node_typing(multiplicative_expression.node_id)
            }
            input_ir::Expression::ExponentiationExpression(exponentiation_expression) => {
                self.binder.node_typing(exponentiation_expression.node_id)
            }
            input_ir::Expression::PostfixExpression(postfix_expression) => {
                self.binder.node_typing(postfix_expression.node_id)
            }
            input_ir::Expression::PrefixExpression(prefix_expression) => {
                self.binder.node_typing(prefix_expression.node_id)
            }
            input_ir::Expression::FunctionCallExpression(function_call_expression) => {
                self.binder.node_typing(function_call_expression.node_id)
            }
            input_ir::Expression::CallOptionsExpression(call_options_expression) => {
                self.binder.node_typing(call_options_expression.node_id)
            }
            input_ir::Expression::MemberAccessExpression(member_access_expression) => {
                self.binder.node_typing(member_access_expression.node_id)
            }
            input_ir::Expression::IndexAccessExpression(index_access_expression) => {
                self.binder.node_typing(index_access_expression.node_id)
            }
            input_ir::Expression::NewExpression(new_expression) => {
                self.binder.node_typing(new_expression.node_id)
            }
            input_ir::Expression::TupleExpression(tuple_expression) => {
                self.binder.node_typing(tuple_expression.node_id)
            }
            input_ir::Expression::TypeExpression(type_expression) => {
                self.binder.node_typing(type_expression.node_id)
            }
            input_ir::Expression::ArrayExpression(array_expression) => {
                self.binder.node_typing(array_expression.node_id)
            }
            input_ir::Expression::HexNumberExpression(hex_number_expression) => {
                self.binder.node_typing(hex_number_expression.node_id)
            }
            input_ir::Expression::DecimalNumberExpression(decimal_number_expression) => {
                self.binder.node_typing(decimal_number_expression.node_id)
            }
            input_ir::Expression::StringExpression(string_expression) => self
                .binder
                .node_typing(Self::string_expression_node_id(string_expression)),
            input_ir::Expression::ElementaryType(elementary_type) => {
                Typing::MetaType(Self::type_of_elementary_type(elementary_type))
            }
            input_ir::Expression::Identifier(identifier) => self.typing_of_identifier(identifier),
            input_ir::Expression::PayableKeyword => {
                Typing::MetaType(Type::Address { payable: true })
            }
            input_ir::Expression::ThisKeyword => Typing::This,
            input_ir::Expression::SuperKeyword => Typing::Super,
        }
    }

    fn type_of_elementary_type(elementary_type: &input_ir::ElementaryType) -> Type {
        match elementary_type {
            input_ir::ElementaryType::AddressType(address_type) => Type::Address {
                payable: address_type.payable_keyword,
            },
            input_ir::ElementaryType::BytesKeyword(terminal) => {
                Type::from_bytes_keyword(&terminal.unparse(), Some(DataLocation::Memory)).unwrap()
            }
            input_ir::ElementaryType::IntKeyword(terminal) => {
                Type::from_int_keyword(&terminal.unparse())
            }
            input_ir::ElementaryType::UintKeyword(terminal) => {
                Type::from_uint_keyword(&terminal.unparse())
            }
            input_ir::ElementaryType::FixedKeyword(terminal) => {
                Type::from_fixed_keyword(&terminal.unparse())
            }
            input_ir::ElementaryType::UfixedKeyword(terminal) => {
                Type::from_ufixed_keyword(&terminal.unparse())
            }
            input_ir::ElementaryType::BoolKeyword => Type::Boolean,
            input_ir::ElementaryType::ByteKeyword => Type::ByteArray { width: 1 },
            input_ir::ElementaryType::StringKeyword => Type::String {
                location: DataLocation::Memory,
            },
        }
    }

    pub(super) fn type_of_definition(&mut self, definition_id: NodeId) -> Option<Type> {
        let definition = self.binder.find_definition_by_id(definition_id)?;
        match definition {
            Definition::Contract(_) => Some(Type::Contract { definition_id }),
            Definition::Enum(_) => Some(Type::Enum { definition_id }),
            Definition::Interface(_) => Some(Type::Interface { definition_id }),
            Definition::Struct(_) => Some(Type::Struct {
                definition_id,
                location: DataLocation::Memory,
            }),
            Definition::UserDefinedValueType(_) => Some(Type::UserDefinedValue { definition_id }),
            _ => None,
        }
    }

    fn typing_of_identifier(&self, identifier: &Rc<TerminalNode>) -> Typing {
        let resolution = &self
            .binder
            .find_reference_by_identifier_node_id(identifier.id())
            .unwrap()
            .resolution;
        // The resolution may point to an imported symbol, so we need to follow
        // through in order to get to the actual typing
        let resolution = self.binder.follow_symbol_aliases(resolution);
        self.typing_of_resolution(&resolution)
    }

    pub(super) fn type_of_integer_binary_expression(
        &self,
        left_operand: &input_ir::Expression,
        right_operand: &input_ir::Expression,
    ) -> Option<TypeId> {
        let left_type_id = self.typing_of_expression(left_operand).as_type_id();
        let right_type_id = self.typing_of_expression(right_operand).as_type_id();
        // TODO(validation): check that both operands are indeed integers
        if let (Some(left_type_id), Some(right_type_id)) = (left_type_id, right_type_id) {
            if self
                .types
                .implicitly_convertible_to(right_type_id, left_type_id)
            {
                Some(left_type_id)
            } else if self
                .types
                .implicitly_convertible_to(left_type_id, right_type_id)
            {
                Some(right_type_id)
            } else {
                // TODO(validation): the types are not compatible, we should
                // emit an error, or signal our caller
                None
            }
        } else {
            None
        }
    }

    pub(super) fn type_of_prefix_expression(
        &mut self,
        node: &input_ir::PrefixExpression,
    ) -> Option<TypeId> {
        match node.operator.kind {
            TerminalKind::Minus => {
                // `-literal` folds into a signed literal kind carrying the
                // two's-complement width of the negated value, matching
                // solc's `RationalNumberType::integerType()` behaviour for
                // negated rational-number literals.
                let folded = match &node.operand {
                    input_ir::Expression::DecimalNumberExpression(decimal_number) => {
                        ConstantValue::from_decimal_number(decimal_number)
                    }
                    input_ir::Expression::HexNumberExpression(hex_number) => {
                        ConstantValue::from_hex_number(hex_number)
                    }
                    _ => None,
                };
                if let Some(constant) = &folded {
                    let ConstantValue::Integer(magnitude) = constant;
                    if magnitude.sign() != num_bigint::Sign::NoSign {
                        return Some(
                            self.types
                                .register_type(Type::Literal(constant.get_signed_literal_kind())),
                        );
                    }
                }
                self.typing_of_expression(&node.operand).as_type_id()
            }
            TerminalKind::PlusPlus
            | TerminalKind::Plus
            | TerminalKind::MinusMinus
            | TerminalKind::Tilde => {
                // TODO(validation): check that the operand is integer
                self.typing_of_expression(&node.operand).as_type_id()
            }
            TerminalKind::Bang => {
                // TODO(validation): check that the operand is boolean
                Some(self.types.boolean())
            }
            TerminalKind::DeleteKeyword => Some(self.types.void()),
            _ => None,
        }
    }

    pub(super) fn typing_of_resolution(&self, resolution: &Resolution) -> Typing {
        match resolution {
            Resolution::Unresolved => Typing::Unresolved,
            Resolution::BuiltIn(built_in) => self.built_ins_resolver().typing_of(built_in),
            Resolution::Definition(definition_id) => self.binder.node_typing(*definition_id),
            Resolution::Ambiguous(definitions) => {
                let mut type_ids = Vec::new();
                for definition_id in definitions {
                    if let Typing::Resolved(type_id) = self.binder.node_typing(*definition_id) {
                        type_ids.push(type_id);
                    }
                }
                Typing::Undetermined(type_ids)
            }
        }
    }

    pub(super) fn typing_is_contract_reference(&self, typing: &Typing) -> bool {
        match typing {
            Typing::This => true,
            Typing::Resolved(type_id) => matches!(
                self.types.get_type_by_id(*type_id),
                Type::Contract { .. } | Type::Interface { .. }
            ),
            _ => false,
        }
    }

    pub(super) fn typing_of_resolution_as_getter(&self, resolution: &Resolution) -> Typing {
        if let Resolution::Definition(definition_id) = resolution {
            if let Definition::StateVariable(state_var_definition) =
                self.binder.find_definition_by_id(*definition_id).unwrap()
            {
                if let Some(getter_type_id) = state_var_definition.getter_type_id {
                    return Typing::Resolved(getter_type_id);
                }
            }
        }
        self.typing_of_resolution(resolution)
    }

    fn type_id_of_receiver(&self, operand: &input_ir::Expression) -> Option<TypeId> {
        if let input_ir::Expression::MemberAccessExpression(member_access_expression) = operand {
            self.typing_of_expression(&member_access_expression.operand)
                .as_type_id()
        } else {
            None
        }
    }

    fn typing_of_cast(&mut self, argument_typing: &Typing, target_type: Type) -> Typing {
        // TODO(validation): this is a cast to the given type, but we
        // need to verify that the (single) argument is convertible
        match argument_typing {
            Typing::Resolved(argument_type_id) => {
                // the resulting cast type inherits the data location of the argument
                let argument_type = self.types.get_type_by_id(*argument_type_id);
                let type_id = if let Some(data_location) = argument_type.data_location() {
                    self.types
                        .register_type_with_data_location(target_type, data_location)
                // special case: address(uint160(_)) should type to `address payable` in <0.8.0
                } else if self.language_version < VERSION_0_8_0
                    && self.language_version >= VERSION_0_5_0
                    && matches!(
                        argument_type,
                        Type::Integer {
                            signed: false,
                            bits: 160
                        }
                    )
                    && matches!(target_type, Type::Address { .. })
                {
                    self.types.address_payable()
                } else {
                    self.types.register_type(target_type)
                };
                Typing::Resolved(type_id)
            }
            Typing::This => {
                // special case: "this" can be cast to an address
                if let Type::Address { .. } = target_type {
                    Typing::Resolved(self.types.address())
                } else {
                    Typing::Unresolved
                }
            }
            _ => Typing::Unresolved,
        }
    }

    pub(super) fn collect_positional_argument_typings(
        &self,
        arguments: &[input_ir::Expression],
    ) -> Vec<Typing> {
        arguments
            .iter()
            .map(|argument| self.typing_of_expression(argument))
            .collect::<Vec<_>>()
    }

    pub(super) fn typing_of_function_call_with_positional_arguments(
        &mut self,
        node: &input_ir::FunctionCallExpression,
        arguments: &[input_ir::Expression],
    ) -> Typing {
        let operand_typing = self.typing_of_expression(&node.operand);
        let argument_typings = self.collect_positional_argument_typings(arguments);

        match operand_typing {
            Typing::Unresolved | Typing::This | Typing::Super => {
                // `this` and `super` are not callable
                Typing::Unresolved
            }
            Typing::Resolved(type_id) => {
                if let Type::Function(FunctionType { return_type, .. }) =
                    self.types.get_type_by_id(type_id)
                {
                    Typing::Resolved(*return_type)
                } else {
                    // TODO(validation): the operand did not resolve to a function
                    Typing::Unresolved
                }
            }
            Typing::Undetermined(type_ids) => {
                let receiver_type_id = self.type_id_of_receiver(&node.operand);
                let candidate = self.lookup_function_matching_positional_arguments(
                    &type_ids,
                    &argument_typings,
                    receiver_type_id,
                );

                if let Some(candidate) = candidate {
                    let return_type = candidate.return_type;

                    let reference_node_id = reference_node_id_for_expression(&node.operand);
                    let definition_id = candidate.definition_id;
                    if let (Some(node_id), Some(definition_id)) = (reference_node_id, definition_id)
                    {
                        // TODO: maybe update the typing of the operand as well?
                        self.binder
                            .fixup_reference(node_id, Resolution::Definition(definition_id));
                    }

                    Typing::Resolved(return_type)
                } else {
                    Typing::Unresolved
                }
            }
            Typing::MetaType(type_) => {
                if argument_typings.len() == 1 {
                    self.typing_of_cast(&argument_typings[0], type_)
                } else {
                    Typing::Unresolved
                }
            }
            Typing::NewExpression(type_id) => {
                match self.types.get_type_by_id(type_id) {
                    Type::Array { .. } | Type::Contract { .. } => Typing::Resolved(type_id),
                    _ => {
                        // only contracts can be created with `new`
                        Typing::Unresolved
                    }
                }
            }
            Typing::UserMetaType(node_id) => {
                // Generally this is a cast to the underlying type of the given
                // definition, except for structs for which we need to construct
                // the value in memory
                match self.binder.find_definition_by_id(node_id) {
                    Some(Definition::Contract(_)) => {
                        // TODO(validation): the type of the first argument should be an address
                        let type_id = self.types.register_type(Type::Contract {
                            definition_id: node_id,
                        });
                        Typing::Resolved(type_id)
                    }
                    Some(Definition::Interface(_)) => {
                        // TODO(validation): the type of the first argument should be an address
                        let type_id = self.types.register_type(Type::Interface {
                            definition_id: node_id,
                        });
                        Typing::Resolved(type_id)
                    }
                    Some(Definition::Struct(_)) => {
                        // struct construction
                        let type_id = self.types.register_type(Type::Struct {
                            definition_id: node_id,
                            location: DataLocation::Memory,
                        });
                        Typing::Resolved(type_id)
                    }
                    _ => Typing::Unresolved,
                }
            }
            // Special case: for `abi.decode` we need to register the types
            // given as the second argument and we need a mutable `TypeRegistry`
            // for that.
            Typing::BuiltIn(BuiltIn::AbiDecode) => self.typing_of_abi_decode(&argument_typings),
            Typing::BuiltIn(built_in) => self
                .built_ins_resolver()
                .typing_of_function_call(&built_in, &argument_typings),
        }
    }

    fn typing_of_abi_decode(&mut self, argument_typings: &[Typing]) -> Typing {
        if argument_typings.len() != 2 {
            return Typing::Unresolved;
        }
        match &argument_typings[1] {
            Typing::Resolved(type_id) => {
                // TODO(validation): this only makes sense if type_id is a tuple
                Typing::Resolved(*type_id)
            }
            Typing::UserMetaType(definition_id) => {
                if let Some(type_) = self.type_of_definition(*definition_id) {
                    Typing::Resolved(self.types.register_type(type_))
                } else {
                    Typing::Unresolved
                }
            }
            Typing::MetaType(type_) => Typing::Resolved(self.types.register_type(type_.clone())),
            _ => Typing::Unresolved,
        }
    }

    pub(super) fn collect_named_argument_typings(
        &self,
        arguments: &[input_ir::NamedArgument],
    ) -> Vec<(String, Typing)> {
        arguments
            .iter()
            .map(|argument| {
                (
                    argument.name.unparse(),
                    self.typing_of_expression(&argument.value),
                )
            })
            .collect::<Vec<_>>()
    }

    pub(super) fn typing_of_function_call_with_named_arguments(
        &mut self,
        node: &input_ir::FunctionCallExpression,
        arguments: &[input_ir::NamedArgument],
    ) -> Typing {
        let operand_typing = self.typing_of_expression(&node.operand);

        let (typing, definition_id) = match operand_typing {
            Typing::Unresolved | Typing::This | Typing::Super => {
                // `this` and `super` are not callable
                (Typing::Unresolved, None)
            }
            Typing::Resolved(type_id) => {
                if let Type::Function(FunctionType {
                    definition_id,
                    return_type,
                    ..
                }) = self.types.get_type_by_id(type_id)
                {
                    (Typing::Resolved(*return_type), *definition_id)
                } else {
                    // TODO(validation): the operand did not resolve to a function
                    (Typing::Unresolved, None)
                }
            }
            Typing::Undetermined(type_ids) => {
                let receiver_type_id = self.type_id_of_receiver(&node.operand);
                let argument_typings = self.collect_named_argument_typings(arguments);
                let candidate = self.lookup_function_matching_named_arguments(
                    &type_ids,
                    &argument_typings,
                    receiver_type_id,
                );

                if let Some(candidate) = candidate {
                    let return_type = candidate.return_type;

                    let reference_node_id = reference_node_id_for_expression(&node.operand);
                    let definition_id = candidate.definition_id;
                    if let (Some(node_id), Some(definition_id)) = (reference_node_id, definition_id)
                    {
                        // TODO: maybe update the typing of the operand as well?
                        self.binder
                            .fixup_reference(node_id, Resolution::Definition(definition_id));
                    }

                    (Typing::Resolved(return_type), definition_id)
                } else {
                    (Typing::Unresolved, None)
                }
            }
            Typing::MetaType(_) => {
                // This is a cast to the given type and is not valid with named arguments
                (Typing::Unresolved, None)
            }
            Typing::NewExpression(type_id) => {
                if let Type::Contract { definition_id } = self.types.get_type_by_id(type_id) {
                    (Typing::Resolved(type_id), Some(*definition_id))
                } else {
                    // only contracts can be created with `new`
                    (Typing::Unresolved, None)
                }
            }
            Typing::UserMetaType(node_id) => {
                // Function call with named arguments are only valid in user
                // types of the struct kind, which results in the construction
                // of such struct in memory
                match self.binder.find_definition_by_id(node_id) {
                    Some(Definition::Struct(_)) => {
                        // struct construction
                        let type_id = self.types.register_type(Type::Struct {
                            definition_id: node_id,
                            location: DataLocation::Memory,
                        });
                        (Typing::Resolved(type_id), Some(node_id))
                    }
                    Some(Definition::Event(_)) => {
                        // this is an event called as a function, which is valid in <0.5.0
                        (Typing::Resolved(self.types.void()), Some(node_id))
                    }
                    _ => (Typing::Unresolved, None),
                }
            }
            Typing::BuiltIn(_) => {
                // built-ins cannot be called with named arguments
                (Typing::Unresolved, None)
            }
        };

        // Reference and resolve named arguments
        self.resolve_named_arguments(arguments, definition_id);

        typing
    }
}

// Given an expression node that resolves to a reference, return the node ID of
// the identifier of the reference. If the expression cannot be traced back to a
// single reference, return `None`.
fn reference_node_id_for_expression(node: &input_ir::Expression) -> Option<NodeId> {
    match &node {
        input_ir::Expression::MemberAccessExpression(f) => Some(f.member.id()),
        input_ir::Expression::Identifier(f) => Some(f.id()),
        input_ir::Expression::CallOptionsExpression(f) => {
            reference_node_id_for_expression(&f.operand)
        }
        _ => None,
    }
}

/// Typing functions for literals
impl Pass<'_> {
    pub(super) fn type_of_string_expression(node: &input_ir::StringExpression) -> Type {
        let kind = match node {
            input_ir::StringExpression::Strings(strings) => {
                let size = strings.iter().fold(0usize, |acc, string| {
                    // TODO: consider escaped characters
                    acc + string.unparse().len() - 2
                });
                LiteralKind::String {
                    bytes: u32::try_from(size).unwrap(),
                }
            }
            input_ir::StringExpression::HexStrings(hex_strings) => {
                let size = hex_strings.iter().fold(0usize, |acc, hex_string| {
                    // 5 is the length of the `hex` prefix plus the quotes
                    acc + (hex_string.unparse().len() - 5) / 2
                });
                LiteralKind::String {
                    bytes: u32::try_from(size).unwrap(),
                }
            }
            input_ir::StringExpression::UnicodeStrings(unicode_strings) => {
                let size = unicode_strings.iter().fold(0usize, |acc, unicode_string| {
                    // TODO: actually parse the string
                    // 9 is the length of the `unicode` prefix plus quotes
                    acc + unicode_string.unparse().len() - 9
                });
                LiteralKind::String {
                    bytes: u32::try_from(size).unwrap(),
                }
            }
        };
        Type::Literal(kind)
    }

    pub(super) fn string_expression_node_id(
        string_expression: &input_ir::StringExpression,
    ) -> NodeId {
        match string_expression {
            input_ir::StringExpression::Strings(strings) => strings[0].id(),
            input_ir::StringExpression::HexStrings(hex_strings) => hex_strings[0].id(),
            input_ir::StringExpression::UnicodeStrings(unicode_strings) => unicode_strings[0].id(),
        }
    }

    pub(super) fn hex_number_literal_kind(
        hex_number_expression: &input_ir::HexNumberExpression,
    ) -> LiteralKind {
        if hex_number_expression.unit.is_some() {
            // this is deprecated in Solidity >= 0.5.0 anyway
            return LiteralKind::DecimalInteger {
                bytes: 32,
                signed: false,
            };
        }
        let mut hex_number = hex_number_expression.literal.unparse();
        hex_number.retain(|character| character != '_');
        if hex_number.len() == 42 {
            // TODO(validation): verify the address is valid (ie. has a valid checksum)
            // We need at least an implementation of SHA3 to compute the checksum
            LiteralKind::Address
        } else if hex_number == "0x0" {
            LiteralKind::Zero
        } else {
            LiteralKind::HexInteger {
                bytes: u32::try_from((hex_number.len() - 3) / 2 + 1).unwrap(),
            }
        }
    }
}