clang API Documentation
00001 //===--- LiteralSupport.h ---------------------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the NumericLiteralParser, CharLiteralParser, and 00011 // StringLiteralParser interfaces. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef CLANG_LITERALSUPPORT_H 00016 #define CLANG_LITERALSUPPORT_H 00017 00018 #include "clang/Basic/LLVM.h" 00019 #include "llvm/ADT/APFloat.h" 00020 #include "llvm/ADT/SmallString.h" 00021 #include "llvm/Support/DataTypes.h" 00022 #include "clang/Basic/TokenKinds.h" 00023 #include <cctype> 00024 00025 namespace clang { 00026 00027 class DiagnosticsEngine; 00028 class Preprocessor; 00029 class Token; 00030 class SourceLocation; 00031 class TargetInfo; 00032 class SourceManager; 00033 class LangOptions; 00034 00035 /// NumericLiteralParser - This performs strict semantic analysis of the content 00036 /// of a ppnumber, classifying it as either integer, floating, or erroneous, 00037 /// determines the radix of the value and can convert it to a useful value. 00038 class NumericLiteralParser { 00039 Preprocessor &PP; // needed for diagnostics 00040 00041 const char *const ThisTokBegin; 00042 const char *const ThisTokEnd; 00043 const char *DigitsBegin, *SuffixBegin; // markers 00044 const char *s; // cursor 00045 00046 unsigned radix; 00047 00048 bool saw_exponent, saw_period, saw_ud_suffix; 00049 00050 public: 00051 NumericLiteralParser(const char *begin, const char *end, 00052 SourceLocation Loc, Preprocessor &PP); 00053 bool hadError; 00054 bool isUnsigned; 00055 bool isLong; // This is *not* set for long long. 00056 bool isLongLong; 00057 bool isFloat; // 1.0f 00058 bool isImaginary; // 1.0i 00059 bool isMicrosoftInteger; // Microsoft suffix extension i8, i16, i32, or i64. 00060 00061 bool isIntegerLiteral() const { 00062 return !saw_period && !saw_exponent; 00063 } 00064 bool isFloatingLiteral() const { 00065 return saw_period || saw_exponent; 00066 } 00067 00068 bool hasUDSuffix() const { 00069 return saw_ud_suffix; 00070 } 00071 StringRef getUDSuffix() const { 00072 assert(saw_ud_suffix); 00073 return StringRef(SuffixBegin, ThisTokEnd - SuffixBegin); 00074 } 00075 unsigned getUDSuffixOffset() const { 00076 assert(saw_ud_suffix); 00077 return SuffixBegin - ThisTokBegin; 00078 } 00079 00080 unsigned getRadix() const { return radix; } 00081 00082 /// GetIntegerValue - Convert this numeric literal value to an APInt that 00083 /// matches Val's input width. If there is an overflow (i.e., if the unsigned 00084 /// value read is larger than the APInt's bits will hold), set Val to the low 00085 /// bits of the result and return true. Otherwise, return false. 00086 bool GetIntegerValue(llvm::APInt &Val); 00087 00088 /// GetFloatValue - Convert this numeric literal to a floating value, using 00089 /// the specified APFloat fltSemantics (specifying float, double, etc). 00090 /// The optional bool isExact (passed-by-reference) has its value 00091 /// set to true if the returned APFloat can represent the number in the 00092 /// literal exactly, and false otherwise. 00093 llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result); 00094 00095 private: 00096 00097 void ParseNumberStartingWithZero(SourceLocation TokLoc); 00098 00099 /// SkipHexDigits - Read and skip over any hex digits, up to End. 00100 /// Return a pointer to the first non-hex digit or End. 00101 const char *SkipHexDigits(const char *ptr) { 00102 while (ptr != ThisTokEnd && isxdigit(*ptr)) 00103 ptr++; 00104 return ptr; 00105 } 00106 00107 /// SkipOctalDigits - Read and skip over any octal digits, up to End. 00108 /// Return a pointer to the first non-hex digit or End. 00109 const char *SkipOctalDigits(const char *ptr) { 00110 while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7'))) 00111 ptr++; 00112 return ptr; 00113 } 00114 00115 /// SkipDigits - Read and skip over any digits, up to End. 00116 /// Return a pointer to the first non-hex digit or End. 00117 const char *SkipDigits(const char *ptr) { 00118 while (ptr != ThisTokEnd && isdigit(*ptr)) 00119 ptr++; 00120 return ptr; 00121 } 00122 00123 /// SkipBinaryDigits - Read and skip over any binary digits, up to End. 00124 /// Return a pointer to the first non-binary digit or End. 00125 const char *SkipBinaryDigits(const char *ptr) { 00126 while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1')) 00127 ptr++; 00128 return ptr; 00129 } 00130 00131 }; 00132 00133 /// CharLiteralParser - Perform interpretation and semantic analysis of a 00134 /// character literal. 00135 class CharLiteralParser { 00136 uint64_t Value; 00137 tok::TokenKind Kind; 00138 bool IsMultiChar; 00139 bool HadError; 00140 SmallString<32> UDSuffixBuf; 00141 unsigned UDSuffixOffset; 00142 public: 00143 CharLiteralParser(const char *begin, const char *end, 00144 SourceLocation Loc, Preprocessor &PP, 00145 tok::TokenKind kind); 00146 00147 bool hadError() const { return HadError; } 00148 bool isAscii() const { return Kind == tok::char_constant; } 00149 bool isWide() const { return Kind == tok::wide_char_constant; } 00150 bool isUTF16() const { return Kind == tok::utf16_char_constant; } 00151 bool isUTF32() const { return Kind == tok::utf32_char_constant; } 00152 bool isMultiChar() const { return IsMultiChar; } 00153 uint64_t getValue() const { return Value; } 00154 StringRef getUDSuffix() const { return UDSuffixBuf; } 00155 unsigned getUDSuffixOffset() const { 00156 assert(!UDSuffixBuf.empty() && "no ud-suffix"); 00157 return UDSuffixOffset; 00158 } 00159 }; 00160 00161 /// StringLiteralParser - This decodes string escape characters and performs 00162 /// wide string analysis and Translation Phase #6 (concatenation of string 00163 /// literals) (C99 5.1.1.2p1). 00164 class StringLiteralParser { 00165 const SourceManager &SM; 00166 const LangOptions &Features; 00167 const TargetInfo &Target; 00168 DiagnosticsEngine *Diags; 00169 00170 unsigned MaxTokenLength; 00171 unsigned SizeBound; 00172 unsigned CharByteWidth; 00173 tok::TokenKind Kind; 00174 SmallString<512> ResultBuf; 00175 char *ResultPtr; // cursor 00176 SmallString<32> UDSuffixBuf; 00177 unsigned UDSuffixToken; 00178 unsigned UDSuffixOffset; 00179 public: 00180 StringLiteralParser(const Token *StringToks, unsigned NumStringToks, 00181 Preprocessor &PP, bool Complain = true); 00182 StringLiteralParser(const Token *StringToks, unsigned NumStringToks, 00183 const SourceManager &sm, const LangOptions &features, 00184 const TargetInfo &target, DiagnosticsEngine *diags = 0) 00185 : SM(sm), Features(features), Target(target), Diags(diags), 00186 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown), 00187 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) { 00188 init(StringToks, NumStringToks); 00189 } 00190 00191 00192 bool hadError; 00193 bool Pascal; 00194 00195 StringRef GetString() const { 00196 return StringRef(ResultBuf.data(), GetStringLength()); 00197 } 00198 unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); } 00199 00200 unsigned GetNumStringChars() const { 00201 return GetStringLength() / CharByteWidth; 00202 } 00203 /// getOffsetOfStringByte - This function returns the offset of the 00204 /// specified byte of the string data represented by Token. This handles 00205 /// advancing over escape sequences in the string. 00206 /// 00207 /// If the Diagnostics pointer is non-null, then this will do semantic 00208 /// checking of the string literal and emit errors and warnings. 00209 unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const; 00210 00211 bool isAscii() const { return Kind == tok::string_literal; } 00212 bool isWide() const { return Kind == tok::wide_string_literal; } 00213 bool isUTF8() const { return Kind == tok::utf8_string_literal; } 00214 bool isUTF16() const { return Kind == tok::utf16_string_literal; } 00215 bool isUTF32() const { return Kind == tok::utf32_string_literal; } 00216 bool isPascal() const { return Pascal; } 00217 00218 StringRef getUDSuffix() const { return UDSuffixBuf; } 00219 00220 /// Get the index of a token containing a ud-suffix. 00221 unsigned getUDSuffixToken() const { 00222 assert(!UDSuffixBuf.empty() && "no ud-suffix"); 00223 return UDSuffixToken; 00224 } 00225 /// Get the spelling offset of the first byte of the ud-suffix. 00226 unsigned getUDSuffixOffset() const { 00227 assert(!UDSuffixBuf.empty() && "no ud-suffix"); 00228 return UDSuffixOffset; 00229 } 00230 00231 private: 00232 void init(const Token *StringToks, unsigned NumStringToks); 00233 bool CopyStringFragment(StringRef Fragment); 00234 bool DiagnoseBadString(const Token& Tok); 00235 void DiagnoseLexingError(SourceLocation Loc); 00236 }; 00237 00238 } // end namespace clang 00239 00240 #endif