Bug 564076: Small parser cleanup changes. (r=mrbkap)
[mozilla-central.git] / js / src / jsparse.h
blob30c2d8b99724c0d1046b0d13016569016adea662
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is Mozilla Communicator client code, released
18 * March 31, 1998.
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
25 * Contributor(s):
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef jsparse_h___
42 #define jsparse_h___
44 * JS parser definitions.
46 #include "jsversion.h"
47 #include "jsprvtd.h"
48 #include "jspubtd.h"
49 #include "jsatom.h"
50 #include "jsscan.h"
52 JS_BEGIN_EXTERN_C
55 * Parsing builds a tree of nodes that directs code generation. This tree is
56 * not a concrete syntax tree in all respects (for example, || and && are left
57 * associative, but (A && B && C) translates into the right-associated tree
58 * <A && <B && C>> so that code generation can emit a left-associative branch
59 * around <B && C> when A is false). Nodes are labeled by token type, with a
60 * JSOp secondary label when needed:
62 * Label Variant Members
63 * ----- ------- -------
64 * <Definitions>
65 * TOK_FUNCTION name pn_funbox: ptr to JSFunctionBox holding function
66 * object containing arg and var properties. We
67 * create the function object at parse (not emit)
68 * time to specialize arg and var bytecodes early.
69 * pn_body: TOK_UPVARS if the function's source body
70 * depends on outer names, else TOK_ARGSBODY
71 * if formal parameters, else TOK_LC node for
72 * function body statements
73 * pn_cookie: static level and var index for function
74 * pn_dflags: PND_* definition/use flags (see below)
75 * pn_blockid: block id number
76 * TOK_ARGSBODY list list of formal parameters followed by TOK_LC node
77 * for function body statements as final element
78 * pn_count: 1 + number of formal parameters
79 * TOK_UPVARS nameset pn_names: lexical dependencies (JSDefinitions)
80 * defined in enclosing scopes, or ultimately not
81 * defined (free variables, either global property
82 * references or reference errors).
83 * pn_tree: TOK_ARGSBODY or TOK_LC node
85 * <Statements>
86 * TOK_LC list pn_head: list of pn_count statements
87 * TOK_IF ternary pn_kid1: cond, pn_kid2: then, pn_kid3: else or null
88 * TOK_SWITCH binary pn_left: discriminant
89 * pn_right: list of TOK_CASE nodes, with at most one
90 * TOK_DEFAULT node, or if there are let bindings
91 * in the top level of the switch body's cases, a
92 * TOK_LEXICALSCOPE node that contains the list of
93 * TOK_CASE nodes.
94 * TOK_CASE, binary pn_left: case expr or null if TOK_DEFAULT
95 * TOK_DEFAULT pn_right: TOK_LC node for this case's statements
96 * pn_val: constant value if lookup or table switch
97 * TOK_WHILE binary pn_left: cond, pn_right: body
98 * TOK_DO binary pn_left: body, pn_right: cond
99 * TOK_FOR binary pn_left: either
100 * for/in loop: a binary TOK_IN node with
101 * pn_left: TOK_VAR or TOK_NAME to left of 'in'
102 * if TOK_VAR, its pn_xflags may have PNX_POPVAR
103 * and PNX_FORINVAR bits set
104 * pn_right: object expr to right of 'in'
105 * for(;;) loop: a ternary TOK_RESERVED node with
106 * pn_kid1: init expr before first ';'
107 * pn_kid2: cond expr before second ';'
108 * pn_kid3: update expr after second ';'
109 * any kid may be null
110 * pn_right: body
111 * TOK_THROW unary pn_op: JSOP_THROW, pn_kid: exception
112 * TOK_TRY ternary pn_kid1: try block
113 * pn_kid2: null or TOK_RESERVED list of
114 * TOK_LEXICALSCOPE nodes, each with pn_expr pointing
115 * to a TOK_CATCH node
116 * pn_kid3: null or finally block
117 * TOK_CATCH ternary pn_kid1: TOK_NAME, TOK_RB, or TOK_RC catch var node
118 * (TOK_RB or TOK_RC if destructuring)
119 * pn_kid2: null or the catch guard expression
120 * pn_kid3: catch block statements
121 * TOK_BREAK name pn_atom: label or null
122 * TOK_CONTINUE name pn_atom: label or null
123 * TOK_WITH binary pn_left: head expr, pn_right: body
124 * TOK_VAR list pn_head: list of TOK_NAME or TOK_ASSIGN nodes
125 * each name node has
126 * pn_used: false
127 * pn_atom: variable name
128 * pn_expr: initializer or null
129 * each assignment node has
130 * pn_left: TOK_NAME with pn_used true and
131 * pn_lexdef (NOT pn_expr) set
132 * pn_right: initializer
133 * TOK_RETURN unary pn_kid: return expr or null
134 * TOK_SEMI unary pn_kid: expr or null statement
135 * TOK_COLON name pn_atom: label, pn_expr: labeled statement
137 * <Expressions>
138 * All left-associated binary trees of the same type are optimized into lists
139 * to avoid recursion when processing expression chains.
140 * TOK_COMMA list pn_head: list of pn_count comma-separated exprs
141 * TOK_ASSIGN binary pn_left: lvalue, pn_right: rvalue
142 * pn_op: JSOP_ADD for +=, etc.
143 * TOK_HOOK ternary pn_kid1: cond, pn_kid2: then, pn_kid3: else
144 * TOK_OR binary pn_left: first in || chain, pn_right: rest of chain
145 * TOK_AND binary pn_left: first in && chain, pn_right: rest of chain
146 * TOK_BITOR binary pn_left: left-assoc | expr, pn_right: ^ expr
147 * TOK_BITXOR binary pn_left: left-assoc ^ expr, pn_right: & expr
148 * TOK_BITAND binary pn_left: left-assoc & expr, pn_right: EQ expr
149 * TOK_EQOP binary pn_left: left-assoc EQ expr, pn_right: REL expr
150 * pn_op: JSOP_EQ, JSOP_NE,
151 * JSOP_STRICTEQ, JSOP_STRICTNE
152 * TOK_RELOP binary pn_left: left-assoc REL expr, pn_right: SH expr
153 * pn_op: JSOP_LT, JSOP_LE, JSOP_GT, JSOP_GE
154 * TOK_SHOP binary pn_left: left-assoc SH expr, pn_right: ADD expr
155 * pn_op: JSOP_LSH, JSOP_RSH, JSOP_URSH
156 * TOK_PLUS, binary pn_left: left-assoc ADD expr, pn_right: MUL expr
157 * pn_xflags: if a left-associated binary TOK_PLUS
158 * tree has been flattened into a list (see above
159 * under <Expressions>), pn_xflags will contain
160 * PNX_STRCAT if at least one list element is a
161 * string literal (TOK_STRING); if such a list has
162 * any non-string, non-number term, pn_xflags will
163 * contain PNX_CANTFOLD.
164 * pn_
165 * TOK_MINUS pn_op: JSOP_ADD, JSOP_SUB
166 * TOK_STAR, binary pn_left: left-assoc MUL expr, pn_right: UNARY expr
167 * TOK_DIVOP pn_op: JSOP_MUL, JSOP_DIV, JSOP_MOD
168 * TOK_UNARYOP unary pn_kid: UNARY expr, pn_op: JSOP_NEG, JSOP_POS,
169 * JSOP_NOT, JSOP_BITNOT, JSOP_TYPEOF, JSOP_VOID
170 * TOK_INC, unary pn_kid: MEMBER expr
171 * TOK_DEC
172 * TOK_NEW list pn_head: list of ctor, arg1, arg2, ... argN
173 * pn_count: 1 + N (where N is number of args)
174 * ctor is a MEMBER expr
175 * TOK_DELETE unary pn_kid: MEMBER expr
176 * TOK_DOT, name pn_expr: MEMBER expr to left of .
177 * TOK_DBLDOT pn_atom: name to right of .
178 * TOK_LB binary pn_left: MEMBER expr to left of [
179 * pn_right: expr between [ and ]
180 * TOK_LP list pn_head: list of call, arg1, arg2, ... argN
181 * pn_count: 1 + N (where N is number of args)
182 * call is a MEMBER expr naming a callable object
183 * TOK_RB list pn_head: list of pn_count array element exprs
184 * [,,] holes are represented by TOK_COMMA nodes
185 * pn_xflags: PN_ENDCOMMA if extra comma at end
186 * TOK_RC list pn_head: list of pn_count binary TOK_COLON nodes
187 * TOK_COLON binary key-value pair in object initializer or
188 * destructuring lhs
189 * pn_left: property id, pn_right: value
190 * var {x} = object destructuring shorthand shares
191 * PN_NAME node for x on left and right of TOK_COLON
192 * node in TOK_RC's list, has PNX_DESTRUCT flag
193 * TOK_DEFSHARP unary pn_num: jsint value of n in #n=
194 * pn_kid: primary function, paren, name, object or
195 * array literal expressions
196 * TOK_USESHARP nullary pn_num: jsint value of n in #n#
197 * TOK_NAME, name pn_atom: name, string, or object atom
198 * TOK_STRING, pn_op: JSOP_NAME, JSOP_STRING, or JSOP_OBJECT, or
199 * JSOP_REGEXP
200 * TOK_REGEXP If JSOP_NAME, pn_op may be JSOP_*ARG or JSOP_*VAR
201 * with pn_cookie telling (staticLevel, slot) (see
202 * jsscript.h's UPVAR macros) and pn_dflags telling
203 * const-ness and static analysis results
204 * TOK_NAME name If pn_used, TOK_NAME uses the lexdef member instead
205 * of the expr member it overlays
206 * TOK_NUMBER dval pn_dval: double value of numeric literal
207 * TOK_PRIMARY nullary pn_op: JSOp bytecode
209 * <E4X node descriptions>
210 * TOK_ANYNAME nullary pn_op: JSOP_ANYNAME
211 * pn_atom: cx->runtime->atomState.starAtom
212 * TOK_AT unary pn_op: JSOP_TOATTRNAME; pn_kid attribute id/expr
213 * TOK_DBLCOLON binary pn_op: JSOP_QNAME
214 * pn_left: TOK_ANYNAME or TOK_NAME node
215 * pn_right: TOK_STRING "*" node, or expr within []
216 * name pn_op: JSOP_QNAMECONST
217 * pn_expr: TOK_ANYNAME or TOK_NAME left operand
218 * pn_atom: name on right of ::
219 * TOK_XMLELEM list XML element node
220 * pn_head: start tag, content1, ... contentN, end tag
221 * pn_count: 2 + N where N is number of content nodes
222 * N may be > x.length() if {expr} embedded
223 * TOK_XMLLIST list XML list node
224 * pn_head: content1, ... contentN
225 * TOK_XMLSTAGO, list XML start, end, and point tag contents
226 * TOK_XMLETAGC, pn_head: tag name or {expr}, ... XML attrs ...
227 * TOK_XMLPTAGO
228 * TOK_XMLNAME nullary pn_atom: XML name, with no {expr} embedded
229 * TOK_XMLNAME list pn_head: tag name or {expr}, ... name or {expr}
230 * TOK_XMLATTR, nullary pn_atom: attribute value string; pn_op: JSOP_STRING
231 * TOK_XMLCDATA,
232 * TOK_XMLCOMMENT
233 * TOK_XMLPI nullary pn_atom: XML processing instruction target
234 * pn_atom2: XML PI content, or null if no content
235 * TOK_XMLTEXT nullary pn_atom: marked-up text, or null if empty string
236 * TOK_LC unary {expr} in XML tag or content; pn_kid is expr
238 * So an XML tag with no {expr} and three attributes is a list with the form:
240 * (tagname attrname1 attrvalue1 attrname2 attrvalue2 attrname2 attrvalue3)
242 * An XML tag with embedded expressions like so:
244 * <name1{expr1} name2{expr2}name3={expr3}>
246 * would have the form:
248 * ((name1 {expr1}) (name2 {expr2} name3) {expr3})
250 * where () bracket a list with elements separated by spaces, and {expr} is a
251 * TOK_LC unary node with expr as its kid.
253 * Thus, the attribute name/value pairs occupy successive odd and even list
254 * locations, where pn_head is the TOK_XMLNAME node at list location 0. The
255 * parser builds the same sort of structures for elements:
257 * <a x={x}>Hi there!<b y={y}>How are you?</b><answer>{x + y}</answer></a>
259 * translates to:
261 * ((a x {x}) 'Hi there!' ((b y {y}) 'How are you?') ((answer) {x + y}))
263 * <Non-E4X node descriptions, continued>
265 * Label Variant Members
266 * ----- ------- -------
267 * TOK_LEXICALSCOPE name pn_op: JSOP_LEAVEBLOCK or JSOP_LEAVEBLOCKEXPR
268 * pn_objbox: block object in JSObjectBox holder
269 * pn_expr: block body
270 * TOK_ARRAYCOMP list pn_head: list of pn_count (1 or 2) elements
271 * if pn_count is 2, first element is #n=[...]
272 * last element is block enclosing for loop(s)
273 * and optionally if-guarded TOK_ARRAYPUSH
274 * TOK_ARRAYPUSH unary pn_op: JSOP_ARRAYCOMP
275 * pn_kid: array comprehension expression
277 typedef enum JSParseNodeArity {
278 PN_NULLARY, /* 0 kids, only pn_atom/pn_dval/etc. */
279 PN_UNARY, /* one kid, plus a couple of scalars */
280 PN_BINARY, /* two kids, plus a couple of scalars */
281 PN_TERNARY, /* three kids */
282 PN_FUNC, /* function definition node */
283 PN_LIST, /* generic singly linked list */
284 PN_NAME, /* name use or definition node */
285 PN_NAMESET /* JSAtomList + JSParseNode ptr */
286 } JSParseNodeArity;
288 struct JSDefinition;
290 struct JSParseNode {
291 uint32 pn_type:16, /* TOK_* type, see jsscan.h */
292 pn_op:8, /* see JSOp enum and jsopcode.tbl */
293 pn_arity:5, /* see JSParseNodeArity enum */
294 pn_parens:1, /* this expr was enclosed in parens */
295 pn_used:1, /* name node is on a use-chain */
296 pn_defn:1; /* this node is a JSDefinition */
298 #define PN_OP(pn) ((JSOp)(pn)->pn_op)
299 #define PN_TYPE(pn) ((js::TokenKind)(pn)->pn_type)
301 js::TokenPos pn_pos; /* two 16-bit pairs here, for 64 bits */
302 int32 pn_offset; /* first generated bytecode offset */
303 JSParseNode *pn_next; /* intrinsic link in parent PN_LIST */
304 JSParseNode *pn_link; /* def/use link (alignment freebie);
305 also links JSFunctionBox::methods
306 lists of would-be |this| methods */
307 union {
308 struct { /* list of next-linked nodes */
309 JSParseNode *head; /* first node in list */
310 JSParseNode **tail; /* ptr to ptr to last node in list */
311 uint32 count; /* number of nodes in list */
312 uint32 xflags:12, /* extra flags, see below */
313 blockid:20; /* see name variant below */
314 } list;
315 struct { /* ternary: if, for(;;), ?: */
316 JSParseNode *kid1; /* condition, discriminant, etc. */
317 JSParseNode *kid2; /* then-part, case list, etc. */
318 JSParseNode *kid3; /* else-part, default case, etc. */
319 } ternary;
320 struct { /* two kids if binary */
321 JSParseNode *left;
322 JSParseNode *right;
323 jsval val; /* switch case value */
324 uintN iflags; /* JSITER_* flags for TOK_FOR node */
325 } binary;
326 struct { /* one kid if unary */
327 JSParseNode *kid;
328 jsint num; /* -1 or sharp variable number */
329 JSBool hidden; /* hidden genexp-induced JSOP_YIELD */
330 } unary;
331 struct { /* name, labeled statement, etc. */
332 union {
333 JSAtom *atom; /* lexical name or label atom */
334 JSFunctionBox *funbox; /* function object */
335 JSObjectBox *objbox; /* block or regexp object */
337 union {
338 JSParseNode *expr; /* function body, var initializer, or
339 base object of TOK_DOT */
340 JSDefinition *lexdef; /* lexical definition for this use */
342 uint32 cookie; /* upvar cookie with absolute frame
343 level (not relative skip), possibly
344 in current frame */
345 uint32 dflags:12, /* definition/use flags, see below */
346 blockid:20; /* block number, for subset dominance
347 computation */
348 } name;
349 struct { /* lexical dependencies + sub-tree */
350 JSAtomSet names; /* set of names with JSDefinitions */
351 JSParseNode *tree; /* sub-tree containing name uses */
352 } nameset;
353 struct { /* PN_NULLARY variant for E4X */
354 JSAtom *atom; /* first atom in pair */
355 JSAtom *atom2; /* second atom in pair or null */
356 } apair;
357 jsdouble dval; /* aligned numeric literal value */
358 } pn_u;
360 #define pn_funbox pn_u.name.funbox
361 #define pn_body pn_u.name.expr
362 #define pn_cookie pn_u.name.cookie
363 #define pn_dflags pn_u.name.dflags
364 #define pn_blockid pn_u.name.blockid
365 #define pn_index pn_u.name.blockid /* reuse as object table index */
366 #define pn_head pn_u.list.head
367 #define pn_tail pn_u.list.tail
368 #define pn_count pn_u.list.count
369 #define pn_xflags pn_u.list.xflags
370 #define pn_kid1 pn_u.ternary.kid1
371 #define pn_kid2 pn_u.ternary.kid2
372 #define pn_kid3 pn_u.ternary.kid3
373 #define pn_left pn_u.binary.left
374 #define pn_right pn_u.binary.right
375 #define pn_val pn_u.binary.val
376 #define pn_iflags pn_u.binary.iflags
377 #define pn_kid pn_u.unary.kid
378 #define pn_num pn_u.unary.num
379 #define pn_hidden pn_u.unary.hidden
380 #define pn_atom pn_u.name.atom
381 #define pn_objbox pn_u.name.objbox
382 #define pn_expr pn_u.name.expr
383 #define pn_lexdef pn_u.name.lexdef
384 #define pn_names pn_u.nameset.names
385 #define pn_tree pn_u.nameset.tree
386 #define pn_dval pn_u.dval
387 #define pn_atom2 pn_u.apair.atom2
389 protected:
390 void inline init(js::TokenKind type, JSOp op, JSParseNodeArity arity) {
391 pn_type = type;
392 pn_op = op;
393 pn_arity = arity;
394 pn_parens = false;
395 JS_ASSERT(!pn_used);
396 JS_ASSERT(!pn_defn);
397 pn_next = pn_link = NULL;
400 static JSParseNode *create(JSParseNodeArity arity, JSTreeContext *tc);
402 public:
403 static JSParseNode *newBinaryOrAppend(js::TokenKind tt, JSOp op, JSParseNode *left,
404 JSParseNode *right, JSTreeContext *tc);
407 * The pn_expr and lexdef members are arms of an unsafe union. Unless you
408 * know exactly what you're doing, use only the following methods to access
409 * them. For less overhead and assertions for protection, use pn->expr()
410 * and pn->lexdef(). Otherwise, use pn->maybeExpr() and pn->maybeLexDef().
412 JSParseNode *expr() const {
413 JS_ASSERT(!pn_used);
414 JS_ASSERT(pn_arity == PN_NAME || pn_arity == PN_FUNC);
415 return pn_expr;
418 JSDefinition *lexdef() const {
419 JS_ASSERT(pn_used || isDeoptimized());
420 JS_ASSERT(pn_arity == PN_NAME);
421 return pn_lexdef;
424 JSParseNode *maybeExpr() { return pn_used ? NULL : expr(); }
425 JSDefinition *maybeLexDef() { return pn_used ? lexdef() : NULL; }
427 /* PN_FUNC and PN_NAME pn_dflags bits. */
428 #define PND_LET 0x01 /* let (block-scoped) binding */
429 #define PND_CONST 0x02 /* const binding (orthogonal to let) */
430 #define PND_INITIALIZED 0x04 /* initialized declaration */
431 #define PND_ASSIGNED 0x08 /* set if ever LHS of assignment */
432 #define PND_TOPLEVEL 0x10 /* function at top of body or prog */
433 #define PND_BLOCKCHILD 0x20 /* use or def is direct block child */
434 #define PND_GVAR 0x40 /* gvar binding, can't close over
435 because it could be deleted */
436 #define PND_PLACEHOLDER 0x80 /* placeholder definition for lexdep */
437 #define PND_FUNARG 0x100 /* downward or upward funarg usage */
438 #define PND_BOUND 0x200 /* bound to a stack or global slot */
439 #define PND_DEOPTIMIZED 0x400 /* former pn_used name node, pn_lexdef
440 still valid, but this use no longer
441 optimizable via an upvar opcode */
443 /* Flags to propagate from uses to definition. */
444 #define PND_USE2DEF_FLAGS (PND_ASSIGNED | PND_FUNARG)
446 /* PN_LIST pn_xflags bits. */
447 #define PNX_STRCAT 0x01 /* TOK_PLUS list has string term */
448 #define PNX_CANTFOLD 0x02 /* TOK_PLUS list has unfoldable term */
449 #define PNX_POPVAR 0x04 /* TOK_VAR last result needs popping */
450 #define PNX_FORINVAR 0x08 /* TOK_VAR is left kid of TOK_IN node,
451 which is left kid of TOK_FOR */
452 #define PNX_ENDCOMMA 0x10 /* array literal has comma at end */
453 #define PNX_XMLROOT 0x20 /* top-most node in XML literal tree */
454 #define PNX_GROUPINIT 0x40 /* var [a, b] = [c, d]; unit list */
455 #define PNX_NEEDBRACES 0x80 /* braces necessary due to closure */
456 #define PNX_FUNCDEFS 0x100 /* contains top-level function
457 statements */
458 #define PNX_DESTRUCT 0x200 /* destructuring special cases:
459 1. shorthand syntax used, at present
460 object destructuring ({x,y}) only;
461 2. the first child of function body
462 is code evaluating destructuring
463 arguments */
464 #define PNX_HOLEY 0x400 /* array initialiser has holes */
466 uintN frameLevel() const {
467 JS_ASSERT(pn_arity == PN_FUNC || pn_arity == PN_NAME);
468 return UPVAR_FRAME_SKIP(pn_cookie);
471 uintN frameSlot() const {
472 JS_ASSERT(pn_arity == PN_FUNC || pn_arity == PN_NAME);
473 return UPVAR_FRAME_SLOT(pn_cookie);
476 inline bool test(uintN flag) const;
478 bool isLet() const { return test(PND_LET); }
479 bool isConst() const { return test(PND_CONST); }
480 bool isInitialized() const { return test(PND_INITIALIZED); }
481 bool isTopLevel() const { return test(PND_TOPLEVEL); }
482 bool isBlockChild() const { return test(PND_BLOCKCHILD); }
483 bool isPlaceholder() const { return test(PND_PLACEHOLDER); }
484 bool isDeoptimized() const { return test(PND_DEOPTIMIZED); }
485 bool isAssigned() const { return test(PND_ASSIGNED); }
486 bool isFunArg() const { return test(PND_FUNARG); }
488 /* Defined below, see after struct JSDefinition. */
489 void setFunArg();
491 void become(JSParseNode *pn2);
492 void clear();
494 /* True if pn is a parsenode representing a literal constant. */
495 bool isLiteral() const {
496 return PN_TYPE(this) == js::TOK_NUMBER ||
497 PN_TYPE(this) == js::TOK_STRING ||
498 (PN_TYPE(this) == js::TOK_PRIMARY && PN_OP(this) != JSOP_THIS);
502 * True if this statement node could be a member of a Directive
503 * Prologue. Note that the prologue may contain strings that
504 * cannot themselves be directives; that's a stricter test.
505 * If Statement begins to simplify trees into this form, then
506 * we'll need additional flags that we can test here.
508 bool isDirectivePrologueMember() const {
509 if (PN_TYPE(this) == js::TOK_SEMI) {
510 JS_ASSERT(pn_arity == PN_UNARY);
511 JSParseNode *kid = pn_kid;
512 return kid && PN_TYPE(kid) == js::TOK_STRING && !kid->pn_parens;
514 return false;
518 * True if this node, known to be a Directive Prologue member,
519 * could be a directive itself.
521 bool isDirective() const {
522 JS_ASSERT(isDirectivePrologueMember());
523 JSParseNode *kid = pn_kid;
524 JSString *str = ATOM_TO_STRING(kid->pn_atom);
527 * Directives must contain no EscapeSequences or LineContinuations.
528 * If the string's length in the source code is its length as a value,
529 * accounting for the quotes, then it qualifies.
531 return (pn_pos.begin.lineno == pn_pos.end.lineno &&
532 pn_pos.begin.index + str->length() + 2 == pn_pos.end.index);
536 * Compute a pointer to the last element in a singly-linked list. NB: list
537 * must be non-empty for correct PN_LAST usage -- this is asserted!
539 JSParseNode *last() const {
540 JS_ASSERT(pn_arity == PN_LIST);
541 JS_ASSERT(pn_count != 0);
542 return (JSParseNode *)((char *)pn_tail - offsetof(JSParseNode, pn_next));
545 void makeEmpty() {
546 JS_ASSERT(pn_arity == PN_LIST);
547 pn_head = NULL;
548 pn_tail = &pn_head;
549 pn_count = 0;
550 pn_xflags = 0;
551 pn_blockid = 0;
554 void initList(JSParseNode *pn) {
555 JS_ASSERT(pn_arity == PN_LIST);
556 pn_head = pn;
557 pn_tail = &pn->pn_next;
558 pn_count = 1;
559 pn_xflags = 0;
560 pn_blockid = 0;
563 void append(JSParseNode *pn) {
564 JS_ASSERT(pn_arity == PN_LIST);
565 *pn_tail = pn;
566 pn_tail = &pn->pn_next;
567 pn_count++;
571 namespace js {
573 struct NullaryNode : public JSParseNode {
574 static inline NullaryNode *create(JSTreeContext *tc) {
575 return (NullaryNode *)JSParseNode::create(PN_NULLARY, tc);
579 struct UnaryNode : public JSParseNode {
580 static inline UnaryNode *create(JSTreeContext *tc) {
581 return (UnaryNode *)JSParseNode::create(PN_UNARY, tc);
585 struct BinaryNode : public JSParseNode {
586 static inline BinaryNode *create(JSTreeContext *tc) {
587 return (BinaryNode *)JSParseNode::create(PN_BINARY, tc);
591 struct TernaryNode : public JSParseNode {
592 static inline TernaryNode *create(JSTreeContext *tc) {
593 return (TernaryNode *)JSParseNode::create(PN_TERNARY, tc);
597 struct ListNode : public JSParseNode {
598 static inline ListNode *create(JSTreeContext *tc) {
599 return (ListNode *)JSParseNode::create(PN_LIST, tc);
603 struct FunctionNode : public JSParseNode {
604 static inline FunctionNode *create(JSTreeContext *tc) {
605 return (FunctionNode *)JSParseNode::create(PN_FUNC, tc);
609 struct NameNode : public JSParseNode {
610 static NameNode *create(JSAtom *atom, JSTreeContext *tc);
612 void inline initCommon(JSTreeContext *tc);
615 struct NameSetNode : public JSParseNode {
616 static inline NameSetNode *create(JSTreeContext *tc) {
617 return (NameSetNode *)JSParseNode::create(PN_NAMESET, tc);
621 struct LexicalScopeNode : public JSParseNode {
622 static inline LexicalScopeNode *create(JSTreeContext *tc) {
623 return (LexicalScopeNode *)JSParseNode::create(PN_NAME, tc);
627 } /* namespace js */
630 * JSDefinition is a degenerate subtype of the PN_FUNC and PN_NAME variants of
631 * JSParseNode, allocated only for function, var, const, and let declarations
632 * that define truly lexical bindings. This means that a child of a TOK_VAR
633 * list may be a JSDefinition instead of a JSParseNode. The pn_defn bit is set
634 * for all JSDefinitions, clear otherwise.
636 * Note that not all var declarations are definitions: JS allows multiple var
637 * declarations in a function or script, but only the first creates the hoisted
638 * binding. JS programmers do redeclare variables for good refactoring reasons,
639 * for example:
641 * function foo() {
642 * ...
643 * for (var i ...) ...;
644 * ...
645 * for (var i ...) ...;
646 * ...
649 * Not all definitions bind lexical variables, alas. In global and eval code
650 * var may re-declare a pre-existing property having any attributes, with or
651 * without JSPROP_PERMANENT. In eval code, indeed, ECMA-262 Editions 1 through
652 * 3 require function and var to bind deletable bindings. Global vars thus are
653 * properties of the global object, so they can be aliased even if they can't
654 * be deleted.
656 * Only bindings within function code may be treated as lexical, of course with
657 * the caveat that hoisting means use before initialization is allowed. We deal
658 * with use before declaration in one pass as follows (error checking elided):
660 * for (each use of unqualified name x in parse order) {
661 * if (this use of x is a declaration) {
662 * if (x in tc->decls) { // redeclaring
663 * pn = allocate a PN_NAME JSParseNode;
664 * } else { // defining
665 * dn = lookup x in tc->lexdeps;
666 * if (dn) // use before def
667 * remove x from tc->lexdeps;
668 * else // def before use
669 * dn = allocate a PN_NAME JSDefinition;
670 * map x to dn via tc->decls;
671 * pn = dn;
673 * insert pn into its parent TOK_VAR list;
674 * } else {
675 * pn = allocate a JSParseNode for this reference to x;
676 * dn = lookup x in tc's lexical scope chain;
677 * if (!dn) {
678 * dn = lookup x in tc->lexdeps;
679 * if (!dn) {
680 * dn = pre-allocate a JSDefinition for x;
681 * map x to dn in tc->lexdeps;
684 * append pn to dn's use chain;
688 * See jsemit.h for JSTreeContext and its top*Stmt, decls, and lexdeps members.
690 * Notes:
692 * 0. To avoid bloating JSParseNode, we steal a bit from pn_arity for pn_defn
693 * and set it on a JSParseNode instead of allocating a JSDefinition.
695 * 1. Due to hoisting, a definition cannot be eliminated even if its "Variable
696 * statement" (ECMA-262 12.2) can be proven to be dead code. RecycleTree in
697 * jsparse.cpp will not recycle a node whose pn_defn bit is set.
699 * 2. "lookup x in tc's lexical scope chain" gives up on def/use chaining if a
700 * with statement is found along the the scope chain, which includes tc,
701 * tc->parent, etc. Thus we eagerly connect an inner function's use of an
702 * outer's var x if the var x was parsed before the inner function.
704 * 3. A use may be eliminated as dead by the constant folder, which therefore
705 * must remove the dead name node from its singly-linked use chain, which
706 * would mean hashing to find the definition node and searching to update
707 * the pn_link pointing at the use to be removed. This is costly, so as for
708 * dead definitions, we do not recycle dead pn_used nodes.
710 * At the end of parsing a function body or global or eval program, tc->lexdeps
711 * holds the lexical dependencies of the parsed unit. The name to def/use chain
712 * mappings are then merged into the parent tc->lexdeps.
714 * Thus if a later var x is parsed in the outer function satisfying an earlier
715 * inner function's use of x, we will remove dn from tc->lexdeps and re-use it
716 * as the new definition node in the outer function's parse tree.
718 * When the compiler unwinds from the outermost tc, tc->lexdeps contains the
719 * definition nodes with use chains for all free variables. These are either
720 * global variables or reference errors.
722 * We analyze whether a binding is initialized, whether the bound names is ever
723 * assigned apart from its initializer, and if the bound name definition or use
724 * is in a direct child of a block. These PND_* flags allow a subset dominance
725 * computation telling whether an initialized var dominates its uses. An inner
726 * function using only such outer vars (and formal parameters) can be optimized
727 * into a flat closure. See JSOP_{GET,CALL}DSLOT.
729 * Another important subset dominance relation: ... { var x = ...; ... x ... }
730 * where x is not assigned after initialization and not used outside the block.
731 * This style is common in the absence of 'let'. Even though the var x is not
732 * at top level, we can tell its initialization dominates all uses cheaply,
733 * because the above one-pass algorithm sees the definition before any uses,
734 * and because all uses are contained in the same block as the definition.
736 * We also analyze function uses to flag upward/downward funargs, optimizing
737 * Algol-like (not passed as funargs, only ever called) lightweight functions
738 * using cx->display. See JSOP_{GET,CALL}UPVAR.
740 * This means that closure optimizations may be frustrated by with, eval, or
741 * assignment to an outer var. Such hard cases require heavyweight functions
742 * and JSOP_NAME, etc.
744 #define dn_uses pn_link
746 struct JSDefinition : public JSParseNode
749 * We store definition pointers in PN_NAMESET JSAtomLists in the AST, but
750 * due to redefinition these nodes may become uses of other definitions.
751 * This is unusual, so we simply chase the pn_lexdef link to find the final
752 * definition node. See methods called from Parser::analyzeFunctions.
754 * FIXME: MakeAssignment mutates for want of a parent link...
756 JSDefinition *resolve() {
757 JSParseNode *pn = this;
758 while (!pn->pn_defn) {
759 if (pn->pn_type == js::TOK_ASSIGN) {
760 pn = pn->pn_left;
761 continue;
763 pn = pn->lexdef();
765 return (JSDefinition *) pn;
768 bool isFreeVar() const {
769 JS_ASSERT(pn_defn);
770 return pn_cookie == FREE_UPVAR_COOKIE || test(PND_GVAR);
773 // Grr, windows.h or something under it #defines CONST...
774 #ifdef CONST
775 # undef CONST
776 #endif
777 enum Kind { VAR, CONST, LET, FUNCTION, ARG, UNKNOWN };
779 bool isBindingForm() { return int(kind()) <= int(LET); }
781 static const char *kindString(Kind kind);
783 Kind kind() {
784 if (PN_TYPE(this) == js::TOK_FUNCTION)
785 return FUNCTION;
786 JS_ASSERT(PN_TYPE(this) == js::TOK_NAME);
787 if (PN_OP(this) == JSOP_NOP)
788 return UNKNOWN;
789 if (PN_OP(this) == JSOP_GETARG)
790 return ARG;
791 if (isConst())
792 return CONST;
793 if (isLet())
794 return LET;
795 return VAR;
799 inline bool
800 JSParseNode::test(uintN flag) const
802 JS_ASSERT(pn_defn || pn_arity == PN_FUNC || pn_arity == PN_NAME);
803 #ifdef DEBUG
804 if ((flag & (PND_ASSIGNED | PND_FUNARG)) && pn_defn && !(pn_dflags & flag)) {
805 for (JSParseNode *pn = ((JSDefinition *) this)->dn_uses; pn; pn = pn->pn_link) {
806 JS_ASSERT(!pn->pn_defn);
807 JS_ASSERT(!(pn->pn_dflags & flag));
810 #endif
811 return !!(pn_dflags & flag);
814 inline void
815 JSParseNode::setFunArg()
818 * pn_defn NAND pn_used must be true, per this chart:
820 * pn_defn pn_used
821 * 0 0 anonymous function used implicitly, e.g. by
822 * hidden yield in a genexp
823 * 0 1 a use of a definition or placeholder
824 * 1 0 a definition or placeholder
825 * 1 1 error: this case must not be possible
827 JS_ASSERT(!(pn_defn & pn_used));
828 if (pn_used)
829 pn_lexdef->pn_dflags |= PND_FUNARG;
830 pn_dflags |= PND_FUNARG;
833 struct JSObjectBox {
834 JSObjectBox *traceLink;
835 JSObjectBox *emitLink;
836 JSObject *object;
839 #define JSFB_LEVEL_BITS 14
841 struct JSFunctionBox : public JSObjectBox
843 JSParseNode *node;
844 JSFunctionBox *siblings;
845 JSFunctionBox *kids;
846 JSFunctionBox *parent;
847 JSParseNode *methods; /* would-be methods set on this;
848 these nodes are linked via
849 pn_link, since lambdas are
850 neither definitions nor uses
851 of a binding */
852 uint32 queued:1,
853 inLoop:1, /* in a loop in parent function */
854 level:JSFB_LEVEL_BITS;
855 uint32 tcflags;
857 bool joinable() const;
860 * Unbrand an object being initialized or constructed if any method cannot
861 * be joined to one compiler-created null closure shared among N different
862 * closure environments.
864 * We despecialize from caching function objects, caching slots or sprops
865 * instead, because an unbranded object may still have joined methods (for
866 * which sprop->isMethod), since PropertyCache::fill gives precedence to
867 * joined methods over branded methods.
869 bool shouldUnbrand(uintN methods, uintN slowMethods) const;
872 struct JSFunctionBoxQueue {
873 JSFunctionBox **vector;
874 size_t head, tail;
875 size_t lengthMask;
877 size_t count() { return head - tail; }
878 size_t length() { return lengthMask + 1; }
880 JSFunctionBoxQueue()
881 : vector(NULL), head(0), tail(0), lengthMask(0) { }
883 bool init(uint32 count) {
884 lengthMask = JS_BITMASK(JS_CeilingLog2(count));
885 vector = new JSFunctionBox*[length()];
886 return !!vector;
889 ~JSFunctionBoxQueue() { delete[] vector; }
891 void push(JSFunctionBox *funbox) {
892 if (!funbox->queued) {
893 JS_ASSERT(count() < length());
894 vector[head++ & lengthMask] = funbox;
895 funbox->queued = true;
899 JSFunctionBox *pull() {
900 if (tail == head)
901 return NULL;
902 JS_ASSERT(tail < head);
903 JSFunctionBox *funbox = vector[tail++ & lengthMask];
904 funbox->queued = false;
905 return funbox;
909 #define NUM_TEMP_FREELISTS 6U /* 32 to 2048 byte size classes (32 bit) */
911 typedef struct BindData BindData;
913 namespace js {
915 struct Parser : private js::AutoGCRooter
917 JSContext * const context; /* FIXME Bug 551291: use AutoGCRooter::context? */
918 JSAtomListElement *aleFreeList;
919 void *tempFreeList[NUM_TEMP_FREELISTS];
920 js::TokenStream tokenStream;
921 void *tempPoolMark; /* initial JSContext.tempPool mark */
922 JSPrincipals *principals; /* principals associated with source */
923 JSStackFrame *const callerFrame; /* scripted caller frame for eval and dbgapi */
924 JSObject *const callerVarObj; /* callerFrame's varObj */
925 JSParseNode *nodeList; /* list of recyclable parse-node structs */
926 uint32 functionCount; /* number of functions in current unit */
927 JSObjectBox *traceListHead; /* list of parsed object for GC tracing */
928 JSTreeContext *tc; /* innermost tree context (stack-allocated) */
930 /* Root atoms and objects allocated for the parsed tree. */
931 js::AutoKeepAtoms keepAtoms;
933 Parser(JSContext *cx, JSPrincipals *prin = NULL, JSStackFrame *cfp = NULL)
934 : js::AutoGCRooter(cx, PARSER), context(cx),
935 aleFreeList(NULL), tokenStream(cx), principals(NULL), callerFrame(cfp),
936 callerVarObj(cfp ? cfp->varobj(cx->containingCallStack(cfp)) : NULL),
937 nodeList(NULL), functionCount(0), traceListHead(NULL), tc(NULL),
938 keepAtoms(cx->runtime)
940 js::PodArrayZero(tempFreeList);
941 setPrincipals(prin);
942 JS_ASSERT_IF(cfp, cfp->script);
945 ~Parser();
947 friend void js::AutoGCRooter::trace(JSTracer *trc);
948 friend struct ::JSTreeContext;
949 friend struct Compiler;
952 * Initialize a parser. Parameters are passed on to init tokenStream.
953 * The compiler owns the arena pool "tops-of-stack" space above the current
954 * JSContext.tempPool mark. This means you cannot allocate from tempPool
955 * and save the pointer beyond the next Parser destructor invocation.
957 bool init(const jschar *base, size_t length,
958 FILE *fp, const char *filename, uintN lineno);
960 void setPrincipals(JSPrincipals *prin);
963 * Parse a top-level JS script.
965 JSParseNode *parse(JSObject *chain);
967 #if JS_HAS_XML_SUPPORT
968 JSParseNode *parseXMLText(JSObject *chain, bool allowList);
969 #endif
972 * Allocate a new parsed object or function container from cx->tempPool.
974 JSObjectBox *newObjectBox(JSObject *obj);
976 JSFunctionBox *newFunctionBox(JSObject *obj, JSParseNode *fn, JSTreeContext *tc);
979 * Create a new function object given tree context (tc), optional name
980 * (atom may be null) and lambda flag (JSFUN_LAMBDA or 0).
982 JSFunction *newFunction(JSTreeContext *tc, JSAtom *atom, uintN lambda);
985 * Analyze the tree of functions nested within a single compilation unit,
986 * starting at funbox, recursively walking its kids, then following its
987 * siblings, their kids, etc.
989 bool analyzeFunctions(JSFunctionBox *funbox, uint32& tcflags);
990 bool markFunArgs(JSFunctionBox *funbox, uintN tcflags);
991 void setFunctionKinds(JSFunctionBox *funbox, uint32& tcflags);
993 void trace(JSTracer *trc);
996 * Report a parse (compile) error.
998 inline bool reportErrorNumber(JSParseNode *pn, uintN flags, uintN errorNumber, ...);
1000 private:
1002 * JS parsers, from lowest to highest precedence.
1004 * Each parser must be called during the dynamic scope of a JSTreeContext
1005 * object, pointed to by this->tc.
1007 * Each returns a parse node tree or null on error.
1009 JSParseNode *functionStmt();
1010 JSParseNode *functionExpr();
1011 JSParseNode *statements();
1012 JSParseNode *statement();
1013 JSParseNode *variables(bool inLetHead);
1014 JSParseNode *expr();
1015 JSParseNode *assignExpr();
1016 JSParseNode *condExpr();
1017 JSParseNode *orExpr();
1018 JSParseNode *andExpr();
1019 JSParseNode *bitOrExpr();
1020 JSParseNode *bitXorExpr();
1021 JSParseNode *bitAndExpr();
1022 JSParseNode *eqExpr();
1023 JSParseNode *relExpr();
1024 JSParseNode *shiftExpr();
1025 JSParseNode *addExpr();
1026 JSParseNode *mulExpr();
1027 JSParseNode *unaryExpr();
1028 JSParseNode *memberExpr(JSBool allowCallSyntax);
1029 JSParseNode *primaryExpr(js::TokenKind tt, JSBool afterDot);
1030 JSParseNode *parenExpr(JSParseNode *pn1, JSBool *genexp);
1033 * Additional JS parsers.
1035 bool recognizeDirectivePrologue(JSParseNode *pn);
1036 JSParseNode *functionBody();
1037 JSParseNode *functionDef(uintN lambda, bool namePermitted);
1038 JSParseNode *condition();
1039 JSParseNode *comprehensionTail(JSParseNode *kid, uintN blockid,
1040 js::TokenKind type = js::TOK_SEMI, JSOp op = JSOP_NOP);
1041 JSParseNode *generatorExpr(JSParseNode *pn, JSParseNode *kid);
1042 JSBool argumentList(JSParseNode *listNode);
1043 JSParseNode *bracketedExpr();
1044 JSParseNode *letBlock(JSBool statement);
1045 JSParseNode *returnOrYield(bool useAssignExpr);
1046 JSParseNode *destructuringExpr(BindData *data, js::TokenKind tt);
1048 #if JS_HAS_XML_SUPPORT
1049 JSParseNode *endBracketedExpr();
1051 JSParseNode *propertySelector();
1052 JSParseNode *qualifiedSuffix(JSParseNode *pn);
1053 JSParseNode *qualifiedIdentifier();
1054 JSParseNode *attributeIdentifier();
1055 JSParseNode *xmlExpr(JSBool inTag);
1056 JSParseNode *xmlAtomNode();
1057 JSParseNode *xmlNameExpr();
1058 JSParseNode *xmlTagContent(js::TokenKind tagtype, JSAtom **namep);
1059 JSBool xmlElementContent(JSParseNode *pn);
1060 JSParseNode *xmlElementOrList(JSBool allowList);
1061 JSParseNode *xmlElementOrListRoot(JSBool allowList);
1062 #endif /* JS_HAS_XML_SUPPORT */
1065 inline bool
1066 Parser::reportErrorNumber(JSParseNode *pn, uintN flags, uintN errorNumber, ...)
1068 va_list args;
1069 va_start(args, errorNumber);
1070 bool result = tokenStream.reportCompileErrorNumberVA(pn, flags, errorNumber, args);
1071 va_end(args);
1072 return result;
1075 struct Compiler
1077 Parser parser;
1079 Compiler(JSContext *cx, JSPrincipals *prin = NULL, JSStackFrame *cfp = NULL)
1080 : parser(cx, prin, cfp)
1085 * Initialize a compiler. Parameters are passed on to init parser.
1087 inline bool
1088 init(const jschar *base, size_t length,
1089 FILE *fp, const char *filename, uintN lineno)
1091 return parser.init(base, length, fp, filename, lineno);
1094 static bool
1095 compileFunctionBody(JSContext *cx, JSFunction *fun, JSPrincipals *principals,
1096 const jschar *chars, size_t length,
1097 const char *filename, uintN lineno);
1099 static JSScript *
1100 compileScript(JSContext *cx, JSObject *scopeChain, JSStackFrame *callerFrame,
1101 JSPrincipals *principals, uint32 tcflags,
1102 const jschar *chars, size_t length,
1103 FILE *file, const char *filename, uintN lineno,
1104 JSString *source = NULL,
1105 unsigned staticLevel = 0);
1108 } /* namespace js */
1111 * Convenience macro to access Parser.tokenStream as a pointer.
1113 #define TS(p) (&(p)->tokenStream)
1115 extern JSBool
1116 js_FoldConstants(JSContext *cx, JSParseNode *pn, JSTreeContext *tc,
1117 bool inCond = false);
1119 JS_END_EXTERN_C
1121 #endif /* jsparse_h___ */