summaryrefslogtreecommitdiff
path: root/src/plugins/cpptools/cpplocalsymbols.cpp
diff options
context:
space:
mode:
authorErik Verbruggen <erik.verbruggen@nokia.com>2012-02-07 15:09:08 +0100
committerErik Verbruggen <erik.verbruggen@nokia.com>2012-02-08 14:30:14 +0100
commitdbc3332b8e1442c0903e129586c08986ce752ff9 (patch)
treeec9b94df1464e6693c09fb6638a38a7095722386 /src/plugins/cpptools/cpplocalsymbols.cpp
parentb64d103bfbe5fea3283e8a3c014f4a48dca3c482 (diff)
downloadqt-creator-dbc3332b8e1442c0903e129586c08986ce752ff9.tar.gz
C++: Moved completion/highlighting into the model manager.
This way the editor does not need to know all the details of instantiating or maintaining classes for highlighting and/or completion, it can just ask the model manager. The change also enables different highlighting- or completion-engines without changes to the cppeditor. Change-Id: I8000d9d9fe446b292defddb2295493cf77d0f14a Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
Diffstat (limited to 'src/plugins/cpptools/cpplocalsymbols.cpp')
-rw-r--r--src/plugins/cpptools/cpplocalsymbols.cpp302
1 files changed, 302 insertions, 0 deletions
diff --git a/src/plugins/cpptools/cpplocalsymbols.cpp b/src/plugins/cpptools/cpplocalsymbols.cpp
new file mode 100644
index 0000000000..fc4933ef17
--- /dev/null
+++ b/src/plugins/cpptools/cpplocalsymbols.cpp
@@ -0,0 +1,302 @@
+/**************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+**
+** GNU Lesser General Public License Usage
+**
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this file.
+** Please review the following information to ensure the GNU Lesser General
+** Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** Other Usage
+**
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**************************************************************************/
+
+#include "cpplocalsymbols.h"
+#include "cppsemanticinfo.h"
+
+#include <cplusplus/CppDocument.h>
+#include <ASTVisitor.h>
+#include <AST.h>
+#include <Scope.h>
+#include <Symbols.h>
+#include <CoreTypes.h>
+#include <Names.h>
+#include <Literals.h>
+
+using namespace CPlusPlus;
+using namespace CppTools;
+
+namespace {
+
+class FindLocalSymbols: protected ASTVisitor
+{
+ Scope *_functionScope;
+ Document::Ptr _doc;
+
+public:
+ FindLocalSymbols(Document::Ptr doc)
+ : ASTVisitor(doc->translationUnit()), _doc(doc), hasD(false), hasQ(false)
+ { }
+
+ // local and external uses.
+ SemanticInfo::LocalUseMap localUses;
+ bool hasD;
+ bool hasQ;
+
+ void operator()(DeclarationAST *ast)
+ {
+ localUses.clear();
+
+ if (!ast)
+ return;
+
+ if (FunctionDefinitionAST *def = ast->asFunctionDefinition()) {
+ if (def->symbol) {
+ _functionScope = def->symbol;
+ accept(ast);
+ }
+ } else if (ObjCMethodDeclarationAST *decl = ast->asObjCMethodDeclaration()) {
+ if (decl->method_prototype->symbol) {
+ _functionScope = decl->method_prototype->symbol;
+ accept(ast);
+ }
+ }
+ }
+
+protected:
+ using ASTVisitor::visit;
+ using ASTVisitor::endVisit;
+
+ void enterScope(Scope *scope)
+ {
+ _scopeStack.append(scope);
+
+ for (unsigned i = 0; i < scope->memberCount(); ++i) {
+ if (Symbol *member = scope->memberAt(i)) {
+ if (member->isTypedef())
+ continue;
+ else if (! member->isGenerated() && (member->isDeclaration() || member->isArgument())) {
+ if (member->name() && member->name()->isNameId()) {
+ const Identifier *id = member->identifier();
+ unsigned line, column;
+ getTokenStartPosition(member->sourceLocation(), &line, &column);
+ localUses[member].append(SemanticInfo::Use(line, column, id->size(), SemanticInfo::LocalUse));
+ }
+ }
+ }
+ }
+ }
+
+ bool checkLocalUse(NameAST *nameAst, unsigned firstToken)
+ {
+ if (SimpleNameAST *simpleName = nameAst->asSimpleName()) {
+ const Identifier *id = identifier(simpleName->identifier_token);
+ for (int i = _scopeStack.size() - 1; i != -1; --i) {
+ if (Symbol *member = _scopeStack.at(i)->find(id)) {
+ if (member->isTypedef() ||
+ !(member->isDeclaration() || member->isArgument()))
+ continue;
+ else if (!member->isGenerated() && (member->sourceLocation() < firstToken || member->enclosingScope()->isFunction())) {
+ unsigned line, column;
+ getTokenStartPosition(simpleName->identifier_token, &line, &column);
+ localUses[member].append(SemanticInfo::Use(line, column, id->size(), SemanticInfo::LocalUse));
+ return false;
+ }
+ }
+ }
+ }
+
+ return true;
+ }
+
+ virtual bool visit(IdExpressionAST *ast)
+ {
+ return checkLocalUse(ast->name, ast->firstToken());
+ }
+
+ virtual bool visit(SizeofExpressionAST *ast)
+ {
+ if (ast->expression && ast->expression->asTypeId()) {
+ TypeIdAST *typeId = ast->expression->asTypeId();
+ if (!typeId->declarator && typeId->type_specifier_list && !typeId->type_specifier_list->next) {
+ if (NamedTypeSpecifierAST *namedTypeSpec = typeId->type_specifier_list->value->asNamedTypeSpecifier()) {
+ if (checkLocalUse(namedTypeSpec->name, namedTypeSpec->firstToken()))
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+
+ virtual bool visit(CastExpressionAST *ast)
+ {
+ if (ast->expression && ast->expression->asUnaryExpression()) {
+ TypeIdAST *typeId = ast->type_id->asTypeId();
+ if (typeId && !typeId->declarator && typeId->type_specifier_list && !typeId->type_specifier_list->next) {
+ if (NamedTypeSpecifierAST *namedTypeSpec = typeId->type_specifier_list->value->asNamedTypeSpecifier()) {
+ if (checkLocalUse(namedTypeSpec->name, namedTypeSpec->firstToken())) {
+ accept(ast->expression);
+ return false;
+ }
+ }
+ }
+ }
+
+ return true;
+ }
+
+ virtual bool visit(QtMemberDeclarationAST *ast)
+ {
+ if (tokenKind(ast->q_token) == T_Q_D)
+ hasD = true;
+ else
+ hasQ = true;
+
+ return true;
+ }
+
+ virtual bool visit(FunctionDefinitionAST *ast)
+ {
+ if (ast->symbol)
+ enterScope(ast->symbol);
+ return true;
+ }
+
+ virtual void endVisit(FunctionDefinitionAST *ast)
+ {
+ if (ast->symbol)
+ _scopeStack.removeLast();
+ }
+
+ virtual bool visit(CompoundStatementAST *ast)
+ {
+ if (ast->symbol)
+ enterScope(ast->symbol);
+ return true;
+ }
+
+ virtual void endVisit(CompoundStatementAST *ast)
+ {
+ if (ast->symbol)
+ _scopeStack.removeLast();
+ }
+
+ virtual bool visit(IfStatementAST *ast)
+ {
+ if (ast->symbol)
+ enterScope(ast->symbol);
+ return true;
+ }
+
+ virtual void endVisit(IfStatementAST *ast)
+ {
+ if (ast->symbol)
+ _scopeStack.removeLast();
+ }
+
+ virtual bool visit(WhileStatementAST *ast)
+ {
+ if (ast->symbol)
+ enterScope(ast->symbol);
+ return true;
+ }
+
+ virtual void endVisit(WhileStatementAST *ast)
+ {
+ if (ast->symbol)
+ _scopeStack.removeLast();
+ }
+
+ virtual bool visit(ForStatementAST *ast)
+ {
+ if (ast->symbol)
+ enterScope(ast->symbol);
+ return true;
+ }
+
+ virtual void endVisit(ForStatementAST *ast)
+ {
+ if (ast->symbol)
+ _scopeStack.removeLast();
+ }
+
+ virtual bool visit(ForeachStatementAST *ast)
+ {
+ if (ast->symbol)
+ enterScope(ast->symbol);
+ return true;
+ }
+
+ virtual void endVisit(ForeachStatementAST *ast)
+ {
+ if (ast->symbol)
+ _scopeStack.removeLast();
+ }
+
+ virtual bool visit(SwitchStatementAST *ast)
+ {
+ if (ast->symbol)
+ enterScope(ast->symbol);
+ return true;
+ }
+
+ virtual void endVisit(SwitchStatementAST *ast)
+ {
+ if (ast->symbol)
+ _scopeStack.removeLast();
+ }
+
+ virtual bool visit(CatchClauseAST *ast)
+ {
+ if (ast->symbol)
+ enterScope(ast->symbol);
+ return true;
+ }
+
+ virtual void endVisit(CatchClauseAST *ast)
+ {
+ if (ast->symbol)
+ _scopeStack.removeLast();
+ }
+
+ virtual bool visit(ExpressionOrDeclarationStatementAST *ast)
+ {
+ accept(ast->declaration);
+ return false;
+ }
+
+private:
+ QList<Scope *> _scopeStack;
+};
+
+} // end of anonymous namespace
+
+
+LocalSymbols::LocalSymbols(CPlusPlus::Document::Ptr doc, CPlusPlus::DeclarationAST *ast)
+{
+ FindLocalSymbols findLocalSymbols(doc);
+ findLocalSymbols(ast);
+ hasD = findLocalSymbols.hasD;
+ hasQ = findLocalSymbols.hasQ;
+ uses = findLocalSymbols.localUses;
+}