yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

CopilotMerge NamePool and RootNamePool into a single type (#7797)3485710e9

master
2.1 KiB66 linesraw
1// slang-name.h
2#ifndef SLANG_NAME_H_INCLUDED
3#define SLANG_NAME_H_INCLUDED
4
5// This file defines the `Name` type, used to represent
6// the name of types, variables, etc. in the AST.
7
8#include "../core/slang-basic.h"
9
10namespace Slang
11{
12
13// The `Name` type is used to represent the name of a type, variable, etc.
14//
15// The key benefit of using `Name`s instead of raw strings is that `Name`s
16// can be compared for equality just by testing pointer equality. Names
17// also don't require any memory management; you can just retain an ordinary
18// pointer to one and not deal with reference-counting overhead.
19//
20// In order to provide these benefits, a `Name` can only be created using
21// a `NamePool` that owns the allocations for all the names (so they get
22// cleaned up when the pool is deleted), and which is responsible for
23// ensuring the uniqueness of name objects.
24//
25class Name : public RefObject
26{
27public:
28    // The raw text of the name.
29    //
30    // Note that at some point in the future we might have other categories
31    // of name than "simple" names, and so this might change to a structured
32    // ADT instead of a simple string.
33    String text;
34};
35
36// Get the textual string representation of a name
37// (e.g., so that it can be printed).
38String getText(Name* name);
39
40/// Get the text as unowned string slice
41UnownedStringSlice getUnownedStringSliceText(Name* name);
42
43// Get a name as a C style string, or nullptr if name is nullptr
44const char* getCstr(Name* name);
45
46// A `NamePool` is used to store and look up names.
47// If two systems need to work together with names, and be sure that they
48// get equivalent names for a string like `"Foo"`, then they need to use
49// the same name pool (directly or indirectly).
50//
51struct NamePool
52{
53    // Find or create the `Name` that represents the given `text`.
54    Name* getName(UnownedStringSlice text);
55    Name* getName(String const& text);
56    // Try find the `Name` that represents the given `text`.
57    // If the name does not exist, return nullptr
58    Name* tryGetName(String const& text);
59
60    // The mapping from text strings to the corresponding name.
61    Dictionary<String, RefPtr<Name>> names;
62};
63
64} // namespace Slang
65
66#endif