Token.h revision 000732226610650837478cba97843d19b75f648e
1//===--- Token.h - Token interface ------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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.
28class Token {
29  /// The location and length of the token text itself.
30  SourceLocation Loc;
31  unsigned Length;
32
33  /// IdentifierInfo - If this was an identifier, this points to the uniqued
34  /// information about this identifier.
35  IdentifierInfo *IdentInfo;
36
37  /// Kind - The actual flavor of token this is.
38  ///
39  tok::TokenKind Kind : 8;
40
41  /// Flags - Bits we track about this token, members of the TokenFlags enum.
42  unsigned Flags : 8;
43public:
44
45  // Various flags set per token:
46  enum TokenFlags {
47    StartOfLine   = 0x01,  // At start of line or only after whitespace.
48    LeadingSpace  = 0x02,  // Whitespace exists before this token.
49    DisableExpand = 0x04,  // This identifier may never be macro expanded.
50    NeedsCleaning = 0x08   // Contained an escaped newline or trigraph.
51  };
52
53  tok::TokenKind getKind() const { return Kind; }
54  void setKind(tok::TokenKind K) { Kind = K; }
55
56  /// is/isNot - Predicates to check if this token is a specific kind, as in
57  /// "if (Tok.is(tok::l_brace)) {...}".
58  bool is(tok::TokenKind K) const { return Kind == K; }
59  bool isNot(tok::TokenKind K) const { return Kind != K; }
60
61  /// getLocation - Return a source location identifier for the specified
62  /// offset in the current file.
63  SourceLocation getLocation() const { return Loc; }
64  unsigned getLength() const { return Length; }
65
66  void setLocation(SourceLocation L) { Loc = L; }
67  void setLength(unsigned Len) { Length = Len; }
68
69  const char *getName() const { return tok::getTokenName(Kind); }
70
71  /// startToken - Reset all flags to cleared.
72  ///
73  void startToken() {
74    Flags = 0;
75    IdentInfo = 0;
76    Loc = SourceLocation();
77  }
78
79  IdentifierInfo *getIdentifierInfo() const { return IdentInfo; }
80  void setIdentifierInfo(IdentifierInfo *II) {
81    IdentInfo = II;
82  }
83
84  /// setFlag - Set the specified flag.
85  void setFlag(TokenFlags Flag) {
86    Flags |= Flag;
87  }
88
89  /// clearFlag - Unset the specified flag.
90  void clearFlag(TokenFlags Flag) {
91    Flags &= ~Flag;
92  }
93
94  /// setFlagValue - Set a flag to either true or false.
95  void setFlagValue(TokenFlags Flag, bool Val) {
96    if (Val)
97      setFlag(Flag);
98    else
99      clearFlag(Flag);
100  }
101
102  /// isAtStartOfLine - Return true if this token is at the start of a line.
103  ///
104  bool isAtStartOfLine() const { return (Flags & StartOfLine) ? true : false; }
105
106  /// hasLeadingSpace - Return true if this token has whitespace before it.
107  ///
108  bool hasLeadingSpace() const { return (Flags & LeadingSpace) ? true : false; }
109
110  /// isExpandDisabled - Return true if this identifier token should never
111  /// be expanded in the future, due to C99 6.10.3.4p2.
112  bool isExpandDisabled() const { return (Flags & DisableExpand) ? true : false; }
113
114  /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
115  bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const;
116
117  /// getObjCKeywordID - Return the ObjC keyword kind.
118  tok::ObjCKeywordKind getObjCKeywordID() const;
119
120  /// needsCleaning - Return true if this token has trigraphs or escaped
121  /// newlines in it.
122  ///
123  bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; }
124};
125
126/// PPConditionalInfo - Information about the conditional stack (#if directives)
127/// currently active.
128struct PPConditionalInfo {
129  /// IfLoc - Location where the conditional started.
130  ///
131  SourceLocation IfLoc;
132
133  /// WasSkipping - True if this was contained in a skipping directive, e.g.
134  /// in a "#if 0" block.
135  bool WasSkipping;
136
137  /// FoundNonSkip - True if we have emitted tokens already, and now we're in
138  /// an #else block or something.  Only useful in Skipping blocks.
139  bool FoundNonSkip;
140
141  /// FoundElse - True if we've seen a #else in this block.  If so,
142  /// #elif/#else directives are not allowed.
143  bool FoundElse;
144};
145
146}  // end namespace clang
147
148#endif
149