OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmljs / qmljscheck.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 qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #ifndef QMLJSCHECK_H
34 #define QMLJSCHECK_H
35
36 #include <qmljs/qmljsdocument.h>
37 #include <qmljs/qmljsinterpreter.h>
38 #include <qmljs/qmljsscopebuilder.h>
39 #include <qmljs/parser/qmljsastvisitor_p.h>
40
41 #include <QtCore/QCoreApplication>
42 #include <QtCore/QSet>
43 #include <QtCore/QStack>
44 #include <QtGui/QColor>
45
46 namespace QmlJS {
47
48 class QMLJS_EXPORT Check: protected AST::Visitor
49 {
50     Q_DECLARE_TR_FUNCTIONS(QmlJS::Check)
51
52     typedef QSet<QString> StringSet;
53
54 public:
55     Check(Document::Ptr doc, const Snapshot &snapshot, const Interpreter::Context *linkedContextNoScope);
56     virtual ~Check();
57
58     QList<DiagnosticMessage> operator()();
59
60     enum Option {
61         WarnDangerousNonStrictEqualityChecks = 1 << 0,
62         WarnAllNonStrictEqualityChecks       = 1 << 1,
63         WarnBlocks                           = 1 << 2,
64         WarnWith                             = 1 << 3,
65         WarnVoid                             = 1 << 4,
66         WarnCommaExpression                  = 1 << 5,
67         WarnExpressionStatement              = 1 << 6,
68         WarnAssignInCondition                = 1 << 7,
69         WarnUseBeforeDeclaration             = 1 << 8,
70         WarnDuplicateDeclaration             = 1 << 9,
71         WarnDeclarationsNotStartOfFunction   = 1 << 10,
72         WarnCaseWithoutFlowControlEnd        = 1 << 11,
73         ErrCheckTypeErrors                   = 1 << 12
74     };
75     Q_DECLARE_FLAGS(Options, Option)
76
77     const Options options() const
78     { return _options; }
79
80     void setOptions(Options options)
81     { _options = options; }
82
83 protected:
84     virtual bool preVisit(AST::Node *ast);
85     virtual void postVisit(AST::Node *ast);
86
87     virtual bool visit(AST::UiProgram *ast);
88     virtual bool visit(AST::UiObjectDefinition *ast);
89     virtual bool visit(AST::UiObjectBinding *ast);
90     virtual bool visit(AST::UiScriptBinding *ast);
91     virtual bool visit(AST::UiArrayBinding *ast);
92     virtual bool visit(AST::IdentifierExpression *ast);
93     virtual bool visit(AST::FieldMemberExpression *ast);
94     virtual bool visit(AST::FunctionDeclaration *ast);
95     virtual bool visit(AST::FunctionExpression *ast);
96     virtual bool visit(AST::UiObjectInitializer *);
97
98     virtual bool visit(AST::BinaryExpression *ast);
99     virtual bool visit(AST::Block *ast);
100     virtual bool visit(AST::WithStatement *ast);
101     virtual bool visit(AST::VoidExpression *ast);
102     virtual bool visit(AST::Expression *ast);
103     virtual bool visit(AST::ExpressionStatement *ast);
104     virtual bool visit(AST::IfStatement *ast);
105     virtual bool visit(AST::ForStatement *ast);
106     virtual bool visit(AST::LocalForStatement *ast);
107     virtual bool visit(AST::WhileStatement *ast);
108     virtual bool visit(AST::DoWhileStatement *ast);
109     virtual bool visit(AST::CaseClause *ast);
110     virtual bool visit(AST::DefaultClause *ast);
111
112     virtual void endVisit(QmlJS::AST::UiObjectInitializer *);
113
114 private:
115     void visitQmlObject(AST::Node *ast, AST::UiQualifiedId *typeId,
116                         AST::UiObjectInitializer *initializer);
117     const Interpreter::Value *checkScopeObjectMember(const AST::UiQualifiedId *id);
118     void checkAssignInCondition(AST::ExpressionNode *condition);
119     void checkEndsWithControlFlow(AST::StatementList *statements, AST::SourceLocation errorLoc);
120     void checkProperty(QmlJS::AST::UiQualifiedId *);
121
122     void warning(const AST::SourceLocation &loc, const QString &message);
123     void error(const AST::SourceLocation &loc, const QString &message);
124
125     AST::Node *parent(int distance = 0);
126
127     Document::Ptr _doc;
128     Snapshot _snapshot;
129
130     Interpreter::Context _context;
131     ScopeBuilder _scopeBuilder;
132
133     QList<DiagnosticMessage> _messages;
134
135     Options _options;
136
137     const Interpreter::Value *_lastValue;
138     QList<AST::Node *> _chain;
139     QStack<StringSet> m_idStack;
140     QStack<StringSet> m_propertyStack;
141 };
142
143 QMLJS_EXPORT QColor toQColor(const QString &qmlColorString);
144
145 QMLJS_EXPORT AST::SourceLocation locationFromRange(const AST::SourceLocation &start,
146                                                    const AST::SourceLocation &end);
147
148 QMLJS_EXPORT AST::SourceLocation fullLocationForQualifiedId(AST::UiQualifiedId *);
149
150 QMLJS_EXPORT DiagnosticMessage errorMessage(const AST::SourceLocation &loc,
151                                             const QString &message);
152
153 template <class T>
154 DiagnosticMessage errorMessage(const T *node, const QString &message)
155 {
156     return DiagnosticMessage(DiagnosticMessage::Error,
157                              locationFromRange(node->firstSourceLocation(),
158                                                node->lastSourceLocation()),
159                              message);
160 }
161
162 } // namespace QmlJS
163
164 #endif // QMLJSCHECK_H