Home | History | Annotate | Download | only in Lex
      1 //===--- LiteralSupport.h ---------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the NumericLiteralParser, CharLiteralParser, and
     11 // StringLiteralParser interfaces.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_LEX_LITERALSUPPORT_H
     16 #define LLVM_CLANG_LEX_LITERALSUPPORT_H
     17 
     18 #include "clang/Basic/CharInfo.h"
     19 #include "clang/Basic/LLVM.h"
     20 #include "clang/Basic/TokenKinds.h"
     21 #include "llvm/ADT/APFloat.h"
     22 #include "llvm/ADT/ArrayRef.h"
     23 #include "llvm/ADT/SmallString.h"
     24 #include "llvm/ADT/StringRef.h"
     25 #include "llvm/Support/DataTypes.h"
     26 
     27 namespace clang {
     28 
     29 class DiagnosticsEngine;
     30 class Preprocessor;
     31 class Token;
     32 class SourceLocation;
     33 class TargetInfo;
     34 class SourceManager;
     35 class LangOptions;
     36 
     37 /// Copy characters from Input to Buf, expanding any UCNs.
     38 void expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input);
     39 
     40 /// NumericLiteralParser - This performs strict semantic analysis of the content
     41 /// of a ppnumber, classifying it as either integer, floating, or erroneous,
     42 /// determines the radix of the value and can convert it to a useful value.
     43 class NumericLiteralParser {
     44   Preprocessor &PP; // needed for diagnostics
     45 
     46   const char *const ThisTokBegin;
     47   const char *const ThisTokEnd;
     48   const char *DigitsBegin, *SuffixBegin; // markers
     49   const char *s; // cursor
     50 
     51   unsigned radix;
     52 
     53   bool saw_exponent, saw_period, saw_ud_suffix;
     54 
     55   SmallString<32> UDSuffixBuf;
     56 
     57 public:
     58   NumericLiteralParser(StringRef TokSpelling,
     59                        SourceLocation TokLoc,
     60                        Preprocessor &PP);
     61   bool hadError : 1;
     62   bool isUnsigned : 1;
     63   bool isLong : 1;          // This is *not* set for long long.
     64   bool isLongLong : 1;
     65   bool isHalf : 1;          // 1.0h
     66   bool isFloat : 1;         // 1.0f
     67   bool isImaginary : 1;     // 1.0i
     68   bool isFloat16 : 1;       // 1.0f16
     69   bool isFloat128 : 1;      // 1.0q
     70   uint8_t MicrosoftInteger; // Microsoft suffix extension i8, i16, i32, or i64.
     71 
     72   bool isIntegerLiteral() const {
     73     return !saw_period && !saw_exponent;
     74   }
     75   bool isFloatingLiteral() const {
     76     return saw_period || saw_exponent;
     77   }
     78 
     79   bool hasUDSuffix() const {
     80     return saw_ud_suffix;
     81   }
     82   StringRef getUDSuffix() const {
     83     assert(saw_ud_suffix);
     84     return UDSuffixBuf;
     85   }
     86   unsigned getUDSuffixOffset() const {
     87     assert(saw_ud_suffix);
     88     return SuffixBegin - ThisTokBegin;
     89   }
     90 
     91   static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix);
     92 
     93   unsigned getRadix() const { return radix; }
     94 
     95   /// GetIntegerValue - Convert this numeric literal value to an APInt that
     96   /// matches Val's input width.  If there is an overflow (i.e., if the unsigned
     97   /// value read is larger than the APInt's bits will hold), set Val to the low
     98   /// bits of the result and return true.  Otherwise, return false.
     99   bool GetIntegerValue(llvm::APInt &Val);
    100 
    101   /// GetFloatValue - Convert this numeric literal to a floating value, using
    102   /// the specified APFloat fltSemantics (specifying float, double, etc).
    103   /// The optional bool isExact (passed-by-reference) has its value
    104   /// set to true if the returned APFloat can represent the number in the
    105   /// literal exactly, and false otherwise.
    106   llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
    107 
    108 private:
    109 
    110   void ParseNumberStartingWithZero(SourceLocation TokLoc);
    111   void ParseDecimalOrOctalCommon(SourceLocation TokLoc);
    112 
    113   static bool isDigitSeparator(char C) { return C == '\''; }
    114 
    115   /// \brief Determine whether the sequence of characters [Start, End) contains
    116   /// any real digits (not digit separators).
    117   bool containsDigits(const char *Start, const char *End) {
    118     return Start != End && (Start + 1 != End || !isDigitSeparator(Start[0]));
    119   }
    120 
    121   enum CheckSeparatorKind { CSK_BeforeDigits, CSK_AfterDigits };
    122 
    123   /// \brief Ensure that we don't have a digit separator here.
    124   void checkSeparator(SourceLocation TokLoc, const char *Pos,
    125                       CheckSeparatorKind IsAfterDigits);
    126 
    127   /// SkipHexDigits - Read and skip over any hex digits, up to End.
    128   /// Return a pointer to the first non-hex digit or End.
    129   const char *SkipHexDigits(const char *ptr) {
    130     while (ptr != ThisTokEnd && (isHexDigit(*ptr) || isDigitSeparator(*ptr)))
    131       ptr++;
    132     return ptr;
    133   }
    134 
    135   /// SkipOctalDigits - Read and skip over any octal digits, up to End.
    136   /// Return a pointer to the first non-hex digit or End.
    137   const char *SkipOctalDigits(const char *ptr) {
    138     while (ptr != ThisTokEnd &&
    139            ((*ptr >= '0' && *ptr <= '7') || isDigitSeparator(*ptr)))
    140       ptr++;
    141     return ptr;
    142   }
    143 
    144   /// SkipDigits - Read and skip over any digits, up to End.
    145   /// Return a pointer to the first non-hex digit or End.
    146   const char *SkipDigits(const char *ptr) {
    147     while (ptr != ThisTokEnd && (isDigit(*ptr) || isDigitSeparator(*ptr)))
    148       ptr++;
    149     return ptr;
    150   }
    151 
    152   /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
    153   /// Return a pointer to the first non-binary digit or End.
    154   const char *SkipBinaryDigits(const char *ptr) {
    155     while (ptr != ThisTokEnd &&
    156            (*ptr == '0' || *ptr == '1' || isDigitSeparator(*ptr)))
    157       ptr++;
    158     return ptr;
    159   }
    160 
    161 };
    162 
    163 /// CharLiteralParser - Perform interpretation and semantic analysis of a
    164 /// character literal.
    165 class CharLiteralParser {
    166   uint64_t Value;
    167   tok::TokenKind Kind;
    168   bool IsMultiChar;
    169   bool HadError;
    170   SmallString<32> UDSuffixBuf;
    171   unsigned UDSuffixOffset;
    172 public:
    173   CharLiteralParser(const char *begin, const char *end,
    174                     SourceLocation Loc, Preprocessor &PP,
    175                     tok::TokenKind kind);
    176 
    177   bool hadError() const { return HadError; }
    178   bool isAscii() const { return Kind == tok::char_constant; }
    179   bool isWide() const { return Kind == tok::wide_char_constant; }
    180   bool isUTF8() const { return Kind == tok::utf8_char_constant; }
    181   bool isUTF16() const { return Kind == tok::utf16_char_constant; }
    182   bool isUTF32() const { return Kind == tok::utf32_char_constant; }
    183   bool isMultiChar() const { return IsMultiChar; }
    184   uint64_t getValue() const { return Value; }
    185   StringRef getUDSuffix() const { return UDSuffixBuf; }
    186   unsigned getUDSuffixOffset() const {
    187     assert(!UDSuffixBuf.empty() && "no ud-suffix");
    188     return UDSuffixOffset;
    189   }
    190 };
    191 
    192 /// StringLiteralParser - This decodes string escape characters and performs
    193 /// wide string analysis and Translation Phase #6 (concatenation of string
    194 /// literals) (C99 5.1.1.2p1).
    195 class StringLiteralParser {
    196   const SourceManager &SM;
    197   const LangOptions &Features;
    198   const TargetInfo &Target;
    199   DiagnosticsEngine *Diags;
    200 
    201   unsigned MaxTokenLength;
    202   unsigned SizeBound;
    203   unsigned CharByteWidth;
    204   tok::TokenKind Kind;
    205   SmallString<512> ResultBuf;
    206   char *ResultPtr; // cursor
    207   SmallString<32> UDSuffixBuf;
    208   unsigned UDSuffixToken;
    209   unsigned UDSuffixOffset;
    210 public:
    211   StringLiteralParser(ArrayRef<Token> StringToks,
    212                       Preprocessor &PP, bool Complain = true);
    213   StringLiteralParser(ArrayRef<Token> StringToks,
    214                       const SourceManager &sm, const LangOptions &features,
    215                       const TargetInfo &target,
    216                       DiagnosticsEngine *diags = nullptr)
    217     : SM(sm), Features(features), Target(target), Diags(diags),
    218       MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
    219       ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
    220     init(StringToks);
    221   }
    222 
    223 
    224   bool hadError;
    225   bool Pascal;
    226 
    227   StringRef GetString() const {
    228     return StringRef(ResultBuf.data(), GetStringLength());
    229   }
    230   unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
    231 
    232   unsigned GetNumStringChars() const {
    233     return GetStringLength() / CharByteWidth;
    234   }
    235   /// getOffsetOfStringByte - This function returns the offset of the
    236   /// specified byte of the string data represented by Token.  This handles
    237   /// advancing over escape sequences in the string.
    238   ///
    239   /// If the Diagnostics pointer is non-null, then this will do semantic
    240   /// checking of the string literal and emit errors and warnings.
    241   unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
    242 
    243   bool isAscii() const { return Kind == tok::string_literal; }
    244   bool isWide() const { return Kind == tok::wide_string_literal; }
    245   bool isUTF8() const { return Kind == tok::utf8_string_literal; }
    246   bool isUTF16() const { return Kind == tok::utf16_string_literal; }
    247   bool isUTF32() const { return Kind == tok::utf32_string_literal; }
    248   bool isPascal() const { return Pascal; }
    249 
    250   StringRef getUDSuffix() const { return UDSuffixBuf; }
    251 
    252   /// Get the index of a token containing a ud-suffix.
    253   unsigned getUDSuffixToken() const {
    254     assert(!UDSuffixBuf.empty() && "no ud-suffix");
    255     return UDSuffixToken;
    256   }
    257   /// Get the spelling offset of the first byte of the ud-suffix.
    258   unsigned getUDSuffixOffset() const {
    259     assert(!UDSuffixBuf.empty() && "no ud-suffix");
    260     return UDSuffixOffset;
    261   }
    262 
    263   static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix);
    264 
    265 private:
    266   void init(ArrayRef<Token> StringToks);
    267   bool CopyStringFragment(const Token &Tok, const char *TokBegin,
    268                           StringRef Fragment);
    269   void DiagnoseLexingError(SourceLocation Loc);
    270 };
    271 
    272 }  // end namespace clang
    273 
    274 #endif
    275