| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #ifndef LLVM_CLANG_LEX_LITERALSUPPORT_H |
| 15 | #define LLVM_CLANG_LEX_LITERALSUPPORT_H |
| 16 | |
| 17 | #include "clang/Basic/CharInfo.h" |
| 18 | #include "clang/Basic/LLVM.h" |
| 19 | #include "clang/Basic/TokenKinds.h" |
| 20 | #include "llvm/ADT/APFloat.h" |
| 21 | #include "llvm/ADT/ArrayRef.h" |
| 22 | #include "llvm/ADT/SmallString.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/Support/DataTypes.h" |
| 25 | |
| 26 | namespace clang { |
| 27 | |
| 28 | class DiagnosticsEngine; |
| 29 | class Preprocessor; |
| 30 | class Token; |
| 31 | class SourceLocation; |
| 32 | class TargetInfo; |
| 33 | class SourceManager; |
| 34 | class LangOptions; |
| 35 | |
| 36 | |
| 37 | void expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input); |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | class NumericLiteralParser { |
| 43 | Preprocessor &PP; |
| 44 | |
| 45 | const char *const ThisTokBegin; |
| 46 | const char *const ThisTokEnd; |
| 47 | const char *DigitsBegin, *SuffixBegin; |
| 48 | const char *s; |
| 49 | |
| 50 | unsigned radix; |
| 51 | |
| 52 | bool saw_exponent, saw_period, saw_ud_suffix, saw_fixed_point_suffix; |
| 53 | |
| 54 | SmallString<32> UDSuffixBuf; |
| 55 | |
| 56 | public: |
| 57 | NumericLiteralParser(StringRef TokSpelling, |
| 58 | SourceLocation TokLoc, |
| 59 | Preprocessor &PP); |
| 60 | bool hadError : 1; |
| 61 | bool isUnsigned : 1; |
| 62 | bool isLong : 1; |
| 63 | bool isLongLong : 1; |
| 64 | bool isHalf : 1; |
| 65 | bool isFloat : 1; |
| 66 | bool isImaginary : 1; |
| 67 | bool isFloat16 : 1; |
| 68 | bool isFloat128 : 1; |
| 69 | uint8_t MicrosoftInteger; |
| 70 | |
| 71 | bool isFract : 1; |
| 72 | bool isAccum : 1; |
| 73 | |
| 74 | bool isFixedPointLiteral() const { return saw_fixed_point_suffix; } |
| 75 | |
| 76 | bool isIntegerLiteral() const { |
| 77 | return !saw_period && !saw_exponent && !isFixedPointLiteral(); |
| 78 | } |
| 79 | bool isFloatingLiteral() const { |
| 80 | return (saw_period || saw_exponent) && !isFixedPointLiteral(); |
| 81 | } |
| 82 | |
| 83 | bool hasUDSuffix() const { |
| 84 | return saw_ud_suffix; |
| 85 | } |
| 86 | StringRef getUDSuffix() const { |
| 87 | assert(saw_ud_suffix); |
| 88 | return UDSuffixBuf; |
| 89 | } |
| 90 | unsigned getUDSuffixOffset() const { |
| 91 | assert(saw_ud_suffix); |
| 92 | return SuffixBegin - ThisTokBegin; |
| 93 | } |
| 94 | |
| 95 | static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix); |
| 96 | |
| 97 | unsigned getRadix() const { return radix; } |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | bool GetIntegerValue(llvm::APInt &Val); |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result); |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | bool GetFixedPointValue(llvm::APInt &StoreVal, unsigned Scale); |
| 117 | |
| 118 | private: |
| 119 | |
| 120 | void ParseNumberStartingWithZero(SourceLocation TokLoc); |
| 121 | void ParseDecimalOrOctalCommon(SourceLocation TokLoc); |
| 122 | |
| 123 | static bool isDigitSeparator(char C) { return C == '\''; } |
| 124 | |
| 125 | |
| 126 | |
| 127 | bool containsDigits(const char *Start, const char *End) { |
| 128 | return Start != End && (Start + 1 != End || !isDigitSeparator(Start[0])); |
| 129 | } |
| 130 | |
| 131 | enum CheckSeparatorKind { CSK_BeforeDigits, CSK_AfterDigits }; |
| 132 | |
| 133 | |
| 134 | void checkSeparator(SourceLocation TokLoc, const char *Pos, |
| 135 | CheckSeparatorKind IsAfterDigits); |
| 136 | |
| 137 | |
| 138 | |
| 139 | const char *SkipHexDigits(const char *ptr) { |
| 140 | while (ptr != ThisTokEnd && (isHexDigit(*ptr) || isDigitSeparator(*ptr))) |
| 141 | ptr++; |
| 142 | return ptr; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | const char *SkipOctalDigits(const char *ptr) { |
| 148 | while (ptr != ThisTokEnd && |
| 149 | ((*ptr >= '0' && *ptr <= '7') || isDigitSeparator(*ptr))) |
| 150 | ptr++; |
| 151 | return ptr; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | const char *SkipDigits(const char *ptr) { |
| 157 | while (ptr != ThisTokEnd && (isDigit(*ptr) || isDigitSeparator(*ptr))) |
| 158 | ptr++; |
| 159 | return ptr; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | const char *SkipBinaryDigits(const char *ptr) { |
| 165 | while (ptr != ThisTokEnd && |
| 166 | (*ptr == '0' || *ptr == '1' || isDigitSeparator(*ptr))) |
| 167 | ptr++; |
| 168 | return ptr; |
| 169 | } |
| 170 | |
| 171 | }; |
| 172 | |
| 173 | |
| 174 | |
| 175 | class CharLiteralParser { |
| 176 | uint64_t Value; |
| 177 | tok::TokenKind Kind; |
| 178 | bool IsMultiChar; |
| 179 | bool HadError; |
| 180 | SmallString<32> UDSuffixBuf; |
| 181 | unsigned UDSuffixOffset; |
| 182 | public: |
| 183 | CharLiteralParser(const char *begin, const char *end, |
| 184 | SourceLocation Loc, Preprocessor &PP, |
| 185 | tok::TokenKind kind); |
| 186 | |
| 187 | bool hadError() const { return HadError; } |
| 188 | bool isAscii() const { return Kind == tok::char_constant; } |
| 189 | bool isWide() const { return Kind == tok::wide_char_constant; } |
| 190 | bool isUTF8() const { return Kind == tok::utf8_char_constant; } |
| 191 | bool isUTF16() const { return Kind == tok::utf16_char_constant; } |
| 192 | bool isUTF32() const { return Kind == tok::utf32_char_constant; } |
| 193 | bool isMultiChar() const { return IsMultiChar; } |
| 194 | uint64_t getValue() const { return Value; } |
| 195 | StringRef getUDSuffix() const { return UDSuffixBuf; } |
| 196 | unsigned getUDSuffixOffset() const { |
| 197 | (0) . __assert_fail ("!UDSuffixBuf.empty() && \"no ud-suffix\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/LiteralSupport.h", 197, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!UDSuffixBuf.empty() && "no ud-suffix"); |
| 198 | return UDSuffixOffset; |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | class StringLiteralParser { |
| 206 | const SourceManager &SM; |
| 207 | const LangOptions &Features; |
| 208 | const TargetInfo &Target; |
| 209 | DiagnosticsEngine *Diags; |
| 210 | |
| 211 | unsigned MaxTokenLength; |
| 212 | unsigned SizeBound; |
| 213 | unsigned CharByteWidth; |
| 214 | tok::TokenKind Kind; |
| 215 | SmallString<512> ResultBuf; |
| 216 | char *ResultPtr; |
| 217 | SmallString<32> UDSuffixBuf; |
| 218 | unsigned UDSuffixToken; |
| 219 | unsigned UDSuffixOffset; |
| 220 | public: |
| 221 | StringLiteralParser(ArrayRef<Token> StringToks, |
| 222 | Preprocessor &PP, bool Complain = true); |
| 223 | StringLiteralParser(ArrayRef<Token> StringToks, |
| 224 | const SourceManager &sm, const LangOptions &features, |
| 225 | const TargetInfo &target, |
| 226 | DiagnosticsEngine *diags = nullptr) |
| 227 | : SM(sm), Features(features), Target(target), Diags(diags), |
| 228 | MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown), |
| 229 | ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) { |
| 230 | init(StringToks); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | bool hadError; |
| 235 | bool Pascal; |
| 236 | |
| 237 | StringRef GetString() const { |
| 238 | return StringRef(ResultBuf.data(), GetStringLength()); |
| 239 | } |
| 240 | unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); } |
| 241 | |
| 242 | unsigned GetNumStringChars() const { |
| 243 | return GetStringLength() / CharByteWidth; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const; |
| 252 | |
| 253 | bool isAscii() const { return Kind == tok::string_literal; } |
| 254 | bool isWide() const { return Kind == tok::wide_string_literal; } |
| 255 | bool isUTF8() const { return Kind == tok::utf8_string_literal; } |
| 256 | bool isUTF16() const { return Kind == tok::utf16_string_literal; } |
| 257 | bool isUTF32() const { return Kind == tok::utf32_string_literal; } |
| 258 | bool isPascal() const { return Pascal; } |
| 259 | |
| 260 | StringRef getUDSuffix() const { return UDSuffixBuf; } |
| 261 | |
| 262 | |
| 263 | unsigned getUDSuffixToken() const { |
| 264 | (0) . __assert_fail ("!UDSuffixBuf.empty() && \"no ud-suffix\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/LiteralSupport.h", 264, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!UDSuffixBuf.empty() && "no ud-suffix"); |
| 265 | return UDSuffixToken; |
| 266 | } |
| 267 | |
| 268 | unsigned getUDSuffixOffset() const { |
| 269 | (0) . __assert_fail ("!UDSuffixBuf.empty() && \"no ud-suffix\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/LiteralSupport.h", 269, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!UDSuffixBuf.empty() && "no ud-suffix"); |
| 270 | return UDSuffixOffset; |
| 271 | } |
| 272 | |
| 273 | static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix); |
| 274 | |
| 275 | private: |
| 276 | void init(ArrayRef<Token> StringToks); |
| 277 | bool CopyStringFragment(const Token &Tok, const char *TokBegin, |
| 278 | StringRef Fragment); |
| 279 | void DiagnoseLexingError(SourceLocation Loc); |
| 280 | }; |
| 281 | |
| 282 | } |
| 283 | |
| 284 | #endif |
| 285 | |