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
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/System/DataTypes.h"
31 /// NumericLiteralParser - This performs strict semantic analysis of the content
32 /// of a ppnumber, classifying it as either integer, floating, or erroneous,
33 /// determines the radix of the value and can convert it to a useful value.
34 class NumericLiteralParser {
35 Preprocessor &PP; // needed for diagnostics
37 const char *const ThisTokBegin;
38 const char *const ThisTokEnd;
39 const char *DigitsBegin, *SuffixBegin; // markers
40 const char *s; // cursor
44 bool saw_exponent, saw_period;
47 NumericLiteralParser(const char *begin, const char *end,
48 SourceLocation Loc, Preprocessor &PP);
51 bool isLong; // This is *not* set for long long.
54 bool isImaginary; // 1.0i
55 bool isMicrosoftInteger; // Microsoft suffix extension i8, i16, i32, or i64.
57 bool isIntegerLiteral() const {
58 return !saw_period && !saw_exponent;
60 bool isFloatingLiteral() const {
61 return saw_period || saw_exponent;
63 bool hasSuffix() const {
64 return SuffixBegin != ThisTokEnd;
67 unsigned getRadix() const { return radix; }
69 /// GetIntegerValue - Convert this numeric literal value to an APInt that
70 /// matches Val's input width. If there is an overflow (i.e., if the unsigned
71 /// value read is larger than the APInt's bits will hold), set Val to the low
72 /// bits of the result and return true. Otherwise, return false.
73 bool GetIntegerValue(llvm::APInt &Val);
75 /// GetFloatValue - Convert this numeric literal to a floating value, using
76 /// the specified APFloat fltSemantics (specifying float, double, etc).
77 /// The optional bool isExact (passed-by-reference) has its value
78 /// set to true if the returned APFloat can represent the number in the
79 /// literal exactly, and false otherwise.
80 llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
84 void ParseNumberStartingWithZero(SourceLocation TokLoc);
86 /// SkipHexDigits - Read and skip over any hex digits, up to End.
87 /// Return a pointer to the first non-hex digit or End.
88 const char *SkipHexDigits(const char *ptr) {
89 while (ptr != ThisTokEnd && isxdigit(*ptr))
94 /// SkipOctalDigits - Read and skip over any octal digits, up to End.
95 /// Return a pointer to the first non-hex digit or End.
96 const char *SkipOctalDigits(const char *ptr) {
97 while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7')))
102 /// SkipDigits - Read and skip over any digits, up to End.
103 /// Return a pointer to the first non-hex digit or End.
104 const char *SkipDigits(const char *ptr) {
105 while (ptr != ThisTokEnd && isdigit(*ptr))
110 /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
111 /// Return a pointer to the first non-binary digit or End.
112 const char *SkipBinaryDigits(const char *ptr) {
113 while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
120 /// CharLiteralParser - Perform interpretation and semantic analysis of a
121 /// character literal.
122 class CharLiteralParser {
128 CharLiteralParser(const char *begin, const char *end,
129 SourceLocation Loc, Preprocessor &PP);
131 bool hadError() const { return HadError; }
132 bool isWide() const { return IsWide; }
133 bool isMultiChar() const { return IsMultiChar; }
134 uint64_t getValue() const { return Value; }
137 /// StringLiteralParser - This decodes string escape characters and performs
138 /// wide string analysis and Translation Phase #6 (concatenation of string
139 /// literals) (C99 5.1.1.2p1).
140 class StringLiteralParser {
143 unsigned MaxTokenLength;
145 unsigned wchar_tByteWidth;
146 llvm::SmallString<512> ResultBuf;
147 char *ResultPtr; // cursor
149 StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
150 Preprocessor &PP, bool Complain = true);
155 const char *GetString() { return &ResultBuf[0]; }
156 unsigned GetStringLength() const { return ResultPtr-&ResultBuf[0]; }
158 unsigned GetNumStringChars() const {
160 return GetStringLength() / wchar_tByteWidth;
161 return GetStringLength();
163 /// getOffsetOfStringByte - This function returns the offset of the
164 /// specified byte of the string data represented by Token. This handles
165 /// advancing over escape sequences in the string.
166 static unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo,
167 Preprocessor &PP, bool Complain = true);
170 } // end namespace clang