src/xmlpatterns/expr/qoptimizationpasses.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 "qoptimizerblocks_p.h"
       
    44 
       
    45 #include "qoptimizationpasses_p.h"
       
    46 
       
    47 QT_BEGIN_NAMESPACE
       
    48 
       
    49 using namespace QPatternist;
       
    50 
       
    51 OptimizationPass::List OptimizationPasses::comparisonPasses;
       
    52 OptimizationPass::List OptimizationPasses::forPasses;
       
    53 OptimizationPass::List OptimizationPasses::ifThenPasses;
       
    54 OptimizationPass::List OptimizationPasses::notFN;
       
    55 
       
    56 void OptimizationPasses::Coordinator::init()
       
    57 {
       
    58     static bool isInitialized = false; // STATIC DATA
       
    59 
       
    60     if(isInitialized)
       
    61         return;
       
    62 
       
    63     isInitialized = true;
       
    64 
       
    65     /* Note, below is many of the building blocks re-used in several passes
       
    66      * in order to reduce memory use. Thus, when changing one building block
       
    67      * it potentially affects many passes. */
       
    68 
       
    69     /* ****************************************************** */
       
    70     /* Rewrite "count(<expr>) ge 1" into "exists(<expr>)" */
       
    71     OptimizationPass::ExpressionMarker firstFirstChild;
       
    72     firstFirstChild.append(0);
       
    73     firstFirstChild.append(0);
       
    74 
       
    75     ExpressionIdentifier::List geOpIDs;
       
    76     const ExpressionIdentifier::Ptr countFN(new ByIDIdentifier(Expression::IDCountFN));
       
    77     geOpIDs.append(countFN);
       
    78     geOpIDs.append(ExpressionIdentifier::Ptr(new IntegerIdentifier(1)));
       
    79 
       
    80     QVector<Expression::ID> geMatcher;
       
    81     geMatcher.append(Expression::IDValueComparison);
       
    82     geMatcher.append(Expression::IDGeneralComparison);
       
    83 
       
    84     const ExpressionIdentifier::Ptr ge(new ComparisonIdentifier(geMatcher,
       
    85                                                                 AtomicComparator::OperatorGreaterOrEqual));
       
    86 
       
    87     const ExpressionCreator::Ptr existsFN(new ByIDCreator(Expression::IDExistsFN));
       
    88     const OptimizationPass::Ptr geToExists(new OptimizationPass(ge, geOpIDs, firstFirstChild, existsFN));
       
    89     comparisonPasses.append(geToExists);
       
    90     /* ****************************************************** */
       
    91 
       
    92     /* ****************************************************** */
       
    93     /* Rewrite "count(<expr>) gt 0" into "exists(<expr>)" */
       
    94     ExpressionIdentifier::List countAndIntZero;
       
    95     countAndIntZero.append(countFN);
       
    96     const ExpressionIdentifier::Ptr zeroInteger(new IntegerIdentifier(0));
       
    97     countAndIntZero.append(zeroInteger);
       
    98 
       
    99     const ExpressionIdentifier::Ptr gt(new ComparisonIdentifier(geMatcher,
       
   100                                                                 AtomicComparator::OperatorGreaterThan));
       
   101 
       
   102     const OptimizationPass::Ptr gtToExists(new OptimizationPass(gt, countAndIntZero,
       
   103                                                                 firstFirstChild, existsFN));
       
   104     comparisonPasses.append(gtToExists);
       
   105     /* ****************************************************** */
       
   106 
       
   107     /* ****************************************************** */
       
   108     /* Rewrite "count(<expr>) ne 0" into "exists(<expr>)" */
       
   109 
       
   110     const ExpressionIdentifier::Ptr ne(new ComparisonIdentifier(geMatcher,
       
   111                                                                 AtomicComparator::OperatorNotEqual));
       
   112     const OptimizationPass::Ptr neToExists(new OptimizationPass(ne, countAndIntZero, firstFirstChild,
       
   113                                                                 existsFN,
       
   114                                                                 OptimizationPass::AnyOrder));
       
   115     comparisonPasses.append(neToExists);
       
   116     /* ****************************************************** */
       
   117 
       
   118     /* ****************************************************** */
       
   119     /* Rewrite "count(<expr>) eq 0" into "empty(<expr>)" */
       
   120     ExpressionIdentifier::List eqOpIDs;
       
   121     eqOpIDs.append(countFN);
       
   122     eqOpIDs.append(zeroInteger);
       
   123     const ExpressionCreator::Ptr emptyFN(new ByIDCreator(Expression::IDEmptyFN));
       
   124     const ExpressionIdentifier::Ptr eq(new ComparisonIdentifier(geMatcher,
       
   125                                                                 AtomicComparator::OperatorEqual));
       
   126     const OptimizationPass::Ptr eqToEmpty(new OptimizationPass(eq, eqOpIDs, firstFirstChild,
       
   127                                                                emptyFN,
       
   128                                                                OptimizationPass::AnyOrder));
       
   129     comparisonPasses.append(eqToEmpty);
       
   130 
       
   131     /* ****************************************************** */
       
   132 
       
   133     /* ****************************************************** */
       
   134     /* Rewrite "for $var in <expr> return $var" into "<expr>" */
       
   135     ExpressionIdentifier::List forOps;
       
   136     OptimizationPass::ExpressionMarker firstChild;
       
   137     firstChild.append(0);
       
   138 
       
   139     forOps.append(ExpressionIdentifier::Ptr());
       
   140     forOps.append(ExpressionIdentifier::Ptr(new ByIDIdentifier(Expression::IDRangeVariableReference)));
       
   141     const OptimizationPass::Ptr simplifyFor(new OptimizationPass(ExpressionIdentifier::Ptr(), forOps,
       
   142                                                                  firstChild, ExpressionCreator::Ptr()));
       
   143     forPasses.append(simplifyFor);
       
   144     /* ****************************************************** */
       
   145 
       
   146     /* ****************************************************** */
       
   147     /* Rewrite "if(<expr>) then true() else false()" to "<expr>" */
       
   148     OptimizationPass::ExpressionMarker marker;
       
   149     marker.append(0);
       
   150 
       
   151     ExpressionIdentifier::List opIDs;
       
   152     opIDs.append(ExpressionIdentifier::Ptr(new BySequenceTypeIdentifier(
       
   153                         CommonSequenceTypes::ExactlyOneBoolean)));
       
   154     opIDs.append(ExpressionIdentifier::Ptr(new BooleanIdentifier(true)));
       
   155     opIDs.append(ExpressionIdentifier::Ptr(new BooleanIdentifier(false)));
       
   156 
       
   157     const OptimizationPass::Ptr pass(new OptimizationPass(ExpressionIdentifier::Ptr(), opIDs, marker));
       
   158     ifThenPasses.append(pass);
       
   159     /* ****************************************************** */
       
   160 
       
   161     /* ****************************************************** */
       
   162     /* Rewrite "not(exists(X))" into "empty(X)" */
       
   163     ExpressionIdentifier::List idExistsFN;
       
   164     idExistsFN.append(ExpressionIdentifier::Ptr(new ByIDIdentifier(Expression::IDExistsFN)));
       
   165 
       
   166     notFN.append(OptimizationPass::Ptr(new OptimizationPass(ExpressionIdentifier::Ptr(),
       
   167                                                             idExistsFN,
       
   168                                                             firstFirstChild,
       
   169                                                             emptyFN)));
       
   170 
       
   171     /* Rewrite "not(empty(X))" into "exists(X)" */
       
   172     ExpressionIdentifier::List idEmptyFN;
       
   173     idEmptyFN.append(ExpressionIdentifier::Ptr(new ByIDIdentifier(Expression::IDEmptyFN)));
       
   174 
       
   175     notFN.append(OptimizationPass::Ptr(new OptimizationPass(ExpressionIdentifier::Ptr(),
       
   176                                                             idEmptyFN,
       
   177                                                             firstFirstChild,
       
   178                                                             existsFN)));
       
   179     /* ****************************************************** */
       
   180 }
       
   181 
       
   182 QT_END_NAMESPACE