CodeForeach.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. using System;
  24. using System.Collections;
  25. using System.Reflection;
  26. using System.Reflection.Emit;
  27. namespace Mono.CodeGeneration
  28. {
  29. public class CodeForeach: CodeStatement
  30. {
  31. Type itemType;
  32. CodeExpression array;
  33. CodeBlock forBlock;
  34. CodeVariableDeclaration itemDec;
  35. public CodeForeach (CodeExpression array, Type itemType)
  36. {
  37. this.array = array;
  38. this.itemType = itemType;
  39. Type t = array.GetResultType ();
  40. if (!t.IsArray && t.GetMethod ("GetEnumerator", Type.EmptyTypes) == null)
  41. 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");
  42. itemDec = new CodeVariableDeclaration (itemType, "item");
  43. }
  44. public CodeValueReference ItemExpression
  45. {
  46. get { return itemDec.Variable; }
  47. }
  48. public CodeBlock ForBlock
  49. {
  50. get { return forBlock; }
  51. set { forBlock = value; }
  52. }
  53. public override void Generate (ILGenerator gen)
  54. {
  55. Type t = array.GetResultType ();
  56. if (t.IsArray)
  57. {
  58. CodeBlock block = new CodeBlock();
  59. CodeVariableDeclaration indexDec;
  60. CodeWhile cw;
  61. CodeValueReference index;
  62. CodeValueReference item;
  63. block.Add (itemDec);
  64. item = itemDec.Variable;
  65. block.Add (indexDec = new CodeVariableDeclaration (typeof(int), "n"));
  66. index = indexDec.Variable;
  67. block.Add (new CodeAssignment (index, new CodeLiteral (0)));
  68. block.Add (cw = new CodeWhile (CodeExpression.IsSmallerThan (index, array.ArrayLength)));
  69. CodeBlock loopBlock = new CodeBlock ();
  70. loopBlock.Add (new CodeAssignment (item, array[index]));
  71. loopBlock.Add (new CodeIncrement(index));
  72. loopBlock.Add (forBlock);
  73. cw.WhileBlock = loopBlock;
  74. block.Generate (gen);
  75. }
  76. else
  77. {
  78. CodeBlock block = new CodeBlock();
  79. CodeVariableDeclaration dec;
  80. CodeWhile cw;
  81. CodeValueReference enumerator;
  82. CodeValueReference item;
  83. block.Add (itemDec);
  84. item = itemDec.Variable;
  85. block.Add (dec = new CodeVariableDeclaration (typeof(IEnumerator), "e"));
  86. enumerator = dec.Variable;
  87. block.Add (new CodeAssignment (enumerator, array.Call("GetEnumerator")));
  88. block.Add (cw = new CodeWhile (enumerator.Call ("MoveNext")));
  89. CodeBlock loopBlock = new CodeBlock ();
  90. loopBlock.Add (new CodeAssignment (item, enumerator["Current"]));
  91. loopBlock.Add (forBlock);
  92. cw.WhileBlock = loopBlock;
  93. block.Generate (gen);
  94. }
  95. }
  96. public override void PrintCode (CodeWriter cp)
  97. {
  98. cp.Write ("foreach (" + itemType + " item in ");
  99. array.PrintCode (cp);
  100. cp.Write (") {");
  101. cp.EndLine ();
  102. cp.Indent ();
  103. forBlock.PrintCode (cp);
  104. cp.Unindent ();
  105. cp.BeginLine ().Write ("}");
  106. }
  107. }
  108. }