Skip to content

Commit b474ede

Browse files
authored
Translate TypeTraitExpr (#44)
1 parent 05f1ce7 commit b474ede

5 files changed

Lines changed: 82 additions & 0 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,14 @@ bool Converter::VisitUnaryExprOrTypeTraitExpr(
26572657
return false;
26582658
}
26592659

2660+
bool Converter::VisitTypeTraitExpr(clang::TypeTraitExpr *expr) {
2661+
clang::Expr::EvalResult result;
2662+
ENSURE(expr->EvaluateAsInt(result, ctx_));
2663+
StrCat(std::to_string(result.Val.getInt().getExtValue()));
2664+
computed_expr_type_ = ComputedExprType::FreshValue;
2665+
return false;
2666+
}
2667+
26602668
bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
26612669
ENSURE(decl_ids_.insert(GetID(decl)).second);
26622670
if (Mapper::Contains(ctx_.getCanonicalTagType(decl))) {

cpp2rust/converter/converter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
286286
virtual bool
287287
VisitUnaryExprOrTypeTraitExpr(clang::UnaryExprOrTypeTraitExpr *expr);
288288

289+
virtual bool VisitTypeTraitExpr(clang::TypeTraitExpr *expr);
290+
289291
virtual bool VisitEnumDecl(clang::EnumDecl *decl);
290292

291293
virtual bool VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <assert.h>
2+
3+
int main(void) {
4+
assert(__builtin_types_compatible_p(int, int) == 1);
5+
assert(__builtin_types_compatible_p(int, long) == 0);
6+
7+
int x = 0;
8+
assert(__builtin_types_compatible_p(__typeof__(x), int) == 1);
9+
assert(__builtin_types_compatible_p(__typeof__(x), long) == 0);
10+
11+
int *p = 0;
12+
assert(__builtin_types_compatible_p(__typeof__(p), int *) == 1);
13+
assert(__builtin_types_compatible_p(__typeof__(p), long *) == 0);
14+
15+
unsigned long ul = 0;
16+
assert(__builtin_types_compatible_p(__typeof__(ul), unsigned long) == 1);
17+
assert(__builtin_types_compatible_p(__typeof__(ul), signed long) == 0);
18+
19+
return 0;
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
assert!((1 == 1));
14+
assert!((0 == 0));
15+
let x: Value<i32> = Rc::new(RefCell::new(0));
16+
assert!((1 == 1));
17+
assert!((0 == 0));
18+
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(Default::default()));
19+
assert!((1 == 1));
20+
assert!((0 == 0));
21+
let ul: Value<u64> = Rc::new(RefCell::new(0_u64));
22+
assert!((1 == 1));
23+
assert!((0 == 0));
24+
return 0;
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
assert!(((1) == (1)));
16+
assert!(((0) == (0)));
17+
let mut x: i32 = 0;
18+
assert!(((1) == (1)));
19+
assert!(((0) == (0)));
20+
let mut p: *mut i32 = Default::default();
21+
assert!(((1) == (1)));
22+
assert!(((0) == (0)));
23+
let mut ul: u64 = 0_u64;
24+
assert!(((1) == (1)));
25+
assert!(((0) == (0)));
26+
return 0;
27+
}

0 commit comments

Comments
 (0)