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 | |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 26 | private final ImmutableMap<Identifier, String> symbolMap; |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 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 | */ |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 37 | public LoadStatement(StringLiteral imp, Map<Identifier, String> symbolMap) { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 38 | this.imp = imp; |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 39 | this.symbolMap = ImmutableMap.copyOf(symbolMap); |
| 40 | this.cachedSymbols = ImmutableList.copyOf(symbolMap.keySet()); |
| 41 | } |
| 42 | |
| 43 | public ImmutableMap<Identifier, String> getSymbolMap() { |
| 44 | return symbolMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 47 | public ImmutableList<Identifier> getSymbols() { |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 48 | return cachedSymbols; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 51 | public StringLiteral getImport() { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 52 | return imp; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public String toString() { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 57 | return String.format( |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 58 | "load(\"%s\", %s)", imp.getValue(), Joiner.on(", ").join(cachedSymbols)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 62 | void doExec(Environment env) throws EvalException, InterruptedException { |
laurentlb | ba69b39 | 2017-06-14 15:32:41 +0200 | [diff] [blame] | 63 | if (env.getSemantics().incompatibleLoadArgumentIsLabel) { |
| 64 | String s = imp.getValue(); |
| 65 | if (!s.startsWith("//") && !s.startsWith(":")) { |
| 66 | throw new EvalException( |
| 67 | getLocation(), |
| 68 | "First argument of 'load' must be a label and start with either '//' or ':'. " |
brandjon | f5b8d6f | 2017-06-23 18:03:28 +0200 | [diff] [blame^] | 69 | + "Use --incompatibleLoadArgumentIsLabel=false to temporarily disable this check."); |
laurentlb | ba69b39 | 2017-06-14 15:32:41 +0200 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 73 | for (Map.Entry<Identifier, String> entry : symbolMap.entrySet()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | try { |
Jon Brandvein | ee8b7aa | 2016-07-28 15:01:26 +0000 | [diff] [blame] | 75 | Identifier name = entry.getKey(); |
| 76 | Identifier declared = new Identifier(entry.getValue()); |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 77 | |
Jon Brandvein | ee8b7aa | 2016-07-28 15:01:26 +0000 | [diff] [blame] | 78 | if (declared.isPrivate()) { |
| 79 | throw new EvalException(getLocation(), |
| 80 | "symbol '" + declared.getName() + "' is private and cannot be imported."); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | } |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 82 | // 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] | 83 | // in the loaded bzl file. |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 84 | env.importSymbol(imp.getValue(), name, declared.getName()); |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 85 | } catch (Environment.LoadFailedException e) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | throw new EvalException(getLocation(), e.getMessage()); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void accept(SyntaxTreeVisitor visitor) { |
| 93 | visitor.visit(this); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | void validate(ValidationEnvironment env) throws EvalException { |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 98 | for (Identifier symbol : cachedSymbols) { |
Laurent Le Brun | 964d8d5 | 2015-04-13 12:15:04 +0000 | [diff] [blame] | 99 | env.declare(symbol.getName(), getLocation()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | } |