C#: Fix type parameter names

This commit is contained in:
Tamas Vajk
2020-10-02 17:02:49 +02:00
parent 2e350caf9f
commit fbc128fcc7
9 changed files with 61 additions and 58 deletions

View File

@@ -7,21 +7,21 @@ namespace Semmle.Extraction.CIL
/// A factory and a cache for mapping source entities to target entities.
/// Could be considered as a memoizer.
/// </summary>
/// <typeparam name="SrcType">The type of the source.</typeparam>
/// <typeparam name="TargetType">The type of the generated object.</typeparam>
public class CachedFunction<SrcType, TargetType> where SrcType : notnull
/// <typeparam name="TSrc">The type of the source.</typeparam>
/// <typeparam name="TTarget">The type of the generated object.</typeparam>
public class CachedFunction<TSrc, TTarget> where TSrc : notnull
{
private readonly Func<SrcType, TargetType> generator;
private readonly Dictionary<SrcType, TargetType> cache;
private readonly Func<TSrc, TTarget> generator;
private readonly Dictionary<TSrc, TTarget> cache;
/// <summary>
/// Initializes the factory with a given mapping.
/// </summary>
/// <param name="g">The mapping.</param>
public CachedFunction(Func<SrcType, TargetType> g)
public CachedFunction(Func<TSrc, TTarget> g)
{
generator = g;
cache = new Dictionary<SrcType, TargetType>();
cache = new Dictionary<TSrc, TTarget>();
}
/// <summary>
@@ -30,7 +30,7 @@ namespace Semmle.Extraction.CIL
/// </summary>
/// <param name="src">The source object.</param>
/// <returns>The created object.</returns>
public TargetType this[SrcType src]
public TTarget this[TSrc src]
{
get
{
@@ -47,22 +47,22 @@ namespace Semmle.Extraction.CIL
/// <summary>
/// A factory for mapping a pair of source entities to a target entity.
/// </summary>
/// <typeparam name="Src1">Source entity type 1.</typeparam>
/// <typeparam name="Src2">Source entity type 2.</typeparam>
/// <typeparam name="Target">The target type.</typeparam>
public class CachedFunction<Src1, Src2, Target>
/// <typeparam name="TSrcEntity1">Source entity type 1.</typeparam>
/// <typeparam name="TSrcEntity2">Source entity type 2.</typeparam>
/// <typeparam name="TTarget">The target type.</typeparam>
public class CachedFunction<TSrcEntity1, TSrcEntity2, TTarget>
{
private readonly CachedFunction<(Src1, Src2), Target> factory;
private readonly CachedFunction<(TSrcEntity1, TSrcEntity2), TTarget> factory;
/// <summary>
/// Initializes the factory with a given mapping.
/// </summary>
/// <param name="g">The mapping.</param>
public CachedFunction(Func<Src1, Src2, Target> g)
public CachedFunction(Func<TSrcEntity1, TSrcEntity2, TTarget> g)
{
factory = new CachedFunction<(Src1, Src2), Target>(p => g(p.Item1, p.Item2));
factory = new CachedFunction<(TSrcEntity1, TSrcEntity2), TTarget>(p => g(p.Item1, p.Item2));
}
public Target this[Src1 s1, Src2 s2] => factory[(s1, s2)];
public TTarget this[TSrcEntity1 s1, TSrcEntity2 s2] => factory[(s1, s2)];
}
}

View File

@@ -291,15 +291,15 @@ namespace Semmle.Extraction.CSharp.Entities
}
}
internal abstract class Expression<SyntaxNode> : Expression
where SyntaxNode : ExpressionSyntax
internal abstract class Expression<TExpressionSyntax> : Expression
where TExpressionSyntax : ExpressionSyntax
{
public readonly SyntaxNode Syntax;
public readonly TExpressionSyntax Syntax;
protected Expression(ExpressionNodeInfo info)
: base(info)
{
Syntax = (SyntaxNode)info.Node;
Syntax = (TExpressionSyntax)info.Node;
}
/// <summary>

View File

@@ -7,12 +7,14 @@ using System.Linq;
namespace Semmle.Extraction.CSharp.Entities.Expressions
{
internal abstract class ArrayCreation<SyntaxNode> : Expression<SyntaxNode> where SyntaxNode : ExpressionSyntax
internal abstract class ArrayCreation<TSyntaxNode> : Expression<TSyntaxNode>
where TSyntaxNode : ExpressionSyntax
{
protected ArrayCreation(ExpressionNodeInfo info) : base(info) { }
}
internal abstract class ExplicitArrayCreation<SyntaxNode> : ArrayCreation<SyntaxNode> where SyntaxNode : ExpressionSyntax
internal abstract class ExplicitArrayCreation<TSyntaxNode> : ArrayCreation<TSyntaxNode>
where TSyntaxNode : ExpressionSyntax
{
protected ExplicitArrayCreation(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.ARRAY_CREATION)) { }

View File

@@ -8,7 +8,8 @@ using System.Linq;
namespace Semmle.Extraction.CSharp.Entities.Expressions
{
internal abstract class ObjectCreation<SyntaxNode> : Expression<SyntaxNode> where SyntaxNode : ExpressionSyntax
internal abstract class ObjectCreation<TExpressionSyntax> : Expression<TExpressionSyntax>
where TExpressionSyntax : ExpressionSyntax
{
protected ObjectCreation(ExpressionNodeInfo info)
: base(info) { }

View File

@@ -88,13 +88,13 @@ namespace Semmle.Extraction
public Label GetNewLabel() => new Label(GetNewId());
public Entity CreateEntity<Type, Entity>(ICachedEntityFactory<Type, Entity> factory, object cacheKey, Type init)
where Entity : ICachedEntity =>
public TEntity CreateEntity<TInit, TEntity>(ICachedEntityFactory<TInit, TEntity> factory, object cacheKey, TInit init)
where TEntity : ICachedEntity =>
cacheKey is ISymbol s ? CreateEntity(factory, s, init, symbolEntityCache) : CreateEntity(factory, cacheKey, init, objectEntityCache);
public Entity CreateEntityFromSymbol<Type, Entity>(ICachedEntityFactory<Type, Entity> factory, Type init)
where Type : ISymbol
where Entity : ICachedEntity => CreateEntity(factory, init, init, symbolEntityCache);
public TEntity CreateEntityFromSymbol<TSymbol, TEntity>(ICachedEntityFactory<TSymbol, TEntity> factory, TSymbol init)
where TSymbol : ISymbol
where TEntity : ICachedEntity => CreateEntity(factory, init, init, symbolEntityCache);
/// <summary>
/// Creates and populates a new entity, or returns the existing one from the cache.
@@ -104,12 +104,12 @@ namespace Semmle.Extraction
/// <param name="init">The initializer for the entity.</param>
/// <param name="dictionary">The dictionary to use for caching.</param>
/// <returns>The new/existing entity.</returns>
private Entity CreateEntity<Type, CacheKeyType, Entity>(ICachedEntityFactory<Type, Entity> factory, CacheKeyType cacheKey, Type init, IDictionary<CacheKeyType, ICachedEntity> dictionary)
where CacheKeyType : notnull
where Entity : ICachedEntity
private TEntity CreateEntity<TInit, TCacheKey, TEntity>(ICachedEntityFactory<TInit, TEntity> factory, TCacheKey cacheKey, TInit init, IDictionary<TCacheKey, ICachedEntity> dictionary)
where TCacheKey : notnull
where TEntity : ICachedEntity
{
if (dictionary.TryGetValue(cacheKey, out var cached))
return (Entity)cached;
return (TEntity)cached;
using (StackGuard)
{

View File

@@ -98,13 +98,13 @@ namespace Semmle.Extraction
/// <summary>
/// A factory for creating cached entities.
/// </summary>
/// <typeparam name="Initializer">The type of the initializer.</typeparam>
public interface ICachedEntityFactory<in Initializer, out Entity> where Entity : ICachedEntity
/// <typeparam name="TInit">The type of the initializer.</typeparam>
public interface ICachedEntityFactory<in TInit, out TEntity> where TEntity : ICachedEntity
{
/// <summary>
/// Initializes the entity, but does not generate any trap code.
/// </summary>
Entity Create(Context cx, Initializer init);
TEntity Create(Context cx, TInit init);
}
public static class ICachedEntityFactoryExtensions
@@ -113,29 +113,29 @@ namespace Semmle.Extraction
/// Creates and populates a new entity, or returns the existing one from the cache,
/// based on the supplied cache key.
/// </summary>
/// <typeparam name="Type">The type used to construct the entity.</typeparam>
/// <typeparam name="Entity">The type of the entity to create.</typeparam>
/// <typeparam name="TInit">The type used to construct the entity.</typeparam>
/// <typeparam name="TEntity">The type of the entity to create.</typeparam>
/// <param name="factory">The factory used to construct the entity.</param>
/// <param name="cx">The extractor context.</param>
/// <param name="cacheKey">The key used for caching.</param>
/// <param name="init">The initializer for the entity.</param>
/// <returns>The entity.</returns>
public static Entity CreateEntity<Type, Entity>(this ICachedEntityFactory<Type, Entity> factory, Context cx, object cacheKey, Type init)
where Entity : ICachedEntity => cx.CreateEntity(factory, cacheKey, init);
public static TEntity CreateEntity<TInit, TEntity>(this ICachedEntityFactory<TInit, TEntity> factory, Context cx, object cacheKey, TInit init)
where TEntity : ICachedEntity => cx.CreateEntity(factory, cacheKey, init);
/// <summary>
/// Creates and populates a new entity from an `ISymbol`, or returns the existing one
/// from the cache.
/// </summary>
/// <typeparam name="Type">The type used to construct the entity.</typeparam>
/// <typeparam name="Entity">The type of the entity to create.</typeparam>
/// <typeparam name="TSymbol">The type used to construct the entity.</typeparam>
/// <typeparam name="TEntity">The type of the entity to create.</typeparam>
/// <param name="factory">The factory used to construct the entity.</param>
/// <param name="cx">The extractor context.</param>
/// <param name="init">The initializer for the entity.</param>
/// <returns>The entity.</returns>
public static Entity CreateEntityFromSymbol<Type, Entity>(this ICachedEntityFactory<Type, Entity> factory, Context cx, Type init)
where Type : ISymbol
where Entity : ICachedEntity => cx.CreateEntityFromSymbol(factory, init);
public static TEntity CreateEntityFromSymbol<TSymbol, TEntity>(this ICachedEntityFactory<TSymbol, TEntity> factory, Context cx, TSymbol init)
where TSymbol : ISymbol
where TEntity : ICachedEntity => cx.CreateEntityFromSymbol(factory, init);
public static void DefineLabel(this IEntity entity, TextWriter trapFile, IExtractor extractor)
{

View File

@@ -6,10 +6,10 @@ namespace Semmle.Extraction
/// <summary>
/// An abstract symbol, which encapsulates a data type (such as a C# symbol).
/// </summary>
/// <typeparam name="Initializer">The type of the symbol.</typeparam>
public abstract class CachedEntity<Initializer> : ICachedEntity
/// <typeparam name="TSymbol">The type of the symbol.</typeparam>
public abstract class CachedEntity<TSymbol> : ICachedEntity
{
protected CachedEntity(Context context, Initializer init)
protected CachedEntity(Context context, TSymbol init)
{
Context = context;
symbol = init;
@@ -41,14 +41,14 @@ namespace Semmle.Extraction
get;
}
public Initializer symbol
public TSymbol symbol
{
get;
}
object? ICachedEntity.UnderlyingObject => symbol;
public Initializer UnderlyingObject => symbol;
public TSymbol UnderlyingObject => symbol;
public abstract void WriteId(System.IO.TextWriter trapFile);
@@ -68,7 +68,7 @@ namespace Semmle.Extraction
public override bool Equals(object? obj)
{
var other = obj as CachedEntity<Initializer>;
var other = obj as CachedEntity<TSymbol>;
return other?.GetType() == GetType() && Equals(other.symbol, symbol);
}

View File

@@ -7,11 +7,11 @@ namespace Semmle.Util
/// A dictionary which performs an action when items are added to the dictionary.
/// The order in which keys and actions are added does not matter.
/// </summary>
/// <typeparam name="Key"></typeparam>
/// <typeparam name="Value"></typeparam>
public class ActionMap<Key, Value> where Key : notnull
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class ActionMap<TKey, TValue> where TKey : notnull
{
public void Add(Key key, Value value)
public void Add(TKey key, TValue value)
{
if (actions.TryGetValue(key, out var a))
@@ -19,7 +19,7 @@ namespace Semmle.Util
values[key] = value;
}
public void OnAdd(Key key, Action<Value> action)
public void OnAdd(TKey key, Action<TValue> action)
{
if (actions.TryGetValue(key, out var a))
{
@@ -37,9 +37,9 @@ namespace Semmle.Util
}
// Action associated with each key.
private readonly Dictionary<Key, Action<Value>> actions = new Dictionary<Key, Action<Value>>();
private readonly Dictionary<TKey, Action<TValue>> actions = new Dictionary<TKey, Action<TValue>>();
// Values associated with each key.
private readonly Dictionary<Key, Value> values = new Dictionary<Key, Value>();
private readonly Dictionary<TKey, TValue> values = new Dictionary<TKey, TValue>();
}
}

View File

@@ -61,7 +61,7 @@ namespace Semmle.Util
/// <param name="v1">Vector 1</param>
/// <param name="v2">Vector 2</param>
/// <returns>The Hamming Distance.</returns>
private static int HammingDistance<U>(IEnumerable<U> v1, IEnumerable<U> v2) where U : notnull
private static int HammingDistance<TElement>(IEnumerable<TElement> v1, IEnumerable<TElement> v2) where TElement : notnull
{
return v1.Zip(v2, (x, y) => x.Equals(y) ? 0 : 1).Sum();
}