src/xmlpatterns/expr/qforclause.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 "qemptysequence_p.h"
       
    44 #include "qgenericsequencetype_p.h"
       
    45 #include "qitemmappingiterator_p.h"
       
    46 #include "qoptimizationpasses_p.h"
       
    47 #include "qsequencemappingiterator_p.h"
       
    48 
       
    49 #include "qforclause_p.h"
       
    50 
       
    51 QT_BEGIN_NAMESPACE
       
    52 
       
    53 using namespace QPatternist;
       
    54 
       
    55 ForClause::ForClause(const VariableSlotID varSlot,
       
    56                      const Expression::Ptr &bindingSequence,
       
    57                      const Expression::Ptr &returnExpression,
       
    58                      const VariableSlotID positionSlot) : PairContainer(bindingSequence, returnExpression),
       
    59                                                           m_varSlot(varSlot),
       
    60                                                           m_positionSlot(positionSlot),
       
    61                                                           m_allowsMany(true)
       
    62 {
       
    63     Q_ASSERT(m_positionSlot > -2);
       
    64 }
       
    65 
       
    66 Item ForClause::mapToItem(const Item &item,
       
    67                           const DynamicContext::Ptr &context) const
       
    68 {
       
    69     context->setRangeVariable(m_varSlot, item);
       
    70     return m_operand2->evaluateSingleton(context);
       
    71 }
       
    72 
       
    73 Item::Iterator::Ptr ForClause::mapToSequence(const Item &item,
       
    74                                              const DynamicContext::Ptr &context) const
       
    75 {
       
    76     context->setRangeVariable(m_varSlot, item);
       
    77     return m_operand2->evaluateSequence(context);
       
    78 }
       
    79 
       
    80 void ForClause::riggPositionalVariable(const DynamicContext::Ptr &context,
       
    81                                        const Item::Iterator::Ptr &source) const
       
    82 {
       
    83     if(m_positionSlot > -1)
       
    84         context->setPositionIterator(m_positionSlot, source);
       
    85 }
       
    86 
       
    87 Item::Iterator::Ptr ForClause::evaluateSequence(const DynamicContext::Ptr &context) const
       
    88 {
       
    89     const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
       
    90 
       
    91     riggPositionalVariable(context, source);
       
    92 
       
    93     if(m_allowsMany)
       
    94     {
       
    95         return makeSequenceMappingIterator<Item>(ConstPtr(this),
       
    96                                                  source,
       
    97                                                  context);
       
    98     }
       
    99     else
       
   100     {
       
   101         return makeItemMappingIterator<Item>(ConstPtr(this),
       
   102                                              source,
       
   103                                              context);
       
   104     }
       
   105 }
       
   106 
       
   107 Item ForClause::evaluateSingleton(const DynamicContext::Ptr &context) const
       
   108 {
       
   109     return evaluateSequence(context)->next();
       
   110 }
       
   111 
       
   112 void ForClause::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
       
   113 {
       
   114     Item::Iterator::Ptr it;
       
   115     const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
       
   116 
       
   117     riggPositionalVariable(context, source);
       
   118 
       
   119     Item next(source->next());
       
   120 
       
   121     while(next)
       
   122     {
       
   123         context->setRangeVariable(m_varSlot, next);
       
   124         m_operand2->evaluateToSequenceReceiver(context);
       
   125         next = source->next();
       
   126     }
       
   127 }
       
   128 
       
   129 Expression::Ptr ForClause::typeCheck(const StaticContext::Ptr &context,
       
   130                                      const SequenceType::Ptr &reqType)
       
   131 {
       
   132     const Expression::Ptr me(PairContainer::typeCheck(context, reqType));
       
   133     const Cardinality card(m_operand1->staticType()->cardinality());
       
   134 
       
   135     /* If our source is empty we will always evaluate to the empty sequence, so rewrite. */
       
   136     if(card.isEmpty())
       
   137         return EmptySequence::create(this, context);
       
   138     else
       
   139         return me;
       
   140 
       
   141     /* This breaks because  the variable references haven't rewritten themselves, so
       
   142      * they dangle. When this is fixed, evaluateSingleton can be removed. */
       
   143     /*
       
   144     else if(card->allowsMany())
       
   145         return me;
       
   146     else
       
   147         return m_operand2;
       
   148         */
       
   149 }
       
   150 
       
   151 Expression::Ptr ForClause::compress(const StaticContext::Ptr &context)
       
   152 {
       
   153     const Expression::Ptr me(PairContainer::compress(context));
       
   154     if(me != this)
       
   155         return me;
       
   156 
       
   157     /* This is done after calling PairContainer::typeCheck(). The advantage of this is that
       
   158      * m_allowsMany is updated according to what the operand says after it has compressed. However,
       
   159      * if it was initialized to false(as it once was..), ForClause::evaluateSequence()
       
   160      * would potentially have been called by PairContainer::compress(), and it would have
       
   161      * used an unsafe branch. */
       
   162     m_allowsMany = m_operand2->staticType()->cardinality().allowsMany();
       
   163 
       
   164     return me;
       
   165 }
       
   166 
       
   167 SequenceType::Ptr ForClause::staticType() const
       
   168 {
       
   169     const SequenceType::Ptr returnType(m_operand2->staticType());
       
   170 
       
   171     return makeGenericSequenceType(returnType->itemType(),
       
   172                                    m_operand1->staticType()->cardinality()
       
   173                                         * /* multiply operator */
       
   174                                    returnType->cardinality());
       
   175 }
       
   176 
       
   177 SequenceType::List ForClause::expectedOperandTypes() const
       
   178 {
       
   179     SequenceType::List result;
       
   180     result.append(CommonSequenceTypes::ZeroOrMoreItems);
       
   181     result.append(CommonSequenceTypes::ZeroOrMoreItems);
       
   182     return result;
       
   183 }
       
   184 
       
   185 ExpressionVisitorResult::Ptr ForClause::accept(const ExpressionVisitor::Ptr &visitor) const
       
   186 {
       
   187     return visitor->visit(this);
       
   188 }
       
   189 
       
   190 OptimizationPass::List ForClause::optimizationPasses() const
       
   191 {
       
   192     return OptimizationPasses::forPasses;
       
   193 }
       
   194 
       
   195 Expression::ID ForClause::id() const
       
   196 {
       
   197     return IDForClause;
       
   198 }
       
   199 
       
   200 QT_END_NAMESPACE