|
| 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 "TypeChecker.h" |
| 19 | +#include "swift/AST/ASTContext.h" |
| 20 | +#include "swift/AST/Expr.h" |
| 21 | +#include "swift/AST/SourceFile.h" |
| 22 | +#include "swift/Basic/SourceManager.h" |
| 23 | +#include "swift/Parse/Lexer.h" |
| 24 | +#include "swift/Parse/Parser.h" |
| 25 | + |
| 26 | +using namespace swift; |
| 27 | + |
| 28 | +extern "C" ptrdiff_t swift_ASTGen_evaluateMacro( |
| 29 | + void *sourceFile, const void *sourceLocation, |
| 30 | + const char **evaluatedSource, ptrdiff_t *evaluatedSourceLength); |
| 31 | + |
| 32 | +#if SWIFT_SWIFT_PARSER |
| 33 | +Expr *swift::expandMacroExpr( |
| 34 | + DeclContext *dc, Expr *expr, StringRef macroName, Type expandedType |
| 35 | +) { |
| 36 | + ASTContext &ctx = dc->getASTContext(); |
| 37 | + |
| 38 | + // FIXME: Introduce a more robust way to ensure that we can get the "exported" |
| 39 | + // source file for a given context. If it's within a macro expansion, it |
| 40 | + // may not have a C++ SourceFile, but will have a Syntax tree. |
| 41 | + // |
| 42 | + // FIXME^2: And find a better name for "exportedSourceFile". |
| 43 | + auto sourceFile = dc->getParentSourceFile(); |
| 44 | + if (!sourceFile) |
| 45 | + return nullptr; |
| 46 | + |
| 47 | + auto astGenSourceFile = sourceFile->exportedSourceFile; |
| 48 | + if (!astGenSourceFile) |
| 49 | + return nullptr; |
| 50 | + |
| 51 | + // Evaluate the macro. |
| 52 | + const char *evaluatedSource; |
| 53 | + ptrdiff_t evaluatedSourceLength; |
| 54 | + swift_ASTGen_evaluateMacro( |
| 55 | + astGenSourceFile, expr->getStartLoc().getOpaquePointerValue(), |
| 56 | + &evaluatedSource, &evaluatedSourceLength); |
| 57 | + if (!evaluatedSource) |
| 58 | + return nullptr; |
| 59 | + |
| 60 | + SourceManager &sourceMgr = ctx.SourceMgr; |
| 61 | + |
| 62 | + // Figure out a reasonable name for the macro expansion buffer. |
| 63 | + std::string bufferName; |
| 64 | + { |
| 65 | + llvm::raw_string_ostream out(bufferName); |
| 66 | + |
| 67 | + out << "Macro expansion of #" << macroName; |
| 68 | + if (auto bufferID = sourceFile->getBufferID()) { |
| 69 | + unsigned startLine, startColumn; |
| 70 | + std::tie(startLine, startColumn) = |
| 71 | + sourceMgr.getLineAndColumnInBuffer(expr->getStartLoc(), *bufferID); |
| 72 | + |
| 73 | + SourceLoc endLoc = |
| 74 | + Lexer::getLocForEndOfToken(sourceMgr, expr->getEndLoc()); |
| 75 | + unsigned endLine, endColumn; |
| 76 | + std::tie(endLine, endColumn) = |
| 77 | + sourceMgr.getLineAndColumnInBuffer(endLoc, *bufferID); |
| 78 | + |
| 79 | + out << " in " << sourceMgr.getIdentifierForBuffer(*bufferID) << ":" |
| 80 | + << startLine << ":" << startColumn |
| 81 | + << "-" << endLine << ":" << endColumn; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Create a new source buffer with the contents of the expanded macro. |
| 86 | + auto macroBuffer = |
| 87 | + llvm::MemoryBuffer::getMemBuffer( |
| 88 | + StringRef(evaluatedSource, evaluatedSourceLength), bufferName); |
| 89 | + unsigned macroBufferID = sourceMgr.addNewSourceBuffer(std::move(macroBuffer)); |
| 90 | + |
| 91 | + // Create a source file to hold the macro buffer. |
| 92 | + // FIXME: Seems like we should record this somewhere? |
| 93 | + auto macroSourceFile = new (ctx) SourceFile( |
| 94 | + *dc->getParentModule(), SourceFileKind::Library, macroBufferID); |
| 95 | + |
| 96 | + // Parse the expression. |
| 97 | + Parser parser(macroBufferID, *macroSourceFile, &ctx.Diags, nullptr, nullptr); |
| 98 | + parser.consumeTokenWithoutFeedingReceiver(); |
| 99 | + auto parsedResult = parser.parseExpr(diag::expected_macro_expansion_expr); |
| 100 | + if (parsedResult.isParseError() || parsedResult.isNull()) { |
| 101 | + // Tack on a note to say where we expanded the macro from? |
| 102 | + return nullptr; |
| 103 | + } |
| 104 | + |
| 105 | + // Type-check the expanded expression. |
| 106 | + // FIXME: Would like to pass through type checking options like "discarded" |
| 107 | + // that are captured by TypeCheckExprOptions. |
| 108 | + Expr *expandedExpr = parsedResult.get(); |
| 109 | + constraints::ContextualTypeInfo contextualType { |
| 110 | + TypeLoc::withoutLoc(expandedType), |
| 111 | + // FIXME: Add a contextual type purpose for macro expansion. |
| 112 | + ContextualTypePurpose::CTP_CoerceOperand |
| 113 | + }; |
| 114 | + |
| 115 | + Type realExpandedType = TypeChecker::typeCheckExpression( |
| 116 | + expandedExpr, dc, contextualType); |
| 117 | + if (!realExpandedType) |
| 118 | + return nullptr; |
| 119 | + |
| 120 | + assert((expandedType->isEqual(realExpandedType) || |
| 121 | + realExpandedType->hasError()) && |
| 122 | + "Type checking changed the result type?"); |
| 123 | + return expandedExpr; |
| 124 | +} |
| 125 | + |
| 126 | +#endif // SWIFT_SWIFT_PARSER |
0 commit comments