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
|
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace ICSharpCode.NRefactory6.CSharp
{
partial class SemanticMap
{
private readonly Dictionary<SyntaxNode, SymbolInfo> _expressionToInfoMap =
new Dictionary<SyntaxNode, SymbolInfo>();
private readonly Dictionary<SyntaxToken, SymbolInfo> _tokenToInfoMap =
new Dictionary<SyntaxToken, SymbolInfo>();
private SemanticMap()
{
}
internal static SemanticMap From(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
{
var map = new SemanticMap();
var walker = new Walker(semanticModel, map, cancellationToken);
walker.Visit(node);
return map;
}
public IEnumerable<ISymbol> AllReferencedSymbols
{
get
{
return _expressionToInfoMap.Values.Concat(_tokenToInfoMap.Values).Select(info => info.Symbol).Distinct();
}
}
private class Walker : SyntaxWalker
{
private readonly SemanticModel _semanticModel;
private readonly SemanticMap _map;
private readonly CancellationToken _cancellationToken;
public Walker(SemanticModel semanticModel, SemanticMap map, CancellationToken cancellationToken) :
base(SyntaxWalkerDepth.Token)
{
_semanticModel = semanticModel;
_map = map;
_cancellationToken = cancellationToken;
}
public override void Visit(SyntaxNode node)
{
var info = _semanticModel.GetSymbolInfo(node);
if (!IsNone(info))
{
_map._expressionToInfoMap.Add(node, info);
}
base.Visit(node);
}
protected override void VisitToken(SyntaxToken token)
{
var info = _semanticModel.GetSymbolInfo(token, _cancellationToken);
if (!IsNone(info))
{
_map._tokenToInfoMap.Add(token, info);
}
base.VisitToken(token);
}
private bool IsNone(SymbolInfo info)
{
return info.Symbol == null && info.CandidateSymbols.Length == 0;
}
}
}
}
|