From aab9c520309a75c8f7637b130033704651c69701 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Mon, 10 May 2010 09:44:18 +0200
Subject: [PATCH] More work on template instantiation.

---
 src/libs/cplusplus/LookupContext.cpp       | 48 ++++++++++++++++------
 src/libs/cplusplus/ResolveExpression.cpp   | 46 +++++++++++++++++++++
 src/plugins/cpptools/cppcodecompletion.cpp |  2 +
 src/shared/cplusplus/Symbol.cpp            |  6 +++
 4 files changed, 90 insertions(+), 12 deletions(-)

diff --git a/src/libs/cplusplus/LookupContext.cpp b/src/libs/cplusplus/LookupContext.cpp
index 33cab4103b5..c25f42c53a3 100644
--- a/src/libs/cplusplus/LookupContext.cpp
+++ b/src/libs/cplusplus/LookupContext.cpp
@@ -381,6 +381,8 @@ void CreateBindings::lookup_helper(const Name *name, Scope *scope,
         }
 
     } else if (const Identifier *id = name->identifier()) {
+        Control *control = new Control();
+
         for (Symbol *s = scope->lookat(id); s; s = s->next()) {
             if (! s->name())
                 continue;
@@ -396,6 +398,39 @@ void CreateBindings::lookup_helper(const Name *name, Scope *scope,
                 continue;
             }
 
+            if (templateId && (s->isDeclaration() || s->isFunction())) {
+
+                FullySpecifiedType ty = GenTemplateInstance::instantiate(templateId, s, control);
+
+                Overview oo;
+                oo.setShowFunctionSignatures(true);
+                oo.setShowReturnTypes(true);
+
+                qDebug() << "TODO: instantiate:" << oo(s->type(), s->name()) << "using:" << oo(templateId)
+                        << oo(ty);
+
+#if 0
+                if (Declaration *decl = s->asDeclaration()) {
+                    qDebug() << "instantiate declaration";
+                    qDebug() << "is typedef:" << ty.isTypedef() << s->isTypedef() << s->type().isTypedef();
+                    Declaration *d = control->newDeclaration(0, 0);
+                    d->setStorage(decl->storage());
+                    d->setName(decl->name());
+                    d->setType(ty);
+                    d->setScope(decl->scope());
+                    result->append(d);
+                    continue;
+                } else if (Function *fun = s->asFunction()) {
+                    qDebug() << "instantiate function";
+                    Function *d = ty->asFunctionType();
+                    d->setStorage(fun->storage());
+                    d->setScope(fun->scope());
+                    result->append(d);
+                    continue;
+                }
+#endif
+            }
+
             result->append(s);
         }
 
@@ -524,21 +559,15 @@ ClassOrNamespace *ClassOrNamespace::nestedClassOrNamespace(const Name *name) con
     ClassOrNamespace *c = it->second;
 
     if (const TemplateNameId *templId = name->asTemplateNameId()) {
-        Overview oo;
-        qDebug() << "search for:" << oo(templId);
-
         foreach (ClassOrNamespace *i, c->_instantiations) {
-            if (templId->isEqualTo(i->_templateId)) {
-                qDebug() << "*** got a match";
+            if (templId->isEqualTo(i->_templateId))
                 return i;
-            }
         }
 
         ClassOrNamespace *i = _factory->allocClassOrNamespace(c);
         i->_templateId = templId;
         i->_usings.append(c);
         c->_instantiations.append(i);
-        qDebug() << "created a new instantiation" << i;
         return i;
     }
 
@@ -601,11 +630,6 @@ ClassOrNamespace *ClassOrNamespace::findOrCreate(const Name *name)
 
         if (! e) {
             e = _factory->allocClassOrNamespace(this);
-
-            if (const TemplateNameId *templId = name->asTemplateNameId()) {
-                Overview oo;
-                qDebug() << "find or create:" << oo(templId);
-            }
             _classOrNamespaces[name] = e;
         }
 
diff --git a/src/libs/cplusplus/ResolveExpression.cpp b/src/libs/cplusplus/ResolveExpression.cpp
index 89c4b5ae271..3611195776e 100644
--- a/src/libs/cplusplus/ResolveExpression.cpp
+++ b/src/libs/cplusplus/ResolveExpression.cpp
@@ -588,8 +588,54 @@ ResolveExpression::resolveBaseExpression(const QList<LookupItem> &baseResults, i
 
     if (accessOp == T_ARROW)  {
         if (NamedType *namedTy = ty->asNamedType()) {
+
+            Overview oo;
+            qDebug() << "got here:" << oo(namedTy->name());
+
             const Name *arrowAccessOp = control()->operatorNameId(OperatorNameId::ArrowOp);
 
+#if 0
+            qDebug() << "last visible symbol is:" << result.lastVisibleSymbol()->fileName()
+                    << result.lastVisibleSymbol()->line()
+                    << result.lastVisibleSymbol()->column();
+#endif
+
+#if 0
+            if (const QualifiedNameId *q = namedTy->name()->asQualifiedNameId()) {
+                QList<const Name *> names;
+                for (unsigned i = 0; i < q->nameCount() - 1; ++i)
+                    names.append(q->nameAt(i));
+                if (ClassOrNamespace *b = _context.globalNamespace()->findClassOrNamespace(names)) {
+                    qDebug() << "yuppi du" << b << oo(b->_templateId);
+                    foreach (Symbol *s, b->lookup(q->unqualifiedNameId())) {
+                        qDebug() << "r:" << oo(s->type(), s->name());
+                    }
+                }
+            }
+#endif
+
+            foreach (Symbol *s, _context.lookup(namedTy->name(), result.lastVisibleSymbol())) {
+                qDebug() << "r:" << oo(s->type(), s->name());
+
+                if (PointerType *ptrTy = s->type()->asPointerType()) {
+                    FullySpecifiedType elementTy = ptrTy->elementType().simplified();
+
+                    if (elementTy->isNamedType() || elementTy->isClassType())
+                        results.append(LookupItem(elementTy, lastVisibleSymbol));
+                } else if (const NamedType *nt = s->type()->asNamedType()) {
+                    qDebug() << "f***";
+                    Symbol *l = _context.lookup(nt->name(), result.lastVisibleSymbol()).first();
+                    qDebug() << "l" << oo(l->type(), l->name());
+
+                    if (PointerType *ptrTy = l->type()->asPointerType()) {
+                        FullySpecifiedType elementTy = ptrTy->elementType().simplified();
+
+                        if (elementTy->isNamedType() || elementTy->isClassType())
+                            results.append(LookupItem(elementTy, lastVisibleSymbol));
+                    }
+                }
+            }
+
             if (ClassOrNamespace *b = _context.classOrNamespace(namedTy->name(), result.lastVisibleSymbol())) {
                 foreach (Symbol *overload, b->lookup(arrowAccessOp)) {
                     if (Function *funTy = overload->type()->asFunctionType()) {
diff --git a/src/plugins/cpptools/cppcodecompletion.cpp b/src/plugins/cpptools/cppcodecompletion.cpp
index 198ff12ccad..e38b12c192a 100644
--- a/src/plugins/cpptools/cppcodecompletion.cpp
+++ b/src/plugins/cpptools/cppcodecompletion.cpp
@@ -1105,6 +1105,8 @@ bool CppCodeCompletion::completeMember(const QList<LookupItem> &baseResults,
                                                     m_completionOperator,
                                                     &replacedDotOperator);
 
+    qDebug() << "results:" << classObjectResults.size() << "repl:" << replacedDotOperator;
+
     ClassOrNamespace *classOrNamespace = 0;
 
     QList<Symbol *> classObjectCandidates;
diff --git a/src/shared/cplusplus/Symbol.cpp b/src/shared/cplusplus/Symbol.cpp
index 29865c9922e..6c506c1700c 100644
--- a/src/shared/cplusplus/Symbol.cpp
+++ b/src/shared/cplusplus/Symbol.cpp
@@ -242,6 +242,8 @@ void Symbol::setSourceLocation(unsigned sourceLocation)
 
 unsigned Symbol::line() const
 {
+    assert(_sourceOffset != 0);
+
     unsigned line = 0, column = 0;
     const StringLiteral *fileId = 0;
     translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId);
@@ -250,6 +252,8 @@ unsigned Symbol::line() const
 
 unsigned Symbol::column() const
 {
+    assert(_sourceOffset != 0);
+
     unsigned line = 0, column = 0;
     const StringLiteral *fileId = 0;
     translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId);
@@ -258,6 +262,8 @@ unsigned Symbol::column() const
 
 const StringLiteral *Symbol::fileId() const
 {
+    assert(_sourceOffset != 0);
+
     unsigned line = 0, column = 0;
     const StringLiteral *fileId = 0;
     translationUnit()->getPosition(_sourceOffset, &line, &column, &fileId);
-- 
GitLab