2 Copyright (C) 2009-2011 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Collections.Generic;
28 using IKVM.Reflection.Emit;
29 using IKVM.Reflection.Writer;
30 using IKVM.Reflection.Reader;
32 namespace IKVM.Reflection
34 sealed class PropertySignature : Signature
36 private CallingConventions callingConvention;
37 private readonly Type propertyType;
38 private readonly Type[] parameterTypes;
39 private readonly PackedCustomModifiers customModifiers;
41 internal static PropertySignature Create(CallingConventions callingConvention, Type propertyType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
43 return new PropertySignature(callingConvention, propertyType, Util.Copy(parameterTypes), customModifiers);
46 private PropertySignature(CallingConventions callingConvention, Type propertyType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
48 this.callingConvention = callingConvention;
49 this.propertyType = propertyType;
50 this.parameterTypes = parameterTypes;
51 this.customModifiers = customModifiers;
54 public override bool Equals(object obj)
56 PropertySignature other = obj as PropertySignature;
58 && other.propertyType.Equals(propertyType)
59 && other.customModifiers.Equals(customModifiers);
62 public override int GetHashCode()
64 return propertyType.GetHashCode() ^ customModifiers.GetHashCode();
67 internal int ParameterCount
69 get { return parameterTypes.Length; }
78 callingConvention |= CallingConventions.HasThis;
82 callingConvention &= ~CallingConventions.HasThis;
87 internal Type PropertyType
89 get { return propertyType; }
92 internal CustomModifiers GetCustomModifiers()
94 return customModifiers.GetReturnTypeCustomModifiers();
97 internal PropertySignature ExpandTypeParameters(Type declaringType)
99 return new PropertySignature(
101 propertyType.BindTypeParameters(declaringType),
102 BindTypeParameters(declaringType, parameterTypes),
103 customModifiers.Bind(declaringType));
106 internal override void WriteSig(ModuleBuilder module, ByteBuffer bb)
108 byte flags = PROPERTY;
109 if ((callingConvention & CallingConventions.HasThis) != 0)
113 if ((callingConvention & CallingConventions.ExplicitThis) != 0)
115 flags |= EXPLICITTHIS;
117 if ((callingConvention & CallingConventions.VarArgs) != 0)
122 bb.WriteCompressedInt(parameterTypes == null ? 0 : parameterTypes.Length);
123 WriteCustomModifiers(module, bb, customModifiers.GetReturnTypeCustomModifiers());
124 WriteType(module, bb, propertyType);
125 if (parameterTypes != null)
127 for (int i = 0; i < parameterTypes.Length; i++)
129 WriteCustomModifiers(module, bb, customModifiers.GetParameterCustomModifiers(i));
130 WriteType(module, bb, parameterTypes[i]);
135 internal Type GetParameter(int parameter)
137 return parameterTypes[parameter];
140 internal CustomModifiers GetParameterCustomModifiers(int parameter)
142 return customModifiers.GetParameterCustomModifiers(parameter);
145 internal CallingConventions CallingConvention
147 get { return callingConvention; }
150 internal bool MatchParameterTypes(Type[] types)
152 return Util.ArrayEquals(types, parameterTypes);
155 internal static PropertySignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context)
157 byte flags = br.ReadByte();
158 if ((flags & PROPERTY) == 0)
160 throw new BadImageFormatException();
162 CallingConventions callingConvention = CallingConventions.Standard;
163 if ((flags & HASTHIS) != 0)
165 callingConvention |= CallingConventions.HasThis;
167 if ((flags & EXPLICITTHIS) != 0)
169 callingConvention |= CallingConventions.ExplicitThis;
172 Type[] parameterTypes;
173 int paramCount = br.ReadCompressedInt();
174 CustomModifiers[] mods = null;
175 PackedCustomModifiers.Pack(ref mods, 0, CustomModifiers.Read(module, br, context), paramCount + 1);
176 returnType = ReadRetType(module, br, context);
177 parameterTypes = new Type[paramCount];
178 for (int i = 0; i < parameterTypes.Length; i++)
180 PackedCustomModifiers.Pack(ref mods, i + 1, CustomModifiers.Read(module, br, context), paramCount + 1);
181 parameterTypes[i] = ReadParam(module, br, context);
183 return new PropertySignature(callingConvention, returnType, parameterTypes, PackedCustomModifiers.Wrap(mods));