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