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 IsClsCompliaceRequired(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 {
130 /// Represents a single method parameter
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;
151 EmitContext ec; // because ApplyAtrribute doesn't have ec
153 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Location loc)
161 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
163 if (a.Type == TypeManager.in_attribute_type && Attributes == ParameterAttributes.Out) {
164 Report.Error (36, a.Location, "An out parameter cannot have the `In' attribute");
168 if (a.Type == TypeManager.param_array_type) {
169 Report.Error (674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
173 if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) != 0 &&
174 !OptAttributes.Contains (TypeManager.in_attribute_type, ec)) {
175 Report.Error (662, a.Location,
176 "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
180 if (a.Type == TypeManager.cls_compliant_attribute_type) {
181 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
184 base.ApplyAttributeBuilder (a, cb);
188 // Resolve is used in method definitions
190 public bool Resolve (EmitContext ec)
194 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
198 parameter_type = texpr.ResolveType (ec);
200 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
201 Report.Error (721, Location, "`{0}': static types cannot be used as parameters", GetSignatureForError ());
205 if (parameter_type == TypeManager.void_type){
206 Report.Error (1536, Location, "Invalid parameter type 'void'");
210 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
211 if (parameter_type == TypeManager.typed_reference_type ||
212 parameter_type == TypeManager.arg_iterator_type){
213 Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
214 GetSignatureForError ());
219 return parameter_type != null;
222 public Type ExternalType ()
224 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
225 return TypeManager.GetReferenceType (parameter_type);
227 return parameter_type;
230 public Type ParameterType {
232 return parameter_type;
236 public ParameterAttributes Attributes {
238 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
239 switch ((Modifier) flags) {
241 return ParameterAttributes.None;
243 return ParameterAttributes.None;
245 return ParameterAttributes.Out;
246 case Modifier.PARAMS:
250 return ParameterAttributes.None;
254 public override AttributeTargets AttributeTargets {
256 return AttributeTargets.Parameter;
261 /// Returns the signature for this parameter evaluating it on the
264 public string GetSignature (EmitContext ec)
266 if (parameter_type == null){
271 return ExternalType ().FullName;
274 public string GetSignatureForError ()
277 if (parameter_type != null)
278 type_name = TypeManager.CSharpName (parameter_type);
279 else if (TypeName.Type != null)
280 type_name = TypeManager.CSharpName (TypeName.Type);
282 type_name = TypeName.ToString ();
284 string mod = GetModifierSignature (ModFlags);
286 return String.Concat (mod, " ", type_name);
291 public static string GetModifierSignature (Modifier mod)
293 switch (mod & unchecked (~Modifier.ISBYREF)) {
296 case Modifier.PARAMS:
300 case Modifier.ARGLIST:
307 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index)
309 ParameterAttributes par_attr = Attributes;
312 builder = cb.DefineParameter (index, par_attr, Name);
314 builder = mb.DefineParameter (index, par_attr, Name);
316 if (OptAttributes != null) {
317 OptAttributes.Emit (ec, this);
321 public override string[] ValidAttributeTargets {
323 return attribute_targets;
329 /// Represents the methods parameters
331 public class Parameters {
332 public Parameter [] FixedParameters;
333 public readonly Parameter ArrayParameter;
334 public readonly bool HasArglist;
338 static Parameters empty_parameters;
340 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter)
342 FixedParameters = fixed_parameters;
343 ArrayParameter = array_parameter;
346 public Parameters (Parameter [] fixed_parameters, bool has_arglist)
348 FixedParameters = fixed_parameters;
349 HasArglist = has_arglist;
353 /// This is used to reuse a set of empty parameters, because they
356 public static Parameters EmptyReadOnlyParameters {
358 if (empty_parameters == null)
359 empty_parameters = new Parameters (null, null);
361 return empty_parameters;
367 return (FixedParameters == null) && (ArrayParameter == null);
371 public void ComputeSignature (EmitContext ec)
374 if (FixedParameters != null){
375 for (int i = 0; i < FixedParameters.Length; i++){
376 Parameter par = FixedParameters [i];
378 signature += par.GetSignature (ec);
382 // Note: as per the spec, the `params' arguments (ArrayParameter)
383 // are not used in the signature computation for a method
387 void Error_DuplicateParameterName (string name, Location loc)
390 100, loc, "The parameter name `" + name + "' is a duplicate");
393 public bool VerifyArgs ()
398 if (FixedParameters == null)
401 count = FixedParameters.Length;
402 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
404 for (i = 0; i < count; i++){
405 string base_name = FixedParameters [i].Name;
406 for (j = i + 1; j < count; j++){
407 if (base_name != FixedParameters [j].Name)
409 Error_DuplicateParameterName (base_name,
410 FixedParameters [i].Location);
414 if (base_name == array_par_name){
415 Error_DuplicateParameterName (base_name,
416 FixedParameters [i].Location);
424 /// Returns the signature of the Parameters evaluated in
425 /// the @ec EmitContext
427 public string GetSignature (EmitContext ec)
429 if (signature == null){
431 ComputeSignature (ec);
438 /// Returns the paramenter information based on the name
440 public Parameter GetParameterByName (string name, out int idx)
445 if (FixedParameters != null){
446 foreach (Parameter par in FixedParameters){
447 if (par.Name == name){
455 if (ArrayParameter != null){
456 if (name == ArrayParameter.Name){
458 return ArrayParameter;
465 public Parameter GetParameterByName (string name)
469 return GetParameterByName (name, out idx);
472 bool ComputeParameterTypes (EmitContext ec)
474 int extra = (ArrayParameter != null) ? 1 : 0;
478 if (FixedParameters == null)
481 pc = extra + FixedParameters.Length;
483 types = new Type [pc];
486 FixedParameters = null;
491 if (FixedParameters != null){
492 foreach (Parameter p in FixedParameters){
496 t = p.ExternalType ();
506 if (ArrayParameter.Resolve (ec))
507 types [i] = ArrayParameter.ExternalType ();
521 /// Returns the argument types as an array
523 static Type [] no_types = new Type [0];
525 public Type [] GetParameterInfo (EmitContext ec)
530 if (FixedParameters == null && ArrayParameter == null)
533 if (ComputeParameterTypes (ec) == false){
542 /// Returns the type of a given parameter, and stores in the `is_out'
543 /// boolean whether this is an out or ref parameter.
545 /// Note that the returned type will not contain any dereference in this
546 /// case (ie, you get "int" for a ref int instead of "int&"
548 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
550 mod = Parameter.Modifier.NONE;
553 FixedParameters = null;
557 if (FixedParameters == null && ArrayParameter == null)
561 if (ComputeParameterTypes (ec) == false)
565 // If this is a request for the variable lenght arg.
567 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
568 if (idx == array_idx)
572 // Otherwise, it is a fixed parameter
574 Parameter p = FixedParameters [idx];
577 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
578 mod |= Parameter.Modifier.ISBYREF;
580 return p.ParameterType;
583 public CallingConventions GetCallingConvention ()
586 return CallingConventions.VarArgs;
588 return CallingConventions.Standard;
592 // The method's attributes are passed in because we need to extract
593 // the "return:" attribute from there to apply on the return type
595 public void LabelParameters (EmitContext ec, MethodBase builder)
598 // Define each type attribute (in/out/ref) and
599 // the argument names.
603 MethodBuilder mb = builder as MethodBuilder;
604 ConstructorBuilder cb = builder as ConstructorBuilder;
606 if (FixedParameters != null) {
607 for (i = 0; i < FixedParameters.Length; i++) {
608 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1);
612 if (ArrayParameter != null){
614 Parameter array_param = ArrayParameter;
617 pb = cb.DefineParameter (
618 i + 1, array_param.Attributes,
621 pb = mb.DefineParameter (
622 i + 1, array_param.Attributes,
625 CustomAttributeBuilder a = new CustomAttributeBuilder (
626 TypeManager.cons_param_array_attribute, new object [0]);
628 pb.SetCustomAttribute (a);
632 public string GetSignatureForError ()
634 StringBuilder sb = new StringBuilder ("(");
635 if (FixedParameters != null) {
636 for (int i = 0; i < FixedParameters.Length; ++i) {
637 sb.Append (FixedParameters[i].GetSignatureForError ());
638 if (i < FixedParameters.Length - 1)
643 return sb.ToString ();