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

rustified_enum doesn't work with anonymous enums #1161

Open
tamird opened this issue Nov 27, 2017 · 7 comments
Open

rustified_enum doesn't work with anonymous enums #1161

tamird opened this issue Nov 27, 2017 · 7 comments
Labels

Comments

@tamird
Copy link
Contributor

tamird commented Nov 27, 2017

Input C/C++ Header

https://github.com/llvm-mirror/llvm/blob/master/include/llvm/IR/CallingConv.h

Bindgen Invocation

bindgen::Builder::default()
    .header("llvm/IR/CallingConv.h")
    .whitelisted_type("llvm::CallingConv.*")
    .rustified_enum("llvm::CallingConv.*")
    .generate()
    .unwrap()
    .write_to_file("bindings.rs")
    .unwrap();

Actual Results

/// LLVM IR allows to use arbitrary numbers as calling convention identifiers. 
pub type llvm_CallingConv_ID = :: std :: os :: raw :: c_uint ; pub const C : llvm_CallingConv__bindgen_ty_1 = 0 ;
pub const Fast : llvm_CallingConv__bindgen_ty_1 = 8 ;
pub const Cold : llvm_CallingConv__bindgen_ty_1 = 9 ;
pub const GHC : llvm_CallingConv__bindgen_ty_1 = 10;
pub const HiPE : llvm_CallingConv__bindgen_ty_1 = 11 ;
pub const WebKit_JS : llvm_CallingConv__bindgen_ty_1 = 12 ;
pub const AnyReg : llvm_CallingConv__bindgen_ty_1 = 13 ;
pub const PreserveMost : llvm_CallingConv__bindgen_ty_1 = 14 ;
pub const PreserveAll : llvm_CallingConv__bindgen_ty_1 = 15 ;
pub const Swift : llvm_CallingConv__bindgen_ty_1 = 16 ;
pub const CXX_FAST_TLS : llvm_CallingConv__bindgen_ty_1 = 17 ;
pub const FirstTargetCC : llvm_CallingConv__bindgen_ty_1 = 64 ;
pub const X86_StdCall : llvm_CallingConv__bindgen_ty_1 = 64 ;
pub const X86_FastCall : llvm_CallingConv__bindgen_ty_1 = 65 ;
pub const ARM_APCS : llvm_CallingConv__bindgen_ty_1 = 66 ;
...
pub const MaxID : llvm_CallingConv__bindgen_ty_1 = 1023 ;
pub type llvm_CallingConv__bindgen_ty_1 = :: std :: os :: raw :: c_uint ; 

Expected Results

A Rust enum resembling:

pub enum LLVMRustResult { Success = 0 , Failure = 1 , }
@emilio
Copy link
Contributor

emilio commented Nov 27, 2017

I'm 99% sure this is a dupe of #1125.

@fitzgen
Copy link
Member

fitzgen commented Nov 27, 2017

I'm 99% sure this is a dupe of #1125.

So shall we close this issue? Any reason you wanted to leave it open?

@tamird
Copy link
Contributor Author

tamird commented Nov 27, 2017

It's not clear to me that this is a dupe; maybe just leave it open and I'll investigate once #1125 is closed.

@tamird
Copy link
Contributor Author

tamird commented Nov 28, 2017

OK, so #1162 didn't fix this for me, at least not the way I'm (naively) trying to use this.

However, looking closer, I'm not really sure what the semantics here are. The C++ code is roughly:

namespace llvm {

namespace CallingConv {

  using ID = unsigned;

  enum {
    C = 0,
    Fast = 8,
    ....
    MaxID = 1023
  };

} // end namespace CallingConv

} // end namespace llvm

My C++ is pretty nascent, so I'm not really sure what this code does, but it looks like it just declares a bunch of integral constants and not a true enum. From my perspective, it would be nice to be able to make this a rustified enum, but I'm quite certain that's not currently a feature of bindgen. @emilio?

@tamird
Copy link
Contributor Author

tamird commented Nov 28, 2017

eh, I misspoke - this is weird. This doesn't work if I pass "llvm::CallingConv::ID" into rustified_enum but if I pass "llvm::CallingConv::.*" I get:

/// LLVM IR allows to use arbitrary numbers as calling convention identifiers.
pub type llvm_CallingConv_ID = ::std::os::raw::c_uint;
pub const C: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::C;
pub const Fast: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::Fast;
pub const Cold: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::Cold;
pub const GHC: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::GHC;
pub const HiPE: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::HiPE;
pub const WebKit_JS: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::WebKit_JS;
pub const AnyReg: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::AnyReg;
pub const PreserveMost: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::PreserveMost;
pub const PreserveAll: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::PreserveAll;
pub const Swift: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::Swift;
pub const CXX_FAST_TLS: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::CXX_FAST_TLS;
pub const FirstTargetCC: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::FirstTargetCC;
pub const X86_StdCall: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::FirstTargetCC;
pub const X86_FastCall: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::X86_FastCall;
pub const ARM_APCS: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::ARM_APCS;
pub const ARM_AAPCS: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::ARM_AAPCS;
pub const ARM_AAPCS_VFP: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::ARM_AAPCS_VFP;
pub const MSP430_INTR: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::MSP430_INTR;
pub const X86_ThisCall: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::X86_ThisCall;
pub const PTX_Kernel: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::PTX_Kernel;
pub const PTX_Device: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::PTX_Device;
pub const SPIR_FUNC: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::SPIR_FUNC;
pub const SPIR_KERNEL: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::SPIR_KERNEL;
pub const Intel_OCL_BI: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::Intel_OCL_BI;
pub const X86_64_SysV: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::X86_64_SysV;
pub const X86_64_Win64: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::X86_64_Win64;
pub const X86_VectorCall: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::X86_VectorCall;
pub const HHVM: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::HHVM;
pub const HHVM_C: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::HHVM_C;
pub const X86_INTR: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::X86_INTR;
pub const AVR_INTR: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::AVR_INTR;
pub const AVR_SIGNAL: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::AVR_SIGNAL;
pub const AVR_BUILTIN: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::AVR_BUILTIN;
pub const AMDGPU_VS: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::AMDGPU_VS;
pub const AMDGPU_GS: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::AMDGPU_GS;
pub const AMDGPU_PS: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::AMDGPU_PS;
pub const AMDGPU_CS: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::AMDGPU_CS;
pub const AMDGPU_KERNEL: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::AMDGPU_KERNEL;
pub const X86_RegCall: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::X86_RegCall;
pub const MSP430_BUILTIN: llvm_CallingConv__bindgen_ty_1 =
    llvm_CallingConv__bindgen_ty_1::MSP430_BUILTIN;
pub const MaxID: llvm_CallingConv__bindgen_ty_1 = llvm_CallingConv__bindgen_ty_1::MaxID;
#[repr(u32)]
/// A set of enums which specify the assigned numeric values for known llvm
/// /// calling conventions.
/// /// @brief LLVM Calling Convention Representation
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum llvm_CallingConv__bindgen_ty_1 {
    C = 0,
    Fast = 8,
    Cold = 9,
    GHC = 10,
    HiPE = 11,
    WebKit_JS = 12,
    AnyReg = 13,
    PreserveMost = 14,
    PreserveAll = 15,
    Swift = 16,
    CXX_FAST_TLS = 17,
    FirstTargetCC = 64,
    X86_FastCall = 65,
    ARM_APCS = 66,
    ARM_AAPCS = 67,
    ARM_AAPCS_VFP = 68,
    MSP430_INTR = 69,
    X86_ThisCall = 70,
    PTX_Kernel = 71,
    PTX_Device = 72,
    SPIR_FUNC = 75,
    SPIR_KERNEL = 76,
    Intel_OCL_BI = 77,
    X86_64_SysV = 78,
    X86_64_Win64 = 79,
    X86_VectorCall = 80,
    HHVM = 81,
    HHVM_C = 82,
    X86_INTR = 83,
    AVR_INTR = 84,
    AVR_SIGNAL = 85,
    AVR_BUILTIN = 86,
    AMDGPU_VS = 87,
    AMDGPU_GS = 88,
    AMDGPU_PS = 89,
    AMDGPU_CS = 90,
    AMDGPU_KERNEL = 91,
    X86_RegCall = 92,
    MSP430_BUILTIN = 94,
    MaxID = 1023,
}

In other words, I get both the rustified enum and all the constants.

@fitzgen
Copy link
Member

fitzgen commented Nov 29, 2017

This is the expected behavior. Whenever there is an explicit value given to an enum variant, we also make a constant (if I remember the rules correctly).

I think we can close this issue, unless I am overlooking something.

@tamird
Copy link
Contributor Author

tamird commented Nov 29, 2017

I wasn't complaining about the generated code, I was just saying that the rules for matching this enum are not clear. Why do I need to use a wildcard?

Separately, I do have a complaint about the generated code: take a look at X86_StdCall - it appears as a constant, but it is absent from the enum (and thus it is not usable as an associated constant). The reason seems to be that it has the same value as FirstTargetCC, which is used in the constant's defintion. Unfortunately, it seems that rust disallows having two enum variants with the same value, so I'm not sure what a sensible solution here is. Perhaps constified_module_enum, but again, the matching rules are not clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants