Skip to content

Commit b8a90d8

Browse files
authored
Merge pull request #523 from voidc/adt-repr
Add method to get repr data of an ADT to ChalkDatabase
2 parents d7d69a6 + 5caf9ec commit b8a90d8

File tree

9 files changed

+75
-5
lines changed

9 files changed

+75
-5
lines changed

chalk-integration/src/db.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use chalk_ir::{
1212
Substitution, TraitId, Ty, UCanonical,
1313
};
1414
use chalk_solve::rust_ir::{
15-
AdtDatum, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureKind, FnDefDatum,
16-
FnDefInputsAndOutputDatum, ImplDatum, OpaqueTyDatum, TraitDatum, WellKnownTrait,
15+
AdtDatum, AdtRepr, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureKind,
16+
FnDefDatum, FnDefInputsAndOutputDatum, ImplDatum, OpaqueTyDatum, TraitDatum, WellKnownTrait,
1717
};
1818
use chalk_solve::{RustIrDatabase, Solution, SolverChoice, SubstitutionResult};
1919
use salsa::Database;
@@ -109,6 +109,10 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
109109
self.program_ir().unwrap().adt_datum(id)
110110
}
111111

112+
fn adt_repr(&self, id: AdtId<ChalkIr>) -> AdtRepr {
113+
self.program_ir().unwrap().adt_repr(id)
114+
}
115+
112116
fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
113117
self.program_ir().unwrap().fn_def_datum(id)
114118
}

chalk-integration/src/lowering.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ impl LowerProgram for Program {
412412
}
413413

414414
let mut adt_data = BTreeMap::new();
415+
let mut adt_reprs = BTreeMap::new();
415416
let mut fn_def_data = BTreeMap::new();
416417
let mut closure_inputs_and_output = BTreeMap::new();
417418
let mut closure_closure_kind = BTreeMap::new();
@@ -446,6 +447,7 @@ impl LowerProgram for Program {
446447
Item::StructDefn(ref d) => {
447448
let adt_id = AdtId(raw_id);
448449
adt_data.insert(adt_id, Arc::new(d.lower_adt(adt_id, &empty_env)?));
450+
adt_reprs.insert(adt_id, d.lower_adt_repr()?);
449451
}
450452
Item::FnDefn(ref defn) => {
451453
let fn_def_id = FnDefId(raw_id);
@@ -639,6 +641,7 @@ impl LowerProgram for Program {
639641
fn_def_kinds,
640642
trait_kinds,
641643
adt_data,
644+
adt_reprs,
642645
fn_def_data,
643646
closure_inputs_and_output,
644647
closure_closure_kind,
@@ -1121,6 +1124,19 @@ impl LowerAdtDefn for StructDefn {
11211124
}
11221125
}
11231126

1127+
trait LowerAdtRepr {
1128+
fn lower_adt_repr(&self) -> LowerResult<rust_ir::AdtRepr>;
1129+
}
1130+
1131+
impl LowerAdtRepr for StructDefn {
1132+
fn lower_adt_repr(&self) -> LowerResult<rust_ir::AdtRepr> {
1133+
Ok(rust_ir::AdtRepr {
1134+
repr_c: self.repr.repr_c,
1135+
repr_packed: self.repr.repr_packed,
1136+
})
1137+
}
1138+
}
1139+
11241140
trait LowerFnDefn {
11251141
fn lower_fn_def(
11261142
&self,

chalk-integration/src/program.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use chalk_ir::{
88
ProgramClauseImplication, ProgramClauses, ProjectionTy, Substitution, TraitId, Ty,
99
};
1010
use chalk_solve::rust_ir::{
11-
AdtDatum, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureKind, FnDefDatum,
12-
FnDefInputsAndOutputDatum, ImplDatum, ImplType, OpaqueTyDatum, TraitDatum, WellKnownTrait,
11+
AdtDatum, AdtRepr, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureKind,
12+
FnDefDatum, FnDefInputsAndOutputDatum, ImplDatum, ImplType, OpaqueTyDatum, TraitDatum,
13+
WellKnownTrait,
1314
};
1415
use chalk_solve::split::Split;
1516
use chalk_solve::RustIrDatabase;
@@ -44,6 +45,8 @@ pub struct Program {
4445
/// For each ADT:
4546
pub adt_data: BTreeMap<AdtId<ChalkIr>, Arc<AdtDatum<ChalkIr>>>,
4647

48+
pub adt_reprs: BTreeMap<AdtId<ChalkIr>, AdtRepr>,
49+
4750
pub fn_def_data: BTreeMap<FnDefId<ChalkIr>, Arc<FnDefDatum<ChalkIr>>>,
4851

4952
pub closure_inputs_and_output:
@@ -359,6 +362,10 @@ impl RustIrDatabase<ChalkIr> for Program {
359362
self.adt_data[&id].clone()
360363
}
361364

365+
fn adt_repr(&self, id: AdtId<ChalkIr>) -> AdtRepr {
366+
self.adt_reprs[&id]
367+
}
368+
362369
fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
363370
self.fn_def_data[&id].clone()
364371
}

chalk-parse/src/ast.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct StructDefn {
3636
pub where_clauses: Vec<QuantifiedWhereClause>,
3737
pub fields: Vec<Field>,
3838
pub flags: StructFlags,
39+
pub repr: StructRepr,
3940
}
4041

4142
#[derive(Clone, PartialEq, Eq, Debug)]
@@ -45,6 +46,12 @@ pub struct StructFlags {
4546
pub phantom_data: bool,
4647
}
4748

49+
#[derive(Clone, PartialEq, Eq, Debug)]
50+
pub struct StructRepr {
51+
pub repr_c: bool,
52+
pub repr_packed: bool,
53+
}
54+
4855
#[derive(Clone, PartialEq, Eq, Debug)]
4956
pub struct FnDefn {
5057
pub name: Identifier,

chalk-parse/src/parser.lalrpop

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ WellKnownTrait: WellKnownTrait = {
5959
"#" "[" "lang" "(" "unsize" ")" "]" => WellKnownTrait::Unsize,
6060
};
6161

62+
StructRepr: Atom = "#" "[" "repr" "(" <name:Id> ")" "]" => name.str;
63+
6264
StructDefn: StructDefn = {
63-
<upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?>
65+
<upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?> <repr:StructRepr*>
6466
"struct" <n:Id><p:Angle<VariableKind>>
6567
<w:QuantifiedWhereClauses> "{" <f:Fields> "}" => StructDefn
6668
{
@@ -73,6 +75,10 @@ StructDefn: StructDefn = {
7375
fundamental: fundamental.is_some(),
7476
phantom_data: phantom_data.is_some(),
7577
},
78+
repr: StructRepr {
79+
repr_c: repr.iter().any(|s| s == "C"),
80+
repr_packed: repr.iter().any(|s| s == "packed"),
81+
},
7682
}
7783
};
7884

chalk-solve/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub trait RustIrDatabase<I: Interner>: Debug {
3939
/// Returns the datum for the impl with the given id.
4040
fn adt_datum(&self, adt_id: AdtId<I>) -> Arc<AdtDatum<I>>;
4141

42+
/// Returns the representation for the ADT definition with the given id.
43+
fn adt_repr(&self, id: AdtId<I>) -> AdtRepr;
44+
4245
fn fn_def_datum(&self, fn_def_id: FnDefId<I>) -> Arc<FnDefDatum<I>>;
4346

4447
/// Returns the datum for the impl with the given id.

chalk-solve/src/rust_ir.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ pub struct AdtFlags {
104104
pub phantom_data: bool,
105105
}
106106

107+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
108+
pub struct AdtRepr {
109+
pub repr_c: bool,
110+
pub repr_packed: bool,
111+
}
112+
107113
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
108114
/// A rust intermediate represention (rust_ir) of a function definition/declaration.
109115
/// For example, in the following rust code:

tests/integration/panic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ impl RustIrDatabase<ChalkIr> for MockDatabase {
129129
unimplemented!()
130130
}
131131

132+
fn adt_repr(&self, id: AdtId<ChalkIr>) -> AdtRepr {
133+
unimplemented!()
134+
}
135+
132136
fn fn_def_datum(&self, fn_def_id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
133137
unimplemented!()
134138
}

tests/lowering/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,20 @@ fn closures() {
737737
}
738738
}
739739
}
740+
741+
#[test]
742+
fn struct_repr() {
743+
lowering_success! {
744+
program {
745+
#[repr(C)]
746+
struct Foo {}
747+
748+
#[repr(packed)]
749+
struct Bar {}
750+
751+
#[repr(C)]
752+
#[repr(packed)]
753+
struct FooBar {}
754+
}
755+
}
756+
}

0 commit comments

Comments
 (0)