summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-extractor/identifier-lookup.h
blob: 3cee909ef0ecfea5e64a4376d984f0febd5c48a7 (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
#ifndef CPP_EXTRACT_IDENTIFIER_LOOKUP_H
#define CPP_EXTRACT_IDENTIFIER_LOOKUP_H

#include "diagnostics.h"

namespace CppExtract {
using namespace Slang;

enum class IdentifierStyle
{
    None,               ///< It's not an identifier

    Identifier,         ///< Just an identifier

    PreDeclare,        ///< Declare a type (not visible in C++ code)
    TypeSet,            ///< TypeSet

    TypeModifier,       ///< const, volatile etc
    Keyword,            ///< A keyword C/C++ keyword that is not another type

    Class,              ///< class
    Struct,             ///< struct
    Namespace,          ///< namespace
    Enum,               ///< enum

    TypeDef,            ///< typedef

    Access,             ///< public, protected, private

    Reflected,
    Unreflected,

    CountOf,
};

typedef uint32_t IdentifierFlags;
struct IdentifierFlag
{
    enum Enum : IdentifierFlags
    {
        StartScope = 0x1,          ///< namespace, struct or class
        ClassLike = 0x2,          ///< Struct or class
        Keyword = 0x4,
        Reflection = 0x8,
    };
};


class IdentifierLookup
{
public:

    struct Pair
    {
        const char* name;
        IdentifierStyle style;
    };

    IdentifierStyle get(const UnownedStringSlice& slice) const
    {
        Index index = m_pool.findIndex(slice);
        return (index >= 0) ? m_styles[index] : IdentifierStyle::None;
    }

    void set(const char* name, IdentifierStyle style)
    {
        set(UnownedStringSlice(name), style);
    }

    void set(const UnownedStringSlice& name, IdentifierStyle style);

    void set(const char*const* names, size_t namesCount, IdentifierStyle style);

    void set(const Pair* pairs, Index pairsCount);

    void reset()
    {
        m_styles.clear();
        m_pool.clear();
    }

    void initDefault(const UnownedStringSlice& markPrefix);

    IdentifierLookup() :
        m_pool(StringSlicePool::Style::Empty)
    {
        SLANG_ASSERT(m_pool.getSlicesCount() == 0);
    }

    static const IdentifierFlags kIdentifierFlags[Index(IdentifierStyle::CountOf)];

protected:
    List<IdentifierStyle> m_styles;
    StringSlicePool m_pool;
};


SLANG_FORCE_INLINE IdentifierFlags getFlags(IdentifierStyle style)
{
    return IdentifierLookup::kIdentifierFlags[Index(style)];
}

SLANG_FORCE_INLINE bool hasFlag(IdentifierStyle style, IdentifierFlag::Enum flag)
{
    return (getFlags(style) & flag) != 0;
}

} // CppExtract

#endif