Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EcmaMethod.cs « Ecma « TypeSystem « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63eb97c3726f2e6e2203a5fd07ac7f12d42f3af6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Reflection.Metadata;
using System.Threading;

namespace Internal.TypeSystem.Ecma
{
    public sealed partial class EcmaMethod : MethodDesc, EcmaModule.IEntityHandleObject
    {
        private static class MethodFlags
        {
            public const int BasicMetadataCache     = 0x00001;
            public const int Virtual                = 0x00002;
            public const int NewSlot                = 0x00004;
            public const int Abstract               = 0x00008;
            public const int Final                  = 0x00010;
            public const int NoInlining             = 0x00020;
            public const int AggressiveInlining     = 0x00040;
            public const int RuntimeImplemented     = 0x00080;
            public const int InternalCall           = 0x00100;
            public const int Synchronized           = 0x00200;
            public const int AggressiveOptimization = 0x00400;
            public const int NoOptimization         = 0x00800;
            public const int RequireSecObject       = 0x01000;

            public const int AttributeMetadataCache = 0x02000;
            public const int Intrinsic              = 0x04000;
            public const int UnmanagedCallersOnly   = 0x08000;
            public const int RuntimeExport          = 0x10000;
        };

        private EcmaType _type;
        private MethodDefinitionHandle _handle;

        // Cached values
        private ThreadSafeFlags _methodFlags;
        private MethodSignature _signature;
        private string _name;
        private TypeDesc[] _genericParameters; // TODO: Optional field?

        internal EcmaMethod(EcmaType type, MethodDefinitionHandle handle)
        {
            _type = type;
            _handle = handle;

#if DEBUG
            // Initialize name eagerly in debug builds for convenience
            InitializeName();
#endif
        }

        EntityHandle EcmaModule.IEntityHandleObject.Handle
        {
            get
            {
                return _handle;
            }
        }

        public override TypeSystemContext Context
        {
            get
            {
                return _type.Module.Context;
            }
        }

        public override TypeDesc OwningType
        {
            get
            {
                return _type;
            }
        }

        private MethodSignature InitializeSignature()
        {
            var metadataReader = MetadataReader;
            BlobReader signatureReader = metadataReader.GetBlobReader(metadataReader.GetMethodDefinition(_handle).Signature);

            EcmaSignatureParser parser = new EcmaSignatureParser(Module, signatureReader, NotFoundBehavior.Throw);
            var signature = parser.ParseMethodSignature();
            return (_signature = signature);
        }

        public override MethodSignature Signature
        {
            get
            {
                if (_signature == null)
                    return InitializeSignature();
                return _signature;
            }
        }

        public EcmaModule Module
        {
            get
            {
                return _type.EcmaModule;
            }
        }

        public MetadataReader MetadataReader
        {
            get
            {
                return _type.MetadataReader;
            }
        }

        public MethodDefinitionHandle Handle
        {
            get
            {
                return _handle;
            }
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private int InitializeMethodFlags(int mask)
        {
            int flags = 0;

            if ((mask & MethodFlags.BasicMetadataCache) != 0)
            {
                var methodAttributes = Attributes;
                var methodImplAttributes = ImplAttributes;

                if ((methodAttributes & MethodAttributes.Virtual) != 0)
                    flags |= MethodFlags.Virtual;

                if ((methodAttributes & MethodAttributes.NewSlot) != 0)
                    flags |= MethodFlags.NewSlot;

                if ((methodAttributes & MethodAttributes.Abstract) != 0)
                    flags |= MethodFlags.Abstract;

                if ((methodAttributes & MethodAttributes.Final) != 0)
                    flags |= MethodFlags.Final;

                if ((methodAttributes & MethodAttributes.RequireSecObject) != 0)
                    flags |= MethodFlags.RequireSecObject;

                if ((methodImplAttributes & MethodImplAttributes.NoInlining) != 0)
                    flags |= MethodFlags.NoInlining;

                // System.Reflection.Primitives we build against doesn't define AggressiveOptimization
                const MethodImplAttributes MethodImplAttributes_AggressiveOptimization = (MethodImplAttributes)0x0200;

                // No optimization bit beats aggressive optimization bit (CLR compatible behavior)
                if ((methodImplAttributes & MethodImplAttributes.NoOptimization) != 0)
                    flags |= MethodFlags.NoOptimization;
                else if ((methodImplAttributes & MethodImplAttributes_AggressiveOptimization) != 0)
                    flags |= MethodFlags.AggressiveOptimization;

                if ((methodImplAttributes & MethodImplAttributes.AggressiveInlining) != 0)
                    flags |= MethodFlags.AggressiveInlining;

                if ((methodImplAttributes & MethodImplAttributes.Runtime) != 0)
                    flags |= MethodFlags.RuntimeImplemented;

                if ((methodImplAttributes & MethodImplAttributes.InternalCall) != 0)
                    flags |= MethodFlags.InternalCall;

                if ((methodImplAttributes & MethodImplAttributes.Synchronized) != 0)
                    flags |= MethodFlags.Synchronized;

                flags |= MethodFlags.BasicMetadataCache;
            }

            // Fetching custom attribute based properties is more expensive, so keep that under
            // a separate cache that might not be accessed very frequently.
            if ((mask & MethodFlags.AttributeMetadataCache) != 0)
            {
                var metadataReader = this.MetadataReader;
                var methodDefinition = metadataReader.GetMethodDefinition(_handle);

                foreach (var attributeHandle in methodDefinition.GetCustomAttributes())
                {
                    StringHandle namespaceHandle, nameHandle;
                    if (!metadataReader.GetAttributeNamespaceAndName(attributeHandle, out namespaceHandle, out nameHandle))
                        continue;

                    if (metadataReader.StringComparer.Equals(namespaceHandle, "System.Runtime.CompilerServices"))
                    {
                        if (metadataReader.StringComparer.Equals(nameHandle, "IntrinsicAttribute"))
                        {
                            flags |= MethodFlags.Intrinsic;
                        }
                    }
                    else
                    if (metadataReader.StringComparer.Equals(namespaceHandle, "System.Runtime.InteropServices"))
                    {
                        if (metadataReader.StringComparer.Equals(nameHandle, "UnmanagedCallersOnlyAttribute"))
                        {
                            flags |= MethodFlags.UnmanagedCallersOnly;
                        }
                    }
                    else
                    if (metadataReader.StringComparer.Equals(namespaceHandle, "System.Runtime"))
                    {
                        if (metadataReader.StringComparer.Equals(nameHandle, "RuntimeExportAttribute"))
                        {
                            flags |= MethodFlags.RuntimeExport;
                        }
                    }
                }

                flags |= MethodFlags.AttributeMetadataCache;
            }

            Debug.Assert((flags & mask) != 0);
            _methodFlags.AddFlags(flags);

            return flags & mask;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private int GetMethodFlags(int mask)
        {
            int flags = _methodFlags.Value & mask;
            if (flags != 0)
                return flags;
            return InitializeMethodFlags(mask);
        }

        public override bool IsVirtual
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.Virtual) & MethodFlags.Virtual) != 0;
            }
        }

        public override bool IsNewSlot
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.NewSlot) & MethodFlags.NewSlot) != 0;
            }
        }

        public override bool IsAbstract
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.Abstract) & MethodFlags.Abstract) != 0;
            }
        }

        public override bool IsFinal
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.Final) & MethodFlags.Final) != 0;
            }
        }

        public override bool IsNoInlining
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.NoInlining) & MethodFlags.NoInlining) != 0;
            }
        }

        public override bool RequireSecObject
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.RequireSecObject) & MethodFlags.RequireSecObject) != 0;
            }
        }

        public override bool IsAggressiveOptimization
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.AggressiveOptimization) & MethodFlags.AggressiveOptimization) != 0;
            }
        }

        public override bool IsNoOptimization
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.NoOptimization) & MethodFlags.NoOptimization) != 0;
            }
        }

        public override bool IsAggressiveInlining
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.AggressiveInlining) & MethodFlags.AggressiveInlining) != 0;
            }
        }

        public override bool IsRuntimeImplemented
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.RuntimeImplemented) & MethodFlags.RuntimeImplemented) != 0;
            }
        }

        public override bool IsIntrinsic
        {
            get
            {
                return (GetMethodFlags(MethodFlags.AttributeMetadataCache | MethodFlags.Intrinsic) & MethodFlags.Intrinsic) != 0;
            }
        }

        public override bool IsInternalCall
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.InternalCall) & MethodFlags.InternalCall) != 0;
            }
        }

        public override bool IsSynchronized
        {
            get
            {
                return (GetMethodFlags(MethodFlags.BasicMetadataCache | MethodFlags.Synchronized) & MethodFlags.Synchronized) != 0;
            }
        }

        public override bool IsUnmanagedCallersOnly
        {
            get
            {
                return (GetMethodFlags(MethodFlags.AttributeMetadataCache | MethodFlags.UnmanagedCallersOnly) & MethodFlags.UnmanagedCallersOnly) != 0;
            }
        }

        public override bool IsRuntimeExport
        {
            get
            {
                return (GetMethodFlags(MethodFlags.AttributeMetadataCache | MethodFlags.RuntimeExport) & MethodFlags.RuntimeExport) != 0;
            }
        }

        public override bool IsSpecialName
        {
            get
            {
                return (Attributes & MethodAttributes.SpecialName) != 0;
            }
        }

        public override bool IsDefaultConstructor
        {
            get
            {
                MethodAttributes attributes = Attributes;
                return attributes.IsRuntimeSpecialName()
                    && attributes.IsPublic()
                    && Signature.Length == 0
                    && Name == ".ctor"
                    && !_type.IsAbstract;
            }
        }

        public MethodAttributes Attributes
        {
            get
            {
                return MetadataReader.GetMethodDefinition(_handle).Attributes;
            }
        }

        public MethodImplAttributes ImplAttributes
        {
            get
            {
                return MetadataReader.GetMethodDefinition(_handle).ImplAttributes;
            }
        }

        private string InitializeName()
        {
            var metadataReader = MetadataReader;
            var name = metadataReader.GetString(metadataReader.GetMethodDefinition(_handle).Name);
            return (_name = name);
        }

        public override string Name
        {
            get
            {
                if (_name == null)
                    return InitializeName();
                return _name;
            }
        }

        private void ComputeGenericParameters()
        {
            var genericParameterHandles = MetadataReader.GetMethodDefinition(_handle).GetGenericParameters();
            int count = genericParameterHandles.Count;
            if (count > 0)
            {
                TypeDesc[] genericParameters = new TypeDesc[count];
                int i = 0;
                foreach (var genericParameterHandle in genericParameterHandles)
                {
                    genericParameters[i++] = new EcmaGenericParameter(Module, genericParameterHandle);
                }
                Interlocked.CompareExchange(ref _genericParameters, genericParameters, null);
            }
            else
            {
                _genericParameters = TypeDesc.EmptyTypes;
            }
        }

        public override Instantiation Instantiation
        {
            get
            {
                if (_genericParameters == null)
                    ComputeGenericParameters();
                return new Instantiation(_genericParameters);
            }
        }

        public override bool HasCustomAttribute(string attributeNamespace, string attributeName)
        {
            return !MetadataReader.GetCustomAttributeHandle(MetadataReader.GetMethodDefinition(_handle).GetCustomAttributes(),
                attributeNamespace, attributeName).IsNil;
        }

        public override bool IsPInvoke
        {
            get
            {
                return (((int)Attributes & (int)MethodAttributes.PinvokeImpl) != 0);
            }
        }

        public override PInvokeMetadata GetPInvokeMethodMetadata()
        {
            if (!IsPInvoke)
                return default(PInvokeMetadata);

            MetadataReader metadataReader = MetadataReader;
            MethodDefinition methodDef = metadataReader.GetMethodDefinition(_handle);
            MethodImport import = methodDef.GetImport();
            string name = metadataReader.GetString(import.Name);

            ModuleReference moduleRef = metadataReader.GetModuleReference(import.Module);
            string moduleName = metadataReader.GetString(moduleRef.Name);

            MethodImportAttributes importAttributes = import.Attributes;

            // If either BestFitMapping or ThrowOnUnmappable wasn't set on the p/invoke,
            // look for the value in the owning type or assembly.
            if ((importAttributes & MethodImportAttributes.BestFitMappingMask) == 0 ||
                (importAttributes & MethodImportAttributes.ThrowOnUnmappableCharMask) == 0)
            {
                TypeDefinition declaringType = metadataReader.GetTypeDefinition(methodDef.GetDeclaringType());

                // Start with owning type
                MethodImportAttributes fromCA = GetImportAttributesFromBestFitMappingAttribute(declaringType.GetCustomAttributes());
                if ((importAttributes & MethodImportAttributes.BestFitMappingMask) == 0)
                    importAttributes |= fromCA & MethodImportAttributes.BestFitMappingMask;
                if ((importAttributes & MethodImportAttributes.ThrowOnUnmappableCharMask) == 0)
                    importAttributes |= fromCA & MethodImportAttributes.ThrowOnUnmappableCharMask;

                // If we still don't know, check the assembly
                if ((importAttributes & MethodImportAttributes.BestFitMappingMask) == 0 ||
                    (importAttributes & MethodImportAttributes.ThrowOnUnmappableCharMask) == 0)
                {
                    fromCA = GetImportAttributesFromBestFitMappingAttribute(metadataReader.GetAssemblyDefinition().GetCustomAttributes());
                    if ((importAttributes & MethodImportAttributes.BestFitMappingMask) == 0)
                        importAttributes |= fromCA & MethodImportAttributes.BestFitMappingMask;
                    if ((importAttributes & MethodImportAttributes.ThrowOnUnmappableCharMask) == 0)
                        importAttributes |= fromCA & MethodImportAttributes.ThrowOnUnmappableCharMask;
                }
            }

            // Spot check the enums match
            Debug.Assert((int)MethodImportAttributes.CallingConventionStdCall == (int)PInvokeAttributes.CallingConventionStdCall);
            Debug.Assert((int)MethodImportAttributes.CharSetAuto == (int)PInvokeAttributes.CharSetAuto);
            Debug.Assert((int)MethodImportAttributes.CharSetUnicode == (int)PInvokeAttributes.CharSetUnicode);
            Debug.Assert((int)MethodImportAttributes.SetLastError == (int)PInvokeAttributes.SetLastError);

            PInvokeAttributes attributes = (PInvokeAttributes)importAttributes;

            if ((ImplAttributes & MethodImplAttributes.PreserveSig) != 0)
                attributes |= PInvokeAttributes.PreserveSig;

            return new PInvokeMetadata(moduleName, name, attributes);
        }

        private MethodImportAttributes GetImportAttributesFromBestFitMappingAttribute(CustomAttributeHandleCollection attributeHandles)
        {
            // Look for the [BestFitMapping(BestFitMapping: x, ThrowOnUnmappableChar = y)] attribute and
            // translate that to MethodImportAttributes

            MethodImportAttributes result = 0;
            MetadataReader reader = MetadataReader;

            CustomAttributeHandle attributeHandle = reader.GetCustomAttributeHandle(
                attributeHandles, "System.Runtime.InteropServices", "BestFitMappingAttribute");
            if (!attributeHandle.IsNil)
            {
                CustomAttribute attribute = reader.GetCustomAttribute(attributeHandle);
                CustomAttributeValue<TypeDesc> decoded = attribute.DecodeValue(
                    new CustomAttributeTypeProvider(_type.EcmaModule));

                if (decoded.FixedArguments.Length != 1 || !(decoded.FixedArguments[0].Value is bool))
                    ThrowHelper.ThrowBadImageFormatException();
                if ((bool)decoded.FixedArguments[0].Value)
                    result |= MethodImportAttributes.BestFitMappingEnable;
                else
                    result |= MethodImportAttributes.BestFitMappingDisable;

                foreach (CustomAttributeNamedArgument<TypeDesc> namedArg in decoded.NamedArguments)
                {
                    if (namedArg.Name == "ThrowOnUnmappableChar")
                    {
                        if (!(namedArg.Value is bool))
                            ThrowHelper.ThrowBadImageFormatException();
                        if ((bool)namedArg.Value)
                            result |= MethodImportAttributes.ThrowOnUnmappableCharEnable;
                        else
                            result |= MethodImportAttributes.ThrowOnUnmappableCharDisable;
                        break;
                    }
                }
            }

            return result;
        }

        public override ParameterMetadata[] GetParameterMetadata()
        {
            MetadataReader metadataReader = MetadataReader;

            // Spot check the enums match
            Debug.Assert((int)ParameterAttributes.In == (int)ParameterMetadataAttributes.In);
            Debug.Assert((int)ParameterAttributes.Out == (int)ParameterMetadataAttributes.Out);
            Debug.Assert((int)ParameterAttributes.Optional == (int)ParameterMetadataAttributes.Optional);
            Debug.Assert((int)ParameterAttributes.HasDefault == (int)ParameterMetadataAttributes.HasDefault);
            Debug.Assert((int)ParameterAttributes.HasFieldMarshal == (int)ParameterMetadataAttributes.HasFieldMarshal);

            ParameterHandleCollection parameterHandles = metadataReader.GetMethodDefinition(_handle).GetParameters();
            ParameterMetadata[] parameterMetadataArray = new ParameterMetadata[parameterHandles.Count];
            int index = 0;
            foreach (ParameterHandle parameterHandle in parameterHandles)
            {
                Parameter parameter = metadataReader.GetParameter(parameterHandle);
                MarshalAsDescriptor marshalAsDescriptor = GetMarshalAsDescriptor(parameter);
                ParameterMetadata data = new ParameterMetadata(parameter.SequenceNumber, (ParameterMetadataAttributes)parameter.Attributes, marshalAsDescriptor);
                parameterMetadataArray[index++] = data;
            }
            return parameterMetadataArray;
        }

        private MarshalAsDescriptor GetMarshalAsDescriptor(Parameter parameter)
        {
            if ((parameter.Attributes & ParameterAttributes.HasFieldMarshal) == ParameterAttributes.HasFieldMarshal)
            {
                MetadataReader metadataReader = MetadataReader;
                BlobReader marshalAsReader = metadataReader.GetBlobReader(parameter.GetMarshallingDescriptor());
                EcmaSignatureParser parser = new EcmaSignatureParser(Module, marshalAsReader, NotFoundBehavior.Throw);
                MarshalAsDescriptor marshalAs = parser.ParseMarshalAsDescriptor();
                Debug.Assert(marshalAs != null);
                return marshalAs;
            }
            return null;
        }
    }
}