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 {
30 // This is a flag which says that it's either REF or OUT.
34 public readonly Expression TypeName;
35 public readonly Modifier ModFlags;
36 public Attributes OptAttributes;
37 public readonly string Name;
40 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
45 OptAttributes = attrs;
49 // Resolve is used in method definitions
51 public bool Resolve (DeclSpace ds, Location l)
53 parameter_type = ds.ResolveType (TypeName, false, l);
55 if (parameter_type == TypeManager.void_type){
56 Report.Error (1536, l, "`void' parameter is not permitted");
60 return parameter_type != null;
63 public Type ExternalType ()
65 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
66 return TypeManager.GetReferenceType (parameter_type);
68 return parameter_type;
71 public Type ParameterType {
73 return parameter_type;
77 public ParameterAttributes Attributes {
79 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
80 switch ((Modifier) flags) {
82 return ParameterAttributes.None;
84 return ParameterAttributes.None;
86 return ParameterAttributes.Out;
91 return ParameterAttributes.None;
96 /// Returns the signature for this parameter evaluating it on the
99 public string GetSignature (DeclSpace ds, Location loc)
101 if (parameter_type == null){
102 if (!Resolve (ds, loc))
106 return ExternalType ().FullName;
111 /// Represents the methods parameters
113 public class Parameters {
114 public Parameter [] FixedParameters;
115 public readonly Parameter ArrayParameter;
120 static Parameters empty_parameters;
122 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
124 FixedParameters = fixed_parameters;
125 ArrayParameter = array_parameter;
130 /// This is used to reuse a set of empty parameters, because they
133 public static Parameters EmptyReadOnlyParameters {
135 if (empty_parameters == null)
136 empty_parameters = new Parameters (null, null, Location.Null);
138 return empty_parameters;
144 return (FixedParameters == null) && (ArrayParameter == null);
148 public void ComputeSignature (DeclSpace ds)
151 if (FixedParameters != null){
152 for (int i = 0; i < FixedParameters.Length; i++){
153 Parameter par = FixedParameters [i];
155 signature += par.GetSignature (ds, loc);
159 // Note: as per the spec, the `params' arguments (ArrayParameter)
160 // are not used in the signature computation for a method
164 void Error_DuplicateParameterName (string name)
167 100, loc, "The parameter name `" + name + "' is a duplicate");
170 public bool VerifyArgs ()
175 if (FixedParameters == null)
178 count = FixedParameters.Length;
179 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
180 for (i = 0; i < count; i++){
181 string base_name = FixedParameters [i].Name;
183 for (j = i + 1; j < count; j++){
184 if (base_name != FixedParameters [j].Name)
186 Error_DuplicateParameterName (base_name);
190 if (base_name == array_par_name){
191 Error_DuplicateParameterName (base_name);
199 /// Returns the signature of the Parameters evaluated in
200 /// the @tc environment
202 public string GetSignature (DeclSpace ds)
204 if (signature == null){
206 ComputeSignature (ds);
213 /// Returns the paramenter information based on the name
215 public Parameter GetParameterByName (string name, out int idx)
220 if (FixedParameters != null){
221 foreach (Parameter par in FixedParameters){
222 if (par.Name == name){
230 if (ArrayParameter != null){
231 if (name == ArrayParameter.Name){
233 return ArrayParameter;
240 bool ComputeParameterTypes (DeclSpace ds)
242 int extra = (ArrayParameter != null) ? 1 : 0;
246 if (FixedParameters == null)
249 pc = extra + FixedParameters.Length;
251 types = new Type [pc];
254 FixedParameters = null;
259 if (FixedParameters != null){
260 foreach (Parameter p in FixedParameters){
263 if (p.Resolve (ds, loc))
264 t = p.ExternalType ();
274 if (ArrayParameter.Resolve (ds, loc))
275 types [i] = ArrayParameter.ExternalType ();
289 // This variant is used by Delegates, because they need to
290 // resolve/define names, instead of the plain LookupType
292 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
294 int extra = (ArrayParameter != null) ? 1 : 0;
298 if (FixedParameters == null)
301 pc = extra + FixedParameters.Length;
303 types = new Type [pc];
306 FixedParameters = null;
312 if (FixedParameters != null){
313 foreach (Parameter p in FixedParameters){
316 if (p.Resolve (ds, loc))
317 t = p.ExternalType ();
327 if (ArrayParameter.Resolve (ds, loc))
328 types [i] = ArrayParameter.ExternalType ();
334 // invalidate the cached types
344 /// Returns the argument types as an array
346 static Type [] no_types = new Type [0];
348 public Type [] GetParameterInfo (DeclSpace ds)
353 if (FixedParameters == null && ArrayParameter == null)
356 if (ComputeParameterTypes (ds) == false){
365 /// Returns the type of a given parameter, and stores in the `is_out'
366 /// boolean whether this is an out or ref parameter.
368 /// Note that the returned type will not contain any dereference in this
369 /// case (ie, you get "int" for a ref int instead of "int&"
371 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
373 mod = Parameter.Modifier.NONE;
376 FixedParameters = null;
380 if (FixedParameters == null && ArrayParameter == null)
384 if (ComputeParameterTypes (ds) == false)
388 // If this is a request for the variable lenght arg.
390 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
391 if (idx == array_idx)
395 // Otherwise, it is a fixed parameter
397 Parameter p = FixedParameters [idx];
400 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
401 mod |= Parameter.Modifier.ISBYREF;
403 return p.ParameterType;
406 public CallingConventions GetCallingConvention ()
408 // For now this is the only correc thing to do
409 return CallingConventions.Standard;