summaryrefslogtreecommitdiff
path: root/src/ast/path.hpp
blob: 2693a070bfa0affb81519e734911780c30f4e05c (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
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
/*
 * MRustC - Mutabah's Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * ast/path.hpp
 * - AST::Path and helper types
 */
#ifndef AST_PATH_HPP_INCLUDED
#define AST_PATH_HPP_INCLUDED

#include "../common.hpp"
#include <string>
#include <stdexcept>
#include <vector>
#include <initializer_list>
#include <cassert>
#include <tagged_union.hpp>
#include <string>
#include "../include/span.hpp"
#include "../include/ident.hpp"

class TypeRef;

namespace HIR {
class Module;
class Trait;
class Enum;
class Struct;
class Union;
class Static;
}   // namespace HIR

namespace AST {

class LifetimeRef;
class GenericParams;
class Crate;
class Module;
class TypeAlias;
class Enum;
class Struct;
class Union;
class Trait;
class Static;
class Function;
class ExternCrate;

TAGGED_UNION_EX(PathBinding, (), Unbound, (
    (Unbound, struct {
        }),
    (Crate, struct {
        const ExternCrate* crate_;
        }),
    (Module, struct {
        const Module* module_;
        const ::HIR::Module* hir;
        }),
    (Struct, struct {
        const Struct* struct_;
        const ::HIR::Struct* hir;
        }),
    (Enum,   struct {
        const Enum* enum_;
        const ::HIR::Enum*  hir;
        }),
    (Union,   struct {
        const Union* union_;
        const ::HIR::Union*  hir;
        }),
    (Trait,  struct {
        const Trait* trait_;
        const ::HIR::Trait* hir;
        }),
    (Static, struct {
        const Static* static_;
        const ::HIR::Static* hir; // if nullptr and static_ == nullptr, points to a `const`
        }),
    (Function, struct {
        const Function* func_;
        }),
    (EnumVar, struct {
        const Enum* enum_;
        unsigned int idx;
        const ::HIR::Enum*  hir;
        }),
    (TypeAlias, struct {
        const TypeAlias* alias_;
        }),
    (StructMethod, struct {
        const Struct* struct_;
        ::std::string name;
        }),
    (TraitMethod, struct {
        const Trait* trait_;
        ::std::string name;
        }),

    (TypeParameter, struct {
        unsigned int level;
        unsigned int idx;
        }),
    (Variable, struct {
        unsigned int slot;
        })
    ),
    (), (),
    (
    public:
        PathBinding clone() const;
        )
    );

extern ::std::ostream& operator<<(::std::ostream& os, const PathBinding& x);

struct PathParams
{
    ::std::vector< LifetimeRef >  m_lifetimes;
    ::std::vector< TypeRef >    m_types;
    ::std::vector< ::std::pair< ::std::string, TypeRef> >   m_assoc;

    PathParams(PathParams&& x) = default;
    PathParams(const PathParams& x);
    PathParams() {}
    PathParams(::std::vector<LifetimeRef> lfts, ::std::vector<TypeRef> tys, ::std::vector<::std::pair<::std::string,TypeRef>> a):
        m_lifetimes(mv$(lfts)),
        m_types(mv$(tys)),
        m_assoc(mv$(a))
    {}

    PathParams& operator=(PathParams&& x) = default;
    PathParams& operator=(const PathParams& x) = delete;

    bool is_empty() const {
        return m_lifetimes.empty() && m_types.empty() && m_assoc.empty();
    }

    Ordering ord(const PathParams& x) const;

    friend ::std::ostream& operator<<(::std::ostream& os, const PathParams& x);
};

class PathNode
{
    ::std::string   m_name;
    PathParams  m_params;
public:
    PathNode() {}
    PathNode(::std::string name, PathParams args = {});
    const ::std::string& name() const { return m_name; }

    const ::AST::PathParams& args() const { return m_params; }
          ::AST::PathParams& args()       { return m_params; }

    Ordering ord(const PathNode& x) const;
    void print_pretty(::std::ostream& os, bool is_type_context) const;

    bool operator==(const PathNode& x) const { return ord(x) == OrdEqual; }
    friend ::std::ostream& operator<<(::std::ostream& os, const PathNode& pn);
};

class Path
{
public:
    TAGGED_UNION(Class, Invalid,
        (Invalid, struct {}),
        (Local, struct {   // Variable / Type param (resolved)
            ::std::string name;
            } ),
        (Relative, struct {    // General relative
            Ident::Hygiene hygiene;
            ::std::vector<PathNode> nodes;
            } ),
        (Self, struct {    // Module-relative
            ::std::vector<PathNode> nodes;
            } ),
        (Super, struct {   // Parent-relative
            unsigned int count; // Number of `super` keywords, must be >= 1
            ::std::vector<PathNode> nodes;
            } ),
        (Absolute, struct {    // Absolute
            ::std::string   crate;
            ::std::vector<PathNode> nodes;
            } ),
        (UFCS, struct {    // Type-relative
            ::std::unique_ptr<TypeRef> type;    // always non-null
            ::std::unique_ptr<Path> trait;   // nullptr = inherent, Invalid = unknown trait
            ::std::vector<PathNode> nodes;
            } )
        );

public:
    Class   m_class;

private:
    PathBinding m_binding;
public:
    virtual ~Path();
    // INVALID
    Path():
        m_class()
    {}
    Path(Path&&) = default;
    Path& operator=(AST::Path&& x) {
        m_class = mv$(x.m_class);
        m_binding = mv$(x.m_binding);
        //DEBUG("Path, " << x);
        return *this;
    }

    Path(const Path& x);
    Path& operator=(const AST::Path&) = delete;

    // ABSOLUTE
    Path(::std::string crate, ::std::vector<PathNode> nodes):
        m_class( Class::make_Absolute({ mv$(crate), mv$(nodes)}) )
    {}

    // UFCS
    struct TagUfcs {};
    Path(TagUfcs, TypeRef type, ::std::vector<PathNode> nodes={});
    Path(TagUfcs, TypeRef type, Path trait, ::std::vector<PathNode> nodes={});

    // VARIABLE
    struct TagLocal {};
    Path(TagLocal, ::std::string name):
        m_class( Class::make_Local({ mv$(name) }) )
    {}
    Path(::std::string name):
        m_class( Class::make_Local({ mv$(name) }) )
    {}

    // RELATIVE
    struct TagRelative {};
    Path(TagRelative, Ident::Hygiene hygiene, ::std::vector<PathNode> nodes):
        m_class( Class::make_Relative({ mv$(hygiene), mv$(nodes) }) )
    {}
    // SELF
    struct TagSelf {};
    Path(TagSelf, ::std::vector<PathNode> nodes):
        m_class( Class::make_Self({ mv$(nodes) }) )
    {}
    // SUPER
    struct TagSuper {};
    Path(TagSuper, unsigned int count, ::std::vector<PathNode> nodes):
        m_class( Class::make_Super({ count, mv$(nodes) }) )
    {}

    //void set_crate(::std::string crate) {
    //    if( m_crate == "" ) {
    //        m_crate = crate;
    //        DEBUG("crate set to " << m_crate);
    //    }
    //}


    Class::Tag class_tag() const {
        return m_class.tag();
    }

    Path operator+(PathNode pn) const {
        Path tmp = Path(*this);
        tmp.nodes().push_back( mv$(pn) );
        return tmp;
    }
    Path operator+(const ::std::string& s) const {
        Path tmp = Path(*this);
        tmp.append(PathNode(s, {}));
        return tmp;
    }
    Path operator+(const Path& x) const {
        return Path(*this) += x;
    }
    Path& operator+=(const Path& x);
    Path& operator+=(PathNode pn) {
        this->nodes().push_back( mv$(pn) );
        return *this;
    }

    void append(PathNode node) {
        assert( !m_class.is_Invalid() );
        //if( m_class.is_Invalid() )
        //    m_class = Class::make_Relative({});
        nodes().push_back( mv$(node) );
        m_binding = PathBinding();
    }

    bool is_trivial() const {
        TU_MATCH_DEF(Class, (m_class), (e),
        (
            return false;
            ),
        (Local,
            return true;
            ),
        (Relative,
            return e.nodes.size() == 1 && e.nodes[0].args().is_empty();
            )
        )
    }

    bool is_valid() const { return !m_class.is_Invalid(); }
    bool is_absolute() const { return m_class.is_Absolute(); }
    bool is_relative() const { return m_class.is_Relative() || m_class.is_Super() || m_class.is_Self(); }

    size_t size() const {
        TU_MATCH(Class, (m_class), (ent),
        (Invalid,  assert(!m_class.is_Invalid()); throw ::std::runtime_error("Path::nodes() on Invalid"); ),
        (Local,    return 1;),
        (Relative, return ent.nodes.size();),
        (Self,     return ent.nodes.size();),
        (Super,    return ent.nodes.size();),
        (Absolute, return ent.nodes.size();),
        (UFCS,     return ent.nodes.size();)
        )
        throw ::std::runtime_error("Path::nodes() fell off");
    }
    //const ::std::string& crate() const { return m_crate; }

    bool is_concrete() const;

    bool is_bound() const { return !m_binding.is_Unbound(); }
    const PathBinding& binding() const { return m_binding; }
    void bind_variable(unsigned int slot);

    ::std::vector<PathNode>& nodes() {
        TU_MATCH(Class, (m_class), (ent),
        (Invalid,  assert(!m_class.is_Invalid()); throw ::std::runtime_error("Path::nodes() on Invalid"); ),
        (Local,    assert(!m_class.is_Local()); throw ::std::runtime_error("Path::nodes() on Local"); ),
        (Relative, return ent.nodes;),
        (Self,     return ent.nodes;),
        (Super,    return ent.nodes;),
        (Absolute, return ent.nodes;),
        (UFCS,     return ent.nodes;)
        )
        throw ::std::runtime_error("Path::nodes() fell off");
    }
    const ::std::vector<PathNode>& nodes() const {
        return ((Path*)this)->nodes();
    }

    PathNode& operator[](int idx) { if(idx>=0) return nodes()[idx]; else return nodes()[size()+idx]; }
    const PathNode& operator[](int idx) const { return (*(Path*)this)[idx]; }

    Ordering ord(const Path& x) const;
    bool operator==(const Path& x) const { return ord(x) == OrdEqual; }
    bool operator!=(const Path& x) const { return ord(x) != OrdEqual; }
    bool operator<(const Path& x) const { return ord(x) != OrdLess; }

    void print_pretty(::std::ostream& os, bool is_type_context, bool is_debug=false) const;
    friend ::std::ostream& operator<<(::std::ostream& os, const Path& path);
private:
    static void resolve_args_nl(::std::vector<PathNode>& nodes, ::std::function<TypeRef(const char*)> fcn);

    void check_param_counts(const GenericParams& params, bool expect_params, PathNode& node);
public:
    void bind_enum_var(const Enum& ent, const ::std::string& name, const ::std::vector<TypeRef>& args={});
    void bind_function(const Function& ent, const ::std::vector<TypeRef>& args={}) {
        (void)args;
        m_binding = PathBinding::make_Function({&ent});
    }

    void bind(::AST::PathBinding pb) {
        m_binding = mv$(pb);
    }
};

}   // namespace AST

#endif