Skip to content

Commit a31261f

Browse files
committed
Allow lazily getting type for macro variables.
1 parent 0adc42b commit a31261f

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

src/codegen/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,18 @@ impl CodeGenerator for Var {
630630
attrs.push(attributes::doc(comment));
631631
}
632632

633-
let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
633+
let var_ty = if let Some(var_ty) = self.ty(ctx) {
634+
var_ty
635+
} else {
636+
// FIXME: Parse/output macro variables as the last step when all types are known.
637+
warn!(
638+
"Failed to determine type for macro variable {}.",
639+
canonical_name
640+
);
641+
return;
642+
};
643+
644+
let ty = var_ty.to_rust_ty_or_opaque(ctx, &());
634645

635646
if let Some(val) = self.val() {
636647
match *val {
@@ -641,8 +652,7 @@ impl CodeGenerator for Var {
641652
});
642653
}
643654
VarType::Int(val) => {
644-
let int_kind = self
645-
.ty()
655+
let int_kind = var_ty
646656
.into_resolver()
647657
.through_type_aliases()
648658
.through_type_refs()

src/ir/item.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ impl Trace for Item {
297297
tracer.visit(fun.signature().into());
298298
}
299299
ItemKind::Var(ref var) => {
300-
tracer.visit_kind(var.ty().into(), EdgeKind::VarType);
300+
if let Some(ty) = var.ty(ctx) {
301+
tracer.visit_kind(ty.into(), EdgeKind::VarType);
302+
}
301303
}
302304
ItemKind::Module(_) => {
303305
// Module -> children edges are "weak", and we do not want to

src/ir/var.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,39 @@ pub struct Var {
4040
/// The mangled name of the variable.
4141
mangled_name: Option<String>,
4242
/// The type of the variable.
43-
ty: TypeId,
43+
ty: VarTypeId,
4444
/// The value of the variable, that needs to be suitable for `ty`.
4545
val: Option<VarType>,
4646
/// Whether this variable is const.
4747
is_const: bool,
4848
}
4949

50+
#[derive(Debug)]
51+
pub enum VarTypeId {
52+
Concrete(TypeId),
53+
Lazy(String),
54+
}
55+
56+
impl From<TypeId> for VarTypeId {
57+
fn from(type_id: TypeId) -> Self {
58+
Self::Concrete(type_id)
59+
}
60+
}
61+
5062
impl Var {
5163
/// Construct a new `Var`.
5264
pub fn new(
5365
name: String,
5466
mangled_name: Option<String>,
55-
ty: TypeId,
67+
ty: impl Into<VarTypeId>,
5668
val: Option<VarType>,
5769
is_const: bool,
58-
) -> Var {
70+
) -> Self {
5971
assert!(!name.is_empty());
60-
Var {
72+
Self {
6173
name,
6274
mangled_name,
63-
ty,
75+
ty: ty.into(),
6476
val,
6577
is_const,
6678
}
@@ -336,30 +348,18 @@ impl ClangSubItemParser for Var {
336348
["int64_t"] => IntKind::I64,
337349
["uint64_t"] => IntKind::U64,
338350
[custom_type] => {
339-
eprintln!(
340-
"looking for type {:?}",
341-
custom_type
342-
);
343-
344-
// FIXME: Doesn't work since types are parsed after macros.
345-
let type_id = ctx
346-
.wrapper_id_by_name(custom_type)
347-
.or_else(|| {
348-
ctx.wrapper_id_by_name(&format!(
349-
"const {}",
350-
custom_type
351-
))
352-
});
353-
354-
if let Some(type_id) = type_id {
355-
todo!(
356-
"type id for {:?} = {:?}",
357-
custom_type,
358-
type_id
359-
);
360-
}
361-
362-
return Err(ParseError::Continue);
351+
return Ok(ParseResult::New(
352+
Var::new(
353+
name,
354+
None,
355+
VarTypeId::Lazy(
356+
custom_type.to_string(),
357+
),
358+
Some(val),
359+
true,
360+
),
361+
Some(cursor),
362+
));
363363
}
364364
_ => return Err(ParseError::Continue),
365365
};

0 commit comments

Comments
 (0)