Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typedef in result #394

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions feature_tests/c/include/OptionStruct.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions feature_tests/cpp/include/OptionStruct.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 14 additions & 93 deletions tool/src/c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn gen_struct_header<'a>(

if let ast::CustomType::Struct(strct) = typ {
for (_, typ, _) in &strct.fields {
gen_includes(typ, in_path, true, false, env, &mut seen_includes, out)?;
gen_includes(typ, in_path, env, &mut seen_includes, out)?;
collect_results(typ, in_path, env, seen_results, all_results);
}
}
Expand Down Expand Up @@ -115,34 +115,18 @@ fn gen_struct_header<'a>(

if let ast::CustomType::Struct(strct) = typ {
for (_, typ, _) in &strct.fields {
gen_includes(typ, in_path, true, false, env, &mut seen_includes, out)?;
gen_includes(typ, in_path, env, &mut seen_includes, out)?;
collect_results(typ, in_path, env, seen_results, all_results);
}
}
for method in typ.methods() {
for param in &method.params {
gen_includes(
&param.ty,
in_path,
false,
false,
env,
&mut seen_includes,
out,
)?;
gen_includes(&param.ty, in_path, env, &mut seen_includes, out)?;
collect_results(&param.ty, in_path, env, seen_results, all_results);
}

if let Some(return_type) = method.return_type.as_ref() {
gen_includes(
return_type,
in_path,
false,
false,
env,
&mut seen_includes,
out,
)?;
gen_includes(return_type, in_path, env, &mut seen_includes, out)?;
collect_results(return_type, in_path, env, seen_results, all_results);
}
}
Expand Down Expand Up @@ -200,24 +184,8 @@ fn gen_result_header(
writeln!(out, "#include \"diplomat_runtime.h\"")?;
writeln!(out)?;
let mut seen_includes = HashSet::new();
gen_includes(
ok.as_ref(),
in_path,
true,
false,
env,
&mut seen_includes,
out,
)?;
gen_includes(
err.as_ref(),
in_path,
true,
false,
env,
&mut seen_includes,
out,
)?;
gen_includes(ok.as_ref(), in_path, env, &mut seen_includes, out)?;
gen_includes(err.as_ref(), in_path, env, &mut seen_includes, out)?;
writeln!(out, "#ifdef __cplusplus")?;
writeln!(out, "namespace capi {{")?;
writeln!(out, "extern \"C\" {{")?;
Expand All @@ -240,83 +208,36 @@ fn gen_result_header(
pub fn gen_includes<W: fmt::Write>(
typ: &ast::TypeName,
in_path: &ast::Path,
pre_struct: bool,
behind_ref: bool,
env: &Env,
seen_includes: &mut HashSet<String>,
out: &mut W,
) -> fmt::Result {
match typ {
ast::TypeName::Named(path_type) | ast::TypeName::SelfType(path_type) => {
let custom_typ = path_type.resolve(in_path, env);
match (custom_typ, behind_ref) {
(ast::CustomType::Opaque(_) | ast::CustomType::Struct(_), true) => {
if pre_struct {
let decl = format!(
"typedef struct {} {};",
custom_typ.name(),
custom_typ.name()
);
if !seen_includes.contains(&decl) {
writeln!(out, "{decl}")?;
seen_includes.insert(decl);
}
} else {
let include = format!("#include \"{}.h\"", custom_typ.name());
if !seen_includes.contains(&include) {
writeln!(out, "{include}")?;
seen_includes.insert(include);
}
}
}

(ast::CustomType::Struct(_), false) | (ast::CustomType::Enum(_), _) => {
match custom_typ {
ast::CustomType::Opaque(_)
| ast::CustomType::Struct(_)
| ast::CustomType::Enum(_) => {
let include = format!("#include \"{}.h\"", custom_typ.name());
if !seen_includes.contains(&include) {
writeln!(out, "{include}")?;
seen_includes.insert(include);
}
}

(ast::CustomType::Opaque(_), false) => {
panic!("Cannot pass opaque types by value")
}
(&_, _) => unreachable!("unknown AST/HIR variant"),
_ => unreachable!("unknown AST/HIR variant"),
}
}
ast::TypeName::Box(underlying) => {
gen_includes(
underlying,
in_path,
pre_struct,
true,
env,
seen_includes,
out,
)?;
gen_includes(underlying, in_path, env, seen_includes, out)?;
}
ast::TypeName::Reference(.., underlying) => {
gen_includes(
underlying,
in_path,
pre_struct,
true,
env,
seen_includes,
out,
)?;
gen_includes(underlying, in_path, env, seen_includes, out)?;
}
ast::TypeName::Primitive(_) => {}
ast::TypeName::Option(underlying) => {
gen_includes(
underlying,
in_path,
pre_struct,
behind_ref,
env,
seen_includes,
out,
)?;
gen_includes(underlying, in_path, env, seen_includes, out)?;
}
ast::TypeName::Result(_, _, _) => {
let include = format!("#include \"{}.h\"", name_for_type(typ));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ expression: out_texts.get(out).unwrap()
#include <stdbool.h>
#include "diplomat_runtime.h"

typedef struct Foo Foo;
#include "Foo.h"
#ifdef __cplusplus
namespace capi {
#endif
Expand All @@ -21,7 +21,7 @@ typedef struct Bar {
#ifdef __cplusplus
} // namespace capi
#endif
typedef struct Foo Foo;
#include "Foo.h"
#ifdef __cplusplus
namespace capi {
extern "C" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ typedef struct MyStruct {
#ifdef __cplusplus
} // namespace capi
#endif
typedef struct MyStruct MyStruct;
#ifdef __cplusplus
namespace capi {
extern "C" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ expression: out_texts.get(out).unwrap()
#include <stdbool.h>
#include "diplomat_runtime.h"

typedef struct MyOpaqueStruct MyOpaqueStruct;
#include "MyOpaqueStruct.h"
#ifdef __cplusplus
namespace capi {
#endif
Expand All @@ -21,7 +21,6 @@ typedef struct MyStruct {
#ifdef __cplusplus
} // namespace capi
#endif
typedef struct MyOpaqueStruct MyOpaqueStruct;
#include "MyOpaqueStruct.h"
#ifdef __cplusplus
namespace capi {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ expression: out_texts.get(out).unwrap()
#include <stdbool.h>
#include "diplomat_runtime.h"

typedef struct MyStruct MyStruct;
#include "MyStruct.h"
#ifdef __cplusplus
namespace capi {
extern "C" {
Expand Down
Loading