blob: 180802141db52908030daa5112e2110fd1125a1c [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
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016import com.google.common.collect.ImmutableList;
Florian Weikert9d659ad2015-07-23 14:44:36 +000017import com.google.common.collect.ImmutableMap;
brandjone2ffd5d2017-06-27 18:14:54 +020018import java.io.IOException;
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
brandjon540aac62017-06-12 23:08:09 +020026 private final ImmutableMap<Identifier, String> symbolMap;
Laurent Le Brun7b1708c2016-10-13 10:05:12 +000027 private final StringLiteral imp;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028
29 /**
30 * Constructs an import statement.
Florian Weikert9d659ad2015-07-23 14:44:36 +000031 *
John Fielda97e17f2015-11-13 02:19:52 +000032 * <p>{@code symbols} maps a symbol to the original name under which it was defined in
33 * the bzl file that should be loaded. If aliasing is used, the value differs from its key's
34 * {@code symbol.getName()}. Otherwise, both values are identical.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035 */
brandjon540aac62017-06-12 23:08:09 +020036 public LoadStatement(StringLiteral imp, Map<Identifier, String> symbolMap) {
John Field1ea7fc32015-12-22 19:37:19 +000037 this.imp = imp;
brandjon540aac62017-06-12 23:08:09 +020038 this.symbolMap = ImmutableMap.copyOf(symbolMap);
brandjon540aac62017-06-12 23:08:09 +020039 }
40
41 public ImmutableMap<Identifier, String> getSymbolMap() {
42 return symbolMap;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043 }
44
Florian Weikert6f864c32015-07-23 11:26:39 +000045 public ImmutableList<Identifier> getSymbols() {
laurentlb3ca2a102017-08-17 13:08:49 +020046 return ImmutableList.copyOf(symbolMap.keySet());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010047 }
48
Laurent Le Brun7b1708c2016-10-13 10:05:12 +000049 public StringLiteral getImport() {
John Field1ea7fc32015-12-22 19:37:19 +000050 return imp;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010051 }
52
53 @Override
brandjone2ffd5d2017-06-27 18:14:54 +020054 public void prettyPrint(Appendable buffer, int indentLevel) throws IOException {
55 printIndent(buffer, indentLevel);
56 buffer.append("load(");
57 imp.prettyPrint(buffer);
laurentlb3ca2a102017-08-17 13:08:49 +020058 for (Identifier symbol : symbolMap.keySet()) {
brandjone2ffd5d2017-06-27 18:14:54 +020059 buffer.append(", ");
60 String origName = symbolMap.get(symbol);
61 if (origName.equals(symbol.getName())) {
62 buffer.append('"');
63 symbol.prettyPrint(buffer);
64 buffer.append('"');
65 } else {
66 symbol.prettyPrint(buffer);
67 buffer.append("=\"");
68 buffer.append(origName);
69 buffer.append('"');
70 }
71 }
72 buffer.append(")\n");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073 }
74
75 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010076 public void accept(SyntaxTreeVisitor visitor) {
77 visitor.visit(this);
78 }
laurentlbaf682d12017-08-24 20:32:02 +020079
80 @Override
81 public Kind kind() {
82 return Kind.LOAD;
83 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010084}