Skip to content
Snippets Groups Projects
DeprecatedGenTemplateInstance.cpp 13 KiB
Newer Older
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "DeprecatedGenTemplateInstance.h"
#include "Overview.h"

#include <Control.h>
#include <Scope.h>
#include <Names.h>
#include <Symbols.h>
#include <CoreTypes.h>
#include <Literals.h>

#include <QtCore/QVarLengthArray>
#include <QtCore/QDebug>

using namespace CPlusPlus;

class ApplySubstitution
{
public:
    ApplySubstitution(Control *control, Symbol *symbol, const DeprecatedGenTemplateInstance::Substitution &substitution);
    ~ApplySubstitution();

    inline Control *control() const { return _control; }
Roberto Raggi's avatar
Roberto Raggi committed
    FullySpecifiedType apply(const Name *name);
Roberto Raggi's avatar
Roberto Raggi committed
    FullySpecifiedType apply(const FullySpecifiedType &type);
Roberto Raggi's avatar
Roberto Raggi committed
    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)
        {
Roberto Raggi's avatar
Roberto Raggi committed
            _type.setType(control()->pointerType(q->apply(ptrTy->elementType())));
        }

        virtual void visit(ReferenceType *refTy)
        {
Roberto Raggi's avatar
Roberto Raggi committed
            _type.setType(control()->referenceType(q->apply(refTy->elementType())));
        }

        virtual void visit(ArrayType *arrayTy)
        {
Roberto Raggi's avatar
Roberto Raggi committed
            _type.setType(control()->arrayType(q->apply(arrayTy->elementType()), arrayTy->size()));
        }

        virtual void visit(NamedType *ty)
        {
Roberto Raggi's avatar
Roberto Raggi committed
            FullySpecifiedType n = q->apply(ty->name());
            _type.setType(n.type());
        }

        virtual void visit(Function *funTy)
        {
            Function *fun = control()->newFunction(/*sourceLocation=*/ 0, funTy->name());
            fun->setScope(funTy->scope());
            fun->setConst(funTy->isConst());
            fun->setVolatile(funTy->isVolatile());
            fun->setVirtual(funTy->isVirtual());
            fun->setAmbiguous(funTy->isAmbiguous());
            fun->setVariadic(funTy->isVariadic());

Roberto Raggi's avatar
Roberto Raggi committed
            fun->setReturnType(q->apply(funTy->returnType()));

            for (unsigned i = 0; i < funTy->argumentCount(); ++i) {
                Argument *originalArgument = funTy->argumentAt(i)->asArgument();
                Argument *arg = control()->newArgument(/*sourceLocation*/ 0,
                                                       originalArgument->name());

Roberto Raggi's avatar
Roberto Raggi committed
                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;
Roberto Raggi's avatar
Roberto Raggi committed
        QHash<Symbol *, FullySpecifiedType> _processed;
    };

    class ApplyToName: protected NameVisitor
    {
    public:
        ApplyToName(ApplySubstitution *q): q(q) {}

Roberto Raggi's avatar
Roberto Raggi committed
        FullySpecifiedType operator()(const Name *name)
        {
            FullySpecifiedType previousType = switchType(FullySpecifiedType());
            accept(name);
            return switchType(previousType);
        }

    protected:
        Control *control() const
        { return q->control(); }

Roberto Raggi's avatar
Roberto Raggi committed
        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;
        }

Roberto Raggi's avatar
Roberto Raggi committed
        virtual void visit(const NameId *name)
        {
            int index = findSubstitution(name->identifier());

            if (index != -1)
                _type = applySubstitution(index);

            else
                _type = control()->namedType(name);
        }

Roberto Raggi's avatar
Roberto Raggi committed
        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);
Roberto Raggi's avatar
Roberto Raggi committed
                arguments[i] = q->apply(argTy);
Roberto Raggi's avatar
Roberto Raggi committed
            const TemplateNameId *templId = control()->templateNameId(name->identifier(),
                                                                      arguments.data(),
                                                                      arguments.size());
            _type = control()->namedType(templId);
        }

        const Name *instantiate(const Name *name)
            if (! name)
                return name;

            else if (const Name *nameId = name->asNameId()) {
                const Identifier *id = control()->identifier(nameId->identifier()->chars(),
                                                                         nameId->identifier()->size());
                return control()->nameId(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, 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);
Roberto Raggi's avatar
Roberto Raggi committed
        virtual void visit(const DestructorNameId *name)
        {
            Overview oo;
            qWarning() << "ignored name:" << oo(name);
        }

Roberto Raggi's avatar
Roberto Raggi committed
        virtual void visit(const OperatorNameId *name)
        {
            Overview oo;
            qWarning() << "ignored name:" << oo(name);
        }

Roberto Raggi's avatar
Roberto Raggi committed
        virtual void visit(const ConversionNameId *name)
        {
            Overview oo;
            qWarning() << "ignored name:" << oo(name);
        }

Roberto Raggi's avatar
Roberto Raggi committed
        virtual void visit(const SelectorNameId *name)
        {
            Overview oo;
            qWarning() << "ignored name:" << oo(name);
        }

    private:
        ApplySubstitution *q;
        FullySpecifiedType _type;
    };

public: // attributes
    Control *_control;
Roberto Raggi's avatar
Roberto Raggi committed
    Symbol *symbol;
    DeprecatedGenTemplateInstance::Substitution substitution;
    ApplyToType applyToType;
    ApplyToName applyToName;
};

ApplySubstitution::ApplySubstitution(Control *control, Symbol *symbol,
                                     const DeprecatedGenTemplateInstance::Substitution &substitution)
    : _control(control), symbol(symbol),
Roberto Raggi's avatar
Roberto Raggi committed
      substitution(substitution),
      applyToType(this), applyToName(this)
ApplySubstitution::~ApplySubstitution()
Roberto Raggi's avatar
Roberto Raggi committed
FullySpecifiedType ApplySubstitution::apply(const Name *name)
Roberto Raggi's avatar
Roberto Raggi committed
{
    FullySpecifiedType ty = applyToName(name);
    return ty;
}
Roberto Raggi's avatar
Roberto Raggi committed
FullySpecifiedType ApplySubstitution::apply(const FullySpecifiedType &type)
{
    FullySpecifiedType ty = applyToType(type);
    return ty;
}
Roberto Raggi's avatar
Roberto Raggi committed
int ApplySubstitution::findSubstitution(const Identifier *id) const
    for (int index = 0; index < substitution.size(); ++index) {
Roberto Raggi's avatar
Roberto Raggi committed
        QPair<const Identifier *, FullySpecifiedType> s = substitution.at(index);
        if (id->isEqualTo(s.first))
            return index;
    }
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);
Roberto Raggi's avatar
Roberto Raggi committed
    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();
}