選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

reference.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  13. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  14. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  15. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  16. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  17. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  19. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. "use strict";
  22. const READ = 0x1;
  23. const WRITE = 0x2;
  24. const RW = READ | WRITE;
  25. /**
  26. * A Reference represents a single occurrence of an identifier in code.
  27. * @class Reference
  28. */
  29. class Reference {
  30. constructor(ident, scope, flag, writeExpr, maybeImplicitGlobal, partial, init) {
  31. /**
  32. * Identifier syntax node.
  33. * @member {espreeIdentifier} Reference#identifier
  34. */
  35. this.identifier = ident;
  36. /**
  37. * Reference to the enclosing Scope.
  38. * @member {Scope} Reference#from
  39. */
  40. this.from = scope;
  41. /**
  42. * Whether the reference comes from a dynamic scope (such as 'eval',
  43. * 'with', etc.), and may be trapped by dynamic scopes.
  44. * @member {boolean} Reference#tainted
  45. */
  46. this.tainted = false;
  47. /**
  48. * The variable this reference is resolved with.
  49. * @member {Variable} Reference#resolved
  50. */
  51. this.resolved = null;
  52. /**
  53. * The read-write mode of the reference. (Value is one of {@link
  54. * Reference.READ}, {@link Reference.RW}, {@link Reference.WRITE}).
  55. * @member {number} Reference#flag
  56. * @private
  57. */
  58. this.flag = flag;
  59. if (this.isWrite()) {
  60. /**
  61. * If reference is writeable, this is the tree being written to it.
  62. * @member {espreeNode} Reference#writeExpr
  63. */
  64. this.writeExpr = writeExpr;
  65. /**
  66. * Whether the Reference might refer to a partial value of writeExpr.
  67. * @member {boolean} Reference#partial
  68. */
  69. this.partial = partial;
  70. /**
  71. * Whether the Reference is to write of initialization.
  72. * @member {boolean} Reference#init
  73. */
  74. this.init = init;
  75. }
  76. this.__maybeImplicitGlobal = maybeImplicitGlobal;
  77. }
  78. /**
  79. * Whether the reference is static.
  80. * @method Reference#isStatic
  81. * @returns {boolean} static
  82. */
  83. isStatic() {
  84. return !this.tainted && this.resolved && this.resolved.scope.isStatic();
  85. }
  86. /**
  87. * Whether the reference is writeable.
  88. * @method Reference#isWrite
  89. * @returns {boolean} write
  90. */
  91. isWrite() {
  92. return !!(this.flag & Reference.WRITE);
  93. }
  94. /**
  95. * Whether the reference is readable.
  96. * @method Reference#isRead
  97. * @returns {boolean} read
  98. */
  99. isRead() {
  100. return !!(this.flag & Reference.READ);
  101. }
  102. /**
  103. * Whether the reference is read-only.
  104. * @method Reference#isReadOnly
  105. * @returns {boolean} read only
  106. */
  107. isReadOnly() {
  108. return this.flag === Reference.READ;
  109. }
  110. /**
  111. * Whether the reference is write-only.
  112. * @method Reference#isWriteOnly
  113. * @returns {boolean} write only
  114. */
  115. isWriteOnly() {
  116. return this.flag === Reference.WRITE;
  117. }
  118. /**
  119. * Whether the reference is read-write.
  120. * @method Reference#isReadWrite
  121. * @returns {boolean} read write
  122. */
  123. isReadWrite() {
  124. return this.flag === Reference.RW;
  125. }
  126. }
  127. /**
  128. * @constant Reference.READ
  129. * @private
  130. */
  131. Reference.READ = READ;
  132. /**
  133. * @constant Reference.WRITE
  134. * @private
  135. */
  136. Reference.WRITE = WRITE;
  137. /**
  138. * @constant Reference.RW
  139. * @private
  140. */
  141. Reference.RW = RW;
  142. module.exports = Reference;
  143. /* vim: set sw=4 ts=4 et tw=80 : */