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 {
136 // This is a flag which says that it's either REF or OUT.
139 // This value is changed from the one in mbas source code
143 static string[] attribute_targets = new string [] { "param" };
145 public Expression TypeName;
146 public readonly Modifier ModFlags;
147 public readonly string Name;
148 GenericConstraints constraints;
151 /* These two fields are mbas specific. The corresponding
152 changes need to be merged from mbas */
154 public readonly Expression ParameterInitializer;
155 public readonly bool IsOptional;
157 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
163 ParameterInitializer = null;
167 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi)
173 ParameterInitializer = pi;
177 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi, bool opt)
183 ParameterInitializer = pi;
187 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
189 if (a.Type == TypeManager.param_array_type) {
190 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
193 base.ApplyAttributeBuilder (a, cb);
197 // Resolve is used in method definitions
199 public bool Resolve (EmitContext ec, Location l)
201 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec);
205 TypeParameterExpr tparam = texpr as TypeParameterExpr;
207 constraints = tparam.TypeParameter.Constraints;
209 parameter_type = texpr.Type;
211 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
212 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
216 if (parameter_type == TypeManager.void_type){
217 Report.Error (1536, l, "`void' parameter is not permitted");
221 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
222 if (parameter_type == TypeManager.typed_reference_type ||
223 parameter_type == TypeManager.arg_iterator_type){
224 Report.Error (1601, l,
225 "out or ref parameter can not be of type TypedReference or ArgIterator");
230 return parameter_type != null;
233 public Type ExternalType ()
235 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
236 return TypeManager.GetReferenceType (parameter_type);
238 return parameter_type;
241 public Type ParameterType {
243 return parameter_type;
247 public GenericConstraints GenericConstraints {
253 public ParameterAttributes Attributes {
255 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
256 switch ((Modifier) flags) {
258 return ParameterAttributes.None;
260 return ParameterAttributes.None;
262 return ParameterAttributes.Out;
263 case Modifier.PARAMS:
267 return ParameterAttributes.None;
271 public static ParameterAttributes GetParameterAttributes (Modifier mod)
273 int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
274 switch ((Modifier) flags) {
276 return ParameterAttributes.None;
278 return ParameterAttributes.None;
280 return ParameterAttributes.Out;
281 case Modifier.PARAMS:
285 return ParameterAttributes.None;
288 public override AttributeTargets AttributeTargets {
290 return AttributeTargets.Parameter;
295 /// Returns the signature for this parameter evaluating it on the
298 public string GetSignature (EmitContext ec, Location loc)
300 if (parameter_type == null){
301 if (!Resolve (ec, loc))
305 return ExternalType ().FullName;
308 public string GetSignatureForError ()
311 if (parameter_type != null)
312 typeName = TypeManager.CSharpName (parameter_type);
313 else if (TypeName.Type != null)
314 typeName = TypeManager.CSharpName (TypeName.Type);
316 typeName = TypeName.ToString ();
318 switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
320 return "out " + typeName;
321 case Modifier.PARAMS:
322 return "params " + typeName;
324 return "ref " + typeName;
329 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
331 ParameterAttributes par_attr = Attributes;
334 builder = cb.DefineParameter (index, par_attr, Name);
336 builder = mb.DefineParameter (index, par_attr, Name);
338 if (OptAttributes != null) {
339 OptAttributes.Emit (ec, this);
341 if (par_attr == ParameterAttributes.Out){
342 if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
343 Report.Error (36, loc, "Can not use [In] attribute on out parameter");
348 public override string[] ValidAttributeTargets {
350 return attribute_targets;
357 /// Represents the methods parameters
359 public class Parameters {
360 public Parameter [] FixedParameters;
361 public readonly Parameter ArrayParameter;
362 public readonly bool HasArglist;
367 static Parameters empty_parameters;
369 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
371 FixedParameters = fixed_parameters;
372 ArrayParameter = array_parameter;
376 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
378 FixedParameters = fixed_parameters;
379 HasArglist = has_arglist;
384 /// This is used to reuse a set of empty parameters, because they
387 public static Parameters EmptyReadOnlyParameters {
389 if (empty_parameters == null)
390 empty_parameters = new Parameters (null, null, Location.Null);
392 return empty_parameters;
398 return (FixedParameters == null) && (ArrayParameter == null);
402 public void ComputeSignature (EmitContext ec)
405 if (FixedParameters != null){
406 for (int i = 0; i < FixedParameters.Length; i++){
407 Parameter par = FixedParameters [i];
409 signature += par.GetSignature (ec, loc);
413 // Note: as per the spec, the `params' arguments (ArrayParameter)
414 // are not used in the signature computation for a method
419 void Error_DuplicateParameterName (string name)
422 100, loc, "The parameter name `" + name + "' is a duplicate");
425 public bool VerifyArgs ()
430 if (FixedParameters == null)
433 count = FixedParameters.Length;
434 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
436 for (i = 0; i < count; i++){
437 string base_name = FixedParameters [i].Name;
438 for (j = i + 1; j < count; j++){
439 if (base_name != FixedParameters [j].Name)
441 Error_DuplicateParameterName (base_name);
445 if (base_name == array_par_name){
446 Error_DuplicateParameterName (base_name);
454 /// Returns the signature of the Parameters evaluated in
455 /// the @ec EmitContext
457 public string GetSignature (EmitContext ec)
459 if (signature == null){
461 ComputeSignature (ec);
468 /// Returns the paramenter information based on the name
470 public Parameter GetParameterByName (string name, out int idx)
475 if (FixedParameters != null){
476 foreach (Parameter par in FixedParameters){
477 if (par.Name == name){
485 if (ArrayParameter != null){
486 if (name == ArrayParameter.Name){
488 return ArrayParameter;
495 public Parameter GetParameterByName (string name)
499 return GetParameterByName (name, out idx);
502 bool ComputeParameterTypes (EmitContext ec)
504 int extra = (ArrayParameter != null) ? 1 : 0;
508 if (FixedParameters == null)
511 pc = extra + FixedParameters.Length;
513 types = new Type [pc];
516 FixedParameters = null;
521 if (FixedParameters != null){
522 foreach (Parameter p in FixedParameters){
525 if (p.Resolve (ec, loc))
526 t = p.ExternalType ();
537 if (ArrayParameter.Resolve (ec, loc))
538 types [i] = ArrayParameter.ExternalType ();
552 // This variant is used by Delegates, because they need to
553 // resolve/define names, instead of the plain LookupType
555 public bool ComputeAndDefineParameterTypes (EmitContext ec)
557 bool old_type_resolving = ec.ResolvingTypeTree;
558 ec.ResolvingTypeTree = true;
559 bool retval = ComputeParameterTypes (ec);
560 ec.ResolvingTypeTree = old_type_resolving;
565 /// Returns the argument types as an array
567 static Type [] no_types = new Type [0];
569 public Type [] GetParameterInfo (EmitContext ec)
574 if (FixedParameters == null && ArrayParameter == null)
577 if (ComputeParameterTypes (ec) == false){
586 /// Returns the type of a given parameter, and stores in the `is_out'
587 /// boolean whether this is an out or ref parameter.
589 /// Note that the returned type will not contain any dereference in this
590 /// case (ie, you get "int" for a ref int instead of "int&"
592 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
594 mod = Parameter.Modifier.NONE;
597 FixedParameters = null;
601 if (FixedParameters == null && ArrayParameter == null)
605 if (ComputeParameterTypes (ec) == false)
609 // If this is a request for the variable lenght arg.
611 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
612 if (idx == array_idx)
616 // Otherwise, it is a fixed parameter
618 Parameter p = FixedParameters [idx];
621 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
622 mod |= Parameter.Modifier.ISBYREF;
624 return p.ParameterType;
627 public CallingConventions GetCallingConvention ()
630 return CallingConventions.VarArgs;
632 return CallingConventions.Standard;
636 // The method's attributes are passed in because we need to extract
637 // the "return:" attribute from there to apply on the return type
639 public void LabelParameters (EmitContext ec,
643 // Define each type attribute (in/out/ref) and
644 // the argument names.
648 MethodBuilder mb = builder as MethodBuilder;
649 ConstructorBuilder cb = builder as ConstructorBuilder;
651 if (FixedParameters != null) {
652 for (i = 0; i < FixedParameters.Length; i++) {
653 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
657 if (ArrayParameter != null){
659 Parameter array_param = ArrayParameter;
662 pb = cb.DefineParameter (
663 i + 1, array_param.Attributes,
666 pb = mb.DefineParameter (
667 i + 1, array_param.Attributes,
670 CustomAttributeBuilder a = new CustomAttributeBuilder (
671 TypeManager.cons_param_array_attribute, new object [0]);
673 pb.SetCustomAttribute (a);
677 public Parameters Copy (Location l)
679 Parameters p = new Parameters (null, null, l);
680 p.FixedParameters = new Parameter[this.FixedParameters.Length];
681 this.FixedParameters.CopyTo (p.FixedParameters, 0);
686 public void AppendParameter (Parameter p)
688 if (FixedParameters != null)
690 Parameter [] pa = new Parameter [FixedParameters.Length+1];
691 FixedParameters.CopyTo (pa, 0);
692 pa[FixedParameters.Length] = p;
693 FixedParameters = pa;
697 FixedParameters = new Parameter [1];
698 FixedParameters[0] = p;