       1 /*
       2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       4  *
       5  * This code is free software; you can redistribute it and/or modify it
       6  * under the terms of the GNU General Public License version 2 only, as
       7  * published by the Free Software Foundation.  Oracle designates this
       8  * particular file as subject to the "Classpath" exception as provided
       9  * by Oracle in the LICENSE file that accompanied this code.
      10  *
      11  * This code is distributed in the hope that it will be useful, but WITHOUT
      12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14  * version 2 for more details (a copy is included in the LICENSE file that
      15  * accompanied this code).
      16  *
      17  * You should have received a copy of the GNU General Public License version
      18  * 2 along with this work; if not, write to the Free Software Foundation,
      19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      20  *
      21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      22  * or visit www.oracle.com if you need additional information or have any
      23  * questions.
      24  */
      25
      26 package jdk.nashorn.api.tree;
      27
      28 /**
      29  * Common interface for all nodes in an abstract syntax tree.
      30  *
      31  * <p><b>WARNING:</b> This interface and its sub-interfaces are
      32  * subject to change as the ECMAScript  programming language evolves.
      33  *
      34  * @deprecated Nashorn JavaScript script engine and APIs, and the jjs tool
      35  * are deprecated with the intent to remove them in a future release.
      36  *
      37  * @since 9
      38  */
      39 @Deprecated(since="11", forRemoval=true)
      40 public interface Tree {
      41
      42     /**
      43      * Enumerates all kinds of trees.
      44      */
      45     @Deprecated(since="11", forRemoval=true)
      46     public enum Kind {
      47         /**
      48          * Used for instances of {@link ArrayAccessTree}.
      49          */
      50         ARRAY_ACCESS(ArrayAccessTree.class),
      51
      52         /**
      53          * Used for instances of {@link ArrayLiteralTree}.
      54          */
      55         ARRAY_LITERAL(ArrayLiteralTree.class),
      56
      57         /**
      58          * Used for instances of {@link AssignmentTree}.
      59          */
      60         ASSIGNMENT(AssignmentTree.class),
      61
      62         /**
      63          * Used for instances of {@link BlockTree}.
      64          */
      65         BLOCK(BlockTree.class),
      66
      67         /**
      68          * Used for instances of {@link BreakTree}.
      69          */
      70         BREAK(BreakTree.class),
      71
      72         /**
      73          * Used for instances of {@link ClassDeclarationTree}.
      74          */
      75         CLASS(ClassDeclarationTree.class),
      76
      77         /**
      78          * Used for instances of {@link ClassExpressionTree}.
      79          */
      80         CLASS_EXPRESSION(ClassExpressionTree.class),
      81
      82         /**
      83          * Used for instances of {@link CaseTree}.
      84          */
      85         CASE(CaseTree.class),
      86
      87         /**
      88          * Used for instances of {@link CatchTree}.
      89          */
      90         CATCH(CatchTree.class),
      91
      92         /**
      93          * Used for instances of {@link CompilationUnitTree}.
      94          */
      95         COMPILATION_UNIT(CompilationUnitTree.class),
      96
      97         /**
      98          * Used for instances of {@link ConditionalExpressionTree}.
      99          */
     100         CONDITIONAL_EXPRESSION(ConditionalExpressionTree.class),
     101
     102         /**
     103          * Used for instances of {@link ContinueTree}.
     104          */
     105         CONTINUE(ContinueTree.class),
     106
     107         /**
     108          * Used for instances of {@link DoWhileLoopTree}.
     109          */
     110         DO_WHILE_LOOP(DoWhileLoopTree.class),
     111
     112         /**
     113          * Used for instances of {@link DebuggerTree}.
     114          */
     115         DEBUGGER(DebuggerTree.class),
     116
     117         /**
     118          * Used for instances of {@link ForInLoopTree}.
     119          */
     120         FOR_IN_LOOP(ForInLoopTree.class),
     121
     122         /**
     123          * Used for instances of {@link FunctionExpressionTree}.
     124          */
     125         FUNCTION_EXPRESSION(FunctionExpressionTree.class),
     126
     127         /**
     128          * Used for instances of {@link ErroneousTree}.
     129          */
     130         ERROR(ErroneousTree.class),
     131
     132         /**
     133          * Used for instances of {@link ExpressionStatementTree}.
     134          */
     135         EXPRESSION_STATEMENT(ExpressionStatementTree.class),
     136
     137         /**
     138          * Used for instances of {@link MemberSelectTree}.
     139          */
     140         MEMBER_SELECT(MemberSelectTree.class),
     141
     142         /**
     143          * Used for instances of {@link ForLoopTree}.
     144          */
     145         FOR_LOOP(ForLoopTree.class),
     146
     147         /**
     148          * Used for instances of {@link IdentifierTree}.
     149          */
     150         IDENTIFIER(IdentifierTree.class),
     151
     152         /**
     153          * Used for instances of {@link IfTree}.
     154          */
     155         IF(IfTree.class),
     156
     157         /**
     158          * Used for instances of {@link InstanceOfTree}.
     159          */
     160         INSTANCE_OF(InstanceOfTree.class),
     161
     162         /**
     163          * Used for instances of {@link LabeledStatementTree}.
     164          */
     165         LABELED_STATEMENT(LabeledStatementTree.class),
     166
     167         /**
     168          * Used for instances of {@link ModuleTree}.
     169          */
     170         MODULE(ModuleTree.class),
     171
     172         /**
     173          * Used for instances of {@link ExportEntryTree}.
     174          */
     175         EXPORT_ENTRY(ExportEntryTree.class),
     176
     177         /**
     178          * Used for instances of {@link ImportEntryTree}.
     179          */
     180         IMPORT_ENTRY(ImportEntryTree.class),
     181
     182         /**
     183          * Used for instances of {@link FunctionDeclarationTree}.
     184          */
     185         FUNCTION(FunctionDeclarationTree.class),
     186
     187         /**
     188          * Used for instances of {@link FunctionCallTree}.
     189          */
     190         FUNCTION_INVOCATION(FunctionCallTree.class),
     191
     192         /**
     193          * Used for instances of {@link NewTree}.
     194          */
     195         NEW(NewTree.class),
     196
     197         /**
     198          * Used for instances of {@link ObjectLiteralTree}.
     199          */
     200         OBJECT_LITERAL(ObjectLiteralTree.class),
     201
     202         /**
     203          * Used for instances of {@link ParenthesizedTree}.
     204          */
     205         PARENTHESIZED(ParenthesizedTree.class),
     206
     207         /**
     208          * Used for instances of {@link PropertyTree}.
     209          */
     210         PROPERTY(PropertyTree.class),
     211
     212         /**
     213          * Used for instances of {@link RegExpLiteralTree}.
     214          */
     215         REGEXP_LITERAL(RegExpLiteralTree.class),
     216
     217         /**
     218          * Used for instances of {@link TemplateLiteralTree}.
     219          */
     220         TEMPLATE_LITERAL(TemplateLiteralTree.class),
     221
     222         /**
     223          * Used for instances of {@link ReturnTree}.
     224          */
     225         RETURN(ReturnTree.class),
     226
     227         /**
     228          * Used for instances of {@link EmptyStatementTree}.
     229          */
     230         EMPTY_STATEMENT(EmptyStatementTree.class),
     231
     232         /**
     233          * Used for instances of {@link SwitchTree}.
     234          */
     235         SWITCH(SwitchTree.class),
     236
     237         /**
     238          * Used for instances of {@link ThrowTree}.
     239          */
     240         THROW(ThrowTree.class),
     241
     242         /**
     243          * Used for instances of {@link TryTree}.
     244          */
     245         TRY(TryTree.class),
     246
     247         /**
     248          * Used for instances of {@link VariableTree}.
     249          */
     250         VARIABLE(VariableTree.class),
     251
     252         /**
     253          * Used for instances of {@link WhileLoopTree}.
     254          */
     255         WHILE_LOOP(WhileLoopTree.class),
     256
     257         /**
     258          * Used for instances of {@link WithTree}.
     259          */
     260         WITH(WithTree.class),
     261
     262         /**
     263          * Used for instances of {@link UnaryTree} representing postfix
     264          * increment operator {@code ++}.
     265          */
     266         POSTFIX_INCREMENT(UnaryTree.class),
     267
     268         /**
     269          * Used for instances of {@link UnaryTree} representing postfix
     270          * decrement operator {@code --}.
     271          */
     272         POSTFIX_DECREMENT(UnaryTree.class),
     273
     274         /**
     275          * Used for instances of {@link UnaryTree} representing prefix
     276          * increment operator {@code ++}.
     277          */
     278         PREFIX_INCREMENT(UnaryTree.class),
     279
     280         /**
     281          * Used for instances of {@link UnaryTree} representing prefix
     282          * decrement operator {@code --}.
     283          */
     284         PREFIX_DECREMENT(UnaryTree.class),
     285
     286         /**
     287          * Used for instances of {@link UnaryTree} representing unary plus
     288          * operator {@code +}.
     289          */
     290         UNARY_PLUS(UnaryTree.class),
     291
     292         /**
     293          * Used for instances of {@link UnaryTree} representing unary minus
     294          * operator {@code -}.
     295          */
     296         UNARY_MINUS(UnaryTree.class),
     297
     298         /**
     299          * Used for instances of {@link UnaryTree} representing bitwise
     300          * complement operator {@code ~}.
     301          */
     302         BITWISE_COMPLEMENT(UnaryTree.class),
     303
     304         /**
     305          * Used for instances of {@link UnaryTree} representing logical
     306          * complement operator {@code !}.
     307          */
     308         LOGICAL_COMPLEMENT(UnaryTree.class),
     309
     310         /**
     311          * Used for instances of {@link UnaryTree} representing logical
     312          * delete operator {@code delete}.
     313          */
     314         DELETE(UnaryTree.class),
     315
     316         /**
     317          * Used for instances of {@link UnaryTree} representing logical
     318          * typeof operator {@code typeof}.
     319          */
     320         TYPEOF(UnaryTree.class),
     321
     322         /**
     323          * Used for instances of {@link UnaryTree} representing logical
     324          * void operator {@code void}.
     325          */
     326         VOID(UnaryTree.class),
     327
     328         /**
     329          * Used for instances of {@link BinaryTree} representing
     330          * comma {@code ,}.
     331          */
     332         COMMA(BinaryTree.class),
     333
     334         /**
     335          * Used for instances of {@link BinaryTree} representing
     336          * multiplication {@code *}.
     337          */
     338         MULTIPLY(BinaryTree.class),
     339
     340         /**
     341          * Used for instances of {@link BinaryTree} representing
     342          * division {@code /}.
     343          */
     344         DIVIDE(BinaryTree.class),
     345
     346         /**
     347          * Used for instances of {@link BinaryTree} representing
     348          * remainder {@code %}.
     349          */
     350         REMAINDER(BinaryTree.class),
     351
     352          /**
     353          * Used for instances of {@link BinaryTree} representing
     354          * addition or string concatenation {@code +}.
     355          */
     356         PLUS(BinaryTree.class),
     357
     358         /**
     359          * Used for instances of {@link BinaryTree} representing
     360          * subtraction {@code -}.
     361          */
     362         MINUS(BinaryTree.class),
     363
     364         /**
     365          * Used for instances of {@link BinaryTree} representing
     366          * left shift {@code <<}.
     367          */
     368         LEFT_SHIFT(BinaryTree.class),
     369
     370         /**
     371          * Used for instances of {@link BinaryTree} representing
     372          * right shift {@code >>}.
     373          */
     374         RIGHT_SHIFT(BinaryTree.class),
     375
     376         /**
     377          * Used for instances of {@link BinaryTree} representing
     378          * unsigned right shift {@code >>>}.
     379          */
     380         UNSIGNED_RIGHT_SHIFT(BinaryTree.class),
     381
     382         /**
     383          * Used for instances of {@link BinaryTree} representing
     384          * less-than {@code <}.
     385          */
     386         LESS_THAN(BinaryTree.class),
     387
     388         /**
     389          * Used for instances of {@link BinaryTree} representing
     390          * greater-than {@code >}.
     391          */
     392         GREATER_THAN(BinaryTree.class),
     393
     394         /**
     395          * Used for instances of {@link BinaryTree} representing
     396          * less-than-equal {@code <=}.
     397          */
     398         LESS_THAN_EQUAL(BinaryTree.class),
     399
     400         /**
     401          * Used for instances of {@link BinaryTree} representing
     402          * greater-than-equal {@code >=}.
     403          */
     404         GREATER_THAN_EQUAL(BinaryTree.class),
     405
     406         /**
     407          * Used for instances of {@link BinaryTree} representing
     408          * in operator {@code in}.
     409          */
     410         IN(BinaryTree.class),
     411
     412         /**
     413          * Used for instances of {@link BinaryTree} representing
     414          * equal-to {@code ==}.
     415          */
     416         EQUAL_TO(BinaryTree.class),
     417
     418         /**
     419          * Used for instances of {@link BinaryTree} representing
     420          * not-equal-to {@code !=}.
     421          */
     422         NOT_EQUAL_TO(BinaryTree.class),
     423
     424         /**
     425          * Used for instances of {@link BinaryTree} representing
     426          * equal-to {@code ===}.
     427          */
     428         STRICT_EQUAL_TO(BinaryTree.class),
     429
     430         /**
     431          * Used for instances of {@link BinaryTree} representing
     432          * not-equal-to {@code !==}.
     433          */
     434         STRICT_NOT_EQUAL_TO(BinaryTree.class),
     435
     436         /**
     437          * Used for instances of {@link BinaryTree} representing
     438          * bitwise and logical "and" {@code &}.
     439          */
     440         AND(BinaryTree.class),
     441
     442         /**
     443          * Used for instances of {@link BinaryTree} representing
     444          * bitwise and logical "xor" {@code ^}.
     445          */
     446         XOR(BinaryTree.class),
     447
     448         /**
     449          * Used for instances of {@link BinaryTree} representing
     450          * bitwise and logical "or" {@code |}.
     451          */
     452         OR(BinaryTree.class),
     453
     454         /**
     455          * Used for instances of {@link BinaryTree} representing
     456          * conditional-and {@code &&}.
     457          */
     458         CONDITIONAL_AND(BinaryTree.class),
     459
     460         /**
     461          * Used for instances of {@link BinaryTree} representing
     462          * conditional-or {@code ||}.
     463          */
     464         CONDITIONAL_OR(BinaryTree.class),
     465
     466         /**
     467          * Used for instances of {@link CompoundAssignmentTree} representing
     468          * multiplication assignment {@code *=}.
     469          */
     470         MULTIPLY_ASSIGNMENT(CompoundAssignmentTree.class),
     471
     472         /**
     473          * Used for instances of {@link CompoundAssignmentTree} representing
     474          * division assignment {@code /=}.
     475          */
     476         DIVIDE_ASSIGNMENT(CompoundAssignmentTree.class),
     477
     478         /**
     479          * Used for instances of {@link CompoundAssignmentTree} representing
     480          * remainder assignment {@code %=}.
     481          */
     482         REMAINDER_ASSIGNMENT(CompoundAssignmentTree.class),
     483
     484         /**
     485          * Used for instances of {@link CompoundAssignmentTree} representing
     486          * addition or string concatenation assignment {@code +=}.
     487          */
     488         PLUS_ASSIGNMENT(CompoundAssignmentTree.class),
     489
     490         /**
     491          * Used for instances of {@link CompoundAssignmentTree} representing
     492          * subtraction assignment {@code -=}.
     493          */
     494         MINUS_ASSIGNMENT(CompoundAssignmentTree.class),
     495
     496         /**
     497          * Used for instances of {@link CompoundAssignmentTree} representing
     498          * left shift assignment {@code <<=}.
     499          */
     500         LEFT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class),
     501
     502         /**
     503          * Used for instances of {@link CompoundAssignmentTree} representing
     504          * right shift assignment {@code >>=}.
     505          */
     506         RIGHT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class),
     507
     508         /**
     509          * Used for instances of {@link CompoundAssignmentTree} representing
     510          * unsigned right shift assignment {@code >>>=}.
     511          */
     512         UNSIGNED_RIGHT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class),
     513
     514         /**
     515          * Used for instances of {@link CompoundAssignmentTree} representing
     516          * bitwise and logical "and" assignment {@code &=}.
     517          */
     518         AND_ASSIGNMENT(CompoundAssignmentTree.class),
     519
     520         /**
     521          * Used for instances of {@link CompoundAssignmentTree} representing
     522          * bitwise and logical "xor" assignment {@code ^=}.
     523          */
     524         XOR_ASSIGNMENT(CompoundAssignmentTree.class),
     525
     526         /**
     527          * Used for instances of {@link CompoundAssignmentTree} representing
     528          * bitwise and logical "or" assignment {@code |=}.
     529          */
     530         OR_ASSIGNMENT(CompoundAssignmentTree.class),
     531
     532         /**
     533          * Used for instances of {@link SpreadTree} representing
     534          * spread "operator" for arrays and function call arguments.
     535          */
     536         SPREAD(SpreadTree.class),
     537
     538         /**
     539          * Used for instances of {@link YieldTree} representing (generator)
     540          * yield expression {@code yield expr}.
     541          */
     542         YIELD(YieldTree.class),
     543
     544         /**
     545          * Used for instances of {@link LiteralTree} representing
     546          * a number literal expression of type {@code double}.
     547          */
     548         NUMBER_LITERAL(LiteralTree.class),
     549
     550         /**
     551          * Used for instances of {@link LiteralTree} representing
     552          * a boolean literal expression of type {@code boolean}.
     553          */
     554         BOOLEAN_LITERAL(LiteralTree.class),
     555
     556         /**
     557          * Used for instances of {@link LiteralTree} representing
     558          * a string literal expression of type {@link String}.
     559          */
     560         STRING_LITERAL(LiteralTree.class),
     561
     562         /**
     563          * Used for instances of {@link LiteralTree} representing
     564          * the use of {@code null}.
     565          */
     566         NULL_LITERAL(LiteralTree.class),
     567
     568         /**
     569          * An implementation-reserved node. This is the not the node
     570          * you are looking for.
     571          */
     572         OTHER(null);
     573
     574         Kind(final Class<? extends Tree> intf) {
     575             associatedInterface = intf;
     576         }
     577
     578        /**
     579         * Returns the associated interface type that uses this kind.
     580         * @return the associated interface
     581         */
     582         public Class<? extends Tree> asInterface() {
     583             return associatedInterface;
     584         }
     585
     586         /**
     587          * Returns if this is a literal tree kind or not.
     588          *
     589          * @return true if this is a literal tree kind, false otherwise
     590          */
     591         public boolean isLiteral() {
     592             return associatedInterface == LiteralTree.class;
     593         }
     594
     595         /**
     596          * Returns if this is an expression tree kind or not.
     597          *
     598          * @return true if this is an expression tree kind, false otherwise
     599          */
     600         public boolean isExpression() {
     601             return ExpressionTree.class.isAssignableFrom(associatedInterface);
     602         }
     603
     604         /**
     605          * Returns if this is a statement tree kind or not.
     606          *
     607          * @return true if this is a statement tree kind, false otherwise
     608          */
     609         public boolean isStatement() {
     610             return StatementTree.class.isAssignableFrom(associatedInterface);
     611         }
     612
     613         private final Class<? extends Tree> associatedInterface;
     614     }
     615
     616     /**
     617      * Start character offset of this Tree within the source.
     618      *
     619      * @return the position
     620      */
     621     long getStartPosition();
     622
     623     /**
     624      * End character offset of this Tree within the source.
     625      *
     626      * @return the position
     627      */
     628     long getEndPosition();
     629
     630     /**
     631      * Gets the kind of this tree.
     632      *
     633      * @return the kind of this tree.
     634      */
     635     Kind getKind();
     636
     637     /**
     638      * Accept method used to implement the visitor pattern.  The
     639      * visitor pattern is used to implement operations on trees.
     640      *
     641      * @param <R> result type of this operation.
     642      * @param <D> type of additional data.
     643      * @param visitor tree visitor
     644      * @param data additional data passed to visitor methods
     645      * @return the value from visitor's visit methods
     646      */
     647     <R,D> R accept(TreeVisitor<R,D> visitor, D data);
     648 }