disable the building of mjs while I solve the problem with the unexposed ctr of Context
[mono.git] / mcs / mcs / parameter.cs
old mode 100755 (executable)
new mode 100644 (file)
index d66a060..51af0f7
@@ -31,12 +31,15 @@ namespace Mono.CSharp {
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
                        if (a.Type == TypeManager.marshal_as_attr_type) {
-                               UnmanagedMarshal marshal = a.GetMarshal ();
+                               UnmanagedMarshal marshal = a.GetMarshal (this);
                                if (marshal != null) {
                                        builder.SetMarshal (marshal);
-                                       return;
                                }
-                               Report.Warning_T (-24, a.Location);
+                               return;
+                       }
+
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
+                               a.Error_InvalidSecurityParent ();
                                return;
                        }
 
@@ -47,13 +50,34 @@ namespace Mono.CSharp {
                {
                        return false;
                }
-
        }
 
+       /// <summary>
+       /// Class for applying custom attributes on the return type
+       /// </summary>
        public class ReturnParameter: ParameterBase {
-               public ReturnParameter (Attributes attrs):
-                       base (attrs)
+               public ReturnParameter (MethodBuilder mb, Location location):
+                       base (null)
                {
+                       try {
+                               builder = mb.DefineParameter (0, ParameterAttributes.None, "");                 
+                       }
+                       catch (ArgumentOutOfRangeException) {
+                               Report.Warning (-28, location, "The Microsoft .NET Runtime 1.x does not permit setting custom attributes on the return type");
+                       }
+               }
+
+               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               {
+                       if (a.Type == TypeManager.cls_compliant_attribute_type) {
+                               Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
+                       }
+
+                       // This occurs after Warning -28
+                       if (builder == null)
+                               return;
+
+                       base.ApplyAttributeBuilder (a, cb);
                }
 
                public override AttributeTargets AttributeTargets {
@@ -62,16 +86,39 @@ namespace Mono.CSharp {
                        }
                }
 
-               public void DefineParameter (EmitContext ec, MethodBuilder mb, Location loc)
+               /// <summary>
+               /// Is never called
+               /// </summary>
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return null;
+                       }
+               }
+       }
+
+       /// <summary>
+       /// Class for applying custom attributes on the implicit parameter type
+       /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
+       /// </summary>
+       public class ImplicitParameter: ParameterBase {
+               public ImplicitParameter (MethodBuilder mb):
+                       base (null)
                {
-                       try {
-                               builder = mb.DefineParameter (0, ParameterAttributes.None, "");                         
-                               OptAttributes.Emit (ec, this);
-                       } catch (ArgumentOutOfRangeException) {
-                               Report.Warning (
-                                       -24, loc,
-                                       ".NET SDK 1.0 does not permit setting custom attributes" +
-                                       " on the return type of a method");
+                       builder = mb.DefineParameter (1, ParameterAttributes.None, "");                 
+               }
+
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Parameter;
+                       }
+               }
+
+               /// <summary>
+               /// Is never called
+               /// </summary>
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return null;
                        }
                }
        }
@@ -89,13 +136,18 @@ namespace Mono.CSharp {
                        OUT     = 2,
                        PARAMS  = 4,
                        // This is a flag which says that it's either REF or OUT.
-                       ISBYREF = 8
+                       ISBYREF = 8,
+                       ARGLIST = 16
                }
 
-               public readonly Expression TypeName;
+               static string[] attribute_targets = new string [] { "param" };
+
+               public Expression TypeName;
                public readonly Modifier ModFlags;
                public readonly string Name;
                Type parameter_type;
+
+               EmitContext ec;  // because ApplyAtrribute doesn't have ec
                
                public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
                        : base (attrs)
@@ -105,15 +157,52 @@ namespace Mono.CSharp {
                        TypeName = type;
                }
 
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               {
+                       if (a.Type == TypeManager.in_attribute_type && Attributes == ParameterAttributes.Out) {
+                               Report.Error (36, a.Location, "Can not use [In] attribute on out parameter");
+                               return;
+                       }
+
+                       if (a.Type == TypeManager.param_array_type) {
+                               Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
+                               return;
+                       }
+
+                       if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) != 0 &&
+                           !OptAttributes.Contains (TypeManager.in_attribute_type, ec)) {
+                               Report.Error (662, a.Location,
+                                       "'{0}' cannot specify only Out attribute on a ref parameter. Use both In and Out attributes, or neither", GetSignatureForError ());
+                               return;
+                       }
+
+                       if (a.Type == TypeManager.cls_compliant_attribute_type) {
+                               Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
+                       }
+
+                       base.ApplyAttributeBuilder (a, cb);
+               }
+
                // <summary>
                //   Resolve is used in method definitions
                // </summary>
-               public bool Resolve (DeclSpace ds, Location l)
+               public bool Resolve (EmitContext ec, Location l)
                {
-                       parameter_type = ds.ResolveType (TypeName, false, l);
+                       this.ec = ec;
+
+                       TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
+                       if (texpr == null)
+                               return false;
+
+                       parameter_type = texpr.ResolveType (ec);
+                       
+                       if (parameter_type.IsAbstract && parameter_type.IsSealed) {
+                               Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
+                               return false;
+                       }
 
                        if (parameter_type == TypeManager.void_type){
-                               Report.Error (1536, l, "`void' parameter is not permitted");
+                               Report.Error (1536, l, "Invalid parameter type 'void'");
                                return false;
                        }
 
@@ -171,10 +260,10 @@ namespace Mono.CSharp {
                ///   Returns the signature for this parameter evaluating it on the
                ///   @tc context
                /// </summary>
-               public string GetSignature (DeclSpace ds, Location loc)
+               public string GetSignature (EmitContext ec, Location loc)
                {
                        if (parameter_type == null){
-                               if (!Resolve (ds, loc))
+                               if (!Resolve (ec, loc))
                                        return null;
                        }
 
@@ -183,8 +272,15 @@ namespace Mono.CSharp {
 
                public string GetSignatureForError ()
                {
-                       string typeName = TypeManager.CSharpName (parameter_type);
-                       switch (ModFlags & ~Modifier.ISBYREF) {
+                       string typeName;
+                       if (parameter_type != null)
+                               typeName = TypeManager.CSharpName (parameter_type);
+                       else if (TypeName.Type != null)
+                               typeName = TypeManager.CSharpName (TypeName.Type);
+                       else
+                               typeName = TypeName.ToString ();
+
+                       switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
                                case Modifier.OUT:
                                        return "out " + typeName;
                                case Modifier.PARAMS:
@@ -206,11 +302,12 @@ namespace Mono.CSharp {
                                        
                        if (OptAttributes != null) {
                                OptAttributes.Emit (ec, this);
-       
-                               if (par_attr == ParameterAttributes.Out){
-                                       if (OptAttributes.Contains (TypeManager.in_attribute_type, ec.DeclSpace))
-                                               Report.Error (36, loc,  "Can not use [In] attribute on out parameter");
-                               }
+                       }
+               }
+
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
                        }
                }
        }
@@ -221,6 +318,7 @@ namespace Mono.CSharp {
        public class Parameters {
                public Parameter [] FixedParameters;
                public readonly Parameter ArrayParameter;
+               public readonly bool HasArglist;
                string signature;
                Type [] types;
                Location loc;
@@ -234,6 +332,13 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
+               public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
+               {
+                       FixedParameters = fixed_parameters;
+                       HasArglist = has_arglist;
+                       loc = l;
+               }
+
                /// <summary>
                ///   This is used to reuse a set of empty parameters, because they
                ///   are common
@@ -253,14 +358,14 @@ namespace Mono.CSharp {
                        }
                }
                
-               public void ComputeSignature (DeclSpace ds)
+               public void ComputeSignature (EmitContext ec)
                {
                        signature = "";
                        if (FixedParameters != null){
                                for (int i = 0; i < FixedParameters.Length; i++){
                                        Parameter par = FixedParameters [i];
                                        
-                                       signature += par.GetSignature (ds, loc);
+                                       signature += par.GetSignature (ec, loc);
                                }
                        }
                        //
@@ -285,9 +390,9 @@ namespace Mono.CSharp {
                        
                        count = FixedParameters.Length;
                        string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
+
                        for (i = 0; i < count; i++){
                                string base_name = FixedParameters [i].Name;
-                               
                                for (j = i + 1; j < count; j++){
                                        if (base_name != FixedParameters [j].Name)
                                                continue;
@@ -305,13 +410,13 @@ namespace Mono.CSharp {
                
                /// <summary>
                ///    Returns the signature of the Parameters evaluated in
-               ///    the @tc environment
+               ///    the @ec EmitContext
                /// </summary>
-               public string GetSignature (DeclSpace ds)
+               public string GetSignature (EmitContext ec)
                {
                        if (signature == null){
                                VerifyArgs ();
-                               ComputeSignature (ds);
+                               ComputeSignature (ec);
                        }
                        
                        return signature;
@@ -345,7 +450,14 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               bool ComputeParameterTypes (DeclSpace ds)
+               public Parameter GetParameterByName (string name)
+               {
+                       int idx;
+
+                       return GetParameterByName (name, out idx);
+               }
+               
+               bool ComputeParameterTypes (EmitContext ec)
                {
                        int extra = (ArrayParameter != null) ? 1 : 0;
                        int i = 0;
@@ -368,7 +480,7 @@ namespace Mono.CSharp {
                                foreach (Parameter p in FixedParameters){
                                        Type t = null;
                                        
-                                       if (p.Resolve (ds, loc))
+                                       if (p.Resolve (ec, loc))
                                                t = p.ExternalType ();
                                        else
                                                failed = true;
@@ -379,7 +491,7 @@ namespace Mono.CSharp {
                        }
                        
                        if (extra > 0){
-                               if (ArrayParameter.Resolve (ds, loc))
+                               if (ArrayParameter.Resolve (ec, loc))
                                        types [i] = ArrayParameter.ExternalType ();
                                else 
                                        failed = true;
@@ -397,55 +509,13 @@ namespace Mono.CSharp {
                // This variant is used by Delegates, because they need to
                // resolve/define names, instead of the plain LookupType
                //
-               public bool ComputeAndDefineParameterTypes (DeclSpace ds)
+               public bool ComputeAndDefineParameterTypes (EmitContext ec)
                {
-                       int extra = (ArrayParameter != null) ? 1 : 0;
-                       int i = 0;
-                       int pc;
-
-                       if (FixedParameters == null)
-                               pc = extra;
-                       else
-                               pc = extra + FixedParameters.Length;
-                       
-                       types = new Type [pc];
-                       
-                       if (!VerifyArgs ()){
-                               FixedParameters = null;
-                               return false;
-                       }
-
-                       bool ok_flag = true;
-                       
-                       if (FixedParameters != null){
-                               foreach (Parameter p in FixedParameters){
-                                       Type t = null;
-                                       
-                                       if (p.Resolve (ds, loc))
-                                               t = p.ExternalType ();
-                                       else
-                                               ok_flag = false;
-                                       
-                                       types [i] = t;
-                                       i++;
-                               }
-                       }
-                       
-                       if (extra > 0){
-                               if (ArrayParameter.Resolve (ds, loc))
-                                       types [i] = ArrayParameter.ExternalType ();
-                               else
-                                       ok_flag = false;
-                       }
-
-                       //
-                       // invalidate the cached types
-                       //
-                       if (!ok_flag){
-                               types = null;
-                       }
-                       
-                       return ok_flag;
+                       bool old_type_resolving = ec.ResolvingTypeTree;
+                       ec.ResolvingTypeTree = true;
+                       bool retval = ComputeParameterTypes (ec);
+                       ec.ResolvingTypeTree = old_type_resolving;
+                       return retval;
                }
                
                /// <summary>
@@ -453,7 +523,7 @@ namespace Mono.CSharp {
                /// </summary>
                static Type [] no_types = new Type [0];
                
-               public Type [] GetParameterInfo (DeclSpace ds)
+               public Type [] GetParameterInfo (EmitContext ec)
                {
                        if (types != null)
                                return types;
@@ -461,7 +531,7 @@ namespace Mono.CSharp {
                        if (FixedParameters == null && ArrayParameter == null)
                                return no_types;
 
-                       if (ComputeParameterTypes (ds) == false){
+                       if (ComputeParameterTypes (ec) == false){
                                types = null;
                                return null;
                        }
@@ -476,7 +546,7 @@ namespace Mono.CSharp {
                ///   Note that the returned type will not contain any dereference in this
                ///   case (ie, you get "int" for a ref int instead of "int&"
                /// </summary>
-               public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
+               public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
                {
                        mod = Parameter.Modifier.NONE;
                        
@@ -489,7 +559,7 @@ namespace Mono.CSharp {
                                return null;
                        
                        if (types == null)
-                               if (ComputeParameterTypes (ds) == false)
+                               if (ComputeParameterTypes (ec) == false)
                                        return null;
 
                        //
@@ -513,8 +583,10 @@ namespace Mono.CSharp {
 
                public CallingConventions GetCallingConvention ()
                {
-                       // For now this is the only correc thing to do
-                       return CallingConventions.Standard;
+                       if (HasArglist)
+                               return CallingConventions.VarArgs;
+                       else
+                               return CallingConventions.Standard;
                }
 
                //
@@ -523,7 +595,6 @@ namespace Mono.CSharp {
                //
                public void LabelParameters (EmitContext ec,
                        MethodBase builder,
-                       Attributes method_attrs,
                        Location loc) {
                        //
                        // Define each type attribute (in/out/ref) and
@@ -558,34 +629,6 @@ namespace Mono.CSharp {
                                
                                pb.SetCustomAttribute (a);
                        }
-
-                       //
-                       // And now for the return type attribute decoration
-                       //
-                               
-                       if (mb == null || method_attrs == null)
-                               return;
-
-                       Attributes ret_attrs = null;
-                       foreach (AttributeSection asec in method_attrs.AttributeSections) {
-
-                               if (asec.Target != "return")
-                                       continue;
-
-                               if (ret_attrs == null)
-                                       ret_attrs = new Attributes (asec);
-                               else
-                                       ret_attrs.AddAttributeSection (asec);
-                       }
-
-                       if (ret_attrs != null) {
-                               ReturnParameter ret = new ReturnParameter (ret_attrs);
-                               ret.DefineParameter (ec, mb, loc);
-                       }
                }
-
        }
 }
-               
-       
-