|
| 1 | +//===------------- IncrementalRanges.h -----------------------------*- C++ |
| 2 | +//-*-===// |
| 3 | +// |
| 4 | +// This source file is part of the Swift.org open source project |
| 5 | +// |
| 6 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 7 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 8 | +// |
| 9 | +// See https://swift.org/LICENSE.txt for license information |
| 10 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef SWIFT_AST_INCREMENTALRANGES_H |
| 15 | +#define SWIFT_AST_INCREMENTALRANGES_H |
| 16 | + |
| 17 | +// These are the declarations for managing serializable source locations so that |
| 18 | +// the frontend and the driver can implement incremental compilation based on |
| 19 | +// source ranges. |
| 20 | + |
| 21 | +#include "swift/Basic/LLVM.h" |
| 22 | +#include "swift/Basic/NullablePtr.h" |
| 23 | +#include "swift/Basic/SourceLoc.h" |
| 24 | +#include "swift/Basic/StringExtras.h" |
| 25 | +#include "llvm/Support/YAMLTraits.h" |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +namespace swift { |
| 29 | +class PersistentParserState; |
| 30 | +class SourceManager; |
| 31 | +class DiagnosticEngine; |
| 32 | +class SourceFile; |
| 33 | +} // namespace swift |
| 34 | + |
| 35 | +//============================================================================== |
| 36 | +// MARK: Range types |
| 37 | +//============================================================================== |
| 38 | + |
| 39 | +namespace swift { |
| 40 | +namespace incremental_ranges { |
| 41 | + |
| 42 | +struct SerializableSourceRange; |
| 43 | + |
| 44 | +typedef std::vector<SerializableSourceRange> Ranges; |
| 45 | +typedef std::map<std::string, Ranges> RangesByFilename; |
| 46 | +} // namespace incremental_ranges |
| 47 | +} // namespace swift |
| 48 | + |
| 49 | +//============================================================================== |
| 50 | +// MARK: SerializableSourceLocation |
| 51 | +//============================================================================== |
| 52 | +namespace swift { |
| 53 | +namespace incremental_ranges { |
| 54 | + |
| 55 | +/// A source location that can be written from the frontend and read by the |
| 56 | +/// driver. 1-origin: lines and columns start at 1 |
| 57 | +struct SerializableSourceLocation { |
| 58 | + uint64_t line = 0; |
| 59 | + uint64_t column = 0; |
| 60 | + |
| 61 | + SerializableSourceLocation(const SourceLoc loc, const SourceManager &SM); |
| 62 | + SerializableSourceLocation(uint64_t line, uint64_t column) |
| 63 | + : line(line), column(column) {} |
| 64 | + SerializableSourceLocation() = default; |
| 65 | + static const SerializableSourceLocation endOfAnyFile; |
| 66 | + |
| 67 | + bool operator< (const SerializableSourceLocation &x) const { |
| 68 | + return line < x.line ? true |
| 69 | + : line > x.line ? false |
| 70 | + : column < x.column; |
| 71 | + } |
| 72 | + bool operator==(const SerializableSourceLocation &x) const { |
| 73 | + return line == x.line && column == x.column; |
| 74 | + } |
| 75 | + bool operator<=(const SerializableSourceLocation &x) const { |
| 76 | + return *this < x || *this == x; |
| 77 | + } |
| 78 | + void print(raw_ostream &out) const; |
| 79 | + void dump() const; |
| 80 | +}; |
| 81 | + |
| 82 | +} // namespace incremental_ranges |
| 83 | +} // namespace swift |
| 84 | + |
| 85 | +template <> |
| 86 | +struct llvm::yaml::MappingTraits< |
| 87 | + swift::incremental_ranges::SerializableSourceLocation> { |
| 88 | + static const bool flow = true; |
| 89 | + static void |
| 90 | + mapping(llvm::yaml::IO &io, |
| 91 | + swift::incremental_ranges::SerializableSourceLocation &loc) { |
| 92 | + io.mapRequired("line", loc.line), io.mapRequired("column", loc.column); |
| 93 | + } |
| 94 | +}; |
| 95 | +//============================================================================== |
| 96 | +// MARK: SerializableSourceRange |
| 97 | +//============================================================================== |
| 98 | + |
| 99 | +namespace swift { |
| 100 | +namespace incremental_ranges { |
| 101 | +/// A range in the source, that can be written by the frontend and read by the |
| 102 | +/// driver. Half-open, to facilitate representing empty ranges. In other words, |
| 103 | +/// an empty region will have start == end |
| 104 | +struct SerializableSourceRange { |
| 105 | + SerializableSourceLocation start, end; |
| 106 | + |
| 107 | + SerializableSourceRange(const CharSourceRange r, const SourceManager &SM); |
| 108 | + SerializableSourceRange(SerializableSourceLocation start, |
| 109 | + SerializableSourceLocation end); |
| 110 | + SerializableSourceRange() = default; |
| 111 | + |
| 112 | + static const SerializableSourceRange wholeFile; |
| 113 | + static Ranges RangesForWholeFile(); |
| 114 | + |
| 115 | + bool isEmpty() const { return start == end; } |
| 116 | + |
| 117 | + bool overlaps(const SerializableSourceRange &x) const { |
| 118 | + return start < x.end && x.start < end; |
| 119 | + } |
| 120 | + bool operator==(const SerializableSourceRange &x) const { |
| 121 | + return start == x.start && end == x.end; |
| 122 | + } |
| 123 | + bool isImproperSubsetOf(const SerializableSourceRange &) const; |
| 124 | + bool properlyPreceeds(const SerializableSourceRange &) const; |
| 125 | + static bool isProperlySorted(ArrayRef<SerializableSourceRange>); |
| 126 | + |
| 127 | + bool |
| 128 | + isImproperSubsetOfAny(ArrayRef<SerializableSourceRange> supersetRanges) const; |
| 129 | + bool isImproperSubsetOfAnySlowlyAndSimply( |
| 130 | + ArrayRef<SerializableSourceRange> supersetRanges) const; |
| 131 | + |
| 132 | + /// Optimized for fewer ranges in the subset |
| 133 | + /// Return first outlier found in subset not in superset |
| 134 | + static Optional<SerializableSourceRange> |
| 135 | + findOutlierIfAny(ArrayRef<SerializableSourceRange> subset, |
| 136 | + ArrayRef<SerializableSourceRange> superset); |
| 137 | + |
| 138 | + static Ranges findAllOutliers(ArrayRef<SerializableSourceRange> subset, |
| 139 | + ArrayRef<SerializableSourceRange> superset); |
| 140 | + |
| 141 | + std::string printString() const; |
| 142 | + void print(raw_ostream &out) const; |
| 143 | + void dump() const; |
| 144 | +}; |
| 145 | + |
| 146 | +} // namespace incremental_ranges |
| 147 | +} // namespace swift |
| 148 | + |
| 149 | + |
| 150 | +template <> |
| 151 | +struct llvm::yaml::MappingTraits< |
| 152 | + swift::incremental_ranges::SerializableSourceRange> { |
| 153 | + static const bool flow = true; |
| 154 | + static void mapping(llvm::yaml::IO &io, |
| 155 | + swift::incremental_ranges::SerializableSourceRange &sr) { |
| 156 | + io.mapRequired("start", sr.start), io.mapRequired("end", sr.end); |
| 157 | + } |
| 158 | +}; |
| 159 | + |
| 160 | +//============================================================================== |
| 161 | +// MARK: SwiftRangesFileContents |
| 162 | +//============================================================================== |
| 163 | + |
| 164 | +namespace swift { |
| 165 | +namespace incremental_ranges { |
| 166 | + |
| 167 | +/// The complete contents of the file written by the frontend and read by the |
| 168 | +/// driver containing source range information for one primary input file. |
| 169 | +struct SwiftRangesFileContents { |
| 170 | + /// For each non-primary, the unparsed ranges in it. |
| 171 | + /// At present these represent the bodies of types defined in the nonprimary |
| 172 | + /// that are not used in the primary. |
| 173 | + RangesByFilename unparsedRangesByNonPrimary; |
| 174 | + Ranges noninlinableFunctionBodies; |
| 175 | + |
| 176 | + SwiftRangesFileContents() : SwiftRangesFileContents({}, {}) {} |
| 177 | + |
| 178 | + SwiftRangesFileContents(RangesByFilename &&unparsedRangesByNonPrimary, |
| 179 | + Ranges &&noninlinableFunctionBodies) |
| 180 | + : unparsedRangesByNonPrimary(std::move(unparsedRangesByNonPrimary)), |
| 181 | + noninlinableFunctionBodies(std::move(noninlinableFunctionBodies)) {} |
| 182 | + |
| 183 | + /// Return None for error. |
| 184 | + static Optional<SwiftRangesFileContents> |
| 185 | + load(const StringRef primaryPath, const llvm::MemoryBuffer &swiftRangesBuffer, |
| 186 | + const bool showIncrementalBuildDecisions, DiagnosticEngine &diags); |
| 187 | + |
| 188 | + void dump(StringRef primaryFilename) const; |
| 189 | + |
| 190 | + static constexpr const char *header = "### Swift source ranges file v0 ###\n"; |
| 191 | +}; |
| 192 | +} // namespace incremental_ranges |
| 193 | +} // namespace swift |
| 194 | + |
| 195 | +template <> |
| 196 | +struct llvm::yaml::MappingTraits< |
| 197 | + swift::incremental_ranges::SwiftRangesFileContents> { |
| 198 | + static void |
| 199 | + mapping(llvm::yaml::IO &io, |
| 200 | + swift::incremental_ranges::SwiftRangesFileContents &srfc) { |
| 201 | + io.mapRequired("unparsedRangesByNonPrimary", |
| 202 | + srfc.unparsedRangesByNonPrimary); |
| 203 | + io.mapRequired("noninlinableFunctionBodies", |
| 204 | + srfc.noninlinableFunctionBodies); |
| 205 | + } |
| 206 | +}; |
| 207 | + |
| 208 | +LLVM_YAML_IS_SEQUENCE_VECTOR(swift::incremental_ranges::SerializableSourceRange) |
| 209 | +LLVM_YAML_IS_STRING_MAP(swift::incremental_ranges::Ranges) |
| 210 | +LLVM_YAML_IS_STRING_MAP(swift::incremental_ranges::RangesByFilename) |
| 211 | + |
| 212 | +//============================================================================== |
| 213 | +// MARK: SwiftRangesEmitter |
| 214 | +//============================================================================== |
| 215 | +namespace swift { |
| 216 | +namespace incremental_ranges { |
| 217 | +/// Gathers up the information from the frontend, processes it, and writes it. |
| 218 | +class SwiftRangesEmitter { |
| 219 | + const StringRef outputPath; |
| 220 | + SourceFile *const primaryFile; |
| 221 | + const PersistentParserState &persistentState; |
| 222 | + const SourceManager &sourceMgr; |
| 223 | + DiagnosticEngine &diags; |
| 224 | + |
| 225 | +public: |
| 226 | + SwiftRangesEmitter(StringRef outputPath, SourceFile *primaryFile, |
| 227 | + const PersistentParserState &persistentState, |
| 228 | + const SourceManager &sourceMgr, DiagnosticEngine &diags) |
| 229 | + : outputPath(outputPath), primaryFile(primaryFile), |
| 230 | + persistentState(persistentState), sourceMgr(sourceMgr), diags(diags) {} |
| 231 | + |
| 232 | + /// True for error |
| 233 | + bool emit() const; |
| 234 | + |
| 235 | +public: |
| 236 | + void emitRanges(llvm::raw_ostream &out) const; |
| 237 | + |
| 238 | +private: |
| 239 | + RangesByFilename collectSerializedUnparsedRangesByNonPrimary() const; |
| 240 | + |
| 241 | + Ranges collectSortedSerializedNoninlinableFunctionBodies() const; |
| 242 | + std::vector<CharSourceRange> collectNoninlinableFunctionBodies() const; |
| 243 | + |
| 244 | + std::map<std::string, std::vector<CharSourceRange>> |
| 245 | + collectUnparsedRanges() const; |
| 246 | + std::vector<CharSourceRange> |
| 247 | + sortRanges(std::vector<CharSourceRange> ranges) const; |
| 248 | + |
| 249 | + /// Assuming \p ranges is sorted, coalesce overlapping ranges in place and |
| 250 | + /// return end of the resultant vector. |
| 251 | + std::vector<CharSourceRange> |
| 252 | + coalesceSortedRanges(std::vector<CharSourceRange>) const; |
| 253 | + |
| 254 | + std::vector<SerializableSourceRange> |
| 255 | + serializeRanges(std::vector<CharSourceRange> ranges) const; |
| 256 | + |
| 257 | + bool isImmediatelyBeforeOrOverlapping(CharSourceRange prev, |
| 258 | + CharSourceRange next) const; |
| 259 | +}; |
| 260 | +} // namespace incremental_ranges |
| 261 | +} // namespace swift |
| 262 | + |
| 263 | +//============================================================================== |
| 264 | +// MARK: CompiledSourceEmitter |
| 265 | +//============================================================================== |
| 266 | +namespace swift { |
| 267 | +namespace incremental_ranges { |
| 268 | +/// The class that writes out the unchanged source code in the primary input so |
| 269 | +/// that the driver can diff it later, after the user has changed the file. |
| 270 | +class CompiledSourceEmitter { |
| 271 | + const StringRef outputPath; |
| 272 | + const SourceFile *const primaryFile; |
| 273 | + const SourceManager &sourceMgr; |
| 274 | + DiagnosticEngine &diags; |
| 275 | + |
| 276 | +public: |
| 277 | + CompiledSourceEmitter(StringRef outputPath, const SourceFile *primaryFile, |
| 278 | + const SourceManager &sourceMgr, DiagnosticEngine &diags) |
| 279 | + : outputPath(outputPath), primaryFile(primaryFile), sourceMgr(sourceMgr), |
| 280 | + diags(diags) {} |
| 281 | + |
| 282 | + /// True for error |
| 283 | + bool emit(); |
| 284 | +}; |
| 285 | + |
| 286 | +} // namespace incremental_ranges |
| 287 | +} // namespace swift |
| 288 | + |
| 289 | +#endif // SWIFT_AST_INCREMENTALRANGES_H |
0 commit comments