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.MonoBASIC {
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()
195 if (FixedParameters == null)
198 foreach (Parameter p in FixedParameters) {
206 /// Returns the number of optional parameters
208 public int CountOptionalParams()
211 if (FixedParameters == null)
214 foreach (Parameter p in FixedParameters) {
221 public Expression GetDefaultValue (int i)
223 Parameter p = FixedParameters[i];
225 return p.ParameterInitializer;
230 public void AppendParameter (Parameter p)
232 if (FixedParameters != null)
234 Parameter [] pa = new Parameter [FixedParameters.Length+1];
235 FixedParameters.CopyTo (pa, 0);
236 pa[FixedParameters.Length] = p;
237 FixedParameters = pa;
241 FixedParameters = new Parameter [1];
242 FixedParameters[0] = p;
246 public void PrependParameter (Parameter p)
248 Parameter [] pa = new Parameter [FixedParameters.Length+1];
249 FixedParameters.CopyTo (pa, 1);
251 FixedParameters = pa;
254 public Parameters Copy (Location l)
256 Parameters p = new Parameters (null, null, l);
257 p.FixedParameters = new Parameter[this.FixedParameters.Length];
258 this.FixedParameters.CopyTo (p.FixedParameters, 0);
266 return (FixedParameters == null) && (ArrayParameter == null);
270 public void ComputeSignature (DeclSpace ds)
273 if (FixedParameters != null){
274 for (int i = 0; i < FixedParameters.Length; i++){
275 Parameter par = FixedParameters [i];
277 signature += par.GetSignature (ds, loc);
281 // Note: as per the spec, the `params' arguments (ArrayParameter)
282 // are not used in the signature computation for a method
286 static void Error_DuplicateParameterName (string name)
289 100, "The parameter name `" + name + "' is a duplicate");
292 public bool VerifyArgs ()
297 if (FixedParameters == null)
300 count = FixedParameters.Length;
301 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
302 for (i = 0; i < count; i++){
303 string base_name = FixedParameters [i].Name;
305 for (j = i + 1; j < count; j++){
306 if (base_name != FixedParameters [j].Name)
308 Error_DuplicateParameterName (base_name);
312 if (base_name == array_par_name){
313 Error_DuplicateParameterName (base_name);
321 /// Returns the signature of the Parameters evaluated in
322 /// the @tc environment
324 public string GetSignature (DeclSpace ds)
326 if (signature == null){
328 ComputeSignature (ds);
335 /// Returns the paramenter information based on the name
337 public Parameter GetParameterByName (string name, out int idx)
342 if (FixedParameters != null){
343 foreach (Parameter par in FixedParameters){
344 if (par.Name == name){
352 if (ArrayParameter != null){
353 if (name == ArrayParameter.Name){
355 return ArrayParameter;
362 bool ComputeParameterTypes (DeclSpace ds)
364 int extra = (ArrayParameter != null) ? 1 : 0;
368 if (FixedParameters == null)
371 pc = extra + FixedParameters.Length;
373 types = new Type [pc];
376 FixedParameters = null;
381 if (FixedParameters != null){
382 foreach (Parameter p in FixedParameters){
385 if (p.Resolve (ds, loc))
386 t = p.ExternalType (ds, loc);
396 if (ArrayParameter.Resolve (ds, loc))
397 types [i] = ArrayParameter.ExternalType (ds, loc);
411 // This variant is used by Delegates, because they need to
412 // resolve/define names, instead of the plain LookupType
414 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
416 int extra = (ArrayParameter != null) ? 1 : 0;
420 if (FixedParameters == null)
423 pc = extra + FixedParameters.Length;
425 types = new Type [pc];
428 FixedParameters = null;
434 if (FixedParameters != null){
435 foreach (Parameter p in FixedParameters){
438 if (p.Resolve (ds, loc))
439 t = p.ExternalType (ds, loc);
449 if (ArrayParameter.Resolve (ds, loc))
450 types [i] = ArrayParameter.ExternalType (ds, loc);
456 // invalidate the cached types
466 /// Returns the argument types as an array
468 static Type [] no_types = new Type [0];
470 public Type [] GetParameterInfo (DeclSpace ds)
475 if (FixedParameters == null && ArrayParameter == null)
478 if (ComputeParameterTypes (ds) == false){
487 /// Returns the type of a given parameter, and stores in the `is_out'
488 /// boolean whether this is an out or ref parameter.
490 /// Note that the returned type will not contain any dereference in this
491 /// case (ie, you get "int" for a ref int instead of "int&"
493 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
495 mod = Parameter.Modifier.NONE;
498 FixedParameters = null;
502 if (FixedParameters == null && ArrayParameter == null)
506 if (ComputeParameterTypes (ds) == false)
510 // If this is a request for the variable lenght arg.
512 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
513 if (idx == array_idx)
517 // Otherwise, it is a fixed parameter
519 Parameter p = FixedParameters [idx];
522 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
523 mod |= Parameter.Modifier.ISBYREF;
525 return p.ParameterType;
528 public CallingConventions GetCallingConvention ()
530 // For now this is the only correc thing to do
531 return CallingConventions.Standard;