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 CLANG_LITERALSUPPORT_H
16 #define CLANG_LITERALSUPPORT_H
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "clang/Basic/TokenKinds.h"
27 class DiagnosticsEngine;
35 /// NumericLiteralParser - This performs strict semantic analysis of the content
36 /// of a ppnumber, classifying it as either integer, floating, or erroneous,
37 /// determines the radix of the value and can convert it to a useful value.
38 class NumericLiteralParser {
39 Preprocessor &PP; // needed for diagnostics
41 const char *const ThisTokBegin;
42 const char *const ThisTokEnd;
43 const char *DigitsBegin, *SuffixBegin; // markers
44 const char *s; // cursor
48 bool saw_exponent, saw_period, saw_ud_suffix;
51 NumericLiteralParser(const char *begin, const char *end,
52 SourceLocation Loc, Preprocessor &PP);
55 bool isLong; // This is *not* set for long long.
58 bool isImaginary; // 1.0i
59 bool isMicrosoftInteger; // Microsoft suffix extension i8, i16, i32, or i64.
61 bool isIntegerLiteral() const {
62 return !saw_period && !saw_exponent;
64 bool isFloatingLiteral() const {
65 return saw_period || saw_exponent;
68 bool hasUDSuffix() const {
71 StringRef getUDSuffix() const {
72 assert(saw_ud_suffix);
73 return StringRef(SuffixBegin, ThisTokEnd - SuffixBegin);
75 unsigned getUDSuffixOffset() const {
76 assert(saw_ud_suffix);
77 return SuffixBegin - ThisTokBegin;
80 unsigned getRadix() const { return radix; }
82 /// GetIntegerValue - Convert this numeric literal value to an APInt that
83 /// matches Val's input width. If there is an overflow (i.e., if the unsigned
84 /// value read is larger than the APInt's bits will hold), set Val to the low
85 /// bits of the result and return true. Otherwise, return false.
86 bool GetIntegerValue(llvm::APInt &Val);
88 /// GetFloatValue - Convert this numeric literal to a floating value, using
89 /// the specified APFloat fltSemantics (specifying float, double, etc).
90 /// The optional bool isExact (passed-by-reference) has its value
91 /// set to true if the returned APFloat can represent the number in the
92 /// literal exactly, and false otherwise.
93 llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
97 void ParseNumberStartingWithZero(SourceLocation TokLoc);
99 /// SkipHexDigits - Read and skip over any hex digits, up to End.
100 /// Return a pointer to the first non-hex digit or End.
101 const char *SkipHexDigits(const char *ptr) {
102 while (ptr != ThisTokEnd && isxdigit(*ptr))
107 /// SkipOctalDigits - Read and skip over any octal digits, up to End.
108 /// Return a pointer to the first non-hex digit or End.
109 const char *SkipOctalDigits(const char *ptr) {
110 while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7')))
115 /// SkipDigits - Read and skip over any digits, up to End.
116 /// Return a pointer to the first non-hex digit or End.
117 const char *SkipDigits(const char *ptr) {
118 while (ptr != ThisTokEnd && isdigit(*ptr))
123 /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
124 /// Return a pointer to the first non-binary digit or End.
125 const char *SkipBinaryDigits(const char *ptr) {
126 while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
133 /// CharLiteralParser - Perform interpretation and semantic analysis of a
134 /// character literal.
135 class CharLiteralParser {
140 SmallString<32> UDSuffixBuf;
141 unsigned UDSuffixOffset;
143 CharLiteralParser(const char *begin, const char *end,
144 SourceLocation Loc, Preprocessor &PP,
145 tok::TokenKind kind);
147 bool hadError() const { return HadError; }
148 bool isAscii() const { return Kind == tok::char_constant; }
149 bool isWide() const { return Kind == tok::wide_char_constant; }
150 bool isUTF16() const { return Kind == tok::utf16_char_constant; }
151 bool isUTF32() const { return Kind == tok::utf32_char_constant; }
152 bool isMultiChar() const { return IsMultiChar; }
153 uint64_t getValue() const { return Value; }
154 StringRef getUDSuffix() const { return UDSuffixBuf; }
155 unsigned getUDSuffixOffset() const {
156 assert(!UDSuffixBuf.empty() && "no ud-suffix");
157 return UDSuffixOffset;
161 /// StringLiteralParser - This decodes string escape characters and performs
162 /// wide string analysis and Translation Phase #6 (concatenation of string
163 /// literals) (C99 5.1.1.2p1).
164 class StringLiteralParser {
165 const SourceManager &SM;
166 const LangOptions &Features;
167 const TargetInfo &Target;
168 DiagnosticsEngine *Diags;
170 unsigned MaxTokenLength;
172 unsigned CharByteWidth;
174 SmallString<512> ResultBuf;
175 char *ResultPtr; // cursor
176 SmallString<32> UDSuffixBuf;
177 unsigned UDSuffixToken;
178 unsigned UDSuffixOffset;
180 StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
181 Preprocessor &PP, bool Complain = true);
182 StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
183 const SourceManager &sm, const LangOptions &features,
184 const TargetInfo &target, DiagnosticsEngine *diags = 0)
185 : SM(sm), Features(features), Target(target), Diags(diags),
186 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
187 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
188 init(StringToks, NumStringToks);
195 StringRef GetString() const {
196 return StringRef(ResultBuf.data(), GetStringLength());
198 unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
200 unsigned GetNumStringChars() const {
201 return GetStringLength() / CharByteWidth;
203 /// getOffsetOfStringByte - This function returns the offset of the
204 /// specified byte of the string data represented by Token. This handles
205 /// advancing over escape sequences in the string.
207 /// If the Diagnostics pointer is non-null, then this will do semantic
208 /// checking of the string literal and emit errors and warnings.
209 unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
211 bool isAscii() const { return Kind == tok::string_literal; }
212 bool isWide() const { return Kind == tok::wide_string_literal; }
213 bool isUTF8() const { return Kind == tok::utf8_string_literal; }
214 bool isUTF16() const { return Kind == tok::utf16_string_literal; }
215 bool isUTF32() const { return Kind == tok::utf32_string_literal; }
216 bool isPascal() const { return Pascal; }
218 StringRef getUDSuffix() const { return UDSuffixBuf; }
220 /// Get the index of a token containing a ud-suffix.
221 unsigned getUDSuffixToken() const {
222 assert(!UDSuffixBuf.empty() && "no ud-suffix");
223 return UDSuffixToken;
225 /// Get the spelling offset of the first byte of the ud-suffix.
226 unsigned getUDSuffixOffset() const {
227 assert(!UDSuffixBuf.empty() && "no ud-suffix");
228 return UDSuffixOffset;
232 void init(const Token *StringToks, unsigned NumStringToks);
233 bool CopyStringFragment(StringRef Fragment);
234 bool DiagnoseBadString(const Token& Tok);
235 void DiagnoseLexingError(SourceLocation Loc);
238 } // end namespace clang