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 (-24, 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 EmitContext ec; // because ApplyAtrribute doesn't have ec
152 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
160 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
162 if (a.Type == TypeManager.in_attribute_type && Attributes == ParameterAttributes.Out) {
163 Report.Error (36, a.Location, "Can not use [In] attribute on out parameter");
167 if (a.Type == TypeManager.param_array_type) {
168 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
172 if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) != 0 &&
173 !OptAttributes.Contains (TypeManager.in_attribute_type, ec)) {
174 Report.Error (662, a.Location,
175 "'{0}' cannot specify only Out attribute on a ref parameter. Use both In and Out attributes, or neither", GetSignatureForError ());
179 if (a.Type == TypeManager.cls_compliant_attribute_type) {
180 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
183 base.ApplyAttributeBuilder (a, cb);
187 // Resolve is used in method definitions
189 public bool Resolve (EmitContext ec, Location l)
193 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
197 parameter_type = texpr.ResolveType (ec);
199 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
200 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
204 if (parameter_type == TypeManager.void_type){
205 Report.Error (1536, l, "Invalid parameter type 'void'");
209 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
210 if (parameter_type == TypeManager.typed_reference_type ||
211 parameter_type == TypeManager.arg_iterator_type){
212 Report.Error (1601, l,
213 "out or ref parameter can not be of type TypedReference or ArgIterator");
218 return parameter_type != null;
221 public Type ExternalType ()
223 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
224 return TypeManager.GetReferenceType (parameter_type);
226 return parameter_type;
229 public Type ParameterType {
231 return parameter_type;
235 public ParameterAttributes Attributes {
237 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
238 switch ((Modifier) flags) {
240 return ParameterAttributes.None;
242 return ParameterAttributes.None;
244 return ParameterAttributes.Out;
245 case Modifier.PARAMS:
249 return ParameterAttributes.None;
253 public override AttributeTargets AttributeTargets {
255 return AttributeTargets.Parameter;
260 /// Returns the signature for this parameter evaluating it on the
263 public string GetSignature (EmitContext ec, Location loc)
265 if (parameter_type == null){
266 if (!Resolve (ec, loc))
270 return ExternalType ().FullName;
273 public string GetSignatureForError ()
276 if (parameter_type != null)
277 type_name = TypeManager.CSharpName (parameter_type);
278 else if (TypeName.Type != null)
279 type_name = TypeManager.CSharpName (TypeName.Type);
281 type_name = TypeName.ToString ();
283 string mod = GetModifierSignature (ModFlags);
285 return String.Concat (mod, " ", type_name);
290 public static string GetModifierSignature (Modifier mod)
292 switch (mod & unchecked (~Modifier.ISBYREF)) {
295 case Modifier.PARAMS:
299 case Modifier.ARGLIST:
306 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
308 ParameterAttributes par_attr = Attributes;
311 builder = cb.DefineParameter (index, par_attr, Name);
313 builder = mb.DefineParameter (index, par_attr, Name);
315 if (OptAttributes != null) {
316 OptAttributes.Emit (ec, this);
320 public override string[] ValidAttributeTargets {
322 return attribute_targets;
328 /// Represents the methods parameters
330 public class Parameters {
331 public Parameter [] FixedParameters;
332 public readonly Parameter ArrayParameter;
333 public readonly bool HasArglist;
338 static Parameters empty_parameters;
340 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
342 FixedParameters = fixed_parameters;
343 ArrayParameter = array_parameter;
347 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
349 FixedParameters = fixed_parameters;
350 HasArglist = has_arglist;
355 /// This is used to reuse a set of empty parameters, because they
358 public static Parameters EmptyReadOnlyParameters {
360 if (empty_parameters == null)
361 empty_parameters = new Parameters (null, null, Location.Null);
363 return empty_parameters;
369 return (FixedParameters == null) && (ArrayParameter == null);
373 public Location Location {
377 public void ComputeSignature (EmitContext ec)
380 if (FixedParameters != null){
381 for (int i = 0; i < FixedParameters.Length; i++){
382 Parameter par = FixedParameters [i];
384 signature += par.GetSignature (ec, loc);
388 // Note: as per the spec, the `params' arguments (ArrayParameter)
389 // are not used in the signature computation for a method
393 void Error_DuplicateParameterName (string name)
396 100, loc, "The parameter name `" + name + "' is a duplicate");
399 public bool VerifyArgs ()
404 if (FixedParameters == null)
407 count = FixedParameters.Length;
408 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
410 for (i = 0; i < count; i++){
411 string base_name = FixedParameters [i].Name;
412 for (j = i + 1; j < count; j++){
413 if (base_name != FixedParameters [j].Name)
415 Error_DuplicateParameterName (base_name);
419 if (base_name == array_par_name){
420 Error_DuplicateParameterName (base_name);
428 /// Returns the signature of the Parameters evaluated in
429 /// the @ec EmitContext
431 public string GetSignature (EmitContext ec)
433 if (signature == null){
435 ComputeSignature (ec);
442 /// Returns the paramenter information based on the name
444 public Parameter GetParameterByName (string name, out int idx)
449 if (FixedParameters != null){
450 foreach (Parameter par in FixedParameters){
451 if (par.Name == name){
459 if (ArrayParameter != null){
460 if (name == ArrayParameter.Name){
462 return ArrayParameter;
469 public Parameter GetParameterByName (string name)
473 return GetParameterByName (name, out idx);
476 bool ComputeParameterTypes (EmitContext ec)
478 int extra = (ArrayParameter != null) ? 1 : 0;
482 if (FixedParameters == null)
485 pc = extra + FixedParameters.Length;
487 types = new Type [pc];
490 FixedParameters = null;
495 if (FixedParameters != null){
496 foreach (Parameter p in FixedParameters){
499 if (p.Resolve (ec, loc))
500 t = p.ExternalType ();
510 if (ArrayParameter.Resolve (ec, loc))
511 types [i] = ArrayParameter.ExternalType ();
525 /// Returns the argument types as an array
527 static Type [] no_types = new Type [0];
529 public Type [] GetParameterInfo (EmitContext ec)
534 if (FixedParameters == null && ArrayParameter == null)
537 if (ComputeParameterTypes (ec) == false){
546 /// Returns the type of a given parameter, and stores in the `is_out'
547 /// boolean whether this is an out or ref parameter.
549 /// Note that the returned type will not contain any dereference in this
550 /// case (ie, you get "int" for a ref int instead of "int&"
552 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
554 mod = Parameter.Modifier.NONE;
557 FixedParameters = null;
561 if (FixedParameters == null && ArrayParameter == null)
565 if (ComputeParameterTypes (ec) == false)
569 // If this is a request for the variable lenght arg.
571 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
572 if (idx == array_idx)
576 // Otherwise, it is a fixed parameter
578 Parameter p = FixedParameters [idx];
581 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
582 mod |= Parameter.Modifier.ISBYREF;
584 return p.ParameterType;
587 public CallingConventions GetCallingConvention ()
590 return CallingConventions.VarArgs;
592 return CallingConventions.Standard;
596 // The method's attributes are passed in because we need to extract
597 // the "return:" attribute from there to apply on the return type
599 public void LabelParameters (EmitContext ec,
603 // Define each type attribute (in/out/ref) and
604 // the argument names.
608 MethodBuilder mb = builder as MethodBuilder;
609 ConstructorBuilder cb = builder as ConstructorBuilder;
611 if (FixedParameters != null) {
612 for (i = 0; i < FixedParameters.Length; i++) {
613 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
617 if (ArrayParameter != null){
619 Parameter array_param = ArrayParameter;
622 pb = cb.DefineParameter (
623 i + 1, array_param.Attributes,
626 pb = mb.DefineParameter (
627 i + 1, array_param.Attributes,
630 CustomAttributeBuilder a = new CustomAttributeBuilder (
631 TypeManager.cons_param_array_attribute, new object [0]);
633 pb.SetCustomAttribute (a);