1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 #ifndef RUST_AST_CONDCOMPILATION
20 #define RUST_AST_CONDCOMPILATION
21 // Conditional compilation-related AST stuff
27 // Base conditional compilation configuration predicate thing - abstract
28 class ConfigurationPredicate
31 virtual ~ConfigurationPredicate () {}
33 // Unique pointer custom clone function
34 std::unique_ptr
<ConfigurationPredicate
> clone_configuration_predicate () const
36 return std::unique_ptr
<ConfigurationPredicate
> (
37 clone_configuration_predicate_impl ());
40 // not sure if I'll use this but here anyway
41 virtual void accept_vis (ASTVisitor
&vis
) = 0;
44 // Clone function impl to be overriden in base classes
45 virtual ConfigurationPredicate
*
46 clone_configuration_predicate_impl () const = 0;
49 // A configuration option - true if option is set, false if option is not set.
50 class ConfigurationOption
: public ConfigurationPredicate
52 Identifier option_name
;
54 // bool has_string_literal_option_body;
55 std::string option_value
; // technically a string or raw string literal
58 /* Returns whether the configuration option has a "value" part of the
60 bool has_option_value () const { return !option_value
.empty (); }
62 // Key-value pair constructor
63 ConfigurationOption (Identifier option_name
, std::string option_value
)
64 : option_name (option_name
), option_value (option_value
)
67 // Name-only constructor
68 ConfigurationOption (Identifier option_name
) : option_name (option_name
) {}
70 void accept_vis (ASTVisitor
&vis
) override
;
73 /* Use covariance to implement clone function as returning this object rather
75 ConfigurationOption
*clone_configuration_predicate_impl () const override
77 return new ConfigurationOption (*this);
82 struct ConfigurationPredicateList
84 std::vector
<std::unique_ptr
<ConfigurationPredicate
>> predicate_list
;
87 // Predicate that returns true if all of the supplied predicates return true.
88 class ConfigurationAll
: public ConfigurationPredicate
90 std::vector
<std::unique_ptr
<ConfigurationPredicate
>>
91 predicate_list
; // inlined form
95 std::vector
<std::unique_ptr
<ConfigurationPredicate
>> predicate_list
)
96 : predicate_list (predicate_list
)
99 void accept_vis (ASTVisitor
&vis
) override
;
102 /* Use covariance to implement clone function as returning this object rather
104 ConfigurationAll
*clone_configuration_predicate_impl () const override
106 return new ConfigurationAll (*this);
110 // Predicate that returns true if any of the supplied predicates are true.
111 class ConfigurationAny
: public ConfigurationPredicate
113 std::vector
<std::unique_ptr
<ConfigurationPredicate
>>
114 predicate_list
; // inlined form
118 std::vector
<std::unique_ptr
<ConfigurationPredicate
>> predicate_list
)
119 : predicate_list (predicate_list
)
122 void accept_vis (ASTVisitor
&vis
) override
;
125 /* Use covariance to implement clone function as returning this object rather
127 ConfigurationAny
*clone_configuration_predicate_impl () const override
129 return new ConfigurationAny (*this);
133 /* Predicate that produces the negation of a supplied other configuration
135 class ConfigurationNot
: public ConfigurationPredicate
137 std::unique_ptr
<ConfigurationPredicate
> config_to_negate
;
140 ConfigurationNot (ConfigurationPredicate
*config_to_negate
)
141 : config_to_negate (config_to_negate
)
144 // Copy constructor with clone
145 ConfigurationNot (ConfigurationNot
const &other
)
147 other
.config_to_negate
->clone_configuration_predicate ())
150 // Overloaded assignment operator to clone
151 ConfigurationNot
&operator= (ConfigurationNot
const &other
)
153 config_to_negate
= other
.config_to_negate
->clone_configuration_predicate ();
159 ConfigurationNot (ConfigurationNot
&&other
) = default;
160 ConfigurationNot
&operator= (ConfigurationNot
&&other
) = default;
162 void accept_vis (ASTVisitor
&vis
) override
;
165 /* Use covariance to implement clone function as returning this object rather
167 ConfigurationNot
*clone_configuration_predicate_impl () const override
169 return new ConfigurationNot (*this);
173 // TODO: relationship to other attributes?
176 std::unique_ptr
<ConfigurationPredicate
> config_to_include
;
179 CfgAttribute (ConfigurationPredicate
*config_to_include
)
180 : config_to_include (config_to_include
)
183 // Copy constructor with clone
184 CfgAttribute (CfgAttribute
const &other
)
185 : config_to_include (
186 other
.config_to_include
->clone_configuration_predicate ())
189 // Overloaded assignment operator to clone
190 CfgAttribute
&operator= (CfgAttribute
const &other
)
193 = other
.config_to_include
->clone_configuration_predicate ();
199 CfgAttribute (CfgAttribute
&&other
) = default;
200 CfgAttribute
&operator= (CfgAttribute
&&other
) = default;
202 /* TODO: ok, best thing to do would be eliminating this class, making Attribute
203 * has a "is_cfg()" method, and having attribute path as "cfg" and AttrInput as
204 * ConfigurationPredicate (so make ConfigurationPredicate a subclass of
205 * AttrInput?). Would need special handling in parser, however. */
210 std::vector
<Attribute
> cfg_attrs
;
213 // TODO: relationship to other attributes?
214 class CfgAttrAttribute
216 std::unique_ptr
<ConfigurationPredicate
> config_to_include
;
217 std::vector
<Attribute
> cfg_attrs
;
220 CfgAttrAttribute (ConfigurationPredicate
*config_to_include
,
221 std::vector
<Attribute
> cfg_attrs
)
222 : config_to_include (config_to_include
), cfg_attrs (cfg_attrs
)
225 // Copy constructor with clone
226 CfgAttrAttribute (CfgAttrAttribute
const &other
)
227 : config_to_include (
228 other
.config_to_include
->clone_configuration_predicate ()),
229 cfg_attrs (cfg_attrs
)
232 // Overloaded assignment operator to clone
233 CfgAttrAttribute
&operator= (CfgAttrAttribute
const &other
)
236 = other
.config_to_include
->clone_configuration_predicate ();
237 cfg_attrs
= other
.cfg_attrs
;
243 CfgAttrAttribute (CfgAttrAttribute
&&other
) = default;
244 CfgAttrAttribute
&operator= (CfgAttrAttribute
&&other
) = default;