CppRewriter.cpp 12.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** 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.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30

31
#include "CppRewriter.h"
32 33 34 35 36 37 38 39 40 41

#include "Overview.h"

#include <cplusplus/TypeVisitor.h>
#include <cplusplus/NameVisitor.h>
#include <cplusplus/CoreTypes.h>
#include <cplusplus/Symbols.h>
#include <cplusplus/Literals.h>
#include <cplusplus/Names.h>
#include <cplusplus/Scope.h>
42

43 44
#include <QVarLengthArray>
#include <QDebug>
45

46
namespace CPlusPlus {
47

48
class Rewrite
49 50
{
public:
51
    Rewrite(Control *control, SubstitutionEnvironment *env)
52 53 54 55 56 57 58 59 60 61 62 63 64 65
        : control(control), env(env), rewriteType(this), rewriteName(this) {}

    class RewriteType: public TypeVisitor
    {
        Rewrite *rewrite;
        QList<FullySpecifiedType> temps;

        Control *control() const
        { return rewrite->control; }

        void accept(const FullySpecifiedType &ty)
        {
            TypeVisitor::accept(ty.type());
            unsigned flags = ty.flags();
66 67 68 69
            if (!temps.isEmpty()) {
                flags |= temps.back().flags();
                temps.back().setFlags(flags);
            }
70 71 72 73 74 75 76 77
        }

    public:
        RewriteType(Rewrite *r): rewrite(r) {}

        FullySpecifiedType operator()(const FullySpecifiedType &ty)
        {
            accept(ty);
78
            return (!temps.isEmpty()) ? temps.takeLast() : ty;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        }

        virtual void visit(UndefinedType *)
        {
            temps.append(FullySpecifiedType());
        }

        virtual void visit(VoidType *)
        {
            temps.append(control()->voidType());
        }

        virtual void visit(IntegerType *type)
        {
            temps.append(control()->integerType(type->kind()));
        }

        virtual void visit(FloatType *type)
        {
            temps.append(control()->floatType(type->kind()));
        }

        virtual void visit(PointerToMemberType *type)
        {
            const Name *memberName = rewrite->rewriteName(type->memberName());
            const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
            temps.append(control()->pointerToMemberType(memberName, elementType));
        }

        virtual void visit(PointerType *type)
        {
            const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
            temps.append(control()->pointerType(elementType));
        }

        virtual void visit(ReferenceType *type)
        {
            const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
117
            temps.append(control()->referenceType(elementType, type->isRvalueReference()));
118 119 120 121 122 123 124 125 126 127
        }

        virtual void visit(ArrayType *type)
        {
            const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
            temps.append(control()->arrayType(elementType, type->size()));
        }

        virtual void visit(NamedType *type)
        {
128
            FullySpecifiedType ty = rewrite->env->apply(type->name(), rewrite);
129
            if (! ty->isUndefinedType()) {
130
                temps.append(ty);
131
            } else {
132 133 134 135 136 137 138 139 140
                const Name *name = rewrite->rewriteName(type->name());
                temps.append(control()->namedType(name));
            }
        }

        virtual void visit(Function *type)
        {
            Function *funTy = control()->newFunction(0, 0);
            funTy->copy(type);
141 142
            funTy->setConst(type->isConst());
            funTy->setVolatile(type->isVolatile());
143 144 145 146 147

            funTy->setName(rewrite->rewriteName(type->name()));

            funTy->setReturnType(rewrite->rewriteType(type->returnType()));

148
            for (unsigned i = 0, argc = type->argumentCount(); i < argc; ++i) {
149 150 151 152 153 154 155
                Symbol *arg = type->argumentAt(i);

                Argument *newArg = control()->newArgument(0, 0);
                newArg->copy(arg);
                newArg->setName(rewrite->rewriteName(arg->name()));
                newArg->setType(rewrite->rewriteType(arg->type()));

156 157
                // the copy() call above set the scope to 'type'
                // reset it to 0 before adding addMember to avoid assert
158
                newArg->resetEnclosingScope();
159
                funTy->addMember(newArg);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
            }

            temps.append(funTy);
        }

        virtual void visit(Namespace *type)
        {
            qWarning() << Q_FUNC_INFO;
            temps.append(type);
        }

        virtual void visit(Class *type)
        {
            qWarning() << Q_FUNC_INFO;
            temps.append(type);
        }

        virtual void visit(Enum *type)
        {
            qWarning() << Q_FUNC_INFO;
            temps.append(type);
        }

        virtual void visit(ForwardClassDeclaration *type)
        {
            qWarning() << Q_FUNC_INFO;
            temps.append(type);
        }

        virtual void visit(ObjCClass *type)
        {
            qWarning() << Q_FUNC_INFO;
            temps.append(type);
        }

        virtual void visit(ObjCProtocol *type)
        {
            qWarning() << Q_FUNC_INFO;
            temps.append(type);
        }

        virtual void visit(ObjCMethod *type)
        {
            qWarning() << Q_FUNC_INFO;
            temps.append(type);
        }

        virtual void visit(ObjCForwardClassDeclaration *type)
        {
            qWarning() << Q_FUNC_INFO;
            temps.append(type);
        }

        virtual void visit(ObjCForwardProtocolDeclaration *type)
        {
            qWarning() << Q_FUNC_INFO;
            temps.append(type);
        }

    };

    class RewriteName: public NameVisitor
    {
        Rewrite *rewrite;
        QList<const Name *> temps;

        Control *control() const
        { return rewrite->control; }

        const Identifier *identifier(const Identifier *other) const
        {
            if (! other)
                return 0;

234
            return control()->identifier(other->chars(), other->size());
235 236 237 238 239 240 241 242 243 244 245
        }

    public:
        RewriteName(Rewrite *r): rewrite(r) {}

        const Name *operator()(const Name *name)
        {
            if (! name)
                return 0;

            accept(name);
246
            return (!temps.isEmpty()) ? temps.takeLast() : name;
247 248 249 250 251 252 253 254 255
        }

        virtual void visit(const QualifiedNameId *name)
        {
            const Name *base = rewrite->rewriteName(name->base());
            const Name *n = rewrite->rewriteName(name->name());
            temps.append(control()->qualifiedNameId(base, n));
        }

256
        virtual void visit(const Identifier *name)
257
        {
258
            temps.append(control()->identifier(name->chars(), name->size()));
259 260 261 262 263 264 265
        }

        virtual void visit(const TemplateNameId *name)
        {
            QVarLengthArray<FullySpecifiedType, 8> args(name->templateArgumentCount());
            for (unsigned i = 0; i < name->templateArgumentCount(); ++i)
                args[i] = rewrite->rewriteType(name->templateArgumentAt(i));
266 267
            temps.append(control()->templateNameId(identifier(name->identifier()), name->isSpecialization(),
                                                   args.data(), args.size()));
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        }

        virtual void visit(const DestructorNameId *name)
        {
            temps.append(control()->destructorNameId(identifier(name->identifier())));
        }

        virtual void visit(const OperatorNameId *name)
        {
            temps.append(control()->operatorNameId(name->kind()));
        }

        virtual void visit(const ConversionNameId *name)
        {
            FullySpecifiedType ty = rewrite->rewriteType(name->type());
            temps.append(control()->conversionNameId(ty));
        }

        virtual void visit(const SelectorNameId *name)
        {
            QVarLengthArray<const Name *, 8> names(name->nameCount());
            for (unsigned i = 0; i < name->nameCount(); ++i)
                names[i] = rewrite->rewriteName(name->nameAt(i));
            temps.append(control()->selectorNameId(names.constData(), names.size(), name->hasArguments()));
        }
    };

public: // attributes
    Control *control;
297
    SubstitutionEnvironment *env;
298 299 300 301
    RewriteType rewriteType;
    RewriteName rewriteName;
};

302 303
SubstitutionEnvironment::SubstitutionEnvironment()
    : _scope(0)
304 305 306
{
}

307
FullySpecifiedType SubstitutionEnvironment::apply(const Name *name, Rewrite *rewrite) const
308
{
309 310 311 312 313 314 315 316 317 318 319
    if (name) {
        for (int index = _substs.size() - 1; index != -1; --index) {
            const Substitution *subst = _substs.at(index);

            FullySpecifiedType ty = subst->apply(name, rewrite);
            if (! ty->isUndefinedType())
                return ty;
        }
    }

    return FullySpecifiedType();
320 321
}

322
void SubstitutionEnvironment::enter(Substitution *subst)
323
{
324 325
    _substs.append(subst);
}
326

327 328 329 330
void SubstitutionEnvironment::leave()
{
    _substs.removeLast();
}
331

332 333 334 335
Scope *SubstitutionEnvironment::scope() const
{
    return _scope;
}
336

337 338 339 340 341 342
Scope *SubstitutionEnvironment::switchScope(Scope *scope)
{
    Scope *previous = _scope;
    _scope = scope;
    return previous;
}
343

344 345 346
const LookupContext &SubstitutionEnvironment::context() const
{
    return _context;
347 348
}

349 350 351 352
void SubstitutionEnvironment::setContext(const LookupContext &context)
{
    _context = context;
}
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

SubstitutionMap::SubstitutionMap()
{

}

SubstitutionMap::~SubstitutionMap()
{

}

void SubstitutionMap::bind(const Name *name, const FullySpecifiedType &ty)
{
    _map.append(qMakePair(name, ty));
}

FullySpecifiedType SubstitutionMap::apply(const Name *name, Rewrite *) const
{
    for (int n = _map.size() - 1; n != -1; --n) {
        const QPair<const Name *, FullySpecifiedType> &p = _map.at(n);

374
        if (name->match(p.first))
375 376 377 378 379 380
            return p.second;
    }

    return FullySpecifiedType();
}

381

382 383
UseMinimalNames::UseMinimalNames(ClassOrNamespace *target)
    : _target(target)
384 385 386 387
{

}

388
UseMinimalNames::~UseMinimalNames()
389 390 391 392
{

}

393
FullySpecifiedType UseMinimalNames::apply(const Name *name, Rewrite *rewrite) const
394 395 396 397
{
    SubstitutionEnvironment *env = rewrite->env;
    Scope *scope = env->scope();

398 399
    if (name->isTemplateNameId() ||
            (name->isQualifiedNameId() && name->asQualifiedNameId()->name()->isTemplateNameId()))
400 401
        return FullySpecifiedType();

402 403 404 405 406 407 408
    if (! scope)
        return FullySpecifiedType();

    const LookupContext &context = env->context();
    Control *control = rewrite->control;

    const QList<LookupItem> results = context.lookup(name, scope);
Orgad Shaneh's avatar
Orgad Shaneh committed
409 410
    if (!results.isEmpty()) {
        const LookupItem &r = results.first();
411
        if (Symbol *d = r.declaration())
412
            return control->namedType(LookupContext::minimalName(d, _target, control));
413 414 415 416 417 418 419 420

        return r.type();
    }

    return FullySpecifiedType();
}


421 422 423 424 425 426 427 428 429 430 431 432
UseQualifiedNames::UseQualifiedNames()
    : UseMinimalNames(0)
{

}

UseQualifiedNames::~UseQualifiedNames()
{

}


433 434 435
FullySpecifiedType rewriteType(const FullySpecifiedType &type,
                               SubstitutionEnvironment *env,
                               Control *control)
436 437 438 439 440
{
    Rewrite rewrite(control, env);
    return rewrite.rewriteType(type);
}

441 442 443
const Name *rewriteName(const Name *name,
                        SubstitutionEnvironment *env,
                        Control *control)
444 445 446 447
{
    Rewrite rewrite(control, env);
    return rewrite.rewriteName(name);
}
448 449

} // namespace CPlusPlus