2001-10-04 Miguel de Icaza <miguel@ximian.com>
[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
13 namespace CIR {
14
15         using System;
16         using System.Reflection;
17         using System.Collections;
18         
19         public class Parameter {
20                 public enum Modifier {
21                         NONE,
22                         REF,
23                         OUT,
24                         PARAMS,
25                 }
26
27                 public readonly string   Type;
28                 public readonly string   Name;
29                 public readonly Modifier ModFlags;
30                 public Attributes OptAttributes;
31                 
32                 public Parameter (string type, string name, Modifier mod, Attributes attrs)
33                 {
34                         Name = name;
35                         ModFlags = mod;
36                         Type = type = type;
37                         OptAttributes = attrs;
38                 }
39
40                 public ParameterAttributes Attributes {
41                         get {
42                                 switch (ModFlags){
43                                 case Modifier.NONE:
44                                         return ParameterAttributes.None;
45                                 case Modifier.REF:
46                                         return ParameterAttributes.Retval;
47                                 case Modifier.OUT:
48                                         return ParameterAttributes.Out | ParameterAttributes.Retval;
49                                 case Modifier.PARAMS:
50                                         return 0;
51                                 }
52                                 
53                                 return ParameterAttributes.None;
54                         }
55                 }
56                 
57                 string ModSignature ()
58                 {
59                         switch (ModFlags){
60                         case Modifier.NONE:
61                                 return "";
62                         case Modifier.REF:
63                                 return "&";
64                         case Modifier.OUT:
65                                 return ">";
66                         case Modifier.PARAMS:
67                                 return "";
68                         }
69                         // This should not happen.
70                         return (string) null;
71                 }
72
73                 // <summary>
74                 //   Returns the signature for this parameter evaluating it on the
75                 //   @tc context
76                 // </summary>
77                 public string GetSignature (TypeContainer tc)
78                 {
79                         Type t = tc.LookupType (Type, false);
80
81                         if (t == null)
82                                 return "";
83                         
84                         return ModSignature () + t.FullName;
85                 }
86         }
87
88         public class Parameters {
89                 public Parameter [] FixedParameters;
90                 public readonly Parameter    ArrayParameter;
91                 string signature;
92                 Type [] types;
93                 
94                 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter)
95                 {
96                         FixedParameters = fixed_parameters;
97                         ArrayParameter  = array_parameter;
98                 }
99
100                 public bool Empty {
101                         get {
102                                 return (FixedParameters == null) && (ArrayParameter == null);
103                         }
104                 }
105                 
106                 public void ComputeSignature (TypeContainer tc)
107                 {
108                         signature = "";
109                         if (FixedParameters != null){
110                                 for (int i = 0; i < FixedParameters.Length; i++){
111                                         Parameter par = FixedParameters [i];
112                                         
113                                         signature += par.GetSignature (tc);
114                                 }
115                         }
116                         //
117                         // Note: as per the spec, the `params' arguments (ArrayParameter)
118                         // are not used in the signature computation for a method
119                         //
120                 }
121
122                 public bool VerifyArgs (TypeContainer tc)
123                 {
124                         int count;
125                         int i, j;
126
127                         if (FixedParameters == null)
128                                 return true;
129                         
130                         count = FixedParameters.Length;
131                         for (i = 0; i < count; i++){
132                                 for (j = i + 1; j < count; j++){
133                                         if (FixedParameters [i].Name != FixedParameters [j].Name)
134                                                 continue;
135                                         Report.Error (
136                                                 100, "The parameter name `" + FixedParameters [i].Name +
137                                                 "' is a duplicate");
138                                         return false;
139                                 }
140                         }
141                         return true;
142                 }
143                 
144                 // <summary>
145                 //    Returns the signature of the Parameters evaluated in
146                 //    the @tc environment
147                 // </summary>
148                 public string GetSignature (TypeContainer tc)
149                 {
150                         if (signature == null){
151                                 VerifyArgs (tc);
152                                 ComputeSignature (tc);
153                         }
154                         
155                         return signature;
156                 }
157                 
158                 // <summary>
159                 //    Returns the paramenter information based on the name
160                 // </summary>
161                 public Parameter GetParameterByName (string name, out int idx)
162                 {
163                         idx = 0;
164
165                         if (FixedParameters == null)
166                                 return null;
167
168                         int i = 0;
169                         foreach (Parameter par in FixedParameters){
170                                 if (par.Name == name){
171                                         idx = i;
172                                         return par;
173                                 }
174                                 i++;
175                         }
176
177                         return null;
178                 }
179
180                 
181                 // <summary>
182                 //   Returns the argument types as an array
183                 // </summary>
184                 public Type [] GetParameterInfo (TypeContainer tc)
185                 {
186                         if (types != null)
187                                 return types;
188                         
189                         if (FixedParameters == null)
190                                 return null;
191                         
192                         int extra = (ArrayParameter != null) ? 1 : 0;
193                         int i = 0;
194                         int pc = FixedParameters.Length + extra;
195                         
196                         types = new Type [pc];
197
198                         if (!VerifyArgs (tc)){
199                                 FixedParameters = null;
200                                 return null;
201                         }
202                         
203                         foreach (Parameter p in FixedParameters){
204                                 Type t = tc.LookupType (p.Type, false);
205
206                                 if ((p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
207                                         t = Type.GetType (t.FullName + "&");
208                                 }
209                                 types [i] = t;
210                                 i++;
211                         }
212
213                         if (extra > 0)
214                                 types [i] = Type.GetType ("System.Object");
215
216                         return types;
217                 }
218
219                 public CallingConventions GetCallingConvention ()
220                 {
221                         if (ArrayParameter != null)
222                                 return CallingConventions.VarArgs;
223                         else
224                                 return CallingConventions.Standard;
225                 }
226         }
227 }
228                 
229         
230