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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "parser/qmljsast_p.h"
#include "qmljsbind.h"
#include "qmljscheck.h"
#include "qmljsdocument.h"
#include <languageutils/componentversion.h>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
using namespace LanguageUtils;
using namespace QmlJS;
using namespace QmlJS::AST;
using namespace QmlJS::Interpreter;
/*!
\class QmlJS::Bind
\brief Collected information about a single Document.
\sa QmlJS::Document QmlJS::Link
Each QmlJS::Document owns a instance of Bind. It provides access to data
that can be derived by looking at the document in isolation. If you need
information that goes beyond that, you need to create a
\l{QmlJS::Interpreter::Context} using \l{QmlJS::Link}.
The document's imports are classified and available through imports().
It allows AST to code model lookup through findQmlObject() and findFunctionScope().
*/
Bind::Bind(Document *doc, QList<DiagnosticMessage> *messages)
: _doc(doc),
_currentObjectValue(0),
_idEnvironment(0),
_rootObjectValue(0),
_diagnosticMessages(messages)
{
if (_doc)
accept(_doc->ast());
}
Bind::~Bind()
{
}
QList<ImportInfo> Bind::imports() const
{
return _imports;
}
Interpreter::ObjectValue *Bind::idEnvironment() const
{
return _idEnvironment;
}
Interpreter::ObjectValue *Bind::rootObjectValue() const
{
return _rootObjectValue;
}
Interpreter::ObjectValue *Bind::findQmlObject(AST::Node *node) const
{
return _qmlObjects.value(node);
}
bool Bind::usesQmlPrototype(ObjectValue *prototype,
const Context *context) const
{
if (!prototype)
return false;
const QString componentName = prototype->className();
// all component objects have classname set
if (componentName.isEmpty())
return false;
// get a list of all the names that may refer to this component
// this can only happen for file imports with an 'as' clause
// if there aren't any, possibleNames will be left empty
QSet<QString> possibleNames;
foreach (const ImportInfo &import, imports()) {
if (import.type() == ImportInfo::FileImport
&& !import.id().isEmpty()
&& import.name().contains(componentName)) {
possibleNames.insert(import.id());
}
}
if (!possibleNames.isEmpty())
possibleNames.insert(componentName);
// if there are no renamed imports and the document does not use
// the className string anywhere, it's out
if (possibleNames.isEmpty()) {
NameId nameId(componentName.data(), componentName.size());
if (!_doc->engine()->literals().contains(nameId))
return false;
}
QHashIterator<Node *, ObjectValue *> it(_qmlObjects);
while (it.hasNext()) {
it.next();
// if the type id does not contain one of the possible names, skip
Node *node = it.key();
UiQualifiedId *id = 0;
if (UiObjectDefinition *n = cast<UiObjectDefinition *>(node)) {
id = n->qualifiedTypeNameId;
} else if (UiObjectBinding *n = cast<UiObjectBinding *>(node)) {
id = n->qualifiedTypeNameId;
}
if (!id)
continue;
bool skip = false;
// optimize the common case of no renamed imports
if (possibleNames.isEmpty()) {
for (UiQualifiedId *idIt = id; idIt; idIt = idIt->next) {
if (!idIt->next && idIt->name->asString() != componentName)
skip = true;
}
} else {
for (UiQualifiedId *idIt = id; idIt; idIt = idIt->next) {
if (!idIt->next && !possibleNames.contains(idIt->name->asString()))
skip = true;
}
}
if (skip)
continue;
// resolve and check the prototype
const ObjectValue *object = it.value();
const ObjectValue *resolvedPrototype = object->prototype(context);
if (resolvedPrototype == prototype)
return true;
}
return false;
}
Interpreter::ObjectValue *Bind::findFunctionScope(AST::FunctionExpression *node) const
{
return _functionScopes.value(node);
}
bool Bind::isGroupedPropertyBinding(AST::Node *node) const
{
return _groupedPropertyBindings.contains(node);
}
ObjectValue *Bind::switchObjectValue(ObjectValue *newObjectValue)
{
ObjectValue *oldObjectValue = _currentObjectValue;
_currentObjectValue = newObjectValue;
return oldObjectValue;
}
QString Bind::toString(UiQualifiedId *qualifiedId, QChar delimiter)
{
QString result;
for (UiQualifiedId *iter = qualifiedId; iter; iter = iter->next) {
if (iter != qualifiedId)
result += delimiter;
if (iter->name)
result += iter->name->asString();
}
return result;
}
ObjectValue *Bind::bindObject(UiQualifiedId *qualifiedTypeNameId, UiObjectInitializer *initializer)
{
ObjectValue *parentObjectValue = 0;
// normal component instance
ASTObjectValue *objectValue = new ASTObjectValue(qualifiedTypeNameId, initializer, _doc, &_engine);
QmlPrototypeReference *prototypeReference =
new QmlPrototypeReference(qualifiedTypeNameId, _doc, &_engine);
objectValue->setPrototype(prototypeReference);
parentObjectValue = switchObjectValue(objectValue);
if (parentObjectValue)
objectValue->setProperty(QLatin1String("parent"), parentObjectValue);
else {
_rootObjectValue = objectValue;
_rootObjectValue->setClassName(_doc->componentName());
}
accept(initializer);
return switchObjectValue(parentObjectValue);
}
void Bind::accept(Node *node)
{
Node::accept(node, this);
}
bool Bind::visit(AST::UiProgram *)
{
_idEnvironment = _engine.newObject(/*prototype =*/ 0);
return true;
}
bool Bind::visit(AST::Program *)
{
_currentObjectValue = _engine.newObject(/*prototype =*/ 0);
_rootObjectValue = _currentObjectValue;
return true;
}
bool Bind::visit(UiImport *ast)
{
ComponentVersion version;
ImportInfo::Type type = ImportInfo::InvalidImport;
QString name;
if (ast->versionToken.isValid()) {
const QString versionString = _doc->source().mid(ast->versionToken.offset, ast->versionToken.length);
const int dotIdx = versionString.indexOf(QLatin1Char('.'));
if (dotIdx != -1) {
version = ComponentVersion(versionString.left(dotIdx).toInt(),
versionString.mid(dotIdx + 1).toInt());
} else {
_diagnosticMessages->append(
errorMessage(ast->versionToken, tr("expected two numbers separated by a dot")));
}
}
if (ast->importUri) {
type = ImportInfo::LibraryImport;
name = toString(ast->importUri, QDir::separator());
if (!version.isValid()) {
_diagnosticMessages->append(
errorMessage(ast, tr("package import requires a version number")));
}
} else if (ast->fileName) {
const QFileInfo importFileInfo(_doc->path() + QDir::separator() + ast->fileName->asString());
name = importFileInfo.absoluteFilePath();
if (importFileInfo.isFile())
type = ImportInfo::FileImport;
else if (importFileInfo.isDir())
type = ImportInfo::DirectoryImport;
else {
_diagnosticMessages->append(
errorMessage(ast, tr("file or directory not found")));
type = ImportInfo::UnknownFileImport;
}
}
_imports += ImportInfo(type, name, version, ast);
return false;
}
bool Bind::visit(UiPublicMember *)
{
// nothing to do.
return true;
}
bool Bind::visit(UiObjectDefinition *ast)
{
// an UiObjectDefinition may be used to group property bindings
// think anchors { ... }
bool isGroupedBinding = false;
for (UiQualifiedId *it = ast->qualifiedTypeNameId; it; it = it->next) {
if (!it->next && it->name)
isGroupedBinding = it->name->asString().at(0).isLower();
}
if (!isGroupedBinding) {
ObjectValue *value = bindObject(ast->qualifiedTypeNameId, ast->initializer);
_qmlObjects.insert(ast, value);
} else {
_groupedPropertyBindings.insert(ast);
}
return false;
}
bool Bind::visit(UiObjectBinding *ast)
{
// const QString name = serialize(ast->qualifiedId);
ObjectValue *value = bindObject(ast->qualifiedTypeNameId, ast->initializer);
_qmlObjects.insert(ast, value);
// ### FIXME: we don't handle dot-properties correctly (i.e. font.size)
// _currentObjectValue->setProperty(name, value);
return false;
}
bool Bind::visit(UiScriptBinding *ast)
{
if (toString(ast->qualifiedId) == QLatin1String("id")) {
if (ExpressionStatement *e = cast<ExpressionStatement*>(ast->statement))
if (IdentifierExpression *i = cast<IdentifierExpression*>(e->expression))
if (i->name)
_idEnvironment->setProperty(i->name->asString(), _currentObjectValue);
}
return true;
}
bool Bind::visit(UiArrayBinding *)
{
// ### FIXME: do we need to store the members into the property? Or, maybe the property type is an JS Array?
return true;
}
bool Bind::visit(VariableDeclaration *ast)
{
if (! ast->name)
return false;
ASTVariableReference *ref = new ASTVariableReference(ast, &_engine);
_currentObjectValue->setProperty(ast->name->asString(), ref);
return true;
}
bool Bind::visit(FunctionExpression *ast)
{
// ### FIXME: the first declaration counts
//if (_currentObjectValue->property(ast->name->asString(), 0))
// return false;
ASTFunctionValue *function = new ASTFunctionValue(ast, _doc, &_engine);
if (ast->name && cast<FunctionDeclaration *>(ast))
_currentObjectValue->setProperty(ast->name->asString(), function);
// build function scope
ObjectValue *functionScope = _engine.newObject(/*prototype=*/0);
_functionScopes.insert(ast, functionScope);
ObjectValue *parent = switchObjectValue(functionScope);
// The order of the following is important. Example: A function with name "arguments"
// overrides the arguments object, a variable doesn't.
// 1. Function formal arguments
for (FormalParameterList *it = ast->formals; it; it = it->next) {
if (it->name)
functionScope->setProperty(it->name->asString(), _engine.undefinedValue());
}
// 2. Functions defined inside the function body
// ### TODO, currently covered by the accept(body)
// 3. Arguments object
ObjectValue *arguments = _engine.newObject(/*prototype=*/0);
arguments->setProperty(QLatin1String("callee"), function);
arguments->setProperty(QLatin1String("length"), _engine.numberValue());
functionScope->setProperty(QLatin1String("arguments"), arguments);
// 4. Variables defined inside the function body
// ### TODO, currently covered by the accept(body)
// visit body
accept(ast->body);
switchObjectValue(parent);
return false;
}
bool Bind::visit(FunctionDeclaration *ast)
{
return visit(static_cast<FunctionExpression *>(ast));
}
|