blob: c37889cfc50969c59b69bc9924860116582a39a7 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/MC/MCAsmParserExtension.h - Asm Parser Hooks --------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_MC_MCPARSER_MCASMPARSEREXTENSION_H
10#define LLVM_MC_MCPARSER_MCASMPARSEREXTENSION_H
11
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/MC/MCParser/MCAsmLexer.h"
15#include "llvm/MC/MCParser/MCAsmParser.h"
16#include "llvm/Support/SMLoc.h"
17
18namespace llvm {
19
20class Twine;
21
Andrew Scullcdfcccc2018-10-05 20:58:37 +010022/// Generic interface for extending the MCAsmParser,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010023/// which is implemented by target and object file assembly parser
24/// implementations.
25class MCAsmParserExtension {
26 MCAsmParser *Parser;
27
28protected:
29 MCAsmParserExtension();
30
31 // Helper template for implementing static dispatch functions.
32 template<typename T, bool (T::*Handler)(StringRef, SMLoc)>
33 static bool HandleDirective(MCAsmParserExtension *Target,
34 StringRef Directive,
35 SMLoc DirectiveLoc) {
36 T *Obj = static_cast<T*>(Target);
37 return (Obj->*Handler)(Directive, DirectiveLoc);
38 }
39
40 bool BracketExpressionsSupported = false;
41
42public:
43 MCAsmParserExtension(const MCAsmParserExtension &) = delete;
44 MCAsmParserExtension &operator=(const MCAsmParserExtension &) = delete;
45 virtual ~MCAsmParserExtension();
46
Andrew Scullcdfcccc2018-10-05 20:58:37 +010047 /// Initialize the extension for parsing using the given \p Parser.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048 /// The extension should use the AsmParser interfaces to register its
49 /// parsing routines.
50 virtual void Initialize(MCAsmParser &Parser);
51
52 /// \name MCAsmParser Proxy Interfaces
53 /// @{
54
55 MCContext &getContext() { return getParser().getContext(); }
56
57 MCAsmLexer &getLexer() { return getParser().getLexer(); }
58 const MCAsmLexer &getLexer() const {
59 return const_cast<MCAsmParserExtension *>(this)->getLexer();
60 }
61
62 MCAsmParser &getParser() { return *Parser; }
63 const MCAsmParser &getParser() const {
64 return const_cast<MCAsmParserExtension*>(this)->getParser();
65 }
66
67 SourceMgr &getSourceManager() { return getParser().getSourceManager(); }
68 MCStreamer &getStreamer() { return getParser().getStreamer(); }
69
70 bool Warning(SMLoc L, const Twine &Msg) {
71 return getParser().Warning(L, Msg);
72 }
73
74 bool Error(SMLoc L, const Twine &Msg, SMRange Range = SMRange()) {
75 return getParser().Error(L, Msg, Range);
76 }
77
78 void Note(SMLoc L, const Twine &Msg) {
79 getParser().Note(L, Msg);
80 }
81
82 bool TokError(const Twine &Msg) {
83 return getParser().TokError(Msg);
84 }
85
86 const AsmToken &Lex() { return getParser().Lex(); }
87 const AsmToken &getTok() { return getParser().getTok(); }
88 bool parseToken(AsmToken::TokenKind T,
89 const Twine &Msg = "unexpected token") {
90 return getParser().parseToken(T, Msg);
91 }
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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200101 bool ParseDirectiveCGProfile(StringRef, SMLoc);
102
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100103 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