1 //===--- LiteralSupport.h ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the NumericLiteralParser, CharLiteralParser, and
11 // StringLiteralParser interfaces.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LEX_LITERALSUPPORT_H
16 #define LLVM_CLANG_LEX_LITERALSUPPORT_H
18 #include "clang/Basic/CharInfo.h"
19 #include "clang/Basic/LLVM.h"
20 #include "clang/Basic/TokenKinds.h"
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/DataTypes.h"
28 class DiagnosticsEngine;
36 /// Copy characters from Input to Buf, expanding any UCNs.
37 void expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input);
39 /// NumericLiteralParser - This performs strict semantic analysis of the content
40 /// of a ppnumber, classifying it as either integer, floating, or erroneous,
41 /// determines the radix of the value and can convert it to a useful value.
42 class NumericLiteralParser {
43 Preprocessor &PP; // needed for diagnostics
45 const char *const ThisTokBegin;
46 const char *const ThisTokEnd;
47 const char *DigitsBegin, *SuffixBegin; // markers
48 const char *s; // cursor
52 bool saw_exponent, saw_period, saw_ud_suffix;
54 SmallString<32> UDSuffixBuf;
57 NumericLiteralParser(StringRef TokSpelling,
58 SourceLocation TokLoc,
62 bool isLong : 1; // This is *not* set for long long.
64 bool isFloat : 1; // 1.0f
65 bool isImaginary : 1; // 1.0i
66 uint8_t MicrosoftInteger; // Microsoft suffix extension i8, i16, i32, or i64.
68 bool isIntegerLiteral() const {
69 return !saw_period && !saw_exponent;
71 bool isFloatingLiteral() const {
72 return saw_period || saw_exponent;
75 bool hasUDSuffix() const {
78 StringRef getUDSuffix() const {
79 assert(saw_ud_suffix);
82 unsigned getUDSuffixOffset() const {
83 assert(saw_ud_suffix);
84 return SuffixBegin - ThisTokBegin;
87 static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix);
89 unsigned getRadix() const { return radix; }
91 /// GetIntegerValue - Convert this numeric literal value to an APInt that
92 /// matches Val's input width. If there is an overflow (i.e., if the unsigned
93 /// value read is larger than the APInt's bits will hold), set Val to the low
94 /// bits of the result and return true. Otherwise, return false.
95 bool GetIntegerValue(llvm::APInt &Val);
97 /// GetFloatValue - Convert this numeric literal to a floating value, using
98 /// the specified APFloat fltSemantics (specifying float, double, etc).
99 /// The optional bool isExact (passed-by-reference) has its value
100 /// set to true if the returned APFloat can represent the number in the
101 /// literal exactly, and false otherwise.
102 llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
106 void ParseNumberStartingWithZero(SourceLocation TokLoc);
108 static bool isDigitSeparator(char C) { return C == '\''; }
110 enum CheckSeparatorKind { CSK_BeforeDigits, CSK_AfterDigits };
112 /// \brief Ensure that we don't have a digit separator here.
113 void checkSeparator(SourceLocation TokLoc, const char *Pos,
114 CheckSeparatorKind IsAfterDigits);
116 /// SkipHexDigits - Read and skip over any hex digits, up to End.
117 /// Return a pointer to the first non-hex digit or End.
118 const char *SkipHexDigits(const char *ptr) {
119 while (ptr != ThisTokEnd && (isHexDigit(*ptr) || isDigitSeparator(*ptr)))
124 /// SkipOctalDigits - Read and skip over any octal digits, up to End.
125 /// Return a pointer to the first non-hex digit or End.
126 const char *SkipOctalDigits(const char *ptr) {
127 while (ptr != ThisTokEnd &&
128 ((*ptr >= '0' && *ptr <= '7') || isDigitSeparator(*ptr)))
133 /// SkipDigits - Read and skip over any digits, up to End.
134 /// Return a pointer to the first non-hex digit or End.
135 const char *SkipDigits(const char *ptr) {
136 while (ptr != ThisTokEnd && (isDigit(*ptr) || isDigitSeparator(*ptr)))
141 /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
142 /// Return a pointer to the first non-binary digit or End.
143 const char *SkipBinaryDigits(const char *ptr) {
144 while (ptr != ThisTokEnd &&
145 (*ptr == '0' || *ptr == '1' || isDigitSeparator(*ptr)))
152 /// CharLiteralParser - Perform interpretation and semantic analysis of a
153 /// character literal.
154 class CharLiteralParser {
159 SmallString<32> UDSuffixBuf;
160 unsigned UDSuffixOffset;
162 CharLiteralParser(const char *begin, const char *end,
163 SourceLocation Loc, Preprocessor &PP,
164 tok::TokenKind kind);
166 bool hadError() const { return HadError; }
167 bool isAscii() const { return Kind == tok::char_constant; }
168 bool isWide() const { return Kind == tok::wide_char_constant; }
169 bool isUTF8() const { return Kind == tok::utf8_char_constant; }
170 bool isUTF16() const { return Kind == tok::utf16_char_constant; }
171 bool isUTF32() const { return Kind == tok::utf32_char_constant; }
172 bool isMultiChar() const { return IsMultiChar; }
173 uint64_t getValue() const { return Value; }
174 StringRef getUDSuffix() const { return UDSuffixBuf; }
175 unsigned getUDSuffixOffset() const {
176 assert(!UDSuffixBuf.empty() && "no ud-suffix");
177 return UDSuffixOffset;
181 /// StringLiteralParser - This decodes string escape characters and performs
182 /// wide string analysis and Translation Phase #6 (concatenation of string
183 /// literals) (C99 5.1.1.2p1).
184 class StringLiteralParser {
185 const SourceManager &SM;
186 const LangOptions &Features;
187 const TargetInfo &Target;
188 DiagnosticsEngine *Diags;
190 unsigned MaxTokenLength;
192 unsigned CharByteWidth;
194 SmallString<512> ResultBuf;
195 char *ResultPtr; // cursor
196 SmallString<32> UDSuffixBuf;
197 unsigned UDSuffixToken;
198 unsigned UDSuffixOffset;
200 StringLiteralParser(ArrayRef<Token> StringToks,
201 Preprocessor &PP, bool Complain = true);
202 StringLiteralParser(ArrayRef<Token> StringToks,
203 const SourceManager &sm, const LangOptions &features,
204 const TargetInfo &target,
205 DiagnosticsEngine *diags = nullptr)
206 : SM(sm), Features(features), Target(target), Diags(diags),
207 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
208 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
216 StringRef GetString() const {
217 return StringRef(ResultBuf.data(), GetStringLength());
219 unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
221 unsigned GetNumStringChars() const {
222 return GetStringLength() / CharByteWidth;
224 /// getOffsetOfStringByte - This function returns the offset of the
225 /// specified byte of the string data represented by Token. This handles
226 /// advancing over escape sequences in the string.
228 /// If the Diagnostics pointer is non-null, then this will do semantic
229 /// checking of the string literal and emit errors and warnings.
230 unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
232 bool isAscii() const { return Kind == tok::string_literal; }
233 bool isWide() const { return Kind == tok::wide_string_literal; }
234 bool isUTF8() const { return Kind == tok::utf8_string_literal; }
235 bool isUTF16() const { return Kind == tok::utf16_string_literal; }
236 bool isUTF32() const { return Kind == tok::utf32_string_literal; }
237 bool isPascal() const { return Pascal; }
239 StringRef getUDSuffix() const { return UDSuffixBuf; }
241 /// Get the index of a token containing a ud-suffix.
242 unsigned getUDSuffixToken() const {
243 assert(!UDSuffixBuf.empty() && "no ud-suffix");
244 return UDSuffixToken;
246 /// Get the spelling offset of the first byte of the ud-suffix.
247 unsigned getUDSuffixOffset() const {
248 assert(!UDSuffixBuf.empty() && "no ud-suffix");
249 return UDSuffixOffset;
253 void init(ArrayRef<Token> StringToks);
254 bool CopyStringFragment(const Token &Tok, const char *TokBegin,
256 void DiagnoseLexingError(SourceLocation Loc);
259 } // end namespace clang