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;
28 using System.Reflection.Emit;
30 namespace Mono.CodeGeneration
32 public class CodeForeach: CodeStatement
38 CodeVariableDeclaration itemDec;
40 public CodeForeach (CodeExpression array, Type itemType)
43 this.itemType = itemType;
45 Type t = array.GetResultType ();
46 if (!t.IsArray && t.GetMethod ("GetEnumerator", Type.EmptyTypes) == null)
47 throw new InvalidOperationException ("foreach statement cannot operate on variables of type `" + t + "' because that class does not provide a GetEnumerator method or it is inaccessible");
49 itemDec = new CodeVariableDeclaration (itemType, "item");
52 public CodeValueReference ItemExpression
54 get { return itemDec.Variable; }
57 public CodeBlock ForBlock
59 get { return forBlock; }
60 set { forBlock = value; }
63 public override void Generate (ILGenerator gen)
65 Type t = array.GetResultType ();
68 CodeBlock block = new CodeBlock();
69 CodeVariableDeclaration indexDec;
71 CodeValueReference index;
72 CodeValueReference item;
75 item = itemDec.Variable;
76 block.Add (indexDec = new CodeVariableDeclaration (typeof(int), "n"));
77 index = indexDec.Variable;
78 block.Add (new CodeAssignment (index, new CodeLiteral (0)));
80 block.Add (cw = new CodeWhile (CodeExpression.IsSmallerThan (index, array.ArrayLength)));
81 CodeBlock loopBlock = new CodeBlock ();
82 loopBlock.Add (new CodeAssignment (item, array[index]));
83 loopBlock.Add (new CodeIncrement(index));
84 loopBlock.Add (forBlock);
85 cw.WhileBlock = loopBlock;
91 CodeBlock block = new CodeBlock();
92 CodeVariableDeclaration dec;
94 CodeValueReference enumerator;
95 CodeValueReference item;
98 item = itemDec.Variable;
99 block.Add (dec = new CodeVariableDeclaration (typeof(IEnumerator), "e"));
100 enumerator = dec.Variable;
101 block.Add (new CodeAssignment (enumerator, array.Call("GetEnumerator")));
103 block.Add (cw = new CodeWhile (enumerator.Call ("MoveNext")));
104 CodeBlock loopBlock = new CodeBlock ();
105 loopBlock.Add (new CodeAssignment (item, enumerator["Current"]));
106 loopBlock.Add (forBlock);
107 cw.WhileBlock = loopBlock;
109 block.Generate (gen);
113 public override void PrintCode (CodeWriter cp)
115 cp.Write ("foreach (" + itemType + " item in ");
116 array.PrintCode (cp);
120 forBlock.PrintCode (cp);
122 cp.BeginLine ().Write ("}");