Commit 29ac9fc6 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh

C++: Remove DeprecatedGenTemplateInstance

It's, well, deprecated...

Change-Id: Ie9d7e80345a8d9404f702dd877b3e940a1a49d93
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent e5d8dbd0
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "DeprecatedGenTemplateInstance.h"
#include "Overview.h"
#include <cplusplus/Control.h>
#include <cplusplus/Scope.h>
#include <cplusplus/Names.h>
#include <cplusplus/Symbols.h>
#include <cplusplus/CoreTypes.h>
#include <cplusplus/Literals.h>
#include <QVarLengthArray>
#include <QDebug>
using namespace CPlusPlus;
namespace {
class ApplySubstitution
{
public:
ApplySubstitution(Control *control, Symbol *symbol, const DeprecatedGenTemplateInstance::Substitution &substitution);
~ApplySubstitution();
inline Control *control() const { return _control; }
FullySpecifiedType apply(const Name *name);
FullySpecifiedType apply(const FullySpecifiedType &type);
int findSubstitution(const Identifier *id) const;
FullySpecifiedType applySubstitution(int index) const;
private:
class ApplyToType: protected TypeVisitor
{
public:
ApplyToType(ApplySubstitution *q)
: q(q) {}
FullySpecifiedType operator()(const FullySpecifiedType &ty)
{
FullySpecifiedType previousType = switchType(ty);
accept(ty.type());
return switchType(previousType);
}
protected:
using TypeVisitor::visit;
Control *control() const
{ return q->control(); }
FullySpecifiedType switchType(const FullySpecifiedType &type)
{
FullySpecifiedType previousType = _type;
_type = type;
return previousType;
}
virtual void visit(VoidType *)
{
// nothing to do
}
virtual void visit(IntegerType *)
{
// nothing to do
}
virtual void visit(FloatType *)
{
// nothing to do
}
virtual void visit(PointerToMemberType *)
{
qDebug() << Q_FUNC_INFO; // ### TODO
}
virtual void visit(PointerType *ptrTy)
{
_type.setType(control()->pointerType(q->apply(ptrTy->elementType())));
}
virtual void visit(ReferenceType *refTy)
{
_type.setType(control()->referenceType(q->apply(refTy->elementType()), refTy->isRvalueReference()));
}
virtual void visit(ArrayType *arrayTy)
{
_type.setType(control()->arrayType(q->apply(arrayTy->elementType()), arrayTy->size()));
}
virtual void visit(NamedType *ty)
{
FullySpecifiedType n = q->apply(ty->name());
_type.setType(n.type());
}
virtual void visit(Function *funTy)
{
Function *fun = control()->newFunction(/*sourceLocation=*/ 0, funTy->name());
fun->setEnclosingScope(funTy->enclosingScope());
fun->setConst(funTy->isConst());
fun->setVolatile(funTy->isVolatile());
fun->setVirtual(funTy->isVirtual());
fun->setOverride(funTy->isOverride());
fun->setFinal(funTy->isFinal());
fun->setAmbiguous(funTy->isAmbiguous());
fun->setVariadic(funTy->isVariadic());
fun->setReturnType(q->apply(funTy->returnType()));
for (unsigned i = 0, argc = funTy->argumentCount(); i < argc; ++i) {
Argument *originalArgument = funTy->argumentAt(i)->asArgument();
Argument *arg = control()->newArgument(/*sourceLocation*/ 0,
originalArgument->name());
arg->setType(q->apply(originalArgument->type()));
arg->setInitializer(originalArgument->initializer());
fun->addMember(arg);
}
_type.setType(fun);
}
virtual void visit(Namespace *)
{
qDebug() << Q_FUNC_INFO;
}
virtual void visit(Class *)
{
qDebug() << Q_FUNC_INFO;
}
virtual void visit(Enum *)
{
qDebug() << Q_FUNC_INFO;
}
virtual void visit(ForwardClassDeclaration *)
{
qDebug() << Q_FUNC_INFO;
}
virtual void visit(ObjCClass *)
{
qDebug() << Q_FUNC_INFO;
}
virtual void visit(ObjCProtocol *)
{
qDebug() << Q_FUNC_INFO;
}
virtual void visit(ObjCMethod *)
{
qDebug() << Q_FUNC_INFO;
}
virtual void visit(ObjCForwardClassDeclaration *)
{
qDebug() << Q_FUNC_INFO;
}
virtual void visit(ObjCForwardProtocolDeclaration *)
{
qDebug() << Q_FUNC_INFO;
}
private:
ApplySubstitution *q;
FullySpecifiedType _type;
QHash<Symbol *, FullySpecifiedType> _processed;
};
class ApplyToName: protected NameVisitor
{
public:
ApplyToName(ApplySubstitution *q): q(q) {}
FullySpecifiedType operator()(const Name *name)
{
FullySpecifiedType previousType = switchType(FullySpecifiedType());
accept(name);
return switchType(previousType);
}
protected:
Control *control() const
{ return q->control(); }
int findSubstitution(const Identifier *id) const
{ return q->findSubstitution(id); }
FullySpecifiedType applySubstitution(int index) const
{ return q->applySubstitution(index); }
FullySpecifiedType switchType(const FullySpecifiedType &type)
{
FullySpecifiedType previousType = _type;
_type = type;
return previousType;
}
virtual void visit(const Identifier *name)
{
int index = findSubstitution(name->identifier());
if (index != -1)
_type = applySubstitution(index);
else
_type = control()->namedType(name);
}
virtual void visit(const TemplateNameId *name)
{
QVarLengthArray<FullySpecifiedType, 8> arguments(name->templateArgumentCount());
for (unsigned i = 0; i < name->templateArgumentCount(); ++i) {
FullySpecifiedType argTy = name->templateArgumentAt(i);
arguments[i] = q->apply(argTy);
}
const TemplateNameId *templId = control()->templateNameId(name->identifier(),
name->isSpecialization(),
arguments.data(),
arguments.size());
_type = control()->namedType(templId);
}
const Name *instantiate(const Name *name)
{
if (! name)
return name;
if (const Identifier *nameId = name->asNameId()) {
const Identifier *id = control()->identifier(nameId->chars(), nameId->size());
return id;
} else if (const TemplateNameId *templId = name->asTemplateNameId()) {
QVarLengthArray<FullySpecifiedType, 8> arguments(templId->templateArgumentCount());
for (unsigned templateArgIndex = 0; templateArgIndex < templId->templateArgumentCount();
++templateArgIndex) {
FullySpecifiedType argTy = templId->templateArgumentAt(templateArgIndex);
arguments[templateArgIndex] = q->apply(argTy);
}
const Identifier *id = control()->identifier(templId->identifier()->chars(),
templId->identifier()->size());
return control()->templateNameId(id, templId->isSpecialization(), arguments.data(),
arguments.size());
} else if (const QualifiedNameId *qq = name->asQualifiedNameId()) {
const Name *base = instantiate(qq->base());
const Name *name = instantiate(qq->name());
return control()->qualifiedNameId(base, name);
} else if (const OperatorNameId *op = name->asOperatorNameId()) {
return control()->operatorNameId(op->kind());
} else if (const ConversionNameId *c = name->asConversionNameId()) {
FullySpecifiedType ty = q->apply(c->type());
return control()->conversionNameId(ty);
}
return 0;
}
virtual void visit(const QualifiedNameId *name)
{
if (const Name *n = instantiate(name))
_type = control()->namedType(n);
}
virtual void visit(const DestructorNameId *name)
{
Overview oo;
qWarning() << "ignored name:" << oo.prettyName(name);
}
virtual void visit(const OperatorNameId *name)
{
Overview oo;
qWarning() << "ignored name:" << oo.prettyName(name);
}
virtual void visit(const ConversionNameId *name)
{
Overview oo;
qWarning() << "ignored name:" << oo.prettyName(name);
}
virtual void visit(const SelectorNameId *name)
{
Overview oo;
qWarning() << "ignored name:" << oo.prettyName(name);
}
private:
ApplySubstitution *q;
FullySpecifiedType _type;
};
public: // attributes
Control *_control;
Symbol *symbol;
DeprecatedGenTemplateInstance::Substitution substitution;
ApplyToType applyToType;
ApplyToName applyToName;
};
ApplySubstitution::ApplySubstitution(Control *control, Symbol *symbol,
const DeprecatedGenTemplateInstance::Substitution &substitution)
: _control(control), symbol(symbol),
substitution(substitution),
applyToType(this), applyToName(this)
{ }
ApplySubstitution::~ApplySubstitution()
{
}
FullySpecifiedType ApplySubstitution::apply(const Name *name)
{
FullySpecifiedType ty = applyToName(name);
return ty;
}
FullySpecifiedType ApplySubstitution::apply(const FullySpecifiedType &type)
{
FullySpecifiedType ty = applyToType(type);
return ty;
}
int ApplySubstitution::findSubstitution(const Identifier *id) const
{
Q_ASSERT(id != 0);
for (int index = 0; index < substitution.size(); ++index) {
QPair<const Identifier *, FullySpecifiedType> s = substitution.at(index);
if (id->match(s.first))
return index;
}
return -1;
}
FullySpecifiedType ApplySubstitution::applySubstitution(int index) const
{
Q_ASSERT(index != -1);
Q_ASSERT(index < substitution.size());
return substitution.at(index).second;
}
} // end of anonymous namespace
DeprecatedGenTemplateInstance::DeprecatedGenTemplateInstance(QSharedPointer<Control> control, const Substitution &substitution)
: _control(control),
_substitution(substitution)
{ }
FullySpecifiedType DeprecatedGenTemplateInstance::gen(Symbol *symbol)
{
ApplySubstitution o(_control.data(), symbol, _substitution);
return o.apply(symbol->type());
}
FullySpecifiedType DeprecatedGenTemplateInstance::instantiate(const Name *className, Symbol *candidate,
QSharedPointer<Control> control)
{
if (className) {
if (const TemplateNameId *templId = className->asTemplateNameId()) {
if (Template *templ = candidate->enclosingTemplate()) {
DeprecatedGenTemplateInstance::Substitution subst;
for (unsigned i = 0; i < templId->templateArgumentCount(); ++i) {
FullySpecifiedType templArgTy = templId->templateArgumentAt(i);
if (i < templ->templateParameterCount()) {
const Name *templArgName = templ->templateParameterAt(i)->name();
if (templArgName && templArgName->identifier()) {
const Identifier *templArgId = templArgName->identifier();
subst.append(qMakePair(templArgId, templArgTy));
}
}
}
DeprecatedGenTemplateInstance inst(control, subst);
return inst.gen(candidate);
}
}
}
return candidate->type();
}
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CPLUSPLUS_DEPRECATEDGENTEMPLATEINSTANCE_H
#define CPLUSPLUS_DEPRECATEDGENTEMPLATEINSTANCE_H
#include <cplusplus/TypeVisitor.h>
#include <cplusplus/NameVisitor.h>
#include <cplusplus/FullySpecifiedType.h>
#include <QList>
#include <QPair>
#include <QSharedPointer>
namespace CPlusPlus {
class CPLUSPLUS_EXPORT DeprecatedGenTemplateInstance
{
public:
typedef QList< QPair<const Identifier *, FullySpecifiedType> > Substitution;
public:
static FullySpecifiedType instantiate(const Name *className, Symbol *candidate, QSharedPointer<Control> control);
private:
DeprecatedGenTemplateInstance(QSharedPointer<Control> control, const Substitution &substitution);
FullySpecifiedType gen(Symbol *symbol);
private:
QSharedPointer<Control> _control;
const Substitution _substitution;
};
} // namespace CPlusPlus
#endif // CPLUSPLUS_DEPRECATEDGENTEMPLATEINSTANCE_H
......@@ -105,8 +105,6 @@ QString CPlusPlus::toString(const ClassOrNamespace *binding, QString id)
QString result = QString::fromLatin1("%0: %1 symbols").arg(
id,
QString::number(binding->symbols().length()));
if (binding->templateId())
result.append(QString::fromLatin1("\n%1").arg(indent(toString(binding->templateId(), QLatin1String("Template")))));
return result;
}
......
......@@ -32,7 +32,6 @@
#include "ResolveExpression.h"
#include "Overview.h"
#include "DeprecatedGenTemplateInstance.h"
#include "CppRewriter.h"
#include <cplusplus/CoreTypes.h>
......@@ -399,7 +398,7 @@ QList<LookupItem> LookupContext::lookup(const Name *name, Scope *scope) const
for (; scope; scope = scope->enclosingScope()) {
if (name->identifier() != 0 && scope->isBlock()) {
bindings()->lookupInScope(name, scope, &candidates, /*templateId = */ 0, /*binding=*/ 0);
bindings()->lookupInScope(name, scope, &candidates);
if (! candidates.isEmpty()) {
// it's a local.
......@@ -435,7 +434,7 @@ QList<LookupItem> LookupContext::lookup(const Name *name, Scope *scope) const
}
} else if (Function *fun = scope->asFunction()) {
bindings()->lookupInScope(name, fun, &candidates, /*templateId = */ 0, /*binding=*/ 0);
bindings()->lookupInScope(name, fun, &candidates);
if (! candidates.isEmpty()) {
// it's an argument or a template parameter.
......@@ -462,13 +461,13 @@ QList<LookupItem> LookupContext::lookup(const Name *name, Scope *scope) const
// continue, and look at the enclosing scope.
} else if (ObjCMethod *method = scope->asObjCMethod()) {
bindings()->lookupInScope(name, method, &candidates, /*templateId = */ 0, /*binding=*/ 0);
bindings()->lookupInScope(name, method, &candidates);
if (! candidates.isEmpty())
break; // it's a formal argument.
} else if (Template *templ = scope->asTemplate()) {
bindings()->lookupInScope(name, templ, &candidates, /*templateId = */ 0, /*binding=*/ 0);
bindings()->lookupInScope(name, templ, &candidates);
if (! candidates.isEmpty()) {
// it's a template parameter.
......@@ -555,7 +554,6 @@ ClassOrNamespace::ClassOrNamespace(CreateBindings *factory, ClassOrNamespace *pa
: _factory(factory)
, _parent(parent)
, _scopeLookupCache(0)
, _templateId(0)
, _instantiationOrigin(0)
, _rootClass(0)
, _name(0)
......@@ -568,11 +566,6 @@ ClassOrNamespace::~ClassOrNamespace()
delete _scopeLookupCache;
}
const TemplateNameId *ClassOrNamespace::templateId() const
{
return _templateId;
}
ClassOrNamespace *ClassOrNamespace::instantiationOrigin() const
{
return _instantiationOrigin;
......@@ -672,7 +665,7 @@ QList<LookupItem> ClassOrNamespace::lookup_helper(const Name *name, bool searchI
if (processedOwnParents.contains(binding))
break;
processedOwnParents.insert(binding);
lookup_helper(name, binding, &result, &processed, /*templateId = */ 0);
lookup_helper(name, binding, &result, &processed);
binding = binding->_parent;
} while (searchInEnclosingScope && binding);
}
......@@ -681,9 +674,8 @@ QList<LookupItem> ClassOrNamespace::lookup_helper(const Name *name, bool searchI
}
void ClassOrNamespace::lookup_helper(const Name *name, ClassOrNamespace *binding,
QList<LookupItem> *result,
QSet<ClassOrNamespace *> *processed,
const TemplateNameId *templateId)
QList<LookupItem> *result,
QSet<ClassOrNamespace *> *processed)
{
if (binding && ! processed->contains(binding)) {
processed->insert(binding);
......@@ -708,15 +700,15 @@ void ClassOrNamespace::lookup_helper(const Name *name, ClassOrNamespace *binding
}
}
}
_factory->lookupInScope(name, scope, result, templateId, binding);
_factory->lookupInScope(name, scope, result, binding);
}
}
foreach (Enum *e, binding->unscopedEnums())
_factory->lookupInScope(name, e, result, templateId, binding);
_factory->lookupInScope(name, e, result, binding);