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