summaryrefslogtreecommitdiff
path: root/src/ast/generics.hpp
blob: 288de0a337bb9b1830f1fcad36ef7483d2fec116 (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
#pragma once

#include <string>
#include "../types.hpp"

namespace AST {


class TypeParam:
    public Serialisable
{
    ::std::string   m_name;
    TypeRef m_default;
public:
    TypeParam(): m_name("") {}
    TypeParam(::std::string name):
        m_name( ::std::move(name) )
    {}
    void setDefault(TypeRef type) {
        assert(m_default.is_wildcard());
        m_default = ::std::move(type);
    }
    
    const ::std::string&    name() const { return m_name; }
    const TypeRef& get_default() const { return m_default; }
    
    TypeRef& get_default() { return m_default; }
    
    friend ::std::ostream& operator<<(::std::ostream& os, const TypeParam& tp);
    SERIALISABLE_PROTOTYPES();
};

TAGGED_UNION_EX( GenericBound, (: public Serialisable), Lifetime,
    (
    // Lifetime bound: 'test must be valid for 'bound
    (Lifetime, struct {
        ::std::string   test;
        ::std::string   bound;
        }),
    // Type lifetime bound
    (TypeLifetime, struct {
        TypeRef type;
        ::std::string   bound;
        }),
    // Standard trait bound: "Type: [for<'a>] Trait"
    (IsTrait, struct {
        TypeRef type;
        ::std::vector< ::std::string>   hrls; // Higher-ranked lifetimes
        AST::Path   trait;
        }),
    // Removed trait bound: "Type: ?Trait"
    (MaybeTrait, struct {
        TypeRef type;
        AST::Path   trait;
        }),
    // Negative trait bound: "Type: !Trait"
    (NotTrait, struct {
        TypeRef type;
        AST::Path   trait;
        }),
    // Type equality: "Type = Replacement"
    (Equality, struct {
        TypeRef type;
        TypeRef replacement;
        })
    ),
    
    (), (),
    (
    public:
        SERIALISABLE_PROTOTYPES();
        
        GenericBound clone() const {
            TU_MATCH(GenericBound, ( (*this) ), (ent),
            (Lifetime,     return make_Lifetime({ent.test, ent.bound});     ),
            (TypeLifetime, return make_TypeLifetime({ent.type, ent.bound}); ),
            (IsTrait,    return make_IsTrait({ent.type, ent.hrls, ent.trait}); ),
            (MaybeTrait, return make_MaybeTrait({ent.type, ent.trait}); ),
            (NotTrait,   return make_NotTrait({ent.type, ent.trait}); ),
            (Equality,   return make_Equality({ent.type, ent.replacement}); )
            )
            return GenericBound();
        }
        )
    );

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

class GenericParams:
    public Serialisable
{
    ::std::vector<TypeParam>    m_type_params;
    ::std::vector< ::std::string > m_lifetime_params;
    ::std::vector<GenericBound>    m_bounds;
public:
    GenericParams() {}
    GenericParams(GenericParams&& x) noexcept:
        m_type_params( mv$(x.m_type_params) ),
        m_lifetime_params( mv$(x.m_lifetime_params) ),
        m_bounds( mv$(x.m_bounds) )
    {}
    GenericParams& operator=(GenericParams&& x) {
        m_type_params = mv$(x.m_type_params);
        m_lifetime_params = mv$(x.m_lifetime_params);
        m_bounds = mv$(x.m_bounds);
        return *this;
    }
    GenericParams(const GenericParams& x):
        m_type_params(x.m_type_params),
        m_lifetime_params(x.m_lifetime_params),
        m_bounds()
    {
        m_bounds.reserve( x.m_bounds.size() );
        for(auto& e: x.m_bounds)
            m_bounds.push_back( e.clone() );
    }
    
    const ::std::vector<TypeParam>& ty_params() const { return m_type_params; }
    const ::std::vector< ::std::string>&    lft_params() const { return m_lifetime_params; }
    const ::std::vector<GenericBound>& bounds() const { return m_bounds; }
    ::std::vector<TypeParam>& ty_params() { return m_type_params; }
    ::std::vector<GenericBound>& bounds() { return m_bounds; }
    
    void add_ty_param(TypeParam param) { m_type_params.push_back( ::std::move(param) ); }
    void add_lft_param(::std::string name) { m_lifetime_params.push_back( ::std::move(name) ); }
    void add_bound(GenericBound bound) {
        m_bounds.push_back( ::std::move(bound) );
    }
    
    int find_name(const char* name) const;
    bool check_params(Crate& crate, const ::std::vector<TypeRef>& types) const;
    bool check_params(Crate& crate, ::std::vector<TypeRef>& types, bool allow_infer) const;
    
    friend ::std::ostream& operator<<(::std::ostream& os, const GenericParams& tp);
    SERIALISABLE_PROTOTYPES();
};


}