Debian Codegraph @

src/com/google/javascript/rhino/JSTypeExpression.java

raw symbols SCIP
1 /*
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Rhino code, released
17 * May 6, 1999.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1997-1999
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * Bob Jervis
26 * Google Inc.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License Version 2 or later (the "GPL"), in which
30 * case the provisions of the GPL are applicable instead of those above. If
31 * you wish to allow use of your version of this file only under the terms of
32 * the GPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replacing
34 * them with the notice and other provisions required by the GPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the GPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 package com.google.javascript.rhino;
41
42 import com.google.javascript.rhino.jstype.JSType;
43 import com.google.javascript.rhino.jstype.JSTypeRegistry;
44 import com.google.javascript.rhino.jstype.StaticScope;
45
46 import java.io.Serializable;
47
48
49 /**
50 * Represents a type expression as a miniature Rhino AST, so that the
51 * type expression can be evaluated later.
52 *
53 * @author [email protected] (Nick Santos)
54 */
55 public final class JSTypeExpression implements Serializable {
56 private static final long serialVersionUID = 1L;
57
58 /** The root of the AST. */
59 private final Node root;
60
61 /** The source name where the type expression appears. */
62 private final String sourceName;
63
64 public JSTypeExpression(Node root, String sourceName) {
65 this.root = root;
66 this.sourceName = sourceName;
67 }
68
69 /**
70 * Make the given type expression into an optional type expression,
71 * if possible.
72 */
73 public static JSTypeExpression makeOptionalArg(JSTypeExpression expr) {
74 if (expr.isOptionalArg() || expr.isVarArgs()) {
75 return expr;
76 } else {
77 return new JSTypeExpression(
78 new Node(Token.EQUALS, expr.root), expr.sourceName);
79 }
80 }
81
82 /**
83 * @return Whether this expression denotes an optional {@code @param}.
84 */
85 public boolean isOptionalArg() {
86 return root.getType() == Token.EQUALS;
87 }
88
89 /**
90 * @return Whether this expression denotes a rest args {@code @param}.
91 */
92 public boolean isVarArgs() {
93 return root.getType() == Token.ELLIPSIS;
94 }
95
96 /**
97 * Evaluates the type expression into a {@code JSType} object.
98 */
99 public JSType evaluate(StaticScope<JSType> scope, JSTypeRegistry registry) {
100 JSType type = registry.createFromTypeNodes(root, sourceName, scope);
101 root.setJSType(type);
102 return type;
103 }
104
105 @Override
106 public boolean equals(Object other) {
107 return other instanceof JSTypeExpression &&
108 ((JSTypeExpression) other).root.isEquivalentTo(root);
109 }
110
111 @Override
112 public int hashCode() {
113 return root.toStringTree().hashCode();
114 }
115
116 /**
117 * @return The source for this type expression. Note that it will not
118 * contain an expression if there's an @override tag.
119 */
120 public Node getRoot() {
121 return root;
122 }
123 }