blob: b098eadfaf4aaea573168ac713241b2a5bb9ed3b [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.lib.syntax;
15
16import com.google.common.base.Joiner;
17import com.google.common.collect.ImmutableList;
Florian Weikert9d659ad2015-07-23 14:44:36 +000018import com.google.common.collect.ImmutableMap;
Florian Weikert9d659ad2015-07-23 14:44:36 +000019import java.util.Map;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010020
21/**
22 * Syntax node for an import statement.
23 */
24public final class LoadStatement extends Statement {
25
Florian Weikert9d659ad2015-07-23 14:44:36 +000026 private final ImmutableMap<Identifier, String> symbols;
27 private final ImmutableList<Identifier> cachedSymbols; // to save time
Laurent Le Brun7b1708c2016-10-13 10:05:12 +000028 private final StringLiteral imp;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029
30 /**
31 * Constructs an import statement.
Florian Weikert9d659ad2015-07-23 14:44:36 +000032 *
John Fielda97e17f2015-11-13 02:19:52 +000033 * <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 Nienhuysd08b27f2015-02-25 16:45:20 +010036 */
Laurent Le Brun7b1708c2016-10-13 10:05:12 +000037 LoadStatement(StringLiteral imp, Map<Identifier, String> symbols) {
John Field1ea7fc32015-12-22 19:37:19 +000038 this.imp = imp;
Florian Weikert9d659ad2015-07-23 14:44:36 +000039 this.symbols = ImmutableMap.copyOf(symbols);
40 this.cachedSymbols = ImmutableList.copyOf(symbols.keySet());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041 }
42
Florian Weikert6f864c32015-07-23 11:26:39 +000043 public ImmutableList<Identifier> getSymbols() {
Florian Weikert9d659ad2015-07-23 14:44:36 +000044 return cachedSymbols;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045 }
46
Laurent Le Brun7b1708c2016-10-13 10:05:12 +000047 public StringLiteral getImport() {
John Field1ea7fc32015-12-22 19:37:19 +000048 return imp;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010049 }
50
51 @Override
52 public String toString() {
John Field1ea7fc32015-12-22 19:37:19 +000053 return String.format(
Laurent Le Brun7b1708c2016-10-13 10:05:12 +000054 "load(\"%s\", %s)", imp.getValue(), Joiner.on(", ").join(cachedSymbols));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055 }
56
57 @Override
Florian Weikert90a15962015-09-11 13:43:10 +000058 void doExec(Environment env) throws EvalException, InterruptedException {
Florian Weikert9d659ad2015-07-23 14:44:36 +000059 for (Map.Entry<Identifier, String> entry : symbols.entrySet()) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010060 try {
Jon Brandveinee8b7aa2016-07-28 15:01:26 +000061 Identifier name = entry.getKey();
62 Identifier declared = new Identifier(entry.getValue());
Florian Weikert9d659ad2015-07-23 14:44:36 +000063
Jon Brandveinee8b7aa2016-07-28 15:01:26 +000064 if (declared.isPrivate()) {
65 throw new EvalException(getLocation(),
66 "symbol '" + declared.getName() + "' is private and cannot be imported.");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010067 }
Florian Weikert9d659ad2015-07-23 14:44:36 +000068 // The key is the original name that was used to define the symbol
John Fielda97e17f2015-11-13 02:19:52 +000069 // in the loaded bzl file.
Laurent Le Brun7b1708c2016-10-13 10:05:12 +000070 env.importSymbol(imp.getValue(), name, declared.getName());
Vladimir Moskva4994cb32016-08-10 15:44:31 +000071 } catch (Environment.LoadFailedException e) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010072 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 Weikert9d659ad2015-07-23 14:44:36 +000084 for (Identifier symbol : cachedSymbols) {
Laurent Le Brun964d8d52015-04-13 12:15:04 +000085 env.declare(symbol.getName(), getLocation());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010086 }
87 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010088}