blob: 958e526f86f2575e32fd27d1d717e7969a066d59 [file] [log] [blame]
Luca Versari99fddff2022-05-25 10:22:32 -07001// 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#ifndef DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_H_
6#define DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_H_
7
Dmitri Gribenkoa087d232023-07-10 08:03:46 -07008#include <optional>
9#include <ostream>
Luca Versari99fddff2022-05-25 10:22:32 -070010#include <string>
11
Luca Versari50824882023-01-23 05:51:35 -080012#include "lifetime_annotations/function_lifetimes.h"
Luca Versari99fddff2022-05-25 10:22:32 -070013#include "lifetime_annotations/lifetime.h"
Luca Versari99fddff2022-05-25 10:22:32 -070014#include "clang/AST/Type.h"
Luca Versari99fddff2022-05-25 10:22:32 -070015
16namespace clang {
17namespace tidy {
18namespace lifetimes {
19
20// Any object that has a lifetime. Multiple objects might have the same
21// lifetime, but two equal objects always have the same lifetime.
Luca Versaria12c9d52023-02-23 08:40:48 -080022// An object may also represent a function whose lifetime signature is
23// known, obtainable by GetFuncLifetimes.
Luca Versari99fddff2022-05-25 10:22:32 -070024class Object {
25 public:
Martin Brænne57e0bfa2022-07-01 13:27:14 -070026 Object(const Object&) = delete;
Martin Brænne617c92b2022-07-01 06:25:54 -070027 Object(Object&&) = delete;
Martin Brænne57e0bfa2022-07-01 13:27:14 -070028 Object& operator=(const Object&) = delete;
Martin Brænne617c92b2022-07-01 06:25:54 -070029 Object& operator=(Object&&) = delete;
Luca Versari99fddff2022-05-25 10:22:32 -070030
Martin Brænne4a1231d2022-07-01 01:45:44 -070031 // Creates an object with the given lifetime and type.
32 // This constructor should only be used in tests. Outside of tests, use
33 // one of the ObjectRepository::CreateObject...() functions.
Luca Versaria12c9d52023-02-23 08:40:48 -080034 Object(Lifetime lifetime, clang::QualType type,
35 std::optional<FunctionLifetimes> func_lifetimes);
Martin Brænne4a1231d2022-07-01 01:45:44 -070036
Luca Versari99fddff2022-05-25 10:22:32 -070037 // Returns the lifetime of the object.
38 Lifetime GetLifetime() const { return lifetime_; }
39
40 clang::QualType Type() const { return type_; }
41
42 // Returns a textual representation of the object for debug logging.
43 std::string DebugString() const;
44
Luca Versari50824882023-01-23 05:51:35 -080045 // Returns the lifetimes of function that this object represents, if known;
46 // note that lifetimes may not be known even if GetFunc() returns non-null.
47 const std::optional<FunctionLifetimes>& GetFuncLifetimes() const {
48 return func_lifetimes_;
49 }
50
Luca Versari99fddff2022-05-25 10:22:32 -070051 private:
Luca Versari99fddff2022-05-25 10:22:32 -070052 Lifetime lifetime_;
53 clang::QualType type_;
Luca Versari50824882023-01-23 05:51:35 -080054 std::optional<FunctionLifetimes> func_lifetimes_;
Luca Versari99fddff2022-05-25 10:22:32 -070055};
56
57std::ostream& operator<<(std::ostream& os, Object object);
58
59} // namespace lifetimes
60} // namespace tidy
61} // namespace clang
62
Luca Versari99fddff2022-05-25 10:22:32 -070063#endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_H_