1 | // |
---|
2 | // field.cs: All field handlers |
---|
3 | // |
---|
4 | // Authors: Miguel de Icaza (miguel@gnu.org) |
---|
5 | // Martin Baulig (martin@ximian.com) |
---|
6 | // Marek Safar (marek.safar@seznam.cz) |
---|
7 | // |
---|
8 | // Dual licensed under the terms of the MIT X11 or GNU GPL |
---|
9 | // |
---|
10 | // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com) |
---|
11 | // Copyright 2004-2008 Novell, Inc |
---|
12 | // Copyright 2011 Xamarin Inc |
---|
13 | // |
---|
14 | |
---|
15 | using System; |
---|
16 | using System.Collections.Generic; |
---|
17 | using System.Runtime.InteropServices; |
---|
18 | |
---|
19 | #if STATIC |
---|
20 | using MetaType = IKVM.Reflection.Type; |
---|
21 | using IKVM.Reflection; |
---|
22 | using IKVM.Reflection.Emit; |
---|
23 | #else |
---|
24 | using MetaType = System.Type; |
---|
25 | using System.Reflection; |
---|
26 | using System.Reflection.Emit; |
---|
27 | #endif |
---|
28 | |
---|
29 | namespace Mono.CSharp |
---|
30 | { |
---|
31 | public class FieldDeclarator |
---|
32 | { |
---|
33 | public FieldDeclarator (SimpleMemberName name, Expression initializer) |
---|
34 | { |
---|
35 | this.Name = name; |
---|
36 | this.Initializer = initializer; |
---|
37 | } |
---|
38 | |
---|
39 | #region Properties |
---|
40 | |
---|
41 | public SimpleMemberName Name { get; private set; } |
---|
42 | public Expression Initializer { get; private set; } |
---|
43 | |
---|
44 | #endregion |
---|
45 | |
---|
46 | public virtual FullNamedExpression GetFieldTypeExpression (FieldBase field) |
---|
47 | { |
---|
48 | return new TypeExpression (field.MemberType, Name.Location); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | // |
---|
53 | // Abstract class for all fields |
---|
54 | // |
---|
55 | abstract public class FieldBase : MemberBase |
---|
56 | { |
---|
57 | protected FieldBuilder FieldBuilder; |
---|
58 | protected FieldSpec spec; |
---|
59 | public Status status; |
---|
60 | protected Expression initializer; |
---|
61 | protected List<FieldDeclarator> declarators; |
---|
62 | |
---|
63 | [Flags] |
---|
64 | public enum Status : byte { |
---|
65 | HAS_OFFSET = 4 // Used by FieldMember. |
---|
66 | } |
---|
67 | |
---|
68 | static readonly string[] attribute_targets = new string [] { "field" }; |
---|
69 | |
---|
70 | protected FieldBase (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name, Attributes attrs) |
---|
71 | : base (parent, type, mod, allowed_mod | Modifiers.ABSTRACT, Modifiers.PRIVATE, name, attrs) |
---|
72 | { |
---|
73 | if ((mod & Modifiers.ABSTRACT) != 0) |
---|
74 | Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead"); |
---|
75 | } |
---|
76 | |
---|
77 | #region Properties |
---|
78 | |
---|
79 | public override AttributeTargets AttributeTargets { |
---|
80 | get { |
---|
81 | return AttributeTargets.Field; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | public Expression Initializer { |
---|
86 | get { |
---|
87 | return initializer; |
---|
88 | } |
---|
89 | set { |
---|
90 | this.initializer = value; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | public string Name { |
---|
95 | get { |
---|
96 | return MemberName.Name; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | public FieldSpec Spec { |
---|
101 | get { |
---|
102 | return spec; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | public override string[] ValidAttributeTargets { |
---|
107 | get { |
---|
108 | return attribute_targets; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | public List<FieldDeclarator> Declarators { |
---|
113 | get { |
---|
114 | return this.declarators; |
---|
115 | } |
---|
116 | } |
---|
117 | #endregion |
---|
118 | |
---|
119 | public void AddDeclarator (FieldDeclarator declarator) |
---|
120 | { |
---|
121 | if (declarators == null) |
---|
122 | declarators = new List<FieldDeclarator> (2); |
---|
123 | |
---|
124 | declarators.Add (declarator); |
---|
125 | |
---|
126 | Parent.AddNameToContainer (this, declarator.Name.Value); |
---|
127 | } |
---|
128 | |
---|
129 | public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa) |
---|
130 | { |
---|
131 | if (a.Type == pa.FieldOffset) { |
---|
132 | status |= Status.HAS_OFFSET; |
---|
133 | |
---|
134 | if (!Parent.PartialContainer.HasExplicitLayout) { |
---|
135 | Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)"); |
---|
136 | return; |
---|
137 | } |
---|
138 | |
---|
139 | if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) { |
---|
140 | Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields"); |
---|
141 | return; |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | if (a.Type == pa.FixedBuffer) { |
---|
146 | Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead"); |
---|
147 | return; |
---|
148 | } |
---|
149 | |
---|
150 | #if false |
---|
151 | if (a.Type == pa.MarshalAs) { |
---|
152 | UnmanagedMarshal marshal = a.GetMarshal (this); |
---|
153 | if (marshal != null) { |
---|
154 | FieldBuilder.SetMarshal (marshal); |
---|
155 | } |
---|
156 | return; |
---|
157 | } |
---|
158 | #endif |
---|
159 | if ((a.HasSecurityAttribute)) { |
---|
160 | a.Error_InvalidSecurityParent (); |
---|
161 | return; |
---|
162 | } |
---|
163 | |
---|
164 | if (a.Type == pa.Dynamic) { |
---|
165 | a.Error_MisusedDynamicAttribute (); |
---|
166 | return; |
---|
167 | } |
---|
168 | |
---|
169 | FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata); |
---|
170 | } |
---|
171 | |
---|
172 | public void SetCustomAttribute (MethodSpec ctor, byte[] data) |
---|
173 | { |
---|
174 | FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), data); |
---|
175 | } |
---|
176 | |
---|
177 | protected override bool CheckBase () |
---|
178 | { |
---|
179 | if (!base.CheckBase ()) |
---|
180 | return false; |
---|
181 | |
---|
182 | MemberSpec candidate; |
---|
183 | bool overrides = false; |
---|
184 | var conflict_symbol = MemberCache.FindBaseMember (this, out candidate, ref overrides); |
---|
185 | if (conflict_symbol == null) |
---|
186 | conflict_symbol = candidate; |
---|
187 | |
---|
188 | if (conflict_symbol == null) { |
---|
189 | if ((ModFlags & Modifiers.NEW) != 0) { |
---|
190 | Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", |
---|
191 | GetSignatureForError ()); |
---|
192 | } |
---|
193 | } else { |
---|
194 | if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0) { |
---|
195 | Report.SymbolRelatedToPreviousError (conflict_symbol); |
---|
196 | Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended", |
---|
197 | GetSignatureForError (), conflict_symbol.GetSignatureForError ()); |
---|
198 | } |
---|
199 | |
---|
200 | if (conflict_symbol.IsAbstract) { |
---|
201 | Report.SymbolRelatedToPreviousError (conflict_symbol); |
---|
202 | Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'", |
---|
203 | GetSignatureForError (), conflict_symbol.GetSignatureForError ()); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | return true; |
---|
208 | } |
---|
209 | |
---|
210 | public virtual Constant ConvertInitializer (ResolveContext rc, Constant expr) |
---|
211 | { |
---|
212 | return expr.ConvertImplicitly (MemberType); |
---|
213 | } |
---|
214 | |
---|
215 | protected override void DoMemberTypeDependentChecks () |
---|
216 | { |
---|
217 | base.DoMemberTypeDependentChecks (); |
---|
218 | |
---|
219 | if (MemberType.IsGenericParameter) |
---|
220 | return; |
---|
221 | |
---|
222 | if (MemberType.IsStatic) |
---|
223 | Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType, Report); |
---|
224 | |
---|
225 | if (!IsCompilerGenerated) |
---|
226 | CheckBase (); |
---|
227 | |
---|
228 | IsTypePermitted (); |
---|
229 | } |
---|
230 | |
---|
231 | // |
---|
232 | // Represents header string for documentation comment. |
---|
233 | // |
---|
234 | public override string DocCommentHeader { |
---|
235 | get { return "F:"; } |
---|
236 | } |
---|
237 | |
---|
238 | public override void Emit () |
---|
239 | { |
---|
240 | if (member_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) { |
---|
241 | Module.PredefinedAttributes.Dynamic.EmitAttribute (FieldBuilder); |
---|
242 | } else if (!Parent.IsCompilerGenerated && member_type.HasDynamicElement) { |
---|
243 | Module.PredefinedAttributes.Dynamic.EmitAttribute (FieldBuilder, member_type, Location); |
---|
244 | } |
---|
245 | |
---|
246 | if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated) |
---|
247 | Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (FieldBuilder); |
---|
248 | if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0) |
---|
249 | Module.PredefinedAttributes.DebuggerBrowsable.EmitAttribute (FieldBuilder, System.Diagnostics.DebuggerBrowsableState.Never); |
---|
250 | |
---|
251 | if (OptAttributes != null) { |
---|
252 | OptAttributes.Emit (); |
---|
253 | } |
---|
254 | |
---|
255 | if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & (Modifiers.STATIC | Modifiers.BACKING_FIELD)) == 0 && Parent.PartialContainer.HasExplicitLayout) { |
---|
256 | Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute", GetSignatureForError ()); |
---|
257 | } |
---|
258 | |
---|
259 | ConstraintChecker.Check (this, member_type, type_expr.Location); |
---|
260 | |
---|
261 | base.Emit (); |
---|
262 | } |
---|
263 | |
---|
264 | public static void Error_VariableOfStaticClass (Location loc, string variable_name, TypeSpec static_class, Report Report) |
---|
265 | { |
---|
266 | Report.SymbolRelatedToPreviousError (static_class); |
---|
267 | Report.Error (723, loc, "`{0}': cannot declare variables of static types", |
---|
268 | variable_name); |
---|
269 | } |
---|
270 | |
---|
271 | protected override bool VerifyClsCompliance () |
---|
272 | { |
---|
273 | if (!base.VerifyClsCompliance ()) |
---|
274 | return false; |
---|
275 | |
---|
276 | if (!MemberType.IsCLSCompliant () || this is FixedField) { |
---|
277 | Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant", |
---|
278 | GetSignatureForError ()); |
---|
279 | } |
---|
280 | return true; |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | // |
---|
285 | // Field specification |
---|
286 | // |
---|
287 | public class FieldSpec : MemberSpec, IInterfaceMemberSpec |
---|
288 | { |
---|
289 | FieldInfo metaInfo; |
---|
290 | TypeSpec memberType; |
---|
291 | |
---|
292 | public FieldSpec (TypeSpec declaringType, IMemberDefinition definition, TypeSpec memberType, FieldInfo info, Modifiers modifiers) |
---|
293 | : base (MemberKind.Field, declaringType, definition, modifiers) |
---|
294 | { |
---|
295 | this.metaInfo = info; |
---|
296 | this.memberType = memberType; |
---|
297 | } |
---|
298 | |
---|
299 | #region Properties |
---|
300 | |
---|
301 | public bool IsReadOnly { |
---|
302 | get { |
---|
303 | return (Modifiers & Modifiers.READONLY) != 0; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | public TypeSpec MemberType { |
---|
308 | get { |
---|
309 | return memberType; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | #endregion |
---|
314 | |
---|
315 | public FieldInfo GetMetaInfo () |
---|
316 | { |
---|
317 | if ((state & StateFlags.PendingMetaInflate) != 0) { |
---|
318 | var decl_meta = DeclaringType.GetMetaInfo (); |
---|
319 | if (DeclaringType.IsTypeBuilder) { |
---|
320 | metaInfo = TypeBuilder.GetField (decl_meta, metaInfo); |
---|
321 | } else { |
---|
322 | var orig_token = metaInfo.MetadataToken; |
---|
323 | metaInfo = decl_meta.GetField (Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); |
---|
324 | if (metaInfo.MetadataToken != orig_token) |
---|
325 | throw new NotImplementedException ("Resolved to wrong meta token"); |
---|
326 | |
---|
327 | // What a stupid API, does not work because field handle is imported |
---|
328 | // metaInfo = FieldInfo.GetFieldFromHandle (metaInfo.FieldHandle, DeclaringType.MetaInfo.TypeHandle); |
---|
329 | } |
---|
330 | |
---|
331 | state &= ~StateFlags.PendingMetaInflate; |
---|
332 | } |
---|
333 | |
---|
334 | return metaInfo; |
---|
335 | } |
---|
336 | |
---|
337 | public override MemberSpec InflateMember (TypeParameterInflator inflator) |
---|
338 | { |
---|
339 | var fs = (FieldSpec) base.InflateMember (inflator); |
---|
340 | fs.memberType = inflator.Inflate (memberType); |
---|
341 | return fs; |
---|
342 | } |
---|
343 | |
---|
344 | public FieldSpec Mutate (TypeParameterMutator mutator) |
---|
345 | { |
---|
346 | var decl = DeclaringType; |
---|
347 | if (DeclaringType.IsGenericOrParentIsGeneric) |
---|
348 | decl = mutator.Mutate (decl); |
---|
349 | |
---|
350 | if (decl == DeclaringType) |
---|
351 | return this; |
---|
352 | |
---|
353 | var fs = (FieldSpec) MemberwiseClone (); |
---|
354 | fs.declaringType = decl; |
---|
355 | fs.state |= StateFlags.PendingMetaInflate; |
---|
356 | |
---|
357 | // Gets back FieldInfo in case of metaInfo was inflated |
---|
358 | fs.metaInfo = MemberCache.GetMember (TypeParameterMutator.GetMemberDeclaringType (DeclaringType), this).metaInfo; |
---|
359 | return fs; |
---|
360 | } |
---|
361 | |
---|
362 | public override List<MissingTypeSpecReference> ResolveMissingDependencies (MemberSpec caller) |
---|
363 | { |
---|
364 | return memberType.ResolveMissingDependencies (this); |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | /// <summary> |
---|
369 | /// Fixed buffer implementation |
---|
370 | /// </summary> |
---|
371 | public class FixedField : FieldBase |
---|
372 | { |
---|
373 | public const string FixedElementName = "FixedElementField"; |
---|
374 | static int GlobalCounter; |
---|
375 | |
---|
376 | TypeBuilder fixed_buffer_type; |
---|
377 | |
---|
378 | const Modifiers AllowedModifiers = |
---|
379 | Modifiers.NEW | |
---|
380 | Modifiers.PUBLIC | |
---|
381 | Modifiers.PROTECTED | |
---|
382 | Modifiers.INTERNAL | |
---|
383 | Modifiers.PRIVATE | |
---|
384 | Modifiers.UNSAFE; |
---|
385 | |
---|
386 | public FixedField (TypeDefinition parent, FullNamedExpression type, Modifiers mod, MemberName name, Attributes attrs) |
---|
387 | : base (parent, type, mod, AllowedModifiers, name, attrs) |
---|
388 | { |
---|
389 | } |
---|
390 | |
---|
391 | #region Properties |
---|
392 | |
---|
393 | // |
---|
394 | // Explicit struct layout set by parent |
---|
395 | // |
---|
396 | public CharSet? CharSet { |
---|
397 | get; set; |
---|
398 | } |
---|
399 | |
---|
400 | #endregion |
---|
401 | |
---|
402 | public override Constant ConvertInitializer (ResolveContext rc, Constant expr) |
---|
403 | { |
---|
404 | return expr.ImplicitConversionRequired (rc, rc.BuiltinTypes.Int); |
---|
405 | } |
---|
406 | |
---|
407 | public override bool Define () |
---|
408 | { |
---|
409 | if (!base.Define ()) |
---|
410 | return false; |
---|
411 | |
---|
412 | if (!BuiltinTypeSpec.IsPrimitiveType (MemberType)) { |
---|
413 | Report.Error (1663, Location, |
---|
414 | "`{0}': Fixed size buffers type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double", |
---|
415 | GetSignatureForError ()); |
---|
416 | } else if (declarators != null) { |
---|
417 | foreach (var d in declarators) { |
---|
418 | var f = new FixedField (Parent, d.GetFieldTypeExpression (this), ModFlags, new MemberName (d.Name.Value, d.Name.Location), OptAttributes); |
---|
419 | f.initializer = d.Initializer; |
---|
420 | ((ConstInitializer) f.initializer).Name = d.Name.Value; |
---|
421 | f.Define (); |
---|
422 | Parent.PartialContainer.Members.Add (f); |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | // Create nested fixed buffer container |
---|
427 | string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++); |
---|
428 | fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, |
---|
429 | TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, |
---|
430 | Compiler.BuiltinTypes.ValueType.GetMetaInfo ()); |
---|
431 | |
---|
432 | var ffield = fixed_buffer_type.DefineField (FixedElementName, MemberType.GetMetaInfo (), FieldAttributes.Public); |
---|
433 | |
---|
434 | FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, ModifiersExtensions.FieldAttr (ModFlags)); |
---|
435 | |
---|
436 | var element_spec = new FieldSpec (null, this, MemberType, ffield, ModFlags); |
---|
437 | spec = new FixedFieldSpec (Module, Parent.Definition, this, FieldBuilder, element_spec, ModFlags); |
---|
438 | |
---|
439 | Parent.MemberCache.AddMember (spec); |
---|
440 | return true; |
---|
441 | } |
---|
442 | |
---|
443 | protected override void DoMemberTypeIndependentChecks () |
---|
444 | { |
---|
445 | base.DoMemberTypeIndependentChecks (); |
---|
446 | |
---|
447 | if (!IsUnsafe) |
---|
448 | Expression.UnsafeError (Report, Location); |
---|
449 | |
---|
450 | if (Parent.PartialContainer.Kind != MemberKind.Struct) { |
---|
451 | Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs", |
---|
452 | GetSignatureForError ()); |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | public override void Accept (StructuralVisitor visitor) |
---|
457 | { |
---|
458 | visitor.Visit (this); |
---|
459 | } |
---|
460 | |
---|
461 | public override void Emit() |
---|
462 | { |
---|
463 | ResolveContext rc = new ResolveContext (this); |
---|
464 | IntConstant buffer_size_const = initializer.Resolve (rc) as IntConstant; |
---|
465 | if (buffer_size_const == null) |
---|
466 | return; |
---|
467 | |
---|
468 | int buffer_size = buffer_size_const.Value; |
---|
469 | |
---|
470 | if (buffer_size <= 0) { |
---|
471 | Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ()); |
---|
472 | return; |
---|
473 | } |
---|
474 | |
---|
475 | EmitFieldSize (buffer_size); |
---|
476 | |
---|
477 | #if STATIC |
---|
478 | if (Module.HasDefaultCharSet) |
---|
479 | fixed_buffer_type.__SetAttributes (fixed_buffer_type.Attributes | Module.DefaultCharSetType); |
---|
480 | #endif |
---|
481 | |
---|
482 | Module.PredefinedAttributes.UnsafeValueType.EmitAttribute (fixed_buffer_type); |
---|
483 | Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (fixed_buffer_type); |
---|
484 | fixed_buffer_type.CreateType (); |
---|
485 | |
---|
486 | base.Emit (); |
---|
487 | } |
---|
488 | |
---|
489 | void EmitFieldSize (int buffer_size) |
---|
490 | { |
---|
491 | int type_size = BuiltinTypeSpec.GetSize (MemberType); |
---|
492 | |
---|
493 | if (buffer_size > int.MaxValue / type_size) { |
---|
494 | Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit", |
---|
495 | GetSignatureForError (), buffer_size.ToString (), MemberType.GetSignatureForError ()); |
---|
496 | return; |
---|
497 | } |
---|
498 | |
---|
499 | AttributeEncoder encoder; |
---|
500 | |
---|
501 | var ctor = Module.PredefinedMembers.StructLayoutAttributeCtor.Resolve (Location); |
---|
502 | if (ctor == null) |
---|
503 | return; |
---|
504 | |
---|
505 | var field_size = Module.PredefinedMembers.StructLayoutSize.Resolve (Location); |
---|
506 | var field_charset = Module.PredefinedMembers.StructLayoutCharSet.Resolve (Location); |
---|
507 | if (field_size == null || field_charset == null) |
---|
508 | return; |
---|
509 | |
---|
510 | var char_set = CharSet ?? Module.DefaultCharSet ?? 0; |
---|
511 | |
---|
512 | encoder = new AttributeEncoder (); |
---|
513 | encoder.Encode ((short)LayoutKind.Sequential); |
---|
514 | encoder.EncodeNamedArguments ( |
---|
515 | new [] { field_size, field_charset }, |
---|
516 | new Constant [] { |
---|
517 | new IntConstant (Compiler.BuiltinTypes, buffer_size * type_size, Location), |
---|
518 | new IntConstant (Compiler.BuiltinTypes, (int) char_set, Location) |
---|
519 | } |
---|
520 | ); |
---|
521 | |
---|
522 | fixed_buffer_type.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ()); |
---|
523 | |
---|
524 | // |
---|
525 | // Don't emit FixedBufferAttribute attribute for private types |
---|
526 | // |
---|
527 | if ((ModFlags & Modifiers.PRIVATE) != 0) |
---|
528 | return; |
---|
529 | |
---|
530 | ctor = Module.PredefinedMembers.FixedBufferAttributeCtor.Resolve (Location); |
---|
531 | if (ctor == null) |
---|
532 | return; |
---|
533 | |
---|
534 | encoder = new AttributeEncoder (); |
---|
535 | encoder.EncodeTypeName (MemberType); |
---|
536 | encoder.Encode (buffer_size); |
---|
537 | encoder.EncodeEmptyNamedArguments (); |
---|
538 | |
---|
539 | FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ()); |
---|
540 | } |
---|
541 | } |
---|
542 | |
---|
543 | class FixedFieldSpec : FieldSpec |
---|
544 | { |
---|
545 | readonly FieldSpec element; |
---|
546 | |
---|
547 | public FixedFieldSpec (ModuleContainer module, TypeSpec declaringType, IMemberDefinition definition, FieldInfo info, FieldSpec element, Modifiers modifiers) |
---|
548 | : base (declaringType, definition, PointerContainer.MakeType (module, element.MemberType), info, modifiers) |
---|
549 | { |
---|
550 | this.element = element; |
---|
551 | |
---|
552 | // It's never CLS-Compliant |
---|
553 | state &= ~StateFlags.CLSCompliant_Undetected; |
---|
554 | } |
---|
555 | |
---|
556 | public FieldSpec Element { |
---|
557 | get { |
---|
558 | return element; |
---|
559 | } |
---|
560 | } |
---|
561 | |
---|
562 | public TypeSpec ElementType { |
---|
563 | get { |
---|
564 | return element.MemberType; |
---|
565 | } |
---|
566 | } |
---|
567 | } |
---|
568 | |
---|
569 | // |
---|
570 | // The Field class is used to represents class/struct fields during parsing. |
---|
571 | // |
---|
572 | public class Field : FieldBase { |
---|
573 | // <summary> |
---|
574 | // Modifiers allowed in a class declaration |
---|
575 | // </summary> |
---|
576 | const Modifiers AllowedModifiers = |
---|
577 | Modifiers.NEW | |
---|
578 | Modifiers.PUBLIC | |
---|
579 | Modifiers.PROTECTED | |
---|
580 | Modifiers.INTERNAL | |
---|
581 | Modifiers.PRIVATE | |
---|
582 | Modifiers.STATIC | |
---|
583 | Modifiers.VOLATILE | |
---|
584 | Modifiers.UNSAFE | |
---|
585 | Modifiers.READONLY; |
---|
586 | |
---|
587 | public Field (TypeDefinition parent, FullNamedExpression type, Modifiers mod, MemberName name, Attributes attrs) |
---|
588 | : base (parent, type, mod, AllowedModifiers, name, attrs) |
---|
589 | { |
---|
590 | } |
---|
591 | |
---|
592 | bool CanBeVolatile () |
---|
593 | { |
---|
594 | switch (MemberType.BuiltinType) { |
---|
595 | case BuiltinTypeSpec.Type.Bool: |
---|
596 | case BuiltinTypeSpec.Type.Char: |
---|
597 | case BuiltinTypeSpec.Type.SByte: |
---|
598 | case BuiltinTypeSpec.Type.Byte: |
---|
599 | case BuiltinTypeSpec.Type.Short: |
---|
600 | case BuiltinTypeSpec.Type.UShort: |
---|
601 | case BuiltinTypeSpec.Type.Int: |
---|
602 | case BuiltinTypeSpec.Type.UInt: |
---|
603 | case BuiltinTypeSpec.Type.Float: |
---|
604 | case BuiltinTypeSpec.Type.UIntPtr: |
---|
605 | case BuiltinTypeSpec.Type.IntPtr: |
---|
606 | return true; |
---|
607 | } |
---|
608 | |
---|
609 | if (TypeSpec.IsReferenceType (MemberType)) |
---|
610 | return true; |
---|
611 | |
---|
612 | if (MemberType.IsEnum) |
---|
613 | return true; |
---|
614 | |
---|
615 | return false; |
---|
616 | } |
---|
617 | |
---|
618 | public override void Accept (StructuralVisitor visitor) |
---|
619 | { |
---|
620 | visitor.Visit (this); |
---|
621 | } |
---|
622 | |
---|
623 | public override bool Define () |
---|
624 | { |
---|
625 | if (!base.Define ()) |
---|
626 | return false; |
---|
627 | |
---|
628 | MetaType[] required_modifier = null; |
---|
629 | if ((ModFlags & Modifiers.VOLATILE) != 0) { |
---|
630 | var mod = Module.PredefinedTypes.IsVolatile.Resolve (); |
---|
631 | if (mod != null) |
---|
632 | required_modifier = new MetaType[] { mod.GetMetaInfo () }; |
---|
633 | } |
---|
634 | |
---|
635 | FieldBuilder = Parent.TypeBuilder.DefineField ( |
---|
636 | Name, member_type.GetMetaInfo (), required_modifier, null, ModifiersExtensions.FieldAttr (ModFlags)); |
---|
637 | |
---|
638 | spec = new FieldSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags); |
---|
639 | |
---|
640 | // |
---|
641 | // Don't cache inaccessible fields except for struct where we |
---|
642 | // need them for definitive assignment checks |
---|
643 | // |
---|
644 | if ((ModFlags & Modifiers.BACKING_FIELD) == 0 || Parent.Kind == MemberKind.Struct) { |
---|
645 | Parent.MemberCache.AddMember (spec); |
---|
646 | } |
---|
647 | |
---|
648 | if (initializer != null) { |
---|
649 | Parent.RegisterFieldForInitialization (this, new FieldInitializer (this, initializer, TypeExpression.Location)); |
---|
650 | } |
---|
651 | |
---|
652 | if (declarators != null) { |
---|
653 | foreach (var d in declarators) { |
---|
654 | var f = new Field (Parent, d.GetFieldTypeExpression (this), ModFlags, new MemberName (d.Name.Value, d.Name.Location), OptAttributes); |
---|
655 | if (d.Initializer != null) |
---|
656 | f.initializer = d.Initializer; |
---|
657 | |
---|
658 | f.Define (); |
---|
659 | Parent.PartialContainer.Members.Add (f); |
---|
660 | } |
---|
661 | } |
---|
662 | |
---|
663 | return true; |
---|
664 | } |
---|
665 | |
---|
666 | protected override void DoMemberTypeDependentChecks () |
---|
667 | { |
---|
668 | if ((ModFlags & Modifiers.BACKING_FIELD) != 0) |
---|
669 | return; |
---|
670 | |
---|
671 | base.DoMemberTypeDependentChecks (); |
---|
672 | |
---|
673 | if ((ModFlags & Modifiers.VOLATILE) != 0) { |
---|
674 | if (!CanBeVolatile ()) { |
---|
675 | Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'", |
---|
676 | GetSignatureForError (), MemberType.GetSignatureForError ()); |
---|
677 | } |
---|
678 | |
---|
679 | if ((ModFlags & Modifiers.READONLY) != 0) { |
---|
680 | Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly", |
---|
681 | GetSignatureForError ()); |
---|
682 | } |
---|
683 | } |
---|
684 | } |
---|
685 | |
---|
686 | protected override bool VerifyClsCompliance () |
---|
687 | { |
---|
688 | if (!base.VerifyClsCompliance ()) |
---|
689 | return false; |
---|
690 | |
---|
691 | if ((ModFlags & Modifiers.VOLATILE) != 0) { |
---|
692 | Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ()); |
---|
693 | } |
---|
694 | |
---|
695 | return true; |
---|
696 | } |
---|
697 | } |
---|
698 | |
---|
699 | class PrimaryConstructorField : Field |
---|
700 | { |
---|
701 | // |
---|
702 | // Proxy resolved parameter type expression to avoid type double resolve |
---|
703 | // and problems with correct resolve context on partial classes |
---|
704 | // |
---|
705 | sealed class TypeExpressionFromParameter : TypeExpr |
---|
706 | { |
---|
707 | Parameter parameter; |
---|
708 | |
---|
709 | public TypeExpressionFromParameter (Parameter parameter) |
---|
710 | { |
---|
711 | this.parameter = parameter; |
---|
712 | eclass = ExprClass.Type; |
---|
713 | loc = parameter.Location; |
---|
714 | } |
---|
715 | |
---|
716 | public override TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments) |
---|
717 | { |
---|
718 | return parameter.Type; |
---|
719 | } |
---|
720 | } |
---|
721 | |
---|
722 | public PrimaryConstructorField (TypeDefinition parent, Parameter parameter) |
---|
723 | : base (parent, new TypeExpressionFromParameter (parameter), Modifiers.PRIVATE, new MemberName (parameter.Name, parameter.Location), null) |
---|
724 | { |
---|
725 | caching_flags |= Flags.IsUsed | Flags.IsAssigned; |
---|
726 | } |
---|
727 | |
---|
728 | public override string GetSignatureForError () |
---|
729 | { |
---|
730 | return MemberName.Name; |
---|
731 | } |
---|
732 | } |
---|
733 | } |
---|