OSDN Git Service

QmlJS: Simplify check if a document instantiates a component.
authorChristian Kamm <christian.d.kamm@nokia.com>
Tue, 13 Sep 2011 08:09:14 +0000 (10:09 +0200)
committerChristian Kamm <christian.d.kamm@nokia.com>
Wed, 14 Sep 2011 09:48:41 +0000 (11:48 +0200)
Change-Id: If45002ecec56ca766ef7a0c7878c0a48fadee452
Reviewed-on: http://codereview.qt-project.org/4734
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
src/libs/qmljs/qmljsbind.cpp
src/libs/qmljs/qmljsbind.h

index 74ba761..c0f5219 100644 (file)
@@ -107,62 +107,8 @@ bool Bind::usesQmlPrototype(ObjectValue *prototype,
     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()) {
-        // ### FIXME!
-//        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 != componentName)
-                    skip = true;
-            }
-        } else {
-            for (UiQualifiedId *idIt = id; idIt; idIt = idIt->next) {
-                if (!idIt->next && !possibleNames.contains(idIt->name.toString()))
-                    skip = true;
-            }
-        }
-        if (skip)
-            continue;
-
+    foreach (const ObjectValue *object, _qmlObjectsByPrototypeName.values(componentName)) {
         // resolve and check the prototype
-        const ObjectValue *object = it.value();
         const ObjectValue *resolvedPrototype = object->prototype(context);
         if (resolvedPrototype == prototype)
             return true;
@@ -212,6 +158,12 @@ ObjectValue *Bind::bindObject(UiQualifiedId *qualifiedTypeNameId, UiObjectInitia
             new QmlPrototypeReference(qualifiedTypeNameId, _doc, &_valueOwner);
     objectValue->setPrototype(prototypeReference);
 
+    // add the prototype name to the prototypes hash
+    for (UiQualifiedId *it = qualifiedTypeNameId; it; it = it->next) {
+        if (!it->next && !it->name.isEmpty())
+            _qmlObjectsByPrototypeName.insert(it->name.toString(), objectValue);
+    }
+
     parentObjectValue = switchObjectValue(objectValue);
 
     if (parentObjectValue)
index 0703d81..f8a562c 100644 (file)
@@ -102,6 +102,7 @@ private:
     ObjectValue *_rootObjectValue;
 
     QHash<AST::Node *, ObjectValue *> _qmlObjects;
+    QMultiHash<QString, const ObjectValue *> _qmlObjectsByPrototypeName;
     QSet<AST::Node *> _groupedPropertyBindings;
     QHash<AST::Node *, ObjectValue *> _attachedJSScopes;
     QStringList _includedScripts;