UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Definition.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2// uLang Compiler Public API
3
4#pragma once
5
12
13#define UE_API VERSECOMPILER_API
14
15namespace uLang
16{
17class CScope;
18class CLogicalScope;
19class CNominalType;
20
21#define VERSE_ENUM_DEFINITION_KINDS(v) \
22 v(Class, "class") \
23 v(Data, "data") \
24 v(Enumeration, "enumeration") \
25 v(Enumerator, "enumerator") \
26 v(Function, "function") \
27 v(Interface, "interface") \
28 v(Module, "module") \
29 v(ModuleAlias, "module alias") \
30 v(TypeAlias, "type alias") \
31 v(TypeVariable, "type variable")
32
35{
36 enum class EType : uint8_t
37 {
38 Local, // i.e. `(local:)`, which is limited to a function's enclosing scopes.
39 NominalType, // This encompasses any nominal type or module.
40 LogicalScope, // This encompasses any parameterized type.
41 Unknown // Sentinel value for invalid/unresolved qualifiers.
42 };
43
45 union {
48 } U;
49
51
53
55 {
57 {
58 return U._NominalType;
59 }
60 return nullptr;
61 }
64 {
65 return U._LogicalScope;
66 }
67 return nullptr;
68 }
70 {
71 return { .U = {._NominalType = NominalType},._Type = EType::NominalType };
72 };
74 {
75 return { .U = {._LogicalScope = LogicalScope}, ._Type = EType::LogicalScope };
76 };
78 {
79 return { .U = {._NominalType = nullptr}, ._Type = EType::Local };
80 };
82 {
83 return { .U = {._NominalType = nullptr}, ._Type = EType::Unknown};
84 };
85
86 bool IsUnspecified() const
87 {
88 return _Type == EType::Unknown;
89 }
90
91 bool IsLocal() const
92 {
93 return _Type == EType::Local;
94 }
95
96 bool operator==(const SQualifier& Other) const
97 {
98 // Doesn't matter if we compare _NominalType or _LogicalType, it's only a pointer anyway.
99 return _Type == Other._Type && U._NominalType == Other.U._NominalType;
100 }
101
102 bool operator!=(const SQualifier& Other) const
103 {
104 return !(*this == Other);
105 }
106};
107
111template<class AstNodeType>
113{
114public:
115 AstNodeType* GetAstNode() const { return _AstNode; }
116 AstNodeType* GetIrNode(bool bForce = false) const { return (bForce || _IrNode) ? _IrNode : _AstNode; }
117 void SetAstNode(AstNodeType* AstNode) { ULANG_ASSERTF(!_IrNode, "Called AST function when IR available"); _AstNode = AstNode; }
118 void SetIrNode(AstNodeType* IrNode) { _IrNode = IrNode; }
119
120private:
121 AstNodeType* _AstNode{ nullptr };
122 AstNodeType* _IrNode{ nullptr };
123};
124
126
130class CDefinition : public CAttributable, public CNamed, public TAstNodeRef<CExpressionBase>, public CSharedMix
131{
132public:
133
134 enum class EKind : uint8_t
135 {
136 #define VISIT_KIND(Name, ...) Name,
138 #undef VISIT_KIND
139 };
140
142
143 // An integer that represents the order the definition occurred in the parent scope:
144 // The absolute value shouldn't be used, but if this value is greater than another definition
145 // from the same scope, this definition occurred after the other.
147
148 // If the definition overrides via an explicit qualifier, this is the qualifier type
150
152
154
155 // If this definition has the given visit stamp, return false.
156 // Otherwise, mark this definition *and all definitions it overrides* with the visit stamp and
157 // return true.
158 // Use CScope::GenerateNewVisitStamp to get a new visit stamp.
160 {
161 if (_PrototypeDefinition->_LastOverridingVisitStamp == VisitStamp)
162 {
163 return false;
164 }
165
166 // Mark this definition and the definitions overridden by it as visited.
167 for (const CDefinition* OverriddenDefinition = _PrototypeDefinition;
170 {
171 OverriddenDefinition->GetPrototypeDefinition()->_LastOverridingVisitStamp = VisitStamp;
172 }
173
174 // TODO? Should this one also use _PrototypeDefinition-> instead of this-> as above?
175 if (_ConstrainedDefinition)
176 {
177 _ConstrainedDefinition->GetPrototypeDefinition()->_LastOverridingVisitStamp = VisitStamp;
178 }
179
180 return true;
181 }
182
183 // Casts
184 EKind GetKind() const { return _Kind; }
185
186 template<typename TDefinition>
188
189 template<typename TDefinition>
190 TDefinition const& AsChecked() const;
191
192 template<typename TDefinition>
193 bool IsA() const { return _Kind == TDefinition::StaticDefinitionKind; }
194
195 template<typename TDefinition>
196 TDefinition* AsNullable() { return IsA<TDefinition>() ? static_cast<TDefinition*>(this) : nullptr; }
197
198 template<typename TDefinition>
199 TDefinition const* AsNullable() const { return IsA<TDefinition>() ? static_cast<TDefinition const*>(this) : nullptr; }
200
201 // Accessors
203 {
204 return _OverriddenDefinition;
205 }
207 {
209 while (BaseOverriddenDefinition->GetOverriddenDefinition() != nullptr)
210 {
212 }
214 }
215
216 // As above but will stop before interface.
218
219 const CDefinition* GetPrototypeDefinition() const { return _PrototypeDefinition; }
220
222 {
223 ULANG_ASSERTF(_PrototypeDefinition == this, "Setting constrained definition on instantiated definition");
224 _ConstrainedDefinition = &ConstrainedDefinition;
225 }
227 {
228 return _PrototypeDefinition->_ConstrainedDefinition;
229 }
230
231 CExpressionBase* GetAstNode() const { return _PrototypeDefinition->CAstNodeRef::GetAstNode(); }
232 CExpressionBase* GetIrNode(bool bForce = false) const { return _PrototypeDefinition->CAstNodeRef::GetIrNode(bForce); }
233
235 {
236 ULANG_ASSERTF(_PrototypeDefinition == this, "Setting access level on instantiated definition");
237 _AccessLevel = AccessLevel;
238 }
239 // AccessLevel declared on this specific definition.
240 const TOptional<SAccessLevel>& SelfAccessLevel() const { return _PrototypeDefinition->_AccessLevel; }
241 // This is the true AccessLevel of this definition. This is what you want to use most of the time when checking if
242 // it's accessible.
244
245 // Returns whether this is a member of some instantiated type like a class or interface.
246 UE_API bool IsInstanceMember() const;
247
248 // Returns whether this definition has the deprecated attribute.
249 UE_API bool IsDeprecated() const;
250
251 // Returns whether this definition has the experimental attribute.
252 UE_API bool IsExperimental() const;
253
254 // Returns whether this is a final field of an inheritable type like a class or interface.
255 UE_API bool IsFinal() const;
256
257 // If the definition is native, returns the native specifier expression.
259
260 // Returns whether this is a native definition.
261 UE_API bool IsNative() const;
262
263 // Returns whether this is a built-in definition.
264 UE_API bool IsBuiltIn() const;
265
266 // If this definition has a corresponding scope, yield it.
267 virtual const CLogicalScope* DefinitionAsLogicalScopeNullable() const { return nullptr; }
268
269 // These classes of functions are helpers to get to the scope of where
270 // the definition or definition's var access scope is defined. In Verse,
271 // access scopes are inherited when not explicitly defined. Note, this isn't
272 // the same as saying the same specifier is passed along the inheritance chain.
273 //
274 // For example:
275 // M1<public> := module {
276 // Base<public> := class {
277 // Field<internal>:int = 42
278 // }
279 // M2<public> := module:
280 // Child<public> := class(Base) {
281 // Field<override>:int = 50
282 // }
283 // }
284 // M2.Child's Field property is still accessible as internal to M1, not internal to M2.
289
290 UE_API bool IsAccessibleFrom(const CScope&) const;
291
292 virtual bool IsPersistenceCompatConstraint() const = 0;
293
299 {
300 _OverriddenDefinition = OverriddenDefinition;
301 }
302
303 // Find a definition that corresponds to the nearest enclosing scope with a corresponding definition.
304 // If there is none, returns nullptr.
306
308 bool IsExplicitlyLocallyQualified() const;
309
311 bool IsLocallyQualified() const;
312
315
316 UE_API void SetName(const CSymbol& NewName);
317
318 TOptional<uint64_t> GetCombinedAvailableVersion() const { return _CombinedAvailableAttributeVersion; }
319 void SetCombinedAvailableVersion(uint64_t InAvailableVersion) const { _CombinedAvailableAttributeVersion = InAvailableVersion; }
320
321protected:
322
323 // Setters that should be wrapped by a subclass to restrict the parameter type.
325 {
326 ULANG_ASSERTF(PrototypeDefinition.GetPrototypeDefinition() == &PrototypeDefinition, "Setting instantiated definition as prototype");
327 _PrototypeDefinition = &PrototypeDefinition;
328 }
330 {
331 ULANG_ASSERTF(_PrototypeDefinition == this, "Setting IR node on instantiated definition");
333 }
335 {
336 ULANG_ASSERTF(_PrototypeDefinition == this, "Setting IR node on instantiated definition");
338 }
339
340private:
341 const EKind _Kind;
342
343 // The stamp for the last time an overriding definition was visited.
344 mutable VisitStampType _LastOverridingVisitStamp{};
345
346 // If the definition was instantiated from a polymorphic prototype, this will reference the
347 // prototype definition. Otherwise, it will reference this definition.
348 const CDefinition* _PrototypeDefinition{ this };
349
350 // If the definition overrides an inherited definition, this will reference the inherited definition.
351 const CDefinition* _OverriddenDefinition{ nullptr };
352
353 // If this definition is a constraint on some other definition, this will reference the constrained definition.
354 const CDefinition* _ConstrainedDefinition{ nullptr };
355
356 // The definition's access level.
357 TOptional<SAccessLevel> _AccessLevel;
358
359 // Set on first use combined @available version for this definition and all enclosing scope definitions
360 mutable TOptional<uint64_t> _CombinedAvailableAttributeVersion;
361};
362
363VERSECOMPILER_API CUTF8String GetQualifiedNameString(const CDefinition& Definition);
364VERSECOMPILER_API CUTF8String GetCrcNameString(const CDefinition& Definition);
366
367template<typename TDefinition>
369{
370 ULANG_ASSERTF(IsA<TDefinition>(), "Failed to cast %s to %s.", DefinitionKindAsCString(_Kind), DefinitionKindAsCString(TDefinition::StaticDefinitionKind));
371 return *static_cast<TDefinition*>(this);
372}
373
374template<typename TDefinition>
376{
377 ULANG_ASSERTF(IsA<TDefinition>(), "Failed to cast %s to %s.", DefinitionKindAsCString(_Kind), DefinitionKindAsCString(TDefinition::StaticDefinitionKind));
378 return *static_cast<const TDefinition*>(this);
379}
380
381} // namespace uLang
382
383#undef UE_API
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define VERSE_ENUM_DEFINITION_KINDS(v)
Definition Definition.h:21
#define UE_API
Definition SColorGradingComponentViewer.h:12
#define ULANG_ASSERTF(expr, format,...)
Definition Common.h:290
Base class for everything that can have attributes attached to it (classes, expressions,...
Definition Attributable.h:51
Definition Definition.h:131
void SetCombinedAvailableVersion(uint64_t InAvailableVersion) const
Definition Definition.h:319
TDefinition * AsNullable()
Definition Definition.h:196
bool IsA() const
Definition Definition.h:193
UE_API SAccessLevel DerivedAccessLevel() const
Definition Definition.cpp:67
const CDefinition * GetPrototypeDefinition() const
Definition Definition.h:219
TOptional< uint64_t > GetCombinedAvailableVersion() const
Definition Definition.h:318
void SetOverriddenDefinition(const CDefinition *OverriddenDefinition)
Definition Definition.h:298
UE_API bool IsNative() const
Definition Definition.cpp:106
const CDefinition & GetDefinitionAccessibilityRoot() const
Definition Definition.h:285
SQualifier _Qualifier
Definition Definition.h:149
UE_API CDefinition(EKind Kind, CScope &EnclosingScope, const CSymbol &Name)
Definition Definition.cpp:39
TDefinition const & AsChecked() const
UE_API void SetName(const CSymbol &NewName)
Definition Definition.cpp:185
void SetConstrainedDefinition(const CDefinition &ConstrainedDefinition)
Definition Definition.h:221
void SetAstNode(CExpressionBase *AstNode)
Definition Definition.h:329
UE_API bool IsBuiltIn() const
Definition Definition.cpp:111
CScope & _EnclosingScope
Definition Definition.h:141
UE_API bool IsFinal() const
Definition Definition.cpp:96
bool IsExplicitlyLocallyQualified() const
If the definition is explicitly qualified with the (local:) identifier.
Definition Definition.cpp:162
UE_API bool IsDeprecated() const
Definition Definition.cpp:86
const CDefinition & GetBaseOverriddenDefinition() const
Definition Definition.h:206
UE_API bool IsExperimental() const
Definition Definition.cpp:91
const int32_t _ParentScopeOrdinal
Definition Definition.h:146
CExpressionBase * GetAstNode() const
Definition Definition.h:231
UE_API const CExpressionBase * GetNativeSpecifierExpression() const
Definition Definition.cpp:101
EKind
Definition Definition.h:135
TDefinition const * AsNullable() const
Definition Definition.h:199
UE_API SQualifier GetImplicitQualifier() const
Determines the qualifier for this definition, even if not explicitly specified (from the original sou...
Definition Definition.cpp:179
void SetAccessLevel(const TOptional< SAccessLevel > &AccessLevel)
Definition Definition.h:234
void SetIrNode(CExpressionBase *IrNode)
Definition Definition.h:334
virtual bool IsPersistenceCompatConstraint() const =0
bool TryMarkOverriddenAndConstrainedDefinitionsVisited(VisitStampType VisitStamp) const
Definition Definition.h:159
bool IsLocallyQualified() const
If the definition is implicitly local by virtue of being a definition within a function body/explicit...
Definition Definition.cpp:167
UE_API bool IsInstanceMember() const
Definition Definition.cpp:80
const TOptional< SAccessLevel > & SelfAccessLevel() const
Definition Definition.h:240
void SetPrototypeDefinition(const CDefinition &PrototypeDefinition)
Definition Definition.h:324
UE_API ~CDefinition()
Definition Definition.cpp:49
CExpressionBase * GetIrNode(bool bForce=false) const
Definition Definition.h:232
UE_API const CDefinition * GetEnclosingDefinition() const
Definition Definition.cpp:150
virtual const CLogicalScope * DefinitionAsLogicalScopeNullable() const
Definition Definition.h:267
EKind GetKind() const
Definition Definition.h:184
const CDefinition * GetOverriddenDefinition() const
Definition Definition.h:202
void SetOverriddenDefinition(const CDefinition &OverriddenDefinition)
Definition Definition.h:294
UE_API bool IsAccessibleFrom(const CScope &) const
Definition Definition.cpp:144
TDefinition & AsChecked()
Definition Definition.h:368
const CDefinition * GetConstrainedDefinition() const
Definition Definition.h:226
UE_API const CDefinition & GetBaseClassOverriddenDefinition() const
Definition Definition.cpp:57
Definition Expression.h:387
Definition SemanticScope.h:242
Definition Named.h:15
Class defining instance and class objects.
Definition SemanticTypes.h:608
Definition SemanticScope.h:73
Definition SharedPointer.h:28
Symbol representing a text string with an associated id.
Definition Symbol.h:98
Definition Definition.h:113
void SetIrNode(AstNodeType *IrNode)
Definition Definition.h:118
AstNodeType * GetAstNode() const
Definition Definition.h:115
void SetAstNode(AstNodeType *AstNode)
Definition Definition.h:117
AstNodeType * GetIrNode(bool bForce=false) const
Definition Definition.h:116
Definition VVMEngineEnvironment.h:23
uint32_t VisitStampType
Used to mark scopes already visited during a search.
Definition VisitStamp.h:11
uLang::TUTF8String< CHeapRawAllocator > CUTF8String
A string allocated on the heap.
Definition UTF8String.h:269
const char * DefinitionKindAsCString(CDefinition::EKind Kind)
Definition Definition.cpp:133
CUTF8String GetQualifiedNameString(const CDefinition &Definition)
Definition Definition.cpp:116
CUTF8String GetCrcNameString(const CDefinition &Definition)
Definition Definition.cpp:121
Definition Optional.h:131
Definition AccessLevel.h:15
Information about a given qualifier.
Definition Definition.h:35
const CNominalType * GetNominalType() const
Definition Definition.h:54
const CNominalType * _NominalType
Definition Definition.h:46
EType _Type
Definition Definition.h:50
const CLogicalScope * _LogicalScope
Definition Definition.h:47
static SQualifier Local()
Definition Definition.h:77
bool operator==(const SQualifier &Other) const
Definition Definition.h:96
union uLang::SQualifier::@2416 U
The definition that the qualifier is referring to.
bool IsUnspecified() const
Definition Definition.h:86
SQualifier GetConstrainedQualifier() const
Definition Definition.cpp:12
static SQualifier LogicalScope(const CLogicalScope *LogicalScope)
Definition Definition.h:73
const CLogicalScope * GetLogicalScope() const
Definition Definition.h:62
static SQualifier Unknown()
Definition Definition.h:81
bool operator!=(const SQualifier &Other) const
Definition Definition.h:102
static SQualifier NominalType(const CNominalType *NominalType)
Definition Definition.h:69
EType
Definition Definition.h:37
bool IsLocal() const
Definition Definition.h:91
Definition Optional.h:23