Token.h revision d217773f106856a11879ec79dc468efefaf2ee75
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  /// getLocation - Return a source location identifier for the specified
57  /// offset in the current file.
58  SourceLocation getLocation() const { return Loc; }
59  unsigned getLength() const { return Length; }
60
61  void setLocation(SourceLocation L) { Loc = L; }
62  void setLength(unsigned Len) { Length = Len; }
63
64  const char *getName() const { return getTokenName(Kind); }
65
66  /// startToken - Reset all flags to cleared.
67  ///
68  void startToken() {
69    Flags = 0;
70    IdentInfo = 0;
71    Loc = SourceLocation();
72  }
73
74  IdentifierInfo *getIdentifierInfo() const { return IdentInfo; }
75  void setIdentifierInfo(IdentifierInfo *II) {
76    IdentInfo = II;
77  }
78
79  /// setFlag - Set the specified flag.
80  void setFlag(TokenFlags Flag) {
81    Flags |= Flag;
82  }
83
84  /// clearFlag - Unset the specified flag.
85  void clearFlag(TokenFlags Flag) {
86    Flags &= ~Flag;
87  }
88
89  /// setFlagValue - Set a flag to either true or false.
90  void setFlagValue(TokenFlags Flag, bool Val) {
91    if (Val)
92      setFlag(Flag);
93    else
94      clearFlag(Flag);
95  }
96
97  /// isAtStartOfLine - Return true if this token is at the start of a line.
98  ///
99  bool isAtStartOfLine() const { return Flags & StartOfLine; }
100
101  /// hasLeadingSpace - Return true if this token has whitespace before it.
102  ///
103  bool hasLeadingSpace() const { return Flags & LeadingSpace; }
104
105  /// isExpandDisabled - Return true if this identifier token should never
106  /// be expanded in the future, due to C99 6.10.3.4p2.
107  bool isExpandDisabled() const { return Flags & DisableExpand; }
108
109  /// needsCleaning - Return true if this token has trigraphs or escaped
110  /// newlines in it.
111  ///
112  bool needsCleaning() const { return Flags & NeedsCleaning; }
113};
114
115/// PPConditionalInfo - Information about the conditional stack (#if directives)
116/// currently active.
117struct PPConditionalInfo {
118  /// IfLoc - Location where the conditional started.
119  ///
120  SourceLocation IfLoc;
121
122  /// WasSkipping - True if this was contained in a skipping directive, e.g.
123  /// in a "#if 0" block.
124  bool WasSkipping;
125
126  /// FoundNonSkip - True if we have emitted tokens already, and now we're in
127  /// an #else block or something.  Only useful in Skipping blocks.
128  bool FoundNonSkip;
129
130  /// FoundElse - True if we've seen a #else in this block.  If so,
131  /// #elif/#else directives are not allowed.
132  bool FoundElse;
133};
134
135}  // end namespace clang
136
137#endif
138