|
| 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 | +// Summary: TBD |
| 18 | + |
| 19 | +#include "swift/Basic/LLVM.h" |
| 20 | +#include "swift/Basic/NullablePtr.h" |
| 21 | +#include "swift/Basic/SourceLoc.h" |
| 22 | +#include "swift/Basic/StringExtras.h" |
| 23 | +#include "llvm/Support/YAMLTraits.h" |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace swift { |
| 27 | +class PersistentParserState; |
| 28 | +class SourceManager; |
| 29 | +class DiagnosticEngine; |
| 30 | +class SourceFile; |
| 31 | +} // namespace swift |
| 32 | + |
| 33 | +//============================================================================== |
| 34 | +// MARK: Range types |
| 35 | +//============================================================================== |
| 36 | + |
| 37 | +namespace swift { |
| 38 | +namespace incremental_ranges { |
| 39 | + |
| 40 | +struct SerializableSourceRange; |
| 41 | + |
| 42 | +typedef std::vector<SerializableSourceRange> Ranges; |
| 43 | +typedef std::map<std::string, Ranges> RangesByFilename; |
| 44 | +} // namespace incremental_ranges |
| 45 | +} // namespace swift |
| 46 | + |
| 47 | +//============================================================================== |
| 48 | +// MARK: SerializableSourceLocation |
| 49 | +//============================================================================== |
| 50 | +namespace swift { |
| 51 | +namespace incremental_ranges { |
| 52 | + |
| 53 | +/// 1-origin |
| 54 | +struct SerializableSourceLocation { |
| 55 | + uint64_t line = 0; |
| 56 | + uint64_t column = 0; |
| 57 | + |
| 58 | + SerializableSourceLocation(const SourceLoc loc, const SourceManager &SM); |
| 59 | + SerializableSourceLocation(uint64_t line, uint64_t column) |
| 60 | + : line(line), column(column) {} |
| 61 | + SerializableSourceLocation() = default; |
| 62 | + static const SerializableSourceLocation endOfAnyFile; |
| 63 | + |
| 64 | + bool operator< (const SerializableSourceLocation &x) const { |
| 65 | + return line < x.line ? true |
| 66 | + : line > x.line ? false |
| 67 | + : column < x.column; |
| 68 | + } |
| 69 | + bool operator==(const SerializableSourceLocation &x) const { |
| 70 | + return line == x.line && column == x.column; |
| 71 | + } |
| 72 | + bool operator<=(const SerializableSourceLocation &x) const { |
| 73 | + return *this < x || *this == x; |
| 74 | + } |
| 75 | + void print(raw_ostream &out) const; |
| 76 | + void dump() const; |
| 77 | +}; |
| 78 | + |
| 79 | +} // namespace incremental_ranges |
| 80 | +} // namespace swift |
| 81 | + |
| 82 | +template <> |
| 83 | +struct llvm::yaml::MappingTraits< |
| 84 | + swift::incremental_ranges::SerializableSourceLocation> { |
| 85 | + static const bool flow = true; |
| 86 | + static void |
| 87 | + mapping(llvm::yaml::IO &io, |
| 88 | + swift::incremental_ranges::SerializableSourceLocation &loc) { |
| 89 | + io.mapRequired("line", loc.line), io.mapRequired("column", loc.column); |
| 90 | + } |
| 91 | +}; |
| 92 | +//============================================================================== |
| 93 | +// MARK: SerializableSourceRange |
| 94 | +//============================================================================== |
| 95 | + |
| 96 | +namespace swift { |
| 97 | +namespace incremental_ranges { |
| 98 | +/// half-open, to facilitate representing empty ranges |
| 99 | +struct SerializableSourceRange { |
| 100 | + SerializableSourceLocation start, end; |
| 101 | + |
| 102 | + SerializableSourceRange(const CharSourceRange r, const SourceManager &SM); |
| 103 | + SerializableSourceRange(SerializableSourceLocation start, |
| 104 | + SerializableSourceLocation end); |
| 105 | + SerializableSourceRange() = default; |
| 106 | + |
| 107 | + static const SerializableSourceRange wholeFile; |
| 108 | + static Ranges RangesForWholeFile(); |
| 109 | + |
| 110 | + bool isEmpty() const { return start == end; } |
| 111 | + |
| 112 | + bool overlaps(const SerializableSourceRange &x) const { |
| 113 | + return start < x.end && x.start < end; |
| 114 | + } |
| 115 | + bool operator==(const SerializableSourceRange &x) const { |
| 116 | + return start == x.start && end == x.end; |
| 117 | + } |
| 118 | + bool isImproperSubsetOf(const SerializableSourceRange &) const; |
| 119 | + bool properlyPreceeds(const SerializableSourceRange &) const; |
| 120 | + static bool isProperlySorted(ArrayRef<SerializableSourceRange>); |
| 121 | + |
| 122 | + bool |
| 123 | + isImproperSubsetOfAny(ArrayRef<SerializableSourceRange> supersetRanges) const; |
| 124 | + bool isImproperSubsetOfAnySlowlyAndSimply( |
| 125 | + ArrayRef<SerializableSourceRange> supersetRanges) const; |
| 126 | + |
| 127 | + /// Optimized for fewer ranges in the subset |
| 128 | + /// Return first outlier found in subset not in superset |
| 129 | + static Optional<SerializableSourceRange> |
| 130 | + findOutlierIfAny(ArrayRef<SerializableSourceRange> subset, |
| 131 | + ArrayRef<SerializableSourceRange> superset); |
| 132 | + |
| 133 | + static Ranges findAllOutliers(ArrayRef<SerializableSourceRange> subset, |
| 134 | + ArrayRef<SerializableSourceRange> superset); |
| 135 | + |
| 136 | + std::string printString() const; |
| 137 | + void print(raw_ostream &out) const; |
| 138 | + void dump() const; |
| 139 | +}; |
| 140 | + |
| 141 | +} // namespace incremental_ranges |
| 142 | +} // namespace swift |
| 143 | + |
| 144 | + |
| 145 | +template <> |
| 146 | +struct llvm::yaml::MappingTraits< |
| 147 | + swift::incremental_ranges::SerializableSourceRange> { |
| 148 | + static const bool flow = true; |
| 149 | + static void mapping(llvm::yaml::IO &io, |
| 150 | + swift::incremental_ranges::SerializableSourceRange &sr) { |
| 151 | + io.mapRequired("start", sr.start), io.mapRequired("end", sr.end); |
| 152 | + } |
| 153 | +}; |
| 154 | + |
| 155 | +//============================================================================== |
| 156 | +// MARK: SwiftRangesFileContents |
| 157 | +//============================================================================== |
| 158 | + |
| 159 | +namespace swift { |
| 160 | +namespace incremental_ranges { |
| 161 | + |
| 162 | +struct SwiftRangesFileContents { |
| 163 | + RangesByFilename unparsedRangesByNonPrimary; |
| 164 | + Ranges noninlinableFunctionBodies; |
| 165 | + |
| 166 | + SwiftRangesFileContents() : SwiftRangesFileContents({}, {}) {} |
| 167 | + |
| 168 | + SwiftRangesFileContents(RangesByFilename &&unparsedRangesByNonPrimary, |
| 169 | + Ranges &&noninlinableFunctionBodies) |
| 170 | + : unparsedRangesByNonPrimary(std::move(unparsedRangesByNonPrimary)), |
| 171 | + noninlinableFunctionBodies(std::move(noninlinableFunctionBodies)) {} |
| 172 | + |
| 173 | + static Optional<SwiftRangesFileContents> |
| 174 | + load(const StringRef primaryPath, const llvm::MemoryBuffer &swiftRangesBuffer, |
| 175 | + const bool showIncrementalBuildDecisions, DiagnosticEngine &diags); |
| 176 | + |
| 177 | + void dump(StringRef primary) const; |
| 178 | + |
| 179 | + static constexpr const char *header = "### Swift source ranges file v0 ###\n"; |
| 180 | +}; |
| 181 | +} // namespace incremental_ranges |
| 182 | +} // namespace swift |
| 183 | + |
| 184 | +template <> |
| 185 | +struct llvm::yaml::MappingTraits< |
| 186 | + swift::incremental_ranges::SwiftRangesFileContents> { |
| 187 | + static void |
| 188 | + mapping(llvm::yaml::IO &io, |
| 189 | + swift::incremental_ranges::SwiftRangesFileContents &srfc) { |
| 190 | + io.mapRequired("unparsedRangesByNonPrimary", |
| 191 | + srfc.unparsedRangesByNonPrimary); |
| 192 | + io.mapRequired("noninlinableFunctionBodies", |
| 193 | + srfc.noninlinableFunctionBodies); |
| 194 | + } |
| 195 | +}; |
| 196 | + |
| 197 | +LLVM_YAML_IS_SEQUENCE_VECTOR(swift::incremental_ranges::SerializableSourceRange) |
| 198 | +LLVM_YAML_IS_STRING_MAP(swift::incremental_ranges::Ranges) |
| 199 | +LLVM_YAML_IS_STRING_MAP(swift::incremental_ranges::RangesByFilename) |
| 200 | + |
| 201 | +//============================================================================== |
| 202 | +// MARK: SwiftRangesEmitter |
| 203 | +//============================================================================== |
| 204 | +namespace swift { |
| 205 | +namespace incremental_ranges { |
| 206 | + |
| 207 | +class SwiftRangesEmitter { |
| 208 | + const StringRef outputPath; |
| 209 | + SourceFile *const primaryFile; |
| 210 | + const PersistentParserState &persistentState; |
| 211 | + const SourceManager &sourceMgr; |
| 212 | + DiagnosticEngine &diags; |
| 213 | + |
| 214 | +public: |
| 215 | + SwiftRangesEmitter(StringRef outputPath, SourceFile *primaryFile, |
| 216 | + const PersistentParserState &persistentState, |
| 217 | + const SourceManager &sourceMgr, DiagnosticEngine &diags) |
| 218 | + : outputPath(outputPath), primaryFile(primaryFile), |
| 219 | + persistentState(persistentState), sourceMgr(sourceMgr), diags(diags) {} |
| 220 | + |
| 221 | + /// True for error |
| 222 | + bool emit() const; |
| 223 | + |
| 224 | +public: |
| 225 | + void emitRanges(llvm::raw_ostream &out) const; |
| 226 | + |
| 227 | +private: |
| 228 | + RangesByFilename collectSerializedUnparsedRangesByNonPrimary() const; |
| 229 | + |
| 230 | + Ranges collectSortedSerializedNoninlinableFunctionBodies() const; |
| 231 | + std::vector<CharSourceRange> collectNoninlinableFunctionBodies() const; |
| 232 | + |
| 233 | + std::map<std::string, std::vector<CharSourceRange>> |
| 234 | + collectUnparsedRanges() const; |
| 235 | + std::vector<CharSourceRange> |
| 236 | + sortRanges(std::vector<CharSourceRange> ranges) const; |
| 237 | + |
| 238 | + /// Assuming \p ranges is sorted, coalesce overlapping ranges in place and |
| 239 | + /// return end of the resultant vector. |
| 240 | + std::vector<CharSourceRange> |
| 241 | + coalesceSortedRanges(std::vector<CharSourceRange>) const; |
| 242 | + |
| 243 | + std::vector<SerializableSourceRange> |
| 244 | + serializeRanges(std::vector<CharSourceRange> ranges) const; |
| 245 | + |
| 246 | + bool isImmediatelyBeforeOrOverlapping(CharSourceRange prev, |
| 247 | + CharSourceRange next) const; |
| 248 | +}; |
| 249 | +} // namespace incremental_ranges |
| 250 | +} // namespace swift |
| 251 | + |
| 252 | +//============================================================================== |
| 253 | +// MARK: CompiledSourceEmitter |
| 254 | +//============================================================================== |
| 255 | +namespace swift { |
| 256 | +namespace incremental_ranges { |
| 257 | + |
| 258 | +class CompiledSourceEmitter { |
| 259 | + const StringRef outputPath; |
| 260 | + const SourceFile *const primaryFile; |
| 261 | + const SourceManager &sourceMgr; |
| 262 | + DiagnosticEngine &diags; |
| 263 | + |
| 264 | +public: |
| 265 | + CompiledSourceEmitter(StringRef outputPath, const SourceFile *primaryFile, |
| 266 | + const SourceManager &sourceMgr, DiagnosticEngine &diags) |
| 267 | + : outputPath(outputPath), primaryFile(primaryFile), sourceMgr(sourceMgr), |
| 268 | + diags(diags) {} |
| 269 | + |
| 270 | + /// True for error |
| 271 | + bool emit(); |
| 272 | +}; |
| 273 | + |
| 274 | +} // namespace incremental_ranges |
| 275 | +} // namespace swift |
| 276 | + |
| 277 | +#endif // SWIFT_AST_INCREMENTALRANGES_H |
0 commit comments