More work
[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 // FIXME: We should deprecate ParameterCollection as it is mostly slow
12 // to access (it uses an arraylist instead of a hashtable) and exposes
13 // no method to quickly locate parameters by name.
14 //
15 // Look at the implementation for GetParameterByName for an example.
16 //
17
18 namespace CIR {
19
20         using System;
21
22         public class Parameter {
23                 public enum Modifier {
24                         NONE,
25                         REF,
26                         OUT,
27                         PARAMS,
28                 }
29
30                 public readonly string   Type;
31                 public readonly string   Name;
32                 public readonly Modifier ModFlags;
33                 
34                 public Parameter (string type, string name, Modifier mod)
35                 {
36                         Name = name;
37                         ModFlags = mod;
38                         Type = type = type;
39                 }
40
41                 string ModSignature ()
42                 {
43                         switch (ModFlags){
44                         case Modifier.NONE:
45                                 return "";
46                         case Modifier.REF:
47                                 return "&";
48                         case Modifier.OUT:
49                                 return ">";
50                         case Modifier.PARAMS:
51                                 return "";
52                         }
53                         // This should not happen.
54                         return (string) null;
55                 }
56
57                 // <summary>
58                 //   Returns the signature for this parameter evaluating it on the
59                 //   @tc context
60                 // </summary>
61                 public string GetSignature (TypeContainer tc)
62                 {
63                         Type t = tc.LookupType (Type, false);
64
65                         if (t == null)
66                                 return "";
67                         
68                         return ModSignature () + t.FullName;
69                 }
70         }
71
72         public class Parameters {
73                 ParameterCollection fixed_parameters;
74                 Parameter array_parameter;
75                 string signature;
76                 
77                 public Parameters (ParameterCollection fixed_parameters, Parameter array_parameter)
78                 {
79                         this.fixed_parameters = fixed_parameters;
80                         this.array_parameter = array_parameter;
81                 }
82
83                 // <summary>
84                 //   Returns the fixed parameters element
85                 // </summary>
86                 public ParameterCollection FixedParameters {
87                         get {
88                                 return fixed_parameters;
89                         }
90                 }
91
92                 // <summary>
93                 //   Returns the array parameter.
94                 // </summary>
95                 public Parameter ArrayParameter {
96                         get {
97                                 return array_parameter;
98                         }
99                 }
100
101                 public void ComputeSignature (TypeContainer tc)
102                 {
103                         signature = "";
104                         if (fixed_parameters != null){
105                                 for (int i = 0; i < fixed_parameters.Count; i++){
106                                         Parameter par = (Parameter) fixed_parameters [i];
107                                         
108                                         signature += par.GetSignature (tc);
109                                 }
110                         }
111                         //
112                         // Note: as per the spec, the `params' arguments (array_parameter)
113                         // are not used in the signature computation for a method
114                         //
115                 }
116
117                 // <summary>
118                 //    Returns the signature of the Parameters evaluated in
119                 //    the @tc environment
120                 // </summary>
121                 public string GetSignature (TypeContainer tc)
122                 {
123                         if (signature == null)
124                                 ComputeSignature (tc);
125                         
126                         return signature;
127                 }
128                 
129                 // <summary>
130                 //    Returns the paramenter information based on the name
131                 // </summary>
132                 public Parameter GetParameterByName (string name)
133                 {
134                         if (fixed_parameters == null)
135                                 return null;
136
137                         foreach (Parameter par in fixed_parameters)
138                                 if (par.Name == name)
139                                         return par;
140
141                         return null;
142                 }
143
144                 // <summary>
145                 //   Returns the argument types as an array
146                 // </summary>
147                 public Type [] GetTypes (TypeContainer tc)
148                 {
149                         int extra = (array_parameter != null) ? 1 : 0;
150                         Type [] types = new Type [fixed_parameters.Count + extra];
151                         int i = 0;
152                         
153                         foreach (Parameter p in fixed_parameters){
154                                 types [i++] = tc.LookupType (p.Name, false);
155                         }
156
157                         if (extra > 0)
158                                 types [i] = Type.GetType ("System.Object");
159
160                         return types;
161                 }
162         }
163 }
164                 
165         
166