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 if (a.Type == TypeManager.cls_compliant_attribute_type) {
73 Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
76 // This occurs after Warning -28
80 base.ApplyAttributeBuilder (a, cb);
83 public override AttributeTargets AttributeTargets {
85 return AttributeTargets.ReturnValue;
92 public override string[] ValidAttributeTargets {
100 /// Class for applying custom attributes on the implicit parameter type
101 /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
103 public class ImplicitParameter: ParameterBase {
104 public ImplicitParameter (MethodBuilder mb):
107 builder = mb.DefineParameter (1, ParameterAttributes.None, "");
110 public override AttributeTargets AttributeTargets {
112 return AttributeTargets.Parameter;
119 public override string[] ValidAttributeTargets {
127 /// Represents a single method parameter
130 //TODO: Add location member to this or base class for better error location and all methods simplification.
131 public class Parameter : ParameterBase {
133 public enum Modifier : byte {
138 // This is a flag which says that it's either REF or OUT.
143 static string[] attribute_targets = new string [] { "param" };
145 public Expression TypeName;
146 public readonly Modifier ModFlags;
147 public readonly string Name;
150 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
158 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
160 if (a.Type == TypeManager.param_array_type) {
161 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
165 if (a.Type == TypeManager.cls_compliant_attribute_type) {
166 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
169 base.ApplyAttributeBuilder (a, cb);
173 // Resolve is used in method definitions
175 public bool Resolve (EmitContext ec, Location l)
177 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
181 parameter_type = texpr.ResolveType (ec);
183 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
184 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
188 if (parameter_type == TypeManager.void_type){
189 Report.Error (1536, l, "Invalid parameter type 'void'");
193 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
194 if (parameter_type == TypeManager.typed_reference_type ||
195 parameter_type == TypeManager.arg_iterator_type){
196 Report.Error (1601, l,
197 "out or ref parameter can not be of type TypedReference or ArgIterator");
202 return parameter_type != null;
205 public Type ExternalType ()
207 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
208 return TypeManager.GetReferenceType (parameter_type);
210 return parameter_type;
213 public Type ParameterType {
215 return parameter_type;
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 (EmitContext ec, Location loc)
249 if (parameter_type == null){
250 if (!Resolve (ec, loc))
254 return ExternalType ().FullName;
257 public string GetSignatureForError ()
260 if (parameter_type != null)
261 typeName = TypeManager.CSharpName (parameter_type);
262 else if (TypeName.Type != null)
263 typeName = TypeManager.CSharpName (TypeName.Type);
265 typeName = TypeName.ToString ();
267 switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
269 return "out " + typeName;
270 case Modifier.PARAMS:
271 return "params " + typeName;
273 return "ref " + typeName;
278 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
280 ParameterAttributes par_attr = Attributes;
283 builder = cb.DefineParameter (index, par_attr, Name);
285 builder = mb.DefineParameter (index, par_attr, Name);
287 if (OptAttributes != null) {
288 OptAttributes.Emit (ec, this);
290 if (par_attr == ParameterAttributes.Out){
291 if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
292 Report.Error (36, loc, "Can not use [In] attribute on out parameter");
297 public override string[] ValidAttributeTargets {
299 return attribute_targets;
305 /// Represents the methods parameters
307 public class Parameters {
308 public Parameter [] FixedParameters;
309 public readonly Parameter ArrayParameter;
310 public readonly bool HasArglist;
315 static Parameters empty_parameters;
317 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
319 FixedParameters = fixed_parameters;
320 ArrayParameter = array_parameter;
324 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
326 FixedParameters = fixed_parameters;
327 HasArglist = has_arglist;
332 /// This is used to reuse a set of empty parameters, because they
335 public static Parameters EmptyReadOnlyParameters {
337 if (empty_parameters == null)
338 empty_parameters = new Parameters (null, null, Location.Null);
340 return empty_parameters;
346 return (FixedParameters == null) && (ArrayParameter == null);
350 public void ComputeSignature (EmitContext ec)
353 if (FixedParameters != null){
354 for (int i = 0; i < FixedParameters.Length; i++){
355 Parameter par = FixedParameters [i];
357 signature += par.GetSignature (ec, loc);
361 // Note: as per the spec, the `params' arguments (ArrayParameter)
362 // are not used in the signature computation for a method
366 void Error_DuplicateParameterName (string name)
369 100, loc, "The parameter name `" + name + "' is a duplicate");
372 public bool VerifyArgs ()
377 if (FixedParameters == null)
380 count = FixedParameters.Length;
381 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
383 for (i = 0; i < count; i++){
384 string base_name = FixedParameters [i].Name;
385 for (j = i + 1; j < count; j++){
386 if (base_name != FixedParameters [j].Name)
388 Error_DuplicateParameterName (base_name);
392 if (base_name == array_par_name){
393 Error_DuplicateParameterName (base_name);
401 /// Returns the signature of the Parameters evaluated in
402 /// the @ec EmitContext
404 public string GetSignature (EmitContext ec)
406 if (signature == null){
408 ComputeSignature (ec);
415 /// Returns the paramenter information based on the name
417 public Parameter GetParameterByName (string name, out int idx)
422 if (FixedParameters != null){
423 foreach (Parameter par in FixedParameters){
424 if (par.Name == name){
432 if (ArrayParameter != null){
433 if (name == ArrayParameter.Name){
435 return ArrayParameter;
442 public Parameter GetParameterByName (string name)
446 return GetParameterByName (name, out idx);
449 bool ComputeParameterTypes (EmitContext ec)
451 int extra = (ArrayParameter != null) ? 1 : 0;
455 if (FixedParameters == null)
458 pc = extra + FixedParameters.Length;
460 types = new Type [pc];
463 FixedParameters = null;
468 if (FixedParameters != null){
469 foreach (Parameter p in FixedParameters){
472 if (p.Resolve (ec, loc))
473 t = p.ExternalType ();
483 if (ArrayParameter.Resolve (ec, loc))
484 types [i] = ArrayParameter.ExternalType ();
498 // This variant is used by Delegates, because they need to
499 // resolve/define names, instead of the plain LookupType
501 public bool ComputeAndDefineParameterTypes (EmitContext ec)
503 bool old_type_resolving = ec.ResolvingTypeTree;
504 ec.ResolvingTypeTree = true;
505 bool retval = ComputeParameterTypes (ec);
506 ec.ResolvingTypeTree = old_type_resolving;
511 /// Returns the argument types as an array
513 static Type [] no_types = new Type [0];
515 public Type [] GetParameterInfo (EmitContext ec)
520 if (FixedParameters == null && ArrayParameter == null)
523 if (ComputeParameterTypes (ec) == false){
532 /// Returns the type of a given parameter, and stores in the `is_out'
533 /// boolean whether this is an out or ref parameter.
535 /// Note that the returned type will not contain any dereference in this
536 /// case (ie, you get "int" for a ref int instead of "int&"
538 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
540 mod = Parameter.Modifier.NONE;
543 FixedParameters = null;
547 if (FixedParameters == null && ArrayParameter == null)
551 if (ComputeParameterTypes (ec) == false)
555 // If this is a request for the variable lenght arg.
557 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
558 if (idx == array_idx)
562 // Otherwise, it is a fixed parameter
564 Parameter p = FixedParameters [idx];
567 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
568 mod |= Parameter.Modifier.ISBYREF;
570 return p.ParameterType;
573 public CallingConventions GetCallingConvention ()
576 return CallingConventions.VarArgs;
578 return CallingConventions.Standard;
582 // The method's attributes are passed in because we need to extract
583 // the "return:" attribute from there to apply on the return type
585 public void LabelParameters (EmitContext ec,
589 // Define each type attribute (in/out/ref) and
590 // the argument names.
594 MethodBuilder mb = builder as MethodBuilder;
595 ConstructorBuilder cb = builder as ConstructorBuilder;
597 if (FixedParameters != null) {
598 for (i = 0; i < FixedParameters.Length; i++) {
599 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
603 if (ArrayParameter != null){
605 Parameter array_param = ArrayParameter;
608 pb = cb.DefineParameter (
609 i + 1, array_param.Attributes,
612 pb = mb.DefineParameter (
613 i + 1, array_param.Attributes,
616 CustomAttributeBuilder a = new CustomAttributeBuilder (
617 TypeManager.cons_param_array_attribute, new object [0]);
619 pb.SetCustomAttribute (a);