-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathCachedEntity.cs
More file actions
103 lines (86 loc) · 3.22 KB
/
CachedEntity.cs
File metadata and controls
103 lines (86 loc) · 3.22 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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.CodeAnalysis;
namespace Semmle.Extraction.CSharp.Entities
{
/// <summary>
/// A cached entity.
///
/// The <see cref="Entity.Label"/> property is used as label in caching.
/// </summary>
public abstract class CachedEntity : LabelledEntity
{
protected CachedEntity(Context context) : base(context)
{
}
/// <summary>
/// Populates the <see cref="Label"/> field and generates output in the trap file
/// as required. Is only called when <see cref="NeedsPopulation"/> returns
/// <code>true</code> and the entity has not already been populated.
/// </summary>
public abstract void Populate(TextWriter trapFile);
public abstract bool NeedsPopulation { get; }
public abstract bool OnlyScaffold { get; }
}
/// <summary>
/// An abstract symbol, which encapsulates a data type (such as a C# symbol).
/// </summary>
/// <typeparam name="TSymbol">The type of the symbol.</typeparam>
public abstract class CachedEntity<TSymbol> : CachedEntity where TSymbol : notnull
{
[NotNull]
public TSymbol Symbol { get; }
protected CachedEntity(Context context, TSymbol symbol) : base(context)
{
this.Symbol = symbol;
}
/// <summary>
/// For debugging.
/// </summary>
public string DebugContents
{
get
{
using var trap = new StringWriter();
Populate(trap);
return trap.ToString();
}
}
protected static void WriteLocationToTrap<T1>(Action<T1, Location> writeAction, T1 entity, Location l)
{
if (l is not EmptyLocation)
{
writeAction(entity, l);
}
}
protected static void WriteLocationsToTrap<T1>(Action<T1, Location> writeAction, T1 entity, IEnumerable<Location> locations)
{
foreach (var loc in locations)
{
WriteLocationToTrap(writeAction, entity, loc);
}
}
public override int GetHashCode() => Symbol is null ? 0 : Symbol.GetHashCode();
public override bool Equals(object? obj)
{
var other = obj as CachedEntity<TSymbol>;
return other?.GetType() == GetType() && Equals(other.Symbol, Symbol);
}
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
public override bool OnlyScaffold => Context.OnlyScaffold;
}
/// <summary>
/// A class used to wrap an `ISymbol` object, which uses `SymbolEqualityComparer.Default`
/// for comparison.
/// </summary>
public struct SymbolEqualityWrapper
{
public ISymbol Symbol { get; }
public SymbolEqualityWrapper(ISymbol symbol) { Symbol = symbol; }
public override bool Equals(object? other) =>
other is SymbolEqualityWrapper sew && SymbolEqualityComparer.Default.Equals(Symbol, sew.Symbol);
public override int GetHashCode() => 11 * SymbolEqualityComparer.Default.GetHashCode(Symbol);
}
}