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 ParameterType;
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 ParameterType = RootContext.LookupType (ds, TypeName, false, l);
52 return ParameterType != 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 ParameterType = ds.FindType (TypeName);
62 return ParameterType != null;
65 public Type ExternalType (DeclSpace ds, Location l)
67 if ((ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
68 string n = ParameterType.FullName + "&";
70 Type t = RootContext.LookupType (ds, n, false, l);
78 public ParameterAttributes Attributes {
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 (ParameterType == null){
102 if (!Resolve (ds, loc))
106 return ExternalType (ds, loc).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 GetEmptyReadOnlyParameters ()
135 if (empty_parameters == null)
136 empty_parameters = new Parameters (null, null, Location.Null);
138 return empty_parameters;
143 return (FixedParameters == null) && (ArrayParameter == null);
147 public void ComputeSignature (DeclSpace ds)
150 if (FixedParameters != null){
151 for (int i = 0; i < FixedParameters.Length; i++){
152 Parameter par = FixedParameters [i];
154 signature += par.GetSignature (ds, loc);
158 // Note: as per the spec, the `params' arguments (ArrayParameter)
159 // are not used in the signature computation for a method
163 static void error100 (string name)
166 100, "The parameter name `" + name + "' is a duplicate");
169 public bool VerifyArgs ()
174 if (FixedParameters == null)
177 count = FixedParameters.Length;
178 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
179 for (i = 0; i < count; i++){
180 string base_name = FixedParameters [i].Name;
182 for (j = i + 1; j < count; j++){
183 if (base_name != FixedParameters [j].Name)
185 error100 (base_name);
189 if (base_name == array_par_name){
190 error100 (base_name);
198 /// Returns the signature of the Parameters evaluated in
199 /// the @tc environment
201 public string GetSignature (DeclSpace ds)
203 if (signature == null){
205 ComputeSignature (ds);
212 /// Returns the paramenter information based on the name
214 public Parameter GetParameterByName (string name, out int idx)
219 if (FixedParameters != null){
220 foreach (Parameter par in FixedParameters){
221 if (par.Name == name){
229 if (ArrayParameter != null){
230 if (name == ArrayParameter.Name){
232 return ArrayParameter;
239 bool ComputeParameterTypes (DeclSpace ds)
241 int extra = (ArrayParameter != null) ? 1 : 0;
245 if (FixedParameters == null)
248 pc = extra + FixedParameters.Length;
250 types = new Type [pc];
253 FixedParameters = null;
258 if (FixedParameters != null){
259 foreach (Parameter p in FixedParameters){
262 if (p.Resolve (ds, loc))
263 t = p.ExternalType (ds, loc);
275 if (ArrayParameter.Resolve (ds, loc))
276 types [i] = ArrayParameter.ExternalType (ds, loc);
283 // This variant is used by Delegates, because they need to
284 // resolve/define names, instead of the plain LookupType
286 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
288 int extra = (ArrayParameter != null) ? 1 : 0;
292 if (FixedParameters == null)
295 pc = extra + FixedParameters.Length;
297 types = new Type [pc];
300 FixedParameters = null;
304 if (FixedParameters != null){
305 foreach (Parameter p in FixedParameters){
308 if (p.ResolveAndDefine (ds))
309 t = p.ExternalType (ds, loc);
317 if (ArrayParameter.ResolveAndDefine (ds))
318 types [i] = ArrayParameter.ExternalType (ds, loc);
325 /// Returns the argument types as an array
327 static Type [] no_types = new Type [0];
329 public Type [] GetParameterInfo (DeclSpace ds)
334 if (FixedParameters == null && ArrayParameter == null)
337 if (ComputeParameterTypes (ds) == false)
344 /// Returns the type of a given parameter, and stores in the `is_out'
345 /// boolean whether this is an out or ref parameter.
347 /// Note that the returned type will not contain any dereference in this
348 /// case (ie, you get "int" for a ref int instead of "int&"
350 public Type GetParameterInfo (DeclSpace ds, int idx, out bool is_out)
355 FixedParameters = null;
359 if (FixedParameters == null && ArrayParameter == null)
363 if (ComputeParameterTypes (ds) == false){
369 // If this is a request for the variable lenght arg.
371 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
372 if (idx == array_idx){
378 // Otherwise, it is a fixed parameter
380 Parameter p = FixedParameters [idx];
381 is_out = ((p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0);
383 return p.ParameterType;
386 public CallingConventions GetCallingConvention ()
388 // For now this is the only correc thing to do
389 return CallingConventions.Standard;