blob: ee6950655cf4e6e8303feebdcbc41817b34fcfc8 [file] [log] [blame]
Richard Trieue7f7ed22017-02-22 01:11:25 +00001//===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// This file implements the ODRHash class, which calculates a hash based
12/// on AST nodes, which is stable across different runs.
13///
14//===----------------------------------------------------------------------===//
15
16#include "clang/AST/ODRHash.h"
17
18#include "clang/AST/DeclVisitor.h"
19#include "clang/AST/NestedNameSpecifier.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/AST/TypeVisitor.h"
22
23using namespace clang;
24
Richard Trieu639d7b62017-02-22 22:22:42 +000025void ODRHash::AddStmt(const Stmt *S) {
26 assert(S && "Expecting non-null pointer.");
27 S->ProcessODRHash(ID, *this);
28}
Richard Trieud0786092017-02-23 00:23:01 +000029
30void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
31 assert(II && "Expecting non-null pointer.");
32 ID.AddString(II->getName());
33}
34
Richard Trieu8459ddf2017-02-24 02:59:12 +000035void ODRHash::AddDeclarationName(DeclarationName Name) {
Richard Trieuf65efae2018-02-22 05:32:25 +000036 // Index all DeclarationName and use index numbers to refer to them.
37 auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
38 ID.AddInteger(Result.first->second);
39 if (!Result.second) {
40 // If found in map, the the DeclarationName has previously been processed.
41 return;
42 }
43
44 // First time processing each DeclarationName, also process its details.
Richard Trieu8459ddf2017-02-24 02:59:12 +000045 AddBoolean(Name.isEmpty());
46 if (Name.isEmpty())
47 return;
48
49 auto Kind = Name.getNameKind();
50 ID.AddInteger(Kind);
51 switch (Kind) {
52 case DeclarationName::Identifier:
53 AddIdentifierInfo(Name.getAsIdentifierInfo());
54 break;
55 case DeclarationName::ObjCZeroArgSelector:
56 case DeclarationName::ObjCOneArgSelector:
57 case DeclarationName::ObjCMultiArgSelector: {
58 Selector S = Name.getObjCSelector();
59 AddBoolean(S.isNull());
60 AddBoolean(S.isKeywordSelector());
61 AddBoolean(S.isUnarySelector());
62 unsigned NumArgs = S.getNumArgs();
63 for (unsigned i = 0; i < NumArgs; ++i) {
64 AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
65 }
66 break;
67 }
68 case DeclarationName::CXXConstructorName:
69 case DeclarationName::CXXDestructorName:
70 AddQualType(Name.getCXXNameType());
71 break;
72 case DeclarationName::CXXOperatorName:
73 ID.AddInteger(Name.getCXXOverloadedOperator());
74 break;
75 case DeclarationName::CXXLiteralOperatorName:
76 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
77 break;
78 case DeclarationName::CXXConversionFunctionName:
79 AddQualType(Name.getCXXNameType());
80 break;
81 case DeclarationName::CXXUsingDirective:
82 break;
83 case DeclarationName::CXXDeductionGuideName: {
84 auto *Template = Name.getCXXDeductionGuideTemplate();
85 AddBoolean(Template);
86 if (Template) {
87 AddDecl(Template);
88 }
89 }
90 }
91}
92
Richard Trieuce81b192017-05-17 03:23:35 +000093void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieu96b41642017-07-01 02:00:05 +000094 assert(NNS && "Expecting non-null pointer.");
95 const auto *Prefix = NNS->getPrefix();
96 AddBoolean(Prefix);
97 if (Prefix) {
98 AddNestedNameSpecifier(Prefix);
Richard Trieuce81b192017-05-17 03:23:35 +000099 }
100 auto Kind = NNS->getKind();
101 ID.AddInteger(Kind);
102 switch (Kind) {
103 case NestedNameSpecifier::Identifier:
104 AddIdentifierInfo(NNS->getAsIdentifier());
105 break;
106 case NestedNameSpecifier::Namespace:
107 AddDecl(NNS->getAsNamespace());
108 break;
109 case NestedNameSpecifier::NamespaceAlias:
110 AddDecl(NNS->getAsNamespaceAlias());
111 break;
112 case NestedNameSpecifier::TypeSpec:
113 case NestedNameSpecifier::TypeSpecWithTemplate:
114 AddType(NNS->getAsType());
115 break;
116 case NestedNameSpecifier::Global:
117 case NestedNameSpecifier::Super:
118 break;
119 }
120}
121
Richard Trieu96b49622017-05-31 00:31:58 +0000122void ODRHash::AddTemplateName(TemplateName Name) {
123 auto Kind = Name.getKind();
124 ID.AddInteger(Kind);
125
126 switch (Kind) {
127 case TemplateName::Template:
128 AddDecl(Name.getAsTemplateDecl());
129 break;
130 // TODO: Support these cases.
131 case TemplateName::OverloadedTemplate:
132 case TemplateName::QualifiedTemplate:
133 case TemplateName::DependentTemplate:
134 case TemplateName::SubstTemplateTemplateParm:
135 case TemplateName::SubstTemplateTemplateParmPack:
136 break;
137 }
138}
139
Richard Trieu3b261bb72017-06-13 22:21:18 +0000140void ODRHash::AddTemplateArgument(TemplateArgument TA) {
141 const auto Kind = TA.getKind();
142 ID.AddInteger(Kind);
Richard Trieu1dcb4052017-06-14 01:28:00 +0000143
144 switch (Kind) {
145 case TemplateArgument::Null:
Richard Trieu8844c522017-06-30 22:40:33 +0000146 llvm_unreachable("Expected valid TemplateArgument");
Richard Trieu1dcb4052017-06-14 01:28:00 +0000147 case TemplateArgument::Type:
Richard Trieu8844c522017-06-30 22:40:33 +0000148 AddQualType(TA.getAsType());
149 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000150 case TemplateArgument::Declaration:
Richard Trieu7282d322018-04-25 00:31:15 +0000151 AddDecl(TA.getAsDecl());
152 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000153 case TemplateArgument::NullPtr:
154 case TemplateArgument::Integral:
Richard Trieuee132d62017-06-14 03:17:26 +0000155 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000156 case TemplateArgument::Template:
157 case TemplateArgument::TemplateExpansion:
Richard Trieuee132d62017-06-14 03:17:26 +0000158 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
Richard Trieu1dcb4052017-06-14 01:28:00 +0000159 break;
160 case TemplateArgument::Expression:
161 AddStmt(TA.getAsExpr());
162 break;
163 case TemplateArgument::Pack:
Richard Trieud9201d02017-06-15 01:35:06 +0000164 ID.AddInteger(TA.pack_size());
165 for (auto SubTA : TA.pack_elements()) {
166 AddTemplateArgument(SubTA);
167 }
Richard Trieu1dcb4052017-06-14 01:28:00 +0000168 break;
169 }
Richard Trieu3b261bb72017-06-13 22:21:18 +0000170}
171
Richard Trieu498117b2017-08-23 02:43:59 +0000172void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
173 assert(TPL && "Expecting non-null pointer.");
174
175 ID.AddInteger(TPL->size());
176 for (auto *ND : TPL->asArray()) {
177 AddSubDecl(ND);
178 }
179}
Richard Trieue7f7ed22017-02-22 01:11:25 +0000180
181void ODRHash::clear() {
Richard Trieuf65efae2018-02-22 05:32:25 +0000182 DeclNameMap.clear();
Richard Trieue7f7ed22017-02-22 01:11:25 +0000183 TypeMap.clear();
184 Bools.clear();
185 ID.clear();
186}
187
188unsigned ODRHash::CalculateHash() {
189 // Append the bools to the end of the data segment backwards. This allows
190 // for the bools data to be compressed 32 times smaller compared to using
191 // ID.AddBoolean
192 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
193 const unsigned size = Bools.size();
194 const unsigned remainder = size % unsigned_bits;
195 const unsigned loops = size / unsigned_bits;
196 auto I = Bools.rbegin();
197 unsigned value = 0;
198 for (unsigned i = 0; i < remainder; ++i) {
199 value <<= 1;
200 value |= *I;
201 ++I;
202 }
203 ID.AddInteger(value);
204
205 for (unsigned i = 0; i < loops; ++i) {
206 value = 0;
207 for (unsigned j = 0; j < unsigned_bits; ++j) {
208 value <<= 1;
209 value |= *I;
210 ++I;
211 }
212 ID.AddInteger(value);
213 }
214
215 assert(I == Bools.rend());
216 Bools.clear();
217 return ID.ComputeHash();
218}
219
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000220namespace {
Richard Trieue7f7ed22017-02-22 01:11:25 +0000221// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
222// methods process the relevant parts of the Decl.
223class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
224 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
225 llvm::FoldingSetNodeID &ID;
Richard Trieu639d7b62017-02-22 22:22:42 +0000226 ODRHash &Hash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000227
228public:
Richard Trieu639d7b62017-02-22 22:22:42 +0000229 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
230 : ID(ID), Hash(Hash) {}
231
232 void AddStmt(const Stmt *S) {
233 Hash.AddBoolean(S);
234 if (S) {
235 Hash.AddStmt(S);
236 }
237 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000238
Richard Trieud0786092017-02-23 00:23:01 +0000239 void AddIdentifierInfo(const IdentifierInfo *II) {
240 Hash.AddBoolean(II);
241 if (II) {
242 Hash.AddIdentifierInfo(II);
243 }
244 }
245
Richard Trieubcaaf962017-02-23 03:25:57 +0000246 void AddQualType(QualType T) {
247 Hash.AddQualType(T);
248 }
249
Richard Trieuac6a1b62017-07-08 02:04:42 +0000250 void AddDecl(const Decl *D) {
251 Hash.AddBoolean(D);
252 if (D) {
253 Hash.AddDecl(D);
254 }
255 }
256
Richard Trieu498117b2017-08-23 02:43:59 +0000257 void AddTemplateArgument(TemplateArgument TA) {
258 Hash.AddTemplateArgument(TA);
259 }
260
Richard Trieue7f7ed22017-02-22 01:11:25 +0000261 void Visit(const Decl *D) {
262 ID.AddInteger(D->getKind());
263 Inherited::Visit(D);
264 }
265
Richard Trieud0786092017-02-23 00:23:01 +0000266 void VisitNamedDecl(const NamedDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000267 Hash.AddDeclarationName(D->getDeclName());
Richard Trieud0786092017-02-23 00:23:01 +0000268 Inherited::VisitNamedDecl(D);
269 }
270
Richard Trieubcaaf962017-02-23 03:25:57 +0000271 void VisitValueDecl(const ValueDecl *D) {
Richard Trieu9747a7c2017-07-14 01:36:41 +0000272 if (!isa<FunctionDecl>(D)) {
273 AddQualType(D->getType());
274 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000275 Inherited::VisitValueDecl(D);
276 }
277
Richard Trieu6e13ff32017-06-16 02:44:29 +0000278 void VisitVarDecl(const VarDecl *D) {
279 Hash.AddBoolean(D->isStaticLocal());
280 Hash.AddBoolean(D->isConstexpr());
281 const bool HasInit = D->hasInit();
282 Hash.AddBoolean(HasInit);
283 if (HasInit) {
284 AddStmt(D->getInit());
285 }
286 Inherited::VisitVarDecl(D);
287 }
288
Richard Trieu02552272017-05-02 23:58:52 +0000289 void VisitParmVarDecl(const ParmVarDecl *D) {
290 // TODO: Handle default arguments.
291 Inherited::VisitParmVarDecl(D);
292 }
293
Richard Trieue7f7ed22017-02-22 01:11:25 +0000294 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
295 ID.AddInteger(D->getAccess());
296 Inherited::VisitAccessSpecDecl(D);
297 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000298
299 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
300 AddStmt(D->getAssertExpr());
301 AddStmt(D->getMessage());
302
303 Inherited::VisitStaticAssertDecl(D);
304 }
Richard Trieud0786092017-02-23 00:23:01 +0000305
306 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000307 const bool IsBitfield = D->isBitField();
308 Hash.AddBoolean(IsBitfield);
309
310 if (IsBitfield) {
311 AddStmt(D->getBitWidth());
312 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000313
314 Hash.AddBoolean(D->isMutable());
315 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000316
317 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000318 }
Richard Trieu48143742017-02-28 21:24:38 +0000319
320 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000321 ID.AddInteger(D->getStorageClass());
322 Hash.AddBoolean(D->isInlineSpecified());
323 Hash.AddBoolean(D->isVirtualAsWritten());
324 Hash.AddBoolean(D->isPure());
325 Hash.AddBoolean(D->isDeletedAsWritten());
326
Richard Trieu02552272017-05-02 23:58:52 +0000327 ID.AddInteger(D->param_size());
328
329 for (auto *Param : D->parameters()) {
330 Hash.AddSubDecl(Param);
331 }
332
Richard Trieu9747a7c2017-07-14 01:36:41 +0000333 AddQualType(D->getReturnType());
334
Richard Trieu7282d322018-04-25 00:31:15 +0000335 const auto* SpecializationArgs = D->getTemplateSpecializationArgs();
336 Hash.AddBoolean(SpecializationArgs);
337 if (SpecializationArgs) {
338 ID.AddInteger(SpecializationArgs->size());
339 for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
340 Hash.AddTemplateArgument(TA);
341 }
342 }
343
Richard Trieu48143742017-02-28 21:24:38 +0000344 Inherited::VisitFunctionDecl(D);
345 }
346
347 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000348 Hash.AddBoolean(D->isConst());
349 Hash.AddBoolean(D->isVolatile());
350
Richard Trieu48143742017-02-28 21:24:38 +0000351 Inherited::VisitCXXMethodDecl(D);
352 }
Richard Trieu33562c22017-03-08 00:13:19 +0000353
354 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
355 AddQualType(D->getUnderlyingType());
356
357 Inherited::VisitTypedefNameDecl(D);
358 }
359
360 void VisitTypedefDecl(const TypedefDecl *D) {
361 Inherited::VisitTypedefDecl(D);
362 }
363
364 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
365 Inherited::VisitTypeAliasDecl(D);
366 }
Richard Trieuac6a1b62017-07-08 02:04:42 +0000367
368 void VisitFriendDecl(const FriendDecl *D) {
369 TypeSourceInfo *TSI = D->getFriendType();
370 Hash.AddBoolean(TSI);
371 if (TSI) {
372 AddQualType(TSI->getType());
373 } else {
374 AddDecl(D->getFriendDecl());
375 }
376 }
Richard Trieu498117b2017-08-23 02:43:59 +0000377
378 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
379 // Only care about default arguments as part of the definition.
380 const bool hasDefaultArgument =
381 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
382 Hash.AddBoolean(hasDefaultArgument);
383 if (hasDefaultArgument) {
384 AddTemplateArgument(D->getDefaultArgument());
385 }
386
387 Inherited::VisitTemplateTypeParmDecl(D);
388 }
389
390 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
391 // Only care about default arguments as part of the definition.
392 const bool hasDefaultArgument =
393 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
394 Hash.AddBoolean(hasDefaultArgument);
395 if (hasDefaultArgument) {
396 AddStmt(D->getDefaultArgument());
397 }
398
399 Inherited::VisitNonTypeTemplateParmDecl(D);
400 }
401
402 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
403 // Only care about default arguments as part of the definition.
404 const bool hasDefaultArgument =
405 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
406 Hash.AddBoolean(hasDefaultArgument);
407 if (hasDefaultArgument) {
408 AddTemplateArgument(D->getDefaultArgument().getArgument());
409 }
410
411 Inherited::VisitTemplateTemplateParmDecl(D);
412 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000413};
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000414} // namespace
Richard Trieue7f7ed22017-02-22 01:11:25 +0000415
416// Only allow a small portion of Decl's to be processed. Remove this once
417// all Decl's can be handled.
418bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
419 if (D->isImplicit()) return false;
420 if (D->getDeclContext() != Parent) return false;
421
422 switch (D->getKind()) {
423 default:
424 return false;
425 case Decl::AccessSpec:
Richard Trieu1c71d512017-07-15 02:55:13 +0000426 case Decl::CXXConstructor:
427 case Decl::CXXDestructor:
Richard Trieu48143742017-02-28 21:24:38 +0000428 case Decl::CXXMethod:
Richard Trieud0786092017-02-23 00:23:01 +0000429 case Decl::Field:
Richard Trieuac6a1b62017-07-08 02:04:42 +0000430 case Decl::Friend:
Richard Trieu639d7b62017-02-22 22:22:42 +0000431 case Decl::StaticAssert:
Richard Trieu33562c22017-03-08 00:13:19 +0000432 case Decl::TypeAlias:
433 case Decl::Typedef:
Richard Trieu6e13ff32017-06-16 02:44:29 +0000434 case Decl::Var:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000435 return true;
436 }
437}
438
439void ODRHash::AddSubDecl(const Decl *D) {
440 assert(D && "Expecting non-null pointer.");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000441
Richard Trieu639d7b62017-02-22 22:22:42 +0000442 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000443}
444
445void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
446 assert(Record && Record->hasDefinition() &&
447 "Expected non-null record to be a definition.");
Richard Trieu02552272017-05-02 23:58:52 +0000448
Richard Trieu5fb82ef2017-08-05 00:54:19 +0000449 const DeclContext *DC = Record;
450 while (DC) {
451 if (isa<ClassTemplateSpecializationDecl>(DC)) {
452 return;
453 }
454 DC = DC->getParent();
Richard Trieu02552272017-05-02 23:58:52 +0000455 }
456
Richard Trieue7f7ed22017-02-22 01:11:25 +0000457 AddDecl(Record);
458
459 // Filter out sub-Decls which will not be processed in order to get an
460 // accurate count of Decl's.
461 llvm::SmallVector<const Decl *, 16> Decls;
462 for (const Decl *SubDecl : Record->decls()) {
463 if (isWhitelistedDecl(SubDecl, Record)) {
464 Decls.push_back(SubDecl);
465 }
466 }
467
468 ID.AddInteger(Decls.size());
469 for (auto SubDecl : Decls) {
470 AddSubDecl(SubDecl);
471 }
Richard Trieu498117b2017-08-23 02:43:59 +0000472
473 const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
474 AddBoolean(TD);
475 if (TD) {
476 AddTemplateParameterList(TD->getTemplateParameters());
477 }
Richard Trieue13eabe2017-09-30 02:19:17 +0000478
479 ID.AddInteger(Record->getNumBases());
480 auto Bases = Record->bases();
481 for (auto Base : Bases) {
482 AddQualType(Base.getType());
483 ID.AddInteger(Base.isVirtual());
484 ID.AddInteger(Base.getAccessSpecifierAsWritten());
485 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000486}
487
Richard Trieue6caa262017-12-23 00:41:01 +0000488void ODRHash::AddFunctionDecl(const FunctionDecl *Function) {
489 assert(Function && "Expecting non-null pointer.");
490
491 // Skip hashing these kinds of function.
492 if (Function->isImplicit()) return;
493 if (Function->isDefaulted()) return;
494 if (Function->isDeleted()) return;
495 if (!Function->hasBody()) return;
496 if (!Function->getBody()) return;
497
498 // Skip functions that are specializations or in specialization context.
499 const DeclContext *DC = Function;
500 while (DC) {
501 if (isa<ClassTemplateSpecializationDecl>(DC)) return;
502 if (auto *F = dyn_cast<FunctionDecl>(DC))
503 if (F->isFunctionTemplateSpecialization()) return;
504 DC = DC->getParent();
505 }
506
507 AddDecl(Function);
508
509 AddQualType(Function->getReturnType());
510
511 ID.AddInteger(Function->param_size());
512 for (auto Param : Function->parameters())
513 AddSubDecl(Param);
514
515 AddStmt(Function->getBody());
516}
517
Richard Trieue7f7ed22017-02-22 01:11:25 +0000518void ODRHash::AddDecl(const Decl *D) {
519 assert(D && "Expecting non-null pointer.");
Richard Trieuf72fb7e2017-12-21 22:38:29 +0000520 D = D->getCanonicalDecl();
Richard Trieuf65efae2018-02-22 05:32:25 +0000521
522 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
523 AddDeclarationName(ND->getDeclName());
Richard Trieue7f7ed22017-02-22 01:11:25 +0000524 return;
525 }
526
527 ID.AddInteger(D->getKind());
Richard Trieuf65efae2018-02-22 05:32:25 +0000528 // TODO: Handle non-NamedDecl here.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000529}
530
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000531namespace {
Richard Trieubcaaf962017-02-23 03:25:57 +0000532// Process a Type pointer. Add* methods call back into ODRHash while Visit*
533// methods process the relevant parts of the Type.
534class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
535 typedef TypeVisitor<ODRTypeVisitor> Inherited;
536 llvm::FoldingSetNodeID &ID;
537 ODRHash &Hash;
538
539public:
540 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
541 : ID(ID), Hash(Hash) {}
542
543 void AddStmt(Stmt *S) {
544 Hash.AddBoolean(S);
545 if (S) {
546 Hash.AddStmt(S);
547 }
548 }
549
Richard Trieu8459ddf2017-02-24 02:59:12 +0000550 void AddDecl(Decl *D) {
551 Hash.AddBoolean(D);
552 if (D) {
553 Hash.AddDecl(D);
554 }
555 }
556
Richard Trieu02552272017-05-02 23:58:52 +0000557 void AddQualType(QualType T) {
558 Hash.AddQualType(T);
559 }
560
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000561 void AddType(const Type *T) {
562 Hash.AddBoolean(T);
563 if (T) {
564 Hash.AddType(T);
565 }
566 }
567
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000568 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieu96b41642017-07-01 02:00:05 +0000569 Hash.AddBoolean(NNS);
570 if (NNS) {
571 Hash.AddNestedNameSpecifier(NNS);
572 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000573 }
574
575 void AddIdentifierInfo(const IdentifierInfo *II) {
576 Hash.AddBoolean(II);
577 if (II) {
578 Hash.AddIdentifierInfo(II);
579 }
580 }
581
Richard Trieu02552272017-05-02 23:58:52 +0000582 void VisitQualifiers(Qualifiers Quals) {
583 ID.AddInteger(Quals.getAsOpaqueValue());
584 }
585
Richard Trieubcaaf962017-02-23 03:25:57 +0000586 void Visit(const Type *T) {
587 ID.AddInteger(T->getTypeClass());
588 Inherited::Visit(T);
589 }
590
591 void VisitType(const Type *T) {}
592
Richard Trieu02552272017-05-02 23:58:52 +0000593 void VisitAdjustedType(const AdjustedType *T) {
594 AddQualType(T->getOriginalType());
595 AddQualType(T->getAdjustedType());
596 VisitType(T);
597 }
598
599 void VisitDecayedType(const DecayedType *T) {
600 AddQualType(T->getDecayedType());
601 AddQualType(T->getPointeeType());
602 VisitAdjustedType(T);
603 }
604
605 void VisitArrayType(const ArrayType *T) {
606 AddQualType(T->getElementType());
607 ID.AddInteger(T->getSizeModifier());
608 VisitQualifiers(T->getIndexTypeQualifiers());
609 VisitType(T);
610 }
611 void VisitConstantArrayType(const ConstantArrayType *T) {
612 T->getSize().Profile(ID);
613 VisitArrayType(T);
614 }
615
616 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
617 AddStmt(T->getSizeExpr());
618 VisitArrayType(T);
619 }
620
621 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
622 VisitArrayType(T);
623 }
624
625 void VisitVariableArrayType(const VariableArrayType *T) {
626 AddStmt(T->getSizeExpr());
627 VisitArrayType(T);
628 }
629
Richard Trieubcaaf962017-02-23 03:25:57 +0000630 void VisitBuiltinType(const BuiltinType *T) {
631 ID.AddInteger(T->getKind());
632 VisitType(T);
633 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000634
Richard Trieu02552272017-05-02 23:58:52 +0000635 void VisitFunctionType(const FunctionType *T) {
636 AddQualType(T->getReturnType());
637 T->getExtInfo().Profile(ID);
638 Hash.AddBoolean(T->isConst());
639 Hash.AddBoolean(T->isVolatile());
640 Hash.AddBoolean(T->isRestrict());
641 VisitType(T);
642 }
643
644 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
645 VisitFunctionType(T);
646 }
647
648 void VisitFunctionProtoType(const FunctionProtoType *T) {
649 ID.AddInteger(T->getNumParams());
650 for (auto ParamType : T->getParamTypes())
651 AddQualType(ParamType);
652
653 VisitFunctionType(T);
654 }
655
Richard Trieu15970af2018-04-13 22:34:43 +0000656 void VisitPointerType(const PointerType *T) {
657 AddQualType(T->getPointeeType());
658 VisitType(T);
659 }
660
661 void VisitReferenceType(const ReferenceType *T) {
662 AddQualType(T->getPointeeTypeAsWritten());
663 VisitType(T);
664 }
665
666 void VisitLValueReferenceType(const LValueReferenceType *T) {
667 VisitReferenceType(T);
668 }
669
670 void VisitRValueReferenceType(const RValueReferenceType *T) {
671 VisitReferenceType(T);
672 }
673
Richard Trieu8459ddf2017-02-24 02:59:12 +0000674 void VisitTypedefType(const TypedefType *T) {
675 AddDecl(T->getDecl());
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000676 QualType UnderlyingType = T->getDecl()->getUnderlyingType();
677 VisitQualifiers(UnderlyingType.getQualifiers());
Richard Trieucaaccee2018-04-12 02:26:49 +0000678 while (true) {
679 if (const TypedefType *Underlying =
680 dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
681 UnderlyingType = Underlying->getDecl()->getUnderlyingType();
682 continue;
683 }
684 if (const ElaboratedType *Underlying =
685 dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
686 UnderlyingType = Underlying->getNamedType();
687 continue;
688 }
689
690 break;
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000691 }
692 AddType(UnderlyingType.getTypePtr());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000693 VisitType(T);
694 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000695
696 void VisitTagType(const TagType *T) {
697 AddDecl(T->getDecl());
698 VisitType(T);
699 }
700
701 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
702 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
703
704 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
705 ID.AddInteger(T->getKeyword());
706 VisitType(T);
707 };
708
709 void VisitDependentNameType(const DependentNameType *T) {
710 AddNestedNameSpecifier(T->getQualifier());
711 AddIdentifierInfo(T->getIdentifier());
712 VisitTypeWithKeyword(T);
713 }
714
715 void VisitDependentTemplateSpecializationType(
716 const DependentTemplateSpecializationType *T) {
717 AddIdentifierInfo(T->getIdentifier());
718 AddNestedNameSpecifier(T->getQualifier());
719 ID.AddInteger(T->getNumArgs());
720 for (const auto &TA : T->template_arguments()) {
721 Hash.AddTemplateArgument(TA);
722 }
723 VisitTypeWithKeyword(T);
724 }
725
726 void VisitElaboratedType(const ElaboratedType *T) {
727 AddNestedNameSpecifier(T->getQualifier());
728 AddQualType(T->getNamedType());
729 VisitTypeWithKeyword(T);
730 }
Richard Trieu96b49622017-05-31 00:31:58 +0000731
732 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
733 ID.AddInteger(T->getNumArgs());
734 for (const auto &TA : T->template_arguments()) {
735 Hash.AddTemplateArgument(TA);
736 }
737 Hash.AddTemplateName(T->getTemplateName());
738 VisitType(T);
739 }
Richard Trieud9201d02017-06-15 01:35:06 +0000740
741 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
742 ID.AddInteger(T->getDepth());
743 ID.AddInteger(T->getIndex());
744 Hash.AddBoolean(T->isParameterPack());
745 AddDecl(T->getDecl());
746 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000747};
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000748} // namespace
Richard Trieubcaaf962017-02-23 03:25:57 +0000749
750void ODRHash::AddType(const Type *T) {
751 assert(T && "Expecting non-null pointer.");
752 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
753 ID.AddInteger(Result.first->second);
754 // On first encounter of a Type pointer, process it. Every time afterwards,
755 // only the index value is needed.
756 if (!Result.second) {
757 return;
758 }
759
760 ODRTypeVisitor(ID, *this).Visit(T);
761}
762
763void ODRHash::AddQualType(QualType T) {
764 AddBoolean(T.isNull());
765 if (T.isNull())
766 return;
767 SplitQualType split = T.split();
768 ID.AddInteger(split.Quals.getAsOpaqueValue());
769 AddType(split.Ty);
770}
771
Richard Trieue7f7ed22017-02-22 01:11:25 +0000772void ODRHash::AddBoolean(bool Value) {
773 Bools.push_back(Value);
774}