llvm_tools.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_BASE_LLVM_TOOLS_H_
  5. #define CARBON_TOOLCHAIN_BASE_LLVM_TOOLS_H_
  6. #include <cstdint>
  7. #include "common/command_line.h"
  8. #include "common/enum_base.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/Support/LLVMDriver.h"
  12. namespace Carbon {
  13. CARBON_DEFINE_RAW_ENUM_CLASS(LLVMTool, uint8_t) {
  14. #define CARBON_LLVM_TOOL(Identifier, Name, BinName, MainFn) \
  15. CARBON_RAW_ENUM_ENUMERATOR(Identifier)
  16. #include "toolchain/base/llvm_tools.def"
  17. };
  18. // An enum-like class for each of the LLVM tools.
  19. //
  20. // This can be used like an enum to track a specific one of the LLVM tools. It
  21. // also has a collection of methods to access various aspects of the tools
  22. // themselves, including the symbol used to invoke the given tool.
  23. //
  24. // The instances of this class are generated from `llvm_tools.bzl`, see that
  25. // file for more details.
  26. class LLVMTool : public CARBON_ENUM_BASE(LLVMTool) {
  27. public:
  28. #define CARBON_LLVM_TOOL(Identifier, Name, BinName, MainFn) \
  29. CARBON_ENUM_CONSTANT_DECL(Identifier)
  30. #include "toolchain/base/llvm_tools.def"
  31. static const llvm::ArrayRef<LLVMTool> Tools;
  32. using MainFnT = auto(int argc, char** argv, const llvm::ToolContext& context)
  33. -> int;
  34. using EnumBase::EnumBase;
  35. auto bin_name() const -> llvm::StringLiteral { return BinNames[AsInt()]; }
  36. auto main_fn() const -> MainFnT* { return MainFns[AsInt()]; }
  37. auto subcommand_info() const -> const CommandLine::CommandInfo& {
  38. return SubcommandInfos[AsInt()];
  39. }
  40. private:
  41. static const LLVMTool ToolsStorage[];
  42. static const llvm::StringLiteral BinNames[];
  43. static MainFnT* const MainFns[];
  44. static const CommandLine::CommandInfo SubcommandInfos[];
  45. };
  46. #define CARBON_LLVM_TOOL(Identifier, Name, BinName, MainFn) \
  47. CARBON_ENUM_CONSTANT_DEFINITION(LLVMTool, Identifier)
  48. #include "toolchain/base/llvm_tools.def"
  49. inline constexpr LLVMTool LLVMTool::ToolsStorage[] = {
  50. #define CARBON_LLVM_TOOL(Identifier, Name, BinName, MainFn) \
  51. LLVMTool::Identifier,
  52. #include "toolchain/base/llvm_tools.def"
  53. };
  54. inline constexpr llvm::ArrayRef<LLVMTool> LLVMTool::Tools = ToolsStorage;
  55. } // namespace Carbon
  56. #endif // CARBON_TOOLCHAIN_BASE_LLVM_TOOLS_H_