|
| 1 | +//===--- Serialization.cpp - Write Swift modules --------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/Serialization/Serialization.h" |
| 14 | +#include "swift/APIDigester/ModuleAnalyzerNodes.h" |
| 15 | +#include "swift/AST/FileSystem.h" |
| 16 | +#include "swift/Subsystems.h" |
| 17 | +#include "swift/SymbolGraphGen/SymbolGraphGen.h" |
| 18 | +#include "swift/SymbolGraphGen/SymbolGraphOptions.h" |
| 19 | +#include "llvm/Support/SmallVectorMemoryBuffer.h" |
| 20 | + |
| 21 | +using namespace swift; |
| 22 | + |
| 23 | +static ModuleDecl *getModule(ModuleOrSourceFile DC) { |
| 24 | + if (auto M = DC.dyn_cast<ModuleDecl *>()) |
| 25 | + return M; |
| 26 | + return DC.get<SourceFile *>()->getParentModule(); |
| 27 | +} |
| 28 | + |
| 29 | +static ASTContext &getContext(ModuleOrSourceFile DC) { |
| 30 | + return getModule(DC)->getASTContext(); |
| 31 | +} |
| 32 | + |
| 33 | +static void emitABIDescriptor(ModuleOrSourceFile DC, |
| 34 | + const SerializationOptions &options) { |
| 35 | + using namespace swift::ide::api; |
| 36 | + if (!options.ABIDescriptorPath.empty()) { |
| 37 | + if (DC.is<ModuleDecl *>()) { |
| 38 | + dumpModuleContent(DC.get<ModuleDecl *>(), options.ABIDescriptorPath, true, |
| 39 | + options.emptyABIDescriptor); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +void swift::serializeToBuffers( |
| 45 | + ModuleOrSourceFile DC, const SerializationOptions &options, |
| 46 | + std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer, |
| 47 | + std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer, |
| 48 | + std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer, |
| 49 | + const SILModule *M) { |
| 50 | + |
| 51 | + assert(!options.OutputPath.empty()); |
| 52 | + { |
| 53 | + FrontendStatsTracer tracer(getContext(DC).Stats, |
| 54 | + "Serialization, swiftmodule, to buffer"); |
| 55 | + llvm::SmallString<1024> buf; |
| 56 | + llvm::raw_svector_ostream stream(buf); |
| 57 | + serialization::writeToStream(stream, DC, M, options, |
| 58 | + /*dependency info*/ nullptr); |
| 59 | + bool hadError = withOutputFile(getContext(DC).Diags, options.OutputPath, |
| 60 | + [&](raw_ostream &out) { |
| 61 | + out << stream.str(); |
| 62 | + return false; |
| 63 | + }); |
| 64 | + if (hadError) |
| 65 | + return; |
| 66 | + |
| 67 | + emitABIDescriptor(DC, options); |
| 68 | + if (moduleBuffer) |
| 69 | + *moduleBuffer = std::make_unique<llvm::SmallVectorMemoryBuffer>( |
| 70 | + std::move(buf), options.OutputPath, |
| 71 | + /*RequiresNullTerminator=*/false); |
| 72 | + } |
| 73 | + |
| 74 | + if (!options.DocOutputPath.empty()) { |
| 75 | + FrontendStatsTracer tracer(getContext(DC).Stats, |
| 76 | + "Serialization, swiftdoc, to buffer"); |
| 77 | + llvm::SmallString<1024> buf; |
| 78 | + llvm::raw_svector_ostream stream(buf); |
| 79 | + serialization::writeDocToStream(stream, DC, options.GroupInfoPath); |
| 80 | + (void)withOutputFile(getContext(DC).Diags, options.DocOutputPath, |
| 81 | + [&](raw_ostream &out) { |
| 82 | + out << stream.str(); |
| 83 | + return false; |
| 84 | + }); |
| 85 | + if (moduleDocBuffer) |
| 86 | + *moduleDocBuffer = std::make_unique<llvm::SmallVectorMemoryBuffer>( |
| 87 | + std::move(buf), options.DocOutputPath, |
| 88 | + /*RequiresNullTerminator=*/false); |
| 89 | + } |
| 90 | + |
| 91 | + if (!options.SourceInfoOutputPath.empty()) { |
| 92 | + FrontendStatsTracer tracer(getContext(DC).Stats, |
| 93 | + "Serialization, swiftsourceinfo, to buffer"); |
| 94 | + llvm::SmallString<1024> buf; |
| 95 | + llvm::raw_svector_ostream stream(buf); |
| 96 | + serialization::writeSourceInfoToStream(stream, DC); |
| 97 | + (void)withOutputFile(getContext(DC).Diags, options.SourceInfoOutputPath, |
| 98 | + [&](raw_ostream &out) { |
| 99 | + out << stream.str(); |
| 100 | + return false; |
| 101 | + }); |
| 102 | + if (moduleSourceInfoBuffer) |
| 103 | + *moduleSourceInfoBuffer = std::make_unique<llvm::SmallVectorMemoryBuffer>( |
| 104 | + std::move(buf), options.SourceInfoOutputPath, |
| 105 | + /*RequiresNullTerminator=*/false); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void swift::serialize( |
| 110 | + ModuleOrSourceFile DC, const SerializationOptions &options, |
| 111 | + const symbolgraphgen::SymbolGraphOptions &symbolGraphOptions, |
| 112 | + const SILModule *M, |
| 113 | + const fine_grained_dependencies::SourceFileDepGraph *DG) { |
| 114 | + assert(!options.OutputPath.empty()); |
| 115 | + |
| 116 | + if (options.OutputPath == "-") { |
| 117 | + // Special-case writing to stdout. |
| 118 | + serialization::writeToStream(llvm::outs(), DC, M, options, DG); |
| 119 | + assert(options.DocOutputPath.empty()); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + bool hadError = withOutputFile(getContext(DC).Diags, |
| 124 | + options.OutputPath, |
| 125 | + [&](raw_ostream &out) { |
| 126 | + FrontendStatsTracer tracer(getContext(DC).Stats, |
| 127 | + "Serialization, swiftmodule"); |
| 128 | + serialization::writeToStream(out, DC, M, options, DG); |
| 129 | + return false; |
| 130 | + }); |
| 131 | + if (hadError) |
| 132 | + return; |
| 133 | + |
| 134 | + if (!options.DocOutputPath.empty()) { |
| 135 | + (void)withOutputFile(getContext(DC).Diags, |
| 136 | + options.DocOutputPath, |
| 137 | + [&](raw_ostream &out) { |
| 138 | + FrontendStatsTracer tracer(getContext(DC).Stats, |
| 139 | + "Serialization, swiftdoc"); |
| 140 | + serialization::writeDocToStream(out, DC, options.GroupInfoPath); |
| 141 | + return false; |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + if (!options.SourceInfoOutputPath.empty()) { |
| 146 | + (void)withOutputFile(getContext(DC).Diags, |
| 147 | + options.SourceInfoOutputPath, |
| 148 | + [&](raw_ostream &out) { |
| 149 | + FrontendStatsTracer tracer(getContext(DC).Stats, |
| 150 | + "Serialization, swiftsourceinfo"); |
| 151 | + serialization::writeSourceInfoToStream(out, DC); |
| 152 | + return false; |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + if (!symbolGraphOptions.OutputDir.empty()) { |
| 157 | + if (DC.is<ModuleDecl *>()) { |
| 158 | + auto *M = DC.get<ModuleDecl *>(); |
| 159 | + FrontendStatsTracer tracer(getContext(DC).Stats, |
| 160 | + "Serialization, symbolgraph"); |
| 161 | + symbolgraphgen::emitSymbolGraphForModule(M, symbolGraphOptions); |
| 162 | + } |
| 163 | + } |
| 164 | + emitABIDescriptor(DC, options); |
| 165 | +} |
0 commit comments