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 (-24, a.Location, "The Microsoft Runtime cannot set this marshal info. Please use the Mono runtime instead.");
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 (-28, location, "The Microsoft .NET Runtime 1.x does not permit setting custom attributes on the return type");
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 public 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 public 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)
153 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
155 if (a.Type == TypeManager.param_array_type) {
156 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
159 base.ApplyAttributeBuilder (a, cb);
163 // Resolve is used in method definitions
165 public bool Resolve (DeclSpace ds, Location l)
167 TypeExpr texpr = ds.ResolveTypeExpr (TypeName, false, l);
171 TypeParameterExpr tparam = texpr as TypeParameterExpr;
173 constraints = tparam.TypeParameter.Constraints;
175 parameter_type = ds.ResolveType (texpr, l);
177 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
178 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
182 if (parameter_type == TypeManager.void_type){
183 Report.Error (1536, l, "`void' parameter is not permitted");
187 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
188 if (parameter_type == TypeManager.typed_reference_type ||
189 parameter_type == TypeManager.arg_iterator_type){
190 Report.Error (1601, l,
191 "out or ref parameter can not be of type TypedReference or ArgIterator");
196 return parameter_type != null;
199 public Type ExternalType ()
201 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
202 return TypeManager.GetReferenceType (parameter_type);
204 return parameter_type;
207 public Type ParameterType {
209 return parameter_type;
213 public GenericConstraints GenericConstraints {
219 public ParameterAttributes Attributes {
221 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
222 switch ((Modifier) flags) {
224 return ParameterAttributes.None;
226 return ParameterAttributes.None;
228 return ParameterAttributes.Out;
229 case Modifier.PARAMS:
233 return ParameterAttributes.None;
237 public override AttributeTargets AttributeTargets {
239 return AttributeTargets.Parameter;
244 /// Returns the signature for this parameter evaluating it on the
247 public string GetSignature (DeclSpace ds, Location loc)
249 if (parameter_type == null){
250 if (!Resolve (ds, loc))
254 return ExternalType ().FullName;
257 public string GetSignatureForError ()
259 string typeName = TypeManager.CSharpName (parameter_type);
260 switch (ModFlags & ~Modifier.ISBYREF) {
262 return "out " + typeName;
263 case Modifier.PARAMS:
264 return "params " + typeName;
266 return "ref " + typeName;
271 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
273 ParameterAttributes par_attr = Attributes;
276 builder = cb.DefineParameter (index, par_attr, Name);
278 builder = mb.DefineParameter (index, par_attr, Name);
280 if (OptAttributes != null) {
281 OptAttributes.Emit (ec, this);
283 if (par_attr == ParameterAttributes.Out){
284 if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
285 Report.Error (36, loc, "Can not use [In] attribute on out parameter");
290 public override string[] ValidAttributeTargets {
292 return attribute_targets;
298 /// Represents the methods parameters
300 public class Parameters {
301 public Parameter [] FixedParameters;
302 public readonly Parameter ArrayParameter;
303 public readonly bool HasArglist;
308 static Parameters empty_parameters;
310 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
312 FixedParameters = fixed_parameters;
313 ArrayParameter = array_parameter;
317 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
319 FixedParameters = fixed_parameters;
320 HasArglist = has_arglist;
325 /// This is used to reuse a set of empty parameters, because they
328 public static Parameters EmptyReadOnlyParameters {
330 if (empty_parameters == null)
331 empty_parameters = new Parameters (null, null, Location.Null);
333 return empty_parameters;
339 return (FixedParameters == null) && (ArrayParameter == null);
343 public void ComputeSignature (DeclSpace ds)
346 if (FixedParameters != null){
347 for (int i = 0; i < FixedParameters.Length; i++){
348 Parameter par = FixedParameters [i];
350 signature += par.GetSignature (ds, loc);
354 // Note: as per the spec, the `params' arguments (ArrayParameter)
355 // are not used in the signature computation for a method
359 void Error_DuplicateParameterName (string name)
362 100, loc, "The parameter name `" + name + "' is a duplicate");
365 public bool VerifyArgs ()
370 if (FixedParameters == null)
373 count = FixedParameters.Length;
374 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
375 for (i = 0; i < count; i++){
376 string base_name = FixedParameters [i].Name;
378 for (j = i + 1; j < count; j++){
379 if (base_name != FixedParameters [j].Name)
381 Error_DuplicateParameterName (base_name);
385 if (base_name == array_par_name){
386 Error_DuplicateParameterName (base_name);
394 /// Returns the signature of the Parameters evaluated in
395 /// the @tc environment
397 public string GetSignature (DeclSpace ds)
399 if (signature == null){
401 ComputeSignature (ds);
408 /// Returns the paramenter information based on the name
410 public Parameter GetParameterByName (string name, out int idx)
415 if (FixedParameters != null){
416 foreach (Parameter par in FixedParameters){
417 if (par.Name == name){
425 if (ArrayParameter != null){
426 if (name == ArrayParameter.Name){
428 return ArrayParameter;
435 bool ComputeParameterTypes (DeclSpace ds)
437 int extra = (ArrayParameter != null) ? 1 : 0;
441 if (FixedParameters == null)
444 pc = extra + FixedParameters.Length;
446 types = new Type [pc];
449 FixedParameters = null;
454 if (FixedParameters != null){
455 foreach (Parameter p in FixedParameters){
458 if (p.Resolve (ds, loc))
459 t = p.ExternalType ();
469 if (ArrayParameter.Resolve (ds, loc))
470 types [i] = ArrayParameter.ExternalType ();
484 // This variant is used by Delegates, because they need to
485 // resolve/define names, instead of the plain LookupType
487 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
489 int extra = (ArrayParameter != null) ? 1 : 0;
493 if (FixedParameters == null)
496 pc = extra + FixedParameters.Length;
498 types = new Type [pc];
501 FixedParameters = null;
507 if (FixedParameters != null){
508 foreach (Parameter p in FixedParameters){
511 if (p.Resolve (ds, loc))
512 t = p.ExternalType ();
522 if (ArrayParameter.Resolve (ds, loc))
523 types [i] = ArrayParameter.ExternalType ();
529 // invalidate the cached types
539 /// Returns the argument types as an array
541 static Type [] no_types = new Type [0];
543 public Type [] GetParameterInfo (DeclSpace ds)
548 if (FixedParameters == null && ArrayParameter == null)
551 if (ComputeParameterTypes (ds) == false){
560 /// Returns the type of a given parameter, and stores in the `is_out'
561 /// boolean whether this is an out or ref parameter.
563 /// Note that the returned type will not contain any dereference in this
564 /// case (ie, you get "int" for a ref int instead of "int&"
566 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
568 mod = Parameter.Modifier.NONE;
571 FixedParameters = null;
575 if (FixedParameters == null && ArrayParameter == null)
579 if (ComputeParameterTypes (ds) == false)
583 // If this is a request for the variable lenght arg.
585 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
586 if (idx == array_idx)
590 // Otherwise, it is a fixed parameter
592 Parameter p = FixedParameters [idx];
595 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
596 mod |= Parameter.Modifier.ISBYREF;
598 return p.ParameterType;
601 public CallingConventions GetCallingConvention ()
604 return CallingConventions.VarArgs;
606 return CallingConventions.Standard;
610 // The method's attributes are passed in because we need to extract
611 // the "return:" attribute from there to apply on the return type
613 public void LabelParameters (EmitContext ec,
617 // Define each type attribute (in/out/ref) and
618 // the argument names.
622 MethodBuilder mb = builder as MethodBuilder;
623 ConstructorBuilder cb = builder as ConstructorBuilder;
625 if (FixedParameters != null) {
626 for (i = 0; i < FixedParameters.Length; i++) {
627 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
631 if (ArrayParameter != null){
633 Parameter array_param = ArrayParameter;
636 pb = cb.DefineParameter (
637 i + 1, array_param.Attributes,
640 pb = mb.DefineParameter (
641 i + 1, array_param.Attributes,
644 CustomAttributeBuilder a = new CustomAttributeBuilder (
645 TypeManager.cons_param_array_attribute, new object [0]);
647 pb.SetCustomAttribute (a);