Files
swift-mirror/include/swift/IDETool/SyntacticMacroExpansion.h
Hamish Knight 309c02410b [IDE] Avoid relying on after-the-fact use of addFile
We shouldn't be attempting to append SourceFiles
to the module after-the-fact for syntactic macro
expansion, refactor things such that the SourceFile
is created alongside the ModuleDecl.
2024-11-17 14:17:20 +00:00

97 lines
3.1 KiB
C++

//===--- SyntacticMacroExpansion.h ----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IDE_SYNTACTICMACROEXPANSION_H
#define SWIFT_IDE_SYNTACTICMACROEXPANSION_H
#include "swift/AST/Decl.h"
#include "swift/AST/MacroDefinition.h"
#include "swift/AST/PluginRegistry.h"
#include "swift/Basic/Fingerprint.h"
#include "swift/Frontend/Frontend.h"
#include "llvm/Support/MemoryBuffer.h"
namespace swift {
class ASTContext;
class SourceFile;
namespace ide {
class SourceEditConsumer;
/// Simple object to specify a syntactic macro expansion.
struct MacroExpansionSpecifier {
unsigned offset;
swift::MacroRoles macroRoles;
swift::MacroDefinition macroDefinition;
};
/// Instance of a syntactic macro expansion context. This is created for each
/// list of compiler arguments (i.e. 'argHash'), and reused as long as the
/// compiler arguments are not changed.
class SyntacticMacroExpansionInstance {
CompilerInvocation invocation;
SourceManager SourceMgr;
DiagnosticEngine Diags{SourceMgr};
std::unique_ptr<ASTContext> Ctx;
ModuleDecl *TheModule = nullptr;
SourceFile *SF = nullptr;
llvm::StringMap<MacroDecl *> MacroDecls;
/// Synthesize 'MacroDecl' AST object to use the expansion.
swift::MacroDecl *
getSynthesizedMacroDecl(swift::Identifier name,
const MacroExpansionSpecifier &expansion);
/// Expand single 'expansion'.
void expand(const MacroExpansionSpecifier &expansion,
SourceEditConsumer &consumer);
public:
SyntacticMacroExpansionInstance() {}
/// Setup the instance with \p args and a given \p inputBuf.
bool setup(StringRef SwiftExecutablePath, ArrayRef<const char *> args,
llvm::MemoryBuffer *inputBuf,
std::shared_ptr<PluginRegistry> plugins, std::string &error);
ASTContext &getASTContext() { return *Ctx; }
/// Expand all macros and send the edit results to \p consumer. Expansions are
/// specified by \p expansions .
void expandAll(ArrayRef<MacroExpansionSpecifier> expansions,
SourceEditConsumer &consumer);
};
/// Manager object to vend 'SyntacticMacroExpansionInstance'.
class SyntacticMacroExpansion {
StringRef SwiftExecutablePath;
std::shared_ptr<PluginRegistry> Plugins;
public:
SyntacticMacroExpansion(StringRef SwiftExecutablePath,
std::shared_ptr<PluginRegistry> Plugins)
: SwiftExecutablePath(SwiftExecutablePath), Plugins(Plugins) {}
/// Get instance configured with the specified compiler arguments and
/// input buffer.
std::shared_ptr<SyntacticMacroExpansionInstance>
getInstance(ArrayRef<const char *> args, llvm::MemoryBuffer *inputBuf,
std::string &error);
};
} // namespace ide
} // namespace swift
#endif // SWIFT_IDE_SYNTACTICMACROEXPANSION_H