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 | package com.google.devtools.build.lib.syntax; |
| 15 | |
| 16 | import com.google.common.base.Joiner; |
| 17 | import com.google.common.collect.ImmutableList; |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 19 | import java.util.Map; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Syntax node for an import statement. |
| 23 | */ |
| 24 | public final class LoadStatement extends Statement { |
| 25 | |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 26 | private final ImmutableMap<Identifier, String> symbols; |
| 27 | private final ImmutableList<Identifier> cachedSymbols; // to save time |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 28 | private final StringLiteral imp; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Constructs an import statement. |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 32 | * |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 33 | * <p>{@code symbols} maps a symbol to the original name under which it was defined in |
| 34 | * the bzl file that should be loaded. If aliasing is used, the value differs from its key's |
| 35 | * {@code symbol.getName()}. Otherwise, both values are identical. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | */ |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 37 | LoadStatement(StringLiteral imp, Map<Identifier, String> symbols) { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 38 | this.imp = imp; |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 39 | this.symbols = ImmutableMap.copyOf(symbols); |
| 40 | this.cachedSymbols = ImmutableList.copyOf(symbols.keySet()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 43 | public ImmutableList<Identifier> getSymbols() { |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 44 | return cachedSymbols; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 47 | public StringLiteral getImport() { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 48 | return imp; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String toString() { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 53 | return String.format( |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 54 | "load(\"%s\", %s)", imp.getValue(), Joiner.on(", ").join(cachedSymbols)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 58 | void doExec(Environment env) throws EvalException, InterruptedException { |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 59 | for (Map.Entry<Identifier, String> entry : symbols.entrySet()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | try { |
Jon Brandvein | ee8b7aa | 2016-07-28 15:01:26 +0000 | [diff] [blame] | 61 | Identifier name = entry.getKey(); |
| 62 | Identifier declared = new Identifier(entry.getValue()); |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 63 | |
Jon Brandvein | ee8b7aa | 2016-07-28 15:01:26 +0000 | [diff] [blame] | 64 | if (declared.isPrivate()) { |
| 65 | throw new EvalException(getLocation(), |
| 66 | "symbol '" + declared.getName() + "' is private and cannot be imported."); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | } |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 68 | // The key is the original name that was used to define the symbol |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 69 | // in the loaded bzl file. |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 70 | env.importSymbol(imp.getValue(), name, declared.getName()); |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 71 | } catch (Environment.LoadFailedException e) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | throw new EvalException(getLocation(), e.getMessage()); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void accept(SyntaxTreeVisitor visitor) { |
| 79 | visitor.visit(this); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | void validate(ValidationEnvironment env) throws EvalException { |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 84 | for (Identifier symbol : cachedSymbols) { |
Laurent Le Brun | 964d8d5 | 2015-04-13 12:15:04 +0000 | [diff] [blame] | 85 | env.declare(symbol.getName(), getLocation()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | } |