Merge pull request #656 from LogosBible/collection_lock
[mono.git] / mcs / class / System.ServiceModel / Mono.CodeGeneration / CodeForeach.cs
1 //
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:
9 // 
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
12 // 
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.
20 //
21 // Copyright (C) Lluis Sanchez Gual, 2004
22 //
23
24 #if !FULL_AOT_RUNTIME
25 using System;
26 using System.Collections;
27 using System.Reflection;
28 using System.Reflection.Emit;
29
30 namespace Mono.CodeGeneration
31 {
32         public class CodeForeach: CodeStatement
33         {
34                 Type itemType;
35                 CodeExpression array;
36                 CodeBlock forBlock;
37                 
38                 CodeVariableDeclaration itemDec;
39                 
40                 public CodeForeach (CodeExpression array, Type itemType)
41                 {
42                         this.array = array;
43                         this.itemType = itemType;
44                         
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");
48                         
49                         itemDec = new CodeVariableDeclaration (itemType, "item");
50                 }
51                 
52                 public CodeValueReference ItemExpression
53                 {
54                         get { return itemDec.Variable; }
55                 }
56                 
57                 public CodeBlock ForBlock
58                 {
59                         get { return forBlock; }
60                         set { forBlock = value; }
61                 }
62                 
63                 public override void Generate (ILGenerator gen)
64                 {
65                         Type t = array.GetResultType ();
66                         if (t.IsArray)
67                         {
68                                 CodeBlock block = new CodeBlock();
69                                 CodeVariableDeclaration indexDec;
70                                 CodeWhile cw;
71                                 CodeValueReference index;
72                                 CodeValueReference item;
73                                 
74                                 block.Add (itemDec);
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)));
79                                 
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;
86                                 
87                                 block.Generate (gen);
88                         }
89                         else
90                         {
91                                 CodeBlock block = new CodeBlock();
92                                 CodeVariableDeclaration dec;
93                                 CodeWhile cw;
94                                 CodeValueReference enumerator;
95                                 CodeValueReference item;
96                                 
97                                 block.Add (itemDec);
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")));
102                                 
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;
108                                 
109                                 block.Generate (gen);
110                         }
111                 }
112                 
113                 public override void PrintCode (CodeWriter cp)
114                 {
115                         cp.Write ("foreach (" + itemType + " item in ");
116                         array.PrintCode (cp);
117                         cp.Write (") {");
118                         cp.EndLine ();
119                         cp.Indent ();
120                         forBlock.PrintCode (cp);
121                         cp.Unindent ();
122                         cp.BeginLine ().Write ("}");
123                 }
124         }
125 }
126 #endif