MCAsmParserExtension.h 3.8 KB

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