| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 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 |
| 8 |
// version. |
| 9 |
|
| 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 |
| 13 |
// for more details. |
| 14 |
|
| 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/>. |
| 18 |
|
| 19 |
#ifndef RUST_AST_CONDCOMPILATION |
| 20 |
#define RUST_AST_CONDCOMPILATION |
| 21 |
// Conditional compilation-related AST stuff |
| 22 |
|
| 23 |
#include "rust-ast.h" |
| 24 |
|
| 25 |
namespace Rust { |
| 26 |
namespace AST { |
| 27 |
// Base conditional compilation configuration predicate thing - abstract |
| 28 |
class ConfigurationPredicate |
| 29 |
{ |
| 30 |
public: |
| 31 |
virtual ~ConfigurationPredicate () {} |
| 32 |
|
| 33 |
// Unique pointer custom clone function |
| 34 |
std::unique_ptr<ConfigurationPredicate> clone_configuration_predicate () const |
| 35 |
{ |
| 36 |
return std::unique_ptr<ConfigurationPredicate> ( |
| 37 |
clone_configuration_predicate_impl ()); |
| 38 |
} |
| 39 |
|
| 40 |
// not sure if I'll use this but here anyway |
| 41 |
virtual void accept_vis (ASTVisitor &vis) = 0; |
| 42 |
|
| 43 |
protected: |
| 44 |
// Clone function impl to be overriden in base classes |
| 45 |
virtual ConfigurationPredicate * |
| 46 |
clone_configuration_predicate_impl () const = 0; |
| 47 |
}; |
| 48 |
|
| 49 |
// A configuration option - true if option is set, false if option is not set. |
| 50 |
class ConfigurationOption : public ConfigurationPredicate |
| 51 |
{ |
| 52 |
Identifier option_name; |
| 53 |
|
| 54 |
// bool has_string_literal_option_body; |
| 55 |
std::string option_value; // technically a string or raw string literal |
| 56 |
|
| 57 |
public: |
| 58 |
/* Returns whether the configuration option has a "value" part of the |
| 59 |
* key-value pair. */ |
| 60 |
bool has_option_value () const { return !option_value.empty (); } |
| 61 |
|
| 62 |
// Key-value pair constructor |
| 63 |
ConfigurationOption (Identifier option_name, std::string option_value) |
| 64 |
: option_name (option_name), option_value (option_value) |
| 65 |
{} |
| 66 |
|
| 67 |
// Name-only constructor |
| 68 |
ConfigurationOption (Identifier option_name) : option_name (option_name) {} |
| 69 |
|
| 70 |
void accept_vis (ASTVisitor &vis) override; |
| 71 |
|
| 72 |
protected: |
| 73 |
/* Use covariance to implement clone function as returning this object rather |
| 74 |
* than base */ |
| 75 |
ConfigurationOption *clone_configuration_predicate_impl () const override |
| 76 |
{ |
| 77 |
return new ConfigurationOption (*this); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
// TODO: inline |
| 82 |
struct ConfigurationPredicateList |
| 83 |
{ |
| 84 |
std::vector<std::unique_ptr<ConfigurationPredicate>> predicate_list; |
| 85 |
}; |
| 86 |
|
| 87 |
// Predicate that returns true if all of the supplied predicates return true. |
| 88 |
class ConfigurationAll : public ConfigurationPredicate |
| 89 |
{ |
| 90 |
std::vector<std::unique_ptr<ConfigurationPredicate>> |
| 91 |
predicate_list; // inlined form |
| 92 |
|
| 93 |
public: |
| 94 |
ConfigurationAll ( |
| 95 |
std::vector<std::unique_ptr<ConfigurationPredicate>> predicate_list) |
| 96 |
: predicate_list (predicate_list) |
| 97 |
{} |
| 98 |
|
| 99 |
void accept_vis (ASTVisitor &vis) override; |
| 100 |
|
| 101 |
protected: |
| 102 |
/* Use covariance to implement clone function as returning this object rather |
| 103 |
* than base */ |
| 104 |
ConfigurationAll *clone_configuration_predicate_impl () const override |
| 105 |
{ |
| 106 |
return new ConfigurationAll (*this); |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
// Predicate that returns true if any of the supplied predicates are true. |
| 111 |
class ConfigurationAny : public ConfigurationPredicate |
| 112 |
{ |
| 113 |
std::vector<std::unique_ptr<ConfigurationPredicate>> |
| 114 |
predicate_list; // inlined form |
| 115 |
|
| 116 |
public: |
| 117 |
ConfigurationAny ( |
| 118 |
std::vector<std::unique_ptr<ConfigurationPredicate>> predicate_list) |
| 119 |
: predicate_list (predicate_list) |
| 120 |
{} |
| 121 |
|
| 122 |
void accept_vis (ASTVisitor &vis) override; |
| 123 |
|
| 124 |
protected: |
| 125 |
/* Use covariance to implement clone function as returning this object rather |
| 126 |
* than base */ |
| 127 |
ConfigurationAny *clone_configuration_predicate_impl () const override |
| 128 |
{ |
| 129 |
return new ConfigurationAny (*this); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
/* Predicate that produces the negation of a supplied other configuration |
| 134 |
* predicate. */ |
| 135 |
class ConfigurationNot : public ConfigurationPredicate |
| 136 |
{ |
| 137 |
std::unique_ptr<ConfigurationPredicate> config_to_negate; |
| 138 |
|
| 139 |
public: |
| 140 |
ConfigurationNot (ConfigurationPredicate *config_to_negate) |
| 141 |
: config_to_negate (config_to_negate) |
| 142 |
{} |
| 143 |
|
| 144 |
// Copy constructor with clone |
| 145 |
ConfigurationNot (ConfigurationNot const &other) |
| 146 |
: config_to_negate ( |
| 147 |
other.config_to_negate->clone_configuration_predicate ()) |
| 148 |
{} |
| 149 |
|
| 150 |
// Overloaded assignment operator to clone |
| 151 |
ConfigurationNot &operator= (ConfigurationNot const &other) |
| 152 |
{ |
| 153 |
config_to_negate = other.config_to_negate->clone_configuration_predicate (); |
| 154 |
|
| 155 |
return *this; |
| 156 |
} |
| 157 |
|
| 158 |
// move constructors |
| 159 |
ConfigurationNot (ConfigurationNot &&other) = default; |
| 160 |
ConfigurationNot &operator= (ConfigurationNot &&other) = default; |
| 161 |
|
| 162 |
void accept_vis (ASTVisitor &vis) override; |
| 163 |
|
| 164 |
protected: |
| 165 |
/* Use covariance to implement clone function as returning this object rather |
| 166 |
* than base */ |
| 167 |
ConfigurationNot *clone_configuration_predicate_impl () const override |
| 168 |
{ |
| 169 |
return new ConfigurationNot (*this); |
| 170 |
} |
| 171 |
}; |
| 172 |
|
| 173 |
// TODO: relationship to other attributes? |
| 174 |
class CfgAttribute |
| 175 |
{ |
| 176 |
std::unique_ptr<ConfigurationPredicate> config_to_include; |
| 177 |
|
| 178 |
public: |
| 179 |
CfgAttribute (ConfigurationPredicate *config_to_include) |
| 180 |
: config_to_include (config_to_include) |
| 181 |
{} |
| 182 |
|
| 183 |
// Copy constructor with clone |
| 184 |
CfgAttribute (CfgAttribute const &other) |
| 185 |
: config_to_include ( |
| 186 |
other.config_to_include->clone_configuration_predicate ()) |
| 187 |
{} |
| 188 |
|
| 189 |
// Overloaded assignment operator to clone |
| 190 |
CfgAttribute &operator= (CfgAttribute const &other) |
| 191 |
{ |
| 192 |
config_to_include |
| 193 |
= other.config_to_include->clone_configuration_predicate (); |
| 194 |
|
| 195 |
return *this; |
| 196 |
} |
| 197 |
|
| 198 |
// move constructors |
| 199 |
CfgAttribute (CfgAttribute &&other) = default; |
| 200 |
CfgAttribute &operator= (CfgAttribute &&other) = default; |
| 201 |
}; |
| 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. */ |
| 206 |
|
| 207 |
// TODO: inline |
| 208 |
struct CfgAttrs |
| 209 |
{ |
| 210 |
std::vector<Attribute> cfg_attrs; |
| 211 |
}; |
| 212 |
|
| 213 |
// TODO: relationship to other attributes? |
| 214 |
class CfgAttrAttribute |
| 215 |
{ |
| 216 |
std::unique_ptr<ConfigurationPredicate> config_to_include; |
| 217 |
std::vector<Attribute> cfg_attrs; |
| 218 |
|
| 219 |
public: |
| 220 |
CfgAttrAttribute (ConfigurationPredicate *config_to_include, |
| 221 |
std::vector<Attribute> cfg_attrs) |
| 222 |
: config_to_include (config_to_include), cfg_attrs (cfg_attrs) |
| 223 |
{} |
| 224 |
|
| 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) |
| 230 |
{} |
| 231 |
|
| 232 |
// Overloaded assignment operator to clone |
| 233 |
CfgAttrAttribute &operator= (CfgAttrAttribute const &other) |
| 234 |
{ |
| 235 |
config_to_include |
| 236 |
= other.config_to_include->clone_configuration_predicate (); |
| 237 |
cfg_attrs = other.cfg_attrs; |
| 238 |
|
| 239 |
return *this; |
| 240 |
} |
| 241 |
|
| 242 |
// move constructors |
| 243 |
CfgAttrAttribute (CfgAttrAttribute &&other) = default; |
| 244 |
CfgAttrAttribute &operator= (CfgAttrAttribute &&other) = default; |
| 245 |
}; |
| 246 |
} // namespace AST |
| 247 |
} // namespace Rust |
| 248 |
|
| 249 |
#endif |