mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
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
22 lines
810 B
C++
22 lines
810 B
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_TOOLCHAIN_CHECK_CPP_MACROS_H_
|
|
#define CARBON_TOOLCHAIN_CHECK_CPP_MACROS_H_
|
|
|
|
#include "toolchain/check/context.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
// Tries to evaluate the given macro to a constant expression. Returns the
|
|
// evaluated expression on success or nullptr otherwise.
|
|
// TODO: Add support for all literal types.
|
|
auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
|
|
SemIR::NameId name_id,
|
|
clang::MacroInfo* macro_info) -> clang::Expr*;
|
|
|
|
} // namespace Carbon::Check
|
|
|
|
#endif // CARBON_TOOLCHAIN_CHECK_CPP_MACROS_H_
|