Rename Record::decl_id to id, Func::decl_id to record_decl_id.
The intention is to meke it clearer which items have a decl_id (record), and which items reference other items (func, mapped types).
While doing this I realized Func had a decl_id similarly to record, but I believe the intention was for methods to be able to reference their owning record (at least I think Devin is planning to rely on that AFAIK). So I fixed that. It is possible that in the future we will add Func::id field as well.
PiperOrigin-RevId: 413751180
diff --git a/rs_bindings_from_cc/ast_visitor.cc b/rs_bindings_from_cc/ast_visitor.cc
index 5fa5dc1..14171d5 100644
--- a/rs_bindings_from_cc/ast_visitor.cc
+++ b/rs_bindings_from_cc/ast_visitor.cc
@@ -42,7 +42,7 @@
constexpr std::string_view kTypeStatusPayloadUrl =
"type.googleapis.com/devtools.rust.cc_interop.rs_binding_from_cc.type";
-DeclId GenerateDeclId(clang::Decl* decl) {
+static DeclId GenerateDeclId(const clang::Decl* decl) {
return DeclId(reinterpret_cast<uintptr_t>(decl->getCanonicalDecl()));
}
@@ -236,12 +236,18 @@
}
}
+ std::optional<DeclId> record_decl_id;
+ if (const auto* method_decl =
+ llvm::dyn_cast<clang::CXXMethodDecl>(function_decl)) {
+ record_decl_id = GenerateDeclId(method_decl->getParent());
+ }
+
std::optional<UnqualifiedIdentifier> translated_name =
GetTranslatedName(function_decl);
if (success && translated_name.has_value()) {
ir_.items.push_back(Func{
.name = *translated_name,
- .decl_id = GenerateDeclId(function_decl),
+ .record_decl_id = record_decl_id,
.owning_target = GetOwningTarget(function_decl),
.doc_comment = GetComment(function_decl),
.mangled_name = GetMangledName(function_decl),
@@ -303,7 +309,7 @@
const clang::ASTRecordLayout& layout = ctx_->getASTRecordLayout(record_decl);
ir_.items.push_back(
Record{.identifier = *record_name,
- .decl_id = GenerateDeclId(record_decl),
+ .id = GenerateDeclId(record_decl),
.owning_target = GetOwningTarget(record_decl),
.doc_comment = GetComment(record_decl),
.fields = *std::move(fields),
diff --git a/rs_bindings_from_cc/ast_visitor_test.cc b/rs_bindings_from_cc/ast_visitor_test.cc
index bbf31fc..86361b4 100644
--- a/rs_bindings_from_cc/ast_visitor_test.cc
+++ b/rs_bindings_from_cc/ast_visitor_test.cc
@@ -29,7 +29,6 @@
using ::testing::IsEmpty;
using ::testing::Not;
using ::testing::Pointee;
-using ::testing::Property;
using ::testing::SizeIs;
using ::testing::VariantWith;
using ::testing::status::StatusIs;
@@ -43,7 +42,7 @@
std::optional<DeclId> DeclIdForRecord(const IR& ir, absl::string_view ident) {
for (const Record* record : ir.get_items_if<Record>()) {
if (record->identifier.Ident() == ident) {
- return record->decl_id;
+ return record->id;
}
}
return std::nullopt;
diff --git a/rs_bindings_from_cc/ir.cc b/rs_bindings_from_cc/ir.cc
index f1a57dc..91388fa 100644
--- a/rs_bindings_from_cc/ir.cc
+++ b/rs_bindings_from_cc/ir.cc
@@ -137,7 +137,9 @@
} else {
func["name"][SpecialNameToString(std::get<SpecialName>(name))] = nullptr;
}
- func["decl_id"] = decl_id.value();
+ if (record_decl_id.has_value()) {
+ func["record_decl_id"] = record_decl_id->value();
+ }
func["owning_target"] = owning_target.value();
if (doc_comment) {
func["doc_comment"] = *doc_comment;
@@ -212,7 +214,7 @@
nlohmann::json Record::ToJson() const {
nlohmann::json record;
record["identifier"] = identifier.ToJson();
- record["decl_id"] = decl_id.value();
+ record["id"] = id.value();
record["owning_target"] = owning_target.value();
if (doc_comment) {
record["doc_comment"] = *doc_comment;
diff --git a/rs_bindings_from_cc/ir.h b/rs_bindings_from_cc/ir.h
index a21367d..4bcf7f7 100644
--- a/rs_bindings_from_cc/ir.h
+++ b/rs_bindings_from_cc/ir.h
@@ -260,7 +260,9 @@
nlohmann::json ToJson() const;
UnqualifiedIdentifier name;
- DeclId decl_id;
+ // `DeclId` of the Record this function (method) belongs to, nullopt for
+ // free-standing functions.
+ std::optional<DeclId> record_decl_id = std::nullopt;
Label owning_target;
std::optional<std::string> doc_comment;
std::string mangled_name;
@@ -341,7 +343,7 @@
nlohmann::json ToJson() const;
Identifier identifier;
- DeclId decl_id;
+ DeclId id;
Label owning_target;
std::optional<std::string> doc_comment;
std::vector<Field> fields;
diff --git a/rs_bindings_from_cc/ir.rs b/rs_bindings_from_cc/ir.rs
index 1ca7131..55916bd 100644
--- a/rs_bindings_from_cc/ir.rs
+++ b/rs_bindings_from_cc/ir.rs
@@ -39,8 +39,8 @@
fn make_ir(flat_ir: FlatIR) -> Result<IR> {
let mut used_decl_ids = HashMap::new();
for item in &flat_ir.items {
- if let Some(decl_id) = item.decl_id() {
- if let Some(existing_decl) = used_decl_ids.insert(decl_id, item) {
+ if let Some(Record { id, .. }) = item.as_record() {
+ if let Some(existing_decl) = used_decl_ids.insert(id, item) {
bail!("Duplicate decl_id found in {:?} and {:?}", existing_decl, item);
}
}
@@ -49,7 +49,7 @@
.items
.iter()
.enumerate()
- .filter_map(|(idx, item)| item.decl_id().map(|decl_id| (decl_id, idx)))
+ .filter_map(|(idx, item)| item.as_record().map(|record| (record.id, idx)))
.collect::<HashMap<_, _>>();
Ok(IR { flat_ir, decl_id_to_item_idx })
}
@@ -74,18 +74,18 @@
pub decl_id: Option<DeclId>,
}
-pub trait OwningDeclId {
- fn owning_decl_id(&self) -> Option<DeclId>;
+pub trait TypeWithDeclId {
+ fn decl_id(&self) -> Option<DeclId>;
}
-impl OwningDeclId for RsType {
- fn owning_decl_id(&self) -> Option<DeclId> {
+impl TypeWithDeclId for RsType {
+ fn decl_id(&self) -> Option<DeclId> {
self.decl_id
}
}
-impl OwningDeclId for CcType {
- fn owning_decl_id(&self) -> Option<DeclId> {
+impl TypeWithDeclId for CcType {
+ fn decl_id(&self) -> Option<DeclId> {
self.decl_id
}
}
@@ -161,7 +161,7 @@
#[derive(Debug, PartialEq, Eq, Hash, Clone, Deserialize)]
pub struct Func {
pub name: UnqualifiedIdentifier,
- pub decl_id: DeclId,
+ pub record_decl_id: Option<DeclId>,
pub owning_target: Label,
pub mangled_name: String,
pub doc_comment: Option<String>,
@@ -205,7 +205,7 @@
#[derive(Debug, PartialEq, Eq, Hash, Clone, Deserialize)]
pub struct Record {
pub identifier: Identifier,
- pub decl_id: DeclId,
+ pub id: DeclId,
pub owning_target: Label,
pub doc_comment: Option<String>,
pub fields: Vec<Field>,
@@ -245,11 +245,9 @@
}
impl Item {
- fn decl_id(&self) -> Option<DeclId> {
+ fn as_record(&self) -> Option<&Record> {
match self {
- Item::Record(Record { decl_id, .. }) | Item::Func(Func { decl_id, .. }) => {
- Some(*decl_id)
- }
+ Item::Record(record) => Some(record),
_ => None,
}
}
@@ -339,9 +337,9 @@
pub fn record_for_type<T>(&self, ty: &T) -> Result<&Record>
where
- T: OwningDeclId + std::fmt::Debug,
+ T: TypeWithDeclId + std::fmt::Debug,
{
- if let Some(decl_id) = ty.owning_decl_id() {
+ if let Some(decl_id) = ty.decl_id() {
let idx = *self
.decl_id_to_item_idx
.get(&decl_id)
@@ -390,7 +388,7 @@
"items": [
{ "Record" : {
"identifier": {"identifier": "SomeStruct" },
- "decl_id": 42,
+ "id": 42,
"owning_target": "//foo:bar",
"fields": [
{
@@ -446,7 +444,7 @@
current_target: "//foo:bar".into(),
items: vec![Item::Record(Record {
identifier: Identifier { identifier: "SomeStruct".to_string() },
- decl_id: DeclId(42),
+ id: DeclId(42),
owning_target: "//foo:bar".into(),
doc_comment: None,
fields: vec![
@@ -536,7 +534,7 @@
"items": [
{ "Record": {
"identifier": {"identifier": "SomeStruct" },
- "decl_id": 42,
+ "id": 42,
"owning_target": "//foo:bar",
"fields": [
{
@@ -583,7 +581,7 @@
current_target: "//foo:bar".into(),
items: vec![Item::Record(Record {
identifier: Identifier { identifier: "SomeStruct".to_string() },
- decl_id: DeclId(42),
+ id: DeclId(42),
owning_target: "//foo:bar".into(),
doc_comment: None,
fields: vec![Field {
diff --git a/rs_bindings_from_cc/ir_from_cc_test.rs b/rs_bindings_from_cc/ir_from_cc_test.rs
index cd8efc0..54036ce 100644
--- a/rs_bindings_from_cc/ir_from_cc_test.rs
+++ b/rs_bindings_from_cc/ir_from_cc_test.rs
@@ -21,10 +21,10 @@
// TODO(hlopko): Handle MappedTypes as well.
match (expected_item, actual_item) {
(Item::Func(ref mut expected_func), Item::Func(actual_func)) => {
- expected_func.decl_id = actual_func.decl_id;
+ expected_func.record_decl_id = actual_func.record_decl_id;
}
(Item::Record(ref mut expected_record), Item::Record(actual_record)) => {
- expected_record.decl_id = actual_record.decl_id;
+ expected_record.id = actual_record.id;
}
(_, _) => (),
}
@@ -38,16 +38,16 @@
assert_cc_produces_ir_items_ignoring_decl_ids(
"int Add(int a, int b);",
vec![Item::Func(Func {
- name: UnqualifiedIdentifier::Identifier(ir_id("Add")),
- decl_id: /* ignored */ DeclId(42),
- owning_target: "//test:testing_target".into(),
- mangled_name: "_Z3Addii".to_string(),
- doc_comment: None,
- return_type: ir_int(),
- params: vec![ir_int_param("a"), ir_int_param("b")],
- is_inline: false,
- member_func_metadata: None,
- })],
+ name: UnqualifiedIdentifier::Identifier(ir_id("Add")),
+ record_decl_id: None,
+ owning_target: "//test:testing_target".into(),
+ mangled_name: "_Z3Addii".to_string(),
+ doc_comment: None,
+ return_type: ir_int(),
+ params: vec![ir_int_param("a"), ir_int_param("b")],
+ is_inline: false,
+ member_func_metadata: None,
+ })],
);
}
diff --git a/rs_bindings_from_cc/ir_test.cc b/rs_bindings_from_cc/ir_test.cc
index 43174c7..2682d2f 100644
--- a/rs_bindings_from_cc/ir_test.cc
+++ b/rs_bindings_from_cc/ir_test.cc
@@ -59,7 +59,7 @@
"items": [
{ "Record": {
"identifier": { "identifier": "SomeStruct" },
- "decl_id": 42,
+ "id": 42,
"owning_target": "//foo:bar",
"fields": [
{
@@ -123,7 +123,7 @@
IR ir = {.used_headers = {HeaderName("foo/bar.h")},
.current_target = Label(std::string("//foo:bar")),
.items = {Record{.identifier = Identifier("SomeStruct"),
- .decl_id = DeclId(42),
+ .id = DeclId(42),
.owning_target = Label(std::string("//foo:bar")),
.fields =
{
diff --git a/rs_bindings_from_cc/src_code_gen.rs b/rs_bindings_from_cc/src_code_gen.rs
index 0cae54e..2792bcc 100644
--- a/rs_bindings_from_cc/src_code_gen.rs
+++ b/rs_bindings_from_cc/src_code_gen.rs
@@ -653,9 +653,9 @@
// `ir_testing`.
fn test_duplicate_decl_ids_err() {
let mut r1 = ir_record("R1");
- r1.decl_id = DeclId(42);
+ r1.id = DeclId(42);
let mut r2 = ir_record("R2");
- r2.decl_id = DeclId(42);
+ r2.id = DeclId(42);
let result = make_ir_from_items([r1.into(), r2.into()]);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Duplicate decl_id found in"));
@@ -665,7 +665,7 @@
fn test_simple_function() -> Result<()> {
let ir = make_ir_from_items([Item::Func(Func {
name: UnqualifiedIdentifier::Identifier(ir_id("add")),
- decl_id: DeclId(42),
+ record_decl_id: None,
owning_target: "//foo:bar".into(),
mangled_name: "_Z3Addii".to_string(),
doc_comment: None,
@@ -706,7 +706,7 @@
let ir = make_ir_from_parts(
vec![Item::Func(Func {
name: UnqualifiedIdentifier::Identifier(ir_id("add")),
- decl_id: DeclId(42),
+ record_decl_id: None,
owning_target: "//foo:bar".into(),
mangled_name: "_Z3Addii".to_string(),
doc_comment: None,
@@ -761,7 +761,7 @@
fn test_simple_struct() -> Result<()> {
let ir = make_ir_from_items([Item::Record(Record {
identifier: ir_id("SomeStruct"),
- decl_id: DeclId(42),
+ id: DeclId(42),
owning_target: "//foo:bar".into(),
doc_comment: None,
fields: vec![
@@ -881,7 +881,7 @@
fn test_ptr_func() -> Result<()> {
let ir = make_ir_from_items([Item::Func(Func {
name: UnqualifiedIdentifier::Identifier(Identifier { identifier: "Deref".to_string() }),
- decl_id: DeclId(42),
+ record_decl_id: None,
owning_target: "//foo:bar".into(),
mangled_name: "_Z5DerefPKPi".to_string(),
doc_comment: None,
@@ -1008,7 +1008,7 @@
fn test_doc_comment_func() -> Result<()> {
let ir = make_ir_from_items([Item::Func(Func {
name: UnqualifiedIdentifier::Identifier(ir_id("func")),
- decl_id: DeclId(42),
+ record_decl_id: None,
owning_target: "//foo:bar".into(),
mangled_name: "foo".to_string(),
doc_comment: Some("Doc Comment".to_string()),
@@ -1030,7 +1030,7 @@
fn test_doc_comment_record() -> Result<()> {
let ir = make_ir_from_items([Item::Record(Record {
identifier: ir_id("SomeStruct"),
- decl_id: DeclId(42),
+ id: DeclId(42),
owning_target: "//foo:bar".into(),
doc_comment: Some("Doc Comment\n\n * with bullet".to_string()),
alignment: 0,