LiteralSupport.h revision b90f4b3fb94056609da9cca5eef7358d95a363b2
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 <string>
19#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/System/DataTypes.h"
22
23namespace clang {
24
25class Diagnostic;
26class Preprocessor;
27class Token;
28class SourceLocation;
29class TargetInfo;
30
31/// NumericLiteralParser - This performs strict semantic analysis of the content
32/// of a ppnumber, classifying it as either integer, floating, or erroneous,
33/// determines the radix of the value and can convert it to a useful value.
34class NumericLiteralParser {
35  Preprocessor &PP; // needed for diagnostics
36
37  const char *const ThisTokBegin;
38  const char *const ThisTokEnd;
39  const char *DigitsBegin, *SuffixBegin; // markers
40  const char *s; // cursor
41
42  unsigned radix;
43
44  bool saw_exponent, saw_period;
45
46public:
47  NumericLiteralParser(const char *begin, const char *end,
48                       SourceLocation Loc, Preprocessor &PP);
49  bool hadError;
50  bool isUnsigned;
51  bool isLong;        // This is *not* set for long long.
52  bool isLongLong;
53  bool isFloat;       // 1.0f
54  bool isImaginary;   // 1.0i
55  bool isMicrosoftInteger;  // Microsoft suffix extension i8, i16, i32, or i64.
56
57  bool isIntegerLiteral() const {
58    return !saw_period && !saw_exponent;
59  }
60  bool isFloatingLiteral() const {
61    return saw_period || saw_exponent;
62  }
63  bool hasSuffix() const {
64    return SuffixBegin != ThisTokEnd;
65  }
66
67  unsigned getRadix() const { return radix; }
68
69  /// GetIntegerValue - Convert this numeric literal value to an APInt that
70  /// matches Val's input width.  If there is an overflow (i.e., if the unsigned
71  /// value read is larger than the APInt's bits will hold), set Val to the low
72  /// bits of the result and return true.  Otherwise, return false.
73  bool GetIntegerValue(llvm::APInt &Val);
74
75  /// GetFloatValue - Convert this numeric literal to a floating value, using
76  /// the specified APFloat fltSemantics (specifying float, double, etc).
77  /// The optional bool isExact (passed-by-reference) has its value
78  /// set to true if the returned APFloat can represent the number in the
79  /// literal exactly, and false otherwise.
80  llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
81
82private:
83
84  void ParseNumberStartingWithZero(SourceLocation TokLoc);
85
86  /// SkipHexDigits - Read and skip over any hex digits, up to End.
87  /// Return a pointer to the first non-hex digit or End.
88  const char *SkipHexDigits(const char *ptr) {
89    while (ptr != ThisTokEnd && isxdigit(*ptr))
90      ptr++;
91    return ptr;
92  }
93
94  /// SkipOctalDigits - Read and skip over any octal digits, up to End.
95  /// Return a pointer to the first non-hex digit or End.
96  const char *SkipOctalDigits(const char *ptr) {
97    while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7')))
98      ptr++;
99    return ptr;
100  }
101
102  /// SkipDigits - Read and skip over any digits, up to End.
103  /// Return a pointer to the first non-hex digit or End.
104  const char *SkipDigits(const char *ptr) {
105    while (ptr != ThisTokEnd && isdigit(*ptr))
106      ptr++;
107    return ptr;
108  }
109
110  /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
111  /// Return a pointer to the first non-binary digit or End.
112  const char *SkipBinaryDigits(const char *ptr) {
113    while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
114      ptr++;
115    return ptr;
116  }
117
118};
119
120/// CharLiteralParser - Perform interpretation and semantic analysis of a
121/// character literal.
122class CharLiteralParser {
123  uint64_t Value;
124  bool IsWide;
125  bool IsMultiChar;
126  bool HadError;
127public:
128  CharLiteralParser(const char *begin, const char *end,
129                    SourceLocation Loc, Preprocessor &PP);
130
131  bool hadError() const { return HadError; }
132  bool isWide() const { return IsWide; }
133  bool isMultiChar() const { return IsMultiChar; }
134  uint64_t getValue() const { return Value; }
135};
136
137/// StringLiteralParser - This decodes string escape characters and performs
138/// wide string analysis and Translation Phase #6 (concatenation of string
139/// literals) (C99 5.1.1.2p1).
140class StringLiteralParser {
141  Preprocessor &PP;
142
143  unsigned MaxTokenLength;
144  unsigned SizeBound;
145  unsigned wchar_tByteWidth;
146  llvm::SmallString<512> ResultBuf;
147  char *ResultPtr; // cursor
148public:
149  StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
150                      Preprocessor &PP, bool Complain = true);
151  bool hadError;
152  bool AnyWide;
153  bool Pascal;
154
155  const char *GetString() { return &ResultBuf[0]; }
156  unsigned GetStringLength() const { return ResultPtr-&ResultBuf[0]; }
157
158  unsigned GetNumStringChars() const {
159    if (AnyWide)
160      return GetStringLength() / wchar_tByteWidth;
161    return GetStringLength();
162  }
163  /// getOffsetOfStringByte - This function returns the offset of the
164  /// specified byte of the string data represented by Token.  This handles
165  /// advancing over escape sequences in the string.
166  static unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo,
167                                        Preprocessor &PP, bool Complain = true);
168};
169
170}  // end namespace clang
171
172#endif
173