LiteralSupport.h revision 03720fce6e709661af020f3e4e6dfd08a96e8044
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
25namespace clang {
26
27class Diagnostic;
28class Preprocessor;
29class Token;
30class SourceLocation;
31class TargetInfo;
32class SourceManager;
33class 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.
38class 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;
49
50public:
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  bool hasSuffix() const {
68    return SuffixBegin != ThisTokEnd;
69  }
70
71  unsigned getRadix() const { return radix; }
72
73  /// GetIntegerValue - Convert this numeric literal value to an APInt that
74  /// matches Val's input width.  If there is an overflow (i.e., if the unsigned
75  /// value read is larger than the APInt's bits will hold), set Val to the low
76  /// bits of the result and return true.  Otherwise, return false.
77  bool GetIntegerValue(llvm::APInt &Val);
78
79  /// GetFloatValue - Convert this numeric literal to a floating value, using
80  /// the specified APFloat fltSemantics (specifying float, double, etc).
81  /// The optional bool isExact (passed-by-reference) has its value
82  /// set to true if the returned APFloat can represent the number in the
83  /// literal exactly, and false otherwise.
84  llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
85
86private:
87
88  void ParseNumberStartingWithZero(SourceLocation TokLoc);
89
90  /// SkipHexDigits - Read and skip over any hex digits, up to End.
91  /// Return a pointer to the first non-hex digit or End.
92  const char *SkipHexDigits(const char *ptr) {
93    while (ptr != ThisTokEnd && isxdigit(*ptr))
94      ptr++;
95    return ptr;
96  }
97
98  /// SkipOctalDigits - Read and skip over any octal digits, up to End.
99  /// Return a pointer to the first non-hex digit or End.
100  const char *SkipOctalDigits(const char *ptr) {
101    while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7')))
102      ptr++;
103    return ptr;
104  }
105
106  /// SkipDigits - Read and skip over any digits, up to End.
107  /// Return a pointer to the first non-hex digit or End.
108  const char *SkipDigits(const char *ptr) {
109    while (ptr != ThisTokEnd && isdigit(*ptr))
110      ptr++;
111    return ptr;
112  }
113
114  /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
115  /// Return a pointer to the first non-binary digit or End.
116  const char *SkipBinaryDigits(const char *ptr) {
117    while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
118      ptr++;
119    return ptr;
120  }
121
122};
123
124/// CharLiteralParser - Perform interpretation and semantic analysis of a
125/// character literal.
126class CharLiteralParser {
127  uint64_t Value;
128  tok::TokenKind Kind;
129  bool IsMultiChar;
130  bool HadError;
131public:
132  CharLiteralParser(const char *begin, const char *end,
133                    SourceLocation Loc, Preprocessor &PP,
134                    tok::TokenKind kind);
135
136  bool hadError() const { return HadError; }
137  bool isAscii() const { return Kind == tok::char_constant; }
138  bool isWide() const { return Kind == tok::wide_char_constant; }
139  bool isUTF16() const { return Kind == tok::utf16_char_constant; }
140  bool isUTF32() const { return Kind == tok::utf32_char_constant; }
141  bool isMultiChar() const { return IsMultiChar; }
142  uint64_t getValue() const { return Value; }
143};
144
145/// StringLiteralParser - This decodes string escape characters and performs
146/// wide string analysis and Translation Phase #6 (concatenation of string
147/// literals) (C99 5.1.1.2p1).
148class StringLiteralParser {
149  const SourceManager &SM;
150  const LangOptions &Features;
151  const TargetInfo &Target;
152  Diagnostic *Diags;
153
154  unsigned MaxTokenLength;
155  unsigned SizeBound;
156  unsigned CharByteWidth;
157  tok::TokenKind Kind;
158  llvm::SmallString<512> ResultBuf;
159  char *ResultPtr; // cursor
160public:
161  StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
162                      Preprocessor &PP, bool Complain = true);
163  StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
164                      const SourceManager &sm, const LangOptions &features,
165                      const TargetInfo &target, Diagnostic *diags = 0)
166    : SM(sm), Features(features), Target(target), Diags(diags),
167      MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
168      ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
169    init(StringToks, NumStringToks);
170  }
171
172
173  bool hadError;
174  bool Pascal;
175
176  StringRef GetString() const {
177    return StringRef(ResultBuf.data(), GetStringLength());
178  }
179  unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
180
181  unsigned GetNumStringChars() const {
182    return GetStringLength() / CharByteWidth;
183  }
184  /// getOffsetOfStringByte - This function returns the offset of the
185  /// specified byte of the string data represented by Token.  This handles
186  /// advancing over escape sequences in the string.
187  ///
188  /// If the Diagnostics pointer is non-null, then this will do semantic
189  /// checking of the string literal and emit errors and warnings.
190  unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
191
192  bool isAscii() { return Kind == tok::string_literal; }
193  bool isWide() { return Kind == tok::wide_string_literal; }
194  bool isUTF8() { return Kind == tok::utf8_string_literal; }
195  bool isUTF16() { return Kind == tok::utf16_string_literal; }
196  bool isUTF32() { return Kind == tok::utf32_string_literal; }
197
198private:
199  void init(const Token *StringToks, unsigned NumStringToks);
200  void CopyStringFragment(StringRef Fragment);
201};
202
203}  // end namespace clang
204
205#endif
206