OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / shared / cplusplus / Literals.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
33 //
34 // Permission is hereby granted, free of charge, to any person obtaining a copy
35 // of this software and associated documentation files (the "Software"), to deal
36 // in the Software without restriction, including without limitation the rights
37 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38 // copies of the Software, and to permit persons to whom the Software is
39 // furnished to do so, subject to the following conditions:
40 //
41 // The above copyright notice and this permission notice shall be included in
42 // all copies or substantial portions of the Software.
43 //
44 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
50 // THE SOFTWARE.
51
52 #include "Literals.h"
53 #include "NameVisitor.h"
54 #include <cstring>
55 #include <algorithm>
56 #include <iostream>
57
58 using namespace CPlusPlus;
59
60 ////////////////////////////////////////////////////////////////////////////////
61 Literal::Literal(const char *chars, unsigned size)
62     : _next(0), _index(0)
63 {
64     _chars = new char[size + 1];
65
66     std::strncpy(_chars, chars, size);
67     _chars[size] = '\0';
68     _size = size;
69
70     _hashCode = hashCode(_chars, _size);
71 }
72
73 Literal::~Literal()
74 { delete[] _chars; }
75
76 bool Literal::equalTo(const Literal *other) const
77 {
78     if (! other)
79         return false;
80     else if (this == other)
81         return true;
82     else if (hashCode() != other->hashCode())
83         return false;
84     else if (size() != other->size())
85         return false;
86     return ! std::strcmp(chars(), other->chars());
87 }
88
89 Literal::iterator Literal::begin() const
90 { return _chars; }
91
92 Literal::iterator Literal::end() const
93 { return _chars + _size; }
94
95 const char *Literal::chars() const
96 { return _chars; }
97
98 char Literal::at(unsigned index) const
99 { return _chars[index]; }
100
101 unsigned Literal::size() const
102 { return _size; }
103
104 unsigned Literal::hashCode() const
105 { return _hashCode; }
106
107 unsigned Literal::hashCode(const char *chars, unsigned size)
108 {
109     unsigned h = 0;
110     for (unsigned i = 0; i < size; ++i)
111         h = (h >> 5) - h + chars[i];
112     return h;
113 }
114
115 ////////////////////////////////////////////////////////////////////////////////
116 StringLiteral::StringLiteral(const char *chars, unsigned size)
117     : Literal(chars, size)
118 { }
119
120 StringLiteral::~StringLiteral()
121 { }
122
123 ////////////////////////////////////////////////////////////////////////////////
124 enum {
125     NumericLiteralIsInt,
126     NumericLiteralIsFloat,
127     NumericLiteralIsDouble,
128     NumericLiteralIsLongDouble,
129     NumericLiteralIsLong,
130     NumericLiteralIsLongLong
131 };
132
133 NumericLiteral::NumericLiteral(const char *chars, unsigned size)
134     : Literal(chars, size), _flags(0)
135 {
136     f._type = NumericLiteralIsInt;
137
138     if (size > 1 && chars[0] == '0' && (chars[1] == 'x' || chars[1] == 'X')) {
139         f._isHex = true;
140     } else {
141         const char *begin = chars;
142         const char *end = begin + size;
143
144         bool done = false;
145         const char *it = end - 1;
146
147         for (; it != begin - 1 && ! done; --it) {
148             switch (*it) {
149             case 'l': case 'L': // long suffix
150             case 'u': case 'U': // unsigned suffix
151             case 'f': case 'F': // floating suffix
152                 break;
153
154             default:
155                 done = true;
156                 break;
157             } // switch
158         }
159
160         for (const char *dot = it; it != begin - 1; --it) {
161             if (*dot == '.')
162                 f._type = NumericLiteralIsDouble;
163         }
164
165         for (++it; it != end; ++it) {
166             if (*it == 'l' || *it == 'L') {
167                 if (f._type == NumericLiteralIsDouble) {
168                     f._type = NumericLiteralIsLongDouble;
169                 } else if (it + 1 != end && (it[1] == 'l' || it[1] == 'L')) {
170                     ++it;
171                     f._type = NumericLiteralIsLongLong;
172                 } else {
173                     f._type = NumericLiteralIsLong;
174                 }
175             } else if (*it == 'f' || *it == 'F') {
176                 f._type = NumericLiteralIsFloat;
177             } else if (*it == 'u' || *it == 'U') {
178                 f._isUnsigned = true;
179             }
180         }
181     }
182 }
183
184 NumericLiteral::~NumericLiteral()
185 { }
186
187 bool NumericLiteral::isHex() const
188 { return f._isHex; }
189
190 bool NumericLiteral::isUnsigned() const
191 { return f._isUnsigned; }
192
193 bool NumericLiteral::isInt() const
194 { return f._type == NumericLiteralIsInt; }
195
196 bool NumericLiteral::isFloat() const
197 { return f._type == NumericLiteralIsFloat; }
198
199 bool NumericLiteral::isDouble() const
200 { return f._type == NumericLiteralIsDouble; }
201
202 bool NumericLiteral::isLongDouble() const
203 { return f._type == NumericLiteralIsLongDouble; }
204
205 bool NumericLiteral::isLong() const
206 { return f._type == NumericLiteralIsLong; }
207
208 bool NumericLiteral::isLongLong() const
209 { return f._type == NumericLiteralIsLongLong; }
210
211 ////////////////////////////////////////////////////////////////////////////////
212 Identifier::Identifier(const char *chars, unsigned size)
213     : Literal(chars, size)
214 { }
215
216 Identifier::~Identifier()
217 { }
218
219 void Identifier::accept0(NameVisitor *visitor) const
220 { visitor->visit(this); }
221
222 bool Identifier::isEqualTo(const Name *other) const
223 {
224     if (this == other)
225         return true;
226
227     else if (other) {
228         if (const Identifier *nameId = other->asNameId()) {
229             return equalTo(nameId);
230         }
231     }
232     return false;
233 }