PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Tools/IronStudio/IronPythonToolsCore/PyAnalysis/Interpreter/InterpreterScope.cs

#
C# | 89 lines | 64 code | 12 blank | 13 comment | 7 complexity | e4d6dbeb5a6da7b13e39d48f91d9c26c 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 Apache License, Version 2.0. 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 Apache License, Version 2.0, please send an email to
  8. * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. * ***************************************************************************/
  14. using System.Collections.Generic;
  15. using IronPython.Compiler.Ast;
  16. using Microsoft.PyAnalysis.Values;
  17. namespace Microsoft.PyAnalysis.Interpreter {
  18. abstract class InterpreterScope {
  19. private readonly Namespace _ns;
  20. private Dictionary<string, VariableDef> _variables = new Dictionary<string, VariableDef>();
  21. public InterpreterScope(Namespace ns) {
  22. _ns = ns;
  23. }
  24. public abstract string Name {
  25. get;
  26. }
  27. public void SetVariable(Node node, AnalysisUnit unit, string name, IEnumerable<Namespace> value, bool addRef = true) {
  28. var variable = CreateVariable(node, unit, name, false);
  29. variable.AddTypes(node, unit, value);
  30. if (addRef) {
  31. variable.AddAssignment(node, unit);
  32. }
  33. }
  34. public VariableDef GetVariable(Node node, AnalysisUnit unit, string name, bool addRef = true) {
  35. VariableDef res;
  36. if (_variables.TryGetValue(name, out res)) {
  37. if (addRef) {
  38. res.AddReference(node, unit);
  39. }
  40. return res;
  41. }
  42. return null;
  43. }
  44. public VariableDef CreateVariable(Node node, AnalysisUnit unit, string name, bool addRef = true) {
  45. var res = GetVariable(node, unit, name, addRef);
  46. if (res == null) {
  47. _variables[name] = res = new VariableDef();
  48. if (addRef) {
  49. res.AddReference(node, unit);
  50. }
  51. }
  52. return res;
  53. }
  54. protected VariableDef CreateVariableWorker(Node node, AnalysisUnit unit, string name) {
  55. VariableDef res;
  56. if (!_variables.TryGetValue(name, out res)) {
  57. _variables[name] = res = new VariableDef();
  58. }
  59. return res;
  60. }
  61. public IDictionary<string, VariableDef> Variables {
  62. get {
  63. return _variables;
  64. }
  65. }
  66. public virtual bool VisibleToChildren {
  67. get {
  68. return true;
  69. }
  70. }
  71. public Namespace Namespace {
  72. get {
  73. return _ns;
  74. }
  75. }
  76. }
  77. }