[mcs] Implements C# 7.2 readonly structs
[mono.git] / mcs / mcs / typespec.cs
index 6b42112f9ed1ae938c96aea045222e19f29405fd..d14e1ead3e317c07b55873ca2e679a7d6d3205a9 100644 (file)
@@ -92,6 +92,12 @@ namespace Mono.CSharp
                        }
                }
 
+               public bool HasNamedTupleElement {
+                       get {
+                               return (state & StateFlags.HasNamedTupleElement) != 0;
+                       }
+               }
+
                //
                // Returns a list of all interfaces including
                // interfaces from base type or base interfaces
@@ -109,7 +115,6 @@ namespace Mono.CSharp
                                        var imported = MemberDefinition as ImportedTypeDefinition;
                                        if (imported != null && Kind != MemberKind.MissingType)
                                                imported.DefineInterfaces (this);
-
                                }
 
                                return ifaces;
@@ -232,6 +237,20 @@ namespace Mono.CSharp
                        }
                }
 
+               public bool IsReadOnly => (modifiers & Modifiers.READONLY) != 0;
+
+               //
+               // Returns true for instances of any System.ValueTuple<......> type
+               //
+               public virtual bool IsTupleType {
+                       get {
+                               return (state & StateFlags.Tuple) != 0;
+                       }
+                       set {
+                               state = value ? state | StateFlags.Tuple : state & ~StateFlags.Tuple;
+                       }
+               }
+
                // TODO: Should probably do
                // IsGenericType -- recursive
                // HasTypeParameter -- non-recursive
@@ -490,17 +509,22 @@ namespace Mono.CSharp
                //
                // Text representation of type used by documentation writer
                //
-               public override string GetSignatureForDocumentation ()
+               public sealed override string GetSignatureForDocumentation ()
+               {
+                       return GetSignatureForDocumentation (false);
+               }
+
+               public virtual string GetSignatureForDocumentation (bool explicitName)
                {
                        StringBuilder sb = new StringBuilder ();
                        if (IsNested) {
-                               sb.Append (DeclaringType.GetSignatureForDocumentation ());
-                       } else {
-                               sb.Append (MemberDefinition.Namespace);
+                               sb.Append (DeclaringType.GetSignatureForDocumentation (explicitName));
+                       } else if (MemberDefinition.Namespace != null) {
+                               sb.Append (explicitName ? MemberDefinition.Namespace.Replace ('.', '#') : MemberDefinition.Namespace);
                        }
 
                        if (sb.Length != 0)
-                               sb.Append (".");
+                               sb.Append (explicitName ? "#" : ".");
 
                        sb.Append (Name);
                        if (Arity > 0) {
@@ -510,7 +534,7 @@ namespace Mono.CSharp
                                        if (i > 0)
                                            sb.Append (",");
 
-                                       sb.Append (TypeArguments[i].GetSignatureForDocumentation ());
+                                               sb.Append (TypeArguments[i].GetSignatureForDocumentation (explicitName));
                                    }
                                    sb.Append ("}");
                                } else {
@@ -522,33 +546,6 @@ namespace Mono.CSharp
                        return sb.ToString ();
                }
 
-               public string GetExplicitNameSignatureForDocumentation ()
-               {
-                       StringBuilder sb = new StringBuilder ();
-                       if (IsNested) {
-                               sb.Append (DeclaringType.GetExplicitNameSignatureForDocumentation ());
-                       } else if (MemberDefinition.Namespace != null) {
-                               sb.Append (MemberDefinition.Namespace.Replace ('.', '#'));
-                       }
-
-                       if (sb.Length != 0)
-                               sb.Append ("#");
-
-                       sb.Append (Name);
-                       if (Arity > 0) {
-                               sb.Append ("{");
-                               for (int i = 0; i < Arity; ++i) {
-                                       if (i > 0)
-                                               sb.Append (",");
-
-                                       sb.Append (TypeArguments[i].GetExplicitNameSignatureForDocumentation ());
-                               }
-                               sb.Append ("}");
-                       }
-
-                       return sb.ToString ();
-               }
-
                public override string GetSignatureForError ()
                {
                        string s;
@@ -556,7 +553,9 @@ namespace Mono.CSharp
                        if (IsNested) {
                                s = DeclaringType.GetSignatureForError ();
                        } else if (MemberDefinition is AnonymousTypeClass) {
-                               return ((AnonymousTypeClass) MemberDefinition).GetSignatureForError ();
+                               return ((AnonymousTypeClass)MemberDefinition).GetSignatureForError ();
+                       } else if (IsTupleType) {
+                               return FormatTupleSignature ();
                        } else {
                                s = MemberDefinition.Namespace;
                        }
@@ -567,9 +566,32 @@ namespace Mono.CSharp
                        return s + Name + GetTypeNameSignature ();
                }
 
+               string FormatTupleSignature ()
+               {
+                       var sb = new StringBuilder ();
+                       sb.Append ("(");
+                       for (int i = 0; i < TypeArguments.Length; ++i) {
+                               if (i != 0)
+                                       sb.Append (", ");
+
+                               sb.Append (TypeArguments[i].GetSignatureForError ());
+                       }
+                       sb.Append (")");
+
+                       return sb.ToString ();
+               }
+
                public string GetSignatureForErrorIncludingAssemblyName ()
                {
-                       return string.Format ("{0} [{1}]", GetSignatureForError (), MemberDefinition.DeclaringAssembly.FullName);
+                       var imported = MemberDefinition.DeclaringAssembly as ImportedAssemblyDefinition;
+
+                       var location = imported != null ?
+                               System.IO.Path.GetFullPath (imported.Location) :
+                           ((MemberCore)MemberDefinition).Location.NameFullPath;
+
+                       return string.Format ("{0} [{1} -- {2}]", GetSignatureForError (),
+                                                                 MemberDefinition.DeclaringAssembly.FullName,
+                                                                 location);
                }
 
                protected virtual string GetTypeNameSignature ()
@@ -1447,6 +1469,7 @@ namespace Mono.CSharp
                public static readonly InternalType Namespace = new InternalType ("<namespace>");
                public static readonly InternalType ErrorType = new InternalType ("<error>");
                public static readonly InternalType VarOutType = new InternalType ("var out");
+               public static readonly InternalType ThrowExpr = new InternalType ("throw expression");
 
                readonly string name;
 
@@ -1588,6 +1611,11 @@ namespace Mono.CSharp
                }
 
                #endregion
+
+               public static bool HasNoType (TypeSpec type)
+               {
+                       return type == AnonymousMethod || type == MethodGroup || type == NullLiteral || type == ThrowExpr;
+               }
        }
 
        //
@@ -1617,6 +1645,12 @@ namespace Mono.CSharp
 
                public TypeSpec Element { get; private set; }
 
+               public override IList<TypeSpec> Interfaces {
+                       set {
+                               throw new NotSupportedException ();
+                       }
+               }
+
                bool ITypeDefinition.IsComImport {
                        get {
                                return false;
@@ -1649,6 +1683,11 @@ namespace Mono.CSharp
 
                #endregion
 
+               public override void CheckObsoleteness (IMemberContext mc, Location loc)
+               {
+                       Element.CheckObsoleteness (mc, loc);
+               }
+
                public override ObsoleteAttribute GetAttributeObsolete ()
                {
                        return Element.GetAttributeObsolete ();
@@ -1659,9 +1698,9 @@ namespace Mono.CSharp
                        return null;
                }
 
-               public override string GetSignatureForDocumentation ()
+               public override string GetSignatureForDocumentation (bool explicitName)
                {
-                       return Element.GetSignatureForDocumentation () + GetPostfixSignature ();
+                       return Element.GetSignatureForDocumentation (explicitName) + GetPostfixSignature ();
                }
 
                public override string GetSignatureForError ()
@@ -1786,13 +1825,19 @@ namespace Mono.CSharp
                readonly int rank;
                readonly ModuleContainer module;
 
-               private ArrayContainer (ModuleContainer module, TypeSpec element, int rank)
+               ArrayContainer (ModuleContainer module, TypeSpec element, int rank)
                        : base (MemberKind.ArrayType, element, null)
                {
                        this.module = module;
                        this.rank = rank;
                }
 
+               public override IList<TypeSpec> Interfaces {
+                       get {
+                               return BaseType.Interfaces;
+                       }
+               }
+
                public int Rank {
                        get {
                                return rank;
@@ -1894,29 +1939,33 @@ namespace Mono.CSharp
                        return sb.ToString ();
                }
 
-               public override string GetSignatureForDocumentation ()
+               public override string GetSignatureForDocumentation (bool explicitName)
                {
                        StringBuilder sb = new StringBuilder ();
-                       GetElementSignatureForDocumentation (sb);
+                       GetElementSignatureForDocumentation (sb, explicitName);
                        return sb.ToString ();
                }
 
-               void GetElementSignatureForDocumentation (StringBuilder sb)
+               void GetElementSignatureForDocumentation (StringBuilder sb, bool explicitName)
                {
                        var ac = Element as ArrayContainer;
                        if (ac == null)
-                               sb.Append (Element.GetSignatureForDocumentation ());
+                               sb.Append (Element.GetSignatureForDocumentation (explicitName));
                        else
-                               ac.GetElementSignatureForDocumentation (sb);
+                               ac.GetElementSignatureForDocumentation (sb, explicitName);
 
-                       sb.Append ("[");
-                       for (int i = 1; i < rank; i++) {
-                               if (i == 1)
-                                       sb.Append ("0:");
+                       if (explicitName) {
+                               sb.Append (GetPostfixSignature (rank));
+                       } else {
+                               sb.Append ("[");
+                               for (int i = 1; i < rank; i++) {
+                                       if (i == 1)
+                                               sb.Append ("0:");
 
-                               sb.Append (",0:");
+                                       sb.Append (",0:");
+                               }
+                               sb.Append ("]");
                        }
-                       sb.Append ("]");
                }
 
                public static ArrayContainer MakeType (ModuleContainer module, TypeSpec element)
@@ -1931,7 +1980,6 @@ namespace Mono.CSharp
                        if (!module.ArrayTypesCache.TryGetValue (key, out ac)) {
                                ac = new ArrayContainer (module, element, rank);
                                ac.BaseType = module.Compiler.BuiltinTypes.Array;
-                               ac.Interfaces = ac.BaseType.Interfaces;
 
                                module.ArrayTypesCache.Add (key, ac);
                        }
@@ -1945,11 +1993,23 @@ namespace Mono.CSharp
                }
        }
 
+       [System.Diagnostics.DebuggerDisplay("{DisplayDebugInfo()}")]
        class ReferenceContainer : ElementTypeSpec
        {
-               private ReferenceContainer (TypeSpec element)
-                       : base (MemberKind.Class, element, null)        // TODO: Kind.Class is most likely wrong
+               ReferenceContainer (TypeSpec element)
+                       : base (MemberKind.ByRef, element, null)
+               {
+               }
+
+               public override IList<TypeSpec> Interfaces {
+                       get {
+                               return null;
+                       }
+               }
+
+               string DisplayDebugInfo()
                {
+                       return "ref " + GetSignatureForError();
                }
 
                public override MetaType GetMetaInfo ()
@@ -1961,8 +2021,16 @@ namespace Mono.CSharp
                        return info;
                }
 
+               public override string GetSignatureForError ()
+               {
+                       return Element.GetSignatureForError ();
+               }
+
                public static ReferenceContainer MakeType (ModuleContainer module, TypeSpec element)
                {
+                       if (element.Kind == MemberKind.ByRef)
+                               throw new ArgumentException ();
+
                        ReferenceContainer pc;
                        if (!module.ReferenceTypesCache.TryGetValue (element, out pc)) {
                                pc = new ReferenceContainer (element);
@@ -1982,6 +2050,12 @@ namespace Mono.CSharp
                        state &= ~StateFlags.CLSCompliant_Undetected;
                }
 
+               public override IList<TypeSpec> Interfaces {
+                       get {
+                               return null;
+                       }
+               }
+
                public override MetaType GetMetaInfo ()
                {
                        if (info == null) {