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 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 17 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.devtools.build.lib.syntax.BuildFileAST; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.skyframe.SkyKey; |
| 21 | import com.google.devtools.build.skyframe.SkyValue; |
| 22 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | /** |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 24 | * 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | */ |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 28 | abstract 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 37 | private ASTLookupWithFile(BuildFileAST ast) { |
| 38 | Preconditions.checkNotNull(ast); |
| 39 | this.ast = ast; |
Michajlo Matijkiw | 2a7c802 | 2015-09-22 02:22:12 +0000 | [diff] [blame] | 40 | } |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 41 | |
| 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | } |
| 100 | } |