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
|
// Copyright (C) 2020-2025 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#ifndef RUST_AST_CONDCOMPILATION
#define RUST_AST_CONDCOMPILATION
// Conditional compilation-related AST stuff
#include "rust-ast.h"
namespace Rust {
namespace AST {
// Base conditional compilation configuration predicate thing - abstract
class ConfigurationPredicate
{
public:
virtual ~ConfigurationPredicate () {}
// Unique pointer custom clone function
std::unique_ptr<ConfigurationPredicate> clone_configuration_predicate () const
{
return std::unique_ptr<ConfigurationPredicate> (
clone_configuration_predicate_impl ());
}
// not sure if I'll use this but here anyway
virtual void accept_vis (ASTVisitor &vis) = 0;
protected:
// Clone function impl to be overriden in base classes
virtual ConfigurationPredicate *
clone_configuration_predicate_impl () const = 0;
};
// A configuration option - true if option is set, false if option is not set.
class ConfigurationOption : public ConfigurationPredicate
{
Identifier option_name;
// bool has_string_literal_option_body;
std::string option_value; // technically a string or raw string literal
public:
/* Returns whether the configuration option has a "value" part of the
* key-value pair. */
bool has_option_value () const { return !option_value.empty (); }
// Key-value pair constructor
ConfigurationOption (Identifier option_name, std::string option_value)
: option_name (option_name), option_value (option_value)
{}
// Name-only constructor
ConfigurationOption (Identifier option_name) : option_name (option_name) {}
void accept_vis (ASTVisitor &vis) override;
protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
ConfigurationOption *clone_configuration_predicate_impl () const override
{
return new ConfigurationOption (*this);
}
};
// TODO: inline
struct ConfigurationPredicateList
{
std::vector<std::unique_ptr<ConfigurationPredicate>> predicate_list;
};
// Predicate that returns true if all of the supplied predicates return true.
class ConfigurationAll : public ConfigurationPredicate
{
std::vector<std::unique_ptr<ConfigurationPredicate>>
predicate_list; // inlined form
public:
ConfigurationAll (
std::vector<std::unique_ptr<ConfigurationPredicate>> predicate_list)
: predicate_list (predicate_list)
{}
void accept_vis (ASTVisitor &vis) override;
protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
ConfigurationAll *clone_configuration_predicate_impl () const override
{
return new ConfigurationAll (*this);
}
};
// Predicate that returns true if any of the supplied predicates are true.
class ConfigurationAny : public ConfigurationPredicate
{
std::vector<std::unique_ptr<ConfigurationPredicate>>
predicate_list; // inlined form
public:
ConfigurationAny (
std::vector<std::unique_ptr<ConfigurationPredicate>> predicate_list)
: predicate_list (predicate_list)
{}
void accept_vis (ASTVisitor &vis) override;
protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
ConfigurationAny *clone_configuration_predicate_impl () const override
{
return new ConfigurationAny (*this);
}
};
/* Predicate that produces the negation of a supplied other configuration
* predicate. */
class ConfigurationNot : public ConfigurationPredicate
{
std::unique_ptr<ConfigurationPredicate> config_to_negate;
public:
ConfigurationNot (ConfigurationPredicate *config_to_negate)
: config_to_negate (config_to_negate)
{}
// Copy constructor with clone
ConfigurationNot (ConfigurationNot const &other)
: config_to_negate (
other.config_to_negate->clone_configuration_predicate ())
{}
// Overloaded assignment operator to clone
ConfigurationNot &operator= (ConfigurationNot const &other)
{
config_to_negate = other.config_to_negate->clone_configuration_predicate ();
return *this;
}
// move constructors
ConfigurationNot (ConfigurationNot &&other) = default;
ConfigurationNot &operator= (ConfigurationNot &&other) = default;
void accept_vis (ASTVisitor &vis) override;
protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
ConfigurationNot *clone_configuration_predicate_impl () const override
{
return new ConfigurationNot (*this);
}
};
// TODO: relationship to other attributes?
class CfgAttribute
{
std::unique_ptr<ConfigurationPredicate> config_to_include;
public:
CfgAttribute (ConfigurationPredicate *config_to_include)
: config_to_include (config_to_include)
{}
// Copy constructor with clone
CfgAttribute (CfgAttribute const &other)
: config_to_include (
other.config_to_include->clone_configuration_predicate ())
{}
// Overloaded assignment operator to clone
CfgAttribute &operator= (CfgAttribute const &other)
{
config_to_include
= other.config_to_include->clone_configuration_predicate ();
return *this;
}
// move constructors
CfgAttribute (CfgAttribute &&other) = default;
CfgAttribute &operator= (CfgAttribute &&other) = default;
};
/* TODO: ok, best thing to do would be eliminating this class, making Attribute
* has a "is_cfg()" method, and having attribute path as "cfg" and AttrInput as
* ConfigurationPredicate (so make ConfigurationPredicate a subclass of
* AttrInput?). Would need special handling in parser, however. */
// TODO: inline
struct CfgAttrs
{
std::vector<Attribute> cfg_attrs;
};
// TODO: relationship to other attributes?
class CfgAttrAttribute
{
std::unique_ptr<ConfigurationPredicate> config_to_include;
std::vector<Attribute> cfg_attrs;
public:
CfgAttrAttribute (ConfigurationPredicate *config_to_include,
std::vector<Attribute> cfg_attrs)
: config_to_include (config_to_include), cfg_attrs (cfg_attrs)
{}
// Copy constructor with clone
CfgAttrAttribute (CfgAttrAttribute const &other)
: config_to_include (
other.config_to_include->clone_configuration_predicate ()),
cfg_attrs (cfg_attrs)
{}
// Overloaded assignment operator to clone
CfgAttrAttribute &operator= (CfgAttrAttribute const &other)
{
config_to_include
= other.config_to_include->clone_configuration_predicate ();
cfg_attrs = other.cfg_attrs;
return *this;
}
// move constructors
CfgAttrAttribute (CfgAttrAttribute &&other) = default;
CfgAttrAttribute &operator= (CfgAttrAttribute &&other) = default;
};
} // namespace AST
} // namespace Rust
#endif
|