OSDN Git Service

Merge remote-tracking branch 'origin/2.3'
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmljs / qmljsdocument.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32 #ifndef QMLDOCUMENT_H
33 #define QMLDOCUMENT_H
34
35 #include <QtCore/QList>
36 #include <QtCore/QMap>
37 #include <QtCore/QPair>
38 #include <QtCore/QSharedPointer>
39 #include <QtCore/QString>
40
41 #include <languageutils/fakemetaobject.h>
42
43 #include "parser/qmldirparser_p.h"
44 #include "parser/qmljsengine_p.h"
45 #include "qmljs_global.h"
46
47 QT_QML_BEGIN_NAMESPACE
48
49 namespace QmlJS {
50
51 class Bind;
52 class Snapshot;
53
54 class QMLJS_EXPORT Document
55 {
56 public:
57     typedef QSharedPointer<Document> Ptr;
58
59     enum Language
60     {
61         QmlLanguage = 0,
62         JavaScriptLanguage = 1,
63         UnknownLanguage = 2
64     };
65
66 protected:
67     Document(const QString &fileName, Language language);
68
69 public:
70     ~Document();
71
72     static Document::Ptr create(const QString &fileName, Language language);
73
74     Document::Ptr ptr() const;
75
76     bool isQmlDocument() const;
77     bool isJSDocument() const;
78     Language language() const;
79
80     AST::UiProgram *qmlProgram() const;
81     AST::Program *jsProgram() const;
82     AST::ExpressionNode *expression() const;
83     AST::Node *ast() const;
84
85     const QmlJS::Engine *engine() const;
86
87     QList<DiagnosticMessage> diagnosticMessages() const;
88
89     QString source() const;
90     void setSource(const QString &source);
91
92     bool parse();
93     bool parseQml();
94     bool parseJavaScript();
95     bool parseExpression();
96
97     bool isParsedCorrectly() const
98     { return _parsedCorrectly; }
99
100     Bind *bind() const;
101
102     int editorRevision() const;
103     void setEditorRevision(int revision);
104
105     QString fileName() const;
106     QString path() const;
107     QString componentName() const;
108
109 private:
110     bool parse_helper(int kind);
111     static void extractPragmas(QString *source);
112
113 private:
114     QmlJS::Engine *_engine;
115     AST::Node *_ast;
116     Bind *_bind;
117     QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
118     QString _fileName;
119     QString _path;
120     QString _componentName;
121     QString _source;
122     QWeakPointer<Document> _ptr;
123     int _editorRevision;
124     Language _language : 2;
125     bool _parsedCorrectly : 1;
126
127     // for documentFromSource
128     friend class Snapshot;
129 };
130
131 class QMLJS_EXPORT LibraryInfo
132 {
133 public:
134     enum PluginTypeInfoStatus {
135         NoTypeInfo,
136         DumpDone,
137         DumpError,
138         TypeInfoFileDone,
139         TypeInfoFileError
140     };
141
142     enum Status {
143         NotScanned,
144         NotFound,
145         Found
146     };
147
148 private:
149     Status _status;
150     QList<QmlDirParser::Component> _components;
151     QList<QmlDirParser::Plugin> _plugins;
152     QList<QmlDirParser::TypeInfo> _typeinfos;
153     typedef QList<LanguageUtils::FakeMetaObject::ConstPtr> FakeMetaObjectList;
154     FakeMetaObjectList _metaObjects;
155
156     PluginTypeInfoStatus _dumpStatus;
157     QString _dumpError;
158
159 public:
160     explicit LibraryInfo(Status status = NotScanned);
161     explicit LibraryInfo(const QmlDirParser &parser);
162     ~LibraryInfo();
163
164     QList<QmlDirParser::Component> components() const
165     { return _components; }
166
167     QList<QmlDirParser::Plugin> plugins() const
168     { return _plugins; }
169
170     QList<QmlDirParser::TypeInfo> typeInfos() const
171     { return _typeinfos; }
172
173     FakeMetaObjectList metaObjects() const
174     { return _metaObjects; }
175
176     void setMetaObjects(const FakeMetaObjectList &objects)
177     { _metaObjects = objects; }
178
179     bool isValid() const
180     { return _status == Found; }
181
182     bool wasScanned() const
183     { return _status != NotScanned; }
184
185     PluginTypeInfoStatus pluginTypeInfoStatus() const
186     { return _dumpStatus; }
187
188     QString pluginTypeInfoError() const
189     { return _dumpError; }
190
191     void setPluginTypeInfoStatus(PluginTypeInfoStatus dumped, const QString &error = QString())
192     { _dumpStatus = dumped; _dumpError = error; }
193 };
194
195 class QMLJS_EXPORT Snapshot
196 {
197     typedef QHash<QString, Document::Ptr> _Base;
198     QHash<QString, Document::Ptr> _documents;
199     QHash<QString, QList<Document::Ptr> > _documentsByPath;
200     QHash<QString, LibraryInfo> _libraries;
201
202 public:
203     Snapshot();
204     ~Snapshot();
205
206     typedef _Base::iterator iterator;
207     typedef _Base::const_iterator const_iterator;
208
209     const_iterator begin() const { return _documents.begin(); }
210     const_iterator end() const { return _documents.end(); }
211
212     void insert(const Document::Ptr &document, bool allowInvalid = false);
213     void insertLibraryInfo(const QString &path, const LibraryInfo &info);
214     void remove(const QString &fileName);
215
216     Document::Ptr document(const QString &fileName) const;
217     QList<Document::Ptr> documentsInDirectory(const QString &path) const;
218     LibraryInfo libraryInfo(const QString &path) const;
219
220     Document::Ptr documentFromSource(const QString &code,
221                                      const QString &fileName,
222                                      Document::Language language) const;
223 };
224
225 } // namespace QmlJS
226
227 QT_QML_END_NAMESPACE
228
229 #endif // QMLDOCUMENT_H