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;
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;
67 bool hasSuffix() const {
68 return SuffixBegin != ThisTokEnd;
71 unsigned getRadix() const { return radix; }
73 /// GetIntegerValue - Convert this numeric literal value to an APInt that
74 /// matches Val's input width. If there is an overflow (i.e., if the unsigned
75 /// value read is larger than the APInt's bits will hold), set Val to the low
76 /// bits of the result and return true. Otherwise, return false.
77 bool GetIntegerValue(llvm::APInt &Val);
79 /// GetFloatValue - Convert this numeric literal to a floating value, using
80 /// the specified APFloat fltSemantics (specifying float, double, etc).
81 /// The optional bool isExact (passed-by-reference) has its value
82 /// set to true if the returned APFloat can represent the number in the
83 /// literal exactly, and false otherwise.
84 llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
88 void ParseNumberStartingWithZero(SourceLocation TokLoc);
90 /// SkipHexDigits - Read and skip over any hex digits, up to End.
91 /// Return a pointer to the first non-hex digit or End.
92 const char *SkipHexDigits(const char *ptr) {
93 while (ptr != ThisTokEnd && isxdigit(*ptr))
98 /// SkipOctalDigits - Read and skip over any octal digits, up to End.
99 /// Return a pointer to the first non-hex digit or End.
100 const char *SkipOctalDigits(const char *ptr) {
101 while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7')))
106 /// SkipDigits - Read and skip over any digits, up to End.
107 /// Return a pointer to the first non-hex digit or End.
108 const char *SkipDigits(const char *ptr) {
109 while (ptr != ThisTokEnd && isdigit(*ptr))
114 /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
115 /// Return a pointer to the first non-binary digit or End.
116 const char *SkipBinaryDigits(const char *ptr) {
117 while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
124 /// CharLiteralParser - Perform interpretation and semantic analysis of a
125 /// character literal.
126 class CharLiteralParser {
132 CharLiteralParser(const char *begin, const char *end,
133 SourceLocation Loc, Preprocessor &PP,
134 tok::TokenKind kind);
136 bool hadError() const { return HadError; }
137 bool isAscii() const { return Kind == tok::char_constant; }
138 bool isWide() const { return Kind == tok::wide_char_constant; }
139 bool isUTF16() const { return Kind == tok::utf16_char_constant; }
140 bool isUTF32() const { return Kind == tok::utf32_char_constant; }
141 bool isMultiChar() const { return IsMultiChar; }
142 uint64_t getValue() const { return Value; }
145 /// StringLiteralParser - This decodes string escape characters and performs
146 /// wide string analysis and Translation Phase #6 (concatenation of string
147 /// literals) (C99 5.1.1.2p1).
148 class StringLiteralParser {
149 const SourceManager &SM;
150 const LangOptions &Features;
151 const TargetInfo &Target;
152 DiagnosticsEngine *Diags;
154 unsigned MaxTokenLength;
156 unsigned CharByteWidth;
158 llvm::SmallString<512> ResultBuf;
159 char *ResultPtr; // cursor
161 StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
162 Preprocessor &PP, bool Complain = true);
163 StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
164 const SourceManager &sm, const LangOptions &features,
165 const TargetInfo &target, DiagnosticsEngine *diags = 0)
166 : SM(sm), Features(features), Target(target), Diags(diags),
167 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
168 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
169 init(StringToks, NumStringToks);
176 StringRef GetString() const {
177 return StringRef(ResultBuf.data(), GetStringLength());
179 unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
181 unsigned GetNumStringChars() const {
182 return GetStringLength() / CharByteWidth;
184 /// getOffsetOfStringByte - This function returns the offset of the
185 /// specified byte of the string data represented by Token. This handles
186 /// advancing over escape sequences in the string.
188 /// If the Diagnostics pointer is non-null, then this will do semantic
189 /// checking of the string literal and emit errors and warnings.
190 unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
192 bool isAscii() { return Kind == tok::string_literal; }
193 bool isWide() { return Kind == tok::wide_string_literal; }
194 bool isUTF8() { return Kind == tok::utf8_string_literal; }
195 bool isUTF16() { return Kind == tok::utf16_string_literal; }
196 bool isUTF32() { return Kind == tok::utf32_string_literal; }
199 void init(const Token *StringToks, unsigned NumStringToks);
200 void CopyStringFragment(StringRef Fragment);
203 } // end namespace clang