LiteralSupport.h revision 506b8dec4ed3db3c60bf9e0dd37901f0cf3d6749
1//===--- LiteralSupport.h ---------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source 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/SmallString.h"
20
21namespace llvm {
22  class APInt;
23}
24
25namespace clang {
26
27class Diagnostic;
28class Preprocessor;
29class Token;
30class SourceLocation;
31class TargetInfo;
32
33/// NumericLiteralParser - This performs strict semantic analysis of the content
34/// of a ppnumber, classifying it as either integer, floating, or erroneous,
35/// determines the radix of the value and can convert it to a useful value.
36class NumericLiteralParser {
37  Preprocessor &PP; // needed for diagnostics
38
39  const char *const ThisTokBegin;
40  const char *const ThisTokEnd;
41  const char *DigitsBegin, *SuffixBegin; // markers
42  const char *s; // cursor
43
44  unsigned radix;
45
46  bool saw_exponent, saw_period;
47  bool saw_float_suffix;    // 1.0f
48
49public:
50  NumericLiteralParser(const char *begin, const char *end,
51                       SourceLocation Loc, Preprocessor &PP);
52  bool hadError;
53  bool isUnsigned;
54  bool isLong;        // This is *not* set for long long.
55  bool isLongLong;
56  bool isImaginary;   // 1.0i
57
58  bool isIntegerLiteral() const {
59    return !saw_period && !saw_exponent;
60  }
61  bool isFloatingLiteral() const {
62    return saw_period || saw_exponent;
63  }
64  bool hasSuffix() const {
65    return SuffixBegin != ThisTokEnd;
66  }
67
68  unsigned getRadix() const { return radix; }
69
70  /// GetIntegerValue - Convert this numeric literal value to an APInt that
71  /// matches Val's input width.  If there is an overflow (i.e., if the unsigned
72  /// value read is larger than the APInt's bits will hold), set Val to the low
73  /// bits of the result and return true.  Otherwise, return false.
74  bool GetIntegerValue(llvm::APInt &Val);
75
76  /// GetFloatValue - Convert this numeric literal to a float.
77  /// FIXME: the return value is fixed size - make more general.
78  float GetFloatValue();
79
80private:
81  void Diag(SourceLocation Loc, unsigned DiagID,
82            const std::string &M = std::string());
83
84  /// SkipHexDigits - Read and skip over any hex digits, up to End.
85  /// Return a pointer to the first non-hex digit or End.
86  const char *SkipHexDigits(const char *ptr) {
87    while (ptr != ThisTokEnd && isxdigit(*ptr))
88      ptr++;
89    return ptr;
90  }
91
92  /// SkipOctalDigits - Read and skip over any octal digits, up to End.
93  /// Return a pointer to the first non-hex digit or End.
94  const char *SkipOctalDigits(const char *ptr) {
95    while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7')))
96      ptr++;
97    return ptr;
98  }
99
100  /// SkipDigits - Read and skip over any digits, up to End.
101  /// Return a pointer to the first non-hex digit or End.
102  const char *SkipDigits(const char *ptr) {
103    while (ptr != ThisTokEnd && isdigit(*ptr))
104      ptr++;
105    return ptr;
106  }
107
108  /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
109  /// Return a pointer to the first non-binary digit or End.
110  const char *SkipBinaryDigits(const char *ptr) {
111    while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
112      ptr++;
113    return ptr;
114  }
115
116};
117
118/// CharLiteralParser - Perform interpretation and semantic analysis of a
119/// character literal.
120class CharLiteralParser {
121  unsigned Value;
122  bool IsWide;
123  bool HadError;
124public:
125  CharLiteralParser(const char *begin, const char *end,
126                    SourceLocation Loc, Preprocessor &PP);
127
128  bool hadError() const { return HadError; }
129  bool isWide() const { return IsWide; }
130  unsigned getValue() const { return Value; }
131};
132
133/// StringLiteralParser - This decodes string escape characters and performs
134/// wide string analysis and Translation Phase #6 (concatenation of string
135/// literals) (C99 5.1.1.2p1).
136class StringLiteralParser {
137  Preprocessor &PP;
138  TargetInfo &Target;
139
140  unsigned MaxTokenLength;
141  unsigned SizeBound;
142  unsigned wchar_tByteWidth;
143  llvm::SmallString<512> ResultBuf;
144  char *ResultPtr; // cursor
145public:
146  StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
147                      Preprocessor &PP, TargetInfo &T);
148  bool hadError;
149  bool AnyWide;
150
151  const char *GetString() { return &ResultBuf[0]; }
152  unsigned GetStringLength() { return ResultPtr-&ResultBuf[0]; }
153};
154
155}  // end namespace clang
156
157#endif
158