1 //===-- lib/Parser/provenance.cpp -----------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "flang/Parser/provenance.h" 10 #include "flang/Common/idioms.h" 11 #include "llvm/Support/raw_ostream.h" 12 #include <algorithm> 13 #include <set> 14 #include <utility> 15 16 namespace Fortran::parser { 17 18 ProvenanceRangeToOffsetMappings::ProvenanceRangeToOffsetMappings() {} 19 ProvenanceRangeToOffsetMappings::~ProvenanceRangeToOffsetMappings() {} 20 21 void ProvenanceRangeToOffsetMappings::Put( 22 ProvenanceRange range, std::size_t offset) { 23 auto fromTo{map_.equal_range(range)}; 24 for (auto iter{fromTo.first}; iter != fromTo.second; ++iter) { 25 if (range == iter->first) { 26 iter->second = std::min(offset, iter->second); 27 return; 28 } 29 } 30 if (fromTo.second != map_.end()) { 31 map_.emplace_hint(fromTo.second, range, offset); 32 } else { 33 map_.emplace(range, offset); 34 } 35 } 36 37 std::optional<std::size_t> ProvenanceRangeToOffsetMappings::Map( 38 ProvenanceRange range) const { 39 auto fromTo{map_.equal_range(range)}; 40 std::optional<std::size_t> result; 41 for (auto iter{fromTo.first}; iter != fromTo.second; ++iter) { 42 ProvenanceRange that{iter->first}; 43 if (that.Contains(range)) { 44 std::size_t offset{iter->second + that.MemberOffset(range.start())}; 45 if (!result || offset < *result) { 46 result = offset; 47 } 48 } 49 } 50 return result; 51 } 52 53 bool ProvenanceRangeToOffsetMappings::WhollyPrecedes::operator()( 54 ProvenanceRange before, ProvenanceRange after) const { 55 return before.start() + before.size() <= after.start(); 56 } 57 58 void OffsetToProvenanceMappings::clear() { provenanceMap_.clear(); } 59 60 void OffsetToProvenanceMappings::swap(OffsetToProvenanceMappings &that) { 61 provenanceMap_.swap(that.provenanceMap_); 62 } 63 64 void OffsetToProvenanceMappings::shrink_to_fit() { 65 provenanceMap_.shrink_to_fit(); 66 } 67 68 std::size_t OffsetToProvenanceMappings::SizeInBytes() const { 69 if (provenanceMap_.empty()) { 70 return 0; 71 } else { 72 const ContiguousProvenanceMapping &last{provenanceMap_.back()}; 73 return last.start + last.range.size(); 74 } 75 } 76 77 void OffsetToProvenanceMappings::Put(ProvenanceRange range) { 78 if (provenanceMap_.empty()) { 79 provenanceMap_.push_back({0, range}); 80 } else { 81 ContiguousProvenanceMapping &last{provenanceMap_.back()}; 82 if (!last.range.AnnexIfPredecessor(range)) { 83 provenanceMap_.push_back({last.start + last.range.size(), range}); 84 } 85 } 86 } 87 88 void OffsetToProvenanceMappings::Put(const OffsetToProvenanceMappings &that) { 89 for (const auto &map : that.provenanceMap_) { 90 Put(map.range); 91 } 92 } 93 94 ProvenanceRange OffsetToProvenanceMappings::Map(std::size_t at) const { 95 if (provenanceMap_.empty()) { 96 CHECK(at == 0); 97 return {}; 98 } 99 std::size_t low{0}, count{provenanceMap_.size()}; 100 while (count > 1) { 101 std::size_t mid{low + (count >> 1)}; 102 if (provenanceMap_[mid].start > at) { 103 count = mid - low; 104 } else { 105 count -= mid - low; 106 low = mid; 107 } 108 } 109 std::size_t offset{at - provenanceMap_[low].start}; 110 return provenanceMap_[low].range.Suffix(offset); 111 } 112 113 void OffsetToProvenanceMappings::RemoveLastBytes(std::size_t bytes) { 114 for (; bytes > 0; provenanceMap_.pop_back()) { 115 CHECK(!provenanceMap_.empty()); 116 ContiguousProvenanceMapping &last{provenanceMap_.back()}; 117 std::size_t chunk{last.range.size()}; 118 if (bytes < chunk) { 119 last.range = last.range.Prefix(chunk - bytes); 120 break; 121 } 122 bytes -= chunk; 123 } 124 } 125 126 ProvenanceRangeToOffsetMappings OffsetToProvenanceMappings::Invert( 127 const AllSources &allSources) const { 128 ProvenanceRangeToOffsetMappings result; 129 for (const auto &contig : provenanceMap_) { 130 ProvenanceRange range{contig.range}; 131 while (!range.empty()) { 132 ProvenanceRange source{allSources.IntersectionWithSourceFiles(range)}; 133 if (source.empty()) { 134 break; 135 } 136 result.Put( 137 source, contig.start + contig.range.MemberOffset(source.start())); 138 Provenance after{source.NextAfter()}; 139 if (range.Contains(after)) { 140 range = range.Suffix(range.MemberOffset(after)); 141 } else { 142 break; 143 } 144 } 145 } 146 return result; 147 } 148 149 AllSources::AllSources() : range_{1, 1} { 150 // Start the origin_ array with a dummy entry that has a forced provenance, 151 // so that provenance offset 0 remains reserved as an uninitialized 152 // value. 153 origin_.emplace_back(range_, std::string{'?'}); 154 } 155 156 AllSources::~AllSources() {} 157 158 const char &AllSources::operator[](Provenance at) const { 159 const Origin &origin{MapToOrigin(at)}; 160 return origin[origin.covers.MemberOffset(at)]; 161 } 162 163 void AllSources::ClearSearchPath() { searchPath_.clear(); } 164 165 void AllSources::AppendSearchPathDirectory(std::string directory) { 166 // gfortran and ifort append to current path, PGI prepends 167 searchPath_.push_back(directory); 168 } 169 170 const SourceFile *AllSources::Open(std::string path, llvm::raw_ostream &error, 171 std::optional<std::string> &&prependPath) { 172 std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)}; 173 if (prependPath) { 174 // Set to "." for the initial source file; set to the directory name 175 // of the including file for #include "quoted-file" directives & 176 // INCLUDE statements. 177 searchPath_.emplace_front(std::move(*prependPath)); 178 } 179 std::optional<std::string> found{LocateSourceFile(path, searchPath_)}; 180 if (prependPath) { 181 searchPath_.pop_front(); 182 } 183 if (!found) { 184 error << "Source file '" << path << "' was not found"; 185 return nullptr; 186 } else if (source->Open(*found, error)) { 187 return ownedSourceFiles_.emplace_back(std::move(source)).get(); 188 } else { 189 return nullptr; 190 } 191 } 192 193 const SourceFile *AllSources::ReadStandardInput(llvm::raw_ostream &error) { 194 std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)}; 195 if (source->ReadStandardInput(error)) { 196 return ownedSourceFiles_.emplace_back(std::move(source)).get(); 197 } 198 return nullptr; 199 } 200 201 ProvenanceRange AllSources::AddIncludedFile( 202 const SourceFile &source, ProvenanceRange from, bool isModule) { 203 ProvenanceRange covers{range_.NextAfter(), source.bytes()}; 204 CHECK(range_.AnnexIfPredecessor(covers)); 205 CHECK(origin_.back().covers.ImmediatelyPrecedes(covers)); 206 origin_.emplace_back(covers, source, from, isModule); 207 return covers; 208 } 209 210 ProvenanceRange AllSources::AddMacroCall( 211 ProvenanceRange def, ProvenanceRange use, const std::string &expansion) { 212 ProvenanceRange covers{range_.NextAfter(), expansion.size()}; 213 CHECK(range_.AnnexIfPredecessor(covers)); 214 CHECK(origin_.back().covers.ImmediatelyPrecedes(covers)); 215 origin_.emplace_back(covers, def, use, expansion); 216 return covers; 217 } 218 219 ProvenanceRange AllSources::AddCompilerInsertion(std::string text) { 220 ProvenanceRange covers{range_.NextAfter(), text.size()}; 221 CHECK(range_.AnnexIfPredecessor(covers)); 222 CHECK(origin_.back().covers.ImmediatelyPrecedes(covers)); 223 origin_.emplace_back(covers, text); 224 return covers; 225 } 226 227 static void EmitPrefix(llvm::raw_ostream &o, llvm::raw_ostream::Colors color, 228 const std::string &prefix, bool showColors) { 229 if (prefix.empty()) { 230 return; 231 } 232 if (showColors) { 233 o.changeColor(color, true); 234 } 235 o << prefix; 236 if (showColors) { 237 o.resetColor(); 238 } 239 } 240 241 void AllSources::EmitMessage(llvm::raw_ostream &o, 242 const std::optional<ProvenanceRange> &range, const std::string &message, 243 const std::string &prefix, llvm::raw_ostream::Colors color, 244 bool echoSourceLine) const { 245 if (!range) { 246 EmitPrefix(o, color, prefix, this->getShowColors()); 247 o << message << '\n'; 248 return; 249 } 250 CHECK(IsValid(*range)); 251 const Origin &origin{MapToOrigin(range->start())}; 252 common::visit( 253 common::visitors{ 254 [&](const Inclusion &inc) { 255 std::size_t offset{origin.covers.MemberOffset(range->start())}; 256 SourcePosition pos{inc.source.GetSourcePosition(offset)}; 257 o << pos.path << ':' << pos.line << ':' << pos.column << ": "; 258 EmitPrefix(o, color, prefix, this->getShowColors()); 259 o << message << '\n'; 260 if (echoSourceLine) { 261 const char *text{inc.source.content().data() + 262 inc.source.GetLineStartOffset(pos.trueLineNumber)}; 263 o << " "; 264 for (const char *p{text}; *p != '\n'; ++p) { 265 o << *p; 266 } 267 o << "\n "; 268 for (int j{1}; j < pos.column; ++j) { 269 char ch{text[j - 1]}; 270 o << (ch == '\t' ? '\t' : ' '); 271 } 272 o << '^'; 273 if (range->size() > 1) { 274 auto last{range->start() + range->size() - 1}; 275 if (&MapToOrigin(last) == &origin) { 276 auto endOffset{origin.covers.MemberOffset(last)}; 277 auto endPos{inc.source.GetSourcePosition(endOffset)}; 278 if (pos.line == endPos.line) { 279 for (int j{pos.column}; j < endPos.column; ++j) { 280 o << '^'; 281 } 282 } 283 } 284 } 285 o << '\n'; 286 } 287 if (IsValid(origin.replaces)) { 288 EmitMessage(o, origin.replaces, 289 inc.isModule ? "used here"s : "included here"s, prefix, color, 290 echoSourceLine); 291 } 292 }, 293 [&](const Macro &mac) { 294 EmitMessage( 295 o, origin.replaces, message, prefix, color, echoSourceLine); 296 EmitMessage(o, mac.definition, "in a macro defined here", ""s, 297 color, echoSourceLine); 298 if (echoSourceLine) { 299 o << "that expanded to:\n " << mac.expansion << "\n "; 300 for (std::size_t j{0}; 301 origin.covers.OffsetMember(j) < range->start(); ++j) { 302 o << (mac.expansion[j] == '\t' ? '\t' : ' '); 303 } 304 o << "^\n"; 305 } 306 }, 307 [&](const CompilerInsertion &) { 308 EmitPrefix(o, color, prefix, this->getShowColors()); 309 o << message << '\n'; 310 }, 311 }, 312 origin.u); 313 } 314 315 const SourceFile *AllSources::GetSourceFile( 316 Provenance at, std::size_t *offset) const { 317 const Origin &origin{MapToOrigin(at)}; 318 return common::visit(common::visitors{ 319 [&](const Inclusion &inc) { 320 if (offset) { 321 *offset = origin.covers.MemberOffset(at); 322 } 323 return &inc.source; 324 }, 325 [&](const Macro &) { 326 return GetSourceFile( 327 origin.replaces.start(), offset); 328 }, 329 [offset](const CompilerInsertion &) { 330 if (offset) { 331 *offset = 0; 332 } 333 return static_cast<const SourceFile *>(nullptr); 334 }, 335 }, 336 origin.u); 337 } 338 339 const char *AllSources::GetSource(ProvenanceRange range) const { 340 Provenance start{range.start()}; 341 const Origin &origin{MapToOrigin(start)}; 342 return origin.covers.Contains(range) 343 ? &origin[origin.covers.MemberOffset(start)] 344 : nullptr; 345 } 346 347 std::optional<SourcePosition> AllSources::GetSourcePosition( 348 Provenance prov) const { 349 const Origin &origin{MapToOrigin(prov)}; 350 return common::visit( 351 common::visitors{ 352 [&](const Inclusion &inc) -> std::optional<SourcePosition> { 353 std::size_t offset{origin.covers.MemberOffset(prov)}; 354 return inc.source.GetSourcePosition(offset); 355 }, 356 [&](const Macro &) { 357 return GetSourcePosition(origin.replaces.start()); 358 }, 359 [](const CompilerInsertion &) -> std::optional<SourcePosition> { 360 return std::nullopt; 361 }, 362 }, 363 origin.u); 364 } 365 366 std::optional<ProvenanceRange> AllSources::GetFirstFileProvenance() const { 367 for (const auto &origin : origin_) { 368 if (std::holds_alternative<Inclusion>(origin.u)) { 369 return origin.covers; 370 } 371 } 372 return std::nullopt; 373 } 374 375 std::string AllSources::GetPath(Provenance at) const { 376 std::size_t offset{0}; 377 const SourceFile *source{GetSourceFile(at, &offset)}; 378 return source ? *source->GetSourcePosition(offset).path : ""s; 379 } 380 381 int AllSources::GetLineNumber(Provenance at) const { 382 std::size_t offset{0}; 383 const SourceFile *source{GetSourceFile(at, &offset)}; 384 return source ? source->GetSourcePosition(offset).line : 0; 385 } 386 387 Provenance AllSources::CompilerInsertionProvenance(char ch) { 388 auto iter{compilerInsertionProvenance_.find(ch)}; 389 if (iter != compilerInsertionProvenance_.end()) { 390 return iter->second; 391 } 392 ProvenanceRange newCharRange{AddCompilerInsertion(std::string{ch})}; 393 Provenance newCharProvenance{newCharRange.start()}; 394 compilerInsertionProvenance_.insert(std::make_pair(ch, newCharProvenance)); 395 return newCharProvenance; 396 } 397 398 ProvenanceRange AllSources::IntersectionWithSourceFiles( 399 ProvenanceRange range) const { 400 if (range.empty()) { 401 return {}; 402 } else { 403 const Origin &origin{MapToOrigin(range.start())}; 404 if (std::holds_alternative<Inclusion>(origin.u)) { 405 return range.Intersection(origin.covers); 406 } else { 407 auto skip{ 408 origin.covers.size() - origin.covers.MemberOffset(range.start())}; 409 return IntersectionWithSourceFiles(range.Suffix(skip)); 410 } 411 } 412 } 413 414 AllSources::Origin::Origin(ProvenanceRange r, const SourceFile &source) 415 : u{Inclusion{source}}, covers{r} {} 416 AllSources::Origin::Origin(ProvenanceRange r, const SourceFile &included, 417 ProvenanceRange from, bool isModule) 418 : u{Inclusion{included, isModule}}, covers{r}, replaces{from} {} 419 AllSources::Origin::Origin(ProvenanceRange r, ProvenanceRange def, 420 ProvenanceRange use, const std::string &expansion) 421 : u{Macro{def, expansion}}, covers{r}, replaces{use} {} 422 AllSources::Origin::Origin(ProvenanceRange r, const std::string &text) 423 : u{CompilerInsertion{text}}, covers{r} {} 424 425 const char &AllSources::Origin::operator[](std::size_t n) const { 426 return common::visit( 427 common::visitors{ 428 [n](const Inclusion &inc) -> const char & { 429 return inc.source.content()[n]; 430 }, 431 [n](const Macro &mac) -> const char & { return mac.expansion[n]; }, 432 [n](const CompilerInsertion &ins) -> const char & { 433 return ins.text[n]; 434 }, 435 }, 436 u); 437 } 438 439 const AllSources::Origin &AllSources::MapToOrigin(Provenance at) const { 440 CHECK(range_.Contains(at)); 441 std::size_t low{0}, count{origin_.size()}; 442 while (count > 1) { 443 std::size_t mid{low + (count >> 1)}; 444 if (at < origin_[mid].covers.start()) { 445 count = mid - low; 446 } else { 447 count -= mid - low; 448 low = mid; 449 } 450 } 451 CHECK(origin_[low].covers.Contains(at)); 452 return origin_[low]; 453 } 454 455 Provenance AllSources::GetReplacedProvenance(Provenance provenance) const { 456 const Origin &origin{MapToOrigin(provenance)}; 457 if (std::holds_alternative<Macro>(origin.u)) { 458 return origin.replaces.start(); 459 } 460 return provenance; 461 } 462 463 std::optional<ProvenanceRange> CookedSource::GetProvenanceRange( 464 CharBlock cookedRange) const { 465 if (!AsCharBlock().Contains(cookedRange)) { 466 return std::nullopt; 467 } 468 ProvenanceRange first{provenanceMap_.Map(cookedRange.begin() - &data_[0])}; 469 if (cookedRange.size() <= first.size()) { // always true when empty 470 return first.Prefix(cookedRange.size()); 471 } 472 ProvenanceRange last{provenanceMap_.Map(cookedRange.end() - 1 - &data_[0])}; 473 if (first.start() <= last.start()) { 474 return {ProvenanceRange{first.start(), last.start() - first.start() + 1}}; 475 } else { 476 // cookedRange may start (resp. end) in a macro expansion while it does not 477 // end (resp. start) in this macro expansion. Attempt to build a range 478 // over the replaced source. 479 Provenance firstStart{allSources_.GetReplacedProvenance(first.start())}; 480 Provenance lastStart{allSources_.GetReplacedProvenance(last.start())}; 481 if (firstStart <= lastStart) { 482 return {ProvenanceRange{firstStart, lastStart - firstStart + 1}}; 483 } else { 484 return std::nullopt; 485 } 486 } 487 } 488 489 std::optional<CharBlock> CookedSource::GetCharBlock( 490 ProvenanceRange range) const { 491 CHECK(!invertedMap_.empty() && 492 "CompileProvenanceRangeToOffsetMappings not called"); 493 if (auto to{invertedMap_.Map(range)}) { 494 return CharBlock{data_.c_str() + *to, range.size()}; 495 } else { 496 return std::nullopt; 497 } 498 } 499 500 std::size_t CookedSource::BufferedBytes() const { return buffer_.bytes(); } 501 502 void CookedSource::Marshal(AllCookedSources &allCookedSources) { 503 CHECK(provenanceMap_.SizeInBytes() == buffer_.bytes()); 504 provenanceMap_.Put(allCookedSources.allSources().AddCompilerInsertion( 505 "(after end of source)")); 506 data_ = buffer_.Marshal(); 507 buffer_.clear(); 508 allCookedSources.Register(*this); 509 } 510 511 void CookedSource::CompileProvenanceRangeToOffsetMappings( 512 AllSources &allSources) { 513 if (invertedMap_.empty()) { 514 invertedMap_ = provenanceMap_.Invert(allSources); 515 } 516 } 517 518 static void DumpRange(llvm::raw_ostream &o, const ProvenanceRange &r) { 519 o << "[" << r.start().offset() << ".." << r.Last().offset() << "] (" 520 << r.size() << " bytes)"; 521 } 522 523 llvm::raw_ostream &ProvenanceRangeToOffsetMappings::Dump( 524 llvm::raw_ostream &o) const { 525 for (const auto &m : map_) { 526 o << "provenances "; 527 DumpRange(o, m.first); 528 o << " -> offsets [" << m.second << ".." << (m.second + m.first.size() - 1) 529 << "]\n"; 530 } 531 return o; 532 } 533 534 llvm::raw_ostream &OffsetToProvenanceMappings::Dump( 535 llvm::raw_ostream &o) const { 536 for (const ContiguousProvenanceMapping &m : provenanceMap_) { 537 std::size_t n{m.range.size()}; 538 o << "offsets [" << m.start << ".." << (m.start + n - 1) 539 << "] -> provenances "; 540 DumpRange(o, m.range); 541 o << '\n'; 542 } 543 return o; 544 } 545 546 llvm::raw_ostream &AllSources::Dump(llvm::raw_ostream &o) const { 547 o << "AllSources range_ "; 548 DumpRange(o, range_); 549 o << '\n'; 550 std::set<const SourceFile *> sources; 551 for (const Origin &m : origin_) { 552 o << " "; 553 DumpRange(o, m.covers); 554 o << " -> "; 555 common::visit(common::visitors{ 556 [&](const Inclusion &inc) { 557 if (inc.isModule) { 558 o << "module "; 559 } 560 o << "file " << inc.source.path(); 561 sources.emplace(&inc.source); 562 }, 563 [&](const Macro &mac) { o << "macro " << mac.expansion; }, 564 [&](const CompilerInsertion &ins) { 565 o << "compiler '" << ins.text << '\''; 566 if (ins.text.length() == 1) { 567 int ch = ins.text[0]; 568 o << "(0x"; 569 o.write_hex(ch & 0xff) << ")"; 570 } 571 }, 572 }, 573 m.u); 574 if (IsValid(m.replaces)) { 575 o << " replaces "; 576 DumpRange(o, m.replaces); 577 } 578 o << '\n'; 579 } 580 for (const SourceFile *sf : sources) { 581 sf->Dump(o); 582 } 583 return o; 584 } 585 586 llvm::raw_ostream &CookedSource::Dump(llvm::raw_ostream &o) const { 587 o << "CookedSource::provenanceMap_:\n"; 588 provenanceMap_.Dump(o); 589 o << "CookedSource::invertedMap_:\n"; 590 invertedMap_.Dump(o); 591 return o; 592 } 593 594 AllCookedSources::AllCookedSources(AllSources &s) : allSources_{s} {} 595 AllCookedSources::~AllCookedSources() {} 596 597 CookedSource &AllCookedSources::NewCookedSource() { 598 return cooked_.emplace_back(allSources_); 599 } 600 601 const CookedSource *AllCookedSources::Find(CharBlock x) const { 602 auto pair{index_.equal_range(x)}; 603 for (auto iter{pair.first}; iter != pair.second; ++iter) { 604 if (iter->second.AsCharBlock().Contains(x)) { 605 return &iter->second; 606 } 607 } 608 return nullptr; 609 } 610 611 std::optional<ProvenanceRange> AllCookedSources::GetProvenanceRange( 612 CharBlock cb) const { 613 if (const CookedSource * c{Find(cb)}) { 614 return c->GetProvenanceRange(cb); 615 } else { 616 return std::nullopt; 617 } 618 } 619 620 std::optional<CharBlock> AllCookedSources::GetCharBlockFromLineAndColumns( 621 int line, int startColumn, int endColumn) const { 622 // 2nd column is exclusive, meaning it is target column + 1. 623 CHECK(line > 0 && startColumn > 0 && endColumn > 0); 624 CHECK(startColumn < endColumn); 625 auto provenanceStart{allSources_.GetFirstFileProvenance().value().start()}; 626 if (auto sourceFile{allSources_.GetSourceFile(provenanceStart)}) { 627 CHECK(line <= static_cast<int>(sourceFile->lines())); 628 return GetCharBlock(ProvenanceRange(sourceFile->GetLineStartOffset(line) + 629 provenanceStart.offset() + startColumn - 1, 630 endColumn - startColumn)); 631 } 632 return std::nullopt; 633 } 634 635 std::optional<std::pair<SourcePosition, SourcePosition>> 636 AllCookedSources::GetSourcePositionRange(CharBlock cookedRange) const { 637 if (auto range{GetProvenanceRange(cookedRange)}) { 638 if (auto firstOffset{allSources_.GetSourcePosition(range->start())}) { 639 if (auto secondOffset{ 640 allSources_.GetSourcePosition(range->start() + range->size())}) { 641 return std::pair{*firstOffset, *secondOffset}; 642 } 643 } 644 } 645 return std::nullopt; 646 } 647 648 std::optional<CharBlock> AllCookedSources::GetCharBlock( 649 ProvenanceRange range) const { 650 for (const auto &c : cooked_) { 651 if (auto result{c.GetCharBlock(range)}) { 652 return result; 653 } 654 } 655 return std::nullopt; 656 } 657 658 void AllCookedSources::Dump(llvm::raw_ostream &o) const { 659 o << "AllSources:\n"; 660 allSources_.Dump(o); 661 for (const auto &c : cooked_) { 662 c.Dump(o); 663 } 664 } 665 666 bool AllCookedSources::Precedes(CharBlock x, CharBlock y) const { 667 if (const CookedSource * xSource{Find(x)}) { 668 if (xSource->AsCharBlock().Contains(y)) { 669 return x.begin() < y.begin(); 670 } else if (const CookedSource * ySource{Find(y)}) { 671 return xSource->number() < ySource->number(); 672 } else { 673 return true; // by fiat, all cooked source < anything outside 674 } 675 } else if (Find(y)) { 676 return false; 677 } else { 678 // Both names are compiler-created (SaveTempName). 679 return x < y; 680 } 681 } 682 683 void AllCookedSources::Register(CookedSource &cooked) { 684 index_.emplace(cooked.AsCharBlock(), cooked); 685 cooked.set_number(static_cast<int>(index_.size())); 686 } 687 688 } // namespace Fortran::parser 689