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.
136 static string[] attribute_targets = new string [] { "param" };
138 public readonly Expression TypeName;
139 public readonly Modifier ModFlags;
140 public readonly string Name;
141 GenericConstraints constraints;
144 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
153 // Resolve is used in method definitions
155 public bool Resolve (DeclSpace ds, Location l)
157 TypeExpr texpr = ds.ResolveTypeExpr (TypeName, false, l);
161 TypeParameterExpr tparam = texpr as TypeParameterExpr;
163 constraints = tparam.TypeParameter.Constraints;
165 parameter_type = ds.ResolveType (texpr, l);
167 if (parameter_type == TypeManager.void_type){
168 Report.Error (1536, l, "`void' parameter is not permitted");
172 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
173 if (parameter_type == TypeManager.typed_reference_type ||
174 parameter_type == TypeManager.arg_iterator_type){
175 Report.Error (1601, l,
176 "out or ref parameter can not be of type TypedReference or ArgIterator");
181 return parameter_type != null;
184 public Type ExternalType ()
186 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
187 return TypeManager.GetReferenceType (parameter_type);
189 return parameter_type;
192 public Type ParameterType {
194 return parameter_type;
198 public GenericConstraints GenericConstraints {
204 public ParameterAttributes Attributes {
206 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
207 switch ((Modifier) flags) {
209 return ParameterAttributes.None;
211 return ParameterAttributes.None;
213 return ParameterAttributes.Out;
214 case Modifier.PARAMS:
218 return ParameterAttributes.None;
222 public override AttributeTargets AttributeTargets {
224 return AttributeTargets.Parameter;
229 /// Returns the signature for this parameter evaluating it on the
232 public string GetSignature (DeclSpace ds, Location loc)
234 if (parameter_type == null){
235 if (!Resolve (ds, loc))
239 return ExternalType ().FullName;
242 public string GetSignatureForError ()
244 string typeName = TypeManager.CSharpName (parameter_type);
245 switch (ModFlags & ~Modifier.ISBYREF) {
247 return "out " + typeName;
248 case Modifier.PARAMS:
249 return "params " + typeName;
251 return "ref " + typeName;
256 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
258 ParameterAttributes par_attr = Attributes;
261 builder = cb.DefineParameter (index, par_attr, Name);
263 builder = mb.DefineParameter (index, par_attr, Name);
265 if (OptAttributes != null) {
266 OptAttributes.Emit (ec, this);
268 if (par_attr == ParameterAttributes.Out){
269 if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
270 Report.Error (36, loc, "Can not use [In] attribute on out parameter");
275 protected override string[] ValidAttributeTargets {
277 return attribute_targets;
283 /// Represents the methods parameters
285 public class Parameters {
286 public Parameter [] FixedParameters;
287 public readonly Parameter ArrayParameter;
292 static Parameters empty_parameters;
294 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
296 FixedParameters = fixed_parameters;
297 ArrayParameter = array_parameter;
302 /// This is used to reuse a set of empty parameters, because they
305 public static Parameters EmptyReadOnlyParameters {
307 if (empty_parameters == null)
308 empty_parameters = new Parameters (null, null, Location.Null);
310 return empty_parameters;
316 return (FixedParameters == null) && (ArrayParameter == null);
320 public void ComputeSignature (DeclSpace ds)
323 if (FixedParameters != null){
324 for (int i = 0; i < FixedParameters.Length; i++){
325 Parameter par = FixedParameters [i];
327 signature += par.GetSignature (ds, loc);
331 // Note: as per the spec, the `params' arguments (ArrayParameter)
332 // are not used in the signature computation for a method
336 void Error_DuplicateParameterName (string name)
339 100, loc, "The parameter name `" + name + "' is a duplicate");
342 public bool VerifyArgs ()
347 if (FixedParameters == null)
350 count = FixedParameters.Length;
351 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
352 for (i = 0; i < count; i++){
353 string base_name = FixedParameters [i].Name;
355 for (j = i + 1; j < count; j++){
356 if (base_name != FixedParameters [j].Name)
358 Error_DuplicateParameterName (base_name);
362 if (base_name == array_par_name){
363 Error_DuplicateParameterName (base_name);
371 /// Returns the signature of the Parameters evaluated in
372 /// the @tc environment
374 public string GetSignature (DeclSpace ds)
376 if (signature == null){
378 ComputeSignature (ds);
385 /// Returns the paramenter information based on the name
387 public Parameter GetParameterByName (string name, out int idx)
392 if (FixedParameters != null){
393 foreach (Parameter par in FixedParameters){
394 if (par.Name == name){
402 if (ArrayParameter != null){
403 if (name == ArrayParameter.Name){
405 return ArrayParameter;
412 bool ComputeParameterTypes (DeclSpace ds)
414 int extra = (ArrayParameter != null) ? 1 : 0;
418 if (FixedParameters == null)
421 pc = extra + FixedParameters.Length;
423 types = new Type [pc];
426 FixedParameters = null;
431 if (FixedParameters != null){
432 foreach (Parameter p in FixedParameters){
435 if (p.Resolve (ds, loc))
436 t = p.ExternalType ();
446 if (ArrayParameter.Resolve (ds, loc))
447 types [i] = ArrayParameter.ExternalType ();
461 // This variant is used by Delegates, because they need to
462 // resolve/define names, instead of the plain LookupType
464 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
466 int extra = (ArrayParameter != null) ? 1 : 0;
470 if (FixedParameters == null)
473 pc = extra + FixedParameters.Length;
475 types = new Type [pc];
478 FixedParameters = null;
484 if (FixedParameters != null){
485 foreach (Parameter p in FixedParameters){
488 if (p.Resolve (ds, loc))
489 t = p.ExternalType ();
499 if (ArrayParameter.Resolve (ds, loc))
500 types [i] = ArrayParameter.ExternalType ();
506 // invalidate the cached types
516 /// Returns the argument types as an array
518 static Type [] no_types = new Type [0];
520 public Type [] GetParameterInfo (DeclSpace ds)
525 if (FixedParameters == null && ArrayParameter == null)
528 if (ComputeParameterTypes (ds) == false){
537 /// Returns the type of a given parameter, and stores in the `is_out'
538 /// boolean whether this is an out or ref parameter.
540 /// Note that the returned type will not contain any dereference in this
541 /// case (ie, you get "int" for a ref int instead of "int&"
543 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
545 mod = Parameter.Modifier.NONE;
548 FixedParameters = null;
552 if (FixedParameters == null && ArrayParameter == null)
556 if (ComputeParameterTypes (ds) == false)
560 // If this is a request for the variable lenght arg.
562 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
563 if (idx == array_idx)
567 // Otherwise, it is a fixed parameter
569 Parameter p = FixedParameters [idx];
572 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
573 mod |= Parameter.Modifier.ISBYREF;
575 return p.ParameterType;
578 public CallingConventions GetCallingConvention ()
580 // For now this is the only correc thing to do
581 return CallingConventions.Standard;
585 // The method's attributes are passed in because we need to extract
586 // the "return:" attribute from there to apply on the return type
588 public void LabelParameters (EmitContext ec,
592 // Define each type attribute (in/out/ref) and
593 // the argument names.
597 MethodBuilder mb = builder as MethodBuilder;
598 ConstructorBuilder cb = builder as ConstructorBuilder;
600 if (FixedParameters != null) {
601 for (i = 0; i < FixedParameters.Length; i++) {
602 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
606 if (ArrayParameter != null){
608 Parameter array_param = ArrayParameter;
611 pb = cb.DefineParameter (
612 i + 1, array_param.Attributes,
615 pb = mb.DefineParameter (
616 i + 1, array_param.Attributes,
619 CustomAttributeBuilder a = new CustomAttributeBuilder (
620 TypeManager.cons_param_array_attribute, new object [0]);
622 pb.SetCustomAttribute (a);