Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/FunctionCall.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.ast;
8
9 import org.mozilla.javascript.Token;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
15 /**
16 * AST node for a function call. Node type is {@link Token#CALL}.<p>
17 */
18 public class FunctionCall extends AstNode {
19
20 protected static final List<AstNode> NO_ARGS =
21 Collections.unmodifiableList(new ArrayList<AstNode>());
22
23 protected AstNode target;
24 protected List<AstNode> arguments;
25 protected int lp = -1;
26 protected int rp = -1;
27
28 {
29 type = Token.CALL;
30 }
31
32 public FunctionCall() {
33 }
34
35 public FunctionCall(int pos) {
36 super(pos);
37 }
38
39 public FunctionCall(int pos, int len) {
40 super(pos, len);
41 }
42
43 /**
44 * Returns node evaluating to the function to call
45 */
46 public AstNode getTarget() {
47 return target;
48 }
49
50 /**
51 * Sets node evaluating to the function to call, and sets
52 * its parent to this node.
53 * @param target node evaluating to the function to call.
54 * @throws IllegalArgumentException} if target is {@code null}
55 */
56 public void setTarget(AstNode target) {
57 assertNotNull(target);
58 this.target = target;
59 target.setParent(this);
60 }
61
62 /**
63 * Returns function argument list
64 * @return function argument list, or an empty immutable list if
65 * there are no arguments.
66 */
67 public List<AstNode> getArguments() {
68 return arguments != null ? arguments : NO_ARGS;
69 }
70
71 /**
72 * Sets function argument list
73 * @param arguments function argument list. Can be {@code null},
74 * in which case any existing args are removed.
75 */
76 public void setArguments(List<AstNode> arguments) {
77 if (arguments == null) {
78 this.arguments = null;
79 } else {
80 if (this.arguments != null)
81 this.arguments.clear();
82 for (AstNode arg : arguments) {
83 addArgument(arg);
84 }
85 }
86 }
87
88 /**
89 * Adds an argument to the list, and sets its parent to this node.
90 * @param arg the argument node to add to the list
91 * @throws IllegalArgumentException} if arg is {@code null}
92 */
93 public void addArgument(AstNode arg) {
94 assertNotNull(arg);
95 if (arguments == null) {
96 arguments = new ArrayList<AstNode>();
97 }
98 arguments.add(arg);
99 arg.setParent(this);
100 }
101
102 /**
103 * Returns left paren position, -1 if missing
104 */
105 public int getLp() {
106 return lp;
107 }
108
109 /**
110 * Sets left paren position
111 * @param lp left paren position
112 */
113 public void setLp(int lp) {
114 this.lp = lp;
115 }
116
117 /**
118 * Returns right paren position, -1 if missing
119 */
120 public int getRp() {
121 return rp;
122 }
123
124 /**
125 * Sets right paren position
126 */
127 public void setRp(int rp) {
128 this.rp = rp;
129 }
130
131 /**
132 * Sets both paren positions
133 */
134 public void setParens(int lp, int rp) {
135 this.lp = lp;
136 this.rp = rp;
137 }
138
139 @Override
140 public String toSource(int depth) {
141 StringBuilder sb = new StringBuilder();
142 sb.append(makeIndent(depth));
143 sb.append(target.toSource(0));
144 sb.append("(");
145 if (arguments != null) {
146 printList(arguments, sb);
147 }
148 sb.append(")");
149 return sb.toString();
150 }
151
152 /**
153 * Visits this node, the target object, and the arguments.
154 */
155 @Override
156 public void visit(NodeVisitor v) {
157 if (v.visit(this)) {
158 target.visit(v);
159 for (AstNode arg : getArguments()) {
160 arg.visit(v);
161 }
162 }
163 }
164 }