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 GenericConstraints constraints;
41 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
46 OptAttributes = attrs;
50 // Resolve is used in method definitions
52 public bool Resolve (DeclSpace ds, Location l)
54 TypeExpr texpr = ds.ResolveTypeExpr (TypeName, false, l);
58 TypeParameterExpr tparam = texpr as TypeParameterExpr;
60 constraints = tparam.TypeParameter.Constraints;
62 parameter_type = ds.ResolveType (texpr, l);
64 if (parameter_type == TypeManager.void_type){
65 Report.Error (1536, l, "`void' parameter is not permitted");
69 return parameter_type != null;
72 public Type ExternalType ()
74 if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
75 return TypeManager.GetReferenceType (parameter_type);
77 return parameter_type;
80 public Type ParameterType {
82 return parameter_type;
86 public GenericConstraints GenericConstraints {
92 public ParameterAttributes Attributes {
94 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
95 switch ((Modifier) flags) {
97 return ParameterAttributes.None;
99 return ParameterAttributes.None;
101 return ParameterAttributes.Out;
102 case Modifier.PARAMS:
106 return ParameterAttributes.None;
111 /// Returns the signature for this parameter evaluating it on the
114 public string GetSignature (DeclSpace ds, Location loc)
116 if (parameter_type == null){
117 if (!Resolve (ds, loc))
121 return ExternalType ().FullName;
126 /// Represents the methods parameters
128 public class Parameters {
129 public Parameter [] FixedParameters;
130 public readonly Parameter ArrayParameter;
135 static Parameters empty_parameters;
137 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
139 FixedParameters = fixed_parameters;
140 ArrayParameter = array_parameter;
145 /// This is used to reuse a set of empty parameters, because they
148 public static Parameters EmptyReadOnlyParameters {
150 if (empty_parameters == null)
151 empty_parameters = new Parameters (null, null, Location.Null);
153 return empty_parameters;
159 return (FixedParameters == null) && (ArrayParameter == null);
163 public void ComputeSignature (DeclSpace ds)
166 if (FixedParameters != null){
167 for (int i = 0; i < FixedParameters.Length; i++){
168 Parameter par = FixedParameters [i];
170 signature += par.GetSignature (ds, loc);
174 // Note: as per the spec, the `params' arguments (ArrayParameter)
175 // are not used in the signature computation for a method
179 void Error_DuplicateParameterName (string name)
182 100, loc, "The parameter name `" + name + "' is a duplicate");
185 public bool VerifyArgs ()
190 if (FixedParameters == null)
193 count = FixedParameters.Length;
194 string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
195 for (i = 0; i < count; i++){
196 string base_name = FixedParameters [i].Name;
198 for (j = i + 1; j < count; j++){
199 if (base_name != FixedParameters [j].Name)
201 Error_DuplicateParameterName (base_name);
205 if (base_name == array_par_name){
206 Error_DuplicateParameterName (base_name);
214 /// Returns the signature of the Parameters evaluated in
215 /// the @tc environment
217 public string GetSignature (DeclSpace ds)
219 if (signature == null){
221 ComputeSignature (ds);
228 /// Returns the paramenter information based on the name
230 public Parameter GetParameterByName (string name, out int idx)
235 if (FixedParameters != null){
236 foreach (Parameter par in FixedParameters){
237 if (par.Name == name){
245 if (ArrayParameter != null){
246 if (name == ArrayParameter.Name){
248 return ArrayParameter;
255 bool ComputeParameterTypes (DeclSpace ds)
257 int extra = (ArrayParameter != null) ? 1 : 0;
261 if (FixedParameters == null)
264 pc = extra + FixedParameters.Length;
266 types = new Type [pc];
269 FixedParameters = null;
274 if (FixedParameters != null){
275 foreach (Parameter p in FixedParameters){
278 if (p.Resolve (ds, loc))
279 t = p.ExternalType ();
289 if (ArrayParameter.Resolve (ds, loc))
290 types [i] = ArrayParameter.ExternalType ();
304 // This variant is used by Delegates, because they need to
305 // resolve/define names, instead of the plain LookupType
307 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
309 int extra = (ArrayParameter != null) ? 1 : 0;
313 if (FixedParameters == null)
316 pc = extra + FixedParameters.Length;
318 types = new Type [pc];
321 FixedParameters = null;
327 if (FixedParameters != null){
328 foreach (Parameter p in FixedParameters){
331 if (p.Resolve (ds, loc))
332 t = p.ExternalType ();
342 if (ArrayParameter.Resolve (ds, loc))
343 types [i] = ArrayParameter.ExternalType ();
349 // invalidate the cached types
359 /// Returns the argument types as an array
361 static Type [] no_types = new Type [0];
363 public Type [] GetParameterInfo (DeclSpace ds)
368 if (FixedParameters == null && ArrayParameter == null)
371 if (ComputeParameterTypes (ds) == false){
380 /// Returns the type of a given parameter, and stores in the `is_out'
381 /// boolean whether this is an out or ref parameter.
383 /// Note that the returned type will not contain any dereference in this
384 /// case (ie, you get "int" for a ref int instead of "int&"
386 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
388 mod = Parameter.Modifier.NONE;
391 FixedParameters = null;
395 if (FixedParameters == null && ArrayParameter == null)
399 if (ComputeParameterTypes (ds) == false)
403 // If this is a request for the variable lenght arg.
405 int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
406 if (idx == array_idx)
410 // Otherwise, it is a fixed parameter
412 Parameter p = FixedParameters [idx];
415 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
416 mod |= Parameter.Modifier.ISBYREF;
418 return p.ParameterType;
421 public CallingConventions GetCallingConvention ()
423 // For now this is the only correc thing to do
424 return CallingConventions.Standard;