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 (this);
35 if (marshal != null) {
36 builder.SetMarshal (marshal);
41 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
42 a.Error_InvalidSecurityParent ();
46 builder.SetCustomAttribute (cb);
49 public override bool IsClsCompliaceRequired(DeclSpace ds)
56 /// Class for applying custom attributes on the return type
58 public class ReturnParameter: ParameterBase {
59 public ReturnParameter (MethodBuilder mb, Location location):
63 builder = mb.DefineParameter (0, ParameterAttributes.None, "");
65 catch (ArgumentOutOfRangeException) {
66 Report.Warning (-28, location, "The Microsoft .NET Runtime 1.x does not permit setting custom attributes on the return type");
70 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
72 // This occurs after Warning -28
76 base.ApplyAttributeBuilder (a, cb);
79 public override AttributeTargets AttributeTargets {
81 return AttributeTargets.ReturnValue;
88 public override string[] ValidAttributeTargets {
96 /// Class for applying custom attributes on the implicit parameter type
97 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
99 public class ImplicitParameter: ParameterBase {
100 public ImplicitParameter (MethodBuilder mb):
103 builder = mb.DefineParameter (1, ParameterAttributes.None, "");
106 public override AttributeTargets AttributeTargets {
108 return AttributeTargets.Parameter;
115 public override string[] ValidAttributeTargets {
124 /// Represents a single method parameter
127 //TODO: Add location member to this or base class for better error location and all methods simplification.
128 public class Parameter : ParameterBase {
130 public enum Modifier : byte {
135 // This is a flag which says that it's either REF or OUT.
140 static string[] attribute_targets = new string [] { "param" };
142 public Expression TypeName;
143 public readonly Modifier ModFlags;
144 public readonly string Name;
145 GenericConstraints constraints;
148 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
156 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
158 if (a.Type == TypeManager.param_array_type) {
159 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
162 base.ApplyAttributeBuilder (a, cb);
166 // Resolve is used in method definitions
168 public bool Resolve (EmitContext ec, Location l)
170 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec);
174 TypeParameterExpr tparam = texpr as TypeParameterExpr;
176 constraints = tparam.TypeParameter.Constraints;
178 parameter_type = texpr.Type;
180 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
181 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
185 if (parameter_type == TypeManager.void_type){
186 Report.Error (1536, l, "`void' parameter is not permitted");
190 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
191 if (parameter_type == TypeManager.typed_reference_type ||
192 parameter_type == TypeManager.arg_iterator_type){
193 Report.Error (1601, l,
194 "out or ref parameter can not be of type TypedReference or ArgIterator");
199 return parameter_type != null;
202 public Type ExternalType ()
204 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
205 return TypeManager.GetReferenceType (parameter_type);
207 return parameter_type;
210 public Type ParameterType {
212 return parameter_type;
216 public GenericConstraints GenericConstraints {
222 public ParameterAttributes Attributes {
224 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
225 switch ((Modifier) flags) {
227 return ParameterAttributes.None;
229 return ParameterAttributes.None;
231 return ParameterAttributes.Out;
232 case Modifier.PARAMS:
236 return ParameterAttributes.None;
240 public static ParameterAttributes GetParameterAttributes (Modifier mod)
242 int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
243 switch ((Modifier) flags) {
245 return ParameterAttributes.None;
247 return ParameterAttributes.None;
249 return ParameterAttributes.Out;
250 case Modifier.PARAMS:
254 return ParameterAttributes.None;
257 public override AttributeTargets AttributeTargets {
259 return AttributeTargets.Parameter;
264 /// Returns the signature for this parameter evaluating it on the
267 public string GetSignature (EmitContext ec, Location loc)
269 if (parameter_type == null){
270 if (!Resolve (ec, loc))
274 return ExternalType ().FullName;
277 public string GetSignatureForError ()
280 if (parameter_type != null)
281 typeName = TypeManager.CSharpName (parameter_type);
282 else if (TypeName.Type != null)
283 typeName = TypeManager.CSharpName (TypeName.Type);
285 typeName = TypeName.ToString ();
287 switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
289 return "out " + typeName;
290 case Modifier.PARAMS:
291 return "params " + typeName;
293 return "ref " + typeName;
298 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
300 ParameterAttributes par_attr = Attributes;
303 builder = cb.DefineParameter (index, par_attr, Name);
305 builder = mb.DefineParameter (index, par_attr, Name);
307 if (OptAttributes != null) {
308 OptAttributes.Emit (ec, this);
310 if (par_attr == ParameterAttributes.Out){
311 if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
312 Report.Error (36, loc, "Can not use [In] attribute on out parameter");
317 public override string[] ValidAttributeTargets {
319 return attribute_targets;
325 /// Represents the methods parameters
327 public class Parameters {
328 public Parameter [] FixedParameters;
329 public readonly Parameter ArrayParameter;
330 public readonly bool HasArglist;
335 static Parameters empty_parameters;
337 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
339 FixedParameters = fixed_parameters;
340 ArrayParameter = array_parameter;
344 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
346 FixedParameters = fixed_parameters;
347 HasArglist = has_arglist;
352 /// This is used to reuse a set of empty parameters, because they
355 public static Parameters EmptyReadOnlyParameters {
357 if (empty_parameters == null)
358 empty_parameters = new Parameters (null, null, Location.Null);
360 return empty_parameters;
366 return (FixedParameters == null) && (ArrayParameter == null);
370 public void ComputeSignature (EmitContext ec)
373 if (FixedParameters != null){
374 for (int i = 0; i < FixedParameters.Length; i++){
375 Parameter par = FixedParameters [i];
377 signature += par.GetSignature (ec, loc);
381 // Note: as per the spec, the `params' arguments (ArrayParameter)
382 // are not used in the signature computation for a method
386 void Error_DuplicateParameterName (string name)
389 100, loc, "The parameter name `" + name + "' is a duplicate");
392 public bool VerifyArgs ()
397 if (FixedParameters == null)
400 count = FixedParameters.Length;
401 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
403 for (i = 0; i < count; i++){
404 string base_name = FixedParameters [i].Name;
405 for (j = i + 1; j < count; j++){
406 if (base_name != FixedParameters [j].Name)
408 Error_DuplicateParameterName (base_name);
412 if (base_name == array_par_name){
413 Error_DuplicateParameterName (base_name);
421 /// Returns the signature of the Parameters evaluated in
422 /// the @ec EmitContext
424 public string GetSignature (EmitContext ec)
426 if (signature == null){
428 ComputeSignature (ec);
435 /// Returns the paramenter information based on the name
437 public Parameter GetParameterByName (string name, out int idx)
442 if (FixedParameters != null){
443 foreach (Parameter par in FixedParameters){
444 if (par.Name == name){
452 if (ArrayParameter != null){
453 if (name == ArrayParameter.Name){
455 return ArrayParameter;
462 public Parameter GetParameterByName (string name)
466 return GetParameterByName (name, out idx);
469 bool ComputeParameterTypes (EmitContext ec)
471 int extra = (ArrayParameter != null) ? 1 : 0;
475 if (FixedParameters == null)
478 pc = extra + FixedParameters.Length;
480 types = new Type [pc];
483 FixedParameters = null;
488 if (FixedParameters != null){
489 foreach (Parameter p in FixedParameters){
492 if (p.Resolve (ec, loc))
493 t = p.ExternalType ();
503 if (ArrayParameter.Resolve (ec, loc))
504 types [i] = ArrayParameter.ExternalType ();
518 // This variant is used by Delegates, because they need to
519 // resolve/define names, instead of the plain LookupType
521 public bool ComputeAndDefineParameterTypes (EmitContext ec)
523 bool old_type_resolving = ec.ResolvingTypeTree;
524 ec.ResolvingTypeTree = true;
525 bool retval = ComputeParameterTypes (ec);
526 ec.ResolvingTypeTree = old_type_resolving;
531 /// Returns the argument types as an array
533 static Type [] no_types = new Type [0];
535 public Type [] GetParameterInfo (EmitContext ec)
540 if (FixedParameters == null && ArrayParameter == null)
543 if (ComputeParameterTypes (ec) == false){
552 /// Returns the type of a given parameter, and stores in the `is_out'
553 /// boolean whether this is an out or ref parameter.
555 /// Note that the returned type will not contain any dereference in this
556 /// case (ie, you get "int" for a ref int instead of "int&"
558 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
560 mod = Parameter.Modifier.NONE;
563 FixedParameters = null;
567 if (FixedParameters == null && ArrayParameter == null)
571 if (ComputeParameterTypes (ec) == false)
575 // If this is a request for the variable lenght arg.
577 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
578 if (idx == array_idx)
582 // Otherwise, it is a fixed parameter
584 Parameter p = FixedParameters [idx];
587 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
588 mod |= Parameter.Modifier.ISBYREF;
590 return p.ParameterType;
593 public CallingConventions GetCallingConvention ()
596 return CallingConventions.VarArgs;
598 return CallingConventions.Standard;
602 // The method's attributes are passed in because we need to extract
603 // the "return:" attribute from there to apply on the return type
605 public void LabelParameters (EmitContext ec,
609 // Define each type attribute (in/out/ref) and
610 // the argument names.
614 MethodBuilder mb = builder as MethodBuilder;
615 ConstructorBuilder cb = builder as ConstructorBuilder;
617 if (FixedParameters != null) {
618 for (i = 0; i < FixedParameters.Length; i++) {
619 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
623 if (ArrayParameter != null){
625 Parameter array_param = ArrayParameter;
628 pb = cb.DefineParameter (
629 i + 1, array_param.Attributes,
632 pb = mb.DefineParameter (
633 i + 1, array_param.Attributes,
636 CustomAttributeBuilder a = new CustomAttributeBuilder (
637 TypeManager.cons_param_array_attribute, new object [0]);
639 pb.SetCustomAttribute (a);