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;
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, MethodSpec ctor, byte[] cdata, 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 ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
52 public ParameterBuilder Builder {
58 public override bool IsClsComplianceRequired()
65 /// Class for applying custom attributes on the return type
67 public class ReturnParameter : ParameterBase
71 // TODO: merge method and mb
72 public ReturnParameter (MemberCore method, MethodBuilder mb, Location location)
76 builder = mb.DefineParameter (0, ParameterAttributes.None, "");
78 catch (ArgumentOutOfRangeException) {
79 method.Compiler.Report.RuntimeMissingSupport (location, "custom attributes on the return type");
83 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
85 if (a.Type == pa.CLSCompliant) {
86 method.Compiler.Report.Warning (3023, 1, a.Location,
87 "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
90 // This occurs after Warning -28
94 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
97 public override AttributeTargets AttributeTargets {
99 return AttributeTargets.ReturnValue;
106 public override string[] ValidAttributeTargets {
114 /// Class for applying custom attributes on the implicit parameter type
115 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
118 // TODO: should use more code from Parameter.ApplyAttributeBuilder
119 public class ImplicitParameter : ParameterBase {
120 public ImplicitParameter (MethodBuilder mb)
122 builder = mb.DefineParameter (1, ParameterAttributes.None, "value");
125 public override AttributeTargets AttributeTargets {
127 return AttributeTargets.Parameter;
134 public override string[] ValidAttributeTargets {
141 public class ImplicitLambdaParameter : Parameter
143 public ImplicitLambdaParameter (string name, Location loc)
144 : base (null, name, Modifier.NONE, null, loc)
148 public override TypeSpec Resolve (IMemberContext ec, int index)
150 if (parameter_type == null)
151 throw new InternalErrorException ("A type of implicit lambda parameter `{0}' is not set",
155 return parameter_type;
158 public TypeSpec Type {
159 set { parameter_type = value; }
163 public class ParamsParameter : Parameter {
164 public ParamsParameter (FullNamedExpression type, string name, Attributes attrs, Location loc):
165 base (type, name, Parameter.Modifier.PARAMS, attrs, loc)
169 public override TypeSpec Resolve (IMemberContext ec, int index)
171 if (base.Resolve (ec, index) == null)
174 var ac = parameter_type as ArrayContainer;
175 if (ac == null || ac.Rank != 1) {
176 ec.Compiler.Report.Error (225, Location, "The params parameter must be a single dimensional array");
180 return parameter_type;
183 public override void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
185 base.ApplyAttributes (mb, cb, index);
186 PredefinedAttributes.Get.ParamArray.EmitAttribute (builder);
190 public class ArglistParameter : Parameter {
191 // Doesn't have proper type because it's never chosen for better conversion
192 public ArglistParameter (Location loc) :
193 base (null, String.Empty, Parameter.Modifier.NONE, null, loc)
195 parameter_type = InternalType.Arglist;
198 public override void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
203 public override bool CheckAccessibility (InterfaceMemberBase member)
208 public override TypeSpec Resolve (IMemberContext ec, int index)
210 return parameter_type;
214 public interface IParameterData
216 Expression DefaultValue { get; }
217 bool HasExtensionMethodModifier { get; }
218 bool HasDefaultValue { get; }
219 Parameter.Modifier ModFlags { get; }
224 // Parameter information created by parser
226 public class Parameter : ParameterBase, IParameterData, ILocalVariable {
228 public enum Modifier : byte {
230 REF = REFMASK | ISBYREF,
231 OUT = OUTMASK | ISBYREF,
233 // This is a flag which says that it's either REF or OUT.
240 static string[] attribute_targets = new string [] { "param" };
242 FullNamedExpression texpr;
243 readonly Modifier modFlags;
245 Expression default_expr;
246 protected TypeSpec parameter_type;
247 public readonly Location Location;
249 public bool HasAddressTaken;
251 Expression expr_tree_variable;
252 static TypeExpr parameter_expr_tree_type;
254 HoistedVariable hoisted_variant;
256 public Parameter (FullNamedExpression type, string name, Modifier mod, Attributes attrs, Location loc)
263 // Only assign, attributes will be attached during resolve
264 base.attributes = attrs;
268 public FullNamedExpression TypeExpression {
275 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
277 if (a.Type == pa.In && ModFlags == Modifier.OUT) {
278 a.Report.Error (36, a.Location, "An out parameter cannot have the `In' attribute");
282 if (a.Type == pa.ParamArray) {
283 a.Report.Error (674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
287 if (a.Type == PredefinedAttributes.Get.Out && (ModFlags & Modifier.REF) == Modifier.REF &&
288 !OptAttributes.Contains (pa.In)) {
289 a.Report.Error (662, a.Location,
290 "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
294 if (a.Type == pa.CLSCompliant) {
295 a.Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
298 if (HasDefaultValue && (a.Type == pa.DefaultParameterValue || a.Type == pa.OptionalParameter)) {
299 a.Report.Error (1745, a.Location,
300 "Cannot specify `{0}' attribute on optional parameter `{1}'",
301 TypeManager.CSharpName (a.Type).Replace ("Attribute", ""), Name);
305 if (a.Type == pa.DefaultParameterValue) {
307 var c = a.GetParameterDefaultValue (out arg_type);
309 if (parameter_type == TypeManager.object_type) {
310 a.Report.Error (1910, a.Location, "Argument of type `{0}' is not applicable for the DefaultParameterValue attribute",
311 arg_type.GetSignatureForError ());
313 a.Report.Error (1909, a.Location, "The DefaultParameterValue attribute is not applicable on parameters of type `{0}'",
314 parameter_type.GetSignatureForError ()); ;
320 if (arg_type == parameter_type || parameter_type == TypeManager.object_type ||
321 (c.IsNull && TypeManager.IsReferenceType (parameter_type) && !TypeManager.IsGenericParameter (parameter_type)))
322 builder.SetConstant (c.GetValue ());
324 a.Report.Error (1908, a.Location, "The type of the default value should match the type of the parameter");
329 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
332 public virtual bool CheckAccessibility (InterfaceMemberBase member)
334 if (parameter_type == null || TypeManager.IsGenericParameter (parameter_type))
337 return member.IsAccessibleAs (parameter_type);
340 public static void Reset ()
342 parameter_expr_tree_type = null;
346 // Resolve is used in method definitions
348 public virtual TypeSpec Resolve (IMemberContext rc, int index)
350 if (parameter_type != null)
351 return parameter_type;
353 if (attributes != null)
354 attributes.AttachTo (this, rc);
356 var expr = texpr.ResolveAsTypeTerminal (rc, false);
362 parameter_type = texpr.Type;
364 if ((modFlags & Parameter.Modifier.ISBYREF) != 0 &&
365 TypeManager.IsSpecialType (parameter_type)) {
366 rc.Compiler.Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
367 GetSignatureForError ());
371 TypeManager.CheckTypeVariance (parameter_type,
372 (modFlags & Parameter.Modifier.ISBYREF) != 0 ? Variance.None : Variance.Contravariant,
375 if (parameter_type.IsStatic) {
376 rc.Compiler.Report.Error (721, Location, "`{0}': static types cannot be used as parameters",
377 texpr.GetSignatureForError ());
378 return parameter_type;
381 if ((modFlags & Modifier.This) != 0 && (parameter_type.IsPointer || parameter_type == InternalType.Dynamic)) {
382 rc.Compiler.Report.Error (1103, Location, "The extension method cannot be of type `{0}'",
383 TypeManager.CSharpName (parameter_type));
386 return parameter_type;
389 public void ResolveDefaultValue (ResolveContext rc)
391 if (default_expr != null)
392 default_expr = ResolveDefaultExpression (rc);
395 Expression ResolveDefaultExpression (ResolveContext rc)
397 default_expr = default_expr.Resolve (rc);
398 if (default_expr == null)
401 if (!(default_expr is Constant || default_expr is DefaultValueExpression)) {
402 if (TypeManager.IsNullableType (parameter_type)) {
403 rc.Compiler.Report.Error (1770, default_expr.Location,
404 "The expression being assigned to nullable optional parameter `{0}' must be default value",
407 rc.Compiler.Report.Error (1736, default_expr.Location,
408 "The expression being assigned to optional parameter `{0}' must be a constant or default value",
415 if (default_expr.Type == parameter_type)
418 if (TypeManager.IsNullableType (parameter_type)) {
419 if (Convert.ImplicitNulableConversion (rc, default_expr, parameter_type) != null)
422 var res = Convert.ImplicitConversionStandard (rc, default_expr, parameter_type, default_expr.Location);
424 if (!default_expr.IsNull && TypeManager.IsReferenceType (parameter_type) && parameter_type != TypeManager.string_type) {
425 rc.Compiler.Report.Error (1763, default_expr.Location,
426 "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
427 Name, GetSignatureForError ());
436 rc.Compiler.Report.Error (1750, Location,
437 "Optional parameter expression of type `{0}' cannot be converted to parameter type `{1}'",
438 TypeManager.CSharpName (default_expr.Type), GetSignatureForError ());
443 public bool HasDefaultValue {
444 get { return default_expr != null; }
447 public bool HasExtensionMethodModifier {
448 get { return (modFlags & Modifier.This) != 0; }
452 // Hoisted parameter variant
454 public HoistedVariable HoistedVariant {
456 return hoisted_variant;
459 hoisted_variant = value;
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 = texpr.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 (IMemberContext ctx)
516 if (parameter_type.IsCLSCompliant ())
519 ctx.Compiler.Report.Warning (3001, 1, Location,
520 "Argument type `{0}' is not CLS-compliant", GetSignatureForError ());
523 public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
526 builder = cb.DefineParameter (index, Attributes, Name);
528 builder = mb.DefineParameter (index, Attributes, Name);
530 if (OptAttributes != null)
531 OptAttributes.Emit ();
533 if (HasDefaultValue) {
535 // Emit constant values for true constants only, the other
536 // constant-like expressions will rely on default value expression
538 Constant c = default_expr as Constant;
540 if (default_expr.Type == TypeManager.decimal_type) {
541 builder.SetCustomAttribute (Const.CreateDecimalConstantAttribute (c));
543 builder.SetConstant (c.GetTypedValue ());
548 if (parameter_type == InternalType.Dynamic) {
549 PredefinedAttributes.Get.Dynamic.EmitAttribute (builder);
551 var trans_flags = TypeManager.HasDynamicTypeUsed (parameter_type);
552 if (trans_flags != null) {
553 var pa = PredefinedAttributes.Get.DynamicTransform;
554 if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
555 builder.SetCustomAttribute (
556 new CustomAttributeBuilder (pa.Constructor, new object [] { trans_flags }));
562 public override string[] ValidAttributeTargets {
564 return attribute_targets;
568 public Parameter Clone ()
570 Parameter p = (Parameter) MemberwiseClone ();
571 if (attributes != null)
572 p.attributes = attributes.Clone ();
577 public ExpressionStatement CreateExpressionTreeVariable (BlockContext ec)
579 if ((modFlags & Modifier.ISBYREF) != 0)
580 ec.Report.Error (1951, Location, "An expression tree parameter cannot use `ref' or `out' modifier");
582 expr_tree_variable = new TemporaryVariable (ResolveParameterExpressionType (ec, Location).Type, Location);
583 expr_tree_variable = expr_tree_variable.Resolve (ec);
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 (ec, "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, arg_idx);
607 public void EmitAssign (EmitContext ec)
614 ec.Emit (OpCodes.Starg_S, (byte) arg_idx);
616 ec.Emit (OpCodes.Starg, arg_idx);
619 public void EmitAddressOf (EmitContext ec)
626 bool is_ref = (ModFlags & Modifier.ISBYREF) != 0;
628 ParameterReference.EmitLdArg (ec, arg_idx);
631 ec.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
633 ec.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 (IMemberContext ec, Location location)
647 if (parameter_expr_tree_type != null)
648 return parameter_expr_tree_type;
650 TypeSpec p_type = TypeManager.parameter_expression_type;
651 if (p_type == null) {
652 p_type = TypeManager.CoreLookupType (ec.Compiler, "System.Linq.Expressions", "ParameterExpression", MemberKind.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 (Report Report)
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 TypeSpec [] types;
725 public CallingConventions CallingConvention {
728 CallingConventions.VarArgs :
729 CallingConventions.Standard;
734 get { return parameters.Length; }
737 public TypeSpec ExtensionMethodType {
742 return FixedParameters [0].HasExtensionMethodModifier ?
747 public IParameterData [] FixedParameters {
753 public static ParameterAttributes GetParameterAttribute (Parameter.Modifier modFlags)
755 return (modFlags & Parameter.Modifier.OUT) == Parameter.Modifier.OUT ?
756 ParameterAttributes.Out : ParameterAttributes.None;
759 // Very expensive operation
760 public Type[] GetMetaInfo ()
765 return Type.EmptyTypes;
767 types = new Type [Count - 1];
770 return Type.EmptyTypes;
772 types = new Type [Count];
775 for (int i = 0; i < types.Length; ++i) {
776 types[i] = Types[i].GetMetaInfo ();
778 if ((FixedParameters [i].ModFlags & Parameter.Modifier.ISBYREF) == 0)
781 // TODO MemberCache: Should go to MetaInfo getter
782 types [i] = types [i].MakeByRefType ();
789 // Returns the parameter information based on the name
791 public int GetParameterIndexByName (string name)
793 for (int idx = 0; idx < Count; ++idx) {
794 if (parameters [idx].Name == name)
801 public string GetSignatureForError ()
803 return GetSignatureForError ("(", ")", Count);
806 public string GetSignatureForError (string start, string end, int count)
808 StringBuilder sb = new StringBuilder (start);
809 for (int i = 0; i < count; ++i) {
812 sb.Append (ParameterDesc (i));
815 return sb.ToString ();
818 public bool HasArglist {
819 get { return has_arglist; }
822 public bool HasExtensionMethodType {
827 return FixedParameters [0].HasExtensionMethodModifier;
831 public bool HasParams {
832 get { return has_params; }
835 public bool IsEmpty {
836 get { return parameters.Length == 0; }
839 public AParametersCollection Inflate (TypeParameterInflator inflator)
841 TypeSpec[] inflated_types = null;
842 bool default_value = false;
844 for (int i = 0; i < Count; ++i) {
845 var inflated_param = inflator.Inflate (types[i]);
846 if (inflated_types == null) {
847 if (inflated_param == types[i])
850 default_value |= FixedParameters[i] is DefaultValueExpression;
851 inflated_types = new TypeSpec[types.Length];
852 Array.Copy (types, inflated_types, types.Length);
855 inflated_types[i] = inflated_param;
858 if (inflated_types == null)
861 var clone = (AParametersCollection) MemberwiseClone ();
862 clone.types = inflated_types;
864 for (int i = 0; i < Count; ++i) {
865 var dve = clone.FixedParameters[i] as DefaultValueExpression;
867 throw new NotImplementedException ("net");
868 // clone.FixedParameters [i].DefaultValue = new DefaultValueExpression ();
876 public string ParameterDesc (int pos)
878 if (types == null || types [pos] == null)
879 return ((Parameter)FixedParameters [pos]).GetSignatureForError ();
881 string type = TypeManager.CSharpName (types [pos]);
882 if (FixedParameters [pos].HasExtensionMethodModifier)
883 return "this " + type;
885 Parameter.Modifier mod = FixedParameters [pos].ModFlags;
889 return Parameter.GetModifierSignature (mod) + " " + type;
892 public TypeSpec[] Types {
893 get { return types; }
894 set { types = value; }
899 // A collection of imported or resolved parameters
901 public class ParametersImported : AParametersCollection
903 ParametersImported (AParametersCollection param, TypeSpec[] types)
905 this.parameters = param.FixedParameters;
907 has_arglist = param.HasArglist;
908 has_params = param.HasParams;
911 ParametersImported (IParameterData [] parameters, TypeSpec [] types, bool hasArglist, bool hasParams)
913 this.parameters = parameters;
915 this.has_arglist = hasArglist;
916 this.has_params = hasParams;
919 public ParametersImported (IParameterData[] param, TypeSpec[] types, bool hasParams)
921 this.parameters = param;
923 this.has_params = hasParams;
926 public static AParametersCollection Create (TypeSpec parent, MethodBase method)
928 return Create (parent, method.GetParameters (), method);
932 // Imports System.Reflection parameters
934 public static AParametersCollection Create (TypeSpec parent, 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 TypeSpec [] types = new TypeSpec [pi.Length + varargs];
942 IParameterData [] par = new IParameterData [pi.Length + varargs];
943 bool is_params = false;
944 for (int i = 0; i < pi.Length; i++) {
945 ParameterInfo p = pi [i];
946 Parameter.Modifier mod = 0;
947 Expression default_value = null;
948 if (p.ParameterType.IsByRef) {
949 if ((p.Attributes & (ParameterAttributes.Out | ParameterAttributes.In)) == ParameterAttributes.Out)
950 mod = Parameter.Modifier.OUT;
952 mod = Parameter.Modifier.REF;
955 // Strip reference wrapping
957 types [i] = Import.ImportType (p.ParameterType.GetElementType ());
958 } else if (i == 0 && method.IsStatic && parent.IsStatic && // TODO: parent.Assembly.IsExtension &&
959 HasExtensionAttribute (method)) {
960 mod = Parameter.Modifier.This;
961 types[i] = Import.ImportType (p.ParameterType);
963 types[i] = Import.ImportType (p.ParameterType);
965 if (i >= pi.Length - 2 && types[i] is ArrayContainer) {
966 var cattrs = CustomAttributeData.GetCustomAttributes (p);
967 if (cattrs != null && cattrs.Any (l => l.Constructor.DeclaringType == typeof (ParamArrayAttribute))) {
968 mod = Parameter.Modifier.PARAMS;
973 if (!is_params && p.IsOptional) {
974 object value = p.DefaultValue;
975 if (value == Missing.Value) {
976 default_value = EmptyExpression.Null;
977 } else if (value == null) {
978 default_value = new NullLiteral (Location.Null);
980 default_value = Constant.CreateConstant (null, Import.ImportType (value.GetType ()), value, Location.Null);
985 par [i] = new ParameterData (p.Name, mod, default_value);
989 par [par.Length - 1] = new ArglistParameter (Location.Null);
990 types [types.Length - 1] = InternalType.Arglist;
993 return method != null ?
994 new ParametersImported (par, types, varargs != 0, is_params) :
995 new ParametersImported (par, types, is_params);
998 static bool HasExtensionAttribute (MethodBase mb)
1000 var all_attributes = CustomAttributeData.GetCustomAttributes (mb);
1001 foreach (var attr in all_attributes) {
1002 var dt = attr.Constructor.DeclaringType;
1003 if (dt.Name == "ExtensionAttribute" && dt.Namespace == "System.Runtime.CompilerServices") {
1013 /// Represents the methods parameters
1015 public class ParametersCompiled : AParametersCollection
1017 public static readonly ParametersCompiled EmptyReadOnlyParameters = new ParametersCompiled ();
1019 // Used by C# 2.0 delegates
1020 public static readonly ParametersCompiled Undefined = new ParametersCompiled ();
1022 private ParametersCompiled ()
1024 parameters = new Parameter [0];
1025 types = TypeSpec.EmptyTypes;
1028 private ParametersCompiled (IParameterData [] parameters, TypeSpec [] types)
1030 this.parameters = parameters;
1034 public ParametersCompiled (CompilerContext ctx, params Parameter[] parameters)
1036 if (parameters == null)
1037 throw new ArgumentException ("Use EmptyReadOnlyParameters");
1039 this.parameters = parameters;
1040 int count = parameters.Length;
1046 has_params = (parameters [0].ModFlags & Parameter.Modifier.PARAMS) != 0;
1050 for (int i = 0; i < count; i++){
1051 has_params |= (parameters [i].ModFlags & Parameter.Modifier.PARAMS) != 0;
1053 string base_name = parameters[i].Name;
1055 for (int j = i + 1; j < count; j++) {
1056 if (base_name != parameters[j].Name)
1059 ErrorDuplicateName (parameters[i], ctx.Report);
1066 public ParametersCompiled (CompilerContext ctx, Parameter [] parameters, bool has_arglist) :
1067 this (ctx, parameters)
1069 this.has_arglist = has_arglist;
1072 public static ParametersCompiled CreateFullyResolved (Parameter p, TypeSpec type)
1074 return new ParametersCompiled (new Parameter [] { p }, new TypeSpec [] { type });
1077 public static ParametersCompiled CreateFullyResolved (IParameterData[] parameters, TypeSpec[] types)
1079 return new ParametersCompiled (parameters, types);
1082 public static AParametersCollection CreateFullyResolved (TypeSpec[] types)
1084 var pd = new ParameterData [types.Length];
1085 for (int i = 0; i < pd.Length; ++i)
1086 pd[i] = new ParameterData (null, Parameter.Modifier.NONE, null);
1088 return new ParametersCompiled (pd, types);
1091 public static ParametersCompiled CreateImplicitParameter (FullNamedExpression texpr, Location loc)
1093 return new ParametersCompiled (
1094 new[] { new Parameter (texpr, "value", Parameter.Modifier.NONE, null, loc) },
1099 // Returns non-zero value for equal CLS parameter signatures
1101 public static int IsSameClsSignature (AParametersCollection a, AParametersCollection b)
1105 for (int i = 0; i < a.Count; ++i) {
1106 var a_type = a.Types[i];
1107 var b_type = b.Types[i];
1108 if (TypeSpecComparer.Override.IsEqual (a_type, b_type)) {
1109 const Parameter.Modifier ref_out = Parameter.Modifier.REF | Parameter.Modifier.OUT;
1110 if ((a.FixedParameters[i].ModFlags & ref_out) != (b.FixedParameters[i].ModFlags & ref_out))
1116 var ac_a = a_type as ArrayContainer;
1120 var ac_b = b_type as ArrayContainer;
1124 if (ac_a.Element is ArrayContainer || ac_b.Element is ArrayContainer) {
1129 if (ac_a.Rank != ac_b.Rank && TypeSpecComparer.Override.IsEqual (ac_a.Element, ac_b.Element)) {
1140 public static ParametersCompiled MergeGenerated (CompilerContext ctx, ParametersCompiled userParams, bool checkConflicts, Parameter compilerParams, TypeSpec compilerTypes)
1142 return MergeGenerated (ctx, userParams, checkConflicts,
1143 new Parameter [] { compilerParams },
1144 new TypeSpec [] { compilerTypes });
1148 // Use this method when you merge compiler generated parameters with user parameters
1150 public static ParametersCompiled MergeGenerated (CompilerContext ctx, ParametersCompiled userParams, bool checkConflicts, Parameter[] compilerParams, TypeSpec[] compilerTypes)
1152 Parameter[] all_params = new Parameter [userParams.Count + compilerParams.Length];
1153 userParams.FixedParameters.CopyTo(all_params, 0);
1155 TypeSpec [] all_types;
1156 if (userParams.types != null) {
1157 all_types = new TypeSpec [all_params.Length];
1158 userParams.Types.CopyTo (all_types, 0);
1163 int last_filled = userParams.Count;
1165 foreach (Parameter p in compilerParams) {
1166 for (int i = 0; i < last_filled; ++i) {
1167 while (p.Name == all_params [i].Name) {
1168 if (checkConflicts && i < userParams.Count) {
1169 ctx.Report.Error (316, userParams[i].Location,
1170 "The parameter name `{0}' conflicts with a compiler generated name", p.Name);
1172 p.Name = '_' + p.Name;
1175 all_params [last_filled] = p;
1176 if (all_types != null)
1177 all_types [last_filled] = compilerTypes [index++];
1181 ParametersCompiled parameters = new ParametersCompiled (all_params, all_types);
1182 parameters.has_params = userParams.has_params;
1186 protected virtual void ErrorDuplicateName (Parameter p, Report Report)
1188 Report.Error (100, p.Location, "The parameter name `{0}' is a duplicate", p.Name);
1191 public bool Resolve (IMemberContext ec)
1196 types = new TypeSpec [Count];
1200 for (int i = 0; i < FixedParameters.Length; ++i) {
1202 TypeSpec t = p.Resolve (ec, i);
1214 public void ResolveDefaultValues (ResolveContext rc)
1216 for (int i = 0; i < FixedParameters.Length; ++i) {
1217 this [i].ResolveDefaultValue (rc);
1221 // Define each type attribute (in/out/ref) and
1222 // the argument names.
1223 public void ApplyAttributes (MethodBase builder)
1228 MethodBuilder mb = builder as MethodBuilder;
1229 ConstructorBuilder cb = builder as ConstructorBuilder;
1231 for (int i = 0; i < Count; i++) {
1232 this [i].ApplyAttributes (mb, cb, i + 1);
1236 public void VerifyClsCompliance (IMemberContext ctx)
1238 foreach (Parameter p in FixedParameters)
1239 p.IsClsCompliant (ctx);
1242 public Parameter this [int pos] {
1243 get { return (Parameter) parameters [pos]; }
1246 public Expression CreateExpressionTree (BlockContext ec, Location loc)
1248 var initializers = new ArrayInitializer (Count, loc);
1249 foreach (Parameter p in FixedParameters) {
1251 // Each parameter expression is stored to local variable
1252 // to save some memory when referenced later.
1254 StatementExpression se = new StatementExpression (p.CreateExpressionTreeVariable (ec));
1255 if (se.Resolve (ec))
1256 ec.CurrentBlock.AddScopeStatement (se);
1258 initializers.Add (p.ExpressionTreeVariableReference ());
1261 return new ArrayCreation (
1262 Parameter.ResolveParameterExpressionType (ec, loc),
1266 public ParametersCompiled Clone ()
1268 ParametersCompiled p = (ParametersCompiled) MemberwiseClone ();
1270 p.parameters = new IParameterData [parameters.Length];
1271 for (int i = 0; i < Count; ++i)
1272 p.parameters [i] = this [i].Clone ();