001 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 002 // for details. All rights reserved. Use of this source code is governed by a 003 // BSD-style license that can be found in the LICENSE file. 004 005 package com.google.dart.compiler.backend.js.ast; 006 007 import com.google.dart.compiler.util.AstUtil; 008 import org.jetbrains.annotations.NotNull; 009 import org.jetbrains.annotations.Nullable; 010 011 public final class JsBinaryOperation extends JsExpressionImpl { 012 private JsExpression arg1; 013 private JsExpression arg2; 014 015 @NotNull 016 private final JsBinaryOperator op; 017 018 public JsBinaryOperation(@NotNull JsBinaryOperator op) { 019 this(op, null, null); 020 } 021 022 public JsBinaryOperation(@NotNull JsBinaryOperator op, @Nullable JsExpression arg1, @Nullable JsExpression arg2) { 023 this.op = op; 024 this.arg1 = arg1; 025 this.arg2 = arg2; 026 } 027 028 public JsExpression getArg1() { 029 return arg1; 030 } 031 032 public JsExpression getArg2() { 033 return arg2; 034 } 035 036 public void setArg1(JsExpression arg1) { 037 this.arg1 = arg1; 038 } 039 040 public void setArg2(JsExpression arg2) { 041 this.arg2 = arg2; 042 } 043 044 @NotNull 045 public JsBinaryOperator getOperator() { 046 return op; 047 } 048 049 @Override 050 public void accept(JsVisitor v) { 051 v.visitBinaryExpression(this); 052 } 053 054 @Override 055 public void acceptChildren(JsVisitor visitor) { 056 if (op.isAssignment()) { 057 visitor.acceptLvalue(arg1); 058 } 059 else { 060 visitor.accept(arg1); 061 } 062 visitor.accept(arg2); 063 } 064 065 @Override 066 public void traverse(JsVisitorWithContext v, JsContext ctx) { 067 if (v.visit(this, ctx)) { 068 if (op.isAssignment()) { 069 arg1 = v.acceptLvalue(arg1); 070 } else { 071 arg1 = v.accept(arg1); 072 } 073 arg2 = v.accept(arg2); 074 } 075 v.endVisit(this, ctx); 076 } 077 078 @NotNull 079 @Override 080 public JsExpression deepCopy() { 081 return new JsBinaryOperation(op, AstUtil.deepCopy(arg1), AstUtil.deepCopy(arg2)).withMetadataFrom(this); 082 } 083 }