1 //===- llvm/MC/MCAsmParserExtension.h - Asm Parser Hooks --------*- C++ -*-===//
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 #ifndef LLVM_MC_MCPARSER_MCASMPARSEREXTENSION_H
10 #define LLVM_MC_MCPARSER_MCASMPARSEREXTENSION_H
11 
12 #include "llvm/ADT/STLFunctionalExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/MC/MCParser/MCAsmParser.h"
15 #include "llvm/Support/SMLoc.h"
16 
17 namespace llvm {
18 
19 class Twine;
20 
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 = nullptr;
26 
27 protected:
28   MCAsmParserExtension();
29 
30   // Helper template for implementing static dispatch functions.
31   template<typename T, bool (T::*Handler)(StringRef, SMLoc)>
32   static bool HandleDirective(MCAsmParserExtension *Target,
33                               StringRef Directive,
34                               SMLoc DirectiveLoc) {
35     T *Obj = static_cast<T*>(Target);
36     return (Obj->*Handler)(Directive, DirectiveLoc);
37   }
38 
39   bool BracketExpressionsSupported = false;
40 
41 public:
42   MCAsmParserExtension(const MCAsmParserExtension &) = delete;
43   MCAsmParserExtension &operator=(const MCAsmParserExtension &) = delete;
44   virtual ~MCAsmParserExtension();
45 
46   /// Initialize the extension for parsing using the given \p Parser.
47   /// The extension should use the AsmParser interfaces to register its
48   /// parsing routines.
49   virtual void Initialize(MCAsmParser &Parser);
50 
51   /// \name MCAsmParser Proxy Interfaces
52   /// @{
53 
54   MCContext &getContext() { return getParser().getContext(); }
55 
56   MCAsmLexer &getLexer() { return getParser().getLexer(); }
57   const MCAsmLexer &getLexer() const {
58     return const_cast<MCAsmParserExtension *>(this)->getLexer();
59   }
60 
61   MCAsmParser &getParser() { return *Parser; }
62   const MCAsmParser &getParser() const {
63     return const_cast<MCAsmParserExtension*>(this)->getParser();
64   }
65 
66   SourceMgr &getSourceManager() { return getParser().getSourceManager(); }
67   MCStreamer &getStreamer() { return getParser().getStreamer(); }
68 
69   bool Warning(SMLoc L, const Twine &Msg) {
70     return getParser().Warning(L, Msg);
71   }
72 
73   bool Error(SMLoc L, const Twine &Msg, SMRange Range = SMRange()) {
74     return getParser().Error(L, Msg, Range);
75   }
76 
77   void Note(SMLoc L, const Twine &Msg) {
78     getParser().Note(L, Msg);
79   }
80 
81   bool TokError(const Twine &Msg) {
82     return getParser().TokError(Msg);
83   }
84 
85   const AsmToken &Lex() { return getParser().Lex(); }
86   const AsmToken &getTok() { return getParser().getTok(); }
87   bool parseToken(AsmToken::TokenKind T,
88                   const Twine &Msg = "unexpected token") {
89     return getParser().parseToken(T, Msg);
90   }
91   bool parseEOL() { return getParser().parseEOL(); }
92 
93   bool parseMany(function_ref<bool()> parseOne, bool hasComma = true) {
94     return getParser().parseMany(parseOne, hasComma);
95   }
96 
97   bool parseOptionalToken(AsmToken::TokenKind T) {
98     return getParser().parseOptionalToken(T);
99   }
100 
101   bool ParseDirectiveCGProfile(StringRef, SMLoc);
102 
103   bool check(bool P, const Twine &Msg) {
104     return getParser().check(P, Msg);
105   }
106 
107   bool check(bool P, SMLoc Loc, const Twine &Msg) {
108     return getParser().check(P, Loc, Msg);
109   }
110 
111   bool addErrorSuffix(const Twine &Suffix) {
112     return getParser().addErrorSuffix(Suffix);
113   }
114 
115   bool HasBracketExpressions() const { return BracketExpressionsSupported; }
116 
117   /// @}
118 };
119 
120 } // end namespace llvm
121 
122 #endif // LLVM_MC_MCPARSER_MCASMPARSEREXTENSION_H
123