[mcs] Initial by ref returns and variables support
[mono.git] / mcs / mcs / property.cs
index 35f4cd74a03fc3a148e3144c0a15f096740fbbfa..3478711470a0fa6d93b668621c5d60392e2fda08 100644 (file)
@@ -17,7 +17,7 @@ using System.Collections.Generic;
 using System.Text;
 using Mono.CompilerServices.SymbolWriter;
 
-#if NET_2_1
+#if MOBILE
 using XmlElement = System.Object;
 #endif
 
@@ -82,8 +82,6 @@ namespace Mono.CSharp
                        }
                }
 
-               public abstract void PrepareEmit ();
-
                protected override bool VerifyClsCompliance ()
                {
                        if (!base.VerifyClsCompliance ())
@@ -246,7 +244,7 @@ namespace Mono.CSharp
                        protected override void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
                        {
                                if (a.Target == AttributeTargets.Parameter) {
-                                       parameters[0].ApplyAttributeBuilder (a, ctor, cdata, pa);
+                                       parameters[parameters.Count - 1].ApplyAttributeBuilder (a, ctor, cdata, pa);
                                        return;
                                }
 
@@ -337,18 +335,22 @@ namespace Mono.CSharp
                                        ModFlags |= method.ModFlags;
                                        flags = method.flags;
                                } else {
-                                       if (container.Kind == MemberKind.Interface)
+                                       CheckModifiers (ModFlags);
+                                       ModFlags |= (method.ModFlags & (~Modifiers.AccessibilityMask));
+                                       ModFlags |= Modifiers.PROPERTY_CUSTOM;
+
+                                       if (container.Kind == MemberKind.Interface) {
                                                Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
                                                        GetSignatureForError ());
-                                       else if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
-                                               Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
+                                       } else if ((ModFlags & Modifiers.PRIVATE) != 0) {
+                                               if ((method.ModFlags & Modifiers.ABSTRACT) != 0) {
+                                                       Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
+                                               }
+
+                                               ModFlags &= ~Modifiers.VIRTUAL;
                                        }
 
-                                       CheckModifiers (ModFlags);
-                                       ModFlags |= (method.ModFlags & (~Modifiers.AccessibilityMask));
-                                       ModFlags |= Modifiers.PROPERTY_CUSTOM;
-                                       flags = ModifiersExtensions.MethodAttr (ModFlags);
-                                       flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
+                                       flags = ModifiersExtensions.MethodAttr (ModFlags) | MethodAttributes.SpecialName;
                                }
 
                                CheckAbstractAndExtern (block != null);
@@ -531,7 +533,12 @@ namespace Mono.CSharp
                                                ok = false;
                                        }
                                } else if (Get.HasCustomAccessModifier || base_prop.HasDifferentAccessibility) {
-                                       if (!CheckAccessModifiers (Get, base_prop.Get)) {
+                                       if (!base_prop.Get.IsAccessible (this)) {
+                                               // Same as csc but it should be different error code
+                                               Report.Error (115, Get.Location, "`{0}' is marked as an override but no accessible `get' accessor found to override",
+                                                       GetSignatureForError ());
+                                               ok = false;
+                                       } else if (!CheckAccessModifiers (Get, base_prop.Get)) {
                                                Error_CannotChangeAccessModifiers (Get, base_prop.Get);
                                                ok = false;
                                        }
@@ -539,13 +546,21 @@ namespace Mono.CSharp
                        }
 
                        if (Set == null) {
-                               if ((ModFlags & Modifiers.SEALED) != 0 && base_prop.HasSet && !base_prop.Set.IsAccessible (this)) {
-                                       // TODO: Should be different error code but csc uses for some reason same
-                                       Report.SymbolRelatedToPreviousError (base_prop);
-                                       Report.Error (546, Location,
-                                               "`{0}': cannot override because `{1}' does not have accessible set accessor",
-                                               GetSignatureForError (), base_prop.GetSignatureForError ());
-                                       ok = false;
+                               if (base_prop.HasSet) {
+                                       if ((ModFlags & Modifiers.SEALED) != 0 && !base_prop.Set.IsAccessible (this)) {
+                                               // TODO: Should be different error code but csc uses for some reason same
+                                               Report.SymbolRelatedToPreviousError (base_prop);
+                                               Report.Error (546, Location,
+                                                       "`{0}': cannot override because `{1}' does not have accessible set accessor",
+                                                       GetSignatureForError (), base_prop.GetSignatureForError ());
+                                               ok = false;
+                                       }
+
+                                       if ((ModFlags & Modifiers.AutoProperty) != 0) {
+                                               Report.Error (8080, Location, "`{0}': Auto-implemented properties must override all accessors of the overridden property",
+                                                       GetSignatureForError ());
+                                               ok = false;
+                                       }
                                }
                        } else {
                                if (!base_prop.HasSet) {
@@ -557,7 +572,12 @@ namespace Mono.CSharp
                                                ok = false;
                                        }
                                } else if (Set.HasCustomAccessModifier || base_prop.HasDifferentAccessibility) {
-                                       if (!CheckAccessModifiers (Set, base_prop.Set)) {
+                                       if (!base_prop.Set.IsAccessible (this)) {
+                                               // Same as csc but it should be different error code
+                                               Report.Error (115, Set.Location, "`{0}' is marked as an override but no accessible `set' accessor found to override",
+                                                       GetSignatureForError ());
+                                               ok = false;
+                                       } else if (!CheckAccessModifiers (Set, base_prop.Set)) {
                                                Error_CannotChangeAccessModifiers (Set, base_prop.Set);
                                                ok = false;
                                        }
@@ -597,8 +617,7 @@ namespace Mono.CSharp
                                                GetSignatureForError ());
                                }
                        } else if ((ModFlags & Modifiers.OVERRIDE) == 0 && 
-                               (Get == null && (Set.ModFlags & Modifiers.AccessibilityMask) != 0) ||
-                               (Set == null && (Get.ModFlags & Modifiers.AccessibilityMask) != 0)) {
+                               ((Get == null && (Set.ModFlags & Modifiers.AccessibilityMask) != 0) || (Set == null && (Get.ModFlags & Modifiers.AccessibilityMask) != 0))) {
                                Report.Error (276, Location, 
                                              "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
                                              GetSignatureForError ());
@@ -661,6 +680,10 @@ namespace Mono.CSharp
                                Module.PredefinedAttributes.Dynamic.EmitAttribute (PropertyBuilder, member_type, Location);
                        }
 
+                       if (member_type.HasNamedTupleElement) {
+                               Module.PredefinedAttributes.TupleElementNames.EmitAttribute (PropertyBuilder, member_type, Location);
+                       }
+
                        ConstraintChecker.Check (this, member_type, type_expr.Location);
 
                        first.Emit (Parent);
@@ -801,8 +824,10 @@ namespace Mono.CSharp
                        Parent.PartialContainer.Members.Add (BackingField);
 
                        FieldExpr fe = new FieldExpr (BackingField, Location);
-                       if ((BackingField.ModFlags & Modifiers.STATIC) == 0)
+                       if ((BackingField.ModFlags & Modifiers.STATIC) == 0) {
                                fe.InstanceExpression = new CompilerGeneratedThis (Parent.CurrentType, Location);
+                               Parent.PartialContainer.HasInstanceField = true;
+                       }
 
                        //
                        // Create get block but we careful with location to
@@ -847,12 +872,18 @@ namespace Mono.CSharp
                        }
 
                        if (auto) {
+                               ModFlags |= Modifiers.AutoProperty;
                                if (Get == null) {
                                        Report.Error (8051, Location, "Auto-implemented property `{0}' must have get accessor",
                                                GetSignatureForError ());
                                        return false;
                                }
 
+                               if (MemberType.Kind == MemberKind.ByRef) {
+                                       Report.Error (8145, Location, "Auto-implemented properties cannot return by reference");
+                                       return false;
+                               }
+
                                if (Compiler.Settings.Version < LanguageVersion.V_3 && Initializer == null)
                                        Report.FeatureIsNotAvailable (Compiler, Location, "auto-implemented properties");
 
@@ -881,7 +912,7 @@ namespace Mono.CSharp
 
                public override void Emit ()
                {
-                       if ((AccessorFirst.ModFlags & (Modifiers.STATIC | Modifiers.COMPILER_GENERATED)) == Modifiers.COMPILER_GENERATED && Parent.PartialContainer.HasExplicitLayout) {
+                       if ((AccessorFirst.ModFlags & (Modifiers.STATIC | Modifiers.AutoProperty)) == Modifiers.AutoProperty && Parent.PartialContainer.HasExplicitLayout) {
                                Report.Error (842, Location,
                                        "Automatically implemented property `{0}' cannot be used inside a type with an explicit StructLayout attribute",
                                        GetSignatureForError ());
@@ -1190,12 +1221,11 @@ namespace Mono.CSharp
 
                        backing_field = new Field (Parent,
                                new TypeExpression (MemberType, Location),
-                               Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
+                               Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.DEBUGGER_HIDDEN | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
                                MemberName, null);
 
                        Parent.PartialContainer.Members.Add (backing_field);
                        backing_field.Initializer = Initializer;
-                       backing_field.ModFlags &= ~Modifiers.COMPILER_GENERATED;
 
                        // Call define because we passed fields definition
                        backing_field.Define ();
@@ -1429,6 +1459,8 @@ namespace Mono.CSharp
 
                public override void PrepareEmit ()
                {
+                       base.PrepareEmit ();
+
                        add.PrepareEmit ();
                        remove.PrepareEmit ();
 
@@ -1736,6 +1768,12 @@ namespace Mono.CSharp
                        return base.GetSignatureForDocumentation () + parameters.GetSignatureForDocumentation ();
                }
 
+               public override void PrepareEmit ()
+               {
+                       base.PrepareEmit ();
+                       parameters.ResolveDefaultValues (this);
+               }
+
                protected override bool VerifyClsCompliance ()
                {
                        if (!base.VerifyClsCompliance ())
@@ -1764,6 +1802,18 @@ namespace Mono.CSharp
                }
                #endregion
 
+               public static ParametersImported CreateParametersFromSetter (MethodSpec setter, int set_param_count)
+               {
+                       //
+                       // Creates indexer parameters based on setter method parameters (the last parameter has to be removed)
+                       //
+                       var data = new IParameterData [set_param_count];
+                       var types = new TypeSpec [set_param_count];
+                       Array.Copy (setter.Parameters.FixedParameters, data, set_param_count);
+                       Array.Copy (setter.Parameters.Types, types, set_param_count);
+                       return new ParametersImported (data, types, setter.Parameters.HasParams);
+               }
+
                public override string GetSignatureForDocumentation ()
                {
                        return base.GetSignatureForDocumentation () + parameters.GetSignatureForDocumentation ();