forge_fmt/solang_ext/
loc.rs

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
use solang_parser::pt;
use std::{borrow::Cow, rc::Rc, sync::Arc};

/// Returns the code location.
///
/// Patched version of [`pt::CodeLocation`]: includes the block of a [`pt::FunctionDefinition`] in
/// its `loc`.
pub trait CodeLocationExt {
    /// Returns the code location of `self`.
    fn loc(&self) -> pt::Loc;
}

impl<T: ?Sized + CodeLocationExt> CodeLocationExt for &T {
    fn loc(&self) -> pt::Loc {
        (**self).loc()
    }
}

impl<T: ?Sized + CodeLocationExt> CodeLocationExt for &mut T {
    fn loc(&self) -> pt::Loc {
        (**self).loc()
    }
}

impl<T: ?Sized + ToOwned + CodeLocationExt> CodeLocationExt for Cow<'_, T> {
    fn loc(&self) -> pt::Loc {
        (**self).loc()
    }
}

impl<T: ?Sized + CodeLocationExt> CodeLocationExt for Box<T> {
    fn loc(&self) -> pt::Loc {
        (**self).loc()
    }
}

impl<T: ?Sized + CodeLocationExt> CodeLocationExt for Rc<T> {
    fn loc(&self) -> pt::Loc {
        (**self).loc()
    }
}

impl<T: ?Sized + CodeLocationExt> CodeLocationExt for Arc<T> {
    fn loc(&self) -> pt::Loc {
        (**self).loc()
    }
}

// FunctionDefinition patch
impl CodeLocationExt for pt::FunctionDefinition {
    #[inline]
    #[track_caller]
    fn loc(&self) -> pt::Loc {
        let mut loc = self.loc;
        if let Some(ref body) = self.body {
            loc.use_end_from(&pt::CodeLocation::loc(body));
        }
        loc
    }
}

impl CodeLocationExt for pt::ContractPart {
    #[inline]
    #[track_caller]
    fn loc(&self) -> pt::Loc {
        match self {
            Self::FunctionDefinition(f) => f.loc(),
            _ => pt::CodeLocation::loc(self),
        }
    }
}

impl CodeLocationExt for pt::SourceUnitPart {
    #[inline]
    #[track_caller]
    fn loc(&self) -> pt::Loc {
        match self {
            Self::FunctionDefinition(f) => f.loc(),
            _ => pt::CodeLocation::loc(self),
        }
    }
}

impl CodeLocationExt for pt::ImportPath {
    fn loc(&self) -> pt::Loc {
        match self {
            Self::Filename(s) => s.loc(),
            Self::Path(i) => i.loc(),
        }
    }
}

macro_rules! impl_delegate {
    ($($t:ty),+ $(,)?) => {$(
        impl CodeLocationExt for $t {
            #[inline]
            #[track_caller]
            fn loc(&self) -> pt::Loc {
                pt::CodeLocation::loc(self)
            }
        }
    )+};
}

impl_delegate! {
    pt::Annotation,
    pt::Base,
    pt::ContractDefinition,
    pt::EnumDefinition,
    pt::ErrorDefinition,
    pt::ErrorParameter,
    pt::EventDefinition,
    pt::EventParameter,
    // pt::FunctionDefinition,
    pt::HexLiteral,
    pt::Identifier,
    pt::IdentifierPath,
    pt::NamedArgument,
    pt::Parameter,
    // pt::SourceUnit,
    pt::StringLiteral,
    pt::StructDefinition,
    pt::TypeDefinition,
    pt::Using,
    pt::UsingFunction,
    pt::VariableDeclaration,
    pt::VariableDefinition,
    pt::YulBlock,
    pt::YulFor,
    pt::YulFunctionCall,
    pt::YulFunctionDefinition,
    pt::YulSwitch,
    pt::YulTypedIdentifier,

    pt::CatchClause,
    pt::Comment,
    // pt::ContractPart,
    pt::ContractTy,
    pt::Expression,
    pt::FunctionAttribute,
    // pt::FunctionTy,
    pt::Import,
    pt::Loc,
    pt::Mutability,
    // pt::SourceUnitPart,
    pt::Statement,
    pt::StorageLocation,
    // pt::Type,
    // pt::UserDefinedOperator,
    pt::UsingList,
    pt::VariableAttribute,
    // pt::Visibility,
    pt::YulExpression,
    pt::YulStatement,
    pt::YulSwitchOptions,
}