2 // import.cs: System.Reflection conversions
4 // Authors: Marek Safar (marek.safar@gmail.com)
6 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 // Copyright 2009 Novell, Inc
12 using System.Reflection;
13 using System.Runtime.CompilerServices;
20 public static FieldSpec CreateField (FieldInfo fi)
22 // TODO MemberCache: remove
23 var cs = TypeManager.GetConstant (fi);
26 var fb = TypeManager.GetFieldCore (fi);
32 var fa = fi.Attributes;
33 switch (fa & FieldAttributes.FieldAccessMask) {
34 case FieldAttributes.Public:
35 mod = Modifiers.PUBLIC;
37 case FieldAttributes.Assembly:
38 mod = Modifiers.INTERNAL;
40 case FieldAttributes.Family:
41 mod = Modifiers.PROTECTED;
43 case FieldAttributes.FamORAssem:
44 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
47 mod = Modifiers.PRIVATE;
51 // TODO MemberCache: Remove completely and use only Imported
52 IMemberDefinition definition;
53 var gfd = TypeManager.GetGenericFieldDefinition (fi);
54 fb = TypeManager.GetFieldCore (gfd);
58 cs = TypeManager.GetConstant (gfd);
60 definition = cs.MemberDefinition;
62 definition = new ImportedMemberDefinition (fi);
65 if ((fa & FieldAttributes.Literal) != 0) {
67 if (gfd is System.Reflection.Emit.FieldBuilder) {
68 // TODO: Remove after MemberCache
69 c = TypeManager.GetConstant (gfd).Value;
71 c = Constant.CreateConstantFromValue (fi.FieldType, gfd.GetValue (gfd), Location.Null);
74 return new ConstSpec (definition, fi, mod, c);
77 if ((fa & FieldAttributes.InitOnly) != 0) {
78 if (fi.FieldType == TypeManager.decimal_type) {
79 var dc = ReadDecimalConstant (gfd);
81 return new ConstSpec (definition, fi, mod, dc);
84 mod |= Modifiers.READONLY;
87 if ((fa & FieldAttributes.Static) != 0)
88 mod |= Modifiers.STATIC;
90 if (!TypeManager.IsReferenceType (fi.FieldType)) {
91 PredefinedAttribute pa = PredefinedAttributes.Get.FixedBuffer;
93 if (gfd is System.Reflection.Emit.FieldBuilder) {
94 // TODO: Remove this after MemberCache fix
95 } else if (gfd.IsDefined (pa.Type, false)) {
96 var element_field = fi.FieldType.GetField (FixedField.FixedElementName);
97 return new FixedFieldSpec (definition, fi, element_field, mod);
104 return new FieldSpec (definition, fi, mod);
107 public static MethodSpec CreateMethod (MethodBase mb)
109 // TODO MemberCache: Remove
110 MethodCore mc = TypeManager.GetMethod (mb) as MethodCore;
115 var ma = mb.Attributes;
116 switch (ma & MethodAttributes.MemberAccessMask) {
117 case MethodAttributes.Public:
118 mod = Modifiers.PUBLIC;
120 case MethodAttributes.Assembly:
121 mod = Modifiers.INTERNAL;
123 case MethodAttributes.Family:
124 mod = Modifiers.PROTECTED;
126 case MethodAttributes.FamORAssem:
127 mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
130 mod = Modifiers.PRIVATE;
134 if ((ma & MethodAttributes.Static) != 0)
135 mod |= Modifiers.STATIC;
136 if ((ma & MethodAttributes.Virtual) != 0)
137 mod |= Modifiers.VIRTUAL;
138 if ((ma & MethodAttributes.Abstract) != 0)
139 mod |= Modifiers.ABSTRACT;
140 if ((ma & MethodAttributes.Final) != 0)
141 mod |= Modifiers.SEALED;
143 IMemberDefinition definition;
144 var gmd = mb as MethodInfo;
145 if (gmd != null && gmd.IsGenericMethodDefinition) {
146 definition = new ImportedGenericMethodDefinition (gmd);
147 } else if (mb.IsGenericMethod) { // TODO MemberCache: Remove me
148 definition = new ImportedGenericMethodDefinition ((MethodInfo) TypeManager.DropGenericMethodArguments (mb));
150 definition = new ImportedMemberDefinition (mb);
153 // TODO MemberCache: Use AParametersCollection p = ParametersImported.Create (mb);
154 AParametersCollection p = TypeManager.GetParameterData (mb);
155 MethodSpec ms = new MethodSpec (definition, mb, p, mod);
159 public static PropertySpec CreateProperty (PropertyInfo pi)
161 var definition = new ImportedMemberDefinition (pi);
162 var mod = Modifiers.PRIVATE; // TODO: modifiers
163 return new PropertySpec (definition, pi, mod);
167 // Decimal constants cannot be encoded in the constant blob, and thus are marked
168 // as IsInitOnly ('readonly' in C# parlance). We get its value from the
169 // DecimalConstantAttribute metadata.
171 static Constant ReadDecimalConstant (FieldInfo fi)
173 PredefinedAttribute pa = PredefinedAttributes.Get.DecimalConstant;
177 object[] attrs = fi.GetCustomAttributes (pa.Type, false);
178 if (attrs.Length != 1)
181 return new DecimalConstant (((DecimalConstantAttribute) attrs [0]).Value, Location.Null);
185 class ImportedMemberDefinition : IMemberDefinition
187 protected readonly ICustomAttributeProvider provider;
189 public ImportedMemberDefinition (ICustomAttributeProvider provider)
191 this.provider = provider;
194 public ObsoleteAttribute GetObsoleteAttribute ()
196 var res = provider.GetCustomAttributes (typeof (ObsoleteAttribute), false);
197 if (res == null || res.Length < 1)
200 return res [0] as ObsoleteAttribute;
203 public void SetIsUsed ()
205 // Unused for imported members
209 class ImportedGenericMethodDefinition : ImportedMemberDefinition, IGenericMethodDefinition
211 public ImportedGenericMethodDefinition (MethodInfo provider)
216 public MethodInfo MakeGenericMethod (Type[] targs)
218 return ((MethodInfo) provider).MakeGenericMethod (targs);