|
| 1 | +//===--- FrontendActions.cpp ----------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "clang/HLSL/Frontend/FrontendActions.h" |
| 10 | +#include "clang/Parse/ParseHLSLRootSignature.h" |
| 11 | +#include "clang/Sema/Sema.h" |
| 12 | + |
| 13 | +namespace clang { |
| 14 | + |
| 15 | +class InjectRootSignatureCallback : public PPCallbacks { |
| 16 | +private: |
| 17 | + Sema &Actions; |
| 18 | + StringRef RootSigName; |
| 19 | + llvm::dxbc::RootSignatureVersion Version; |
| 20 | + |
| 21 | + std::optional<StringLiteral *> processStringLiteral(ArrayRef<Token> Tokens) { |
| 22 | + for (Token Tok : Tokens) |
| 23 | + if (!tok::isStringLiteral(Tok.getKind())) |
| 24 | + return std::nullopt; |
| 25 | + |
| 26 | + ExprResult StringResult = Actions.ActOnUnevaluatedStringLiteral(Tokens); |
| 27 | + if (StringResult.isInvalid()) |
| 28 | + return std::nullopt; |
| 29 | + |
| 30 | + if (auto Signature = dyn_cast<StringLiteral>(StringResult.get())) |
| 31 | + return Signature; |
| 32 | + |
| 33 | + return std::nullopt; |
| 34 | + } |
| 35 | + |
| 36 | +public: |
| 37 | + void MacroDefined(const Token &MacroNameTok, |
| 38 | + const MacroDirective *MD) override { |
| 39 | + if (RootSigName != MacroNameTok.getIdentifierInfo()->getName()) |
| 40 | + return; |
| 41 | + |
| 42 | + const MacroInfo *MI = MD->getMacroInfo(); |
| 43 | + auto Signature = processStringLiteral(MI->tokens()); |
| 44 | + if (!Signature.has_value()) { |
| 45 | + Actions.getDiagnostics().Report(MI->getDefinitionLoc(), |
| 46 | + diag::err_expected_string_literal) |
| 47 | + << /*in attributes...*/ 4 << "RootSignature"; |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + IdentifierInfo *DeclIdent = |
| 52 | + hlsl::ParseHLSLRootSignature(Actions, Version, *Signature); |
| 53 | + } |
| 54 | + |
| 55 | + InjectRootSignatureCallback(Sema &Actions, StringRef RootSigName, |
| 56 | + llvm::dxbc::RootSignatureVersion Version) |
| 57 | + : PPCallbacks(), Actions(Actions), RootSigName(RootSigName), |
| 58 | + Version(Version) {} |
| 59 | +}; |
| 60 | + |
| 61 | +void HLSLFrontendAction::ExecuteAction() { |
| 62 | + // Pre-requisites to invoke |
| 63 | + CompilerInstance &CI = getCompilerInstance(); |
| 64 | + if (!CI.hasASTContext() || !CI.hasPreprocessor()) |
| 65 | + return WrapperFrontendAction::ExecuteAction(); |
| 66 | + |
| 67 | + // InjectRootSignatureCallback requires access to invoke Sema to lookup/ |
| 68 | + // register a root signature declaration. The wrapped action is required to |
| 69 | + // account for this by only creating a Sema if one doesn't already exist |
| 70 | + // (like we have done, and, ASTFrontendAction::ExecuteAction) |
| 71 | + if (!CI.hasSema()) |
| 72 | + CI.createSema(getTranslationUnitKind(), |
| 73 | + /*CodeCompleteConsumer=*/nullptr); |
| 74 | + Sema &S = CI.getSema(); |
| 75 | + |
| 76 | + // Register HLSL specific callbacks |
| 77 | + auto LangOpts = CI.getLangOpts(); |
| 78 | + auto MacroCallback = std::make_unique<InjectRootSignatureCallback>( |
| 79 | + S, LangOpts.HLSLRootSigOverride, LangOpts.HLSLRootSigVer); |
| 80 | + |
| 81 | + Preprocessor &PP = CI.getPreprocessor(); |
| 82 | + PP.addPPCallbacks(std::move(MacroCallback)); |
| 83 | + |
| 84 | + // Invoke as normal |
| 85 | + WrapperFrontendAction::ExecuteAction(); |
| 86 | +} |
| 87 | + |
| 88 | +HLSLFrontendAction::HLSLFrontendAction( |
| 89 | + std::unique_ptr<FrontendAction> WrappedAction) |
| 90 | + : WrapperFrontendAction(std::move(WrappedAction)) {} |
| 91 | + |
| 92 | +} // namespace clang |
0 commit comments