Skip to content

Commit fd4dbc5

Browse files
authored
Remove clang prefixes from isa/cast when optional (#5909)
This is from a quick discussion with zygoloid. There's a mix of uses, we're both comfortable with a "minimum syntax" decision here to use ADL. Note this doesn't touch llvm::cast uses in yaml_test_helpers, but ADL doesn't work there (because the objects are in a `llvm::yaml` namespace). This gets to another choice made here: if we specify a namespace, prefer `llvm::` because it's shorter. `clang` has a using of them, but it's probably better to point at the canonical version if we're being explicit about it.
1 parent 905c964 commit fd4dbc5

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

toolchain/check/import_cpp.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,10 @@ static auto ClangLookup(Context& context, SemIR::NameScopeId scope_id,
446446
auto scope_clang_decl_context_id =
447447
context.name_scopes().Get(scope_id).clang_decl_context_id();
448448
bool found = sema.LookupQualifiedName(
449-
lookup,
450-
clang::dyn_cast<clang::DeclContext>(context.sem_ir()
451-
.clang_decls()
452-
.Get(scope_clang_decl_context_id)
453-
.decl));
449+
lookup, dyn_cast<clang::DeclContext>(context.sem_ir()
450+
.clang_decls()
451+
.Get(scope_clang_decl_context_id)
452+
.decl));
454453

455454
if (!found) {
456455
return std::nullopt;
@@ -720,7 +719,7 @@ static auto ImportClassObjectRepr(Context& context, SemIR::ClassId class_id,
720719

721720
// Import fields.
722721
for (auto* decl : clang_def->decls()) {
723-
auto* field = clang::dyn_cast<clang::FieldDecl>(decl);
722+
auto* field = dyn_cast<clang::FieldDecl>(decl);
724723

725724
// Track the chain of fields from the class to this field. This chain is
726725
// only one element long unless the field is a member of an anonymous struct
@@ -731,7 +730,7 @@ static auto ImportClassObjectRepr(Context& context, SemIR::ClassId class_id,
731730
// If this isn't a field, it might be an indirect field in an anonymous
732731
// struct or union.
733732
if (!field) {
734-
auto* indirect_field = clang::dyn_cast<clang::IndirectFieldDecl>(decl);
733+
auto* indirect_field = dyn_cast<clang::IndirectFieldDecl>(decl);
735734
if (!indirect_field) {
736735
continue;
737736
}
@@ -773,12 +772,12 @@ static auto ImportClassObjectRepr(Context& context, SemIR::ClassId class_id,
773772

774773
// Compute the offset to the field that appears directly in the class.
775774
uint64_t offset = clang_layout.getFieldOffset(
776-
clang::cast<clang::FieldDecl>(chain.front())->getFieldIndex());
775+
cast<clang::FieldDecl>(chain.front())->getFieldIndex());
777776

778777
// If this is an indirect field, walk the path and accumulate the offset to
779778
// the named field.
780779
for (auto* inner_decl : chain.drop_front()) {
781-
auto* inner_field = clang::cast<clang::FieldDecl>(inner_decl);
780+
auto* inner_field = cast<clang::FieldDecl>(inner_decl);
782781
const auto& inner_layout =
783782
context.ast_context().getASTRecordLayout(inner_field->getParent());
784783
offset += inner_layout.getFieldOffset(inner_field->getFieldIndex());
@@ -930,7 +929,7 @@ static auto MapBuiltinType(Context& context, clang::QualType qual_type,
930929
// Maps a C++ record type to a Carbon type.
931930
static auto MapRecordType(Context& context, const clang::RecordType& type)
932931
-> TypeExpr {
933-
auto* record_decl = clang::dyn_cast<clang::CXXRecordDecl>(type.getDecl());
932+
auto* record_decl = dyn_cast<clang::CXXRecordDecl>(type.getDecl());
934933
if (!record_decl) {
935934
return {.inst_id = SemIR::TypeInstId::None, .type_id = SemIR::TypeId::None};
936935
}
@@ -1407,7 +1406,7 @@ static auto AddDependentUnimportedDecls(const Context& context,
14071406

14081407
if (auto* clang_function_decl = clang_decl->getAsFunction()) {
14091408
AddDependentUnimportedFunctionDecls(context, *clang_function_decl, decls);
1410-
} else if (auto* type_decl = clang::dyn_cast<clang::TypeDecl>(clang_decl)) {
1409+
} else if (auto* type_decl = dyn_cast<clang::TypeDecl>(clang_decl)) {
14111410
AddDependentUnimportedTypeDecls(
14121411
context, type_decl->getASTContext().getTypeDeclType(type_decl), decls);
14131412
}
@@ -1422,11 +1421,10 @@ static auto ImportDeclAfterDependencies(Context& context, SemIR::LocId loc_id,
14221421
if (auto* clang_function_decl = clang_decl->getAsFunction()) {
14231422
return ImportFunctionDecl(context, loc_id, clang_function_decl);
14241423
}
1425-
if (auto* clang_namespace_decl =
1426-
clang::dyn_cast<clang::NamespaceDecl>(clang_decl)) {
1424+
if (auto* clang_namespace_decl = dyn_cast<clang::NamespaceDecl>(clang_decl)) {
14271425
return ImportNamespaceDecl(context, clang_namespace_decl);
14281426
}
1429-
if (auto* type_decl = clang::dyn_cast<clang::TypeDecl>(clang_decl)) {
1427+
if (auto* type_decl = dyn_cast<clang::TypeDecl>(clang_decl)) {
14301428
auto type = type_decl->getASTContext().getTypeDeclType(type_decl);
14311429
auto type_inst_id = MapType(context, loc_id, type).inst_id;
14321430
if (!type_inst_id.has_value()) {
@@ -1436,7 +1434,7 @@ static auto ImportDeclAfterDependencies(Context& context, SemIR::LocId loc_id,
14361434
}
14371435
return type_inst_id;
14381436
}
1439-
if (clang::isa<clang::FieldDecl, clang::IndirectFieldDecl>(clang_decl)) {
1437+
if (isa<clang::FieldDecl, clang::IndirectFieldDecl>(clang_decl)) {
14401438
// Usable fields get imported as a side effect of importing the class.
14411439
if (SemIR::InstId existing_inst_id =
14421440
LookupClangDeclInstId(context, clang_decl);

toolchain/lower/constant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ static auto EmitAsConstant(ConstantContext& context, SemIR::IntValue inst)
234234

235235
// IntLiteral is represented as an empty struct. All other integer types are
236236
// represented as an LLVM integer type.
237-
auto* int_type = llvm::dyn_cast<llvm::IntegerType>(type);
237+
auto* int_type = dyn_cast<llvm::IntegerType>(type);
238238
if (!int_type) {
239239
auto* int_literal_value = context.GetIntLiteralAsValue();
240240
CARBON_CHECK(int_literal_value->getType() == type);

toolchain/lower/mangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ auto Mangler::Mangle(SemIR::FunctionId function_id,
151151
return "main";
152152
}
153153
if (function.clang_decl_id.has_value()) {
154-
return MangleCppClang(llvm::dyn_cast<clang::NamedDecl>(
154+
return MangleCppClang(dyn_cast<clang::NamedDecl>(
155155
sem_ir().clang_decls().Get(function.clang_decl_id).decl));
156156
}
157157
RawStringOstream os;

0 commit comments

Comments
 (0)