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 {
21 /// Represents a single method parameter
23 public class Parameter {
25 public enum Modifier : byte {
31 // This is a flag which says that it's either REF or OUT.
36 public readonly Expression TypeName;
37 public readonly Modifier ModFlags;
38 public Attributes OptAttributes;
39 public readonly string Name;
40 public Type parameter_type;
41 public readonly Expression ParameterInitializer;
42 public readonly bool IsOptional;
44 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
49 OptAttributes = attrs;
50 ParameterInitializer = null;
54 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi)
59 OptAttributes = attrs;
60 ParameterInitializer = pi;
64 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi, bool opt)
69 OptAttributes = attrs;
70 ParameterInitializer = pi;
76 // Resolve is used in method definitions
78 public bool Resolve (DeclSpace ds, Location l)
80 parameter_type = ds.ResolveType (TypeName, false, l);
82 if (parameter_type == TypeManager.void_type){
83 Report.Error (1536, l, "`void' parameter is not permitted");
87 return parameter_type != null;
90 public Type ExternalType (DeclSpace ds, Location l)
92 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
93 string n = parameter_type.FullName + "&";
95 Type t = RootContext.LookupType (ds, n, false, l);
100 return parameter_type;
103 public Type ParameterType {
105 return parameter_type;
109 public ParameterAttributes Attributes {
111 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
112 switch ((Modifier) flags) {
114 return ParameterAttributes.None;
116 return ParameterAttributes.None;
118 return ParameterAttributes.Out;
119 case Modifier.PARAMS:
123 return ParameterAttributes.None;
128 /// Returns the signature for this parameter evaluating it on the
131 public string GetSignature (DeclSpace ds, Location loc)
133 if (parameter_type == null){
134 if (!Resolve (ds, loc))
138 return ExternalType (ds, loc).FullName;
143 /// Represents the methods parameters
145 public class Parameters {
146 public Parameter [] FixedParameters;
147 public readonly Parameter ArrayParameter;
152 static Parameters empty_parameters;
154 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
156 FixedParameters = fixed_parameters;
157 ArrayParameter = array_parameter;
162 /// This is used to reuse a set of empty parameters, because they
165 public static Parameters EmptyReadOnlyParameters {
167 if (empty_parameters == null)
168 empty_parameters = new Parameters (null, null, Location.Null);
170 return empty_parameters;
174 public bool HasOptional()
178 foreach (Parameter p in FixedParameters)
190 /// Returns the number of standard (i.e. non-optional) parameters
192 public int CountStandardParams()
196 foreach (Parameter p in FixedParameters) {
204 /// Returns the number of optional parameters
206 public int CountOptionalParams()
210 foreach (Parameter p in FixedParameters) {
217 public Expression GetDefaultValue (int i)
219 Parameter p = FixedParameters[i];
221 return p.ParameterInitializer;
226 public void AppendParameter (Parameter p)
228 if (FixedParameters != null)
230 Parameter [] pa = new Parameter [FixedParameters.Length+1];
231 FixedParameters.CopyTo (pa, 0);
232 pa[FixedParameters.Length] = p;
233 FixedParameters = pa;
237 FixedParameters = new Parameter [1];
238 FixedParameters[0] = p;
242 public void PrependParameter (Parameter p)
244 Parameter [] pa = new Parameter [FixedParameters.Length+1];
245 FixedParameters.CopyTo (pa, 1);
247 FixedParameters = pa;
250 public Parameters Copy (Location l)
252 Parameters p = new Parameters (null, null, l);
253 p.FixedParameters = new Parameter[this.FixedParameters.Length];
254 this.FixedParameters.CopyTo (p.FixedParameters, 0);
262 return (FixedParameters == null) && (ArrayParameter == null);
266 public void ComputeSignature (DeclSpace ds)
269 if (FixedParameters != null){
270 for (int i = 0; i < FixedParameters.Length; i++){
271 Parameter par = FixedParameters [i];
273 signature += par.GetSignature (ds, loc);
277 // Note: as per the spec, the `params' arguments (ArrayParameter)
278 // are not used in the signature computation for a method
282 static void Error_DuplicateParameterName (string name)
285 100, "The parameter name `" + name + "' is a duplicate");
288 public bool VerifyArgs ()
293 if (FixedParameters == null)
296 count = FixedParameters.Length;
297 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
298 for (i = 0; i < count; i++){
299 string base_name = FixedParameters [i].Name;
301 for (j = i + 1; j < count; j++){
302 if (base_name != FixedParameters [j].Name)
304 Error_DuplicateParameterName (base_name);
308 if (base_name == array_par_name){
309 Error_DuplicateParameterName (base_name);
317 /// Returns the signature of the Parameters evaluated in
318 /// the @tc environment
320 public string GetSignature (DeclSpace ds)
322 if (signature == null){
324 ComputeSignature (ds);
331 /// Returns the paramenter information based on the name
333 public Parameter GetParameterByName (string name, out int idx)
338 if (FixedParameters != null){
339 foreach (Parameter par in FixedParameters){
340 if (par.Name == name){
348 if (ArrayParameter != null){
349 if (name == ArrayParameter.Name){
351 return ArrayParameter;
358 bool ComputeParameterTypes (DeclSpace ds)
360 int extra = (ArrayParameter != null) ? 1 : 0;
364 if (FixedParameters == null)
367 pc = extra + FixedParameters.Length;
369 types = new Type [pc];
372 FixedParameters = null;
377 if (FixedParameters != null){
378 foreach (Parameter p in FixedParameters){
381 if (p.Resolve (ds, loc))
382 t = p.ExternalType (ds, loc);
392 if (ArrayParameter.Resolve (ds, loc))
393 types [i] = ArrayParameter.ExternalType (ds, loc);
407 // This variant is used by Delegates, because they need to
408 // resolve/define names, instead of the plain LookupType
410 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
412 int extra = (ArrayParameter != null) ? 1 : 0;
416 if (FixedParameters == null)
419 pc = extra + FixedParameters.Length;
421 types = new Type [pc];
424 FixedParameters = null;
430 if (FixedParameters != null){
431 foreach (Parameter p in FixedParameters){
434 if (p.Resolve (ds, loc))
435 t = p.ExternalType (ds, loc);
445 if (ArrayParameter.Resolve (ds, loc))
446 types [i] = ArrayParameter.ExternalType (ds, loc);
452 // invalidate the cached types
462 /// Returns the argument types as an array
464 static Type [] no_types = new Type [0];
466 public Type [] GetParameterInfo (DeclSpace ds)
471 if (FixedParameters == null && ArrayParameter == null)
474 if (ComputeParameterTypes (ds) == false){
483 /// Returns the type of a given parameter, and stores in the `is_out'
484 /// boolean whether this is an out or ref parameter.
486 /// Note that the returned type will not contain any dereference in this
487 /// case (ie, you get "int" for a ref int instead of "int&"
489 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
491 mod = Parameter.Modifier.NONE;
494 FixedParameters = null;
498 if (FixedParameters == null && ArrayParameter == null)
502 if (ComputeParameterTypes (ds) == false)
506 // If this is a request for the variable lenght arg.
508 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
509 if (idx == array_idx)
513 // Otherwise, it is a fixed parameter
515 Parameter p = FixedParameters [idx];
518 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
519 mod |= Parameter.Modifier.ISBYREF;
521 return p.ParameterType;
524 public CallingConventions GetCallingConvention ()
526 // For now this is the only correc thing to do
527 return CallingConventions.Standard;