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 public override void ApplyAttributeBuilder (Attribute a
, CustomAttributeBuilder cb
, PredefinedAttributes pa
)
31 if (a
.Type
== pa
.MarshalAs
) {
32 UnmanagedMarshal marshal
= a
.GetMarshal (this);
33 if (marshal
!= null) {
34 builder
.SetMarshal (marshal
);
39 if (a
.HasSecurityAttribute
) {
40 a
.Error_InvalidSecurityParent ();
44 if (a
.Type
== pa
.Dynamic
) {
45 a
.Error_MisusedDynamicAttribute ();
49 builder
.SetCustomAttribute (cb
);
52 public ParameterBuilder Builder
{
58 public override bool IsClsComplianceRequired()
65 /// Class for applying custom attributes on the return type
67 public class ReturnParameter
: ParameterBase
{
68 public ReturnParameter (MethodBuilder mb
, Location location
)
71 builder
= mb
.DefineParameter (0, ParameterAttributes
.None
, "");
73 catch (ArgumentOutOfRangeException
) {
74 RootContext
.ToplevelTypes
.Compiler
.Report
.RuntimeMissingSupport (location
, "custom attributes on the return type");
78 public override void ApplyAttributeBuilder (Attribute a
, CustomAttributeBuilder cb
, PredefinedAttributes pa
)
80 if (a
.Type
== pa
.CLSCompliant
) {
81 RootContext
.ToplevelTypes
.Compiler
.Report
.Warning (3023, 1, a
.Location
,
82 "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
85 // This occurs after Warning -28
89 base.ApplyAttributeBuilder (a
, cb
, pa
);
92 public override AttributeTargets AttributeTargets
{
94 return AttributeTargets
.ReturnValue
;
101 public override string[] ValidAttributeTargets
{
109 /// Class for applying custom attributes on the implicit parameter type
110 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
113 // TODO: should use more code from Parameter.ApplyAttributeBuilder
114 public class ImplicitParameter
: ParameterBase
{
115 public ImplicitParameter (MethodBuilder mb
)
117 builder
= mb
.DefineParameter (1, ParameterAttributes
.None
, "value");
120 public override AttributeTargets AttributeTargets
{
122 return AttributeTargets
.Parameter
;
129 public override string[] ValidAttributeTargets
{
136 public class ImplicitLambdaParameter
: Parameter
138 public ImplicitLambdaParameter (string name
, Location loc
)
139 : base (null, name
, Modifier
.NONE
, null, loc
)
143 public override Type
Resolve (IMemberContext ec
)
145 if (parameter_type
== null)
146 throw new InternalErrorException ("A type of implicit lambda parameter `{0}' is not set",
149 return parameter_type
;
153 set { parameter_type = value; }
157 public class ParamsParameter
: Parameter
{
158 public ParamsParameter (FullNamedExpression type
, string name
, Attributes attrs
, Location loc
):
159 base (type
, name
, Parameter
.Modifier
.PARAMS
, attrs
, loc
)
163 public override Type
Resolve (IMemberContext ec
)
165 if (base.Resolve (ec
) == null)
168 if (!parameter_type
.IsArray
|| parameter_type
.GetArrayRank () != 1) {
169 ec
.Compiler
.Report
.Error (225, Location
, "The params parameter must be a single dimensional array");
173 return parameter_type
;
176 public override void ApplyAttributes (MethodBuilder mb
, ConstructorBuilder cb
, int index
)
178 base.ApplyAttributes (mb
, cb
, index
);
179 PredefinedAttributes
.Get
.ParamArray
.EmitAttribute (builder
);
183 public class ArglistParameter
: Parameter
{
184 // Doesn't have proper type because it's never chosen for better conversion
185 public ArglistParameter (Location loc
) :
186 base (null, String
.Empty
, Parameter
.Modifier
.NONE
, null, loc
)
190 public override void ApplyAttributes (MethodBuilder mb
, ConstructorBuilder cb
, int index
)
195 public override bool CheckAccessibility (InterfaceMemberBase member
)
200 public override Type
Resolve (IMemberContext ec
)
202 return InternalType
.Arglist
;
205 public override string GetSignatureForError ()
211 public interface IParameterData
213 Expression DefaultValue { get; }
214 bool HasExtensionMethodModifier { get; }
215 bool HasDefaultValue { get; }
216 Parameter
.Modifier ModFlags { get; }
221 // Parameter information created by parser
223 public class Parameter
: ParameterBase
, IParameterData
, ILocalVariable
{
225 public enum Modifier
: byte {
227 REF
= REFMASK
| ISBYREF
,
228 OUT
= OUTMASK
| ISBYREF
,
230 // This is a flag which says that it's either REF or OUT.
237 static string[] attribute_targets
= new string [] { "param" }
;
239 protected FullNamedExpression TypeName
;
240 readonly Modifier modFlags
;
242 Expression default_expr
;
243 protected Type parameter_type
;
244 public readonly Location Location
;
246 public bool HasAddressTaken
;
248 LocalVariableReference expr_tree_variable
;
249 static TypeExpr parameter_expr_tree_type
;
251 HoistedVariable hoisted_variant
;
253 public Parameter (FullNamedExpression type
, string name
, Modifier mod
, Attributes attrs
, Location loc
)
260 // Only assign, attributes will be attached during resolve
261 base.attributes
= attrs
;
264 public override void ApplyAttributeBuilder (Attribute a
, CustomAttributeBuilder cb
, PredefinedAttributes pa
)
266 Report Report
= RootContext
.ToplevelTypes
.Compiler
.Report
;
268 if (a
.Type
== pa
.In
&& ModFlags
== Modifier
.OUT
) {
269 Report
.Error (36, a
.Location
, "An out parameter cannot have the `In' attribute");
273 if (a
.Type
== pa
.ParamArray
) {
274 Report
.Error (674, a
.Location
, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
278 if (a
.Type
== PredefinedAttributes
.Get
.Out
&& (ModFlags
& Modifier
.REF
) == Modifier
.REF
&&
279 !OptAttributes
.Contains (pa
.In
)) {
280 Report
.Error (662, a
.Location
,
281 "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
285 if (a
.Type
== pa
.CLSCompliant
) {
286 Report
.Warning (3022, 1, a
.Location
, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
289 if (HasDefaultValue
&& (a
.Type
== pa
.DefaultParameterValue
|| a
.Type
== pa
.OptionalParameter
)) {
290 Report
.Error (1745, a
.Location
,
291 "Cannot specify `{0}' attribute on optional parameter `{1}'",
292 TypeManager
.CSharpName (a
.Type
).Replace ("Attribute", ""), Name
);
296 if (a
.Type
== pa
.DefaultParameterValue
) {
297 object val
= a
.GetParameterDefaultValue ();
299 Type t
= val
.GetType ();
300 if (t
.IsArray
|| TypeManager
.IsSubclassOf (t
, TypeManager
.type_type
)) {
301 if (parameter_type
== TypeManager
.object_type
) {
303 t
= TypeManager
.type_type
;
305 Report
.Error (1910, a
.Location
, "Argument of type `{0}' is not applicable for the DefaultParameterValue attribute",
306 TypeManager
.CSharpName (t
));
308 Report
.Error (1909, a
.Location
, "The DefaultParameterValue attribute is not applicable on parameters of type `{0}'",
309 TypeManager
.CSharpName (parameter_type
)); ;
315 if (parameter_type
== TypeManager
.object_type
||
316 (val
== null && !TypeManager
.IsGenericParameter (parameter_type
) && TypeManager
.IsReferenceType (parameter_type
)) ||
317 (val
!= null && TypeManager
.TypeToCoreType (val
.GetType ()) == parameter_type
))
318 builder
.SetConstant (val
);
320 Report
.Error (1908, a
.Location
, "The type of the default value should match the type of the parameter");
324 base.ApplyAttributeBuilder (a
, cb
, pa
);
327 public virtual bool CheckAccessibility (InterfaceMemberBase member
)
329 if (parameter_type
== null || TypeManager
.IsGenericParameter (parameter_type
))
332 return member
.IsAccessibleAs (parameter_type
);
336 // Resolve is used in method definitions
338 public virtual Type
Resolve (IMemberContext rc
)
340 if (parameter_type
!= null)
341 return parameter_type
;
343 if (attributes
!= null)
344 attributes
.AttachTo (this, rc
);
346 TypeExpr texpr
= TypeName
.ResolveAsTypeTerminal (rc
, false);
350 parameter_type
= texpr
.Type
;
352 // Ignore all checks for dummy members
353 AbstractPropertyEventMethod pem
= rc
as AbstractPropertyEventMethod
;
354 if (pem
!= null && pem
.IsDummy
)
355 return parameter_type
;
357 if (default_expr
!= null)
358 default_expr
= ResolveDefaultValue (new ResolveContext (rc
));
360 if ((modFlags
& Parameter
.Modifier
.ISBYREF
) != 0 &&
361 TypeManager
.IsSpecialType (parameter_type
)) {
362 rc
.Compiler
.Report
.Error (1601, Location
, "Method or delegate parameter cannot be of type `{0}'",
363 GetSignatureForError ());
367 TypeManager
.CheckTypeVariance (parameter_type
,
368 (modFlags
& Parameter
.Modifier
.ISBYREF
) != 0 ? Variance
.None
: Variance
.Contravariant
,
371 if (TypeManager
.IsGenericParameter (parameter_type
))
372 return parameter_type
;
374 if ((parameter_type
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
) {
375 rc
.Compiler
.Report
.Error (721, Location
, "`{0}': static types cannot be used as parameters",
376 texpr
.GetSignatureForError ());
377 return parameter_type
;
380 if ((modFlags
& Modifier
.This
) != 0 && (parameter_type
.IsPointer
|| TypeManager
.IsDynamicType (parameter_type
))) {
381 rc
.Compiler
.Report
.Error (1103, Location
, "The extension method cannot be of type `{0}'",
382 TypeManager
.CSharpName (parameter_type
));
385 return parameter_type
;
388 Expression
ResolveDefaultValue (ResolveContext rc
)
390 default_expr
= default_expr
.Resolve (rc
);
391 if (default_expr
== null)
394 if (!(default_expr
is Constant
|| default_expr
is DefaultValueExpression
)) {
395 if (TypeManager
.IsNullableType (parameter_type
)) {
396 rc
.Compiler
.Report
.Error (1770, default_expr
.Location
,
397 "The expression being assigned to nullable optional parameter `{0}' must be default value",
400 rc
.Compiler
.Report
.Error (1736, default_expr
.Location
,
401 "The expression being assigned to optional parameter `{0}' must be a constant or default value",
408 if (TypeManager
.IsEqual (default_expr
.Type
, parameter_type
))
411 if (TypeManager
.IsNullableType (parameter_type
)) {
412 if (Convert
.ImplicitNulableConversion (rc
, default_expr
, parameter_type
) != null)
415 var res
= Convert
.ImplicitConversionStandard (rc
, default_expr
, parameter_type
, default_expr
.Location
);
417 if (!default_expr
.IsNull
&& TypeManager
.IsReferenceType (parameter_type
) && parameter_type
!= TypeManager
.string_type
) {
418 rc
.Compiler
.Report
.Error (1763, default_expr
.Location
,
419 "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
420 Name
, GetSignatureForError ());
429 rc
.Compiler
.Report
.Error (1750, Location
,
430 "Optional parameter expression of type `{0}' cannot be converted to parameter type `{1}'",
431 TypeManager
.CSharpName (default_expr
.Type
), GetSignatureForError ());
436 public void ResolveVariable (int idx
)
441 public bool HasDefaultValue
{
442 get { return default_expr != null; }
445 public bool HasExtensionMethodModifier
{
446 get { return (modFlags & Modifier.This) != 0; }
450 // Hoisted parameter variant
452 public HoistedVariable HoistedVariant
{
454 return hoisted_variant
;
457 hoisted_variant
= value;
461 public Modifier ModFlags
{
462 get { return modFlags & ~Modifier.This; }
467 set { name = value; }
470 ParameterAttributes Attributes
{
471 get { return ParametersCompiled
.GetParameterAttribute (modFlags
) |
472 (HasDefaultValue
? ParameterAttributes
.Optional
: ParameterAttributes
.None
); }
475 public override AttributeTargets AttributeTargets
{
477 return AttributeTargets
.Parameter
;
481 public virtual string GetSignatureForError ()
484 if (parameter_type
!= null)
485 type_name
= TypeManager
.CSharpName (parameter_type
);
487 type_name
= TypeName
.GetSignatureForError ();
489 string mod
= GetModifierSignature (modFlags
);
491 return String
.Concat (mod
, " ", type_name
);
496 public static string GetModifierSignature (Modifier mod
)
501 case Modifier
.PARAMS
:
512 public void IsClsCompliant ()
514 if (AttributeTester
.IsClsCompliant (parameter_type
))
517 RootContext
.ToplevelTypes
.Compiler
.Report
.Warning (3001, 1, Location
, "Argument type `{0}' is not CLS-compliant", GetSignatureForError ());
520 public virtual void ApplyAttributes (MethodBuilder mb
, ConstructorBuilder cb
, int index
)
523 builder
= cb
.DefineParameter (index
, Attributes
, Name
);
525 builder
= mb
.DefineParameter (index
, Attributes
, Name
);
527 if (OptAttributes
!= null)
528 OptAttributes
.Emit ();
530 if (HasDefaultValue
) {
532 // Emit constant values for true constants only, the other
533 // constant-like expressions will rely on default value expression
535 Constant c
= default_expr
as Constant
;
537 if (default_expr
.Type
== TypeManager
.decimal_type
) {
538 builder
.SetCustomAttribute (Const
.CreateDecimalConstantAttribute (c
));
540 builder
.SetConstant (c
.GetTypedValue ());
545 if (TypeManager
.IsDynamicType (parameter_type
))
546 PredefinedAttributes
.Get
.Dynamic
.EmitAttribute (builder
);
549 public override string[] ValidAttributeTargets
{
551 return attribute_targets
;
555 public Parameter
Clone ()
557 Parameter p
= (Parameter
) MemberwiseClone ();
558 if (attributes
!= null)
559 p
.attributes
= attributes
.Clone ();
564 public ExpressionStatement
CreateExpressionTreeVariable (BlockContext ec
)
566 if ((modFlags
& Modifier
.ISBYREF
) != 0)
567 ec
.Report
.Error (1951, Location
, "An expression tree parameter cannot use `ref' or `out' modifier");
569 LocalInfo variable
= ec
.CurrentBlock
.AddTemporaryVariable (
570 ResolveParameterExpressionType (ec
, Location
), Location
);
571 variable
.Resolve (ec
);
573 expr_tree_variable
= new LocalVariableReference (
574 ec
.CurrentBlock
, variable
.Name
, Location
, variable
, false);
576 Arguments arguments
= new Arguments (2);
577 arguments
.Add (new Argument (new TypeOf (
578 new TypeExpression (parameter_type
, Location
), Location
)));
579 arguments
.Add (new Argument (new StringConstant (Name
, Location
)));
580 return new SimpleAssign (ExpressionTreeVariableReference (),
581 Expression
.CreateExpressionFactoryCall (ec
, "Parameter", null, arguments
, Location
));
584 public Expression DefaultValue
{
585 get { return default_expr; }
586 set { default_expr = value; }
589 public void Emit (EmitContext ec
)
595 ParameterReference
.EmitLdArg (ec
.ig
, arg_idx
);
598 public void EmitAssign (EmitContext ec
)
605 ec
.ig
.Emit (OpCodes
.Starg_S
, (byte) arg_idx
);
607 ec
.ig
.Emit (OpCodes
.Starg
, arg_idx
);
610 public void EmitAddressOf (EmitContext ec
)
617 bool is_ref
= (ModFlags
& Modifier
.ISBYREF
) != 0;
619 ParameterReference
.EmitLdArg (ec
.ig
, arg_idx
);
622 ec
.ig
.Emit (OpCodes
.Ldarga_S
, (byte) arg_idx
);
624 ec
.ig
.Emit (OpCodes
.Ldarga
, arg_idx
);
628 public Expression
ExpressionTreeVariableReference ()
630 return expr_tree_variable
;
634 // System.Linq.Expressions.ParameterExpression type
636 public static TypeExpr
ResolveParameterExpressionType (IMemberContext ec
, Location location
)
638 if (parameter_expr_tree_type
!= null)
639 return parameter_expr_tree_type
;
641 Type p_type
= TypeManager
.parameter_expression_type
;
642 if (p_type
== null) {
643 p_type
= TypeManager
.CoreLookupType (ec
.Compiler
, "System.Linq.Expressions", "ParameterExpression", Kind
.Class
, true);
644 TypeManager
.parameter_expression_type
= p_type
;
647 parameter_expr_tree_type
= new TypeExpression (p_type
, location
).
648 ResolveAsTypeTerminal (ec
, false);
650 return parameter_expr_tree_type
;
653 public void Warning_UselessOptionalParameter (Report Report
)
655 Report
.Warning (1066, 1, Location
,
656 "The default value specified for optional parameter `{0}' will never be used",
662 // Imported or resolved parameter information
664 public class ParameterData
: IParameterData
666 readonly string name
;
667 readonly Parameter
.Modifier modifiers
;
668 readonly Expression default_value
;
670 public ParameterData (string name
, Parameter
.Modifier modifiers
)
673 this.modifiers
= modifiers
;
676 public ParameterData (string name
, Parameter
.Modifier modifiers
, Expression defaultValue
)
677 : this (name
, modifiers
)
679 this.default_value
= defaultValue
;
682 #region IParameterData Members
684 public Expression DefaultValue
{
685 get { return default_value; }
688 public bool HasExtensionMethodModifier
{
689 get { return (modifiers & Parameter.Modifier.This) != 0; }
692 public bool HasDefaultValue
{
693 get { return default_value != null; }
696 public Parameter
.Modifier ModFlags
{
697 get { return modifiers & ~Parameter.Modifier.This; }
707 public abstract class AParametersCollection
709 protected bool has_arglist
;
710 protected bool has_params
;
712 // Null object pattern
713 protected IParameterData
[] parameters
;
714 protected Type
[] types
;
717 get { return parameters.Length; }
720 public Type ExtensionMethodType
{
725 return FixedParameters
[0].HasExtensionMethodModifier
?
730 public IParameterData
[] FixedParameters
{
736 public static ParameterAttributes
GetParameterAttribute (Parameter
.Modifier modFlags
)
738 return (modFlags
& Parameter
.Modifier
.OUT
) == Parameter
.Modifier
.OUT
?
739 ParameterAttributes
.Out
: ParameterAttributes
.None
;
742 public Type
[] GetEmitTypes ()
744 Type
[] types
= null;
747 return Type
.EmptyTypes
;
749 types
= new Type
[Count
- 1];
750 Array
.Copy (Types
, types
, types
.Length
);
753 for (int i
= 0; i
< Count
; ++i
) {
754 if ((FixedParameters
[i
].ModFlags
& Parameter
.Modifier
.ISBYREF
) == 0)
758 types
= (Type
[]) Types
.Clone ();
760 types
[i
] = TypeManager
.GetReferenceType (types
[i
]);
770 // Returns the parameter information based on the name
772 public int GetParameterIndexByName (string name
)
774 for (int idx
= 0; idx
< Count
; ++idx
) {
775 if (parameters
[idx
].Name
== name
)
782 public string GetSignatureForError ()
784 StringBuilder sb
= new StringBuilder ("(");
785 for (int i
= 0; i
< Count
; ++i
) {
788 sb
.Append (ParameterDesc (i
));
791 return sb
.ToString ();
794 public bool HasArglist
{
795 get { return has_arglist; }
798 public bool HasExtensionMethodType
{
803 return FixedParameters
[0].HasExtensionMethodModifier
;
807 public bool HasParams
{
808 get { return has_params; }
811 public bool IsEmpty
{
812 get { return parameters.Length == 0; }
815 public string ParameterDesc (int pos
)
817 if (types
== null || types
[pos
] == null)
818 return ((Parameter
)FixedParameters
[pos
]).GetSignatureForError ();
820 string type
= TypeManager
.CSharpName (types
[pos
]);
821 if (FixedParameters
[pos
].HasExtensionMethodModifier
)
822 return "this " + type
;
824 Parameter
.Modifier mod
= FixedParameters
[pos
].ModFlags
;
828 return Parameter
.GetModifierSignature (mod
) + " " + type
;
831 public Type
[] Types
{
832 get { return types; }
833 set { types = value; }
837 public AParametersCollection
InflateTypes (Type
[] genArguments
, Type
[] argTypes
)
839 AParametersCollection p
= (AParametersCollection
) MemberwiseClone (); // Clone ();
841 for (int i
= 0; i
< Count
; ++i
) {
842 if (types
[i
].IsGenericType
) {
843 Type
[] gen_arguments_open
= new Type
[types
[i
].GetGenericTypeDefinition ().GetGenericArguments ().Length
];
844 Type
[] gen_arguments
= types
[i
].GetGenericArguments ();
845 for (int ii
= 0; ii
< gen_arguments_open
.Length
; ++ii
) {
846 if (gen_arguments
[ii
].IsGenericParameter
) {
847 Type t
= argTypes
[gen_arguments
[ii
].GenericParameterPosition
];
848 gen_arguments_open
[ii
] = t
;
850 gen_arguments_open
[ii
] = gen_arguments
[ii
];
853 p
.types
[i
] = types
[i
].GetGenericTypeDefinition ().MakeGenericType (gen_arguments_open
);
857 if (types
[i
].IsGenericParameter
) {
858 Type gen_argument
= argTypes
[types
[i
].GenericParameterPosition
];
859 p
.types
[i
] = gen_argument
;
870 // A collection of imported or resolved parameters
872 public class ParametersImported
: AParametersCollection
874 ParametersImported (AParametersCollection param
, Type
[] types
)
876 this.parameters
= param
.FixedParameters
;
878 has_arglist
= param
.HasArglist
;
879 has_params
= param
.HasParams
;
882 ParametersImported (IParameterData
[] parameters
, Type
[] types
, bool hasArglist
, bool hasParams
)
884 this.parameters
= parameters
;
886 this.has_arglist
= hasArglist
;
887 this.has_params
= hasParams
;
890 public ParametersImported (IParameterData
[] param
, Type
[] types
)
892 this.parameters
= param
;
896 public static AParametersCollection
Create (MethodBase method
)
898 return Create (method
.GetParameters (), method
);
902 // Generic method parameters importer, param is shared between all instances
904 public static AParametersCollection
Create (AParametersCollection param
, MethodBase method
)
909 ParameterInfo
[] pi
= method
.GetParameters ();
910 Type
[] types
= new Type
[pi
.Length
];
911 for (int i
= 0; i
< types
.Length
; i
++) {
912 Type t
= pi
[i
].ParameterType
;
914 t
= TypeManager
.GetElementType (t
);
916 types
[i
] = TypeManager
.TypeToCoreType (t
);
919 return new ParametersImported (param
, types
);
923 // Imports SRE parameters
925 public static AParametersCollection
Create (ParameterInfo
[] pi
, MethodBase method
)
927 int varargs
= method
!= null && (method
.CallingConvention
& CallingConventions
.VarArgs
) != 0 ? 1 : 0;
929 if (pi
.Length
== 0 && varargs
== 0)
930 return ParametersCompiled
.EmptyReadOnlyParameters
;
932 Type
[] types
= new Type
[pi
.Length
+ varargs
];
933 IParameterData
[] par
= new IParameterData
[pi
.Length
+ varargs
];
934 bool is_params
= false;
935 PredefinedAttribute extension_attr
= PredefinedAttributes
.Get
.Extension
;
936 PredefinedAttribute param_attr
= PredefinedAttributes
.Get
.ParamArray
;
937 for (int i
= 0; i
< pi
.Length
; i
++) {
938 types
[i
] = TypeManager
.TypeToCoreType (pi
[i
].ParameterType
);
940 ParameterInfo p
= pi
[i
];
941 Parameter
.Modifier mod
= 0;
942 Expression default_value
= null;
943 if (types
[i
].IsByRef
) {
944 if ((p
.Attributes
& (ParameterAttributes
.Out
| ParameterAttributes
.In
)) == ParameterAttributes
.Out
)
945 mod
= Parameter
.Modifier
.OUT
;
947 mod
= Parameter
.Modifier
.REF
;
950 // Strip reference wrapping
952 types
[i
] = TypeManager
.GetElementType (types
[i
]);
953 } else if (i
== 0 && extension_attr
.IsDefined
&& method
!= null && method
.IsStatic
&&
954 (method
.DeclaringType
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
&&
955 method
.IsDefined (extension_attr
.Type
, false)) {
956 mod
= Parameter
.Modifier
.This
;
958 if (i
>= pi
.Length
- 2 && types
[i
].IsArray
) {
959 if (p
.IsDefined (param_attr
.Type
, false)) {
960 mod
= Parameter
.Modifier
.PARAMS
;
965 if (!is_params
&& p
.IsOptional
) {
966 object value = p
.DefaultValue
;
967 if (value == Missing
.Value
) {
968 default_value
= EmptyExpression
.Null
;
969 } else if (value == null) {
970 default_value
= new NullLiteral (Location
.Null
);
972 default_value
= Constant
.CreateConstant (value.GetType (), value, Location
.Null
);
977 par
[i
] = new ParameterData (p
.Name
, mod
, default_value
);
981 par
[par
.Length
- 1] = new ArglistParameter (Location
.Null
);
982 types
[types
.Length
- 1] = InternalType
.Arglist
;
985 return method
!= null ?
986 new ParametersImported (par
, types
, varargs
!= 0, is_params
) :
987 new ParametersImported (par
, types
);
992 /// Represents the methods parameters
994 public class ParametersCompiled
: AParametersCollection
996 public static readonly ParametersCompiled EmptyReadOnlyParameters
= new ParametersCompiled ();
998 // Used by C# 2.0 delegates
999 public static readonly ParametersCompiled Undefined
= new ParametersCompiled ();
1001 private ParametersCompiled ()
1003 parameters
= new Parameter
[0];
1004 types
= Type
.EmptyTypes
;
1007 private ParametersCompiled (Parameter
[] parameters
, Type
[] types
)
1009 this.parameters
= parameters
;
1013 public ParametersCompiled (params Parameter
[] parameters
)
1015 if (parameters
== null)
1016 throw new ArgumentException ("Use EmptyReadOnlyParameters");
1018 this.parameters
= parameters
;
1019 int count
= parameters
.Length
;
1025 has_params
= (parameters
[0].ModFlags
& Parameter
.Modifier
.PARAMS
) != 0;
1029 for (int i
= 0; i
< count
; i
++){
1030 string base_name
= parameters
[i
].Name
;
1031 has_params
|= (parameters
[i
].ModFlags
& Parameter
.Modifier
.PARAMS
) != 0;
1033 for (int j
= i
+ 1; j
< count
; j
++){
1034 if (base_name
!= parameters
[j
].Name
)
1037 ErrorDuplicateName (parameters
[i
], RootContext
.ToplevelTypes
.Compiler
.Report
);
1043 public ParametersCompiled (Parameter
[] parameters
, bool has_arglist
) :
1046 this.has_arglist
= has_arglist
;
1049 public static ParametersCompiled
CreateFullyResolved (Parameter p
, Type type
)
1051 return new ParametersCompiled (new Parameter
[] { p }
, new Type
[] { type }
);
1054 public static ParametersCompiled
CreateFullyResolved (Parameter
[] parameters
, Type
[] types
)
1056 return new ParametersCompiled (parameters
, types
);
1059 public static ParametersCompiled
MergeGenerated (ParametersCompiled userParams
, bool checkConflicts
, Parameter compilerParams
, Type compilerTypes
)
1061 return MergeGenerated (userParams
, checkConflicts
,
1062 new Parameter
[] { compilerParams }
,
1063 new Type
[] { compilerTypes }
);
1067 // Use this method when you merge compiler generated parameters with user parameters
1069 public static ParametersCompiled
MergeGenerated (ParametersCompiled userParams
, bool checkConflicts
, Parameter
[] compilerParams
, Type
[] compilerTypes
)
1071 Parameter
[] all_params
= new Parameter
[userParams
.Count
+ compilerParams
.Length
];
1072 userParams
.FixedParameters
.CopyTo(all_params
, 0);
1075 if (userParams
.types
!= null) {
1076 all_types
= new Type
[all_params
.Length
];
1077 userParams
.Types
.CopyTo (all_types
, 0);
1082 int last_filled
= userParams
.Count
;
1084 foreach (Parameter p
in compilerParams
) {
1085 for (int i
= 0; i
< last_filled
; ++i
) {
1086 while (p
.Name
== all_params
[i
].Name
) {
1087 if (checkConflicts
&& i
< userParams
.Count
) {
1088 RootContext
.ToplevelTypes
.Compiler
.Report
.Error (316, userParams
[i
].Location
,
1089 "The parameter name `{0}' conflicts with a compiler generated name", p
.Name
);
1091 p
.Name
= '_' + p
.Name
;
1094 all_params
[last_filled
] = p
;
1095 if (all_types
!= null)
1096 all_types
[last_filled
] = compilerTypes
[index
++];
1100 ParametersCompiled parameters
= new ParametersCompiled (all_params
, all_types
);
1101 parameters
.has_params
= userParams
.has_params
;
1105 protected virtual void ErrorDuplicateName (Parameter p
, Report Report
)
1107 Report
.Error (100, p
.Location
, "The parameter name `{0}' is a duplicate", p
.Name
);
1110 public bool Resolve (IMemberContext ec
)
1115 types
= new Type
[Count
];
1119 for (int i
= 0; i
< FixedParameters
.Length
; ++i
) {
1121 Type t
= p
.Resolve (ec
);
1133 public void ResolveVariable ()
1135 for (int i
= 0; i
< FixedParameters
.Length
; ++i
) {
1136 this [i
].ResolveVariable (i
);
1140 public CallingConventions CallingConvention
1144 return CallingConventions
.VarArgs
;
1146 return CallingConventions
.Standard
;
1150 // Define each type attribute (in/out/ref) and
1151 // the argument names.
1152 public void ApplyAttributes (MethodBase builder
)
1157 MethodBuilder mb
= builder
as MethodBuilder
;
1158 ConstructorBuilder cb
= builder
as ConstructorBuilder
;
1160 for (int i
= 0; i
< Count
; i
++) {
1161 this [i
].ApplyAttributes (mb
, cb
, i
+ 1);
1165 public void VerifyClsCompliance ()
1167 foreach (Parameter p
in FixedParameters
)
1168 p
.IsClsCompliant ();
1171 public Parameter
this [int pos
] {
1172 get { return (Parameter) parameters [pos]; }
1175 public Expression
CreateExpressionTree (BlockContext ec
, Location loc
)
1177 ArrayList initializers
= new ArrayList (Count
);
1178 foreach (Parameter p
in FixedParameters
) {
1180 // Each parameter expression is stored to local variable
1181 // to save some memory when referenced later.
1183 StatementExpression se
= new StatementExpression (p
.CreateExpressionTreeVariable (ec
));
1184 if (se
.Resolve (ec
))
1185 ec
.CurrentBlock
.AddScopeStatement (se
);
1187 initializers
.Add (p
.ExpressionTreeVariableReference ());
1190 return new ArrayCreation (
1191 Parameter
.ResolveParameterExpressionType (ec
, loc
),
1192 "[]", initializers
, loc
);
1195 public ParametersCompiled
Clone ()
1197 ParametersCompiled p
= (ParametersCompiled
) MemberwiseClone ();
1199 p
.parameters
= new IParameterData
[parameters
.Length
];
1200 for (int i
= 0; i
< Count
; ++i
)
1201 p
.parameters
[i
] = this [i
].Clone ();