|
| 1 | +using System; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using System.Linq; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using Semmle.Util; |
| 7 | + |
| 8 | +namespace Semmle.Extraction.CIL.Entities |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// A constructed type. |
| 12 | + /// </summary> |
| 13 | + public sealed class ConstructedType : Type |
| 14 | + { |
| 15 | + private readonly Type unboundGenericType; |
| 16 | + |
| 17 | + // Either null or notEmpty |
| 18 | + private readonly Type[]? thisTypeArguments; |
| 19 | + |
| 20 | + public override IEnumerable<Type> ThisTypeArguments => thisTypeArguments.EnumerateNull(); |
| 21 | + |
| 22 | + public override IEnumerable<Type> ThisGenericArguments => thisTypeArguments.EnumerateNull(); |
| 23 | + |
| 24 | + public override IEnumerable<IExtractionProduct> Contents |
| 25 | + { |
| 26 | + get |
| 27 | + { |
| 28 | + foreach (var c in base.Contents) |
| 29 | + yield return c; |
| 30 | + |
| 31 | + var i = 0; |
| 32 | + foreach (var type in ThisGenericArguments) |
| 33 | + { |
| 34 | + yield return type; |
| 35 | + yield return Tuples.cil_type_argument(this, i++, type); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public override Type SourceDeclaration => unboundGenericType; |
| 41 | + |
| 42 | + public ConstructedType(Context cx, Type unboundType, IEnumerable<Type> typeArguments) : base(cx) |
| 43 | + { |
| 44 | + var suppliedArgs = typeArguments.Count(); |
| 45 | + if (suppliedArgs != unboundType.TotalTypeParametersCheck) |
| 46 | + throw new InternalError("Unexpected number of type arguments in ConstructedType"); |
| 47 | + |
| 48 | + unboundGenericType = unboundType; |
| 49 | + var thisParams = unboundType.ThisTypeParameters; |
| 50 | + |
| 51 | + if (typeArguments.Count() == thisParams) |
| 52 | + { |
| 53 | + containingType = unboundType.ContainingType; |
| 54 | + thisTypeArguments = typeArguments.ToArray(); |
| 55 | + } |
| 56 | + else if (thisParams == 0) |
| 57 | + { |
| 58 | + // all type arguments belong to containing type |
| 59 | + containingType = unboundType.ContainingType!.Construct(typeArguments); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + // some type arguments belong to containing type |
| 64 | + var parentParams = suppliedArgs - thisParams; |
| 65 | + containingType = unboundType.ContainingType!.Construct(typeArguments.Take(parentParams)); |
| 66 | + thisTypeArguments = typeArguments.Skip(parentParams).ToArray(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public override bool Equals(object? obj) |
| 71 | + { |
| 72 | + if (obj is ConstructedType t && Equals(unboundGenericType, t.unboundGenericType) && Equals(containingType, t.containingType)) |
| 73 | + { |
| 74 | + if (thisTypeArguments is null) |
| 75 | + return t.thisTypeArguments is null; |
| 76 | + if (!(t.thisTypeArguments is null)) |
| 77 | + return thisTypeArguments.SequenceEqual(t.thisTypeArguments); |
| 78 | + } |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + public override int GetHashCode() |
| 83 | + { |
| 84 | + var h = unboundGenericType.GetHashCode(); |
| 85 | + h = 13 * h + (containingType is null ? 0 : containingType.GetHashCode()); |
| 86 | + if (!(thisTypeArguments is null)) |
| 87 | + h = h * 13 + thisTypeArguments.SequenceHash(); |
| 88 | + return h; |
| 89 | + } |
| 90 | + |
| 91 | + private readonly Type? containingType; |
| 92 | + public override Type? ContainingType => containingType; |
| 93 | + |
| 94 | + public override string Name => unboundGenericType.Name; |
| 95 | + |
| 96 | + public override Namespace Namespace => unboundGenericType.Namespace!; |
| 97 | + |
| 98 | + public override int ThisTypeParameters => thisTypeArguments == null ? 0 : thisTypeArguments.Length; |
| 99 | + |
| 100 | + public override CilTypeKind Kind => unboundGenericType.Kind; |
| 101 | + |
| 102 | + public override Type Construct(IEnumerable<Type> typeArguments) |
| 103 | + { |
| 104 | + throw new NotImplementedException(); |
| 105 | + } |
| 106 | + |
| 107 | + public override void WriteId(TextWriter trapFile, bool inContext) |
| 108 | + { |
| 109 | + if (ContainingType != null) |
| 110 | + { |
| 111 | + ContainingType.GetId(trapFile, inContext); |
| 112 | + trapFile.Write('.'); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + WriteAssemblyPrefix(trapFile); |
| 117 | + |
| 118 | + if (!Namespace.IsGlobalNamespace) |
| 119 | + { |
| 120 | + Namespace.WriteId(trapFile); |
| 121 | + trapFile.Write('.'); |
| 122 | + } |
| 123 | + } |
| 124 | + trapFile.Write(unboundGenericType.Name); |
| 125 | + |
| 126 | + if (thisTypeArguments != null && thisTypeArguments.Any()) |
| 127 | + { |
| 128 | + trapFile.Write('<'); |
| 129 | + var index = 0; |
| 130 | + foreach (var t in thisTypeArguments) |
| 131 | + { |
| 132 | + trapFile.WriteSeparator(",", ref index); |
| 133 | + t.WriteId(trapFile); |
| 134 | + } |
| 135 | + trapFile.Write('>'); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public override void WriteAssemblyPrefix(TextWriter trapFile) => unboundGenericType.WriteAssemblyPrefix(trapFile); |
| 140 | + |
| 141 | + public override IEnumerable<Type> TypeParameters => GenericArguments; |
| 142 | + |
| 143 | + public override IEnumerable<Type> MethodParameters => throw new NotImplementedException(); |
| 144 | + } |
| 145 | +} |
0 commit comments