qv4irbuilder.cpp 37.9 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/
5
**
6
** This file is part of the QtQml module of the Qt Toolkit.
7 8 9
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
10 11 12 13 14 15
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 17
**
** In addition, as a special exception, Nokia gives you certain additional
18
** rights. These rights are described in the Nokia Qt LGPL Exception
19 20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
21 22 23 24 25 26 27
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
28
**
29 30 31
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
32 33 34 35 36
**
**
**
**
**
37
**
38 39 40 41
** $QT_END_LICENSE$
**
****************************************************************************/

42 43
#include "qv4irbuilder_p.h"
#include "qv4compiler_p_p.h"
44

45 46 47
#include <private/qqmlglobal_p.h>
#include <private/qqmlmetatype_p.h>
#include <private/qqmltypenamecache_p.h>
48 49 50 51 52

DEFINE_BOOL_CONFIG_OPTION(qmlVerboseCompiler, QML_VERBOSE_COMPILER)

QT_BEGIN_NAMESPACE

53
using namespace QQmlJS;
54

55
static IR::Type irTypeFromVariantType(int t, QQmlEnginePrivate *engine, const QMetaObject * /* meta */)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
{
    switch (t) {
    case QMetaType::Bool:
        return IR::BoolType;

    case QMetaType::Int:
        return IR::IntType;

    case QMetaType::QReal:
        return IR::RealType;

    case QMetaType::QString:
        return IR::StringType;

    case QMetaType::QUrl:
        return IR::UrlType;

73 74 75
    case QMetaType::QColor:
        return IR::ColorType;

76
    default:
77
        if (t == QQmlMetaType::QQuickAnchorLineMetaTypeId()) {
78
            return IR::SGAnchorLineType;
79
        } else if (engine->metaObjectForType(t)) {
80 81 82 83 84 85 86
            return IR::ObjectType;
        }

        return IR::InvalidType;
    }
}

87
QV4IRBuilder::QV4IRBuilder(const QV4Compiler::Expression *expr, 
88
                                                             QQmlEnginePrivate *engine)
89
: m_expression(expr), m_engine(engine), _function(0), _block(0), _discard(false)
90 91 92
{
}

93 94
bool QV4IRBuilder::operator()(QQmlJS::IR::Function *function,
                                         QQmlJS::AST::Node *ast)
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
{
    bool discarded = false;

    IR::BasicBlock *block = function->newBasicBlock();

    qSwap(_discard, discarded);
    qSwap(_function, function);
    qSwap(_block, block);

    ExprResult r;
    AST::SourceLocation location;
    if (AST::ExpressionNode *asExpr = ast->expressionCast()) {
        r = expression(asExpr);
        location = asExpr->firstSourceLocation();
    } else if (AST::Statement *asStmt = ast->statementCast()) {
        r = statement(asStmt);
        location = asStmt->firstSourceLocation();
    }

    //_block->MOVE(_block->TEMP(IR::InvalidType), r.code);
    if (r.code) {
        const QMetaObject *m = 0;
        const IR::Type targetType = irTypeFromVariantType(m_expression->property->type, m_engine, m);
        if (targetType != r.type()) {
            IR::Expr *x = _block->TEMP(targetType);
            _block->MOVE(x, r, true);
            r.code = x;
        }
        _block->RET(r.code, targetType, location.startLine, location.startColumn);
    }

    qSwap(_block, block);
    qSwap(_function, function);
    qSwap(_discard, discarded);

130
    return !discarded;
131 132
}

133
bool QV4IRBuilder::buildName(QList<QStringRef> &name,
134 135 136 137
                                              AST::Node *node,
                                              QList<AST::ExpressionNode *> *nodes)
{
    if (node->kind == AST::Node::Kind_IdentifierExpression) {
138
        name << static_cast<AST::IdentifierExpression*>(node)->name;
139 140 141 142 143 144 145 146
        if (nodes) *nodes << static_cast<AST::IdentifierExpression*>(node);
    } else if (node->kind == AST::Node::Kind_FieldMemberExpression) {
        AST::FieldMemberExpression *expr =
            static_cast<AST::FieldMemberExpression *>(node);

        if (!buildName(name, expr->base, nodes))
            return false;

147
        name << expr->name;
148 149 150 151 152 153 154 155
        if (nodes) *nodes << expr;
    } else {
        return false;
    }

    return true;
}

156
void QV4IRBuilder::discard() 
157 158 159 160
{ 
    _discard = true; 
}

161 162
QV4IRBuilder::ExprResult 
QV4IRBuilder::expression(AST::ExpressionNode *ast)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
{
    ExprResult r;
    if (ast) {
        qSwap(_expr, r);
        accept(ast);
        qSwap(_expr, r);

        if (r.is(IR::InvalidType))
            discard();
        else {
            Q_ASSERT(r.hint == r.format);
        }
    }

    return r;
}

180
void QV4IRBuilder::condition(AST::ExpressionNode *ast, IR::BasicBlock *iftrue, IR::BasicBlock *iffalse)
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
{
    if (! ast)
        return;
    ExprResult r(iftrue, iffalse);
    qSwap(_expr, r);
    accept(ast);
    qSwap(_expr, r);

    if (r.format != ExprResult::cx) {
        if (! r.code)
            discard();

        Q_ASSERT(r.hint == ExprResult::cx);
        Q_ASSERT(r.format == ExprResult::ex);

        if (r.type() != IR::BoolType) {
            IR::Temp *t = _block->TEMP(IR::BoolType);
            _block->MOVE(t, r);
            r = t;
        }

        _block->CJUMP(_block->UNOP(IR::OpIfTrue, r), iftrue, iffalse);
    }
}

206 207
QV4IRBuilder::ExprResult
QV4IRBuilder::statement(AST::Statement *ast)
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
{
    ExprResult r;
    if (ast) {
        qSwap(_expr, r);
        accept(ast);
        qSwap(_expr, r);

        if (r.is(IR::InvalidType))
            discard();
        else {
            Q_ASSERT(r.hint == r.format);
        }
    }

    return r;
}

225
void QV4IRBuilder::sourceElement(AST::SourceElement *ast)
226 227 228 229
{
    accept(ast);
}

230
void QV4IRBuilder::implicitCvt(ExprResult &expr, IR::Type type)
231 232 233 234 235 236 237 238 239 240
{
    if (expr.type() == type)
        return; // nothing to do

    IR::Expr *x = _block->TEMP(type);
    _block->MOVE(x, expr.code);
    expr.code = x;
}

// QML
241
bool QV4IRBuilder::visit(AST::UiProgram *)
242 243 244 245 246
{
    Q_ASSERT(!"unreachable");
    return false;
}

247
bool QV4IRBuilder::visit(AST::UiImportList *)
248 249 250 251 252
{
    Q_ASSERT(!"unreachable");
    return false;
}

253
bool QV4IRBuilder::visit(AST::UiImport *)
254 255 256 257 258
{
    Q_ASSERT(!"unreachable");
    return false;
}

259
bool QV4IRBuilder::visit(AST::UiPublicMember *)
260 261 262 263 264
{
    Q_ASSERT(!"unreachable");
    return false;
}

265
bool QV4IRBuilder::visit(AST::UiSourceElement *)
266 267 268 269 270
{
    Q_ASSERT(!"unreachable");
    return false;
}

271
bool QV4IRBuilder::visit(AST::UiObjectDefinition *)
272 273 274 275 276
{
    Q_ASSERT(!"unreachable");
    return false;
}

277
bool QV4IRBuilder::visit(AST::UiObjectInitializer *)
278 279 280 281 282
{
    Q_ASSERT(!"unreachable");
    return false;
}

283
bool QV4IRBuilder::visit(AST::UiObjectBinding *)
284 285 286 287 288
{
    Q_ASSERT(!"unreachable");
    return false;
}

289
bool QV4IRBuilder::visit(AST::UiScriptBinding *)
290 291 292 293 294
{
    Q_ASSERT(!"unreachable");
    return false;
}

295
bool QV4IRBuilder::visit(AST::UiArrayBinding *)
296 297 298 299 300
{
    Q_ASSERT(!"unreachable");
    return false;
}

301
bool QV4IRBuilder::visit(AST::UiObjectMemberList *)
302 303 304 305 306
{
    Q_ASSERT(!"unreachable");
    return false;
}

307
bool QV4IRBuilder::visit(AST::UiArrayMemberList *)
308 309 310 311 312
{
    Q_ASSERT(!"unreachable");
    return false;
}

313
bool QV4IRBuilder::visit(AST::UiQualifiedId *)
314 315 316 317 318 319 320
{
    Q_ASSERT(!"unreachable");
    return false;
}


// JS
321
bool QV4IRBuilder::visit(AST::Program *)
322
{
323
    Q_ASSERT(!"unreachable");
324 325 326
    return false;
}

327
bool QV4IRBuilder::visit(AST::SourceElements *)
328
{
329
    Q_ASSERT(!"unreachable");
330 331 332
    return false;
}

333
bool QV4IRBuilder::visit(AST::FunctionSourceElement *)
334
{
335 336
    Q_ASSERT(!"unreachable");
    return false;
337 338
}

339
bool QV4IRBuilder::visit(AST::StatementSourceElement *)
340
{
341 342
    Q_ASSERT(!"unreachable");
    return false;
343 344 345
}

// object literals
346
bool QV4IRBuilder::visit(AST::PropertyNameAndValueList *)
347 348 349 350 351
{
    Q_ASSERT(!"unreachable");
    return false;
}

352
bool QV4IRBuilder::visit(AST::IdentifierPropertyName *)
353 354 355 356 357
{
    Q_ASSERT(!"unreachable");
    return false;
}

358
bool QV4IRBuilder::visit(AST::StringLiteralPropertyName *)
359 360 361 362 363
{
    Q_ASSERT(!"unreachable");
    return false;
}

364
bool QV4IRBuilder::visit(AST::NumericLiteralPropertyName *)
365 366 367 368 369 370 371
{
    Q_ASSERT(!"unreachable");
    return false;
}


// array literals
372
bool QV4IRBuilder::visit(AST::ElementList *)
373 374 375 376 377
{
    Q_ASSERT(!"unreachable");
    return false;
}

378
bool QV4IRBuilder::visit(AST::Elision *)
379 380 381 382 383 384 385
{
    Q_ASSERT(!"unreachable");
    return false;
}


// function calls
386
bool QV4IRBuilder::visit(AST::ArgumentList *)
387 388 389 390 391 392
{
    Q_ASSERT(!"unreachable");
    return false;
}

// expressions
393
bool QV4IRBuilder::visit(AST::ObjectLiteral *)
394 395 396 397
{
    return false;
}

398
bool QV4IRBuilder::visit(AST::ArrayLiteral *)
399 400 401 402
{
    return false;
}

403
bool QV4IRBuilder::visit(AST::ThisExpression *)
404 405 406 407
{
    return false;
}

408
bool QV4IRBuilder::visit(AST::IdentifierExpression *ast)
409 410 411 412
{
    const quint32 line = ast->identifierToken.startLine;
    const quint32 column = ast->identifierToken.startColumn;

413
    const QString name = ast->name.toString();
414 415 416

    if (name.at(0) == QLatin1Char('u') && name.length() == 9 && name == QLatin1String("undefined")) {
        _expr.code = _block->CONST(IR::UndefinedType, 0); // ### undefined value
417
    } else if (m_engine->v8engine()->illegalNames().contains(name) ) {
418 419
        if (qmlVerboseCompiler()) qWarning() << "*** illegal symbol:" << name;
        return false;
420
    } else if (const QQmlScript::Object *obj = m_expression->ids->value(name)) {
421 422 423 424 425 426
        IR::Name *code = _block->ID_OBJECT(name, obj, line, column);
        if (obj == m_expression->component)
            code->storage = IR::Name::RootStorage;
        _expr.code = code;
    } else {

427
        QQmlTypeNameCache::Result r = m_expression->importCache->query(name);
428 429 430
        if (r.isValid()) {
            if (r.type) {
                _expr.code = _block->ATTACH_TYPE(name, r.type, IR::Name::ScopeStorage, line, column);
431
            } /*else if (r.importNamespace) {
432 433 434 435 436 437 438 439 440 441
                QQmlMetaType::ModuleApiInstance *moduleApi = m_expression->importCache->moduleApi(r.importNamespace);
                if (moduleApi) {
                    if (moduleApi->qobjectCallback) {
                        moduleApi->qobjectApi = moduleApi->qobjectCallback(QQmlEnginePrivate::get(m_engine), QQmlEnginePrivate::get(m_engine));
                        moduleApi->qobjectCallback = 0;
                        moduleApi->scriptCallback = 0;
                    }
                    if (moduleApi->qobjectApi)
                        _expr.code = _block->MODULE_OBJECT(name, moduleApi->qobjectApi->metaObject(), IR::Name::MemberStorage, line, column);
                }
442
            }*/ //### we can't create the actual QObject here, as we may be running in a thread (can be reenabled once QTBUG-24894 is handled)
443 444 445 446 447 448
            // We don't support anything else
        } else {
            bool found = false;

            if (m_expression->context != m_expression->component) {
                // RootStorage is more efficient than ScopeStorage, so prefer that if they are the same
449
                QQmlPropertyCache *cache = m_expression->context->synthCache;
450 451 452
                const QMetaObject *metaObject = m_expression->context->metaObject();
                if (!cache) cache = m_engine->cache(metaObject);

453
                QQmlPropertyData *data = cache->property(name);
454

455
                if (data && data->hasRevision()) {
456 457 458 459 460 461 462 463
                    if (qmlVerboseCompiler()) 
                        qWarning() << "*** versioned symbol:" << name;
                    discard();
                    return false;
                }

                if (data && !data->isFunction()) {
                    IR::Type irType = irTypeFromVariantType(data->propType, m_engine, metaObject);
464
                    _expr.code = _block->SYMBOL(irType, name, metaObject, data, IR::Name::ScopeStorage, line, column);
465 466
                    found = true;
                } 
467 468
            }

469
            if (!found) {
470
                QQmlPropertyCache *cache = m_expression->component->synthCache;
471 472 473
                const QMetaObject *metaObject = m_expression->component->metaObject();
                if (!cache) cache = m_engine->cache(metaObject);

474
                QQmlPropertyData *data = cache->property(name);
475

476
                if (data && data->hasRevision()) {
477 478 479 480 481 482 483 484
                    if (qmlVerboseCompiler()) 
                        qWarning() << "*** versioned symbol:" << name;
                    discard();
                    return false;
                }

                if (data && !data->isFunction()) {
                    IR::Type irType = irTypeFromVariantType(data->propType, m_engine, metaObject);
485
                    _expr.code = _block->SYMBOL(irType, name, metaObject, data, IR::Name::RootStorage, line, column);
486 487
                    found = true;
                } 
488 489
            }

490 491
            if (!found && qmlVerboseCompiler())
                qWarning() << "*** unknown symbol:" << name;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
        }
    }

    if (_expr.code && _expr.hint == ExprResult::cx) {
        _expr.format = ExprResult::cx;

        if (_expr.type() != IR::BoolType) {
            IR::Temp *t = _block->TEMP(IR::BoolType);
            _block->MOVE(t, _expr);
            _expr.code = t;
        }

        _block->CJUMP(_expr.code, _expr.iftrue, _expr.iffalse);
        _expr.code = 0;
    }

    return false;
}

511
bool QV4IRBuilder::visit(AST::NullExpression *)
512 513 514 515 516 517
{
    // ### TODO: cx format
    _expr.code = _block->CONST(IR::NullType, 0);
    return false;
}

518
bool QV4IRBuilder::visit(AST::TrueLiteral *)
519 520 521 522 523 524
{
    // ### TODO: cx format
    _expr.code = _block->CONST(IR::BoolType, 1);
    return false;
}

525
bool QV4IRBuilder::visit(AST::FalseLiteral *)
526 527 528 529 530 531
{
    // ### TODO: cx format
    _expr.code = _block->CONST(IR::BoolType, 0);
    return false;
}

532
bool QV4IRBuilder::visit(AST::StringLiteral *ast)
533 534
{
    // ### TODO: cx format
535
    _expr.code = _block->STRING(ast->value);
536 537 538
    return false;
}

539
bool QV4IRBuilder::visit(AST::NumericLiteral *ast)
540 541 542 543 544
{
    if (_expr.hint == ExprResult::cx) {
        _expr.format = ExprResult::cx;
        _block->JUMP(ast->value ? _expr.iftrue : _expr.iffalse);
    } else {
545
        _expr.code = _block->CONST(IR::RealType, ast->value);
546 547 548 549
    }
    return false;
}

550
bool QV4IRBuilder::visit(AST::RegExpLiteral *)
551 552 553 554
{
    return false;
}

555
bool QV4IRBuilder::visit(AST::NestedExpression *)
556 557 558 559
{
    return true; // the value of the nested expression
}

560
bool QV4IRBuilder::visit(AST::ArrayMemberExpression *)
561 562 563 564
{
    return false;
}

565
bool QV4IRBuilder::visit(AST::FieldMemberExpression *ast)
566 567 568 569 570 571
{
    if (IR::Expr *left = expression(ast->base)) {
        if (IR::Name *baseName = left->asName()) {
            const quint32 line = ast->identifierToken.startLine;
            const quint32 column = ast->identifierToken.startColumn;

572
            QString name = ast->name.toString();
573 574 575 576 577 578

            switch(baseName->symbol) {
            case IR::Name::Unbound:
                break;

            case IR::Name::AttachType:
579
                if (name.at(0).isUpper()) {
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
                    QByteArray utf8Name = name.toUtf8();
                    const char *enumName = utf8Name.constData();

                    const QMetaObject *meta = baseName->declarativeType->metaObject();
                    bool found = false;
                    for (int ii = 0; !found && ii < meta->enumeratorCount(); ++ii) {
                        QMetaEnum e = meta->enumerator(ii);
                        for (int jj = 0; !found && jj < e.keyCount(); ++jj) {
                            if (0 == strcmp(e.key(jj), enumName)) {
                                found = true;
                                _expr.code = _block->CONST(IR::IntType, e.value(jj));
                            }
                        }
                    }

                    if (!found && qmlVerboseCompiler())
                        qWarning() << "*** unresolved enum:" 
597
                                   << (*baseName->id + QLatin1String(".") + ast->name.toString());
598
                } else if(const QMetaObject *attachedMeta = baseName->declarativeType->attachedPropertiesType()) {
599 600
                    QQmlPropertyCache *cache = m_engine->cache(attachedMeta);
                    QQmlPropertyData *data = cache->property(name);
601

602
                    if (!data || data->isFunction())
603 604
                        return false; // Don't support methods (or non-existing properties ;)

605
                    if(!data->isFinal()) {
606 607
                        if (qmlVerboseCompiler())
                            qWarning() << "*** non-final attached property:"
608
                                       << (*baseName->id + QLatin1String(".") + ast->name.toString());
609 610 611 612
                        return false; // We don't know enough about this property
                    }

                    IR::Type irType = irTypeFromVariantType(data->propType, m_engine, attachedMeta);
613
                    _expr.code = _block->SYMBOL(baseName, irType, name, attachedMeta, data, line, column);
614 615 616
                }
                break;

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
            case IR::Name::ModuleObject: {
                if (name.at(0).isUpper()) {
                    QByteArray utf8Name = name.toUtf8();
                    const char *enumName = utf8Name.constData();

                    const QMetaObject *meta = baseName->meta;
                    bool found = false;
                    for (int ii = 0; !found && ii < meta->enumeratorCount(); ++ii) {
                        QMetaEnum e = meta->enumerator(ii);
                        for (int jj = 0; !found && jj < e.keyCount(); ++jj) {
                            if (0 == strcmp(e.key(jj), enumName)) {
                                found = true;
                                _expr.code = _block->CONST(IR::IntType, e.value(jj));
                            }
                        }
                    }
                    if (!found && qmlVerboseCompiler())
                        qWarning() << "*** unresolved enum:"
                                   << (*baseName->id + QLatin1String(".") + ast->name.toString());
                } else {
                    QQmlPropertyCache *cache = m_engine->cache(baseName->meta);
                    if (!cache) return false;
                    QQmlPropertyData *data = cache->property(name);

                    if (!data || data->isFunction())
                        return false; // Don't support methods (or non-existing properties ;)

                    if (!data->isFinal()) {
                        if (qmlVerboseCompiler())
                            qWarning() << "*** non-final attached property:"
                                       << (*baseName->id + QLatin1String(".") + ast->name.toString());
                        return false; // We don't know enough about this property
                    }

                    IR::Type irType = irTypeFromVariantType(data->propType, m_engine, baseName->meta);
                    _expr.code = _block->SYMBOL(baseName, irType, name, baseName->meta, data, line, column);
                }
            }
            break;

657
            case IR::Name::IdObject: {
658 659
                const QQmlScript::Object *idObject = baseName->idObject;
                QQmlPropertyCache *cache = 
660 661
                    idObject->synthCache?idObject->synthCache:m_engine->cache(idObject->metaObject());

662
                QQmlPropertyData *data = cache->property(name);
663

664
                if (!data || data->isFunction())
665 666
                    return false; // Don't support methods (or non-existing properties ;)

667
                if (data->hasRevision()) {
668 669 670 671 672 673 674 675
                    if (qmlVerboseCompiler()) 
                        qWarning() << "*** versioned symbol:" << name;
                    discard();
                    return false;
                }

                IR::Type irType = irTypeFromVariantType(data->propType, m_engine, idObject->metaObject());
                _expr.code = _block->SYMBOL(baseName, irType, name,
676
                                            idObject->metaObject(), data, line, column);
677 678 679 680
                }
                break;

            case IR::Name::Property: 
681
                if (baseName->type == IR::ObjectType && baseName->meta && baseName->property->isFinal()) {
682 683
                    const QMetaObject *meta = m_engine->metaObjectForType(baseName->property->propType);
                    QQmlPropertyCache *cache = m_engine->cache(meta);
684 685 686
                    if (!cache)
                        return false;

687
                    if (QQmlPropertyData *data = cache->property(name)) {
688 689 690 691 692 693
                        if (!data->isFinal()) {
                            if (qmlVerboseCompiler())
                                qWarning() << "*** non-final property access:"
                                           << (*baseName->id + QLatin1String(".") + ast->name.toString());
                            return false; // We don't know enough about this property
                        }
694

695
                        IR::Type irType = irTypeFromVariantType(data->propType, m_engine, meta);
696
                        _expr.code = _block->SYMBOL(baseName, irType, name,
697
                                                    meta, data, line, column);
698 699 700 701 702 703 704 705 706 707 708 709 710 711
                    }
                }
                break;

            case IR::Name::Object: 
            case IR::Name::Slot:
                break;
            }
        }
    }

    return false;
}

712
bool QV4IRBuilder::preVisit(AST::Node *)
713 714 715 716
{
    return ! _discard;
}

717
bool QV4IRBuilder::visit(AST::NewMemberExpression *)
718 719 720 721
{
    return false;
}

722
bool QV4IRBuilder::visit(AST::NewExpression *)
723 724 725 726
{
    return false;
}

727
bool QV4IRBuilder::visit(AST::CallExpression *ast)
728
{
729
    QList<QStringRef> names;
730
    QList<AST::ExpressionNode *> nameNodes;
731 732 733 734

    names.reserve(4);
    nameNodes.reserve(4);

735 736
    if (buildName(names, ast->base, &nameNodes)) {
        //ExprResult base = expression(ast->base);
737 738
        QString id;
        for (int i = 0; i < names.size(); ++i) {
739
            if (i)
740 741 742 743 744
                id += QLatin1Char('.');
            id += names.at(i);
        }
        const AST::SourceLocation loc = nameNodes.last()->firstSourceLocation();
        IR::Expr *base = _block->NAME(id, loc.startLine, loc.startColumn);
745

746 747 748
        IR::ExprList *args = 0, **argsInserter = &args;
        for (AST::ArgumentList *it = ast->arguments; it; it = it->next) {
            IR::Expr *arg = expression(it->expression);
749
            *argsInserter = _function->pool->New<IR::ExprList>();
750
            (*argsInserter)->init(arg);
751 752
            argsInserter = &(*argsInserter)->next;
        }
753 754 755 756 757 758 759 760 761 762 763

        IR::Temp *r = _block->TEMP(IR::InvalidType);
        IR::Expr *call = _block->CALL(base, args);
        _block->MOVE(r, call);
        r->type = call->type;
        _expr.code = r;
    }

    return false;
}

764
bool QV4IRBuilder::visit(AST::PostIncrementExpression *)
765 766 767 768
{
    return false;
}

769
bool QV4IRBuilder::visit(AST::PostDecrementExpression *)
770 771 772 773
{
    return false;
}

774
bool QV4IRBuilder::visit(AST::DeleteExpression *)
775 776 777 778
{
    return false;
}

779
bool QV4IRBuilder::visit(AST::VoidExpression *)
780 781 782 783
{
    return false;
}

784
bool QV4IRBuilder::visit(AST::TypeOfExpression *)
785 786 787 788
{
    return false;
}

789
bool QV4IRBuilder::visit(AST::PreIncrementExpression *)
790 791 792 793
{
    return false;
}

794
bool QV4IRBuilder::visit(AST::PreDecrementExpression *)
795 796 797 798
{
    return false;
}

799
bool QV4IRBuilder::visit(AST::UnaryPlusExpression *ast)
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
{
    ExprResult expr = expression(ast->expression);
    if (expr.isNot(IR::InvalidType)) {
        if (expr.code->asConst() != 0) {
            _expr = expr;
            return false;
        }

        IR::Expr *code = _block->UNOP(IR::OpUPlus, expr);
        _expr.code = _block->TEMP(code->type);
        _block->MOVE(_expr, code);
    }

    return false;
}

816
bool QV4IRBuilder::visit(AST::UnaryMinusExpression *ast)
817 818 819 820 821
{
    ExprResult expr = expression(ast->expression);
    if (expr.isNot(IR::InvalidType)) {
        if (IR::Const *c = expr.code->asConst()) {
            _expr = expr;
822
            _expr.code = _block->CONST(expr->type, -c->value);
823 824 825 826 827 828 829 830 831 832 833
            return false;
        }

        IR::Expr *code = _block->UNOP(IR::OpUMinus, expr);
        _expr.code = _block->TEMP(code->type);
        _block->MOVE(_expr, code);
    }

    return false;
}

834
bool QV4IRBuilder::visit(AST::TildeExpression *ast)
835 836 837 838 839
{
    ExprResult expr = expression(ast->expression);
    if (expr.isNot(IR::InvalidType)) {
        if (IR::Const *c = expr.code->asConst()) {
            _expr = expr;
840
            _expr.code = _block->CONST(expr->type, ~int(c->value));
841 842 843 844 845 846 847 848 849 850
            return false;
        }
        IR::Expr *code = _block->UNOP(IR::OpCompl, expr);
        _expr.code = _block->TEMP(code->type);
        _block->MOVE(_expr, code);
    }

    return false;
}

851
bool QV4IRBuilder::visit(AST::NotExpression *ast)
852 853 854 855 856 857
{
    ExprResult expr = expression(ast->expression);

    if (expr.isNot(IR::InvalidType)) {
        if (IR::Const *c = expr.code->asConst()) {
            _expr = expr;
858
            _expr.code = _block->CONST(IR::BoolType, !c->value);
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
            return false;
        }

        IR::Expr *code = _block->UNOP(IR::OpNot, expr);
        _expr.code = _block->TEMP(code->type);
        _block->MOVE(_expr, code);

    } else if (expr.hint == ExprResult::cx) {
        expr.format = ExprResult::cx;
        _block->CJUMP(_block->UNOP(IR::OpNot, expr), _expr.iftrue, _expr.iffalse);
        return false;
    }

    return false;
}

875
void QV4IRBuilder::binop(AST::BinaryExpression *ast, ExprResult left, ExprResult right)
876 877
{
    if (IR::Type t = maxType(left.type(), right.type())) {
878 879 880 881 882 883
        if (!left->asConst() && !right->asConst()) {
            // the implicit conversions are needed only
            // when compiling non-constant expressions.
            implicitCvt(left, t);
            implicitCvt(right, t);
        }
884 885 886
    } else if ((left.type() != IR::ObjectType && left.type() != IR::NullType) ||
               (right.type() != IR::ObjectType && right.type() != IR::NullType))
        return;
887

888 889 890 891 892
    if (_expr.hint == ExprResult::cx) {
        _expr.format = ExprResult::cx;
        _block->CJUMP(_block->BINOP(IR::binaryOperator(ast->op), left, right), _expr.iftrue, _expr.iffalse);
    } else {
        _expr.code = _block->BINOP(IR::binaryOperator(ast->op), left, right);
893 894 895
    }
}

896
bool QV4IRBuilder::visit(AST::BinaryExpression *ast)
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
{
    switch (ast->op) {
    case QSOperator::And: {
        if (_expr.hint == ExprResult::cx) {
            _expr.format = ExprResult::cx;

            Q_ASSERT(_expr.iffalse != 0);
            Q_ASSERT(_expr.iftrue != 0);

            IR::BasicBlock *iftrue = _function->newBasicBlock();
            condition(ast->left, iftrue, _expr.iffalse);

            _block = iftrue;
            condition(ast->right, _expr.iftrue, _expr.iffalse);
        } else {
            IR::BasicBlock *iftrue = _function->newBasicBlock();
            IR::BasicBlock *iffalse = _function->newBasicBlock();
            IR::BasicBlock *endif = _function->newBasicBlock();

916 917 918 919
            ExprResult left = expression(ast->left);
            IR::Temp *cond = _block->TEMP(IR::BoolType);
            _block->MOVE(cond, left);
            _block->CJUMP(cond, iftrue, iffalse);
920 921 922 923

            IR::Temp *r = _block->TEMP(IR::InvalidType);

            _block = iffalse;
924
            _block->MOVE(r, cond);
925 926 927 928 929 930 931
            _block->JUMP(endif);

            _block = iftrue;
            ExprResult right = expression(ast->right);
            _block->MOVE(r, right);
            _block->JUMP(endif);

932 933 934
            if (left.type() != right.type())
                discard();

935 936
            _block = endif;

937
            r->type = right.type();
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
            _expr.code = r;
        }
    } break;

    case QSOperator::Or: {
        IR::BasicBlock *iftrue = _function->newBasicBlock();
        IR::BasicBlock *endif = _function->newBasicBlock();

        ExprResult left = expression(ast->left);
        IR::Temp *r = _block->TEMP(left.type());
        _block->MOVE(r, left);

        IR::Expr *cond = r;
        if (r->type != IR::BoolType) {
            cond = _block->TEMP(IR::BoolType);
            _block->MOVE(cond, r);
        }

        _block->CJUMP(_block->UNOP(IR::OpNot, cond), iftrue, endif);

        _block = iftrue;
        ExprResult right = expression(ast->right);
        _block->MOVE(r, right);
961
        _block->JUMP(endif);
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

        if (left.type() != right.type())
            discard();

        _expr.code = r;

        _block = endif;
    } break;

    case QSOperator::Lt:
    case QSOperator::Gt:
    case QSOperator::Le:
    case QSOperator::Ge: {
        ExprResult left = expression(ast->left);
        ExprResult right = expression(ast->right);
        if (left.type() == IR::StringType && right.type() == IR::StringType) {
            binop(ast, left, right);
        } else if (left.isValid() && right.isValid()) {
            implicitCvt(left, IR::RealType);
            implicitCvt(right, IR::RealType);
            binop(ast, left, right);
        }
    } break;

    case QSOperator::NotEqual:
    case QSOperator::Equal: {
        ExprResult left = expression(ast->left);
        ExprResult right = expression(ast->right);
        if ((left.type() == IR::NullType || left.type() == IR::UndefinedType) &&
                (right.type() == IR::NullType || right.type() == IR::UndefinedType)) {
            const bool isEq = ast->op == QSOperator::Equal;
            if (_expr.hint == ExprResult::cx) {
                _expr.format = ExprResult::cx;
                _block->JUMP(isEq ? _expr.iftrue : _expr.iffalse);
            } else {
                _expr.code = _block->CONST(IR::BoolType, isEq ? 1 : 0);
            }
        } else if ((left.type() == IR::StringType && right.type() >= IR::FirstNumberType) ||
                   (left.type() >= IR::FirstNumberType && right.type() == IR::StringType)) {
            implicitCvt(left, IR::RealType);
            implicitCvt(right, IR::RealType);
            binop(ast, left, right);
        } else if (left.isValid() && right.isValid()) {
            binop(ast, left, right);
        }
    } break;

    case QSOperator::StrictEqual:
    case QSOperator::StrictNotEqual: {
        ExprResult left = expression(ast->left);
        ExprResult right = expression(ast->right);
        if (left.type() == right.type()) {
            binop(ast, left, right);
1015
        } else if (left.type() > IR::BoolType && right.type() > IR::BoolType) {
1016 1017
            // left and right have numeric type (int or real)
            binop(ast, left, right);
1018 1019 1020 1021
        } else if ((left.type() == IR::ObjectType && right.type() == IR::NullType) ||
                   (right.type() == IR::ObjectType && left.type() == IR::NullType)) {
            // comparing a qobject with null
            binop(ast, left, right);
1022
        } else if (left.isValid() && right.isValid()) {
1023
            // left and right have different types
1024 1025 1026
            const bool isEq = ast->op == QSOperator::StrictEqual;
            if (_expr.hint == ExprResult::cx) {
                _expr.format = ExprResult::cx;
1027
                _block->JUMP(isEq ? _expr.iffalse : _expr.iftrue);
1028
            } else {
1029
                _expr.code = _block->CONST(IR::BoolType, isEq ? 0 : 1);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
            }
        }
    } break;

    case QSOperator::BitAnd:
    case QSOperator::BitOr:
    case QSOperator::BitXor:
    case QSOperator::LShift:
    case QSOperator::RShift:
    case QSOperator::URShift: {
        ExprResult left = expression(ast->left);
        if (left.is(IR::InvalidType))
            return false;

        ExprResult right = expression(ast->right);
        if (right.is(IR::InvalidType))
            return false;

        implicitCvt(left, IR::IntType);
        implicitCvt(right, IR::IntType);

        IR::Expr *code = _block->BINOP(IR::binaryOperator(ast->op), left, right);
        _expr.code = _block->TEMP(code->type);
        _block->MOVE(_expr.code, code);

    } break;

    case QSOperator::Add: {
        ExprResult left = expression(ast->left);
        if (left.is(IR::InvalidType))
            return false;

        ExprResult right = expression(ast->right);
        if (right.is(IR::InvalidType))
            return false;

        if (left.isPrimitive() && right.isPrimitive()) {
            if (left.type() == IR::StringType || right.type() == IR::StringType) {
                implicitCvt(left, IR::StringType);
                implicitCvt(right, IR::StringType);
            }
            binop(ast, left, right);
        }
    } break;

    case QSOperator::Div:
    case QSOperator::Mod:
    case QSOperator::Mul:
    case QSOperator::Sub: {
        ExprResult left = expression(ast->left);
        if (left.is(IR::InvalidType))
            return false;

        ExprResult right = expression(ast->right);
        if (right.is(IR::InvalidType))
            return false;

        IR::Type t = maxType(left.type(), right.type());
        if (t >= IR::FirstNumberType) {
            implicitCvt(left, IR::RealType);
            implicitCvt(right, IR::RealType);

            IR::Expr *code = _block->BINOP(IR::binaryOperator(ast->op), left, right);
            _expr.code = _block->TEMP(code->type);
            _block->MOVE(_expr.code, code);
        }
    } break;

    case QSOperator::In:
    case QSOperator::InstanceOf:
    case QSOperator::Assign:
    case QSOperator::InplaceAnd:
    case QSOperator::InplaceSub:
    case QSOperator::InplaceDiv:
    case QSOperator::InplaceAdd:
    case QSOperator::InplaceLeftShift:
    case QSOperator::InplaceMod:
    case QSOperator::InplaceMul:
    case QSOperator::InplaceOr:
    case QSOperator::InplaceRightShift:
    case QSOperator::InplaceURightShift:
    case QSOperator::InplaceXor:
        // yup, we don't do those.
        break;
    } // switch

    return false;
}

1119
bool QV4IRBuilder::visit(AST::ConditionalExpression *ast)
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
{
    IR::BasicBlock *iftrue = _function->newBasicBlock();
    IR::BasicBlock *iffalse = _function->newBasicBlock();
    IR::BasicBlock *endif = _function->newBasicBlock();

    condition(ast->expression, iftrue, iffalse);

    IR::Temp *r = _block->TEMP(IR::InvalidType);

    qSwap(_block, iftrue);
    ExprResult ok = expression(ast->ok);
    _block->MOVE(r, ok);
    _block->JUMP(endif);
    qSwap(_block, iftrue);

    qSwap(_block, iffalse);
    ExprResult ko = expression(ast->ko);
    _block->MOVE(r, ko);
    _block->JUMP(endif);
    qSwap(_block, iffalse);

    r->type = maxType(ok.type(), ko.type());
    _expr.code = r;

    _block = endif;

    return false;
}

1149
bool QV4IRBuilder::visit(AST::Expression *ast)
1150 1151 1152 1153 1154 1155 1156 1157 1158
{
    _block->EXP(expression(ast->left));
    _expr = expression(ast->right);

    return false;
}


// statements
1159
bool QV4IRBuilder::visit(AST::Block *ast)
1160 1161 1162 1163 1164 1165 1166 1167 1168
{
    if (ast->statements && ! ast->statements->next) {
        // we have one and only one statement
        accept(ast->statements->statement);
    }

    return false;
}

1169
bool QV4IRBuilder::visit(AST::StatementList *)
1170 1171 1172 1173
{
    return false;
}

1174
bool QV4IRBuilder::visit(AST::VariableStatement *)
1175 1176 1177 1178
{
    return false;
}

1179
bool QV4IRBuilder::visit(AST::VariableDeclarationList *)
1180 1181 1182 1183
{
    return false;
}

1184
bool QV4IRBuilder::visit(AST::VariableDeclaration *)
1185 1186 1187 1188
{
    return false;
}

1189
bool QV4IRBuilder::visit(AST::EmptyStatement *)
1190 1191 1192 1193
{
    return false;
}

1194
bool QV4IRBuilder::visit(AST::ExpressionStatement *ast)
1195 1196 1197 1198 1199 1200 1201 1202 1203
{
    if (ast->expression) {
         // return the value of this expression
        return true;
    }

    return false;
}

1204
bool QV4IRBuilder::visit(AST::IfStatement *ast)
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
{
    if (! ast->ko) {
        // This is an if statement without an else branch.
        discard();
    } else {
        IR::BasicBlock *iftrue = _function->newBasicBlock();
        IR::BasicBlock *iffalse = _function->newBasicBlock();
        IR::BasicBlock *endif = _function->newBasicBlock();

        condition(ast->expression, iftrue, iffalse);

        IR::Temp *r = _block->TEMP(IR::InvalidType);

        qSwap(_block, iftrue);
        ExprResult ok = statement(ast->ok);
        _block->MOVE(r, ok);
        _block->JUMP(endif);
        qSwap(_block, iftrue);

        qSwap(_block, iffalse);
        ExprResult ko = statement(ast->ko);
        _block->MOVE(r, ko);
        _block->JUMP(endif);
        qSwap(_block, iffalse);

        r->type = maxType(ok.type(), ko.type());
        _expr.code = r;

        _block = endif;
    }

    return false;
}

1239
bool QV4IRBuilder::visit(AST::DoWhileStatement *)
1240 1241 1242 1243
{
    return false;
}

1244
bool QV4IRBuilder::visit(AST::WhileStatement *)
1245 1246 1247 1248
{
    return false;
}

1249
bool QV4IRBuilder::visit(AST::ForStatement *)
1250 1251 1252 1253
{
    return false;
}

1254
bool QV4IRBuilder::visit(AST::LocalForStatement *)
1255 1256 1257 1258
{
    return false;
}

1259
bool QV4IRBuilder::visit(AST::ForEachStatement *)
1260 1261 1262 1263
{
    return false;
}

1264
bool QV4IRBuilder::visit(AST::LocalForEachStatement *)
1265 1266 1267 1268 1269
{
    discard();
    return false;
}

1270
bool QV4IRBuilder::visit(AST::ContinueStatement *)
1271 1272 1273 1274
{
    return false;
}

1275
bool QV4IRBuilder::visit(AST::BreakStatement *)
1276 1277 1278 1279
{
    return false;
}

1280
bool QV4IRBuilder::visit(AST::ReturnStatement *ast)
1281 1282 1283 1284 1285 1286 1287 1288 1289
{
    if (ast->expression) {
        // return the value of the expression
        return true;
    }

    return false;
}

1290
bool QV4IRBuilder::visit(AST::WithStatement *)
1291 1292 1293 1294
{
    return false;
}

1295
bool QV4IRBuilder::visit(AST::SwitchStatement *)
1296 1297 1298 1299
{
    return false;
}

1300
bool QV4IRBuilder::visit(AST::CaseBlock *)
1301 1302 1303 1304
{
    return false;
}

1305
bool QV4IRBuilder::visit(AST::CaseClauses *)
1306 1307 1308 1309
{
    return false;
}

1310
bool QV4IRBuilder::visit(AST::CaseClause *)
1311 1312 1313 1314
{
    return false;
}

1315
bool QV4IRBuilder::visit(AST::DefaultClause *)
1316 1317 1318 1319
{
    return false;
}

1320
bool QV4IRBuilder::visit(AST::LabelledStatement *)
1321 1322 1323 1324
{
    return false;
}

1325
bool QV4IRBuilder::visit(AST::ThrowStatement *)
1326 1327 1328 1329
{
    return false;
}

1330
bool QV4IRBuilder::visit(AST::TryStatement *)
1331 1332 1333 1334
{
    return false;
}

1335
bool QV4IRBuilder::visit(AST::Catch *)
1336 1337 1338 1339
{
    return false;
}

1340
bool QV4IRBuilder::visit(AST::Finally *)
1341 1342 1343 1344
{
    return false;
}

1345
bool QV4IRBuilder::visit(AST::FunctionDeclaration *)
1346 1347 1348 1349
{
    return false;
}

1350
bool QV4IRBuilder::visit(AST::FunctionExpression *)
1351 1352 1353 1354
{
    return false;
}

1355
bool QV4IRBuilder::visit(AST::FormalParameterList *)
1356 1357 1358 1359
{
    return false;
}

1360
bool QV4IRBuilder::visit(AST::FunctionBody *)
1361 1362 1363 1364
{
    return false;
}

1365
bool QV4IRBuilder::visit(AST::DebuggerStatement *)
1366 1367 1368 1369 1370
{
    return false;
}

QT_END_NAMESPACE