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

UtcNameMangler.cs « Compiler « src « ILCompiler.Compiler « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a0aa6515dbb784f94291c82177f3192cfe5bf6c (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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;

using Internal.Text;
using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;

namespace ILCompiler
{
    public class ImportExportOrdinals
    {
        public bool isImport;
        public uint tlsIndexOrdinal;
        public ReadOnlyDictionary<TypeDesc, uint> typeOrdinals;
        public ReadOnlyDictionary<TypeDesc, uint> nonGcStaticOrdinals;
        public ReadOnlyDictionary<TypeDesc, uint> gcStaticOrdinals;
        public ReadOnlyDictionary<TypeDesc, uint> tlsStaticOrdinals;
        public ReadOnlyDictionary<MethodDesc, uint> methodOrdinals;
        public ReadOnlyDictionary<MethodDesc, uint> unboxingStubMethodOrdinals;
        public ReadOnlyDictionary<MethodDesc, uint> methodDictionaryOrdinals;
    }

    public class UTCNameMangler : NameMangler
    {
        private SHA256 _sha256;
        private string _compilationUnitPrefix;
        private string OrdinalPrefix => "_[O]_";
        private string deDuplicatePrefix => "_[D]_";

        private ImportExportOrdinals _importOrdinals;
        private ImportExportOrdinals _exportOrdinals;

        private Dictionary<EcmaModule, int> _inputModuleIndices;

        private bool HasImport { get; set; }
        private bool HasExport { get; set; }
        private bool BuildingClassLib { get; }

        public UTCNameMangler(bool hasImport, bool hasExport, ImportExportOrdinals ordinals, TypeSystemContext context, List<EcmaModule> inputModules, bool buildingClassLib) : base(new UtcNodeMangler())
        {
            // Do not support both imports and exports for one module
            Debug.Assert(!hasImport || !hasExport);
            HasImport = hasImport;
            HasExport = hasExport;
            BuildingClassLib = buildingClassLib;

            if (hasImport)
            {
                _importOrdinals = ordinals;
            }
            else if (hasExport)
            {
                _exportOrdinals = ordinals;
            }

            _inputModuleIndices = new Dictionary<EcmaModule, int>();
            for (int i = 0; i < inputModules.Count; i++)
                _inputModuleIndices[inputModules[i]] = i;

            // Use SHA256 hash here to provide a high degree of uniqueness to symbol names without requiring them to be long
            // This hash function provides an exceedingly high likelihood that no two strings will be given equal symbol names
            // This is not considered used for security purpose; however collisions would be highly unfortunate as they will cause compilation
            // failure.
            _sha256 = SHA256.Create();

            // Special case primitive types and use shortened names. This reduces string sizes in symbol names, and reduces the overall native memory
            // usage of the compiler
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Void), "void");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Boolean), "bool");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Char), "char");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.SByte), "sbyte");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Byte), "byte");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Int16), "short");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.UInt16), "ushort");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Int32), "int");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.UInt32), "uint");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Int64), "long");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.UInt64), "ulong");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Single), "float");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Double), "double");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.Object), "object");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.String), "string");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.IntPtr), "IntPtr");
            _mangledTypeNames = _mangledTypeNames.Add(context.GetWellKnownType(WellKnownType.UIntPtr), "UIntPtr");
        }

        private bool GetMethodOrdinal(MethodDesc method, out uint ordinal)
        {
            if (HasImport && _importOrdinals.methodOrdinals.TryGetValue(method, out ordinal))
            {
                return true;
            }

            if (HasExport && _exportOrdinals.methodOrdinals.TryGetValue(method, out ordinal))
            {
                return true;
            }

            ordinal = 0;
            return false;
        }

        private bool GetMethodDictionaryOrdinal(MethodDesc method, out uint ordinal)
        {
            if (HasImport && _importOrdinals.methodDictionaryOrdinals.TryGetValue(method, out ordinal))
            {
                return true;
            }

            if (HasExport && _exportOrdinals.methodDictionaryOrdinals.TryGetValue(method, out ordinal))
            {
                return true;
            }

            ordinal = 0;
            return false;
        }

        private bool GetTlsIndexOrdinal(out uint ordinal)
        {
            if (HasImport)
            {
                ordinal = _importOrdinals.tlsIndexOrdinal;
                return true;
            }

            if (HasExport)
            {
                ordinal = _exportOrdinals.tlsIndexOrdinal;
                return true;
            }

            ordinal = 0;
            return false;
        }

        public override string CompilationUnitPrefix
        {
            set { _compilationUnitPrefix = SanitizeNameWithHash(value); }
            get
            {
                System.Diagnostics.Debug.Assert(_compilationUnitPrefix != null);
                return _compilationUnitPrefix;
            }
        }

        //
        // Turn a name into a valid C/C++ identifier
        //
        public override string SanitizeName(string s, bool typeName = false)
        {
            StringBuilder sb = null;
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];

                if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')))
                {
                    if (sb != null)
                        sb.Append(c);
                    continue;
                }

                if ((c >= '0') && (c <= '9'))
                {
                    // C identifiers cannot start with a digit. Prepend underscores.
                    if (i == 0)
                    {
                        if (sb == null)
                            sb = new StringBuilder(s.Length + 2);
                        sb.Append("_");
                    }
                    if (sb != null)
                        sb.Append(c);
                    continue;
                }

                if (sb != null)
                {
                    if (c == '[' || c == ']' || c == '&' || c == '*' || c == '$' || c == '<' || c == '>')
                    {
                        sb.Append(c);
                        continue;
                    }
                }

                if (sb == null)
                    sb = new StringBuilder(s, 0, i, s.Length);

                // For CppCodeGen, replace "." (C# namespace separator) with "::" (C++ namespace separator)
                if (typeName && c == '.')
                {
                    sb.Append("::");
                    continue;
                }

                if (c == '$' && i == 0)
                {
                    // '$' is used at the begining of the string as assembly identifiers (ex: $0_, similar to ProjectN)
                    sb.Append(c);
                    continue;
                }

                // Everything else is replaced by underscore.
                // TODO: We assume that there won't be collisions with our own or C++ built-in identifiers.
                sb.Append("_");
            }

            string sanitizedName = (sb != null) ? sb.ToString() : s;

            // The character sequences denoting generic instantiations, arrays, byrefs, or pointers must be
            // restricted to that use only. Replace them if they happened to be used in any identifiers in 
            // the compilation input.
            return sanitizedName;
        }

        private static byte[] GetBytesFromString(string literal)
        {
            byte[] bytes = new byte[checked(literal.Length * 2)];
            for (int i = 0; i < literal.Length; i++)
            {
                int iByteBase = i * 2;
                char c = literal[i];
                bytes[iByteBase] = (byte)c;
                bytes[iByteBase + 1] = (byte)(c >> 8);
            }
            return bytes;
        }

        private string SanitizeNameWithHash(string literal)
        {
            string mangledName = SanitizeName(literal);

            if (mangledName.Length > 30)
                mangledName = mangledName.Substring(0, 30);

            if (mangledName != literal)
            {
                byte[] hash;
                lock (this)
                {
                    hash = _sha256.ComputeHash(GetBytesFromString(literal));
                }

                mangledName += "_" + BitConverter.ToString(hash).Replace("-", "");
            }

            return mangledName;
        }

        /// <summary>
        /// Dictionary given a mangled name for a given <see cref="TypeDesc"/>
        /// </summary>
        private ImmutableDictionary<TypeDesc, string> _mangledTypeNames = ImmutableDictionary<TypeDesc, string>.Empty;

        /// <summary>
        /// Given a set of names <param name="set"/> check if <param name="origName"/>
        /// is unique, if not add a numbered suffix until it becomes unique.
        /// </summary>
        /// <param name="origName">Name to check for uniqueness.</param>
        /// <param name="set">Set of names already used.</param>
        /// <returns>A name based on <param name="origName"/> that is not part of <param name="set"/>.</returns>
        private string DisambiguateName(string origName, ISet<string> set)
        {
            int iter = 0;
            string result = origName;
            while (set.Contains(result))
            {
                result = string.Concat(origName, deDuplicatePrefix, (iter++).ToStringInvariant());
            }
            return result;
        }               

        public override string GetMangledTypeName(TypeDesc type)
        {
            string mangledName;
            if (_mangledTypeNames.TryGetValue(type, out mangledName))
                return mangledName;

            return ComputeMangledTypeName(type);
        }

        private string EnterNameScopeSequence => "<";
        private string ExitNameScopeSequence => ">";

        protected string NestMangledName(string name)
        {
            return EnterNameScopeSequence + name + ExitNameScopeSequence;
        }

        private string ComputeMangledModuleName(EcmaAssembly module)
        {
            // Do not prepend the module prefix when building pntestcl because the prefix is unknown
            // when building an app against pntestcl.
            if (!BuildingClassLib)
            {
                int index;
                if (_inputModuleIndices.TryGetValue(module, out index))
                    return "$" + index;
            }

            return SanitizeName(module.GetName().Name);
        }

        /// <summary>
        /// If given <param name="type"/> is an <see cref="EcmaType"/> precompute its mangled type name
        /// along with all the other types from the same module as <param name="type"/>.
        /// Otherwise, it is a constructed type and to the EcmaType's mangled name we add a suffix to
        /// show what kind of constructed type it is (e.g. appending __Array for an array type).
        /// </summary>
        /// <param name="type">Type to mangled</param>
        /// <returns>Mangled name for <param name="type"/>.</returns>
        private string ComputeMangledTypeName(TypeDesc type)
        {
            if (type is EcmaType)
            {
                EcmaType ecmaType = (EcmaType)type;

                string prependAssemblyName = ComputeMangledModuleName((EcmaAssembly)ecmaType.EcmaModule);

                var deduplicator = new HashSet<string>();

                // Add consistent names for all types in the module, independent on the order in which
                // they are compiled
                lock (this)
                {
                    if (!_mangledTypeNames.ContainsKey(type))
                    {
                        foreach (MetadataType t in ((EcmaType)type).EcmaModule.GetAllTypes())
                        {
                            if (_mangledTypeNames.ContainsKey(t))
                            {
                                Debug.Assert(
                                    (t.Category & TypeFlags.CategoryMask) <= TypeFlags.Double ||
                                    t == type.Context.GetWellKnownType(WellKnownType.Object) ||
                                    t == type.Context.GetWellKnownType(WellKnownType.String));
                                continue;
                            }

                            string name = t.GetFullName();

                            // Include encapsulating type
                            DefType containingType = t.ContainingType;
                            while (containingType != null)
                            {
                                name = containingType.GetFullName() + "_" + name;
                                containingType = containingType.ContainingType;
                            }

                            name = SanitizeName(prependAssemblyName + "_" + name, true);

                            // Ensure that name is unique and update our tables accordingly.
                            name = DisambiguateName(name, deduplicator);
                            deduplicator.Add(name);
                            _mangledTypeNames = _mangledTypeNames.Add(t, name);
                        }
                    }
                }

                return _mangledTypeNames[type];
            }

            string mangledName;

            switch (type.Category)
            {
                case TypeFlags.Array:
                case TypeFlags.SzArray:
                    mangledName = GetMangledTypeName(((ArrayType)type).ElementType);

                    if (type.IsMdArray)
                    {
                        mangledName += "[md" + ((ArrayType)type).Rank.ToString() + "]"; 
                    }
                    else
                    {
                        mangledName += "[]";
                    }
                    break;
                case TypeFlags.ByRef:
                    mangledName = GetMangledTypeName(((ByRefType)type).ParameterType) + "&";
                    break;
                case TypeFlags.Pointer:
                    mangledName = GetMangledTypeName(((PointerType)type).ParameterType) + "*";
                    break;

                default:
                    // Case of a generic type. If `type' is a type definition we use the type name
                    // for mangling, otherwise we use the mangling of the type and its generic type
                    // parameters, e.g. A <B> becomes A_<___B_>_ in RyuJIT compilation, or A_A___B_V_
                    // in C++ compilation.
                    var typeDefinition = type.GetTypeDefinition();
                    if (typeDefinition != type)
                    {
                        mangledName = GetMangledTypeName(typeDefinition);

                        var inst = type.Instantiation;
                        string mangledInstantiation = "";
                        for (int i = 0; i < inst.Length; i++)
                        {
                            string instArgName = GetMangledTypeName(inst[i]);
                            if (i > 0)
                                mangledInstantiation += "__";

                            mangledInstantiation += instArgName;
                        }
                        mangledName += NestMangledName(mangledInstantiation);
                    }
                    else
                    {
                        mangledName = SanitizeName(((DefType)type).GetFullName(), true);
                    }
                    break;
            }

            lock (this)
            {
                // Ensure that name is unique and update our tables accordingly.
                if (!_mangledTypeNames.ContainsKey(type))
                    _mangledTypeNames = _mangledTypeNames.Add(type, mangledName);
            }

            return mangledName;
        }

        protected ImmutableDictionary<MethodDesc, Utf8String> _mangledMethodNames = ImmutableDictionary<MethodDesc, Utf8String>.Empty;

        public override Utf8String GetMangledMethodName(MethodDesc method)
        {
            Utf8String mangledName;
            if (_mangledMethodNames.TryGetValue(method, out mangledName))
                return mangledName;

            return ComputeMangledMethodName(method);
        }

        string RemoveDeduplicatePrefix(string name)
        {
            char lastChar = name[name.Length - 1];
            if ((lastChar >= '0') && (lastChar <= '9'))
            {
                int deDupPos = name.LastIndexOf(deDuplicatePrefix);
                if (deDupPos != -1)
                {
                    name = name.Substring(0, deDupPos);
                }
            }

            return name;
        }

        public string GetLinkageNameForPInvokeMethod(MethodDesc method, out int ordinal)
        {
            string methodName = method.GetPInvokeMethodMetadata().Name;

            if (methodName.StartsWith("#"))
            {
                if (int.TryParse(methodName.Substring(1), out ordinal))
                {
                    string moduleName = method.GetPInvokeMethodMetadata().Module;
                    return moduleName + methodName;
                }
            }

            ordinal = -1;
            return methodName;
        }

        private Utf8String ComputeMangledNameMethodWithoutInstantiation(MethodDesc method)
        {
            Debug.Assert(method == method.GetMethodDefinition());

            string prependTypeName = GetMangledTypeName(method.OwningType);
            var deduplicator = new HashSet<string>();

            // Add consistent names for all methods of the type, independent on the order in which
            // they are compiled
            lock (this)
            {
                if (!_mangledMethodNames.ContainsKey(method))
                {
                    foreach (var m in method.OwningType.GetMethods())
                    {
                        string name;

                        if (m.IsPInvoke)
                        {
                            int ordinal;
                            name = GetLinkageNameForPInvokeMethod(m, out ordinal);
                        }
                        else
                        {
                            name = SanitizeName(m.Name);
                            uint ordinal;

                            // Ensure that name is unique and update our tables accordingly.
                            if (GetMethodOrdinal(m, out ordinal))
                            {
                                name += OrdinalPrefix + ordinal;
                            }
                            else
                            {
                                name = DisambiguateName(name, deduplicator);
                            }

                            Debug.Assert(!deduplicator.Contains(name));
                            deduplicator.Add(name);

                            if (prependTypeName != null)
                                name = prependTypeName + "__" + name;
                        }

                        _mangledMethodNames = _mangledMethodNames.Add(m, name);
                    }
                }
            }

            return _mangledMethodNames[method];
        }

        private Utf8String ComputeMangledMethodName(MethodDesc method)
        {
            // Method is either a generic method instantiation or an instance method of a generic type instantiation
            var methodDefinition = method.GetMethodDefinition();
            Utf8String utf8MangledName = ComputeMangledNameMethodWithoutInstantiation(methodDefinition);

            // Append the instantiation vector for a generic method instantiation 
            if (methodDefinition != method)
            {
                string mangledInstantiation = "";
                var inst = method.Instantiation;
                for (int i = 0; i < inst.Length; i++)
                {
                    string instArgName = GetMangledTypeName(inst[i]);
                    if (i > 0)
                        mangledInstantiation += "__";
                    mangledInstantiation += instArgName;
                }

                mangledInstantiation = NestMangledName(mangledInstantiation);

                string mangledName = utf8MangledName.ToString();

                // Do not need the deDuplicator (which is not stable across builds) if the method has an importExport ordinal
                uint ordinal;
                if (GetMethodOrdinal(method, out ordinal))
                {
                    mangledName = RemoveDeduplicatePrefix(mangledName);
                    mangledName += mangledInstantiation;
                    mangledName += OrdinalPrefix + ordinal;
                }
                else
                {
                    mangledName += mangledInstantiation;
                }

                lock (this)
                {
                    utf8MangledName = new Utf8String(mangledName);
                    if (!_mangledMethodNames.ContainsKey(method))
                        _mangledMethodNames = _mangledMethodNames.Add(method, utf8MangledName);
                }
            }

            return utf8MangledName;
        }

        protected ImmutableDictionary<MethodDesc, Utf8String> _mangledMethodDictNames = ImmutableDictionary<MethodDesc, Utf8String>.Empty;

        public Utf8String GetMangledMethodNameForDictionary(MethodDesc method)
        {
            Utf8String mangledName;
            if (_mangledMethodDictNames.TryGetValue(method, out mangledName))
                return mangledName;

            return ComputeMangledMethodDictonaryName(method);
        }

        private Utf8String ComputeMangledMethodDictonaryName(MethodDesc method)
        {
            Utf8String methodName = GetMangledMethodName(method);
            uint ordinal;
            if (GetMethodDictionaryOrdinal(method, out ordinal))
            {
                methodName += OrdinalPrefix + ordinal;
            }

            lock (this)
            {
                if (!_mangledMethodDictNames.ContainsKey(method))
                    _mangledMethodDictNames = _mangledMethodDictNames.Add(method, methodName);
            }

            return _mangledMethodDictNames[method];
        }

        private ImmutableDictionary<FieldDesc, Utf8String> _mangledFieldNames = ImmutableDictionary<FieldDesc, Utf8String>.Empty;

        public override Utf8String GetMangledFieldName(FieldDesc field)
        {
            Utf8String mangledName;
            if (_mangledFieldNames.TryGetValue(field, out mangledName))
                return mangledName;

            return ComputeMangledFieldName(field);
        }

        private Utf8String ComputeMangledFieldName(FieldDesc field)
        {
            string prependTypeName = null;
            prependTypeName = GetMangledTypeName(field.OwningType);

            if (field is EcmaField)
            {
                var deduplicator = new HashSet<string>();

                // Add consistent names for all fields of the type, independent on the order in which
                // they are compiled
                lock (this)
                {
                    if (!_mangledFieldNames.ContainsKey(field))
                    {
                        foreach (var f in field.OwningType.GetFields())
                        {
                            string name = SanitizeName(f.Name);

                            name = DisambiguateName(name, deduplicator);
                            deduplicator.Add(name);

                            if (prependTypeName != null)
                                name = prependTypeName + "__" + name;

                            _mangledFieldNames = _mangledFieldNames.Add(f, name);
                        }
                    }
                }

                return _mangledFieldNames[field];
            }


            string mangledName = SanitizeName(field.Name);

            if (prependTypeName != null)
                mangledName = prependTypeName + "__" + mangledName;

            Utf8String utf8MangledName = new Utf8String(mangledName);

            lock (this)
            {
                if (!_mangledFieldNames.ContainsKey(field))
                    _mangledFieldNames = _mangledFieldNames.Add(field, utf8MangledName);
            }

            return utf8MangledName;
        }

        private ImmutableDictionary<string, string> _mangledStringLiterals = ImmutableDictionary<string, string>.Empty;

        public override string GetMangledStringName(string literal)
        {
            string mangledName;
            if (_mangledStringLiterals.TryGetValue(literal, out mangledName))
                return mangledName;

            mangledName = SanitizeNameWithHash(literal);

            lock (this)
            {
                if (!_mangledStringLiterals.ContainsKey(literal))
                    _mangledStringLiterals = _mangledStringLiterals.Add(literal, mangledName);
            }

            return mangledName;
        }

        public string GetMangledDataBlobName(FieldDesc field)
        {
            return "__Data_" + ComputeMangledFieldName(field);
        }

        public string GetImportedTlsIndexPrefix()
        {
            uint ordinal;

            if (HasImport && GetTlsIndexOrdinal(out ordinal))
            {
                return OrdinalPrefix + ordinal;
            }
            else
            {
                return null;
            }
        }

        public string GetCurrentModuleTlsIndexPrefix()
        {
            uint ordinal;

            if (HasExport && GetTlsIndexOrdinal(out ordinal))
            {
                return OrdinalPrefix + ordinal;
            }
            else
            {
                // A module that doesn't have imports or exports, e.g, a single app binary, uses its CompilationUnitPrefix
                // as the tls index a prefix.
                return CompilationUnitPrefix;
            }
        }

        public string GetTlsIndexPrefix(MetadataType type)
        {
            uint ordinal;

            if (HasImport && _importOrdinals.typeOrdinals.TryGetValue(type, out ordinal))
            {
                return GetImportedTlsIndexPrefix();
            }
            else
            {
                return GetCurrentModuleTlsIndexPrefix();
            }
        }
    }
}