PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Tools/IronStudio/IronPythonToolsCore/PyAnalysis/Values/ListInfo.cs

http://github.com/IronLanguages/main
C# | 100 lines | 71 code | 13 blank | 16 comment | 13 complexity | 39833242b050ec7989a16fb94d60d58d MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  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;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using IronPython.Compiler.Ast;
  19. using Microsoft.PyAnalysis.Interpreter;
  20. namespace Microsoft.PyAnalysis.Values {
  21. /// <summary>
  22. /// Represents a list object with tracked type information.
  23. /// </summary>
  24. class ListInfo : SequenceInfo {
  25. private ListAppendBoundBuiltinMethodInfo _appendMethod;
  26. private ListPopBoundBuiltinMethodInfo _popMethod;
  27. private ListInsertBoundBuiltinMethodInfo _insertMethod;
  28. private ListExtendBoundBuiltinMethodInfo _extendMethod;
  29. public ListInfo(ISet<Namespace>[] indexTypes, BuiltinClassInfo seqType)
  30. : base(indexTypes, seqType) {
  31. EnsureAppend();
  32. }
  33. public override ISet<Namespace> GetMember(Node node, AnalysisUnit unit, string name) {
  34. switch (name) {
  35. case "append":
  36. EnsureAppend();
  37. return _appendMethod.SelfSet;
  38. case "pop":
  39. EnsurePop();
  40. return _popMethod.SelfSet;
  41. case "insert":
  42. EnsureInsert();
  43. return _insertMethod.SelfSet;
  44. case "extend":
  45. EnsureExtend();
  46. return _extendMethod.SelfSet;
  47. }
  48. return base.GetMember(node, unit, name);
  49. }
  50. internal void AppendItem(ISet<Namespace> set) {
  51. ISet<Namespace> newTypes = set;
  52. bool madeSet = false;
  53. foreach (var type in IndexTypes) {
  54. newTypes = newTypes.Union(type, ref madeSet);
  55. }
  56. if (IndexTypes.Length != 1 || IndexTypes[0].Count != newTypes.Count) {
  57. ReturnValue.EnqueueDependents();
  58. }
  59. UnionType = newTypes;
  60. IndexTypes = new[] { newTypes };
  61. }
  62. private void EnsureAppend() {
  63. if (_appendMethod == null) {
  64. var appendMeth = VariableDict["append"];
  65. _appendMethod = new ListAppendBoundBuiltinMethodInfo(this, (BuiltinMethodInfo)appendMeth.First());
  66. }
  67. }
  68. private void EnsurePop() {
  69. if (_popMethod == null) {
  70. var popMethod = VariableDict["pop"];
  71. _popMethod = new ListPopBoundBuiltinMethodInfo(this, (BuiltinMethodInfo)popMethod.First());
  72. }
  73. }
  74. private void EnsureInsert() {
  75. if (_insertMethod == null) {
  76. var method = VariableDict["insert"];
  77. _insertMethod = new ListInsertBoundBuiltinMethodInfo(this, (BuiltinMethodInfo)method.First());
  78. }
  79. }
  80. private void EnsureExtend() {
  81. if (_extendMethod == null) {
  82. var method = VariableDict["extend"];
  83. _extendMethod = new ListExtendBoundBuiltinMethodInfo(this, (BuiltinMethodInfo)method.First());
  84. }
  85. }
  86. }
  87. }