@@ -52,41 +52,100 @@ struct Origin {
5252 }
5353};
5454
55+ // / A tree of origins representing levels of indirection for pointer-like types.
56+ // /
57+ // / Each node in the tree contains an OriginID representing a level of
58+ // / indirection. The tree structure captures the multi-level nature of
59+ // / pointer and reference types in the lifetime analysis.
60+ // /
61+ // / Examples:
62+ // / - For `int& x`, the tree has depth 2:
63+ // / * Root: origin for the reference storage itself (the lvalue `x`)
64+ // / * Pointee: origin for what `x` refers to
65+ // /
66+ // / - For `int* p`, the tree has depth 2:
67+ // / * Root: origin for the pointer variable `p`
68+ // / * Pointee: origin for what `p` points to
69+ // /
70+ // / - For `View v` (where View is gsl::Pointer), the tree has depth 2:
71+ // / * Root: origin for the view object itself
72+ // / * Pointee: origin for what the view refers to
73+ // /
74+ // / - For `int** pp`, the tree has depth 3:
75+ // / * Root: origin for `pp` itself
76+ // / * Pointee: origin for `*pp` (what `pp` points to)
77+ // / * Pointee->Pointee: origin for `**pp` (what `*pp` points to)
78+ // /
79+ // / The tree structure enables the analysis to track how loans flow through
80+ // / different levels of indirection when assignments and dereferences occur.
81+ struct OriginTree {
82+ OriginID OID;
83+ OriginTree *Pointee = nullptr ;
84+
85+ OriginTree (OriginID OID) : OID(OID) {}
86+
87+ size_t getDepth () const {
88+ size_t Depth = 1 ;
89+ const OriginTree *T = this ;
90+ while (T->Pointee ) {
91+ T = T->Pointee ;
92+ Depth++;
93+ }
94+ return Depth;
95+ }
96+ };
97+
98+ bool isPointerLikeType (QualType QT);
99+
55100// / Manages the creation, storage, and retrieval of origins for pointer-like
56101// / variables and expressions.
57102class OriginManager {
58103public:
59- OriginManager () = default ;
104+ // / Gets or creates the OriginTree for a given ValueDecl.
105+ OriginTree *getOrCreateTree (const ValueDecl *D);
60106
61- Origin &addOrigin (OriginID ID, const clang::ValueDecl &D);
62- Origin &addOrigin (OriginID ID, const clang::Expr &E);
63-
64- // TODO: Mark this method as const once we remove the call to getOrCreate.
65- OriginID get (const Expr &E);
66-
67- OriginID get (const ValueDecl &D);
68-
69- OriginID getOrCreate (const Expr &E);
107+ // / Gets or creates the OriginTree for a given Expr.
108+ // / Returns nullptr for rvalues of non-pointer type as these do not have
109+ // / origins.
110+ OriginTree *getOrCreateTree (const Expr *E, ASTContext &Ctx);
70111
71112 const Origin &getOrigin (OriginID ID) const ;
72113
73114 llvm::ArrayRef<Origin> getOrigins () const { return AllOrigins; }
74115
75- OriginID getOrCreate (const ValueDecl &D);
76-
77116 unsigned getNumOrigins () const { return NextOriginID.Value ; }
78117
79118 void dump (OriginID OID, llvm::raw_ostream &OS) const ;
80119
81120private:
82121 OriginID getNextOriginID () { return NextOriginID++; }
83122
123+ OriginID createOrigin (const ValueDecl *D) {
124+ OriginID NewID = getNextOriginID ();
125+ AllOrigins.emplace_back (NewID, D);
126+ return NewID;
127+ }
128+
129+ OriginID createOrigin (const Expr *E) {
130+ OriginID NewID = getNextOriginID ();
131+ AllOrigins.emplace_back (NewID, E);
132+ return NewID;
133+ }
134+
135+ OriginTree *createNode (OriginID OID) {
136+ return new (TreeAllocator.Allocate <OriginTree>()) OriginTree (OID);
137+ }
138+
139+ template <typename T>
140+ OriginTree *buildTreeForType (QualType QT, const T *Node);
141+
84142 OriginID NextOriginID{0 };
85- // / TODO(opt): Profile and evaluate the usefullness of small buffer
143+ // / TODO(opt): Profile and evaluate the usefulness of small buffer
86144 // / optimisation.
87145 llvm::SmallVector<Origin> AllOrigins;
88- llvm::DenseMap<const clang::ValueDecl *, OriginID> DeclToOriginID;
89- llvm::DenseMap<const clang::Expr *, OriginID> ExprToOriginID;
146+ llvm::BumpPtrAllocator TreeAllocator;
147+ llvm::DenseMap<const clang::ValueDecl *, OriginTree *> DeclToTreeMap;
148+ llvm::DenseMap<const clang::Expr *, OriginTree *> ExprToTreeMap;
90149};
91150} // namespace clang::lifetimes::internal
92151
0 commit comments