(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / mcs / parameter.cs
blob209cd0a2d44bf456497f48a35a8816021425d32e
1 //
2 // parameter.cs: Parameter definition.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //
6 // Licensed under the terms of the GNU GPL
7 //
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
9 //
12 using System;
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Collections;
17 namespace Mono.CSharp {
19 /// <summary>
20 /// Abstract Base class for parameters of a method.
21 /// </summary>
22 public abstract class ParameterBase : Attributable {
24 protected ParameterBuilder builder;
26 public ParameterBase (Attributes attrs)
27 : base (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);
38 return;
41 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
42 a.Error_InvalidSecurityParent ();
43 return;
46 builder.SetCustomAttribute (cb);
49 public override bool IsClsCompliaceRequired(DeclSpace ds)
51 return false;
55 /// <summary>
56 /// Class for applying custom attributes on the return type
57 /// </summary>
58 public class ReturnParameter: ParameterBase {
59 public ReturnParameter (MethodBuilder mb, Location location):
60 base (null)
62 try {
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
73 if (builder == null)
74 return;
76 base.ApplyAttributeBuilder (a, cb);
79 public override AttributeTargets AttributeTargets {
80 get {
81 return AttributeTargets.ReturnValue;
85 /// <summary>
86 /// Is never called
87 /// </summary>
88 public override string[] ValidAttributeTargets {
89 get {
90 return null;
95 /// <summary>
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.
98 /// </summary>
99 public class ImplicitParameter: ParameterBase {
100 public ImplicitParameter (MethodBuilder mb):
101 base (null)
103 builder = mb.DefineParameter (1, ParameterAttributes.None, "");
106 public override AttributeTargets AttributeTargets {
107 get {
108 return AttributeTargets.Parameter;
112 /// <summary>
113 /// Is never called
114 /// </summary>
115 public override string[] ValidAttributeTargets {
116 get {
117 return null;
122 /// <summary>
123 /// Represents a single method parameter
124 /// </summary>
126 //TODO: Add location member to this or base class for better error location and all methods simplification.
127 public class Parameter : ParameterBase {
128 [Flags]
129 public enum Modifier : byte {
130 NONE = 0,
131 REF = 1,
132 OUT = 2,
133 PARAMS = 4,
134 // This is a flag which says that it's either REF or OUT.
135 ISBYREF = 8,
136 ARGLIST = 16
139 static string[] attribute_targets = new string [] { "param" };
141 public Expression TypeName;
142 public readonly Modifier ModFlags;
143 public readonly string Name;
144 Type parameter_type;
146 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
147 : base (attrs)
149 Name = name;
150 ModFlags = mod;
151 TypeName = type;
154 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
156 if (a.Type == TypeManager.param_array_type) {
157 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
158 return;
160 base.ApplyAttributeBuilder (a, cb);
163 // <summary>
164 // Resolve is used in method definitions
165 // </summary>
166 public bool Resolve (EmitContext ec, Location l)
168 TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
169 if (texpr == null)
170 return false;
172 parameter_type = texpr.ResolveType (ec);
174 if (parameter_type.IsAbstract && parameter_type.IsSealed) {
175 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
176 return false;
179 if (parameter_type == TypeManager.void_type){
180 Report.Error (1536, l, "`void' parameter is not permitted");
181 return false;
184 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
185 if (parameter_type == TypeManager.typed_reference_type ||
186 parameter_type == TypeManager.arg_iterator_type){
187 Report.Error (1601, l,
188 "out or ref parameter can not be of type TypedReference or ArgIterator");
189 return false;
193 return parameter_type != null;
196 public Type ExternalType ()
198 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
199 return TypeManager.GetReferenceType (parameter_type);
201 return parameter_type;
204 public Type ParameterType {
205 get {
206 return parameter_type;
210 public ParameterAttributes Attributes {
211 get {
212 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
213 switch ((Modifier) flags) {
214 case Modifier.NONE:
215 return ParameterAttributes.None;
216 case Modifier.REF:
217 return ParameterAttributes.None;
218 case Modifier.OUT:
219 return ParameterAttributes.Out;
220 case Modifier.PARAMS:
221 return 0;
224 return ParameterAttributes.None;
228 public override AttributeTargets AttributeTargets {
229 get {
230 return AttributeTargets.Parameter;
234 /// <summary>
235 /// Returns the signature for this parameter evaluating it on the
236 /// @tc context
237 /// </summary>
238 public string GetSignature (EmitContext ec, Location loc)
240 if (parameter_type == null){
241 if (!Resolve (ec, loc))
242 return null;
245 return ExternalType ().FullName;
248 public string GetSignatureForError ()
250 string typeName;
251 if (parameter_type != null)
252 typeName = TypeManager.CSharpName (parameter_type);
253 else if (TypeName.Type != null)
254 typeName = TypeManager.CSharpName (TypeName.Type);
255 else
256 typeName = TypeName.ToString ();
258 switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
259 case Modifier.OUT:
260 return "out " + typeName;
261 case Modifier.PARAMS:
262 return "params " + typeName;
263 case Modifier.REF:
264 return "ref " + typeName;
266 return typeName;
269 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
271 ParameterAttributes par_attr = Attributes;
273 if (mb == null)
274 builder = cb.DefineParameter (index, par_attr, Name);
275 else
276 builder = mb.DefineParameter (index, par_attr, Name);
278 if (OptAttributes != null) {
279 OptAttributes.Emit (ec, this);
281 if (par_attr == ParameterAttributes.Out){
282 if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
283 Report.Error (36, loc, "Can not use [In] attribute on out parameter");
288 public override string[] ValidAttributeTargets {
289 get {
290 return attribute_targets;
295 /// <summary>
296 /// Represents the methods parameters
297 /// </summary>
298 public class Parameters {
299 public Parameter [] FixedParameters;
300 public readonly Parameter ArrayParameter;
301 public readonly bool HasArglist;
302 string signature;
303 Type [] types;
304 Location loc;
306 static Parameters empty_parameters;
308 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
310 FixedParameters = fixed_parameters;
311 ArrayParameter = array_parameter;
312 loc = l;
315 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
317 FixedParameters = fixed_parameters;
318 HasArglist = has_arglist;
319 loc = l;
322 /// <summary>
323 /// This is used to reuse a set of empty parameters, because they
324 /// are common
325 /// </summary>
326 public static Parameters EmptyReadOnlyParameters {
327 get {
328 if (empty_parameters == null)
329 empty_parameters = new Parameters (null, null, Location.Null);
331 return empty_parameters;
335 public bool Empty {
336 get {
337 return (FixedParameters == null) && (ArrayParameter == null);
341 public void ComputeSignature (EmitContext ec)
343 signature = "";
344 if (FixedParameters != null){
345 for (int i = 0; i < FixedParameters.Length; i++){
346 Parameter par = FixedParameters [i];
348 signature += par.GetSignature (ec, loc);
352 // Note: as per the spec, the `params' arguments (ArrayParameter)
353 // are not used in the signature computation for a method
357 void Error_DuplicateParameterName (string name)
359 Report.Error (
360 100, loc, "The parameter name `" + name + "' is a duplicate");
363 public bool VerifyArgs ()
365 int count;
366 int i, j;
368 if (FixedParameters == null)
369 return true;
371 count = FixedParameters.Length;
372 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
374 for (i = 0; i < count; i++){
375 string base_name = FixedParameters [i].Name;
376 for (j = i + 1; j < count; j++){
377 if (base_name != FixedParameters [j].Name)
378 continue;
379 Error_DuplicateParameterName (base_name);
380 return false;
383 if (base_name == array_par_name){
384 Error_DuplicateParameterName (base_name);
385 return false;
388 return true;
391 /// <summary>
392 /// Returns the signature of the Parameters evaluated in
393 /// the @ec EmitContext
394 /// </summary>
395 public string GetSignature (EmitContext ec)
397 if (signature == null){
398 VerifyArgs ();
399 ComputeSignature (ec);
402 return signature;
405 /// <summary>
406 /// Returns the paramenter information based on the name
407 /// </summary>
408 public Parameter GetParameterByName (string name, out int idx)
410 idx = 0;
411 int i = 0;
413 if (FixedParameters != null){
414 foreach (Parameter par in FixedParameters){
415 if (par.Name == name){
416 idx = i;
417 return par;
419 i++;
423 if (ArrayParameter != null){
424 if (name == ArrayParameter.Name){
425 idx = i;
426 return ArrayParameter;
430 return null;
433 public Parameter GetParameterByName (string name)
435 int idx;
437 return GetParameterByName (name, out idx);
440 bool ComputeParameterTypes (EmitContext ec)
442 int extra = (ArrayParameter != null) ? 1 : 0;
443 int i = 0;
444 int pc;
446 if (FixedParameters == null)
447 pc = extra;
448 else
449 pc = extra + FixedParameters.Length;
451 types = new Type [pc];
453 if (!VerifyArgs ()){
454 FixedParameters = null;
455 return false;
458 bool failed = false;
459 if (FixedParameters != null){
460 foreach (Parameter p in FixedParameters){
461 Type t = null;
463 if (p.Resolve (ec, loc))
464 t = p.ExternalType ();
465 else
466 failed = true;
468 types [i] = t;
469 i++;
473 if (extra > 0){
474 if (ArrayParameter.Resolve (ec, loc))
475 types [i] = ArrayParameter.ExternalType ();
476 else
477 failed = true;
480 if (failed){
481 types = null;
482 return false;
485 return true;
489 // This variant is used by Delegates, because they need to
490 // resolve/define names, instead of the plain LookupType
492 public bool ComputeAndDefineParameterTypes (EmitContext ec)
494 bool old_type_resolving = ec.ResolvingTypeTree;
495 ec.ResolvingTypeTree = true;
496 bool retval = ComputeParameterTypes (ec);
497 ec.ResolvingTypeTree = old_type_resolving;
498 return retval;
501 /// <summary>
502 /// Returns the argument types as an array
503 /// </summary>
504 static Type [] no_types = new Type [0];
506 public Type [] GetParameterInfo (EmitContext ec)
508 if (types != null)
509 return types;
511 if (FixedParameters == null && ArrayParameter == null)
512 return no_types;
514 if (ComputeParameterTypes (ec) == false){
515 types = null;
516 return null;
519 return types;
522 /// <summary>
523 /// Returns the type of a given parameter, and stores in the `is_out'
524 /// boolean whether this is an out or ref parameter.
526 /// Note that the returned type will not contain any dereference in this
527 /// case (ie, you get "int" for a ref int instead of "int&"
528 /// </summary>
529 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
531 mod = Parameter.Modifier.NONE;
533 if (!VerifyArgs ()){
534 FixedParameters = null;
535 return null;
538 if (FixedParameters == null && ArrayParameter == null)
539 return null;
541 if (types == null)
542 if (ComputeParameterTypes (ec) == false)
543 return null;
546 // If this is a request for the variable lenght arg.
548 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
549 if (idx == array_idx)
550 return types [idx];
553 // Otherwise, it is a fixed parameter
555 Parameter p = FixedParameters [idx];
556 mod = p.ModFlags;
558 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
559 mod |= Parameter.Modifier.ISBYREF;
561 return p.ParameterType;
564 public CallingConventions GetCallingConvention ()
566 if (HasArglist)
567 return CallingConventions.VarArgs;
568 else
569 return CallingConventions.Standard;
573 // The method's attributes are passed in because we need to extract
574 // the "return:" attribute from there to apply on the return type
576 public void LabelParameters (EmitContext ec,
577 MethodBase builder,
578 Location loc) {
580 // Define each type attribute (in/out/ref) and
581 // the argument names.
583 int i = 0;
585 MethodBuilder mb = builder as MethodBuilder;
586 ConstructorBuilder cb = builder as ConstructorBuilder;
588 if (FixedParameters != null) {
589 for (i = 0; i < FixedParameters.Length; i++) {
590 FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
594 if (ArrayParameter != null){
595 ParameterBuilder pb;
596 Parameter array_param = ArrayParameter;
598 if (mb == null)
599 pb = cb.DefineParameter (
600 i + 1, array_param.Attributes,
601 array_param.Name);
602 else
603 pb = mb.DefineParameter (
604 i + 1, array_param.Attributes,
605 array_param.Name);
607 CustomAttributeBuilder a = new CustomAttributeBuilder (
608 TypeManager.cons_param_array_attribute, new object [0]);
610 pb.SetCustomAttribute (a);