aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/ApiExtractor/parser/rpp/pp-engine.h
blob: 689d7720ed9b8e285706abdaf241facd20775ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
** Contact: https://www.qt.io/licensing/
**
** This file is part of PySide2.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef PP_ENGINE_H
#define PP_ENGINE_H

#include <string>
#include <vector>
#include "pp-scanner.h"
#include "pp-macro-expander.h"
#include "pp-environment.h"

namespace rpp
{

struct Value {
    Value() : kind(Kind_Long), l(0) {}

    enum Kind {
        Kind_Long,
        Kind_ULong,
    };

    Kind kind;

    union {
        long l;
        unsigned long ul;
    };

    inline bool is_ulong() const {
        return kind == Kind_ULong;
    }

    inline void set_ulong(unsigned long v) {
        ul = v;
        kind = Kind_ULong;
    }

    inline void set_long(long v) {
        l = v;
        kind = Kind_Long;
    }

    inline bool is_zero() const {
        return l == 0;
    }

#define PP_DEFINE_BIN_OP(name, op) \
    inline Value &name (const Value &other) \
    { \
        if (is_ulong () || other.is_ulong ()) \
            set_ulong (ul op other.ul); \
        else \
            set_long (l op other.l); \
        return *this; \
    }

    PP_DEFINE_BIN_OP(op_add, +)
    PP_DEFINE_BIN_OP(op_sub, -)
    PP_DEFINE_BIN_OP(op_mult, *)
    PP_DEFINE_BIN_OP(op_div, /)
    PP_DEFINE_BIN_OP(op_mod, %)
    PP_DEFINE_BIN_OP(op_lhs, <<)
    PP_DEFINE_BIN_OP(op_rhs, >>)
    PP_DEFINE_BIN_OP(op_lt, <)
    PP_DEFINE_BIN_OP(op_gt, >)
    PP_DEFINE_BIN_OP(op_le, <=)
    PP_DEFINE_BIN_OP(op_ge, >=)
    PP_DEFINE_BIN_OP(op_eq, ==)
    PP_DEFINE_BIN_OP(op_ne, !=)
    PP_DEFINE_BIN_OP(op_bit_and, &)
    PP_DEFINE_BIN_OP(op_bit_or, |)
    PP_DEFINE_BIN_OP(op_bit_xor, ^)
    PP_DEFINE_BIN_OP(op_and, &&)
    PP_DEFINE_BIN_OP(op_or, ||)

#undef PP_DEFINE_BIN_OP
};

class pp
{
    pp_environment &env;
    pp_macro_expander expand;
    pp_skip_identifier skip_identifier;
    pp_skip_comment_or_divop skip_comment_or_divop;
    pp_skip_blanks skip_blanks;
    pp_skip_number skip_number;
    std::vector<std::string> include_paths;
    std::string _M_current_text;

    enum { MAX_LEVEL = 512 };
    int _M_skipping[MAX_LEVEL];
    int _M_true_test[MAX_LEVEL];
    int iflevel;

    union {
        long token_value;
        unsigned long token_uvalue;
        std::string *token_text;
    };

    enum INCLUDE_POLICY {
        INCLUDE_GLOBAL,
        INCLUDE_LOCAL
    };

    enum TOKEN_TYPE {
        TOKEN_NUMBER = 1000,
        TOKEN_UNUMBER,
        TOKEN_IDENTIFIER,
        TOKEN_DEFINED,
        TOKEN_LT_LT,
        TOKEN_LT_EQ,
        TOKEN_GT_GT,
        TOKEN_GT_EQ,
        TOKEN_EQ_EQ,
        TOKEN_NOT_EQ,
        TOKEN_OR_OR,
        TOKEN_AND_AND,
    };

    enum PP_DIRECTIVE_TYPE {
        PP_UNKNOWN_DIRECTIVE,
        PP_UNNAMED_DIRECTIVE,
        PP_DEFINE,
        PP_INCLUDE,
        PP_INCLUDE_NEXT,
        PP_ELIF,
        PP_ELSE,
        PP_ENDIF,
        PP_IF,
        PP_IFDEF,
        PP_IFNDEF,
        PP_UNDEF,
        PP_PRAGMA,
        PP_ERROR,
        PP_WARNING
    };

public:
    pp(pp_environment &__env);

    inline std::back_insert_iterator<std::vector<std::string> > include_paths_inserter();

    inline void push_include_path(std::string const &__path);

    inline std::vector<std::string>::iterator include_paths_begin();
    inline std::vector<std::string>::iterator include_paths_end();

    inline std::vector<std::string>::const_iterator include_paths_begin() const;
    inline std::vector<std::string>::const_iterator include_paths_end() const;

    template <typename _InputIterator>
    inline _InputIterator eval_expression(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _OutputIterator>
    void file(std::string const &filename, _OutputIterator __result);

    template <typename _OutputIterator>
    void file(FILE *fp, _OutputIterator __result);

    template <typename _InputIterator, typename _OutputIterator>
    void operator()(_InputIterator __first, _InputIterator __last, _OutputIterator __result);

private:
    inline bool file_isdir(std::string const &__filename) const;
    inline bool file_exists(std::string const &__filename) const;
    FILE *find_include_file(std::string const &__filename, std::string *__filepath,
                            INCLUDE_POLICY __include_policy, bool __skip_current_path = false) const;

    inline int skipping() const;
    bool test_if_level();

    inline std::string fix_file_path(std::string const &filename) const;
    inline bool is_absolute(std::string const &filename) const;

    PP_DIRECTIVE_TYPE find_directive(char const *__directive, std::size_t __size) const;

    template <typename _InputIterator>
    bool find_header_protection(_InputIterator __first, _InputIterator __last, std::string *__prot);

    template <typename _InputIterator>
    _InputIterator skip(_InputIterator __first, _InputIterator __last);

    template <typename _InputIterator>
    _InputIterator eval_primary(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_multiplicative(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_additive(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_shift(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_relational(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_equality(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_and(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_xor(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_or(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_logical_and(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_logical_or(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator>
    _InputIterator eval_constant_expression(_InputIterator __first, _InputIterator __last, Value *result);

    template <typename _InputIterator, typename _OutputIterator>
    _InputIterator handle_directive(char const *__directive, std::size_t __size,
                                    _InputIterator __first, _InputIterator __last, _OutputIterator __result);

    template <typename _InputIterator, typename _OutputIterator>
    _InputIterator handle_include(bool skip_current_path, _InputIterator __first, _InputIterator __last,
                                  _OutputIterator __result);

    template <typename _InputIterator>
    _InputIterator handle_define(_InputIterator __first, _InputIterator __last);

    template <typename _InputIterator>
    _InputIterator handle_if(_InputIterator __first, _InputIterator __last);

    template <typename _InputIterator>
    _InputIterator handle_else(_InputIterator __first, _InputIterator __last);

    template <typename _InputIterator>
    _InputIterator handle_elif(_InputIterator __first, _InputIterator __last);

    template <typename _InputIterator>
    _InputIterator handle_endif(_InputIterator __first, _InputIterator __last);

    template <typename _InputIterator>
    _InputIterator handle_ifdef(bool check_undefined, _InputIterator __first, _InputIterator __last);

    template <typename _InputIterator>
    _InputIterator handle_undef(_InputIterator __first, _InputIterator __last);

    template <typename _InputIterator>
    inline char peek_char(_InputIterator __first, _InputIterator __last);

    template <typename _InputIterator>
    _InputIterator next_token(_InputIterator __first, _InputIterator __last, int *kind);
};

} // namespace rpp

#endif // PP_ENGINE_H

// kate: space-indent on; indent-width 2; replace-tabs on;