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 {
139 // This is a flag which says that it's either REF or OUT.
144 static string[] attribute_targets = new string [] { "param" };
146 public Expression TypeName;
147 public readonly Modifier ModFlags;
148 public readonly string Name;
149 GenericConstraints constraints;
152 EmitContext ec; // because ApplyAtrribute doesn't have ec
154 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
162 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
164 if (a.Type == TypeManager.in_attribute_type && Attributes == ParameterAttributes.Out) {
165 Report.Error (36, a.Location, "Can not use [In] attribute on out parameter");
169 if (a.Type == TypeManager.param_array_type) {
170 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
174 if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) != 0 &&
175 !OptAttributes.Contains (TypeManager.in_attribute_type, ec)) {
176 Report.Error (662, a.Location,
177 "'{0}' cannot specify only Out attribute on a ref parameter. Use both In and Out attributes, or neither", GetSignatureForError ());
181 if (a.Type == TypeManager.cls_compliant_attribute_type) {
182 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
185 base.ApplyAttributeBuilder (a, cb);
189 // Resolve is used in method definitions
191 public bool Resolve (EmitContext ec, Location l)
193 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec);
199 TypeParameterExpr tparam = texpr as TypeParameterExpr;
201 constraints = tparam.TypeParameter.Constraints;
203 parameter_type = texpr.Type;
205 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
206 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
210 if (parameter_type == TypeManager.void_type){
211 Report.Error (1536, l, "Invalid parameter type 'void'");
215 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
216 if (parameter_type == TypeManager.typed_reference_type ||
217 parameter_type == TypeManager.arg_iterator_type){
218 Report.Error (1601, l,
219 "out or ref parameter can not be of type TypedReference or ArgIterator");
224 return parameter_type != null;
227 public Type ExternalType ()
229 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
230 return TypeManager.GetReferenceType (parameter_type);
232 return parameter_type;
235 public Type ParameterType {
237 return parameter_type;
241 public GenericConstraints GenericConstraints {
247 public ParameterAttributes Attributes {
249 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
250 switch ((Modifier) flags) {
252 return ParameterAttributes.None;
254 return ParameterAttributes.None;
256 return ParameterAttributes.Out;
257 case Modifier.PARAMS:
261 return ParameterAttributes.None;
265 public static ParameterAttributes GetParameterAttributes (Modifier mod)
267 int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
268 switch ((Modifier) flags) {
270 return ParameterAttributes.None;
272 return ParameterAttributes.None;
274 return ParameterAttributes.Out;
275 case Modifier.PARAMS:
279 return ParameterAttributes.None;
282 public override AttributeTargets AttributeTargets {
284 return AttributeTargets.Parameter;
289 /// Returns the signature for this parameter evaluating it on the
292 public string GetSignature (EmitContext ec, Location loc)
294 if (parameter_type == null){
295 if (!Resolve (ec, loc))
299 return ExternalType ().FullName;
302 public string GetSignatureForError ()
305 if (parameter_type != null)
306 typeName = TypeManager.CSharpName (parameter_type);
307 else if (TypeName.Type != null)
308 typeName = TypeManager.CSharpName (TypeName.Type);
310 typeName = TypeName.ToString ();
312 switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
314 return "out " + typeName;
315 case Modifier.PARAMS:
316 return "params " + typeName;
318 return "ref " + typeName;
323 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
325 ParameterAttributes par_attr = Attributes;
328 builder = cb.DefineParameter (index, par_attr, Name);
330 builder = mb.DefineParameter (index, par_attr, Name);
332 if (OptAttributes != null) {
333 OptAttributes.Emit (ec, this);
337 public override string[] ValidAttributeTargets {
339 return attribute_targets;
345 /// Represents the methods parameters
347 public class Parameters {
348 public Parameter [] FixedParameters;
349 public readonly Parameter ArrayParameter;
350 public readonly bool HasArglist;
355 static Parameters empty_parameters;
357 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
359 FixedParameters = fixed_parameters;
360 ArrayParameter = array_parameter;
364 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
366 FixedParameters = fixed_parameters;
367 HasArglist = has_arglist;
372 /// This is used to reuse a set of empty parameters, because they
375 public static Parameters EmptyReadOnlyParameters {
377 if (empty_parameters == null)
378 empty_parameters = new Parameters (null, null, Location.Null);
380 return empty_parameters;
386 return (FixedParameters == null) && (ArrayParameter == null);
390 public void ComputeSignature (EmitContext ec)
393 if (FixedParameters != null){
394 for (int i = 0; i < FixedParameters.Length; i++){
395 Parameter par = FixedParameters [i];
397 signature += par.GetSignature (ec, loc);
401 // Note: as per the spec, the `params' arguments (ArrayParameter)
402 // are not used in the signature computation for a method
406 void Error_DuplicateParameterName (string name)
409 100, loc, "The parameter name `" + name + "' is a duplicate");
412 public bool VerifyArgs ()
417 if (FixedParameters == null)
420 count = FixedParameters.Length;
421 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
423 for (i = 0; i < count; i++){
424 string base_name = FixedParameters [i].Name;
425 for (j = i + 1; j < count; j++){
426 if (base_name != FixedParameters [j].Name)
428 Error_DuplicateParameterName (base_name);
432 if (base_name == array_par_name){
433 Error_DuplicateParameterName (base_name);
441 /// Returns the signature of the Parameters evaluated in
442 /// the @ec EmitContext
444 public string GetSignature (EmitContext ec)
446 if (signature == null){
448 ComputeSignature (ec);
455 /// Returns the paramenter information based on the name
457 public Parameter GetParameterByName (string name, out int idx)
462 if (FixedParameters != null){
463 foreach (Parameter par in FixedParameters){
464 if (par.Name == name){
472 if (ArrayParameter != null){
473 if (name == ArrayParameter.Name){
475 return ArrayParameter;
482 public Parameter GetParameterByName (string name)
486 return GetParameterByName (name, out idx);
489 bool ComputeParameterTypes (EmitContext ec)
491 int extra = (ArrayParameter != null) ? 1 : 0;
495 if (FixedParameters == null)
498 pc = extra + FixedParameters.Length;
500 types = new Type [pc];
503 FixedParameters = null;
508 if (FixedParameters != null){
509 foreach (Parameter p in FixedParameters){
512 if (p.Resolve (ec, loc))
513 t = p.ExternalType ();
523 if (ArrayParameter.Resolve (ec, loc))
524 types [i] = ArrayParameter.ExternalType ();
538 // This variant is used by Delegates, because they need to
539 // resolve/define names, instead of the plain LookupType
541 public bool ComputeAndDefineParameterTypes (EmitContext ec)
543 bool old_type_resolving = ec.ResolvingTypeTree;
544 ec.ResolvingTypeTree = true;
545 bool retval = ComputeParameterTypes (ec);
546 ec.ResolvingTypeTree = old_type_resolving;
551 /// Returns the argument types as an array
553 static Type [] no_types = new Type [0];
555 public Type [] GetParameterInfo (EmitContext ec)
560 if (FixedParameters == null && ArrayParameter == null)
563 if (ComputeParameterTypes (ec) == false){
572 /// Returns the type of a given parameter, and stores in the `is_out'
573 /// boolean whether this is an out or ref parameter.
575 /// Note that the returned type will not contain any dereference in this
576 /// case (ie, you get "int" for a ref int instead of "int&"
578 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
580 mod = Parameter.Modifier.NONE;
583 FixedParameters = null;
587 if (FixedParameters == null && ArrayParameter == null)
591 if (ComputeParameterTypes (ec) == false)
595 // If this is a request for the variable lenght arg.
597 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
598 if (idx == array_idx)
602 // Otherwise, it is a fixed parameter
604 Parameter p = FixedParameters [idx];
607 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
608 mod |= Parameter.Modifier.ISBYREF;
610 return p.ParameterType;
613 public CallingConventions GetCallingConvention ()
616 return CallingConventions.VarArgs;
618 return CallingConventions.Standard;
622 // The method's attributes are passed in because we need to extract
623 // the "return:" attribute from there to apply on the return type
625 public void LabelParameters (EmitContext ec,
629 // Define each type attribute (in/out/ref) and
630 // the argument names.
634 MethodBuilder mb = builder as MethodBuilder;
635 ConstructorBuilder cb = builder as ConstructorBuilder;
637 if (FixedParameters != null) {
638 for (i = 0; i < FixedParameters.Length; i++) {
639 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
643 if (ArrayParameter != null){
645 Parameter array_param = ArrayParameter;
648 pb = cb.DefineParameter (
649 i + 1, array_param.Attributes,
652 pb = mb.DefineParameter (
653 i + 1, array_param.Attributes,
656 CustomAttributeBuilder a = new CustomAttributeBuilder (
657 TypeManager.cons_param_array_attribute, new object [0]);
659 pb.SetCustomAttribute (a);