PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/DICK.B1/IronPython/Compiler/Ast/Parameter.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 159 lines | 109 code | 26 blank | 24 comment | 13 complexity | 3f18271b20a7e87412df373087f88935 MD5 | raw file
  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.Collections.Generic;
  16. using Microsoft.Scripting;
  17. using Microsoft.Scripting.Runtime;
  18. using IronPython.Runtime;
  19. using IronPython.Runtime.Binding;
  20. #if !CLR2
  21. using MSAst = System.Linq.Expressions;
  22. #else
  23. using MSAst = Microsoft.Scripting.Ast;
  24. #endif
  25. namespace IronPython.Compiler.Ast {
  26. using Ast = MSAst.Expression;
  27. public enum ParameterKind {
  28. Normal,
  29. List,
  30. Dictionary,
  31. };
  32. /// <summary>
  33. /// Parameter base class
  34. /// </summary>
  35. public class Parameter : Node {
  36. /// <summary>
  37. /// Position of the parameter: 0-based index
  38. /// </summary>
  39. private readonly string _name;
  40. protected readonly ParameterKind _kind;
  41. protected Expression _defaultValue;
  42. private PythonVariable _variable;
  43. private MSAst.ParameterExpression _parameter;
  44. public Parameter(string name)
  45. : this(name, ParameterKind.Normal) {
  46. }
  47. public Parameter(string name, ParameterKind kind) {
  48. _name = name;
  49. _kind = kind;
  50. }
  51. /// <summary>
  52. /// Parameter name
  53. /// </summary>
  54. public string Name {
  55. get { return _name; }
  56. }
  57. public Expression DefaultValue {
  58. get { return _defaultValue; }
  59. set { _defaultValue = value; }
  60. }
  61. public bool IsList {
  62. get {
  63. return _kind == ParameterKind.List;
  64. }
  65. }
  66. public bool IsDictionary {
  67. get {
  68. return _kind == ParameterKind.Dictionary;
  69. }
  70. }
  71. internal ParameterKind Kind {
  72. get {
  73. return _kind;
  74. }
  75. }
  76. internal PythonVariable PythonVariable {
  77. get { return _variable; }
  78. set { _variable = value; }
  79. }
  80. internal MSAst.Expression FinishBind(bool needsLocalsDictionary) {
  81. string name = Name;
  82. if (_variable.AccessedInNestedScope || needsLocalsDictionary) {
  83. _parameter = Expression.Parameter(typeof(object), Name);
  84. var cell = Ast.Parameter(typeof(ClosureCell), Name);
  85. return new ClosureExpression(_variable, cell, _parameter);
  86. } else {
  87. return _parameter = Ast.Parameter(typeof(object), Name);
  88. }
  89. }
  90. internal MSAst.ParameterExpression ParameterExpression {
  91. get {
  92. return _parameter;
  93. }
  94. }
  95. internal virtual void Init(List<MSAst.Expression> init) {
  96. // Regular parameter has no initialization
  97. }
  98. public override void Walk(PythonWalker walker) {
  99. if (walker.Walk(this)) {
  100. if (_defaultValue != null) {
  101. _defaultValue.Walk(walker);
  102. }
  103. }
  104. walker.PostWalk(this);
  105. }
  106. }
  107. public class SublistParameter : Parameter {
  108. private readonly TupleExpression _tuple;
  109. public SublistParameter(int position, TupleExpression tuple)
  110. : base("." + position, ParameterKind.Normal) {
  111. _tuple = tuple;
  112. }
  113. public TupleExpression Tuple {
  114. get { return _tuple; }
  115. }
  116. internal override void Init(List<MSAst.Expression> init) {
  117. init.Add(
  118. _tuple.TransformSet(Span, ParameterExpression, PythonOperationKind.None)
  119. );
  120. }
  121. public override void Walk(PythonWalker walker) {
  122. if (walker.Walk(this)) {
  123. if (_tuple != null) {
  124. _tuple.Walk(walker);
  125. }
  126. if (_defaultValue != null) {
  127. _defaultValue.Walk(walker);
  128. }
  129. }
  130. walker.PostWalk(this);
  131. }
  132. }
  133. }