Skip to content

Commit 770f352

Browse files
committed
constant: Add support for other expressions WebRender uses.
1 parent 882af0b commit 770f352

11 files changed

+292
-16
lines changed

src/bindgen/ir/constant.rs

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,56 @@ fn member_to_ident(member: &syn::Member) -> String {
2727
}
2828
}
2929

30+
// TODO: Maybe add support to more std associated constants.
31+
fn to_known_assoc_constant(associated_to: &Path, name: &str) -> Option<String> {
32+
use crate::bindgen::ir::{IntKind, PrimitiveType};
33+
34+
if name != "MAX" && name != "MIN" {
35+
return None;
36+
}
37+
38+
let prim = PrimitiveType::maybe(associated_to.name())?;
39+
let prefix = match prim {
40+
PrimitiveType::Integer {
41+
kind,
42+
signed,
43+
zeroable: _,
44+
} => match kind {
45+
IntKind::B8 => {
46+
if signed {
47+
"INT8"
48+
} else {
49+
"UINT8"
50+
}
51+
}
52+
IntKind::B16 => {
53+
if signed {
54+
"INT16"
55+
} else {
56+
"UINT16"
57+
}
58+
}
59+
IntKind::B32 => {
60+
if signed {
61+
"INT32"
62+
} else {
63+
"UINT32"
64+
}
65+
}
66+
IntKind::B64 => {
67+
if signed {
68+
"INT64"
69+
} else {
70+
"UINT64"
71+
}
72+
}
73+
_ => return None,
74+
},
75+
_ => return None,
76+
};
77+
Some(format!("{}_{}", prefix, name))
78+
}
79+
3080
#[derive(Debug, Clone)]
3181
pub enum Literal {
3282
Expr(String),
@@ -112,10 +162,12 @@ impl Literal {
112162
match *self {
113163
Literal::Expr(..) => true,
114164
Literal::Path {
115-
ref associated_to, ..
165+
ref associated_to,
166+
ref name,
116167
} => {
117168
if let Some((ref path, _export_name)) = associated_to {
118-
return bindings.struct_exists(path);
169+
return bindings.struct_exists(path)
170+
|| to_known_assoc_constant(path, name).is_some();
119171
}
120172
true
121173
}
@@ -274,6 +326,30 @@ impl Literal {
274326
field: member_to_ident(member),
275327
}),
276328

329+
syn::Expr::Call(syn::ExprCall {
330+
ref func, ref args, ..
331+
}) => {
332+
let struct_name = match Literal::load(func)? {
333+
Literal::Path {
334+
associated_to: None,
335+
name,
336+
} => name,
337+
_ => return Err(format!("Unsupported call expression. {:?}", *expr)),
338+
};
339+
let mut fields = HashMap::<String, Literal>::default();
340+
for (index, arg) in args.iter().enumerate() {
341+
let ident =
342+
member_to_ident(&syn::Member::Unnamed(syn::Index::from(index))).to_string();
343+
let value = Literal::load(arg)?;
344+
fields.insert(ident, value);
345+
}
346+
Ok(Literal::Struct {
347+
path: Path::new(struct_name.clone()),
348+
export_name: struct_name,
349+
fields,
350+
})
351+
}
352+
277353
syn::Expr::Struct(syn::ExprStruct {
278354
ref path,
279355
ref fields,
@@ -282,10 +358,9 @@ impl Literal {
282358
let struct_name = path.segments[0].ident.unraw().to_string();
283359
let mut field_map = HashMap::<String, Literal>::default();
284360
for field in fields {
285-
let ident = member_to_ident(&field.member);
286-
let key = ident.to_string();
361+
let ident = member_to_ident(&field.member).to_string();
287362
let value = Literal::load(&field.expr)?;
288-
field_map.insert(key, value);
363+
field_map.insert(ident, value);
289364
}
290365
Ok(Literal::Struct {
291366
path: Path::new(struct_name.clone()),
@@ -357,7 +432,10 @@ impl Literal {
357432
ref associated_to,
358433
ref name,
359434
} => {
360-
if let Some((_, ref export_name)) = associated_to {
435+
if let Some((ref path, ref export_name)) = associated_to {
436+
if let Some(known) = to_known_assoc_constant(path, name) {
437+
return write!(out, "{}", known);
438+
}
361439
let path_separator = match config.language {
362440
Language::Cython | Language::C => "_",
363441
Language::Cxx => {

tests/expectations/associated_in_body.both.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,25 @@ typedef struct StyleAlignFlags {
3535
#define StyleAlignFlags_MIXED (StyleAlignFlags){ .bits = (uint8_t)(((1 << 4) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3636
#define StyleAlignFlags_MIXED_SELF (StyleAlignFlags){ .bits = (uint8_t)(((1 << 5) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3737

38-
void root(struct StyleAlignFlags flags);
38+
/**
39+
* An arbitrary identifier for a native (OS compositor) surface
40+
*/
41+
typedef struct StyleNativeSurfaceId {
42+
uint64_t _0;
43+
} StyleNativeSurfaceId;
44+
/**
45+
* A special id for the native surface that is used for debug / profiler overlays.
46+
*/
47+
#define StyleNativeSurfaceId_DEBUG_OVERLAY (StyleNativeSurfaceId){ ._0 = UINT64_MAX }
48+
49+
typedef struct StyleNativeTileId {
50+
struct StyleNativeSurfaceId surface_id;
51+
int32_t x;
52+
int32_t y;
53+
} StyleNativeTileId;
54+
/**
55+
* A special id for the native surface that is used for debug / profiler overlays.
56+
*/
57+
#define StyleNativeTileId_DEBUG_OVERLAY (StyleNativeTileId){ .surface_id = StyleNativeSurfaceId_DEBUG_OVERLAY, .x = 0, .y = 0 }
58+
59+
void root(struct StyleAlignFlags flags, struct StyleNativeTileId tile);

tests/expectations/associated_in_body.both.compat.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,32 @@ typedef struct StyleAlignFlags {
3535
#define StyleAlignFlags_MIXED (StyleAlignFlags){ .bits = (uint8_t)(((1 << 4) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3636
#define StyleAlignFlags_MIXED_SELF (StyleAlignFlags){ .bits = (uint8_t)(((1 << 5) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3737

38+
/**
39+
* An arbitrary identifier for a native (OS compositor) surface
40+
*/
41+
typedef struct StyleNativeSurfaceId {
42+
uint64_t _0;
43+
} StyleNativeSurfaceId;
44+
/**
45+
* A special id for the native surface that is used for debug / profiler overlays.
46+
*/
47+
#define StyleNativeSurfaceId_DEBUG_OVERLAY (StyleNativeSurfaceId){ ._0 = UINT64_MAX }
48+
49+
typedef struct StyleNativeTileId {
50+
struct StyleNativeSurfaceId surface_id;
51+
int32_t x;
52+
int32_t y;
53+
} StyleNativeTileId;
54+
/**
55+
* A special id for the native surface that is used for debug / profiler overlays.
56+
*/
57+
#define StyleNativeTileId_DEBUG_OVERLAY (StyleNativeTileId){ .surface_id = StyleNativeSurfaceId_DEBUG_OVERLAY, .x = 0, .y = 0 }
58+
3859
#ifdef __cplusplus
3960
extern "C" {
4061
#endif // __cplusplus
4162

42-
void root(struct StyleAlignFlags flags);
63+
void root(struct StyleAlignFlags flags, struct StyleNativeTileId tile);
4364

4465
#ifdef __cplusplus
4566
} // extern "C"

tests/expectations/associated_in_body.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,25 @@ typedef struct {
3535
#define StyleAlignFlags_MIXED (StyleAlignFlags){ .bits = (uint8_t)(((1 << 4) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3636
#define StyleAlignFlags_MIXED_SELF (StyleAlignFlags){ .bits = (uint8_t)(((1 << 5) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3737

38-
void root(StyleAlignFlags flags);
38+
/**
39+
* An arbitrary identifier for a native (OS compositor) surface
40+
*/
41+
typedef struct {
42+
uint64_t _0;
43+
} StyleNativeSurfaceId;
44+
/**
45+
* A special id for the native surface that is used for debug / profiler overlays.
46+
*/
47+
#define StyleNativeSurfaceId_DEBUG_OVERLAY (StyleNativeSurfaceId){ ._0 = UINT64_MAX }
48+
49+
typedef struct {
50+
StyleNativeSurfaceId surface_id;
51+
int32_t x;
52+
int32_t y;
53+
} StyleNativeTileId;
54+
/**
55+
* A special id for the native surface that is used for debug / profiler overlays.
56+
*/
57+
#define StyleNativeTileId_DEBUG_OVERLAY (StyleNativeTileId){ .surface_id = StyleNativeSurfaceId_DEBUG_OVERLAY, .x = 0, .y = 0 }
58+
59+
void root(StyleAlignFlags flags, StyleNativeTileId tile);

tests/expectations/associated_in_body.compat.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,32 @@ typedef struct {
3535
#define StyleAlignFlags_MIXED (StyleAlignFlags){ .bits = (uint8_t)(((1 << 4) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3636
#define StyleAlignFlags_MIXED_SELF (StyleAlignFlags){ .bits = (uint8_t)(((1 << 5) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3737

38+
/**
39+
* An arbitrary identifier for a native (OS compositor) surface
40+
*/
41+
typedef struct {
42+
uint64_t _0;
43+
} StyleNativeSurfaceId;
44+
/**
45+
* A special id for the native surface that is used for debug / profiler overlays.
46+
*/
47+
#define StyleNativeSurfaceId_DEBUG_OVERLAY (StyleNativeSurfaceId){ ._0 = UINT64_MAX }
48+
49+
typedef struct {
50+
StyleNativeSurfaceId surface_id;
51+
int32_t x;
52+
int32_t y;
53+
} StyleNativeTileId;
54+
/**
55+
* A special id for the native surface that is used for debug / profiler overlays.
56+
*/
57+
#define StyleNativeTileId_DEBUG_OVERLAY (StyleNativeTileId){ .surface_id = StyleNativeSurfaceId_DEBUG_OVERLAY, .x = 0, .y = 0 }
58+
3859
#ifdef __cplusplus
3960
extern "C" {
4061
#endif // __cplusplus
4162

42-
void root(StyleAlignFlags flags);
63+
void root(StyleAlignFlags flags, StyleNativeTileId tile);
4364

4465
#ifdef __cplusplus
4566
} // extern "C"

tests/expectations/associated_in_body.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,25 @@ inline const StyleAlignFlags StyleAlignFlags::FLEX_START = StyleAlignFlags{ /* .
6060
inline const StyleAlignFlags StyleAlignFlags::MIXED = StyleAlignFlags{ /* .bits = */ (uint8_t)(((1 << 4) | (StyleAlignFlags::FLEX_START).bits) | (StyleAlignFlags::END).bits) };
6161
inline const StyleAlignFlags StyleAlignFlags::MIXED_SELF = StyleAlignFlags{ /* .bits = */ (uint8_t)(((1 << 5) | (StyleAlignFlags::FLEX_START).bits) | (StyleAlignFlags::END).bits) };
6262

63+
/// An arbitrary identifier for a native (OS compositor) surface
64+
struct StyleNativeSurfaceId {
65+
uint64_t _0;
66+
static const StyleNativeSurfaceId DEBUG_OVERLAY;
67+
};
68+
/// A special id for the native surface that is used for debug / profiler overlays.
69+
inline const StyleNativeSurfaceId StyleNativeSurfaceId::DEBUG_OVERLAY = StyleNativeSurfaceId{ /* ._0 = */ UINT64_MAX };
70+
71+
struct StyleNativeTileId {
72+
StyleNativeSurfaceId surface_id;
73+
int32_t x;
74+
int32_t y;
75+
static const StyleNativeTileId DEBUG_OVERLAY;
76+
};
77+
/// A special id for the native surface that is used for debug / profiler overlays.
78+
inline const StyleNativeTileId StyleNativeTileId::DEBUG_OVERLAY = StyleNativeTileId{ /* .surface_id = */ StyleNativeSurfaceId::DEBUG_OVERLAY, /* .x = */ 0, /* .y = */ 0 };
79+
6380
extern "C" {
6481

65-
void root(StyleAlignFlags flags);
82+
void root(StyleAlignFlags flags, StyleNativeTileId tile);
6683

6784
} // extern "C"

tests/expectations/associated_in_body.pyx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,17 @@ cdef extern from *:
2525
const StyleAlignFlags StyleAlignFlags_MIXED # = <StyleAlignFlags>{ <uint8_t>(((1 << 4) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
2626
const StyleAlignFlags StyleAlignFlags_MIXED_SELF # = <StyleAlignFlags>{ <uint8_t>(((1 << 5) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
2727

28-
void root(StyleAlignFlags flags);
28+
# An arbitrary identifier for a native (OS compositor) surface
29+
ctypedef struct StyleNativeSurfaceId:
30+
uint64_t _0;
31+
# A special id for the native surface that is used for debug / profiler overlays.
32+
const StyleNativeSurfaceId StyleNativeSurfaceId_DEBUG_OVERLAY # = <StyleNativeSurfaceId>{ UINT64_MAX }
33+
34+
ctypedef struct StyleNativeTileId:
35+
StyleNativeSurfaceId surface_id;
36+
int32_t x;
37+
int32_t y;
38+
# A special id for the native surface that is used for debug / profiler overlays.
39+
const StyleNativeTileId StyleNativeTileId_DEBUG_OVERLAY # = <StyleNativeTileId>{ StyleNativeSurfaceId_DEBUG_OVERLAY, 0, 0 }
40+
41+
void root(StyleAlignFlags flags, StyleNativeTileId tile);

tests/expectations/associated_in_body.tag.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,25 @@ struct StyleAlignFlags {
3535
#define StyleAlignFlags_MIXED (StyleAlignFlags){ .bits = (uint8_t)(((1 << 4) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3636
#define StyleAlignFlags_MIXED_SELF (StyleAlignFlags){ .bits = (uint8_t)(((1 << 5) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3737

38-
void root(struct StyleAlignFlags flags);
38+
/**
39+
* An arbitrary identifier for a native (OS compositor) surface
40+
*/
41+
struct StyleNativeSurfaceId {
42+
uint64_t _0;
43+
};
44+
/**
45+
* A special id for the native surface that is used for debug / profiler overlays.
46+
*/
47+
#define StyleNativeSurfaceId_DEBUG_OVERLAY (StyleNativeSurfaceId){ ._0 = UINT64_MAX }
48+
49+
struct StyleNativeTileId {
50+
struct StyleNativeSurfaceId surface_id;
51+
int32_t x;
52+
int32_t y;
53+
};
54+
/**
55+
* A special id for the native surface that is used for debug / profiler overlays.
56+
*/
57+
#define StyleNativeTileId_DEBUG_OVERLAY (StyleNativeTileId){ .surface_id = StyleNativeSurfaceId_DEBUG_OVERLAY, .x = 0, .y = 0 }
58+
59+
void root(struct StyleAlignFlags flags, struct StyleNativeTileId tile);

tests/expectations/associated_in_body.tag.compat.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,32 @@ struct StyleAlignFlags {
3535
#define StyleAlignFlags_MIXED (StyleAlignFlags){ .bits = (uint8_t)(((1 << 4) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3636
#define StyleAlignFlags_MIXED_SELF (StyleAlignFlags){ .bits = (uint8_t)(((1 << 5) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
3737

38+
/**
39+
* An arbitrary identifier for a native (OS compositor) surface
40+
*/
41+
struct StyleNativeSurfaceId {
42+
uint64_t _0;
43+
};
44+
/**
45+
* A special id for the native surface that is used for debug / profiler overlays.
46+
*/
47+
#define StyleNativeSurfaceId_DEBUG_OVERLAY (StyleNativeSurfaceId){ ._0 = UINT64_MAX }
48+
49+
struct StyleNativeTileId {
50+
struct StyleNativeSurfaceId surface_id;
51+
int32_t x;
52+
int32_t y;
53+
};
54+
/**
55+
* A special id for the native surface that is used for debug / profiler overlays.
56+
*/
57+
#define StyleNativeTileId_DEBUG_OVERLAY (StyleNativeTileId){ .surface_id = StyleNativeSurfaceId_DEBUG_OVERLAY, .x = 0, .y = 0 }
58+
3859
#ifdef __cplusplus
3960
extern "C" {
4061
#endif // __cplusplus
4162

42-
void root(struct StyleAlignFlags flags);
63+
void root(struct StyleAlignFlags flags, struct StyleNativeTileId tile);
4364

4465
#ifdef __cplusplus
4566
} // extern "C"

tests/expectations/associated_in_body.tag.pyx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,17 @@ cdef extern from *:
2525
const StyleAlignFlags StyleAlignFlags_MIXED # = <StyleAlignFlags>{ <uint8_t>(((1 << 4) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
2626
const StyleAlignFlags StyleAlignFlags_MIXED_SELF # = <StyleAlignFlags>{ <uint8_t>(((1 << 5) | (StyleAlignFlags_FLEX_START).bits) | (StyleAlignFlags_END).bits) }
2727

28-
void root(StyleAlignFlags flags);
28+
# An arbitrary identifier for a native (OS compositor) surface
29+
cdef struct StyleNativeSurfaceId:
30+
uint64_t _0;
31+
# A special id for the native surface that is used for debug / profiler overlays.
32+
const StyleNativeSurfaceId StyleNativeSurfaceId_DEBUG_OVERLAY # = <StyleNativeSurfaceId>{ UINT64_MAX }
33+
34+
cdef struct StyleNativeTileId:
35+
StyleNativeSurfaceId surface_id;
36+
int32_t x;
37+
int32_t y;
38+
# A special id for the native surface that is used for debug / profiler overlays.
39+
const StyleNativeTileId StyleNativeTileId_DEBUG_OVERLAY # = <StyleNativeTileId>{ StyleNativeSurfaceId_DEBUG_OVERLAY, 0, 0 }
40+
41+
void root(StyleAlignFlags flags, StyleNativeTileId tile);

0 commit comments

Comments
 (0)