MCAsmParserExtension.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/MC/MCAsmParserExtension.h - Asm Parser Hooks --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCPARSER_MCASMPARSEREXTENSION_H
  14. #define LLVM_MC_MCPARSER_MCASMPARSEREXTENSION_H
  15. #include "llvm/ADT/STLFunctionalExtras.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/MC/MCParser/MCAsmParser.h"
  18. #include "llvm/Support/SMLoc.h"
  19. namespace llvm {
  20. class Twine;
  21. /// Generic interface for extending the MCAsmParser,
  22. /// which is implemented by target and object file assembly parser
  23. /// implementations.
  24. class MCAsmParserExtension {
  25. MCAsmParser *Parser;
  26. protected:
  27. MCAsmParserExtension();
  28. // Helper template for implementing static dispatch functions.
  29. template<typename T, bool (T::*Handler)(StringRef, SMLoc)>
  30. static bool HandleDirective(MCAsmParserExtension *Target,
  31. StringRef Directive,
  32. SMLoc DirectiveLoc) {
  33. T *Obj = static_cast<T*>(Target);
  34. return (Obj->*Handler)(Directive, DirectiveLoc);
  35. }
  36. bool BracketExpressionsSupported = false;
  37. public:
  38. MCAsmParserExtension(const MCAsmParserExtension &) = delete;
  39. MCAsmParserExtension &operator=(const MCAsmParserExtension &) = delete;
  40. virtual ~MCAsmParserExtension();
  41. /// Initialize the extension for parsing using the given \p Parser.
  42. /// The extension should use the AsmParser interfaces to register its
  43. /// parsing routines.
  44. virtual void Initialize(MCAsmParser &Parser);
  45. /// \name MCAsmParser Proxy Interfaces
  46. /// @{
  47. MCContext &getContext() { return getParser().getContext(); }
  48. MCAsmLexer &getLexer() { return getParser().getLexer(); }
  49. const MCAsmLexer &getLexer() const {
  50. return const_cast<MCAsmParserExtension *>(this)->getLexer();
  51. }
  52. MCAsmParser &getParser() { return *Parser; }
  53. const MCAsmParser &getParser() const {
  54. return const_cast<MCAsmParserExtension*>(this)->getParser();
  55. }
  56. SourceMgr &getSourceManager() { return getParser().getSourceManager(); }
  57. MCStreamer &getStreamer() { return getParser().getStreamer(); }
  58. bool Warning(SMLoc L, const Twine &Msg) {
  59. return getParser().Warning(L, Msg);
  60. }
  61. bool Error(SMLoc L, const Twine &Msg, SMRange Range = SMRange()) {
  62. return getParser().Error(L, Msg, Range);
  63. }
  64. void Note(SMLoc L, const Twine &Msg) {
  65. getParser().Note(L, Msg);
  66. }
  67. bool TokError(const Twine &Msg) {
  68. return getParser().TokError(Msg);
  69. }
  70. const AsmToken &Lex() { return getParser().Lex(); }
  71. const AsmToken &getTok() { return getParser().getTok(); }
  72. bool parseToken(AsmToken::TokenKind T,
  73. const Twine &Msg = "unexpected token") {
  74. return getParser().parseToken(T, Msg);
  75. }
  76. bool parseEOL() { return getParser().parseEOL(); }
  77. bool parseMany(function_ref<bool()> parseOne, bool hasComma = true) {
  78. return getParser().parseMany(parseOne, hasComma);
  79. }
  80. bool parseOptionalToken(AsmToken::TokenKind T) {
  81. return getParser().parseOptionalToken(T);
  82. }
  83. bool ParseDirectiveCGProfile(StringRef, SMLoc);
  84. bool check(bool P, const Twine &Msg) {
  85. return getParser().check(P, Msg);
  86. }
  87. bool check(bool P, SMLoc Loc, const Twine &Msg) {
  88. return getParser().check(P, Loc, Msg);
  89. }
  90. bool addErrorSuffix(const Twine &Suffix) {
  91. return getParser().addErrorSuffix(Suffix);
  92. }
  93. bool HasBracketExpressions() const { return BracketExpressionsSupported; }
  94. /// @}
  95. };
  96. } // end namespace llvm
  97. #endif // LLVM_MC_MCPARSER_MCASMPARSEREXTENSION_H
  98. #ifdef __GNUC__
  99. #pragma GCC diagnostic pop
  100. #endif