OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / cplusplus / pp-engine.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   Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
34
35   Permission to use, copy, modify, distribute, and sell this software and its
36   documentation for any purpose is hereby granted without fee, provided that
37   the above copyright notice appear in all copies and that both that
38   copyright notice and this permission notice appear in supporting
39   documentation.
40
41   The above copyright notice and this permission notice shall be included in
42   all copies or substantial portions of the Software.
43
44   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
47   KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
48   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
49   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 */
51
52 #ifndef CPLUSPLUS_PP_ENGINE_H
53 #define CPLUSPLUS_PP_ENGINE_H
54
55 #include "PreprocessorClient.h"
56 #include "pp-macro-expander.h"
57
58 #include <Token.h>
59 #include <QVector>
60 #include <QBitArray>
61
62 namespace CPlusPlus {
63
64 struct Value;
65 class Environment;
66
67 class CPLUSPLUS_EXPORT Preprocessor
68 {
69 public:
70     Preprocessor(Client *client, Environment *env);
71
72     QByteArray operator()(const QString &filename, const QString &source);
73     QByteArray operator()(const QString &filename, const QByteArray &source);
74
75     void preprocess(const QString &filename,
76                     const QByteArray &source,
77                     QByteArray *result);
78
79     bool expandMacros() const;
80     void setExpandMacros(bool expandMacros);
81
82 private:
83     enum { MAX_LEVEL = 512 };
84
85     enum PP_DIRECTIVE_TYPE
86     {
87         PP_UNKNOWN_DIRECTIVE,
88         PP_DEFINE,
89         PP_IMPORT,
90         PP_INCLUDE,
91         PP_INCLUDE_NEXT,
92         PP_ELIF,
93         PP_ELSE,
94         PP_ENDIF,
95         PP_IF,
96         PP_IFDEF,
97         PP_IFNDEF,
98         PP_UNDEF
99     };
100
101     typedef const CPlusPlus::Token *TokenIterator;
102
103     struct State {
104         QByteArray source;
105         QVector<CPlusPlus::Token> tokens;
106         TokenIterator dot;
107     };
108
109     bool markGeneratedTokens(bool markGeneratedTokens, TokenIterator dot = 0);
110
111     QByteArray expand(const QByteArray &source);
112     void expand(const QByteArray &source, QByteArray *result);
113     void expand(const char *first, const char *last, QByteArray *result);
114     void expandBuiltinMacro(TokenIterator identifierToken,
115                             const QByteArray &spell);
116     void expandObjectLikeMacro(TokenIterator identifierToken,
117                                const QByteArray &spell,
118                                Macro *m, QByteArray *result);
119     void expandFunctionLikeMacro(TokenIterator identifierToken, Macro *m,
120                                  const QVector<MacroArgumentReference> &actuals);
121
122     void resetIfLevel();
123     bool testIfLevel();
124     int skipping() const;
125
126     PP_DIRECTIVE_TYPE classifyDirective(const QByteArray &directive) const;
127
128     Value evalExpression(TokenIterator firstToken,
129                          TokenIterator lastToken,
130                          const QByteArray &source) const;
131
132     QVector<CPlusPlus::Token> tokenize(const QByteArray &text) const;
133
134     const char *startOfToken(const CPlusPlus::Token &token) const;
135     const char *endOfToken(const CPlusPlus::Token &token) const;
136
137     QByteArray tokenSpell(const CPlusPlus::Token &token) const;
138     QByteArray tokenText(const CPlusPlus::Token &token) const; // does a deep copy
139
140     void collectActualArguments(QVector<MacroArgumentReference> *actuals);
141     MacroArgumentReference collectOneActualArgument();
142
143     void processNewline(bool force = false);
144
145     void processSkippingBlocks(bool skippingBlocks,
146                                TokenIterator dot, TokenIterator lastToken);
147
148     Macro *processObjectLikeMacro(TokenIterator identifierToken,
149                                   const QByteArray &spell,
150                                   Macro *m);
151
152     void processDirective(TokenIterator dot, TokenIterator lastToken);
153     void processInclude(bool skipCurrentPath,
154                         TokenIterator dot, TokenIterator lastToken,
155                         bool acceptMacros = true);
156     void processDefine(TokenIterator dot, TokenIterator lastToken);
157     void processIf(TokenIterator dot, TokenIterator lastToken);
158     void processElse(TokenIterator dot, TokenIterator lastToken);
159     void processElif(TokenIterator dot, TokenIterator lastToken);
160     void processEndif(TokenIterator dot, TokenIterator lastToken);
161     void processIfdef(bool checkUndefined,
162                       TokenIterator dot, TokenIterator lastToken);
163     void processUndef(TokenIterator dot, TokenIterator lastToken);
164
165     bool isQtReservedWord(const QByteArray &name) const;
166
167     State state() const;
168     void pushState(const State &state);
169     void popState();
170
171     State createStateFromSource(const QByteArray &source) const;
172
173     void out(const QByteArray &text);
174     void out(char ch);
175     void out(const char *s);
176
177     QString string(const char *first, int len) const;
178     bool maybeAfterComment() const;
179
180 private:
181     Client *client;
182     Environment *env;
183     MacroExpander _expand;
184
185     QBitArray _skipping; // ### move in state
186     QBitArray _trueTest; // ### move in state
187     int iflevel; // ### move in state
188
189     QList<State> _savedStates;
190
191     QByteArray _source;
192     QVector<CPlusPlus::Token> _tokens;
193     TokenIterator _dot;
194
195     QByteArray *_result;
196     bool _markGeneratedTokens;
197
198     QString _originalSource;
199     bool _expandMacros;
200 };
201
202 } // namespace CPlusPlus
203
204 #endif // CPLUSPLUS_PP_ENGINE_H