llvm-mc: Tweak MCCodeEmitter skeleton.
[oota-llvm.git] / include / llvm / MC / MCAsmLexer.h
1 //===-- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface -----*- 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_MCASMLEXER_H
11 #define LLVM_MC_MCASMLEXER_H
12
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/DataTypes.h"
15
16 namespace llvm {
17 class MCAsmLexer;
18 class MCInst;
19 class SMLoc;
20 class Target;
21
22 /// AsmToken - Target independent representation for an assembler token.
23 struct AsmToken {
24   enum TokenKind {
25     // Markers
26     Eof, Error,
27
28     // String values.
29     Identifier,
30     Register,
31     String,
32     
33     // Integer values.
34     Integer,
35     
36     // No-value.
37     EndOfStatement,
38     Colon,
39     Plus, Minus, Tilde,
40     Slash,    // '/'
41     LParen, RParen,
42     Star, Comma, Dollar, Equal, EqualEqual,
43     
44     Pipe, PipePipe, Caret, 
45     Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, 
46     Less, LessEqual, LessLess, LessGreater,
47     Greater, GreaterEqual, GreaterGreater
48   };
49
50   TokenKind Kind;
51
52   /// A reference to the entire token contents; this is always a pointer into
53   /// a memory buffer owned by the source manager.
54   StringRef Str;
55
56   int64_t IntVal;
57
58 public:
59   AsmToken() {}
60   AsmToken(TokenKind _Kind, const StringRef &_Str, int64_t _IntVal = 0)
61     : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
62
63   TokenKind getKind() const { return Kind; }
64   bool is(TokenKind K) const { return Kind == K; }
65   bool isNot(TokenKind K) const { return Kind != K; }
66
67   SMLoc getLoc() const;
68
69   /// getStringContents - Get the contents of a string token (without quotes).
70   StringRef getStringContents() const { 
71     assert(Kind == String && "This token isn't a string!");
72     return Str.slice(1, Str.size() - 1);
73   }
74
75   /// getIdentifier - Get the identifier string for the current token, which
76   /// should be an identifier or a string. This gets the portion of the string
77   /// which should be used as the identifier, e.g., it does not include the
78   /// quotes on strings.
79   StringRef getIdentifier() const {
80     if (Kind == Identifier)
81       return getString();
82     return getStringContents();
83   }
84
85   /// getString - Get the string for the current token, this includes all
86   /// characters (for example, the quotes on strings) in the token.
87   ///
88   /// The returned StringRef points into the source manager's memory buffer, and
89   /// is safe to store across calls to Lex().
90   StringRef getString() const { return Str; }
91
92   // FIXME: Don't compute this in advance, it makes every token larger, and is
93   // also not generally what we want (it is nicer for recovery etc. to lex 123br
94   // as a single token, then diagnose as an invalid number).
95   int64_t getIntVal() const { 
96     assert(Kind == Integer && "This token isn't an integer!");
97     return IntVal; 
98   }
99 };
100
101 /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
102 /// assembly lexers.
103 class MCAsmLexer {
104   /// The current token, stored in the base class for faster access.
105   AsmToken CurTok;
106
107   MCAsmLexer(const MCAsmLexer &);   // DO NOT IMPLEMENT
108   void operator=(const MCAsmLexer &);  // DO NOT IMPLEMENT
109 protected: // Can only create subclasses.
110   MCAsmLexer();
111
112   virtual AsmToken LexToken() = 0;
113
114 public:
115   virtual ~MCAsmLexer();
116
117   /// Lex - Consume the next token from the input stream and return it.
118   ///
119   /// The lexer will continuosly return the end-of-file token once the end of
120   /// the main input file has been reached.
121   const AsmToken &Lex() {
122     return CurTok = LexToken();
123   }
124
125   /// getTok - Get the current (last) lexed token.
126   const AsmToken &getTok() {
127     return CurTok;
128   }
129
130   /// getKind - Get the kind of current token.
131   AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
132
133   /// is - Check if the current token has kind \arg K.
134   bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
135
136   /// isNot - Check if the current token has kind \arg K.
137   bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
138 };
139
140 } // End llvm namespace
141
142 #endif