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
|
// slang-name.h
#ifndef SLANG_NAME_H_INCLUDED
#define SLANG_NAME_H_INCLUDED
// This file defines the `Name` type, used to represent
// the name of types, variables, etc. in the AST.
#include "../core/slang-basic.h"
namespace Slang
{
// The `Name` type is used to represent the name of a type, variable, etc.
//
// The key benefit of using `Name`s instead of raw strings is that `Name`s
// can be compared for equality just by testing pointer equality. Names
// also don't require any memory management; you can just retain an ordinary
// pointer to one and not deal with reference-counting overhead.
//
// In order to provide these benefits, a `Name` can only be created using
// a `NamePool` that owns the allocations for all the names (so they get
// cleaned up when the pool is deleted), and which is responsible for
// ensuring the uniqueness of name objects.
//
class Name : public RefObject
{
public:
// The raw text of the name.
//
// Note that at some point in the future we might have other categories
// of name than "simple" names, and so this might change to a structured
// ADT instead of a simple string.
String text;
};
// Get the textual string representation of a name
// (e.g., so that it can be printed).
String getText(Name* name);
/// Get the text as unowned string slice
UnownedStringSlice getUnownedStringSliceText(Name* name);
// Get a name as a C style string, or nullptr if name is nullptr
const char* getCstr(Name* name);
// A `NamePool` is used to store and look up names.
// If two systems need to work together with names, and be sure that they
// get equivalent names for a string like `"Foo"`, then they need to use
// the same name pool (directly or indirectly).
//
struct NamePool
{
// Find or create the `Name` that represents the given `text`.
Name* getName(UnownedStringSlice text);
Name* getName(String const& text);
// Try find the `Name` that represents the given `text`.
// If the name does not exist, return nullptr
Name* tryGetName(String const& text);
// The mapping from text strings to the corresponding name.
Dictionary<String, RefPtr<Name>> names;
};
} // namespace Slang
#endif
|