src/xmlpatterns/expr/qrangeexpression.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 QtXmlPatterns module of the Qt Toolkit.
       
     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 
       
    42 #include "qbuiltintypes_p.h"
       
    43 #include "qcommonsequencetypes_p.h"
       
    44 #include "qcommonvalues_p.h"
       
    45 #include "qgenericsequencetype_p.h"
       
    46 #include "qinteger_p.h"
       
    47 #include "qliteral_p.h"
       
    48 #include "qrangeiterator_p.h"
       
    49 
       
    50 #include "qrangeexpression_p.h"
       
    51 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 using namespace QPatternist;
       
    55 
       
    56 RangeExpression::RangeExpression(const Expression::Ptr &operand1,
       
    57                                  const Expression::Ptr &operand2) : PairContainer(operand1, operand2)
       
    58 {
       
    59 }
       
    60 
       
    61 Item::Iterator::Ptr RangeExpression::evaluateSequence(const DynamicContext::Ptr &context) const
       
    62 {
       
    63     const Item s(m_operand1->evaluateSingleton(context));
       
    64 
       
    65     if(!s)
       
    66         return CommonValues::emptyIterator;
       
    67 
       
    68     const Item e(m_operand2->evaluateSingleton(context));
       
    69     if(!e)
       
    70         return CommonValues::emptyIterator;
       
    71 
       
    72     const xsInteger start = s.as<Numeric>()->toInteger();
       
    73     const xsInteger end = e.as<Numeric>()->toInteger();
       
    74 
       
    75     if(start > end)
       
    76         return CommonValues::emptyIterator;
       
    77     else if(start == end)
       
    78         return makeSingletonIterator(s);
       
    79     else
       
    80         return Item::Iterator::Ptr(new RangeIterator(start, RangeIterator::Forward, end));
       
    81 }
       
    82 
       
    83 Item RangeExpression::evaluateSingleton(const DynamicContext::Ptr &context) const
       
    84 {
       
    85     return m_operand1->evaluateSingleton(context);
       
    86 }
       
    87 
       
    88 SequenceType::List RangeExpression::expectedOperandTypes() const
       
    89 {
       
    90     SequenceType::List result;
       
    91     result.append(CommonSequenceTypes::ZeroOrOneInteger);
       
    92     result.append(CommonSequenceTypes::ZeroOrOneInteger);
       
    93     return result;
       
    94 }
       
    95 
       
    96 SequenceType::Ptr RangeExpression::staticType() const
       
    97 {
       
    98     /* This logic makes the Cardinality more specific. */
       
    99     Cardinality::Count from;
       
   100     bool hasFrom;
       
   101 
       
   102     if(m_operand1->is(IDIntegerValue))
       
   103     {
       
   104         from = m_operand1->as<Literal>()->item().as<Integer>()->toInteger();
       
   105         hasFrom = true;
       
   106     }
       
   107     else
       
   108     {
       
   109         hasFrom = false;
       
   110         from = 0;
       
   111     }
       
   112 
       
   113     /* We can't check whether to is -1 since maybe the user wrote -1. Hence
       
   114      * hasTo is required. */
       
   115     bool hasTo;
       
   116     Cardinality::Count to;
       
   117 
       
   118     if(m_operand2->is(IDIntegerValue))
       
   119     {
       
   120         const xsInteger asInt = m_operand2->as<Literal>()->item().as<Integer>()->toInteger();
       
   121         to = asInt;
       
   122 
       
   123         if(to == asInt)
       
   124             hasTo = true;
       
   125         else
       
   126         {
       
   127             /* Cardinality::Count is not the same as type xsInteger. We had overflow. */
       
   128             to = -1;
       
   129             hasTo = false;
       
   130         }
       
   131     }
       
   132     else
       
   133     {
       
   134         to = -1;
       
   135         hasTo = false;
       
   136     }
       
   137 
       
   138     if(hasTo && hasFrom)
       
   139     {
       
   140         if(from > to)
       
   141         {
       
   142             /* The query is incorrectly written, we'll evaluate to the empty sequence.
       
   143              * Just return what's correct. */
       
   144             return CommonSequenceTypes::ZeroOrMoreIntegers;
       
   145         }
       
   146         else
       
   147         {
       
   148             Cardinality::Count count = (to - from) + 1; /* + 1, since it's inclusive. */
       
   149             return makeGenericSequenceType(BuiltinTypes::xsInteger, Cardinality::fromExact(count));
       
   150         }
       
   151     }
       
   152     else
       
   153     {
       
   154         /* We can't do fromExact(from, -1) since the latter can evaluate to a value that actually is
       
   155          * lower than from, although that unfortunately is very unlikely. */
       
   156         return CommonSequenceTypes::ZeroOrMoreIntegers;
       
   157     }
       
   158 }
       
   159 
       
   160 Expression::Properties RangeExpression::properties() const
       
   161 {
       
   162     return Expression::DisableElimination;
       
   163 }
       
   164 
       
   165 ExpressionVisitorResult::Ptr RangeExpression::accept(const ExpressionVisitor::Ptr &visitor) const
       
   166 {
       
   167     return visitor->visit(this);
       
   168 }
       
   169 
       
   170 QT_END_NAMESPACE