-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathCachedSymbol.cs
More file actions
149 lines (131 loc) · 5.21 KB
/
CachedSymbol.cs
File metadata and controls
149 lines (131 loc) · 5.21 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
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Semmle.Extraction.CSharp.Entities
{
internal abstract class CachedSymbol<T> : CachedEntity<T> where T : class, ISymbol
{
protected CachedSymbol(Context cx, T init)
: base(cx, init)
{
}
public virtual Type? ContainingType => Symbol.ContainingType is not null
? Symbol.ContainingType.IsTupleType
? NamedType.CreateNamedTypeFromTupleType(Context, Symbol.ContainingType)
: Type.Create(Context, Symbol.ContainingType)
: null;
public void PopulateModifiers(TextWriter trapFile)
{
Modifier.ExtractModifiers(Context, trapFile, this, Symbol);
}
protected void PopulateAttributes()
{
// Only extract attributes for source declarations
if (ReferenceEquals(Symbol, Symbol.OriginalDefinition))
Attribute.ExtractAttributes(Context, Symbol, this);
}
protected void PopulateNullability(TextWriter trapFile, AnnotatedTypeSymbol type)
{
var n = NullabilityEntity.Create(Context, Nullability.Create(type));
if (!type.HasObliviousNullability())
{
trapFile.type_nullability(this, n);
}
}
protected void PopulateRefKind(TextWriter trapFile, RefKind kind)
{
switch (kind)
{
case RefKind.Out:
trapFile.type_annotation(this, Kinds.TypeAnnotation.Out);
break;
case RefKind.Ref:
trapFile.type_annotation(this, Kinds.TypeAnnotation.Ref);
break;
case RefKind.RefReadOnly:
case RefKind.RefReadOnlyParameter:
trapFile.type_annotation(this, Kinds.TypeAnnotation.ReadonlyRef);
break;
}
}
protected void PopulateScopedKind(TextWriter trapFile, ScopedKind kind)
{
switch (kind)
{
case ScopedKind.ScopedRef:
trapFile.scoped_annotation(this, Kinds.ScopedAnnotation.ScopedRef);
break;
case ScopedKind.ScopedValue:
trapFile.scoped_annotation(this, Kinds.ScopedAnnotation.ScopedValue);
break;
}
}
protected void ExtractCompilerGenerated(TextWriter trapFile)
{
if (Symbol.IsImplicitlyDeclared)
trapFile.compiler_generated(this);
}
/// <summary>
/// The location which is stored in the database and is used when highlighting source code.
/// It's generally short, e.g. a method name.
/// </summary>
public override Microsoft.CodeAnalysis.Location? ReportingLocation => Symbol.Locations.BestOrDefault();
/// <summary>
/// The full text span of the entity, e.g. for binding comments.
/// </summary>
public virtual Microsoft.CodeAnalysis.Location? FullLocation => Symbol.Locations.BestOrDefault();
public virtual IEnumerable<Location> Locations
{
get
{
var loc = ReportingLocation;
if (loc is not null)
{
// Some built in operators lack locations, so loc is null.
yield return Context.CreateLocation(ReportingLocation);
if (loc.Kind == LocationKind.SourceFile)
yield return Assembly.CreateOutputAssembly(Context);
}
}
}
/// <summary>
/// Bind comments to this symbol.
/// Comments are only bound to source declarations.
/// </summary>
protected void BindComments()
{
if (!Symbol.IsImplicitlyDeclared && IsSourceDeclaration && Symbol.FromSource())
Context.BindComments(this, FullLocation);
}
protected virtual T BodyDeclaringSymbol => Symbol;
public BlockSyntax? Block
{
get
{
return BodyDeclaringSymbol.DeclaringSyntaxReferences
.SelectMany(r => r.GetSyntax().ChildNodes())
.OfType<BlockSyntax>()
.FirstOrDefault();
}
}
public ExpressionSyntax? ExpressionBody
{
get
{
return BodyDeclaringSymbol.DeclaringSyntaxReferences
.SelectMany(r => r.GetSyntax().ChildNodes())
.OfType<ArrowExpressionClauseSyntax>()
.Select(arrow => arrow.Expression)
.FirstOrDefault();
}
}
public virtual bool IsSourceDeclaration => Symbol.IsSourceDeclaration();
// When extracting in overlay mode we always need to populate to ensure that
// all transitive dependencies are extracted.
//public override bool NeedsPopulation => Context.Defines(Symbol) || Context.IsOverlayMode;
public Location Location => Context.CreateLocation(ReportingLocation);
}
}