src/xmlpatterns/expr/qcombinenodes.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 "qcommonsequencetypes_p.h"
       
    43 #include "qexceptiterator_p.h"
       
    44 #include "qgenericsequencetype_p.h"
       
    45 #include "qintersectiterator_p.h"
       
    46 #include "qitemmappingiterator_p.h"
       
    47 #include "qnodesort_p.h"
       
    48 #include "qunioniterator_p.h"
       
    49 
       
    50 #include "qcombinenodes_p.h"
       
    51 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 using namespace QPatternist;
       
    55 
       
    56 CombineNodes::CombineNodes(const Expression::Ptr &operand1,
       
    57                            const Operator op,
       
    58                            const Expression::Ptr &operand2) : PairContainer(operand1, operand2),
       
    59                                                               m_operator(op)
       
    60 {
       
    61     Q_ASSERT(op == Union    ||
       
    62              op == Except   ||
       
    63              op == Intersect);
       
    64 }
       
    65 
       
    66 Item::Iterator::Ptr CombineNodes::evaluateSequence(const DynamicContext::Ptr &context) const
       
    67 {
       
    68     const Item::Iterator::Ptr op1(m_operand1->evaluateSequence(context));
       
    69     const Item::Iterator::Ptr op2(m_operand2->evaluateSequence(context));
       
    70 
       
    71     switch(m_operator)
       
    72     {
       
    73         case Intersect:
       
    74             return Item::Iterator::Ptr(new IntersectIterator(op1, op2));
       
    75         case Except:
       
    76             return Item::Iterator::Ptr(new ExceptIterator(op1, op2));
       
    77         default:
       
    78         {
       
    79             Q_ASSERT(m_operator == Union);
       
    80             return Item::Iterator::Ptr(new UnionIterator(op1, op2));
       
    81         }
       
    82     }
       
    83 }
       
    84 
       
    85 Item CombineNodes::evaluateSingleton(const DynamicContext::Ptr &context) const
       
    86 {
       
    87     return evaluateSequence(context)->next();
       
    88 }
       
    89 
       
    90 Expression::Ptr CombineNodes::typeCheck(const StaticContext::Ptr &context,
       
    91                                         const SequenceType::Ptr &reqType)
       
    92 {
       
    93 
       
    94     const Expression::Ptr me(PairContainer::typeCheck(context, reqType));
       
    95 
       
    96     m_operand1 = NodeSortExpression::wrapAround(m_operand1, context);
       
    97     m_operand2 = NodeSortExpression::wrapAround(m_operand2, context);
       
    98 
       
    99     return me;
       
   100 }
       
   101 
       
   102 bool CombineNodes::evaluateEBV(const DynamicContext::Ptr &context) const
       
   103 {
       
   104     /* If it's the union operator, we can possibly avoid
       
   105      * evaluating the second operand. */
       
   106 
       
   107     if(m_operator == Union)
       
   108     {
       
   109         return m_operand1->evaluateEBV(context) ||
       
   110                m_operand2->evaluateEBV(context);
       
   111     }
       
   112     else
       
   113         return PairContainer::evaluateEBV(context);
       
   114 }
       
   115 
       
   116 QString CombineNodes::displayName(const Operator op)
       
   117 {
       
   118     switch(op)
       
   119     {
       
   120         case Intersect:
       
   121             return QLatin1String("intersect");
       
   122         case Except:
       
   123             return QLatin1String("except");
       
   124         default:
       
   125         {
       
   126             Q_ASSERT(op == Union);
       
   127             return QLatin1String("union");
       
   128         }
       
   129     }
       
   130 }
       
   131 
       
   132 SequenceType::Ptr CombineNodes::staticType() const
       
   133 {
       
   134     const SequenceType::Ptr t1(m_operand1->staticType());
       
   135     const SequenceType::Ptr t2(m_operand2->staticType());
       
   136 
       
   137      Cardinality card;
       
   138 
       
   139      /* Optimization: the cardinality can be better inferred for
       
   140       * Intersect and Except, although it's not trivial code. */
       
   141      if(m_operator == Union)
       
   142          card = t1->cardinality() | t2->cardinality();
       
   143      else /* Except. */
       
   144          card = Cardinality::zeroOrMore();
       
   145 
       
   146      return makeGenericSequenceType(t1->itemType() | t2->itemType(), card);
       
   147 }
       
   148 
       
   149 SequenceType::List CombineNodes::expectedOperandTypes() const
       
   150 {
       
   151     SequenceType::List result;
       
   152     result.append(CommonSequenceTypes::ZeroOrMoreNodes);
       
   153     result.append(CommonSequenceTypes::ZeroOrMoreNodes);
       
   154     return result;
       
   155 }
       
   156 
       
   157 CombineNodes::Operator CombineNodes::operatorID() const
       
   158 {
       
   159     return m_operator;
       
   160 }
       
   161 
       
   162 ExpressionVisitorResult::Ptr CombineNodes::accept(const ExpressionVisitor::Ptr &visitor) const
       
   163 {
       
   164     return visitor->visit(this);
       
   165 }
       
   166 
       
   167 Expression::ID CombineNodes::id() const
       
   168 {
       
   169     return IDCombineNodes;
       
   170 }
       
   171 
       
   172 QT_END_NAMESPACE