Token.h revision ef3b215c946d4813408b5fe872fe2baa3b246b00
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.
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  unsigned Kind : 8;  // DON'T make Kind a 'tok::TokenKind';
40                      // MSVC will treat it as a signed char and
41                      // TokenKinds > 127 won't be handled correctly.
42
43  /// Flags - Bits we track about this token, members of the TokenFlags enum.
44  unsigned Flags : 8;
45public:
46
47  // Various flags set per token:
48  enum TokenFlags {
49    StartOfLine   = 0x01,  // At start of line or only after whitespace.
50    LeadingSpace  = 0x02,  // Whitespace exists before this token.
51    DisableExpand = 0x04,  // This identifier may never be macro expanded.
52    NeedsCleaning = 0x08   // Contained an escaped newline or trigraph.
53  };
54
55  tok::TokenKind getKind() const { return (tok::TokenKind)Kind; }
56  void setKind(tok::TokenKind K) { Kind = K; }
57
58  /// is/isNot - Predicates to check if this token is a specific kind, as in
59  /// "if (Tok.is(tok::l_brace)) {...}".
60  bool is(tok::TokenKind K) const { return Kind == (unsigned) K; }
61  bool isNot(tok::TokenKind K) const { return Kind != (unsigned) K; }
62
63  /// getLocation - Return a source location identifier for the specified
64  /// offset in the current file.
65  SourceLocation getLocation() const { return Loc; }
66  unsigned getLength() const { return Length; }
67
68  void setLocation(SourceLocation L) { Loc = L; }
69  void setLength(unsigned Len) { Length = Len; }
70
71  const char *getName() const {
72    return tok::getTokenName( (tok::TokenKind) Kind);
73  }
74
75  /// startToken - Reset all flags to cleared.
76  ///
77  void startToken() {
78    Flags = 0;
79    IdentInfo = 0;
80    Loc = SourceLocation();
81  }
82
83  IdentifierInfo *getIdentifierInfo() const { return IdentInfo; }
84  void setIdentifierInfo(IdentifierInfo *II) {
85    IdentInfo = II;
86  }
87
88  /// setFlag - Set the specified flag.
89  void setFlag(TokenFlags Flag) {
90    Flags |= Flag;
91  }
92
93  /// clearFlag - Unset the specified flag.
94  void clearFlag(TokenFlags Flag) {
95    Flags &= ~Flag;
96  }
97
98  /// getFlags - Return the internal represtation of the flags.
99  ///  Only intended for low-level operations such as writing tokens to
100  //   disk.
101  unsigned getFlags() const {
102    return Flags;
103  }
104
105  /// setFlagValue - Set a flag to either true or false.
106  void setFlagValue(TokenFlags Flag, bool Val) {
107    if (Val)
108      setFlag(Flag);
109    else
110      clearFlag(Flag);
111  }
112
113  /// isAtStartOfLine - Return true if this token is at the start of a line.
114  ///
115  bool isAtStartOfLine() const { return (Flags & StartOfLine) ? true : false; }
116
117  /// hasLeadingSpace - Return true if this token has whitespace before it.
118  ///
119  bool hasLeadingSpace() const { return (Flags & LeadingSpace) ? true : false; }
120
121  /// isExpandDisabled - Return true if this identifier token should never
122  /// be expanded in the future, due to C99 6.10.3.4p2.
123  bool isExpandDisabled() const {
124    return (Flags & DisableExpand) ? true : false;
125  }
126
127  /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
128  bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const;
129
130  /// getObjCKeywordID - Return the ObjC keyword kind.
131  tok::ObjCKeywordKind getObjCKeywordID() const;
132
133  /// needsCleaning - Return true if this token has trigraphs or escaped
134  /// newlines in it.
135  ///
136  bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; }
137};
138
139/// PPConditionalInfo - Information about the conditional stack (#if directives)
140/// currently active.
141struct PPConditionalInfo {
142  /// IfLoc - Location where the conditional started.
143  ///
144  SourceLocation IfLoc;
145
146  /// WasSkipping - True if this was contained in a skipping directive, e.g.
147  /// in a "#if 0" block.
148  bool WasSkipping;
149
150  /// FoundNonSkip - True if we have emitted tokens already, and now we're in
151  /// an #else block or something.  Only useful in Skipping blocks.
152  bool FoundNonSkip;
153
154  /// FoundElse - True if we've seen a #else in this block.  If so,
155  /// #elif/#else directives are not allowed.
156  bool FoundElse;
157};
158
159}  // end namespace clang
160
161#endif
162