Skip to content
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
16 changes: 16 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,8 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {

void Converter::ConvertVarDeclInitializer(clang::VarDecl *decl) {
if (decl->hasInit()) {
ScopedValue constant(constant_evaluated_,
HasConstantEvaluatedInit(ctx_, decl));
ConvertVarInit(decl->getType(), decl->getInit());
} else if (!clang::isa<clang::ParmVarDecl>(decl)) {
StrCat(ConvertVarDefaultInit(decl));
Expand All @@ -620,6 +622,8 @@ void Converter::EmitHoistedInArmAssignment(clang::VarDecl *decl) {
return;
}
StrCat(GetNamedDeclAsString(decl), token::kAssign);
ScopedValue constant(constant_evaluated_,
HasConstantEvaluatedInit(ctx_, decl));
ConvertVarInit(decl->getType(), decl->getInit());
StrCat(token::kSemiColon);
}
Expand Down Expand Up @@ -1925,6 +1929,11 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) {
}

bool Converter::ConvertSpecialCall(clang::CallExpr *expr) {
if (IsConstantEvaluatedQuery(expr)) {
EmitBoolLiteral(constant_evaluated_);
return true;
}

if (IsBuiltinVaStart(expr) || IsBuiltinVaEnd(expr) || IsBuiltinVaCopy(expr)) {
ConvertVAArgCall(expr);
MarkFresh();
Expand Down Expand Up @@ -3397,6 +3406,10 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
}

if (auto init = AsLambdaUncapturedConstant(curr_function_, expr)) {
ScopedValue constant(
constant_evaluated_,
HasConstantEvaluatedInit(ctx_,
clang::cast<clang::VarDecl>(expr->getDecl())));
Convert(init);
return false;
}
Expand Down Expand Up @@ -4264,6 +4277,8 @@ bool Converter::VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr) {
}

bool Converter::VisitConstantExpr(clang::ConstantExpr *expr) {
ScopedValue constant(constant_evaluated_,
constant_evaluated_ || expr->isImmediateInvocation());
Convert(expr->getSubExpr());
MarkFresh();
return false;
Expand Down Expand Up @@ -4329,6 +4344,7 @@ void Converter::ConvertLambdaClosure(const clang::CXXRecordDecl *decl,
EmitFunctionPreamble(call_operator);
ScopedValue push_fn(curr_function_, call_operator);
ScopedValue function_body(in_const_initializer_, false);
ScopedValue runtime_body(constant_evaluated_, false);
ConvertFunctionBody(curr_function_);
}

Expand Down
1 change: 1 addition & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

std::string ufcs_receiver_;
bool in_const_initializer_ = false;
bool constant_evaluated_ = false;
std::optional<bool> autoref_mut_;

std::vector<clang::Expr *> curr_for_inc_;
Expand Down
12 changes: 12 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2299,6 +2299,18 @@ bool IsBuiltinVaCopy(const clang::CallExpr *expr) {
return IsBuiltinCall(expr, clang::Builtin::BI__builtin_va_copy);
}

bool IsConstantEvaluatedQuery(const clang::CallExpr *expr) {
return expr->getBuiltinCallee() ==
clang::Builtin::BI__builtin_is_constant_evaluated ||
IsStdDecl(expr->getDirectCallee(), "is_constant_evaluated");
}

bool HasConstantEvaluatedInit(const clang::ASTContext &ctx,
const clang::VarDecl *decl) {
return decl->isUsableInConstantExpressions(ctx) ||
decl->hasConstantInitialization();
}

bool NeedsImplicitScalarCast(clang::ASTContext &ctx, clang::QualType from,
clang::QualType to) {
return !from.isNull() && !to.isNull() && from->isIntegerType() &&
Expand Down
5 changes: 5 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ bool IsBuiltinVaEnd(const clang::CallExpr *expr);

bool IsBuiltinVaCopy(const clang::CallExpr *expr);

bool IsConstantEvaluatedQuery(const clang::CallExpr *expr);

bool HasConstantEvaluatedInit(const clang::ASTContext &ctx,
const clang::VarDecl *decl);

bool IsTransparentStdCall(const clang::CallExpr *expr);

const clang::Expr *IgnoreTransparentStdCall(const clang::Expr *expr);
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,8 @@ void ConverterRefCount::EmitHoistedInArmAssignment(clang::VarDecl *decl) {

PushConversionKind push(*this, VarConversionKind(decl));
StrCat(token::kAssign);
ScopedValue constant(constant_evaluated_,
HasConstantEvaluatedInit(ctx_, decl));
StrCat(ConvertVarInitValue(type, decl->getInit()));
StrCat(token::kSemiColon);
}
Expand Down
2 changes: 0 additions & 2 deletions rules/builtin/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ bool f9(long a, long b, long *r) { return __builtin_mul_overflow(a, b, r); }
bool f10(long long a, long long b, long long *r) {
return __builtin_mul_overflow(a, b, r);
}

bool f19() { return __builtin_is_constant_evaluated(); }
4 changes: 0 additions & 4 deletions rules/builtin/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,3 @@ unsafe fn f17() -> f32 {
unsafe fn f18(a0: *const ::core::ffi::c_char) -> f32 {
f32::NAN
}

unsafe fn f19() -> bool {
false
}
26 changes: 26 additions & 0 deletions tests/std/meta/type.traits/meta.const.eval.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
#include <cassert>
#include <type_traits>

// N4950 [meta.const.eval]/1: Effects: Equivalent to:
// if consteval {
// return true;
// } else {
// return false;
// }
namespace p1_is_constant_evaluated {
int pick() { return std::is_constant_evaluated() ? 1 : 2; }

void test() {
constexpr bool compile_time = std::is_constant_evaluated();
assert(compile_time);
bool run_time = std::is_constant_evaluated();
assert(!run_time);
assert(pick() == 2);
}
} // namespace p1_is_constant_evaluated

int main() {
p1_is_constant_evaluated::test();
return 0;
}
27 changes: 27 additions & 0 deletions tests/std/meta/type.traits/out/refcount/meta.const.eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn pick_0() -> i32 {
return if false { 1 } else { 2 };
}
pub fn test_1() {
let mut compile_time: bool = true;
assert!(compile_time);
let mut run_time: bool = false;
assert!(!(run_time));
assert!((({ pick_0() }) == 2));
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
({ test_1() });
return 0;
}
pub fn __cpp2rust_init_globals() {}
29 changes: 29 additions & 0 deletions tests/std/meta/type.traits/out/unsafe/meta.const.eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn pick_0() -> i32 {
return if false { 1 } else { 2 };
}
pub unsafe fn test_1() {
let compile_time: bool = true;
assert!(compile_time);
let mut run_time: bool = false;
assert!(!(run_time));
assert!(((unsafe { pick_0() }) == (2)));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
(unsafe { test_1() });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
13 changes: 13 additions & 0 deletions tests/unit/constexpr_function.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
#include <cassert>

int runtime_only(int x) { return x * 2; }
Expand Down Expand Up @@ -37,6 +38,8 @@ constexpr bool in_constant_context() {

static_assert(in_constant_context());

consteval bool imm(bool b) { return b; }

struct P {
int v;
constexpr int get() const { return v; }
Expand All @@ -63,5 +66,15 @@ int main() {
assert(p.get() == 9);
constexpr int k = scaled(4);
assert(k == 4);
constexpr bool ce = __builtin_is_constant_evaluated();
assert(ce);
const int n = __builtin_is_constant_evaluated() ? 13 : 17;
assert(n == 13);
static bool s = __builtin_is_constant_evaluated();
assert(s);
bool rt = __builtin_is_constant_evaluated();
assert(!rt);
bool b = imm(__builtin_is_constant_evaluated());
assert(b);
return 0;
}
15 changes: 15 additions & 0 deletions tests/unit/out/refcount/constexpr_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub fn in_constant_context_6() -> bool {
return (({ runtime_only_0(1) }) == 0);
}
const _: () = assert!(true, "in_constant_context()");
pub fn imm_7(mut b: bool) -> bool {
return b;
}
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(4)]
pub struct P {
Expand Down Expand Up @@ -72,8 +75,20 @@ fn main_0() -> i32 {
assert!((({ PImpl::get(&p.as_pointer(),) }) == 9));
let mut k: i32 = ({ scaled_2(4) });
assert!((k == 4));
let mut ce: bool = true;
assert!(ce);
let mut n: i32 = if true { 13 } else { 17 };
assert!((n == 13));
assert!(s_8.with(|rc| *rc.borrow()));
let mut rt: bool = false;
assert!(!(rt));
let mut b: bool = ({ imm_7(true) });
assert!(b);
return 0;
}
thread_local!(
static s_8: Value<bool> = Rc::new(RefCell::new(true));
);
pub trait FlagImpl {
fn to_bool(&self) -> bool;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/out/unsafe/constexpr_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub unsafe fn in_constant_context_6() -> bool {
return ((unsafe { runtime_only_0(1) }) == (0));
}
const _: () = assert!(true, "in_constant_context()");
pub unsafe fn imm_7(mut b: bool) -> bool {
return b;
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct P {
Expand Down Expand Up @@ -81,6 +84,16 @@ unsafe fn main_0() -> i32 {
assert!(((unsafe { P::get(&p,) }) == (9)));
let k: i32 = (unsafe { scaled_2(4) });
assert!(((k) == (4)));
let ce: bool = true;
assert!(ce);
let n: i32 = if true { 13 } else { 17 };
assert!(((n) == (13)));
assert!((*std::cell::LazyCell::force_mut(&mut *&raw mut s_8)));
let mut rt: bool = false;
assert!(!(rt));
let mut b: bool = (unsafe { imm_7(true) });
assert!(b);
return 0;
}
static mut s_8: std::cell::LazyCell<bool> = std::cell::LazyCell::new(|| unsafe { true });
pub unsafe fn __cpp2rust_init_globals() {}
Loading