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;
25 public readonly Location Location;
27 public ParameterBase (Attributes attrs, Location loc)
33 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
35 if (a.Type == TypeManager.marshal_as_attr_type) {
36 UnmanagedMarshal marshal = a.GetMarshal (this);
37 if (marshal != null) {
38 builder.SetMarshal (marshal);
43 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
44 a.Error_InvalidSecurityParent ();
48 builder.SetCustomAttribute (cb);
51 public override bool IsClsComplianceRequired(DeclSpace ds)
58 /// Class for applying custom attributes on the return type
60 public class ReturnParameter: ParameterBase {
61 public ReturnParameter (MethodBuilder mb, Location location):
65 builder = mb.DefineParameter (0, ParameterAttributes.None, "");
67 catch (ArgumentOutOfRangeException) {
68 Report.Warning (-24, location, "The Microsoft .NET Runtime 1.x does not permit setting custom attributes on the return type");
72 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
74 if (a.Type == TypeManager.cls_compliant_attribute_type) {
75 Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
78 // This occurs after Warning -28
82 base.ApplyAttributeBuilder (a, cb);
85 public override AttributeTargets AttributeTargets {
87 return AttributeTargets.ReturnValue;
94 public override string[] ValidAttributeTargets {
102 /// Class for applying custom attributes on the implicit parameter type
103 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
105 public class ImplicitParameter: ParameterBase {
106 public ImplicitParameter (MethodBuilder mb, Location loc):
109 builder = mb.DefineParameter (1, ParameterAttributes.None, "");
112 public override AttributeTargets AttributeTargets {
114 return AttributeTargets.Parameter;
121 public override string[] ValidAttributeTargets {
130 /// Represents a single method parameter
133 //TODO: Add location member to this or base class for better error location and all methods simplification.
134 public class Parameter : ParameterBase {
136 public enum Modifier : byte {
141 // This is a flag which says that it's either REF or OUT.
146 static string[] attribute_targets = new string [] { "param" };
148 public Expression TypeName;
149 public readonly Modifier ModFlags;
150 public readonly string Name;
151 GenericConstraints constraints;
154 EmitContext ec; // because ApplyAtrribute doesn't have ec
156 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Location loc)
164 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
166 if (a.Type == TypeManager.in_attribute_type && Attributes == ParameterAttributes.Out) {
167 Report.Error (36, a.Location, "Can not use [In] attribute on out parameter");
171 if (a.Type == TypeManager.param_array_type) {
172 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
176 if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) != 0 &&
177 !OptAttributes.Contains (TypeManager.in_attribute_type, ec)) {
178 Report.Error (662, a.Location,
179 "'{0}' cannot specify only Out attribute on a ref parameter. Use both In and Out attributes, or neither", GetSignatureForError ());
183 if (a.Type == TypeManager.cls_compliant_attribute_type) {
184 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
187 base.ApplyAttributeBuilder (a, cb);
191 // Resolve is used in method definitions
193 public bool Resolve (EmitContext ec)
195 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec);
201 TypeParameterExpr tparam = texpr as TypeParameterExpr;
203 constraints = tparam.TypeParameter.Constraints;
205 parameter_type = texpr.Type;
207 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
208 Report.Error (721, Location, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
212 if (parameter_type == TypeManager.void_type){
213 Report.Error (1536, Location, "Invalid parameter type 'void'");
217 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
218 if (parameter_type == TypeManager.typed_reference_type ||
219 parameter_type == TypeManager.arg_iterator_type){
220 Report.Error (1601, Location,
221 "out or ref parameter can not be of type TypedReference or ArgIterator");
226 return parameter_type != null;
229 public Type ExternalType ()
231 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
232 return TypeManager.GetReferenceType (parameter_type);
234 return parameter_type;
237 public Type ParameterType {
239 return parameter_type;
243 public GenericConstraints GenericConstraints {
249 public ParameterAttributes Attributes {
251 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
252 switch ((Modifier) flags) {
254 return ParameterAttributes.None;
256 return ParameterAttributes.None;
258 return ParameterAttributes.Out;
259 case Modifier.PARAMS:
263 return ParameterAttributes.None;
267 public static ParameterAttributes GetParameterAttributes (Modifier mod)
269 int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
270 switch ((Modifier) flags) {
272 return ParameterAttributes.None;
274 return ParameterAttributes.None;
276 return ParameterAttributes.Out;
277 case Modifier.PARAMS:
281 return ParameterAttributes.None;
284 public override AttributeTargets AttributeTargets {
286 return AttributeTargets.Parameter;
291 /// Returns the signature for this parameter evaluating it on the
294 public string GetSignature (EmitContext ec)
296 if (parameter_type == null){
301 return ExternalType ().FullName;
304 public string GetSignatureForError ()
307 if (parameter_type != null)
308 type_name = TypeManager.CSharpName (parameter_type);
309 else if (TypeName.Type != null)
310 type_name = TypeManager.CSharpName (TypeName.Type);
312 type_name = TypeName.ToString ();
314 string mod = GetModifierSignature (ModFlags);
316 return String.Concat (mod, " ", type_name);
321 public static string GetModifierSignature (Modifier mod)
323 switch (mod & unchecked (~Modifier.ISBYREF)) {
326 case Modifier.PARAMS:
330 case Modifier.ARGLIST:
337 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
339 ParameterAttributes par_attr = Attributes;
342 builder = cb.DefineParameter (index, par_attr, Name);
344 builder = mb.DefineParameter (index, par_attr, Name);
346 if (OptAttributes != null) {
347 OptAttributes.Emit (ec, this);
351 public override string[] ValidAttributeTargets {
353 return attribute_targets;
359 /// Represents the methods parameters
361 public class Parameters {
362 public Parameter [] FixedParameters;
363 public readonly Parameter ArrayParameter;
364 public readonly bool HasArglist;
368 static Parameters empty_parameters;
370 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter)
372 FixedParameters = fixed_parameters;
373 ArrayParameter = array_parameter;
376 public Parameters (Parameter [] fixed_parameters, bool has_arglist)
378 FixedParameters = fixed_parameters;
379 HasArglist = has_arglist;
383 /// This is used to reuse a set of empty parameters, because they
386 public static Parameters EmptyReadOnlyParameters {
388 if (empty_parameters == null)
389 empty_parameters = new Parameters (null, null);
391 return empty_parameters;
397 return (FixedParameters == null) && (ArrayParameter == null);
401 public void ComputeSignature (EmitContext ec)
404 if (FixedParameters != null){
405 for (int i = 0; i < FixedParameters.Length; i++){
406 Parameter par = FixedParameters [i];
408 signature += par.GetSignature (ec);
412 // Note: as per the spec, the `params' arguments (ArrayParameter)
413 // are not used in the signature computation for a method
417 void Error_DuplicateParameterName (string name, Location loc)
420 100, loc, "The parameter name `" + name + "' is a duplicate");
423 public bool VerifyArgs ()
428 if (FixedParameters == null)
431 count = FixedParameters.Length;
432 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
434 for (i = 0; i < count; i++){
435 string base_name = FixedParameters [i].Name;
436 for (j = i + 1; j < count; j++){
437 if (base_name != FixedParameters [j].Name)
439 Error_DuplicateParameterName (base_name,
440 FixedParameters [i].Location);
444 if (base_name == array_par_name){
445 Error_DuplicateParameterName (base_name,
446 FixedParameters [i].Location);
454 /// Returns the signature of the Parameters evaluated in
455 /// the @ec EmitContext
457 public string GetSignature (EmitContext ec)
459 if (signature == null){
461 ComputeSignature (ec);
468 /// Returns the paramenter information based on the name
470 public Parameter GetParameterByName (string name, out int idx)
475 if (FixedParameters != null){
476 foreach (Parameter par in FixedParameters){
477 if (par.Name == name){
485 if (ArrayParameter != null){
486 if (name == ArrayParameter.Name){
488 return ArrayParameter;
495 public Parameter GetParameterByName (string name)
499 return GetParameterByName (name, out idx);
502 bool ComputeParameterTypes (EmitContext ec)
504 int extra = (ArrayParameter != null) ? 1 : 0;
508 if (FixedParameters == null)
511 pc = extra + FixedParameters.Length;
513 types = new Type [pc];
516 FixedParameters = null;
521 if (FixedParameters != null){
522 foreach (Parameter p in FixedParameters){
526 t = p.ExternalType ();
536 if (ArrayParameter.Resolve (ec))
537 types [i] = ArrayParameter.ExternalType ();
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);