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 ();
35 if (marshal != null) {
36 builder.SetMarshal (marshal);
39 Report.Warning_T (-24, a.Location);
43 builder.SetCustomAttribute (cb);
46 public override bool IsClsCompliaceRequired(DeclSpace ds)
53 /// Class for applying custom attributes on the return type
55 public class ReturnParameter: ParameterBase {
56 public ReturnParameter (MethodBuilder mb, Location location):
60 builder = mb.DefineParameter (0, ParameterAttributes.None, "");
62 catch (ArgumentOutOfRangeException) {
63 Report.Warning_T (-28, location);
67 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
69 // This occurs after Warning -28
73 base.ApplyAttributeBuilder (a, cb);
76 public override AttributeTargets AttributeTargets {
78 return AttributeTargets.ReturnValue;
85 protected override string[] ValidAttributeTargets {
93 /// Class for applying custom attributes on the implicit parameter type
94 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
96 public class ImplicitParameter: ParameterBase {
97 public ImplicitParameter (MethodBuilder mb):
100 builder = mb.DefineParameter (1, ParameterAttributes.None, "");
103 public override AttributeTargets AttributeTargets {
105 return AttributeTargets.Parameter;
112 protected override string[] ValidAttributeTargets {
121 /// Represents a single method parameter
124 //TODO: Add location member to this or base class for better error location and all methods simplification.
125 public class Parameter : ParameterBase {
127 public enum Modifier : byte {
132 // This is a flag which says that it's either REF or OUT.
137 static string[] attribute_targets = new string [] { "param" };
139 public readonly Expression TypeName;
140 public readonly Modifier ModFlags;
141 public readonly string Name;
142 GenericConstraints constraints;
145 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
154 // Resolve is used in method definitions
156 public bool Resolve (DeclSpace ds, Location l)
158 TypeExpr texpr = ds.ResolveTypeExpr (TypeName, false, l);
162 TypeParameterExpr tparam = texpr as TypeParameterExpr;
164 constraints = tparam.TypeParameter.Constraints;
166 parameter_type = ds.ResolveType (texpr, l);
168 if (parameter_type == TypeManager.void_type){
169 Report.Error (1536, l, "`void' parameter is not permitted");
173 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
174 if (parameter_type == TypeManager.typed_reference_type ||
175 parameter_type == TypeManager.arg_iterator_type){
176 Report.Error (1601, l,
177 "out or ref parameter can not be of type TypedReference or ArgIterator");
182 return parameter_type != null;
185 public Type ExternalType ()
187 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
188 return TypeManager.GetReferenceType (parameter_type);
190 return parameter_type;
193 public Type ParameterType {
195 return parameter_type;
199 public GenericConstraints GenericConstraints {
205 public ParameterAttributes Attributes {
207 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
208 switch ((Modifier) flags) {
210 return ParameterAttributes.None;
212 return ParameterAttributes.None;
214 return ParameterAttributes.Out;
215 case Modifier.PARAMS:
219 return ParameterAttributes.None;
223 public override AttributeTargets AttributeTargets {
225 return AttributeTargets.Parameter;
230 /// Returns the signature for this parameter evaluating it on the
233 public string GetSignature (DeclSpace ds, Location loc)
235 if (parameter_type == null){
236 if (!Resolve (ds, loc))
240 return ExternalType ().FullName;
243 public string GetSignatureForError ()
245 string typeName = TypeManager.CSharpName (parameter_type);
246 switch (ModFlags & ~Modifier.ISBYREF) {
248 return "out " + typeName;
249 case Modifier.PARAMS:
250 return "params " + typeName;
252 return "ref " + typeName;
257 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
259 ParameterAttributes par_attr = Attributes;
262 builder = cb.DefineParameter (index, par_attr, Name);
264 builder = mb.DefineParameter (index, par_attr, Name);
266 if (OptAttributes != null) {
267 OptAttributes.Emit (ec, this);
269 if (par_attr == ParameterAttributes.Out){
270 if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
271 Report.Error (36, loc, "Can not use [In] attribute on out parameter");
276 protected override string[] ValidAttributeTargets {
278 return attribute_targets;
284 /// Represents the methods parameters
286 public class Parameters {
287 public Parameter [] FixedParameters;
288 public readonly Parameter ArrayParameter;
289 public readonly bool HasArglist;
294 static Parameters empty_parameters;
296 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
298 FixedParameters = fixed_parameters;
299 ArrayParameter = array_parameter;
303 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
305 FixedParameters = fixed_parameters;
306 HasArglist = has_arglist;
311 /// This is used to reuse a set of empty parameters, because they
314 public static Parameters EmptyReadOnlyParameters {
316 if (empty_parameters == null)
317 empty_parameters = new Parameters (null, null, Location.Null);
319 return empty_parameters;
325 return (FixedParameters == null) && (ArrayParameter == null);
329 public void ComputeSignature (DeclSpace ds)
332 if (FixedParameters != null){
333 for (int i = 0; i < FixedParameters.Length; i++){
334 Parameter par = FixedParameters [i];
336 signature += par.GetSignature (ds, loc);
340 // Note: as per the spec, the `params' arguments (ArrayParameter)
341 // are not used in the signature computation for a method
345 void Error_DuplicateParameterName (string name)
348 100, loc, "The parameter name `" + name + "' is a duplicate");
351 public bool VerifyArgs ()
356 if (FixedParameters == null)
359 count = FixedParameters.Length;
360 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
361 for (i = 0; i < count; i++){
362 string base_name = FixedParameters [i].Name;
364 for (j = i + 1; j < count; j++){
365 if (base_name != FixedParameters [j].Name)
367 Error_DuplicateParameterName (base_name);
371 if (base_name == array_par_name){
372 Error_DuplicateParameterName (base_name);
380 /// Returns the signature of the Parameters evaluated in
381 /// the @tc environment
383 public string GetSignature (DeclSpace ds)
385 if (signature == null){
387 ComputeSignature (ds);
394 /// Returns the paramenter information based on the name
396 public Parameter GetParameterByName (string name, out int idx)
401 if (FixedParameters != null){
402 foreach (Parameter par in FixedParameters){
403 if (par.Name == name){
411 if (ArrayParameter != null){
412 if (name == ArrayParameter.Name){
414 return ArrayParameter;
421 bool ComputeParameterTypes (DeclSpace ds)
423 int extra = (ArrayParameter != null) ? 1 : 0;
427 if (FixedParameters == null)
430 pc = extra + FixedParameters.Length;
432 types = new Type [pc];
435 FixedParameters = null;
440 if (FixedParameters != null){
441 foreach (Parameter p in FixedParameters){
444 if (p.Resolve (ds, loc))
445 t = p.ExternalType ();
455 if (ArrayParameter.Resolve (ds, loc))
456 types [i] = ArrayParameter.ExternalType ();
470 // This variant is used by Delegates, because they need to
471 // resolve/define names, instead of the plain LookupType
473 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
475 int extra = (ArrayParameter != null) ? 1 : 0;
479 if (FixedParameters == null)
482 pc = extra + FixedParameters.Length;
484 types = new Type [pc];
487 FixedParameters = null;
493 if (FixedParameters != null){
494 foreach (Parameter p in FixedParameters){
497 if (p.Resolve (ds, loc))
498 t = p.ExternalType ();
508 if (ArrayParameter.Resolve (ds, loc))
509 types [i] = ArrayParameter.ExternalType ();
515 // invalidate the cached types
525 /// Returns the argument types as an array
527 static Type [] no_types = new Type [0];
529 public Type [] GetParameterInfo (DeclSpace ds)
534 if (FixedParameters == null && ArrayParameter == null)
537 if (ComputeParameterTypes (ds) == false){
546 /// Returns the type of a given parameter, and stores in the `is_out'
547 /// boolean whether this is an out or ref parameter.
549 /// Note that the returned type will not contain any dereference in this
550 /// case (ie, you get "int" for a ref int instead of "int&"
552 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
554 mod = Parameter.Modifier.NONE;
557 FixedParameters = null;
561 if (FixedParameters == null && ArrayParameter == null)
565 if (ComputeParameterTypes (ds) == false)
569 // If this is a request for the variable lenght arg.
571 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
572 if (idx == array_idx)
576 // Otherwise, it is a fixed parameter
578 Parameter p = FixedParameters [idx];
581 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
582 mod |= Parameter.Modifier.ISBYREF;
584 return p.ParameterType;
587 public CallingConventions GetCallingConvention ()
590 return CallingConventions.VarArgs;
592 return CallingConventions.Standard;
596 // The method's attributes are passed in because we need to extract
597 // the "return:" attribute from there to apply on the return type
599 public void LabelParameters (EmitContext ec,
603 // Define each type attribute (in/out/ref) and
604 // the argument names.
608 MethodBuilder mb = builder as MethodBuilder;
609 ConstructorBuilder cb = builder as ConstructorBuilder;
611 if (FixedParameters != null) {
612 for (i = 0; i < FixedParameters.Length; i++) {
613 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
617 if (ArrayParameter != null){
619 Parameter array_param = ArrayParameter;
622 pb = cb.DefineParameter (
623 i + 1, array_param.Attributes,
626 pb = mb.DefineParameter (
627 i + 1, array_param.Attributes,
630 CustomAttributeBuilder a = new CustomAttributeBuilder (
631 TypeManager.cons_param_array_attribute, new object [0]);
633 pb.SetCustomAttribute (a);