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 {
120 /// Represents a single method parameter
123 //TODO: Add location member to this or base class for better error location and all methods simplification.
124 public class Parameter : ParameterBase {
126 public enum Modifier : byte {
131 // 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;
273 public readonly bool HasArglist;
278 static Parameters empty_parameters;
280 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
282 FixedParameters = fixed_parameters;
283 ArrayParameter = array_parameter;
287 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
289 FixedParameters = fixed_parameters;
290 HasArglist = has_arglist;
295 /// This is used to reuse a set of empty parameters, because they
298 public static Parameters EmptyReadOnlyParameters {
300 if (empty_parameters == null)
301 empty_parameters = new Parameters (null, null, Location.Null);
303 return empty_parameters;
309 return (FixedParameters == null) && (ArrayParameter == null);
313 public void ComputeSignature (DeclSpace ds)
316 if (FixedParameters != null){
317 for (int i = 0; i < FixedParameters.Length; i++){
318 Parameter par = FixedParameters [i];
320 signature += par.GetSignature (ds, loc);
324 // Note: as per the spec, the `params' arguments (ArrayParameter)
325 // are not used in the signature computation for a method
329 void Error_DuplicateParameterName (string name)
332 100, loc, "The parameter name `" + name + "' is a duplicate");
335 public bool VerifyArgs ()
340 if (FixedParameters == null)
343 count = FixedParameters.Length;
344 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
345 for (i = 0; i < count; i++){
346 string base_name = FixedParameters [i].Name;
348 for (j = i + 1; j < count; j++){
349 if (base_name != FixedParameters [j].Name)
351 Error_DuplicateParameterName (base_name);
355 if (base_name == array_par_name){
356 Error_DuplicateParameterName (base_name);
364 /// Returns the signature of the Parameters evaluated in
365 /// the @tc environment
367 public string GetSignature (DeclSpace ds)
369 if (signature == null){
371 ComputeSignature (ds);
378 /// Returns the paramenter information based on the name
380 public Parameter GetParameterByName (string name, out int idx)
385 if (FixedParameters != null){
386 foreach (Parameter par in FixedParameters){
387 if (par.Name == name){
395 if (ArrayParameter != null){
396 if (name == ArrayParameter.Name){
398 return ArrayParameter;
405 bool ComputeParameterTypes (DeclSpace ds)
407 int extra = (ArrayParameter != null) ? 1 : 0;
411 if (FixedParameters == null)
414 pc = extra + FixedParameters.Length;
416 types = new Type [pc];
419 FixedParameters = null;
424 if (FixedParameters != null){
425 foreach (Parameter p in FixedParameters){
428 if (p.Resolve (ds, loc))
429 t = p.ExternalType ();
439 if (ArrayParameter.Resolve (ds, loc))
440 types [i] = ArrayParameter.ExternalType ();
454 // This variant is used by Delegates, because they need to
455 // resolve/define names, instead of the plain LookupType
457 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
459 int extra = (ArrayParameter != null) ? 1 : 0;
463 if (FixedParameters == null)
466 pc = extra + FixedParameters.Length;
468 types = new Type [pc];
471 FixedParameters = null;
477 if (FixedParameters != null){
478 foreach (Parameter p in FixedParameters){
481 if (p.Resolve (ds, loc))
482 t = p.ExternalType ();
492 if (ArrayParameter.Resolve (ds, loc))
493 types [i] = ArrayParameter.ExternalType ();
499 // invalidate the cached types
509 /// Returns the argument types as an array
511 static Type [] no_types = new Type [0];
513 public Type [] GetParameterInfo (DeclSpace ds)
518 if (FixedParameters == null && ArrayParameter == null)
521 if (ComputeParameterTypes (ds) == false){
530 /// Returns the type of a given parameter, and stores in the `is_out'
531 /// boolean whether this is an out or ref parameter.
533 /// Note that the returned type will not contain any dereference in this
534 /// case (ie, you get "int" for a ref int instead of "int&"
536 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
538 mod = Parameter.Modifier.NONE;
541 FixedParameters = null;
545 if (FixedParameters == null && ArrayParameter == null)
549 if (ComputeParameterTypes (ds) == false)
553 // If this is a request for the variable lenght arg.
555 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
556 if (idx == array_idx)
560 // Otherwise, it is a fixed parameter
562 Parameter p = FixedParameters [idx];
565 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
566 mod |= Parameter.Modifier.ISBYREF;
568 return p.ParameterType;
571 public CallingConventions GetCallingConvention ()
574 return CallingConventions.VarArgs;
576 return CallingConventions.Standard;
580 // The method's attributes are passed in because we need to extract
581 // the "return:" attribute from there to apply on the return type
583 public void LabelParameters (EmitContext ec,
587 // Define each type attribute (in/out/ref) and
588 // the argument names.
592 MethodBuilder mb = builder as MethodBuilder;
593 ConstructorBuilder cb = builder as ConstructorBuilder;
595 if (FixedParameters != null) {
596 for (i = 0; i < FixedParameters.Length; i++) {
597 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
601 if (ArrayParameter != null){
603 Parameter array_param = ArrayParameter;
606 pb = cb.DefineParameter (
607 i + 1, array_param.Attributes,
610 pb = mb.DefineParameter (
611 i + 1, array_param.Attributes,
614 CustomAttributeBuilder a = new CustomAttributeBuilder (
615 TypeManager.cons_param_array_attribute, new object [0]);
617 pb.SetCustomAttribute (a);