**** Merged from MCS ****
[mono-project.git] / mcs / ilasm / codegen / Method.cs
blob416b38d7b44bcf66f6b2b35e6ee43eaf05c0d8b7
1 // Method.cs
2 // (C) Sergey Chaban (serge@wildwestsoftware.com)
4 using System;
5 using System.Collections;
6 using System.Reflection;
7 using System.Reflection.Emit;
9 namespace Mono.ILASM {
12 public class MethodName {
13 private static int methodCount = 0;
15 private bool isCtor;
16 private string name;
18 /// <summary>
19 /// </summary>
20 public MethodName () : this ("M_" + (methodCount++))
24 /// <summary>
25 /// </summary>
26 /// <param name="name"></param>
27 public MethodName (string name) : this (name, false)
31 /// <summary>
32 /// </summary>
33 /// <param name="name"></param>
34 /// <param name="ctor"></param>
35 public MethodName (string name, bool ctor)
37 this.name = name;
38 this.isCtor = ctor;
42 /// <summary>
43 /// </summary>
44 public string Name {
45 get {
46 return name;
48 set {
49 name = value;
54 /// <summary>
55 /// </summary>
56 public bool IsCtor {
57 get {
58 return isCtor;
60 set {
61 isCtor = value;
69 /// <summary>
70 /// </summary>
71 public class Method {
73 private MethodName name;
74 private MethodAttributes attrs;
75 private CallingConventions callConv;
76 private string retType;
77 private MethodBuilder method_builder;
78 private bool entry_point = false;
80 private ArrayList param_list;
81 private ArrayList instructions;
82 private ArrayList local_list;
84 /// <summary>
85 /// </summary>
86 public Method ()
88 name = new MethodName ();
89 attrs = 0;
93 /// <summary>
94 /// </summary>
95 public string Name {
96 get {
97 return name.Name;
99 set {
100 name.Name = value;
105 /// <summary>
106 /// </summary>
107 /// <param name="name"></param>
108 public void SetMethodName (MethodName name)
110 this.name = name;
114 /// <summary>
115 /// </summary>
116 public bool IsCtor {
117 get {
118 return name.IsCtor;
120 set {
121 name.IsCtor = value;
126 /// <summary>
127 /// </summary>
128 public string RetType {
129 get {
130 return retType;
132 set {
133 retType = value;
138 /// <summary>
139 /// </summary>
140 public MethodAttributes Attrs {
141 get {
142 return attrs;
144 set {
145 attrs = value;
150 /// <summary>
151 /// </summary>
152 public CallingConventions CallConv {
153 get {
154 return callConv;
156 set {
157 callConv = value;
162 /// <summary>
163 /// </summary>
164 public bool IsEntryPoint {
165 get {
166 return entry_point;
168 set {
169 entry_point = value;
173 /// <summary>
174 /// </summary>
175 /// <param name="instr"></param>
176 public void AddInstruction (InstrBase instr)
178 if (instr == null) {
179 throw new System.ArgumentNullException ("instr", "<null> instruction");
182 if (instructions == null) {
183 this.instructions = new ArrayList ();
186 instructions.Add (instr);
189 public void AddLocal (DictionaryEntry local)
191 if (local_list == null)
192 local_list = new ArrayList ();
194 local_list.Add (local);
198 public void SetParamList (ArrayList param_list)
200 this.param_list = param_list;
203 /// <summary>
204 /// </summary>
205 public int InstrCount {
206 get {
207 return (instructions != null) ? instructions.Count : 0;
211 /// <summary>
212 /// </summary>
213 /// <returns></returns>
214 public override string ToString ()
216 return String.Format ("IL.Method [Name: {0}, Attrs: {1}, CallConv: {2}, RetType: {3}, Instr: {4}]",
217 Name, Attrs, CallConv, RetType, InstrCount);
220 public MethodBuilder Builder {
221 get {
222 return method_builder;
226 public void Resolve (Class host)
228 Type return_type = host.CodeGen.TypeManager[RetType];
229 method_builder = host.TypeBuilder.DefineMethod (Name, Attrs,
230 CallConv, return_type, CreateTypeList (host.CodeGen.TypeManager));
233 /// <summary>
234 /// </summary>
235 /// <param name="tb"></param>
236 public void Emit (Class host)
238 TypeBuilder tb = host.TypeBuilder;
240 if (IsCtor) {
241 } else {
242 ILGenerator ilgen = method_builder.GetILGenerator ();
244 if (local_list != null) {
245 foreach (DictionaryEntry local in local_list) {
246 Type local_type = host.CodeGen.TypeManager[(string)local.Key];
247 if (local_type == null) {
248 Console.WriteLine ("Could not find type: {0}", local.Key);
249 return;
251 ilgen.DeclareLocal (local_type);
255 if (instructions != null) {
256 foreach (InstrBase instr in instructions)
257 instr.Emit (ilgen, host);
262 private Type[] CreateTypeList (TypeManager type_manager)
264 if (param_list == null)
265 return new Type[0];
267 int count = param_list.Count;
268 Type[] type_list = new Type[count];
270 for (int i=0; i<count; i++) {
271 type_list[i] = type_manager[(string) param_list[i]];
274 return type_list;