Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
| 17 | import com.google.common.annotations.VisibleForTesting; |
| 18 | import com.google.devtools.build.lib.events.Location; |
| 19 | |
| 20 | import java.io.Serializable; |
| 21 | |
| 22 | /** |
| 23 | * Root class for nodes in the Abstract Syntax Tree of the Build language. |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 24 | * |
| 25 | * The standard {@link Object#equals} and {@link Object#hashCode} methods are not supported. This is |
| 26 | * because their implementation would require traversing the entire tree in the worst case, and we |
| 27 | * don't want this kind of cost to occur implicitly. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | */ |
| 29 | public abstract class ASTNode implements Serializable { |
| 30 | |
| 31 | private Location location; |
| 32 | |
| 33 | protected ASTNode() {} |
| 34 | |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 35 | /** |
| 36 | * Returns whether this node represents a new scope, e.g. a function call. |
| 37 | */ |
| 38 | protected boolean isNewScope() { |
| 39 | return false; |
| 40 | } |
| 41 | |
Janak Ramakrishnan | 3745c90 | 2016-05-11 08:12:10 +0000 | [diff] [blame] | 42 | /** Returns an exception which should be thrown instead of the original one. */ |
| 43 | protected final EvalException maybeTransformException(EvalException original) { |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 44 | // If there is already a non-empty stack trace, we only add this node iff it describes a |
| 45 | // new scope (e.g. FuncallExpression). |
Florian Weikert | 4b67d4f | 2015-09-14 13:35:34 +0000 | [diff] [blame] | 46 | if (original instanceof EvalExceptionWithStackTrace) { |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 47 | EvalExceptionWithStackTrace real = (EvalExceptionWithStackTrace) original; |
Florian Weikert | 4b67d4f | 2015-09-14 13:35:34 +0000 | [diff] [blame] | 48 | if (isNewScope()) { |
| 49 | real.registerNode(this); |
| 50 | } |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 51 | return real; |
| 52 | } |
| 53 | |
Janak Ramakrishnan | 3745c90 | 2016-05-11 08:12:10 +0000 | [diff] [blame] | 54 | if (original.canBeAddedToStackTrace()) { |
| 55 | return new EvalExceptionWithStackTrace(original, this); |
| 56 | } else { |
| 57 | return original; |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 58 | } |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 61 | @VisibleForTesting // productionVisibility = Visibility.PACKAGE_PRIVATE |
| 62 | public void setLocation(Location location) { |
| 63 | this.location = location; |
| 64 | } |
| 65 | |
| 66 | public Location getLocation() { |
| 67 | return location; |
| 68 | } |
| 69 | |
Francois-Rene Rideau | edf7bdb | 2015-03-02 17:12:45 +0000 | [diff] [blame] | 70 | /** @return the same node with its location set, in a slightly more fluent style */ |
| 71 | public static <NODE extends ASTNode> NODE setLocation(Location location, NODE node) { |
| 72 | node.setLocation(location); |
| 73 | return node; |
| 74 | } |
| 75 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | /** |
| 77 | * Print the syntax node in a form useful for debugging. The output is not |
| 78 | * precisely specified, and should not be used by pretty-printing routines. |
| 79 | */ |
| 80 | @Override |
| 81 | public abstract String toString(); |
| 82 | |
| 83 | @Override |
| 84 | public int hashCode() { |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 85 | throw new UnsupportedOperationException(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean equals(Object that) { |
| 90 | throw new UnsupportedOperationException(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Implements the double dispatch by calling into the node specific |
| 95 | * <code>visit</code> method of the {@link SyntaxTreeVisitor} |
| 96 | * |
| 97 | * @param visitor the {@link SyntaxTreeVisitor} instance to dispatch to. |
| 98 | */ |
| 99 | public abstract void accept(SyntaxTreeVisitor visitor); |
| 100 | |
| 101 | } |