forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedEntity.cs
More file actions
85 lines (71 loc) · 2.64 KB
/
CachedEntity.cs
File metadata and controls
85 lines (71 loc) · 2.64 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
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; }
}
/// <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();
}
}
public override bool NeedsPopulation { get; }
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;
}
/// <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);
}
}