src/jdk/nashorn/internal/ir/VarNode.java

Print this page




  28 import jdk.nashorn.internal.ir.annotations.Immutable;
  29 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
  30 import jdk.nashorn.internal.parser.Token;
  31 
  32 /**
  33  * Node represents a var/let declaration.
  34  */
  35 @Immutable
  36 public final class VarNode extends Statement implements Assignment<IdentNode> {
  37     private static final long serialVersionUID = 1L;
  38 
  39     /** Var name. */
  40     private final IdentNode name;
  41 
  42     /** Initialization expression. */
  43     private final Expression init;
  44 
  45     /** Is this a var statement (as opposed to a "var" in a for loop statement) */
  46     private final int flags;
  47 
  48     /** Flag that determines if this function node is a statement */
  49     public static final int IS_STATEMENT                 = 1 << 0;
  50 
  51     /** Flag for ES6 LET declaration */
  52     public static final int IS_LET                       = 1 << 1;
  53 
  54     /** Flag for ES6 CONST declaration */
  55     public static final int IS_CONST                     = 1 << 2;
  56 
  57     /** Flag that determines if this is the last function declaration in a function
  58      *  This is used to micro optimize the placement of return value assignments for
  59      *  a program node */
  60     public static final int IS_LAST_FUNCTION_DECLARATION = 1 << 3;
  61 
  62     /**
  63      * Constructor
  64      *
  65      * @param lineNumber line number
  66      * @param token      token
  67      * @param finish     finish
  68      * @param name       name of variable
  69      * @param init       init node or null if just a declaration
  70      */
  71     public VarNode(final int lineNumber, final long token, final int finish, final IdentNode name, final Expression init) {
  72         this(lineNumber, token, finish, name, init, IS_STATEMENT);
  73     }
  74 
  75     private VarNode(final VarNode varNode, final IdentNode name, final Expression init, final int flags) {
  76         super(varNode);
  77         this.name = init == null ? name : name.setIsInitializedHere();
  78         this.init = init;
  79         this.flags = flags;
  80     }
  81 
  82     /**
  83      * Constructor
  84      *
  85      * @param lineNumber line number
  86      * @param token      token
  87      * @param finish     finish
  88      * @param name       name of variable
  89      * @param init       init node or null if just a declaration
  90      * @param flags      flags
  91      */
  92     public VarNode(final int lineNumber, final long token, final int finish, final IdentNode name, final Expression init, final int flags) {


 240         }
 241         return new VarNode(this, name, init, flags);
 242     }
 243 
 244     /**
 245      * Check if a flag is set for this var node
 246      * @param flag flag
 247      * @return true if flag is set
 248      */
 249     public boolean getFlag(final int flag) {
 250         return (flags & flag) == flag;
 251     }
 252 
 253     /**
 254      * Set a flag for this var node
 255      * @param flag flag
 256      * @return new node if flags changed, same otherwise
 257      */
 258     public VarNode setFlag(final int flag) {
 259         return setFlags(flags | flag);
 260     }
 261 
 262     /**
 263      * Returns true if this is a var statement (as opposed to a var initializer in a for loop).
 264      * @return true if this is a var statement (as opposed to a var initializer in a for loop).
 265      */
 266     public boolean isStatement() {
 267         return (flags & IS_STATEMENT) != 0;
 268     }
 269 
 270     /**
 271      * Returns true if this is a function declaration.
 272      * @return true if this is a function declaration.
 273      */
 274     public boolean isFunctionDeclaration() {
 275         return init instanceof FunctionNode && ((FunctionNode)init).isDeclared();
 276     }
 277 
 278     /**
 279      * Returns true if this is an anonymous function declaration.
 280      * @return true if this is an anonymous function declaration.
 281      */
 282     public boolean isAnonymousFunctionDeclaration() {
 283         return isFunctionDeclaration() && ((FunctionNode)init).isAnonymous();
 284     }
 285 }


  28 import jdk.nashorn.internal.ir.annotations.Immutable;
  29 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
  30 import jdk.nashorn.internal.parser.Token;
  31 
  32 /**
  33  * Node represents a var/let declaration.
  34  */
  35 @Immutable
  36 public final class VarNode extends Statement implements Assignment<IdentNode> {
  37     private static final long serialVersionUID = 1L;
  38 
  39     /** Var name. */
  40     private final IdentNode name;
  41 
  42     /** Initialization expression. */
  43     private final Expression init;
  44 
  45     /** Is this a var statement (as opposed to a "var" in a for loop statement) */
  46     private final int flags;
  47 



  48     /** Flag for ES6 LET declaration */
  49     public static final int IS_LET                       = 1 << 0;
  50 
  51     /** Flag for ES6 CONST declaration */
  52     public static final int IS_CONST                     = 1 << 1;
  53 
  54     /** Flag that determines if this is the last function declaration in a function
  55      *  This is used to micro optimize the placement of return value assignments for
  56      *  a program node */
  57     public static final int IS_LAST_FUNCTION_DECLARATION = 1 << 2;
  58 
  59     /**
  60      * Constructor
  61      *
  62      * @param lineNumber line number
  63      * @param token      token
  64      * @param finish     finish
  65      * @param name       name of variable
  66      * @param init       init node or null if just a declaration
  67      */
  68     public VarNode(final int lineNumber, final long token, final int finish, final IdentNode name, final Expression init) {
  69         this(lineNumber, token, finish, name, init, 0);
  70     }
  71 
  72     private VarNode(final VarNode varNode, final IdentNode name, final Expression init, final int flags) {
  73         super(varNode);
  74         this.name = init == null ? name : name.setIsInitializedHere();
  75         this.init = init;
  76         this.flags = flags;
  77     }
  78 
  79     /**
  80      * Constructor
  81      *
  82      * @param lineNumber line number
  83      * @param token      token
  84      * @param finish     finish
  85      * @param name       name of variable
  86      * @param init       init node or null if just a declaration
  87      * @param flags      flags
  88      */
  89     public VarNode(final int lineNumber, final long token, final int finish, final IdentNode name, final Expression init, final int flags) {


 237         }
 238         return new VarNode(this, name, init, flags);
 239     }
 240 
 241     /**
 242      * Check if a flag is set for this var node
 243      * @param flag flag
 244      * @return true if flag is set
 245      */
 246     public boolean getFlag(final int flag) {
 247         return (flags & flag) == flag;
 248     }
 249 
 250     /**
 251      * Set a flag for this var node
 252      * @param flag flag
 253      * @return new node if flags changed, same otherwise
 254      */
 255     public VarNode setFlag(final int flag) {
 256         return setFlags(flags | flag);








 257     }
 258 
 259     /**
 260      * Returns true if this is a function declaration.
 261      * @return true if this is a function declaration.
 262      */
 263     public boolean isFunctionDeclaration() {
 264         return init instanceof FunctionNode && ((FunctionNode)init).isDeclared();
 265     }
 266 
 267     /**
 268      * Returns true if this is an anonymous function declaration.
 269      * @return true if this is an anonymous function declaration.
 270      */
 271     public boolean isAnonymousFunctionDeclaration() {
 272         return isFunctionDeclaration() && ((FunctionNode)init).isAnonymous();
 273     }
 274 }