Add well-known identifier caching (#6486)

I'm doing this because I figured it'd be an incremental improvement for
all the operator lookups that we do. Even to the extent that we've
discussed witness caching, I think it'll still apply. It does add one
more step to adding new interfaces (before, you'd just write the string,
now you add it to the def file and reference it).

I'll claim it makes GetClangOperatorKind a lot friendlier to read/edit,
nevermind removing the string comparisons. :)
This commit is contained in:
Jon Ross-Perkins
2025-12-11 21:41:47 +00:00
committed by GitHub
parent a848ae11e4
commit c0b335b87f
19 changed files with 482 additions and 239 deletions
+17 -11
View File
@@ -32,6 +32,7 @@
#include "toolchain/check/context.h"
#include "toolchain/check/control_flow.h"
#include "toolchain/check/convert.h"
#include "toolchain/check/core_identifier.h"
#include "toolchain/check/cpp/access.h"
#include "toolchain/check/cpp/custom_type_mapping.h"
#include "toolchain/check/cpp/generate_ast.h"
@@ -730,16 +731,17 @@ static auto MakeIntType(Context& context, IntId size_id, bool is_signed)
}
static auto MakeCppCompatType(Context& context, SemIR::LocId loc_id,
llvm::StringRef name) -> TypeExpr {
return ExprAsType(context, loc_id,
LookupNameInCore(context, loc_id, {"CppCompat", name}));
CoreIdentifier name) -> TypeExpr {
return ExprAsType(
context, loc_id,
LookupNameInCore(context, loc_id, {CoreIdentifier::CppCompat, name}));
}
// Maps a C++ builtin integer type to a Carbon `Core.CppCompat` type.
static auto MapBuiltinCppCompatIntegerType(Context& context,
unsigned int cpp_width,
unsigned int carbon_width,
llvm::StringRef cpp_compat_name)
CoreIdentifier cpp_compat_name)
-> TypeExpr {
if (cpp_width != carbon_width) {
return TypeExpr::None;
@@ -775,23 +777,27 @@ static auto MapBuiltinIntegerType(Context& context, SemIR::LocId loc_id,
MakeCharTypeLiteral(context, Parse::NodeId::None));
}
if (clang::ASTContext::hasSameType(qual_type, ast_context.LongTy)) {
return MapBuiltinCppCompatIntegerType(context, width, 32, "Long32");
return MapBuiltinCppCompatIntegerType(context, width, 32,
CoreIdentifier::Long32);
}
if (clang::ASTContext::hasSameType(qual_type, ast_context.UnsignedLongTy)) {
return MapBuiltinCppCompatIntegerType(context, width, 32, "ULong32");
return MapBuiltinCppCompatIntegerType(context, width, 32,
CoreIdentifier::ULong32);
}
if (clang::ASTContext::hasSameType(qual_type, ast_context.LongLongTy)) {
return MapBuiltinCppCompatIntegerType(context, width, 64, "LongLong64");
return MapBuiltinCppCompatIntegerType(context, width, 64,
CoreIdentifier::LongLong64);
}
if (clang::ASTContext::hasSameType(qual_type,
ast_context.UnsignedLongLongTy)) {
return MapBuiltinCppCompatIntegerType(context, width, 64, "ULongLong64");
return MapBuiltinCppCompatIntegerType(context, width, 64,
CoreIdentifier::ULongLong64);
}
return TypeExpr::None;
}
static auto MapNullptrType(Context& context, SemIR::LocId loc_id) -> TypeExpr {
return MakeCppCompatType(context, loc_id, "NullptrT");
return MakeCppCompatType(context, loc_id, CoreIdentifier::NullptrT);
}
// Maps a C++ builtin type to a Carbon type.
@@ -820,7 +826,7 @@ static auto MapBuiltinType(Context& context, SemIR::LocId loc_id,
}
// TODO: Handle floating-point types that map to named aliases.
} else if (type.isVoidType()) {
return MakeCppCompatType(context, loc_id, "VoidBase");
return MakeCppCompatType(context, loc_id, CoreIdentifier::VoidBase);
} else if (type.isNullPtrType()) {
return MapNullptrType(context, loc_id);
}
@@ -937,7 +943,7 @@ static auto ClangGetUnqualifiedTypePreserveNonNull(
// `inner_type_inst_id`.
static auto MakeOptionalType(Context& context, SemIR::LocId loc_id,
SemIR::InstId inner_type_inst_id) -> TypeExpr {
auto fn_inst_id = LookupNameInCore(context, loc_id, "Optional");
auto fn_inst_id = LookupNameInCore(context, loc_id, CoreIdentifier::Optional);
auto call_id = PerformCall(context, loc_id, fn_inst_id, {inner_type_inst_id});
return ExprAsType(context, loc_id, call_id);
}