summaryrefslogtreecommitdiff
path: root/src/include/ident.hpp
blob: e8cc4e12e13e85e722eeb58f2556c0ec610cf354 (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
/*
 * MRustC - Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * include/ident.hpp
 * - Identifiers with hygiene
 */
#pragma once
#include <vector>
#include <string>
#include <memory>
#include <rc_string.hpp>

struct Ident
{
    struct ModPath
    {
        ::std::vector<RcString> ents;
    };
    class Hygiene
    {
        static unsigned g_next_scope;

        ::std::vector<unsigned int> contexts;
        ::std::shared_ptr<ModPath> search_module;

        Hygiene(unsigned int index):
            contexts({index})
        {}
    public:
        Hygiene():
            contexts({})
        {}

        static Hygiene new_scope()
        {
            return Hygiene(++g_next_scope);
        }
        static Hygiene new_scope_chained(const Hygiene& parent)
        {
            Hygiene rv;
            rv.search_module = parent.search_module;
            rv.contexts.reserve( parent.contexts.size() + 1 );
            rv.contexts.insert( rv.contexts.begin(),  parent.contexts.begin(), parent.contexts.end() );
            rv.contexts.push_back( ++g_next_scope );
            return rv;
        }
        Hygiene get_parent() const
        {
            //assert(this->contexts.size() > 1);
            Hygiene rv;
            rv.contexts.insert(rv.contexts.begin(),  this->contexts.begin(), this->contexts.end()-1);
            return rv;
        }

        bool has_mod_path() const {
            return this->search_module != 0;
        }
        const ModPath& mod_path() const {
            return *this->search_module;
        }
        void set_mod_path(ModPath p) {
            this->search_module.reset( new ModPath(::std::move(p)) );
        }

        Hygiene(Hygiene&& x) = default;
        Hygiene(const Hygiene& x) = default;
        Hygiene& operator=(Hygiene&& x) = default;
        Hygiene& operator=(const Hygiene& x) = default;

        // Returns true if an ident with hygine `source` can see an ident with this hygine
        bool is_visible(const Hygiene& source) const;
        //bool operator==(const Hygiene& x) const { return scope_index == x.scope_index; }
        //bool operator!=(const Hygiene& x) const { return scope_index != x.scope_index; }

        friend ::std::ostream& operator<<(::std::ostream& os, const Hygiene& v);
    };

    Hygiene hygiene;
    RcString   name;

    Ident(const char* name):
        hygiene(),
        name(name)
    { }
    Ident(RcString name):
        hygiene(),
        name(::std::move(name))
    { }
    Ident(Hygiene hygiene, RcString name):
        hygiene(::std::move(hygiene)), name(::std::move(name))
    { }

    Ident(Ident&& x) = default;
    Ident(const Ident& x) = default;
    Ident& operator=(Ident&& x) = default;
    Ident& operator=(const Ident& x) = default;

    RcString into_string() {
        return ::std::move(name);
    }

    bool operator==(const char* s) const {
        return this->name == s;
    }

    bool operator==(const Ident& x) const {
        if( this->name != x.name )
            return false;
        //if( this->hygine.indexes != x.hygine.indexes )
        //    return false;
        return true;
    }
    bool operator!=(const Ident& x) const {
        return !(*this == x);
    }
    bool operator<(const Ident& x) const;

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