|
| 1 | +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM |
| 2 | +// Exceptions. See /LICENSE for license information. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + |
| 5 | +#include "toolchain/check/cpp_custom_type_mapping.h" |
| 6 | + |
| 7 | +#include "clang/AST/DeclCXX.h" |
| 8 | +#include "clang/AST/DeclTemplate.h" |
| 9 | +#include "llvm/ADT/STLExtras.h" |
| 10 | +#include "llvm/ADT/StringRef.h" |
| 11 | + |
| 12 | +namespace Carbon::Check { |
| 13 | + |
| 14 | +// A small, lightweight library of AST matchers. Unlike clang's ASTMatchers, |
| 15 | +// this avoids heap allocations and is suitable for one-off matching rather than |
| 16 | +// matching against a whole AST. |
| 17 | +namespace Matchers { |
| 18 | +// A matcher for a type T is just a function that takes a T and returns whether |
| 19 | +// it matched. Matchers should be invoked immediately, and are not expected to |
| 20 | +// outlive the arguments of the call that created them. |
| 21 | +// TODO: We could avoid the indirect calls by making the below functions be |
| 22 | +// templated on the inner matcher. |
| 23 | +template <typename T> |
| 24 | +using Matcher = llvm::function_ref<auto(T)->bool>; |
| 25 | + |
| 26 | +// Returns a matcher for class declarations that determines whether the given |
| 27 | +// class is a class template specialization in namespace std with the specified |
| 28 | +// name and template arguments matching the given predicate. |
| 29 | +static auto StdClassTemplate( |
| 30 | + llvm::StringLiteral name, |
| 31 | + Matcher<const clang::TemplateArgumentList&> args_matcher |
| 32 | + [[clang::lifetimebound]]) -> auto { |
| 33 | + return [=](const clang::CXXRecordDecl* class_decl) -> bool { |
| 34 | + const auto* specialization = |
| 35 | + dyn_cast<clang::ClassTemplateSpecializationDecl>(class_decl); |
| 36 | + const auto* identifier = class_decl->getIdentifier(); |
| 37 | + return specialization && identifier && identifier->isStr(name) && |
| 38 | + specialization->isInStdNamespace() && |
| 39 | + args_matcher(specialization->getTemplateArgs()); |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +// Returns a matcher that matches types if they are class types whose class |
| 44 | +// matches the given matcher. |
| 45 | +static auto Class(Matcher<const clang::CXXRecordDecl*> class_matcher |
| 46 | + [[clang::lifetimebound]]) -> auto { |
| 47 | + return [=](clang::QualType type) -> bool { |
| 48 | + const auto* class_decl = type->getAsCXXRecordDecl(); |
| 49 | + return !type.hasQualifiers() && class_decl && class_matcher(class_decl); |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +// Returns a matcher that determines whether the given template argument is a |
| 54 | +// type matching the given predicate. |
| 55 | +static auto TypeTemplateArgument(Matcher<clang::QualType> type_matcher |
| 56 | + [[clang::lifetimebound]]) -> auto { |
| 57 | + return [=](clang::TemplateArgument arg) -> bool { |
| 58 | + return arg.getKind() == clang::TemplateArgument::Type && |
| 59 | + type_matcher(arg.getAsType()); |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +// A matcher that determines whether the given type is `char`. |
| 64 | +static auto Char(clang::QualType type) -> bool { |
| 65 | + return !type.hasQualifiers() && type->isCharType(); |
| 66 | +} |
| 67 | + |
| 68 | +// Returns a matcher that determines whether the given template argument list |
| 69 | +// matches the given sequence of template argument matchers. |
| 70 | +static auto TemplateArgumentsAre( |
| 71 | + std::initializer_list<Matcher<clang::TemplateArgument>> arg_matchers |
| 72 | + [[clang::lifetimebound]]) -> auto { |
| 73 | + return [=](const clang::TemplateArgumentList& args) -> bool { |
| 74 | + if (args.size() != arg_matchers.size()) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + for (auto [arg, matcher] : llvm::zip_equal(args.asArray(), arg_matchers)) { |
| 78 | + if (!matcher(arg)) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + } |
| 82 | + return true; |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +// A matcher for `std::char_traits<char>`. |
| 87 | +static auto StdCharTraitsChar(clang::QualType type) -> bool { |
| 88 | + return Class(StdClassTemplate( |
| 89 | + "char_traits", TemplateArgumentsAre({TypeTemplateArgument(Char)})))(type); |
| 90 | +} |
| 91 | + |
| 92 | +// A matcher for `std::string_view`. |
| 93 | +static auto StdStringView(const clang::CXXRecordDecl* record_decl) -> bool { |
| 94 | + return StdClassTemplate( |
| 95 | + "basic_string_view", |
| 96 | + TemplateArgumentsAre({TypeTemplateArgument(Char), |
| 97 | + TypeTemplateArgument(StdCharTraitsChar)}))( |
| 98 | + record_decl); |
| 99 | +} |
| 100 | +} // end namespace Matchers |
| 101 | + |
| 102 | +auto GetCustomCppTypeMapping(const clang::CXXRecordDecl* record_decl) |
| 103 | + -> CustomCppTypeMapping { |
| 104 | + if (Matchers::StdStringView(record_decl)) { |
| 105 | + return CustomCppTypeMapping::Str; |
| 106 | + } |
| 107 | + |
| 108 | + return CustomCppTypeMapping::None; |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace Carbon::Check |
0 commit comments