2 // Permission is hereby granted, free of charge, to any person obtaining
3 // a copy of this software and associated documentation files (the
4 // "Software"), to deal in the Software without restriction, including
5 // without limitation the rights to use, copy, modify, merge, publish,
6 // distribute, sublicense, and/or sell copies of the Software, and to
7 // permit persons to whom the Software is furnished to do so, subject to
8 // the following conditions:
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 // Copyright (C) Lluis Sanchez Gual, 2004
26 using System.Collections;
27 using System.Reflection.Emit;
28 using System.Reflection;
30 namespace Mono.CodeGeneration
32 public class CodeBuilder
35 CodeBlock currentBlock;
36 Stack blockStack = new Stack ();
39 ArrayList nestedIfs = new ArrayList();
40 int currentIfSerie = -1;
43 public CodeBuilder (CodeClass codeClass)
45 this.codeClass = codeClass;
46 mainBlock = new CodeBlock ();
47 currentBlock = mainBlock;
50 CodeBuilder (CodeBlock block)
55 public CodeBlock CurrentBlock
62 public CodeClass OwnerClass
64 get { return codeClass; }
67 public void Generate (ILGenerator gen)
70 mainBlock.Generate (gen);
73 catch (Exception ex) {
74 string m = ex.Message + "\nCode block:\n";
75 m += "-----------------------\n";
77 m += "-----------------------\n";
78 throw new Exception (m, ex);
83 public string PrintCode ()
85 StringWriter sw = new StringWriter ();
86 CodeWriter cw = new CodeWriter (sw);
88 return sw.ToString ();
91 public void PrintCode (CodeWriter cp)
93 mainBlock.PrintCode (cp);
96 public CodeVariableReference DeclareVariable (Type type)
98 return DeclareVariable (type, null);
101 public CodeVariableReference DeclareVariable (Type type, object ob)
103 return DeclareVariable (type, Exp.Literal(ob));
106 public CodeVariableReference DeclareVariable (CodeExpression initValue)
108 return DeclareVariable (initValue.GetResultType(), initValue);
111 public CodeVariableReference DeclareVariable (Type type, CodeExpression initValue)
113 string name = "v" + (varId++);
114 CodeVariableDeclaration var = new CodeVariableDeclaration (type, name);
115 currentBlock.Add (var);
116 if (!object.ReferenceEquals (initValue, null))
117 Assign (var.Variable, initValue);
121 public void Assign (CodeValueReference var, CodeExpression val)
123 currentBlock.Add (new CodeAssignment (var, val));
126 public void If (CodeExpression condition)
128 currentBlock.Add (new CodeIf (condition));
133 public void ElseIf (CodeExpression condition)
135 if (nestedIfs.Count == 0)
136 throw new InvalidOperationException ("'Else' not allowed here");
139 currentBlock.Add (new CodeIf (condition));
141 nestedIfs [nestedIfs.Count-1] = 1 + (int)nestedIfs [nestedIfs.Count-1];
146 CodeBlock block = PopBlock ();
147 CodeIf cif = currentBlock.GetLastItem () as CodeIf;
149 if (cif == null || cif.TrueBlock != null)
150 throw new InvalidOperationException ("'Else' not allowed here");
152 cif.TrueBlock = block;
158 CodeBlock block = PopBlock ();
159 CodeIf cif = currentBlock.GetLastItem () as CodeIf;
161 if (cif == null || cif.FalseBlock != null || nestedIfs.Count == 0)
162 throw new InvalidOperationException ("'EndIf' not allowed here");
164 if (cif.TrueBlock == null)
165 cif.TrueBlock = block;
167 cif.FalseBlock = block;
169 int num = (int) nestedIfs [nestedIfs.Count-1];
171 nestedIfs [nestedIfs.Count-1] = --num;
175 nestedIfs.RemoveAt (nestedIfs.Count - 1);
179 public void Select ()
181 currentBlock.Add (new CodeSelect ());
185 public void Case (CodeExpression condition)
188 CodeSelect select = currentBlock.GetLastItem () as CodeSelect;
190 throw new InvalidOperationException ("'Case' not allowed here");
193 select.AddCase (condition, currentBlock);
196 public void EndSelect ()
199 CodeSelect select = currentBlock.GetLastItem () as CodeSelect;
201 throw new InvalidOperationException ("'EndSelect' not allowed here");
205 public void While (CodeExpression condition)
207 currentBlock.Add (new CodeWhile (condition));
211 public void EndWhile ()
213 CodeBlock block = PopBlock ();
214 CodeWhile cif = currentBlock.GetLastItem () as CodeWhile;
216 if (cif == null || cif.WhileBlock != null)
217 throw new InvalidOperationException ("'EndWhile' not allowed here");
219 cif.WhileBlock = block;
222 public void Foreach (Type type, out CodeExpression item, CodeExpression array)
224 CodeForeach cfe = new CodeForeach (array, type);
225 item = cfe.ItemExpression;
226 currentBlock.Add (cfe);
230 public void EndForeach ()
232 CodeBlock block = PopBlock ();
233 CodeForeach cif = currentBlock.GetLastItem () as CodeForeach;
235 if (cif == null || cif.ForBlock != null)
236 throw new InvalidOperationException ("'EndForeach' not allowed here");
238 cif.ForBlock = block;
241 public void For (CodeExpression initExp, CodeExpression conditionExp, CodeExpression nextExp)
243 currentBlock.Add (new CodeFor (initExp, conditionExp, nextExp));
247 public void EndFor ()
249 CodeBlock block = PopBlock ();
250 CodeFor cif = currentBlock.GetLastItem () as CodeFor;
252 if (cif == null || cif.ForBlock != null)
253 throw new InvalidOperationException ("'EndFor' not allowed here");
255 cif.ForBlock = block;
259 public void Call (CodeExpression target, string name, params CodeExpression[] parameters)
261 if ((object) target == null)
262 throw new ArgumentNullException ("target");
264 throw new ArgumentNullException ("name");
265 currentBlock.Add (new CodeMethodCall (target, name, parameters));
268 public void Call (CodeExpression target, MethodBase method, params CodeExpression[] parameters)
270 if ((object) target == null)
271 throw new ArgumentNullException ("target");
273 throw new ArgumentNullException ("method");
274 currentBlock.Add (new CodeMethodCall (target, method, parameters));
277 public void Call (CodeExpression target, CodeMethod method, params CodeExpression[] parameters)
279 if ((object) target == null)
280 throw new ArgumentNullException ("target");
282 throw new ArgumentNullException ("method");
283 currentBlock.Add (new CodeMethodCall (target, method, parameters));
286 public void Call (Type type, string name, params CodeExpression[] parameters)
289 throw new ArgumentNullException ("type");
291 throw new ArgumentNullException ("name");
292 currentBlock.Add (new CodeMethodCall (type, name, parameters));
295 public void Call (MethodInfo method, params CodeExpression[] parameters)
298 throw new ArgumentNullException ("method");
299 currentBlock.Add (new CodeMethodCall (method, parameters));
302 public void Call (CodeMethod method, params CodeExpression[] parameters)
304 if ((object) method == null)
305 throw new ArgumentNullException ("method");
306 currentBlock.Add (new CodeMethodCall (method, parameters));
309 public CodeExpression CallFunc (CodeExpression target, string name, params CodeExpression[] parameters)
311 if ((object) target == null)
312 throw new ArgumentNullException ("target");
314 throw new ArgumentNullException ("name");
315 return new CodeMethodCall (target, name, parameters);
318 public CodeExpression CallFunc (CodeExpression target, MethodInfo method, params CodeExpression[] parameters)
320 if ((object) target == null)
321 throw new ArgumentNullException ("target");
323 throw new ArgumentNullException ("method");
324 return new CodeMethodCall (target, method, parameters);
327 public CodeExpression CallFunc (CodeExpression target, CodeMethod method, params CodeExpression[] parameters)
329 if ((object) target == null)
330 throw new ArgumentNullException ("target");
332 throw new ArgumentNullException ("method");
333 return new CodeMethodCall (target, method, parameters);
336 public CodeExpression CallFunc (Type type, string name, params CodeExpression[] parameters)
339 throw new ArgumentNullException ("type");
341 throw new ArgumentNullException ("name");
342 return new CodeMethodCall (type, name, parameters);
345 public CodeExpression CallFunc (MethodInfo method, params CodeExpression[] parameters)
348 throw new ArgumentNullException ("method");
349 return new CodeMethodCall (method, parameters);
352 public CodeExpression CallFunc (CodeMethod method, params CodeExpression[] parameters)
354 if ((object) method == null)
355 throw new ArgumentNullException ("method");
356 return new CodeMethodCall (method, parameters);
359 public void Inc (CodeValueReference val)
361 Assign (val, new CodeIncrement (val));
364 public void Dec (CodeValueReference val)
366 Assign (val, new CodeDecrement (val));
369 public CodeExpression When (CodeExpression condition, CodeExpression trueResult, CodeExpression falseResult)
371 return new CodeWhen (condition, trueResult, falseResult);
374 public void ConsoleWriteLine (params CodeExpression[] parameters)
376 Call (typeof(Console), "WriteLine", parameters);
379 public void ConsoleWriteLine (params object[] parameters)
381 CodeExpression[] exps = new CodeExpression [parameters.Length];
382 for (int n=0; n<exps.Length; n++)
383 exps[n] = Exp.Literal (parameters[n]);
385 ConsoleWriteLine (exps);
388 public void Return (CodeExpression exp)
390 currentBlock.Add (new CodeReturn (this, exp));
393 public void Return ()
395 currentBlock.Add (new CodeReturn (this));
398 public static CodeBuilder operator+(CodeBuilder cb, CodeItem e)
400 cb.currentBlock.Add (e);
404 internal Label ReturnLabel
406 get { return returnLabel; }
407 set { returnLabel = value; }
412 blockStack.Push (currentBlock);
413 currentBlock = new CodeBlock ();
416 CodeBlock PopBlock ()
418 CodeBlock block = currentBlock;
419 currentBlock = (CodeBlock) blockStack.Pop ();