Files
carbon-lang/toolchain/diagnostics/format_providers.cpp
T
Richard Smith 1b2ae912fc Add basic support for eval fn and musteval fn. (#6694)
Add support for compile-time functions. `eval fn` is analogous to C++
`constexpr`, and is evaluated at compile time when it has compile-time
arguments. `musteval fn` is analogous to C++ `consteval`, and requires
that its arguments be available at compile time and is always evaluated
at compile time. For now we require the modifier to match across
redeclarations of the function. The specific modifier syntax here is a
placeholder and not yet part of an approved design.

Limitations: Only very basic support for evaluation is provided. So far
there's no support for mutable state or `if` expressions, but otherwise
control flow and passing and returning values should work. Carbon
evaluation recursion is modeled by C++ recursion for now, so you can
overflow the toolchain stack easily. Functions that use in-place
initialization will generally not work yet, as they are modeled as
passing a non-compile-time-constant reference to a temporary to the
call.

Add missing categorization of `name_binding_decl` as `NotExpr` to match
other similar declaration instructions like `FunctionDecl`, so that we
can uniformly skip over them when they occur within function bodies.

Assisted-by: Gemini 3 Pro and Flash via Antigravity
2026-02-11 02:08:16 +00:00

101 lines
3.3 KiB
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
#include "toolchain/diagnostics/format_providers.h"
#include "common/check.h"
#include "llvm/ADT/StringExtras.h"
auto llvm::format_provider<Carbon::Diagnostics::BoolAsSelect>::format(
const Carbon::Diagnostics::BoolAsSelect& wrapper, raw_ostream& out,
StringRef style) -> void {
if (style.empty()) {
llvm::format_provider<bool>::format(wrapper.value, out, style);
return;
}
auto sep = style.find('|');
CARBON_CHECK(
sep != llvm::StringRef::npos,
"BoolAsSelect requires a `|` separating true and false results: `{0}`",
style);
CARBON_CHECK(style.find('|', sep + 1) == llvm::StringRef::npos,
"BoolAsSelect only allows one `|`: `{0}`", style);
if (wrapper.value) {
out << style.take_front(sep);
} else {
out << style.drop_front(sep + 1);
}
}
auto llvm::format_provider<Carbon::Diagnostics::IntAsSelect>::format(
const Carbon::Diagnostics::IntAsSelect& wrapper, raw_ostream& out,
StringRef style) -> void {
if (style == "s") {
if (wrapper.value != 1) {
out << "s";
}
return;
} else if (style.empty()) {
llvm::format_provider<int>::format(wrapper.value, out, style);
return;
}
auto cursor = style;
while (!cursor.empty()) {
auto case_sep = cursor.find("|");
auto token = cursor.substr(0, case_sep);
if (case_sep == llvm::StringRef::npos) {
cursor = llvm::StringRef();
} else {
cursor = cursor.drop_front(case_sep + 1);
}
auto pair_sep = token.find(':');
CARBON_CHECK(pair_sep != llvm::StringRef::npos,
"IntAsSelect requires a `:` separating each comparison and "
"output string: `{0}`",
style);
auto comp = token.take_front(pair_sep);
auto output_string = token.drop_front(pair_sep + 1);
if (comp.empty()) {
// Default case.
CARBON_CHECK(cursor.empty(),
"IntAsSelect requires the default case be last: `{0}`",
style);
out << output_string;
return;
} else if (auto op = comp.take_while(
[](char c) { return c == '<' || c == '=' || c == '>'; });
!op.empty()) {
// Comparison.
comp = comp.drop_front(op.size());
int value;
CARBON_CHECK(to_integer(comp, value),
"IntAsSelect has invalid value in comparison: `{0}`", style);
auto result = llvm::StringSwitch<std::optional<bool>>(op)
.Case("=", wrapper.value == value)
.Case("<", wrapper.value < value)
.Case("<=", wrapper.value <= value)
.Case(">", wrapper.value > value)
.Case(">=", wrapper.value >= value)
.Default(std::nullopt);
if (!result) {
CARBON_FATAL("IntAsSelect has unrecognized comparison: `{0}`", style);
}
if (*result) {
out << output_string;
return;
}
} else {
CARBON_FATAL("IntAsSelect has unrecognized syntax: `{0}`", style);
}
}
CARBON_FATAL("IntAsSelect doesn't handle `{0}`: `{1}`", wrapper.value, style);
}