2 // parameter.cs: Parameter definition.
4 // Author: Miguel de Icaza (miguel@gnu.org)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Collections;
17 namespace Mono.CSharp {
20 /// Abstract Base class for parameters of a method.
22 public abstract class ParameterBase : Attributable {
24 protected ParameterBuilder builder;
26 public ParameterBase (Attributes attrs)
31 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
33 if (a.Type == TypeManager.marshal_as_attr_type) {
34 UnmanagedMarshal marshal = a.GetMarshal (this);
35 if (marshal != null) {
36 builder.SetMarshal (marshal);
41 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
42 a.Error_InvalidSecurityParent ();
46 builder.SetCustomAttribute (cb);
49 public override bool IsClsCompliaceRequired(DeclSpace ds)
56 /// Class for applying custom attributes on the return type
58 public class ReturnParameter: ParameterBase {
59 public ReturnParameter (MethodBuilder mb, Location location):
63 builder = mb.DefineParameter (0, ParameterAttributes.None, "");
65 catch (ArgumentOutOfRangeException) {
66 Report.Warning (-28, location, "The Microsoft .NET Runtime 1.x does not permit setting custom attributes on the return type");
70 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
72 if (a.Type == TypeManager.cls_compliant_attribute_type) {
73 Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
76 // This occurs after Warning -28
80 base.ApplyAttributeBuilder (a, cb);
83 public override AttributeTargets AttributeTargets {
85 return AttributeTargets.ReturnValue;
92 public override string[] ValidAttributeTargets {
100 /// Class for applying custom attributes on the implicit parameter type
101 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
103 public class ImplicitParameter: ParameterBase {
104 public ImplicitParameter (MethodBuilder mb):
107 builder = mb.DefineParameter (1, ParameterAttributes.None, "");
110 public override AttributeTargets AttributeTargets {
112 return AttributeTargets.Parameter;
119 public override string[] ValidAttributeTargets {
128 /// Represents a single method parameter
131 //TODO: Add location member to this or base class for better error location and all methods simplification.
132 public class Parameter : ParameterBase {
134 public enum Modifier : byte {
140 // This is a flag which says that it's either REF or OUT.
143 // This value is changed from the one in mbas source code
147 static string[] attribute_targets = new string [] { "param" };
149 public Expression TypeName;
150 public readonly Modifier ModFlags;
151 public readonly string Name;
152 GenericConstraints constraints;
155 EmitContext ec; // because ApplyAtrribute doesn't have ec
157 /* These two fields are mbas specific. The corresponding
158 changes need to be merged from mbas */
160 public readonly Expression ParameterInitializer;
161 public readonly bool IsOptional;
163 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
169 ParameterInitializer = null;
173 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi)
179 ParameterInitializer = pi;
183 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi, bool opt)
189 ParameterInitializer = pi;
193 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
195 if (a.Type == TypeManager.in_attribute_type && Attributes == ParameterAttributes.Out) {
196 Report.Error (36, a.Location, "Can not use [In] attribute on out parameter");
200 if (a.Type == TypeManager.param_array_type) {
201 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
205 if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) != 0 &&
206 !OptAttributes.Contains (TypeManager.in_attribute_type, ec)) {
207 Report.Error (662, a.Location,
208 "'{0}' cannot specify only Out attribute on a ref parameter. Use both In and Out attributes, or neither", GetSignatureForError ());
212 if (a.Type == TypeManager.cls_compliant_attribute_type) {
213 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
216 base.ApplyAttributeBuilder (a, cb);
220 // Resolve is used in method definitions
222 public bool Resolve (EmitContext ec, Location l)
224 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec);
230 TypeParameterExpr tparam = texpr as TypeParameterExpr;
232 constraints = tparam.TypeParameter.Constraints;
234 parameter_type = texpr.Type;
236 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
237 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
241 if (parameter_type == TypeManager.void_type){
242 Report.Error (1536, l, "Invalid parameter type 'void'");
246 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
247 if (parameter_type == TypeManager.typed_reference_type ||
248 parameter_type == TypeManager.arg_iterator_type){
249 Report.Error (1601, l,
250 "out or ref parameter can not be of type TypedReference or ArgIterator");
255 return parameter_type != null;
258 public Type ExternalType ()
260 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
261 return TypeManager.GetReferenceType (parameter_type);
263 return parameter_type;
266 public Type ParameterType {
268 return parameter_type;
272 public GenericConstraints GenericConstraints {
278 public ParameterAttributes Attributes {
280 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
281 switch ((Modifier) flags) {
283 return ParameterAttributes.None;
285 return ParameterAttributes.None;
287 return ParameterAttributes.Out;
288 case Modifier.PARAMS:
292 return ParameterAttributes.None;
296 public static ParameterAttributes GetParameterAttributes (Modifier mod)
298 int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
299 switch ((Modifier) flags) {
301 return ParameterAttributes.None;
303 return ParameterAttributes.None;
305 return ParameterAttributes.Out;
306 case Modifier.PARAMS:
310 return ParameterAttributes.None;
313 public override AttributeTargets AttributeTargets {
315 return AttributeTargets.Parameter;
320 /// Returns the signature for this parameter evaluating it on the
323 public string GetSignature (EmitContext ec, Location loc)
325 if (parameter_type == null){
326 if (!Resolve (ec, loc))
330 return ExternalType ().FullName;
333 public string GetSignatureForError ()
336 if (parameter_type != null)
337 typeName = TypeManager.CSharpName (parameter_type);
338 else if (TypeName.Type != null)
339 typeName = TypeManager.CSharpName (TypeName.Type);
341 typeName = TypeName.ToString ();
343 switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
345 return "out " + typeName;
346 case Modifier.PARAMS:
347 return "params " + typeName;
349 return "ref " + typeName;
354 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
356 ParameterAttributes par_attr = Attributes;
359 builder = cb.DefineParameter (index, par_attr, Name);
361 builder = mb.DefineParameter (index, par_attr, Name);
363 if (OptAttributes != null) {
364 OptAttributes.Emit (ec, this);
368 public override string[] ValidAttributeTargets {
370 return attribute_targets;
376 /// Represents the methods parameters
378 public class Parameters {
379 public Parameter [] FixedParameters;
380 public readonly Parameter ArrayParameter;
381 public readonly bool HasArglist;
386 static Parameters empty_parameters;
388 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
390 FixedParameters = fixed_parameters;
391 ArrayParameter = array_parameter;
395 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
397 FixedParameters = fixed_parameters;
398 HasArglist = has_arglist;
403 /// This is used to reuse a set of empty parameters, because they
406 public static Parameters EmptyReadOnlyParameters {
408 if (empty_parameters == null)
409 empty_parameters = new Parameters (null, null, Location.Null);
411 return empty_parameters;
417 return (FixedParameters == null) && (ArrayParameter == null);
421 public void ComputeSignature (EmitContext ec)
424 if (FixedParameters != null){
425 for (int i = 0; i < FixedParameters.Length; i++){
426 Parameter par = FixedParameters [i];
428 signature += par.GetSignature (ec, loc);
432 // Note: as per the spec, the `params' arguments (ArrayParameter)
433 // are not used in the signature computation for a method
437 void Error_DuplicateParameterName (string name)
440 100, loc, "The parameter name `" + name + "' is a duplicate");
443 public bool VerifyArgs ()
448 if (FixedParameters == null)
451 count = FixedParameters.Length;
452 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
454 for (i = 0; i < count; i++){
455 string base_name = FixedParameters [i].Name;
456 for (j = i + 1; j < count; j++){
457 if (base_name != FixedParameters [j].Name)
459 Error_DuplicateParameterName (base_name);
463 if (base_name == array_par_name){
464 Error_DuplicateParameterName (base_name);
472 /// Returns the signature of the Parameters evaluated in
473 /// the @ec EmitContext
475 public string GetSignature (EmitContext ec)
477 if (signature == null){
479 ComputeSignature (ec);
486 /// Returns the paramenter information based on the name
488 public Parameter GetParameterByName (string name, out int idx)
493 if (FixedParameters != null){
494 foreach (Parameter par in FixedParameters){
495 if (par.Name == name){
503 if (ArrayParameter != null){
504 if (name == ArrayParameter.Name){
506 return ArrayParameter;
513 public Parameter GetParameterByName (string name)
517 return GetParameterByName (name, out idx);
520 bool ComputeParameterTypes (EmitContext ec)
522 int extra = (ArrayParameter != null) ? 1 : 0;
526 if (FixedParameters == null)
529 pc = extra + FixedParameters.Length;
531 types = new Type [pc];
534 FixedParameters = null;
539 if (FixedParameters != null){
540 foreach (Parameter p in FixedParameters){
543 if (p.Resolve (ec, loc))
544 t = p.ExternalType ();
554 if (ArrayParameter.Resolve (ec, loc))
555 types [i] = ArrayParameter.ExternalType ();
569 // This variant is used by Delegates, because they need to
570 // resolve/define names, instead of the plain LookupType
572 public bool ComputeAndDefineParameterTypes (EmitContext ec)
574 bool old_type_resolving = ec.ResolvingTypeTree;
575 ec.ResolvingTypeTree = true;
576 bool retval = ComputeParameterTypes (ec);
577 ec.ResolvingTypeTree = old_type_resolving;
582 /// Returns the argument types as an array
584 static Type [] no_types = new Type [0];
586 public Type [] GetParameterInfo (EmitContext ec)
591 if (FixedParameters == null && ArrayParameter == null)
594 if (ComputeParameterTypes (ec) == false){
603 /// Returns the type of a given parameter, and stores in the `is_out'
604 /// boolean whether this is an out or ref parameter.
606 /// Note that the returned type will not contain any dereference in this
607 /// case (ie, you get "int" for a ref int instead of "int&"
609 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
611 mod = Parameter.Modifier.NONE;
614 FixedParameters = null;
618 if (FixedParameters == null && ArrayParameter == null)
622 if (ComputeParameterTypes (ec) == false)
626 // If this is a request for the variable lenght arg.
628 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
629 if (idx == array_idx)
633 // Otherwise, it is a fixed parameter
635 Parameter p = FixedParameters [idx];
638 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
639 mod |= Parameter.Modifier.ISBYREF;
641 return p.ParameterType;
644 public CallingConventions GetCallingConvention ()
647 return CallingConventions.VarArgs;
649 return CallingConventions.Standard;
653 // The method's attributes are passed in because we need to extract
654 // the "return:" attribute from there to apply on the return type
656 public void LabelParameters (EmitContext ec,
660 // Define each type attribute (in/out/ref) and
661 // the argument names.
665 MethodBuilder mb = builder as MethodBuilder;
666 ConstructorBuilder cb = builder as ConstructorBuilder;
668 if (FixedParameters != null) {
669 for (i = 0; i < FixedParameters.Length; i++) {
670 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
674 if (ArrayParameter != null){
676 Parameter array_param = ArrayParameter;
679 pb = cb.DefineParameter (
680 i + 1, array_param.Attributes,
683 pb = mb.DefineParameter (
684 i + 1, array_param.Attributes,
687 CustomAttributeBuilder a = new CustomAttributeBuilder (
688 TypeManager.cons_param_array_attribute, new object [0]);
690 pb.SetCustomAttribute (a);
694 public Parameters Copy (Location l)
696 Parameters p = new Parameters (null, null, l);
697 p.FixedParameters = new Parameter[this.FixedParameters.Length];
698 this.FixedParameters.CopyTo (p.FixedParameters, 0);
703 public void AppendParameter (Parameter p)
705 if (FixedParameters != null)
707 Parameter [] pa = new Parameter [FixedParameters.Length+1];
708 FixedParameters.CopyTo (pa, 0);
709 pa[FixedParameters.Length] = p;
710 FixedParameters = pa;
714 FixedParameters = new Parameter [1];
715 FixedParameters[0] = p;