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;
18 namespace Mono.CSharp {
21 /// Abstract Base class for parameters of a method.
23 public abstract class ParameterBase : Attributable {
25 protected ParameterBuilder builder;
26 public readonly Location Location;
28 public ParameterBase (Attributes attrs, Location loc)
34 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
36 if (a.Type == TypeManager.marshal_as_attr_type) {
37 UnmanagedMarshal marshal = a.GetMarshal (this);
38 if (marshal != null) {
39 builder.SetMarshal (marshal);
44 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
45 a.Error_InvalidSecurityParent ();
49 builder.SetCustomAttribute (cb);
52 public override bool IsClsComplianceRequired(DeclSpace ds)
59 /// Class for applying custom attributes on the return type
61 public class ReturnParameter: ParameterBase {
62 public ReturnParameter (MethodBuilder mb, Location location):
66 builder = mb.DefineParameter (0, ParameterAttributes.None, "");
68 catch (ArgumentOutOfRangeException) {
69 Report.Warning (-24, location, "The Microsoft .NET Runtime 1.x does not permit setting custom attributes on the return type");
73 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
75 if (a.Type == TypeManager.cls_compliant_attribute_type) {
76 Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
79 // This occurs after Warning -28
83 base.ApplyAttributeBuilder (a, cb);
86 public override AttributeTargets AttributeTargets {
88 return AttributeTargets.ReturnValue;
95 public override string[] ValidAttributeTargets {
103 /// Class for applying custom attributes on the implicit parameter type
104 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
106 public class ImplicitParameter: ParameterBase {
107 public ImplicitParameter (MethodBuilder mb, Location loc):
110 builder = mb.DefineParameter (1, ParameterAttributes.None, "");
113 public override AttributeTargets AttributeTargets {
115 return AttributeTargets.Parameter;
122 public override string[] ValidAttributeTargets {
131 /// Represents a single method parameter
133 public class Parameter : ParameterBase {
135 public enum Modifier : byte {
140 // This is a flag which says that it's either REF or OUT.
145 static string[] attribute_targets = new string [] { "param" };
147 public Expression TypeName;
148 public readonly Modifier ModFlags;
149 public readonly string Name;
150 GenericConstraints constraints;
153 EmitContext ec; // because ApplyAtrribute doesn't have ec
155 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Location loc)
163 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
165 if (a.Type == TypeManager.in_attribute_type && Attributes == ParameterAttributes.Out) {
166 Report.Error (36, a.Location, "An out parameter cannot have the `In' attribute");
170 if (a.Type == TypeManager.param_array_type) {
171 Report.Error (674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
175 if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) != 0 &&
176 !OptAttributes.Contains (TypeManager.in_attribute_type, ec)) {
177 Report.Error (662, a.Location,
178 "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
182 if (a.Type == TypeManager.cls_compliant_attribute_type) {
183 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
186 base.ApplyAttributeBuilder (a, cb);
190 // Resolve is used in method definitions
192 public bool Resolve (EmitContext ec)
194 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec);
200 TypeParameterExpr tparam = texpr as TypeParameterExpr;
202 constraints = tparam.TypeParameter.Constraints;
204 parameter_type = texpr.Type;
206 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
207 Report.Error (721, Location, "`{0}': static types cannot be used as parameters", GetSignatureForError ());
211 if (parameter_type == TypeManager.void_type){
212 Report.Error (1536, Location, "Invalid parameter type 'void'");
216 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
217 if (parameter_type == TypeManager.typed_reference_type ||
218 parameter_type == TypeManager.arg_iterator_type){
219 Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
220 GetSignatureForError ());
225 return parameter_type != null;
228 public Type ExternalType ()
230 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
231 return TypeManager.GetReferenceType (parameter_type);
233 return parameter_type;
236 public Type ParameterType {
238 return parameter_type;
242 public GenericConstraints GenericConstraints {
248 public ParameterAttributes Attributes {
250 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
251 switch ((Modifier) flags) {
253 return ParameterAttributes.None;
255 return ParameterAttributes.None;
257 return ParameterAttributes.Out;
258 case Modifier.PARAMS:
262 return ParameterAttributes.None;
266 public static ParameterAttributes GetParameterAttributes (Modifier mod)
268 int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
269 switch ((Modifier) flags) {
271 return ParameterAttributes.None;
273 return ParameterAttributes.None;
275 return ParameterAttributes.Out;
276 case Modifier.PARAMS:
280 return ParameterAttributes.None;
283 public override AttributeTargets AttributeTargets {
285 return AttributeTargets.Parameter;
290 /// Returns the signature for this parameter evaluating it on the
293 public string GetSignature (EmitContext ec)
295 if (parameter_type == null){
300 return ExternalType ().FullName;
303 public string GetSignatureForError ()
306 if (parameter_type != null)
307 type_name = TypeManager.CSharpName (parameter_type);
308 else if (TypeName.Type != null)
309 type_name = TypeManager.CSharpName (TypeName.Type);
311 type_name = TypeName.ToString ();
313 string mod = GetModifierSignature (ModFlags);
315 return String.Concat (mod, " ", type_name);
320 public static string GetModifierSignature (Modifier mod)
322 switch (mod & unchecked (~Modifier.ISBYREF)) {
325 case Modifier.PARAMS:
329 case Modifier.ARGLIST:
336 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index)
338 ParameterAttributes par_attr = Attributes;
341 builder = cb.DefineParameter (index, par_attr, Name);
343 builder = mb.DefineParameter (index, par_attr, Name);
345 if (OptAttributes != null) {
346 OptAttributes.Emit (ec, this);
350 public override string[] ValidAttributeTargets {
352 return attribute_targets;
358 /// Represents the methods parameters
360 public class Parameters {
361 public Parameter [] FixedParameters;
362 public readonly Parameter ArrayParameter;
363 public readonly bool HasArglist;
367 static Parameters empty_parameters;
369 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter)
371 FixedParameters = fixed_parameters;
372 ArrayParameter = array_parameter;
375 public Parameters (Parameter [] fixed_parameters, bool has_arglist)
377 FixedParameters = fixed_parameters;
378 HasArglist = has_arglist;
382 /// This is used to reuse a set of empty parameters, because they
385 public static Parameters EmptyReadOnlyParameters {
387 if (empty_parameters == null)
388 empty_parameters = new Parameters (null, null);
390 return empty_parameters;
396 return (FixedParameters == null) && (ArrayParameter == null);
400 public void ComputeSignature (EmitContext ec)
403 if (FixedParameters != null){
404 for (int i = 0; i < FixedParameters.Length; i++){
405 Parameter par = FixedParameters [i];
407 signature += par.GetSignature (ec);
411 // Note: as per the spec, the `params' arguments (ArrayParameter)
412 // are not used in the signature computation for a method
416 void Error_DuplicateParameterName (string name, Location loc)
419 100, loc, "The parameter name `" + name + "' is a duplicate");
422 public bool VerifyArgs ()
427 if (FixedParameters == null)
430 count = FixedParameters.Length;
431 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
433 for (i = 0; i < count; i++){
434 string base_name = FixedParameters [i].Name;
435 for (j = i + 1; j < count; j++){
436 if (base_name != FixedParameters [j].Name)
438 Error_DuplicateParameterName (base_name,
439 FixedParameters [i].Location);
443 if (base_name == array_par_name){
444 Error_DuplicateParameterName (base_name,
445 FixedParameters [i].Location);
453 /// Returns the signature of the Parameters evaluated in
454 /// the @ec EmitContext
456 public string GetSignature (EmitContext ec)
458 if (signature == null){
460 ComputeSignature (ec);
467 /// Returns the paramenter information based on the name
469 public Parameter GetParameterByName (string name, out int idx)
474 if (FixedParameters != null){
475 foreach (Parameter par in FixedParameters){
476 if (par.Name == name){
484 if (ArrayParameter != null){
485 if (name == ArrayParameter.Name){
487 return ArrayParameter;
494 public Parameter GetParameterByName (string name)
498 return GetParameterByName (name, out idx);
501 bool ComputeParameterTypes (EmitContext ec)
503 int extra = (ArrayParameter != null) ? 1 : 0;
507 if (FixedParameters == null)
510 pc = extra + FixedParameters.Length;
512 types = new Type [pc];
515 FixedParameters = null;
520 if (FixedParameters != null){
521 foreach (Parameter p in FixedParameters){
525 t = p.ExternalType ();
535 if (ArrayParameter.Resolve (ec))
536 types [i] = ArrayParameter.ExternalType ();
550 /// Returns the argument types as an array
552 static Type [] no_types = new Type [0];
554 public Type [] GetParameterInfo (EmitContext ec)
559 if (FixedParameters == null && ArrayParameter == null)
562 if (ComputeParameterTypes (ec) == false){
571 /// Returns the type of a given parameter, and stores in the `is_out'
572 /// boolean whether this is an out or ref parameter.
574 /// Note that the returned type will not contain any dereference in this
575 /// case (ie, you get "int" for a ref int instead of "int&"
577 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
579 mod = Parameter.Modifier.NONE;
582 FixedParameters = null;
586 if (FixedParameters == null && ArrayParameter == null)
590 if (ComputeParameterTypes (ec) == false)
594 // If this is a request for the variable lenght arg.
596 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
597 if (idx == array_idx)
601 // Otherwise, it is a fixed parameter
603 Parameter p = FixedParameters [idx];
606 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
607 mod |= Parameter.Modifier.ISBYREF;
609 return p.ParameterType;
612 public CallingConventions GetCallingConvention ()
615 return CallingConventions.VarArgs;
617 return CallingConventions.Standard;
621 // The method's attributes are passed in because we need to extract
622 // the "return:" attribute from there to apply on the return type
624 public void LabelParameters (EmitContext ec, MethodBase builder)
627 // Define each type attribute (in/out/ref) and
628 // the argument names.
632 MethodBuilder mb = builder as MethodBuilder;
633 ConstructorBuilder cb = builder as ConstructorBuilder;
635 if (FixedParameters != null) {
636 for (i = 0; i < FixedParameters.Length; i++) {
637 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1);
641 if (ArrayParameter != null){
643 Parameter array_param = ArrayParameter;
646 pb = cb.DefineParameter (
647 i + 1, array_param.Attributes,
650 pb = mb.DefineParameter (
651 i + 1, array_param.Attributes,
654 CustomAttributeBuilder a = new CustomAttributeBuilder (
655 TypeManager.cons_param_array_attribute, new object [0]);
657 pb.SetCustomAttribute (a);
661 public string GetSignatureForError ()
663 StringBuilder sb = new StringBuilder ("(");
664 if (FixedParameters != null) {
665 for (int i = 0; i < FixedParameters.Length; ++i) {
666 sb.Append (FixedParameters[i].GetSignatureForError ());
667 if (i < FixedParameters.Length - 1)
672 return sb.ToString ();