LLVM API Documentation

MCAsmLexer.h

Go to the documentation of this file.
00001 //===-- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_MC_MCASMLEXER_H
00011 #define LLVM_MC_MCASMLEXER_H
00012 
00013 #include "llvm/ADT/StringRef.h"
00014 #include "llvm/Support/DataTypes.h"
00015 
00016 namespace llvm {
00017 class MCAsmLexer;
00018 class MCInst;
00019 class SMLoc;
00020 class Target;
00021 
00022 /// AsmToken - Target independent representation for an assembler token.
00023 struct AsmToken {
00024   enum TokenKind {
00025     // Markers
00026     Eof, Error,
00027 
00028     // String values.
00029     Identifier,
00030     Register,
00031     String,
00032     
00033     // Integer values.
00034     Integer,
00035     
00036     // No-value.
00037     EndOfStatement,
00038     Colon,
00039     Plus, Minus, Tilde,
00040     Slash,    // '/'
00041     LParen, RParen,
00042     Star, Comma, Dollar, Equal, EqualEqual,
00043     
00044     Pipe, PipePipe, Caret, 
00045     Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, 
00046     Less, LessEqual, LessLess, LessGreater,
00047     Greater, GreaterEqual, GreaterGreater
00048   };
00049 
00050   TokenKind Kind;
00051 
00052   /// A reference to the entire token contents; this is always a pointer into
00053   /// a memory buffer owned by the source manager.
00054   StringRef Str;
00055 
00056   int64_t IntVal;
00057 
00058 public:
00059   AsmToken() {}
00060   AsmToken(TokenKind _Kind, const StringRef &_Str, int64_t _IntVal = 0)
00061     : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
00062 
00063   TokenKind getKind() const { return Kind; }
00064   bool is(TokenKind K) const { return Kind == K; }
00065   bool isNot(TokenKind K) const { return Kind != K; }
00066 
00067   SMLoc getLoc() const;
00068 
00069   /// getStringContents - Get the contents of a string token (without quotes).
00070   StringRef getStringContents() const { 
00071     assert(Kind == String && "This token isn't a string!");
00072     return Str.slice(1, Str.size() - 1);
00073   }
00074 
00075   /// getIdentifier - Get the identifier string for the current token, which
00076   /// should be an identifier or a string. This gets the portion of the string
00077   /// which should be used as the identifier, e.g., it does not include the
00078   /// quotes on strings.
00079   StringRef getIdentifier() const {
00080     if (Kind == Identifier)
00081       return getString();
00082     return getStringContents();
00083   }
00084 
00085   /// getString - Get the string for the current token, this includes all
00086   /// characters (for example, the quotes on strings) in the token.
00087   ///
00088   /// The returned StringRef points into the source manager's memory buffer, and
00089   /// is safe to store across calls to Lex().
00090   StringRef getString() const { return Str; }
00091 
00092   // FIXME: Don't compute this in advance, it makes every token larger, and is
00093   // also not generally what we want (it is nicer for recovery etc. to lex 123br
00094   // as a single token, then diagnose as an invalid number).
00095   int64_t getIntVal() const { 
00096     assert(Kind == Integer && "This token isn't an integer!");
00097     return IntVal; 
00098   }
00099 };
00100 
00101 /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
00102 /// assembly lexers.
00103 class MCAsmLexer {
00104   /// The current token, stored in the base class for faster access.
00105   AsmToken CurTok;
00106 
00107   MCAsmLexer(const MCAsmLexer &);   // DO NOT IMPLEMENT
00108   void operator=(const MCAsmLexer &);  // DO NOT IMPLEMENT
00109 protected: // Can only create subclasses.
00110   MCAsmLexer();
00111 
00112   virtual AsmToken LexToken() = 0;
00113 
00114 public:
00115   virtual ~MCAsmLexer();
00116 
00117   /// Lex - Consume the next token from the input stream and return it.
00118   ///
00119   /// The lexer will continuosly return the end-of-file token once the end of
00120   /// the main input file has been reached.
00121   const AsmToken &Lex() {
00122     return CurTok = LexToken();
00123   }
00124 
00125   /// getTok - Get the current (last) lexed token.
00126   const AsmToken &getTok() {
00127     return CurTok;
00128   }
00129 
00130   /// getKind - Get the kind of current token.
00131   AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
00132 
00133   /// is - Check if the current token has kind \arg K.
00134   bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
00135 
00136   /// isNot - Check if the current token has kind \arg K.
00137   bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
00138 };
00139 
00140 } // End llvm namespace
00141 
00142 #endif