1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
32 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
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:
41 // The above copyright notice and this permission notice shall be included in
42 // all copies or substantial portions of the Software.
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
53 #include "NameVisitor.h"
58 using namespace CPlusPlus;
60 ////////////////////////////////////////////////////////////////////////////////
61 Literal::Literal(const char *chars, unsigned size)
64 _chars = new char[size + 1];
66 std::strncpy(_chars, chars, size);
70 _hashCode = hashCode(_chars, _size);
76 bool Literal::equalTo(const Literal *other) const
80 else if (this == other)
82 else if (hashCode() != other->hashCode())
84 else if (size() != other->size())
86 return ! std::strcmp(chars(), other->chars());
89 Literal::iterator Literal::begin() const
92 Literal::iterator Literal::end() const
93 { return _chars + _size; }
95 const char *Literal::chars() const
98 char Literal::at(unsigned index) const
99 { return _chars[index]; }
101 unsigned Literal::size() const
104 unsigned Literal::hashCode() const
105 { return _hashCode; }
107 unsigned Literal::hashCode(const char *chars, unsigned size)
110 for (unsigned i = 0; i < size; ++i)
111 h = (h >> 5) - h + chars[i];
115 ////////////////////////////////////////////////////////////////////////////////
116 StringLiteral::StringLiteral(const char *chars, unsigned size)
117 : Literal(chars, size)
120 StringLiteral::~StringLiteral()
123 ////////////////////////////////////////////////////////////////////////////////
126 NumericLiteralIsFloat,
127 NumericLiteralIsDouble,
128 NumericLiteralIsLongDouble,
129 NumericLiteralIsLong,
130 NumericLiteralIsLongLong
133 NumericLiteral::NumericLiteral(const char *chars, unsigned size)
134 : Literal(chars, size), _flags(0)
136 f._type = NumericLiteralIsInt;
138 if (size > 1 && chars[0] == '0' && (chars[1] == 'x' || chars[1] == 'X')) {
141 const char *begin = chars;
142 const char *end = begin + size;
145 const char *it = end - 1;
147 for (; it != begin - 1 && ! done; --it) {
149 case 'l': case 'L': // long suffix
150 case 'u': case 'U': // unsigned suffix
151 case 'f': case 'F': // floating suffix
160 for (const char *dot = it; it != begin - 1; --it) {
162 f._type = NumericLiteralIsDouble;
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')) {
171 f._type = NumericLiteralIsLongLong;
173 f._type = NumericLiteralIsLong;
175 } else if (*it == 'f' || *it == 'F') {
176 f._type = NumericLiteralIsFloat;
177 } else if (*it == 'u' || *it == 'U') {
178 f._isUnsigned = true;
184 NumericLiteral::~NumericLiteral()
187 bool NumericLiteral::isHex() const
190 bool NumericLiteral::isUnsigned() const
191 { return f._isUnsigned; }
193 bool NumericLiteral::isInt() const
194 { return f._type == NumericLiteralIsInt; }
196 bool NumericLiteral::isFloat() const
197 { return f._type == NumericLiteralIsFloat; }
199 bool NumericLiteral::isDouble() const
200 { return f._type == NumericLiteralIsDouble; }
202 bool NumericLiteral::isLongDouble() const
203 { return f._type == NumericLiteralIsLongDouble; }
205 bool NumericLiteral::isLong() const
206 { return f._type == NumericLiteralIsLong; }
208 bool NumericLiteral::isLongLong() const
209 { return f._type == NumericLiteralIsLongLong; }
211 ////////////////////////////////////////////////////////////////////////////////
212 Identifier::Identifier(const char *chars, unsigned size)
213 : Literal(chars, size)
216 Identifier::~Identifier()
219 void Identifier::accept0(NameVisitor *visitor) const
220 { visitor->visit(this); }
222 bool Identifier::isEqualTo(const Name *other) const
228 if (const Identifier *nameId = other->asNameId()) {
229 return equalTo(nameId);