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