C++: Cleanup NestedClassInstantiator in LookupContext

* Rename Instantiator
* Shorten some variable names

Change-Id: I0d1d6280b6157e9ebc4bbaaa77f462fe6ce233c4
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
This commit is contained in:
Orgad Shaneh
2015-04-09 09:10:21 +03:00
committed by Orgad Shaneh
parent cf4ae8c63f
commit 5566146607
2 changed files with 37 additions and 56 deletions

View File

@@ -89,17 +89,15 @@ static void path_helper(Symbol *symbol, QList<const Name *> *names)
} }
} }
static bool isNestedInstantiationEnclosingTemplate( static bool isNestedInstantiationEnclosingTemplate(ClassOrNamespace *nestedInstantiation,
ClassOrNamespace *nestedClassOrNamespaceInstantiation, ClassOrNamespace *enclosingInstantiation)
ClassOrNamespace *enclosingTemplateClassInstantiation)
{ {
QList<ClassOrNamespace *> processed; QList<ClassOrNamespace *> processed;
while (enclosingTemplateClassInstantiation while (enclosingInstantiation && !processed.contains(enclosingInstantiation)) {
&& !processed.contains(enclosingTemplateClassInstantiation)) { processed.append(enclosingInstantiation);
processed.append(enclosingTemplateClassInstantiation); if (enclosingInstantiation == nestedInstantiation)
if (enclosingTemplateClassInstantiation == nestedClassOrNamespaceInstantiation)
return false; return false;
enclosingTemplateClassInstantiation = enclosingTemplateClassInstantiation->parent(); enclosingInstantiation = enclosingInstantiation->parent();
} }
return true; return true;
@@ -538,23 +536,22 @@ ClassOrNamespace *LookupContext::lookupParent(Symbol *symbol) const
return binding; return binding;
} }
class ClassOrNamespace::NestedClassInstantiator class ClassOrNamespace::Instantiator
{ {
public: public:
NestedClassInstantiator(CreateBindings *factory, Clone &cloner, Subst &subst) Instantiator(CreateBindings *factory, Clone &cloner, Subst &subst)
: _factory(factory) : _factory(factory)
, _cloner(cloner) , _cloner(cloner)
, _subst(subst) , _subst(subst)
{} {}
void instantiate(ClassOrNamespace *enclosingTemplateClass, void instantiate(ClassOrNamespace *classOrNamespace, ClassOrNamespace *instantiation);
ClassOrNamespace *enclosingTemplateClassInstantiation);
private: private:
bool isInstantiateNestedClassNeeded(const QList<Symbol *> &symbols) const; bool isInstantiationNeeded(ClassOrNamespace *classOrNamespace) const;
bool containsTemplateType(Declaration *declaration) const; bool containsTemplateType(Declaration *declaration) const;
bool containsTemplateType(Function *function) const; bool containsTemplateType(Function *function) const;
NamedType *findNamedType(Type *memberType) const; NamedType *findNamedType(Type *memberType) const;
QSet<ClassOrNamespace *> _alreadyConsideredNestedClassInstantiations; QSet<ClassOrNamespace *> _alreadyConsideredInstantiations;
CreateBindings *_factory; CreateBindings *_factory;
Clone &_cloner; Clone &_cloner;
Subst &_subst; Subst &_subst;
@@ -1239,7 +1236,8 @@ ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name, ClassOrNamespac
} }
} }
} }
instantiateNestedClasses(reference, cloner, subst, instantiation); Instantiator instantiator(_factory, cloner, subst);
instantiator.instantiate(reference, instantiation);
} else { } else {
instantiation->_symbols.append(reference->symbols()); instantiation->_symbols.append(reference->symbols());
} }
@@ -1352,57 +1350,44 @@ ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name, ClassOrNamespac
return reference; return reference;
} }
void ClassOrNamespace::Instantiator::instantiate(ClassOrNamespace *classOrNamespace,
void ClassOrNamespace::instantiateNestedClasses(ClassOrNamespace *enclosingTemplateClass, ClassOrNamespace *instantiation)
Clone &cloner,
Subst &subst,
ClassOrNamespace *enclosingTemplateClassInstantiation)
{ {
NestedClassInstantiator nestedClassInstantiator(_factory, cloner, subst); if (_alreadyConsideredInstantiations.contains(classOrNamespace))
nestedClassInstantiator.instantiate(enclosingTemplateClass, enclosingTemplateClassInstantiation);
}
void ClassOrNamespace::NestedClassInstantiator::instantiate(ClassOrNamespace *enclosingTemplateClass,
ClassOrNamespace *enclosingTemplateClassInstantiation)
{
if (_alreadyConsideredNestedClassInstantiations.contains(enclosingTemplateClass))
return; return;
_alreadyConsideredNestedClassInstantiations.insert(enclosingTemplateClass); _alreadyConsideredInstantiations.insert(classOrNamespace);
ClassOrNamespace::Table::const_iterator cit = enclosingTemplateClass->_classOrNamespaces.begin(); ClassOrNamespace::Table::const_iterator cit = classOrNamespace->_classOrNamespaces.begin();
for (; cit != enclosingTemplateClass->_classOrNamespaces.end(); ++cit) { for (; cit != classOrNamespace->_classOrNamespaces.end(); ++cit) {
const Name *nestedName = cit->first; const Name *nestedName = cit->first;
ClassOrNamespace *nestedClassOrNamespace = cit->second; ClassOrNamespace *nestedClassOrNamespace = cit->second;
ClassOrNamespace *nestedClassOrNamespaceInstantiation = nestedClassOrNamespace; ClassOrNamespace *nestedInstantiation = nestedClassOrNamespace;
if (isInstantiateNestedClassNeeded(nestedClassOrNamespace->_symbols)) { if (isInstantiationNeeded(nestedClassOrNamespace)) {
nestedClassOrNamespaceInstantiation = _factory->allocClassOrNamespace(nestedClassOrNamespace); nestedInstantiation = _factory->allocClassOrNamespace(nestedClassOrNamespace);
nestedClassOrNamespaceInstantiation->_enums.append(nestedClassOrNamespace->unscopedEnums()); nestedInstantiation->_enums.append(nestedClassOrNamespace->unscopedEnums());
nestedClassOrNamespaceInstantiation->_usings.append(nestedClassOrNamespace->usings()); nestedInstantiation->_usings.append(nestedClassOrNamespace->usings());
nestedClassOrNamespaceInstantiation->_instantiationOrigin = nestedClassOrNamespace; nestedInstantiation->_instantiationOrigin = nestedClassOrNamespace;
foreach (Symbol *s, nestedClassOrNamespace->_symbols) { foreach (Symbol *s, nestedClassOrNamespace->_symbols) {
Symbol *clone = _cloner.symbol(s, &_subst); Symbol *clone = _cloner.symbol(s, &_subst);
if (!clone->enclosingScope()) // Not from the cache but just cloned. if (!clone->enclosingScope()) // Not from the cache but just cloned.
clone->setEnclosingScope(s->enclosingScope()); clone->setEnclosingScope(s->enclosingScope());
nestedClassOrNamespaceInstantiation->_symbols.append(clone); nestedInstantiation->_symbols.append(clone);
} }
} }
if (isNestedInstantiationEnclosingTemplate(nestedClassOrNamespaceInstantiation, if (isNestedInstantiationEnclosingTemplate(nestedInstantiation, classOrNamespace))
enclosingTemplateClass)) { nestedInstantiation->_parent = instantiation;
nestedClassOrNamespaceInstantiation->_parent = enclosingTemplateClassInstantiation; instantiate(nestedClassOrNamespace, nestedInstantiation);
}
instantiate(nestedClassOrNamespace, nestedClassOrNamespaceInstantiation);
enclosingTemplateClassInstantiation->_classOrNamespaces[nestedName] = instantiation->_classOrNamespaces[nestedName] = nestedInstantiation;
nestedClassOrNamespaceInstantiation;
} }
_alreadyConsideredNestedClassInstantiations.remove(enclosingTemplateClass); _alreadyConsideredInstantiations.remove(classOrNamespace);
} }
bool ClassOrNamespace::NestedClassInstantiator::isInstantiateNestedClassNeeded(const QList<Symbol *> &symbols) const bool ClassOrNamespace::Instantiator::isInstantiationNeeded(ClassOrNamespace *classOrNamespace) const
{ {
foreach (Symbol *s, symbols) { foreach (Symbol *s, classOrNamespace->_symbols) {
if (Class *klass = s->asClass()) { if (Class *klass = s->asClass()) {
int memberCount = klass->memberCount(); int memberCount = klass->memberCount();
for (int i = 0; i < memberCount; ++i) { for (int i = 0; i < memberCount; ++i) {
@@ -1421,14 +1406,14 @@ bool ClassOrNamespace::NestedClassInstantiator::isInstantiateNestedClassNeeded(c
return false; return false;
} }
bool ClassOrNamespace::NestedClassInstantiator::containsTemplateType(Declaration *declaration) const bool ClassOrNamespace::Instantiator::containsTemplateType(Declaration *declaration) const
{ {
Type *memberType = declaration->type().type(); Type *memberType = declaration->type().type();
NamedType *namedType = findNamedType(memberType); NamedType *namedType = findNamedType(memberType);
return namedType && _subst.contains(namedType->name()); return namedType && _subst.contains(namedType->name());
} }
bool ClassOrNamespace::NestedClassInstantiator::containsTemplateType(Function *function) const bool ClassOrNamespace::Instantiator::containsTemplateType(Function *function) const
{ {
Type *returnType = function->returnType().type(); Type *returnType = function->returnType().type();
NamedType *namedType = findNamedType(returnType); NamedType *namedType = findNamedType(returnType);
@@ -1436,7 +1421,7 @@ bool ClassOrNamespace::NestedClassInstantiator::containsTemplateType(Function *f
//TODO: in future we will need also check function arguments, for now returned value is enough //TODO: in future we will need also check function arguments, for now returned value is enough
} }
NamedType *ClassOrNamespace::NestedClassInstantiator::findNamedType(Type *memberType) const NamedType *ClassOrNamespace::Instantiator::findNamedType(Type *memberType) const
{ {
if (NamedType *namedType = memberType->asNamedType()) if (NamedType *namedType = memberType->asNamedType())
return namedType; return namedType;

View File

@@ -129,10 +129,6 @@ private:
ClassOrNamespace *nestedType(const Name *name, ClassOrNamespace *origin); ClassOrNamespace *nestedType(const Name *name, ClassOrNamespace *origin);
void instantiateNestedClasses(ClassOrNamespace *enclosingTemplateClass,
Clone &cloner,
Subst &subst,
ClassOrNamespace *enclosingTemplateClassInstantiation);
ClassOrNamespace *findSpecialization(const TemplateNameId *templId, ClassOrNamespace *findSpecialization(const TemplateNameId *templId,
const TemplateNameIdTable &specializations); const TemplateNameIdTable &specializations);
@@ -161,7 +157,7 @@ private:
Class *_rootClass; Class *_rootClass;
class NestedClassInstantiator; class Instantiator;
public: public:
const Name *_name; // For debug const Name *_name; // For debug