File: expression.cpp

package info (click to toggle)
khtml 5.78.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 19,012 kB
  • sloc: cpp: 206,167; java: 4,060; ansic: 2,829; perl: 2,317; yacc: 1,497; python: 339; sh: 116; xml: 37; makefile: 7
file content (297 lines) | stat: -rw-r--r-- 6,729 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * expression.cc - Copyright 2005 Frerich Raabe <raabe@kde.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "expression.h"
#include "xml/dom_nodeimpl.h"
#include "xml/dom_nodelistimpl.h"
#include "dom/dom_exception.h"
#include "dom/dom3_xpath.h"
//#include "xml/dom_stringimpl.h"

#include <cmath>

using namespace DOM;
using namespace khtml;
using namespace khtml::XPath;
using namespace std;

// Use KJS's numeric FP stuff
#include "kjs/JSImmediate.h"
#include "kjs/operations.h"

Value::Value():
    m_type(Number),
    m_number(KJS::NaN)
{
    // ### remove eventually
}

Value::Value(NodeImpl *value)
    : m_type(Nodeset)
{
    m_nodeset = new StaticNodeListImpl;
    m_nodeset->append(value);
}

Value::Value(const DomNodeList &value)
    : m_type(Nodeset),
      m_nodeset(value)
{
}

Value::Value(bool value)
    : m_type(Boolean),
      m_bool(value)
{
}

Value::Value(double value)
    : m_type(Number),
      m_number(value)
{
}

Value::Value(const DOMString &value)
    : m_type(String),
      m_string(value)
{
}

Value::Type Value::type() const
{
    return m_type;
}

bool Value::isNodeset() const
{
    return m_type == Nodeset;
}

bool Value::isBoolean() const
{
    return m_type == Boolean;
}

bool Value::isNumber() const
{
    return m_type == Number;
}

bool Value::isString() const
{
    return m_type == String;
}

DomNodeList &Value::toNodeset()
{
    if (m_type != Nodeset) {
        qCWarning(KHTML_LOG) << "Cannot convert anything to a nodeset.";
    }
    return m_nodeset;
}

const DomNodeList &Value::toNodeset() const
{
    if (m_type != Nodeset) {
        qCWarning(KHTML_LOG) << "Cannot convert anything to a nodeset.";
    }
    return m_nodeset;
}

bool Value::toBoolean() const
{
    switch (m_type) {
    case Nodeset:
        return m_nodeset && m_nodeset->length() != 0;
    case Boolean:
        return m_bool;
    case Number:
        return m_number != 0;
    case String:
        return !m_string.isEmpty();
    }
    return bool();
}

double Value::toNumber() const
{
    switch (m_type) {
    case Nodeset:
        return Value(toString()).toNumber();
    case Number:
        return m_number;
    case String: {
        bool canConvert;
        QString s = m_string.string().simplified();
        double value = s.toDouble(&canConvert);
        if (canConvert) {
            return value;
        } else {
            return KJS::NaN;
        }
    }
    case Boolean:
        return m_bool ? 1 : 0;
    }
    return double();
}

DOMString Value::toString() const
{
    switch (m_type) {
    case Nodeset:
        if (m_nodeset && m_nodeset->length() == 0) {
            return DOMString("");
        }
        m_nodeset->normalizeUpto(StaticNodeListImpl::AxisOrder);
        return stringValue(m_nodeset->item(0));
    case String:
        return m_string;
    case Number:
        if (KJS::isNaN(m_number)) {
            return DOMString("NaN");
        } else if (m_number == 0) {
            return DOMString("0");
        } else if (KJS::isInf(m_number)) {
            if (signbit(m_number) == 0) {
                return DOMString("Infinity");
            } else {
                return DOMString("-Infinity");
            }
        }
        return QString::number(m_number);
    case Boolean:
        return m_bool ? DOMString("true")
               : DOMString("false");
    }
    return DOMString();
}

QString Value::dump() const
{
    QString s = "<value type=\"";
    switch (m_type) {
    case Nodeset:
        s += "nodeset";
        break;
    case String:
        s += "string";
        break;
    case Number:
        s += "number";
        break;
    case Boolean:
        s += "boolean";
        break;
    };
    s += "\">" + toString().string() + "</value>";
    return s;
}

EvaluationContext &Expression::evaluationContext()
{
    static EvaluationContext evaluationContext;
    return evaluationContext;
}

Expression::Expression()
    : m_constantValue(nullptr)
{
}

Expression::~Expression()
{
    qDeleteAll(m_subExpressions);
    delete m_constantValue;
}

Value Expression::evaluate() const
{
    if (m_constantValue) {
        return *m_constantValue;
    }
    return doEvaluate();
}

void Expression::addSubExpression(Expression *expr)
{
    m_subExpressions.append(expr);
}

void Expression::optimize()
{
    bool allSubExpressionsConstant = true;
    foreach (Expression *expr, m_subExpressions) {
        if (expr->isConstant()) {
            expr->optimize();
        } else {
            allSubExpressionsConstant = false;
        }
    }

    if (allSubExpressionsConstant) {
        m_constantValue = new Value(doEvaluate());

        qDeleteAll(m_subExpressions);
        m_subExpressions.clear();
    }
}

unsigned int Expression::subExprCount() const
{
    return m_subExpressions.count();
}

Expression *Expression::subExpr(unsigned int i)
{
    Q_ASSERT(i < subExprCount());
    return m_subExpressions.at(i);
}

const Expression *Expression::subExpr(unsigned int i) const
{
    Q_ASSERT(i < subExprCount());
    return m_subExpressions.at(i);
}

bool Expression::isConstant() const
{
    foreach (Expression *expr, m_subExpressions) {
        if (!expr->isConstant()) {
            return false;
        }
    }
    return true;
}

void Expression::reportInvalidExpressionErr()
{
    Expression::evaluationContext().reportException(XPathException::toCode(INVALID_EXPRESSION_ERR));
}

void Expression::reportNamespaceErr()
{
    Expression::evaluationContext().reportException(DOMException::NAMESPACE_ERR);
}