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 {
32 public readonly string TypeName;
33 public readonly string Name;
34 public readonly Modifier ModFlags;
35 public Attributes OptAttributes;
36 public Type parameter_type;
38 public Parameter (string type, string name, Modifier mod, Attributes attrs)
43 OptAttributes = attrs;
47 // Resolve is used in method definitions
49 public bool Resolve (DeclSpace ds, Location l)
51 parameter_type = RootContext.LookupType (ds, TypeName, false, l);
52 return parameter_type != null;
56 // ResolveAndDefine is used by delegate declarations, because
57 // they happen during the initial tree resolution process
59 public bool ResolveAndDefine (DeclSpace ds)
61 parameter_type = ds.FindType (TypeName);
62 return parameter_type != null;
65 public Type ExternalType (DeclSpace ds, Location l)
67 if ((ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
68 string n = parameter_type.FullName + "&";
70 Type t = RootContext.LookupType (ds, n, false, l);
75 return parameter_type;
78 public Type ParameterType {
80 return parameter_type;
84 public ParameterAttributes Attributes {
88 return ParameterAttributes.None;
90 return ParameterAttributes.None;
92 return ParameterAttributes.Out;
97 return ParameterAttributes.None;
102 /// Returns the signature for this parameter evaluating it on the
105 public string GetSignature (DeclSpace ds, Location loc)
107 if (parameter_type == null){
108 if (!Resolve (ds, loc))
112 return ExternalType (ds, loc).FullName;
117 /// Represents the methods parameters
119 public class Parameters {
120 public Parameter [] FixedParameters;
121 public readonly Parameter ArrayParameter;
126 static Parameters empty_parameters;
128 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
130 FixedParameters = fixed_parameters;
131 ArrayParameter = array_parameter;
136 /// This is used to reuse a set of empty parameters, because they
139 public static Parameters GetEmptyReadOnlyParameters ()
141 if (empty_parameters == null)
142 empty_parameters = new Parameters (null, null, Location.Null);
144 return empty_parameters;
149 return (FixedParameters == null) && (ArrayParameter == null);
153 public void ComputeSignature (DeclSpace ds)
156 if (FixedParameters != null){
157 for (int i = 0; i < FixedParameters.Length; i++){
158 Parameter par = FixedParameters [i];
160 signature += par.GetSignature (ds, loc);
164 // Note: as per the spec, the `params' arguments (ArrayParameter)
165 // are not used in the signature computation for a method
169 static void error100 (string name)
172 100, "The parameter name `" + name + "' is a duplicate");
175 public bool VerifyArgs ()
180 if (FixedParameters == null)
183 count = FixedParameters.Length;
184 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
185 for (i = 0; i < count; i++){
186 string base_name = FixedParameters [i].Name;
188 for (j = i + 1; j < count; j++){
189 if (base_name != FixedParameters [j].Name)
191 error100 (base_name);
195 if (base_name == array_par_name){
196 error100 (base_name);
204 /// Returns the signature of the Parameters evaluated in
205 /// the @tc environment
207 public string GetSignature (DeclSpace ds)
209 if (signature == null){
211 ComputeSignature (ds);
218 /// Returns the paramenter information based on the name
220 public Parameter GetParameterByName (string name, out int idx)
225 if (FixedParameters != null){
226 foreach (Parameter par in FixedParameters){
227 if (par.Name == name){
235 if (ArrayParameter != null){
236 if (name == ArrayParameter.Name){
238 return ArrayParameter;
245 bool ComputeParameterTypes (DeclSpace ds)
247 int extra = (ArrayParameter != null) ? 1 : 0;
251 if (FixedParameters == null)
254 pc = extra + FixedParameters.Length;
256 types = new Type [pc];
259 FixedParameters = null;
264 if (FixedParameters != null){
265 foreach (Parameter p in FixedParameters){
268 if (p.Resolve (ds, loc))
269 t = p.ExternalType (ds, loc);
282 if (ArrayParameter.Resolve (ds, loc))
283 types [i] = ArrayParameter.ExternalType (ds, loc);
292 // This variant is used by Delegates, because they need to
293 // resolve/define names, instead of the plain LookupType
295 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
297 int extra = (ArrayParameter != null) ? 1 : 0;
301 if (FixedParameters == null)
304 pc = extra + FixedParameters.Length;
306 types = new Type [pc];
309 FixedParameters = null;
315 if (FixedParameters != null){
316 foreach (Parameter p in FixedParameters){
319 if (p.ResolveAndDefine (ds))
320 t = p.ExternalType (ds, loc);
330 if (ArrayParameter.ResolveAndDefine (ds))
331 types [i] = ArrayParameter.ExternalType (ds, loc);
337 // invalidate the cached types
347 /// Returns the argument types as an array
349 static Type [] no_types = new Type [0];
351 public Type [] GetParameterInfo (DeclSpace ds)
356 if (FixedParameters == null && ArrayParameter == null)
359 if (ComputeParameterTypes (ds) == false){
368 /// Returns the type of a given parameter, and stores in the `is_out'
369 /// boolean whether this is an out or ref parameter.
371 /// Note that the returned type will not contain any dereference in this
372 /// case (ie, you get "int" for a ref int instead of "int&"
374 public Type GetParameterInfo (DeclSpace ds, int idx, out bool is_out)
379 FixedParameters = null;
383 if (FixedParameters == null && ArrayParameter == null)
387 if (ComputeParameterTypes (ds) == false){
393 // If this is a request for the variable lenght arg.
395 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
396 if (idx == array_idx){
402 // Otherwise, it is a fixed parameter
404 Parameter p = FixedParameters [idx];
405 is_out = ((p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0);
407 return p.ParameterType;
410 public CallingConventions GetCallingConvention ()
412 // For now this is the only correc thing to do
413 return CallingConventions.Standard;