|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection.Metadata; |
| 5 | + |
| 6 | +namespace Semmle.Extraction.CIL.Entities |
| 7 | +{ |
| 8 | + internal sealed class FunctionPointerType : Type, IParameterizable, ICustomModifierReceiver |
| 9 | + { |
| 10 | + private readonly MethodSignature<Type> signature; |
| 11 | + |
| 12 | + public FunctionPointerType(Context cx, MethodSignature<Type> signature) : base(cx) |
| 13 | + { |
| 14 | + this.signature = signature; |
| 15 | + } |
| 16 | + |
| 17 | + public override CilTypeKind Kind => CilTypeKind.FunctionPointer; |
| 18 | + |
| 19 | + public override string Name |
| 20 | + { |
| 21 | + get |
| 22 | + { |
| 23 | + using var id = new StringWriter(); |
| 24 | + WriteName( |
| 25 | + id.Write, |
| 26 | + t => id.Write(t.Name), |
| 27 | + signature |
| 28 | + ); |
| 29 | + return id.ToString(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public override Namespace? ContainingNamespace => Cx.GlobalNamespace; |
| 34 | + |
| 35 | + public override Type? ContainingType => null; |
| 36 | + |
| 37 | + public override int ThisTypeParameterCount => throw new System.NotImplementedException(); |
| 38 | + |
| 39 | + public override IEnumerable<Type> TypeParameters => throw new System.NotImplementedException(); |
| 40 | + |
| 41 | + public override Type Construct(IEnumerable<Type> typeArguments) => throw new System.NotImplementedException(); |
| 42 | + |
| 43 | + public override void WriteAssemblyPrefix(TextWriter trapFile) { } |
| 44 | + |
| 45 | + public override void WriteId(TextWriter trapFile, bool inContext) |
| 46 | + { |
| 47 | + WriteName( |
| 48 | + trapFile.Write, |
| 49 | + t => t.WriteId(trapFile, inContext), |
| 50 | + signature |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + internal static void WriteName<TType>(Action<string> write, Action<TType> writeType, MethodSignature<TType> signature) |
| 55 | + { |
| 56 | + write("delegate* "); |
| 57 | + write(GetCallingConvention(signature.Header.CallingConvention)); |
| 58 | + write("<"); |
| 59 | + foreach (var pt in signature.ParameterTypes) |
| 60 | + { |
| 61 | + writeType(pt); |
| 62 | + write(","); |
| 63 | + } |
| 64 | + writeType(signature.ReturnType); |
| 65 | + write(">"); |
| 66 | + } |
| 67 | + |
| 68 | + internal static string GetCallingConvention(SignatureCallingConvention callingConvention) |
| 69 | + { |
| 70 | + if (callingConvention == SignatureCallingConvention.Default) |
| 71 | + { |
| 72 | + return "managed"; |
| 73 | + } |
| 74 | + |
| 75 | + if (callingConvention == SignatureCallingConvention.Unmanaged) |
| 76 | + { |
| 77 | + return "unmanaged"; |
| 78 | + } |
| 79 | + |
| 80 | + return $"unmanaged[{callingConvention}]"; |
| 81 | + } |
| 82 | + |
| 83 | + public override IEnumerable<IExtractionProduct> Contents |
| 84 | + { |
| 85 | + get |
| 86 | + { |
| 87 | + foreach (var c in base.Contents) |
| 88 | + { |
| 89 | + yield return c; |
| 90 | + } |
| 91 | + |
| 92 | + var retType = signature.ReturnType; |
| 93 | + if (retType is ModifiedType mt) |
| 94 | + { |
| 95 | + retType = mt.Unmodified; |
| 96 | + yield return Tuples.cil_custom_modifiers(this, mt.Modifier, mt.IsRequired); |
| 97 | + } |
| 98 | + yield return Tuples.cil_function_pointer_return_type(this, retType); |
| 99 | + |
| 100 | + yield return Tuples.cil_function_pointer_calling_conventions(this, signature.Header.CallingConvention); |
| 101 | + |
| 102 | + var i = 0; |
| 103 | + foreach (var p in signature.ParameterTypes) |
| 104 | + { |
| 105 | + var t = p; |
| 106 | + if (t is ModifiedType mtparam) |
| 107 | + { |
| 108 | + t = mtparam.Unmodified; |
| 109 | + yield return Tuples.cil_custom_modifiers(this, mtparam.Modifier, mtparam.IsRequired); |
| 110 | + } |
| 111 | + yield return Cx.Populate(new Parameter(Cx, this, i++, t)); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments