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;
38 public Type parameter_type;
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);
54 return parameter_type != null;
58 // ResolveAndDefine is used by delegate declarations, because
59 // they happen during the initial tree resolution process
61 public bool ResolveAndDefine (DeclSpace ds)
63 // FIXME: Should use something else instead of Location.Null
64 parameter_type = ds.ResolveType (TypeName, true, Location.Null);
65 return parameter_type != null;
68 public Type ExternalType (DeclSpace ds, Location l)
70 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
71 string n = parameter_type.FullName + "&";
73 Type t = RootContext.LookupType (ds, n, false, l);
78 return parameter_type;
81 public Type ParameterType {
83 return parameter_type;
87 public ParameterAttributes Attributes {
91 return ParameterAttributes.None;
93 return ParameterAttributes.None;
95 return ParameterAttributes.Out;
100 return ParameterAttributes.None;
105 /// Returns the signature for this parameter evaluating it on the
108 public string GetSignature (DeclSpace ds, Location loc)
110 if (parameter_type == null){
111 if (!Resolve (ds, loc))
115 return ExternalType (ds, loc).FullName;
120 /// Represents the methods parameters
122 public class Parameters {
123 public Parameter [] FixedParameters;
124 public readonly Parameter ArrayParameter;
129 static Parameters empty_parameters;
131 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
133 FixedParameters = fixed_parameters;
134 ArrayParameter = array_parameter;
139 /// This is used to reuse a set of empty parameters, because they
142 public static Parameters EmptyReadOnlyParameters {
144 if (empty_parameters == null)
145 empty_parameters = new Parameters (null, null, Location.Null);
147 return empty_parameters;
153 return (FixedParameters == null) && (ArrayParameter == null);
157 public void ComputeSignature (DeclSpace ds)
160 if (FixedParameters != null){
161 for (int i = 0; i < FixedParameters.Length; i++){
162 Parameter par = FixedParameters [i];
164 signature += par.GetSignature (ds, loc);
168 // Note: as per the spec, the `params' arguments (ArrayParameter)
169 // are not used in the signature computation for a method
173 static void error100 (string name)
176 100, "The parameter name `" + name + "' is a duplicate");
179 public bool VerifyArgs ()
184 if (FixedParameters == null)
187 count = FixedParameters.Length;
188 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
189 for (i = 0; i < count; i++){
190 string base_name = FixedParameters [i].Name;
192 for (j = i + 1; j < count; j++){
193 if (base_name != FixedParameters [j].Name)
195 error100 (base_name);
199 if (base_name == array_par_name){
200 error100 (base_name);
208 /// Returns the signature of the Parameters evaluated in
209 /// the @tc environment
211 public string GetSignature (DeclSpace ds)
213 if (signature == null){
215 ComputeSignature (ds);
222 /// Returns the paramenter information based on the name
224 public Parameter GetParameterByName (string name, out int idx)
229 if (FixedParameters != null){
230 foreach (Parameter par in FixedParameters){
231 if (par.Name == name){
239 if (ArrayParameter != null){
240 if (name == ArrayParameter.Name){
242 return ArrayParameter;
249 bool ComputeParameterTypes (DeclSpace ds)
251 int extra = (ArrayParameter != null) ? 1 : 0;
255 if (FixedParameters == null)
258 pc = extra + FixedParameters.Length;
260 types = new Type [pc];
263 FixedParameters = null;
268 if (FixedParameters != null){
269 foreach (Parameter p in FixedParameters){
272 if (p.Resolve (ds, loc))
273 t = p.ExternalType (ds, loc);
286 if (ArrayParameter.Resolve (ds, loc))
287 types [i] = ArrayParameter.ExternalType (ds, loc);
296 // This variant is used by Delegates, because they need to
297 // resolve/define names, instead of the plain LookupType
299 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
301 int extra = (ArrayParameter != null) ? 1 : 0;
305 if (FixedParameters == null)
308 pc = extra + FixedParameters.Length;
310 types = new Type [pc];
313 FixedParameters = null;
319 if (FixedParameters != null){
320 foreach (Parameter p in FixedParameters){
323 if (p.ResolveAndDefine (ds))
324 t = p.ExternalType (ds, loc);
334 if (ArrayParameter.ResolveAndDefine (ds))
335 types [i] = ArrayParameter.ExternalType (ds, loc);
341 // invalidate the cached types
351 /// Returns the argument types as an array
353 static Type [] no_types = new Type [0];
355 public Type [] GetParameterInfo (DeclSpace ds)
360 if (FixedParameters == null && ArrayParameter == null)
363 if (ComputeParameterTypes (ds) == false){
372 /// Returns the type of a given parameter, and stores in the `is_out'
373 /// boolean whether this is an out or ref parameter.
375 /// Note that the returned type will not contain any dereference in this
376 /// case (ie, you get "int" for a ref int instead of "int&"
378 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
380 mod = Parameter.Modifier.NONE;
383 FixedParameters = null;
387 if (FixedParameters == null && ArrayParameter == null)
391 if (ComputeParameterTypes (ds) == false)
395 // If this is a request for the variable lenght arg.
397 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
398 if (idx == array_idx)
402 // Otherwise, it is a fixed parameter
404 Parameter p = FixedParameters [idx];
407 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
408 mod |= Parameter.Modifier.ISBYREF;
410 return p.ParameterType;
413 public CallingConventions GetCallingConvention ()
415 // For now this is the only correc thing to do
416 return CallingConventions.Standard;