-
-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathSymbolVisitor.cs
More file actions
119 lines (105 loc) · 4.69 KB
/
SymbolVisitor.cs
File metadata and controls
119 lines (105 loc) · 4.69 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Immutable;
namespace Silk.NET.SilkTouch.Symbols;
/// <summary>
/// Implements a base visitor to extend when rewriting or otherwise searching through a <see cref="Symbol"/>
/// </summary>
public abstract class SymbolVisitor
{
/// <summary>
/// Visit a <see cref="Symbol"/>. This will call the appropriate method based on the actual type of the <paramref name="symbol"/>
/// </summary>
/// <param name="symbol">The symbol to visit</param>
/// <returns>The rewritten symbol. May be equal to the original symbol of no rewriting has taken place.</returns>
public virtual Symbol Visit(Symbol symbol)
{
if (symbol is TypeSymbol ts) return VisitType(ts);
if (symbol is MemberSymbol ms) return VisitMember(ms);
if (symbol is NamespaceSymbol ns) return VisitNamespace(ns);
if (symbol is IdentifierSymbol @is) return VisitIdentifier(@is);
return ThrowUnknownSymbol<Symbol>(symbol);
}
/// <summary>
/// Visit a <see cref="MemberSymbol"/>. This will call the appropriate method based on the actual type of the <paramref name="memberSymbol"/>
/// </summary>
/// <param name="memberSymbol">The member symbol to visit</param>
/// <returns>The rewritten symbol</returns>
/// <seealso cref="VisitField"/>
protected virtual MemberSymbol VisitMember(MemberSymbol memberSymbol)
{
if (memberSymbol is FieldSymbol fs) return VisitField(fs);
return ThrowUnknownSymbol<MemberSymbol>(memberSymbol);
}
/// <summary>
/// Visit a <see cref="FieldSymbol"/>. Will call the appropriate methods to visit the different parts of the field.
/// </summary>
/// <param name="fieldSymbol">The field symbol to visit</param>
/// <returns>The rewritten symbol</returns>
/// <remarks>
/// The order in which the parts of the struct are visited is kept as an implementation detail. Do not rely on this order.
/// </remarks>
protected virtual FieldSymbol VisitField(FieldSymbol fieldSymbol)
{
return new FieldSymbol(VisitType(fieldSymbol.Type), VisitIdentifier(fieldSymbol.Identifier));
}
/// <summary>
/// Visit a <see cref="TypeSymbol"/>. This will call the appropriate method based on the actual type of the <paramref name="typeSymbol"/>
/// </summary>
/// <param name="typeSymbol">The type symbol to visit</param>
/// <returns>The rewritten symbol</returns>
/// <seealso cref="VisitStruct"/>
protected virtual TypeSymbol VisitType(TypeSymbol typeSymbol)
{
if (typeSymbol is StructSymbol @struct) return VisitStruct(@struct);
return ThrowUnknownSymbol<TypeSymbol>(typeSymbol);
}
/// <summary>
/// Visit a <see cref="StructSymbol"/>. Will call the appropriate methods to visit the different parts of the struct.
/// </summary>
/// <param name="structSymbol">The struct symbol to visit</param>
/// <returns>The rewritten symbol</returns>
/// <seealso cref="VisitType"/>
/// <remarks>
/// The order in which the parts of the struct are visited is kept as an implementation detail. Do not rely on this order.
/// </remarks>
protected virtual StructSymbol VisitStruct(StructSymbol structSymbol)
{
return new StructSymbol
(
VisitIdentifier(structSymbol.Identifier),
new StructLayout
(
structSymbol.Layout.Entries.Select
(x => new LayoutEntry(VisitMember(x.Member), x.ByteOffset))
.ToImmutableArray()
)
);
}
/// <summary>
/// Visit an <see cref="IdentifierSymbol"/>.
/// </summary>
/// <param name="identifierSymbol">The Identifier to Visit</param>
/// <returns>The rewritten symbol</returns>
protected virtual IdentifierSymbol VisitIdentifier(IdentifierSymbol identifierSymbol)
{
return identifierSymbol;
}
/// <summary>
/// Visit a <see cref="NamespaceSymbol"/>.
/// </summary>
/// <param name="namespaceSymbol">The Namespace to Visit.</param>
/// <returns>The rewritten symbol</returns>
protected virtual NamespaceSymbol VisitNamespace(NamespaceSymbol namespaceSymbol)
{
return namespaceSymbol with
{
Identifier = VisitIdentifier(namespaceSymbol.Identifier),
Types = namespaceSymbol.Types.Select(VisitType).ToImmutableArray()
};
}
private static T ThrowUnknownSymbol<T>(Symbol symbol)
{
throw new NotImplementedException($"Unknown symbol of type {symbol.GetType().FullName} subclass of {typeof(T).Name}");
}
}