OSDN Git Service

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