blob: fa0f17558c2e085defb3de28d12f5b06b2cfa1ed [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.
14
15package com.google.devtools.build.lib.skyframe;
16
John Fielda97e17f2015-11-13 02:19:52 +000017import com.google.devtools.build.lib.cmdline.Label;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.devtools.build.lib.syntax.BuildFileAST;
Mark Schaller6df81792015-12-10 18:47:47 +000019import com.google.devtools.build.lib.util.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010020import com.google.devtools.build.skyframe.SkyKey;
21import com.google.devtools.build.skyframe.SkyValue;
22
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023/**
John Fielda97e17f2015-11-13 02:19:52 +000024 * A value that represents an AST file lookup result. There are two subclasses: one for the
25 * case where the file is found, and another for the case where the file is missing (but there
26 * are no other errors).
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027 */
John Fielda97e17f2015-11-13 02:19:52 +000028abstract class ASTFileLookupValue implements SkyValue {
29 public abstract boolean lookupSuccessful();
30 public abstract BuildFileAST getAST();
31 public abstract String getErrorMsg();
32
33 /** If the file is found, this class encapsulates the parsed AST. */
34 private static class ASTLookupWithFile extends ASTFileLookupValue {
35 private final BuildFileAST ast;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010036
John Fielda97e17f2015-11-13 02:19:52 +000037 private ASTLookupWithFile(BuildFileAST ast) {
38 Preconditions.checkNotNull(ast);
39 this.ast = ast;
Michajlo Matijkiw2a7c8022015-09-22 02:22:12 +000040 }
John Fielda97e17f2015-11-13 02:19:52 +000041
42 @Override
43 public boolean lookupSuccessful() {
44 return true;
45 }
46
47 @Override
48 public BuildFileAST getAST() {
49 return this.ast;
50 }
51
52 @Override
53 public String getErrorMsg() {
54 throw new IllegalStateException(
55 "attempted to retrieve unsuccessful lookup reason for successful lookup");
56 }
57 }
58
59 /** If the file isn't found, this class encapsulates a message with the reason. */
60 private static class ASTLookupNoFile extends ASTFileLookupValue {
61 private final String errorMsg;
62
63 private ASTLookupNoFile(String errorMsg) {
64 this.errorMsg = Preconditions.checkNotNull(errorMsg);
65 }
66
67 @Override
68 public boolean lookupSuccessful() {
69 return false;
70 }
71
72 @Override
73 public BuildFileAST getAST() {
74 throw new IllegalStateException("attempted to retrieve AST from an unsuccessful lookup");
75 }
76
77 @Override
78 public String getErrorMsg() {
79 return this.errorMsg;
80 }
81 }
82
83 static ASTFileLookupValue forBadPackage(Label fileLabel, String reason) {
84 return new ASTLookupNoFile(
85 String.format("Unable to load package for '%s': %s", fileLabel, reason));
86 }
87
88 static ASTFileLookupValue forBadFile(Label fileLabel) {
89 return new ASTLookupNoFile(
90 String.format("Unable to load file '%s': file doesn't exist or isn't a file", fileLabel));
91 }
92
93 public static ASTFileLookupValue withFile(BuildFileAST ast) {
94 return new ASTLookupWithFile(ast);
95 }
96
97 static SkyKey key(Label astFileLabel) {
98 return new SkyKey(SkyFunctions.AST_FILE_LOOKUP, astFileLabel);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010099 }
100}