OSDN Git Service

Merge remote branch 'origin/2.0'
[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) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29 #ifndef QMLDOCUMENT_H
30 #define QMLDOCUMENT_H
31
32 #include <QtCore/QList>
33 #include <QtCore/QMap>
34 #include <QtCore/QPair>
35 #include <QtCore/QSharedPointer>
36 #include <QtCore/QString>
37
38 #include "parser/qmldirparser_p.h"
39 #include "parser/qmljsengine_p.h"
40 #include "qmljs_global.h"
41
42 namespace QmlJS {
43
44 class Bind;
45 class Snapshot;
46
47 namespace Interpreter {
48     class FakeMetaObject;
49 }
50
51 class QMLJS_EXPORT Document
52 {
53 public:
54     typedef QSharedPointer<Document> Ptr;
55
56 protected:
57     Document(const QString &fileName);
58
59 public:
60     ~Document();
61
62     static Document::Ptr create(const QString &fileName);
63
64     bool isQmlDocument() const;
65     bool isJSDocument() const;
66
67     AST::UiProgram *qmlProgram() const;
68     AST::Program *jsProgram() const;
69     AST::ExpressionNode *expression() const;
70     AST::Node *ast() const;
71
72     QList<DiagnosticMessage> diagnosticMessages() const;
73
74     QString source() const;
75     void setSource(const QString &source);
76
77     bool parse();
78     bool parseQml();
79     bool parseJavaScript();
80     bool parseExpression();
81
82     bool isParsedCorrectly() const
83     { return _parsedCorrectly; }
84
85     Bind *bind() const;
86
87     int documentRevision() const;
88     void setDocumentRevision(int documentRevision);
89
90     QString fileName() const;
91     QString path() const;
92     QString componentName() const;
93
94 private:
95     bool parse_helper(int kind);
96
97 private:
98     QmlJS::Engine *_engine;
99     NodePool *_pool;
100     AST::Node *_ast;
101     Bind *_bind;
102     bool _isQmlDocument;
103     int _documentRevision;
104     bool _parsedCorrectly;
105     QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
106     QString _fileName;
107     QString _path;
108     QString _componentName;
109     QString _source;
110
111     // for documentFromSource
112     friend class Snapshot;
113 };
114
115 class QMLJS_EXPORT LibraryInfo
116 {
117     bool _valid;
118     QList<QmlDirParser::Component> _components;
119     QList<QmlDirParser::Plugin> _plugins;
120     typedef QList<const Interpreter::FakeMetaObject *> FakeMetaObjectList;
121     FakeMetaObjectList _metaObjects;
122
123 public:
124     LibraryInfo();
125     LibraryInfo(const QmlDirParser &parser);
126     ~LibraryInfo();
127
128     QList<QmlDirParser::Component> components() const
129     { return _components; }
130
131     QList<QmlDirParser::Plugin> plugins() const
132     { return _plugins; }
133
134     FakeMetaObjectList metaObjects() const
135     { return _metaObjects; }
136
137     void setMetaObjects(const FakeMetaObjectList &objects)
138     { _metaObjects = objects; }
139
140     bool isValid() const
141     { return _valid; }
142 };
143
144 class QMLJS_EXPORT Snapshot
145 {
146     typedef QHash<QString, Document::Ptr> _Base;
147     QHash<QString, Document::Ptr> _documents;
148     QMultiHash<QString, Document::Ptr> _documentsByPath;
149     QHash<QString, LibraryInfo> _libraries;
150
151 public:
152     Snapshot();
153     ~Snapshot();
154
155     typedef _Base::iterator iterator;
156     typedef _Base::const_iterator const_iterator;
157
158     const_iterator begin() const { return _documents.begin(); }
159     const_iterator end() const { return _documents.end(); }
160
161     void insert(const Document::Ptr &document);
162     void insertLibraryInfo(const QString &path, const LibraryInfo &info);
163     void remove(const QString &fileName);
164
165     Document::Ptr document(const QString &fileName) const;
166     QList<Document::Ptr> documentsInDirectory(const QString &path) const;
167     LibraryInfo libraryInfo(const QString &path) const;
168
169     Document::Ptr documentFromSource(const QString &code,
170                                      const QString &fileName) const;
171
172     QList<Document::Ptr> importedDocuments(const Document::Ptr &doc, const QString &importPath) const;
173     QMap<QString, Document::Ptr> componentsDefinedByImportedDocuments(const Document::Ptr &doc, const QString &importPath) const;
174 };
175
176 } // end of namespace Qml
177
178 #endif // QMLDOCUMENT_H