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;
46 public bool Resolve (DeclSpace ds, Location l)
48 ParameterType = RootContext.LookupType (ds, TypeName, false, l);
49 return ParameterType != null;
52 public Type ExternalType (DeclSpace ds, Location l)
54 if ((ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
55 string n = ParameterType.FullName + "&";
57 Type t = RootContext.LookupType (ds, n, false, l);
65 public ParameterAttributes Attributes {
69 return ParameterAttributes.None;
71 return ParameterAttributes.None;
73 return ParameterAttributes.Out;
78 return ParameterAttributes.None;
83 /// Returns the signature for this parameter evaluating it on the
86 public string GetSignature (DeclSpace ds, Location loc)
88 if (ParameterType == null){
89 if (!Resolve (ds, loc))
93 return ExternalType (ds, loc).FullName;
98 /// Represents the methods parameters
100 public class Parameters {
101 public Parameter [] FixedParameters;
102 public readonly Parameter ArrayParameter;
107 static Parameters empty_parameters;
109 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
111 FixedParameters = fixed_parameters;
112 ArrayParameter = array_parameter;
117 /// This is used to reuse a set of empty parameters, because they
120 public static Parameters GetEmptyReadOnlyParameters ()
122 if (empty_parameters == null)
123 empty_parameters = new Parameters (null, null, Location.Null);
125 return empty_parameters;
130 return (FixedParameters == null) && (ArrayParameter == null);
134 public void ComputeSignature (DeclSpace ds)
137 if (FixedParameters != null){
138 for (int i = 0; i < FixedParameters.Length; i++){
139 Parameter par = FixedParameters [i];
141 signature += par.GetSignature (ds, loc);
145 // Note: as per the spec, the `params' arguments (ArrayParameter)
146 // are not used in the signature computation for a method
150 static void error100 (string name)
153 100, "The parameter name `" + name + "' is a duplicate");
156 public bool VerifyArgs ()
161 if (FixedParameters == null)
164 count = FixedParameters.Length;
165 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
166 for (i = 0; i < count; i++){
167 string base_name = FixedParameters [i].Name;
169 for (j = i + 1; j < count; j++){
170 if (base_name != FixedParameters [j].Name)
172 error100 (base_name);
176 if (base_name == array_par_name){
177 error100 (base_name);
185 /// Returns the signature of the Parameters evaluated in
186 /// the @tc environment
188 public string GetSignature (DeclSpace ds)
190 if (signature == null){
192 ComputeSignature (ds);
199 /// Returns the paramenter information based on the name
201 public Parameter GetParameterByName (string name, out int idx)
206 if (FixedParameters != null){
207 foreach (Parameter par in FixedParameters){
208 if (par.Name == name){
216 if (ArrayParameter != null){
217 if (name == ArrayParameter.Name){
219 return ArrayParameter;
226 bool ComputeParameterTypes (DeclSpace ds)
228 int extra = (ArrayParameter != null) ? 1 : 0;
232 if (FixedParameters == null)
235 pc = extra + FixedParameters.Length;
237 types = new Type [pc];
240 FixedParameters = null;
244 if (FixedParameters != null){
245 foreach (Parameter p in FixedParameters){
248 if (p.Resolve (ds, loc))
249 t = p.ExternalType (ds, loc);
257 if (ArrayParameter.Resolve (ds, loc))
258 types [i] = ArrayParameter.ExternalType (ds, loc);
265 /// Returns the argument types as an array
267 static Type [] no_types = new Type [0];
269 public Type [] GetParameterInfo (DeclSpace ds)
274 if (FixedParameters == null && ArrayParameter == null)
277 if (ComputeParameterTypes (ds) == false)
284 /// Returns the type of a given parameter, and stores in the `is_out'
285 /// boolean whether this is an out or ref parameter.
287 /// Note that the returned type will not contain any dereference in this
288 /// case (ie, you get "int" for a ref int instead of "int&"
290 public Type GetParameterInfo (DeclSpace ds, int idx, out bool is_out)
295 FixedParameters = null;
299 if (FixedParameters == null && ArrayParameter == null)
303 if (ComputeParameterTypes (ds) == false){
309 // If this is a request for the variable lenght arg.
311 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
312 if (idx == array_idx){
318 // Otherwise, it is a fixed parameter
320 Parameter p = FixedParameters [idx];
321 is_out = ((p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0);
323 return p.ParameterType;
326 public CallingConventions GetCallingConvention ()
328 // For now this is the only correc thing to do
329 return CallingConventions.Standard;