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 IsClsComplianceRequired(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 (-24, 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 type_name = TypeManager.CSharpName (parameter_type);
307 else if (TypeName.Type != null)
308 type_name = TypeManager.CSharpName (TypeName.Type);
310 type_name = TypeName.ToString ();
312 switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
314 return "out " + type_name;
315 case Modifier.PARAMS:
316 return "params " + type_name;
318 return "ref " + type_name;
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 Location Location {
394 public void ComputeSignature (EmitContext ec)
397 if (FixedParameters != null){
398 for (int i = 0; i < FixedParameters.Length; i++){
399 Parameter par = FixedParameters [i];
401 signature += par.GetSignature (ec, loc);
405 // Note: as per the spec, the `params' arguments (ArrayParameter)
406 // are not used in the signature computation for a method
410 void Error_DuplicateParameterName (string name)
413 100, loc, "The parameter name `" + name + "' is a duplicate");
416 public bool VerifyArgs ()
421 if (FixedParameters == null)
424 count = FixedParameters.Length;
425 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
427 for (i = 0; i < count; i++){
428 string base_name = FixedParameters [i].Name;
429 for (j = i + 1; j < count; j++){
430 if (base_name != FixedParameters [j].Name)
432 Error_DuplicateParameterName (base_name);
436 if (base_name == array_par_name){
437 Error_DuplicateParameterName (base_name);
445 /// Returns the signature of the Parameters evaluated in
446 /// the @ec EmitContext
448 public string GetSignature (EmitContext ec)
450 if (signature == null){
452 ComputeSignature (ec);
459 /// Returns the paramenter information based on the name
461 public Parameter GetParameterByName (string name, out int idx)
466 if (FixedParameters != null){
467 foreach (Parameter par in FixedParameters){
468 if (par.Name == name){
476 if (ArrayParameter != null){
477 if (name == ArrayParameter.Name){
479 return ArrayParameter;
486 public Parameter GetParameterByName (string name)
490 return GetParameterByName (name, out idx);
493 bool ComputeParameterTypes (EmitContext ec)
495 int extra = (ArrayParameter != null) ? 1 : 0;
499 if (FixedParameters == null)
502 pc = extra + FixedParameters.Length;
504 types = new Type [pc];
507 FixedParameters = null;
512 if (FixedParameters != null){
513 foreach (Parameter p in FixedParameters){
516 if (p.Resolve (ec, loc))
517 t = p.ExternalType ();
527 if (ArrayParameter.Resolve (ec, loc))
528 types [i] = ArrayParameter.ExternalType ();
542 /// Returns the argument types as an array
544 static Type [] no_types = new Type [0];
546 public Type [] GetParameterInfo (EmitContext ec)
551 if (FixedParameters == null && ArrayParameter == null)
554 if (ComputeParameterTypes (ec) == false){
563 /// Returns the type of a given parameter, and stores in the `is_out'
564 /// boolean whether this is an out or ref parameter.
566 /// Note that the returned type will not contain any dereference in this
567 /// case (ie, you get "int" for a ref int instead of "int&"
569 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
571 mod = Parameter.Modifier.NONE;
574 FixedParameters = null;
578 if (FixedParameters == null && ArrayParameter == null)
582 if (ComputeParameterTypes (ec) == false)
586 // If this is a request for the variable lenght arg.
588 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
589 if (idx == array_idx)
593 // Otherwise, it is a fixed parameter
595 Parameter p = FixedParameters [idx];
598 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
599 mod |= Parameter.Modifier.ISBYREF;
601 return p.ParameterType;
604 public CallingConventions GetCallingConvention ()
607 return CallingConventions.VarArgs;
609 return CallingConventions.Standard;
613 // The method's attributes are passed in because we need to extract
614 // the "return:" attribute from there to apply on the return type
616 public void LabelParameters (EmitContext ec,
620 // Define each type attribute (in/out/ref) and
621 // the argument names.
625 MethodBuilder mb = builder as MethodBuilder;
626 ConstructorBuilder cb = builder as ConstructorBuilder;
628 if (FixedParameters != null) {
629 for (i = 0; i < FixedParameters.Length; i++) {
630 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
634 if (ArrayParameter != null){
636 Parameter array_param = ArrayParameter;
639 pb = cb.DefineParameter (
640 i + 1, array_param.Attributes,
643 pb = mb.DefineParameter (
644 i + 1, array_param.Attributes,
647 CustomAttributeBuilder a = new CustomAttributeBuilder (
648 TypeManager.cons_param_array_attribute, new object [0]);
650 pb.SetCustomAttribute (a);