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