OSDN Git Service

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