|
| 1 | +//===--- TypeCheckMacros.cpp - Macro Handling ----------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 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 | +// This file implements support for the evaluation of macros. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "TypeCheckMacros.h" |
| 18 | +#include "swift/AST/ASTContext.h" |
| 19 | +#include "swift/AST/Expr.h" |
| 20 | +#include "swift/AST/SourceFile.h" |
| 21 | +#include "swift/Basic/SourceManager.h" |
| 22 | +#include "swift/Parse/Lexer.h" |
| 23 | + |
| 24 | +using namespace swift; |
| 25 | + |
| 26 | +extern "C" ptrdiff_t swift_ASTGen_evaluateMacro( |
| 27 | + void *sourceFile, const void *sourceLocation, |
| 28 | + const char **evaluatedSource, ptrdiff_t *evaluatedSourceLength); |
| 29 | + |
| 30 | +#if SWIFT_SWIFT_PARSER |
| 31 | +Expr *swift::expandMacroExpr( |
| 32 | + DeclContext *dc, Expr *expr, StringRef macroName, Type expandedType |
| 33 | +) { |
| 34 | + ASTContext &ctx = dc->getASTContext(); |
| 35 | + |
| 36 | + // FIXME: Introduce a more robust way to ensure that we can get the "exported" |
| 37 | + // source file for a given context. If it's within a macro expansion, it |
| 38 | + // may not have a C++ SourceFile, but will have a Syntax tree. |
| 39 | + // |
| 40 | + // FIXME^2: And find a better name for "exportedSourceFile". |
| 41 | + auto sourceFile = dc->getParentSourceFile(); |
| 42 | + if (!sourceFile) |
| 43 | + return nullptr; |
| 44 | + |
| 45 | + auto astGenSourceFile = sourceFile->exportedSourceFile; |
| 46 | + if (!astGenSourceFile) |
| 47 | + return nullptr; |
| 48 | + |
| 49 | + // Evaluate the macro. |
| 50 | + const char *evaluatedSource; |
| 51 | + ptrdiff_t evaluatedSourceLength; |
| 52 | + swift_ASTGen_evaluateMacro( |
| 53 | + astGenSourceFile, expr->getStartLoc().getOpaquePointerValue(), |
| 54 | + &evaluatedSource, &evaluatedSourceLength); |
| 55 | + if (!evaluatedSource) |
| 56 | + return nullptr; |
| 57 | + |
| 58 | + SourceManager &sourceMgr = ctx.SourceMgr; |
| 59 | + |
| 60 | + // Figure out a reasonable name for the macro expansion buffer. |
| 61 | + std::string bufferName; |
| 62 | + { |
| 63 | + llvm::raw_string_ostream out(bufferName); |
| 64 | + |
| 65 | + out << "Macro expansion of #" << macroName; |
| 66 | + if (auto bufferID = sourceFile->getBufferID()) { |
| 67 | + unsigned startLine, startColumn; |
| 68 | + std::tie(startLine, startColumn) = |
| 69 | + sourceMgr.getLineAndColumnInBuffer(expr->getStartLoc(), *bufferID); |
| 70 | + |
| 71 | + SourceLoc endLoc = |
| 72 | + Lexer::getLocForEndOfToken(sourceMgr, expr->getEndLoc()); |
| 73 | + unsigned endLine, endColumn; |
| 74 | + std::tie(endLine, endColumn) = |
| 75 | + sourceMgr.getLineAndColumnInBuffer(endLoc, *bufferID); |
| 76 | + |
| 77 | + out << sourceMgr.getIdentifierForBuffer(*bufferID) << ":" |
| 78 | + << startLine << ":" << startColumn |
| 79 | + << "-" << endLine << ":" << endColumn; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Create a new source buffer with the contents of the expanded macro. |
| 84 | + auto macroBuffer = |
| 85 | + llvm::MemoryBuffer::getMemBuffer( |
| 86 | + StringRef(evaluatedSource, evaluatedSourceLength), bufferName); |
| 87 | + unsigned macroBufferID = sourceMgr.addNewSourceBuffer(std::move(macroBuffer)); |
| 88 | + |
| 89 | + // FIXME: Debug output. |
| 90 | + llvm::outs() << "Macro rewrite: " |
| 91 | + << "#" << macroName |
| 92 | + << " --> " << sourceMgr.getEntireTextForBuffer(macroBufferID) |
| 93 | + << "\n"; |
| 94 | + |
| 95 | + return nullptr; |
| 96 | +} |
| 97 | + |
| 98 | +#endif // SWIFT_SWIFT_PARSER |
0 commit comments