LiteralSupport.h revision 5f016e2cb5d11daeb237544de1c5d59f20fe1a6e
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 LexerToken;
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;
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 also set for long long.
55  bool isLongLong;
56
57  bool isIntegerLiteral() const {
58    return !saw_period && !saw_exponent ? true : false;
59  }
60  bool isFloatingLiteral() const {
61    return saw_period || saw_exponent ? true : false;
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 float.
76  /// FIXME: the return value is fixed size - make more general.
77  float GetFloatValue();
78
79private:
80  void Diag(SourceLocation Loc, unsigned DiagID,
81            const std::string &M = std::string());
82
83  /// SkipHexDigits - Read and skip over any hex digits, up to End.
84  /// Return a pointer to the first non-hex digit or End.
85  const char *SkipHexDigits(const char *ptr) {
86    while (ptr != ThisTokEnd && isxdigit(*ptr))
87      ptr++;
88    return ptr;
89  }
90
91  /// SkipOctalDigits - Read and skip over any octal digits, up to End.
92  /// Return a pointer to the first non-hex digit or End.
93  const char *SkipOctalDigits(const char *ptr) {
94    while (ptr != ThisTokEnd && ((*ptr >= '0') && (*ptr <= '7')))
95      ptr++;
96    return ptr;
97  }
98
99  /// SkipDigits - Read and skip over any digits, up to End.
100  /// Return a pointer to the first non-hex digit or End.
101  const char *SkipDigits(const char *ptr) {
102    while (ptr != ThisTokEnd && isdigit(*ptr))
103      ptr++;
104    return ptr;
105  }
106
107  /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
108  /// Return a pointer to the first non-binary digit or End.
109  const char *SkipBinaryDigits(const char *ptr) {
110    while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
111      ptr++;
112    return ptr;
113  }
114
115};
116
117/// CharLiteralParser - Perform interpretation and semantic analysis of a
118/// character literal.
119class CharLiteralParser {
120  unsigned Value;
121  bool IsWide;
122  bool HadError;
123public:
124  CharLiteralParser(const char *begin, const char *end,
125                    SourceLocation Loc, Preprocessor &PP);
126
127  bool hadError() const { return HadError; }
128  bool isWide() const { return IsWide; }
129  unsigned getValue() const { return Value; }
130};
131
132/// StringLiteralParser - This decodes string escape characters and performs
133/// wide string analysis and Translation Phase #6 (concatenation of string
134/// literals) (C99 5.1.1.2p1).
135class StringLiteralParser {
136  Preprocessor &PP;
137  TargetInfo &Target;
138
139  unsigned MaxTokenLength;
140  unsigned SizeBound;
141  unsigned wchar_tByteWidth;
142  llvm::SmallString<512> ResultBuf;
143  char *ResultPtr; // cursor
144public:
145  StringLiteralParser(const LexerToken *StringToks, unsigned NumStringToks,
146                      Preprocessor &PP, TargetInfo &T);
147  bool hadError;
148  bool AnyWide;
149
150  const char *GetString() { return &ResultBuf[0]; }
151  unsigned GetStringLength() { return ResultPtr-&ResultBuf[0]; }
152};
153
154}  // end namespace clang
155
156#endif