|
| 1 | +// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using LLVMSharp.Interop; |
| 6 | + |
| 7 | +namespace LLVMSharp |
| 8 | +{ |
| 9 | + public sealed class LLVMContext : IEquatable<LLVMContext> |
| 10 | + { |
| 11 | + private readonly Dictionary<LLVMValueRef, WeakReference<Value>> _createdValues = new Dictionary<LLVMValueRef, WeakReference<Value>>(); |
| 12 | + private readonly Dictionary<LLVMTypeRef, WeakReference<Type>> _createdTypes = new Dictionary<LLVMTypeRef, WeakReference<Type>>(); |
| 13 | + |
| 14 | + public LLVMContext() |
| 15 | + { |
| 16 | + Handle = LLVMContextRef.Create(); |
| 17 | + } |
| 18 | + |
| 19 | + public LLVMContextRef Handle { get; } |
| 20 | + |
| 21 | + public static bool operator ==(LLVMContext left, LLVMContext right) => (left is object) ? ((right is object) && (left.Handle == right.Handle)) : (right is null); |
| 22 | + |
| 23 | + public static bool operator !=(LLVMContext left, LLVMContext right) => (left is object) ? ((right is null) || (left.Handle != right.Handle)) : (right is object); |
| 24 | + |
| 25 | + public override bool Equals(object obj) => (obj is LLVMContext other) && Equals(other); |
| 26 | + |
| 27 | + public bool Equals(LLVMContext other) => this == other; |
| 28 | + |
| 29 | + public override int GetHashCode() => Handle.GetHashCode(); |
| 30 | + |
| 31 | + public override string ToString() => Handle.ToString(); |
| 32 | + |
| 33 | + internal BasicBlock GetOrCreate(LLVMBasicBlockRef handle) => GetOrCreate<BasicBlock>(handle.AsValue()); |
| 34 | + |
| 35 | + internal TType GetOrCreate<TType>(LLVMTypeRef handle) |
| 36 | + where TType : Type |
| 37 | + { |
| 38 | + WeakReference<Type> typeRef; |
| 39 | + |
| 40 | + if (handle == null) |
| 41 | + { |
| 42 | + return null; |
| 43 | + } |
| 44 | + else if (!_createdTypes.TryGetValue(handle, out typeRef)) |
| 45 | + { |
| 46 | + typeRef = new WeakReference<Type>(null); |
| 47 | + _createdTypes.Add(handle, typeRef); |
| 48 | + } |
| 49 | + |
| 50 | + if (!typeRef.TryGetTarget(out Type type)) |
| 51 | + { |
| 52 | + type = Type.Create(handle); |
| 53 | + typeRef.SetTarget(type); |
| 54 | + } |
| 55 | + return (TType)type; |
| 56 | + } |
| 57 | + |
| 58 | + internal TValue GetOrCreate<TValue>(LLVMValueRef handle) |
| 59 | + where TValue : Value |
| 60 | + { |
| 61 | + WeakReference<Value> valueRef; |
| 62 | + |
| 63 | + if (handle == null) |
| 64 | + { |
| 65 | + return null; |
| 66 | + } |
| 67 | + else if (!_createdValues.TryGetValue(handle, out valueRef)) |
| 68 | + { |
| 69 | + valueRef = new WeakReference<Value>(null); |
| 70 | + _createdValues.Add(handle, valueRef); |
| 71 | + } |
| 72 | + |
| 73 | + if (!valueRef.TryGetTarget(out Value value)) |
| 74 | + { |
| 75 | + value = Value.Create(handle); |
| 76 | + valueRef.SetTarget(value); |
| 77 | + } |
| 78 | + return (TValue)value; |
| 79 | + } |
| 80 | + |
| 81 | + internal Type GetOrCreate(LLVMTypeRef handle) => GetOrCreate<Type>(handle); |
| 82 | + |
| 83 | + internal Value GetOrCreate(LLVMValueRef handle) => GetOrCreate<Value>(handle); |
| 84 | + } |
| 85 | +} |
0 commit comments