solang 0.3.4

Solang Solidity Compiler
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
// SPDX-License-Identifier: Apache-2.0

use super::{
    annotions_not_allowed,
    ast::{
        Diagnostic, Expression, Function, Mapping, Namespace, Parameter, Statement, StructType,
        Symbol, Type, Variable,
    },
    contracts::is_base,
    diagnostics::Diagnostics,
    expression::{ExprContext, ResolveTo},
    symtable::{Symtable, VariableInitializer, VariableUsage},
    tags::resolve_tags,
    ContractDefinition,
};
use crate::sema::expression::resolve_expression::expression;
use crate::sema::namespace::ResolveTypeContext;
use crate::Target;
use solang_parser::{
    doccomment::DocComment,
    pt::{self, CodeLocation, OptionalCodeLocation},
};
use std::sync::Arc;

pub struct DelayedResolveInitializer<'a> {
    var_no: usize,
    contract_no: usize,
    initializer: &'a pt::Expression,
}

pub fn contract_variables<'a>(
    def: &'a ContractDefinition,
    file_no: usize,
    ns: &mut Namespace,
) -> Vec<DelayedResolveInitializer<'a>> {
    let mut symtable = Symtable::default();
    let mut delayed = Vec::new();

    for part in &def.parts {
        if let pt::ContractPart::VariableDefinition(ref s) = &part.part {
            annotions_not_allowed(&part.annotations, "variable", ns);

            if let Some(delay) = variable_decl(
                Some(def),
                s,
                file_no,
                &part.doccomments,
                Some(def.contract_no),
                ns,
                &mut symtable,
            ) {
                delayed.push(delay);
            }
        }
    }

    delayed
}

pub fn variable_decl<'a>(
    contract: Option<&ContractDefinition>,
    def: &'a pt::VariableDefinition,
    file_no: usize,
    tags: &[DocComment],
    contract_no: Option<usize>,
    ns: &mut Namespace,
    symtable: &mut Symtable,
) -> Option<DelayedResolveInitializer<'a>> {
    let mut attrs = def.attrs.clone();
    let mut ty = def.ty.clone();
    let mut ret = None;

    // For function types, the parser adds the attributes incl visibility to the type,
    // not the pt::VariableDefinition attrs. We need to chomp off the visibility
    // from the attributes before resolving the type
    if let pt::Expression::Type(
        _,
        pt::Type::Function {
            attributes,
            returns,
            ..
        },
    ) = &mut ty
    {
        let mut filter_var_attrs = |attributes: &mut Vec<pt::FunctionAttribute>| {
            if attributes.is_empty() {
                return;
            }

            let mut seen_visibility = false;

            // here we must iterate in reverse order: we can only remove the *last* visibility attribute
            // This is due to the insane syntax
            // contract c {
            //    function() external internal x;
            // }
            // The first external means the function type is external, the second internal means the visibility
            // of the x is internal.
            for attr_no in (0..attributes.len()).rev() {
                if let pt::FunctionAttribute::Immutable(loc) = &attributes[attr_no] {
                    attrs.push(pt::VariableAttribute::Immutable(*loc));
                    attributes.remove(attr_no);
                } else if !seen_visibility {
                    if let pt::FunctionAttribute::Visibility(v) = &attributes[attr_no] {
                        attrs.push(pt::VariableAttribute::Visibility(v.clone()));
                        attributes.remove(attr_no);
                        seen_visibility = true;
                    }
                }
            }
        };

        if let Some((_, trailing_attributes)) = returns {
            filter_var_attrs(trailing_attributes);
        } else {
            filter_var_attrs(attributes);
        }
    }

    let mut diagnostics = Diagnostics::default();

    let ty = match ns.resolve_type(
        file_no,
        contract_no,
        ResolveTypeContext::None,
        &ty,
        &mut diagnostics,
    ) {
        Ok(s) => s,
        Err(()) => {
            ns.diagnostics.extend(diagnostics);
            return None;
        }
    };

    let mut constant = false;
    let mut visibility: Option<pt::Visibility> = None;
    let mut has_immutable: Option<pt::Loc> = None;
    let mut is_override: Option<(pt::Loc, Vec<usize>)> = None;
    let mut storage_type: Option<pt::StorageType> = None;

    for attr in attrs {
        match &attr {
            pt::VariableAttribute::Constant(loc) => {
                if constant {
                    ns.diagnostics.push(Diagnostic::error(
                        *loc,
                        "duplicate constant attribute".to_string(),
                    ));
                }
                constant = true;
            }
            pt::VariableAttribute::Immutable(loc) => {
                if let Some(prev) = &has_immutable {
                    ns.diagnostics.push(Diagnostic::error_with_note(
                        *loc,
                        "duplicate 'immutable' attribute".to_string(),
                        *prev,
                        "previous 'immutable' attribute".to_string(),
                    ));
                }
                has_immutable = Some(*loc);
            }
            pt::VariableAttribute::Override(loc, bases) => {
                if let Some((prev, _)) = &is_override {
                    ns.diagnostics.push(Diagnostic::error_with_note(
                        *loc,
                        "duplicate 'override' attribute".to_string(),
                        *prev,
                        "previous 'override' attribute".to_string(),
                    ));
                }

                let mut list = Vec::new();
                let mut diagnostics = Diagnostics::default();

                if let Some(contract_no) = contract_no {
                    for name in bases {
                        if let Ok(no) =
                            ns.resolve_contract_with_namespace(file_no, name, &mut diagnostics)
                        {
                            if list.contains(&no) {
                                diagnostics.push(Diagnostic::error(
                                    name.loc,
                                    format!("duplicate override '{name}'"),
                                ));
                            } else if !is_base(no, contract_no, ns) {
                                diagnostics.push(Diagnostic::error(
                                    name.loc,
                                    format!(
                                        "override '{}' is not a base contract of '{}'",
                                        name, ns.contracts[contract_no].id
                                    ),
                                ));
                            } else {
                                list.push(no);
                            }
                        }
                    }

                    is_override = Some((*loc, list));
                } else {
                    diagnostics.push(Diagnostic::error(
                        *loc,
                        "global variable has no bases contracts to override".to_string(),
                    ));
                }

                ns.diagnostics.extend(diagnostics);
            }
            pt::VariableAttribute::Visibility(v) if contract_no.is_none() => {
                ns.diagnostics.push(Diagnostic::error(
                    v.loc_opt().unwrap(),
                    format!("'{v}': global variable cannot have visibility specifier"),
                ));
                return None;
            }
            pt::VariableAttribute::Visibility(pt::Visibility::External(loc)) => {
                ns.diagnostics.push(Diagnostic::error(
                    loc.unwrap(),
                    "variable cannot be declared external".to_string(),
                ));
                return None;
            }
            pt::VariableAttribute::Visibility(v) => {
                if let Some(e) = &visibility {
                    ns.diagnostics.push(Diagnostic::error_with_note(
                        v.loc_opt().unwrap(),
                        format!("variable visibility redeclared '{v}'"),
                        e.loc_opt().unwrap(),
                        format!("location of previous declaration of '{e}'"),
                    ));
                    return None;
                }

                visibility = Some(v.clone());
            }
            pt::VariableAttribute::StorageType(s) => {
                if storage_type.is_some() {
                    ns.diagnostics.push(Diagnostic::error(
                        attr.loc(),
                        format!(
                            "mutliple storage type specifiers for '{}'",
                            def.name.as_ref().unwrap().name
                        ),
                    ));
                } else {
                    storage_type = Some(s.clone());
                }
            }
        }
    }

    if ns.target == Target::Soroban {
        if storage_type.is_none() {
            ns.diagnostics.push(Diagnostic::warning(
                def.loc,
                format!(
                    "storage type not specified for `{}`, defaulting to `persistent`",
                    def.name.as_ref().unwrap().name
                ),
            ));
        }
    } else if storage_type.is_some() {
        ns.diagnostics.push(Diagnostic::warning(
            def.loc,
            format!(
                "variable `{}`: storage types are only valid for Soroban targets",
                def.name.as_ref().unwrap().name
            ),
        ));
    }

    if let Some(loc) = &has_immutable {
        if constant {
            ns.diagnostics.push(Diagnostic::error(
                *loc,
                "variable cannot be declared both 'immutable' and 'constant'".to_string(),
            ));
            constant = false;
        }
    }

    let visibility = match visibility {
        Some(v) => v,
        None => pt::Visibility::Internal(Some(def.ty.loc())),
    };

    if let pt::Visibility::Public(_) = &visibility {
        // override allowed
    } else if let Some((loc, _)) = &is_override {
        ns.diagnostics.push(Diagnostic::error(
            *loc,
            "only public variable can be declared 'override'".to_string(),
        ));
        is_override = None;
    }

    if let Some(contract) = contract {
        if matches!(contract.ty, pt::ContractTy::Interface(_))
            || (matches!(contract.ty, pt::ContractTy::Library(_)) && !constant)
        {
            if contract.name.is_none() || def.name.is_none() {
                return None;
            }
            ns.diagnostics.push(Diagnostic::error(
                def.loc,
                format!(
                    "{} '{}' is not allowed to have contract variable '{}'",
                    contract.ty,
                    contract.name.as_ref().unwrap().name,
                    def.name.as_ref().unwrap().name
                ),
            ));
            return None;
        }
    } else {
        if !constant {
            ns.diagnostics.push(Diagnostic::error(
                def.ty.loc(),
                "global variable must be constant".to_string(),
            ));
            return None;
        }
        if ty.contains_internal_function(ns) {
            ns.diagnostics.push(Diagnostic::error(
                def.ty.loc(),
                "global variable cannot be of type internal function".to_string(),
            ));
            return None;
        }
    }

    if ty.contains_internal_function(ns)
        && matches!(
            visibility,
            pt::Visibility::Public(_) | pt::Visibility::External(_)
        )
    {
        ns.diagnostics.push(Diagnostic::error(
            def.ty.loc(),
            format!("variable of type internal function cannot be '{visibility}'"),
        ));
        return None;
    } else if let Some(ty) = ty.contains_builtins(ns, &StructType::AccountInfo) {
        let message = format!("variable cannot be of builtin type '{}'", ty.to_string(ns));
        ns.diagnostics
            .push(Diagnostic::error(def.ty.loc(), message));
        return None;
    } else if let Some(ty) = ty.contains_builtins(ns, &StructType::AccountMeta) {
        let message = format!("variable cannot be of builtin type '{}'", ty.to_string(ns));
        ns.diagnostics
            .push(Diagnostic::error(def.ty.loc(), message));
        return None;
    }

    let mut diagnostics = Diagnostics::default();

    let initializer = if constant {
        if let Some(initializer) = &def.initializer {
            let mut context = ExprContext {
                file_no,
                contract_no,
                constant,
                ..Default::default()
            };
            context.enter_scope();

            match expression(
                initializer,
                &mut context,
                ns,
                symtable,
                &mut diagnostics,
                ResolveTo::Type(&ty),
            ) {
                Ok(res) => {
                    // implicitly conversion to correct ty
                    match res.cast(&def.loc, &ty, true, ns, &mut diagnostics) {
                        Ok(res) => {
                            res.check_constant_overflow(&mut diagnostics);
                            Some(res)
                        }
                        Err(_) => None,
                    }
                }
                Err(()) => None,
            }
        } else {
            diagnostics.push(Diagnostic::decl_error(
                def.loc,
                "missing initializer for constant".to_string(),
            ));

            None
        }
    } else {
        None
    };

    ns.diagnostics.extend(diagnostics);

    let bases = contract_no.map(|contract_no| ns.contract_bases(contract_no));

    let tags = resolve_tags(
        def.name.as_ref().unwrap().loc.file_no(),
        if contract_no.is_none() {
            "global variable"
        } else {
            "state variable"
        },
        tags,
        None,
        None,
        bases,
        ns,
    );

    let sdecl = Variable {
        name: def.name.as_ref().unwrap().name.to_string(),
        loc: def.loc,
        tags,
        visibility: visibility.clone(),
        ty: ty.clone(),
        constant,
        immutable: has_immutable.is_some(),
        assigned: def.initializer.is_some(),
        initializer,
        read: matches!(visibility, pt::Visibility::Public(_)),
        storage_type,
    };

    let var_no = if let Some(contract_no) = contract_no {
        let var_no = ns.contracts[contract_no].variables.len();

        ns.contracts[contract_no].variables.push(sdecl);

        if !constant {
            if let Some(initializer) = &def.initializer {
                ret = Some(DelayedResolveInitializer {
                    var_no,
                    contract_no,
                    initializer,
                });
            }
        }

        var_no
    } else {
        let var_no = ns.constants.len();

        ns.constants.push(sdecl);

        var_no
    };

    let success = ns.add_symbol(
        file_no,
        contract_no,
        def.name.as_ref().unwrap(),
        Symbol::Variable(def.loc, contract_no, var_no),
    );

    // for public variables in contracts, create an accessor function
    if success && matches!(visibility, pt::Visibility::Public(_)) {
        if let Some(contract_no) = contract_no {
            // The accessor function returns the value of the storage variable, constant or not.
            let mut expr = if constant {
                Expression::ConstantVariable {
                    loc: pt::Loc::Implicit,
                    ty: ty.clone(),
                    contract_no: Some(contract_no),
                    var_no,
                }
            } else {
                Expression::StorageVariable {
                    loc: pt::Loc::Implicit,
                    ty: Type::StorageRef(false, Box::new(ty.clone())),
                    contract_no,
                    var_no,
                }
            };

            // If the variable is an array or mapping, the accessor function takes mapping keys
            // or array indices as arguments, and returns the dereferenced value
            let mut symtable = Symtable::default();
            let mut context = ExprContext::default();
            context.enter_scope();
            let mut params = Vec::new();
            let param = collect_parameters(
                &ty,
                &def.name,
                &mut symtable,
                &mut context,
                &mut params,
                &mut expr,
                ns,
            )?;

            if param.ty.contains_mapping(ns) {
                // we can't return a mapping
                ns.diagnostics.push(Diagnostic::decl_error(
                    def.loc,
                    "mapping in a struct variable cannot be public".to_string(),
                ));
            }

            let (body, returns) =
                accessor_body(expr, param, constant, &mut symtable, &mut context, ns);

            let mut func = Function::new(
                def.name.as_ref().unwrap().loc,
                def.name.as_ref().unwrap().loc,
                def.name.as_ref().unwrap().clone(),
                Some(contract_no),
                Vec::new(),
                pt::FunctionTy::Function,
                // accessors for constant variables have view mutability
                Some(pt::Mutability::View(def.name.as_ref().unwrap().loc)),
                pt::Visibility::External(None),
                params,
                returns,
                ns,
            );

            func.body = body;
            func.is_accessor = true;
            func.has_body = true;
            func.is_override = is_override;
            func.symtable = symtable;

            // add the function to the namespace and then to our contract
            let func_no = ns.functions.len();

            ns.functions.push(func);

            ns.contracts[contract_no].functions.push(func_no);

            // we already have a symbol for
            let symbol = Symbol::Function(vec![(def.loc, func_no)]);

            ns.function_symbols.insert(
                (
                    def.loc.file_no(),
                    Some(contract_no),
                    def.name.as_ref().unwrap().name.to_owned(),
                ),
                symbol,
            );
        }
    }

    ret
}

/// For accessor functions, create the parameter list and the return expression
fn collect_parameters(
    ty: &Type,
    name: &Option<pt::Identifier>,
    symtable: &mut Symtable,
    context: &mut ExprContext,
    params: &mut Vec<Parameter<Type>>,
    expr: &mut Expression,
    ns: &mut Namespace,
) -> Option<Parameter<Type>> {
    match ty {
        Type::Mapping(Mapping {
            key,
            key_name,
            value,
            value_name,
        }) => {
            let map = (*expr).clone();

            let id = if let Some(name) = &key_name {
                name.clone()
            } else {
                pt::Identifier {
                    loc: pt::Loc::Implicit,
                    name: String::new(),
                }
            };
            let arg_ty = key.as_ref().clone();

            let arg_no = symtable.add(
                &id,
                arg_ty.clone(),
                ns,
                VariableInitializer::Solidity(None),
                VariableUsage::Parameter,
                None,
                context,
            )?;

            symtable.arguments.push(Some(arg_no));

            *expr = Expression::Subscript {
                loc: pt::Loc::Implicit,
                ty: ty.storage_array_elem(),
                array_ty: Type::StorageRef(false, Box::new(ty.clone())),
                array: Box::new(map),
                index: Box::new(Expression::Variable {
                    loc: pt::Loc::Implicit,
                    ty: arg_ty,
                    var_no: arg_no,
                }),
            };

            params.push(Parameter {
                id: key_name.clone(),
                loc: pt::Loc::Implicit,
                ty: key.as_ref().clone(),
                ty_loc: None,
                indexed: false,
                readonly: false,
                infinite_size: false,
                recursive: false,
                annotation: None,
            });

            collect_parameters(value, value_name, symtable, context, params, expr, ns)
        }
        Type::Array(elem_ty, dims) => {
            let mut ty = Type::StorageRef(false, Box::new(ty.clone()));
            for _ in 0..dims.len() {
                let map = (*expr).clone();

                let id = pt::Identifier {
                    loc: pt::Loc::Implicit,
                    name: "".to_owned(),
                };
                let arg_ty = Type::Uint(256);

                let var_no = symtable
                    .add(
                        &id,
                        arg_ty.clone(),
                        ns,
                        VariableInitializer::Solidity(None),
                        VariableUsage::Parameter,
                        None,
                        context,
                    )
                    .unwrap();

                symtable.arguments.push(Some(var_no));

                *expr = Expression::Subscript {
                    loc: pt::Loc::Implicit,
                    ty: ty.storage_array_elem(),
                    array_ty: ty.clone(),
                    array: Box::new(map),
                    index: Box::new(Expression::Variable {
                        loc: pt::Loc::Implicit,
                        ty: Type::Uint(256),
                        var_no,
                    }),
                };

                ty = ty.storage_array_elem();

                params.push(Parameter {
                    id: Some(id),
                    loc: pt::Loc::Implicit,
                    ty: arg_ty,
                    ty_loc: None,
                    indexed: false,
                    readonly: false,
                    infinite_size: false,
                    recursive: false,
                    annotation: None,
                });
            }

            collect_parameters(elem_ty, &None, symtable, context, params, expr, ns)
        }
        _ => Some(Parameter {
            id: name.clone(),
            loc: if let Some(name) = name {
                name.loc
            } else {
                pt::Loc::Implicit
            },
            ty: ty.clone(),
            ty_loc: None,
            indexed: false,
            readonly: false,
            infinite_size: false,
            recursive: false,
            annotation: None,
        }),
    }
}

/// Build up an ast for the implict accessor function for public state variables.
fn accessor_body(
    expr: Expression,
    param: Parameter<Type>,
    constant: bool,
    symtable: &mut Symtable,
    context: &mut ExprContext,
    ns: &mut Namespace,
) -> (Vec<Statement>, Vec<Parameter<Type>>) {
    // This could be done in codegen rather than sema, however I am not sure how we would implement
    // that. After building up the parameter/returns list for the implicit function, we have done almost
    // all the work already for building the body (see `expr` and `param` above). So, we would need to
    // share some code between codegen and sema for this.

    let mut ty = param.ty.clone();

    // If the return value of an accessor function is a struct, return the members rather than
    // the struct. This is a particular inconsistency in Solidity which does not apply recursively,
    // i.e. if the struct contains structs, then those values are returned as structs.
    if let Type::Struct(str) = &param.ty {
        let expr = Arc::new(expr);

        if !constant {
            ty = Type::StorageRef(false, ty.into());
        }

        let var_no = symtable
            .add(
                &pt::Identifier {
                    loc: pt::Loc::Implicit,
                    name: "_".into(),
                },
                ty.clone(),
                ns,
                VariableInitializer::Solidity(Some(expr.clone())),
                VariableUsage::LocalVariable,
                Some(pt::StorageLocation::Storage(pt::Loc::Implicit)),
                context,
            )
            .unwrap();

        symtable.vars[&var_no].read = true;
        symtable.vars[&var_no].assigned = true;

        let def = str.definition(ns);
        let returns = def.fields.clone();

        let list = if constant {
            def.fields
                .iter()
                .enumerate()
                .map(|(field, param)| Expression::Load {
                    loc: pt::Loc::Implicit,
                    ty: param.ty.clone(),
                    expr: Expression::StructMember {
                        loc: pt::Loc::Implicit,
                        ty: Type::Ref(def.fields[field].ty.clone().into()),
                        expr: Expression::Variable {
                            loc: pt::Loc::Implicit,
                            ty: ty.clone(),
                            var_no,
                        }
                        .into(),
                        field,
                    }
                    .into(),
                })
                .collect()
        } else {
            def.fields
                .iter()
                .enumerate()
                .map(|(field, param)| Expression::StorageLoad {
                    loc: pt::Loc::Implicit,
                    ty: param.ty.clone(),
                    expr: Expression::StructMember {
                        loc: pt::Loc::Implicit,
                        ty: Type::StorageRef(false, def.fields[field].ty.clone().into()),
                        expr: Expression::Variable {
                            loc: pt::Loc::Implicit,
                            ty: ty.clone(),
                            var_no,
                        }
                        .into(),
                        field,
                    }
                    .into(),
                })
                .collect()
        };

        let body = vec![
            Statement::VariableDecl(pt::Loc::Implicit, var_no, param, Some(expr)),
            Statement::Return(
                pt::Loc::Implicit,
                Some(Expression::List {
                    loc: pt::Loc::Implicit,
                    list,
                }),
            ),
        ];

        (body, returns)
    } else {
        let body = vec![Statement::Return(
            pt::Loc::Implicit,
            Some(if constant {
                expr
            } else {
                Expression::StorageLoad {
                    loc: pt::Loc::Implicit,
                    ty,
                    expr: Box::new(expr),
                }
            }),
        )];

        let returns = vec![param];

        (body, returns)
    }
}

pub fn resolve_initializers(
    initializers: &[DelayedResolveInitializer],
    file_no: usize,
    ns: &mut Namespace,
) {
    let mut symtable = Symtable::default();
    let mut diagnostics = Diagnostics::default();

    for DelayedResolveInitializer {
        var_no,
        contract_no,
        initializer,
    } in initializers
    {
        let var = &ns.contracts[*contract_no].variables[*var_no];
        let ty = var.ty.clone();

        let mut context = ExprContext {
            file_no,
            contract_no: Some(*contract_no),
            ..Default::default()
        };
        context.enter_scope();

        if let Ok(res) = expression(
            initializer,
            &mut context,
            ns,
            &mut symtable,
            &mut diagnostics,
            ResolveTo::Type(&ty),
        ) {
            if let Ok(res) = res.cast(&initializer.loc(), &ty, true, ns, &mut diagnostics) {
                res.check_constant_overflow(&mut diagnostics);
                ns.contracts[*contract_no].variables[*var_no].initializer = Some(res);
            }
        }
    }

    ns.diagnostics.extend(diagnostics);
}