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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
// slang-ast-dump.h
#ifndef SLANG_AST_BUILDER_H
#define SLANG_AST_BUILDER_H
#include <type_traits>
#include "slang-ast-support-types.h"
#include "slang-ast-all.h"
#include "../core/slang-type-traits.h"
#include "../core/slang-memory-arena.h"
namespace Slang
{
class SharedASTBuilder : public RefObject
{
friend class ASTBuilder;
public:
void registerBuiltinDecl(Decl* decl, BuiltinTypeModifier* modifier);
void registerMagicDecl(Decl* decl, MagicTypeModifier* modifier);
/// Get the string type
Type* getStringType();
/// Get the enum type type
Type* getEnumTypeType();
/// Get the __Dynamic type
Type* getDynamicType();
const ReflectClassInfo* findClassInfo(Name* name);
SyntaxClass<NodeBase> findSyntaxClass(Name* name);
const ReflectClassInfo* findClassInfo(const UnownedStringSlice& slice);
SyntaxClass<NodeBase> findSyntaxClass(const UnownedStringSlice& slice);
// Look up a magic declaration by its name
Decl* findMagicDecl(String const& name);
/// A name pool that can be used for lookup for findClassInfo etc. It is the same pool as the Session.
NamePool* getNamePool() { return m_namePool; }
/// Must be called before used
void init(Session* session);
SharedASTBuilder();
~SharedASTBuilder();
protected:
// State shared between ASTBuilders
Type* m_errorType = nullptr;
Type* m_initializerListType = nullptr;
Type* m_overloadedType = nullptr;
// The following types are created lazily, such that part of their definition
// can be in the standard library
//
// Note(tfoley): These logically belong to `Type`,
// but order-of-declaration stuff makes that tricky
//
// TODO(tfoley): These should really belong to the compilation context!
//
Type* m_stringType = nullptr;
Type* m_enumTypeType = nullptr;
Type* m_dynamicType = nullptr;
Type* m_builtinTypes[Index(BaseType::CountOf)];
Dictionary<String, Decl*> m_magicDecls;
Dictionary<UnownedStringSlice, const ReflectClassInfo*> m_sliceToTypeMap;
Dictionary<Name*, const ReflectClassInfo*> m_nameToTypeMap;
NamePool* m_namePool = nullptr;
// This is a private builder used for these shared types
ASTBuilder* m_astBuilder = nullptr;
Session* m_session = nullptr;
Index m_id = 1;
};
class ASTBuilder : public RefObject
{
friend class SharedASTBuilder;
public:
// For compile time check to see if thing being constructed is an AST type
template <typename T>
struct IsValidType
{
enum
{
Value = IsBaseOf<NodeBase, T>::Value
};
};
/// Create AST types
template <typename T>
T* create() { return _initAndAdd(new (m_arena.allocate(sizeof(T))) T); }
template<typename T, typename P0>
T* create(const P0& p0) { return _initAndAdd(new (m_arena.allocate(sizeof(T))) T(p0)); }
template<typename T, typename P0, typename P1>
T* create(const P0& p0, const P1& p1) { return _initAndAdd(new (m_arena.allocate(sizeof(T))) T(p0, p1));}
NodeBase* createByNodeType(ASTNodeType nodeType);
/// Get the built in types
SLANG_FORCE_INLINE Type* getBoolType() { return m_sharedASTBuilder->m_builtinTypes[Index(BaseType::Bool)]; }
SLANG_FORCE_INLINE Type* getHalfType() { return m_sharedASTBuilder->m_builtinTypes[Index(BaseType::Half)]; }
SLANG_FORCE_INLINE Type* getFloatType() { return m_sharedASTBuilder->m_builtinTypes[Index(BaseType::Float)]; }
SLANG_FORCE_INLINE Type* getDoubleType() { return m_sharedASTBuilder->m_builtinTypes[Index(BaseType::Double)]; }
SLANG_FORCE_INLINE Type* getIntType() { return m_sharedASTBuilder->m_builtinTypes[Index(BaseType::Int)]; }
SLANG_FORCE_INLINE Type* getInt64Type() { return m_sharedASTBuilder->m_builtinTypes[Index(BaseType::Int64)]; }
SLANG_FORCE_INLINE Type* getUIntType() { return m_sharedASTBuilder->m_builtinTypes[Index(BaseType::UInt)]; }
SLANG_FORCE_INLINE Type* getUInt64Type() { return m_sharedASTBuilder->m_builtinTypes[Index(BaseType::UInt64)]; }
SLANG_FORCE_INLINE Type* getVoidType() { return m_sharedASTBuilder->m_builtinTypes[Index(BaseType::Void)]; }
/// Get a builtin type by the BaseType
SLANG_FORCE_INLINE Type* getBuiltinType(BaseType flavor) { return m_sharedASTBuilder->m_builtinTypes[Index(flavor)]; }
Type* getInitializerListType() { return m_sharedASTBuilder->m_initializerListType; }
Type* getOverloadedType() { return m_sharedASTBuilder->m_overloadedType; }
Type* getErrorType() { return m_sharedASTBuilder->m_errorType; }
Type* getStringType() { return m_sharedASTBuilder->getStringType(); }
Type* getEnumTypeType() { return m_sharedASTBuilder->getEnumTypeType(); }
// Construct the type `Ptr<valueType>`, where `Ptr`
// is looked up as a builtin type.
PtrType* getPtrType(Type* valueType);
// Construct the type `Out<valueType>`
OutType* getOutType(Type* valueType);
// Construct the type `InOut<valueType>`
InOutType* getInOutType(Type* valueType);
// Construct the type `Ref<valueType>`
RefType* getRefType(Type* valueType);
// Construct a pointer type like `Ptr<valueType>`, but where
// the actual type name for the pointer type is given by `ptrTypeName`
PtrTypeBase* getPtrType(Type* valueType, char const* ptrTypeName);
// Construct a pointer type like `Ptr<valueType>`, but where
// the generic declaration for the pointer type is `genericDecl`
PtrTypeBase* getPtrType(Type* valueType, GenericDecl* genericDecl);
ArrayExpressionType* getArrayType(Type* elementType, IntVal* elementCount);
VectorExpressionType* getVectorType(Type* elementType, IntVal* elementCount);
DeclRef<Decl> getBuiltinDeclRef(const char* builtinMagicTypeName, ConstArrayView<Val*> genericArgs);
Type* getAndType(Type* left, Type* right);
TypeType* getTypeType(Type* type);
/// Helpers to get type info from the SharedASTBuilder
const ReflectClassInfo* findClassInfo(const UnownedStringSlice& slice) { return m_sharedASTBuilder->findClassInfo(slice); }
SyntaxClass<NodeBase> findSyntaxClass(const UnownedStringSlice& slice) { return m_sharedASTBuilder->findSyntaxClass(slice); }
const ReflectClassInfo* findClassInfo(Name* name) { return m_sharedASTBuilder->findClassInfo(name); }
SyntaxClass<NodeBase> findSyntaxClass(Name* name) { return m_sharedASTBuilder->findSyntaxClass(name); }
MemoryArena& getMemoryArena() { return m_arena; }
/// Get the shared AST builder
SharedASTBuilder* getSharedASTBuilder() { return m_sharedASTBuilder; }
/// Get the global session
Session* getGlobalSession() { return m_sharedASTBuilder->m_session; }
/// Ctor
ASTBuilder(SharedASTBuilder* sharedASTBuilder, const String& name);
/// Dtor
~ASTBuilder();
protected:
// Special default Ctor that can only be used by SharedASTBuilder
ASTBuilder();
template <typename T>
SLANG_FORCE_INLINE T* _initAndAdd(T* node)
{
SLANG_COMPILE_TIME_ASSERT(IsValidType<T>::Value);
node->init(T::kType, this);
// Only add it if it has a dtor that does some work
if (!std::is_trivially_destructible<T>::value)
{
// Keep such that dtor can be run on ASTBuilder being dtored
m_dtorNodes.add(node);
}
return node;
}
String m_name;
Index m_id;
/// List of all nodes that require being dtored when ASTBuilder is dtored
List<NodeBase*> m_dtorNodes;
SharedASTBuilder* m_sharedASTBuilder;
MemoryArena m_arena;
};
} // namespace Slang
#endif
|