Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef frontend_ParseNode_h |
michael@0 | 8 | #define frontend_ParseNode_h |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/Attributes.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "frontend/TokenStream.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace js { |
michael@0 | 15 | namespace frontend { |
michael@0 | 16 | |
michael@0 | 17 | template <typename ParseHandler> |
michael@0 | 18 | struct ParseContext; |
michael@0 | 19 | |
michael@0 | 20 | class FullParseHandler; |
michael@0 | 21 | class FunctionBox; |
michael@0 | 22 | class ObjectBox; |
michael@0 | 23 | |
michael@0 | 24 | /* |
michael@0 | 25 | * Indicates a location in the stack that an upvar value can be retrieved from |
michael@0 | 26 | * as a two tuple of (level, slot). |
michael@0 | 27 | * |
michael@0 | 28 | * Some existing client code uses the level value as a delta, or level "skip" |
michael@0 | 29 | * quantity. We could probably document that through use of more types at some |
michael@0 | 30 | * point in the future. |
michael@0 | 31 | */ |
michael@0 | 32 | class UpvarCookie |
michael@0 | 33 | { |
michael@0 | 34 | uint32_t level_ : SCOPECOORD_HOPS_BITS; |
michael@0 | 35 | uint32_t slot_ : SCOPECOORD_SLOT_BITS; |
michael@0 | 36 | |
michael@0 | 37 | void checkInvariants() { |
michael@0 | 38 | static_assert(sizeof(UpvarCookie) == sizeof(uint32_t), |
michael@0 | 39 | "Not necessary for correctness, but good for ParseNode memory use"); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | public: |
michael@0 | 43 | // Steal one value to represent the sentinel value for UpvarCookie. |
michael@0 | 44 | static const uint32_t FREE_LEVEL = SCOPECOORD_HOPS_LIMIT - 1; |
michael@0 | 45 | bool isFree() const { return level_ == FREE_LEVEL; } |
michael@0 | 46 | |
michael@0 | 47 | uint32_t level() const { JS_ASSERT(!isFree()); return level_; } |
michael@0 | 48 | uint32_t slot() const { JS_ASSERT(!isFree()); return slot_; } |
michael@0 | 49 | |
michael@0 | 50 | // This fails and issues an error message if newLevel or newSlot are too large. |
michael@0 | 51 | bool set(TokenStream &ts, unsigned newLevel, uint32_t newSlot) { |
michael@0 | 52 | if (newLevel >= FREE_LEVEL) |
michael@0 | 53 | return ts.reportError(JSMSG_TOO_DEEP, js_function_str); |
michael@0 | 54 | |
michael@0 | 55 | if (newSlot >= SCOPECOORD_SLOT_LIMIT) |
michael@0 | 56 | return ts.reportError(JSMSG_TOO_MANY_LOCALS); |
michael@0 | 57 | |
michael@0 | 58 | level_ = newLevel; |
michael@0 | 59 | slot_ = newSlot; |
michael@0 | 60 | return true; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | void makeFree() { |
michael@0 | 64 | level_ = FREE_LEVEL; |
michael@0 | 65 | slot_ = 0; // value doesn't matter, won't be used |
michael@0 | 66 | JS_ASSERT(isFree()); |
michael@0 | 67 | } |
michael@0 | 68 | }; |
michael@0 | 69 | |
michael@0 | 70 | #define FOR_EACH_PARSE_NODE_KIND(F) \ |
michael@0 | 71 | F(NOP) \ |
michael@0 | 72 | F(SEMI) \ |
michael@0 | 73 | F(COMMA) \ |
michael@0 | 74 | F(CONDITIONAL) \ |
michael@0 | 75 | F(COLON) \ |
michael@0 | 76 | F(POS) \ |
michael@0 | 77 | F(NEG) \ |
michael@0 | 78 | F(PREINCREMENT) \ |
michael@0 | 79 | F(POSTINCREMENT) \ |
michael@0 | 80 | F(PREDECREMENT) \ |
michael@0 | 81 | F(POSTDECREMENT) \ |
michael@0 | 82 | F(DOT) \ |
michael@0 | 83 | F(ELEM) \ |
michael@0 | 84 | F(ARRAY) \ |
michael@0 | 85 | F(ELISION) \ |
michael@0 | 86 | F(STATEMENTLIST) \ |
michael@0 | 87 | F(LABEL) \ |
michael@0 | 88 | F(OBJECT) \ |
michael@0 | 89 | F(CALL) \ |
michael@0 | 90 | F(NAME) \ |
michael@0 | 91 | F(NUMBER) \ |
michael@0 | 92 | F(STRING) \ |
michael@0 | 93 | F(REGEXP) \ |
michael@0 | 94 | F(TRUE) \ |
michael@0 | 95 | F(FALSE) \ |
michael@0 | 96 | F(NULL) \ |
michael@0 | 97 | F(THIS) \ |
michael@0 | 98 | F(FUNCTION) \ |
michael@0 | 99 | F(IF) \ |
michael@0 | 100 | F(ELSE) \ |
michael@0 | 101 | F(SWITCH) \ |
michael@0 | 102 | F(CASE) \ |
michael@0 | 103 | F(DEFAULT) \ |
michael@0 | 104 | F(WHILE) \ |
michael@0 | 105 | F(DOWHILE) \ |
michael@0 | 106 | F(FOR) \ |
michael@0 | 107 | F(BREAK) \ |
michael@0 | 108 | F(CONTINUE) \ |
michael@0 | 109 | F(VAR) \ |
michael@0 | 110 | F(CONST) \ |
michael@0 | 111 | F(WITH) \ |
michael@0 | 112 | F(RETURN) \ |
michael@0 | 113 | F(NEW) \ |
michael@0 | 114 | F(DELETE) \ |
michael@0 | 115 | F(TRY) \ |
michael@0 | 116 | F(CATCH) \ |
michael@0 | 117 | F(CATCHLIST) \ |
michael@0 | 118 | F(FINALLY) \ |
michael@0 | 119 | F(THROW) \ |
michael@0 | 120 | F(DEBUGGER) \ |
michael@0 | 121 | F(YIELD) \ |
michael@0 | 122 | F(YIELD_STAR) \ |
michael@0 | 123 | F(GENEXP) \ |
michael@0 | 124 | F(ARRAYCOMP) \ |
michael@0 | 125 | F(ARRAYPUSH) \ |
michael@0 | 126 | F(LEXICALSCOPE) \ |
michael@0 | 127 | F(LET) \ |
michael@0 | 128 | F(IMPORT) \ |
michael@0 | 129 | F(IMPORT_SPEC_LIST) \ |
michael@0 | 130 | F(IMPORT_SPEC) \ |
michael@0 | 131 | F(EXPORT) \ |
michael@0 | 132 | F(EXPORT_FROM) \ |
michael@0 | 133 | F(EXPORT_SPEC_LIST) \ |
michael@0 | 134 | F(EXPORT_SPEC) \ |
michael@0 | 135 | F(EXPORT_BATCH_SPEC) \ |
michael@0 | 136 | F(SEQ) \ |
michael@0 | 137 | F(FORIN) \ |
michael@0 | 138 | F(FOROF) \ |
michael@0 | 139 | F(FORHEAD) \ |
michael@0 | 140 | F(ARGSBODY) \ |
michael@0 | 141 | F(SPREAD) \ |
michael@0 | 142 | \ |
michael@0 | 143 | /* Unary operators. */ \ |
michael@0 | 144 | F(TYPEOF) \ |
michael@0 | 145 | F(VOID) \ |
michael@0 | 146 | F(NOT) \ |
michael@0 | 147 | F(BITNOT) \ |
michael@0 | 148 | \ |
michael@0 | 149 | /* \ |
michael@0 | 150 | * Binary operators. \ |
michael@0 | 151 | * These must be in the same order as TOK_OR and friends in TokenStream.h. \ |
michael@0 | 152 | */ \ |
michael@0 | 153 | F(OR) \ |
michael@0 | 154 | F(AND) \ |
michael@0 | 155 | F(BITOR) \ |
michael@0 | 156 | F(BITXOR) \ |
michael@0 | 157 | F(BITAND) \ |
michael@0 | 158 | F(STRICTEQ) \ |
michael@0 | 159 | F(EQ) \ |
michael@0 | 160 | F(STRICTNE) \ |
michael@0 | 161 | F(NE) \ |
michael@0 | 162 | F(LT) \ |
michael@0 | 163 | F(LE) \ |
michael@0 | 164 | F(GT) \ |
michael@0 | 165 | F(GE) \ |
michael@0 | 166 | F(INSTANCEOF) \ |
michael@0 | 167 | F(IN) \ |
michael@0 | 168 | F(LSH) \ |
michael@0 | 169 | F(RSH) \ |
michael@0 | 170 | F(URSH) \ |
michael@0 | 171 | F(ADD) \ |
michael@0 | 172 | F(SUB) \ |
michael@0 | 173 | F(STAR) \ |
michael@0 | 174 | F(DIV) \ |
michael@0 | 175 | F(MOD) \ |
michael@0 | 176 | \ |
michael@0 | 177 | /* Assignment operators (= += -= etc.). */ \ |
michael@0 | 178 | /* ParseNode::isAssignment assumes all these are consecutive. */ \ |
michael@0 | 179 | F(ASSIGN) \ |
michael@0 | 180 | F(ADDASSIGN) \ |
michael@0 | 181 | F(SUBASSIGN) \ |
michael@0 | 182 | F(BITORASSIGN) \ |
michael@0 | 183 | F(BITXORASSIGN) \ |
michael@0 | 184 | F(BITANDASSIGN) \ |
michael@0 | 185 | F(LSHASSIGN) \ |
michael@0 | 186 | F(RSHASSIGN) \ |
michael@0 | 187 | F(URSHASSIGN) \ |
michael@0 | 188 | F(MULASSIGN) \ |
michael@0 | 189 | F(DIVASSIGN) \ |
michael@0 | 190 | F(MODASSIGN) |
michael@0 | 191 | |
michael@0 | 192 | /* |
michael@0 | 193 | * Parsing builds a tree of nodes that directs code generation. This tree is |
michael@0 | 194 | * not a concrete syntax tree in all respects (for example, || and && are left |
michael@0 | 195 | * associative, but (A && B && C) translates into the right-associated tree |
michael@0 | 196 | * <A && <B && C>> so that code generation can emit a left-associative branch |
michael@0 | 197 | * around <B && C> when A is false). Nodes are labeled by kind, with a |
michael@0 | 198 | * secondary JSOp label when needed. |
michael@0 | 199 | * |
michael@0 | 200 | * The long comment after this enum block describes the kinds in detail. |
michael@0 | 201 | */ |
michael@0 | 202 | enum ParseNodeKind |
michael@0 | 203 | { |
michael@0 | 204 | #define EMIT_ENUM(name) PNK_##name, |
michael@0 | 205 | FOR_EACH_PARSE_NODE_KIND(EMIT_ENUM) |
michael@0 | 206 | #undef EMIT_ENUM |
michael@0 | 207 | PNK_LIMIT, /* domain size */ |
michael@0 | 208 | PNK_BINOP_FIRST = PNK_OR, |
michael@0 | 209 | PNK_BINOP_LAST = PNK_MOD, |
michael@0 | 210 | PNK_ASSIGNMENT_START = PNK_ASSIGN, |
michael@0 | 211 | PNK_ASSIGNMENT_LAST = PNK_MODASSIGN |
michael@0 | 212 | }; |
michael@0 | 213 | |
michael@0 | 214 | /* |
michael@0 | 215 | * Label Variant Members |
michael@0 | 216 | * ----- ------- ------- |
michael@0 | 217 | * <Definitions> |
michael@0 | 218 | * PNK_FUNCTION name pn_funbox: ptr to js::FunctionBox holding function |
michael@0 | 219 | * object containing arg and var properties. We |
michael@0 | 220 | * create the function object at parse (not emit) |
michael@0 | 221 | * time to specialize arg and var bytecodes early. |
michael@0 | 222 | * pn_body: PNK_ARGSBODY, ordinarily; |
michael@0 | 223 | * PNK_LEXICALSCOPE for implicit function in genexpr |
michael@0 | 224 | * pn_cookie: static level and var index for function |
michael@0 | 225 | * pn_dflags: PND_* definition/use flags (see below) |
michael@0 | 226 | * pn_blockid: block id number |
michael@0 | 227 | * PNK_ARGSBODY list list of formal parameters followed by: |
michael@0 | 228 | * PNK_STATEMENTLIST node for function body |
michael@0 | 229 | * statements, |
michael@0 | 230 | * PNK_RETURN for expression closure, or |
michael@0 | 231 | * PNK_SEQ for expression closure with |
michael@0 | 232 | * destructured formal parameters |
michael@0 | 233 | * pn_count: 1 + number of formal parameters |
michael@0 | 234 | * pn_tree: PNK_ARGSBODY or PNK_STATEMENTLIST node |
michael@0 | 235 | * PNK_SPREAD unary pn_kid: expression being spread |
michael@0 | 236 | * |
michael@0 | 237 | * <Statements> |
michael@0 | 238 | * PNK_STATEMENTLIST list pn_head: list of pn_count statements |
michael@0 | 239 | * PNK_IF ternary pn_kid1: cond, pn_kid2: then, pn_kid3: else or null. |
michael@0 | 240 | * In body of a comprehension or desugared generator |
michael@0 | 241 | * expression, pn_kid2 is PNK_YIELD, PNK_ARRAYPUSH, |
michael@0 | 242 | * or (if the push was optimized away) empty |
michael@0 | 243 | * PNK_STATEMENTLIST. |
michael@0 | 244 | * PNK_SWITCH binary pn_left: discriminant |
michael@0 | 245 | * pn_right: list of PNK_CASE nodes, with at most one |
michael@0 | 246 | * PNK_DEFAULT node, or if there are let bindings |
michael@0 | 247 | * in the top level of the switch body's cases, a |
michael@0 | 248 | * PNK_LEXICALSCOPE node that contains the list of |
michael@0 | 249 | * PNK_CASE nodes. |
michael@0 | 250 | * PNK_CASE, binary pn_left: case expr |
michael@0 | 251 | * pn_right: PNK_STATEMENTLIST node for this case's |
michael@0 | 252 | * statements |
michael@0 | 253 | * PNK_DEFAULT binary pn_left: null |
michael@0 | 254 | * pn_right: PNK_STATEMENTLIST node for this default's |
michael@0 | 255 | * statements |
michael@0 | 256 | * pn_val: constant value if lookup or table switch |
michael@0 | 257 | * PNK_WHILE binary pn_left: cond, pn_right: body |
michael@0 | 258 | * PNK_DOWHILE binary pn_left: body, pn_right: cond |
michael@0 | 259 | * PNK_FOR binary pn_left: either PNK_FORIN (for-in statement), |
michael@0 | 260 | * PNK_FOROF (for-of) or PNK_FORHEAD (for(;;)) |
michael@0 | 261 | * pn_right: body |
michael@0 | 262 | * PNK_FORIN ternary pn_kid1: PNK_VAR to left of 'in', or nullptr |
michael@0 | 263 | * its pn_xflags may have PNX_POPVAR |
michael@0 | 264 | * bit set |
michael@0 | 265 | * pn_kid2: PNK_NAME or destructuring expr |
michael@0 | 266 | * to left of 'in'; if pn_kid1, then this |
michael@0 | 267 | * is a clone of pn_kid1->pn_head |
michael@0 | 268 | * pn_kid3: object expr to right of 'in' |
michael@0 | 269 | * PNK_FOROF ternary pn_kid1: PNK_VAR to left of 'of', or nullptr |
michael@0 | 270 | * its pn_xflags may have PNX_POPVAR |
michael@0 | 271 | * bit set |
michael@0 | 272 | * pn_kid2: PNK_NAME or destructuring expr |
michael@0 | 273 | * to left of 'of'; if pn_kid1, then this |
michael@0 | 274 | * is a clone of pn_kid1->pn_head |
michael@0 | 275 | * pn_kid3: expr to right of 'of' |
michael@0 | 276 | * PNK_FORHEAD ternary pn_kid1: init expr before first ';' or nullptr |
michael@0 | 277 | * pn_kid2: cond expr before second ';' or nullptr |
michael@0 | 278 | * pn_kid3: update expr after second ';' or nullptr |
michael@0 | 279 | * PNK_THROW unary pn_op: JSOP_THROW, pn_kid: exception |
michael@0 | 280 | * PNK_TRY ternary pn_kid1: try block |
michael@0 | 281 | * pn_kid2: null or PNK_CATCHLIST list of |
michael@0 | 282 | * PNK_LEXICALSCOPE nodes, each with pn_expr pointing |
michael@0 | 283 | * to a PNK_CATCH node |
michael@0 | 284 | * pn_kid3: null or finally block |
michael@0 | 285 | * PNK_CATCH ternary pn_kid1: PNK_NAME, PNK_ARRAY, or PNK_OBJECT catch var node |
michael@0 | 286 | * (PNK_ARRAY or PNK_OBJECT if destructuring) |
michael@0 | 287 | * pn_kid2: null or the catch guard expression |
michael@0 | 288 | * pn_kid3: catch block statements |
michael@0 | 289 | * PNK_BREAK name pn_atom: label or null |
michael@0 | 290 | * PNK_CONTINUE name pn_atom: label or null |
michael@0 | 291 | * PNK_WITH binary-obj pn_left: head expr; pn_right: body; pn_binary_obj: StaticWithObject |
michael@0 | 292 | * PNK_VAR, list pn_head: list of PNK_NAME or PNK_ASSIGN nodes |
michael@0 | 293 | * PNK_CONST each name node has either |
michael@0 | 294 | * pn_used: false |
michael@0 | 295 | * pn_atom: variable name |
michael@0 | 296 | * pn_expr: initializer or null |
michael@0 | 297 | * or |
michael@0 | 298 | * pn_used: true |
michael@0 | 299 | * pn_atom: variable name |
michael@0 | 300 | * pn_lexdef: def node |
michael@0 | 301 | * each assignment node has |
michael@0 | 302 | * pn_left: PNK_NAME with pn_used true and |
michael@0 | 303 | * pn_lexdef (NOT pn_expr) set |
michael@0 | 304 | * pn_right: initializer |
michael@0 | 305 | * PNK_RETURN unary pn_kid: return expr or null |
michael@0 | 306 | * PNK_SEMI unary pn_kid: expr or null statement |
michael@0 | 307 | * pn_prologue: true if Directive Prologue member |
michael@0 | 308 | * in original source, not introduced via |
michael@0 | 309 | * constant folding or other tree rewriting |
michael@0 | 310 | * PNK_LABEL name pn_atom: label, pn_expr: labeled statement |
michael@0 | 311 | * |
michael@0 | 312 | * <Expressions> |
michael@0 | 313 | * All left-associated binary trees of the same type are optimized into lists |
michael@0 | 314 | * to avoid recursion when processing expression chains. |
michael@0 | 315 | * PNK_COMMA list pn_head: list of pn_count comma-separated exprs |
michael@0 | 316 | * PNK_ASSIGN binary pn_left: lvalue, pn_right: rvalue |
michael@0 | 317 | * PNK_ADDASSIGN, binary pn_left: lvalue, pn_right: rvalue |
michael@0 | 318 | * PNK_SUBASSIGN, pn_op: JSOP_ADD for +=, etc. |
michael@0 | 319 | * PNK_BITORASSIGN, |
michael@0 | 320 | * PNK_BITXORASSIGN, |
michael@0 | 321 | * PNK_BITANDASSIGN, |
michael@0 | 322 | * PNK_LSHASSIGN, |
michael@0 | 323 | * PNK_RSHASSIGN, |
michael@0 | 324 | * PNK_URSHASSIGN, |
michael@0 | 325 | * PNK_MULASSIGN, |
michael@0 | 326 | * PNK_DIVASSIGN, |
michael@0 | 327 | * PNK_MODASSIGN |
michael@0 | 328 | * PNK_CONDITIONAL ternary (cond ? trueExpr : falseExpr) |
michael@0 | 329 | * pn_kid1: cond, pn_kid2: then, pn_kid3: else |
michael@0 | 330 | * PNK_OR binary pn_left: first in || chain, pn_right: rest of chain |
michael@0 | 331 | * PNK_AND binary pn_left: first in && chain, pn_right: rest of chain |
michael@0 | 332 | * PNK_BITOR binary pn_left: left-assoc | expr, pn_right: ^ expr |
michael@0 | 333 | * PNK_BITXOR binary pn_left: left-assoc ^ expr, pn_right: & expr |
michael@0 | 334 | * PNK_BITAND binary pn_left: left-assoc & expr, pn_right: EQ expr |
michael@0 | 335 | * |
michael@0 | 336 | * PNK_EQ, binary pn_left: left-assoc EQ expr, pn_right: REL expr |
michael@0 | 337 | * PNK_NE, |
michael@0 | 338 | * PNK_STRICTEQ, |
michael@0 | 339 | * PNK_STRICTNE |
michael@0 | 340 | * PNK_LT, binary pn_left: left-assoc REL expr, pn_right: SH expr |
michael@0 | 341 | * PNK_LE, |
michael@0 | 342 | * PNK_GT, |
michael@0 | 343 | * PNK_GE |
michael@0 | 344 | * PNK_LSH, binary pn_left: left-assoc SH expr, pn_right: ADD expr |
michael@0 | 345 | * PNK_RSH, |
michael@0 | 346 | * PNK_URSH |
michael@0 | 347 | * PNK_ADD binary pn_left: left-assoc ADD expr, pn_right: MUL expr |
michael@0 | 348 | * pn_xflags: if a left-associated binary PNK_ADD |
michael@0 | 349 | * tree has been flattened into a list (see above |
michael@0 | 350 | * under <Expressions>), pn_xflags will contain |
michael@0 | 351 | * PNX_STRCAT if at least one list element is a |
michael@0 | 352 | * string literal (PNK_STRING); if such a list has |
michael@0 | 353 | * any non-string, non-number term, pn_xflags will |
michael@0 | 354 | * contain PNX_CANTFOLD. |
michael@0 | 355 | * PNK_SUB binary pn_left: left-assoc SH expr, pn_right: ADD expr |
michael@0 | 356 | * PNK_STAR, binary pn_left: left-assoc MUL expr, pn_right: UNARY expr |
michael@0 | 357 | * PNK_DIV, pn_op: JSOP_MUL, JSOP_DIV, JSOP_MOD |
michael@0 | 358 | * PNK_MOD |
michael@0 | 359 | * PNK_POS, unary pn_kid: UNARY expr |
michael@0 | 360 | * PNK_NEG |
michael@0 | 361 | * PNK_TYPEOF, unary pn_kid: UNARY expr |
michael@0 | 362 | * PNK_VOID, |
michael@0 | 363 | * PNK_NOT, |
michael@0 | 364 | * PNK_BITNOT |
michael@0 | 365 | * PNK_PREINCREMENT, unary pn_kid: MEMBER expr |
michael@0 | 366 | * PNK_POSTINCREMENT, |
michael@0 | 367 | * PNK_PREDECREMENT, |
michael@0 | 368 | * PNK_POSTDECREMENT |
michael@0 | 369 | * PNK_NEW list pn_head: list of ctor, arg1, arg2, ... argN |
michael@0 | 370 | * pn_count: 1 + N (where N is number of args) |
michael@0 | 371 | * ctor is a MEMBER expr |
michael@0 | 372 | * PNK_DELETE unary pn_kid: MEMBER expr |
michael@0 | 373 | * PNK_DOT name pn_expr: MEMBER expr to left of . |
michael@0 | 374 | * pn_atom: name to right of . |
michael@0 | 375 | * PNK_ELEM binary pn_left: MEMBER expr to left of [ |
michael@0 | 376 | * pn_right: expr between [ and ] |
michael@0 | 377 | * PNK_CALL list pn_head: list of call, arg1, arg2, ... argN |
michael@0 | 378 | * pn_count: 1 + N (where N is number of args) |
michael@0 | 379 | * call is a MEMBER expr naming a callable object |
michael@0 | 380 | * PNK_GENEXP list Exactly like PNK_CALL, used for the implicit call |
michael@0 | 381 | * in the desugaring of a generator-expression. |
michael@0 | 382 | * PNK_ARRAY list pn_head: list of pn_count array element exprs |
michael@0 | 383 | * [,,] holes are represented by PNK_ELISION nodes |
michael@0 | 384 | * pn_xflags: PN_ENDCOMMA if extra comma at end |
michael@0 | 385 | * PNK_OBJECT list pn_head: list of pn_count binary PNK_COLON nodes |
michael@0 | 386 | * PNK_COLON binary key-value pair in object initializer or |
michael@0 | 387 | * destructuring lhs |
michael@0 | 388 | * pn_left: property id, pn_right: value |
michael@0 | 389 | * var {x} = object destructuring shorthand shares |
michael@0 | 390 | * PN_NAME node for x on left and right of PNK_COLON |
michael@0 | 391 | * node in PNK_OBJECT's list, has PNX_DESTRUCT flag |
michael@0 | 392 | * PNK_NAME, name pn_atom: name, string, or object atom |
michael@0 | 393 | * PNK_STRING pn_op: JSOP_NAME, JSOP_STRING, or JSOP_OBJECT |
michael@0 | 394 | * If JSOP_NAME, pn_op may be JSOP_*ARG or JSOP_*VAR |
michael@0 | 395 | * with pn_cookie telling (staticLevel, slot) (see |
michael@0 | 396 | * jsscript.h's UPVAR macros) and pn_dflags telling |
michael@0 | 397 | * const-ness and static analysis results |
michael@0 | 398 | * PNK_REGEXP nullary pn_objbox: RegExp model object |
michael@0 | 399 | * PNK_NAME name If pn_used, PNK_NAME uses the lexdef member instead |
michael@0 | 400 | * of the expr member it overlays |
michael@0 | 401 | * PNK_NUMBER dval pn_dval: double value of numeric literal |
michael@0 | 402 | * PNK_TRUE, nullary pn_op: JSOp bytecode |
michael@0 | 403 | * PNK_FALSE, |
michael@0 | 404 | * PNK_NULL, |
michael@0 | 405 | * PNK_THIS |
michael@0 | 406 | * |
michael@0 | 407 | * PNK_LEXICALSCOPE name pn_objbox: block object in ObjectBox holder |
michael@0 | 408 | * pn_expr: block body |
michael@0 | 409 | * PNK_ARRAYCOMP list pn_count: 1 |
michael@0 | 410 | * pn_head: list of 1 element, which is block |
michael@0 | 411 | * enclosing for loop(s) and optionally |
michael@0 | 412 | * if-guarded PNK_ARRAYPUSH |
michael@0 | 413 | * PNK_ARRAYPUSH unary pn_op: JSOP_ARRAYCOMP |
michael@0 | 414 | * pn_kid: array comprehension expression |
michael@0 | 415 | * PNK_NOP nullary |
michael@0 | 416 | */ |
michael@0 | 417 | enum ParseNodeArity |
michael@0 | 418 | { |
michael@0 | 419 | PN_NULLARY, /* 0 kids, only pn_atom/pn_dval/etc. */ |
michael@0 | 420 | PN_UNARY, /* one kid, plus a couple of scalars */ |
michael@0 | 421 | PN_BINARY, /* two kids, plus a couple of scalars */ |
michael@0 | 422 | PN_BINARY_OBJ, /* two kids, plus an objbox */ |
michael@0 | 423 | PN_TERNARY, /* three kids */ |
michael@0 | 424 | PN_CODE, /* module or function definition node */ |
michael@0 | 425 | PN_LIST, /* generic singly linked list */ |
michael@0 | 426 | PN_NAME /* name use or definition node */ |
michael@0 | 427 | }; |
michael@0 | 428 | |
michael@0 | 429 | struct Definition; |
michael@0 | 430 | |
michael@0 | 431 | class LabeledStatement; |
michael@0 | 432 | class LoopControlStatement; |
michael@0 | 433 | class BreakStatement; |
michael@0 | 434 | class ContinueStatement; |
michael@0 | 435 | class ConditionalExpression; |
michael@0 | 436 | class PropertyAccess; |
michael@0 | 437 | |
michael@0 | 438 | class ParseNode |
michael@0 | 439 | { |
michael@0 | 440 | uint32_t pn_type : 16, /* PNK_* type */ |
michael@0 | 441 | pn_op : 8, /* see JSOp enum and jsopcode.tbl */ |
michael@0 | 442 | pn_arity : 5, /* see ParseNodeArity enum */ |
michael@0 | 443 | pn_parens : 1, /* this expr was enclosed in parens */ |
michael@0 | 444 | pn_used : 1, /* name node is on a use-chain */ |
michael@0 | 445 | pn_defn : 1; /* this node is a Definition */ |
michael@0 | 446 | |
michael@0 | 447 | ParseNode(const ParseNode &other) MOZ_DELETE; |
michael@0 | 448 | void operator=(const ParseNode &other) MOZ_DELETE; |
michael@0 | 449 | |
michael@0 | 450 | public: |
michael@0 | 451 | ParseNode(ParseNodeKind kind, JSOp op, ParseNodeArity arity) |
michael@0 | 452 | : pn_type(kind), pn_op(op), pn_arity(arity), pn_parens(0), pn_used(0), pn_defn(0), |
michael@0 | 453 | pn_pos(0, 0), pn_offset(0), pn_next(nullptr), pn_link(nullptr) |
michael@0 | 454 | { |
michael@0 | 455 | JS_ASSERT(kind < PNK_LIMIT); |
michael@0 | 456 | memset(&pn_u, 0, sizeof pn_u); |
michael@0 | 457 | } |
michael@0 | 458 | |
michael@0 | 459 | ParseNode(ParseNodeKind kind, JSOp op, ParseNodeArity arity, const TokenPos &pos) |
michael@0 | 460 | : pn_type(kind), pn_op(op), pn_arity(arity), pn_parens(0), pn_used(0), pn_defn(0), |
michael@0 | 461 | pn_pos(pos), pn_offset(0), pn_next(nullptr), pn_link(nullptr) |
michael@0 | 462 | { |
michael@0 | 463 | JS_ASSERT(kind < PNK_LIMIT); |
michael@0 | 464 | memset(&pn_u, 0, sizeof pn_u); |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | JSOp getOp() const { return JSOp(pn_op); } |
michael@0 | 468 | void setOp(JSOp op) { pn_op = op; } |
michael@0 | 469 | bool isOp(JSOp op) const { return getOp() == op; } |
michael@0 | 470 | |
michael@0 | 471 | ParseNodeKind getKind() const { |
michael@0 | 472 | JS_ASSERT(pn_type < PNK_LIMIT); |
michael@0 | 473 | return ParseNodeKind(pn_type); |
michael@0 | 474 | } |
michael@0 | 475 | void setKind(ParseNodeKind kind) { |
michael@0 | 476 | JS_ASSERT(kind < PNK_LIMIT); |
michael@0 | 477 | pn_type = kind; |
michael@0 | 478 | } |
michael@0 | 479 | bool isKind(ParseNodeKind kind) const { return getKind() == kind; } |
michael@0 | 480 | |
michael@0 | 481 | ParseNodeArity getArity() const { return ParseNodeArity(pn_arity); } |
michael@0 | 482 | bool isArity(ParseNodeArity a) const { return getArity() == a; } |
michael@0 | 483 | void setArity(ParseNodeArity a) { pn_arity = a; } |
michael@0 | 484 | |
michael@0 | 485 | bool isAssignment() const { |
michael@0 | 486 | ParseNodeKind kind = getKind(); |
michael@0 | 487 | return PNK_ASSIGNMENT_START <= kind && kind <= PNK_ASSIGNMENT_LAST; |
michael@0 | 488 | } |
michael@0 | 489 | |
michael@0 | 490 | /* Boolean attributes. */ |
michael@0 | 491 | bool isInParens() const { return pn_parens; } |
michael@0 | 492 | void setInParens(bool enabled) { pn_parens = enabled; } |
michael@0 | 493 | bool isUsed() const { return pn_used; } |
michael@0 | 494 | void setUsed(bool enabled) { pn_used = enabled; } |
michael@0 | 495 | bool isDefn() const { return pn_defn; } |
michael@0 | 496 | void setDefn(bool enabled) { pn_defn = enabled; } |
michael@0 | 497 | |
michael@0 | 498 | static const unsigned NumDefinitionFlagBits = 10; |
michael@0 | 499 | static const unsigned NumListFlagBits = 10; |
michael@0 | 500 | static const unsigned NumBlockIdBits = 22; |
michael@0 | 501 | static_assert(NumDefinitionFlagBits == NumListFlagBits, |
michael@0 | 502 | "Assumed below to achieve consistent blockid offset"); |
michael@0 | 503 | static_assert(NumDefinitionFlagBits + NumBlockIdBits <= 32, |
michael@0 | 504 | "This is supposed to fit in a single uint32_t"); |
michael@0 | 505 | |
michael@0 | 506 | TokenPos pn_pos; /* two 16-bit pairs here, for 64 bits */ |
michael@0 | 507 | int32_t pn_offset; /* first generated bytecode offset */ |
michael@0 | 508 | ParseNode *pn_next; /* intrinsic link in parent PN_LIST */ |
michael@0 | 509 | ParseNode *pn_link; /* def/use link (alignment freebie) */ |
michael@0 | 510 | |
michael@0 | 511 | union { |
michael@0 | 512 | struct { /* list of next-linked nodes */ |
michael@0 | 513 | ParseNode *head; /* first node in list */ |
michael@0 | 514 | ParseNode **tail; /* ptr to ptr to last node in list */ |
michael@0 | 515 | uint32_t count; /* number of nodes in list */ |
michael@0 | 516 | uint32_t xflags:NumListFlagBits, /* see PNX_* below */ |
michael@0 | 517 | blockid:NumBlockIdBits; /* see name variant below */ |
michael@0 | 518 | } list; |
michael@0 | 519 | struct { /* ternary: if, for(;;), ?: */ |
michael@0 | 520 | ParseNode *kid1; /* condition, discriminant, etc. */ |
michael@0 | 521 | ParseNode *kid2; /* then-part, case list, etc. */ |
michael@0 | 522 | ParseNode *kid3; /* else-part, default case, etc. */ |
michael@0 | 523 | } ternary; |
michael@0 | 524 | struct { /* two kids if binary */ |
michael@0 | 525 | ParseNode *left; |
michael@0 | 526 | ParseNode *right; |
michael@0 | 527 | union { |
michael@0 | 528 | unsigned iflags; /* JSITER_* flags for PNK_FOR node */ |
michael@0 | 529 | ObjectBox *objbox; /* Only for PN_BINARY_OBJ */ |
michael@0 | 530 | }; |
michael@0 | 531 | } binary; |
michael@0 | 532 | struct { /* one kid if unary */ |
michael@0 | 533 | ParseNode *kid; |
michael@0 | 534 | bool prologue; /* directive prologue member (as |
michael@0 | 535 | pn_prologue) */ |
michael@0 | 536 | } unary; |
michael@0 | 537 | struct { /* name, labeled statement, etc. */ |
michael@0 | 538 | union { |
michael@0 | 539 | JSAtom *atom; /* lexical name or label atom */ |
michael@0 | 540 | ObjectBox *objbox; /* block or regexp object */ |
michael@0 | 541 | FunctionBox *funbox; /* function object */ |
michael@0 | 542 | }; |
michael@0 | 543 | union { |
michael@0 | 544 | ParseNode *expr; /* module or function body, var |
michael@0 | 545 | initializer, argument default, or |
michael@0 | 546 | base object of PNK_DOT */ |
michael@0 | 547 | Definition *lexdef; /* lexical definition for this use */ |
michael@0 | 548 | }; |
michael@0 | 549 | UpvarCookie cookie; /* upvar cookie with absolute frame |
michael@0 | 550 | level (not relative skip), possibly |
michael@0 | 551 | in current frame */ |
michael@0 | 552 | uint32_t dflags:NumDefinitionFlagBits, /* see PND_* below */ |
michael@0 | 553 | blockid:NumBlockIdBits; /* block number, for subset dominance |
michael@0 | 554 | computation */ |
michael@0 | 555 | } name; |
michael@0 | 556 | struct { |
michael@0 | 557 | double value; /* aligned numeric literal value */ |
michael@0 | 558 | DecimalPoint decimalPoint; /* Whether the number has a decimal point */ |
michael@0 | 559 | } number; |
michael@0 | 560 | class { |
michael@0 | 561 | friend class LoopControlStatement; |
michael@0 | 562 | PropertyName *label; /* target of break/continue statement */ |
michael@0 | 563 | } loopControl; |
michael@0 | 564 | } pn_u; |
michael@0 | 565 | |
michael@0 | 566 | #define pn_modulebox pn_u.name.modulebox |
michael@0 | 567 | #define pn_funbox pn_u.name.funbox |
michael@0 | 568 | #define pn_body pn_u.name.expr |
michael@0 | 569 | #define pn_cookie pn_u.name.cookie |
michael@0 | 570 | #define pn_dflags pn_u.name.dflags |
michael@0 | 571 | #define pn_blockid pn_u.name.blockid |
michael@0 | 572 | #define pn_index pn_u.name.blockid /* reuse as object table index */ |
michael@0 | 573 | #define pn_head pn_u.list.head |
michael@0 | 574 | #define pn_tail pn_u.list.tail |
michael@0 | 575 | #define pn_count pn_u.list.count |
michael@0 | 576 | #define pn_xflags pn_u.list.xflags |
michael@0 | 577 | #define pn_kid1 pn_u.ternary.kid1 |
michael@0 | 578 | #define pn_kid2 pn_u.ternary.kid2 |
michael@0 | 579 | #define pn_kid3 pn_u.ternary.kid3 |
michael@0 | 580 | #define pn_left pn_u.binary.left |
michael@0 | 581 | #define pn_right pn_u.binary.right |
michael@0 | 582 | #define pn_pval pn_u.binary.pval |
michael@0 | 583 | #define pn_iflags pn_u.binary.iflags |
michael@0 | 584 | #define pn_binary_obj pn_u.binary.objbox |
michael@0 | 585 | #define pn_kid pn_u.unary.kid |
michael@0 | 586 | #define pn_prologue pn_u.unary.prologue |
michael@0 | 587 | #define pn_atom pn_u.name.atom |
michael@0 | 588 | #define pn_objbox pn_u.name.objbox |
michael@0 | 589 | #define pn_expr pn_u.name.expr |
michael@0 | 590 | #define pn_lexdef pn_u.name.lexdef |
michael@0 | 591 | #define pn_dval pn_u.number.value |
michael@0 | 592 | |
michael@0 | 593 | protected: |
michael@0 | 594 | void init(TokenKind type, JSOp op, ParseNodeArity arity) { |
michael@0 | 595 | pn_type = type; |
michael@0 | 596 | pn_op = op; |
michael@0 | 597 | pn_arity = arity; |
michael@0 | 598 | pn_parens = false; |
michael@0 | 599 | JS_ASSERT(!pn_used); |
michael@0 | 600 | JS_ASSERT(!pn_defn); |
michael@0 | 601 | pn_next = pn_link = nullptr; |
michael@0 | 602 | } |
michael@0 | 603 | |
michael@0 | 604 | static ParseNode *create(ParseNodeKind kind, ParseNodeArity arity, FullParseHandler *handler); |
michael@0 | 605 | |
michael@0 | 606 | public: |
michael@0 | 607 | /* |
michael@0 | 608 | * Append right to left, forming a list node. |left| must have the given |
michael@0 | 609 | * kind and op, and op must be left-associative. |
michael@0 | 610 | */ |
michael@0 | 611 | static ParseNode * |
michael@0 | 612 | append(ParseNodeKind tt, JSOp op, ParseNode *left, ParseNode *right, FullParseHandler *handler); |
michael@0 | 613 | |
michael@0 | 614 | /* |
michael@0 | 615 | * Either append right to left, if left meets the conditions necessary to |
michael@0 | 616 | * append (see append), or form a binary node whose children are right and |
michael@0 | 617 | * left. |
michael@0 | 618 | */ |
michael@0 | 619 | static ParseNode * |
michael@0 | 620 | newBinaryOrAppend(ParseNodeKind kind, JSOp op, ParseNode *left, ParseNode *right, |
michael@0 | 621 | FullParseHandler *handler, ParseContext<FullParseHandler> *pc, |
michael@0 | 622 | bool foldConstants); |
michael@0 | 623 | |
michael@0 | 624 | inline PropertyName *name() const; |
michael@0 | 625 | inline JSAtom *atom() const; |
michael@0 | 626 | |
michael@0 | 627 | /* |
michael@0 | 628 | * The pn_expr and lexdef members are arms of an unsafe union. Unless you |
michael@0 | 629 | * know exactly what you're doing, use only the following methods to access |
michael@0 | 630 | * them. For less overhead and assertions for protection, use pn->expr() |
michael@0 | 631 | * and pn->lexdef(). Otherwise, use pn->maybeExpr() and pn->maybeLexDef(). |
michael@0 | 632 | */ |
michael@0 | 633 | ParseNode *expr() const { |
michael@0 | 634 | JS_ASSERT(!pn_used); |
michael@0 | 635 | JS_ASSERT(pn_arity == PN_NAME || pn_arity == PN_CODE); |
michael@0 | 636 | return pn_expr; |
michael@0 | 637 | } |
michael@0 | 638 | |
michael@0 | 639 | Definition *lexdef() const { |
michael@0 | 640 | JS_ASSERT(pn_used || isDeoptimized()); |
michael@0 | 641 | JS_ASSERT(pn_arity == PN_NAME); |
michael@0 | 642 | return pn_lexdef; |
michael@0 | 643 | } |
michael@0 | 644 | |
michael@0 | 645 | ParseNode *maybeExpr() { return pn_used ? nullptr : expr(); } |
michael@0 | 646 | Definition *maybeLexDef() { return pn_used ? lexdef() : nullptr; } |
michael@0 | 647 | |
michael@0 | 648 | Definition *resolve(); |
michael@0 | 649 | |
michael@0 | 650 | /* PN_CODE and PN_NAME pn_dflags bits. */ |
michael@0 | 651 | #define PND_LET 0x01 /* let (block-scoped) binding */ |
michael@0 | 652 | #define PND_CONST 0x02 /* const binding (orthogonal to let) */ |
michael@0 | 653 | #define PND_ASSIGNED 0x04 /* set if ever LHS of assignment */ |
michael@0 | 654 | #define PND_PLACEHOLDER 0x08 /* placeholder definition for lexdep */ |
michael@0 | 655 | #define PND_BOUND 0x10 /* bound to a stack or global slot */ |
michael@0 | 656 | #define PND_DEOPTIMIZED 0x20 /* former pn_used name node, pn_lexdef |
michael@0 | 657 | still valid, but this use no longer |
michael@0 | 658 | optimizable via an upvar opcode */ |
michael@0 | 659 | #define PND_CLOSED 0x40 /* variable is closed over */ |
michael@0 | 660 | #define PND_DEFAULT 0x80 /* definition is an arg with a default */ |
michael@0 | 661 | #define PND_IMPLICITARGUMENTS 0x100 /* the definition is a placeholder for |
michael@0 | 662 | 'arguments' that has been converted |
michael@0 | 663 | into a definition after the function |
michael@0 | 664 | body has been parsed. */ |
michael@0 | 665 | #define PND_EMITTEDFUNCTION 0x200 /* hoisted function that was emitted */ |
michael@0 | 666 | |
michael@0 | 667 | static_assert(PND_EMITTEDFUNCTION < (1 << NumDefinitionFlagBits), "Not enough bits"); |
michael@0 | 668 | |
michael@0 | 669 | /* Flags to propagate from uses to definition. */ |
michael@0 | 670 | #define PND_USE2DEF_FLAGS (PND_ASSIGNED | PND_CLOSED) |
michael@0 | 671 | |
michael@0 | 672 | /* PN_LIST pn_xflags bits. */ |
michael@0 | 673 | #define PNX_POPVAR 0x01 /* PNK_VAR or PNK_CONST last result |
michael@0 | 674 | needs popping */ |
michael@0 | 675 | #define PNX_GROUPINIT 0x02 /* var [a, b] = [c, d]; unit list */ |
michael@0 | 676 | #define PNX_FUNCDEFS 0x04 /* contains top-level function statements */ |
michael@0 | 677 | #define PNX_SETCALL 0x08 /* call expression in lvalue context */ |
michael@0 | 678 | #define PNX_DESTRUCT 0x10 /* destructuring special cases: |
michael@0 | 679 | 1. shorthand syntax used, at present |
michael@0 | 680 | object destructuring ({x,y}) only; |
michael@0 | 681 | 2. code evaluating destructuring |
michael@0 | 682 | arguments occurs before function |
michael@0 | 683 | body */ |
michael@0 | 684 | #define PNX_SPECIALARRAYINIT 0x20 /* one or more of |
michael@0 | 685 | 1. array initialiser has holes |
michael@0 | 686 | 2. array initializer has spread node */ |
michael@0 | 687 | #define PNX_NONCONST 0x40 /* initialiser has non-constants */ |
michael@0 | 688 | |
michael@0 | 689 | static_assert(PNX_NONCONST < (1 << NumListFlagBits), "Not enough bits"); |
michael@0 | 690 | |
michael@0 | 691 | unsigned frameLevel() const { |
michael@0 | 692 | JS_ASSERT(pn_arity == PN_CODE || pn_arity == PN_NAME); |
michael@0 | 693 | return pn_cookie.level(); |
michael@0 | 694 | } |
michael@0 | 695 | |
michael@0 | 696 | uint32_t frameSlot() const { |
michael@0 | 697 | JS_ASSERT(pn_arity == PN_CODE || pn_arity == PN_NAME); |
michael@0 | 698 | return pn_cookie.slot(); |
michael@0 | 699 | } |
michael@0 | 700 | |
michael@0 | 701 | bool functionIsHoisted() const { |
michael@0 | 702 | JS_ASSERT(pn_arity == PN_CODE && getKind() == PNK_FUNCTION); |
michael@0 | 703 | JS_ASSERT(isOp(JSOP_LAMBDA) || // lambda, genexpr |
michael@0 | 704 | isOp(JSOP_LAMBDA_ARROW) || // arrow function |
michael@0 | 705 | isOp(JSOP_DEFFUN) || // non-body-level function statement |
michael@0 | 706 | isOp(JSOP_NOP) || // body-level function stmt in global code |
michael@0 | 707 | isOp(JSOP_GETLOCAL) || // body-level function stmt in function code |
michael@0 | 708 | isOp(JSOP_GETARG)); // body-level function redeclaring formal |
michael@0 | 709 | return !isOp(JSOP_LAMBDA) && !isOp(JSOP_LAMBDA_ARROW) && !isOp(JSOP_DEFFUN); |
michael@0 | 710 | } |
michael@0 | 711 | |
michael@0 | 712 | /* |
michael@0 | 713 | * True if this statement node could be a member of a Directive Prologue: an |
michael@0 | 714 | * expression statement consisting of a single string literal. |
michael@0 | 715 | * |
michael@0 | 716 | * This considers only the node and its children, not its context. After |
michael@0 | 717 | * parsing, check the node's pn_prologue flag to see if it is indeed part of |
michael@0 | 718 | * a directive prologue. |
michael@0 | 719 | * |
michael@0 | 720 | * Note that a Directive Prologue can contain statements that cannot |
michael@0 | 721 | * themselves be directives (string literals that include escape sequences |
michael@0 | 722 | * or escaped newlines, say). This member function returns true for such |
michael@0 | 723 | * nodes; we use it to determine the extent of the prologue. |
michael@0 | 724 | */ |
michael@0 | 725 | JSAtom *isStringExprStatement() const { |
michael@0 | 726 | if (getKind() == PNK_SEMI) { |
michael@0 | 727 | JS_ASSERT(pn_arity == PN_UNARY); |
michael@0 | 728 | ParseNode *kid = pn_kid; |
michael@0 | 729 | if (kid && kid->getKind() == PNK_STRING && !kid->pn_parens) |
michael@0 | 730 | return kid->pn_atom; |
michael@0 | 731 | } |
michael@0 | 732 | return nullptr; |
michael@0 | 733 | } |
michael@0 | 734 | |
michael@0 | 735 | inline bool test(unsigned flag) const; |
michael@0 | 736 | |
michael@0 | 737 | bool isLet() const { return test(PND_LET); } |
michael@0 | 738 | bool isConst() const { return test(PND_CONST); } |
michael@0 | 739 | bool isPlaceholder() const { return test(PND_PLACEHOLDER); } |
michael@0 | 740 | bool isDeoptimized() const { return test(PND_DEOPTIMIZED); } |
michael@0 | 741 | bool isAssigned() const { return test(PND_ASSIGNED); } |
michael@0 | 742 | bool isClosed() const { return test(PND_CLOSED); } |
michael@0 | 743 | bool isBound() const { return test(PND_BOUND); } |
michael@0 | 744 | bool isImplicitArguments() const { return test(PND_IMPLICITARGUMENTS); } |
michael@0 | 745 | |
michael@0 | 746 | /* True if pn is a parsenode representing a literal constant. */ |
michael@0 | 747 | bool isLiteral() const { |
michael@0 | 748 | return isKind(PNK_NUMBER) || |
michael@0 | 749 | isKind(PNK_STRING) || |
michael@0 | 750 | isKind(PNK_TRUE) || |
michael@0 | 751 | isKind(PNK_FALSE) || |
michael@0 | 752 | isKind(PNK_NULL); |
michael@0 | 753 | } |
michael@0 | 754 | |
michael@0 | 755 | /* Return true if this node appears in a Directive Prologue. */ |
michael@0 | 756 | bool isDirectivePrologueMember() const { return pn_prologue; } |
michael@0 | 757 | |
michael@0 | 758 | #ifdef JS_HAS_GENERATOR_EXPRS |
michael@0 | 759 | ParseNode *generatorExpr() const { |
michael@0 | 760 | JS_ASSERT(isKind(PNK_GENEXP)); |
michael@0 | 761 | ParseNode *callee = this->pn_head; |
michael@0 | 762 | ParseNode *body = callee->pn_body; |
michael@0 | 763 | JS_ASSERT(body->isKind(PNK_LEXICALSCOPE)); |
michael@0 | 764 | return body->pn_expr; |
michael@0 | 765 | } |
michael@0 | 766 | #endif |
michael@0 | 767 | |
michael@0 | 768 | inline void markAsAssigned(); |
michael@0 | 769 | |
michael@0 | 770 | /* |
michael@0 | 771 | * Compute a pointer to the last element in a singly-linked list. NB: list |
michael@0 | 772 | * must be non-empty for correct PN_LAST usage -- this is asserted! |
michael@0 | 773 | */ |
michael@0 | 774 | ParseNode *last() const { |
michael@0 | 775 | JS_ASSERT(pn_arity == PN_LIST); |
michael@0 | 776 | JS_ASSERT(pn_count != 0); |
michael@0 | 777 | return (ParseNode *)(uintptr_t(pn_tail) - offsetof(ParseNode, pn_next)); |
michael@0 | 778 | } |
michael@0 | 779 | |
michael@0 | 780 | void initNumber(double value, DecimalPoint decimalPoint) { |
michael@0 | 781 | JS_ASSERT(pn_arity == PN_NULLARY); |
michael@0 | 782 | JS_ASSERT(getKind() == PNK_NUMBER); |
michael@0 | 783 | pn_u.number.value = value; |
michael@0 | 784 | pn_u.number.decimalPoint = decimalPoint; |
michael@0 | 785 | } |
michael@0 | 786 | |
michael@0 | 787 | void makeEmpty() { |
michael@0 | 788 | JS_ASSERT(pn_arity == PN_LIST); |
michael@0 | 789 | pn_head = nullptr; |
michael@0 | 790 | pn_tail = &pn_head; |
michael@0 | 791 | pn_count = 0; |
michael@0 | 792 | pn_xflags = 0; |
michael@0 | 793 | pn_blockid = 0; |
michael@0 | 794 | } |
michael@0 | 795 | |
michael@0 | 796 | void initList(ParseNode *pn) { |
michael@0 | 797 | JS_ASSERT(pn_arity == PN_LIST); |
michael@0 | 798 | if (pn->pn_pos.begin < pn_pos.begin) |
michael@0 | 799 | pn_pos.begin = pn->pn_pos.begin; |
michael@0 | 800 | pn_pos.end = pn->pn_pos.end; |
michael@0 | 801 | pn_head = pn; |
michael@0 | 802 | pn_tail = &pn->pn_next; |
michael@0 | 803 | pn_count = 1; |
michael@0 | 804 | pn_xflags = 0; |
michael@0 | 805 | pn_blockid = 0; |
michael@0 | 806 | } |
michael@0 | 807 | |
michael@0 | 808 | void append(ParseNode *pn) { |
michael@0 | 809 | JS_ASSERT(pn_arity == PN_LIST); |
michael@0 | 810 | JS_ASSERT(pn->pn_pos.begin >= pn_pos.begin); |
michael@0 | 811 | pn_pos.end = pn->pn_pos.end; |
michael@0 | 812 | *pn_tail = pn; |
michael@0 | 813 | pn_tail = &pn->pn_next; |
michael@0 | 814 | pn_count++; |
michael@0 | 815 | } |
michael@0 | 816 | |
michael@0 | 817 | void checkListConsistency() |
michael@0 | 818 | #ifndef DEBUG |
michael@0 | 819 | {} |
michael@0 | 820 | #endif |
michael@0 | 821 | ; |
michael@0 | 822 | |
michael@0 | 823 | bool getConstantValue(ExclusiveContext *cx, bool strictChecks, MutableHandleValue vp); |
michael@0 | 824 | inline bool isConstant(); |
michael@0 | 825 | |
michael@0 | 826 | template <class NodeType> |
michael@0 | 827 | inline bool is() const { |
michael@0 | 828 | return NodeType::test(*this); |
michael@0 | 829 | } |
michael@0 | 830 | |
michael@0 | 831 | /* Casting operations. */ |
michael@0 | 832 | template <class NodeType> |
michael@0 | 833 | inline NodeType &as() { |
michael@0 | 834 | JS_ASSERT(NodeType::test(*this)); |
michael@0 | 835 | return *static_cast<NodeType *>(this); |
michael@0 | 836 | } |
michael@0 | 837 | |
michael@0 | 838 | template <class NodeType> |
michael@0 | 839 | inline const NodeType &as() const { |
michael@0 | 840 | JS_ASSERT(NodeType::test(*this)); |
michael@0 | 841 | return *static_cast<const NodeType *>(this); |
michael@0 | 842 | } |
michael@0 | 843 | |
michael@0 | 844 | #ifdef DEBUG |
michael@0 | 845 | void dump(); |
michael@0 | 846 | void dump(int indent); |
michael@0 | 847 | #endif |
michael@0 | 848 | }; |
michael@0 | 849 | |
michael@0 | 850 | struct NullaryNode : public ParseNode |
michael@0 | 851 | { |
michael@0 | 852 | NullaryNode(ParseNodeKind kind, const TokenPos &pos) |
michael@0 | 853 | : ParseNode(kind, JSOP_NOP, PN_NULLARY, pos) {} |
michael@0 | 854 | NullaryNode(ParseNodeKind kind, JSOp op, const TokenPos &pos) |
michael@0 | 855 | : ParseNode(kind, op, PN_NULLARY, pos) {} |
michael@0 | 856 | |
michael@0 | 857 | // This constructor is for a few mad uses in the emitter. It populates |
michael@0 | 858 | // the pn_atom field even though that field belongs to a branch in pn_u |
michael@0 | 859 | // that nullary nodes shouldn't use -- bogus. |
michael@0 | 860 | NullaryNode(ParseNodeKind kind, JSOp op, const TokenPos &pos, JSAtom *atom) |
michael@0 | 861 | : ParseNode(kind, op, PN_NULLARY, pos) |
michael@0 | 862 | { |
michael@0 | 863 | pn_atom = atom; |
michael@0 | 864 | } |
michael@0 | 865 | |
michael@0 | 866 | static bool test(const ParseNode &node) { |
michael@0 | 867 | return node.isArity(PN_NULLARY); |
michael@0 | 868 | } |
michael@0 | 869 | |
michael@0 | 870 | #ifdef DEBUG |
michael@0 | 871 | void dump(); |
michael@0 | 872 | #endif |
michael@0 | 873 | }; |
michael@0 | 874 | |
michael@0 | 875 | struct UnaryNode : public ParseNode |
michael@0 | 876 | { |
michael@0 | 877 | UnaryNode(ParseNodeKind kind, JSOp op, const TokenPos &pos, ParseNode *kid) |
michael@0 | 878 | : ParseNode(kind, op, PN_UNARY, pos) |
michael@0 | 879 | { |
michael@0 | 880 | pn_kid = kid; |
michael@0 | 881 | } |
michael@0 | 882 | |
michael@0 | 883 | static inline UnaryNode *create(ParseNodeKind kind, FullParseHandler *handler) { |
michael@0 | 884 | return (UnaryNode *) ParseNode::create(kind, PN_UNARY, handler); |
michael@0 | 885 | } |
michael@0 | 886 | |
michael@0 | 887 | static bool test(const ParseNode &node) { |
michael@0 | 888 | return node.isArity(PN_UNARY); |
michael@0 | 889 | } |
michael@0 | 890 | |
michael@0 | 891 | #ifdef DEBUG |
michael@0 | 892 | void dump(int indent); |
michael@0 | 893 | #endif |
michael@0 | 894 | }; |
michael@0 | 895 | |
michael@0 | 896 | struct BinaryNode : public ParseNode |
michael@0 | 897 | { |
michael@0 | 898 | BinaryNode(ParseNodeKind kind, JSOp op, const TokenPos &pos, ParseNode *left, ParseNode *right) |
michael@0 | 899 | : ParseNode(kind, op, PN_BINARY, pos) |
michael@0 | 900 | { |
michael@0 | 901 | pn_left = left; |
michael@0 | 902 | pn_right = right; |
michael@0 | 903 | } |
michael@0 | 904 | |
michael@0 | 905 | BinaryNode(ParseNodeKind kind, JSOp op, ParseNode *left, ParseNode *right) |
michael@0 | 906 | : ParseNode(kind, op, PN_BINARY, TokenPos::box(left->pn_pos, right->pn_pos)) |
michael@0 | 907 | { |
michael@0 | 908 | pn_left = left; |
michael@0 | 909 | pn_right = right; |
michael@0 | 910 | } |
michael@0 | 911 | |
michael@0 | 912 | static inline BinaryNode *create(ParseNodeKind kind, FullParseHandler *handler) { |
michael@0 | 913 | return (BinaryNode *) ParseNode::create(kind, PN_BINARY, handler); |
michael@0 | 914 | } |
michael@0 | 915 | |
michael@0 | 916 | static bool test(const ParseNode &node) { |
michael@0 | 917 | return node.isArity(PN_BINARY); |
michael@0 | 918 | } |
michael@0 | 919 | |
michael@0 | 920 | #ifdef DEBUG |
michael@0 | 921 | void dump(int indent); |
michael@0 | 922 | #endif |
michael@0 | 923 | }; |
michael@0 | 924 | |
michael@0 | 925 | struct BinaryObjNode : public ParseNode |
michael@0 | 926 | { |
michael@0 | 927 | BinaryObjNode(ParseNodeKind kind, JSOp op, const TokenPos &pos, ParseNode *left, ParseNode *right, |
michael@0 | 928 | ObjectBox *objbox) |
michael@0 | 929 | : ParseNode(kind, op, PN_BINARY_OBJ, pos) |
michael@0 | 930 | { |
michael@0 | 931 | pn_left = left; |
michael@0 | 932 | pn_right = right; |
michael@0 | 933 | pn_binary_obj = objbox; |
michael@0 | 934 | } |
michael@0 | 935 | |
michael@0 | 936 | static inline BinaryObjNode *create(ParseNodeKind kind, FullParseHandler *handler) { |
michael@0 | 937 | return (BinaryObjNode *) ParseNode::create(kind, PN_BINARY_OBJ, handler); |
michael@0 | 938 | } |
michael@0 | 939 | |
michael@0 | 940 | static bool test(const ParseNode &node) { |
michael@0 | 941 | return node.isArity(PN_BINARY_OBJ); |
michael@0 | 942 | } |
michael@0 | 943 | |
michael@0 | 944 | #ifdef DEBUG |
michael@0 | 945 | void dump(int indent); |
michael@0 | 946 | #endif |
michael@0 | 947 | }; |
michael@0 | 948 | |
michael@0 | 949 | struct TernaryNode : public ParseNode |
michael@0 | 950 | { |
michael@0 | 951 | TernaryNode(ParseNodeKind kind, JSOp op, ParseNode *kid1, ParseNode *kid2, ParseNode *kid3) |
michael@0 | 952 | : ParseNode(kind, op, PN_TERNARY, |
michael@0 | 953 | TokenPos((kid1 ? kid1 : kid2 ? kid2 : kid3)->pn_pos.begin, |
michael@0 | 954 | (kid3 ? kid3 : kid2 ? kid2 : kid1)->pn_pos.end)) |
michael@0 | 955 | { |
michael@0 | 956 | pn_kid1 = kid1; |
michael@0 | 957 | pn_kid2 = kid2; |
michael@0 | 958 | pn_kid3 = kid3; |
michael@0 | 959 | } |
michael@0 | 960 | |
michael@0 | 961 | TernaryNode(ParseNodeKind kind, JSOp op, ParseNode *kid1, ParseNode *kid2, ParseNode *kid3, |
michael@0 | 962 | const TokenPos &pos) |
michael@0 | 963 | : ParseNode(kind, op, PN_TERNARY, pos) |
michael@0 | 964 | { |
michael@0 | 965 | pn_kid1 = kid1; |
michael@0 | 966 | pn_kid2 = kid2; |
michael@0 | 967 | pn_kid3 = kid3; |
michael@0 | 968 | } |
michael@0 | 969 | |
michael@0 | 970 | static inline TernaryNode *create(ParseNodeKind kind, FullParseHandler *handler) { |
michael@0 | 971 | return (TernaryNode *) ParseNode::create(kind, PN_TERNARY, handler); |
michael@0 | 972 | } |
michael@0 | 973 | |
michael@0 | 974 | static bool test(const ParseNode &node) { |
michael@0 | 975 | return node.isArity(PN_TERNARY); |
michael@0 | 976 | } |
michael@0 | 977 | |
michael@0 | 978 | #ifdef DEBUG |
michael@0 | 979 | void dump(int indent); |
michael@0 | 980 | #endif |
michael@0 | 981 | }; |
michael@0 | 982 | |
michael@0 | 983 | struct ListNode : public ParseNode |
michael@0 | 984 | { |
michael@0 | 985 | ListNode(ParseNodeKind kind, const TokenPos &pos) |
michael@0 | 986 | : ParseNode(kind, JSOP_NOP, PN_LIST, pos) |
michael@0 | 987 | { |
michael@0 | 988 | makeEmpty(); |
michael@0 | 989 | } |
michael@0 | 990 | |
michael@0 | 991 | ListNode(ParseNodeKind kind, JSOp op, ParseNode *kid) |
michael@0 | 992 | : ParseNode(kind, op, PN_LIST, kid->pn_pos) |
michael@0 | 993 | { |
michael@0 | 994 | initList(kid); |
michael@0 | 995 | } |
michael@0 | 996 | |
michael@0 | 997 | static inline ListNode *create(ParseNodeKind kind, FullParseHandler *handler) { |
michael@0 | 998 | return (ListNode *) ParseNode::create(kind, PN_LIST, handler); |
michael@0 | 999 | } |
michael@0 | 1000 | |
michael@0 | 1001 | static bool test(const ParseNode &node) { |
michael@0 | 1002 | return node.isArity(PN_LIST); |
michael@0 | 1003 | } |
michael@0 | 1004 | |
michael@0 | 1005 | #ifdef DEBUG |
michael@0 | 1006 | void dump(int indent); |
michael@0 | 1007 | #endif |
michael@0 | 1008 | }; |
michael@0 | 1009 | |
michael@0 | 1010 | struct CodeNode : public ParseNode |
michael@0 | 1011 | { |
michael@0 | 1012 | static inline CodeNode *create(ParseNodeKind kind, FullParseHandler *handler) { |
michael@0 | 1013 | return (CodeNode *) ParseNode::create(kind, PN_CODE, handler); |
michael@0 | 1014 | } |
michael@0 | 1015 | |
michael@0 | 1016 | static bool test(const ParseNode &node) { |
michael@0 | 1017 | return node.isArity(PN_CODE); |
michael@0 | 1018 | } |
michael@0 | 1019 | |
michael@0 | 1020 | #ifdef DEBUG |
michael@0 | 1021 | void dump(int indent); |
michael@0 | 1022 | #endif |
michael@0 | 1023 | }; |
michael@0 | 1024 | |
michael@0 | 1025 | struct NameNode : public ParseNode |
michael@0 | 1026 | { |
michael@0 | 1027 | NameNode(ParseNodeKind kind, JSOp op, JSAtom *atom, uint32_t blockid, |
michael@0 | 1028 | const TokenPos &pos) |
michael@0 | 1029 | : ParseNode(kind, op, PN_NAME, pos) |
michael@0 | 1030 | { |
michael@0 | 1031 | pn_atom = atom; |
michael@0 | 1032 | pn_expr = nullptr; |
michael@0 | 1033 | pn_cookie.makeFree(); |
michael@0 | 1034 | pn_dflags = 0; |
michael@0 | 1035 | pn_blockid = blockid; |
michael@0 | 1036 | JS_ASSERT(pn_blockid == blockid); // check for bitfield overflow |
michael@0 | 1037 | } |
michael@0 | 1038 | |
michael@0 | 1039 | static bool test(const ParseNode &node) { |
michael@0 | 1040 | return node.isArity(PN_NAME); |
michael@0 | 1041 | } |
michael@0 | 1042 | |
michael@0 | 1043 | #ifdef DEBUG |
michael@0 | 1044 | void dump(int indent); |
michael@0 | 1045 | #endif |
michael@0 | 1046 | }; |
michael@0 | 1047 | |
michael@0 | 1048 | struct LexicalScopeNode : public ParseNode |
michael@0 | 1049 | { |
michael@0 | 1050 | static inline LexicalScopeNode *create(ParseNodeKind kind, FullParseHandler *handler) { |
michael@0 | 1051 | return (LexicalScopeNode *) ParseNode::create(kind, PN_NAME, handler); |
michael@0 | 1052 | } |
michael@0 | 1053 | }; |
michael@0 | 1054 | |
michael@0 | 1055 | class LabeledStatement : public ParseNode |
michael@0 | 1056 | { |
michael@0 | 1057 | public: |
michael@0 | 1058 | LabeledStatement(PropertyName *label, ParseNode *stmt, uint32_t begin) |
michael@0 | 1059 | : ParseNode(PNK_LABEL, JSOP_NOP, PN_NAME, TokenPos(begin, stmt->pn_pos.end)) |
michael@0 | 1060 | { |
michael@0 | 1061 | pn_atom = label; |
michael@0 | 1062 | pn_expr = stmt; |
michael@0 | 1063 | } |
michael@0 | 1064 | |
michael@0 | 1065 | PropertyName *label() const { |
michael@0 | 1066 | return pn_atom->asPropertyName(); |
michael@0 | 1067 | } |
michael@0 | 1068 | |
michael@0 | 1069 | ParseNode *statement() const { |
michael@0 | 1070 | return pn_expr; |
michael@0 | 1071 | } |
michael@0 | 1072 | |
michael@0 | 1073 | static bool test(const ParseNode &node) { |
michael@0 | 1074 | bool match = node.isKind(PNK_LABEL); |
michael@0 | 1075 | JS_ASSERT_IF(match, node.isArity(PN_NAME)); |
michael@0 | 1076 | JS_ASSERT_IF(match, node.isOp(JSOP_NOP)); |
michael@0 | 1077 | return match; |
michael@0 | 1078 | } |
michael@0 | 1079 | }; |
michael@0 | 1080 | |
michael@0 | 1081 | class LoopControlStatement : public ParseNode |
michael@0 | 1082 | { |
michael@0 | 1083 | protected: |
michael@0 | 1084 | LoopControlStatement(ParseNodeKind kind, PropertyName *label, const TokenPos &pos) |
michael@0 | 1085 | : ParseNode(kind, JSOP_NOP, PN_NULLARY, pos) |
michael@0 | 1086 | { |
michael@0 | 1087 | JS_ASSERT(kind == PNK_BREAK || kind == PNK_CONTINUE); |
michael@0 | 1088 | pn_u.loopControl.label = label; |
michael@0 | 1089 | } |
michael@0 | 1090 | |
michael@0 | 1091 | public: |
michael@0 | 1092 | /* Label associated with this break/continue statement, if any. */ |
michael@0 | 1093 | PropertyName *label() const { |
michael@0 | 1094 | return pn_u.loopControl.label; |
michael@0 | 1095 | } |
michael@0 | 1096 | |
michael@0 | 1097 | static bool test(const ParseNode &node) { |
michael@0 | 1098 | bool match = node.isKind(PNK_BREAK) || node.isKind(PNK_CONTINUE); |
michael@0 | 1099 | JS_ASSERT_IF(match, node.isArity(PN_NULLARY)); |
michael@0 | 1100 | JS_ASSERT_IF(match, node.isOp(JSOP_NOP)); |
michael@0 | 1101 | return match; |
michael@0 | 1102 | } |
michael@0 | 1103 | }; |
michael@0 | 1104 | |
michael@0 | 1105 | class BreakStatement : public LoopControlStatement |
michael@0 | 1106 | { |
michael@0 | 1107 | public: |
michael@0 | 1108 | BreakStatement(PropertyName *label, const TokenPos &pos) |
michael@0 | 1109 | : LoopControlStatement(PNK_BREAK, label, pos) |
michael@0 | 1110 | { } |
michael@0 | 1111 | |
michael@0 | 1112 | static bool test(const ParseNode &node) { |
michael@0 | 1113 | bool match = node.isKind(PNK_BREAK); |
michael@0 | 1114 | JS_ASSERT_IF(match, node.isArity(PN_NULLARY)); |
michael@0 | 1115 | JS_ASSERT_IF(match, node.isOp(JSOP_NOP)); |
michael@0 | 1116 | return match; |
michael@0 | 1117 | } |
michael@0 | 1118 | }; |
michael@0 | 1119 | |
michael@0 | 1120 | class ContinueStatement : public LoopControlStatement |
michael@0 | 1121 | { |
michael@0 | 1122 | public: |
michael@0 | 1123 | ContinueStatement(PropertyName *label, const TokenPos &pos) |
michael@0 | 1124 | : LoopControlStatement(PNK_CONTINUE, label, pos) |
michael@0 | 1125 | { } |
michael@0 | 1126 | |
michael@0 | 1127 | static bool test(const ParseNode &node) { |
michael@0 | 1128 | bool match = node.isKind(PNK_CONTINUE); |
michael@0 | 1129 | JS_ASSERT_IF(match, node.isArity(PN_NULLARY)); |
michael@0 | 1130 | JS_ASSERT_IF(match, node.isOp(JSOP_NOP)); |
michael@0 | 1131 | return match; |
michael@0 | 1132 | } |
michael@0 | 1133 | }; |
michael@0 | 1134 | |
michael@0 | 1135 | class DebuggerStatement : public ParseNode |
michael@0 | 1136 | { |
michael@0 | 1137 | public: |
michael@0 | 1138 | DebuggerStatement(const TokenPos &pos) |
michael@0 | 1139 | : ParseNode(PNK_DEBUGGER, JSOP_NOP, PN_NULLARY, pos) |
michael@0 | 1140 | { } |
michael@0 | 1141 | }; |
michael@0 | 1142 | |
michael@0 | 1143 | class ConditionalExpression : public ParseNode |
michael@0 | 1144 | { |
michael@0 | 1145 | public: |
michael@0 | 1146 | ConditionalExpression(ParseNode *condition, ParseNode *thenExpr, ParseNode *elseExpr) |
michael@0 | 1147 | : ParseNode(PNK_CONDITIONAL, JSOP_NOP, PN_TERNARY, |
michael@0 | 1148 | TokenPos(condition->pn_pos.begin, elseExpr->pn_pos.end)) |
michael@0 | 1149 | { |
michael@0 | 1150 | JS_ASSERT(condition); |
michael@0 | 1151 | JS_ASSERT(thenExpr); |
michael@0 | 1152 | JS_ASSERT(elseExpr); |
michael@0 | 1153 | pn_u.ternary.kid1 = condition; |
michael@0 | 1154 | pn_u.ternary.kid2 = thenExpr; |
michael@0 | 1155 | pn_u.ternary.kid3 = elseExpr; |
michael@0 | 1156 | } |
michael@0 | 1157 | |
michael@0 | 1158 | ParseNode &condition() const { |
michael@0 | 1159 | return *pn_u.ternary.kid1; |
michael@0 | 1160 | } |
michael@0 | 1161 | |
michael@0 | 1162 | ParseNode &thenExpression() const { |
michael@0 | 1163 | return *pn_u.ternary.kid2; |
michael@0 | 1164 | } |
michael@0 | 1165 | |
michael@0 | 1166 | ParseNode &elseExpression() const { |
michael@0 | 1167 | return *pn_u.ternary.kid3; |
michael@0 | 1168 | } |
michael@0 | 1169 | |
michael@0 | 1170 | static bool test(const ParseNode &node) { |
michael@0 | 1171 | bool match = node.isKind(PNK_CONDITIONAL); |
michael@0 | 1172 | JS_ASSERT_IF(match, node.isArity(PN_TERNARY)); |
michael@0 | 1173 | JS_ASSERT_IF(match, node.isOp(JSOP_NOP)); |
michael@0 | 1174 | return match; |
michael@0 | 1175 | } |
michael@0 | 1176 | }; |
michael@0 | 1177 | |
michael@0 | 1178 | class ThisLiteral : public ParseNode |
michael@0 | 1179 | { |
michael@0 | 1180 | public: |
michael@0 | 1181 | ThisLiteral(const TokenPos &pos) : ParseNode(PNK_THIS, JSOP_THIS, PN_NULLARY, pos) { } |
michael@0 | 1182 | }; |
michael@0 | 1183 | |
michael@0 | 1184 | class NullLiteral : public ParseNode |
michael@0 | 1185 | { |
michael@0 | 1186 | public: |
michael@0 | 1187 | NullLiteral(const TokenPos &pos) : ParseNode(PNK_NULL, JSOP_NULL, PN_NULLARY, pos) { } |
michael@0 | 1188 | }; |
michael@0 | 1189 | |
michael@0 | 1190 | class BooleanLiteral : public ParseNode |
michael@0 | 1191 | { |
michael@0 | 1192 | public: |
michael@0 | 1193 | BooleanLiteral(bool b, const TokenPos &pos) |
michael@0 | 1194 | : ParseNode(b ? PNK_TRUE : PNK_FALSE, b ? JSOP_TRUE : JSOP_FALSE, PN_NULLARY, pos) |
michael@0 | 1195 | { } |
michael@0 | 1196 | }; |
michael@0 | 1197 | |
michael@0 | 1198 | class RegExpLiteral : public NullaryNode |
michael@0 | 1199 | { |
michael@0 | 1200 | public: |
michael@0 | 1201 | RegExpLiteral(ObjectBox *reobj, const TokenPos &pos) |
michael@0 | 1202 | : NullaryNode(PNK_REGEXP, JSOP_REGEXP, pos) |
michael@0 | 1203 | { |
michael@0 | 1204 | pn_objbox = reobj; |
michael@0 | 1205 | } |
michael@0 | 1206 | |
michael@0 | 1207 | ObjectBox *objbox() const { return pn_objbox; } |
michael@0 | 1208 | |
michael@0 | 1209 | static bool test(const ParseNode &node) { |
michael@0 | 1210 | bool match = node.isKind(PNK_REGEXP); |
michael@0 | 1211 | JS_ASSERT_IF(match, node.isArity(PN_NULLARY)); |
michael@0 | 1212 | JS_ASSERT_IF(match, node.isOp(JSOP_REGEXP)); |
michael@0 | 1213 | return match; |
michael@0 | 1214 | } |
michael@0 | 1215 | }; |
michael@0 | 1216 | |
michael@0 | 1217 | class PropertyAccess : public ParseNode |
michael@0 | 1218 | { |
michael@0 | 1219 | public: |
michael@0 | 1220 | PropertyAccess(ParseNode *lhs, PropertyName *name, uint32_t begin, uint32_t end) |
michael@0 | 1221 | : ParseNode(PNK_DOT, JSOP_NOP, PN_NAME, TokenPos(begin, end)) |
michael@0 | 1222 | { |
michael@0 | 1223 | JS_ASSERT(lhs != nullptr); |
michael@0 | 1224 | JS_ASSERT(name != nullptr); |
michael@0 | 1225 | pn_u.name.expr = lhs; |
michael@0 | 1226 | pn_u.name.atom = name; |
michael@0 | 1227 | } |
michael@0 | 1228 | |
michael@0 | 1229 | static bool test(const ParseNode &node) { |
michael@0 | 1230 | bool match = node.isKind(PNK_DOT); |
michael@0 | 1231 | JS_ASSERT_IF(match, node.isArity(PN_NAME)); |
michael@0 | 1232 | return match; |
michael@0 | 1233 | } |
michael@0 | 1234 | |
michael@0 | 1235 | ParseNode &expression() const { |
michael@0 | 1236 | return *pn_u.name.expr; |
michael@0 | 1237 | } |
michael@0 | 1238 | |
michael@0 | 1239 | PropertyName &name() const { |
michael@0 | 1240 | return *pn_u.name.atom->asPropertyName(); |
michael@0 | 1241 | } |
michael@0 | 1242 | }; |
michael@0 | 1243 | |
michael@0 | 1244 | class PropertyByValue : public ParseNode |
michael@0 | 1245 | { |
michael@0 | 1246 | public: |
michael@0 | 1247 | PropertyByValue(ParseNode *lhs, ParseNode *propExpr, uint32_t begin, uint32_t end) |
michael@0 | 1248 | : ParseNode(PNK_ELEM, JSOP_NOP, PN_BINARY, TokenPos(begin, end)) |
michael@0 | 1249 | { |
michael@0 | 1250 | pn_u.binary.left = lhs; |
michael@0 | 1251 | pn_u.binary.right = propExpr; |
michael@0 | 1252 | } |
michael@0 | 1253 | }; |
michael@0 | 1254 | |
michael@0 | 1255 | #ifdef DEBUG |
michael@0 | 1256 | void DumpParseTree(ParseNode *pn, int indent = 0); |
michael@0 | 1257 | #endif |
michael@0 | 1258 | |
michael@0 | 1259 | /* |
michael@0 | 1260 | * js::Definition is a degenerate subtype of the PN_FUNC and PN_NAME variants |
michael@0 | 1261 | * of js::ParseNode, allocated only for function, var, const, and let |
michael@0 | 1262 | * declarations that define truly lexical bindings. This means that a child of |
michael@0 | 1263 | * a PNK_VAR list may be a Definition as well as a ParseNode. The pn_defn bit |
michael@0 | 1264 | * is set for all Definitions, clear otherwise. |
michael@0 | 1265 | * |
michael@0 | 1266 | * In an upvars list, defn->resolve() is the outermost definition the |
michael@0 | 1267 | * name may reference. If a with block or a function that calls eval encloses |
michael@0 | 1268 | * the use, the name may end up referring to something else at runtime. |
michael@0 | 1269 | * |
michael@0 | 1270 | * Note that not all var declarations are definitions: JS allows multiple var |
michael@0 | 1271 | * declarations in a function or script, but only the first creates the hoisted |
michael@0 | 1272 | * binding. JS programmers do redeclare variables for good refactoring reasons, |
michael@0 | 1273 | * for example: |
michael@0 | 1274 | * |
michael@0 | 1275 | * function foo() { |
michael@0 | 1276 | * ... |
michael@0 | 1277 | * for (var i ...) ...; |
michael@0 | 1278 | * ... |
michael@0 | 1279 | * for (var i ...) ...; |
michael@0 | 1280 | * ... |
michael@0 | 1281 | * } |
michael@0 | 1282 | * |
michael@0 | 1283 | * Not all definitions bind lexical variables, alas. In global and eval code |
michael@0 | 1284 | * var may re-declare a pre-existing property having any attributes, with or |
michael@0 | 1285 | * without JSPROP_PERMANENT. In eval code, indeed, ECMA-262 Editions 1 through |
michael@0 | 1286 | * 3 require function and var to bind deletable bindings. Global vars thus are |
michael@0 | 1287 | * properties of the global object, so they can be aliased even if they can't |
michael@0 | 1288 | * be deleted. |
michael@0 | 1289 | * |
michael@0 | 1290 | * Only bindings within function code may be treated as lexical, of course with |
michael@0 | 1291 | * the caveat that hoisting means use before initialization is allowed. We deal |
michael@0 | 1292 | * with use before declaration in one pass as follows (error checking elided): |
michael@0 | 1293 | * |
michael@0 | 1294 | * for (each use of unqualified name x in parse order) { |
michael@0 | 1295 | * if (this use of x is a declaration) { |
michael@0 | 1296 | * if (x in pc->decls) { // redeclaring |
michael@0 | 1297 | * pn = allocate a PN_NAME ParseNode; |
michael@0 | 1298 | * } else { // defining |
michael@0 | 1299 | * dn = lookup x in pc->lexdeps; |
michael@0 | 1300 | * if (dn) // use before def |
michael@0 | 1301 | * remove x from pc->lexdeps; |
michael@0 | 1302 | * else // def before use |
michael@0 | 1303 | * dn = allocate a PN_NAME Definition; |
michael@0 | 1304 | * map x to dn via pc->decls; |
michael@0 | 1305 | * pn = dn; |
michael@0 | 1306 | * } |
michael@0 | 1307 | * insert pn into its parent PNK_VAR/PNK_CONST list; |
michael@0 | 1308 | * } else { |
michael@0 | 1309 | * pn = allocate a ParseNode for this reference to x; |
michael@0 | 1310 | * dn = lookup x in pc's lexical scope chain; |
michael@0 | 1311 | * if (!dn) { |
michael@0 | 1312 | * dn = lookup x in pc->lexdeps; |
michael@0 | 1313 | * if (!dn) { |
michael@0 | 1314 | * dn = pre-allocate a Definition for x; |
michael@0 | 1315 | * map x to dn in pc->lexdeps; |
michael@0 | 1316 | * } |
michael@0 | 1317 | * } |
michael@0 | 1318 | * append pn to dn's use chain; |
michael@0 | 1319 | * } |
michael@0 | 1320 | * } |
michael@0 | 1321 | * |
michael@0 | 1322 | * See frontend/BytecodeEmitter.h for js::ParseContext and its top*Stmt, |
michael@0 | 1323 | * decls, and lexdeps members. |
michael@0 | 1324 | * |
michael@0 | 1325 | * Notes: |
michael@0 | 1326 | * |
michael@0 | 1327 | * 0. To avoid bloating ParseNode, we steal a bit from pn_arity for pn_defn |
michael@0 | 1328 | * and set it on a ParseNode instead of allocating a Definition. |
michael@0 | 1329 | * |
michael@0 | 1330 | * 1. Due to hoisting, a definition cannot be eliminated even if its "Variable |
michael@0 | 1331 | * statement" (ECMA-262 12.2) can be proven to be dead code. RecycleTree in |
michael@0 | 1332 | * ParseNode.cpp will not recycle a node whose pn_defn bit is set. |
michael@0 | 1333 | * |
michael@0 | 1334 | * 2. "lookup x in pc's lexical scope chain" gives up on def/use chaining if a |
michael@0 | 1335 | * with statement is found along the the scope chain, which includes pc, |
michael@0 | 1336 | * pc->parent, etc. Thus we eagerly connect an inner function's use of an |
michael@0 | 1337 | * outer's var x if the var x was parsed before the inner function. |
michael@0 | 1338 | * |
michael@0 | 1339 | * 3. A use may be eliminated as dead by the constant folder, which therefore |
michael@0 | 1340 | * must remove the dead name node from its singly-linked use chain, which |
michael@0 | 1341 | * would mean hashing to find the definition node and searching to update |
michael@0 | 1342 | * the pn_link pointing at the use to be removed. This is costly, so as for |
michael@0 | 1343 | * dead definitions, we do not recycle dead pn_used nodes. |
michael@0 | 1344 | * |
michael@0 | 1345 | * At the end of parsing a function body or global or eval program, pc->lexdeps |
michael@0 | 1346 | * holds the lexical dependencies of the parsed unit. The name to def/use chain |
michael@0 | 1347 | * mappings are then merged into the parent pc->lexdeps. |
michael@0 | 1348 | * |
michael@0 | 1349 | * Thus if a later var x is parsed in the outer function satisfying an earlier |
michael@0 | 1350 | * inner function's use of x, we will remove dn from pc->lexdeps and re-use it |
michael@0 | 1351 | * as the new definition node in the outer function's parse tree. |
michael@0 | 1352 | * |
michael@0 | 1353 | * When the compiler unwinds from the outermost pc, pc->lexdeps contains the |
michael@0 | 1354 | * definition nodes with use chains for all free variables. These are either |
michael@0 | 1355 | * global variables or reference errors. |
michael@0 | 1356 | */ |
michael@0 | 1357 | #define dn_uses pn_link |
michael@0 | 1358 | |
michael@0 | 1359 | struct Definition : public ParseNode |
michael@0 | 1360 | { |
michael@0 | 1361 | bool isFreeVar() const { |
michael@0 | 1362 | JS_ASSERT(isDefn()); |
michael@0 | 1363 | return pn_cookie.isFree(); |
michael@0 | 1364 | } |
michael@0 | 1365 | |
michael@0 | 1366 | enum Kind { MISSING = 0, VAR, CONST, LET, ARG, NAMED_LAMBDA, PLACEHOLDER }; |
michael@0 | 1367 | |
michael@0 | 1368 | bool canHaveInitializer() { return int(kind()) <= int(ARG); } |
michael@0 | 1369 | |
michael@0 | 1370 | static const char *kindString(Kind kind); |
michael@0 | 1371 | |
michael@0 | 1372 | Kind kind() { |
michael@0 | 1373 | if (getKind() == PNK_FUNCTION) { |
michael@0 | 1374 | if (isOp(JSOP_GETARG)) |
michael@0 | 1375 | return ARG; |
michael@0 | 1376 | return VAR; |
michael@0 | 1377 | } |
michael@0 | 1378 | JS_ASSERT(getKind() == PNK_NAME); |
michael@0 | 1379 | if (isOp(JSOP_CALLEE)) |
michael@0 | 1380 | return NAMED_LAMBDA; |
michael@0 | 1381 | if (isPlaceholder()) |
michael@0 | 1382 | return PLACEHOLDER; |
michael@0 | 1383 | if (isOp(JSOP_GETARG)) |
michael@0 | 1384 | return ARG; |
michael@0 | 1385 | if (isConst()) |
michael@0 | 1386 | return CONST; |
michael@0 | 1387 | if (isLet()) |
michael@0 | 1388 | return LET; |
michael@0 | 1389 | return VAR; |
michael@0 | 1390 | } |
michael@0 | 1391 | }; |
michael@0 | 1392 | |
michael@0 | 1393 | class ParseNodeAllocator |
michael@0 | 1394 | { |
michael@0 | 1395 | public: |
michael@0 | 1396 | explicit ParseNodeAllocator(ExclusiveContext *cx, LifoAlloc &alloc) |
michael@0 | 1397 | : cx(cx), alloc(alloc), freelist(nullptr) |
michael@0 | 1398 | {} |
michael@0 | 1399 | |
michael@0 | 1400 | void *allocNode(); |
michael@0 | 1401 | void freeNode(ParseNode *pn); |
michael@0 | 1402 | ParseNode *freeTree(ParseNode *pn); |
michael@0 | 1403 | void prepareNodeForMutation(ParseNode *pn); |
michael@0 | 1404 | |
michael@0 | 1405 | private: |
michael@0 | 1406 | ExclusiveContext *cx; |
michael@0 | 1407 | LifoAlloc &alloc; |
michael@0 | 1408 | ParseNode *freelist; |
michael@0 | 1409 | }; |
michael@0 | 1410 | |
michael@0 | 1411 | inline bool |
michael@0 | 1412 | ParseNode::test(unsigned flag) const |
michael@0 | 1413 | { |
michael@0 | 1414 | JS_ASSERT(pn_defn || pn_arity == PN_CODE || pn_arity == PN_NAME); |
michael@0 | 1415 | #ifdef DEBUG |
michael@0 | 1416 | if ((flag & PND_ASSIGNED) && pn_defn && !(pn_dflags & flag)) { |
michael@0 | 1417 | for (ParseNode *pn = ((Definition *) this)->dn_uses; pn; pn = pn->pn_link) { |
michael@0 | 1418 | JS_ASSERT(!pn->pn_defn); |
michael@0 | 1419 | JS_ASSERT(!(pn->pn_dflags & flag)); |
michael@0 | 1420 | } |
michael@0 | 1421 | } |
michael@0 | 1422 | #endif |
michael@0 | 1423 | return !!(pn_dflags & flag); |
michael@0 | 1424 | } |
michael@0 | 1425 | |
michael@0 | 1426 | inline void |
michael@0 | 1427 | ParseNode::markAsAssigned() |
michael@0 | 1428 | { |
michael@0 | 1429 | JS_ASSERT(js_CodeSpec[pn_op].format & JOF_NAME); |
michael@0 | 1430 | if (isUsed()) |
michael@0 | 1431 | pn_lexdef->pn_dflags |= PND_ASSIGNED; |
michael@0 | 1432 | pn_dflags |= PND_ASSIGNED; |
michael@0 | 1433 | } |
michael@0 | 1434 | |
michael@0 | 1435 | inline Definition * |
michael@0 | 1436 | ParseNode::resolve() |
michael@0 | 1437 | { |
michael@0 | 1438 | if (isDefn()) |
michael@0 | 1439 | return (Definition *)this; |
michael@0 | 1440 | JS_ASSERT(lexdef()->isDefn()); |
michael@0 | 1441 | return (Definition *)lexdef(); |
michael@0 | 1442 | } |
michael@0 | 1443 | |
michael@0 | 1444 | inline bool |
michael@0 | 1445 | ParseNode::isConstant() |
michael@0 | 1446 | { |
michael@0 | 1447 | switch (pn_type) { |
michael@0 | 1448 | case PNK_NUMBER: |
michael@0 | 1449 | case PNK_STRING: |
michael@0 | 1450 | case PNK_NULL: |
michael@0 | 1451 | case PNK_FALSE: |
michael@0 | 1452 | case PNK_TRUE: |
michael@0 | 1453 | return true; |
michael@0 | 1454 | case PNK_ARRAY: |
michael@0 | 1455 | case PNK_OBJECT: |
michael@0 | 1456 | JS_ASSERT(isOp(JSOP_NEWINIT)); |
michael@0 | 1457 | return !(pn_xflags & PNX_NONCONST); |
michael@0 | 1458 | default: |
michael@0 | 1459 | return false; |
michael@0 | 1460 | } |
michael@0 | 1461 | } |
michael@0 | 1462 | |
michael@0 | 1463 | class ObjectBox |
michael@0 | 1464 | { |
michael@0 | 1465 | public: |
michael@0 | 1466 | JSObject *object; |
michael@0 | 1467 | |
michael@0 | 1468 | ObjectBox(JSObject *object, ObjectBox *traceLink); |
michael@0 | 1469 | bool isFunctionBox() { return object->is<JSFunction>(); } |
michael@0 | 1470 | FunctionBox *asFunctionBox(); |
michael@0 | 1471 | void trace(JSTracer *trc); |
michael@0 | 1472 | |
michael@0 | 1473 | protected: |
michael@0 | 1474 | friend struct CGObjectList; |
michael@0 | 1475 | |
michael@0 | 1476 | ObjectBox *traceLink; |
michael@0 | 1477 | ObjectBox *emitLink; |
michael@0 | 1478 | |
michael@0 | 1479 | ObjectBox(JSFunction *function, ObjectBox *traceLink); |
michael@0 | 1480 | }; |
michael@0 | 1481 | |
michael@0 | 1482 | enum ParseReportKind |
michael@0 | 1483 | { |
michael@0 | 1484 | ParseError, |
michael@0 | 1485 | ParseWarning, |
michael@0 | 1486 | ParseExtraWarning, |
michael@0 | 1487 | ParseStrictError |
michael@0 | 1488 | }; |
michael@0 | 1489 | |
michael@0 | 1490 | enum FunctionSyntaxKind { Expression, Statement, Arrow }; |
michael@0 | 1491 | |
michael@0 | 1492 | static inline ParseNode * |
michael@0 | 1493 | FunctionArgsList(ParseNode *fn, unsigned *numFormals) |
michael@0 | 1494 | { |
michael@0 | 1495 | JS_ASSERT(fn->isKind(PNK_FUNCTION)); |
michael@0 | 1496 | ParseNode *argsBody = fn->pn_body; |
michael@0 | 1497 | JS_ASSERT(argsBody->isKind(PNK_ARGSBODY)); |
michael@0 | 1498 | *numFormals = argsBody->pn_count; |
michael@0 | 1499 | if (*numFormals > 0 && argsBody->last()->isKind(PNK_STATEMENTLIST)) |
michael@0 | 1500 | (*numFormals)--; |
michael@0 | 1501 | JS_ASSERT(argsBody->isArity(PN_LIST)); |
michael@0 | 1502 | return argsBody->pn_head; |
michael@0 | 1503 | } |
michael@0 | 1504 | |
michael@0 | 1505 | } /* namespace frontend */ |
michael@0 | 1506 | } /* namespace js */ |
michael@0 | 1507 | |
michael@0 | 1508 | #endif /* frontend_ParseNode_h */ |