2008-12-02 12:01:29 +01:00
|
|
|
// Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
#include "Literals.h"
|
2010-09-02 11:59:01 +02:00
|
|
|
#include "NameVisitor.h"
|
2014-03-24 16:06:39 +01:00
|
|
|
#include "Matcher.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2009-10-20 11:21:25 +02:00
|
|
|
using namespace CPlusPlus;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
Literal::Literal(const char *chars, unsigned size)
|
2010-09-03 12:11:15 +02:00
|
|
|
: _next(0), _index(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
_chars = new char[size + 1];
|
|
|
|
|
|
2009-11-23 11:56:44 +01:00
|
|
|
std::strncpy(_chars, chars, size);
|
2008-12-02 12:01:29 +01:00
|
|
|
_chars[size] = '\0';
|
|
|
|
|
_size = size;
|
|
|
|
|
|
|
|
|
|
_hashCode = hashCode(_chars, _size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Literal::~Literal()
|
|
|
|
|
{ delete[] _chars; }
|
|
|
|
|
|
2010-09-02 11:59:01 +02:00
|
|
|
bool Literal::equalTo(const Literal *other) const
|
2009-11-13 15:17:27 +01:00
|
|
|
{
|
|
|
|
|
if (! other)
|
|
|
|
|
return false;
|
|
|
|
|
else if (this == other)
|
|
|
|
|
return true;
|
|
|
|
|
else if (hashCode() != other->hashCode())
|
|
|
|
|
return false;
|
|
|
|
|
else if (size() != other->size())
|
|
|
|
|
return false;
|
2009-11-23 11:56:44 +01:00
|
|
|
return ! std::strcmp(chars(), other->chars());
|
2009-11-13 15:17:27 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned Literal::hashCode(const char *chars, unsigned size)
|
|
|
|
|
{
|
2011-09-07 08:45:01 +02:00
|
|
|
/* Hash taken from QtCore's qHash for strings, which in turn has the note:
|
|
|
|
|
|
|
|
|
|
These functions are based on Peter J. Weinberger's hash function
|
|
|
|
|
(from the Dragon Book). The constant 24 in the original function
|
|
|
|
|
was replaced with 23 to produce fewer collisions on input such as
|
|
|
|
|
"a", "aa", "aaa", "aaaa", ...
|
|
|
|
|
*/
|
|
|
|
|
|
2011-11-29 10:51:56 +01:00
|
|
|
unsigned h = 0;
|
2011-09-07 08:45:01 +02:00
|
|
|
|
|
|
|
|
while (size--) {
|
|
|
|
|
h = (h << 4) + *chars++;
|
|
|
|
|
h ^= (h & 0xf0000000) >> 23;
|
|
|
|
|
h &= 0x0fffffff;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
NumericLiteral::NumericLiteral(const char *chars, unsigned size)
|
2009-07-10 17:00:19 +02:00
|
|
|
: Literal(chars, size), _flags(0)
|
|
|
|
|
{
|
2009-07-27 21:47:03 +02:00
|
|
|
f._type = NumericLiteralIsInt;
|
2009-07-10 17:00:19 +02:00
|
|
|
|
2010-07-01 15:27:03 +02:00
|
|
|
if (size > 1 && chars[0] == '0' && (chars[1] == 'x' || chars[1] == 'X')) {
|
2009-07-27 21:47:03 +02:00
|
|
|
f._isHex = true;
|
2009-07-10 17:00:19 +02:00
|
|
|
} else {
|
|
|
|
|
const char *begin = chars;
|
|
|
|
|
const char *end = begin + size;
|
|
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
|
const char *it = end - 1;
|
|
|
|
|
|
|
|
|
|
for (; it != begin - 1 && ! done; --it) {
|
|
|
|
|
switch (*it) {
|
|
|
|
|
case 'l': case 'L': // long suffix
|
|
|
|
|
case 'u': case 'U': // unsigned suffix
|
|
|
|
|
case 'f': case 'F': // floating suffix
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
done = true;
|
|
|
|
|
break;
|
|
|
|
|
} // switch
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const char *dot = it; it != begin - 1; --it) {
|
|
|
|
|
if (*dot == '.')
|
2009-07-27 21:47:03 +02:00
|
|
|
f._type = NumericLiteralIsDouble;
|
2009-07-10 17:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (++it; it != end; ++it) {
|
|
|
|
|
if (*it == 'l' || *it == 'L') {
|
2009-07-27 21:47:03 +02:00
|
|
|
if (f._type == NumericLiteralIsDouble) {
|
|
|
|
|
f._type = NumericLiteralIsLongDouble;
|
2009-07-10 17:00:19 +02:00
|
|
|
} else if (it + 1 != end && (it[1] == 'l' || it[1] == 'L')) {
|
|
|
|
|
++it;
|
2009-07-27 21:47:03 +02:00
|
|
|
f._type = NumericLiteralIsLongLong;
|
2009-07-10 17:00:19 +02:00
|
|
|
} else {
|
2009-07-27 21:47:03 +02:00
|
|
|
f._type = NumericLiteralIsLong;
|
2009-07-10 17:00:19 +02:00
|
|
|
}
|
|
|
|
|
} else if (*it == 'f' || *it == 'F') {
|
2009-07-27 21:47:03 +02:00
|
|
|
f._type = NumericLiteralIsFloat;
|
2009-07-10 17:00:19 +02:00
|
|
|
} else if (*it == 'u' || *it == 'U') {
|
2009-07-27 21:47:03 +02:00
|
|
|
f._isUnsigned = true;
|
2009-07-10 17:00:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2010-09-02 11:59:01 +02:00
|
|
|
void Identifier::accept0(NameVisitor *visitor) const
|
|
|
|
|
{ visitor->visit(this); }
|
|
|
|
|
|
2014-03-24 16:06:39 +01:00
|
|
|
bool Identifier::match0(const Name *otherName, Matcher *matcher) const
|
|
|
|
|
{
|
|
|
|
|
if (const Identifier *id = otherName->asNameId())
|
|
|
|
|
return matcher->match(this, id);
|
|
|
|
|
return false;
|
|
|
|
|
}
|