Token.h revision 0bc735ffcfb223c0186419547abaa5c84482663e
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  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  /// isNamedIdentifier - Return true if this token is a ppidentifier with the
85  /// specified name.  For example, tok.isNamedIdentifier("this").
86  bool isNamedIdentifier(const char *Name) const;
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  /// setFlagValue - Set a flag to either true or false.
99  void setFlagValue(TokenFlags Flag, bool Val) {
100    if (Val)
101      setFlag(Flag);
102    else
103      clearFlag(Flag);
104  }
105
106  /// isAtStartOfLine - Return true if this token is at the start of a line.
107  ///
108  bool isAtStartOfLine() const { return (Flags & StartOfLine) ? true : false; }
109
110  /// hasLeadingSpace - Return true if this token has whitespace before it.
111  ///
112  bool hasLeadingSpace() const { return (Flags & LeadingSpace) ? true : false; }
113
114  /// isExpandDisabled - Return true if this identifier token should never
115  /// be expanded in the future, due to C99 6.10.3.4p2.
116  bool isExpandDisabled() const { return (Flags & DisableExpand) ? true : false; }
117
118  /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
119  bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const;
120
121  /// getObjCKeywordID - Return the ObjC keyword kind.
122  tok::ObjCKeywordKind getObjCKeywordID() const;
123
124  /// needsCleaning - Return true if this token has trigraphs or escaped
125  /// newlines in it.
126  ///
127  bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; }
128};
129
130/// PPConditionalInfo - Information about the conditional stack (#if directives)
131/// currently active.
132struct PPConditionalInfo {
133  /// IfLoc - Location where the conditional started.
134  ///
135  SourceLocation IfLoc;
136
137  /// WasSkipping - True if this was contained in a skipping directive, e.g.
138  /// in a "#if 0" block.
139  bool WasSkipping;
140
141  /// FoundNonSkip - True if we have emitted tokens already, and now we're in
142  /// an #else block or something.  Only useful in Skipping blocks.
143  bool FoundNonSkip;
144
145  /// FoundElse - True if we've seen a #else in this block.  If so,
146  /// #elif/#else directives are not allowed.
147  bool FoundElse;
148};
149
150}  // end namespace clang
151
152#endif
153