Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1 | // Part of the Crubit 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 "lifetime_annotations/type_lifetimes.h" |
| 6 | |
| 7 | #include <algorithm> |
Dmitri Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame] | 8 | #include <cassert> |
| 9 | #include <cstddef> |
| 10 | #include <functional> |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 11 | #include <memory> |
| 12 | #include <optional> |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 13 | #include <string> |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 14 | #include <utility> |
Dmitri Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame] | 15 | #include <vector> |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 16 | |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 17 | #include "absl/strings/str_cat.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 18 | #include "absl/strings/str_join.h" |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 19 | #include "lifetime_annotations/function_lifetimes.h" |
Marcel Hlopko | 97a68a1 | 2022-03-09 11:38:51 +0000 | [diff] [blame] | 20 | #include "lifetime_annotations/lifetime.h" |
Martin Brænne | 198ff49 | 2023-04-05 01:41:25 -0700 | [diff] [blame] | 21 | #include "lifetime_annotations/lifetime_error.h" |
Dmitri Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame] | 22 | #include "lifetime_annotations/lifetime_substitutions.h" |
Marcel Hlopko | 97a68a1 | 2022-03-09 11:38:51 +0000 | [diff] [blame] | 23 | #include "lifetime_annotations/lifetime_symbol_table.h" |
| 24 | #include "lifetime_annotations/pointee_type.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 25 | #include "clang/AST/Attr.h" |
| 26 | #include "clang/AST/Attrs.inc" |
| 27 | #include "clang/AST/DeclCXX.h" |
| 28 | #include "clang/AST/DeclTemplate.h" |
Dmitri Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame] | 29 | #include "clang/AST/Expr.h" |
| 30 | #include "clang/AST/NestedNameSpecifier.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 31 | #include "clang/AST/TemplateBase.h" |
| 32 | #include "clang/AST/Type.h" |
Dmitri Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame] | 33 | #include "clang/AST/TypeLoc.h" |
| 34 | #include "clang/AST/TypeOrdering.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 35 | #include "clang/Basic/LLVM.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 36 | #include "llvm/ADT/ArrayRef.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 37 | #include "llvm/ADT/Hashing.h" |
| 38 | #include "llvm/ADT/SmallVector.h" |
| 39 | #include "llvm/ADT/StringRef.h" |
| 40 | #include "llvm/Support/Error.h" |
| 41 | #include "llvm/Support/ErrorHandling.h" |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 42 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 43 | namespace clang { |
| 44 | namespace tidy { |
| 45 | namespace lifetimes { |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 46 | |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 47 | clang::QualType StripAttributes(clang::QualType type) { |
| 48 | while (true) { |
| 49 | if (auto macro_qualified_type = type->getAs<clang::MacroQualifiedType>()) { |
| 50 | type = macro_qualified_type->getUnderlyingType(); |
| 51 | } else if (auto attr_type = type->getAs<clang::AttributedType>()) { |
| 52 | type = attr_type->getModifiedType(); |
| 53 | } else { |
| 54 | return type; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | clang::TypeLoc StripAttributes(clang::TypeLoc type_loc, |
| 60 | llvm::SmallVector<const clang::Attr*>& attrs) { |
| 61 | while (true) { |
| 62 | if (auto macro_qualified_type_loc = |
| 63 | type_loc.getAs<clang::MacroQualifiedTypeLoc>()) { |
| 64 | type_loc = macro_qualified_type_loc.getInnerLoc(); |
| 65 | } else if (auto attr_type_loc = |
| 66 | type_loc.getAs<clang::AttributedTypeLoc>()) { |
| 67 | attrs.push_back(attr_type_loc.getAttr()); |
| 68 | type_loc = attr_type_loc.getModifiedLoc(); |
| 69 | } else { |
| 70 | // The last attribute in a chain of attributes will appear as the |
| 71 | // outermost AttributedType, e.g. `int $a $b` will be represented as |
| 72 | // follows (in pseudocode): |
| 73 | // |
| 74 | // AttributedType(annotate_type("lifetime", "b"), |
| 75 | // AttributedType(annotate_type("lifetime", "a"), |
| 76 | // BuiltinType("int") |
| 77 | // ) |
| 78 | // ) |
| 79 | // |
| 80 | // We reverse the attributes so that we obtain the more intuitive ordering |
| 81 | // "a, b". |
| 82 | std::reverse(attrs.begin(), attrs.end()); |
| 83 | return type_loc; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 88 | llvm::Expected<llvm::SmallVector<const clang::Expr*>> GetAttributeLifetimes( |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 89 | llvm::ArrayRef<const clang::Attr*> attrs) { |
| 90 | llvm::SmallVector<const clang::Expr*> result; |
| 91 | |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 92 | bool saw_annotate_type = false; |
| 93 | |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 94 | for (const clang::Attr* attr : attrs) { |
| 95 | auto annotate_type_attr = clang::dyn_cast<clang::AnnotateTypeAttr>(attr); |
| 96 | if (!annotate_type_attr || |
| 97 | annotate_type_attr->getAnnotation() != "lifetime") |
| 98 | continue; |
| 99 | |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 100 | if (saw_annotate_type) { |
Martin Brænne | 198ff49 | 2023-04-05 01:41:25 -0700 | [diff] [blame] | 101 | return llvm::make_error<LifetimeError>( |
| 102 | LifetimeError::Type::Other, |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 103 | "Only one `[[annotate_type(\"lifetime\", ...)]]` attribute may be " |
| 104 | "placed on a type"); |
| 105 | } |
| 106 | saw_annotate_type = true; |
| 107 | |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 108 | for (const clang::Expr* arg : annotate_type_attr->args()) { |
| 109 | result.push_back(arg); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return result; |
| 114 | } |
| 115 | |
Googler | 247cef7 | 2022-03-02 15:13:38 +0000 | [diff] [blame] | 116 | llvm::SmallVector<std::string> GetLifetimeParameters(clang::QualType type) { |
| 117 | // TODO(mboehme): |
| 118 | // - Add support for type aliases with lifetime parameters |
| 119 | // - Report errors as Clang diagnostics, not through |
| 120 | // llvm::report_fatal_error(). |
| 121 | |
| 122 | auto record = type->getAs<clang::RecordType>(); |
| 123 | if (!record) { |
| 124 | return {}; |
| 125 | } |
| 126 | |
| 127 | auto cxx_record = record->getAsCXXRecordDecl(); |
| 128 | if (!cxx_record) { |
| 129 | return {}; |
| 130 | } |
Googler | 17a2d51 | 2022-02-23 12:09:43 +0000 | [diff] [blame] | 131 | |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 132 | const clang::AnnotateAttr* lifetime_params_attr = nullptr; |
| 133 | for (auto annotate : cxx_record->specific_attrs<clang::AnnotateAttr>()) { |
Googler | 17a2d51 | 2022-02-23 12:09:43 +0000 | [diff] [blame] | 134 | if (annotate->getAnnotation() == "lifetime_params") { |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 135 | if (lifetime_params_attr) { |
| 136 | llvm::report_fatal_error("repeated lifetime annotation"); |
| 137 | } |
| 138 | lifetime_params_attr = annotate; |
| 139 | } |
| 140 | } |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 141 | |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 142 | llvm::SmallVector<std::string> ret; |
Martin Brænne | cf5d897 | 2022-04-25 23:27:52 -0700 | [diff] [blame] | 143 | |
| 144 | // TODO(mboehme): Require derived class to explicitly declare all lifetime |
| 145 | // parameters inherited from the base class. |
| 146 | if (cxx_record->hasDefinition()) { |
| 147 | for (const clang::CXXBaseSpecifier& base : cxx_record->bases()) { |
| 148 | if (lifetime_params_attr) { |
| 149 | llvm::report_fatal_error( |
| 150 | "derived classes may not add lifetime parameters"); |
| 151 | } |
| 152 | if (!ret.empty()) { |
| 153 | llvm::report_fatal_error( |
| 154 | "only one base class may have lifetime parameters"); |
| 155 | } |
| 156 | ret = GetLifetimeParameters(base.getType()); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (!lifetime_params_attr) { |
| 161 | return ret; |
| 162 | } |
| 163 | |
Googler | 17a2d51 | 2022-02-23 12:09:43 +0000 | [diff] [blame] | 164 | for (const auto& arg : lifetime_params_attr->args()) { |
| 165 | llvm::StringRef lifetime; |
| 166 | if (llvm::Error err = |
| 167 | EvaluateAsStringLiteral(arg, cxx_record->getASTContext()) |
| 168 | .moveInto(lifetime)) { |
| 169 | llvm::report_fatal_error(llvm::StringRef(toString(std::move(err)))); |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 170 | } |
Googler | 17a2d51 | 2022-02-23 12:09:43 +0000 | [diff] [blame] | 171 | ret.push_back(lifetime.str()); |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | return ret; |
| 175 | } |
| 176 | |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 177 | ValueLifetimes::ValueLifetimes(const ValueLifetimes& other) { *this = other; } |
| 178 | |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 179 | ValueLifetimes& ValueLifetimes::operator=(const ValueLifetimes& other) { |
Googler | 41f3e35 | 2021-12-16 15:24:21 +0000 | [diff] [blame] | 180 | type_ = other.type_; |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 181 | template_argument_lifetimes_ = other.template_argument_lifetimes_; |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 182 | lifetime_parameters_by_name_ = other.lifetime_parameters_by_name_; |
Devin Jeanpierre | 108e9c0 | 2022-06-02 07:10:09 -0700 | [diff] [blame] | 183 | auto pointee_lifetimes = |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 184 | other.pointee_lifetimes_ |
| 185 | ? std::make_unique<ObjectLifetimes>(*other.pointee_lifetimes_) |
| 186 | : nullptr; |
Devin Jeanpierre | 108e9c0 | 2022-06-02 07:10:09 -0700 | [diff] [blame] | 187 | auto function_lifetimes = |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 188 | other.function_lifetimes_ |
| 189 | ? std::make_unique<FunctionLifetimes>(*other.function_lifetimes_) |
| 190 | : nullptr; |
Devin Jeanpierre | 108e9c0 | 2022-06-02 07:10:09 -0700 | [diff] [blame] | 191 | // Note: because ValueLifetimes is a recursive type (pointee_lifetimes_ |
| 192 | // contains a ValueLifetimes), the following line can destroy `other`. |
| 193 | // (Thus the temporary local variables before we perform the assignment.) |
| 194 | pointee_lifetimes_ = std::move(pointee_lifetimes); |
| 195 | function_lifetimes_ = std::move(function_lifetimes); |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 196 | return *this; |
| 197 | } |
| 198 | |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 199 | // Defined here because FunctionLifetimes is an incomplete type in the header. |
| 200 | ValueLifetimes::~ValueLifetimes() = default; |
| 201 | |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 202 | namespace { |
| 203 | |
| 204 | llvm::Error ForEachTemplateArgument( |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 205 | clang::QualType type, clang::TypeLoc type_loc, |
| 206 | const std::function<llvm::Error(int, clang::QualType, clang::TypeLoc)>& |
| 207 | callback) { |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 208 | llvm::SmallVector<llvm::ArrayRef<clang::TemplateArgument>> template_args = |
| 209 | GetTemplateArgs(type); |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 210 | std::optional<llvm::SmallVector<llvm::SmallVector<clang::TypeLoc>>> |
| 211 | maybe_template_arg_locs; |
| 212 | if (type_loc) { |
| 213 | maybe_template_arg_locs = GetTemplateArgs(type_loc); |
| 214 | } |
| 215 | llvm::SmallVector<llvm::SmallVector<clang::TypeLoc>> template_arg_locs; |
| 216 | if (maybe_template_arg_locs) { |
| 217 | template_arg_locs = *std::move(maybe_template_arg_locs); |
| 218 | } else { |
| 219 | // Fill all of `template_arg_locs` with empty `TypeLoc`s so we don't need to |
| 220 | // make any case distinctions below. |
| 221 | template_arg_locs.resize(template_args.size()); |
| 222 | for (size_t depth = 0; depth < template_args.size(); depth++) { |
| 223 | template_arg_locs[depth].resize(template_args[depth].size()); |
| 224 | } |
| 225 | } |
Martin Brænne | c2f5e40 | 2023-03-31 05:26:27 -0700 | [diff] [blame] | 226 | if (template_arg_locs.size() != template_args.size()) { |
Jing Lu | e835875 | 2023-12-12 07:32:41 -0800 | [diff] [blame] | 227 | llvm::report_fatal_error(llvm::StringRef( |
| 228 | absl::StrCat("In ForEachTemplateArgument(", type.getAsString(), |
| 229 | "), template_args_locs has ", template_arg_locs.size(), |
| 230 | " entries and template_args has ", template_args.size(), |
| 231 | " entries, but they should be equal"))); |
Martin Brænne | c2f5e40 | 2023-03-31 05:26:27 -0700 | [diff] [blame] | 232 | } |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 233 | for (int depth = 0; depth < template_args.size(); depth++) { |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 234 | const auto& args_at_depth = template_args[depth]; |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 235 | const auto& arg_locs_at_depth = template_arg_locs[depth]; |
| 236 | assert(args_at_depth.size() == arg_locs_at_depth.size()); |
| 237 | for (size_t i = 0; i < args_at_depth.size(); ++i) { |
| 238 | const clang::TemplateArgument& arg = args_at_depth[i]; |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 239 | if (arg.getKind() == clang::TemplateArgument::Type) { |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 240 | if (llvm::Error err = |
| 241 | callback(depth, arg.getAsType(), arg_locs_at_depth[i])) { |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 242 | return err; |
| 243 | } |
| 244 | } else if (arg.getKind() == clang::TemplateArgument::Pack) { |
| 245 | for (const clang::TemplateArgument& inner_arg : arg.getPackAsArray()) { |
| 246 | if (inner_arg.getKind() == clang::TemplateArgument::Type) { |
Martin Brænne | 33f4f50 | 2022-06-27 01:15:15 -0700 | [diff] [blame] | 247 | // We pass `TypeLoc()` here because if the argument is a parameter |
| 248 | // pack, the only thing spelled out in the source code is that |
| 249 | // parameter pack -- there is no source location for the individual |
| 250 | // arguments contained in the pack. |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 251 | if (llvm::Error err = |
| 252 | callback(depth, inner_arg.getAsType(), clang::TypeLoc())) { |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 253 | return err; |
| 254 | } |
| 255 | } |
| 256 | } |
Martin Brænne | 16d86dd | 2022-04-08 00:33:36 -0700 | [diff] [blame] | 257 | } else { |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 258 | if (llvm::Error err = |
| 259 | callback(depth, clang::QualType(), clang::TypeLoc())) { |
Martin Brænne | 16d86dd | 2022-04-08 00:33:36 -0700 | [diff] [blame] | 260 | return err; |
| 261 | } |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } |
| 265 | return llvm::Error::success(); |
| 266 | } |
| 267 | |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 268 | QualType Undecay(clang::QualType type) { |
| 269 | if (auto decayed = type->getAs<clang::DecayedType>()) { |
| 270 | return decayed->getOriginalType(); |
| 271 | } |
| 272 | return type; |
| 273 | } |
| 274 | |
| 275 | bool SameType(clang::QualType type1, clang::QualType type2) { |
| 276 | // If both types are an AutoType, ignore the actual type and assume they're |
| 277 | // the same. |
| 278 | // An `AutoType` that came from a TypeLoc will have type `auto` (i.e. as |
| 279 | // written), whereas an `AutoType` that didn't come from a `TypeLoc` will be |
| 280 | // the actual deduced type. We still want these to compare equal though. |
| 281 | if (type1->getAs<clang::AutoType>() && type2->getAs<clang::AutoType>()) { |
| 282 | return true; |
| 283 | } |
| 284 | return Undecay(type1) == Undecay(type2); |
| 285 | } |
| 286 | |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 287 | } // namespace |
| 288 | |
Luca Versari | 91a56ff | 2022-08-22 01:58:33 -0700 | [diff] [blame] | 289 | ValueLifetimes ValueLifetimes::PointerTo(const clang::QualType pointer_type, |
| 290 | const ObjectLifetimes& obj) { |
| 291 | ValueLifetimes ret; |
| 292 | ret.type_ = pointer_type; |
| 293 | assert(pointer_type->getPointeeType().getCanonicalType() == |
| 294 | obj.Type().getCanonicalType()); |
| 295 | ret.pointee_lifetimes_ = std::make_unique<ObjectLifetimes>(obj); |
| 296 | return ret; |
| 297 | } |
| 298 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 299 | llvm::Expected<ValueLifetimes> ValueLifetimes::Create( |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 300 | clang::QualType type, clang::TypeLoc type_loc, |
| 301 | LifetimeFactory lifetime_factory) { |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 302 | assert(!type.isNull()); |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 303 | if (type_loc) { |
| 304 | assert(SameType(type_loc.getType(), type)); |
| 305 | } |
| 306 | |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 307 | type = type.IgnoreParens(); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 308 | |
| 309 | type = StripAttributes(type); |
| 310 | llvm::SmallVector<const clang::Attr*> attrs; |
| 311 | if (!type_loc.isNull()) { |
| 312 | type_loc = StripAttributes(type_loc, attrs); |
| 313 | } |
Martin Brænne | 8bc2de3 | 2022-06-24 07:01:52 -0700 | [diff] [blame] | 314 | llvm::SmallVector<const clang::Expr*> lifetime_names; |
| 315 | if (llvm::Error err = GetAttributeLifetimes(attrs).moveInto(lifetime_names)) { |
| 316 | return std::move(err); |
| 317 | } |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 318 | |
Googler | 6a06dbc | 2021-12-16 15:25:02 +0000 | [diff] [blame] | 319 | ValueLifetimes ret(type); |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 320 | |
Martin Brænne | fd2d10f | 2023-07-10 04:00:55 -0700 | [diff] [blame] | 321 | if (const auto* fn = type->getAs<clang::FunctionProtoType>()) { |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 322 | // TODO(veluca): this will not correctly handle the distinction between |
| 323 | // parameter and return lifetimes. |
| 324 | FunctionLifetimeFactorySingleCallback factory(lifetime_factory); |
| 325 | FunctionLifetimes fn_lftm; |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 326 | if (llvm::Error err = |
| 327 | FunctionLifetimes::CreateForFunctionType(fn, type_loc, factory) |
| 328 | .moveInto(fn_lftm)) { |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 329 | return std::move(err); |
| 330 | } |
| 331 | ret.function_lifetimes_ = |
| 332 | std::make_unique<FunctionLifetimes>(std::move(fn_lftm)); |
| 333 | return ret; |
| 334 | } |
| 335 | |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 336 | llvm::SmallVector<std::string> lifetime_params = GetLifetimeParameters(type); |
| 337 | if (!lifetime_params.empty() && !lifetime_names.empty() && |
| 338 | lifetime_names.size() != lifetime_params.size()) { |
Martin Brænne | 198ff49 | 2023-04-05 01:41:25 -0700 | [diff] [blame] | 339 | return llvm::make_error<LifetimeError>( |
| 340 | LifetimeError::Type::Other, |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 341 | absl::StrCat("Type has ", lifetime_params.size(), |
| 342 | " lifetime parameters but ", lifetime_names.size(), |
| 343 | " lifetime arguments were given")); |
| 344 | } |
| 345 | for (size_t i = 0; i < lifetime_params.size(); ++i) { |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 346 | Lifetime l; |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 347 | const clang::Expr* lifetime_name = nullptr; |
| 348 | if (i < lifetime_names.size()) { |
| 349 | lifetime_name = lifetime_names[i]; |
| 350 | } |
| 351 | if (llvm::Error err = lifetime_factory(lifetime_name).moveInto(l)) { |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 352 | return std::move(err); |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 353 | } |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 354 | ret.lifetime_parameters_by_name_.Add(lifetime_params[i], l); |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 357 | // Add implicit lifetime parameters for type template parameters. |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 358 | if (llvm::Error err = ForEachTemplateArgument( |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 359 | type, type_loc, |
| 360 | [&ret, &lifetime_factory]( |
| 361 | int depth, clang::QualType arg_type, |
| 362 | clang::TypeLoc arg_type_loc) -> llvm::Error { |
Martin Brænne | 16d86dd | 2022-04-08 00:33:36 -0700 | [diff] [blame] | 363 | std::optional<ValueLifetimes> maybe_template_arg_lifetime; |
| 364 | if (!arg_type.isNull()) { |
| 365 | maybe_template_arg_lifetime.emplace(); |
| 366 | if (llvm::Error err = |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 367 | ValueLifetimes::Create(arg_type, arg_type_loc, |
| 368 | lifetime_factory) |
Martin Brænne | 16d86dd | 2022-04-08 00:33:36 -0700 | [diff] [blame] | 369 | .moveInto(*maybe_template_arg_lifetime)) { |
| 370 | return err; |
| 371 | } |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 372 | } |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 373 | if (ret.template_argument_lifetimes_.size() <= depth) { |
| 374 | ret.template_argument_lifetimes_.resize(depth + 1); |
| 375 | } |
| 376 | ret.template_argument_lifetimes_[depth].push_back( |
Martin Brænne | 16d86dd | 2022-04-08 00:33:36 -0700 | [diff] [blame] | 377 | maybe_template_arg_lifetime); |
Luca Versari | af14c37 | 2022-04-04 07:30:24 -0700 | [diff] [blame] | 378 | return llvm::Error::success(); |
| 379 | })) { |
| 380 | return std::move(err); |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 383 | clang::QualType pointee = PointeeType(type); |
Martin Brænne | afad7ca | 2023-03-30 06:29:31 -0700 | [diff] [blame] | 384 | if (pointee.isNull() || type->isFunctionPointerType() || |
| 385 | type->isFunctionReferenceType()) { |
Martin Brænne | 2c6f94b | 2023-03-29 05:24:56 -0700 | [diff] [blame] | 386 | if (lifetime_params.empty() && !lifetime_names.empty()) |
Martin Brænne | 198ff49 | 2023-04-05 01:41:25 -0700 | [diff] [blame] | 387 | return llvm::make_error<LifetimeError>( |
| 388 | LifetimeError::Type::Other, |
Martin Brænne | 2c6f94b | 2023-03-29 05:24:56 -0700 | [diff] [blame] | 389 | absl::StrCat("Type may not be annotated with lifetimes")); |
Martin Brænne | afad7ca | 2023-03-30 06:29:31 -0700 | [diff] [blame] | 390 | } |
| 391 | if (pointee.isNull()) { |
Martin Brænne | 2c6f94b | 2023-03-29 05:24:56 -0700 | [diff] [blame] | 392 | return ret; |
| 393 | } |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 394 | |
| 395 | clang::TypeLoc pointee_type_loc; |
| 396 | if (type_loc) { |
| 397 | pointee_type_loc = PointeeTypeLoc(type_loc); |
| 398 | // Note: We can't assert that `pointee_type_loc` is non-null here. If |
| 399 | // `type_loc` is a `TypedefTypeLoc`, then there will be no `TypeLoc` for |
| 400 | // the pointee type because the pointee type never got spelled out at the |
| 401 | // location of the original `TypeLoc`. |
| 402 | } |
| 403 | |
| 404 | ValueLifetimes value_lifetimes; |
| 405 | if (llvm::Error err = |
| 406 | ValueLifetimes::Create(pointee, pointee_type_loc, lifetime_factory) |
| 407 | .moveInto(value_lifetimes)) { |
| 408 | return std::move(err); |
| 409 | } |
| 410 | |
| 411 | Lifetime object_lifetime; |
Martin Brænne | afad7ca | 2023-03-30 06:29:31 -0700 | [diff] [blame] | 412 | if (type->isFunctionPointerType() || type->isFunctionReferenceType()) { |
| 413 | // Function pointers and function references may not be annotated with a |
| 414 | // lifetime. Instead, it is always implied that they have static lifetime. |
| 415 | object_lifetime = Lifetime::Static(); |
| 416 | } else { |
| 417 | const clang::Expr* lifetime_name = nullptr; |
| 418 | if (!lifetime_names.empty()) { |
| 419 | if (lifetime_names.size() != 1) { |
Martin Brænne | 198ff49 | 2023-04-05 01:41:25 -0700 | [diff] [blame] | 420 | return llvm::make_error<LifetimeError>( |
| 421 | LifetimeError::Type::Other, |
Martin Brænne | afad7ca | 2023-03-30 06:29:31 -0700 | [diff] [blame] | 422 | absl::StrCat("Expected a single lifetime but ", |
| 423 | lifetime_names.size(), " were given")); |
| 424 | } |
| 425 | lifetime_name = lifetime_names.front(); |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 426 | } |
Martin Brænne | afad7ca | 2023-03-30 06:29:31 -0700 | [diff] [blame] | 427 | if (llvm::Error err = |
| 428 | lifetime_factory(lifetime_name).moveInto(object_lifetime)) { |
| 429 | return std::move(err); |
| 430 | } |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 431 | } |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 432 | ret.pointee_lifetimes_ = |
Martin Brænne | 712b7f4 | 2022-06-23 00:55:17 -0700 | [diff] [blame] | 433 | std::make_unique<ObjectLifetimes>(object_lifetime, value_lifetimes); |
Googler | c071e24 | 2021-12-16 15:23:41 +0000 | [diff] [blame] | 434 | return ret; |
| 435 | } |
| 436 | |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 437 | ValueLifetimes ValueLifetimes::ForLifetimeLessType(clang::QualType type) { |
Googler | fc1b55c | 2022-03-04 13:03:46 +0000 | [diff] [blame] | 438 | assert(PointeeType(type).isNull() && !type->isRecordType()); |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 439 | return ValueLifetimes(type); |
| 440 | } |
| 441 | |
| 442 | ValueLifetimes ValueLifetimes::ForPointerLikeType( |
| 443 | clang::QualType type, const ObjectLifetimes& object_lifetimes) { |
Googler | fc1b55c | 2022-03-04 13:03:46 +0000 | [diff] [blame] | 444 | assert(!PointeeType(type).isNull()); |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 445 | ValueLifetimes result(type); |
| 446 | result.pointee_lifetimes_ = |
| 447 | std::make_unique<ObjectLifetimes>(object_lifetimes); |
| 448 | return result; |
| 449 | } |
| 450 | |
| 451 | ValueLifetimes ValueLifetimes::ForRecord( |
| 452 | clang::QualType type, |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 453 | std::vector<std::vector<std::optional<ValueLifetimes>>> |
| 454 | template_argument_lifetimes, |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 455 | LifetimeSymbolTable lifetime_parameters) { |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 456 | assert(type->isRecordType()); |
| 457 | assert(GetTemplateArgs(type).size() == template_argument_lifetimes.size()); |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 458 | for (size_t depth = 0; depth < template_argument_lifetimes.size(); ++depth) { |
| 459 | assert(GetTemplateArgs(type)[depth].size() == |
| 460 | template_argument_lifetimes[depth].size()); |
| 461 | } |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 462 | ValueLifetimes result(type); |
| 463 | result.template_argument_lifetimes_ = std::move(template_argument_lifetimes); |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 464 | result.lifetime_parameters_by_name_ = lifetime_parameters; |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 465 | return result; |
| 466 | } |
| 467 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 468 | std::string ValueLifetimes::DebugString( |
| 469 | const LifetimeFormatter& formatter) const { |
Martin Brænne | afad7ca | 2023-03-30 06:29:31 -0700 | [diff] [blame] | 470 | if (Type()->isFunctionPointerType() || Type()->isFunctionReferenceType()) { |
| 471 | // Function pointers and function references have an implied static |
| 472 | // lifetime. This is never annotated, so don't print it either. |
| 473 | // Note: If at some point we decide we want to distinguish between regular |
| 474 | // pointers (to objects) and function pointers in more places, we could |
| 475 | // consider adding a `function_pointee_lifetimes_` in addition to |
| 476 | // `pointee_lifetimes_`. |
| 477 | assert(pointee_lifetimes_->GetLifetime() == Lifetime::Static()); |
| 478 | return pointee_lifetimes_->GetValueLifetimes().DebugString(formatter); |
| 479 | } |
Luca Versari | 6ee6b34 | 2022-04-05 05:40:52 -0700 | [diff] [blame] | 480 | if (!PointeeType(Type()).isNull()) { |
| 481 | assert(pointee_lifetimes_); |
| 482 | return pointee_lifetimes_->DebugString(formatter); |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 483 | } |
Martin Brænne | fd2d10f | 2023-07-10 04:00:55 -0700 | [diff] [blame] | 484 | if (Type()->getAs<clang::FunctionProtoType>()) { |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 485 | assert(function_lifetimes_); |
| 486 | std::string fn_lifetimes = function_lifetimes_->DebugString(formatter); |
| 487 | if (fn_lifetimes.empty()) return ""; |
| 488 | return absl::StrCat("(", fn_lifetimes, ")"); |
| 489 | } |
Luca Versari | 6ee6b34 | 2022-04-05 05:40:52 -0700 | [diff] [blame] | 490 | |
| 491 | std::vector<std::vector<std::string>> tmpl_lifetimes; |
| 492 | for (auto& tmpl_arg_at_depth : template_argument_lifetimes_) { |
| 493 | tmpl_lifetimes.emplace_back(); |
| 494 | for (const std::optional<ValueLifetimes>& tmpl_arg : tmpl_arg_at_depth) { |
| 495 | if (tmpl_arg) { |
| 496 | std::string inner = tmpl_arg->DebugString(formatter); |
| 497 | if (!inner.empty()) { |
| 498 | tmpl_lifetimes.back().push_back(std::move(inner)); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | std::vector<std::string> lifetime_parameters; |
| 504 | for (const auto& lftm_arg : GetLifetimeParameters(type_)) { |
| 505 | std::optional<Lifetime> lifetime = |
| 506 | lifetime_parameters_by_name_.LookupName(lftm_arg); |
| 507 | assert(lifetime.has_value()); |
| 508 | lifetime_parameters.push_back(formatter(*lifetime)); |
| 509 | } |
| 510 | bool empty_tmpl_lifetimes = true; |
| 511 | for (const auto& tmpl_arg_at_depth : tmpl_lifetimes) { |
| 512 | for (const auto& tmpl : tmpl_arg_at_depth) { |
| 513 | empty_tmpl_lifetimes &= tmpl.empty(); |
| 514 | } |
| 515 | } |
| 516 | if (empty_tmpl_lifetimes && lifetime_parameters.empty()) { |
| 517 | return ""; |
| 518 | } else if (empty_tmpl_lifetimes) { |
| 519 | if (lifetime_parameters.size() == 1) { |
| 520 | return lifetime_parameters[0]; |
| 521 | } |
| 522 | return absl::StrCat("[", absl::StrJoin(lifetime_parameters, ", "), "]"); |
| 523 | } else if (lifetime_parameters.empty() && tmpl_lifetimes.size() == 1 && |
| 524 | tmpl_lifetimes[0].size() == 1) { |
| 525 | return tmpl_lifetimes[0][0]; |
| 526 | } |
| 527 | |
| 528 | std::vector<std::string> tmpl_lifetimes_per_depth; |
| 529 | tmpl_lifetimes_per_depth.reserve(tmpl_lifetimes.size()); |
| 530 | for (const auto& tmpl_depth : tmpl_lifetimes) { |
| 531 | tmpl_lifetimes_per_depth.push_back( |
| 532 | absl::StrCat("<", absl::StrJoin(tmpl_depth, ", "), ">")); |
| 533 | } |
| 534 | std::string lifetime_parameter_string; |
| 535 | if (!lifetime_parameters.empty()) { |
| 536 | lifetime_parameter_string = |
| 537 | absl::StrCat(" [", absl::StrJoin(lifetime_parameters, ", "), "]"); |
| 538 | } |
| 539 | |
| 540 | return absl::StrCat(absl::StrJoin(tmpl_lifetimes_per_depth, "::"), |
| 541 | lifetime_parameter_string); |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Googler | 41f3e35 | 2021-12-16 15:24:21 +0000 | [diff] [blame] | 544 | const ObjectLifetimes& ValueLifetimes::GetPointeeLifetimes() const { |
Googler | fc1b55c | 2022-03-04 13:03:46 +0000 | [diff] [blame] | 545 | assert(!PointeeType(type_).isNull()); |
Martin Brænne | fd2d10f | 2023-07-10 04:00:55 -0700 | [diff] [blame] | 546 | assert(pointee_lifetimes_); |
Googler | 41f3e35 | 2021-12-16 15:24:21 +0000 | [diff] [blame] | 547 | return *pointee_lifetimes_; |
| 548 | } |
| 549 | |
Luca Versari | 5082488 | 2023-01-23 05:51:35 -0800 | [diff] [blame] | 550 | const FunctionLifetimes& ValueLifetimes::GetFuncLifetimes() const { |
| 551 | assert(clang::isa<clang::FunctionProtoType>(type_)); |
Martin Brænne | 83bda99 | 2023-07-10 12:03:12 -0700 | [diff] [blame] | 552 | assert(function_lifetimes_); |
Luca Versari | 5082488 | 2023-01-23 05:51:35 -0800 | [diff] [blame] | 553 | return *function_lifetimes_; |
| 554 | } |
| 555 | |
Luca Versari | 043a36e | 2022-04-08 00:55:36 -0700 | [diff] [blame] | 556 | bool ValueLifetimes::HasAny( |
| 557 | const std::function<bool(Lifetime)>& predicate) const { |
| 558 | for (const auto& tmpl_arg_at_depth : template_argument_lifetimes_) { |
| 559 | for (const std::optional<ValueLifetimes>& tmpl_arg : tmpl_arg_at_depth) { |
| 560 | if (tmpl_arg && tmpl_arg->HasAny(predicate)) { |
| 561 | return true; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | if (pointee_lifetimes_ && pointee_lifetimes_->HasAny(predicate)) { |
| 566 | return true; |
| 567 | } |
| 568 | for (const auto& lftm_arg : GetLifetimeParameters(type_)) { |
| 569 | std::optional<Lifetime> lifetime = |
| 570 | lifetime_parameters_by_name_.LookupName(lftm_arg); |
| 571 | assert(lifetime.has_value()); |
| 572 | if (predicate(lifetime.value())) { |
| 573 | return true; |
| 574 | } |
| 575 | } |
| 576 | if (function_lifetimes_ && function_lifetimes_->HasAny(predicate)) { |
| 577 | return true; |
| 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | |
Luca Versari | c523163 | 2022-04-11 07:36:35 -0700 | [diff] [blame] | 582 | void ValueLifetimes::SubstituteLifetimes(const LifetimeSubstitutions& subst) { |
| 583 | for (auto& tmpl_arg_at_depth : template_argument_lifetimes_) { |
| 584 | for (std::optional<ValueLifetimes>& tmpl_arg : tmpl_arg_at_depth) { |
| 585 | if (tmpl_arg) { |
| 586 | tmpl_arg->SubstituteLifetimes(subst); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | if (pointee_lifetimes_) { |
| 591 | pointee_lifetimes_->SubstituteLifetimes(subst); |
| 592 | } |
| 593 | for (const auto& lftm_arg : GetLifetimeParameters(type_)) { |
| 594 | std::optional<Lifetime> lifetime = |
| 595 | lifetime_parameters_by_name_.LookupName(lftm_arg); |
| 596 | assert(lifetime.has_value()); |
| 597 | lifetime_parameters_by_name_.Rebind(lftm_arg, subst.Substitute(*lifetime)); |
| 598 | } |
| 599 | if (function_lifetimes_) { |
| 600 | function_lifetimes_->SubstituteLifetimes(subst); |
| 601 | } |
| 602 | } |
| 603 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 604 | void ValueLifetimes::Traverse(std::function<void(Lifetime&, Variance)> visitor, |
| 605 | Variance variance) { |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 606 | for (auto& tmpl_arg_at_depth : template_argument_lifetimes_) { |
| 607 | for (std::optional<ValueLifetimes>& tmpl_arg : tmpl_arg_at_depth) { |
| 608 | if (tmpl_arg) { |
| 609 | tmpl_arg->Traverse(visitor, kInvariant); |
| 610 | } |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | if (pointee_lifetimes_) { |
| 614 | pointee_lifetimes_->Traverse(visitor, variance, Type()); |
| 615 | } |
| 616 | for (const auto& lftm_arg : GetLifetimeParameters(type_)) { |
| 617 | std::optional<Lifetime> lifetime = |
| 618 | lifetime_parameters_by_name_.LookupName(lftm_arg); |
| 619 | assert(lifetime.has_value()); |
| 620 | Lifetime new_lifetime = *lifetime; |
| 621 | visitor(new_lifetime, variance); |
| 622 | |
| 623 | // Note that this check is not an optimization, but a guard against UB: |
| 624 | // if one calls the const version of Traverse, with a callback that does not |
| 625 | // mutate the lifetime, on a const instance of the Value/ObjectLifetimes, |
| 626 | // then calling Rebind would be UB, even if Rebind would do nothing in |
| 627 | // practice. |
| 628 | if (new_lifetime != lifetime) { |
| 629 | lifetime_parameters_by_name_.Rebind(lftm_arg, new_lifetime); |
| 630 | } |
| 631 | } |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 632 | if (function_lifetimes_) { |
| 633 | function_lifetimes_->Traverse(visitor); |
| 634 | } |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 637 | void ValueLifetimes::Traverse( |
| 638 | std::function<void(const Lifetime&, Variance)> visitor, |
| 639 | Variance variance) const { |
| 640 | const_cast<ValueLifetimes*>(this)->Traverse( |
| 641 | [&visitor](Lifetime& l, Variance v) { visitor(l, v); }, variance); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Luca Versari | cc7e6cb | 2022-04-05 08:08:23 -0700 | [diff] [blame] | 644 | ValueLifetimes::ValueLifetimes(clang::QualType type) : type_(type) {} |
| 645 | |
Dmitri Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame] | 646 | llvm::SmallVector<llvm::ArrayRef<clang::TemplateArgument>> GetTemplateArgs( |
| 647 | clang::QualType type) { |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 648 | llvm::SmallVector<llvm::ArrayRef<clang::TemplateArgument>> result; |
| 649 | |
Martin Brænne | 5cc52fc | 2022-04-20 05:42:30 -0700 | [diff] [blame] | 650 | // Desugar any typedefs on `type`. We need to do this so that the "depth" of |
| 651 | // the template arguments is compatible with |
| 652 | // TemplateTypeParmType::getDepth(), which likewise assumes that typedefs are |
| 653 | // desugared; see the call to getCanonicalTypeInternal() in |
| 654 | // TemplateTypeParmType::getCanTTPTInfo(). Unlike that function, we can't |
| 655 | // simply canonicalize the type, as that would also remove type annotations, |
| 656 | // and we need those. |
| 657 | while (const auto* typedef_type = type->getAs<clang::TypedefType>()) { |
| 658 | type = typedef_type->desugar(); |
| 659 | } |
| 660 | |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 661 | if (auto elaborated = type->getAs<clang::ElaboratedType>()) { |
| 662 | if (clang::NestedNameSpecifier* qualifier = elaborated->getQualifier()) { |
| 663 | if (const clang::Type* qualifier_type = qualifier->getAsType()) { |
| 664 | result = GetTemplateArgs(clang::QualType(qualifier_type, 0)); |
| 665 | } |
| 666 | } |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 667 | } |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 668 | |
| 669 | if (auto specialization = type->getAs<clang::TemplateSpecializationType>()) { |
| 670 | result.push_back(specialization->template_arguments()); |
| 671 | } else if (auto record = type->getAs<clang::RecordType>()) { |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 672 | if (auto specialization_decl = |
| 673 | clang::dyn_cast<clang::ClassTemplateSpecializationDecl>( |
| 674 | record->getDecl())) { |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 675 | result.push_back(specialization_decl->getTemplateArgs().asArray()); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 678 | |
| 679 | return result; |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Martin Brænne | 4640933 | 2022-06-23 00:45:49 -0700 | [diff] [blame] | 682 | std::optional<llvm::SmallVector<llvm::SmallVector<clang::TypeLoc>>> |
Fangrui Song | aff93e4 | 2024-05-15 13:48:34 -0700 | [diff] [blame] | 683 | GetTemplateArgs(llvm::ArrayRef<clang::TemplateArgumentLoc> template_arg_locs) { |
| 684 | llvm::SmallVector<llvm::SmallVector<clang::TypeLoc>> args; |
| 685 | args.push_back({}); |
| 686 | for (const auto &loc: template_arg_locs) { |
| 687 | if (auto* source_info = loc.getTypeSourceInfo()) { |
| 688 | args.back().push_back(source_info->getTypeLoc()); |
| 689 | } else { |
| 690 | args.back().push_back(clang::TypeLoc()); |
| 691 | } |
| 692 | } |
| 693 | return args; |
| 694 | } |
| 695 | |
| 696 | std::optional<llvm::SmallVector<llvm::SmallVector<clang::TypeLoc>>> |
Martin Brænne | 4640933 | 2022-06-23 00:45:49 -0700 | [diff] [blame] | 697 | GetTemplateArgs(clang::TypeLoc type_loc) { |
| 698 | assert(type_loc); |
| 699 | |
| 700 | type_loc = type_loc.getUnqualifiedLoc(); |
| 701 | |
| 702 | // We can't get template arguments from a `SubstTemplateTypeParmTypeLoc` |
| 703 | // because there is no `TypeLoc` for the type that replaces the template |
Martin Brænne | e1fa519 | 2023-03-31 05:22:26 -0700 | [diff] [blame] | 704 | // parameter. |
| 705 | if (type_loc.getAs<clang::SubstTemplateTypeParmTypeLoc>()) { |
Martin Brænne | 4640933 | 2022-06-23 00:45:49 -0700 | [diff] [blame] | 706 | assert(!type_loc.getNextTypeLoc()); |
| 707 | return std::nullopt; |
| 708 | } |
| 709 | |
Martin Brænne | e1fa519 | 2023-03-31 05:22:26 -0700 | [diff] [blame] | 710 | // Similarly, there is no `TypeLoc` for the type that a `TypedefType` resolves |
| 711 | // to. Note that we intentionally use Type::getAs<TypedefType>() instead of |
| 712 | // TypeLoc::getAs<TypedefTypeLoc>() here because the former will strip off any |
| 713 | // potential other layers of sugar to get to the `TypedefType`, and we want to |
| 714 | // be consistent with `GetTemplateArgs<QualType>`, which obviously uses |
| 715 | // `Type::getAs<TypedefType>()`, too. |
| 716 | if (type_loc.getType()->getAs<clang::TypedefType>() != nullptr) { |
| 717 | return std::nullopt; |
| 718 | } |
| 719 | |
Jing Lu | 1cec4a8 | 2023-12-12 09:28:25 -0800 | [diff] [blame] | 720 | // Again, there is no `TypeLoc` for the type that a `DecltypeType` or |
| 721 | // `AutoType` refers to. |
| 722 | if (type_loc.getType()->getAs<clang::DecltypeType>() != nullptr || |
| 723 | type_loc.getType()->getAs<clang::AutoType>() != nullptr) { |
Jing Lu | c6c47e0 | 2023-12-12 09:07:02 -0800 | [diff] [blame] | 724 | return std::nullopt; |
| 725 | } |
| 726 | |
Martin Brænne | 43be128 | 2022-06-23 00:24:44 -0700 | [diff] [blame] | 727 | llvm::SmallVector<llvm::SmallVector<clang::TypeLoc>> args; |
| 728 | |
| 729 | if (auto elaborated_type_loc = type_loc.getAs<clang::ElaboratedTypeLoc>()) { |
| 730 | if (clang::NestedNameSpecifierLoc qualifier = |
| 731 | elaborated_type_loc.getQualifierLoc()) { |
Martin Brænne | 4640933 | 2022-06-23 00:45:49 -0700 | [diff] [blame] | 732 | if (qualifier.getTypeLoc()) { |
| 733 | if (auto qualifier_args = GetTemplateArgs(qualifier.getTypeLoc())) { |
| 734 | args = *qualifier_args; |
| 735 | } else { |
| 736 | return std::nullopt; |
| 737 | } |
| 738 | } |
Martin Brænne | 43be128 | 2022-06-23 00:24:44 -0700 | [diff] [blame] | 739 | } |
Martin Brænne | 4640933 | 2022-06-23 00:45:49 -0700 | [diff] [blame] | 740 | if (auto elaborated_args = |
| 741 | GetTemplateArgs(elaborated_type_loc.getNamedTypeLoc())) { |
| 742 | args.append(*elaborated_args); |
| 743 | } else { |
| 744 | return std::nullopt; |
| 745 | } |
Martin Brænne | 43be128 | 2022-06-23 00:24:44 -0700 | [diff] [blame] | 746 | } else if (auto template_specialization_type_loc = |
| 747 | type_loc.getAs<clang::TemplateSpecializationTypeLoc>()) { |
| 748 | args.push_back({}); |
| 749 | for (unsigned i = 0; i < template_specialization_type_loc.getNumArgs(); |
| 750 | ++i) { |
Kinuko Yasuda | 74d5a3c | 2022-08-29 14:40:04 -0700 | [diff] [blame] | 751 | if (auto* source_info = template_specialization_type_loc.getArgLoc(i) |
| 752 | .getTypeSourceInfo()) { |
| 753 | args.back().push_back(source_info->getTypeLoc()); |
| 754 | } else { |
| 755 | args.back().push_back(clang::TypeLoc()); |
| 756 | } |
Martin Brænne | 43be128 | 2022-06-23 00:24:44 -0700 | [diff] [blame] | 757 | } |
Martin Brænne | 4640933 | 2022-06-23 00:45:49 -0700 | [diff] [blame] | 758 | } else if (auto record_type_loc = type_loc.getAs<clang::RecordTypeLoc>()) { |
| 759 | if (auto specialization_decl = |
| 760 | clang::dyn_cast<clang::ClassTemplateSpecializationDecl>( |
| 761 | record_type_loc.getDecl())) { |
Fangrui Song | aff93e4 | 2024-05-15 13:48:34 -0700 | [diff] [blame] | 762 | if (specialization_decl->getTemplateArgsAsWritten()) { |
Martin Brænne | 4640933 | 2022-06-23 00:45:49 -0700 | [diff] [blame] | 763 | return GetTemplateArgs( |
Fangrui Song | aff93e4 | 2024-05-15 13:48:34 -0700 | [diff] [blame] | 764 | specialization_decl->getTemplateArgsAsWritten()->arguments()); |
Martin Brænne | 4640933 | 2022-06-23 00:45:49 -0700 | [diff] [blame] | 765 | } |
| 766 | return std::nullopt; |
| 767 | } |
Martin Brænne | 43be128 | 2022-06-23 00:24:44 -0700 | [diff] [blame] | 768 | } else if (auto dependent_template_specialization_type_loc = |
| 769 | type_loc |
| 770 | .getAs<clang::DependentTemplateSpecializationTypeLoc>()) { |
| 771 | args.push_back({}); |
Martin Brænne | fa6fc84 | 2022-06-23 00:25:43 -0700 | [diff] [blame] | 772 | for (unsigned i = 0; |
| 773 | i < dependent_template_specialization_type_loc.getNumArgs(); ++i) { |
| 774 | args.back().push_back( |
| 775 | dependent_template_specialization_type_loc.getArgLoc(i) |
| 776 | .getTypeSourceInfo() |
| 777 | ->getTypeLoc()); |
| 778 | } |
Martin Brænne | 43be128 | 2022-06-23 00:24:44 -0700 | [diff] [blame] | 779 | // TODO(mboehme): Where does this occur exactly? Do we need to be handling |
| 780 | // it? |
| 781 | // AFAICT, this happens if we're looking at a dependent template name |
| 782 | // (https://en.cppreference.com/w/cpp/language/dependent_name), which |
| 783 | // probably means that this can only happen in template definitions (as |
| 784 | // opposed to template instantiations), and we aren't analyzing those for |
| 785 | // now. At the least, I haven't been able to trigger this case from a test. |
| 786 | // Triggering a fatal error here so that we notice this case if it does come |
Martin Brænne | fa6fc84 | 2022-06-23 00:25:43 -0700 | [diff] [blame] | 787 | // up. We only trigger the fatal error _after_ we've done the processing |
| 788 | // above so that we don't get a warning about dead code. |
Martin Brænne | 43be128 | 2022-06-23 00:24:44 -0700 | [diff] [blame] | 789 | llvm::report_fatal_error( |
| 790 | "Unexpectedly got a DependentSpecializationTypeLoc"); |
Martin Brænne | 43be128 | 2022-06-23 00:24:44 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | return args; |
| 794 | } |
| 795 | |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 796 | ObjectLifetimes ObjectLifetimes::GetObjectLifetimesForTypeInContext( |
| 797 | clang::QualType type, llvm::SmallVector<std::string> type_lifetime_args, |
| 798 | llvm::StringRef object_lifetime_parameter) const { |
Googler | 41f3e35 | 2021-12-16 15:24:21 +0000 | [diff] [blame] | 799 | assert(value_lifetimes_.Type()->isRecordType()); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 800 | |
| 801 | // The object of the `type` (i.e. field or a base class) basically has the |
| 802 | // same lifetime as the struct. |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 803 | Lifetime ret_lifetime = lifetime_; |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 804 | // ... unless the field has lifetime parameters. |
| 805 | if (!object_lifetime_parameter.empty()) { |
| 806 | ret_lifetime = |
| 807 | value_lifetimes_.GetLifetimeParameter(object_lifetime_parameter); |
| 808 | } |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 809 | |
| 810 | // `type` is one of a template argument, a struct, a pointer, or a type |
| 811 | // with no lifetimes (other than its own). |
| 812 | |
| 813 | // First case: template argument. We just attach the |
| 814 | // template argument's lifetimes to the leaf ObjectLifetimes. |
| 815 | if (auto targ = type->getAs<clang::SubstTemplateTypeParmType>()) { |
Googler | 59be2f9 | 2022-10-17 06:45:57 -0700 | [diff] [blame] | 816 | const clang::TemplateTypeParmDecl* type_parm = targ->getReplacedParameter(); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 817 | const std::optional<ValueLifetimes>& arg_lifetimes = |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 818 | value_lifetimes_.GetTemplateArgumentLifetimes(type_parm->getDepth(), |
| 819 | type_parm->getIndex()); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 820 | if (!arg_lifetimes) { |
| 821 | assert(false); |
Googler | 6a06dbc | 2021-12-16 15:25:02 +0000 | [diff] [blame] | 822 | return llvm::DenseMapInfo<ObjectLifetimes>::getEmptyKey(); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 823 | } |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 824 | return ObjectLifetimes(ret_lifetime, *arg_lifetimes); |
Googler | f1009fd | 2023-05-03 06:29:04 -0700 | [diff] [blame] | 825 | } else if (type->isRecordType()) { |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 826 | // Second case: struct. |
| 827 | // Resolve lifetime parameters for the struct, if it has any. |
| 828 | LifetimeSymbolTable lifetime_params; |
Googler | 247cef7 | 2022-03-02 15:13:38 +0000 | [diff] [blame] | 829 | llvm::SmallVector<std::string> params = GetLifetimeParameters(type); |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 830 | for (size_t i = params.size(); i-- > 0;) { |
| 831 | assert(!type_lifetime_args.empty()); |
| 832 | auto lftm_arg = type_lifetime_args.back(); |
| 833 | type_lifetime_args.pop_back(); |
| 834 | lifetime_params.Add(params[i], |
| 835 | value_lifetimes_.GetLifetimeParameter(lftm_arg)); |
| 836 | } |
| 837 | |
Googler | f1009fd | 2023-05-03 06:29:04 -0700 | [diff] [blame] | 838 | // We need to construct potentially reshuffled |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 839 | // template arguments, if the struct is a template. |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 840 | |
| 841 | // TODO(veluca): mixing lifetimes and template parameters is not supported |
| 842 | // yet. |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 843 | std::vector<std::vector<std::optional<ValueLifetimes>>> |
| 844 | template_argument_lifetimes; |
| 845 | for (const auto& args_at_depth : GetTemplateArgs(type)) { |
| 846 | size_t parameter_pack_expansion_index = 0; |
| 847 | template_argument_lifetimes.push_back({}); |
| 848 | for (const clang::TemplateArgument& arg : args_at_depth) { |
| 849 | if (arg.getKind() == clang::TemplateArgument::Type) { |
| 850 | if (auto templ_arg = |
| 851 | clang::dyn_cast<clang::SubstTemplateTypeParmType>( |
| 852 | arg.getAsType())) { |
Googler | 59be2f9 | 2022-10-17 06:45:57 -0700 | [diff] [blame] | 853 | const clang::TemplateTypeParmDecl* type_parm = |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 854 | templ_arg->getReplacedParameter(); |
| 855 | // Template parameter packs get the index of the *pack*, not the |
| 856 | // index of the type inside the pack itself. As they must appear |
| 857 | // last, we can just increase the counter at every occurrence and |
| 858 | // wraparound when we run out of template arguments. |
| 859 | size_t index = type_parm->getIndex(); |
| 860 | if (type_parm->isParameterPack()) { |
| 861 | index += parameter_pack_expansion_index++; |
| 862 | if (index + 1 >= value_lifetimes_.GetNumTemplateArgumentsAtDepth( |
| 863 | type_parm->getDepth())) { |
| 864 | parameter_pack_expansion_index = 0; |
| 865 | } |
Luca Versari | 29c0086 | 2022-01-21 20:53:06 +0000 | [diff] [blame] | 866 | } |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 867 | template_argument_lifetimes.back().push_back( |
| 868 | value_lifetimes_.GetTemplateArgumentLifetimes( |
| 869 | type_parm->getDepth(), index)); |
| 870 | } else { |
| 871 | // Create a new ValueLifetimes of the type of the template |
| 872 | // parameter, with lifetime `lifetime_`. |
| 873 | // TODO(veluca): we need to propagate lifetime parameters here. |
| 874 | template_argument_lifetimes.back().push_back( |
Martin Brænne | 03d9330 | 2022-06-23 00:23:38 -0700 | [diff] [blame] | 875 | ValueLifetimes::Create( |
| 876 | arg.getAsType(), |
| 877 | [this](const clang::Expr*) { return this->lifetime_; }) |
| 878 | .get()); |
Luca Versari | 29c0086 | 2022-01-21 20:53:06 +0000 | [diff] [blame] | 879 | } |
Luca Versari | 162a4c5 | 2021-12-10 08:57:05 +0000 | [diff] [blame] | 880 | } else { |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 881 | template_argument_lifetimes.back().push_back(std::nullopt); |
Luca Versari | 162a4c5 | 2021-12-10 08:57:05 +0000 | [diff] [blame] | 882 | } |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 885 | |
| 886 | return ObjectLifetimes( |
| 887 | ret_lifetime, |
| 888 | ValueLifetimes::ForRecord(type, std::move(template_argument_lifetimes), |
| 889 | std::move(lifetime_params))); |
Googler | fc1b55c | 2022-03-04 13:03:46 +0000 | [diff] [blame] | 890 | } else if (clang::QualType pointee_type = PointeeType(type); |
| 891 | !pointee_type.isNull()) { |
Martin Brænne | 0656ebf | 2022-04-25 23:52:54 -0700 | [diff] [blame] | 892 | if (type_lifetime_args.empty()) { |
| 893 | llvm::report_fatal_error( |
| 894 | llvm::Twine("didn't find type lifetimes for object of type " + |
| 895 | type.getAsString())); |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 896 | } |
Martin Brænne | 0656ebf | 2022-04-25 23:52:54 -0700 | [diff] [blame] | 897 | std::string pointee_object_lifetime_parameter = |
| 898 | std::move(type_lifetime_args.back()); |
| 899 | type_lifetime_args.pop_back(); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 900 | // Third case: pointer. |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 901 | return ObjectLifetimes( |
Googler | fc1b55c | 2022-03-04 13:03:46 +0000 | [diff] [blame] | 902 | ret_lifetime, ValueLifetimes::ForPointerLikeType( |
| 903 | type, GetObjectLifetimesForTypeInContext( |
| 904 | pointee_type, std::move(type_lifetime_args), |
Martin Brænne | 0656ebf | 2022-04-25 23:52:54 -0700 | [diff] [blame] | 905 | pointee_object_lifetime_parameter))); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Googler | 705f945 | 2021-12-16 15:30:08 +0000 | [diff] [blame] | 908 | return ObjectLifetimes(ret_lifetime, |
| 909 | ValueLifetimes::ForLifetimeLessType(type)); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 912 | std::string ObjectLifetimes::DebugString( |
| 913 | const LifetimeFormatter& formatter) const { |
Luca Versari | 6ee6b34 | 2022-04-05 05:40:52 -0700 | [diff] [blame] | 914 | std::string inner_lifetimes = value_lifetimes_.DebugString(formatter); |
| 915 | std::string lifetime = formatter(lifetime_); |
| 916 | if (inner_lifetimes.empty()) { |
| 917 | return lifetime; |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 918 | } |
Luca Versari | 6ee6b34 | 2022-04-05 05:40:52 -0700 | [diff] [blame] | 919 | return absl::StrCat(std::move(inner_lifetimes), ", ", std::move(lifetime)); |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 922 | ObjectLifetimes ObjectLifetimes::GetFieldOrBaseLifetimes( |
| 923 | clang::QualType type, |
| 924 | llvm::SmallVector<std::string> type_lifetime_args) const { |
| 925 | return GetObjectLifetimesForTypeInContext(type, std::move(type_lifetime_args), |
| 926 | ""); |
| 927 | } |
| 928 | |
Luca Versari | 043a36e | 2022-04-08 00:55:36 -0700 | [diff] [blame] | 929 | bool ObjectLifetimes::HasAny( |
| 930 | const std::function<bool(Lifetime)>& predicate) const { |
| 931 | return predicate(lifetime_) || value_lifetimes_.HasAny(predicate); |
| 932 | } |
| 933 | |
Luca Versari | c523163 | 2022-04-11 07:36:35 -0700 | [diff] [blame] | 934 | void ObjectLifetimes::SubstituteLifetimes(const LifetimeSubstitutions& subst) { |
| 935 | lifetime_ = subst.Substitute(lifetime_); |
| 936 | value_lifetimes_.SubstituteLifetimes(subst); |
| 937 | } |
| 938 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 939 | void ObjectLifetimes::Traverse(std::function<void(Lifetime&, Variance)> visitor, |
| 940 | Variance variance, |
| 941 | clang::QualType indirection_type) { |
| 942 | assert(indirection_type.isNull() || |
Martin Brænne | 9fb786f | 2022-06-23 01:18:19 -0700 | [diff] [blame] | 943 | StripAttributes(indirection_type->getPointeeType().IgnoreParens()) == |
| 944 | Type()); |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 945 | value_lifetimes_.Traverse( |
| 946 | visitor, indirection_type.isNull() || indirection_type.isConstQualified() |
| 947 | ? kCovariant |
| 948 | : kInvariant); |
| 949 | visitor(lifetime_, variance); |
| 950 | } |
| 951 | |
| 952 | void ObjectLifetimes::Traverse( |
| 953 | std::function<void(const Lifetime&, Variance)> visitor, Variance variance, |
| 954 | clang::QualType indirection_type) const { |
| 955 | const_cast<ObjectLifetimes*>(this)->Traverse( |
| 956 | [&visitor](Lifetime& l, Variance v) { visitor(l, v); }, variance, |
| 957 | indirection_type); |
| 958 | } |
| 959 | |
Googler | 17a2d51 | 2022-02-23 12:09:43 +0000 | [diff] [blame] | 960 | llvm::Expected<llvm::StringRef> EvaluateAsStringLiteral( |
| 961 | const clang::Expr* expr, const clang::ASTContext& ast_context) { |
| 962 | auto error = []() { |
Martin Brænne | 198ff49 | 2023-04-05 01:41:25 -0700 | [diff] [blame] | 963 | return llvm::make_error<LifetimeError>( |
| 964 | LifetimeError::Type::Other, |
Googler | 17a2d51 | 2022-02-23 12:09:43 +0000 | [diff] [blame] | 965 | "cannot evaluate argument as a string literal"); |
| 966 | }; |
| 967 | |
| 968 | clang::Expr::EvalResult eval_result; |
| 969 | if (!expr->EvaluateAsConstantExpr(eval_result, ast_context) || |
| 970 | !eval_result.Val.isLValue()) { |
| 971 | return error(); |
| 972 | } |
| 973 | |
| 974 | const auto* eval_result_expr = |
| 975 | eval_result.Val.getLValueBase().dyn_cast<const clang::Expr*>(); |
| 976 | if (!eval_result_expr) { |
| 977 | return error(); |
| 978 | } |
| 979 | |
| 980 | const auto* strlit = clang::dyn_cast<clang::StringLiteral>(eval_result_expr); |
| 981 | if (!strlit) { |
| 982 | return error(); |
| 983 | } |
| 984 | |
| 985 | return strlit->getString(); |
| 986 | } |
| 987 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 988 | } // namespace lifetimes |
| 989 | } // namespace tidy |
| 990 | } // namespace clang |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 991 | |
| 992 | namespace llvm { |
| 993 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 994 | bool DenseMapInfo<clang::tidy::lifetimes::ValueLifetimes>::isEqual( |
| 995 | const clang::tidy::lifetimes::ValueLifetimes& lhs, |
| 996 | const clang::tidy::lifetimes::ValueLifetimes& rhs) { |
Googler | 41f3e35 | 2021-12-16 15:24:21 +0000 | [diff] [blame] | 997 | if (lhs.type_ != rhs.type_) { |
| 998 | return false; |
| 999 | } |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1000 | if ((lhs.pointee_lifetimes_ == nullptr) != |
| 1001 | (rhs.pointee_lifetimes_ == nullptr)) { |
| 1002 | return false; |
| 1003 | } |
| 1004 | if (lhs.pointee_lifetimes_ && |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 1005 | !DenseMapInfo<clang::tidy::lifetimes::ObjectLifetimes>::isEqual( |
Googler | 2e87fae | 2021-12-16 15:24:41 +0000 | [diff] [blame] | 1006 | *lhs.pointee_lifetimes_, *rhs.pointee_lifetimes_)) { |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1007 | return false; |
| 1008 | } |
| 1009 | if (lhs.template_argument_lifetimes_.size() != |
| 1010 | rhs.template_argument_lifetimes_.size()) { |
| 1011 | return false; |
| 1012 | } |
| 1013 | for (size_t i = 0; i < lhs.template_argument_lifetimes_.size(); i++) { |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 1014 | if (lhs.template_argument_lifetimes_[i].size() != |
| 1015 | rhs.template_argument_lifetimes_[i].size()) { |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1016 | return false; |
| 1017 | } |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 1018 | for (size_t j = 0; j < lhs.template_argument_lifetimes_[i].size(); j++) { |
| 1019 | const auto& alhs = lhs.template_argument_lifetimes_[i][j]; |
| 1020 | const auto& arhs = rhs.template_argument_lifetimes_[i][j]; |
| 1021 | if (alhs.has_value() != arhs.has_value()) { |
| 1022 | return false; |
| 1023 | } |
| 1024 | if (alhs.has_value() && !isEqual(*alhs, *arhs)) { |
| 1025 | return false; |
| 1026 | } |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1027 | } |
| 1028 | } |
Luca Versari | 7f1783d | 2022-01-31 14:40:39 +0000 | [diff] [blame] | 1029 | if (lhs.lifetime_parameters_by_name_.GetMapping() != |
| 1030 | rhs.lifetime_parameters_by_name_.GetMapping()) { |
| 1031 | return false; |
| 1032 | } |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1033 | return true; |
| 1034 | } |
| 1035 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 1036 | unsigned DenseMapInfo<clang::tidy::lifetimes::ValueLifetimes>::getHashValue( |
| 1037 | const clang::tidy::lifetimes::ValueLifetimes& value_lifetimes) { |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1038 | llvm::hash_code hash = 0; |
Googler | 2e87fae | 2021-12-16 15:24:41 +0000 | [diff] [blame] | 1039 | if (value_lifetimes.pointee_lifetimes_) { |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 1040 | hash = DenseMapInfo<clang::tidy::lifetimes::ObjectLifetimes>::getHashValue( |
Googler | 2e87fae | 2021-12-16 15:24:41 +0000 | [diff] [blame] | 1041 | *value_lifetimes.pointee_lifetimes_); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1042 | } |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 1043 | for (const auto& lifetimes_at_depth : |
Googler | 2e87fae | 2021-12-16 15:24:41 +0000 | [diff] [blame] | 1044 | value_lifetimes.template_argument_lifetimes_) { |
Googler | e4d4ec4 | 2022-03-28 06:52:23 -0700 | [diff] [blame] | 1045 | for (const auto& tmpl_lifetime : lifetimes_at_depth) { |
| 1046 | if (tmpl_lifetime) { |
| 1047 | hash = hash_combine(hash, getHashValue(*tmpl_lifetime)); |
| 1048 | } |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1049 | } |
| 1050 | } |
Luca Versari | 7f1783d | 2022-01-31 14:40:39 +0000 | [diff] [blame] | 1051 | for (const auto& lifetime_arg : |
| 1052 | value_lifetimes.lifetime_parameters_by_name_.GetMapping()) { |
| 1053 | hash = hash_combine(hash, DenseMapInfo<llvm::StringRef>::getHashValue( |
| 1054 | lifetime_arg.first())); |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 1055 | hash = hash_combine( |
| 1056 | hash, DenseMapInfo<clang::tidy::lifetimes::Lifetime>::getHashValue( |
| 1057 | lifetime_arg.second)); |
Luca Versari | 7f1783d | 2022-01-31 14:40:39 +0000 | [diff] [blame] | 1058 | } |
Googler | 41f3e35 | 2021-12-16 15:24:21 +0000 | [diff] [blame] | 1059 | return hash_combine( |
Googler | 2e87fae | 2021-12-16 15:24:41 +0000 | [diff] [blame] | 1060 | hash, DenseMapInfo<clang::QualType>::getHashValue(value_lifetimes.type_)); |
Googler | ace85a2 | 2021-11-26 14:18:31 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | } // namespace llvm |