|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using Semmle.Util; |
| 7 | + |
| 8 | +namespace Semmle.Extraction.CIL.Entities |
| 9 | +{ |
| 10 | + internal sealed class NoMetadataHandleType : Type |
| 11 | + { |
| 12 | + private readonly string originalName; |
| 13 | + private readonly string name; |
| 14 | + private readonly string? assemblyName; |
| 15 | + private readonly string containerName; |
| 16 | + private readonly bool isContainerNamespace; |
| 17 | + |
| 18 | + private readonly Lazy<TypeTypeParameter[]> typeParams; |
| 19 | + |
| 20 | + // Either null or notEmpty |
| 21 | + private readonly Type[]? thisTypeArguments; |
| 22 | + private readonly Type unboundGenericType; |
| 23 | + |
| 24 | + private readonly NamedTypeIdWriter idWriter; |
| 25 | + |
| 26 | + public NoMetadataHandleType(Context cx, string originalName) : base(cx) |
| 27 | + { |
| 28 | + this.originalName = originalName; |
| 29 | + this.name = originalName; |
| 30 | + this.idWriter = new NamedTypeIdWriter(this); |
| 31 | + |
| 32 | + // N1.N2.T1`3+T2`1[T3,[T4, Assembly1, Version=...],T5,T6], Assembly2, Version=... |
| 33 | + // for example: |
| 34 | + // typeof(System.Collections.Generic.List<int>.Enumerator) |
| 35 | + // -> System.Collections.Generic.List`1+Enumerator[[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e |
| 36 | + // typeof(System.Collections.Generic.List<>.Enumerator) |
| 37 | + // -> System.Collections.Generic.List`1+Enumerator, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e |
| 38 | + |
| 39 | + var lastBracketIndex = name.LastIndexOf(']'); |
| 40 | + var assemblyCommaIndex = name.IndexOf(',', lastBracketIndex < 0 ? 0 : lastBracketIndex); |
| 41 | + if (assemblyCommaIndex >= 0) |
| 42 | + { |
| 43 | + assemblyName = name.Substring(assemblyCommaIndex + 2); |
| 44 | + name = name.Substring(0, assemblyCommaIndex); |
| 45 | + } |
| 46 | + |
| 47 | + var firstBracketIndex = name.IndexOf('['); |
| 48 | + if (firstBracketIndex >= 0) |
| 49 | + { |
| 50 | + // TODO: |
| 51 | + // * create types for arguments. |
| 52 | + // * this type is a constructed generic -> create non constructed one too. |
| 53 | + // * adjust containing type name, which can also be a constructed generic |
| 54 | + |
| 55 | + // thisTypeArguments = |
| 56 | + // unboundGenericType = |
| 57 | + // containerName = |
| 58 | + |
| 59 | + throw new NotImplementedException(); |
| 60 | + // name = name.Substring(0, firstBracketIndex); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + thisTypeArguments = null; |
| 65 | + unboundGenericType = this; |
| 66 | + } |
| 67 | + |
| 68 | + var lastPlusIndex = name.LastIndexOf('+'); |
| 69 | + if (lastPlusIndex >= 0) |
| 70 | + { |
| 71 | + containerName = name.Substring(0, lastPlusIndex); |
| 72 | + name = name.Substring(lastPlusIndex + 1); |
| 73 | + isContainerNamespace = false; |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + var lastDotIndex = name.LastIndexOf('.'); |
| 78 | + if (lastDotIndex >= 0) |
| 79 | + { |
| 80 | + containerName = name.Substring(0, lastDotIndex); |
| 81 | + name = name.Substring(lastDotIndex + 1); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + containerName = cx.GlobalNamespace.Name; |
| 86 | + } |
| 87 | + isContainerNamespace = true; |
| 88 | + } |
| 89 | + |
| 90 | + this.typeParams = new Lazy<TypeTypeParameter[]>(GenericsHelper.MakeTypeParameters(this, ThisTypeParameterCount)); |
| 91 | + |
| 92 | + if (isContainerNamespace && |
| 93 | + !ContainingNamespace!.IsGlobalNamespace) |
| 94 | + { |
| 95 | + cx.Populate(ContainingNamespace); |
| 96 | + } |
| 97 | + |
| 98 | + cx.Populate(this); |
| 99 | + } |
| 100 | + |
| 101 | + public override bool Equals(object? obj) |
| 102 | + { |
| 103 | + return obj is NoMetadataHandleType t && originalName.Equals(t.originalName, StringComparison.Ordinal); |
| 104 | + } |
| 105 | + |
| 106 | + public override int GetHashCode() |
| 107 | + { |
| 108 | + return originalName.GetHashCode(StringComparison.Ordinal); |
| 109 | + } |
| 110 | + |
| 111 | + public override IEnumerable<IExtractionProduct> Contents |
| 112 | + { |
| 113 | + get |
| 114 | + { |
| 115 | + foreach (var tp in typeParams.Value) |
| 116 | + yield return tp; |
| 117 | + |
| 118 | + foreach (var c in base.Contents) |
| 119 | + yield return c; |
| 120 | + |
| 121 | + var i = 0; |
| 122 | + foreach (var type in ThisTypeArguments) |
| 123 | + { |
| 124 | + yield return type; |
| 125 | + yield return Tuples.cil_type_argument(this, i++, type); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public override CilTypeKind Kind => CilTypeKind.ValueOrRefType; |
| 131 | + |
| 132 | + public override string Name => GenericsHelper.GetNonGenericName(name); |
| 133 | + |
| 134 | + public override Namespace? ContainingNamespace => isContainerNamespace |
| 135 | + ? containerName == Cx.GlobalNamespace.Name |
| 136 | + ? Cx.GlobalNamespace |
| 137 | + : new Namespace(Cx, containerName) |
| 138 | + : null; |
| 139 | + |
| 140 | + public override Type? ContainingType => isContainerNamespace |
| 141 | + ? null |
| 142 | + : new NoMetadataHandleType( |
| 143 | + Cx, |
| 144 | + string.IsNullOrWhiteSpace(assemblyName) |
| 145 | + ? containerName |
| 146 | + : containerName + ", " + assemblyName); |
| 147 | + |
| 148 | + public override int ThisTypeParameterCount => GenericsHelper.GetGenericTypeParameterCount(name); |
| 149 | + |
| 150 | + public override IEnumerable<Type> TypeParameters => typeParams.Value; |
| 151 | + |
| 152 | + public override IEnumerable<Type> ThisTypeArguments => thisTypeArguments.EnumerateNull(); |
| 153 | + |
| 154 | + public override Type SourceDeclaration => unboundGenericType; |
| 155 | + |
| 156 | + public override Type Construct(IEnumerable<Type> typeArguments) |
| 157 | + { |
| 158 | + if (TotalTypeParametersCount != typeArguments.Count()) |
| 159 | + throw new InternalError("Mismatched type arguments"); |
| 160 | + |
| 161 | + return Cx.Populate(new ConstructedType(Cx, this, typeArguments)); |
| 162 | + } |
| 163 | + |
| 164 | + public override void WriteAssemblyPrefix(TextWriter trapFile) |
| 165 | + { |
| 166 | + if (!string.IsNullOrWhiteSpace(assemblyName)) |
| 167 | + { |
| 168 | + var an = new AssemblyName(assemblyName); |
| 169 | + trapFile.Write(an.Name); |
| 170 | + trapFile.Write('_'); |
| 171 | + trapFile.Write((an.Version ?? new Version(0, 0, 0, 0)).ToString()); |
| 172 | + trapFile.Write(Type.AssemblyTypeNameSeparator); |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + Cx.WriteAssemblyPrefix(trapFile); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + public override void WriteId(TextWriter trapFile, bool inContext) |
| 181 | + { |
| 182 | + idWriter.WriteId(trapFile, inContext); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments