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 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 typeName = TypeManager.CSharpName (parameter_type);
278 else if (TypeName.Type != null)
279 typeName = TypeManager.CSharpName (TypeName.Type);
281 typeName = TypeName.ToString ();
283 switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
285 return "out " + typeName;
286 case Modifier.PARAMS:
287 return "params " + typeName;
289 return "ref " + typeName;
294 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
296 ParameterAttributes par_attr = Attributes;
299 builder = cb.DefineParameter (index, par_attr, Name);
301 builder = mb.DefineParameter (index, par_attr, Name);
303 if (OptAttributes != null) {
304 OptAttributes.Emit (ec, this);
308 public override string[] ValidAttributeTargets {
310 return attribute_targets;
316 /// Represents the methods parameters
318 public class Parameters {
319 public Parameter [] FixedParameters;
320 public readonly Parameter ArrayParameter;
321 public readonly bool HasArglist;
326 static Parameters empty_parameters;
328 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
330 FixedParameters = fixed_parameters;
331 ArrayParameter = array_parameter;
335 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
337 FixedParameters = fixed_parameters;
338 HasArglist = has_arglist;
343 /// This is used to reuse a set of empty parameters, because they
346 public static Parameters EmptyReadOnlyParameters {
348 if (empty_parameters == null)
349 empty_parameters = new Parameters (null, null, Location.Null);
351 return empty_parameters;
357 return (FixedParameters == null) && (ArrayParameter == null);
361 public void ComputeSignature (EmitContext ec)
364 if (FixedParameters != null){
365 for (int i = 0; i < FixedParameters.Length; i++){
366 Parameter par = FixedParameters [i];
368 signature += par.GetSignature (ec, loc);
372 // Note: as per the spec, the `params' arguments (ArrayParameter)
373 // are not used in the signature computation for a method
377 void Error_DuplicateParameterName (string name)
380 100, loc, "The parameter name `" + name + "' is a duplicate");
383 public bool VerifyArgs ()
388 if (FixedParameters == null)
391 count = FixedParameters.Length;
392 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
394 for (i = 0; i < count; i++){
395 string base_name = FixedParameters [i].Name;
396 for (j = i + 1; j < count; j++){
397 if (base_name != FixedParameters [j].Name)
399 Error_DuplicateParameterName (base_name);
403 if (base_name == array_par_name){
404 Error_DuplicateParameterName (base_name);
412 /// Returns the signature of the Parameters evaluated in
413 /// the @ec EmitContext
415 public string GetSignature (EmitContext ec)
417 if (signature == null){
419 ComputeSignature (ec);
426 /// Returns the paramenter information based on the name
428 public Parameter GetParameterByName (string name, out int idx)
433 if (FixedParameters != null){
434 foreach (Parameter par in FixedParameters){
435 if (par.Name == name){
443 if (ArrayParameter != null){
444 if (name == ArrayParameter.Name){
446 return ArrayParameter;
453 public Parameter GetParameterByName (string name)
457 return GetParameterByName (name, out idx);
460 bool ComputeParameterTypes (EmitContext ec)
462 int extra = (ArrayParameter != null) ? 1 : 0;
466 if (FixedParameters == null)
469 pc = extra + FixedParameters.Length;
471 types = new Type [pc];
474 FixedParameters = null;
479 if (FixedParameters != null){
480 foreach (Parameter p in FixedParameters){
483 if (p.Resolve (ec, loc))
484 t = p.ExternalType ();
494 if (ArrayParameter.Resolve (ec, loc))
495 types [i] = ArrayParameter.ExternalType ();
509 /// Returns the argument types as an array
511 static Type [] no_types = new Type [0];
513 public Type [] GetParameterInfo (EmitContext ec)
518 if (FixedParameters == null && ArrayParameter == null)
521 if (ComputeParameterTypes (ec) == false){
530 /// Returns the type of a given parameter, and stores in the `is_out'
531 /// boolean whether this is an out or ref parameter.
533 /// Note that the returned type will not contain any dereference in this
534 /// case (ie, you get "int" for a ref int instead of "int&"
536 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
538 mod = Parameter.Modifier.NONE;
541 FixedParameters = null;
545 if (FixedParameters == null && ArrayParameter == null)
549 if (ComputeParameterTypes (ec) == false)
553 // If this is a request for the variable lenght arg.
555 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
556 if (idx == array_idx)
560 // Otherwise, it is a fixed parameter
562 Parameter p = FixedParameters [idx];
565 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
566 mod |= Parameter.Modifier.ISBYREF;
568 return p.ParameterType;
571 public CallingConventions GetCallingConvention ()
574 return CallingConventions.VarArgs;
576 return CallingConventions.Standard;
580 // The method's attributes are passed in because we need to extract
581 // the "return:" attribute from there to apply on the return type
583 public void LabelParameters (EmitContext ec,
587 // Define each type attribute (in/out/ref) and
588 // the argument names.
592 MethodBuilder mb = builder as MethodBuilder;
593 ConstructorBuilder cb = builder as ConstructorBuilder;
595 if (FixedParameters != null) {
596 for (i = 0; i < FixedParameters.Length; i++) {
597 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
601 if (ArrayParameter != null){
603 Parameter array_param = ArrayParameter;
606 pb = cb.DefineParameter (
607 i + 1, array_param.Attributes,
610 pb = mb.DefineParameter (
611 i + 1, array_param.Attributes,
614 CustomAttributeBuilder a = new CustomAttributeBuilder (
615 TypeManager.cons_param_array_attribute, new object [0]);
617 pb.SetCustomAttribute (a);