1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file defines the names used by GC infrastructure.
7 #ifndef TOOLS_BLINK_GC_PLUGIN_CONFIG_H_
8 #define TOOLS_BLINK_GC_PLUGIN_CONFIG_H_
10 #include "clang/AST/AST.h"
11 #include "clang/AST/Attr.h"
13 const char kNewOperatorName[] = "operator new";
14 const char kCreateName[] = "create";
15 const char kTraceName[] = "trace";
16 const char kFinalizeName[] = "finalizeGarbageCollectedObject";
17 const char kTraceAfterDispatchName[] = "traceAfterDispatch";
18 const char kRegisterWeakMembersName[] = "registerWeakMembers";
19 const char kHeapAllocatorName[] = "HeapAllocator";
23 static bool IsMember(const std::string& name) {
24 return name == "Member";
27 static bool IsWeakMember(const std::string& name) {
28 return name == "WeakMember";
31 static bool IsMemberHandle(const std::string& name) {
32 return IsMember(name) ||
36 static bool IsPersistent(const std::string& name) {
37 return name == "Persistent";
40 static bool IsPersistentHandle(const std::string& name) {
41 return IsPersistent(name) ||
42 IsPersistentGCCollection(name);
45 static bool IsRawPtr(const std::string& name) {
46 return name == "RawPtr";
49 static bool IsRefPtr(const std::string& name) {
50 return name == "RefPtr";
53 static bool IsOwnPtr(const std::string& name) {
54 return name == "OwnPtr";
57 static bool IsWTFCollection(const std::string& name) {
58 return name == "Vector" ||
61 name == "ListHashSet" ||
62 name == "LinkedHashSet" ||
63 name == "HashCountedSet" ||
67 static bool IsGCCollection(const std::string& name) {
68 return name == "HeapVector" ||
69 name == "HeapDeque" ||
70 name == "HeapHashSet" ||
71 name == "HeapListHashSet" ||
72 name == "HeapLinkedHashSet" ||
73 name == "HeapHashCountedSet" ||
74 name == "HeapHashMap" ||
75 IsPersistentGCCollection(name);
78 static bool IsPersistentGCCollection(const std::string& name) {
79 return name == "PersistentHeapVector" ||
80 name == "PersistentHeapDeque" ||
81 name == "PersistentHeapHashSet" ||
82 name == "PersistentHeapListHashSet" ||
83 name == "PersistentHeapLinkedHashSet" ||
84 name == "PersistentHeapHashCountedSet" ||
85 name == "PersistentHeapHashMap";
88 static bool IsHashMap(const std::string& name) {
89 return name == "HashMap" ||
90 name == "HeapHashMap" ||
91 name == "PersistentHeapHashMap";
94 // Assumes name is a valid collection name.
95 static size_t CollectionDimension(const std::string& name) {
96 return (IsHashMap(name) || name == "pair") ? 2 : 1;
99 static bool IsGCMixinBase(const std::string& name) {
100 return name == "GarbageCollectedMixin";
103 static bool IsGCFinalizedBase(const std::string& name) {
104 return name == "GarbageCollectedFinalized" ||
105 name == "RefCountedGarbageCollected";
108 static bool IsGCBase(const std::string& name) {
109 return name == "GarbageCollected" ||
110 IsGCFinalizedBase(name) ||
114 static bool IsTreeSharedBase(const std::string& name) {
115 return name == "TreeShared";
118 static bool IsAnnotated(clang::Decl* decl, const std::string& anno) {
119 clang::AnnotateAttr* attr = decl->getAttr<clang::AnnotateAttr>();
120 return attr && (attr->getAnnotation() == anno);
123 static bool IsStackAnnotated(clang::Decl* decl) {
124 return IsAnnotated(decl, "blink_stack_allocated");
127 static bool IsIgnoreAnnotated(clang::Decl* decl) {
128 return IsAnnotated(decl, "blink_gc_plugin_ignore");
131 static bool IsIgnoreCycleAnnotated(clang::Decl* decl) {
132 return IsAnnotated(decl, "blink_gc_plugin_ignore_cycle") ||
133 IsIgnoreAnnotated(decl);
136 static bool IsVisitor(const std::string& name) { return name == "Visitor"; }
138 static bool IsTraceMethod(clang::CXXMethodDecl* method,
139 bool* isTraceAfterDispatch = 0) {
140 if (method->getNumParams() != 1)
143 const std::string& name = method->getNameAsString();
144 if (name != kTraceName && name != kTraceAfterDispatchName)
147 const clang::QualType& formal_type = method->getParamDecl(0)->getType();
148 if (!formal_type->isPointerType())
151 clang::CXXRecordDecl* pointee_type =
152 formal_type->getPointeeType()->getAsCXXRecordDecl();
156 if (!IsVisitor(pointee_type->getName()))
159 if (isTraceAfterDispatch)
160 *isTraceAfterDispatch = (name == kTraceAfterDispatchName);
164 static bool StartsWith(const std::string& str, const std::string& prefix) {
165 if (prefix.size() > str.size())
167 return str.compare(0, prefix.size(), prefix) == 0;
170 static bool EndsWith(const std::string& str, const std::string& suffix) {
171 if (suffix.size() > str.size())
173 return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
177 #endif // TOOLS_BLINK_GC_PLUGIN_CONFIG_H_