CodeForeach.cs 3.8 KB

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