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/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 /// NumericLiteralParser - This performs strict semantic analysis of the content
37 /// of a ppnumber, classifying it as either integer, floating, or erroneous,
38 /// determines the radix of the value and can convert it to a useful value.
39 class NumericLiteralParser {
40 Preprocessor &PP; // needed for diagnostics
42 const char *const ThisTokBegin;
43 const char *const ThisTokEnd;
44 const char *DigitsBegin, *SuffixBegin; // markers
45 const char *s; // cursor
49 bool saw_exponent, saw_period, saw_ud_suffix;
52 NumericLiteralParser(StringRef TokSpelling,
53 SourceLocation TokLoc,
57 bool isLong; // This is *not* set for long long.
60 bool isImaginary; // 1.0i
61 bool isMicrosoftInteger; // Microsoft suffix extension i8, i16, i32, or i64.
63 bool isIntegerLiteral() const {
64 return !saw_period && !saw_exponent;
66 bool isFloatingLiteral() const {
67 return saw_period || saw_exponent;
70 bool hasUDSuffix() const {
73 StringRef getUDSuffix() const {
74 assert(saw_ud_suffix);
75 return StringRef(SuffixBegin, ThisTokEnd - SuffixBegin);
77 unsigned getUDSuffixOffset() const {
78 assert(saw_ud_suffix);
79 return SuffixBegin - ThisTokBegin;
82 unsigned getRadix() const { return radix; }
84 /// GetIntegerValue - Convert this numeric literal value to an APInt that
85 /// matches Val's input width. If there is an overflow (i.e., if the unsigned
86 /// value read is larger than the APInt's bits will hold), set Val to the low
87 /// bits of the result and return true. Otherwise, return false.
88 bool GetIntegerValue(llvm::APInt &Val);
90 /// GetFloatValue - Convert this numeric literal to a floating value, using
91 /// the specified APFloat fltSemantics (specifying float, double, etc).
92 /// The optional bool isExact (passed-by-reference) has its value
93 /// set to true if the returned APFloat can represent the number in the
94 /// literal exactly, and false otherwise.
95 llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
99 void ParseNumberStartingWithZero(SourceLocation TokLoc);
101 /// SkipHexDigits - Read and skip over any hex digits, up to End.
102 /// Return a pointer to the first non-hex digit or End.
103 const char *SkipHexDigits(const char *ptr) {
104 while (ptr != ThisTokEnd && isHexDigit(*ptr))
109 /// SkipOctalDigits - Read and skip over any octal digits, up to End.
110 /// Return a pointer to the first non-hex digit or End.
111 const char *SkipOctalDigits(const char *ptr) {
112 while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7')))
117 /// SkipDigits - Read and skip over any digits, up to End.
118 /// Return a pointer to the first non-hex digit or End.
119 const char *SkipDigits(const char *ptr) {
120 while (ptr != ThisTokEnd && isDigit(*ptr))
125 /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
126 /// Return a pointer to the first non-binary digit or End.
127 const char *SkipBinaryDigits(const char *ptr) {
128 while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
135 /// CharLiteralParser - Perform interpretation and semantic analysis of a
136 /// character literal.
137 class CharLiteralParser {
142 SmallString<32> UDSuffixBuf;
143 unsigned UDSuffixOffset;
145 CharLiteralParser(const char *begin, const char *end,
146 SourceLocation Loc, Preprocessor &PP,
147 tok::TokenKind kind);
149 bool hadError() const { return HadError; }
150 bool isAscii() const { return Kind == tok::char_constant; }
151 bool isWide() const { return Kind == tok::wide_char_constant; }
152 bool isUTF16() const { return Kind == tok::utf16_char_constant; }
153 bool isUTF32() const { return Kind == tok::utf32_char_constant; }
154 bool isMultiChar() const { return IsMultiChar; }
155 uint64_t getValue() const { return Value; }
156 StringRef getUDSuffix() const { return UDSuffixBuf; }
157 unsigned getUDSuffixOffset() const {
158 assert(!UDSuffixBuf.empty() && "no ud-suffix");
159 return UDSuffixOffset;
163 /// StringLiteralParser - This decodes string escape characters and performs
164 /// wide string analysis and Translation Phase #6 (concatenation of string
165 /// literals) (C99 5.1.1.2p1).
166 class StringLiteralParser {
167 const SourceManager &SM;
168 const LangOptions &Features;
169 const TargetInfo &Target;
170 DiagnosticsEngine *Diags;
172 unsigned MaxTokenLength;
174 unsigned CharByteWidth;
176 SmallString<512> ResultBuf;
177 char *ResultPtr; // cursor
178 SmallString<32> UDSuffixBuf;
179 unsigned UDSuffixToken;
180 unsigned UDSuffixOffset;
182 StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
183 Preprocessor &PP, bool Complain = true);
184 StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
185 const SourceManager &sm, const LangOptions &features,
186 const TargetInfo &target, DiagnosticsEngine *diags = 0)
187 : SM(sm), Features(features), Target(target), Diags(diags),
188 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
189 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
190 init(StringToks, NumStringToks);
197 StringRef GetString() const {
198 return StringRef(ResultBuf.data(), GetStringLength());
200 unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
202 unsigned GetNumStringChars() const {
203 return GetStringLength() / CharByteWidth;
205 /// getOffsetOfStringByte - This function returns the offset of the
206 /// specified byte of the string data represented by Token. This handles
207 /// advancing over escape sequences in the string.
209 /// If the Diagnostics pointer is non-null, then this will do semantic
210 /// checking of the string literal and emit errors and warnings.
211 unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
213 bool isAscii() const { return Kind == tok::string_literal; }
214 bool isWide() const { return Kind == tok::wide_string_literal; }
215 bool isUTF8() const { return Kind == tok::utf8_string_literal; }
216 bool isUTF16() const { return Kind == tok::utf16_string_literal; }
217 bool isUTF32() const { return Kind == tok::utf32_string_literal; }
218 bool isPascal() const { return Pascal; }
220 StringRef getUDSuffix() const { return UDSuffixBuf; }
222 /// Get the index of a token containing a ud-suffix.
223 unsigned getUDSuffixToken() const {
224 assert(!UDSuffixBuf.empty() && "no ud-suffix");
225 return UDSuffixToken;
227 /// Get the spelling offset of the first byte of the ud-suffix.
228 unsigned getUDSuffixOffset() const {
229 assert(!UDSuffixBuf.empty() && "no ud-suffix");
230 return UDSuffixOffset;
234 void init(const Token *StringToks, unsigned NumStringToks);
235 bool CopyStringFragment(const Token &Tok, const char *TokBegin,
237 void DiagnoseLexingError(SourceLocation Loc);
240 } // end namespace clang