summaryrefslogtreecommitdiff
path: root/src/resolve/use.cpp
blob: 254a237401daa0adf9e72c842b3a6478b5333bcd (plain)
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
/*
 * Absolutise and check all 'use' statements
 */
#include <main_bindings.hpp>
#include <ast/crate.hpp>
#include <ast/ast.hpp>

void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path path);
::AST::Path Resolve_Use_AbsolutisePath(const ::AST::Path& base_path, ::AST::Path path);
::AST::PathBinding Resolve_Use_GetBinding(const Span& span, const ::AST::Crate& crate, const ::AST::Path& path);

void Resolve_Use(::AST::Crate& crate)
{
    Resolve_Use_Mod(crate, crate.m_root_module, ::AST::Path("", {}));
}

::AST::Path Resolve_Use_AbsolutisePath(const Span& span, const ::AST::Path& base_path, ::AST::Path path)
{
    TU_MATCH(::AST::Path::Class, (path.m_class), (e),
    (Invalid,
        // Should never happen
        BUG(span, "Invalid path class encountered");
        ),
    (Local,
        // Wait, how is this already known?
        BUG(span, "Local path class in use statement");
        ),
    (UFCS,
        // Wait, how is this already known?
        BUG(span, "UFCS path class in use statement");
        ),
    (Relative,
        return base_path + path;
        ),
    (Self,
        return base_path + path;
        ),
    (Super,
        assert(e.count >= 1);
        AST::Path   np(base_path.crate(), {});
        if( e.count > base_path.nodes().size() ) {
            ERROR(span, E0000, "Too many `super` components");
        }
        for( unsigned int i = 0; i < base_path.nodes().size() - e.count; i ++ )
            np.nodes().push_back( base_path.nodes()[i] );
        np += path;
        return np;
        ),
    (Absolute,
        // Leave as is
        return path;
        )
    )
    throw "BUG: Reached end of Resolve_Use_AbsolutisePath";
}

void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path path)
{
    TRACE_FUNCTION_F("path = " << path << ", mod.path() = " << mod.path());
    for(auto& use_stmt : mod.imports())
    {
        const Span  span;
        use_stmt.data = Resolve_Use_AbsolutisePath(span, path, mv$(use_stmt.data));
        if( !use_stmt.data.m_class.is_Absolute() )
            BUG(span, "Use path is not absolute after absolutisation");
        
        // TODO: Is this a valid assertion?
        if( use_stmt.data.crate() != "" )
            BUG(span, "Use path crate was set before resolve");
        
        use_stmt.data.bind( Resolve_Use_GetBinding(span, crate, use_stmt.data) );
        
        // - If doing a glob, ensure the item type is valid
        if( use_stmt.name == "" )
        {
            TU_MATCH_DEF(::AST::PathBinding, (use_stmt.data.binding()), (e),
            (
                ERROR(span, E0000, "Wildcard import of invalid item type");
                ),
            (Enum,
                ),
            (Module,
                )
            )
        }
    }
    
    for(auto& i : mod.items())
    {
        if( i.data.is_Module() )
        {
            Resolve_Use_Mod(crate, i.data.as_Module(), path + i.name);
        }
    }
}

::AST::PathBinding Resolve_Use_GetBinding_Mod(const Span& span, const ::AST::Crate& crate, const ::AST::Module& mod, const ::std::string& des_item_name)
{
    for( const auto& item : mod.items() )
    {
        if( item.data.is_None() )
            continue ;
        
        if( item.name == des_item_name ) {
            TU_MATCH(::AST::Item, (item.data), (e),
            (None,
                // IMPOSSIBLe - Handled above
                ),
            (Crate,
                //return ::AST::PathBinding::make_Crate({&e});
                TODO(span, "Handle importing from a crate");
                ),
            (Type,
                return ::AST::PathBinding::make_TypeAlias({&e});
                ),
            (Trait,
                return ::AST::PathBinding::make_Trait({&e});
                ),
            
            (Function,
                return ::AST::PathBinding::make_Function({&e});
                ),
            (Static,
                return ::AST::PathBinding::make_Static({&e});
                ),
            (Struct,
                return ::AST::PathBinding::make_Struct({&e});
                ),
            (Enum,
                return ::AST::PathBinding::make_Enum({&e});
                ),
            (Module,
                return ::AST::PathBinding::make_Module({&e});
                )
            )
            break ;
        }
    }
    
    // Imports
    for( const auto& imp : mod.imports() )
    {
        if( imp.name == des_item_name ) {
            DEBUG("- Named import " << imp.name << " = " << imp.data);
            if( imp.data.binding().is_Unbound() ) {
                DEBUG(" > Needs resolve");
                const Span sp2;
                return Resolve_Use_GetBinding(sp2, crate, Resolve_Use_AbsolutisePath(sp2, mod.path(), imp.data));
            }
            else {
                return imp.data.binding().clone();
            }
        }
        if( imp.is_pub && imp.name == "" ) {
            const Span sp2;
            // INEFFICIENT! Resolves and throws away the result (because we can't/shouldn't mutate here)
            ::AST::PathBinding  binding_;
            const auto* binding = &imp.data.binding();
            if( binding->is_Unbound() ) {
                DEBUG("Temp resolving wildcard " << imp.data);
                binding_ = Resolve_Use_GetBinding(sp2, crate, Resolve_Use_AbsolutisePath(sp2, mod.path(), imp.data));
                binding = &binding_;
            }
            
            TU_MATCH_DEF(::AST::PathBinding, ((*binding)), (e),
            (
                BUG(sp2, "Wildcard import expanded to an invalid item class");
                ),
            (Module,
                TODO(span, "Look up wildcard in module");
                ),
            (Enum,
                TODO(span, "Look up wildcard in enum");
                )
            )
        }
    }

    ERROR(span, E0000, "Could not find node '" << des_item_name << "' in module " << mod.path());
}

::AST::PathBinding Resolve_Use_GetBinding(const Span& span, const ::AST::Crate& crate, const ::AST::Path& path)
{
    const AST::Module* mod = &crate.m_root_module;
    const auto& nodes = path.nodes();
    for( unsigned int i = 0; i < nodes.size()-1; i ++ )
    {
        auto b = Resolve_Use_GetBinding_Mod(span, crate, *mod, nodes[i].name());
        TU_MATCH_DEF(::AST::PathBinding, (b), (e),
        (
            ERROR(span, E0000, "Unexpected item type in import");
            ),
        (Enum,
            const auto& enum_ = *e.enum_;
            i += 1;
            if( i != nodes.size() - 1 ) {
                ERROR(span, E0000, "Encountered enum at unexpected location in import");
            }
            
            const auto& node2 = nodes[i];
             int    variant_index = -1;
            for( unsigned int j = 0; j < enum_.variants().size(); j ++ )
            {
                if( enum_.variants()[j].m_name == node2.name() ) {
                    variant_index = j;
                    break ;
                }
            }
            if( variant_index < 0 ) {
                ERROR(span, E0000, "Unknown enum variant '" << node2.name() << "'");
            }
            
            return ::AST::PathBinding::make_EnumVar({&enum_, static_cast<unsigned int>(variant_index)});
            ),
        (Module,
            mod = e.module_;
            )
        )
    }
    
    return Resolve_Use_GetBinding_Mod(span, crate, *mod, nodes.back().name());
}