1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sw=4 et tw=78:
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
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/
11  *
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.
16  *
17  * The Original Code is Mozilla Communicator client code, released
18  * March 31, 1998.
19  *
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.
24  *
25  * Contributor(s):
26  *
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.
38  *
39  * ***** END LICENSE BLOCK ***** */
40 
41 #ifndef jsparse_h___
42 #define jsparse_h___
43 /*
44  * JS parser definitions.
45  */
46 #include "jsconfig.h"
47 #include "jsprvtd.h"
48 #include "jspubtd.h"
49 #include "jsscan.h"
50 
51 JS_BEGIN_EXTERN_C
52 
53 /*
54  * Parsing builds a tree of nodes that directs code generation.  This tree is
55  * not a concrete syntax tree in all respects (for example, || and && are left
56  * associative, but (A && B && C) translates into the right-associated tree
57  * <A && <B && C>> so that code generation can emit a left-associative branch
58  * around <B && C> when A is false).  Nodes are labeled by token type, with a
59  * JSOp secondary label when needed:
60  *
61  * Label        Variant     Members
62  * -----        -------     -------
63  * <Definitions>
64  * TOK_FUNCTION func        pn_funAtom: atom holding function object containing
65  *                            arg and var properties.  We create the function
66  *                            object at parse (not emit) time to specialize arg
67  *                            and var bytecodes early.
68  *                          pn_body: TOK_LC node for function body statements
69  *                          pn_flags: TCF_FUN_* flags (see jsemit.h) collected
70  *                            while parsing the function's body
71  *                          pn_tryCount: of try statements in function
72  *
73  * <Statements>
74  * TOK_LC       list        pn_head: list of pn_count statements
75  * TOK_EXPORT   list        pn_head: list of pn_count TOK_NAMEs or one TOK_STAR
76  *                            (which is not a multiply node)
77  * TOK_IMPORT   list        pn_head: list of pn_count sub-trees of the form
78  *                            a.b.*, a[b].*, a.*, a.b, or a[b] -- but never a.
79  *                            Each member is expressed with TOK_DOT or TOK_LB.
80  *                            Each sub-tree's root node has a pn_op in the set
81  *                            JSOP_IMPORT{ALL,PROP,ELEM}
82  * TOK_IF       ternary     pn_kid1: cond, pn_kid2: then, pn_kid3: else or null
83  * TOK_SWITCH   binary      pn_left: discriminant
84  *                          pn_right: list of TOK_CASE nodes, with at most one
85  *                            TOK_DEFAULT node, or if there are let bindings
86  *                            in the top level of the switch body's cases, a
87  *                            TOK_LEXICALSCOPE node that contains the list of
88  *                            TOK_CASE nodes.
89  * TOK_CASE,    binary      pn_left: case expr or null if TOK_DEFAULT
90  * TOK_DEFAULT              pn_right: TOK_LC node for this case's statements
91  *                          pn_val: constant value if lookup or table switch
92  * TOK_WHILE    binary      pn_left: cond, pn_right: body
93  * TOK_DO       binary      pn_left: body, pn_right: cond
94  * TOK_FOR      binary      pn_left: either
95  *                            for/in loop: a binary TOK_IN node with
96  *                              pn_left:  TOK_VAR or TOK_NAME to left of 'in'
97  *                                if TOK_VAR, its pn_extra may have PNX_POPVAR
98  *                                and PNX_FORINVAR bits set
99  *                              pn_right: object expr to right of 'in'
100  *                            for(;;) loop: a ternary TOK_RESERVED node with
101  *                              pn_kid1:  init expr before first ';'
102  *                              pn_kid2:  cond expr before second ';'
103  *                              pn_kid3:  update expr after second ';'
104  *                              any kid may be null
105  *                          pn_right: body
106  * TOK_THROW    unary       pn_op: JSOP_THROW, pn_kid: exception
107  * TOK_TRY      ternary     pn_kid1: try block
108  *                          pn_kid2: null or TOK_RESERVED list of
109  *                          TOK_LEXICALSCOPE nodes, each with pn_expr pointing
110  *                          to a TOK_CATCH node
111  *                          pn_kid3: null or finally block
112  * TOK_CATCH    ternary     pn_kid1: TOK_NAME, TOK_RB, or TOK_RC catch var node
113  *                                   (TOK_RB or TOK_RC if destructuring)
114  *                          pn_kid2: null or the catch guard expression
115  *                          pn_kid3: catch block statements
116  * TOK_BREAK    name        pn_atom: label or null
117  * TOK_CONTINUE name        pn_atom: label or null
118  * TOK_WITH     binary      pn_left: head expr, pn_right: body
119  * TOK_VAR      list        pn_head: list of pn_count TOK_NAME nodes
120  *                                   each name node has
121  *                                     pn_atom: variable name
122  *                                     pn_expr: initializer or null
123  * TOK_RETURN   unary       pn_kid: return expr or null
124  * TOK_SEMI     unary       pn_kid: expr or null statement
125  * TOK_COLON    name        pn_atom: label, pn_expr: labeled statement
126  *
127  * <Expressions>
128  * All left-associated binary trees of the same type are optimized into lists
129  * to avoid recursion when processing expression chains.
130  * TOK_COMMA    list        pn_head: list of pn_count comma-separated exprs
131  * TOK_ASSIGN   binary      pn_left: lvalue, pn_right: rvalue
132  *                          pn_op: JSOP_ADD for +=, etc.
133  * TOK_HOOK     ternary     pn_kid1: cond, pn_kid2: then, pn_kid3: else
134  * TOK_OR       binary      pn_left: first in || chain, pn_right: rest of chain
135  * TOK_AND      binary      pn_left: first in && chain, pn_right: rest of chain
136  * TOK_BITOR    binary      pn_left: left-assoc | expr, pn_right: ^ expr
137  * TOK_BITXOR   binary      pn_left: left-assoc ^ expr, pn_right: & expr
138  * TOK_BITAND   binary      pn_left: left-assoc & expr, pn_right: EQ expr
139  * TOK_EQOP     binary      pn_left: left-assoc EQ expr, pn_right: REL expr
140  *                          pn_op: JSOP_EQ, JSOP_NE, JSOP_NEW_EQ, JSOP_NEW_NE
141  * TOK_RELOP    binary      pn_left: left-assoc REL expr, pn_right: SH expr
142  *                          pn_op: JSOP_LT, JSOP_LE, JSOP_GT, JSOP_GE
143  * TOK_SHOP     binary      pn_left: left-assoc SH expr, pn_right: ADD expr
144  *                          pn_op: JSOP_LSH, JSOP_RSH, JSOP_URSH
145  * TOK_PLUS,    binary      pn_left: left-assoc ADD expr, pn_right: MUL expr
146  *                          pn_extra: if a left-associated binary TOK_PLUS
147  *                            tree has been flattened into a list (see above
148  *                            under <Expressions>), pn_extra will contain
149  *                            PNX_STRCAT if at least one list element is a
150  *                            string literal (TOK_STRING); if such a list has
151  *                            any non-string, non-number term, pn_extra will
152  *                            contain PNX_CANTFOLD.
153  *                          pn_
154  * TOK_MINUS                pn_op: JSOP_ADD, JSOP_SUB
155  * TOK_STAR,    binary      pn_left: left-assoc MUL expr, pn_right: UNARY expr
156  * TOK_DIVOP                pn_op: JSOP_MUL, JSOP_DIV, JSOP_MOD
157  * TOK_UNARYOP  unary       pn_kid: UNARY expr, pn_op: JSOP_NEG, JSOP_POS,
158  *                          JSOP_NOT, JSOP_BITNOT, JSOP_TYPEOF, JSOP_VOID
159  * TOK_INC,     unary       pn_kid: MEMBER expr
160  * TOK_DEC
161  * TOK_NEW      list        pn_head: list of ctor, arg1, arg2, ... argN
162  *                          pn_count: 1 + N (where N is number of args)
163  *                          ctor is a MEMBER expr
164  * TOK_DELETE   unary       pn_kid: MEMBER expr
165  * TOK_DOT,     name        pn_expr: MEMBER expr to left of .
166  * TOK_DBLDOT               pn_atom: name to right of .
167  * TOK_LB       binary      pn_left: MEMBER expr to left of [
168  *                          pn_right: expr between [ and ]
169  * TOK_LP       list        pn_head: list of call, arg1, arg2, ... argN
170  *                          pn_count: 1 + N (where N is number of args)
171  *                          call is a MEMBER expr naming a callable object
172  * TOK_RB       list        pn_head: list of pn_count array element exprs
173  *                          [,,] holes are represented by TOK_COMMA nodes
174  *                          #n=[...] produces TOK_DEFSHARP at head of list
175  *                          pn_extra: PN_ENDCOMMA if extra comma at end
176  * TOK_RC       list        pn_head: list of pn_count TOK_COLON nodes where
177  *                          each has pn_left: property id, pn_right: value
178  *                          #n={...} produces TOK_DEFSHARP at head of list
179  * TOK_DEFSHARP unary       pn_num: jsint value of n in #n=
180  *                          pn_kid: null for #n=[...] and #n={...}, primary
181  *                          if #n=primary for function, paren, name, object
182  *                          literal expressions
183  * TOK_USESHARP nullary     pn_num: jsint value of n in #n#
184  * TOK_RP       unary       pn_kid: parenthesized expression
185  * TOK_NAME,    name        pn_atom: name, string, or object atom
186  * TOK_STRING,              pn_op: JSOP_NAME, JSOP_STRING, or JSOP_OBJECT, or
187  *                                 JSOP_REGEXP
188  * TOK_OBJECT               If JSOP_NAME, pn_op may be JSOP_*ARG or JSOP_*VAR
189  *                          with pn_slot >= 0 and pn_attrs telling const-ness
190  * TOK_NUMBER   dval        pn_dval: double value of numeric literal
191  * TOK_PRIMARY  nullary     pn_op: JSOp bytecode
192  *
193  * <E4X node descriptions>
194  * TOK_ANYNAME  nullary     pn_op: JSOP_ANYNAME
195  *                          pn_atom: cx->runtime->atomState.starAtom
196  * TOK_AT       unary       pn_op: JSOP_TOATTRNAME; pn_kid attribute id/expr
197  * TOK_DBLCOLON binary      pn_op: JSOP_QNAME
198  *                          pn_left: TOK_ANYNAME or TOK_NAME node
199  *                          pn_right: TOK_STRING "*" node, or expr within []
200  *              name        pn_op: JSOP_QNAMECONST
201  *                          pn_expr: TOK_ANYNAME or TOK_NAME left operand
202  *                          pn_atom: name on right of ::
203  * TOK_XMLELEM  list        XML element node
204  *                          pn_head: start tag, content1, ... contentN, end tag
205  *                          pn_count: 2 + N where N is number of content nodes
206  *                                    N may be > x.length() if {expr} embedded
207  * TOK_XMLLIST  list        XML list node
208  *                          pn_head: content1, ... contentN
209  * TOK_XMLSTAGO, list       XML start, end, and point tag contents
210  * TOK_XMLETAGC,            pn_head: tag name or {expr}, ... XML attrs ...
211  * TOK_XMLPTAGO
212  * TOK_XMLNAME  nullary     pn_atom: XML name, with no {expr} embedded
213  * TOK_XMLNAME  list        pn_head: tag name or {expr}, ... name or {expr}
214  * TOK_XMLATTR, nullary     pn_atom: attribute value string; pn_op: JSOP_STRING
215  * TOK_XMLCDATA,
216  * TOK_XMLCOMMENT
217  * TOK_XMLPI    nullary     pn_atom: XML processing instruction target
218  *                          pn_atom2: XML PI content, or null if no content
219  * TOK_XMLTEXT  nullary     pn_atom: marked-up text, or null if empty string
220  * TOK_LC       unary       {expr} in XML tag or content; pn_kid is expr
221  *
222  * So an XML tag with no {expr} and three attributes is a list with the form:
223  *
224  *    (tagname attrname1 attrvalue1 attrname2 attrvalue2 attrname2 attrvalue3)
225  *
226  * An XML tag with embedded expressions like so:
227  *
228  *    <name1{expr1} name2{expr2}name3={expr3}>
229  *
230  * would have the form:
231  *
232  *    ((name1 {expr1}) (name2 {expr2} name3) {expr3})
233  *
234  * where () bracket a list with elements separated by spaces, and {expr} is a
235  * TOK_LC unary node with expr as its kid.
236  *
237  * Thus, the attribute name/value pairs occupy successive odd and even list
238  * locations, where pn_head is the TOK_XMLNAME node at list location 0.  The
239  * parser builds the same sort of structures for elements:
240  *
241  *    <a x={x}>Hi there!<b y={y}>How are you?</b><answer>{x + y}</answer></a>
242  *
243  * translates to:
244  *
245  *    ((a x {x}) 'Hi there!' ((b y {y}) 'How are you?') ((answer) {x + y}))
246  *
247  * <Non-E4X node descriptions, continued>
248  *
249  * Label              Variant   Members
250  * -----              -------   -------
251  * TOK_LEXICALSCOPE   name      pn_op: JSOP_LEAVEBLOCK or JSOP_LEAVEBLOCKEXPR
252  *                              pn_atom: block object
253  *                              pn_expr: block body
254  * TOK_ARRAYCOMP      list      pn_head: list of pn_count (1 or 2) elements
255  *                              if pn_count is 2, first element is #n=[...]
256  *                                last element is block enclosing for loop(s)
257  *                                and optionally if-guarded TOK_ARRAYPUSH
258  *                              pn_extra: stack slot, used during code gen
259  * TOK_ARRAYPUSH      unary     pn_op: JSOP_ARRAYCOMP
260  *                              pn_kid: array comprehension expression
261  */
262 typedef enum JSParseNodeArity {
263     PN_FUNC     = -3,
264     PN_LIST     = -2,
265     PN_TERNARY  =  3,
266     PN_BINARY   =  2,
267     PN_UNARY    =  1,
268     PN_NAME     = -1,
269     PN_NULLARY  =  0
270 } JSParseNodeArity;
271 
272 struct JSParseNode {
273     uint16              pn_type;
274     uint8               pn_op;
275     int8                pn_arity;
276     JSTokenPos          pn_pos;
277     ptrdiff_t           pn_offset;      /* first generated bytecode offset */
278     union {
279         struct {                        /* TOK_FUNCTION node */
280             JSAtom      *funAtom;       /* atomized function object */
281             JSParseNode *body;          /* TOK_LC list of statements */
282             uint32      flags;          /* accumulated tree context flags */
283             uint32      tryCount;       /* count of try statements in body */
284         } func;
285         struct {                        /* list of next-linked nodes */
286             JSParseNode *head;          /* first node in list */
287             JSParseNode **tail;         /* ptr to ptr to last node in list */
288             uint32      count;          /* number of nodes in list */
289             uint32      extra;          /* extra flags, see below */
290         } list;
291         struct {                        /* ternary: if, for(;;), ?: */
292             JSParseNode *kid1;          /* condition, discriminant, etc. */
293             JSParseNode *kid2;          /* then-part, case list, etc. */
294             JSParseNode *kid3;          /* else-part, default case, etc. */
295         } ternary;
296         struct {                        /* two kids if binary */
297             JSParseNode *left;
298             JSParseNode *right;
299             jsval       val;            /* switch case value */
300         } binary;
301         struct {                        /* one kid if unary */
302             JSParseNode *kid;
303             jsint       num;            /* -1 or sharp variable number */
304         } unary;
305         struct {                        /* name, labeled statement, etc. */
306             JSAtom      *atom;          /* name or label atom, null if slot */
307             JSParseNode *expr;          /* object or initializer */
308             jsint       slot;           /* -1 or arg or local var slot */
309             uintN       attrs;          /* attributes if local var or const */
310         } name;
311         struct {
312             JSAtom      *atom;          /* first atom in pair */
313             JSAtom      *atom2;         /* second atom in pair or null */
314         } apair;
315         jsdouble        dval;           /* aligned numeric literal value */
316     } pn_u;
317     JSParseNode         *pn_next;       /* to align dval and pn_u on RISCs */
318     JSTokenStream       *pn_ts;         /* token stream for error reports */
319     JSAtom              *pn_source;     /* saved source for decompilation */
320 };
321 
322 #define pn_funAtom      pn_u.func.funAtom
323 #define pn_body         pn_u.func.body
324 #define pn_flags        pn_u.func.flags
325 #define pn_tryCount     pn_u.func.tryCount
326 #define pn_head         pn_u.list.head
327 #define pn_tail         pn_u.list.tail
328 #define pn_count        pn_u.list.count
329 #define pn_extra        pn_u.list.extra
330 #define pn_kid1         pn_u.ternary.kid1
331 #define pn_kid2         pn_u.ternary.kid2
332 #define pn_kid3         pn_u.ternary.kid3
333 #define pn_left         pn_u.binary.left
334 #define pn_right        pn_u.binary.right
335 #define pn_val          pn_u.binary.val
336 #define pn_kid          pn_u.unary.kid
337 #define pn_num          pn_u.unary.num
338 #define pn_atom         pn_u.name.atom
339 #define pn_expr         pn_u.name.expr
340 #define pn_slot         pn_u.name.slot
341 #define pn_attrs        pn_u.name.attrs
342 #define pn_dval         pn_u.dval
343 #define pn_atom2        pn_u.apair.atom2
344 
345 /* PN_LIST pn_extra flags. */
346 #define PNX_STRCAT      0x01            /* TOK_PLUS list has string term */
347 #define PNX_CANTFOLD    0x02            /* TOK_PLUS list has unfoldable term */
348 #define PNX_POPVAR      0x04            /* TOK_VAR last result needs popping */
349 #define PNX_FORINVAR    0x08            /* TOK_VAR is left kid of TOK_IN node,
350                                            which is left kid of TOK_FOR */
351 #define PNX_ENDCOMMA    0x10            /* array literal has comma at end */
352 #define PNX_XMLROOT     0x20            /* top-most node in XML literal tree */
353 #define PNX_GROUPINIT   0x40            /* var [a, b] = [c, d]; unit list */
354 #define PNX_NEEDBRACES  0x80            /* braces necessary due to closure */
355 
356 /*
357  * Move pn2 into pn, preserving pn->pn_pos and pn->pn_offset and handing off
358  * any kids in pn2->pn_u, by clearing pn2.
359  */
360 #define PN_MOVE_NODE(pn, pn2)                                                 \
361     JS_BEGIN_MACRO                                                            \
362         (pn)->pn_type = (pn2)->pn_type;                                       \
363         (pn)->pn_op = (pn2)->pn_op;                                           \
364         (pn)->pn_arity = (pn2)->pn_arity;                                     \
365         (pn)->pn_u = (pn2)->pn_u;                                             \
366         PN_CLEAR_NODE(pn2);                                                   \
367     JS_END_MACRO
368 
369 #define PN_CLEAR_NODE(pn)                                                     \
370     JS_BEGIN_MACRO                                                            \
371         (pn)->pn_type = TOK_EOF;                                              \
372         (pn)->pn_op = JSOP_NOP;                                               \
373         (pn)->pn_arity = PN_NULLARY;                                          \
374     JS_END_MACRO
375 
376 /* True if pn is a parsenode representing a literal constant. */
377 #define PN_IS_CONSTANT(pn)                                                    \
378     ((pn)->pn_type == TOK_NUMBER ||                                           \
379      (pn)->pn_type == TOK_STRING ||                                           \
380      ((pn)->pn_type == TOK_PRIMARY && (pn)->pn_op != JSOP_THIS))
381 
382 /*
383  * Compute a pointer to the last JSParseNode element in a singly-linked list.
384  * NB: list must be non-empty for correct PN_LAST usage!
385  */
386 #define PN_LAST(list) \
387     ((JSParseNode *)((char *)(list)->pn_tail - offsetof(JSParseNode, pn_next)))
388 
389 #define PN_INIT_LIST(list)                                                    \
390     JS_BEGIN_MACRO                                                            \
391         (list)->pn_head = NULL;                                               \
392         (list)->pn_tail = &(list)->pn_head;                                   \
393         (list)->pn_count = (list)->pn_extra = 0;                              \
394     JS_END_MACRO
395 
396 #define PN_INIT_LIST_1(list, pn)                                              \
397     JS_BEGIN_MACRO                                                            \
398         (list)->pn_head = (pn);                                               \
399         (list)->pn_tail = &(pn)->pn_next;                                     \
400         (list)->pn_count = 1;                                                 \
401         (list)->pn_extra = 0;                                                 \
402     JS_END_MACRO
403 
404 #define PN_APPEND(list, pn)                                                   \
405     JS_BEGIN_MACRO                                                            \
406         *(list)->pn_tail = (pn);                                              \
407         (list)->pn_tail = &(pn)->pn_next;                                     \
408         (list)->pn_count++;                                                   \
409     JS_END_MACRO
410 
411 /*
412  * Parse a top-level JS script.
413  *
414  * The caller must prevent the GC from running while this function is active,
415  * because atoms and function newborns are not rooted yet.
416  */
417 extern JS_FRIEND_API(JSParseNode *)
418 js_ParseTokenStream(JSContext *cx, JSObject *chain, JSTokenStream *ts);
419 
420 extern JS_FRIEND_API(JSBool)
421 js_CompileTokenStream(JSContext *cx, JSObject *chain, JSTokenStream *ts,
422                       JSCodeGenerator *cg);
423 
424 extern JSBool
425 js_CompileFunctionBody(JSContext *cx, JSTokenStream *ts, JSFunction *fun);
426 
427 extern JSBool
428 js_FoldConstants(JSContext *cx, JSParseNode *pn, JSTreeContext *tc);
429 
430 #if JS_HAS_XML_SUPPORT
431 JS_FRIEND_API(JSParseNode *)
432 js_ParseXMLTokenStream(JSContext *cx, JSObject *chain, JSTokenStream *ts,
433                        JSBool allowList);
434 #endif
435 
436 JS_END_EXTERN_C
437 
438 #endif /* jsparse_h___ */
439