Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:57

0001 //===--- LiteralSupport.h ---------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the NumericLiteralParser, CharLiteralParser, and
0010 // StringLiteralParser interfaces.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_LEX_LITERALSUPPORT_H
0015 #define LLVM_CLANG_LEX_LITERALSUPPORT_H
0016 
0017 #include "clang/Basic/CharInfo.h"
0018 #include "clang/Basic/LLVM.h"
0019 #include "clang/Basic/TokenKinds.h"
0020 #include "llvm/ADT/APFloat.h"
0021 #include "llvm/ADT/ArrayRef.h"
0022 #include "llvm/ADT/SmallString.h"
0023 #include "llvm/ADT/StringRef.h"
0024 #include "llvm/Support/DataTypes.h"
0025 
0026 namespace clang {
0027 
0028 class DiagnosticsEngine;
0029 class Preprocessor;
0030 class Token;
0031 class SourceLocation;
0032 class TargetInfo;
0033 class SourceManager;
0034 class LangOptions;
0035 
0036 /// Copy characters from Input to Buf, expanding any UCNs.
0037 void expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input);
0038 
0039 /// Return true if the token corresponds to a function local predefined macro,
0040 /// which expands to a string literal, that can be concatenated with other
0041 /// string literals (only in Microsoft mode).
0042 bool isFunctionLocalStringLiteralMacro(tok::TokenKind K, const LangOptions &LO);
0043 
0044 /// Return true if the token is a string literal, or a function local
0045 /// predefined macro, which expands to a string literal.
0046 bool tokenIsLikeStringLiteral(const Token &Tok, const LangOptions &LO);
0047 
0048 /// NumericLiteralParser - This performs strict semantic analysis of the content
0049 /// of a ppnumber, classifying it as either integer, floating, or erroneous,
0050 /// determines the radix of the value and can convert it to a useful value.
0051 class NumericLiteralParser {
0052   const SourceManager &SM;
0053   const LangOptions &LangOpts;
0054   DiagnosticsEngine &Diags;
0055 
0056   const char *const ThisTokBegin;
0057   const char *const ThisTokEnd;
0058   const char *DigitsBegin, *SuffixBegin; // markers
0059   const char *s; // cursor
0060 
0061   unsigned radix;
0062 
0063   bool saw_exponent, saw_period, saw_ud_suffix, saw_fixed_point_suffix;
0064 
0065   SmallString<32> UDSuffixBuf;
0066 
0067 public:
0068   NumericLiteralParser(StringRef TokSpelling, SourceLocation TokLoc,
0069                        const SourceManager &SM, const LangOptions &LangOpts,
0070                        const TargetInfo &Target, DiagnosticsEngine &Diags);
0071   bool hadError : 1;
0072   bool isUnsigned : 1;
0073   bool isLong : 1;          // This is *not* set for long long.
0074   bool isLongLong : 1;
0075   bool isSizeT : 1;         // 1z, 1uz (C++23)
0076   bool isHalf : 1;          // 1.0h
0077   bool isFloat : 1;         // 1.0f
0078   bool isImaginary : 1;     // 1.0i
0079   bool isFloat16 : 1;       // 1.0f16
0080   bool isFloat128 : 1;      // 1.0q
0081   bool isFract : 1;         // 1.0hr/r/lr/uhr/ur/ulr
0082   bool isAccum : 1;         // 1.0hk/k/lk/uhk/uk/ulk
0083   bool isBitInt : 1;        // 1wb, 1uwb (C23) or 1__wb, 1__uwb (Clang extension in C++
0084                             // mode)
0085   uint8_t MicrosoftInteger; // Microsoft suffix extension i8, i16, i32, or i64.
0086 
0087 
0088   bool isFixedPointLiteral() const {
0089     return (saw_period || saw_exponent) && saw_fixed_point_suffix;
0090   }
0091 
0092   bool isIntegerLiteral() const {
0093     return !saw_period && !saw_exponent && !isFixedPointLiteral();
0094   }
0095   bool isFloatingLiteral() const {
0096     return (saw_period || saw_exponent) && !isFixedPointLiteral();
0097   }
0098 
0099   bool hasUDSuffix() const {
0100     return saw_ud_suffix;
0101   }
0102   StringRef getUDSuffix() const {
0103     assert(saw_ud_suffix);
0104     return UDSuffixBuf;
0105   }
0106   unsigned getUDSuffixOffset() const {
0107     assert(saw_ud_suffix);
0108     return SuffixBegin - ThisTokBegin;
0109   }
0110 
0111   static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix);
0112 
0113   unsigned getRadix() const { return radix; }
0114 
0115   /// GetIntegerValue - Convert this numeric literal value to an APInt that
0116   /// matches Val's input width.  If there is an overflow (i.e., if the unsigned
0117   /// value read is larger than the APInt's bits will hold), set Val to the low
0118   /// bits of the result and return true.  Otherwise, return false.
0119   bool GetIntegerValue(llvm::APInt &Val);
0120 
0121   /// Convert this numeric literal to a floating value, using the specified
0122   /// APFloat fltSemantics (specifying float, double, etc) and rounding mode.
0123   llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result,
0124                                         llvm::RoundingMode RM);
0125 
0126   /// GetFixedPointValue - Convert this numeric literal value into a
0127   /// scaled integer that represents this value. Returns true if an overflow
0128   /// occurred when calculating the integral part of the scaled integer or
0129   /// calculating the digit sequence of the exponent.
0130   bool GetFixedPointValue(llvm::APInt &StoreVal, unsigned Scale);
0131 
0132   /// Get the digits that comprise the literal. This excludes any prefix or
0133   /// suffix associated with the literal.
0134   StringRef getLiteralDigits() const {
0135     assert(!hadError && "cannot reliably get the literal digits with an error");
0136     return StringRef(DigitsBegin, SuffixBegin - DigitsBegin);
0137   }
0138 
0139 private:
0140 
0141   void ParseNumberStartingWithZero(SourceLocation TokLoc);
0142   void ParseDecimalOrOctalCommon(SourceLocation TokLoc);
0143 
0144   static bool isDigitSeparator(char C) { return C == '\''; }
0145 
0146   /// Determine whether the sequence of characters [Start, End) contains
0147   /// any real digits (not digit separators).
0148   bool containsDigits(const char *Start, const char *End) {
0149     return Start != End && (Start + 1 != End || !isDigitSeparator(Start[0]));
0150   }
0151 
0152   enum CheckSeparatorKind { CSK_BeforeDigits, CSK_AfterDigits };
0153 
0154   /// Ensure that we don't have a digit separator here.
0155   void checkSeparator(SourceLocation TokLoc, const char *Pos,
0156                       CheckSeparatorKind IsAfterDigits);
0157 
0158   /// SkipHexDigits - Read and skip over any hex digits, up to End.
0159   /// Return a pointer to the first non-hex digit or End.
0160   const char *SkipHexDigits(const char *ptr) {
0161     while (ptr != ThisTokEnd && (isHexDigit(*ptr) || isDigitSeparator(*ptr)))
0162       ptr++;
0163     return ptr;
0164   }
0165 
0166   /// SkipOctalDigits - Read and skip over any octal digits, up to End.
0167   /// Return a pointer to the first non-hex digit or End.
0168   const char *SkipOctalDigits(const char *ptr) {
0169     while (ptr != ThisTokEnd &&
0170            ((*ptr >= '0' && *ptr <= '7') || isDigitSeparator(*ptr)))
0171       ptr++;
0172     return ptr;
0173   }
0174 
0175   /// SkipDigits - Read and skip over any digits, up to End.
0176   /// Return a pointer to the first non-hex digit or End.
0177   const char *SkipDigits(const char *ptr) {
0178     while (ptr != ThisTokEnd && (isDigit(*ptr) || isDigitSeparator(*ptr)))
0179       ptr++;
0180     return ptr;
0181   }
0182 
0183   /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
0184   /// Return a pointer to the first non-binary digit or End.
0185   const char *SkipBinaryDigits(const char *ptr) {
0186     while (ptr != ThisTokEnd &&
0187            (*ptr == '0' || *ptr == '1' || isDigitSeparator(*ptr)))
0188       ptr++;
0189     return ptr;
0190   }
0191 
0192 };
0193 
0194 /// CharLiteralParser - Perform interpretation and semantic analysis of a
0195 /// character literal.
0196 class CharLiteralParser {
0197   uint64_t Value;
0198   tok::TokenKind Kind;
0199   bool IsMultiChar;
0200   bool HadError;
0201   SmallString<32> UDSuffixBuf;
0202   unsigned UDSuffixOffset;
0203 public:
0204   CharLiteralParser(const char *begin, const char *end,
0205                     SourceLocation Loc, Preprocessor &PP,
0206                     tok::TokenKind kind);
0207 
0208   bool hadError() const { return HadError; }
0209   bool isOrdinary() const { return Kind == tok::char_constant; }
0210   bool isWide() const { return Kind == tok::wide_char_constant; }
0211   bool isUTF8() const { return Kind == tok::utf8_char_constant; }
0212   bool isUTF16() const { return Kind == tok::utf16_char_constant; }
0213   bool isUTF32() const { return Kind == tok::utf32_char_constant; }
0214   bool isMultiChar() const { return IsMultiChar; }
0215   uint64_t getValue() const { return Value; }
0216   StringRef getUDSuffix() const { return UDSuffixBuf; }
0217   unsigned getUDSuffixOffset() const {
0218     assert(!UDSuffixBuf.empty() && "no ud-suffix");
0219     return UDSuffixOffset;
0220   }
0221 };
0222 
0223 enum class StringLiteralEvalMethod {
0224   Evaluated,
0225   Unevaluated,
0226 };
0227 
0228 /// StringLiteralParser - This decodes string escape characters and performs
0229 /// wide string analysis and Translation Phase #6 (concatenation of string
0230 /// literals) (C99 5.1.1.2p1).
0231 class StringLiteralParser {
0232   const SourceManager &SM;
0233   const LangOptions &Features;
0234   const TargetInfo &Target;
0235   DiagnosticsEngine *Diags;
0236 
0237   unsigned MaxTokenLength;
0238   unsigned SizeBound;
0239   unsigned CharByteWidth;
0240   tok::TokenKind Kind;
0241   SmallString<512> ResultBuf;
0242   char *ResultPtr; // cursor
0243   SmallString<32> UDSuffixBuf;
0244   unsigned UDSuffixToken;
0245   unsigned UDSuffixOffset;
0246   StringLiteralEvalMethod EvalMethod;
0247 
0248 public:
0249   StringLiteralParser(ArrayRef<Token> StringToks, Preprocessor &PP,
0250                       StringLiteralEvalMethod StringMethod =
0251                           StringLiteralEvalMethod::Evaluated);
0252   StringLiteralParser(ArrayRef<Token> StringToks, const SourceManager &sm,
0253                       const LangOptions &features, const TargetInfo &target,
0254                       DiagnosticsEngine *diags = nullptr)
0255       : SM(sm), Features(features), Target(target), Diags(diags),
0256         MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
0257         ResultPtr(ResultBuf.data()),
0258         EvalMethod(StringLiteralEvalMethod::Evaluated), hadError(false),
0259         Pascal(false) {
0260     init(StringToks);
0261   }
0262 
0263   bool hadError;
0264   bool Pascal;
0265 
0266   StringRef GetString() const {
0267     return StringRef(ResultBuf.data(), GetStringLength());
0268   }
0269   unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
0270 
0271   unsigned GetNumStringChars() const {
0272     return GetStringLength() / CharByteWidth;
0273   }
0274   /// getOffsetOfStringByte - This function returns the offset of the
0275   /// specified byte of the string data represented by Token.  This handles
0276   /// advancing over escape sequences in the string.
0277   ///
0278   /// If the Diagnostics pointer is non-null, then this will do semantic
0279   /// checking of the string literal and emit errors and warnings.
0280   unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
0281 
0282   bool isOrdinary() const { return Kind == tok::string_literal; }
0283   bool isWide() const { return Kind == tok::wide_string_literal; }
0284   bool isUTF8() const { return Kind == tok::utf8_string_literal; }
0285   bool isUTF16() const { return Kind == tok::utf16_string_literal; }
0286   bool isUTF32() const { return Kind == tok::utf32_string_literal; }
0287   bool isPascal() const { return Pascal; }
0288   bool isUnevaluated() const {
0289     return EvalMethod == StringLiteralEvalMethod::Unevaluated;
0290   }
0291 
0292   StringRef getUDSuffix() const { return UDSuffixBuf; }
0293 
0294   /// Get the index of a token containing a ud-suffix.
0295   unsigned getUDSuffixToken() const {
0296     assert(!UDSuffixBuf.empty() && "no ud-suffix");
0297     return UDSuffixToken;
0298   }
0299   /// Get the spelling offset of the first byte of the ud-suffix.
0300   unsigned getUDSuffixOffset() const {
0301     assert(!UDSuffixBuf.empty() && "no ud-suffix");
0302     return UDSuffixOffset;
0303   }
0304 
0305   static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix);
0306 
0307 private:
0308   void init(ArrayRef<Token> StringToks);
0309   bool CopyStringFragment(const Token &Tok, const char *TokBegin,
0310                           StringRef Fragment);
0311   void DiagnoseLexingError(SourceLocation Loc);
0312 };
0313 
0314 }  // end namespace clang
0315 
0316 #endif