File: RecordInfo.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (239 lines) | stat: -rw-r--r-- 6,962 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file provides a wrapper for CXXRecordDecl that accumulates GC related
// information about a class. Accumulated information is memoized and the info
// objects are stored in a RecordCache.

#ifndef TOOLS_BLINK_GC_PLUGIN_RECORD_INFO_H_
#define TOOLS_BLINK_GC_PLUGIN_RECORD_INFO_H_

#include <map>
#include <vector>

#include "Edge.h"

#include "clang/AST/AST.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/Frontend/CompilerInstance.h"

class RecordCache;

// A potentially tracable and/or lifetime affecting point in the object graph.
class GraphPoint {
 public:
  virtual ~GraphPoint() {}
  void MarkTraced() { traced_ = TracedStatus::kTraced; }
  void MarkTracedIfNeeded() { traced_ = TracedStatus::kTracedIfNeeded; }
  bool IsProperlyTraced() {
    return (traced_ != TracedStatus::kUntraced) || !NeedsTracing().IsNeeded();
  }
  bool IsInproperlyTraced() {
    return (traced_ == TracedStatus::kTraced) && NeedsTracing().IsIllegal();
  }
  virtual const TracingStatus NeedsTracing() = 0;

 private:
  enum class TracedStatus { kUntraced, kTraced, kTracedIfNeeded };
  TracedStatus traced_ = TracedStatus::kUntraced;
};

class BasePoint : public GraphPoint {
 public:
  BasePoint(const clang::CXXBaseSpecifier& spec,
            RecordInfo* info,
            const TracingStatus& status)
      : spec_(spec), info_(info), status_(status) {}
  const TracingStatus NeedsTracing() override { return status_; }
  const clang::CXXBaseSpecifier& spec() { return spec_; }
  RecordInfo* info() { return info_; }

 private:
  const clang::CXXBaseSpecifier& spec_;
  RecordInfo* info_;
  TracingStatus status_;
};

class FieldPoint : public GraphPoint {
 public:
  FieldPoint(clang::FieldDecl* field, Edge* edge)
      : field_(field), edge_(edge) {}
  const TracingStatus NeedsTracing() override {
    return edge_->NeedsTracing(Edge::kRecursive);
  }
  clang::FieldDecl* field() { return field_; }
  Edge* edge() { return edge_; }

 private:
  clang::FieldDecl* field_;
  Edge* edge_;

  friend class RecordCache;
  void deleteEdge() { delete edge_; }
};

// Wrapper class to lazily collect information about a C++ record.
class RecordInfo {
 public:
  typedef std::vector<std::pair<clang::CXXRecordDecl*, BasePoint>> Bases;

  struct FieldDeclCmp {
    bool operator()(clang::FieldDecl* a, clang::FieldDecl *b) const {
      return a->getBeginLoc() < b->getBeginLoc();
    }
  };
  typedef std::map<clang::FieldDecl*, FieldPoint, FieldDeclCmp> Fields;

  typedef std::vector<const clang::Type*> TemplateArgs;

  ~RecordInfo();

  clang::CXXRecordDecl* record() const { return record_; }
  const std::string& name() const { return name_; }
  Fields& GetFields();
  Bases& GetBases();
  const clang::CXXBaseSpecifier* GetDirectGCBase();
  clang::CXXMethodDecl* GetTraceMethod();
  clang::CXXMethodDecl* GetTraceWrappersMethod();
  clang::CXXMethodDecl* GetTraceDispatchMethod();
  clang::CXXMethodDecl* GetFinalizeDispatchMethod();

  bool HasMultipleTraceDispatchMethods() const {
    return extra_trace_dispatch_method_ != nullptr;
  }
  bool HasMultipleFinalizeDispatchMethods() const {
    return extra_finalize_dispatch_method_ != nullptr;
  }

  clang::CXXMethodDecl* GetExtraTraceDispatchMethod() {
    assert(determined_trace_methods_);
    return extra_trace_dispatch_method_;
  }
  clang::CXXMethodDecl* GetExtraFinalizeDispatchMethod() {
    assert(determined_trace_methods_);
    return extra_finalize_dispatch_method_;
  }

  bool GetTemplateArgs(size_t count, TemplateArgs* output_args);

  bool IsHeapAllocatedCollection();
  bool IsGCDerived();
  bool IsGCDirectlyDerived();
  bool IsGCAllocated();
  bool IsGCMixin();
  bool IsStackAllocated();
  bool IsNewDisallowed();

  bool HasDefinition();

  clang::CXXMethodDecl* DeclaresNewOperator();

  bool RequiresTraceMethod();
  bool NeedsFinalization();
  bool DeclaresLocalTraceMethod();
  TracingStatus NeedsTracing(Edge::NeedsTracingOption);
  clang::CXXMethodDecl* InheritsNonVirtualTrace();
  bool IsConsideredAbstract();

  static clang::CXXRecordDecl* GetDependentTemplatedDecl(const clang::Type&);

 private:
  RecordInfo(clang::CXXRecordDecl* record, RecordCache* cache);

  void walkBases();

  Fields* CollectFields();
  Bases* CollectBases();
  void DetermineTracingMethods();
  bool InheritsTrace();

  Edge* CreateEdge(const clang::Type* type);
  Edge* CreateEdgeFromOriginalType(const clang::Type* type);

  bool HasOptionalFinalizer();

  bool HasTypeAlias(std::string marker_name) const;
  bool GetTemplateArgsInternal(
      const llvm::ArrayRef<clang::TemplateArgument>& args,
      size_t count,
      TemplateArgs* output_args);

  RecordCache* cache_;
  clang::CXXRecordDecl* record_;
  const std::string name_;
  TracingStatus fields_need_tracing_;
  Bases* bases_ = nullptr;
  Fields* fields_ = nullptr;

  enum CachedBool { kFalse = 0, kTrue = 1, kNotComputed = 2 };
  CachedBool is_stack_allocated_ = kNotComputed;
  CachedBool does_need_finalization_ = kNotComputed;
  CachedBool is_declaring_local_trace_ = kNotComputed;

  bool determined_new_operator_ = false;
  clang::CXXMethodDecl* new_operator_ = nullptr;

  bool determined_trace_methods_ = false;
  clang::CXXMethodDecl* trace_method_ = nullptr;
  clang::CXXMethodDecl* trace_dispatch_method_ = nullptr;
  clang::CXXMethodDecl* finalize_dispatch_method_ = nullptr;
  clang::CXXMethodDecl* extra_trace_dispatch_method_ = nullptr;
  clang::CXXMethodDecl* extra_finalize_dispatch_method_ = nullptr;

  bool is_gc_derived_ = false;

  std::vector<std::string> gc_base_names_;

  const clang::CXXBaseSpecifier* directly_derived_gc_base_ = nullptr;

  friend class RecordCache;
};

class RecordCache {
 public:
  RecordCache(clang::CompilerInstance& instance)
    : instance_(instance)
  {
  }

  RecordInfo* Lookup(clang::CXXRecordDecl* record);

  RecordInfo* Lookup(const clang::CXXRecordDecl* record) {
    return Lookup(const_cast<clang::CXXRecordDecl*>(record));
  }

  RecordInfo* Lookup(clang::DeclContext* decl) {
    return Lookup(clang::dyn_cast<clang::CXXRecordDecl>(decl));
  }

  RecordInfo* Lookup(const clang::Type* type) {
    return Lookup(type->getAsCXXRecordDecl());
  }

  RecordInfo* Lookup(const clang::QualType& type) {
    return Lookup(type.getTypePtr());
  }

  ~RecordCache() {
    for (Cache::iterator it = cache_.begin(); it != cache_.end(); ++it) {
      if (!it->second.fields_)
        continue;
      for (RecordInfo::Fields::iterator fit = it->second.fields_->begin();
        fit != it->second.fields_->end();
        ++fit) {
        fit->second.deleteEdge();
      }
    }
  }

  clang::CompilerInstance& instance() const { return instance_; }

 private:
  clang::CompilerInstance& instance_;

  typedef std::map<clang::CXXRecordDecl*, RecordInfo> Cache;
  Cache cache_;
};

#endif  // TOOLS_BLINK_GC_PLUGIN_RECORD_INFO_H_