1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
33 #include "CppRewriter.h"
34 #include <TypeVisitor.h>
35 #include <NameVisitor.h>
36 #include <CoreTypes.h>
43 #include <QtCore/QVarLengthArray>
44 #include <QtCore/QRegExp>
45 #include <QtCore/QDebug>
52 Rewrite(Control *control, SubstitutionEnvironment *env)
53 : control(control), env(env), rewriteType(this), rewriteName(this) {}
55 class RewriteType: public TypeVisitor
58 QList<FullySpecifiedType> temps;
60 Control *control() const
61 { return rewrite->control; }
63 void accept(const FullySpecifiedType &ty)
65 TypeVisitor::accept(ty.type());
66 unsigned flags = ty.flags();
67 flags |= temps.back().flags();
68 temps.back().setFlags(flags);
72 RewriteType(Rewrite *r): rewrite(r) {}
74 FullySpecifiedType operator()(const FullySpecifiedType &ty)
77 return temps.takeLast();
80 virtual void visit(UndefinedType *)
82 temps.append(FullySpecifiedType());
85 virtual void visit(VoidType *)
87 temps.append(control()->voidType());
90 virtual void visit(IntegerType *type)
92 temps.append(control()->integerType(type->kind()));
95 virtual void visit(FloatType *type)
97 temps.append(control()->floatType(type->kind()));
100 virtual void visit(PointerToMemberType *type)
102 const Name *memberName = rewrite->rewriteName(type->memberName());
103 const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
104 temps.append(control()->pointerToMemberType(memberName, elementType));
107 virtual void visit(PointerType *type)
109 const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
110 temps.append(control()->pointerType(elementType));
113 virtual void visit(ReferenceType *type)
115 const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
116 temps.append(control()->referenceType(elementType));
119 virtual void visit(ArrayType *type)
121 const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
122 temps.append(control()->arrayType(elementType, type->size()));
125 virtual void visit(NamedType *type)
127 FullySpecifiedType ty = rewrite->env->apply(type->name(), rewrite);
128 if (! ty->isUndefinedType())
131 const Name *name = rewrite->rewriteName(type->name());
132 temps.append(control()->namedType(name));
136 virtual void visit(Function *type)
138 Function *funTy = control()->newFunction(0, 0);
140 funTy->setConst(type->isConst());
141 funTy->setVolatile(type->isVolatile());
143 funTy->setName(rewrite->rewriteName(type->name()));
145 funTy->setReturnType(rewrite->rewriteType(type->returnType()));
147 for (unsigned i = 0; i < type->argumentCount(); ++i) {
148 Symbol *arg = type->argumentAt(i);
150 Argument *newArg = control()->newArgument(0, 0);
152 newArg->setName(rewrite->rewriteName(arg->name()));
153 newArg->setType(rewrite->rewriteType(arg->type()));
155 funTy->addMember(newArg);
161 virtual void visit(Namespace *type)
163 qWarning() << Q_FUNC_INFO;
167 virtual void visit(Class *type)
169 qWarning() << Q_FUNC_INFO;
173 virtual void visit(Enum *type)
175 qWarning() << Q_FUNC_INFO;
179 virtual void visit(ForwardClassDeclaration *type)
181 qWarning() << Q_FUNC_INFO;
185 virtual void visit(ObjCClass *type)
187 qWarning() << Q_FUNC_INFO;
191 virtual void visit(ObjCProtocol *type)
193 qWarning() << Q_FUNC_INFO;
197 virtual void visit(ObjCMethod *type)
199 qWarning() << Q_FUNC_INFO;
203 virtual void visit(ObjCForwardClassDeclaration *type)
205 qWarning() << Q_FUNC_INFO;
209 virtual void visit(ObjCForwardProtocolDeclaration *type)
211 qWarning() << Q_FUNC_INFO;
217 class RewriteName: public NameVisitor
220 QList<const Name *> temps;
222 Control *control() const
223 { return rewrite->control; }
225 const Identifier *identifier(const Identifier *other) const
230 return control()->identifier(other->chars(), other->size());
234 RewriteName(Rewrite *r): rewrite(r) {}
236 const Name *operator()(const Name *name)
242 return temps.takeLast();
245 virtual void visit(const QualifiedNameId *name)
247 const Name *base = rewrite->rewriteName(name->base());
248 const Name *n = rewrite->rewriteName(name->name());
249 temps.append(control()->qualifiedNameId(base, n));
252 virtual void visit(const Identifier *name)
254 temps.append(control()->identifier(name->chars(), name->size()));
257 virtual void visit(const TemplateNameId *name)
259 QVarLengthArray<FullySpecifiedType, 8> args(name->templateArgumentCount());
260 for (unsigned i = 0; i < name->templateArgumentCount(); ++i)
261 args[i] = rewrite->rewriteType(name->templateArgumentAt(i));
262 temps.append(control()->templateNameId(identifier(name->identifier()), args.data(), args.size()));
265 virtual void visit(const DestructorNameId *name)
267 temps.append(control()->destructorNameId(identifier(name->identifier())));
270 virtual void visit(const OperatorNameId *name)
272 temps.append(control()->operatorNameId(name->kind()));
275 virtual void visit(const ConversionNameId *name)
277 FullySpecifiedType ty = rewrite->rewriteType(name->type());
278 temps.append(control()->conversionNameId(ty));
281 virtual void visit(const SelectorNameId *name)
283 QVarLengthArray<const Name *, 8> names(name->nameCount());
284 for (unsigned i = 0; i < name->nameCount(); ++i)
285 names[i] = rewrite->rewriteName(name->nameAt(i));
286 temps.append(control()->selectorNameId(names.constData(), names.size(), name->hasArguments()));
290 public: // attributes
292 SubstitutionEnvironment *env;
293 RewriteType rewriteType;
294 RewriteName rewriteName;
297 SubstitutionEnvironment::SubstitutionEnvironment()
302 FullySpecifiedType SubstitutionEnvironment::apply(const Name *name, Rewrite *rewrite) const
305 for (int index = _substs.size() - 1; index != -1; --index) {
306 const Substitution *subst = _substs.at(index);
308 FullySpecifiedType ty = subst->apply(name, rewrite);
309 if (! ty->isUndefinedType())
314 return FullySpecifiedType();
317 void SubstitutionEnvironment::enter(Substitution *subst)
319 _substs.append(subst);
322 void SubstitutionEnvironment::leave()
324 _substs.removeLast();
327 Scope *SubstitutionEnvironment::scope() const
332 Scope *SubstitutionEnvironment::switchScope(Scope *scope)
334 Scope *previous = _scope;
339 const LookupContext &SubstitutionEnvironment::context() const
344 void SubstitutionEnvironment::setContext(const LookupContext &context)
349 SubstitutionMap::SubstitutionMap()
354 SubstitutionMap::~SubstitutionMap()
359 void SubstitutionMap::bind(const Name *name, const FullySpecifiedType &ty)
361 _map.append(qMakePair(name, ty));
364 FullySpecifiedType SubstitutionMap::apply(const Name *name, Rewrite *) const
366 for (int n = _map.size() - 1; n != -1; --n) {
367 const QPair<const Name *, FullySpecifiedType> &p = _map.at(n);
369 if (name->isEqualTo(p.first))
373 return FullySpecifiedType();
377 UseQualifiedNames::UseQualifiedNames()
382 UseQualifiedNames::~UseQualifiedNames()
387 FullySpecifiedType UseQualifiedNames::apply(const Name *name, Rewrite *rewrite) const
389 SubstitutionEnvironment *env = rewrite->env;
390 Scope *scope = env->scope();
392 if (name->isQualifiedNameId() || name->isTemplateNameId())
393 return FullySpecifiedType();
396 return FullySpecifiedType();
398 const LookupContext &context = env->context();
399 Control *control = rewrite->control;
401 const QList<LookupItem> results = context.lookup(name, scope);
402 foreach (const LookupItem &r, results) {
403 if (Symbol *d = r.declaration()) {
405 foreach (const Name *c, LookupContext::fullyQualifiedName(d)) {
409 n = control->qualifiedNameId(n, c);
412 return control->namedType(n);
418 return FullySpecifiedType();
422 FullySpecifiedType rewriteType(const FullySpecifiedType &type,
423 SubstitutionEnvironment *env,
426 Rewrite rewrite(control, env);
427 return rewrite.rewriteType(type);
430 const Name *rewriteName(const Name *name,
431 SubstitutionEnvironment *env,
434 Rewrite rewrite(control, env);
435 return rewrite.rewriteName(name);
438 // Simplify complicated STL template types,
439 // such as 'std::basic_string<char,std::char_traits<char>,std::allocator<char> >'
440 // -> 'std::string' and helpers.
442 static QString chopConst(QString type)
445 if (type.startsWith(QLatin1String("const")))
447 else if (type.startsWith(QLatin1Char(' ')))
449 else if (type.endsWith(QLatin1String("const")))
451 else if (type.endsWith(QLatin1Char(' ')))
459 static inline QRegExp stdStringRegExp(const QString &charType)
461 QString rc = QLatin1String("basic_string<");
463 rc += QLatin1String(",[ ]?std::char_traits<");
465 rc += QLatin1String(">,[ ]?std::allocator<");
467 rc += QLatin1String("> >");
468 const QRegExp re(rc);
469 Q_ASSERT(re.isValid());
473 // Simplify string types in a type
474 // 'std::set<std::basic_string<char... > >' -> std::set<std::string>'
475 static inline void simplifyStdString(const QString &charType, const QString &replacement,
478 QRegExp stringRegexp = stdStringRegExp(charType);
479 const int replacementSize = replacement.size();
480 for (int pos = 0; pos < type->size(); ) {
482 const int matchPos = stringRegexp.indexIn(*type, pos);
485 const int matchedLength = stringRegexp.matchedLength();
486 type->replace(matchPos, matchedLength, replacement);
487 pos = matchPos + replacementSize;
488 // If we were inside an 'allocator<std::basic_string..char > >'
489 // kill the following blank -> 'allocator<std::string>'
490 if (pos + 1 < type->size() && type->at(pos) == QLatin1Char(' ')
491 && type->at(pos + 1) == QLatin1Char('>'))
492 type->remove(pos, 1);
496 // Fix 'std::allocator<std::string >' -> 'std::allocator<std::string>',
497 // which can happen when replacing/simplifying
498 static inline QString fixNestedTemplates(QString s)
500 const int size = s.size();
502 && s.at(size - 1) == QLatin1Char('>')
503 && s.at(size - 2) == QLatin1Char(' ')
504 && s.at(size - 3) != QLatin1Char('>'))
505 s.remove(size - 2, 1);
509 CPLUSPLUS_EXPORT QString simplifySTLType(const QString &typeIn)
511 QString type = typeIn;
512 if (type.startsWith("class ")) // MSVC prepends class,struct
514 if (type.startsWith("struct "))
517 type.replace(QLatin1Char('*'), QLatin1Char('@'));
519 for (int i = 0; i < 10; ++i) {
520 int start = type.indexOf("std::allocator<");
523 // search for matching '>'
526 for (pos = start + 12; pos < type.size(); ++pos) {
527 int c = type.at(pos).unicode();
530 } else if (c == '>') {
536 const QString alloc = fixNestedTemplates(type.mid(start, pos + 1 - start).trimmed());
537 const QString inner = fixNestedTemplates(alloc.mid(15, alloc.size() - 16).trimmed());
538 if (inner == QLatin1String("char")) { // std::string
539 simplifyStdString(QLatin1String("char"), QLatin1String("string"), &type);
540 } else if (inner == QLatin1String("wchar_t")) { // std::wstring
541 simplifyStdString(QLatin1String("wchar_t"), QLatin1String("wstring"), &type);
542 } else if (inner == QLatin1String("unsigned short")) { // std::wstring/MSVC
543 simplifyStdString(QLatin1String("unsigned short"), QLatin1String("wstring"), &type);
545 // std::vector, std::deque, std::list
546 const QRegExp re1(QString::fromLatin1("(vector|list|deque)<%1, ?%2\\s*>").arg(inner, alloc));
547 Q_ASSERT(re1.isValid());
548 if (re1.indexIn(type) != -1)
549 type.replace(re1.cap(0), QString::fromLatin1("%1<%2>").arg(re1.cap(1), inner));
552 QRegExp stackRE(QString::fromLatin1("stack<%1, ?std::deque<%2> >").arg(inner, inner));
553 stackRE.setMinimal(true);
554 Q_ASSERT(stackRE.isValid());
555 if (stackRE.indexIn(type) != -1)
556 type.replace(stackRE.cap(0), QString::fromLatin1("stack<%1>").arg(inner));
559 QRegExp setRE(QString::fromLatin1("set<%1, ?std::less<%2>, ?%3\\s*>").arg(inner, inner, alloc));
560 setRE.setMinimal(true);
561 Q_ASSERT(setRE.isValid());
562 if (setRE.indexIn(type) != -1)
563 type.replace(setRE.cap(0), QString::fromLatin1("set<%1>").arg(inner));
566 if (inner.startsWith("std::pair<")) {
567 // search for outermost ',', split key and value
570 for (pos = 10; pos < inner.size(); ++pos) {
571 int c = inner.at(pos).unicode();
576 else if (c == ',' && level == 0)
579 const QString key = chopConst(inner.mid(10, pos - 10));
580 // Get value: MSVC: 'pair<a const ,b>', gcc: 'pair<const a, b>'
581 if (inner.at(++pos) == QLatin1Char(' '))
583 QString value = inner.mid(pos, inner.size() - pos - 1).trimmed();
584 QRegExp mapRE1(QString("map<%1, ?%2, ?std::less<%3 ?>, ?%4\\s*>")
585 .arg(key, value, key, alloc));
586 mapRE1.setMinimal(true);
587 Q_ASSERT(mapRE1.isValid());
588 if (mapRE1.indexIn(type) != -1) {
589 type.replace(mapRE1.cap(0), QString("map<%1, %2>").arg(key, value));
591 QRegExp mapRE2(QString("map<const %1, ?%2, ?std::less<const %3>, ?%4\\s*>")
592 .arg(key, value, key, alloc));
593 mapRE2.setMinimal(true);
594 if (mapRE2.indexIn(type) != -1) {
595 type.replace(mapRE2.cap(0), QString("map<const %1, %2>").arg(key, value));
600 type.replace(QLatin1Char('@'), QLatin1Char('*'));
601 type.replace(QLatin1String(" >"), QLatin1String(">"));
605 } // namespace CPlusPlus