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 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Interner; |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.cmdline.Label; |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.concurrent.BlazeInterners; |
| 21 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.syntax.StarlarkFile; |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 23 | import com.google.devtools.build.skyframe.AbstractSkyKey; |
nharmata | 82cfb55 | 2018-03-28 10:43:48 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.skyframe.NotComparableSkyValue; |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 25 | import com.google.devtools.build.skyframe.SkyFunctionName; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | /** |
Janak Ramakrishnan | d7d951d | 2017-02-15 05:41:19 +0000 | [diff] [blame] | 28 | * A value that represents an AST file lookup result. There are two subclasses: one for the case |
| 29 | * where the file is found, and another for the case where the file is missing (but there are no |
| 30 | * other errors). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | */ |
nharmata | 82cfb55 | 2018-03-28 10:43:48 -0700 | [diff] [blame] | 32 | // In practice, if a ASTFileLookupValue is re-computed (i.e. not changed pruned), then it will |
| 33 | // almost certainly be unequal to the previous value. This is because of (i) the change-pruning |
| 34 | // semantics of the PackageLookupValue dep and the FileValue dep; consider the latter: if the |
| 35 | // FileValue for the bzl file has changed, then the contents of the bzl file probably changed and |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 36 | // (ii) we don't currently have skylark-semantic-equality in StarlarkFile, so two StarlarkFile |
nharmata | 82cfb55 | 2018-03-28 10:43:48 -0700 | [diff] [blame] | 37 | // instances representing two different contents of a bzl file will be different. |
| 38 | // TODO(bazel-team): Consider doing better here. As a pre-req, we would need |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 39 | // skylark-semantic-equality in StarlarkFile, rather than equality naively based on the contents of |
nharmata | 82cfb55 | 2018-03-28 10:43:48 -0700 | [diff] [blame] | 40 | // the bzl file. For a concrete example, the contents of comment lines do not currently impact |
| 41 | // skylark semantics. |
| 42 | public abstract class ASTFileLookupValue implements NotComparableSkyValue { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 43 | public abstract boolean lookupSuccessful(); |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 44 | |
| 45 | public abstract StarlarkFile getAST(); |
| 46 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 47 | public abstract String getErrorMsg(); |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 48 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 49 | /** If the file is found, this class encapsulates the parsed AST. */ |
shahan | 3827f0a | 2018-04-09 11:37:30 -0700 | [diff] [blame] | 50 | @AutoCodec.VisibleForSerialization |
| 51 | public static class ASTLookupWithFile extends ASTFileLookupValue { |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 52 | private final StarlarkFile ast; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 54 | private ASTLookupWithFile(StarlarkFile ast) { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 55 | Preconditions.checkNotNull(ast); |
| 56 | this.ast = ast; |
Michajlo Matijkiw | 2a7c802 | 2015-09-22 02:22:12 +0000 | [diff] [blame] | 57 | } |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 58 | |
| 59 | @Override |
| 60 | public boolean lookupSuccessful() { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | @Override |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 65 | public StarlarkFile getAST() { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 66 | return this.ast; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String getErrorMsg() { |
| 71 | throw new IllegalStateException( |
| 72 | "attempted to retrieve unsuccessful lookup reason for successful lookup"); |
| 73 | } |
| 74 | } |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 75 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 76 | /** If the file isn't found, this class encapsulates a message with the reason. */ |
shahan | 3827f0a | 2018-04-09 11:37:30 -0700 | [diff] [blame] | 77 | @AutoCodec.VisibleForSerialization |
| 78 | public static class ASTLookupNoFile extends ASTFileLookupValue { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 79 | private final String errorMsg; |
| 80 | |
| 81 | private ASTLookupNoFile(String errorMsg) { |
| 82 | this.errorMsg = Preconditions.checkNotNull(errorMsg); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean lookupSuccessful() { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | @Override |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 91 | public StarlarkFile getAST() { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 92 | throw new IllegalStateException("attempted to retrieve AST from an unsuccessful lookup"); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String getErrorMsg() { |
| 97 | return this.errorMsg; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static ASTFileLookupValue forBadPackage(Label fileLabel, String reason) { |
| 102 | return new ASTLookupNoFile( |
| 103 | String.format("Unable to load package for '%s': %s", fileLabel, reason)); |
| 104 | } |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 105 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 106 | static ASTFileLookupValue forMissingFile(Label fileLabel) { |
| 107 | return new ASTLookupNoFile( |
| 108 | String.format("Unable to load file '%s': file doesn't exist", fileLabel)); |
| 109 | } |
| 110 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 111 | static ASTFileLookupValue forBadFile(Label fileLabel) { |
| 112 | return new ASTLookupNoFile( |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 113 | String.format("Unable to load file '%s': it isn't a regular file", fileLabel)); |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 114 | } |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 115 | |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 116 | public static ASTFileLookupValue withFile(StarlarkFile ast) { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 117 | return new ASTLookupWithFile(ast); |
| 118 | } |
| 119 | |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 120 | public static Key key(Label astFileLabel) { |
| 121 | return ASTFileLookupValue.Key.create(astFileLabel); |
| 122 | } |
| 123 | |
| 124 | @AutoCodec.VisibleForSerialization |
| 125 | @AutoCodec |
| 126 | static class Key extends AbstractSkyKey<Label> { |
| 127 | private static final Interner<Key> interner = BlazeInterners.newWeakInterner(); |
| 128 | |
| 129 | private Key(Label arg) { |
| 130 | super(arg); |
| 131 | } |
| 132 | |
| 133 | @AutoCodec.VisibleForSerialization |
| 134 | @AutoCodec.Instantiator |
| 135 | static Key create(Label arg) { |
| 136 | return interner.intern(new Key(arg)); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public SkyFunctionName functionName() { |
| 141 | return SkyFunctions.AST_FILE_LOOKUP; |
| 142 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 143 | } |
| 144 | } |