Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / clang / blink_gc_plugin / Config.h
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.
4
5 // This file defines the names used by GC infrastructure.
6
7 #ifndef TOOLS_BLINK_GC_PLUGIN_CONFIG_H_
8 #define TOOLS_BLINK_GC_PLUGIN_CONFIG_H_
9
10 #include "clang/AST/AST.h"
11 #include "clang/AST/Attr.h"
12
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";
20
21 class Config {
22  public:
23   static bool IsMember(const std::string& name) {
24     return name == "Member";
25   }
26
27   static bool IsWeakMember(const std::string& name) {
28     return name == "WeakMember";
29   }
30
31   static bool IsMemberHandle(const std::string& name) {
32     return IsMember(name) ||
33            IsWeakMember(name);
34   }
35
36   static bool IsPersistent(const std::string& name) {
37     return name == "Persistent";
38   }
39
40   static bool IsPersistentHandle(const std::string& name) {
41     return IsPersistent(name) ||
42            IsPersistentGCCollection(name);
43   }
44
45   static bool IsRawPtr(const std::string& name) {
46     return name == "RawPtr";
47   }
48
49   static bool IsRefPtr(const std::string& name) {
50     return name == "RefPtr";
51   }
52
53   static bool IsOwnPtr(const std::string& name) {
54     return name == "OwnPtr";
55   }
56
57   static bool IsWTFCollection(const std::string& name) {
58     return name == "Vector" ||
59            name == "Deque" ||
60            name == "HashSet" ||
61            name == "ListHashSet" ||
62            name == "LinkedHashSet" ||
63            name == "HashCountedSet" ||
64            name == "HashMap";
65   }
66
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);
76   }
77
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";
86   }
87
88   static bool IsHashMap(const std::string& name) {
89     return name == "HashMap" ||
90            name == "HeapHashMap" ||
91            name == "PersistentHeapHashMap";
92   }
93
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;
97   }
98
99   static bool IsGCMixinBase(const std::string& name) {
100     return name == "GarbageCollectedMixin";
101   }
102
103   static bool IsGCFinalizedBase(const std::string& name) {
104     return name == "GarbageCollectedFinalized" ||
105            name == "RefCountedGarbageCollected";
106   }
107
108   static bool IsGCBase(const std::string& name) {
109     return name == "GarbageCollected" ||
110            IsGCFinalizedBase(name) ||
111            IsGCMixinBase(name);
112   }
113
114   static bool IsTreeSharedBase(const std::string& name) {
115     return name == "TreeShared";
116   }
117
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);
121   }
122
123   static bool IsStackAnnotated(clang::Decl* decl) {
124     return IsAnnotated(decl, "blink_stack_allocated");
125   }
126
127   static bool IsIgnoreAnnotated(clang::Decl* decl) {
128     return IsAnnotated(decl, "blink_gc_plugin_ignore");
129   }
130
131   static bool IsIgnoreCycleAnnotated(clang::Decl* decl) {
132     return IsAnnotated(decl, "blink_gc_plugin_ignore_cycle") ||
133            IsIgnoreAnnotated(decl);
134   }
135
136   static bool IsVisitor(const std::string& name) { return name == "Visitor"; }
137
138   static bool IsTraceMethod(clang::CXXMethodDecl* method,
139                             bool* isTraceAfterDispatch = 0) {
140     if (method->getNumParams() != 1)
141       return false;
142
143     const std::string& name = method->getNameAsString();
144     if (name != kTraceName && name != kTraceAfterDispatchName)
145       return false;
146
147     const clang::QualType& formal_type = method->getParamDecl(0)->getType();
148     if (!formal_type->isPointerType())
149       return false;
150
151     clang::CXXRecordDecl* pointee_type =
152         formal_type->getPointeeType()->getAsCXXRecordDecl();
153     if (!pointee_type)
154       return false;
155
156     if (!IsVisitor(pointee_type->getName()))
157       return false;
158
159     if (isTraceAfterDispatch)
160       *isTraceAfterDispatch = (name == kTraceAfterDispatchName);
161     return true;
162   }
163
164   static bool StartsWith(const std::string& str, const std::string& prefix) {
165     if (prefix.size() > str.size())
166       return false;
167     return str.compare(0, prefix.size(), prefix) == 0;
168   }
169
170   static bool EndsWith(const std::string& str, const std::string& suffix) {
171     if (suffix.size() > str.size())
172       return false;
173     return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
174   }
175 };
176
177 #endif  // TOOLS_BLINK_GC_PLUGIN_CONFIG_H_