blob: 2eb959f77d0e44ba77dcfd3e7ff687deccaab2ee [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_VISIT_LIFETIMES_H_
6#define DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_VISIT_LIFETIMES_H_
7
8#include "lifetime_analysis/object_set.h"
9#include "lifetime_annotations/type_lifetimes.h"
10#include "clang/AST/Decl.h"
11#include "clang/AST/Type.h"
12#include "llvm/ADT/DenseSet.h"
13
14namespace clang {
15namespace tidy {
16namespace lifetimes {
17
18// A visitor interface used with VisitLifetimes.
19//
20// An implementation of this interface does two things:
21// - Defines how a type with lifetimes should be traversed
22// - Collects information from the traversal
23//
24// An implementation needs to define two functions:
25// - Traverse() maps an object of reference-like type to the corresponding
26// points-to set. This function typically also collects information from
27// the traversal.
28// - GetFieldObjects() maps an object of struct type to the objects for
29// its fields.
30class LifetimeVisitor {
31 public:
32 // Returns the object representing the given `field` of the struct represented
33 // by `objects`. As all the objects in `objects` represent a single class
34 // hierarchy, down to the class that defines the field, they must all have the
35 // same field object.
Martin Brænne2c003b62022-07-01 05:45:42 -070036 virtual const Object* GetFieldObject(const ObjectSet& objects,
37 const clang::FieldDecl* field) = 0;
Luca Versari99fddff2022-05-25 10:22:32 -070038 // Returns the object representing the given `base` of the struct represented
39 // by `objects`. As all the objects in `objects` represent a single class
40 // hierarchy, down to the class that defines the base class, they must all
41 // have the same base object.
Martin Brænne2c003b62022-07-01 05:45:42 -070042 virtual const Object* GetBaseClassObject(const ObjectSet& objects,
43 clang::QualType base) = 0;
Luca Versari99fddff2022-05-25 10:22:32 -070044 // Returns the ObjectSet pointed to by the objects in the input
45 // ObjectSet, which are assumed to have lifetimes
46 // `lifetimes`. Returning an empty set will stop the visit.
47 virtual ObjectSet Traverse(const ObjectLifetimes& lifetimes,
48 const ObjectSet& objects, int pointee_depth) = 0;
49 virtual ~LifetimeVisitor() {}
50};
51
52// Visits the objects and fields of `type` using the given `visitor`;
53// `object_lifetimes` corresponds to the lifetimes of an object of type `type`.
54// `points_to_set` should contain a set of objects that are assumed to be of
55// type `type`.
56void VisitLifetimes(const ObjectSet& points_to_set, clang::QualType type,
57 const ObjectLifetimes& object_lifetimes,
58 LifetimeVisitor& visitor);
59
60} // namespace lifetimes
61} // namespace tidy
62} // namespace clang
63
64#endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_VISIT_LIFETIMES_H_