-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeModelBuilder.cs
More file actions
137 lines (114 loc) · 4.89 KB
/
CodeModelBuilder.cs
File metadata and controls
137 lines (114 loc) · 4.89 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using MetaPrograms.Members;
using Microsoft.CodeAnalysis;
namespace MetaPrograms
{
public class CodeModelBuilder
{
private readonly ImperativeCodeModel _codeModel;
private readonly ImmutableArray<Compilation> _compilations;
private readonly ImmutableDictionary<SyntaxTree, Compilation> _compilationBySyntaxTree;
public CodeModelBuilder(Compilation mainCompilation)
: this(new[] { mainCompilation })
{
}
public CodeModelBuilder(IEnumerable<Compilation> knownCompilations)
{
_codeModel = new ImperativeCodeModel();
_compilations = WithReferencedCompilations(knownCompilations.ToArray()).ToImmutableArray();
_compilationBySyntaxTree = _compilations
.SelectMany(
compilation => compilation.SyntaxTrees
.Select(tree => new KeyValuePair<SyntaxTree, Compilation>(tree, compilation)))
.ToImmutableDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
//public void BindMember<TBinding>(MemberRef<AbstractMember> member, TBinding binding)
// where TBinding : class
//{
// member.Bindings.Add(binding);
// _memberByBinding[binding] = member;
//}
public void RegisterMember(AbstractMember member, bool isTopLevel)
{
_codeModel.Add(member, isTopLevel);
}
//public bool TryGetMember<TMember, TBinding>(TBinding binding, out TMember member)
// where TMember : AbstractMember
// where TBinding : class
//{
// if (_memberByBinding.TryGetValue(binding, out AbstractMember abstractMember))
// {
// member = (TMember)abstractMember;
// return true;
// }
// member = default;
// return false;
//}
// public MemberRef<TMember> GetMember<TMember, TBinding>(TBinding binding)
// where TMember : AbstractMember
// where TBinding : class
// {
// if (_memberByBinding.TryGetValue(binding, out MemberRef<AbstractMember> existingMember))
// {
// return existingMember.AsRef<TMember>();
// }
//
// return MemberRef<TMember>.Null;
// // throw new KeyNotFoundException(
// // $"{typeof(TMember).Name} with binding '{typeof(TBinding).Name}={binding}' could not be found.");
// }
public TMember TryGetMember<TMember>(object binding)
where TMember : AbstractMember
{
return _codeModel.TryGet<TMember>(binding);
}
public Compilation GetCompilation(SyntaxTree syntax)
{
if (_compilationBySyntaxTree.TryGetValue(syntax, out Compilation compilation))
{
return compilation;
}
throw new KeyNotFoundException(
$"Syntax tree with path '{syntax.FilePath}' could not be found in any of the compilations.");
}
public SemanticModel GetSemanticModel(SyntaxTree syntaxTree)
{
var compilation = GetCompilation(syntaxTree);
return compilation.GetSemanticModel(syntaxTree, ignoreAccessibility: true);
}
public SemanticModel GetSemanticModel(SyntaxNode syntax)
{
return GetSemanticModel(syntax.SyntaxTree);
}
//public TMember GetOrAddMember<TMember, TBinding>(TBinding binding, Func<TMember> memberFactory)
// where TMember : AbstractMember
// where TBinding : class
//{
// if (_memberByBinding.TryGetValue(binding, out AbstractMember existingMember))
// {
// return (TMember)existingMember;
// }
// var newMember = memberFactory();
// BindMember(newMember, binding);
// return newMember;
//}
public IEnumerable<AbstractMember> GetRgisteredMembers() => new HashSet<AbstractMember>(_codeModel.MembersByBndings.Values);
public IEnumerable<AbstractMember> GetTopLevelMembers() => _codeModel.TopLevelMembers;
public ImmutableArray<Compilation> GetCompilations() => _compilations;
public ImperativeCodeModel GetCodeModel() => _codeModel;
private static IEnumerable<Compilation> WithReferencedCompilations(Compilation[] knownCompilations)
{
var dedup = new HashSet<Compilation>();
return
knownCompilations
.Concat(
knownCompilations.SelectMany(compilation => compilation.References.OfType<CompilationReference>())
.Select(r => r.Compilation)
)
.Where(dedup.Add); // Distinct which preserves original ordering
}
}
}