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;
147 public void AppendParameter (Parameter p)
149 Parameter [] pa = new Parameter [FixedParameters.Length+1];
150 FixedParameters.CopyTo (pa, 0);
151 pa[FixedParameters.Length] = p;
152 FixedParameters = pa;
157 return (FixedParameters == null) && (ArrayParameter == null);
161 public void ComputeSignature (DeclSpace ds)
164 if (FixedParameters != null){
165 for (int i = 0; i < FixedParameters.Length; i++){
166 Parameter par = FixedParameters [i];
168 signature += par.GetSignature (ds, loc);
172 // Note: as per the spec, the `params' arguments (ArrayParameter)
173 // are not used in the signature computation for a method
177 static void Error_DuplicateParameterName (string name)
180 100, "The parameter name `" + name + "' is a duplicate");
183 public bool VerifyArgs ()
188 if (FixedParameters == null)
191 count = FixedParameters.Length;
192 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
193 for (i = 0; i < count; i++){
194 string base_name = FixedParameters [i].Name;
196 for (j = i + 1; j < count; j++){
197 if (base_name != FixedParameters [j].Name)
199 Error_DuplicateParameterName (base_name);
203 if (base_name == array_par_name){
204 Error_DuplicateParameterName (base_name);
212 /// Returns the signature of the Parameters evaluated in
213 /// the @tc environment
215 public string GetSignature (DeclSpace ds)
217 if (signature == null){
219 ComputeSignature (ds);
226 /// Returns the paramenter information based on the name
228 public Parameter GetParameterByName (string name, out int idx)
233 if (FixedParameters != null){
234 foreach (Parameter par in FixedParameters){
235 if (par.Name == name){
243 if (ArrayParameter != null){
244 if (name == ArrayParameter.Name){
246 return ArrayParameter;
253 bool ComputeParameterTypes (DeclSpace ds)
255 int extra = (ArrayParameter != null) ? 1 : 0;
259 if (FixedParameters == null)
262 pc = extra + FixedParameters.Length;
264 types = new Type [pc];
267 FixedParameters = null;
272 if (FixedParameters != null){
273 foreach (Parameter p in FixedParameters){
276 if (p.Resolve (ds, loc))
277 t = p.ExternalType (ds, loc);
287 if (ArrayParameter.Resolve (ds, loc))
288 types [i] = ArrayParameter.ExternalType (ds, loc);
302 // This variant is used by Delegates, because they need to
303 // resolve/define names, instead of the plain LookupType
305 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
307 int extra = (ArrayParameter != null) ? 1 : 0;
311 if (FixedParameters == null)
314 pc = extra + FixedParameters.Length;
316 types = new Type [pc];
319 FixedParameters = null;
325 if (FixedParameters != null){
326 foreach (Parameter p in FixedParameters){
329 if (p.Resolve (ds, loc))
330 t = p.ExternalType (ds, loc);
340 if (ArrayParameter.Resolve (ds, loc))
341 types [i] = ArrayParameter.ExternalType (ds, loc);
347 // invalidate the cached types
357 /// Returns the argument types as an array
359 static Type [] no_types = new Type [0];
361 public Type [] GetParameterInfo (DeclSpace ds)
366 if (FixedParameters == null && ArrayParameter == null)
369 if (ComputeParameterTypes (ds) == false){
378 /// Returns the type of a given parameter, and stores in the `is_out'
379 /// boolean whether this is an out or ref parameter.
381 /// Note that the returned type will not contain any dereference in this
382 /// case (ie, you get "int" for a ref int instead of "int&"
384 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
386 mod = Parameter.Modifier.NONE;
389 FixedParameters = null;
393 if (FixedParameters == null && ArrayParameter == null)
397 if (ComputeParameterTypes (ds) == false)
401 // If this is a request for the variable lenght arg.
403 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
404 if (idx == array_idx)
408 // Otherwise, it is a fixed parameter
410 Parameter p = FixedParameters [idx];
413 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
414 mod |= Parameter.Modifier.ISBYREF;
416 return p.ParameterType;
419 public CallingConventions GetCallingConvention ()
421 // For now this is the only correc thing to do
422 return CallingConventions.Standard;