blob: 414429ebd2ee6ab513296246e2e6ff4a121934cd [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001// Copyright 2014 Google Inc. All rights reserved.
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.
14package com.google.devtools.build.lib.syntax;
15
16import com.google.common.base.Joiner;
17import com.google.common.collect.ImmutableList;
18import com.google.devtools.build.lib.vfs.PathFragment;
19
20import java.util.List;
21
22/**
23 * Syntax node for an import statement.
24 */
25public final class LoadStatement extends Statement {
26
Googlerecb643d2015-02-26 10:22:49 +000027 public static final String PATH_ERROR_MSG = "Path '%s' is not valid. "
28 + "It should either start with a slash or refer to a file in the current directory.";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029 private final ImmutableList<Ident> symbols;
30 private final PathFragment importPath;
31
32 /**
33 * Constructs an import statement.
34 */
35 LoadStatement(String path, List<Ident> symbols) {
36 this.symbols = ImmutableList.copyOf(symbols);
37 this.importPath = new PathFragment(path + ".bzl");
38 }
39
40 public ImmutableList<Ident> getSymbols() {
41 return symbols;
42 }
43
44 public PathFragment getImportPath() {
45 return importPath;
46 }
47
48 @Override
49 public String toString() {
50 return String.format("load(\"%s\", %s)", importPath, Joiner.on(", ").join(symbols));
51 }
52
53 @Override
54 void exec(Environment env) throws EvalException, InterruptedException {
55 for (Ident i : symbols) {
56 try {
57 if (i.getName().startsWith("_")) {
58 throw new EvalException(getLocation(), "symbol '" + i + "' is private and cannot "
59 + "be imported");
60 }
61 env.importSymbol(getImportPath(), i.getName());
62 } catch (Environment.NoSuchVariableException | Environment.LoadFailedException e) {
63 throw new EvalException(getLocation(), e.getMessage());
64 }
65 }
66 }
67
68 @Override
69 public void accept(SyntaxTreeVisitor visitor) {
70 visitor.visit(this);
71 }
72
73 @Override
74 void validate(ValidationEnvironment env) throws EvalException {
Googlerecb643d2015-02-26 10:22:49 +000075 if (!importPath.isAbsolute() && importPath.segmentCount() > 1) {
76 throw new EvalException(getLocation(), String.format(PATH_ERROR_MSG, importPath));
77 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010078 for (Ident symbol : symbols) {
Laurent Le Brun964d8d52015-04-13 12:15:04 +000079 env.declare(symbol.getName(), getLocation());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010080 }
81 }
82}