qmljslink.cpp 8.96 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "qmljslink.h"

#include "parser/qmljsast_p.h"
#include "qmljsdocument.h"
#include "qmljsbind.h"

#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QDebug>

using namespace QmlJS;
using namespace QmlJS::Interpreter;
using namespace QmlJS::AST;

15
Link::Link(Context *context, Document::Ptr currentDoc, const Snapshot &snapshot)
16
    : _snapshot(snapshot)
17
    , _context(context)
18
19
20
21
22
23
24
25
26
{
    _docs = reachableDocuments(currentDoc, snapshot);
    linkImports();
}

Link::~Link()
{
}

27
Interpreter::Engine *Link::engine()
28
{
29
    return _context->engine();
30
}
31

32
33
void Link::scopeChainAt(Document::Ptr doc, Node *currentObject)
{
34
    _context->pushScope(engine()->globalObject());
35

36
    if (! doc)
37
        return;
38
39

    if (doc->qmlProgram() != 0)
40
        _context->setLookupMode(Context::QmlLookup);
41

Roberto Raggi's avatar
Roberto Raggi committed
42
    Bind *bind = doc->bind();
43
44
45

    // Build the scope chain.

46
    // ### FIXME: May want to link to instantiating components from here.
47

48
    qDebug() << "**** here" << currentObject;
49

50
51
52
53
54
    ObjectValue *scopeObject = 0;
    if (UiObjectDefinition *definition = cast<UiObjectDefinition *>(currentObject))
        scopeObject = bind->_qmlObjects.value(definition);
    else if (UiObjectBinding *binding = cast<UiObjectBinding *>(currentObject))
        scopeObject = bind->_qmlObjects.value(binding);
55
56
    else if (FunctionDeclaration *fun = cast<FunctionDeclaration *>(currentObject)) {
        _context->pushScope(bind->_rootObjectValue);
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        ObjectValue *activation = engine()->newObject(/*prototype = */ 0);
        for (FormalParameterList *it = fun->formals; it; it = it->next) {
            if (it->name)
                activation->setProperty(it->name->asString(), engine()->undefinedValue());
        }
        _context->pushScope(activation);
    }

    if (scopeObject) {
        if (bind->_rootObjectValue)
            _context->pushScope(bind->_rootObjectValue);

        if (scopeObject != bind->_rootObjectValue)
            _context->pushScope(scopeObject);
    }
73

74
75
76
    const QStringList &includedScripts = bind->includedScripts();
    for (int index = includedScripts.size() - 1; index != -1; --index) {
        const QString &scriptFile = includedScripts.at(index);
77

78
79
        if (Document::Ptr scriptDoc = _snapshot.document(scriptFile)) {
            if (scriptDoc->jsProgram()) {
80
                _context->pushScope(scriptDoc->bind()->_rootObjectValue);
81
            }
82
83
        }
    }
84

85
    if (bind->_functionEnvironment)
86
        _context->pushScope(bind->_functionEnvironment);
87
88

    if (bind->_idEnvironment)
89
        _context->pushScope(bind->_idEnvironment);
90

91
92
    if (const ObjectValue *typeEnvironment = _context->typeEnvironment(doc.data()))
        _context->pushScope(typeEnvironment);
93
94
95
96
97
}

void Link::linkImports()
{
    foreach (Document::Ptr doc, _docs) {
98
        ObjectValue *typeEnv = engine()->newObject(/*prototype =*/0); // ### FIXME
99
100
101

        // Populate the _typeEnvironment with imports.
        populateImportedTypes(typeEnv, doc);
102

103
        _context->setTypeEnvironment(doc.data(), typeEnv);
104
105
106
    }
}

107
108
109
110
111
112
113
114
115
116
static QString componentName(const QString &fileName)
{
    QString componentName = fileName;
    int dotIndex = componentName.indexOf(QLatin1Char('.'));
    if (dotIndex != -1)
        componentName.truncate(dotIndex);
    componentName[0] = componentName[0].toUpper();
    return componentName;
}

117
void Link::populateImportedTypes(Interpreter::ObjectValue *typeEnv, Document::Ptr doc)
118
119
120
121
122
123
124
{
    if (! (doc->qmlProgram() && doc->qmlProgram()->imports))
        return;

    QFileInfo fileInfo(doc->fileName());
    const QString absolutePath = fileInfo.absolutePath();

125
126
    // implicit imports:
    // qml files in the same directory are available without explicit imports
127
128
    foreach (Document::Ptr otherDoc, _docs) {
        if (otherDoc == doc)
129
130
131
132
133
134
135
136
            continue;

        QFileInfo otherFileInfo(otherDoc->fileName());
        const QString otherAbsolutePath = otherFileInfo.absolutePath();

        if (otherAbsolutePath != absolutePath)
            continue;

137
138
        typeEnv->setProperty(componentName(otherFileInfo.fileName()),
                             otherDoc->bind()->_rootObjectValue);
139
140
141
142
    }

    // explicit imports, whether directories or files
    for (UiImportList *it = doc->qmlProgram()->imports; it; it = it->next) {
143
        if (! it->import)
144
145
            continue;

146
147
148
149
150
151
152
        if (it->import->fileName) {
            importFile(typeEnv, doc, it->import, absolutePath);
        } else if (it->import->importUri) {
            importNonFile(typeEnv, doc, it->import);
        }
    }
}
153

154
155
156
157
158
/*
    import "content"
    import "content" as Xxx
    import "content" 4.6
    import "content" 4.6 as Xxx
159

160
161
162
163
164
    import "http://www.ovi.com/" as Ovi
*/
void Link::importFile(Interpreter::ObjectValue *typeEnv, Document::Ptr doc,
                      AST::UiImport *import, const QString &startPath)
{
165
166
    Q_UNUSED(doc)

167
168
    if (!import->fileName)
        return;
169

170
171
172
173
    QString path = startPath;
    path += QLatin1Char('/');
    path += import->fileName->asString();
    path = QDir::cleanPath(path);
174

175
    ObjectValue *importNamespace = 0;
176

177
178
179
180
181
182
183
184
    foreach (Document::Ptr otherDoc, _docs) {
        QFileInfo otherFileInfo(otherDoc->fileName());
        const QString otherAbsolutePath = otherFileInfo.absolutePath();

        bool directoryImport = (path == otherAbsolutePath);
        bool fileImport = (path == otherDoc->fileName());
        if (!directoryImport && !fileImport)
            continue;
185

186
        if (directoryImport && import->importId && !importNamespace) {
187
            importNamespace = engine()->newObject(/*prototype =*/0);
188
189
            typeEnv->setProperty(import->importId->asString(), importNamespace);
        }
190

191
192
193
194
195
        QString targetName;
        if (fileImport && import->importId) {
            targetName = import->importId->asString();
        } else {
            targetName = componentName(otherFileInfo.fileName());
196
        }
197
198
199
200
201
202

        ObjectValue *importInto = typeEnv;
        if (importNamespace)
            importInto = importNamespace;

        importInto->setProperty(targetName, otherDoc->bind()->_rootObjectValue);
203
204
205
    }
}

206
207
208
209
210
211
212
213
214
215
/*
  import Qt 4.6
  import Qt 4.6 as Xxx
  (import com.nokia.qt is the same as the ones above)
*/
void Link::importNonFile(Interpreter::ObjectValue *typeEnv, Document::Ptr doc, AST::UiImport *import)
{
    ObjectValue *namespaceObject = 0;

    if (import->importId) { // with namespace we insert an object in the type env. to hold the imported types
216
        namespaceObject = engine()->newObject(/*prototype */ 0);
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
        typeEnv->setProperty(import->importId->asString(), namespaceObject);

    } else { // without namespace we insert all types directly into the type env.
        namespaceObject = typeEnv;
    }

    // try the metaobject system
    if (import->importUri) {
        const QString package = Bind::toString(import->importUri, '/');
        int majorVersion = -1; // ### TODO: Check these magic version numbers
        int minorVersion = -1; // ### TODO: Check these magic version numbers

        if (import->versionToken.isValid()) {
            const QString versionString = doc->source().mid(import->versionToken.offset, import->versionToken.length);
            const int dotIdx = versionString.indexOf(QLatin1Char('.'));
            if (dotIdx == -1) {
                // only major (which is probably invalid, but let's handle it anyway)
                majorVersion = versionString.toInt();
                minorVersion = 0; // ### TODO: Check with magic version numbers above
            } else {
                majorVersion = versionString.left(dotIdx).toInt();
                minorVersion = versionString.mid(dotIdx + 1).toInt();
            }
        }
#ifndef NO_DECLARATIVE_BACKEND
242
        foreach (QmlObjectValue *object, engine()->metaTypeSystem().staticTypesForImport(package, majorVersion, minorVersion)) {
243
244
245
246
247
248
            namespaceObject->setProperty(object->qmlTypeName(), object);
        }
#endif // NO_DECLARATIVE_BACKEND
    }
}

249
250
251
252
253
254
255
256
257
258
UiQualifiedId *Link::qualifiedTypeNameId(Node *node)
{
    if (UiObjectBinding *binding = AST::cast<UiObjectBinding *>(node))
        return binding->qualifiedTypeNameId;
    else if (UiObjectDefinition *binding = AST::cast<UiObjectDefinition *>(node))
        return binding->qualifiedTypeNameId;
    else
        return 0;
}

259
QList<Document::Ptr> Link::reachableDocuments(Document::Ptr startDoc, const Snapshot &snapshot)
260
{
261
    QList<Document::Ptr> docs;
262

263
264
    QSet<QString> processed;
    QStringList todo;
265

266
267
268
    QMultiHash<QString, Document::Ptr> documentByPath;
    foreach (Document::Ptr doc, snapshot)
        documentByPath.insert(doc->path(), doc);
269

270
    todo.append(startDoc->path());
271

272
273
274
    // Find the reachable documents.
    while (! todo.isEmpty()) {
        const QString path = todo.takeFirst();
275

276
277
        if (processed.contains(path))
            continue;
278

279
280
281
282
283
284
285
286
287
288
        processed.insert(path);

        QStringList localImports;
        foreach (Document::Ptr doc, documentByPath.values(path)) {
            docs += doc;
            localImports += doc->bind()->localImports();
        }

        localImports.removeDuplicates();
        todo += localImports;
289
290
    }

291
    return docs;
292
}