Skip to content

Commit f8f764f

Browse files
Apply review suggestions
1 parent e5470a5 commit f8f764f

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

toolchain/check/cpp/import.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
#include "llvm/ADT/StringMap.h"
3131
#include "llvm/ADT/StringRef.h"
3232
#include "llvm/Support/raw_ostream.h"
33+
#include "toolchain/base/int.h"
3334
#include "toolchain/base/kind_switch.h"
35+
#include "toolchain/base/value_ids.h"
3436
#include "toolchain/check/call.h"
3537
#include "toolchain/check/class.h"
3638
#include "toolchain/check/context.h"
@@ -2247,8 +2249,8 @@ static auto IsIncompleteClass(Context& context, SemIR::NameScopeId scope_id)
22472249
context.classes().Get(class_decl->class_id).self_type_id);
22482250
}
22492251

2250-
// Maps a Clang constant expression to a Carbon constant. Currently supports
2251-
// only integer constants.
2252+
// Maps a Clang literal expression to a Carbon constant. Currently supports
2253+
// only integer and floating-point literals.
22522254
// TODO: Add support for the other constant types for which a C++ to Carbon type
22532255
// mapping exists.
22542256
static auto MapConstant(Context& context, SemIR::LocId loc_id,
@@ -2261,24 +2263,31 @@ static auto MapConstant(Context& context, SemIR::LocId loc_id,
22612263
loc_id, "Unsupported: constant type: " + expr->getType().getAsString());
22622264
return SemIR::ErrorInst::InstId;
22632265
}
2266+
22642267
SemIR::InstId inst_id = SemIR::InstId::None;
2268+
SemIR::ImportIRInstId imported_loc_id =
2269+
AddImportIRInst(context.sem_ir(), expr->getExprLoc());
2270+
22652271
if (auto* integer_literal = dyn_cast<clang::IntegerLiteral>(expr)) {
2266-
auto int_id =
2272+
IntId int_id =
22672273
context.ints().Add(integer_literal->getValue().getSExtValue());
2268-
inst_id = AddInstInNoBlock<SemIR::IntValue>(
2269-
context, loc_id, {.type_id = type_id, .int_id = int_id});
2274+
inst_id = AddInstInNoBlock(
2275+
context,
2276+
MakeImportedLocIdAndInst<SemIR::IntValue>(
2277+
context, imported_loc_id, {.type_id = type_id, .int_id = int_id}));
22702278
} else if (auto* float_literal = dyn_cast<clang::FloatingLiteral>(expr)) {
2271-
auto float_id = context.floats().Add(float_literal->getValue());
2272-
inst_id = AddInstInNoBlock<SemIR::FloatValue>(
2273-
context, loc_id, {.type_id = type_id, .float_id = float_id});
2279+
FloatId float_id = context.floats().Add(float_literal->getValue());
2280+
inst_id = AddInstInNoBlock(context,
2281+
MakeImportedLocIdAndInst<SemIR::FloatValue>(
2282+
context, imported_loc_id,
2283+
{.type_id = type_id, .float_id = float_id}));
22742284
} else {
22752285
context.TODO(
22762286
loc_id, "Unsupported: constant type: " + expr->getType().getAsString());
2277-
inst_id = SemIR::ErrorInst::InstId;
2278-
}
2279-
if (inst_id != SemIR::ErrorInst::InstId) {
2280-
context.imports().push_back(inst_id);
2287+
return SemIR::ErrorInst::InstId;
22812288
}
2289+
2290+
context.imports().push_back(inst_id);
22822291
return inst_id;
22832292
}
22842293

toolchain/check/cpp/macros.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "clang/AST/Expr.h"
99
#include "clang/Parse/Parser.h"
1010
#include "clang/Sema/Sema.h"
11+
#include "common/check.h"
1112

1213
namespace Carbon::Check {
1314

@@ -54,25 +55,30 @@ auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
5455
parser.SkipUntil(clang::tok::eof);
5556
CARBON_DIAGNOSTIC(
5657
InCppMacroEvaluation, Error,
57-
"failed to evaluate macro Cpp.{0} to a valid constant expression",
58+
"failed to parse macro Cpp.{0} to a valid constant expression",
5859
std::string);
5960
context.emitter().Emit(loc_id, InCppMacroEvaluation, (*name_str_opt).str());
6061
return nullptr;
6162
}
6263

6364
clang::Expr::EvalResult evaluated_result;
64-
if (result_expr->EvaluateAsInt(evaluated_result, sema.getASTContext())) {
65+
if (!result_expr->EvaluateAsConstantExpr(evaluated_result,
66+
sema.getASTContext())) {
67+
context.TODO(loc_id,
68+
"failed to evaluate macro to a valid constant expression");
69+
return nullptr;
70+
}
71+
clang::APValue ap_value = evaluated_result.Val;
72+
if (ap_value.isInt()) {
6573
return clang::IntegerLiteral::Create(
66-
sema.getASTContext(), evaluated_result.Val.getInt(),
67-
result_expr->getType(), result_expr->getExprLoc());
74+
sema.getASTContext(), ap_value.getInt(), result_expr->getType(),
75+
result_expr->getExprLoc());
6876
}
69-
llvm::APFloat ap_value(0.0);
70-
if (result_expr->EvaluateAsFloat(ap_value, sema.getASTContext())) {
77+
if (ap_value.isFloat()) {
7178
return clang::FloatingLiteral::Create(
72-
sema.getASTContext(), ap_value,
79+
sema.getASTContext(), ap_value.getFloat(),
7380
/*isExact=*/true, result_expr->getType(), result_expr->getExprLoc());
7481
}
75-
7682
context.TODO(loc_id, "Unsupported: constant type:" +
7783
result_expr->getType().getAsString());
7884
return nullptr;

toolchain/check/testdata/interop/cpp/macros.carbon

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn F() {
5757
// CHECK:STDERR: Cpp.CONFIG_VALUE;
5858
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
5959
// CHECK:STDERR:
60-
// CHECK:STDERR: fail_import_bad_suffix.carbon:[[@LINE+11]]:3: error: failed to evaluate macro Cpp.CONFIG_VALUE to a valid constant expression [InCppMacroEvaluation]
60+
// CHECK:STDERR: fail_import_bad_suffix.carbon:[[@LINE+11]]:3: error: failed to parse macro Cpp.CONFIG_VALUE to a valid constant expression [InCppMacroEvaluation]
6161
// CHECK:STDERR: Cpp.CONFIG_VALUE;
6262
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
6363
// CHECK:STDERR: fail_import_bad_suffix.carbon:[[@LINE+8]]:3: note: in `Cpp` name lookup for `CONFIG_VALUE` [InCppNameLookup]
@@ -321,6 +321,20 @@ fn F() {
321321
Cpp.MyLongDouble;
322322
}
323323

324+
// --- fail_import_assign_to_float.carbon
325+
326+
library "[[@TEST_NAME]]";
327+
328+
import Cpp library "floating_point_literal_macro.h";
329+
330+
fn F() {
331+
// CHECK:STDERR: fail_import_assign_to_float.carbon:[[@LINE+4]]:3: error: expression is not assignable [AssignmentToNonAssignable]
332+
// CHECK:STDERR: Cpp.MyDouble = 1.0;
333+
// CHECK:STDERR: ^~~~~~~~~~~~
334+
// CHECK:STDERR:
335+
Cpp.MyDouble = 1.0;
336+
}
337+
324338
// --- macro_undefined.h
325339

326340
#define CONFIG_VALUE 1

toolchain/sem_ir/typed_insts.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,10 @@ struct FloatType {
683683

684684
// A floating point value.
685685
struct FloatValue {
686-
static constexpr auto Kind = InstKind::FloatValue.Define<Parse::NodeId>(
687-
{.ir_name = "float_value", .constant_kind = InstConstantKind::Always});
686+
static constexpr auto Kind =
687+
InstKind::FloatValue.Define<Parse::RealLiteralId>(
688+
{.ir_name = "float_value",
689+
.constant_kind = InstConstantKind::Always});
688690

689691
TypeId type_id;
690692
FloatId float_id;

0 commit comments

Comments
 (0)