Token.h revision b28d6de75e0cb27ce3106ce6052f87ad0ab276d7
1//===--- Token.h - Token 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//  This file defines the Token interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_TOKEN_H
15#define LLVM_CLANG_TOKEN_H
16
17#include "clang/Basic/TokenKinds.h"
18#include "clang/Basic/SourceLocation.h"
19
20namespace clang {
21
22class IdentifierInfo;
23
24/// Token - This structure provides full information about a lexed token.
25/// It is not intended to be space efficient, it is intended to return as much
26/// information as possible about each returned token.  This is expected to be
27/// compressed into a smaller form if memory footprint is important.
28///
29/// The parser can create a special "annotation token" representing a stream of
30/// tokens that were parsed and semantically resolved, e.g.: "foo::MyClass<int>"
31/// can be represented by a single typename annotation token that carries
32/// information about the SourceRange of the tokens and the type object.
33class Token {
34  /// The location of the token.
35  SourceLocation Loc;
36
37  // Conceptually these next two fields could be in a union with
38  // access depending on isAnnotationToken(). However, this causes gcc
39  // 4.2 to pessimize LexTokenInternal, a very performance critical
40  // routine. Keeping as separate members with casts until a more
41  // beautiful fix presents itself.
42
43  /// UintData - This holds either the length of the token text, when
44  /// a normal token, or the end of the SourceRange when an annotation
45  /// token.
46  unsigned UintData;
47
48  /// PtrData - For normal tokens, this points to the uniqued
49  /// information for the identifier (if an identifier token) or
50  /// null. For annotation tokens, this points to information specific
51  /// to the annotation token.
52  void *PtrData;
53
54  /// Kind - The actual flavor of token this is.
55  ///
56  unsigned Kind : 8;  // DON'T make Kind a 'tok::TokenKind';
57                      // MSVC will treat it as a signed char and
58                      // TokenKinds > 127 won't be handled correctly.
59
60  /// Flags - Bits we track about this token, members of the TokenFlags enum.
61  unsigned Flags : 8;
62public:
63
64  // Various flags set per token:
65  enum TokenFlags {
66    StartOfLine   = 0x01,  // At start of line or only after whitespace.
67    LeadingSpace  = 0x02,  // Whitespace exists before this token.
68    DisableExpand = 0x04,  // This identifier may never be macro expanded.
69    NeedsCleaning = 0x08   // Contained an escaped newline or trigraph.
70  };
71
72  tok::TokenKind getKind() const { return (tok::TokenKind)Kind; }
73  void setKind(tok::TokenKind K) { Kind = K; }
74
75  /// is/isNot - Predicates to check if this token is a specific kind, as in
76  /// "if (Tok.is(tok::l_brace)) {...}".
77  bool is(tok::TokenKind K) const { return Kind == (unsigned) K; }
78  bool isNot(tok::TokenKind K) const { return Kind != (unsigned) K; }
79
80  bool isAnnotationToken() const {
81    return is(tok::annot_qualtypename) || is(tok::annot_cxxscope);
82  }
83
84  /// getLocation - Return a source location identifier for the specified
85  /// offset in the current file.
86  SourceLocation getLocation() const { return Loc; }
87  unsigned getLength() const {
88    assert(!isAnnotationToken() && "Used Length on annotation token");
89    return UintData;
90  }
91
92  void setLocation(SourceLocation L) { Loc = L; }
93  void setLength(unsigned Len) { UintData = Len; }
94
95  SourceLocation getAnnotationEndLoc() const {
96    assert(isAnnotationToken() && "Used AnnotEndLocID on non-annotation token");
97    return SourceLocation::getFromRawEncoding(UintData);
98  }
99  void setAnnotationEndLoc(SourceLocation L) {
100    assert(isAnnotationToken() && "Used AnnotEndLocID on non-annotation token");
101    UintData = L.getRawEncoding();
102  }
103
104  /// getAnnotationRange - SourceRange of the group of tokens that this
105  /// annotation token represents.
106  SourceRange getAnnotationRange() const {
107    return SourceRange(getLocation(), getAnnotationEndLoc());
108  }
109  void setAnnotationRange(SourceRange R) {
110    setLocation(R.getBegin());
111    setAnnotationEndLoc(R.getEnd());
112  }
113
114  const char *getName() const {
115    return tok::getTokenName( (tok::TokenKind) Kind);
116  }
117
118  /// startToken - Reset all flags to cleared.
119  ///
120  void startToken() {
121    Flags = 0;
122    PtrData = 0;
123    Loc = SourceLocation();
124  }
125
126  IdentifierInfo *getIdentifierInfo() const {
127    assert(!isAnnotationToken() && "Used IdentInfo on annotation token");
128    return (IdentifierInfo*) PtrData;
129  }
130  void setIdentifierInfo(IdentifierInfo *II) {
131    PtrData = (void*) II;
132  }
133
134  void *getAnnotationValue() const {
135    assert(isAnnotationToken() && "Used AnnotVal on non-annotation token");
136    return PtrData;
137  }
138  void setAnnotationValue(void *val) {
139    assert(isAnnotationToken() && "Used AnnotVal on non-annotation token");
140    PtrData = val;
141  }
142
143  /// setFlag - Set the specified flag.
144  void setFlag(TokenFlags Flag) {
145    Flags |= Flag;
146  }
147
148  /// clearFlag - Unset the specified flag.
149  void clearFlag(TokenFlags Flag) {
150    Flags &= ~Flag;
151  }
152
153  /// getFlags - Return the internal represtation of the flags.
154  ///  Only intended for low-level operations such as writing tokens to
155  //   disk.
156  unsigned getFlags() const {
157    return Flags;
158  }
159
160  /// setFlagValue - Set a flag to either true or false.
161  void setFlagValue(TokenFlags Flag, bool Val) {
162    if (Val)
163      setFlag(Flag);
164    else
165      clearFlag(Flag);
166  }
167
168  /// isAtStartOfLine - Return true if this token is at the start of a line.
169  ///
170  bool isAtStartOfLine() const { return (Flags & StartOfLine) ? true : false; }
171
172  /// hasLeadingSpace - Return true if this token has whitespace before it.
173  ///
174  bool hasLeadingSpace() const { return (Flags & LeadingSpace) ? true : false; }
175
176  /// isExpandDisabled - Return true if this identifier token should never
177  /// be expanded in the future, due to C99 6.10.3.4p2.
178  bool isExpandDisabled() const {
179    return (Flags & DisableExpand) ? true : false;
180  }
181
182  /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
183  bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const;
184
185  /// getObjCKeywordID - Return the ObjC keyword kind.
186  tok::ObjCKeywordKind getObjCKeywordID() const;
187
188  /// needsCleaning - Return true if this token has trigraphs or escaped
189  /// newlines in it.
190  ///
191  bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; }
192};
193
194/// PPConditionalInfo - Information about the conditional stack (#if directives)
195/// currently active.
196struct PPConditionalInfo {
197  /// IfLoc - Location where the conditional started.
198  ///
199  SourceLocation IfLoc;
200
201  /// WasSkipping - True if this was contained in a skipping directive, e.g.
202  /// in a "#if 0" block.
203  bool WasSkipping;
204
205  /// FoundNonSkip - True if we have emitted tokens already, and now we're in
206  /// an #else block or something.  Only useful in Skipping blocks.
207  bool FoundNonSkip;
208
209  /// FoundElse - True if we've seen a #else in this block.  If so,
210  /// #elif/#else directives are not allowed.
211  bool FoundElse;
212};
213
214}  // end namespace clang
215
216#endif
217