2 // parameter.cs: Parameter definition.
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Marek Safar (marek.safar@seznam.cz)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Collections;
19 namespace Mono.CSharp {
22 /// Abstract Base class for parameters of a method.
24 public abstract class ParameterBase : Attributable {
26 protected ParameterBuilder builder;
28 protected ParameterBase (Attributes attrs)
33 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
35 if (a.Type == TypeManager.marshal_as_attr_type) {
36 UnmanagedMarshal marshal = a.GetMarshal (this);
37 if (marshal != null) {
38 builder.SetMarshal (marshal);
43 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
44 a.Error_InvalidSecurityParent ();
48 builder.SetCustomAttribute (cb);
51 public override bool IsClsComplianceRequired()
58 /// Class for applying custom attributes on the return type
60 public class ReturnParameter : ParameterBase {
61 public ReturnParameter (MethodBuilder mb, Location location):
65 builder = mb.DefineParameter (0, ParameterAttributes.None, "");
67 catch (ArgumentOutOfRangeException) {
68 Report.RuntimeMissingSupport (location, "custom attributes on the return type");
72 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
74 if (a.Type == TypeManager.cls_compliant_attribute_type) {
75 Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
78 // This occurs after Warning -28
82 base.ApplyAttributeBuilder (a, cb);
85 public override AttributeTargets AttributeTargets {
87 return AttributeTargets.ReturnValue;
91 public override IResolveContext ResolveContext {
93 throw new NotSupportedException ();
100 public override string[] ValidAttributeTargets {
108 /// Class for applying custom attributes on the implicit parameter type
109 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
112 // TODO: should use more code from Parameter.ApplyAttributeBuilder
113 public class ImplicitParameter : ParameterBase {
114 public ImplicitParameter (MethodBuilder mb):
117 builder = mb.DefineParameter (1, ParameterAttributes.None, "");
120 public override AttributeTargets AttributeTargets {
122 return AttributeTargets.Parameter;
126 public override IResolveContext ResolveContext {
128 throw new NotSupportedException ();
135 public override string[] ValidAttributeTargets {
142 public class ParamsParameter : Parameter {
143 public ParamsParameter (Expression type, string name, Attributes attrs, Location loc):
144 base (type, name, Parameter.Modifier.PARAMS, attrs, loc)
148 public override bool Resolve (IResolveContext ec)
150 if (!base.Resolve (ec))
153 if (!parameter_type.IsArray || parameter_type.GetArrayRank () != 1) {
154 Report.Error (225, Location, "The params parameter must be a single dimensional array");
160 public override void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
162 base.ApplyAttributes (mb, cb, index);
164 CustomAttributeBuilder a = new CustomAttributeBuilder (
165 TypeManager.ConsParamArrayAttribute, new object [0]);
167 builder.SetCustomAttribute (a);
171 public class ArglistParameter : Parameter {
172 // Doesn't have proper type because it's never choosed for better conversion
173 public ArglistParameter () :
174 base (typeof (ArglistParameter), "", Parameter.Modifier.ARGLIST, null, Location.Null)
178 public override bool Resolve (IResolveContext ec)
183 public override string GetSignatureForError ()
190 /// Represents a single method parameter
192 public class Parameter : ParameterBase {
194 public enum Modifier : byte {
196 REF = REFMASK | ISBYREF,
197 OUT = OUTMASK | ISBYREF,
199 // This is a flag which says that it's either REF or OUT.
206 static string[] attribute_targets = new string [] { "param" };
208 public Expression TypeName;
209 public readonly Modifier ModFlags;
211 protected Type parameter_type;
212 public readonly Location Location;
214 IResolveContext resolve_context;
216 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Location loc)
225 public Parameter (Type type, string name, Modifier mod, Attributes attrs, Location loc)
230 parameter_type = type;
234 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
236 if (a.Type == TypeManager.in_attribute_type && ModFlags == Modifier.OUT) {
237 Report.Error (36, a.Location, "An out parameter cannot have the `In' attribute");
241 if (a.Type == TypeManager.param_array_type) {
242 Report.Error (674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
246 if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) == Modifier.REF &&
247 !OptAttributes.Contains (TypeManager.in_attribute_type)) {
248 Report.Error (662, a.Location,
249 "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
253 if (a.Type == TypeManager.cls_compliant_attribute_type) {
254 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
257 base.ApplyAttributeBuilder (a, cb);
260 public override IResolveContext ResolveContext {
262 return resolve_context;
267 // Resolve is used in method definitions
269 public virtual bool Resolve (IResolveContext ec)
271 if (parameter_type != null)
274 this.resolve_context = ec;
276 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
280 parameter_type = texpr.Type;
282 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
283 Report.Error (721, Location, "`{0}': static types cannot be used as parameters", GetSignatureForError ());
287 if (parameter_type == TypeManager.void_type){
288 Report.Error (1536, Location, "Invalid parameter type 'void'");
292 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
293 if (parameter_type == TypeManager.typed_reference_type ||
294 parameter_type == TypeManager.arg_iterator_type){
295 Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
296 GetSignatureForError ());
304 public Type ExternalType ()
306 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
307 return TypeManager.GetReferenceType (parameter_type);
309 return parameter_type;
312 public Type ParameterType {
314 return parameter_type;
318 public ParameterAttributes Attributes {
322 return ParameterAttributes.None;
324 return ParameterAttributes.None;
326 return ParameterAttributes.Out;
327 case Modifier.PARAMS:
331 return ParameterAttributes.None;
335 public override AttributeTargets AttributeTargets {
337 return AttributeTargets.Parameter;
341 public virtual string GetSignatureForError ()
344 if (parameter_type != null)
345 type_name = TypeManager.CSharpName (parameter_type);
347 type_name = TypeName.GetSignatureForError ();
349 string mod = GetModifierSignature (ModFlags);
351 return String.Concat (mod, ' ', type_name);
356 public static string GetModifierSignature (Modifier mod)
361 case Modifier.PARAMS:
365 case Modifier.ARGLIST:
372 public void IsClsCompliant ()
374 if (AttributeTester.IsClsCompliant (ExternalType ()))
377 Report.Error (3001, Location, "Argument type `{0}' is not CLS-compliant", GetSignatureForError ());
380 public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
383 builder = cb.DefineParameter (index, Attributes, Name);
385 builder = mb.DefineParameter (index, Attributes, Name);
387 if (OptAttributes != null)
388 OptAttributes.Emit ();
391 public override string[] ValidAttributeTargets {
393 return attribute_targets;
399 /// Represents the methods parameters
401 public class Parameters : ParameterData {
402 // Null object pattern
403 public Parameter [] FixedParameters;
404 public readonly bool HasArglist;
408 public static readonly Parameters EmptyReadOnlyParameters = new Parameters ();
409 static readonly Parameter ArgList = new ArglistParameter ();
411 private Parameters ()
413 FixedParameters = new Parameter[0];
414 types = new Type [0];
417 public Parameters (Parameter[] parameters, Type[] types)
419 FixedParameters = parameters;
421 count = types.Length;
424 public Parameters (Parameter[] parameters)
426 if (parameters == null)
427 throw new ArgumentException ("Use EmptyReadOnlyPatameters");
429 FixedParameters = parameters;
430 count = parameters.Length;
433 public Parameters (Parameter[] parameters, bool has_arglist):
436 HasArglist = has_arglist;
440 /// Use this method when you merge compiler generated argument with user arguments
442 public static Parameters MergeGenerated (Parameters userParams, params Parameter[] compilerParams)
444 Parameter[] all_params = new Parameter [userParams.count + compilerParams.Length];
445 Type[] all_types = new Type[all_params.Length];
446 userParams.FixedParameters.CopyTo(all_params, 0);
447 userParams.Types.CopyTo (all_types, 0);
449 int last_filled = userParams.Count;
450 foreach (Parameter p in compilerParams) {
451 for (int i = 0; i < last_filled; ++i) {
452 while (p.Name == all_params [i].Name) {
453 p.Name = '_' + p.Name;
456 all_params [last_filled] = p;
457 all_types [last_filled] = p.ParameterType;
461 return new Parameters (all_params, all_types);
472 return HasArglist ? count + 1 : count;
481 for (int i = 0; i < count; i++){
482 string base_name = FixedParameters [i].Name;
483 for (int j = i + 1; j < count; j++){
484 if (base_name != FixedParameters [j].Name)
487 Report.Error (100, FixedParameters [i].Location,
488 "The parameter name `{0}' is a duplicate", base_name);
497 /// Returns the paramenter information based on the name
499 public Parameter GetParameterByName (string name, out int idx)
508 foreach (Parameter par in FixedParameters){
509 if (par.Name == name){
518 public Parameter GetParameterByName (string name)
522 return GetParameterByName (name, out idx);
525 public bool Resolve (IResolveContext ec)
530 types = new Type [count];
537 for (int i = 0; i < FixedParameters.Length; ++i) {
538 p = FixedParameters [i];
539 if (!p.Resolve (ec)) {
543 types [i] = p.ExternalType ();
549 public CallingConventions CallingConvention
553 return CallingConventions.VarArgs;
555 return CallingConventions.Standard;
559 // Define each type attribute (in/out/ref) and
560 // the argument names.
561 public void ApplyAttributes (MethodBase builder)
566 MethodBuilder mb = builder as MethodBuilder;
567 ConstructorBuilder cb = builder as ConstructorBuilder;
569 for (int i = 0; i < FixedParameters.Length; i++) {
570 FixedParameters [i].ApplyAttributes (mb, cb, i + 1);
574 public void VerifyClsCompliance ()
576 foreach (Parameter p in FixedParameters)
580 public string GetSignatureForError ()
582 StringBuilder sb = new StringBuilder ("(");
584 for (int i = 0; i < FixedParameters.Length; ++i) {
585 sb.Append (FixedParameters[i].GetSignatureForError ());
586 if (i < FixedParameters.Length - 1)
595 sb.Append ("__arglist");
600 return sb.ToString ();
603 public Type[] Types {
609 Parameter this [int pos]
612 if (pos >= count && (HasArglist || HasParams)) {
613 if (HasArglist && (pos == 0 || pos >= count))
618 return FixedParameters [pos];
622 #region ParameterData Members
624 public Type ParameterType (int pos)
626 return this [pos].ExternalType ();
629 public bool HasParams {
634 return FixedParameters [count - 1] is ParamsParameter;
638 public string ParameterName (int pos)
640 return this [pos].Name;
643 public string ParameterDesc (int pos)
645 return this [pos].GetSignatureForError ();
648 public Parameter.Modifier ParameterModifier (int pos)
650 return this [pos].ModFlags;