Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
/*
Copyright (C) 2009 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Runtime.Serialization;
namespace IKVM.Reflection
{
[Serializable]
public sealed class AmbiguousMatchException : Exception
{
public AmbiguousMatchException()
{
}
public AmbiguousMatchException(string message)
: base(message)
{
}
public AmbiguousMatchException(string message, Exception inner)
: base(message, inner)
{
}
private AmbiguousMatchException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}

264
external/ikvm/reflect/Assembly.cs vendored Normal file
View File

@@ -0,0 +1,264 @@
/*
Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
namespace IKVM.Reflection
{
public delegate Module ModuleResolveEventHandler(object sender, ResolveEventArgs e);
public abstract class Assembly : ICustomAttributeProvider
{
internal readonly Universe universe;
protected string fullName; // AssemblyBuilder needs access to this field to clear it when the name changes
protected List<ModuleResolveEventHandler> resolvers;
internal Assembly(Universe universe)
{
this.universe = universe;
}
public sealed override string ToString()
{
return FullName;
}
public event ModuleResolveEventHandler ModuleResolve
{
add
{
if (resolvers == null)
{
resolvers = new List<ModuleResolveEventHandler>();
}
resolvers.Add(value);
}
remove
{
resolvers.Remove(value);
}
}
public abstract Type[] GetTypes();
public abstract AssemblyName GetName();
public abstract string ImageRuntimeVersion { get; }
public abstract Module ManifestModule { get; }
public abstract MethodInfo EntryPoint { get; }
public abstract string Location { get; }
public abstract AssemblyName[] GetReferencedAssemblies();
public abstract Module[] GetModules(bool getResourceModules);
public abstract Module[] GetLoadedModules(bool getResourceModules);
public abstract Module GetModule(string name);
public abstract string[] GetManifestResourceNames();
public abstract ManifestResourceInfo GetManifestResourceInfo(string resourceName);
public abstract System.IO.Stream GetManifestResourceStream(string name);
internal abstract Type FindType(TypeName name);
internal abstract Type FindTypeIgnoreCase(TypeName lowerCaseName);
// The differences between ResolveType and FindType are:
// - ResolveType is only used when a type is assumed to exist (because another module's metadata claims it)
// - ResolveType can return a MissingType
internal Type ResolveType(Module requester, TypeName typeName)
{
return FindType(typeName) ?? universe.GetMissingTypeOrThrow(requester, this.ManifestModule, null, typeName);
}
public string FullName
{
get { return fullName ?? (fullName = GetName().FullName); }
}
public Module[] GetModules()
{
return GetModules(true);
}
public IEnumerable<Module> Modules
{
get { return GetLoadedModules(); }
}
public Module[] GetLoadedModules()
{
return GetLoadedModules(true);
}
public AssemblyName GetName(bool copiedName)
{
return GetName();
}
public bool ReflectionOnly
{
get { return true; }
}
public Type[] GetExportedTypes()
{
List<Type> list = new List<Type>();
foreach (Type type in GetTypes())
{
if (type.IsVisible)
{
list.Add(type);
}
}
return list.ToArray();
}
public IEnumerable<Type> ExportedTypes
{
get { return GetExportedTypes(); }
}
public IEnumerable<TypeInfo> DefinedTypes
{
get
{
Type[] types = GetTypes();
TypeInfo[] typeInfos = new TypeInfo[types.Length];
for (int i = 0; i < types.Length; i++)
{
typeInfos[i] = types[i].GetTypeInfo();
}
return typeInfos;
}
}
public Type GetType(string name)
{
return GetType(name, false);
}
public Type GetType(string name, bool throwOnError)
{
return GetType(name, throwOnError, false);
}
public Type GetType(string name, bool throwOnError, bool ignoreCase)
{
TypeNameParser parser = TypeNameParser.Parse(name, throwOnError);
if (parser.Error)
{
return null;
}
if (parser.AssemblyName != null)
{
if (throwOnError)
{
throw new ArgumentException("Type names passed to Assembly.GetType() must not specify an assembly.");
}
else
{
return null;
}
}
TypeName typeName = TypeName.Split(TypeNameParser.Unescape(parser.FirstNamePart));
Type type = ignoreCase
? FindTypeIgnoreCase(typeName.ToLowerInvariant())
: FindType(typeName);
if (type == null && __IsMissing)
{
throw new MissingAssemblyException((MissingAssembly)this);
}
return parser.Expand(type, this.ManifestModule, throwOnError, name, false, ignoreCase);
}
public virtual Module LoadModule(string moduleName, byte[] rawModule)
{
throw new NotSupportedException();
}
public Module LoadModule(string moduleName, byte[] rawModule, byte[] rawSymbolStore)
{
return LoadModule(moduleName, rawModule);
}
public bool IsDefined(Type attributeType, bool inherit)
{
return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
}
public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
{
return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
}
public IList<CustomAttributeData> GetCustomAttributesData()
{
return CustomAttributeData.GetCustomAttributes(this);
}
public IEnumerable<CustomAttributeData> CustomAttributes
{
get { return GetCustomAttributesData(); }
}
public static string CreateQualifiedName(string assemblyName, string typeName)
{
return typeName + ", " + assemblyName;
}
public static Assembly GetAssembly(Type type)
{
return type.Assembly;
}
public string CodeBase
{
get
{
string path = this.Location.Replace(System.IO.Path.DirectorySeparatorChar, '/');
if (!path.StartsWith("/"))
{
path = "/" + path;
}
return "file://" + path;
}
}
public virtual bool IsDynamic
{
get { return false; }
}
public virtual bool __IsMissing
{
get { return false; }
}
public AssemblyNameFlags __AssemblyFlags
{
get { return GetAssemblyFlags(); }
}
protected virtual AssemblyNameFlags GetAssemblyFlags()
{
return GetName().Flags;
}
internal abstract IList<CustomAttributeData> GetCustomAttributesData(Type attributeType);
}
}

531
external/ikvm/reflect/AssemblyName.cs vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,51 @@
/*
Copyright (C) 2009 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Runtime.Serialization;
namespace IKVM.Reflection
{
[Serializable]
public sealed class BadImageFormatException : Exception
{
public BadImageFormatException()
{
}
public BadImageFormatException(string message)
: base(message)
{
}
public BadImageFormatException(string message, Exception inner)
: base(message, inner)
{
}
private BadImageFormatException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}

403
external/ikvm/reflect/Binder.cs vendored Normal file
View File

@@ -0,0 +1,403 @@
/*
Copyright (C) 2010-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Globalization;
namespace IKVM.Reflection
{
public abstract class Binder
{
protected Binder()
{
}
public virtual MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
{
throw new InvalidOperationException();
}
public virtual FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture)
{
throw new InvalidOperationException();
}
public virtual object ChangeType(object value, Type type, CultureInfo culture)
{
throw new InvalidOperationException();
}
public virtual void ReorderArgumentArray(ref object[] args, object state)
{
throw new InvalidOperationException();
}
public abstract MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers);
public abstract PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers);
}
sealed class DefaultBinder : Binder
{
public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
{
int matchCount = 0;
foreach (MethodBase method in match)
{
if (MatchParameterTypes(method.GetParameters(), types))
{
match[matchCount++] = method;
}
}
if (matchCount == 0)
{
return null;
}
MethodBase bestMatch = match[0];
bool ambiguous = false;
for (int i = 1; i < matchCount; i++)
{
SelectBestMatch(match[i], types, ref bestMatch, ref ambiguous);
}
if (ambiguous)
{
throw new AmbiguousMatchException();
}
return bestMatch;
}
private static bool MatchParameterTypes(ParameterInfo[] parameters, Type[] types)
{
if (parameters.Length != types.Length)
{
return false;
}
for (int i = 0; i < parameters.Length; i++)
{
Type sourceType = types[i];
Type targetType = parameters[i].ParameterType;
if (sourceType != targetType
&& !targetType.IsAssignableFrom(sourceType)
&& !IsAllowedPrimitiveConversion(sourceType, targetType))
{
return false;
}
}
return true;
}
private static void SelectBestMatch(MethodBase candidate, Type[] types, ref MethodBase currentBest, ref bool ambiguous)
{
switch (MatchSignatures(currentBest.MethodSignature, candidate.MethodSignature, types))
{
case 1:
return;
case 2:
ambiguous = false;
currentBest = candidate;
return;
}
if (currentBest.MethodSignature.MatchParameterTypes(candidate.MethodSignature))
{
int depth1 = GetInheritanceDepth(currentBest.DeclaringType);
int depth2 = GetInheritanceDepth(candidate.DeclaringType);
if (depth1 > depth2)
{
return;
}
else if (depth1 < depth2)
{
ambiguous = false;
currentBest = candidate;
return;
}
}
ambiguous = true;
}
private static int GetInheritanceDepth(Type type)
{
int depth = 0;
while (type != null)
{
depth++;
type = type.BaseType;
}
return depth;
}
private static int MatchSignatures(MethodSignature sig1, MethodSignature sig2, Type[] types)
{
for (int i = 0; i < sig1.GetParameterCount(); i++)
{
Type type1 = sig1.GetParameterType(i);
Type type2 = sig2.GetParameterType(i);
if (type1 != type2)
{
return MatchTypes(type1, type2, types[i]);
}
}
return 0;
}
private static int MatchSignatures(PropertySignature sig1, PropertySignature sig2, Type[] types)
{
for (int i = 0; i < sig1.ParameterCount; i++)
{
Type type1 = sig1.GetParameter(i);
Type type2 = sig2.GetParameter(i);
if (type1 != type2)
{
return MatchTypes(type1, type2, types[i]);
}
}
return 0;
}
private static int MatchTypes(Type type1, Type type2, Type type)
{
if (type1 == type)
{
return 1;
}
if (type2 == type)
{
return 2;
}
bool conv = type1.IsAssignableFrom(type2);
return conv == type2.IsAssignableFrom(type1) ? 0 : conv ? 2 : 1;
}
private static bool IsAllowedPrimitiveConversion(Type source, Type target)
{
// we need to check for primitives, because GetTypeCode will return the underlying type for enums
if (!source.IsPrimitive || !target.IsPrimitive)
{
return false;
}
TypeCode sourceType = Type.GetTypeCode(source);
TypeCode targetType = Type.GetTypeCode(target);
switch (sourceType)
{
case TypeCode.Char:
switch (targetType)
{
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.Int32:
case TypeCode.UInt64:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
case TypeCode.Byte:
switch (targetType)
{
case TypeCode.Char:
case TypeCode.UInt16:
case TypeCode.Int16:
case TypeCode.UInt32:
case TypeCode.Int32:
case TypeCode.UInt64:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
case TypeCode.SByte:
switch (targetType)
{
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
case TypeCode.UInt16:
switch (targetType)
{
case TypeCode.UInt32:
case TypeCode.Int32:
case TypeCode.UInt64:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
case TypeCode.Int16:
switch (targetType)
{
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
case TypeCode.UInt32:
switch (targetType)
{
case TypeCode.UInt64:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
case TypeCode.Int32:
switch (targetType)
{
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
case TypeCode.UInt64:
switch (targetType)
{
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
case TypeCode.Int64:
switch (targetType)
{
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
case TypeCode.Single:
switch (targetType)
{
case TypeCode.Double:
return true;
default:
return false;
}
default:
return false;
}
}
public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
{
int matchCount = 0;
foreach (PropertyInfo property in match)
{
if (indexes == null || MatchParameterTypes(property.GetIndexParameters(), indexes))
{
if (returnType != null)
{
if (property.PropertyType.IsPrimitive)
{
if (!IsAllowedPrimitiveConversion(returnType, property.PropertyType))
{
continue;
}
}
else
{
if (!property.PropertyType.IsAssignableFrom(returnType))
{
continue;
}
}
}
match[matchCount++] = property;
}
}
if (matchCount == 0)
{
return null;
}
if (matchCount == 1)
{
return match[0];
}
PropertyInfo bestMatch = match[0];
bool ambiguous = false;
for (int i = 1; i < matchCount; i++)
{
int best = MatchTypes(bestMatch.PropertyType, match[i].PropertyType, returnType);
if (best == 0 && indexes != null)
{
best = MatchSignatures(bestMatch.PropertySignature, match[i].PropertySignature, indexes);
}
if (best == 0)
{
int depth1 = GetInheritanceDepth(bestMatch.DeclaringType);
int depth2 = GetInheritanceDepth(match[i].DeclaringType);
if (bestMatch.Name == match[i].Name && depth1 != depth2)
{
if (depth1 > depth2)
{
best = 1;
}
else
{
best = 2;
}
}
else
{
ambiguous = true;
}
}
if (best == 2)
{
ambiguous = false;
bestMatch = match[i];
}
}
if (ambiguous)
{
throw new AmbiguousMatchException();
}
return bestMatch;
}
}
}

239
external/ikvm/reflect/ConstructorInfo.cs vendored Normal file
View File

@@ -0,0 +1,239 @@
/*
Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace IKVM.Reflection
{
public abstract class ConstructorInfo : MethodBase
{
// prevent external subclasses
internal ConstructorInfo()
{
}
public sealed override string ToString()
{
return GetMethodInfo().ToString();
}
public static readonly string ConstructorName = ".ctor";
public static readonly string TypeConstructorName = ".cctor";
internal abstract MethodInfo GetMethodInfo();
internal override MethodBase BindTypeParameters(Type type)
{
return new ConstructorInfoImpl((MethodInfo)GetMethodInfo().BindTypeParameters(type));
}
public sealed override MethodBase __GetMethodOnTypeDefinition()
{
return new ConstructorInfoImpl((MethodInfo)GetMethodInfo().__GetMethodOnTypeDefinition());
}
public sealed override MemberTypes MemberType
{
get { return MemberTypes.Constructor; }
}
public sealed override int __MethodRVA
{
get { return GetMethodInfo().__MethodRVA; }
}
public sealed override bool ContainsGenericParameters
{
get { return GetMethodInfo().ContainsGenericParameters; }
}
public ParameterInfo __ReturnParameter
{
get { return new ParameterInfoWrapper(this, GetMethodInfo().ReturnParameter); }
}
public sealed override ParameterInfo[] GetParameters()
{
ParameterInfo[] parameters = GetMethodInfo().GetParameters();
for (int i = 0; i < parameters.Length; i++)
{
parameters[i] = new ParameterInfoWrapper(this, parameters[i]);
}
return parameters;
}
public sealed override CallingConventions CallingConvention
{
get { return GetMethodInfo().CallingConvention; }
}
public sealed override MethodAttributes Attributes
{
get { return GetMethodInfo().Attributes; }
}
public sealed override MethodImplAttributes GetMethodImplementationFlags()
{
return GetMethodInfo().GetMethodImplementationFlags();
}
public sealed override Type DeclaringType
{
get { return GetMethodInfo().DeclaringType; }
}
public sealed override string Name
{
get { return GetMethodInfo().Name; }
}
public sealed override int MetadataToken
{
get { return GetMethodInfo().MetadataToken; }
}
public sealed override Module Module
{
get { return GetMethodInfo().Module; }
}
public sealed override MethodBody GetMethodBody()
{
return GetMethodInfo().GetMethodBody();
}
public sealed override bool __IsMissing
{
get { return GetMethodInfo().__IsMissing; }
}
internal sealed override int ParameterCount
{
get { return GetMethodInfo().ParameterCount; }
}
internal sealed override MemberInfo SetReflectedType(Type type)
{
return new ConstructorInfoWithReflectedType(type, this);
}
internal sealed override int GetCurrentToken()
{
return GetMethodInfo().GetCurrentToken();
}
internal sealed override List<CustomAttributeData> GetPseudoCustomAttributes(Type attributeType)
{
return GetMethodInfo().GetPseudoCustomAttributes(attributeType);
}
internal sealed override bool IsBaked
{
get { return GetMethodInfo().IsBaked; }
}
internal sealed override MethodSignature MethodSignature
{
get { return GetMethodInfo().MethodSignature; }
}
internal sealed override int ImportTo(Emit.ModuleBuilder module)
{
return GetMethodInfo().ImportTo(module);
}
}
sealed class ConstructorInfoImpl : ConstructorInfo
{
private readonly MethodInfo method;
internal ConstructorInfoImpl(MethodInfo method)
{
this.method = method;
}
public override bool Equals(object obj)
{
ConstructorInfoImpl other = obj as ConstructorInfoImpl;
return other != null && other.method.Equals(method);
}
public override int GetHashCode()
{
return method.GetHashCode();
}
internal override MethodInfo GetMethodInfo()
{
return method;
}
internal override MethodInfo GetMethodOnTypeDefinition()
{
return method.GetMethodOnTypeDefinition();
}
}
sealed class ConstructorInfoWithReflectedType : ConstructorInfo
{
private readonly Type reflectedType;
private readonly ConstructorInfo ctor;
internal ConstructorInfoWithReflectedType(Type reflectedType, ConstructorInfo ctor)
{
Debug.Assert(reflectedType != ctor.DeclaringType);
this.reflectedType = reflectedType;
this.ctor = ctor;
}
public override bool Equals(object obj)
{
ConstructorInfoWithReflectedType other = obj as ConstructorInfoWithReflectedType;
return other != null
&& other.reflectedType == reflectedType
&& other.ctor == ctor;
}
public override int GetHashCode()
{
return reflectedType.GetHashCode() ^ ctor.GetHashCode();
}
public override Type ReflectedType
{
get { return reflectedType; }
}
internal override MethodInfo GetMethodInfo()
{
return ctor.GetMethodInfo();
}
internal override MethodInfo GetMethodOnTypeDefinition()
{
return ctor.GetMethodOnTypeDefinition();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,81 @@
/*
Copyright (C) 2009 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace IKVM.Reflection
{
public struct CustomAttributeNamedArgument
{
private readonly MemberInfo member;
private readonly CustomAttributeTypedArgument value;
internal CustomAttributeNamedArgument(MemberInfo member, CustomAttributeTypedArgument value)
{
this.member = member;
this.value = value;
}
public override bool Equals(object obj)
{
return this == obj as CustomAttributeNamedArgument?;
}
public override int GetHashCode()
{
return member.GetHashCode() ^ 53 * value.GetHashCode();
}
public MemberInfo MemberInfo
{
get { return member; }
}
public CustomAttributeTypedArgument TypedValue
{
get { return value; }
}
public bool IsField
{
get { return member.MemberType == MemberTypes.Field; }
}
public string MemberName
{
get { return member.Name; }
}
public static bool operator ==(CustomAttributeNamedArgument arg1, CustomAttributeNamedArgument arg2)
{
return arg1.member.Equals(arg2.member) && arg1.value == arg2.value;
}
public static bool operator !=(CustomAttributeNamedArgument arg1, CustomAttributeNamedArgument arg2)
{
return !(arg1 == arg2);
}
}
}

View File

@@ -0,0 +1,71 @@
/*
Copyright (C) 2009 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace IKVM.Reflection
{
public struct CustomAttributeTypedArgument
{
private readonly Type type;
private readonly object value;
internal CustomAttributeTypedArgument(Type type, object value)
{
this.type = type;
this.value = value;
}
public override bool Equals(object obj)
{
return this == obj as CustomAttributeTypedArgument?;
}
public override int GetHashCode()
{
return type.GetHashCode() ^ 77 * (value == null ? 0 : value.GetHashCode());
}
public Type ArgumentType
{
get { return type; }
}
public Object Value
{
get { return value; }
}
public static bool operator ==(CustomAttributeTypedArgument arg1, CustomAttributeTypedArgument arg2)
{
return arg1.type.Equals(arg2.type) && (arg1.value == arg2.value || (arg1.value != null && arg1.value.Equals(arg2.value)));
}
public static bool operator !=(CustomAttributeTypedArgument arg1, CustomAttributeTypedArgument arg2)
{
return !(arg1 == arg2);
}
}
}

357
external/ikvm/reflect/CustomModifiers.cs vendored Normal file
View File

@@ -0,0 +1,357 @@
/*
Copyright (C) 2011 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using IKVM.Reflection.Emit;
using IKVM.Reflection.Reader;
namespace IKVM.Reflection
{
public struct CustomModifiers : IEquatable<CustomModifiers>, IEnumerable<CustomModifiers.Entry>
{
// note that FromReqOpt assumes that Initial == ModOpt
private static Type Initial { get { return MarkerType.ModOpt; } }
private readonly Type[] types;
internal CustomModifiers(List<CustomModifiersBuilder.Item> list)
{
bool required = Initial == MarkerType.ModReq;
int count = list.Count;
foreach (CustomModifiersBuilder.Item item in list)
{
if (item.required != required)
{
required = item.required;
count++;
}
}
types = new Type[count];
required = Initial == MarkerType.ModReq;
int index = 0;
foreach (CustomModifiersBuilder.Item item in list)
{
if (item.required != required)
{
required = item.required;
types[index++] = required ? MarkerType.ModReq : MarkerType.ModOpt;
}
types[index++] = item.type;
}
}
private CustomModifiers(Type[] types)
{
Debug.Assert(types == null || types.Length != 0);
this.types = types;
}
public struct Enumerator : IEnumerator<Entry>
{
private readonly Type[] types;
private int index;
private bool required;
internal Enumerator(Type[] types)
{
this.types = types;
this.index = -1;
this.required = Initial == MarkerType.ModReq;
}
void System.Collections.IEnumerator.Reset()
{
this.index = -1;
this.required = Initial == MarkerType.ModReq;
}
public Entry Current
{
get { return new Entry(types[index], required); }
}
public bool MoveNext()
{
if (types == null || index == types.Length)
{
return false;
}
index++;
if (index == types.Length)
{
return false;
}
else if (types[index] == MarkerType.ModOpt)
{
required = false;
index++;
}
else if (types[index] == MarkerType.ModReq)
{
required = true;
index++;
}
return true;
}
object System.Collections.IEnumerator.Current
{
get { return Current; }
}
void IDisposable.Dispose()
{
}
}
public struct Entry
{
private readonly Type type;
private readonly bool required;
internal Entry(Type type, bool required)
{
this.type = type;
this.required = required;
}
public Type Type
{
get { return type; }
}
public bool IsRequired
{
get { return required; }
}
}
public Enumerator GetEnumerator()
{
return new Enumerator(types);
}
IEnumerator<Entry> IEnumerable<Entry>.GetEnumerator()
{
return GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool IsEmpty
{
get { return types == null; }
}
public bool Equals(CustomModifiers other)
{
return Util.ArrayEquals(types, other.types);
}
public override bool Equals(object obj)
{
CustomModifiers? other = obj as CustomModifiers?;
return other != null && Equals(other.Value);
}
public override int GetHashCode()
{
return Util.GetHashCode(types);
}
public override string ToString()
{
if (types == null)
{
return string.Empty;
}
StringBuilder sb = new StringBuilder();
string sep = "";
foreach (Entry e in this)
{
sb.Append(sep).Append(e.IsRequired ? "modreq(" : "modopt(").Append(e.Type.FullName).Append(')');
sep = " ";
}
return sb.ToString();
}
public bool ContainsMissingType
{
get { return Type.ContainsMissingType(types); }
}
private Type[] GetRequiredOrOptional(bool required)
{
if (types == null)
{
return Type.EmptyTypes;
}
int count = 0;
foreach (Entry e in this)
{
if (e.IsRequired == required)
{
count++;
}
}
Type[] result = new Type[count];
foreach (Entry e in this)
{
if (e.IsRequired == required)
{
// FXBUG reflection (and ildasm) return custom modifiers in reverse order
// while SRE writes them in the specified order
result[--count] = e.Type;
}
}
return result;
}
internal Type[] GetRequired()
{
return GetRequiredOrOptional(true);
}
internal Type[] GetOptional()
{
return GetRequiredOrOptional(false);
}
internal CustomModifiers Bind(IGenericBinder binder)
{
if (types == null)
{
return this;
}
Type[] result = types;
for (int i = 0; i < types.Length; i++)
{
if (types[i] == MarkerType.ModOpt || types[i] == MarkerType.ModReq)
{
continue;
}
Type type = types[i].BindTypeParameters(binder);
if (!ReferenceEquals(type, types[i]))
{
if (result == types)
{
result = (Type[])types.Clone();
}
result[i] = type;
}
}
return new CustomModifiers(result);
}
internal static CustomModifiers Read(ModuleReader module, ByteReader br, IGenericContext context)
{
byte b = br.PeekByte();
if (!IsCustomModifier(b))
{
return new CustomModifiers();
}
List<Type> list = new List<Type>();
Type mode = Initial;
do
{
Type cmod = br.ReadByte() == Signature.ELEMENT_TYPE_CMOD_REQD ? MarkerType.ModReq : MarkerType.ModOpt;
if (mode != cmod)
{
mode = cmod;
list.Add(mode);
}
list.Add(Signature.ReadTypeDefOrRefEncoded(module, br, context));
b = br.PeekByte();
}
while (IsCustomModifier(b));
return new CustomModifiers(list.ToArray());
}
internal static void Skip(ByteReader br)
{
byte b = br.PeekByte();
while (IsCustomModifier(b))
{
br.ReadByte();
br.ReadCompressedUInt();
b = br.PeekByte();
}
}
internal static CustomModifiers FromReqOpt(Type[] req, Type[] opt)
{
List<Type> list = null;
if (opt != null && opt.Length != 0)
{
Debug.Assert(Initial == MarkerType.ModOpt);
list = new List<Type>(opt);
}
if (req != null && req.Length != 0)
{
if (list == null)
{
list = new List<Type>();
}
list.Add(MarkerType.ModReq);
list.AddRange(req);
}
if (list == null)
{
return new CustomModifiers();
}
else
{
return new CustomModifiers(list.ToArray());
}
}
private static bool IsCustomModifier(byte b)
{
return b == Signature.ELEMENT_TYPE_CMOD_OPT || b == Signature.ELEMENT_TYPE_CMOD_REQD;
}
internal static CustomModifiers Combine(CustomModifiers mods1, CustomModifiers mods2)
{
if (mods1.IsEmpty)
{
return mods2;
}
else if (mods2.IsEmpty)
{
return mods1;
}
else
{
Type[] combo = new Type[mods1.types.Length + mods2.types.Length];
Array.Copy(mods1.types, combo, mods1.types.Length);
Array.Copy(mods2.types, 0, combo, mods1.types.Length, mods2.types.Length);
return new CustomModifiers(combo);
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,141 @@
/*
Copyright (C) 2008-2011 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
namespace IKVM.Reflection.Emit
{
public sealed class ConstructorBuilder : ConstructorInfo
{
private readonly MethodBuilder methodBuilder;
internal ConstructorBuilder(MethodBuilder mb)
{
this.methodBuilder = mb;
}
public override bool Equals(object obj)
{
ConstructorBuilder other = obj as ConstructorBuilder;
return other != null && other.methodBuilder.Equals(methodBuilder);
}
public override int GetHashCode()
{
return methodBuilder.GetHashCode();
}
public void __SetSignature(Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
{
methodBuilder.__SetSignature(returnType, returnTypeCustomModifiers, parameterTypes, parameterTypeCustomModifiers);
}
[Obsolete("Please use __SetSignature(Type, CustomModifiers, Type[], CustomModifiers[]) instead.")]
public void __SetSignature(Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
{
methodBuilder.SetSignature(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
}
public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string strParamName)
{
return methodBuilder.DefineParameter(position, attributes, strParamName);
}
public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
{
methodBuilder.SetCustomAttribute(customBuilder);
}
public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
{
methodBuilder.SetCustomAttribute(con, binaryAttribute);
}
public void __AddDeclarativeSecurity(CustomAttributeBuilder customBuilder)
{
methodBuilder.__AddDeclarativeSecurity(customBuilder);
}
public void AddDeclarativeSecurity(System.Security.Permissions.SecurityAction securityAction, System.Security.PermissionSet permissionSet)
{
methodBuilder.AddDeclarativeSecurity(securityAction, permissionSet);
}
public void SetImplementationFlags(MethodImplAttributes attributes)
{
methodBuilder.SetImplementationFlags(attributes);
}
public ILGenerator GetILGenerator()
{
return methodBuilder.GetILGenerator();
}
public ILGenerator GetILGenerator(int streamSize)
{
return methodBuilder.GetILGenerator(streamSize);
}
public void __ReleaseILGenerator()
{
methodBuilder.__ReleaseILGenerator();
}
public Type ReturnType
{
get { return methodBuilder.ReturnType; }
}
public Module GetModule()
{
return methodBuilder.GetModule();
}
public MethodToken GetToken()
{
return methodBuilder.GetToken();
}
public bool InitLocals
{
get { return methodBuilder.InitLocals; }
set { methodBuilder.InitLocals = value; }
}
public void SetMethodBody(byte[] il, int maxStack, byte[] localSignature, IEnumerable<ExceptionHandler> exceptionHandlers, IEnumerable<int> tokenFixups)
{
methodBuilder.SetMethodBody(il, maxStack, localSignature, exceptionHandlers, tokenFixups);
}
internal override MethodInfo GetMethodInfo()
{
return methodBuilder;
}
internal override MethodInfo GetMethodOnTypeDefinition()
{
return methodBuilder;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,75 @@
/*
Copyright (C) 2011 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace IKVM.Reflection.Emit
{
public sealed class CustomModifiersBuilder
{
private readonly List<Item> list = new List<Item>();
internal struct Item
{
internal Type type;
internal bool required;
}
public void AddRequired(Type type)
{
Item item;
item.type = type;
item.required = true;
list.Add(item);
}
public void AddOptional(Type type)
{
Item item;
item.type = type;
item.required = false;
list.Add(item);
}
// this adds the custom modifiers in the same order as the normal SRE APIs
// (the advantage over using the SRE APIs is that a CustomModifiers object is slightly more efficient,
// because unlike the Type arrays it doesn't need to be copied)
public void Add(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
{
foreach (CustomModifiers.Entry entry in CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers))
{
Item item;
item.type = entry.Type;
item.required = entry.IsRequired;
list.Add(item);
}
}
public CustomModifiers Create()
{
return new CustomModifiers(list);
}
}
}

View File

@@ -0,0 +1,124 @@
/*
Copyright (C) 2010 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace IKVM.Reflection.Emit
{
public sealed class EnumBuilder : TypeInfo
{
private readonly TypeBuilder typeBuilder;
private readonly FieldBuilder fieldBuilder;
internal EnumBuilder(TypeBuilder typeBuilder, FieldBuilder fieldBuilder)
: base(typeBuilder)
{
this.typeBuilder = typeBuilder;
this.fieldBuilder = fieldBuilder;
}
public override string __Name
{
get { return typeBuilder.__Name; }
}
public override string __Namespace
{
get { return typeBuilder.__Namespace; }
}
public override string Name
{
get { return typeBuilder.Name; }
}
public override string FullName
{
get { return typeBuilder.FullName; }
}
public override Type BaseType
{
get { return typeBuilder.BaseType; }
}
public override TypeAttributes Attributes
{
get { return typeBuilder.Attributes; }
}
public override Module Module
{
get { return typeBuilder.Module; }
}
public FieldBuilder DefineLiteral(string literalName, object literalValue)
{
FieldBuilder fb = typeBuilder.DefineField(literalName, typeBuilder, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal);
fb.SetConstant(literalValue);
return fb;
}
public Type CreateType()
{
return typeBuilder.CreateType();
}
public TypeInfo CreateTypeInfo()
{
return typeBuilder.CreateTypeInfo();
}
public TypeToken TypeToken
{
get { return typeBuilder.TypeToken; }
}
public FieldBuilder UnderlyingField
{
get { return fieldBuilder; }
}
public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
{
typeBuilder.SetCustomAttribute(con, binaryAttribute);
}
public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
{
typeBuilder.SetCustomAttribute(customBuilder);
}
public override Type GetEnumUnderlyingType()
{
return fieldBuilder.FieldType;
}
internal override bool IsBaked
{
get { return typeBuilder.IsBaked; }
}
}
}

129
external/ikvm/reflect/Emit/Enums.cs vendored Normal file
View File

@@ -0,0 +1,129 @@
/*
Copyright (C) 2008 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
namespace IKVM.Reflection.Emit
{
public enum AssemblyBuilderAccess
{
Save = 2,
ReflectionOnly = 6,
}
public enum OpCodeType
{
Annotation = 0,
Macro = 1,
Nternal = 2,
Objmodel = 3,
Prefix = 4,
Primitive = 5,
}
public enum OperandType
{
InlineBrTarget = 0,
InlineField = 1,
InlineI = 2,
InlineI8 = 3,
InlineMethod = 4,
InlineNone = 5,
InlinePhi = 6,
InlineR = 7,
InlineSig = 9,
InlineString = 10,
InlineSwitch = 11,
InlineTok = 12,
InlineType = 13,
InlineVar = 14,
ShortInlineBrTarget = 15,
ShortInlineI = 16,
ShortInlineR = 17,
ShortInlineVar = 18,
}
public enum FlowControl
{
Branch = 0,
Break = 1,
Call = 2,
Cond_Branch = 3,
Meta = 4,
Next = 5,
Return = 7,
Throw = 8,
}
public enum PackingSize
{
Unspecified = 0,
Size1 = 1,
Size2 = 2,
Size4 = 4,
Size8 = 8,
Size16 = 16,
Size32 = 32,
Size64 = 64,
Size128 = 128,
}
public enum PEFileKinds
{
Dll = 1,
ConsoleApplication = 2,
WindowApplication = 3,
}
public enum StackBehaviour
{
Pop0 = 0,
Pop1 = 1,
Pop1_pop1 = 2,
Popi = 3,
Popi_pop1 = 4,
Popi_popi = 5,
Popi_popi8 = 6,
Popi_popi_popi = 7,
Popi_popr4 = 8,
Popi_popr8 = 9,
Popref = 10,
Popref_pop1 = 11,
Popref_popi = 12,
Popref_popi_popi = 13,
Popref_popi_popi8 = 14,
Popref_popi_popr4 = 15,
Popref_popi_popr8 = 16,
Popref_popi_popref = 17,
Push0 = 18,
Push1 = 19,
Push1_push1 = 20,
Pushi = 21,
Pushi8 = 22,
Pushr4 = 23,
Pushr8 = 24,
Pushref = 25,
Varpop = 26,
Varpush = 27,
Popref_popi_pop1 = 28,
}
}

View File

@@ -0,0 +1,281 @@
/*
Copyright (C) 2009-2011 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using IKVM.Reflection.Metadata;
using IKVM.Reflection.Writer;
namespace IKVM.Reflection.Emit
{
public sealed class EventBuilder : EventInfo
{
private readonly TypeBuilder typeBuilder;
private readonly string name;
private EventAttributes attributes;
private readonly int eventtype;
private MethodBuilder addOnMethod;
private MethodBuilder removeOnMethod;
private MethodBuilder fireMethod;
private readonly List<Accessor> accessors = new List<Accessor>();
private int lazyPseudoToken;
private struct Accessor
{
internal short Semantics;
internal MethodBuilder Method;
}
internal EventBuilder(TypeBuilder typeBuilder, string name, EventAttributes attributes, Type eventtype)
{
this.typeBuilder = typeBuilder;
this.name = name;
this.attributes = attributes;
this.eventtype = typeBuilder.ModuleBuilder.GetTypeTokenForMemberRef(eventtype);
}
public void SetAddOnMethod(MethodBuilder mdBuilder)
{
addOnMethod = mdBuilder;
Accessor acc;
acc.Semantics = MethodSemanticsTable.AddOn;
acc.Method = mdBuilder;
accessors.Add(acc);
}
public void SetRemoveOnMethod(MethodBuilder mdBuilder)
{
removeOnMethod = mdBuilder;
Accessor acc;
acc.Semantics = MethodSemanticsTable.RemoveOn;
acc.Method = mdBuilder;
accessors.Add(acc);
}
public void SetRaiseMethod(MethodBuilder mdBuilder)
{
fireMethod = mdBuilder;
Accessor acc;
acc.Semantics = MethodSemanticsTable.Fire;
acc.Method = mdBuilder;
accessors.Add(acc);
}
public void AddOtherMethod(MethodBuilder mdBuilder)
{
Accessor acc;
acc.Semantics = MethodSemanticsTable.Other;
acc.Method = mdBuilder;
accessors.Add(acc);
}
public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
{
SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
}
public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
{
Universe u = typeBuilder.ModuleBuilder.universe;
if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute)
{
attributes |= EventAttributes.SpecialName;
}
else
{
if (lazyPseudoToken == 0)
{
lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken();
}
typeBuilder.ModuleBuilder.SetCustomAttribute(lazyPseudoToken, customBuilder);
}
}
public override EventAttributes Attributes
{
get { return attributes; }
}
public override MethodInfo GetAddMethod(bool nonPublic)
{
return nonPublic || (addOnMethod != null && addOnMethod.IsPublic) ? addOnMethod : null;
}
public override MethodInfo GetRemoveMethod(bool nonPublic)
{
return nonPublic || (removeOnMethod != null && removeOnMethod.IsPublic) ? removeOnMethod : null;
}
public override MethodInfo GetRaiseMethod(bool nonPublic)
{
return nonPublic || (fireMethod != null && fireMethod.IsPublic) ? fireMethod : null;
}
public override MethodInfo[] GetOtherMethods(bool nonPublic)
{
List<MethodInfo> list = new List<MethodInfo>();
foreach (Accessor acc in accessors)
{
if (acc.Semantics == MethodSemanticsTable.Other && (nonPublic || acc.Method.IsPublic))
{
list.Add(acc.Method);
}
}
return list.ToArray();
}
public override MethodInfo[] __GetMethods()
{
List<MethodInfo> list = new List<MethodInfo>();
foreach (Accessor acc in accessors)
{
list.Add(acc.Method);
}
return list.ToArray();
}
public override Type DeclaringType
{
get { return typeBuilder; }
}
public override string Name
{
get { return name; }
}
public override Module Module
{
get { return typeBuilder.ModuleBuilder; }
}
public EventToken GetEventToken()
{
if (lazyPseudoToken == 0)
{
lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken();
}
return new EventToken(lazyPseudoToken);
}
public override Type EventHandlerType
{
get { return typeBuilder.ModuleBuilder.ResolveType(eventtype); }
}
internal void Bake()
{
EventTable.Record rec = new EventTable.Record();
rec.EventFlags = (short)attributes;
rec.Name = typeBuilder.ModuleBuilder.Strings.Add(name);
rec.EventType = eventtype;
int token = 0x14000000 | typeBuilder.ModuleBuilder.Event.AddRecord(rec);
if (lazyPseudoToken == 0)
{
lazyPseudoToken = token;
}
else
{
typeBuilder.ModuleBuilder.RegisterTokenFixup(lazyPseudoToken, token);
}
foreach (Accessor acc in accessors)
{
AddMethodSemantics(acc.Semantics, acc.Method.MetadataToken, token);
}
}
private void AddMethodSemantics(short semantics, int methodToken, int propertyToken)
{
MethodSemanticsTable.Record rec = new MethodSemanticsTable.Record();
rec.Semantics = semantics;
rec.Method = methodToken;
rec.Association = propertyToken;
typeBuilder.ModuleBuilder.MethodSemantics.AddRecord(rec);
}
internal override bool IsPublic
{
get
{
foreach (Accessor acc in accessors)
{
if (acc.Method.IsPublic)
{
return true;
}
}
return false;
}
}
internal override bool IsNonPrivate
{
get
{
foreach (Accessor acc in accessors)
{
if ((acc.Method.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private)
{
return true;
}
}
return false;
}
}
internal override bool IsStatic
{
get
{
foreach (Accessor acc in accessors)
{
if (acc.Method.IsStatic)
{
return true;
}
}
return false;
}
}
internal override bool IsBaked
{
get { return typeBuilder.IsBaked; }
}
internal override int GetCurrentToken()
{
if (typeBuilder.ModuleBuilder.IsSaved && ModuleBuilder.IsPseudoToken(lazyPseudoToken))
{
return typeBuilder.ModuleBuilder.ResolvePseudoToken(lazyPseudoToken);
}
else
{
return lazyPseudoToken;
}
}
}
}

View File

@@ -0,0 +1,121 @@
/*
Copyright (C) 2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
namespace IKVM.Reflection.Emit
{
public struct ExceptionHandler : IEquatable<ExceptionHandler>
{
private readonly int tryOffset;
private readonly int tryLength;
private readonly int filterOffset;
private readonly int handlerOffset;
private readonly int handlerLength;
private readonly ExceptionHandlingClauseOptions kind;
private readonly int exceptionTypeToken;
public ExceptionHandler(int tryOffset, int tryLength, int filterOffset, int handlerOffset, int handlerLength, ExceptionHandlingClauseOptions kind, int exceptionTypeToken)
{
if (tryOffset < 0 || tryLength < 0 || filterOffset < 0 || handlerOffset < 0 || handlerLength < 0)
{
throw new ArgumentOutOfRangeException();
}
this.tryOffset = tryOffset;
this.tryLength = tryLength;
this.filterOffset = filterOffset;
this.handlerOffset = handlerOffset;
this.handlerLength = handlerLength;
this.kind = kind;
this.exceptionTypeToken = exceptionTypeToken;
}
public int TryOffset
{
get { return tryOffset; }
}
public int TryLength
{
get { return tryLength; }
}
public int FilterOffset
{
get { return filterOffset; }
}
public int HandlerOffset
{
get { return handlerOffset; }
}
public int HandlerLength
{
get { return handlerLength; }
}
public ExceptionHandlingClauseOptions Kind
{
get { return kind; }
}
public int ExceptionTypeToken
{
get { return exceptionTypeToken; }
}
public bool Equals(ExceptionHandler other)
{
return tryOffset == other.tryOffset
&& tryLength == other.tryLength
&& filterOffset == other.filterOffset
&& handlerOffset == other.handlerOffset
&& handlerLength == other.handlerLength
&& kind == other.kind
&& exceptionTypeToken == other.exceptionTypeToken;
}
public override bool Equals(object obj)
{
ExceptionHandler? other = obj as ExceptionHandler?;
return other != null && Equals(other.Value);
}
public override int GetHashCode()
{
return tryOffset ^ tryLength * 33 ^ filterOffset * 333 ^ handlerOffset * 3333 ^ handlerLength * 33333;
}
public static bool operator ==(ExceptionHandler left, ExceptionHandler right)
{
return left.Equals(right);
}
public static bool operator !=(ExceptionHandler left, ExceptionHandler right)
{
return !left.Equals(right);
}
}
}

View File

@@ -0,0 +1,229 @@
/*
Copyright (C) 2008-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using IKVM.Reflection.Metadata;
using IKVM.Reflection.Writer;
namespace IKVM.Reflection.Emit
{
public sealed class FieldBuilder : FieldInfo
{
private readonly TypeBuilder typeBuilder;
private readonly string name;
private readonly int pseudoToken;
private FieldAttributes attribs;
private readonly int nameIndex;
private readonly int signature;
private readonly FieldSignature fieldSig;
internal FieldBuilder(TypeBuilder type, string name, Type fieldType, CustomModifiers customModifiers, FieldAttributes attribs)
{
this.typeBuilder = type;
this.name = name;
this.pseudoToken = type.ModuleBuilder.AllocPseudoToken();
this.nameIndex = type.ModuleBuilder.Strings.Add(name);
this.fieldSig = FieldSignature.Create(fieldType, customModifiers);
ByteBuffer sig = new ByteBuffer(5);
fieldSig.WriteSig(this.typeBuilder.ModuleBuilder, sig);
this.signature = this.typeBuilder.ModuleBuilder.Blobs.Add(sig);
this.attribs = attribs;
this.typeBuilder.ModuleBuilder.Field.AddVirtualRecord();
}
public void SetConstant(object defaultValue)
{
attribs |= FieldAttributes.HasDefault;
typeBuilder.ModuleBuilder.AddConstant(pseudoToken, defaultValue);
}
public override object GetRawConstantValue()
{
if (!typeBuilder.IsCreated())
{
// the .NET FieldBuilder doesn't support this method
// (since we dont' have a different FieldInfo object after baking, we will support it once we're baked)
throw new NotSupportedException();
}
return typeBuilder.Module.Constant.GetRawConstantValue(typeBuilder.Module, GetCurrentToken());
}
public void __SetDataAndRVA(byte[] data)
{
SetDataAndRvaImpl(data, typeBuilder.ModuleBuilder.initializedData, 0);
}
public void __SetReadOnlyDataAndRVA(byte[] data)
{
SetDataAndRvaImpl(data, typeBuilder.ModuleBuilder.methodBodies, unchecked((int)0x80000000));
}
private void SetDataAndRvaImpl(byte[] data, ByteBuffer bb, int readonlyMarker)
{
attribs |= FieldAttributes.HasFieldRVA;
FieldRVATable.Record rec = new FieldRVATable.Record();
bb.Align(8);
rec.RVA = bb.Position + readonlyMarker;
rec.Field = pseudoToken;
typeBuilder.ModuleBuilder.FieldRVA.AddRecord(rec);
bb.Write(data);
}
public override void __GetDataFromRVA(byte[] data, int offset, int length)
{
throw new NotImplementedException();
}
public override int __FieldRVA
{
get { throw new NotImplementedException(); }
}
public override bool __TryGetFieldOffset(out int offset)
{
int pseudoTokenOrIndex = pseudoToken;
if (typeBuilder.ModuleBuilder.IsSaved)
{
pseudoTokenOrIndex = typeBuilder.ModuleBuilder.ResolvePseudoToken(pseudoToken) & 0xFFFFFF;
}
foreach (int i in this.Module.FieldLayout.Filter(pseudoTokenOrIndex))
{
offset = this.Module.FieldLayout.records[i].Offset;
return true;
}
offset = 0;
return false;
}
public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
{
SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
}
public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
{
Universe u = this.Module.universe;
if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_FieldOffsetAttribute)
{
customBuilder = customBuilder.DecodeBlob(this.Module.Assembly);
SetOffset((int)customBuilder.GetConstructorArgument(0));
}
else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_MarshalAsAttribute)
{
FieldMarshal.SetMarshalAsAttribute(typeBuilder.ModuleBuilder, pseudoToken, customBuilder);
attribs |= FieldAttributes.HasFieldMarshal;
}
else if (customBuilder.Constructor.DeclaringType == u.System_NonSerializedAttribute)
{
attribs |= FieldAttributes.NotSerialized;
}
else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute)
{
attribs |= FieldAttributes.SpecialName;
}
else
{
typeBuilder.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder);
}
}
public void SetOffset(int iOffset)
{
FieldLayoutTable.Record rec = new FieldLayoutTable.Record();
rec.Offset = iOffset;
rec.Field = pseudoToken;
typeBuilder.ModuleBuilder.FieldLayout.AddRecord(rec);
}
public override FieldAttributes Attributes
{
get { return attribs; }
}
public override Type DeclaringType
{
get { return typeBuilder.IsModulePseudoType ? null : typeBuilder; }
}
public override string Name
{
get { return name; }
}
public override int MetadataToken
{
get { return pseudoToken; }
}
public override Module Module
{
get { return typeBuilder.Module; }
}
public FieldToken GetToken()
{
return new FieldToken(pseudoToken);
}
internal void WriteFieldRecords(MetadataWriter mw)
{
mw.Write((short)attribs);
mw.WriteStringIndex(nameIndex);
mw.WriteBlobIndex(signature);
}
internal void FixupToken(int token)
{
typeBuilder.ModuleBuilder.RegisterTokenFixup(this.pseudoToken, token);
}
internal override FieldSignature FieldSignature
{
get { return fieldSig; }
}
internal override int ImportTo(ModuleBuilder other)
{
return other.ImportMethodOrField(typeBuilder, name, fieldSig);
}
internal override int GetCurrentToken()
{
if (typeBuilder.ModuleBuilder.IsSaved)
{
return typeBuilder.ModuleBuilder.ResolvePseudoToken(pseudoToken);
}
else
{
return pseudoToken;
}
}
internal override bool IsBaked
{
get { return typeBuilder.IsBaked; }
}
}
}

1138
external/ikvm/reflect/Emit/ILGenerator.cs vendored Normal file

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More