Commit a4e85dda authored by Christian Kamm's avatar Christian Kamm

C++ insert definition: Use minimally qualified names.

Change-Id: I633dbc77557fc2b6563888103350612a262536ee
Reviewed-on: http://codereview.qt.nokia.com/2731Reviewed-by: default avatarLeandro T. C. Melo <leandro.melo@nokia.com>
parent 0e54183d
......@@ -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;
Scope *scope = env->scope();
if (name->isQualifiedNameId() || name->isTemplateNameId())
if (name->isTemplateNameId())
return FullySpecifiedType();
if (! scope)
......@@ -401,11 +414,22 @@ FullySpecifiedType UseQualifiedNames::apply(const Name *name, Rewrite *rewrite)
foreach (const LookupItem &r, results) {
if (Symbol *d = r.declaration()) {
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)
n = c;
n = names.at(i);
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);
......@@ -418,6 +442,18 @@ FullySpecifiedType UseQualifiedNames::apply(const Name *name, Rewrite *rewrite)
}
UseQualifiedNames::UseQualifiedNames()
: UseMinimalNames(0)
{
}
UseQualifiedNames::~UseQualifiedNames()
{
}
FullySpecifiedType rewriteType(const FullySpecifiedType &type,
SubstitutionEnvironment *env,
Control *control)
......
......@@ -88,15 +88,24 @@ private:
QList<QPair<const Name *, FullySpecifiedType> > _map;
};
class CPLUSPLUS_EXPORT UseQualifiedNames: public Substitution
class CPLUSPLUS_EXPORT UseMinimalNames: public Substitution
{
public:
UseQualifiedNames();
virtual ~UseQualifiedNames();
UseMinimalNames(ClassOrNamespace *target);
virtual ~UseMinimalNames();
virtual FullySpecifiedType apply(const Name *name, Rewrite *rewrite) const;
private:
ClassOrNamespace *_target;
};
class CPLUSPLUS_EXPORT UseQualifiedNames: public UseMinimalNames
{
public:
UseQualifiedNames();
virtual ~UseQualifiedNames();
};
CPLUSPLUS_EXPORT FullySpecifiedType rewriteType(const FullySpecifiedType &type,
......
......@@ -242,17 +242,33 @@ public:
oo.setShowReturnTypes(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;
env.setContext(assistInterface()->context());
env.switchScope(m_decl->enclosingScope());
UseQualifiedNames q;
UseMinimalNames q(targetCoN);
env.enter(&q);
Control *control = assistInterface()->context().control().data();
// rewrite the function type
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}";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment