Fix typos.
[mono.git] / mcs / mcs / parameter.cs
1 //
2 // parameter.cs: Parameter definition.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //
6 // Licensed under the terms of the GNU GPL
7 //
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
9 //
10 //
11 //
12 using System;
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Collections;
16
17 namespace Mono.CSharp {
18
19
20         /// <summary>
21         ///   Represents a single method parameter
22         /// </summary>
23         public class Parameter {
24                 [Flags]
25                 public enum Modifier : byte {
26                         NONE   = 0,
27                         REF    = 1,
28                         OUT    = 2,
29                         PARAMS = 4,
30                 }
31
32                 public readonly string   TypeName;
33                 public readonly string   Name;
34                 public readonly Modifier ModFlags;
35                 public Attributes OptAttributes;
36                 public Type ParameterType;
37                 
38                 public Parameter (string type, string name, Modifier mod, Attributes attrs)
39                 {
40                         Name = name;
41                         ModFlags = mod;
42                         TypeName = type;
43                         OptAttributes = attrs;
44                 }
45
46                 // <summary>
47                 //   Resolve is used in method definitions
48                 // </summary>
49                 public bool Resolve (DeclSpace ds, Location l)
50                 {
51                         ParameterType = RootContext.LookupType (ds, TypeName, false, l);
52                         return ParameterType != null;
53                 }
54
55                 // <summary>
56                 //   ResolveAndDefine is used by delegate declarations, because
57                 //   they happen during the initial tree resolution process
58                 // </summary>
59                 public bool ResolveAndDefine (DeclSpace ds)
60                 {
61                         ParameterType = ds.FindType (TypeName);
62                         return ParameterType != null;
63                 }
64                 
65                 public Type ExternalType (DeclSpace ds, Location l)
66                 {
67                         if ((ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
68                                 string n = ParameterType.FullName + "&";
69
70                                 Type t = RootContext.LookupType (ds, n, false, l);
71
72                                 return t;
73                         }
74                         
75                         return ParameterType;
76                 }
77                 
78                 public ParameterAttributes Attributes {
79                         get {
80                                 switch (ModFlags){
81                                 case Modifier.NONE:
82                                         return ParameterAttributes.None;
83                                 case Modifier.REF:
84                                         return ParameterAttributes.None;
85                                 case Modifier.OUT:
86                                         return ParameterAttributes.Out;
87                                 case Modifier.PARAMS:
88                                         return 0;
89                                 }
90                                 
91                                 return ParameterAttributes.None;
92                         }
93                 }
94                 
95                 /// <summary>
96                 ///   Returns the signature for this parameter evaluating it on the
97                 ///   @tc context
98                 /// </summary>
99                 public string GetSignature (DeclSpace ds, Location loc)
100                 {
101                         if (ParameterType == null){
102                                 if (!Resolve (ds, loc))
103                                         return null;
104                         }
105
106                         return ExternalType (ds, loc).FullName;
107                 }
108         }
109
110         /// <summary>
111         ///   Represents the methods parameters
112         /// </summary>
113         public class Parameters {
114                 public Parameter [] FixedParameters;
115                 public readonly Parameter ArrayParameter;
116                 string signature;
117                 Type [] types;
118                 Location loc;
119                 
120                 static Parameters empty_parameters;
121                 
122                 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
123                 {
124                         FixedParameters = fixed_parameters;
125                         ArrayParameter  = array_parameter;
126                         loc = l;
127                 }
128
129                 /// <summary>
130                 ///   This is used to reuse a set of empty parameters, because they
131                 ///   are common
132                 /// </summary>
133                 public static Parameters GetEmptyReadOnlyParameters ()
134                 {
135                         if (empty_parameters == null)
136                                 empty_parameters = new Parameters (null, null, Location.Null);
137                         
138                         return empty_parameters;
139                 }
140                 
141                 public bool Empty {
142                         get {
143                                 return (FixedParameters == null) && (ArrayParameter == null);
144                         }
145                 }
146                 
147                 public void ComputeSignature (DeclSpace ds)
148                 {
149                         signature = "";
150                         if (FixedParameters != null){
151                                 for (int i = 0; i < FixedParameters.Length; i++){
152                                         Parameter par = FixedParameters [i];
153                                         
154                                         signature += par.GetSignature (ds, loc);
155                                 }
156                         }
157                         //
158                         // Note: as per the spec, the `params' arguments (ArrayParameter)
159                         // are not used in the signature computation for a method
160                         //
161                 }
162
163                 static void error100 (string name)
164                 {
165                         Report.Error (
166                                 100, "The parameter name `" + name + "' is a duplicate");
167                 }
168                 
169                 public bool VerifyArgs ()
170                 {
171                         int count;
172                         int i, j;
173
174                         if (FixedParameters == null)
175                                 return true;
176                         
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;
181                                 
182                                 for (j = i + 1; j < count; j++){
183                                         if (base_name != FixedParameters [j].Name)
184                                                 continue;
185                                         error100 (base_name);
186                                         return false;
187                                 }
188
189                                 if (base_name == array_par_name){
190                                         error100 (base_name);
191                                         return false;
192                                 }
193                         }
194                         return true;
195                 }
196                 
197                 /// <summary>
198                 ///    Returns the signature of the Parameters evaluated in
199                 ///    the @tc environment
200                 /// </summary>
201                 public string GetSignature (DeclSpace ds)
202                 {
203                         if (signature == null){
204                                 VerifyArgs ();
205                                 ComputeSignature (ds);
206                         }
207                         
208                         return signature;
209                 }
210                 
211                 /// <summary>
212                 ///    Returns the paramenter information based on the name
213                 /// </summary>
214                 public Parameter GetParameterByName (string name, out int idx)
215                 {
216                         idx = 0;
217                         int i = 0;
218
219                         if (FixedParameters != null){
220                                 foreach (Parameter par in FixedParameters){
221                                         if (par.Name == name){
222                                                 idx = i;
223                                                 return par;
224                                         }
225                                         i++;
226                                 }
227                         }
228
229                         if (ArrayParameter != null){
230                                 if (name == ArrayParameter.Name){
231                                         idx = i;
232                                         return ArrayParameter;
233                                 }
234                         }
235                         
236                         return null;
237                 }
238
239                 bool ComputeParameterTypes (DeclSpace ds)
240                 {
241                         int extra = (ArrayParameter != null) ? 1 : 0;
242                         int i = 0;
243                         int pc;
244
245                         if (FixedParameters == null)
246                                 pc = extra;
247                         else
248                                 pc = extra + FixedParameters.Length;
249                         
250                         types = new Type [pc];
251                         
252                         if (!VerifyArgs ()){
253                                 FixedParameters = null;
254                                 return false;
255                         }
256
257                         bool failed = false;
258                         if (FixedParameters != null){
259                                 foreach (Parameter p in FixedParameters){
260                                         Type t = null;
261                                         
262                                         if (p.Resolve (ds, loc))
263                                                 t = p.ExternalType (ds, loc);
264                                         else
265                                                 failed = true;
266                                         
267                                         types [i] = t;
268                                         i++;
269                                 }
270                         }
271                         if (failed)
272                                 return false;
273                         
274                         if (extra > 0){
275                                 if (ArrayParameter.Resolve (ds, loc))
276                                         types [i] = ArrayParameter.ExternalType (ds, loc);
277                         }
278
279                         return true;
280                 }
281
282                 //
283                 // This variant is used by Delegates, because they need to
284                 // resolve/define names, instead of the plain LookupType
285                 //
286                 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
287                 {
288                         int extra = (ArrayParameter != null) ? 1 : 0;
289                         int i = 0;
290                         int pc;
291
292                         if (FixedParameters == null)
293                                 pc = extra;
294                         else
295                                 pc = extra + FixedParameters.Length;
296                         
297                         types = new Type [pc];
298                         
299                         if (!VerifyArgs ()){
300                                 FixedParameters = null;
301                                 return false;
302                         }
303
304                         if (FixedParameters != null){
305                                 foreach (Parameter p in FixedParameters){
306                                         Type t = null;
307                                         
308                                         if (p.ResolveAndDefine (ds))
309                                                 t = p.ExternalType (ds, loc);
310                                         
311                                         types [i] = t;
312                                         i++;
313                                 }
314                         }
315                         
316                         if (extra > 0){
317                                 if (ArrayParameter.ResolveAndDefine (ds))
318                                         types [i] = ArrayParameter.ExternalType (ds, loc);
319                         }
320
321                         return true;
322                 }
323                 
324                 /// <summary>
325                 ///   Returns the argument types as an array
326                 /// </summary>
327                 static Type [] no_types = new Type [0];
328                 
329                 public Type [] GetParameterInfo (DeclSpace ds)
330                 {
331                         if (types != null)
332                                 return types;
333                         
334                         if (FixedParameters == null && ArrayParameter == null)
335                                 return no_types;
336
337                         if (ComputeParameterTypes (ds) == false)
338                                 return null;
339                         
340                         return types;
341                 }
342
343                 /// <summary>
344                 ///   Returns the type of a given parameter, and stores in the `is_out'
345                 ///   boolean whether this is an out or ref parameter.
346                 ///
347                 ///   Note that the returned type will not contain any dereference in this
348                 ///   case (ie, you get "int" for a ref int instead of "int&"
349                 /// </summary>
350                 public Type GetParameterInfo (DeclSpace ds, int idx, out bool is_out)
351                 {
352                         is_out = false;
353                         
354                         if (!VerifyArgs ()){
355                                 FixedParameters = null;
356                                 return null;
357                         }
358
359                         if (FixedParameters == null && ArrayParameter == null)
360                                 return null;
361                         
362                         if (types == null)
363                                 if (ComputeParameterTypes (ds) == false){
364                                         is_out = false;
365                                         return null;
366                                 }
367
368                         //
369                         // If this is a request for the variable lenght arg.
370                         //
371                         int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
372                         if (idx == array_idx){
373                                 is_out = false;
374                                 return types [idx];
375                         } 
376
377                         //
378                         // Otherwise, it is a fixed parameter
379                         //
380                         Parameter p = FixedParameters [idx];
381                         is_out = ((p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0);
382
383                         return p.ParameterType;
384                 }
385
386                 public CallingConventions GetCallingConvention ()
387                 {
388                         // For now this is the only correc thing to do
389                         return CallingConventions.Standard;
390                 }
391         }
392 }
393                 
394         
395