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 GetEmptyReadOnlyParameters ()
144 if (empty_parameters == null)
145 empty_parameters = new Parameters (null, null, Location.Null);
147 return empty_parameters;
152 return (FixedParameters == null) && (ArrayParameter == null);
156 public void ComputeSignature (DeclSpace ds)
159 if (FixedParameters != null){
160 for (int i = 0; i < FixedParameters.Length; i++){
161 Parameter par = FixedParameters [i];
163 signature += par.GetSignature (ds, loc);
167 // Note: as per the spec, the `params' arguments (ArrayParameter)
168 // are not used in the signature computation for a method
172 static void error100 (string name)
175 100, "The parameter name `" + name + "' is a duplicate");
178 public bool VerifyArgs ()
183 if (FixedParameters == null)
186 count = FixedParameters.Length;
187 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
188 for (i = 0; i < count; i++){
189 string base_name = FixedParameters [i].Name;
191 for (j = i + 1; j < count; j++){
192 if (base_name != FixedParameters [j].Name)
194 error100 (base_name);
198 if (base_name == array_par_name){
199 error100 (base_name);
207 /// Returns the signature of the Parameters evaluated in
208 /// the @tc environment
210 public string GetSignature (DeclSpace ds)
212 if (signature == null){
214 ComputeSignature (ds);
221 /// Returns the paramenter information based on the name
223 public Parameter GetParameterByName (string name, out int idx)
228 if (FixedParameters != null){
229 foreach (Parameter par in FixedParameters){
230 if (par.Name == name){
238 if (ArrayParameter != null){
239 if (name == ArrayParameter.Name){
241 return ArrayParameter;
248 bool ComputeParameterTypes (DeclSpace ds)
250 int extra = (ArrayParameter != null) ? 1 : 0;
254 if (FixedParameters == null)
257 pc = extra + FixedParameters.Length;
259 types = new Type [pc];
262 FixedParameters = null;
267 if (FixedParameters != null){
268 foreach (Parameter p in FixedParameters){
271 if (p.Resolve (ds, loc))
272 t = p.ExternalType (ds, loc);
285 if (ArrayParameter.Resolve (ds, loc))
286 types [i] = ArrayParameter.ExternalType (ds, loc);
295 // This variant is used by Delegates, because they need to
296 // resolve/define names, instead of the plain LookupType
298 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
300 int extra = (ArrayParameter != null) ? 1 : 0;
304 if (FixedParameters == null)
307 pc = extra + FixedParameters.Length;
309 types = new Type [pc];
312 FixedParameters = null;
318 if (FixedParameters != null){
319 foreach (Parameter p in FixedParameters){
322 if (p.ResolveAndDefine (ds))
323 t = p.ExternalType (ds, loc);
333 if (ArrayParameter.ResolveAndDefine (ds))
334 types [i] = ArrayParameter.ExternalType (ds, loc);
340 // invalidate the cached types
350 /// Returns the argument types as an array
352 static Type [] no_types = new Type [0];
354 public Type [] GetParameterInfo (DeclSpace ds)
359 if (FixedParameters == null && ArrayParameter == null)
362 if (ComputeParameterTypes (ds) == false){
371 /// Returns the type of a given parameter, and stores in the `is_out'
372 /// boolean whether this is an out or ref parameter.
374 /// Note that the returned type will not contain any dereference in this
375 /// case (ie, you get "int" for a ref int instead of "int&"
377 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
379 mod = Parameter.Modifier.NONE;
382 FixedParameters = null;
386 if (FixedParameters == null && ArrayParameter == null)
390 if (ComputeParameterTypes (ds) == false)
394 // If this is a request for the variable lenght arg.
396 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
397 if (idx == array_idx)
401 // Otherwise, it is a fixed parameter
403 Parameter p = FixedParameters [idx];
406 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
407 mod |= Parameter.Modifier.ISBYREF;
409 return p.ParameterType;
412 public CallingConventions GetCallingConvention ()
414 // For now this is the only correc thing to do
415 return CallingConventions.Standard;