summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-extractor/identifier-lookup.cpp
blob: c7be75d8287d94778af282cc04cb7d50f136c4c2 (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
#include "identifier-lookup.h"

namespace CppExtract {
using namespace Slang;

/* static */const IdentifierFlags IdentifierLookup::kIdentifierFlags[Index(IdentifierStyle::CountOf)] =
{
    0,              /// None
    0,              /// Identifier
    0,              /// Declare type
    0,              /// Type set
    IdentifierFlag::Keyword,              /// TypeModifier
    IdentifierFlag::Keyword,              /// Keyword

    IdentifierFlag::Keyword | IdentifierFlag::StartScope | IdentifierFlag::ClassLike, /// Class
    IdentifierFlag::Keyword | IdentifierFlag::StartScope | IdentifierFlag::ClassLike, /// Struct
    IdentifierFlag::Keyword | IdentifierFlag::StartScope, /// Namespace
    IdentifierFlag::Keyword | IdentifierFlag::StartScope, /// Enum

    IdentifierFlag::Keyword,                              /// Typedef 

    IdentifierFlag::Keyword,                              /// Access
    IdentifierFlag::Reflection,                           /// Reflected
    IdentifierFlag::Reflection,                           /// Unreflected
};

void IdentifierLookup::set(const UnownedStringSlice& name, IdentifierStyle style)
{
    StringSlicePool::Handle handle;
    if (m_pool.findOrAdd(name, handle))
    {
        // Add the extra flags
        m_styles[Index(handle)] = style;
    }
    else
    {
        Index index = Index(handle);
        SLANG_ASSERT(index == m_styles.getCount());
        m_styles.add(style);
    }
}

void IdentifierLookup::set(const char*const* names, size_t namesCount, IdentifierStyle style)
{
    for (size_t i = 0; i < namesCount; ++i)
    {
        set(UnownedStringSlice(names[i]), style);
    }
}

void IdentifierLookup::set(const Pair* pairs, Index pairsCount)
{
    for (Index i = 0; i < pairsCount; ++i)
    {
        const auto& pair = pairs[i];
        set(UnownedStringSlice(pair.name), pair.style);
    }
}

void IdentifierLookup::initDefault(const UnownedStringSlice& markPrefix)
{
    reset();

    // Some keywords
    {
        const char* names[] = { "virtual", "continue", "if", "case", "break", "catch", "default", "delete", "do", "else", "for", "new", "goto", "return", "switch", "throw", "using", "while", "operator" };
        set(names, SLANG_COUNT_OF(names), IdentifierStyle::Keyword);
    }

    // Type modifier keywords
    {
        const char* names[] = { "const", "volatile" };
        set(names, SLANG_COUNT_OF(names), IdentifierStyle::TypeModifier);
    }

    // Special markers
    {
        const char* names[] = { "PRE_DECLARE", "TYPE_SET", "REFLECTED", "UNREFLECTED" };
        const IdentifierStyle styles[] = { IdentifierStyle::PreDeclare, IdentifierStyle::TypeSet, IdentifierStyle::Reflected, IdentifierStyle::Unreflected };
        SLANG_COMPILE_TIME_ASSERT(SLANG_COUNT_OF(names) == SLANG_COUNT_OF(styles));

        StringBuilder buf;
        for (Index i = 0; i < SLANG_COUNT_OF(names); ++i)
        {
            buf.Clear();
            buf << markPrefix << names[i];
            set(buf.getUnownedSlice(), styles[i]);
        }
    }

    // Keywords which introduce types/scopes
    {
        const Pair pairs[] =
        {
            { "struct", IdentifierStyle::Struct },
            { "class", IdentifierStyle::Class },
            { "namespace", IdentifierStyle::Namespace },
            { "enum", IdentifierStyle::Enum },
            { "typedef", IdentifierStyle::TypeDef },
        };

        set(pairs, SLANG_COUNT_OF(pairs));
    }

    // Keywords that control access
    {
        const char* names[] = { "private", "protected", "public" };
        set(names, SLANG_COUNT_OF(names), IdentifierStyle::Access);
    }
}

} // namespace CppExtract