/js/src/frontend/ParseNode-inl.h

http://github.com/zpao/v8monkey · C Header · 205 lines · 151 code · 16 blank · 38 comment · 12 complexity · 6d8904363c0a40cdde4f3c5a8a6b017c MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sw=4 et tw=99:
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is Mozilla Communicator client code, released
  17. * March 31, 1998.
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Netscape Communications Corporation.
  21. * Portions created by the Initial Developer are Copyright (C) 1998-2011
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either the GNU General Public License Version 2 or later (the "GPL"), or
  28. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  39. #ifndef ParseNode_inl_h__
  40. #define ParseNode_inl_h__
  41. #include "frontend/ParseNode.h"
  42. #include "frontend/BytecodeEmitter.h"
  43. #include "frontend/TokenStream.h"
  44. namespace js {
  45. inline bool
  46. ParseNode::isConstant()
  47. {
  48. switch (pn_type) {
  49. case PNK_NUMBER:
  50. case PNK_STRING:
  51. case PNK_NULL:
  52. case PNK_FALSE:
  53. case PNK_TRUE:
  54. return true;
  55. case PNK_RB:
  56. case PNK_RC:
  57. return isOp(JSOP_NEWINIT) && !(pn_xflags & PNX_NONCONST);
  58. default:
  59. return false;
  60. }
  61. }
  62. #ifdef DEBUG
  63. inline void
  64. IndentNewLine(int indent)
  65. {
  66. fputc('\n', stderr);
  67. for (int i = 0; i < indent; ++i)
  68. fputc(' ', stderr);
  69. }
  70. inline void
  71. ParseNode::dump(int indent)
  72. {
  73. switch (pn_arity) {
  74. case PN_NULLARY:
  75. ((NullaryNode *) this)->dump();
  76. break;
  77. case PN_UNARY:
  78. ((UnaryNode *) this)->dump(indent);
  79. break;
  80. case PN_BINARY:
  81. ((BinaryNode *) this)->dump(indent);
  82. break;
  83. case PN_TERNARY:
  84. ((TernaryNode *) this)->dump(indent);
  85. break;
  86. case PN_FUNC:
  87. ((FunctionNode *) this)->dump(indent);
  88. break;
  89. case PN_LIST:
  90. ((ListNode *) this)->dump(indent);
  91. break;
  92. case PN_NAME:
  93. ((NameNode *) this)->dump(indent);
  94. break;
  95. default:
  96. fprintf(stderr, "?");
  97. break;
  98. }
  99. }
  100. inline void
  101. NullaryNode::dump()
  102. {
  103. fprintf(stderr, "(%s)", js_CodeName[getOp()]);
  104. }
  105. inline void
  106. UnaryNode::dump(int indent)
  107. {
  108. const char *name = js_CodeName[getOp()];
  109. fprintf(stderr, "(%s ", name);
  110. indent += strlen(name) + 2;
  111. DumpParseTree(pn_kid, indent);
  112. fprintf(stderr, ")");
  113. }
  114. inline void
  115. BinaryNode::dump(int indent)
  116. {
  117. const char *name = js_CodeName[getOp()];
  118. fprintf(stderr, "(%s ", name);
  119. indent += strlen(name) + 2;
  120. DumpParseTree(pn_left, indent);
  121. IndentNewLine(indent);
  122. DumpParseTree(pn_right, indent);
  123. fprintf(stderr, ")");
  124. }
  125. inline void
  126. TernaryNode::dump(int indent)
  127. {
  128. const char *name = js_CodeName[getOp()];
  129. fprintf(stderr, "(%s ", name);
  130. indent += strlen(name) + 2;
  131. DumpParseTree(pn_kid1, indent);
  132. IndentNewLine(indent);
  133. DumpParseTree(pn_kid2, indent);
  134. IndentNewLine(indent);
  135. DumpParseTree(pn_kid3, indent);
  136. fprintf(stderr, ")");
  137. }
  138. inline void
  139. FunctionNode::dump(int indent)
  140. {
  141. const char *name = js_CodeName[getOp()];
  142. fprintf(stderr, "(%s ", name);
  143. indent += strlen(name) + 2;
  144. DumpParseTree(pn_body, indent);
  145. fprintf(stderr, ")");
  146. }
  147. inline void
  148. ListNode::dump(int indent)
  149. {
  150. const char *name = js_CodeName[getOp()];
  151. fprintf(stderr, "(%s ", name);
  152. if (pn_head != NULL) {
  153. indent += strlen(name) + 2;
  154. DumpParseTree(pn_head, indent);
  155. ParseNode *pn = pn_head->pn_next;
  156. while (pn != NULL) {
  157. IndentNewLine(indent);
  158. DumpParseTree(pn, indent);
  159. pn = pn->pn_next;
  160. }
  161. }
  162. fprintf(stderr, ")");
  163. }
  164. inline void
  165. NameNode::dump(int indent)
  166. {
  167. const char *name = js_CodeName[getOp()];
  168. if (isUsed())
  169. fprintf(stderr, "(%s)", name);
  170. else {
  171. fprintf(stderr, "(%s ", name);
  172. indent += strlen(name) + 2;
  173. DumpParseTree(expr(), indent);
  174. fprintf(stderr, ")");
  175. }
  176. }
  177. #endif
  178. inline void
  179. NameNode::initCommon(TreeContext *tc)
  180. {
  181. pn_expr = NULL;
  182. pn_cookie.makeFree();
  183. pn_dflags = (!tc->topStmt || tc->topStmt->type == STMT_BLOCK)
  184. ? PND_BLOCKCHILD
  185. : 0;
  186. pn_blockid = tc->blockid();
  187. }
  188. } /* namespace js */
  189. #endif /* ParseNode_inl_h__ */