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

struct Ident
{
    class Hygiene
    {
        static unsigned g_next_scope;
        
        ::std::vector<unsigned int> contexts;
        
        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.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(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 `souce` 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;
    ::std::string   name;
    
    Ident(const char* name):
        hygiene(),
        name(name)
    { }
    Ident(::std::string name):
        hygiene(),
        name(::std::move(name))
    { }
    Ident(Hygiene hygiene, ::std::string 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;
    
    ::std::string 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);
};