1 //===-- StorageLocation.h ---------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines classes that represent elements of the local variable store
10 // and of the heap during dataflow analysis.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_STORAGELOCATION_H
15 #define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_STORAGELOCATION_H
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/Type.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Debug.h"
23 #define DEBUG_TYPE "dataflow"
28 /// Base class for elements of the local variable store and of the heap.
30 /// Each storage location holds a value. The mapping from storage locations to
31 /// values is stored in the environment.
32 class StorageLocation {
34 enum class Kind { Scalar, Aggregate };
36 StorageLocation(Kind LocKind, QualType Type) : LocKind(LocKind), Type(Type) {
37 assert(Type.isNull() || !Type->isReferenceType());
40 // Non-copyable because addresses of storage locations are used as their
41 // identities throughout framework and user code. The framework is responsible
42 // for construction and destruction of storage locations.
43 StorageLocation(const StorageLocation &) = delete;
44 StorageLocation &operator=(const StorageLocation &) = delete;
46 virtual ~StorageLocation() = default;
48 Kind getKind() const { return LocKind; }
50 QualType getType() const { return Type; }
57 /// A storage location that is not subdivided further for the purposes of
58 /// abstract interpretation. For example: `int`, `int*`, `int&`.
59 class ScalarStorageLocation final : public StorageLocation {
61 explicit ScalarStorageLocation(QualType Type)
62 : StorageLocation(Kind::Scalar, Type) {}
64 static bool classof(const StorageLocation *Loc) {
65 return Loc->getKind() == Kind::Scalar;
69 /// A storage location which is subdivided into smaller storage locations that
70 /// can be traced independently by abstract interpretation. For example: a
71 /// struct with public members. The child map is flat, so when used for a struct
72 /// or class type, all accessible members of base struct and class types are
73 /// directly accesible as children of this location.
75 /// The storage location for a field of reference type may be null. This
76 /// typically occurs in one of two situations:
77 /// - The record has not been fully initialized.
78 /// - The maximum depth for modelling a self-referential data structure has been
80 /// Storage locations for fields of all other types must be non-null.
82 /// FIXME: Currently, the storage location of unions is modelled the same way as
83 /// that of structs or classes. Eventually, we need to change this modelling so
84 /// that all of the members of a given union have the same storage location.
85 class AggregateStorageLocation final : public StorageLocation {
87 using FieldToLoc = llvm::DenseMap<const ValueDecl *, StorageLocation *>;
89 explicit AggregateStorageLocation(QualType Type)
90 : AggregateStorageLocation(Type, FieldToLoc()) {}
92 AggregateStorageLocation(QualType Type, FieldToLoc TheChildren)
93 : StorageLocation(Kind::Aggregate, Type),
94 Children(std::move(TheChildren)) {
95 assert(!Type.isNull());
96 assert(Type->isRecordType());
98 for (auto [Field, Loc] : Children) {
99 if (!Field->getType()->isReferenceType() && Loc == nullptr)
106 static bool classof(const StorageLocation *Loc) {
107 return Loc->getKind() == Kind::Aggregate;
110 /// Returns the child storage location for `D`.
112 /// May return null if `D` has reference type; guaranteed to return non-null
113 /// in all other cases.
115 /// Note that it is an error to call this with a field that does not exist.
116 /// The function does not return null in this case.
117 StorageLocation *getChild(const ValueDecl &D) const {
118 auto It = Children.find(&D);
120 if (It == Children.end()) {
121 llvm::dbgs() << "Couldn't find child " << D.getNameAsString()
122 << " on StorageLocation " << this << " of type "
123 << getType() << "\n";
124 llvm::dbgs() << "Existing children:\n";
125 for ([[maybe_unused]] auto [Field, Loc] : Children) {
126 llvm::dbgs() << Field->getNameAsString() << "\n";
130 assert(It != Children.end());
134 /// Changes the child storage location for a field `D` of reference type.
135 /// All other fields cannot change their storage location and always retain
136 /// the storage location passed to the `AggregateStorageLocation` constructor.
140 /// `D` must have reference type.
141 void setChild(const ValueDecl &D, StorageLocation *Loc) {
142 assert(D.getType()->isReferenceType());
146 llvm::iterator_range<FieldToLoc::const_iterator> children() const {
147 return {Children.begin(), Children.end()};
154 } // namespace dataflow
159 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_STORAGELOCATION_H