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;
143 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
152 // Resolve is used in method definitions
154 public bool Resolve (DeclSpace ds, Location l)
156 parameter_type = ds.ResolveType (TypeName, false, l);
158 if (parameter_type == TypeManager.void_type){
159 Report.Error (1536, l, "`void' parameter is not permitted");
163 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
164 if (parameter_type == TypeManager.typed_reference_type ||
165 parameter_type == TypeManager.arg_iterator_type){
166 Report.Error (1601, l,
167 "out or ref parameter can not be of type TypedReference or ArgIterator");
172 return parameter_type != null;
175 public Type ExternalType ()
177 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
178 return TypeManager.GetReferenceType (parameter_type);
180 return parameter_type;
183 public Type ParameterType {
185 return parameter_type;
189 public ParameterAttributes Attributes {
191 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
192 switch ((Modifier) flags) {
194 return ParameterAttributes.None;
196 return ParameterAttributes.None;
198 return ParameterAttributes.Out;
199 case Modifier.PARAMS:
203 return ParameterAttributes.None;
207 public override AttributeTargets AttributeTargets {
209 return AttributeTargets.Parameter;
214 /// Returns the signature for this parameter evaluating it on the
217 public string GetSignature (DeclSpace ds, Location loc)
219 if (parameter_type == null){
220 if (!Resolve (ds, loc))
224 return ExternalType ().FullName;
227 public string GetSignatureForError ()
229 string typeName = TypeManager.CSharpName (parameter_type);
230 switch (ModFlags & ~Modifier.ISBYREF) {
232 return "out " + typeName;
233 case Modifier.PARAMS:
234 return "params " + typeName;
236 return "ref " + typeName;
241 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
243 ParameterAttributes par_attr = Attributes;
246 builder = cb.DefineParameter (index, par_attr, Name);
248 builder = mb.DefineParameter (index, par_attr, Name);
250 if (OptAttributes != null) {
251 OptAttributes.Emit (ec, this);
253 if (par_attr == ParameterAttributes.Out){
254 if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
255 Report.Error (36, loc, "Can not use [In] attribute on out parameter");
260 protected override string[] ValidAttributeTargets {
262 return attribute_targets;
268 /// Represents the methods parameters
270 public class Parameters {
271 public Parameter [] FixedParameters;
272 public readonly Parameter ArrayParameter;
277 static Parameters empty_parameters;
279 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
281 FixedParameters = fixed_parameters;
282 ArrayParameter = array_parameter;
287 /// This is used to reuse a set of empty parameters, because they
290 public static Parameters EmptyReadOnlyParameters {
292 if (empty_parameters == null)
293 empty_parameters = new Parameters (null, null, Location.Null);
295 return empty_parameters;
301 return (FixedParameters == null) && (ArrayParameter == null);
305 public void ComputeSignature (DeclSpace ds)
308 if (FixedParameters != null){
309 for (int i = 0; i < FixedParameters.Length; i++){
310 Parameter par = FixedParameters [i];
312 signature += par.GetSignature (ds, loc);
316 // Note: as per the spec, the `params' arguments (ArrayParameter)
317 // are not used in the signature computation for a method
321 void Error_DuplicateParameterName (string name)
324 100, loc, "The parameter name `" + name + "' is a duplicate");
327 public bool VerifyArgs ()
332 if (FixedParameters == null)
335 count = FixedParameters.Length;
336 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
337 for (i = 0; i < count; i++){
338 string base_name = FixedParameters [i].Name;
340 for (j = i + 1; j < count; j++){
341 if (base_name != FixedParameters [j].Name)
343 Error_DuplicateParameterName (base_name);
347 if (base_name == array_par_name){
348 Error_DuplicateParameterName (base_name);
356 /// Returns the signature of the Parameters evaluated in
357 /// the @tc environment
359 public string GetSignature (DeclSpace ds)
361 if (signature == null){
363 ComputeSignature (ds);
370 /// Returns the paramenter information based on the name
372 public Parameter GetParameterByName (string name, out int idx)
377 if (FixedParameters != null){
378 foreach (Parameter par in FixedParameters){
379 if (par.Name == name){
387 if (ArrayParameter != null){
388 if (name == ArrayParameter.Name){
390 return ArrayParameter;
397 bool ComputeParameterTypes (DeclSpace ds)
399 int extra = (ArrayParameter != null) ? 1 : 0;
403 if (FixedParameters == null)
406 pc = extra + FixedParameters.Length;
408 types = new Type [pc];
411 FixedParameters = null;
416 if (FixedParameters != null){
417 foreach (Parameter p in FixedParameters){
420 if (p.Resolve (ds, loc))
421 t = p.ExternalType ();
431 if (ArrayParameter.Resolve (ds, loc))
432 types [i] = ArrayParameter.ExternalType ();
446 // This variant is used by Delegates, because they need to
447 // resolve/define names, instead of the plain LookupType
449 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
451 int extra = (ArrayParameter != null) ? 1 : 0;
455 if (FixedParameters == null)
458 pc = extra + FixedParameters.Length;
460 types = new Type [pc];
463 FixedParameters = null;
469 if (FixedParameters != null){
470 foreach (Parameter p in FixedParameters){
473 if (p.Resolve (ds, loc))
474 t = p.ExternalType ();
484 if (ArrayParameter.Resolve (ds, loc))
485 types [i] = ArrayParameter.ExternalType ();
491 // invalidate the cached types
501 /// Returns the argument types as an array
503 static Type [] no_types = new Type [0];
505 public Type [] GetParameterInfo (DeclSpace ds)
510 if (FixedParameters == null && ArrayParameter == null)
513 if (ComputeParameterTypes (ds) == false){
522 /// Returns the type of a given parameter, and stores in the `is_out'
523 /// boolean whether this is an out or ref parameter.
525 /// Note that the returned type will not contain any dereference in this
526 /// case (ie, you get "int" for a ref int instead of "int&"
528 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
530 mod = Parameter.Modifier.NONE;
533 FixedParameters = null;
537 if (FixedParameters == null && ArrayParameter == null)
541 if (ComputeParameterTypes (ds) == false)
545 // If this is a request for the variable lenght arg.
547 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
548 if (idx == array_idx)
552 // Otherwise, it is a fixed parameter
554 Parameter p = FixedParameters [idx];
557 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
558 mod |= Parameter.Modifier.ISBYREF;
560 return p.ParameterType;
563 public CallingConventions GetCallingConvention ()
565 // For now this is the only correc thing to do
566 return CallingConventions.Standard;
570 // The method's attributes are passed in because we need to extract
571 // the "return:" attribute from there to apply on the return type
573 public void LabelParameters (EmitContext ec,
577 // Define each type attribute (in/out/ref) and
578 // the argument names.
582 MethodBuilder mb = builder as MethodBuilder;
583 ConstructorBuilder cb = builder as ConstructorBuilder;
585 if (FixedParameters != null) {
586 for (i = 0; i < FixedParameters.Length; i++) {
587 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
591 if (ArrayParameter != null){
593 Parameter array_param = ArrayParameter;
596 pb = cb.DefineParameter (
597 i + 1, array_param.Attributes,
600 pb = mb.DefineParameter (
601 i + 1, array_param.Attributes,
604 CustomAttributeBuilder a = new CustomAttributeBuilder (
605 TypeManager.cons_param_array_attribute, new object [0]);
607 pb.SetCustomAttribute (a);