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

Namespace types in C++ #428

Merged
merged 16 commits into from
Feb 24, 2024
Merged

Namespace types in C++ #428

merged 16 commits into from
Feb 24, 2024

Conversation

Manishearth
Copy link
Contributor

@Manishearth Manishearth commented Feb 13, 2024

First step of #427

Does not yet deal with capi namespacing, or putting the headers in the right folders.

@Manishearth Manishearth requested a review from sffc February 13, 2024 00:30
@Manishearth Manishearth mentioned this pull request Feb 13, 2024
3 tasks
feature_tests/cpp2/include/AttrOpaque1Renamed.d.hpp Outdated Show resolved Hide resolved
class AttrOpaque1Renamed {
public:

inline static std::unique_ptr<AttrOpaque1Renamed> totally_not_new();
inline static std::unique_ptr<ns::AttrOpaque1Renamed> totally_not_new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, here and elsewhere: You don't need to restate the namespace when you are inside a namespace block. I think how C++ resolution works is it will look in the current namespace and strip one level from the namespace until it either finds the item or fails. So, specifying ns::Foo here is potentially ambiguous because if there is a namespace ns { namespace ns { Foo } } it will find that one instead of the one at the current level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree: I'd like the generating code to be clean and don't care as much about redundancy in this code. Tracking namespaces everywhere will be somewhat painful

As for ambiguity, my general take is "don't do that", in the long run we can patch this if desired, or use unambiguous ::ns imports (I believe that works?).

feature_tests/cpp2/include/AttrOpaque1.h Show resolved Hide resolved
tool/src/cpp2/header.rs Show resolved Hide resolved
tool/src/cpp2/ty.rs Outdated Show resolved Hide resolved
pub fn append_forward(&mut self, def: TypeDef, ty_name_unnamespaced: &str) {
let forward = Self::forward_for(def, ty_name_unnamespaced);

let ns = def.attrs().namespace.clone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: We should make a type Namespace. It shouldn't be a string, because:

  1. It could have multiple levels which some backends may care about
  2. There are so many types flying around in Diplomat that I have tried hard to use newtypes whenever possible to prevent coding errors like passing the wrong string as a positional argument

Copy link
Contributor Author

@Manishearth Manishearth Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

body: String::new(),
indent_str: " ",
}
}

fn forward_for(def: TypeDef, ty_name_unnamespaced: &str) -> Forward {
match def {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought: I kind-of like the old style of putting the Forward type directly in gen_type_name to avoid the double match

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I wasn't sure of this but decided to go this way so that the rest of the code doesn't need to worry about the header's internal ontology of forward kinds.

@Manishearth Manishearth requested a review from sffc February 21, 2024 23:07
@Manishearth Manishearth merged commit cdd0232 into rust-diplomat:main Feb 24, 2024
6 checks passed
@Manishearth Manishearth deleted the cpp-ns branch February 24, 2024 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants