qmljs: uniform property info

CppComponent has extra information on properties that makes sense
also for ObjectValues, change the processProperty method to pass
in an extra struct with that info (and that can be later extended).

Change-Id: If09b178a4095bed03ff59bfb2164088309d2c8e2
Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com>
This commit is contained in:
Fawzi Mohamed
2014-07-14 18:38:27 +02:00
parent 5518d70d72
commit 4a60aa7f05
5 changed files with 134 additions and 18 deletions

View File

@@ -187,9 +187,18 @@ public:
return true; return true;
} }
bool processProperty(const QString &name, const Value *value) QTC_OVERRIDE bool processProperty(const QString &name, const Value *value, const PropertyInfo &pInfo) QTC_OVERRIDE
{ {
return dump(name, value); d.dumpNewline();
d.dump(name);
d.openContext(":");
d.dump("<");
d.dump(pInfo.toString());
d.dump(">");
d.dumpNewline(); // avoid?
value->accept(&d);
d.closeContext("");
return true;
} }
bool processEnumerator(const QString &name, const Value *value) QTC_OVERRIDE bool processEnumerator(const QString &name, const Value *value) QTC_OVERRIDE
{ {

View File

@@ -107,7 +107,7 @@ public:
const Value *value() const { return m_value; } const Value *value() const { return m_value; }
virtual bool processProperty(const QString &name, const Value *value) virtual bool processProperty(const QString &name, const Value *value, const PropertyInfo &)
{ {
return process(name, value); return process(name, value);
} }
@@ -186,6 +186,45 @@ uint qHash(const FakeMetaObjectWithOrigin &fmoo)
return qHash(fmoo.fakeMetaObject); return qHash(fmoo.fakeMetaObject);
} }
PropertyInfo::PropertyInfo(uint flags)
: flags(flags)
{ }
QString PropertyInfo::toString() const
{
bool join = false;
QString res;
if (isReadable()) {
res += QLatin1String("Readable");
join = true;
}
if (isWriteable()) {
if (join)
res += QLatin1String("|");
res += QLatin1String("Writeable");
join = true;
}
if (isList()) {
if (join)
res += QLatin1String("|");
res += QLatin1String("ListType");
join = true;
}
if (canBePointer()) {
if (join)
res += QLatin1String("|");
res += QLatin1String("Pointer");
join = true;
}
if (canBeValue()) {
if (join)
res += QLatin1String("|");
res += QLatin1String("Value");
join = true;
}
return res;
}
} // namespace QmlJS } // namespace QmlJS
CppComponentValue::CppComponentValue(FakeMetaObject::ConstPtr metaObject, const QString &className, CppComponentValue::CppComponentValue(FakeMetaObject::ConstPtr metaObject, const QString &className,
@@ -303,7 +342,17 @@ void CppComponentValue::processMembers(MemberProcessor *processor) const
continue; continue;
const QString propertyName = prop.name(); const QString propertyName = prop.name();
processor->processProperty(propertyName, valueForCppName(prop.typeName())); uint propertyFlags = PropertyInfo::Readable;
if (isWritable(propertyName))
propertyFlags |= PropertyInfo::Writeable;
if (isListProperty(propertyName))
propertyFlags |= PropertyInfo::ListType;
if (isPointer(propertyName))
propertyFlags |= PropertyInfo::PointerType;
else
propertyFlags |= PropertyInfo::ValueType;
processor->processProperty(propertyName, valueForCppName(prop.typeName()),
PropertyInfo(propertyFlags));
// every property always has a onXyzChanged slot, even if the NOTIFY // every property always has a onXyzChanged slot, even if the NOTIFY
// signal has a different name // signal has a different name
@@ -951,7 +1000,7 @@ MemberProcessor::~MemberProcessor()
{ {
} }
bool MemberProcessor::processProperty(const QString &, const Value *) bool MemberProcessor::processProperty(const QString &, const Value *, const PropertyInfo &)
{ {
return true; return true;
} }
@@ -1024,7 +1073,12 @@ void ObjectValue::setPrototype(const Value *prototype)
void ObjectValue::setMember(const QString &name, const Value *value) void ObjectValue::setMember(const QString &name, const Value *value)
{ {
m_members[name] = value; m_members[name].value = value;
}
void ObjectValue::setPropertyInfo(const QString &name, const PropertyInfo &propertyInfo)
{
m_members[name].propertyInfo = propertyInfo;
} }
void ObjectValue::removeMember(const QString &name) void ObjectValue::removeMember(const QString &name)
@@ -1063,12 +1117,12 @@ bool ObjectValue::checkPrototype(const ObjectValue *, QSet<const ObjectValue *>
void ObjectValue::processMembers(MemberProcessor *processor) const void ObjectValue::processMembers(MemberProcessor *processor) const
{ {
QHashIterator<QString, const Value *> it(m_members); QHashIterator<QString, PropertyData> it(m_members);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
if (! processor->processProperty(it.key(), it.value())) if (! processor->processProperty(it.key(), it.value().value, it.value().propertyInfo))
break; break;
} }
} }
@@ -1077,7 +1131,7 @@ const Value *ObjectValue::lookupMember(const QString &name, const Context *conte
const ObjectValue **foundInObject, const ObjectValue **foundInObject,
bool examinePrototypes) const bool examinePrototypes) const
{ {
if (const Value *m = m_members.value(name)) { if (const Value *m = m_members.value(name).value) {
if (foundInObject) if (foundInObject)
*foundInObject = this; *foundInObject = this;
return m; return m;
@@ -1849,7 +1903,10 @@ bool ASTObjectValue::getSourceLocation(QString *fileName, int *line, int *column
void ASTObjectValue::processMembers(MemberProcessor *processor) const void ASTObjectValue::processMembers(MemberProcessor *processor) const
{ {
foreach (ASTPropertyReference *ref, m_properties) { foreach (ASTPropertyReference *ref, m_properties) {
processor->processProperty(ref->ast()->name.toString(), ref); uint pFlags = PropertyInfo::Readable;
if (!ref->ast()->isReadonlyMember)
pFlags |= PropertyInfo::Writeable;
processor->processProperty(ref->ast()->name.toString(), ref, PropertyInfo(pFlags));
// ### Should get a different value? // ### Should get a different value?
processor->processGeneratedSlot(ref->onChangedSlotName(), ref); processor->processGeneratedSlot(ref->onChangedSlotName(), ref);
} }
@@ -2334,7 +2391,7 @@ void TypeScope::processMembers(MemberProcessor *processor) const
continue; continue;
if (!info.as().isEmpty()) if (!info.as().isEmpty())
processor->processProperty(info.as(), import); processor->processProperty(info.as(), import, PropertyInfo(PropertyInfo::Readable));
else else
import->processMembers(processor); import->processMembers(processor);
} }
@@ -2387,7 +2444,7 @@ void JSImportScope::processMembers(MemberProcessor *processor) const
const ImportInfo &info = i.info; const ImportInfo &info = i.info;
if (info.type() == ImportType::File || info.type() == ImportType::QrcFile) if (info.type() == ImportType::File || info.type() == ImportType::QrcFile)
processor->processProperty(info.as(), import); processor->processProperty(info.as(), import, PropertyInfo(PropertyInfo::Readable));
} }
} }
@@ -2513,9 +2570,9 @@ class MemberDumper: public MemberProcessor
public: public:
MemberDumper() {} MemberDumper() {}
virtual bool processProperty(const QString &name, const Value *) virtual bool processProperty(const QString &name, const Value *, const PropertyInfo &pInfo)
{ {
qCDebug(qmljsLog) << "property: " << name; qCDebug(qmljsLog) << "property: " << name << " flags:" << pInfo.toString();
return true; return true;
} }

View File

@@ -388,6 +388,44 @@ public:
const UrlValue *asUrlValue() const QTC_OVERRIDE; const UrlValue *asUrlValue() const QTC_OVERRIDE;
}; };
class PropertyInfo {
public:
enum PropertyFlag {
Readable = 1,
Writeable = 2,
ListType = 4,
PointerType= 8,
ValueType = 16,
PointerOrValue = PointerType|ValueType,
Default = Readable|Writeable|PointerOrValue
};
PropertyInfo(uint flags = Default);
uint flags;
bool isPointer() const {
return (flags & PointerOrValue) == PointerType;
}
bool isValue() const {
return (flags & PointerOrValue) == ValueType;
}
bool canBePointer() const {
return (flags & PointerType) != 0;
}
bool canBeValue() const {
return (flags & ValueType) != 0;
}
bool isReadable() const {
return (flags & Readable) != 0;
}
bool isWriteable() const {
return (flags & Writeable) != 0;
}
bool isList() const {
return (flags & ListType) != 0;
}
QString toString() const;
};
class QMLJS_EXPORT MemberProcessor class QMLJS_EXPORT MemberProcessor
{ {
MemberProcessor(const MemberProcessor &other); MemberProcessor(const MemberProcessor &other);
@@ -398,7 +436,8 @@ public:
virtual ~MemberProcessor(); virtual ~MemberProcessor();
// Returns false to stop the processor. // Returns false to stop the processor.
virtual bool processProperty(const QString &name, const Value *value); virtual bool processProperty(const QString &name, const Value *value,
const PropertyInfo &propertyInfo);
virtual bool processEnumerator(const QString &name, const Value *value); virtual bool processEnumerator(const QString &name, const Value *value);
virtual bool processSignal(const QString &name, const Value *value); virtual bool processSignal(const QString &name, const Value *value);
virtual bool processSlot(const QString &name, const Value *value); virtual bool processSlot(const QString &name, const Value *value);
@@ -440,6 +479,16 @@ public:
void accept(ValueVisitor *) const QTC_OVERRIDE; void accept(ValueVisitor *) const QTC_OVERRIDE;
}; };
class QMLJS_EXPORT PropertyData {
public:
const Value *value;
PropertyInfo propertyInfo;
PropertyData(const Value *value = 0,
PropertyInfo propertyInfo = PropertyInfo(PropertyInfo::Default))
: value(value), propertyInfo(propertyInfo)
{ }
};
class QMLJS_EXPORT ObjectValue: public Value class QMLJS_EXPORT ObjectValue: public Value
{ {
public: public:
@@ -462,6 +511,7 @@ public:
virtual void processMembers(MemberProcessor *processor) const; virtual void processMembers(MemberProcessor *processor) const;
virtual void setMember(const QString &name, const Value *value); virtual void setMember(const QString &name, const Value *value);
virtual void setPropertyInfo(const QString &name, const PropertyInfo &propertyInfo);
virtual void removeMember(const QString &name); virtual void removeMember(const QString &name);
virtual const Value *lookupMember(const QString &name, const Context *context, virtual const Value *lookupMember(const QString &name, const Context *context,
@@ -484,7 +534,7 @@ private:
private: private:
ValueOwner *m_valueOwner; ValueOwner *m_valueOwner;
QHash<QString, const Value *> m_members; QHash<QString, PropertyData> m_members;
QString m_className; QString m_className;
QString m_originId; QString m_originId;

View File

@@ -278,7 +278,7 @@ class PropertyMemberProcessor : public MemberProcessor
public: public:
PropertyMemberProcessor(const ContextPtr &context) : m_context(context) PropertyMemberProcessor(const ContextPtr &context) : m_context(context)
{} {}
bool processProperty(const QString &name, const Value *value) bool processProperty(const QString &name, const Value *value, const QmlJS::PropertyInfo &)
{ {
PropertyName propertyName = name.toUtf8(); PropertyName propertyName = name.toUtf8();
const ASTPropertyReference *ref = value_cast<ASTPropertyReference>(value); const ASTPropertyReference *ref = value_cast<ASTPropertyReference>(value);

View File

@@ -245,7 +245,7 @@ private:
(*_propertyProcessor)(_currentObject, name, value); (*_propertyProcessor)(_currentObject, name, value);
} }
bool processProperty(const QString &name, const Value *value) QTC_OVERRIDE bool processProperty(const QString &name, const Value *value, const PropertyInfo &) QTC_OVERRIDE
{ {
process(name, value); process(name, value);
return true; return true;