1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
32 #include "CppRewriter.h"
33 #include <TypeVisitor.h>
34 #include <NameVisitor.h>
35 #include <CoreTypes.h>
42 #include <QtCore/QVarLengthArray>
43 #include <QtCore/QRegExp>
44 #include <QtCore/QDebug>
51 Rewrite(Control *control, SubstitutionEnvironment *env)
52 : control(control), env(env), rewriteType(this), rewriteName(this) {}
54 class RewriteType: public TypeVisitor
57 QList<FullySpecifiedType> temps;
59 Control *control() const
60 { return rewrite->control; }
62 void accept(const FullySpecifiedType &ty)
64 TypeVisitor::accept(ty.type());
65 unsigned flags = ty.flags();
66 flags |= temps.back().flags();
67 temps.back().setFlags(flags);
71 RewriteType(Rewrite *r): rewrite(r) {}
73 FullySpecifiedType operator()(const FullySpecifiedType &ty)
76 return temps.takeLast();
79 virtual void visit(UndefinedType *)
81 temps.append(FullySpecifiedType());
84 virtual void visit(VoidType *)
86 temps.append(control()->voidType());
89 virtual void visit(IntegerType *type)
91 temps.append(control()->integerType(type->kind()));
94 virtual void visit(FloatType *type)
96 temps.append(control()->floatType(type->kind()));
99 virtual void visit(PointerToMemberType *type)
101 const Name *memberName = rewrite->rewriteName(type->memberName());
102 const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
103 temps.append(control()->pointerToMemberType(memberName, elementType));
106 virtual void visit(PointerType *type)
108 const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
109 temps.append(control()->pointerType(elementType));
112 virtual void visit(ReferenceType *type)
114 const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
115 temps.append(control()->referenceType(elementType));
118 virtual void visit(ArrayType *type)
120 const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
121 temps.append(control()->arrayType(elementType, type->size()));
124 virtual void visit(NamedType *type)
126 FullySpecifiedType ty = rewrite->env->apply(type->name(), rewrite);
127 if (! ty->isUndefinedType())
130 const Name *name = rewrite->rewriteName(type->name());
131 temps.append(control()->namedType(name));
135 virtual void visit(Function *type)
137 Function *funTy = control()->newFunction(0, 0);
139 funTy->setConst(type->isConst());
140 funTy->setVolatile(type->isVolatile());
142 funTy->setName(rewrite->rewriteName(type->name()));
144 funTy->setReturnType(rewrite->rewriteType(type->returnType()));
146 for (unsigned i = 0; i < type->argumentCount(); ++i) {
147 Symbol *arg = type->argumentAt(i);
149 Argument *newArg = control()->newArgument(0, 0);
151 newArg->setName(rewrite->rewriteName(arg->name()));
152 newArg->setType(rewrite->rewriteType(arg->type()));
154 funTy->addMember(newArg);
160 virtual void visit(Namespace *type)
162 qWarning() << Q_FUNC_INFO;
166 virtual void visit(Class *type)
168 qWarning() << Q_FUNC_INFO;
172 virtual void visit(Enum *type)
174 qWarning() << Q_FUNC_INFO;
178 virtual void visit(ForwardClassDeclaration *type)
180 qWarning() << Q_FUNC_INFO;
184 virtual void visit(ObjCClass *type)
186 qWarning() << Q_FUNC_INFO;
190 virtual void visit(ObjCProtocol *type)
192 qWarning() << Q_FUNC_INFO;
196 virtual void visit(ObjCMethod *type)
198 qWarning() << Q_FUNC_INFO;
202 virtual void visit(ObjCForwardClassDeclaration *type)
204 qWarning() << Q_FUNC_INFO;
208 virtual void visit(ObjCForwardProtocolDeclaration *type)
210 qWarning() << Q_FUNC_INFO;
216 class RewriteName: public NameVisitor
219 QList<const Name *> temps;
221 Control *control() const
222 { return rewrite->control; }
224 const Identifier *identifier(const Identifier *other) const
229 return control()->identifier(other->chars(), other->size());
233 RewriteName(Rewrite *r): rewrite(r) {}
235 const Name *operator()(const Name *name)
241 return temps.takeLast();
244 virtual void visit(const QualifiedNameId *name)
246 const Name *base = rewrite->rewriteName(name->base());
247 const Name *n = rewrite->rewriteName(name->name());
248 temps.append(control()->qualifiedNameId(base, n));
251 virtual void visit(const Identifier *name)
253 temps.append(control()->identifier(name->chars(), name->size()));
256 virtual void visit(const TemplateNameId *name)
258 QVarLengthArray<FullySpecifiedType, 8> args(name->templateArgumentCount());
259 for (unsigned i = 0; i < name->templateArgumentCount(); ++i)
260 args[i] = rewrite->rewriteType(name->templateArgumentAt(i));
261 temps.append(control()->templateNameId(identifier(name->identifier()), args.data(), args.size()));
264 virtual void visit(const DestructorNameId *name)
266 temps.append(control()->destructorNameId(identifier(name->identifier())));
269 virtual void visit(const OperatorNameId *name)
271 temps.append(control()->operatorNameId(name->kind()));
274 virtual void visit(const ConversionNameId *name)
276 FullySpecifiedType ty = rewrite->rewriteType(name->type());
277 temps.append(control()->conversionNameId(ty));
280 virtual void visit(const SelectorNameId *name)
282 QVarLengthArray<const Name *, 8> names(name->nameCount());
283 for (unsigned i = 0; i < name->nameCount(); ++i)
284 names[i] = rewrite->rewriteName(name->nameAt(i));
285 temps.append(control()->selectorNameId(names.constData(), names.size(), name->hasArguments()));
289 public: // attributes
291 SubstitutionEnvironment *env;
292 RewriteType rewriteType;
293 RewriteName rewriteName;
296 SubstitutionEnvironment::SubstitutionEnvironment()
301 FullySpecifiedType SubstitutionEnvironment::apply(const Name *name, Rewrite *rewrite) const
304 for (int index = _substs.size() - 1; index != -1; --index) {
305 const Substitution *subst = _substs.at(index);
307 FullySpecifiedType ty = subst->apply(name, rewrite);
308 if (! ty->isUndefinedType())
313 return FullySpecifiedType();
316 void SubstitutionEnvironment::enter(Substitution *subst)
318 _substs.append(subst);
321 void SubstitutionEnvironment::leave()
323 _substs.removeLast();
326 Scope *SubstitutionEnvironment::scope() const
331 Scope *SubstitutionEnvironment::switchScope(Scope *scope)
333 Scope *previous = _scope;
338 const LookupContext &SubstitutionEnvironment::context() const
343 void SubstitutionEnvironment::setContext(const LookupContext &context)
348 SubstitutionMap::SubstitutionMap()
353 SubstitutionMap::~SubstitutionMap()
358 void SubstitutionMap::bind(const Name *name, const FullySpecifiedType &ty)
360 _map.append(qMakePair(name, ty));
363 FullySpecifiedType SubstitutionMap::apply(const Name *name, Rewrite *) const
365 for (int n = _map.size() - 1; n != -1; --n) {
366 const QPair<const Name *, FullySpecifiedType> &p = _map.at(n);
368 if (name->isEqualTo(p.first))
372 return FullySpecifiedType();
376 UseQualifiedNames::UseQualifiedNames()
381 UseQualifiedNames::~UseQualifiedNames()
386 FullySpecifiedType UseQualifiedNames::apply(const Name *name, Rewrite *rewrite) const
388 SubstitutionEnvironment *env = rewrite->env;
389 Scope *scope = env->scope();
391 if (name->isQualifiedNameId() || name->isTemplateNameId())
392 return FullySpecifiedType();
395 return FullySpecifiedType();
397 const LookupContext &context = env->context();
398 Control *control = rewrite->control;
400 const QList<LookupItem> results = context.lookup(name, scope);
401 foreach (const LookupItem &r, results) {
402 if (Symbol *d = r.declaration()) {
404 foreach (const Name *c, LookupContext::fullyQualifiedName(d)) {
408 n = control->qualifiedNameId(n, c);
411 return control->namedType(n);
417 return FullySpecifiedType();
421 FullySpecifiedType rewriteType(const FullySpecifiedType &type,
422 SubstitutionEnvironment *env,
425 Rewrite rewrite(control, env);
426 return rewrite.rewriteType(type);
429 const Name *rewriteName(const Name *name,
430 SubstitutionEnvironment *env,
433 Rewrite rewrite(control, env);
434 return rewrite.rewriteName(name);
437 // Simplify complicated STL template types,
438 // such as 'std::basic_string<char,std::char_traits<char>,std::allocator<char> >'
439 // -> 'std::string' and helpers.
441 static QString chopConst(QString type)
444 if (type.startsWith(QLatin1String("const")))
446 else if (type.startsWith(QLatin1Char(' ')))
448 else if (type.endsWith(QLatin1String("const")))
450 else if (type.endsWith(QLatin1Char(' ')))
458 static inline QRegExp stdStringRegExp(const QString &charType)
460 QString rc = QLatin1String("basic_string<");
462 rc += QLatin1String(",[ ]?std::char_traits<");
464 rc += QLatin1String(">,[ ]?std::allocator<");
466 rc += QLatin1String("> >");
467 const QRegExp re(rc);
468 Q_ASSERT(re.isValid());
472 // Simplify string types in a type
473 // 'std::set<std::basic_string<char... > >' -> std::set<std::string>'
474 static inline void simplifyStdString(const QString &charType, const QString &replacement,
477 QRegExp stringRegexp = stdStringRegExp(charType);
478 const int replacementSize = replacement.size();
479 for (int pos = 0; pos < type->size(); ) {
481 const int matchPos = stringRegexp.indexIn(*type, pos);
484 const int matchedLength = stringRegexp.matchedLength();
485 type->replace(matchPos, matchedLength, replacement);
486 pos = matchPos + replacementSize;
487 // If we were inside an 'allocator<std::basic_string..char > >'
488 // kill the following blank -> 'allocator<std::string>'
489 if (pos + 1 < type->size() && type->at(pos) == QLatin1Char(' ')
490 && type->at(pos + 1) == QLatin1Char('>'))
491 type->remove(pos, 1);
495 // Fix 'std::allocator<std::string >' -> 'std::allocator<std::string>',
496 // which can happen when replacing/simplifying
497 static inline QString fixNestedTemplates(QString s)
499 const int size = s.size();
501 && s.at(size - 1) == QLatin1Char('>')
502 && s.at(size - 2) == QLatin1Char(' ')
503 && s.at(size - 3) != QLatin1Char('>'))
504 s.remove(size - 2, 1);
508 CPLUSPLUS_EXPORT QString simplifySTLType(const QString &typeIn)
510 QString type = typeIn;
511 if (type.startsWith("class ")) // MSVC prepends class,struct
513 if (type.startsWith("struct "))
516 type.replace(QLatin1Char('*'), QLatin1Char('@'));
518 for (int i = 0; i < 10; ++i) {
519 int start = type.indexOf("std::allocator<");
522 // search for matching '>'
525 for (pos = start + 12; pos < type.size(); ++pos) {
526 int c = type.at(pos).unicode();
529 } else if (c == '>') {
535 const QString alloc = fixNestedTemplates(type.mid(start, pos + 1 - start).trimmed());
536 const QString inner = fixNestedTemplates(alloc.mid(15, alloc.size() - 16).trimmed());
537 if (inner == QLatin1String("char")) { // std::string
538 simplifyStdString(QLatin1String("char"), QLatin1String("string"), &type);
539 } else if (inner == QLatin1String("wchar_t")) { // std::wstring
540 simplifyStdString(QLatin1String("wchar_t"), QLatin1String("wstring"), &type);
541 } else if (inner == QLatin1String("unsigned short")) { // std::wstring/MSVC
542 simplifyStdString(QLatin1String("unsigned short"), QLatin1String("wstring"), &type);
544 // std::vector, std::deque, std::list
545 const QRegExp re1(QString::fromLatin1("(vector|list|deque)<%1, ?%2\\s*>").arg(inner, alloc));
546 Q_ASSERT(re1.isValid());
547 if (re1.indexIn(type) != -1)
548 type.replace(re1.cap(0), QString::fromLatin1("%1<%2>").arg(re1.cap(1), inner));
551 QRegExp stackRE(QString::fromLatin1("stack<%1, ?std::deque<%2> >").arg(inner, inner));
552 stackRE.setMinimal(true);
553 Q_ASSERT(stackRE.isValid());
554 if (stackRE.indexIn(type) != -1)
555 type.replace(stackRE.cap(0), QString::fromLatin1("stack<%1>").arg(inner));
558 QRegExp setRE(QString::fromLatin1("set<%1, ?std::less<%2>, ?%3\\s*>").arg(inner, inner, alloc));
559 setRE.setMinimal(true);
560 Q_ASSERT(setRE.isValid());
561 if (setRE.indexIn(type) != -1)
562 type.replace(setRE.cap(0), QString::fromLatin1("set<%1>").arg(inner));
565 if (inner.startsWith("std::pair<")) {
566 // search for outermost ',', split key and value
569 for (pos = 10; pos < inner.size(); ++pos) {
570 int c = inner.at(pos).unicode();
575 else if (c == ',' && level == 0)
578 const QString key = chopConst(inner.mid(10, pos - 10));
579 // Get value: MSVC: 'pair<a const ,b>', gcc: 'pair<const a, b>'
580 if (inner.at(++pos) == QLatin1Char(' '))
582 QString value = inner.mid(pos, inner.size() - pos - 1).trimmed();
583 QRegExp mapRE1(QString("map<%1, ?%2, ?std::less<%3 ?>, ?%4\\s*>")
584 .arg(key, value, key, alloc));
585 mapRE1.setMinimal(true);
586 Q_ASSERT(mapRE1.isValid());
587 if (mapRE1.indexIn(type) != -1) {
588 type.replace(mapRE1.cap(0), QString("map<%1, %2>").arg(key, value));
590 QRegExp mapRE2(QString("map<const %1, ?%2, ?std::less<const %3>, ?%4\\s*>")
591 .arg(key, value, key, alloc));
592 mapRE2.setMinimal(true);
593 if (mapRE2.indexIn(type) != -1) {
594 type.replace(mapRE2.cap(0), QString("map<const %1, %2>").arg(key, value));
599 type.replace(QLatin1Char('@'), QLatin1Char('*'));
600 type.replace(QLatin1String(" >"), QLatin1String(">"));
604 } // namespace CPlusPlus