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 static void error100 (string name)
163 100, "The parameter name `" + name + "' is a duplicate");
166 public bool VerifyArgs ()
171 if (FixedParameters == null)
174 count = FixedParameters.Length;
175 for (i = 0; i < count; i++){
176 string base_name = FixedParameters [i].Name;
178 for (j = i + 1; j < count; j++){
179 if (base_name != FixedParameters [j].Name)
181 error100 (base_name);
185 if (base_name == ArrayParameter.Name){
186 error100 (base_name);
194 /// Returns the signature of the Parameters evaluated in
195 /// the @tc environment
197 public string GetSignature (DeclSpace ds)
199 if (signature == null){
201 ComputeSignature (ds);
208 /// Returns the paramenter information based on the name
210 public Parameter GetParameterByName (string name, out int idx)
215 if (FixedParameters != null){
216 foreach (Parameter par in FixedParameters){
217 if (par.Name == name){
225 if (ArrayParameter != null){
226 if (name == ArrayParameter.Name){
228 return ArrayParameter;
235 bool ComputeParameterTypes (DeclSpace ds)
237 int extra = (ArrayParameter != null) ? 1 : 0;
241 if (FixedParameters == null)
244 pc = extra + FixedParameters.Length;
246 types = new Type [pc];
249 FixedParameters = null;
253 if (FixedParameters != null){
254 foreach (Parameter p in FixedParameters){
257 if (p.Resolve (ds, loc))
258 t = p.ExternalType ();
266 if (ArrayParameter.Resolve (ds, loc))
267 types [i] = ArrayParameter.ExternalType ();
274 /// Returns the argument types as an array
276 static Type [] no_types = new Type [0];
278 public Type [] GetParameterInfo (DeclSpace ds)
283 if (FixedParameters == null && ArrayParameter == null)
286 if (ComputeParameterTypes (ds) == false)
293 /// Returns the type of a given parameter, and stores in the `is_out'
294 /// boolean whether this is an out or ref parameter.
296 /// Note that the returned type will not contain any dereference in this
297 /// case (ie, you get "int" for a ref int instead of "int&"
299 public Type GetParameterInfo (DeclSpace ds, int idx, out bool is_out)
304 FixedParameters = null;
308 if (FixedParameters == null && ArrayParameter == null)
312 if (ComputeParameterTypes (ds) == false){
318 // If this is a request for the variable lenght arg.
320 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
321 if (idx == array_idx){
327 // Otherwise, it is a fixed parameter
329 Parameter p = FixedParameters [idx];
330 is_out = ((p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0);
332 return p.ParameterType;
335 public CallingConventions GetCallingConvention ()
337 // For now this is the only correc thing to do
338 return CallingConventions.Standard;