C++ insert definition: Use minimally qualified names.

Change-Id: I633dbc77557fc2b6563888103350612a262536ee
Reviewed-on: http://codereview.qt.nokia.com/2731
Reviewed-by: Leandro T. C. Melo <leandro.melo@nokia.com>
This commit is contained in:
Christian Kamm
2011-07-28 21:17:28 +02:00
parent 0e54183d4d
commit a4e85dda53
3 changed files with 77 additions and 16 deletions

View File

@@ -373,22 +373,35 @@ FullySpecifiedType SubstitutionMap::apply(const Name *name, Rewrite *) const
} }
UseQualifiedNames::UseQualifiedNames() UseMinimalNames::UseMinimalNames(ClassOrNamespace *target)
: _target(target)
{ {
} }
UseQualifiedNames::~UseQualifiedNames() UseMinimalNames::~UseMinimalNames()
{ {
} }
FullySpecifiedType UseQualifiedNames::apply(const Name *name, Rewrite *rewrite) const static bool symbolIdentical(Symbol *s1, Symbol *s2)
{
if (!s1 || !s2)
return false;
if (s1->line() != s2->line())
return false;
if (s1->column() != s2->column())
return false;
return QByteArray(s1->fileName()) == QByteArray(s2->fileName());
}
FullySpecifiedType UseMinimalNames::apply(const Name *name, Rewrite *rewrite) const
{ {
SubstitutionEnvironment *env = rewrite->env; SubstitutionEnvironment *env = rewrite->env;
Scope *scope = env->scope(); Scope *scope = env->scope();
if (name->isQualifiedNameId() || name->isTemplateNameId()) if (name->isTemplateNameId())
return FullySpecifiedType(); return FullySpecifiedType();
if (! scope) if (! scope)
@@ -401,11 +414,22 @@ FullySpecifiedType UseQualifiedNames::apply(const Name *name, Rewrite *rewrite)
foreach (const LookupItem &r, results) { foreach (const LookupItem &r, results) {
if (Symbol *d = r.declaration()) { if (Symbol *d = r.declaration()) {
const Name *n = 0; const Name *n = 0;
foreach (const Name *c, LookupContext::fullyQualifiedName(d)) { QList<const Name *> names = LookupContext::fullyQualifiedName(d);
for (int i = names.size() - 1; i >= 0; --i) {
if (! n) if (! n)
n = c; n = names.at(i);
else else
n = control->qualifiedNameId(n, c); n = control->qualifiedNameId(names.at(i), n);
if (_target) {
// minimize the qualifications
const QList<LookupItem> tresults = _target->lookup(n);
foreach (const LookupItem &tr, tresults) {
if (symbolIdentical(tr.declaration(), d)) {
i = 0; // break outer
break;
}
}
}
} }
return control->namedType(n); return control->namedType(n);
@@ -418,6 +442,18 @@ FullySpecifiedType UseQualifiedNames::apply(const Name *name, Rewrite *rewrite)
} }
UseQualifiedNames::UseQualifiedNames()
: UseMinimalNames(0)
{
}
UseQualifiedNames::~UseQualifiedNames()
{
}
FullySpecifiedType rewriteType(const FullySpecifiedType &type, FullySpecifiedType rewriteType(const FullySpecifiedType &type,
SubstitutionEnvironment *env, SubstitutionEnvironment *env,
Control *control) Control *control)

View File

@@ -88,17 +88,26 @@ private:
QList<QPair<const Name *, FullySpecifiedType> > _map; QList<QPair<const Name *, FullySpecifiedType> > _map;
}; };
class CPLUSPLUS_EXPORT UseQualifiedNames: public Substitution class CPLUSPLUS_EXPORT UseMinimalNames: public Substitution
{
public:
UseMinimalNames(ClassOrNamespace *target);
virtual ~UseMinimalNames();
virtual FullySpecifiedType apply(const Name *name, Rewrite *rewrite) const;
private:
ClassOrNamespace *_target;
};
class CPLUSPLUS_EXPORT UseQualifiedNames: public UseMinimalNames
{ {
public: public:
UseQualifiedNames(); UseQualifiedNames();
virtual ~UseQualifiedNames(); virtual ~UseQualifiedNames();
virtual FullySpecifiedType apply(const Name *name, Rewrite *rewrite) const;
}; };
CPLUSPLUS_EXPORT FullySpecifiedType rewriteType(const FullySpecifiedType &type, CPLUSPLUS_EXPORT FullySpecifiedType rewriteType(const FullySpecifiedType &type,
SubstitutionEnvironment *env, SubstitutionEnvironment *env,
Control *control); Control *control);

View File

@@ -242,17 +242,33 @@ public:
oo.setShowReturnTypes(true); oo.setShowReturnTypes(true);
oo.setShowArgumentNames(true); oo.setShowArgumentNames(true);
//-- // make target lookup context
Document::Ptr targetDoc = targetFile.cppDocument();
Scope *targetScope = targetDoc->scopeAt(m_loc.line(), m_loc.column());
LookupContext targetContext(targetDoc, assistInterface()->snapshot());
ClassOrNamespace *targetCoN = targetContext.lookupType(targetScope);
if (!targetCoN)
targetCoN = targetContext.globalNamespace();
// setup rewriting to get minimally qualified names
SubstitutionEnvironment env; SubstitutionEnvironment env;
env.setContext(assistInterface()->context()); env.setContext(assistInterface()->context());
env.switchScope(m_decl->enclosingScope()); env.switchScope(m_decl->enclosingScope());
UseQualifiedNames q; UseMinimalNames q(targetCoN);
env.enter(&q); env.enter(&q);
Control *control = assistInterface()->context().control().data(); Control *control = assistInterface()->context().control().data();
// rewrite the function type
FullySpecifiedType tn = rewriteType(m_decl->type(), &env, control); FullySpecifiedType tn = rewriteType(m_decl->type(), &env, control);
QString name = oo(LookupContext::fullyQualifiedName(m_decl));
//-- // rewrite the function name
QString name;
const FullySpecifiedType nametype = rewriteType(control->namedType(m_decl->name()), &env, control);
if (NamedType *nt = nametype.type()->asNamedType()) {
name = oo(nt->name());
} else {
name = oo(LookupContext::fullyQualifiedName(m_decl));
}
QString defText = oo.prettyType(tn, name) + "\n{\n}"; QString defText = oo.prettyType(tn, name) + "\n{\n}";