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 ()
54 if ((ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
55 string n = ParameterType.FullName + "&";
61 // It is a type defined by the source code we are compiling
64 ModuleBuilder mb = RootContext.ModuleBuilder;
75 public ParameterAttributes Attributes {
79 return ParameterAttributes.None;
81 return ParameterAttributes.None;
83 return ParameterAttributes.Out;
88 return ParameterAttributes.None;
93 /// Returns the signature for this parameter evaluating it on the
96 public string GetSignature (DeclSpace ds, Location loc)
98 if (ParameterType == null){
99 if (!Resolve (ds, loc))
103 return ExternalType ().FullName;
108 /// Represents the methods parameters
110 public class Parameters {
111 public Parameter [] FixedParameters;
112 public readonly Parameter ArrayParameter;
117 static Parameters empty_parameters;
119 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
121 FixedParameters = fixed_parameters;
122 ArrayParameter = array_parameter;
127 /// This is used to reuse a set of empty parameters, because they
130 public static Parameters GetEmptyReadOnlyParameters ()
132 if (empty_parameters == null)
133 empty_parameters = new Parameters (null, null, Location.Null);
135 return empty_parameters;
140 return (FixedParameters == null) && (ArrayParameter == null);
144 public void ComputeSignature (DeclSpace ds)
147 if (FixedParameters != null){
148 for (int i = 0; i < FixedParameters.Length; i++){
149 Parameter par = FixedParameters [i];
151 signature += par.GetSignature (ds, loc);
155 // Note: as per the spec, the `params' arguments (ArrayParameter)
156 // are not used in the signature computation for a method
160 public bool VerifyArgs ()
165 if (FixedParameters == null)
168 count = FixedParameters.Length;
169 for (i = 0; i < count; i++){
170 for (j = i + 1; j < count; j++){
171 if (FixedParameters [i].Name != FixedParameters [j].Name)
174 100, "The parameter name `" + FixedParameters [i].Name +
183 /// Returns the signature of the Parameters evaluated in
184 /// the @tc environment
186 public string GetSignature (DeclSpace ds)
188 if (signature == null){
190 ComputeSignature (ds);
197 /// Returns the paramenter information based on the name
199 public Parameter GetParameterByName (string name, out int idx)
203 if (FixedParameters == null)
207 foreach (Parameter par in FixedParameters){
208 if (par.Name == name){
215 if (ArrayParameter != null)
216 if (name == ArrayParameter.Name){
218 return ArrayParameter;
224 bool ComputeParameterTypes (DeclSpace ds)
226 int extra = (ArrayParameter != null) ? 1 : 0;
228 int pc = FixedParameters.Length + extra;
230 types = new Type [pc];
233 FixedParameters = null;
237 foreach (Parameter p in FixedParameters){
240 if (p.Resolve (ds, loc))
241 t = p.ExternalType ();
248 if (ArrayParameter.Resolve (ds, loc))
249 types [i] = ArrayParameter.ExternalType ();
256 /// Returns the argument types as an array
258 static Type [] no_types = new Type [0];
260 public Type [] GetParameterInfo (DeclSpace ds)
265 if (FixedParameters == null)
268 if (ComputeParameterTypes (ds) == false)
275 /// Returns the type of a given parameter, and stores in the `is_out'
276 /// boolean whether this is an out or ref parameter.
278 /// Note that the returned type will not contain any dereference in this
279 /// case (ie, you get "int" for a ref int instead of "int&"
281 public Type GetParameterInfo (DeclSpace ds, int idx, out bool is_out)
286 FixedParameters = null;
290 if (FixedParameters == null)
294 if (ComputeParameterTypes (ds) == false){
300 // If this is a request for the variable lenght arg.
302 if (idx == FixedParameters.Length){
308 // Otherwise, it is a fixed parameter
310 Parameter p = FixedParameters [idx];
311 is_out = ((p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0);
313 return p.ParameterType;
316 public CallingConventions GetCallingConvention ()
318 // For now this is the only correc thing to do
319 return CallingConventions.Standard;