Skip to content

Commit 0cbf4c0

Browse files
[CIR][CodeGen] Implemented VisitTypeTraitExpr (#1850)
Implements Issue #1802
1 parent 16e2250 commit 0cbf4c0

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/CIR/MissingFeatures.h"
1919

2020
#include "clang/AST/StmtVisitor.h"
21+
#include "clang/AST/ExprCXX.h"
2122
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
2223
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
2324
#include "clang/CIR/Dialect/IR/CIRDialect.h"
@@ -702,7 +703,13 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
702703
return {};
703704
}
704705
mlir::Value VisitTypeTraitExpr(const TypeTraitExpr *E) {
705-
llvm_unreachable("NYI");
706+
mlir::Location loc = CGF.getLoc(E->getExprLoc());
707+
if (E->isStoredAsBoolean())
708+
return Builder.getBool(E->getBoolValue(), loc);
709+
710+
assert(E->getAPValue().isInt() && "APValue type not supported");
711+
mlir::Type ty = CGF.convertType(E->getType());
712+
return Builder.getConstAPInt(loc, ty, E->getAPValue().getInt());
706713
}
707714
mlir::Value
708715
VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) {

clang/test/CIR/CodeGen/type-trait.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR
2+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
4+
5+
// CIR-LABEL: cir.func dso_local @_Z4testv() -> !cir.bool
6+
// CIR: %[[RETVAL:.+]] = cir.alloca !cir.bool
7+
// CIR: %[[CONST_TRUE:.+]] = cir.const #true
8+
// CIR: cir.store %[[CONST_TRUE]], %[[RETVAL]]
9+
// CIR: %[[LOADED_VAL:.+]] = cir.load %[[RETVAL]]
10+
// CIR: cir.return %[[LOADED_VAL]]
11+
12+
// LLVM-LABEL: define dso_local {{.*}}i1 @_Z4testv()
13+
// LLVM: ret i1 true
14+
15+
namespace B {
16+
template <class _0p> class B {
17+
public:
18+
typedef _0p A;
19+
B() { __has_trivial_destructor(A); }
20+
};
21+
template <class _0p, class _0e0uence = B<_0p>> class A { _0e0uence A; };
22+
} // namespace B
23+
24+
class A { public: B::A<A> A; };
25+
26+
bool test() {
27+
return __has_trivial_destructor(A);
28+
}
29+

0 commit comments

Comments
 (0)