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 CLANG_LITERALSUPPORT_H
     16 #define CLANG_LITERALSUPPORT_H
     17 
     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"
     23 #include <cctype>
     24 
     25 namespace clang {
     26 
     27 class DiagnosticsEngine;
     28 class Preprocessor;
     29 class Token;
     30 class SourceLocation;
     31 class TargetInfo;
     32 class SourceManager;
     33 class LangOptions;
     34 
     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
     40 
     41   const char *const ThisTokBegin;
     42   const char *const ThisTokEnd;
     43   const char *DigitsBegin, *SuffixBegin; // markers
     44   const char *s; // cursor
     45 
     46   unsigned radix;
     47 
     48   bool saw_exponent, saw_period, saw_ud_suffix;
     49 
     50 public:
     51   NumericLiteralParser(const char *begin, const char *end,
     52                        SourceLocation Loc, Preprocessor &PP);
     53   bool hadError;
     54   bool isUnsigned;
     55   bool isLong;        // This is *not* set for long long.
     56   bool isLongLong;
     57   bool isFloat;       // 1.0f
     58   bool isImaginary;   // 1.0i
     59   bool isMicrosoftInteger;  // Microsoft suffix extension i8, i16, i32, or i64.
     60 
     61   bool isIntegerLiteral() const {
     62     return !saw_period && !saw_exponent;
     63   }
     64   bool isFloatingLiteral() const {
     65     return saw_period || saw_exponent;
     66   }
     67 
     68   bool hasUDSuffix() const {
     69     return saw_ud_suffix;
     70   }
     71   StringRef getUDSuffix() const {
     72     assert(saw_ud_suffix);
     73     return StringRef(SuffixBegin, ThisTokEnd - SuffixBegin);
     74   }
     75   unsigned getUDSuffixOffset() const {
     76     assert(saw_ud_suffix);
     77     return SuffixBegin - ThisTokBegin;
     78   }
     79 
     80   unsigned getRadix() const { return radix; }
     81 
     82   /// GetIntegerValue - Convert this numeric literal value to an APInt that
     83   /// matches Val's input width.  If there is an overflow (i.e., if the unsigned
     84   /// value read is larger than the APInt's bits will hold), set Val to the low
     85   /// bits of the result and return true.  Otherwise, return false.
     86   bool GetIntegerValue(llvm::APInt &Val);
     87 
     88   /// GetFloatValue - Convert this numeric literal to a floating value, using
     89   /// the specified APFloat fltSemantics (specifying float, double, etc).
     90   /// The optional bool isExact (passed-by-reference) has its value
     91   /// set to true if the returned APFloat can represent the number in the
     92   /// literal exactly, and false otherwise.
     93   llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
     94 
     95 private:
     96 
     97   void ParseNumberStartingWithZero(SourceLocation TokLoc);
     98 
     99   /// SkipHexDigits - Read and skip over any hex digits, up to End.
    100   /// Return a pointer to the first non-hex digit or End.
    101   const char *SkipHexDigits(const char *ptr) {
    102     while (ptr != ThisTokEnd && isxdigit(*ptr))
    103       ptr++;
    104     return ptr;
    105   }
    106 
    107   /// SkipOctalDigits - Read and skip over any octal digits, up to End.
    108   /// Return a pointer to the first non-hex digit or End.
    109   const char *SkipOctalDigits(const char *ptr) {
    110     while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7')))
    111       ptr++;
    112     return ptr;
    113   }
    114 
    115   /// SkipDigits - Read and skip over any digits, up to End.
    116   /// Return a pointer to the first non-hex digit or End.
    117   const char *SkipDigits(const char *ptr) {
    118     while (ptr != ThisTokEnd && isdigit(*ptr))
    119       ptr++;
    120     return ptr;
    121   }
    122 
    123   /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
    124   /// Return a pointer to the first non-binary digit or End.
    125   const char *SkipBinaryDigits(const char *ptr) {
    126     while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
    127       ptr++;
    128     return ptr;
    129   }
    130 
    131 };
    132 
    133 /// CharLiteralParser - Perform interpretation and semantic analysis of a
    134 /// character literal.
    135 class CharLiteralParser {
    136   uint64_t Value;
    137   tok::TokenKind Kind;
    138   bool IsMultiChar;
    139   bool HadError;
    140   SmallString<32> UDSuffixBuf;
    141   unsigned UDSuffixOffset;
    142 public:
    143   CharLiteralParser(const char *begin, const char *end,
    144                     SourceLocation Loc, Preprocessor &PP,
    145                     tok::TokenKind kind);
    146 
    147   bool hadError() const { return HadError; }
    148   bool isAscii() const { return Kind == tok::char_constant; }
    149   bool isWide() const { return Kind == tok::wide_char_constant; }
    150   bool isUTF16() const { return Kind == tok::utf16_char_constant; }
    151   bool isUTF32() const { return Kind == tok::utf32_char_constant; }
    152   bool isMultiChar() const { return IsMultiChar; }
    153   uint64_t getValue() const { return Value; }
    154   StringRef getUDSuffix() const { return UDSuffixBuf; }
    155   unsigned getUDSuffixOffset() const {
    156     assert(!UDSuffixBuf.empty() && "no ud-suffix");
    157     return UDSuffixOffset;
    158   }
    159 };
    160 
    161 /// StringLiteralParser - This decodes string escape characters and performs
    162 /// wide string analysis and Translation Phase #6 (concatenation of string
    163 /// literals) (C99 5.1.1.2p1).
    164 class StringLiteralParser {
    165   const SourceManager &SM;
    166   const LangOptions &Features;
    167   const TargetInfo &Target;
    168   DiagnosticsEngine *Diags;
    169 
    170   unsigned MaxTokenLength;
    171   unsigned SizeBound;
    172   unsigned CharByteWidth;
    173   tok::TokenKind Kind;
    174   SmallString<512> ResultBuf;
    175   char *ResultPtr; // cursor
    176   SmallString<32> UDSuffixBuf;
    177   unsigned UDSuffixToken;
    178   unsigned UDSuffixOffset;
    179 public:
    180   StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
    181                       Preprocessor &PP, bool Complain = true);
    182   StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
    183                       const SourceManager &sm, const LangOptions &features,
    184                       const TargetInfo &target, DiagnosticsEngine *diags = 0)
    185     : SM(sm), Features(features), Target(target), Diags(diags),
    186       MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
    187       ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
    188     init(StringToks, NumStringToks);
    189   }
    190 
    191 
    192   bool hadError;
    193   bool Pascal;
    194 
    195   StringRef GetString() const {
    196     return StringRef(ResultBuf.data(), GetStringLength());
    197   }
    198   unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
    199 
    200   unsigned GetNumStringChars() const {
    201     return GetStringLength() / CharByteWidth;
    202   }
    203   /// getOffsetOfStringByte - This function returns the offset of the
    204   /// specified byte of the string data represented by Token.  This handles
    205   /// advancing over escape sequences in the string.
    206   ///
    207   /// If the Diagnostics pointer is non-null, then this will do semantic
    208   /// checking of the string literal and emit errors and warnings.
    209   unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
    210 
    211   bool isAscii() const { return Kind == tok::string_literal; }
    212   bool isWide() const { return Kind == tok::wide_string_literal; }
    213   bool isUTF8() const { return Kind == tok::utf8_string_literal; }
    214   bool isUTF16() const { return Kind == tok::utf16_string_literal; }
    215   bool isUTF32() const { return Kind == tok::utf32_string_literal; }
    216   bool isPascal() const { return Pascal; }
    217 
    218   StringRef getUDSuffix() const { return UDSuffixBuf; }
    219 
    220   /// Get the index of a token containing a ud-suffix.
    221   unsigned getUDSuffixToken() const {
    222     assert(!UDSuffixBuf.empty() && "no ud-suffix");
    223     return UDSuffixToken;
    224   }
    225   /// Get the spelling offset of the first byte of the ud-suffix.
    226   unsigned getUDSuffixOffset() const {
    227     assert(!UDSuffixBuf.empty() && "no ud-suffix");
    228     return UDSuffixOffset;
    229   }
    230 
    231 private:
    232   void init(const Token *StringToks, unsigned NumStringToks);
    233   bool CopyStringFragment(StringRef Fragment);
    234   bool DiagnoseBadString(const Token& Tok);
    235 };
    236 
    237 }  // end namespace clang
    238 
    239 #endif
    240