Files
qt-creator/src/libs/cplusplus/CppDocument.h

412 lines
11 KiB
C
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
2011-01-11 16:28:15 +01:00
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
2011-04-13 08:42:33 +02:00
** Contact: Nokia Corporation (info@qt.nokia.com)
2008-12-02 12:01:29 +01:00
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** 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.
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** 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.
**
2010-12-17 16:01:08 +01:00
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 14:09:21 +01:00
2010-05-28 17:17:11 +02:00
#ifndef CPLUSPLUS_CPPDOCUMENT_H
#define CPLUSPLUS_CPPDOCUMENT_H
2008-12-02 12:01:29 +01:00
#include <CPlusPlusForwardDeclarations.h>
#include "Macro.h"
2008-12-08 12:59:33 +01:00
2009-12-07 11:12:55 +01:00
#include <QtCore/QSharedPointer>
#include <QtCore/QDateTime>
#include <QtCore/QHash>
#include <QtCore/QFileInfo>
2008-12-02 12:01:29 +01:00
namespace CPlusPlus {
2008-12-08 12:59:33 +01:00
class Macro;
class MacroArgumentReference;
class LookupContext;
2008-12-08 12:59:33 +01:00
2008-12-02 12:01:29 +01:00
class CPLUSPLUS_EXPORT Document
{
Document(const Document &other);
void operator =(const Document &other);
Document(const QString &fileName);
public:
typedef QSharedPointer<Document> Ptr;
public:
~Document();
unsigned revision() const;
void setRevision(unsigned revision);
unsigned editorRevision() const;
void setEditorRevision(unsigned editorRevision);
QDateTime lastModified() const;
void setLastModified(const QDateTime &lastModified);
2008-12-02 12:01:29 +01:00
QString fileName() const;
QStringList includedFiles() const;
void addIncludeFile(const QString &fileName, unsigned line);
2008-12-02 12:01:29 +01:00
2008-12-08 12:59:33 +01:00
void appendMacro(const Macro &macro);
void addMacroUse(const Macro &macro, unsigned offset, unsigned length,
unsigned beginLine, const QVector<MacroArgumentReference> &range,
bool inCondition);
void addUndefinedMacroUse(const QByteArray &name, unsigned offset);
2008-12-02 12:01:29 +01:00
Control *control() const;
TranslationUnit *translationUnit() const;
bool skipFunctionBody() const;
void setSkipFunctionBody(bool skipFunctionBody);
unsigned globalSymbolCount() const;
Symbol *globalSymbolAt(unsigned index) const;
2008-12-02 12:01:29 +01:00
Namespace *globalNamespace() const;
void setGlobalNamespace(Namespace *globalNamespace); // ### internal
2008-12-02 12:01:29 +01:00
2008-12-08 12:59:33 +01:00
QList<Macro> definedMacros() const
{ return _definedMacros; }
Symbol *lastVisibleSymbolAt(unsigned line, unsigned column = 0) const;
Scope *scopeAt(unsigned line, unsigned column = 0);
2008-12-02 12:01:29 +01:00
2009-06-05 14:29:57 +02:00
QByteArray source() const;
2008-12-02 12:01:29 +01:00
void setSource(const QByteArray &source);
2009-06-05 14:29:57 +02:00
2008-12-02 12:01:29 +01:00
void startSkippingBlocks(unsigned offset);
void stopSkippingBlocks(unsigned offset);
enum ParseMode { // ### keep in sync with CPlusPlus::TranslationUnit
ParseTranlationUnit,
ParseDeclaration,
ParseExpression,
2009-03-30 15:07:30 +02:00
ParseDeclarator,
ParseStatement
};
bool isTokenized() const;
void tokenize();
bool isParsed() const;
bool parse(ParseMode mode = ParseTranlationUnit);
2009-06-26 09:11:14 +02:00
enum CheckMode {
FullCheck,
FastCheck
};
void check(CheckMode mode = FullCheck);
void findExposedQmlTypes();
void releaseSource();
2008-12-02 12:01:29 +01:00
void releaseTranslationUnit();
static Ptr create(const QString &fileName);
class DiagnosticMessage
{
public:
enum Level {
Warning,
Error,
Fatal
};
public:
DiagnosticMessage(int level, const QString &fileName,
unsigned line, unsigned column,
const QString &text,
unsigned length = 0)
2008-12-02 12:01:29 +01:00
: _level(level),
_fileName(fileName),
_line(line),
_column(column),
_length(length),
2008-12-02 12:01:29 +01:00
_text(text)
{ }
int level() const
{ return _level; }
bool isWarning() const
{ return _level == Warning; }
bool isError() const
{ return _level == Error; }
bool isFatal() const
{ return _level == Fatal; }
QString fileName() const
{ return _fileName; }
2008-12-18 10:51:25 +01:00
unsigned line() const
2008-12-02 12:01:29 +01:00
{ return _line; }
2008-12-18 10:51:25 +01:00
unsigned column() const
2008-12-02 12:01:29 +01:00
{ return _column; }
unsigned length() const
{ return _length; }
2008-12-02 12:01:29 +01:00
QString text() const
{ return _text; }
private:
int _level;
QString _fileName;
2008-12-18 10:51:25 +01:00
unsigned _line;
unsigned _column;
unsigned _length;
2008-12-02 12:01:29 +01:00
QString _text;
};
void addDiagnosticMessage(const DiagnosticMessage &d)
{ _diagnosticMessages.append(d); }
QList<DiagnosticMessage> diagnosticMessages() const
{ return _diagnosticMessages; }
class Block
{
unsigned _begin;
unsigned _end;
public:
inline Block(unsigned begin = 0, unsigned end = 0)
: _begin(begin), _end(end)
{ }
inline bool isNull() const
{ return length() == 0; }
inline unsigned position() const
{ return _begin; }
inline unsigned length() const
{ return _end - _begin; }
2008-12-02 12:01:29 +01:00
inline unsigned begin() const
{ return _begin; }
inline unsigned end() const
{ return _end; }
2008-12-09 15:23:47 +01:00
bool contains(unsigned pos) const
{ return pos >= _begin && pos < _end; }
};
class Include {
QString _fileName;
unsigned _line;
public:
Include(const QString &fileName, unsigned line)
: _fileName(fileName), _line(line)
{ }
QString fileName() const
{ return _fileName; }
unsigned line() const
{ return _line; }
bool resolved() const
{ return QFileInfo(_fileName).isAbsolute(); }
};
2008-12-09 15:23:47 +01:00
class MacroUse: public Block {
Macro _macro;
QVector<Block> _arguments;
bool _inCondition;
unsigned _beginLine;
2008-12-09 15:23:47 +01:00
public:
inline MacroUse(const Macro &macro,
unsigned begin, unsigned end, unsigned beginLine)
2008-12-09 15:23:47 +01:00
: Block(begin, end),
_macro(macro),
_inCondition(false),
_beginLine(beginLine)
2008-12-09 15:23:47 +01:00
{ }
const Macro &macro() const
{ return _macro; }
bool isFunctionLike() const
{ return _macro.isFunctionLike(); }
QVector<Block> arguments() const
{ return _arguments; }
bool isInCondition() const
{ return _inCondition; }
unsigned beginLine() const
{ return _beginLine; }
private:
void setArguments(const QVector<Block> &arguments)
{ _arguments = arguments; }
void addArgument(const Block &block)
{ _arguments.append(block); }
void setInCondition(bool set)
{ _inCondition = set; }
friend class Document;
};
class UndefinedMacroUse: public Block {
QByteArray _name;
public:
inline UndefinedMacroUse(
const QByteArray &name,
unsigned begin)
: Block(begin, begin + name.length()),
_name(name)
{ }
QByteArray name() const
{
return _name;
}
2008-12-02 12:01:29 +01:00
};
QList<Include> includes() const
{ return _includes; }
2008-12-02 12:01:29 +01:00
QList<Block> skippedBlocks() const
{ return _skippedBlocks; }
2008-12-09 15:23:47 +01:00
QList<MacroUse> macroUses() const
{ return _macroUses; }
QList<UndefinedMacroUse> undefinedMacroUses() const
{ return _undefinedMacroUses; }
const Macro *findMacroDefinitionAt(unsigned line) const;
const MacroUse *findMacroUseAt(unsigned offset) const;
const UndefinedMacroUse *findUndefinedMacroUseAt(unsigned offset) const;
class ExportedQmlType {
public:
QString packageName;
QString typeName;
int majorVersion;
int minorVersion;
Scope *scope;
QString typeExpression;
};
QList<ExportedQmlType> exportedQmlTypes() const
{ return _exportedQmlTypes; }
2008-12-02 12:01:29 +01:00
private:
QString _fileName;
Control *_control;
TranslationUnit *_translationUnit;
Namespace *_globalNamespace;
QList<DiagnosticMessage> _diagnosticMessages;
QList<Include> _includes;
2008-12-08 12:59:33 +01:00
QList<Macro> _definedMacros;
2008-12-02 12:01:29 +01:00
QList<Block> _skippedBlocks;
2008-12-09 15:23:47 +01:00
QList<MacroUse> _macroUses;
QList<UndefinedMacroUse> _undefinedMacroUses;
QList<ExportedQmlType> _exportedQmlTypes;
QByteArray _source;
QDateTime _lastModified;
unsigned _revision;
unsigned _editorRevision;
friend class Snapshot;
2008-12-02 12:01:29 +01:00
};
class CPLUSPLUS_EXPORT Snapshot
{
2009-12-07 11:12:55 +01:00
typedef QHash<QString, Document::Ptr> _Base;
public:
Snapshot();
~Snapshot();
typedef _Base::const_iterator iterator;
typedef _Base::const_iterator const_iterator;
int size() const; // ### remove
bool isEmpty() const;
void insert(Document::Ptr doc); // ### remove
void remove(const QString &fileName); // ### remove
const_iterator begin() const { return _documents.begin(); }
const_iterator end() const { return _documents.end(); }
bool contains(const QString &fileName) const;
Document::Ptr document(const QString &fileName) const;
Document::Ptr operator[](const QString &fileName) const;
const_iterator find(const QString &fileName) const;
2009-07-10 11:59:01 +02:00
Snapshot simplified(Document::Ptr doc) const;
QByteArray preprocessedCode(const QString &source,
const QString &fileName) const;
Document::Ptr documentFromSource(const QByteArray &preprocessedCode,
const QString &fileName) const;
Symbol *findMatchingDefinition(Symbol *symbol, bool strict = false) const;
Class *findMatchingClassDeclaration(Symbol *symbol) const;
2009-07-10 11:59:01 +02:00
private:
void simplified_helper(Document::Ptr doc, Snapshot *snapshot) const;
private:
_Base _documents;
};
void CPLUSPLUS_EXPORT findMatchingDeclaration(
const LookupContext &context,
Function *functionType,
QList<Declaration *> *typeMatch,
QList<Declaration *> *argumentCountMatch,
QList<Declaration *> *nameMatch);
QList<Declaration *> CPLUSPLUS_EXPORT findMatchingDeclaration(
const LookupContext &context, Function *functionType);
} // namespace CPlusPlus
2008-12-02 12:01:29 +01:00
2010-05-28 17:17:11 +02:00
#endif // CPLUSPLUS_CPPDOCUMENT_H