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 | |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 17 | import com.google.devtools.build.lib.events.Event; |
| 18 | import com.google.devtools.build.lib.events.EventHandler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.events.Location; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import java.util.HashMap; |
| 22 | import java.util.HashSet; |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 23 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | import java.util.Stack; |
| 27 | |
| 28 | /** |
| 29 | * An Environment for the semantic checking of Skylark files. |
| 30 | * |
| 31 | * @see Statement#validate |
| 32 | * @see Expression#validate |
| 33 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 34 | public final class ValidationEnvironment { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | |
| 36 | private final ValidationEnvironment parent; |
| 37 | |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 38 | private final Set<String> variables = new HashSet<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 40 | private final Map<String, Location> variableLocations = new HashMap<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 42 | private final Set<String> readOnlyVariables = new HashSet<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | |
| 44 | // A stack of variable-sets which are read only but can be assigned in different |
| 45 | // branches of if-else statements. |
Laurent Le Brun | e51a4d2 | 2016-10-11 18:04:16 +0000 | [diff] [blame] | 46 | private final Stack<Set<String>> futureReadOnlyVariables = new Stack<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 48 | /** |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 49 | * Create a ValidationEnvironment for a given global Environment. |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 50 | */ |
| 51 | public ValidationEnvironment(Environment env) { |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 52 | Preconditions.checkArgument(env.isGlobal()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | parent = null; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 54 | Set<String> builtinVariables = env.getVariableNames(); |
Laurent Le Brun | 352b9da | 2015-04-16 14:59:59 +0000 | [diff] [blame] | 55 | variables.addAll(builtinVariables); |
| 56 | readOnlyVariables.addAll(builtinVariables); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates a local ValidationEnvironment to validate user defined function bodies. |
| 61 | */ |
Laurent Le Brun | 8d966f7 | 2015-04-15 18:47:34 +0000 | [diff] [blame] | 62 | public ValidationEnvironment(ValidationEnvironment parent) { |
Laurent Le Brun | 8fc603c | 2015-03-24 10:04:50 +0000 | [diff] [blame] | 63 | // Don't copy readOnlyVariables: Variables may shadow global values. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | this.parent = parent; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns true if this ValidationEnvironment is top level i.e. has no parent. |
| 69 | */ |
| 70 | public boolean isTopLevel() { |
| 71 | return parent == null; |
| 72 | } |
| 73 | |
| 74 | /** |
Laurent Le Brun | 964d8d5 | 2015-04-13 12:15:04 +0000 | [diff] [blame] | 75 | * Declare a variable and add it to the environment. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | */ |
Laurent Le Brun | 964d8d5 | 2015-04-13 12:15:04 +0000 | [diff] [blame] | 77 | public void declare(String varname, Location location) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | throws EvalException { |
| 79 | checkReadonly(varname, location); |
| 80 | if (parent == null) { // top-level values are immutable |
| 81 | readOnlyVariables.add(varname); |
| 82 | if (!futureReadOnlyVariables.isEmpty()) { |
| 83 | // Currently validating an if-else statement |
| 84 | futureReadOnlyVariables.peek().add(varname); |
| 85 | } |
| 86 | } |
Laurent Le Brun | 352b9da | 2015-04-16 14:59:59 +0000 | [diff] [blame] | 87 | variables.add(varname); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | variableLocations.put(varname, location); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private void checkReadonly(String varname, Location location) throws EvalException { |
| 92 | if (readOnlyVariables.contains(varname)) { |
Laurent Le Brun | fa407e5 | 2016-11-04 15:53:08 +0000 | [diff] [blame] | 93 | throw new EvalException( |
| 94 | location, |
| 95 | String.format("Variable %s is read only", varname), |
| 96 | "https://bazel.build/versions/master/docs/skylark/errors/read-only-variable.html"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | /** |
| 101 | * Returns true if the symbol exists in the validation environment. |
| 102 | */ |
| 103 | public boolean hasSymbolInEnvironment(String varname) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 104 | return variables.contains(varname) |
| 105 | || (parent != null && topLevel().variables.contains(varname)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Laurent Le Brun | e102a2d | 2017-01-02 12:06:18 +0000 | [diff] [blame] | 108 | /** Returns the set of all accessible symbols (both local and global) */ |
| 109 | public Set<String> getAllSymbols() { |
| 110 | Set<String> all = new HashSet<>(); |
| 111 | all.addAll(variables); |
| 112 | if (parent != null) { |
| 113 | all.addAll(parent.getAllSymbols()); |
| 114 | } |
| 115 | return all; |
| 116 | } |
| 117 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | private ValidationEnvironment topLevel() { |
| 119 | return Preconditions.checkNotNull(parent == null ? this : parent); |
| 120 | } |
| 121 | |
| 122 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 123 | * Starts a session with temporarily disabled readonly checking for variables between branches. |
| 124 | * This is useful to validate control flows like if-else when we know that certain parts of the |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 125 | * code cannot both be executed. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 126 | */ |
| 127 | public void startTemporarilyDisableReadonlyCheckSession() { |
| 128 | futureReadOnlyVariables.add(new HashSet<String>()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Finishes the session with temporarily disabled readonly checking. |
| 133 | */ |
| 134 | public void finishTemporarilyDisableReadonlyCheckSession() { |
| 135 | Set<String> variables = futureReadOnlyVariables.pop(); |
| 136 | readOnlyVariables.addAll(variables); |
| 137 | if (!futureReadOnlyVariables.isEmpty()) { |
| 138 | futureReadOnlyVariables.peek().addAll(variables); |
| 139 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Finishes a branch of temporarily disabled readonly checking. |
| 144 | */ |
| 145 | public void finishTemporarilyDisableReadonlyCheckBranch() { |
| 146 | readOnlyVariables.removeAll(futureReadOnlyVariables.peek()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 147 | } |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Validates the AST and runs static checks. |
| 151 | */ |
| 152 | public void validateAst(List<Statement> statements) throws EvalException { |
| 153 | // Add every function in the environment before validating. This is |
| 154 | // necessary because functions may call other functions defined |
| 155 | // later in the file. |
| 156 | for (Statement statement : statements) { |
| 157 | if (statement instanceof FunctionDefStatement) { |
| 158 | FunctionDefStatement fct = (FunctionDefStatement) statement; |
| 159 | declare(fct.getIdent().getName(), fct.getLocation()); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | for (Statement statement : statements) { |
| 164 | statement.validate(this); |
| 165 | } |
| 166 | } |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 167 | |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 168 | public boolean validateAst(List<Statement> statements, EventHandler eventHandler) { |
| 169 | try { |
| 170 | validateAst(statements); |
| 171 | return true; |
| 172 | } catch (EvalException e) { |
| 173 | if (!e.isDueToIncompleteAST()) { |
| 174 | eventHandler.handle(Event.error(e.getLocation(), e.getMessage())); |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 179 | } |