PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_2_0/Src/Microsoft.Scripting.Core/Compiler/LocalVariableAccess.cs

#
C# | 101 lines | 50 code | 17 blank | 34 comment | 2 complexity | d60fee4a8cfd83c7fe95070205c352c2 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System; using Microsoft;
  16. using System.Collections.ObjectModel;
  17. using System.Diagnostics;
  18. using Microsoft.Linq.Expressions.Compiler;
  19. using System.Runtime.CompilerServices;
  20. using Microsoft.Runtime.CompilerServices;
  21. using Microsoft.Scripting.Utils;
  22. namespace Microsoft.Linq.Expressions.Compiler {
  23. /// <summary>
  24. /// Provides a list of local variables, supporing read/write of elements
  25. /// Exposed via LocalScopeExpression
  26. /// </summary>
  27. internal sealed class LocalVariableAccess : ILocalVariables {
  28. // The names of the variables
  29. private readonly ReadOnlyCollection<string> _names;
  30. // The top level environment. It contains pointers to parent
  31. // environments, which are always in the first element
  32. private readonly object[] _data;
  33. // An array of (int, int) pairs, each representing how to find a
  34. // variable in the environment data struction.
  35. //
  36. // The first integer indicates the number of times to go up in the
  37. // closure chain, the second integer indicates the index into that
  38. // closure chain.
  39. private readonly long[] _indexes;
  40. internal static readonly ILocalVariables Empty = new LocalVariableAccess(new object[0], new string[0], new long[0]);
  41. internal LocalVariableAccess(object[] data, string[] names, long[] indexes) {
  42. Assert.NotNull(names, data, indexes);
  43. Debug.Assert(names.Length == indexes.Length);
  44. _names = new ReadOnlyCollection<string>(names);
  45. _data = data;
  46. _indexes = indexes;
  47. }
  48. public int Count {
  49. get { return _indexes.Length; }
  50. }
  51. public ReadOnlyCollection<string> Names {
  52. get { return _names; }
  53. }
  54. public IStrongBox this[int index] {
  55. get {
  56. // We lookup the closure using two ints:
  57. // 1. The high dword is the number of parents to go up
  58. // 2. The low dword is the index into that array
  59. long closureKey = _indexes[index];
  60. // walk up the parent chain to find the real environment
  61. object[] result = _data;
  62. for (int parents = (int)(closureKey >> 32); parents > 0; parents--) {
  63. result = HoistedLocals.GetParent(result);
  64. }
  65. // Return the variable storage
  66. return (IStrongBox)result[(int)closureKey];
  67. }
  68. }
  69. }
  70. }
  71. namespace Microsoft.Runtime.CompilerServices {
  72. public static partial class RuntimeOps {
  73. // creates access for local variables in scope
  74. [Obsolete("used by generated code", true)]
  75. public static ILocalVariables CreateVariableAccess(object[] data, string[] names, long[] indexes) {
  76. return new LocalVariableAccess(data, names, indexes);
  77. }
  78. // creates access when there are no variables in scope
  79. [Obsolete("used by generated code", true)]
  80. public static ILocalVariables CreateEmptyVariableAccess() {
  81. return LocalVariableAccess.Empty;
  82. }
  83. }
  84. }