From cceaffca5bfeb0cb54ccad64602391234595abd9 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Fri, 4 Jun 2010 17:58:29 +0200
Subject: [PATCH] Keep the Control around for as long needed.

---
 .../DeprecatedGenTemplateInstance.cpp         |  7 ++---
 .../cplusplus/DeprecatedGenTemplateInstance.h |  7 ++---
 src/libs/cplusplus/LookupContext.cpp          | 27 +++++++++++--------
 src/libs/cplusplus/LookupContext.h            | 11 +++++---
 src/libs/cplusplus/ResolveExpression.cpp      |  2 +-
 5 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/src/libs/cplusplus/DeprecatedGenTemplateInstance.cpp b/src/libs/cplusplus/DeprecatedGenTemplateInstance.cpp
index 760d8bce4ec..9c8ebfe3b58 100644
--- a/src/libs/cplusplus/DeprecatedGenTemplateInstance.cpp
+++ b/src/libs/cplusplus/DeprecatedGenTemplateInstance.cpp
@@ -363,7 +363,7 @@ FullySpecifiedType ApplySubstitution::applySubstitution(int index) const
 
 } // end of anonymous namespace
 
-DeprecatedGenTemplateInstance::DeprecatedGenTemplateInstance(Control *control, const Substitution &substitution)
+DeprecatedGenTemplateInstance::DeprecatedGenTemplateInstance(QSharedPointer<Control> control, const Substitution &substitution)
     : _symbol(0),
       _control(control),
       _substitution(substitution)
@@ -371,11 +371,12 @@ DeprecatedGenTemplateInstance::DeprecatedGenTemplateInstance(Control *control, c
 
 FullySpecifiedType DeprecatedGenTemplateInstance::gen(Symbol *symbol)
 {
-    ApplySubstitution o(_control, symbol, _substitution);
+    ApplySubstitution o(_control.data(), symbol, _substitution);
     return o.apply(symbol->type());
 }
 
-FullySpecifiedType DeprecatedGenTemplateInstance::instantiate(const Name *className, Symbol *candidate, Control *control)
+FullySpecifiedType DeprecatedGenTemplateInstance::instantiate(const Name *className, Symbol *candidate,
+                                                              QSharedPointer<Control> control)
 {
     if (className) {
         if (const TemplateNameId *templId = className->asTemplateNameId()) {
diff --git a/src/libs/cplusplus/DeprecatedGenTemplateInstance.h b/src/libs/cplusplus/DeprecatedGenTemplateInstance.h
index d90990eb92d..f40c951ba50 100644
--- a/src/libs/cplusplus/DeprecatedGenTemplateInstance.h
+++ b/src/libs/cplusplus/DeprecatedGenTemplateInstance.h
@@ -36,6 +36,7 @@
 
 #include <QtCore/QList>
 #include <QtCore/QPair>
+#include <QtCore/QSharedPointer>
 
 namespace CPlusPlus {
 
@@ -45,15 +46,15 @@ public:
     typedef QList< QPair<const Identifier *, FullySpecifiedType> > Substitution;
 
 public:
-    static FullySpecifiedType instantiate(const Name *className, Symbol *candidate, Control *control);
+    static FullySpecifiedType instantiate(const Name *className, Symbol *candidate, QSharedPointer<Control> control);
 
 private:
-    DeprecatedGenTemplateInstance(Control *control, const Substitution &substitution);
+    DeprecatedGenTemplateInstance(QSharedPointer<Control> control, const Substitution &substitution);
     FullySpecifiedType gen(Symbol *symbol);
 
 private:
     Symbol *_symbol;
-    Control *_control;
+    QSharedPointer<Control> _control;
     const Substitution _substitution;
 };
 
diff --git a/src/libs/cplusplus/LookupContext.cpp b/src/libs/cplusplus/LookupContext.cpp
index 21cecac3098..4e035dbca3a 100644
--- a/src/libs/cplusplus/LookupContext.cpp
+++ b/src/libs/cplusplus/LookupContext.cpp
@@ -90,13 +90,15 @@ bool ClassOrNamespace::CompareName::operator()(const Name *name, const Name *oth
 // LookupContext
 /////////////////////////////////////////////////////////////////////
 LookupContext::LookupContext()
+    : _control(new Control())
 { }
 
 LookupContext::LookupContext(Document::Ptr thisDocument,
                              const Snapshot &snapshot)
     : _expressionDocument(Document::create("<LookupContext>")),
       _thisDocument(thisDocument),
-      _snapshot(snapshot)
+      _snapshot(snapshot),
+      _control(new Control())
 {
 }
 
@@ -105,7 +107,8 @@ LookupContext::LookupContext(Document::Ptr expressionDocument,
                              const Snapshot &snapshot)
     : _expressionDocument(expressionDocument),
       _thisDocument(thisDocument),
-      _snapshot(snapshot)
+      _snapshot(snapshot),
+      _control(new Control())
 {
 }
 
@@ -113,7 +116,8 @@ LookupContext::LookupContext(const LookupContext &other)
     : _expressionDocument(other._expressionDocument),
       _thisDocument(other._thisDocument),
       _snapshot(other._snapshot),
-      _bindings(other._bindings)
+      _bindings(other._bindings),
+      _control(other._control)
 { }
 
 LookupContext &LookupContext::operator = (const LookupContext &other)
@@ -122,6 +126,7 @@ LookupContext &LookupContext::operator = (const LookupContext &other)
     _thisDocument = other._thisDocument;
     _snapshot = other._snapshot;
     _bindings = other._bindings;
+    _control = other._control;
     return *this;
 }
 
@@ -135,7 +140,7 @@ QList<const Name *> LookupContext::fullyQualifiedName(Symbol *symbol)
 QSharedPointer<CreateBindings> LookupContext::bindings() const
 {
     if (! _bindings)
-        _bindings = QSharedPointer<CreateBindings>(new CreateBindings(_thisDocument, _snapshot));
+        _bindings = QSharedPointer<CreateBindings>(new CreateBindings(_thisDocument, _snapshot, control()));
 
     return _bindings;
 }
@@ -145,8 +150,10 @@ void LookupContext::setBindings(QSharedPointer<CreateBindings> bindings)
     _bindings = bindings;
 }
 
-Control *LookupContext::control() const
-{ return bindings()->control(); }
+QSharedPointer<Control> LookupContext::control() const
+{
+    return _control;
+}
 
 Document::Ptr LookupContext::expressionDocument() const
 { return _expressionDocument; }
@@ -618,10 +625,9 @@ ClassOrNamespace *ClassOrNamespace::findOrCreateType(const Name *name)
     return 0;
 }
 
-CreateBindings::CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot)
-    : _snapshot(snapshot)
+CreateBindings::CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot, QSharedPointer<Control> control)
+    : _snapshot(snapshot), _control(control)
 {
-    _control = new Control();
     _globalNamespace = allocClassOrNamespace(/*parent = */ 0);
     _currentClassOrNamespace = _globalNamespace;
 
@@ -631,7 +637,6 @@ CreateBindings::CreateBindings(Document::Ptr thisDocument, const Snapshot &snaps
 CreateBindings::~CreateBindings()
 {
     qDeleteAll(_entities);
-    delete _control;
 }
 
 ClassOrNamespace *CreateBindings::switchCurrentClassOrNamespace(ClassOrNamespace *classOrNamespace)
@@ -673,7 +678,7 @@ void CreateBindings::process(Symbol *symbol)
     _currentClassOrNamespace->addTodo(symbol);
 }
 
-Control *CreateBindings::control() const
+QSharedPointer<Control> CreateBindings::control() const
 {
     return _control;
 }
diff --git a/src/libs/cplusplus/LookupContext.h b/src/libs/cplusplus/LookupContext.h
index 195fea8275f..73893a66701 100644
--- a/src/libs/cplusplus/LookupContext.h
+++ b/src/libs/cplusplus/LookupContext.h
@@ -35,6 +35,7 @@
 #include <FullySpecifiedType.h>
 #include <Type.h>
 #include <SymbolVisitor.h>
+#include <Control.h>
 #include <QtCore/QSet>
 #include <map>
 #include <functional>
@@ -116,7 +117,7 @@ class CPLUSPLUS_EXPORT CreateBindings: protected SymbolVisitor
     Q_DISABLE_COPY(CreateBindings)
 
 public:
-    CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot);
+    CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot, QSharedPointer<Control> control);
     virtual ~CreateBindings();
 
     /// Returns the binding for the global namespace.
@@ -127,7 +128,7 @@ public:
 
     /// Returns the Control that must be used to create temporary symbols.
     /// \internal
-    Control *control() const;
+    QSharedPointer<Control> control() const;
 
     /// Searches in \a scope for symbols with the given \a name.
     /// Store the result in \a results.
@@ -182,8 +183,8 @@ protected:
     virtual bool visit(ObjCMethod *);
 
 private:
-    Control *_control;
     Snapshot _snapshot;
+    QSharedPointer<Control> _control;
     QSet<Namespace *> _processed;
     QList<ClassOrNamespace *> _entities;
     ClassOrNamespace *_globalNamespace;
@@ -222,7 +223,7 @@ public:
     /// \internal
     void setBindings(QSharedPointer<CreateBindings> bindings);
 
-    Control *control() const; // ### deprecate
+    QSharedPointer<Control> control() const; // ### deprecate
 
     static QList<const Name *> fullyQualifiedName(Symbol *symbol);
 
@@ -238,6 +239,8 @@ private:
 
     // Bindings
     mutable QSharedPointer<CreateBindings> _bindings;
+
+    QSharedPointer<Control> _control;
 };
 
 } // end of namespace CPlusPlus
diff --git a/src/libs/cplusplus/ResolveExpression.cpp b/src/libs/cplusplus/ResolveExpression.cpp
index 939c48eb18c..5ac7ca2df78 100644
--- a/src/libs/cplusplus/ResolveExpression.cpp
+++ b/src/libs/cplusplus/ResolveExpression.cpp
@@ -604,7 +604,7 @@ ClassOrNamespace *ResolveExpression::baseExpression(const QList<LookupItem> &bas
 
                 foreach (Symbol *overload, binding->find(arrowOp)) {
                     if (overload->type()->isFunctionType()) {
-                        FullySpecifiedType overloadTy = DeprecatedGenTemplateInstance::instantiate(binding->templateId(), overload, control());
+                        FullySpecifiedType overloadTy = instantiate(binding->templateId(), overload);
                         Function *instantiatedFunction = overloadTy->asFunctionType();
                         Q_ASSERT(instantiatedFunction != 0);
 
-- 
GitLab