2 // parameter.cs: Parameter definition.
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Marek Safar (marek.safar@seznam.cz)
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2003-2008 Novell, Inc.
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
, PredefinedAttributes pa
)
36 if (a
.Type
== pa
.MarshalAs
) {
37 UnmanagedMarshal marshal
= a
.GetMarshal (this);
38 if (marshal
!= null) {
39 builder
.SetMarshal (marshal
);
44 if (a
.HasSecurityAttribute
) {
45 a
.Error_InvalidSecurityParent ();
49 builder
.SetCustomAttribute (cb
);
52 public override bool IsClsComplianceRequired()
59 /// Class for applying custom attributes on the return type
61 public class ReturnParameter
: ParameterBase
{
62 public ReturnParameter (MethodBuilder mb
, Location location
):
66 builder
= mb
.DefineParameter (0, ParameterAttributes
.None
, "");
68 catch (ArgumentOutOfRangeException
) {
69 Report
.RuntimeMissingSupport (location
, "custom attributes on the return type");
73 public override void ApplyAttributeBuilder (Attribute a
, CustomAttributeBuilder cb
, PredefinedAttributes pa
)
75 if (a
.Type
== pa
.CLSCompliant
) {
76 Report
.Warning (3023, 1, a
.Location
, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
79 // This occurs after Warning -28
83 base.ApplyAttributeBuilder (a
, cb
, pa
);
86 public override AttributeTargets AttributeTargets
{
88 return AttributeTargets
.ReturnValue
;
92 public void EmitPredefined (PredefinedAttribute pa
, Location loc
)
95 pa
.EmitAttribute (builder
, loc
);
98 public override IResolveContext ResolveContext
{
100 throw new NotSupportedException ();
107 public override string[] ValidAttributeTargets
{
115 /// Class for applying custom attributes on the implicit parameter type
116 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
119 // TODO: should use more code from Parameter.ApplyAttributeBuilder
120 public class ImplicitParameter
: ParameterBase
{
121 public ImplicitParameter (MethodBuilder mb
):
124 builder
= mb
.DefineParameter (1, ParameterAttributes
.None
, "value");
127 public override AttributeTargets AttributeTargets
{
129 return AttributeTargets
.Parameter
;
133 public override IResolveContext ResolveContext
{
135 throw new NotSupportedException ();
142 public override string[] ValidAttributeTargets
{
149 public class ImplicitLambdaParameter
: Parameter
151 public ImplicitLambdaParameter (string name
, Location loc
)
152 : base (null, name
, Modifier
.NONE
, null, loc
)
156 public override Type
Resolve (IResolveContext ec
)
158 if (parameter_type
== null)
159 throw new InternalErrorException ("A type of implicit lambda parameter `{0}' is not set",
162 return parameter_type
;
166 set { parameter_type = value; }
170 public class ParamsParameter
: Parameter
{
171 public ParamsParameter (FullNamedExpression type
, string name
, Attributes attrs
, Location loc
):
172 base (type
, name
, Parameter
.Modifier
.PARAMS
, attrs
, loc
)
176 public override Type
Resolve (IResolveContext ec
)
178 if (base.Resolve (ec
) == null)
181 if (!parameter_type
.IsArray
|| parameter_type
.GetArrayRank () != 1) {
182 Report
.Error (225, Location
, "The params parameter must be a single dimensional array");
186 return parameter_type
;
189 public override void ApplyAttributes (MethodBuilder mb
, ConstructorBuilder cb
, int index
)
191 base.ApplyAttributes (mb
, cb
, index
);
192 PredefinedAttributes
.Get
.ParamArray
.EmitAttribute (builder
, Location
);
196 public class ArglistParameter
: Parameter
{
197 // Doesn't have proper type because it's never chosen for better conversion
198 public ArglistParameter (Location loc
) :
199 base (null, String
.Empty
, Parameter
.Modifier
.NONE
, null, loc
)
203 public override void ApplyAttributes (MethodBuilder mb
, ConstructorBuilder cb
, int index
)
208 public override bool CheckAccessibility (InterfaceMemberBase member
)
213 public override Type
Resolve (IResolveContext ec
)
215 return InternalType
.Arglist
;
218 public override string GetSignatureForError ()
224 public interface IParameterData
226 Expression DefaultValue { get; }
227 bool HasExtensionMethodModifier { get; }
228 bool HasDefaultValue { get; }
229 Parameter
.Modifier ModFlags { get; }
234 // Parameter information created by parser
236 public class Parameter
: ParameterBase
, IParameterData
, ILocalVariable
{
238 public enum Modifier
: byte {
240 REF
= REFMASK
| ISBYREF
,
241 OUT
= OUTMASK
| ISBYREF
,
243 // This is a flag which says that it's either REF or OUT.
250 static string[] attribute_targets
= new string [] { "param" }
;
252 protected FullNamedExpression TypeName
;
253 readonly Modifier modFlags
;
255 Expression default_expr
;
256 protected Type parameter_type
;
257 public readonly Location Location
;
259 public bool HasAddressTaken
;
261 IResolveContext resolve_context
;
262 LocalVariableReference expr_tree_variable
;
263 static TypeExpr parameter_expr_tree_type
;
265 public HoistedVariable HoistedVariableReference
;
267 public Parameter (FullNamedExpression type
, string name
, Modifier mod
, Attributes attrs
, Location loc
)
270 if (type
== TypeManager
.system_void_expr
)
271 Report
.Error (1536, loc
, "Invalid parameter type `void'");
279 public override void ApplyAttributeBuilder (Attribute a
, CustomAttributeBuilder cb
, PredefinedAttributes pa
)
281 if (a
.Type
== pa
.In
&& ModFlags
== Modifier
.OUT
) {
282 Report
.Error (36, a
.Location
, "An out parameter cannot have the `In' attribute");
286 if (a
.Type
== pa
.ParamArray
) {
287 Report
.Error (674, a
.Location
, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
291 if (a
.Type
== PredefinedAttributes
.Get
.Out
&& (ModFlags
& Modifier
.REF
) == Modifier
.REF
&&
292 !OptAttributes
.Contains (pa
.In
)) {
293 Report
.Error (662, a
.Location
,
294 "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
298 if (a
.Type
== pa
.CLSCompliant
) {
299 Report
.Warning (3022, 1, a
.Location
, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
302 if (HasDefaultValue
&& (a
.Type
== pa
.DefaultParameterValue
|| a
.Type
== pa
.OptionalParameter
)) {
303 Report
.Error (1745, a
.Location
,
304 "Cannot specify `{0}' attribute on optional parameter `{1}'",
305 TypeManager
.CSharpName (a
.Type
).Replace ("Attribute", ""), Name
);
309 if (a
.Type
== pa
.DefaultParameterValue
) {
310 object val
= a
.GetParameterDefaultValue ();
312 Type t
= val
.GetType ();
313 if (t
.IsArray
|| TypeManager
.IsSubclassOf (t
, TypeManager
.type_type
)) {
314 if (parameter_type
== TypeManager
.object_type
) {
316 t
= TypeManager
.type_type
;
318 Report
.Error (1910, a
.Location
, "Argument of type `{0}' is not applicable for the DefaultParameterValue attribute",
319 TypeManager
.CSharpName (t
));
321 Report
.Error (1909, a
.Location
, "The DefaultParameterValue attribute is not applicable on parameters of type `{0}'",
322 TypeManager
.CSharpName (parameter_type
)); ;
328 if (parameter_type
== TypeManager
.object_type
||
329 (val
== null && !TypeManager
.IsGenericParameter (parameter_type
) && TypeManager
.IsReferenceType (parameter_type
)) ||
330 (val
!= null && TypeManager
.TypeToCoreType (val
.GetType ()) == parameter_type
))
331 builder
.SetConstant (val
);
333 Report
.Error (1908, a
.Location
, "The type of the default value should match the type of the parameter");
337 base.ApplyAttributeBuilder (a
, cb
, pa
);
340 public virtual bool CheckAccessibility (InterfaceMemberBase member
)
342 if (parameter_type
== null || TypeManager
.IsGenericParameter (parameter_type
))
345 return member
.IsAccessibleAs (parameter_type
);
348 public override IResolveContext ResolveContext
{
350 return resolve_context
;
355 // Resolve is used in method definitions
357 public virtual Type
Resolve (IResolveContext rc
)
359 // HACK: to resolve attributes correctly
360 this.resolve_context
= rc
;
362 if (parameter_type
!= null)
363 return parameter_type
;
365 TypeExpr texpr
= TypeName
.ResolveAsTypeTerminal (rc
, false);
369 parameter_type
= texpr
.Type
;
371 // Ignore all checks for dummy members
372 AbstractPropertyEventMethod pem
= rc
as AbstractPropertyEventMethod
;
373 if (pem
!= null && pem
.IsDummy
)
374 return parameter_type
;
376 if (default_expr
!= null) {
377 EmitContext ec
= new EmitContext (rc
, rc
.GenericDeclContainer
, Location
, null, parameter_type
, 0);
378 default_expr
= default_expr
.Resolve (ec
);
379 if (default_expr
!= null) {
380 Constant
value = default_expr
as Constant
;
382 if (default_expr
!= null) {
383 bool is_valid
= false;
384 if (default_expr
is DefaultValueExpression
) {
386 } else if (default_expr
is New
&& ((New
) default_expr
).IsDefaultValueType
) {
387 is_valid
= TypeManager
.IsEqual (parameter_type
, default_expr
.Type
) ||
388 (TypeManager
.IsNullableType (parameter_type
) &&
389 Convert
.ImplicitNulableConversion (ec
, default_expr
, parameter_type
) != EmptyExpression
.Null
);
391 Report
.Error (1736, default_expr
.Location
,
392 "The expression being assigned to optional parameter `{0}' must be a constant or default value",
399 Report
.Error (1763, Location
,
400 "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
401 Name
, GetSignatureForError ());
405 Constant c
= value.ConvertImplicitly (parameter_type
);
407 if (parameter_type
== TypeManager
.object_type
) {
408 Report
.Error (1763, Location
,
409 "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
410 Name
, GetSignatureForError ());
412 Report
.Error (1750, Location
,
413 "Optional parameter value `{0}' cannot be converted to parameter type `{1}'",
414 value.GetValue (), GetSignatureForError ());
422 if ((modFlags
& Parameter
.Modifier
.ISBYREF
) != 0 &&
423 TypeManager
.IsSpecialType (parameter_type
)) {
424 Report
.Error (1601, Location
, "Method or delegate parameter cannot be of type `{0}'",
425 GetSignatureForError ());
429 TypeManager
.CheckTypeVariance (parameter_type
,
430 (modFlags
& Parameter
.Modifier
.ISBYREF
) != 0 ? Variance
.None
: Variance
.Contravariant
,
433 if (texpr
is TypeParameterExpr
)
434 return parameter_type
;
436 if ((parameter_type
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
) {
437 Report
.Error (721, Location
, "`{0}': static types cannot be used as parameters",
438 texpr
.GetSignatureForError ());
439 return parameter_type
;
442 if ((modFlags
& Modifier
.This
) != 0 && parameter_type
.IsPointer
) {
443 Report
.Error (1103, Location
, "The type of extension method cannot be `{0}'",
444 TypeManager
.CSharpName (parameter_type
));
447 return parameter_type
;
450 public void ResolveVariable (int idx
)
455 public bool HasDefaultValue
{
456 get { return default_expr != null; }
459 public bool HasExtensionMethodModifier
{
460 get { return (modFlags & Modifier.This) != 0; }
463 public Modifier ModFlags
{
464 get { return modFlags & ~Modifier.This; }
469 set { name = value; }
472 ParameterAttributes Attributes
{
473 get { return ParametersCompiled
.GetParameterAttribute (modFlags
) |
474 (HasDefaultValue
? ParameterAttributes
.Optional
: ParameterAttributes
.None
); }
477 public override AttributeTargets AttributeTargets
{
479 return AttributeTargets
.Parameter
;
483 public virtual string GetSignatureForError ()
486 if (parameter_type
!= null)
487 type_name
= TypeManager
.CSharpName (parameter_type
);
489 type_name
= TypeName
.GetSignatureForError ();
491 string mod
= GetModifierSignature (modFlags
);
493 return String
.Concat (mod
, " ", type_name
);
498 public static string GetModifierSignature (Modifier mod
)
503 case Modifier
.PARAMS
:
514 public void IsClsCompliant ()
516 if (AttributeTester
.IsClsCompliant (parameter_type
))
519 Report
.Warning (3001, 1, Location
, "Argument type `{0}' is not CLS-compliant", GetSignatureForError ());
522 public virtual void ApplyAttributes (MethodBuilder mb
, ConstructorBuilder cb
, int index
)
525 builder
= cb
.DefineParameter (index
, Attributes
, Name
);
527 builder
= mb
.DefineParameter (index
, Attributes
, Name
);
529 if (OptAttributes
!= null)
530 OptAttributes
.Emit ();
532 if (HasDefaultValue
) {
534 // Emit constant values for true constants only, the other
535 // constant-like expressions will rely on default value expression
537 Constant c
= default_expr
as Constant
;
539 if (default_expr
.Type
== TypeManager
.decimal_type
) {
540 builder
.SetCustomAttribute (Const
.CreateDecimalConstantAttribute (c
));
542 builder
.SetConstant (c
.GetValue ());
547 if (TypeManager
.IsDynamicType (parameter_type
))
548 PredefinedAttributes
.Get
.Dynamic
.EmitAttribute (builder
, Location
);
551 public override string[] ValidAttributeTargets
{
553 return attribute_targets
;
557 public Parameter
Clone ()
559 Parameter p
= (Parameter
) MemberwiseClone ();
560 if (attributes
!= null) {
561 p
.attributes
= attributes
.Clone ();
562 p
.attributes
.AttachTo (p
);
568 public ExpressionStatement
CreateExpressionTreeVariable (EmitContext ec
)
571 // A parameter is not hoisted when used directly as ET
573 HoistedVariableReference
= null;
575 if ((modFlags
& Modifier
.ISBYREF
) != 0)
576 Report
.Error (1951, Location
, "An expression tree parameter cannot use `ref' or `out' modifier");
578 LocalInfo variable
= ec
.CurrentBlock
.AddTemporaryVariable (
579 ResolveParameterExpressionType (ec
, Location
), Location
);
580 variable
.Resolve (ec
);
582 expr_tree_variable
= new LocalVariableReference (
583 ec
.CurrentBlock
, variable
.Name
, Location
, variable
, false);
585 Arguments arguments
= new Arguments (2);
586 arguments
.Add (new Argument (new TypeOf (
587 new TypeExpression (parameter_type
, Location
), Location
)));
588 arguments
.Add (new Argument (new StringConstant (Name
, Location
)));
589 return new SimpleAssign (ExpressionTreeVariableReference (),
590 Expression
.CreateExpressionFactoryCall ("Parameter", null, arguments
, Location
));
593 public Expression DefaultValue
{
594 get { return default_expr; }
595 set { default_expr = value; }
598 public void Emit (EmitContext ec
)
604 ParameterReference
.EmitLdArg (ec
.ig
, arg_idx
);
607 public void EmitAssign (EmitContext ec
)
614 ec
.ig
.Emit (OpCodes
.Starg_S
, (byte) arg_idx
);
616 ec
.ig
.Emit (OpCodes
.Starg
, arg_idx
);
619 public void EmitAddressOf (EmitContext ec
)
626 bool is_ref
= (ModFlags
& Modifier
.ISBYREF
) != 0;
628 ParameterReference
.EmitLdArg (ec
.ig
, arg_idx
);
631 ec
.ig
.Emit (OpCodes
.Ldarga_S
, (byte) arg_idx
);
633 ec
.ig
.Emit (OpCodes
.Ldarga
, arg_idx
);
637 public Expression
ExpressionTreeVariableReference ()
639 return expr_tree_variable
;
643 // System.Linq.Expressions.ParameterExpression type
645 public static TypeExpr
ResolveParameterExpressionType (EmitContext ec
, Location location
)
647 if (parameter_expr_tree_type
!= null)
648 return parameter_expr_tree_type
;
650 Type p_type
= TypeManager
.parameter_expression_type
;
651 if (p_type
== null) {
652 p_type
= TypeManager
.CoreLookupType ("System.Linq.Expressions", "ParameterExpression", Kind
.Class
, true);
653 TypeManager
.parameter_expression_type
= p_type
;
656 parameter_expr_tree_type
= new TypeExpression (p_type
, location
).
657 ResolveAsTypeTerminal (ec
, false);
659 return parameter_expr_tree_type
;
662 public void Warning_UselessOptionalParameter ()
664 Report
.Warning (1066, 1, Location
,
665 "The default value specified for optional parameter `{0}' will never be used",
671 // Imported or resolved parameter information
673 public class ParameterData
: IParameterData
675 readonly string name
;
676 readonly Parameter
.Modifier modifiers
;
677 readonly Expression default_value
;
679 public ParameterData (string name
, Parameter
.Modifier modifiers
)
682 this.modifiers
= modifiers
;
685 public ParameterData (string name
, Parameter
.Modifier modifiers
, Expression defaultValue
)
686 : this (name
, modifiers
)
688 this.default_value
= defaultValue
;
691 #region IParameterData Members
693 public Expression DefaultValue
{
694 get { return default_value; }
697 public bool HasExtensionMethodModifier
{
698 get { return (modifiers & Parameter.Modifier.This) != 0; }
701 public bool HasDefaultValue
{
702 get { return default_value != null; }
705 public Parameter
.Modifier ModFlags
{
706 get { return modifiers & ~Parameter.Modifier.This; }
716 public abstract class AParametersCollection
718 protected bool has_arglist
;
719 protected bool has_params
;
721 // Null object pattern
722 protected IParameterData
[] parameters
;
723 protected Type
[] types
;
726 get { return parameters.Length; }
729 public Type ExtensionMethodType
{
734 return FixedParameters
[0].HasExtensionMethodModifier
?
739 public IParameterData
[] FixedParameters
{
745 public static ParameterAttributes
GetParameterAttribute (Parameter
.Modifier modFlags
)
747 return (modFlags
& Parameter
.Modifier
.OUT
) == Parameter
.Modifier
.OUT
?
748 ParameterAttributes
.Out
: ParameterAttributes
.None
;
751 public Type
[] GetEmitTypes ()
753 Type
[] types
= null;
756 return Type
.EmptyTypes
;
758 types
= new Type
[Count
- 1];
759 Array
.Copy (Types
, types
, types
.Length
);
762 for (int i
= 0; i
< Count
; ++i
) {
763 if ((FixedParameters
[i
].ModFlags
& Parameter
.Modifier
.ISBYREF
) == 0)
767 types
= (Type
[]) Types
.Clone ();
769 types
[i
] = TypeManager
.GetReferenceType (types
[i
]);
779 // Returns the parameter information based on the name
781 public int GetParameterIndexByName (string name
)
783 for (int idx
= 0; idx
< Count
; ++idx
) {
784 if (parameters
[idx
].Name
== name
)
791 public string GetSignatureForError ()
793 StringBuilder sb
= new StringBuilder ("(");
794 for (int i
= 0; i
< Count
; ++i
) {
797 sb
.Append (ParameterDesc (i
));
800 return sb
.ToString ();
803 public bool HasArglist
{
804 get { return has_arglist; }
807 public bool HasExtensionMethodType
{
812 return FixedParameters
[0].HasExtensionMethodModifier
;
816 public bool HasParams
{
817 get { return has_params; }
820 public bool IsEmpty
{
821 get { return parameters.Length == 0; }
824 public string ParameterDesc (int pos
)
826 if (types
== null || types
[pos
] == null)
827 return ((Parameter
)FixedParameters
[pos
]).GetSignatureForError ();
829 string type
= TypeManager
.CSharpName (types
[pos
]);
830 if (FixedParameters
[pos
].HasExtensionMethodModifier
)
831 return "this " + type
;
833 Parameter
.Modifier mod
= FixedParameters
[pos
].ModFlags
;
837 return Parameter
.GetModifierSignature (mod
) + " " + type
;
840 public Type
[] Types
{
841 get { return types; }
842 set { types = value; }
846 public AParametersCollection
InflateTypes (Type
[] genArguments
, Type
[] argTypes
)
848 AParametersCollection p
= (AParametersCollection
) MemberwiseClone (); // Clone ();
850 for (int i
= 0; i
< Count
; ++i
) {
851 if (types
[i
].IsGenericType
) {
852 Type
[] gen_arguments_open
= new Type
[types
[i
].GetGenericTypeDefinition ().GetGenericArguments ().Length
];
853 Type
[] gen_arguments
= types
[i
].GetGenericArguments ();
854 for (int ii
= 0; ii
< gen_arguments_open
.Length
; ++ii
) {
855 if (gen_arguments
[ii
].IsGenericParameter
) {
856 Type t
= argTypes
[gen_arguments
[ii
].GenericParameterPosition
];
857 gen_arguments_open
[ii
] = t
;
859 gen_arguments_open
[ii
] = gen_arguments
[ii
];
862 p
.types
[i
] = types
[i
].GetGenericTypeDefinition ().MakeGenericType (gen_arguments_open
);
866 if (types
[i
].IsGenericParameter
) {
867 Type gen_argument
= argTypes
[types
[i
].GenericParameterPosition
];
868 p
.types
[i
] = gen_argument
;
879 // A collection of imported or resolved parameters
881 public class ParametersImported
: AParametersCollection
883 ParametersImported (AParametersCollection param
, Type
[] types
)
885 this.parameters
= param
.FixedParameters
;
887 has_arglist
= param
.HasArglist
;
888 has_params
= param
.HasParams
;
891 ParametersImported (IParameterData
[] parameters
, Type
[] types
, bool hasArglist
, bool hasParams
)
893 this.parameters
= parameters
;
895 this.has_arglist
= hasArglist
;
896 this.has_params
= hasParams
;
899 public ParametersImported (IParameterData
[] param
, Type
[] types
)
901 this.parameters
= param
;
905 public static AParametersCollection
Create (MethodBase method
)
907 return Create (method
.GetParameters (), method
);
911 // Generic method parameters importer, param is shared between all instances
913 public static AParametersCollection
Create (AParametersCollection param
, MethodBase method
)
918 ParameterInfo
[] pi
= method
.GetParameters ();
919 Type
[] types
= new Type
[pi
.Length
];
920 for (int i
= 0; i
< types
.Length
; i
++) {
921 Type t
= pi
[i
].ParameterType
;
923 t
= TypeManager
.GetElementType (t
);
925 types
[i
] = TypeManager
.TypeToCoreType (t
);
928 return new ParametersImported (param
, types
);
932 // Imports SRE parameters
934 public static AParametersCollection
Create (ParameterInfo
[] pi
, MethodBase method
)
936 int varargs
= method
!= null && (method
.CallingConvention
& CallingConventions
.VarArgs
) != 0 ? 1 : 0;
938 if (pi
.Length
== 0 && varargs
== 0)
939 return ParametersCompiled
.EmptyReadOnlyParameters
;
941 Type
[] types
= new Type
[pi
.Length
+ varargs
];
942 IParameterData
[] par
= new IParameterData
[pi
.Length
+ varargs
];
943 bool is_params
= false;
944 PredefinedAttribute extension_attr
= PredefinedAttributes
.Get
.Extension
;
945 PredefinedAttribute param_attr
= PredefinedAttributes
.Get
.ParamArray
;
946 for (int i
= 0; i
< pi
.Length
; i
++) {
947 types
[i
] = TypeManager
.TypeToCoreType (pi
[i
].ParameterType
);
949 ParameterInfo p
= pi
[i
];
950 Parameter
.Modifier mod
= 0;
951 Expression default_value
= null;
952 if (types
[i
].IsByRef
) {
953 if ((p
.Attributes
& (ParameterAttributes
.Out
| ParameterAttributes
.In
)) == ParameterAttributes
.Out
)
954 mod
= Parameter
.Modifier
.OUT
;
956 mod
= Parameter
.Modifier
.REF
;
959 // Strip reference wrapping
961 types
[i
] = TypeManager
.GetElementType (types
[i
]);
962 } else if (i
== 0 && extension_attr
.IsDefined
&& method
!= null && method
.IsStatic
&&
963 (method
.DeclaringType
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
&&
964 method
.IsDefined (extension_attr
.Type
, false)) {
965 mod
= Parameter
.Modifier
.This
;
967 if (i
>= pi
.Length
- 2 && types
[i
].IsArray
) {
968 if (p
.IsDefined (param_attr
.Type
, false)) {
969 mod
= Parameter
.Modifier
.PARAMS
;
974 if (!is_params
&& p
.IsOptional
) {
975 if (p
.DefaultValue
== Missing
.Value
)
976 default_value
= EmptyExpression
.Null
;
978 default_value
= Constant
.CreateConstant (types
[i
], p
.DefaultValue
, Location
.Null
);
982 par
[i
] = new ParameterData (p
.Name
, mod
, default_value
);
986 par
[par
.Length
- 1] = new ArglistParameter (Location
.Null
);
987 types
[types
.Length
- 1] = InternalType
.Arglist
;
990 return method
!= null ?
991 new ParametersImported (par
, types
, varargs
!= 0, is_params
) :
992 new ParametersImported (par
, types
);
997 /// Represents the methods parameters
999 public class ParametersCompiled
: AParametersCollection
1001 public static readonly ParametersCompiled EmptyReadOnlyParameters
= new ParametersCompiled ();
1003 // Used by C# 2.0 delegates
1004 public static readonly ParametersCompiled Undefined
= new ParametersCompiled ();
1006 private ParametersCompiled ()
1008 parameters
= new Parameter
[0];
1009 types
= Type
.EmptyTypes
;
1012 private ParametersCompiled (Parameter
[] parameters
, Type
[] types
)
1014 this.parameters
= parameters
;
1018 public ParametersCompiled (params Parameter
[] parameters
)
1020 if (parameters
== null)
1021 throw new ArgumentException ("Use EmptyReadOnlyParameters");
1023 this.parameters
= parameters
;
1024 int count
= parameters
.Length
;
1030 has_params
= (parameters
[0].ModFlags
& Parameter
.Modifier
.PARAMS
) != 0;
1034 for (int i
= 0; i
< count
; i
++){
1035 string base_name
= parameters
[i
].Name
;
1036 has_params
|= (parameters
[i
].ModFlags
& Parameter
.Modifier
.PARAMS
) != 0;
1038 for (int j
= i
+ 1; j
< count
; j
++){
1039 if (base_name
!= parameters
[j
].Name
)
1042 ErrorDuplicateName (parameters
[i
]);
1048 public ParametersCompiled (Parameter
[] parameters
, bool has_arglist
) :
1051 this.has_arglist
= has_arglist
;
1054 public static ParametersCompiled
CreateFullyResolved (Parameter p
, Type type
)
1056 return new ParametersCompiled (new Parameter
[] { p }
, new Type
[] { type }
);
1059 public static ParametersCompiled
CreateFullyResolved (Parameter
[] parameters
, Type
[] types
)
1061 return new ParametersCompiled (parameters
, types
);
1064 public static ParametersCompiled
MergeGenerated (ParametersCompiled userParams
, bool checkConflicts
, Parameter compilerParams
, Type compilerTypes
)
1066 return MergeGenerated (userParams
, checkConflicts
,
1067 new Parameter
[] { compilerParams }
,
1068 new Type
[] { compilerTypes }
);
1072 // Use this method when you merge compiler generated parameters with user parameters
1074 public static ParametersCompiled
MergeGenerated (ParametersCompiled userParams
, bool checkConflicts
, Parameter
[] compilerParams
, Type
[] compilerTypes
)
1076 Parameter
[] all_params
= new Parameter
[userParams
.Count
+ compilerParams
.Length
];
1077 userParams
.FixedParameters
.CopyTo(all_params
, 0);
1080 if (userParams
.types
!= null) {
1081 all_types
= new Type
[all_params
.Length
];
1082 userParams
.Types
.CopyTo (all_types
, 0);
1087 int last_filled
= userParams
.Count
;
1089 foreach (Parameter p
in compilerParams
) {
1090 for (int i
= 0; i
< last_filled
; ++i
) {
1091 while (p
.Name
== all_params
[i
].Name
) {
1092 if (checkConflicts
&& i
< userParams
.Count
) {
1093 Report
.Error (316, userParams
[i
].Location
,
1094 "The parameter name `{0}' conflicts with a compiler generated name", p
.Name
);
1096 p
.Name
= '_' + p
.Name
;
1099 all_params
[last_filled
] = p
;
1100 if (all_types
!= null)
1101 all_types
[last_filled
] = compilerTypes
[index
++];
1105 ParametersCompiled parameters
= new ParametersCompiled (all_params
, all_types
);
1106 parameters
.has_params
= userParams
.has_params
;
1110 protected virtual void ErrorDuplicateName (Parameter p
)
1112 Report
.Error (100, p
.Location
, "The parameter name `{0}' is a duplicate", p
.Name
);
1115 public bool Resolve (IResolveContext ec
)
1120 types
= new Type
[Count
];
1124 for (int i
= 0; i
< FixedParameters
.Length
; ++i
) {
1126 Type t
= p
.Resolve (ec
);
1138 public void ResolveVariable ()
1140 for (int i
= 0; i
< FixedParameters
.Length
; ++i
) {
1141 this [i
].ResolveVariable (i
);
1145 public CallingConventions CallingConvention
1149 return CallingConventions
.VarArgs
;
1151 return CallingConventions
.Standard
;
1155 // Define each type attribute (in/out/ref) and
1156 // the argument names.
1157 public void ApplyAttributes (MethodBase builder
)
1162 MethodBuilder mb
= builder
as MethodBuilder
;
1163 ConstructorBuilder cb
= builder
as ConstructorBuilder
;
1165 for (int i
= 0; i
< Count
; i
++) {
1166 this [i
].ApplyAttributes (mb
, cb
, i
+ 1);
1170 public void VerifyClsCompliance ()
1172 foreach (Parameter p
in FixedParameters
)
1173 p
.IsClsCompliant ();
1176 public Parameter
this [int pos
] {
1177 get { return (Parameter) parameters [pos]; }
1180 public Expression
CreateExpressionTree (EmitContext ec
, Location loc
)
1182 ArrayList initializers
= new ArrayList (Count
);
1183 foreach (Parameter p
in FixedParameters
) {
1185 // Each parameter expression is stored to local variable
1186 // to save some memory when referenced later.
1188 StatementExpression se
= new StatementExpression (p
.CreateExpressionTreeVariable (ec
));
1189 if (se
.Resolve (ec
))
1190 ec
.CurrentBlock
.AddScopeStatement (se
);
1192 initializers
.Add (p
.ExpressionTreeVariableReference ());
1195 return new ArrayCreation (
1196 Parameter
.ResolveParameterExpressionType (ec
, loc
),
1197 "[]", initializers
, loc
);
1200 public ParametersCompiled
Clone ()
1202 ParametersCompiled p
= (ParametersCompiled
) MemberwiseClone ();
1204 p
.parameters
= new IParameterData
[parameters
.Length
];
1205 for (int i
= 0; i
< Count
; ++i
)
1206 p
.parameters
[i
] = this [i
].Clone ();