C#: Use covariant return for context overrides

This commit is contained in:
Tamas Vajk
2021-03-03 14:35:07 +01:00
parent cfd8a87496
commit bd2b3e72f8
38 changed files with 168 additions and 176 deletions

View File

@@ -42,7 +42,7 @@ namespace Semmle.Extraction.CIL.Entities
public override string Name => elementType.Name + "[]";
public override Namespace ContainingNamespace => Cx.SystemNamespace;
public override Namespace ContainingNamespace => Context.SystemNamespace;
public override Type? ContainingType => null;
@@ -50,9 +50,9 @@ namespace Semmle.Extraction.CIL.Entities
public override CilTypeKind Kind => CilTypeKind.Array;
public override Type Construct(IEnumerable<Type> typeArguments) => Cx.Populate(new ArrayType(Cx, elementType.Construct(typeArguments)));
public override Type Construct(IEnumerable<Type> typeArguments) => Context.Populate(new ArrayType(Context, elementType.Construct(typeArguments)));
public override Type SourceDeclaration => Cx.Populate(new ArrayType(Cx, elementType.SourceDeclaration));
public override Type SourceDeclaration => Context.Populate(new ArrayType(Context, elementType.SourceDeclaration));
public override IEnumerable<IExtractionProduct> Contents
{

View File

@@ -36,7 +36,7 @@ namespace Semmle.Extraction.CIL.Entities
{
trapFile.Write(FullName);
trapFile.Write("#file:///");
trapFile.Write(Cx.AssemblyPath.Replace("\\", "/"));
trapFile.Write(Context.AssemblyPath.Replace("\\", "/"));
trapFile.Write(";assembly");
}
@@ -56,24 +56,24 @@ namespace Semmle.Extraction.CIL.Entities
yield return file;
yield return Tuples.assemblies(this, file, FullName, assemblyName.Name ?? string.Empty, assemblyName.Version?.ToString() ?? string.Empty);
if (Cx.Pdb != null)
if (Context.Pdb != null)
{
foreach (var f in Cx.Pdb.SourceFiles)
foreach (var f in Context.Pdb.SourceFiles)
{
yield return Cx.CreateSourceFile(f);
yield return Context.CreateSourceFile(f);
}
}
foreach (var handle in Cx.MdReader.TypeDefinitions)
foreach (var handle in Context.MdReader.TypeDefinitions)
{
IExtractionProduct? product = null;
try
{
product = Cx.Create(handle);
product = Context.Create(handle);
}
catch (InternalError e)
{
Cx.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
Context.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Context), e.StackTrace);
}
// Limitation of C#: Cannot yield return inside a try-catch.
@@ -81,16 +81,16 @@ namespace Semmle.Extraction.CIL.Entities
yield return product;
}
foreach (var handle in Cx.MdReader.MethodDefinitions)
foreach (var handle in Context.MdReader.MethodDefinitions)
{
IExtractionProduct? product = null;
try
{
product = Cx.Create(handle);
product = Context.Create(handle);
}
catch (InternalError e)
{
Cx.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
Context.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Context), e.StackTrace);
}
if (product != null)

View File

@@ -32,7 +32,7 @@ namespace Semmle.Extraction.CIL.Entities
{
get
{
var constructor = (Method)Cx.Create(attrib.Constructor);
var constructor = (Method)Context.Create(attrib.Constructor);
yield return constructor;
yield return Tuples.cil_attribute(this, @object, constructor);
@@ -41,11 +41,11 @@ namespace Semmle.Extraction.CIL.Entities
try
{
decoded = attrib.DecodeValue(new CustomAttributeDecoder(Cx));
decoded = attrib.DecodeValue(new CustomAttributeDecoder(Context));
}
catch
{
Cx.Extractor.Logger.Log(Util.Logging.Severity.Info,
Context.Extractor.Logger.Log(Util.Logging.Severity.Info,
$"Attribute decoding is partial. Decoding attribute {constructor.DeclaringType.GetQualifiedName()} failed on {@object}.");
yield break;
}

View File

@@ -8,7 +8,7 @@ namespace Semmle.Extraction.CIL
/// </summary>
internal interface IGenericContext
{
Context Cx { get; }
Context Context { get; }
/// <summary>
/// The list of generic type parameters/arguments, including type parameters/arguments of

View File

@@ -10,8 +10,7 @@ namespace Semmle.Extraction.CIL
/// </summary>
internal abstract class LabelledEntity : Extraction.LabelledEntity, IExtractedEntity
{
// todo: with .NET 5 this can override the base context, and change the return type.
public Context Cx => (Context)base.Context;
public override Context Context => (Context)base.Context;
protected LabelledEntity(Context cx) : base(cx)
{

View File

@@ -9,8 +9,7 @@ namespace Semmle.Extraction.CIL
/// </summary>
internal abstract class UnlabelledEntity : Extraction.UnlabelledEntity, IExtractedEntity
{
// todo: with .NET 5 this can override the base context, and change the return type.
public Context Cx => (Context)base.Context;
public override Context Context => (Context)base.Context;
protected UnlabelledEntity(Context cx) : base(cx)
{

View File

@@ -14,7 +14,7 @@ namespace Semmle.Extraction.CIL.Entities
public DefinitionField(Context cx, FieldDefinitionHandle handle) : base(cx)
{
this.handle = handle;
fd = Cx.MdReader.GetFieldDefinition(handle);
fd = Context.MdReader.GetFieldDefinition(handle);
}
public override bool Equals(object? obj)
@@ -28,7 +28,7 @@ namespace Semmle.Extraction.CIL.Entities
{
get
{
yield return Tuples.metadata_handle(this, Cx.Assembly, MetadataTokens.GetToken(handle));
yield return Tuples.metadata_handle(this, Context.Assembly, MetadataTokens.GetToken(handle));
foreach (var c in base.Contents)
yield return c;
@@ -48,16 +48,16 @@ namespace Semmle.Extraction.CIL.Entities
if (fd.Attributes.HasFlag(FieldAttributes.Assembly))
yield return Tuples.cil_internal(this);
foreach (var c in Attribute.Populate(Cx, this, fd.GetCustomAttributes()))
foreach (var c in Attribute.Populate(Context, this, fd.GetCustomAttributes()))
yield return c;
}
}
public override string Name => Cx.GetString(fd.Name);
public override string Name => Context.GetString(fd.Name);
public override Type DeclaringType => (Type)Cx.Create(fd.GetDeclaringType());
public override Type DeclaringType => (Type)Context.Create(fd.GetDeclaringType());
public override Type Type => fd.DecodeSignature(Cx.TypeSignatureDecoder, DeclaringType);
public override Type Type => fd.DecodeSignature(Context.TypeSignatureDecoder, DeclaringType);
public override IEnumerable<Type> TypeParameters => throw new NotImplementedException();

View File

@@ -25,16 +25,16 @@ namespace Semmle.Extraction.CIL.Entities
public DefinitionMethod(IGenericContext gc, MethodDefinitionHandle handle) : base(gc)
{
md = Cx.MdReader.GetMethodDefinition(handle);
md = Context.MdReader.GetMethodDefinition(handle);
this.gc = gc;
this.handle = handle;
name = Cx.GetString(md.Name);
name = Context.GetString(md.Name);
declaringType = (Type)Cx.CreateGeneric(this, md.GetDeclaringType());
declaringType = (Type)Context.CreateGeneric(this, md.GetDeclaringType());
signature = md.DecodeSignature(new SignatureDecoder(), this);
methodDebugInformation = Cx.GetMethodDebugInformation(handle);
methodDebugInformation = Context.GetMethodDebugInformation(handle);
}
public override bool Equals(object? obj)
@@ -48,7 +48,7 @@ namespace Semmle.Extraction.CIL.Entities
public override Type DeclaringType => declaringType;
public override string Name => Cx.ShortName(md.Name);
public override string Name => Context.ShortName(md.Name);
public override string NameLabel => name;
@@ -67,14 +67,14 @@ namespace Semmle.Extraction.CIL.Entities
// depend on other type parameters (as a constraint).
genericParams = new MethodTypeParameter[md.GetGenericParameters().Count];
for (var i = 0; i < genericParams.Length; ++i)
genericParams[i] = Cx.Populate(new MethodTypeParameter(this, this, i));
genericParams[i] = Context.Populate(new MethodTypeParameter(this, this, i));
for (var i = 0; i < genericParams.Length; ++i)
genericParams[i].PopulateHandle(md.GetGenericParameters()[i]);
foreach (var p in genericParams)
yield return p;
}
var typeSignature = md.DecodeSignature(Cx.TypeSignatureDecoder, this);
var typeSignature = md.DecodeSignature(Context.TypeSignatureDecoder, this);
var parameters = GetParameterExtractionProducts(typeSignature.ParameterTypes).ToArray();
Parameters = parameters.OfType<Parameter>().ToArray();
@@ -85,17 +85,17 @@ namespace Semmle.Extraction.CIL.Entities
foreach (var c in PopulateFlags)
yield return c;
foreach (var p in md.GetParameters().Select(h => Cx.MdReader.GetParameter(h)).Where(p => p.SequenceNumber > 0))
foreach (var p in md.GetParameters().Select(h => Context.MdReader.GetParameter(h)).Where(p => p.SequenceNumber > 0))
{
var pe = Parameters[IsStatic ? p.SequenceNumber - 1 : p.SequenceNumber];
if (p.Attributes.HasFlag(ParameterAttributes.Out))
yield return Tuples.cil_parameter_out(pe);
if (p.Attributes.HasFlag(ParameterAttributes.In))
yield return Tuples.cil_parameter_in(pe);
Attribute.Populate(Cx, pe, p.GetCustomAttributes());
Attribute.Populate(Context, pe, p.GetCustomAttributes());
}
yield return Tuples.metadata_handle(this, Cx.Assembly, MetadataTokens.GetToken(handle));
yield return Tuples.metadata_handle(this, Context.Assembly, MetadataTokens.GetToken(handle));
foreach (var m in GetMethodExtractionProducts(Name, declaringType, typeSignature.ReturnType))
{
@@ -103,19 +103,19 @@ namespace Semmle.Extraction.CIL.Entities
}
yield return Tuples.cil_method_source_declaration(this, this);
yield return Tuples.cil_method_location(this, Cx.Assembly);
yield return Tuples.cil_method_location(this, Context.Assembly);
if (HasBytecode)
{
Implementation = new MethodImplementation(this);
yield return Implementation;
var body = Cx.PeReader.GetMethodBody(md.RelativeVirtualAddress);
var body = Context.PeReader.GetMethodBody(md.RelativeVirtualAddress);
if (!body.LocalSignature.IsNil)
{
var locals = Cx.MdReader.GetStandaloneSignature(body.LocalSignature);
var localVariableTypes = locals.DecodeLocalSignature(Cx.TypeSignatureDecoder, this);
var locals = Context.MdReader.GetStandaloneSignature(body.LocalSignature);
var localVariableTypes = locals.DecodeLocalSignature(Context.TypeSignatureDecoder, this);
this.locals = new LocalVariable[localVariableTypes.Length];
@@ -125,13 +125,13 @@ namespace Semmle.Extraction.CIL.Entities
if (t is ByRefType brt)
{
t = brt.ElementType;
this.locals[l] = Cx.Populate(new LocalVariable(Cx, Implementation, l, t));
this.locals[l] = Context.Populate(new LocalVariable(Context, Implementation, l, t));
yield return this.locals[l];
yield return Tuples.cil_type_annotation(this.locals[l], TypeAnnotation.Ref);
}
else
{
this.locals[l] = Cx.Populate(new LocalVariable(Cx, Implementation, l, t));
this.locals[l] = Context.Populate(new LocalVariable(Context, Implementation, l, t));
yield return this.locals[l];
}
}
@@ -152,7 +152,7 @@ namespace Semmle.Extraction.CIL.Entities
if (methodDebugInformation != null)
{
var sourceLocation = Cx.CreateSourceLocation(methodDebugInformation.Location);
var sourceLocation = Context.CreateSourceLocation(methodDebugInformation.Location);
yield return sourceLocation;
yield return Tuples.cil_method_location(this, sourceLocation);
}
@@ -191,7 +191,7 @@ namespace Semmle.Extraction.CIL.Entities
yield return Tuples.cil_newslot(this);
// Populate attributes
Attribute.Populate(Cx, this, md.GetCustomAttributes());
Attribute.Populate(Context, this, md.GetCustomAttributes());
}
}
@@ -210,7 +210,7 @@ namespace Semmle.Extraction.CIL.Entities
nextSequencePoint = methodDebugInformation.SequencePoints.GetEnumerator();
if (nextSequencePoint.MoveNext())
{
instructionLocation = Cx.CreateSourceLocation(nextSequencePoint.Current.Location);
instructionLocation = Context.CreateSourceLocation(nextSequencePoint.Current.Location);
yield return instructionLocation;
}
else
@@ -222,12 +222,12 @@ namespace Semmle.Extraction.CIL.Entities
var child = 0;
for (var offset = 0; offset < (ilbytes?.Length ?? 0);)
{
var instruction = new Instruction(Cx, this, ilbytes!, offset, child++);
var instruction = new Instruction(Context, this, ilbytes!, offset, child++);
yield return instruction;
if (nextSequencePoint != null && offset >= nextSequencePoint.Current.Offset)
{
instructionLocation = Cx.CreateSourceLocation(nextSequencePoint.Current.Location);
instructionLocation = Context.CreateSourceLocation(nextSequencePoint.Current.Location);
yield return instructionLocation;
if (!nextSequencePoint.MoveNext())
nextSequencePoint = null;
@@ -257,7 +257,7 @@ namespace Semmle.Extraction.CIL.Entities
{
if (md.ImplAttributes == MethodImplAttributes.IL && md.RelativeVirtualAddress != 0)
{
var body = Cx.PeReader.GetMethodBody(md.RelativeVirtualAddress);
var body = Context.PeReader.GetMethodBody(md.RelativeVirtualAddress);
var ilbytes = body.GetILBytes();
@@ -267,7 +267,7 @@ namespace Semmle.Extraction.CIL.Entities
Instruction decoded;
try
{
decoded = new Instruction(Cx, this, ilbytes!, offset, child++);
decoded = new Instruction(Context, this, ilbytes!, offset, child++);
offset += decoded.Width;
}
catch // lgtm[cs/catch-of-all-exceptions]

View File

@@ -16,7 +16,7 @@ namespace Semmle.Extraction.CIL.Entities
public override string Name => "!error";
public override Namespace ContainingNamespace => Cx.GlobalNamespace;
public override Namespace ContainingNamespace => Context.GlobalNamespace;
public override Type? ContainingType => null;

View File

@@ -24,7 +24,7 @@ namespace Semmle.Extraction.CIL.Entities
{
parent.WriteId(trapFile);
trapFile.Write('.');
trapFile.Write(Cx.ShortName(ed.Name));
trapFile.Write(Context.ShortName(ed.Name));
trapFile.Write(";cil-event");
}
@@ -39,34 +39,34 @@ namespace Semmle.Extraction.CIL.Entities
{
get
{
var signature = (Type)Cx.CreateGeneric(parent, ed.Type);
var signature = (Type)Context.CreateGeneric(parent, ed.Type);
yield return signature;
yield return Tuples.cil_event(this, parent, Cx.ShortName(ed.Name), signature);
yield return Tuples.cil_event(this, parent, Context.ShortName(ed.Name), signature);
var accessors = ed.GetAccessors();
if (!accessors.Adder.IsNil)
{
var adder = (Method)Cx.CreateGeneric(parent, accessors.Adder);
var adder = (Method)Context.CreateGeneric(parent, accessors.Adder);
yield return adder;
yield return Tuples.cil_adder(this, adder);
}
if (!accessors.Remover.IsNil)
{
var remover = (Method)Cx.CreateGeneric(parent, accessors.Remover);
var remover = (Method)Context.CreateGeneric(parent, accessors.Remover);
yield return remover;
yield return Tuples.cil_remover(this, remover);
}
if (!accessors.Raiser.IsNil)
{
var raiser = (Method)Cx.CreateGeneric(parent, accessors.Raiser);
var raiser = (Method)Context.CreateGeneric(parent, accessors.Raiser);
yield return raiser;
yield return Tuples.cil_raiser(this, raiser);
}
foreach (var c in Attribute.Populate(Cx, this, ed.GetCustomAttributes()))
foreach (var c in Attribute.Populate(Context, this, ed.GetCustomAttributes()))
yield return c;
}
}

View File

@@ -13,7 +13,7 @@ namespace Semmle.Extraction.CIL.Entities
private readonly System.Reflection.Metadata.ExceptionRegion r;
private readonly Dictionary<int, Instruction> jump_table;
public ExceptionRegion(IGenericContext gc, MethodImplementation method, int index, System.Reflection.Metadata.ExceptionRegion r, Dictionary<int, Instruction> jump_table) : base(gc.Cx)
public ExceptionRegion(IGenericContext gc, MethodImplementation method, int index, System.Reflection.Metadata.ExceptionRegion r, Dictionary<int, Instruction> jump_table) : base(gc.Context)
{
this.gc = gc;
this.method = method;
@@ -46,7 +46,7 @@ namespace Semmle.Extraction.CIL.Entities
if (!r.CatchType.IsNil)
{
var catchType = (Type)Cx.CreateGeneric(gc, r.CatchType);
var catchType = (Type)Context.CreateGeneric(gc, r.CatchType);
yield return catchType;
yield return Tuples.cil_handler_type(this, catchType);
}

View File

@@ -11,7 +11,7 @@ namespace Semmle.Extraction.CIL.Entities
public File(Context cx, string path) : base(cx)
{
this.OriginalPath = path;
TransformedPath = Cx.Extractor.PathTransformer.Transform(OriginalPath);
TransformedPath = Context.Extractor.PathTransformer.Transform(OriginalPath);
}
public override void WriteId(TextWriter trapFile)
@@ -33,7 +33,7 @@ namespace Semmle.Extraction.CIL.Entities
{
if (TransformedPath.ParentDirectory is PathTransformer.ITransformedPath dir)
{
var parent = Cx.CreateFolder(dir);
var parent = Context.CreateFolder(dir);
yield return parent;
yield return Tuples.containerparent(parent, this);
}

View File

@@ -24,7 +24,7 @@ namespace Semmle.Extraction.CIL.Entities
{
if (transformedPath.ParentDirectory is PathTransformer.ITransformedPath parent)
{
var parentFolder = Cx.CreateFolder(parent);
var parentFolder = Context.CreateFolder(parent);
yield return parentFolder;
yield return Tuples.containerparent(parentFolder, this);
}

View File

@@ -30,7 +30,7 @@ namespace Semmle.Extraction.CIL.Entities
}
}
public override Namespace? ContainingNamespace => Cx.GlobalNamespace;
public override Namespace? ContainingNamespace => Context.GlobalNamespace;
public override Type? ContainingType => null;
@@ -104,7 +104,7 @@ namespace Semmle.Extraction.CIL.Entities
yield return Tuples.cil_function_pointer_calling_conventions(this, signature.Header.CallingConvention);
foreach (var p in Method.GetParameterExtractionProducts(signature.ParameterTypes, this, this, Cx, 0))
foreach (var p in Method.GetParameterExtractionProducts(signature.ParameterTypes, this, this, Context, 0))
{
yield return p;
}

View File

@@ -356,7 +356,7 @@ namespace Semmle.Extraction.CIL.Entities
switch (PayloadType)
{
case Payload.String:
yield return Tuples.cil_value(this, Cx.MdReader.GetUserString(MetadataTokens.UserStringHandle(payloadValue)));
yield return Tuples.cil_value(this, Context.MdReader.GetUserString(MetadataTokens.UserStringHandle(payloadValue)));
break;
case Payload.Float32:
yield return Tuples.cil_value(this, BitConverter.ToSingle(data, offset).ToString());
@@ -388,7 +388,7 @@ namespace Semmle.Extraction.CIL.Entities
{
// A generic EntityHandle.
var handle = MetadataTokens.EntityHandle(payloadValue);
var target = Cx.CreateGeneric(Method, handle);
var target = Context.CreateGeneric(Method, handle);
yield return target;
if (target != null)
{
@@ -428,11 +428,11 @@ namespace Semmle.Extraction.CIL.Entities
IExtractedEntity? target = null;
try
{
target = Cx.CreateGeneric(Method, handle);
target = Context.CreateGeneric(Method, handle);
}
catch (Exception exc)
{
Cx.Extractor.Logger.Log(Util.Logging.Severity.Warning, $"Couldn't interpret payload of payload type {PayloadType} as a function pointer type. {exc.Message} {exc.StackTrace}");
Context.Extractor.Logger.Log(Util.Logging.Severity.Warning, $"Couldn't interpret payload of payload type {PayloadType} as a function pointer type. {exc.Message} {exc.StackTrace}");
}
if (target != null)
@@ -501,7 +501,7 @@ namespace Semmle.Extraction.CIL.Entities
// TODO: Find a solution to this.
// For now, just log the error
Cx.ExtractionError("A CIL instruction jumps outside the current method", null, Extraction.Entities.GeneratedLocation.Create(Cx), "", Util.Logging.Severity.Warning);
Context.ExtractionError("A CIL instruction jumps outside the current method", null, Extraction.Entities.GeneratedLocation.Create(Context), "", Util.Logging.Severity.Warning);
}
}
}

View File

@@ -11,12 +11,12 @@ namespace Semmle.Extraction.CIL.Entities
private readonly IGenericContext gc;
private readonly Type declType;
public MemberReferenceField(IGenericContext gc, MemberReferenceHandle handle) : base(gc.Cx)
public MemberReferenceField(IGenericContext gc, MemberReferenceHandle handle) : base(gc.Context)
{
this.handle = handle;
this.gc = gc;
mr = Cx.MdReader.GetMemberReference(handle);
declType = (Type)Cx.CreateGeneric(gc, mr.Parent);
mr = Context.MdReader.GetMemberReference(handle);
declType = (Type)Context.CreateGeneric(gc, mr.Parent);
}
public override bool Equals(object? obj)
@@ -29,11 +29,11 @@ namespace Semmle.Extraction.CIL.Entities
return handle.GetHashCode();
}
public override string Name => Cx.GetString(mr.Name);
public override string Name => Context.GetString(mr.Name);
public override Type DeclaringType => declType;
public override Type Type => mr.DecodeFieldSignature(Cx.TypeSignatureDecoder, this);
public override Type Type => mr.DecodeFieldSignature(Context.TypeSignatureDecoder, this);
public override IEnumerable<Type> TypeParameters => gc.TypeParameters.Concat(declType.TypeParameters);

View File

@@ -19,11 +19,11 @@ namespace Semmle.Extraction.CIL.Entities
{
this.handle = handle;
this.gc = gc;
mr = Cx.MdReader.GetMemberReference(handle);
mr = Context.MdReader.GetMemberReference(handle);
signature = mr.DecodeMethodSignature(new SignatureDecoder(), gc);
parent = (IGenericContext)Cx.CreateGeneric(gc, mr.Parent);
parent = (IGenericContext)Context.CreateGeneric(gc, mr.Parent);
var declType = parent is Method parentMethod
? parentMethod.DeclaringType
@@ -33,7 +33,7 @@ namespace Semmle.Extraction.CIL.Entities
throw new InternalError("Parent context of method is not a type");
declaringType = declType;
nameLabel = Cx.GetString(mr.Name);
nameLabel = Context.GetString(mr.Name);
var typeSourceDeclaration = declaringType.SourceDeclaration;
sourceDeclaration = typeSourceDeclaration == declaringType ? (Method)this : typeSourceDeclaration.LookupMethod(mr.Name, mr.Signature);
@@ -59,7 +59,7 @@ namespace Semmle.Extraction.CIL.Entities
public override Type DeclaringType => declaringType;
public override string Name => Cx.ShortName(mr.Name);
public override string Name => Context.ShortName(mr.Name);
public override IEnumerable<Type> TypeParameters => parent.TypeParameters.Concat(gc.TypeParameters);
@@ -69,12 +69,12 @@ namespace Semmle.Extraction.CIL.Entities
{
genericParams = new MethodTypeParameter[signature.GenericParameterCount];
for (var p = 0; p < genericParams.Length; ++p)
genericParams[p] = Cx.Populate(new MethodTypeParameter(this, this, p));
genericParams[p] = Context.Populate(new MethodTypeParameter(this, this, p));
foreach (var p in genericParams)
yield return p;
var typeSignature = mr.DecodeMethodSignature(Cx.TypeSignatureDecoder, this);
var typeSignature = mr.DecodeMethodSignature(Context.TypeSignatureDecoder, this);
var parameters = GetParameterExtractionProducts(typeSignature.ParameterTypes).ToArray();
Parameters = parameters.OfType<Parameter>().ToArray();

View File

@@ -15,7 +15,7 @@ namespace Semmle.Extraction.CIL.Entities
protected IGenericContext gc;
protected MethodSignature<ITypeSignature> signature;
protected Method(IGenericContext gc) : base(gc.Cx)
protected Method(IGenericContext gc) : base(gc.Context)
{
this.gc = gc;
}
@@ -83,10 +83,10 @@ namespace Semmle.Extraction.CIL.Entities
if (!IsStatic)
{
yield return Cx.Populate(new Parameter(Cx, this, i++, DeclaringType));
yield return Context.Populate(new Parameter(Context, this, i++, DeclaringType));
}
foreach (var p in GetParameterExtractionProducts(parameterTypes, this, this, Cx, i))
foreach (var p in GetParameterExtractionProducts(parameterTypes, this, this, Context, i))
{
yield return p;
}

View File

@@ -10,7 +10,7 @@ namespace Semmle.Extraction.CIL.Entities
{
private readonly Method m;
public MethodImplementation(Method m) : base(m.Cx)
public MethodImplementation(Method m) : base(m.Context)
{
this.m = m;
}
@@ -19,7 +19,7 @@ namespace Semmle.Extraction.CIL.Entities
{
get
{
yield return Tuples.cil_method_implementation(this, m, Cx.Assembly);
yield return Tuples.cil_method_implementation(this, m, Context.Assembly);
}
}
}

View File

@@ -21,9 +21,9 @@ namespace Semmle.Extraction.CIL.Entities
public MethodSpecificationMethod(IGenericContext gc, MethodSpecificationHandle handle) : base(gc)
{
this.handle = handle;
ms = Cx.MdReader.GetMethodSpecification(handle);
typeParams = ms.DecodeSignature(Cx.TypeSignatureDecoder, gc);
unboundMethod = (Method)Cx.CreateGeneric(gc, ms.Method);
ms = Context.MdReader.GetMethodSpecification(handle);
typeParams = ms.DecodeSignature(Context.TypeSignatureDecoder, gc);
unboundMethod = (Method)Context.CreateGeneric(gc, ms.Method);
}
public override void WriteId(TextWriter trapFile)
@@ -66,12 +66,12 @@ namespace Semmle.Extraction.CIL.Entities
switch (ms.Method.Kind)
{
case HandleKind.MemberReference:
var mr = Cx.MdReader.GetMemberReference((MemberReferenceHandle)ms.Method);
constructedTypeSignature = mr.DecodeMethodSignature(Cx.TypeSignatureDecoder, this);
var mr = Context.MdReader.GetMemberReference((MemberReferenceHandle)ms.Method);
constructedTypeSignature = mr.DecodeMethodSignature(Context.TypeSignatureDecoder, this);
break;
case HandleKind.MethodDefinition:
var md = Cx.MdReader.GetMethodDefinition((MethodDefinitionHandle)ms.Method);
constructedTypeSignature = md.DecodeSignature(Cx.TypeSignatureDecoder, this);
var md = Context.MdReader.GetMethodDefinition((MethodDefinitionHandle)ms.Method);
constructedTypeSignature = md.DecodeSignature(Context.TypeSignatureDecoder, this);
break;
default:
throw new InternalError($"Unexpected constructed method handle kind {ms.Method.Kind}");

View File

@@ -38,11 +38,11 @@ namespace Semmle.Extraction.CIL.Entities
unboundGenericType = nameParser.UnboundGenericTypeName == null
? this
: new NoMetadataHandleType(Cx, nameParser.UnboundGenericTypeName);
: new NoMetadataHandleType(Context, nameParser.UnboundGenericTypeName);
if (nameParser.TypeArguments != null)
{
thisTypeArguments = nameParser.TypeArguments.Select(t => new NoMetadataHandleType(Cx, t)).ToArray();
thisTypeArguments = nameParser.TypeArguments.Select(t => new NoMetadataHandleType(Context, t)).ToArray();
}
else
{
@@ -51,14 +51,14 @@ namespace Semmle.Extraction.CIL.Entities
containingType = isContainerNamespace
? null
: new NoMetadataHandleType(Cx, containerName);
: new NoMetadataHandleType(Context, containerName);
containingNamespace = isContainerNamespace
? containerName == Cx.GlobalNamespace.Name
? Cx.GlobalNamespace
: containerName == Cx.SystemNamespace.Name
? Cx.SystemNamespace
: new Namespace(Cx, containerName)
? containerName == Context.GlobalNamespace.Name
? Context.GlobalNamespace
: containerName == Context.SystemNamespace.Name
? Context.SystemNamespace
: new Namespace(Context, containerName)
: null;
Populate();
@@ -68,10 +68,10 @@ namespace Semmle.Extraction.CIL.Entities
{
if (ContainingNamespace is object)
{
Cx.Populate(ContainingNamespace);
Context.Populate(ContainingNamespace);
}
Cx.Populate(this);
Context.Populate(this);
}
public override bool Equals(object? obj)
@@ -118,7 +118,7 @@ namespace Semmle.Extraction.CIL.Entities
if (TotalTypeParametersCount != typeArguments.Count())
throw new InternalError("Mismatched type arguments");
return Cx.Populate(new ConstructedType(Cx, this, typeArguments));
return Context.Populate(new ConstructedType(Context, this, typeArguments));
}
public override void WriteAssemblyPrefix(TextWriter trapFile)
@@ -133,7 +133,7 @@ namespace Semmle.Extraction.CIL.Entities
}
else
{
Cx.WriteAssemblyPrefix(trapFile);
Context.WriteAssemblyPrefix(trapFile);
}
}

View File

@@ -21,9 +21,9 @@ namespace Semmle.Extraction.CIL.Entities
var text = file.Contents;
if (text == null)
Cx.Extractor.Logger.Log(Util.Logging.Severity.Warning, string.Format("PDB source file {0} could not be found", OriginalPath));
Context.Extractor.Logger.Log(Util.Logging.Severity.Warning, string.Format("PDB source file {0} could not be found", OriginalPath));
else
Cx.TrapWriter.Archive(TransformedPath, text);
Context.TrapWriter.Archive(TransformedPath, text);
yield return Tuples.file_extraction_mode(this, 2);
}

View File

@@ -27,7 +27,7 @@ namespace Semmle.Extraction.CIL.Entities
public override string Name => typeCode.Id();
public override Namespace ContainingNamespace => Cx.SystemNamespace;
public override Namespace ContainingNamespace => Context.SystemNamespace;
public override Type? ContainingType => null;

View File

@@ -15,11 +15,11 @@ namespace Semmle.Extraction.CIL.Entities
private readonly PropertyDefinition pd;
private readonly IGenericContext gc;
public Property(IGenericContext gc, Type type, PropertyDefinitionHandle handle) : base(gc.Cx)
public Property(IGenericContext gc, Type type, PropertyDefinitionHandle handle) : base(gc.Context)
{
this.gc = gc;
this.handle = handle;
pd = Cx.MdReader.GetPropertyDefinition(handle);
pd = Context.MdReader.GetPropertyDefinition(handle);
this.type = type;
}
@@ -27,7 +27,7 @@ namespace Semmle.Extraction.CIL.Entities
{
trapFile.WriteSubId(type);
trapFile.Write('.');
trapFile.Write(Cx.GetString(pd.Name));
trapFile.Write(Context.GetString(pd.Name));
trapFile.Write("(");
var index = 0;
var signature = pd.DecodeSignature(new SignatureDecoder(), gc);
@@ -51,10 +51,10 @@ namespace Semmle.Extraction.CIL.Entities
{
get
{
yield return Tuples.metadata_handle(this, Cx.Assembly, MetadataTokens.GetToken(handle));
var sig = pd.DecodeSignature(Cx.TypeSignatureDecoder, type);
yield return Tuples.metadata_handle(this, Context.Assembly, MetadataTokens.GetToken(handle));
var sig = pd.DecodeSignature(Context.TypeSignatureDecoder, type);
var name = Cx.ShortName(pd.Name);
var name = Context.ShortName(pd.Name);
var t = sig.ReturnType;
if (t is ModifiedType mt)
@@ -72,19 +72,19 @@ namespace Semmle.Extraction.CIL.Entities
var accessors = pd.GetAccessors();
if (!accessors.Getter.IsNil)
{
var getter = (Method)Cx.CreateGeneric(type, accessors.Getter);
var getter = (Method)Context.CreateGeneric(type, accessors.Getter);
yield return getter;
yield return Tuples.cil_getter(this, getter);
}
if (!accessors.Setter.IsNil)
{
var setter = (Method)Cx.CreateGeneric(type, accessors.Setter);
var setter = (Method)Context.CreateGeneric(type, accessors.Setter);
yield return setter;
yield return Tuples.cil_setter(this, setter);
}
foreach (var c in Attribute.Populate(Cx, this, pd.GetCustomAttributes()))
foreach (var c in Attribute.Populate(Context, this, pd.GetCustomAttributes()))
yield return c;
}
}

View File

@@ -250,7 +250,7 @@ namespace Semmle.Extraction.CIL.Entities
public void WriteId(TextWriter trapFile, IGenericContext gc)
{
var type = (Type)gc.Cx.Create(handle);
var type = (Type)gc.Context.Create(handle);
type.WriteId(trapFile);
}
}
@@ -271,7 +271,7 @@ namespace Semmle.Extraction.CIL.Entities
public void WriteId(TextWriter trapFile, IGenericContext gc)
{
var type = (Type)gc.Cx.Create(handle);
var type = (Type)gc.Context.Create(handle);
type.WriteId(trapFile);
}
}

View File

@@ -174,7 +174,7 @@ namespace Semmle.Extraction.CIL.Entities
{
if (TryGetPrimitiveTypeCode(out var code))
{
t = Cx.Create(code);
t = Context.Create(code);
return true;
}
@@ -185,7 +185,7 @@ namespace Semmle.Extraction.CIL.Entities
private bool TryGetPrimitiveTypeCode(out PrimitiveTypeCode code)
{
if (ContainingType == null &&
ContainingNamespace?.Name == Cx.SystemNamespace.Name &&
ContainingNamespace?.Name == Context.SystemNamespace.Name &&
primitiveTypeCodeMapping.TryGetValue(Name, out code))
{
return true;
@@ -200,6 +200,6 @@ namespace Semmle.Extraction.CIL.Entities
public sealed override IEnumerable<Type> MethodParameters => Enumerable.Empty<Type>();
public static Type DecodeType(IGenericContext gc, TypeSpecificationHandle handle) =>
gc.Cx.MdReader.GetTypeSpecification(handle).DecodeSignature(gc.Cx.TypeSignatureDecoder, gc);
gc.Context.MdReader.GetTypeSpecification(handle).DecodeSignature(gc.Context.TypeSignatureDecoder, gc);
}
}

View File

@@ -45,9 +45,9 @@ namespace Semmle.Extraction.CIL.Entities
idWriter.WriteId(trapFile, inContext);
}
public override string Name => GenericsHelper.GetNonGenericName(td.Name, Cx.MdReader);
public override string Name => GenericsHelper.GetNonGenericName(td.Name, Context.MdReader);
public override Namespace ContainingNamespace => Cx.Create(td.NamespaceDefinition);
public override Namespace ContainingNamespace => Context.Create(td.NamespaceDefinition);
public override Type? ContainingType => declType;
@@ -58,14 +58,14 @@ namespace Semmle.Extraction.CIL.Entities
if (TotalTypeParametersCount != typeArguments.Count())
throw new InternalError("Mismatched type arguments");
return Cx.Populate(new ConstructedType(Cx, this, typeArguments));
return Context.Populate(new ConstructedType(Context, this, typeArguments));
}
public override void WriteAssemblyPrefix(TextWriter trapFile)
{
var ct = ContainingType;
if (ct is null)
Cx.WriteAssemblyPrefix(trapFile);
Context.WriteAssemblyPrefix(trapFile);
else if (IsPrimitiveType)
trapFile.Write(Type.PrimitiveTypePrefix);
else
@@ -83,7 +83,7 @@ namespace Semmle.Extraction.CIL.Entities
// Two-phase population because type parameters can be mutually dependent
for (var i = 0; i < newTypeParams.Length; ++i)
newTypeParams[i] = Cx.Populate(new TypeTypeParameter(this, i));
newTypeParams[i] = Context.Populate(new TypeTypeParameter(this, i));
for (var i = 0; i < newTypeParams.Length; ++i)
newTypeParams[i].PopulateHandle(genericParams[i + toSkip]);
return newTypeParams;
@@ -96,7 +96,7 @@ namespace Semmle.Extraction.CIL.Entities
var containingType = td.GetDeclaringType();
var parentTypeParameters = containingType.IsNil
? 0
: Cx.MdReader.GetTypeDefinition(containingType).GetGenericParameters().Count;
: Context.MdReader.GetTypeDefinition(containingType).GetGenericParameters().Count;
return td.GetGenericParameters().Count - parentTypeParameters;
}
@@ -110,7 +110,7 @@ namespace Semmle.Extraction.CIL.Entities
{
get
{
yield return Tuples.metadata_handle(this, Cx.Assembly, MetadataTokens.GetToken(handle));
yield return Tuples.metadata_handle(this, Context.Assembly, MetadataTokens.GetToken(handle));
foreach (var c in base.Contents) yield return c;
@@ -119,7 +119,7 @@ namespace Semmle.Extraction.CIL.Entities
foreach (var f in td.GetFields())
{
// Populate field if needed
yield return Cx.CreateGeneric(this, f);
yield return Context.CreateGeneric(this, f);
}
foreach (var prop in td.GetProperties())
@@ -129,16 +129,16 @@ namespace Semmle.Extraction.CIL.Entities
foreach (var @event in td.GetEvents())
{
yield return new Event(Cx, this, @event);
yield return new Event(Context, this, @event);
}
foreach (var a in Attribute.Populate(Cx, this, td.GetCustomAttributes()))
foreach (var a in Attribute.Populate(Context, this, td.GetCustomAttributes()))
yield return a;
foreach (var impl in td.GetMethodImplementations().Select(i => Cx.MdReader.GetMethodImplementation(i)))
foreach (var impl in td.GetMethodImplementations().Select(i => Context.MdReader.GetMethodImplementation(i)))
{
var m = (Method)Cx.CreateGeneric(this, impl.MethodBody);
var decl = (Method)Cx.CreateGeneric(this, impl.MethodDeclaration);
var m = (Method)Context.CreateGeneric(this, impl.MethodBody);
var decl = (Method)Context.CreateGeneric(this, impl.MethodDeclaration);
yield return m;
yield return decl;
@@ -166,7 +166,7 @@ namespace Semmle.Extraction.CIL.Entities
if (!td.BaseType.IsNil)
{
var @base = (Type)Cx.CreateGeneric(this, td.BaseType);
var @base = (Type)Context.CreateGeneric(this, td.BaseType);
yield return @base;
yield return Tuples.cil_base_class(this, @base);
@@ -174,21 +174,21 @@ namespace Semmle.Extraction.CIL.Entities
GetUnderlyingEnumType() is var underlying &&
underlying.HasValue)
{
var underlyingType = Cx.Create(underlying.Value);
var underlyingType = Context.Create(underlying.Value);
yield return underlyingType;
yield return Tuples.cil_enum_underlying_type(this, underlyingType);
}
}
foreach (var @interface in td.GetInterfaceImplementations().Select(i => Cx.MdReader.GetInterfaceImplementation(i)))
foreach (var @interface in td.GetInterfaceImplementations().Select(i => Context.MdReader.GetInterfaceImplementation(i)))
{
var t = (Type)Cx.CreateGeneric(this, @interface.Interface);
var t = (Type)Context.CreateGeneric(this, @interface.Interface);
yield return t;
yield return Tuples.cil_base_interface(this, t);
}
// Only type definitions have locations.
yield return Tuples.cil_type_location(this, Cx.Assembly);
yield return Tuples.cil_type_location(this, Context.Assembly);
}
}
@@ -204,36 +204,36 @@ namespace Semmle.Extraction.CIL.Entities
private bool IsSystemEnum(TypeReferenceHandle baseType)
{
var baseTypeReference = Cx.MdReader.GetTypeReference(baseType);
var baseTypeReference = Context.MdReader.GetTypeReference(baseType);
return IsSystemEnum(baseTypeReference.Name, baseTypeReference.Namespace);
}
private bool IsSystemEnum(TypeDefinitionHandle baseType)
{
var baseTypeDefinition = Cx.MdReader.GetTypeDefinition(baseType);
var baseTypeDefinition = Context.MdReader.GetTypeDefinition(baseType);
return IsSystemEnum(baseTypeDefinition.Name, baseTypeDefinition.Namespace);
}
private bool IsSystemEnum(StringHandle typeName, StringHandle namespaceName)
{
return Cx.MdReader.StringComparer.Equals(typeName, "Enum") &&
return Context.MdReader.StringComparer.Equals(typeName, "Enum") &&
!namespaceName.IsNil &&
Cx.MdReader.StringComparer.Equals(namespaceName, "System");
Context.MdReader.StringComparer.Equals(namespaceName, "System");
}
internal PrimitiveTypeCode? GetUnderlyingEnumType()
{
foreach (var handle in td.GetFields())
{
var field = Cx.MdReader.GetFieldDefinition(handle);
var field = Context.MdReader.GetFieldDefinition(handle);
if (field.Attributes.HasFlag(FieldAttributes.Static))
{
continue;
}
var blob = Cx.MdReader.GetBlobReader(field.Signature);
var blob = Context.MdReader.GetBlobReader(field.Signature);
if (blob.ReadSignatureHeader().Kind != SignatureKind.Field)
{
break;
@@ -249,11 +249,11 @@ namespace Semmle.Extraction.CIL.Entities
{
foreach (var h in td.GetMethods())
{
var md = Cx.MdReader.GetMethodDefinition(h);
var md = Context.MdReader.GetMethodDefinition(h);
if (md.Name == name && md.Signature == signature)
{
return (Method)Cx.Create(h);
return (Method)Context.Create(h);
}
}

View File

@@ -11,7 +11,7 @@ namespace Semmle.Extraction.CIL.Entities
{
protected readonly IGenericContext gc;
protected TypeParameter(IGenericContext gc) : base(gc.Cx)
protected TypeParameter(IGenericContext gc) : base(gc.Context)
{
this.gc = gc;
}
@@ -32,7 +32,7 @@ namespace Semmle.Extraction.CIL.Entities
{
if (!parameterHandle.IsNil)
{
var tp = Cx.MdReader.GetGenericParameter(parameterHandle);
var tp = Context.MdReader.GetGenericParameter(parameterHandle);
if (tp.Attributes.HasFlag(GenericParameterAttributes.Contravariant))
yield return Tuples.cil_typeparam_contravariant(this);
@@ -45,9 +45,9 @@ namespace Semmle.Extraction.CIL.Entities
if (tp.Attributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint))
yield return Tuples.cil_typeparam_struct(this);
foreach (var constraint in tp.GetConstraints().Select(h => Cx.MdReader.GetGenericParameterConstraint(h)))
foreach (var constraint in tp.GetConstraints().Select(h => Context.MdReader.GetGenericParameterConstraint(h)))
{
var t = (Type)Cx.CreateGeneric(this.gc, constraint.Type);
var t = (Type)Context.CreateGeneric(this.gc, constraint.Type);
yield return t;
yield return Tuples.cil_typeparam_constraint(this, t);
}

View File

@@ -46,16 +46,16 @@ namespace Semmle.Extraction.CIL.Entities
}
}
public override string Name => GenericsHelper.GetNonGenericName(tr.Name, Cx.MdReader);
public override string Name => GenericsHelper.GetNonGenericName(tr.Name, Context.MdReader);
public override Namespace ContainingNamespace => Cx.CreateNamespace(tr.Namespace);
public override Namespace ContainingNamespace => Context.CreateNamespace(tr.Namespace);
public override Type? ContainingType
{
get
{
return tr.ResolutionScope.Kind == HandleKind.TypeReference
? (Type)Cx.Create((TypeReferenceHandle)tr.ResolutionScope)
? (Type)Context.Create((TypeReferenceHandle)tr.ResolutionScope)
: null;
}
}
@@ -70,19 +70,19 @@ namespace Semmle.Extraction.CIL.Entities
ContainingType!.WriteAssemblyPrefix(trapFile);
break;
case HandleKind.AssemblyReference:
var assemblyDef = Cx.MdReader.GetAssemblyReference((AssemblyReferenceHandle)tr.ResolutionScope);
trapFile.Write(Cx.GetString(assemblyDef.Name));
var assemblyDef = Context.MdReader.GetAssemblyReference((AssemblyReferenceHandle)tr.ResolutionScope);
trapFile.Write(Context.GetString(assemblyDef.Name));
trapFile.Write('_');
trapFile.Write(assemblyDef.Version.ToString());
trapFile.Write(Entities.Type.AssemblyTypeNameSeparator);
break;
default:
Cx.WriteAssemblyPrefix(trapFile);
Context.WriteAssemblyPrefix(trapFile);
break;
}
}
public override int ThisTypeParameterCount => GenericsHelper.GetGenericTypeParameterCount(tr.Name, Cx.MdReader);
public override int ThisTypeParameterCount => GenericsHelper.GetGenericTypeParameterCount(tr.Name, Context.MdReader);
public override IEnumerable<Type> TypeParameters => GenericsHelper.GetAllTypeParameters(ContainingType, typeParams!.Value);
@@ -98,7 +98,7 @@ namespace Semmle.Extraction.CIL.Entities
if (TotalTypeParametersCount != typeArguments.Count())
throw new InternalError("Mismatched type arguments");
return Cx.Populate(new ConstructedType(Cx, this, typeArguments));
return Context.Populate(new ConstructedType(Context, this, typeArguments));
}
}
}