Skip to content

Commit

Permalink
Allow #[cfg] to be used with #[constant]
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Oct 7, 2023
1 parent 165afeb commit b0acd87
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
32 changes: 27 additions & 5 deletions godot-macros/src/class/godot_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ struct SignalDefinition {
attributes: Vec<Attribute>,
}

/// Holds information known from a constant's definition
struct ConstantDefinition {
constant: Constant,
attributes: Vec<Attribute>,
}

/// Codegen for `#[godot_api] impl MyType`
fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
let class_name = util::validate_impl(&decl, None, "godot_api")?;
Expand Down Expand Up @@ -129,16 +135,27 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
.map(|func_def| make_method_registration(&class_name, func_def));

let consts = process_godot_constants(&mut decl)?;
let mut integer_constant_cfg_attrs = Vec::new();
let mut integer_constant_names = Vec::new();
let mut integer_constant_values = Vec::new();

for constant in consts.iter() {
for constant_def in consts.iter() {
let ConstantDefinition {
constant,
attributes,
} = constant_def;
if constant.initializer.is_none() {
return bail!(constant, "exported const should have initializer");
};

let name = &constant.name;
let cfg_attrs = util::extract_cfg_attrs(attributes)
.into_iter()
.collect::<Vec<_>>();

// Transport #[cfg] attrs to the FFI glue to ensure constants which were conditionally
// removed from compilation don't cause errors.
integer_constant_cfg_attrs.push(cfg_attrs);
integer_constant_names.push(constant.name.to_string());
integer_constant_values.push(quote! { #class_name::#name });
}
Expand All @@ -150,6 +167,7 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
use ::godot::builtin::StringName;

#(
#(#integer_constant_cfg_attrs)*
ExportConstant::new(
#class_name_obj,
ConstantKind::Integer(
Expand Down Expand Up @@ -307,15 +325,16 @@ fn process_godot_fns(
Ok((func_definitions, signal_definitions))
}

fn process_godot_constants(decl: &mut Impl) -> Result<Vec<Constant>, Error> {
let mut constant_signatures = vec![];
fn process_godot_constants(decl: &mut Impl) -> Result<Vec<ConstantDefinition>, Error> {
let mut constant_definitions = vec![];

for item in decl.body_items.iter_mut() {
let ImplMember::Constant(constant) = item else {
continue;
};

if let Some(attr) = extract_attributes(&constant, &constant.attributes)? {
let attributes = constant.attributes.clone();
// Remaining code no longer has attribute -- rest stays
constant.attributes.remove(attr.index);

Expand All @@ -330,13 +349,16 @@ fn process_godot_constants(decl: &mut Impl) -> Result<Vec<Constant>, Error> {
if constant.initializer.is_none() {
return bail!(constant, "exported constant must have initializer");
}
constant_signatures.push(constant.clone());
constant_definitions.push(ConstantDefinition {
constant: constant.clone(),
attributes,
});
}
}
}
}

Ok(constant_signatures)
Ok(constant_definitions)
}

fn extract_attributes<T>(
Expand Down
19 changes: 19 additions & 0 deletions itest/rust/src/register_tests/constant_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ impl HasConstants {
#[constant]
#[cfg(all())]
const CONSTANT_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_BELOW_CONST_ATTR: bool = true;

#[constant]
const CFG_REMOVES_CONSTANT: bool = true;

#[cfg(any())]
#[constant]
const CFG_REMOVES_CONSTANT: bool = false;

#[constant]
#[cfg(any())]
const CFG_REMOVES_CONSTANT: bool = false;

#[cfg(any())]
#[constant]
const CFG_REMOVES_CONSTANT_FFI_GLUE: bool = true;

#[constant]
#[cfg(any())]
const CFG_REMOVES_CONSTANT_FFI_GLUE: bool = true;
}

#[itest]
Expand Down

0 comments on commit b0acd87

Please sign in to comment.