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);
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 (DeclSpace ds, Location l)
65 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
66 string n = parameter_type.FullName + "&";
68 Type t = RootContext.LookupType (ds, n, false, l);
73 return parameter_type;
76 public Type ParameterType {
78 return parameter_type;
82 public ParameterAttributes Attributes {
84 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
85 switch ((Modifier) flags) {
87 return ParameterAttributes.None;
89 return ParameterAttributes.None;
91 return ParameterAttributes.Out;
96 return ParameterAttributes.None;
101 /// Returns the signature for this parameter evaluating it on the
104 public string GetSignature (DeclSpace ds, Location loc)
106 if (parameter_type == null){
107 if (!Resolve (ds, loc))
111 return ExternalType (ds, loc).FullName;
116 /// Represents the methods parameters
118 public class Parameters {
119 public Parameter [] FixedParameters;
120 public readonly Parameter ArrayParameter;
125 static Parameters empty_parameters;
127 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
129 FixedParameters = fixed_parameters;
130 ArrayParameter = array_parameter;
135 /// This is used to reuse a set of empty parameters, because they
138 public static Parameters EmptyReadOnlyParameters {
140 if (empty_parameters == null)
141 empty_parameters = new Parameters (null, null, Location.Null);
143 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 Error_DuplicateParameterName (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 Error_DuplicateParameterName (base_name);
195 if (base_name == array_par_name){
196 Error_DuplicateParameterName (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);
279 if (ArrayParameter.Resolve (ds, loc))
280 types [i] = ArrayParameter.ExternalType (ds, loc);
294 // This variant is used by Delegates, because they need to
295 // resolve/define names, instead of the plain LookupType
297 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
299 int extra = (ArrayParameter != null) ? 1 : 0;
303 if (FixedParameters == null)
306 pc = extra + FixedParameters.Length;
308 types = new Type [pc];
311 FixedParameters = null;
317 if (FixedParameters != null){
318 foreach (Parameter p in FixedParameters){
321 if (p.Resolve (ds, loc))
322 t = p.ExternalType (ds, loc);
332 if (ArrayParameter.Resolve (ds, loc))
333 types [i] = ArrayParameter.ExternalType (ds, loc);
339 // invalidate the cached types
349 /// Returns the argument types as an array
351 static Type [] no_types = new Type [0];
353 public Type [] GetParameterInfo (DeclSpace ds)
358 if (FixedParameters == null && ArrayParameter == null)
361 if (ComputeParameterTypes (ds) == false){
370 /// Returns the type of a given parameter, and stores in the `is_out'
371 /// boolean whether this is an out or ref parameter.
373 /// Note that the returned type will not contain any dereference in this
374 /// case (ie, you get "int" for a ref int instead of "int&"
376 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
378 mod = Parameter.Modifier.NONE;
381 FixedParameters = null;
385 if (FixedParameters == null && ArrayParameter == null)
389 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)
400 // Otherwise, it is a fixed parameter
402 Parameter p = FixedParameters [idx];
405 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
406 mod |= Parameter.Modifier.ISBYREF;
408 return p.ParameterType;
411 public CallingConventions GetCallingConvention ()
413 // For now this is the only correc thing to do
414 return CallingConventions.Standard;