mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Add support for boolean literals in macros (#6418)
Adding support for macros with boolean literals.
Demo:
```c++
// main.carbon
library "Main";
import Core library "io";
import Cpp inline '''
#define M_TRUE true
''';
fn Run() -> i32 {
let a: bool = Cpp.M_TRUE;
if (a) {
Core.Print(1);
} else {
Core.Print(0);
}
return 0;
}
```
```
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link main.o \--output=demo_carbon
$ ./demo_carbon
1
```
Part of #6303
This commit is contained in:
@@ -71,6 +71,11 @@ auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
|
||||
clang::APValue ap_value = evaluated_result.Val;
|
||||
switch (ap_value.getKind()) {
|
||||
case clang::APValue::Int:
|
||||
if (result_expr->getType()->isBooleanType()) {
|
||||
return clang::CXXBoolLiteralExpr::Create(
|
||||
sema.getASTContext(), ap_value.getInt().getBoolValue(),
|
||||
result_expr->getType(), result_expr->getExprLoc());
|
||||
}
|
||||
return clang::IntegerLiteral::Create(
|
||||
sema.getASTContext(), ap_value.getInt(), result_expr->getType(),
|
||||
result_expr->getExprLoc());
|
||||
|
||||
Reference in New Issue
Block a user