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;
57 public Type ExternalType (DeclSpace ds, Location l)
59 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
60 string n = parameter_type.FullName + "&";
62 Type t = RootContext.LookupType (ds, n, false, l);
67 return parameter_type;
70 public Type ParameterType {
72 return parameter_type;
76 public ParameterAttributes Attributes {
78 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
79 switch ((Modifier) flags) {
81 return ParameterAttributes.None;
83 return ParameterAttributes.None;
85 return ParameterAttributes.Out;
90 return ParameterAttributes.None;
95 /// Returns the signature for this parameter evaluating it on the
98 public string GetSignature (DeclSpace ds, Location loc)
100 if (parameter_type == null){
101 if (!Resolve (ds, loc))
105 return ExternalType (ds, loc).FullName;
110 /// Represents the methods parameters
112 public class Parameters {
113 public Parameter [] FixedParameters;
114 public readonly Parameter ArrayParameter;
119 static Parameters empty_parameters;
121 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
123 FixedParameters = fixed_parameters;
124 ArrayParameter = array_parameter;
129 /// This is used to reuse a set of empty parameters, because they
132 public static Parameters EmptyReadOnlyParameters {
134 if (empty_parameters == null)
135 empty_parameters = new Parameters (null, null, Location.Null);
137 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 Error_DuplicateParameterName (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 Error_DuplicateParameterName (base_name);
189 if (base_name == array_par_name){
190 Error_DuplicateParameterName (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);
273 if (ArrayParameter.Resolve (ds, loc))
274 types [i] = ArrayParameter.ExternalType (ds, loc);
288 // This variant is used by Delegates, because they need to
289 // resolve/define names, instead of the plain LookupType
291 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
293 int extra = (ArrayParameter != null) ? 1 : 0;
297 if (FixedParameters == null)
300 pc = extra + FixedParameters.Length;
302 types = new Type [pc];
305 FixedParameters = null;
311 if (FixedParameters != null){
312 foreach (Parameter p in FixedParameters){
315 if (p.Resolve (ds, loc))
316 t = p.ExternalType (ds, loc);
326 if (ArrayParameter.Resolve (ds, loc))
327 types [i] = ArrayParameter.ExternalType (ds, loc);
333 // invalidate the cached types
343 /// Returns the argument types as an array
345 static Type [] no_types = new Type [0];
347 public Type [] GetParameterInfo (DeclSpace ds)
352 if (FixedParameters == null && ArrayParameter == null)
355 if (ComputeParameterTypes (ds) == false){
364 /// Returns the type of a given parameter, and stores in the `is_out'
365 /// boolean whether this is an out or ref parameter.
367 /// Note that the returned type will not contain any dereference in this
368 /// case (ie, you get "int" for a ref int instead of "int&"
370 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
372 mod = Parameter.Modifier.NONE;
375 FixedParameters = null;
379 if (FixedParameters == null && ArrayParameter == null)
383 if (ComputeParameterTypes (ds) == false)
387 // If this is a request for the variable lenght arg.
389 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
390 if (idx == array_idx)
394 // Otherwise, it is a fixed parameter
396 Parameter p = FixedParameters [idx];
399 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
400 mod |= Parameter.Modifier.ISBYREF;
402 return p.ParameterType;
405 public CallingConventions GetCallingConvention ()
407 // For now this is the only correc thing to do
408 return CallingConventions.Standard;