Files
carbon-lang/toolchain/check/cpp/overload_resolution.h
T
Chandler Carruth 7901fb3857 Don't include expensive Clang headers in widely-included headers (#7319)
Fundamentally, this uses forward declarations of Clang types to reduce
the overall compile time cost of Clang headers across the codebase.

Tracing and profiling showed ~2s of every check TU's ~8-12s compile time
going just to parsing Clang frontend and AST headers pulled in via a few
sem_ir and check headers that only use the Clang types by pointer or
reference:

- sem_ir/cpp_file.h (reached via sem_ir/file.h by ~150 TUs) included
clang/Frontend/CompilerInstance.h, clang/CodeGen/ModuleBuilder.h,
clang/AST/Mangle.h, and llvm/IR/Module.h. CppFile's accessors move out
of line to a new cpp_file.cpp and the header now forward-declares the
Clang types.
- check/cpp/context.h (reached via check/context.h by ~100 TUs) included
clang/Frontend/FrontendAction.h and clang/Parse/Parser.h, pulling in
clang's Sema.h and ASTUnit.h.
- sem_ir/clang_decl.h included clang/AST/Decl.h; the three small
functions that need complete Clang types move out of line.
- sem_ir/cpp_overload_set.h included clang/Sema/Overload.h solely for
the three-field OverloadCandidateSet::OperatorRewriteInfo, which is now
mirrored as CppOverloadSet::OperatorRewriteInfo, and clang/AST/Decl.h
solely for a pointer.
- sem_ir/name_scope.h's clang/AST/DeclBase.h include was vestigial.

TUs (and more narrowly included headers) that genuinely use the Clang
definitions now include the Clang headers directly.

Representative compile times (fastbuild, aarch64), combined with the
preceding instantiation-cost changes, relative to trunk:
- check/eval.cpp: 11.85s -> 6.94s (-41%)
- check/handle_operator.cpp: 7.71s -> 3.30s (-57%)
- language_server.cpp: 6.68s -> 3.16s (-53%)
- lower/handle.cpp: 6.75s -> 3.66s (-46%)
- sem_ir/file.cpp: 8.60s -> 6.11s (-29%)
- driver.cpp: 6.68s -> 4.78s (-28%)

Measured full-rebuild impact (316 first-party TUs, fastbuild): -689.5s
CPU, -29.9% relative to trunk.

Assisted-by: Claude
2026-06-07 16:27:22 +00:00

54 lines
2.5 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
#ifndef CARBON_TOOLCHAIN_CHECK_CPP_OVERLOAD_RESOLUTION_H_
#define CARBON_TOOLCHAIN_CHECK_CPP_OVERLOAD_RESOLUTION_H_
#include "clang/Sema/Overload.h"
#include "toolchain/check/context.h"
#include "toolchain/sem_ir/function.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::Check {
// Checks whether a selected overload is accessible and diagnoses if not.
// `parent_scope_id`, if specified, describes the scope that was named to find
// the overload. If unspecified, we assume the overload was found in the class
// that it is a direct member of, rather than a derived class.
auto CheckCppOverloadAccess(
Context& context, SemIR::LocId loc_id, clang::DeclAccessPair overload,
SemIR::KnownInstId<SemIR::FunctionDecl> overload_inst_id,
SemIR::NameScopeId parent_scope_id = SemIR::NameScopeId::None) -> void;
// Returns the passing mode to use for a parameter given the implicit
// conversion sequence and the argument expression.
auto GetPassingModeForCppParameter(const clang::ImplicitConversionSequence& ics,
const clang::Expr* arg_expr)
-> SemIR::ClangDeclSignature::PassingMode;
auto ComputeClangDeclSignatureFromBestViableFunction(
Context& context, clang::OverloadCandidateSet::iterator candidate,
clang::Expr* self_expr, llvm::ArrayRef<clang::Expr*> arg_exprs,
SemIR::ClangDeclSignature::Kind kind = SemIR::ClangDeclSignature::Normal)
-> SemIR::ClangDeclSignatureId;
// Resolves which function to call using Clang overload resolution. Returns an
// instruction referring to that function, or an error instruction if overload
// resolution failed.
//
// A set with a single non-templated function goes through the same rules for
// overload resolution. This is to make sure that calls that have no viable
// implicit conversion sequence are rejected even when an implicit conversion is
// possible. Keeping the same behavior here for consistency and supporting
// migrations so that the migrated callers from C++ remain valid.
auto PerformCppOverloadResolution(
Context& context, SemIR::LocId loc_id,
const SemIR::CppOverloadSet& overload_set,
llvm::ArrayRef<SemIR::InstId> template_arg_ids, SemIR::InstId self_id,
llvm::ArrayRef<SemIR::InstId> arg_ids) -> SemIR::InstId;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_CPP_OVERLOAD_RESOLUTION_H_