1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
//===--- SyntacticMacroExpansion.cpp --------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "swift/IDETool/SyntacticMacroExpansion.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/MacroDefinition.h"
#include "swift/AST/PluginLoader.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Driver/FrontendUtil.h"
#include "swift/Frontend/Frontend.h"
#include "swift/IDE/Utils.h"
#include "swift/Sema/IDETypeChecking.h"
using namespace swift;
using namespace ide;
std::shared_ptr<SyntacticMacroExpansionInstance>
SyntacticMacroExpansion::getInstance(ArrayRef<const char *> args,
std::string &error) {
// Create and configure a new instance.
auto instance = std::make_shared<SyntacticMacroExpansionInstance>();
bool failed = instance->setup(SwiftExecutablePath, args, Plugins, error);
if (failed)
return nullptr;
return instance;
}
bool SyntacticMacroExpansionInstance::setup(
StringRef SwiftExecutablePath, ArrayRef<const char *> args,
std::shared_ptr<PluginRegistry> plugins, std::string &error) {
SmallString<256> driverPath(SwiftExecutablePath);
llvm::sys::path::remove_filename(driverPath);
llvm::sys::path::append(driverPath, "swiftc");
// Setup CompilerInstance to configure the plugin search path correctly.
bool hadError = driver::getSingleFrontendInvocationFromDriverArguments(
driverPath, args, Diags,
[&](ArrayRef<const char *> frontendArgs) {
return invocation.parseArgs(
frontendArgs, Diags, /*ConfigurationFileBuffers=*/nullptr,
/*workingDirectory=*/{}, SwiftExecutablePath);
},
/*ForceNoOutput=*/true);
if (hadError) {
error = "failed to setup compiler invocation";
return true;
}
// Setup ASTContext.
Ctx.reset(ASTContext::get(
invocation.getLangOptions(), invocation.getTypeCheckerOptions(),
invocation.getSILOptions(), invocation.getSearchPathOptions(),
invocation.getClangImporterOptions(), invocation.getSymbolGraphOptions(),
invocation.getCASOptions(), SourceMgr, Diags));
registerParseRequestFunctions(Ctx->evaluator);
registerTypeCheckerRequestFunctions(Ctx->evaluator);
std::unique_ptr<PluginLoader> pluginLoader =
std::make_unique<PluginLoader>(*Ctx, /*DepTracker=*/nullptr);
pluginLoader->setRegistry(plugins.get());
Ctx->setPluginLoader(std::move(pluginLoader));
// Create a module where SourceFiles reside.
Identifier ID = Ctx->getIdentifier(invocation.getModuleName());
TheModule = ModuleDecl::create(ID, *Ctx);
return false;
}
SourceFile *
SyntacticMacroExpansionInstance::getSourceFile(llvm::MemoryBuffer *inputBuf) {
// If there is a SourceFile with the same name and the content, use it.
// Note that this finds the generated source file that was created in the
// previous expansion requests.
if (auto bufID =
SourceMgr.getIDForBufferIdentifier(inputBuf->getBufferIdentifier())) {
if (inputBuf->getBuffer() == SourceMgr.getEntireTextForBuffer(*bufID)) {
SourceLoc bufLoc = SourceMgr.getLocForBufferStart(*bufID);
if (SourceFile *existing =
TheModule->getSourceFileContainingLocation(bufLoc)) {
return existing;
}
}
}
// Otherwise, create a new SourceFile.
SourceFile *SF = new (getASTContext()) SourceFile(
*TheModule, SourceFileKind::Main, SourceMgr.addMemBufferCopy(inputBuf));
SF->setImports({});
TheModule->addFile(*SF);
return SF;
}
MacroDecl *SyntacticMacroExpansionInstance::getSynthesizedMacroDecl(
Identifier name, const MacroExpansionSpecifier &expansion) {
auto &ctx = getASTContext();
std::string macroID;
switch (expansion.macroDefinition.kind) {
case MacroDefinition::Kind::External: {
// '<module name>.<type name>'
// It's safe to use without 'kind' because 'Expanded' always starts with a
// sigil '#' which can't be valid in a module name.
auto external = expansion.macroDefinition.getExternalMacro();
macroID += external.moduleName.str();
macroID += ".";
macroID += external.macroTypeName.str();
break;
}
case MacroDefinition::Kind::Expanded: {
auto expanded = expansion.macroDefinition.getExpanded();
macroID += expanded.getExpansionText();
break;
}
case MacroDefinition::Kind::Builtin:
case MacroDefinition::Kind::Invalid:
case MacroDefinition::Kind::Undefined:
assert(false && "invalid macro definition for syntactic expansion");
macroID += name.str();
}
// Reuse cached MacroDecl of the same name if it's already created.
MacroDecl *macro;
auto found = MacroDecls.find(macroID);
if (found != MacroDecls.end()) {
macro = found->second;
} else {
macro = new (ctx) MacroDecl(
/*macroLoc=*/{}, DeclName(name), /*nameLoc=*/{},
/*genericParams=*/nullptr, /*parameterList=*/nullptr,
/*arrowLoc=*/{}, /*resultType=*/nullptr,
/*definition=*/nullptr, /*parent=*/TheModule);
macro->setImplicit();
MacroDecls.insert({macroID, macro});
}
// Add missing role attributes to MacroDecl.
MacroRoles roles = expansion.macroRoles;
for (auto attr : macro->getAttrs().getAttributes<MacroRoleAttr>()) {
roles -= attr->getMacroRole();
}
for (MacroRole role : getAllMacroRoles()) {
if (!roles.contains(role))
continue;
MacroSyntax syntax = getFreestandingMacroRoles().contains(role)
? MacroSyntax::Freestanding
: MacroSyntax::Attached;
auto *attr = MacroRoleAttr::create(ctx, /*atLoc=*/{}, /*range=*/{}, syntax,
/*lParenLoc=*/{}, role, /*names=*/{},
/*conformances=*/{},
/*rParenLoc=*/{}, /*implicit=*/true);
macro->getAttrs().add(attr);
}
// Set the macro definition.
macro->setDefinition(expansion.macroDefinition);
return macro;
}
/// Create a unique name of the expansion. The result is *appended* to \p out.
static void addExpansionDiscriminator(
SmallString<32> &out, const SourceFile *SF, SourceLoc loc,
std::optional<SourceLoc> supplementalLoc = std::nullopt,
std::optional<MacroRole> role = std::nullopt) {
SourceManager &SM = SF->getASTContext().SourceMgr;
StableHasher hasher = StableHasher::defaultHasher();
// Module name.
hasher.combine(SF->getParentModule()->getName().str());
hasher.combine(uint8_t{0});
// File base name.
// Do not use the full path because we want this hash stable.
hasher.combine(llvm::sys::path::filename(SF->getFilename()));
hasher.combine(uint8_t{0});
// Line/column.
auto lineColumn = SM.getLineAndColumnInBuffer(loc);
hasher.combine(lineColumn.first);
hasher.combine(lineColumn.second);
// Supplemental line/column.
if (supplementalLoc.has_value()) {
auto supLineColumn = SM.getLineAndColumnInBuffer(*supplementalLoc);
hasher.combine(supLineColumn.first);
hasher.combine(supLineColumn.second);
}
// Macro role.
if (role.has_value()) {
hasher.combine(*role);
}
Fingerprint hash(std::move(hasher));
out.append(hash.getRawValue());
}
/// Perform expansion of the specified freestanding macro using the 'MacroDecl'.
static std::vector<unsigned>
expandFreestandingMacro(MacroDecl *macro,
FreestandingMacroExpansion *expansion) {
std::vector<unsigned> bufferIDs;
SmallString<32> discriminator;
discriminator.append("__syntactic_macro_");
addExpansionDiscriminator(discriminator,
expansion->getDeclContext()->getParentSourceFile(),
expansion->getPoundLoc());
expansion->setMacroRef(macro);
SourceFile *expandedSource =
swift::evaluateFreestandingMacro(expansion, discriminator);
if (expandedSource)
bufferIDs.push_back(*expandedSource->getBufferID());
return bufferIDs;
}
/// Perform expansion of the specified decl and the attribute using the
/// 'MacroDecl'. If the macro has multiple roles, evaluate it for all macro
/// roles.
static std::vector<unsigned>
expandAttachedMacro(MacroDecl *macro, CustomAttr *attr, Decl *attachedDecl) {
std::vector<unsigned> bufferIDs;
auto evaluate = [&](Decl *target, bool passParent, MacroRole role) {
SmallString<32> discriminator;
discriminator.append("macro_");
addExpansionDiscriminator(discriminator,
target->getDeclContext()->getParentSourceFile(),
target->getLoc(), attr->getLocation(), role);
SourceFile *expandedSource = swift::evaluateAttachedMacro(
macro, target, attr, passParent, role, discriminator);
if (expandedSource)
bufferIDs.push_back(*expandedSource->getBufferID());
};
MacroRoles roles = macro->getMacroRoles();
if (roles.contains(MacroRole::Accessor)) {
if (isa<AbstractStorageDecl>(attachedDecl))
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Accessor);
}
if (roles.contains(MacroRole::MemberAttribute)) {
if (auto *idc = dyn_cast<IterableDeclContext>(attachedDecl)) {
for (auto *member : idc->getParsedMembers()) {
// 'VarDecl' in 'IterableDeclContext' are part of 'PatternBindingDecl'.
if (isa<VarDecl>(member))
continue;
evaluate(member, /*passParent=*/true, MacroRole::MemberAttribute);
}
}
}
if (roles.contains(MacroRole::Member)) {
if (isa<IterableDeclContext>(attachedDecl))
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Member);
}
if (roles.contains(MacroRole::Peer)) {
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Peer);
}
if (roles.contains(MacroRole::Conformance)) {
if (isa<NominalTypeDecl>(attachedDecl))
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Conformance);
}
if (roles.contains(MacroRole::Extension)) {
if (isa<NominalTypeDecl>(attachedDecl))
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Extension);
}
if (roles.contains(MacroRole::Preamble)) {
if (isa<AbstractFunctionDecl>(attachedDecl))
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Preamble);
}
if (roles.contains(MacroRole::Body)) {
if (isa<AbstractFunctionDecl>(attachedDecl))
evaluate(attachedDecl, /*passParent=*/false, MacroRole::Body);
}
return bufferIDs;
}
/// Get the name of the custom attribute. This is used to create a dummy
/// MacroDecl.
static Identifier getCustomAttrName(ASTContext &ctx, const CustomAttr *attr) {
TypeRepr *tyR = attr->getTypeRepr();
if (auto ref = dyn_cast<DeclRefTypeRepr>(tyR)) {
return ref->getNameRef().getBaseIdentifier();
}
// If the attribute is not an identifier type, create an identifier with its
// textual representation. This is *not* expected to be reachable.
// The only case is like `@Foo?` where the client should not send the
// expansion request on this in the first place.
SmallString<32> name;
llvm::raw_svector_ostream OS(name);
tyR->print(OS);
return ctx.getIdentifier(name);
}
namespace {
/// Find macro expansion i.e. '#foo' or '@foo' at the specified source location.
/// If a freestanding expansion (i.e. #foo) is found, the result 'ExpansionNode'
/// only has the node. If an attribute is found, the attribute and the attached
/// decl object is returned.
struct ExpansionNode {
CustomAttr *attribute;
ASTNode node;
};
class MacroExpansionFinder : public ASTWalker {
SourceManager &SM;
SourceLoc locToResolve;
std::optional<ExpansionNode> result;
bool rangeContainsLocToResolve(SourceRange Range) const {
return SM.rangeContainsTokenLoc(Range, locToResolve);
}
public:
MacroExpansionFinder(SourceManager &SM, SourceLoc locToResolve)
: SM(SM), locToResolve(locToResolve) {}
std::optional<ExpansionNode> getResult() const { return result; }
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::None;
}
PreWalkAction walkToDeclPre(Decl *D) override {
// Visit all 'VarDecl' because 'getSourceRangeIncludingAttrs()' doesn't
// include its attribute ranges (because attributes are part of PBD.)
if (!isa<VarDecl>(D) &&
!rangeContainsLocToResolve(D->getSourceRangeIncludingAttrs())) {
return Action::SkipNode();
}
// Check the attributes.
for (DeclAttribute *attr : D->getAttrs()) {
if (auto customAttr = dyn_cast<CustomAttr>(attr)) {
SourceRange nameRange(customAttr->getRangeWithAt().Start,
customAttr->getTypeExpr()->getEndLoc());
if (rangeContainsLocToResolve(nameRange)) {
result = ExpansionNode{customAttr, ASTNode(D)};
return Action::Stop();
}
}
}
// Check 'MacroExpansionDecl'.
if (auto med = dyn_cast<MacroExpansionDecl>(D)) {
SourceRange nameRange(med->getExpansionInfo()->SigilLoc,
med->getMacroNameLoc().getEndLoc());
if (rangeContainsLocToResolve(nameRange)) {
result = ExpansionNode{nullptr, ASTNode(med)};
return Action::Stop();
}
}
return Action::Continue();
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
if (!rangeContainsLocToResolve(E->getSourceRange())) {
return Action::SkipNode(E);
}
// Check 'MacroExpansionExpr'.
if (auto mee = dyn_cast<MacroExpansionExpr>(E)) {
SourceRange nameRange(mee->getExpansionInfo()->SigilLoc,
mee->getMacroNameLoc().getEndLoc());
if (rangeContainsLocToResolve(nameRange)) {
result = ExpansionNode{nullptr, ASTNode(mee)};
return Action::Stop();
}
}
return Action::Continue(E);
}
PreWalkResult<Stmt *> walkToStmtPre(Stmt *S) override {
if (!rangeContainsLocToResolve(S->getSourceRange())) {
return Action::SkipNode(S);
}
return Action::Continue(S);
}
PreWalkResult<ArgumentList *>
walkToArgumentListPre(ArgumentList *AL) override {
if (!rangeContainsLocToResolve(AL->getSourceRange())) {
return Action::SkipNode(AL);
}
return Action::Continue(AL);
}
PreWalkAction walkToParameterListPre(ParameterList *PL) override {
if (!rangeContainsLocToResolve(PL->getSourceRange())) {
return Action::SkipNode();
}
return Action::Continue();
}
PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
// TypeRepr cannot have macro expansions in it.
return Action::SkipNode();
}
};
} // namespace
void SyntacticMacroExpansionInstance::expand(
SourceFile *SF, const MacroExpansionSpecifier &expansion,
SourceEditConsumer &consumer) {
// Find the expansion at 'expansion.offset'.
MacroExpansionFinder expansionFinder(
SourceMgr,
SourceMgr.getLocForOffset(*SF->getBufferID(), expansion.offset));
SF->walk(expansionFinder);
auto expansionNode = expansionFinder.getResult();
if (!expansionNode)
return;
// Expand the macro.
std::vector<unsigned> bufferIDs;
if (auto *attr = expansionNode->attribute) {
// Attached macros.
MacroDecl *macro = getSynthesizedMacroDecl(
getCustomAttrName(getASTContext(), attr), expansion);
auto *attachedTo = expansionNode->node.get<Decl *>();
bufferIDs = expandAttachedMacro(macro, attr, attachedTo);
// For an attached macro, remove the custom attribute; it's been fully
// subsumed by its expansions.
SourceRange range = attr->getRangeWithAt();
auto charRange = Lexer::getCharSourceRangeFromSourceRange(SourceMgr, range);
consumer.remove(SourceMgr, charRange);
} else {
// Freestanding macros.
FreestandingMacroExpansion *freestanding;
auto node = expansionNode->node;
if (node.is<Expr *>()) {
freestanding = cast<MacroExpansionExpr>(node.get<Expr *>());
} else {
freestanding = cast<MacroExpansionDecl>(node.get<Decl *>());
}
MacroDecl *macro = getSynthesizedMacroDecl(
freestanding->getMacroName().getBaseIdentifier(), expansion);
bufferIDs = expandFreestandingMacro(macro, freestanding);
}
// Send all edits to the consumer.
for (unsigned bufferID : bufferIDs) {
consumer.acceptMacroExpansionBuffer(SourceMgr, bufferID, SF,
/*adjust=*/false,
/*includeBufferName=*/false);
}
}
void SyntacticMacroExpansionInstance::expandAll(
llvm::MemoryBuffer *inputBuf, ArrayRef<MacroExpansionSpecifier> expansions,
SourceEditConsumer &consumer) {
// Create a source file.
SourceFile *SF = getSourceFile(inputBuf);
for (const auto &expansion : expansions) {
expand(SF, expansion, consumer);
}
}
|