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