| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | //
|
|---|
| 43 | // W A R N I N G
|
|---|
| 44 | // -------------
|
|---|
| 45 | //
|
|---|
| 46 | // This file is not part of the Qt API. It exists purely as an
|
|---|
| 47 | // implementation detail. This header file may change from version to
|
|---|
| 48 | // version without notice, or even be removed.
|
|---|
| 49 | //
|
|---|
| 50 | // We mean it.
|
|---|
| 51 |
|
|---|
| 52 | #ifndef Patternist_Expression_H
|
|---|
| 53 | #define Patternist_Expression_H
|
|---|
| 54 |
|
|---|
| 55 | #include <QFlags>
|
|---|
| 56 | #include <QSharedData>
|
|---|
| 57 |
|
|---|
| 58 | #include "qcppcastinghelper_p.h"
|
|---|
| 59 | #include "qdebug_p.h"
|
|---|
| 60 | #include "qdynamiccontext_p.h"
|
|---|
| 61 | #include "qexpressiondispatch_p.h"
|
|---|
| 62 | #include "qitem_p.h"
|
|---|
| 63 | #include "qsequencetype_p.h"
|
|---|
| 64 | #include "qsourcelocationreflection_p.h"
|
|---|
| 65 | #include "qstaticcontext_p.h"
|
|---|
| 66 |
|
|---|
| 67 | QT_BEGIN_HEADER
|
|---|
| 68 |
|
|---|
| 69 | QT_BEGIN_NAMESPACE
|
|---|
| 70 |
|
|---|
| 71 | template<typename T> class QList;
|
|---|
| 72 | template<typename T> class QVector;
|
|---|
| 73 |
|
|---|
| 74 | namespace QPatternist
|
|---|
| 75 | {
|
|---|
| 76 | template<typename T, typename ListType> class ListIterator;
|
|---|
| 77 | class OptimizationPass;
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * @short Base class for all AST nodes in an XPath/XQuery/XSL-T expression.
|
|---|
| 81 | *
|
|---|
| 82 | * @section ExpressionCreation Expression Compilation
|
|---|
| 83 | *
|
|---|
| 84 | * @subsection ExpressionCreationParser The process of creating an Expression
|
|---|
| 85 | *
|
|---|
| 86 | * The initial step of creating an internal representation(in some circles
|
|---|
| 87 | * called an IR tree) of the XPath string follows classic compiler design: a scanner
|
|---|
| 88 | * is invoked, resulting in tokens, which sub-sequently are consumed by a parser
|
|---|
| 89 | * which groups the tokens into rules, resulting in the creation of
|
|---|
| 90 | * Abstract Syntax Tree(AST) nodes that are arranged in a hierarchical structure
|
|---|
| 91 | * similar to the EBNF.
|
|---|
| 92 | *
|
|---|
| 93 | * More specifically, ExpressionFactory::createExpression() is called with a
|
|---|
| 94 | * pointer to a static context, and the string for the expression. This is subsequently
|
|---|
| 95 | * tokenized by a Flex scanner. Mistakes detected at this stage is syntax
|
|---|
| 96 | * errors, as well as a few semantical errors. Syntax errors can be divided
|
|---|
| 97 | * in two types:
|
|---|
| 98 | *
|
|---|
| 99 | * - The scanner detects it. An example is the expression "23Eb3" which
|
|---|
| 100 | * is not a valid number literal, or "1prefix:my-element" which is not a
|
|---|
| 101 | * valid QName.
|
|---|
| 102 | * - The parser detects it. This means a syntax error at a
|
|---|
| 103 | * higher level, that a group of tokens couldn't be reduced to a
|
|---|
| 104 | * rule(expression). An example is the expression "if(a = b) 'match' else
|
|---|
| 105 | * 'no match'"; the tokenizer would handle it fine, but the parser would
|
|---|
| 106 | * fail because the tokens could not be reduced to a rule due to the token
|
|---|
| 107 | * for the "then" word was missing.
|
|---|
| 108 | *
|
|---|
| 109 | * Apart from the syntax errors, the actions in the parser also detects
|
|---|
| 110 | * errors when creating the corresponding expressions. This is for example
|
|---|
| 111 | * that no namespace binding for a prefix could be found, or that a function
|
|---|
| 112 | * call was used which no function implementation could be found for.
|
|---|
| 113 | *
|
|---|
| 114 | * When the parser has finished, the result is an AST. That is, a
|
|---|
| 115 | * hierarchical structure consisting of Expression sub-classes. The
|
|---|
| 116 | * individual expressions haven't at this point done anything beyond
|
|---|
| 117 | * receiving their child-expressions(if any), and hence reminds of a
|
|---|
| 118 | * "construction scaffold". In other words, a tree for the expression
|
|---|
| 119 | * <tt>'string' + 1 and xs:date('2001-03-13')</tt> could have been created, even if
|
|---|
| 120 | * that expression contains errors(one can't add a xs:integer to a xs:string,
|
|---|
| 121 | * and the Effective %Boolean Value cannot be extracted for date types).
|
|---|
| 122 | *
|
|---|
| 123 | * @subsection ExpressionCreationTypeChecking Type Checking
|
|---|
| 124 | *
|
|---|
| 125 | * After the AST creation, ExpressionFactory::createExpression continues with
|
|---|
| 126 | * calling the AST node(which is an Expression instance)'s typeCheck()
|
|---|
| 127 | * function. This step ensures that the static types of the operands matches
|
|---|
| 128 | * the operators, and in the cases where it doesn't, modifies the AST such
|
|---|
| 129 | * that the necessary conversions are done -- if possible, otherwise the
|
|---|
| 130 | * result is a type error.
|
|---|
| 131 | *
|
|---|
| 132 | *
|
|---|
| 133 | * This step corresponds roughly to what <a
|
|---|
| 134 | * href="http://www.w3.org/TR/xpath20/#id-static-analysis">2.2.3.1 Static Analysis Phase</a>
|
|---|
| 135 | * labels operation tree normalization; step SQ5.
|
|---|
| 136 | *
|
|---|
| 137 | * @subsection ExpressionCreationCompression Compressing -- Optimization and Fixup
|
|---|
| 138 | *
|
|---|
| 139 | * The last step is calling compress(). This function is not called
|
|---|
| 140 | * 'optimize', 'simplify' or the like, because although it performs all
|
|---|
| 141 | * optimization, it also involves mandatory stages.
|
|---|
| 142 | *
|
|---|
| 143 | * One such is const folding, which while being an efficient optimization,
|
|---|
| 144 | * also is a necessity for many XSL-T constructs. Another important step is
|
|---|
| 145 | * that functions which had an evaluation dependency on the static context(as
|
|---|
| 146 | * opposed to the dynamic) performs their "fixup".
|
|---|
| 147 | *
|
|---|
| 148 | * In other words, this stage potentially performs AST re-writes. For example,
|
|---|
| 149 | * the expression <tt>3 + 3, concat('foo', '-', 'bar'), true() and false()</tt> would
|
|---|
| 150 | * result in an AST corresponding to <tt>6, 'foo-bar', false()</tt>. This process
|
|---|
| 151 | * is done backwards; each expression asks its operands to compress before it
|
|---|
| 152 | * performs its own compression(and so forth, until the root expression's call
|
|---|
| 153 | * returns to the caller).
|
|---|
| 154 | *
|
|---|
| 155 | * @see <a href="http://www.w3.org/TR/xpath20/#id-errors-and-opt">XML Path Language
|
|---|
| 156 | * (XPath) 2.0, 2.3.4 Errors and Optimization</a>
|
|---|
| 157 | * @see <a href="http://www.w3.org/TR/xpath20/#id-expression-processing">XML Path
|
|---|
| 158 | * Language (XPath) 2.0, 2.2.3 Expression Processing</a>
|
|---|
| 159 | * @see <a href="http://www.w3.org/TR/xquery-xpath-parsing/">Building a Tokenizer
|
|---|
| 160 | * for XPath or XQuery</a>
|
|---|
| 161 | * @see ExpressionFactory
|
|---|
| 162 | * @author Frans Englich <[email protected]>
|
|---|
| 163 | * @ingroup Patternist_expressions
|
|---|
|
|---|