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; // This is *not* set for long long.
65 bool isImaginary; // 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 isUTF16() const { return Kind == tok::utf16_char_constant; }
170 bool isUTF32() const { return Kind == tok::utf32_char_constant; }
171 bool isMultiChar() const { return IsMultiChar; }
172 uint64_t getValue() const { return Value; }
173 StringRef getUDSuffix() const { return UDSuffixBuf; }
174 unsigned getUDSuffixOffset() const {
175 assert(!UDSuffixBuf.empty() && "no ud-suffix");
176 return UDSuffixOffset;
180 /// StringLiteralParser - This decodes string escape characters and performs
181 /// wide string analysis and Translation Phase #6 (concatenation of string
182 /// literals) (C99 5.1.1.2p1).
183 class StringLiteralParser {
184 const SourceManager &SM;
185 const LangOptions &Features;
186 const TargetInfo &Target;
187 DiagnosticsEngine *Diags;
189 unsigned MaxTokenLength;
191 unsigned CharByteWidth;
193 SmallString<512> ResultBuf;
194 char *ResultPtr; // cursor
195 SmallString<32> UDSuffixBuf;
196 unsigned UDSuffixToken;
197 unsigned UDSuffixOffset;
199 StringLiteralParser(ArrayRef<Token> StringToks,
200 Preprocessor &PP, bool Complain = true);
201 StringLiteralParser(ArrayRef<Token> StringToks,
202 const SourceManager &sm, const LangOptions &features,
203 const TargetInfo &target,
204 DiagnosticsEngine *diags = nullptr)
205 : SM(sm), Features(features), Target(target), Diags(diags),
206 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
207 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
215 StringRef GetString() const {
216 return StringRef(ResultBuf.data(), GetStringLength());
218 unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
220 unsigned GetNumStringChars() const {
221 return GetStringLength() / CharByteWidth;
223 /// getOffsetOfStringByte - This function returns the offset of the
224 /// specified byte of the string data represented by Token. This handles
225 /// advancing over escape sequences in the string.
227 /// If the Diagnostics pointer is non-null, then this will do semantic
228 /// checking of the string literal and emit errors and warnings.
229 unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
231 bool isAscii() const { return Kind == tok::string_literal; }
232 bool isWide() const { return Kind == tok::wide_string_literal; }
233 bool isUTF8() const { return Kind == tok::utf8_string_literal; }
234 bool isUTF16() const { return Kind == tok::utf16_string_literal; }
235 bool isUTF32() const { return Kind == tok::utf32_string_literal; }
236 bool isPascal() const { return Pascal; }
238 StringRef getUDSuffix() const { return UDSuffixBuf; }
240 /// Get the index of a token containing a ud-suffix.
241 unsigned getUDSuffixToken() const {
242 assert(!UDSuffixBuf.empty() && "no ud-suffix");
243 return UDSuffixToken;
245 /// Get the spelling offset of the first byte of the ud-suffix.
246 unsigned getUDSuffixOffset() const {
247 assert(!UDSuffixBuf.empty() && "no ud-suffix");
248 return UDSuffixOffset;
252 void init(ArrayRef<Token> StringToks);
253 bool CopyStringFragment(const Token &Tok, const char *TokBegin,
255 void DiagnoseLexingError(SourceLocation Loc);
258 } // end namespace clang