DxcLangExtensionsHelper.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxcLangExtensionsHelper.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides a helper class to implement language extensions to HLSL. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef __DXCLANGEXTENSIONSHELPER_H__
  12. #define __DXCLANGEXTENSIONSHELPER_H__
  13. #include "dxc/Support/Unicode.h"
  14. #include "dxc/Support/FileIOHelper.h"
  15. #include "dxc/Support/DxcLangExtensionsCommonHelper.h"
  16. #include <vector>
  17. namespace llvm {
  18. class raw_string_ostream;
  19. class CallInst;
  20. class Value;
  21. }
  22. namespace clang {
  23. class CompilerInstance;
  24. }
  25. namespace hlsl {
  26. class DxcLangExtensionsHelper : public DxcLangExtensionsCommonHelper, public DxcLangExtensionsHelperApply {
  27. private:
  28. public:
  29. void SetupSema(clang::Sema &S) override {
  30. clang::ExternalASTSource *astSource = S.getASTContext().getExternalSource();
  31. if (clang::ExternalSemaSource *externalSema =
  32. llvm::dyn_cast_or_null<clang::ExternalSemaSource>(astSource)) {
  33. for (auto &&table : GetIntrinsicTables()) {
  34. hlsl::RegisterIntrinsicTable(externalSema, table);
  35. }
  36. }
  37. }
  38. void SetupPreprocessorOptions(clang::PreprocessorOptions &PPOpts) override {
  39. for (const auto &define : GetDefines()) {
  40. PPOpts.addMacroDef(llvm::StringRef(define.c_str()));
  41. }
  42. }
  43. DxcLangExtensionsHelper *GetDxcLangExtensionsHelper() override {
  44. return this;
  45. }
  46. DxcLangExtensionsHelper() {}
  47. };
  48. // A parsed semantic define is a semantic define that has actually been
  49. // parsed by the compiler. It has a name (required), a value (could be
  50. // the empty string), and a location. We use an encoded clang::SourceLocation
  51. // for the location to avoid a clang include dependency.
  52. struct ParsedSemanticDefine{
  53. std::string Name;
  54. std::string Value;
  55. unsigned Location;
  56. };
  57. typedef std::vector<ParsedSemanticDefine> ParsedSemanticDefineList;
  58. // Confirm that <name> matches the star pattern in <mask>
  59. inline bool IsMacroMatch(StringRef name, const std::string &mask) {
  60. return Unicode::IsStarMatchUTF8(mask.c_str(), mask.size(), name.data(),
  61. name.size());
  62. }
  63. // Return the collection of semantic defines parsed by the compiler instance.
  64. ParsedSemanticDefineList
  65. CollectSemanticDefinesParsedByCompiler(clang::CompilerInstance &compiler,
  66. _In_ DxcLangExtensionsHelper *helper);
  67. } // namespace hlsl
  68. #endif