xref: /freebsd/contrib/llvm-project/clang/lib/AST/ASTConcept.cpp (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 //===--- ASTConcept.cpp - Concepts Related AST Data Structures --*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// \brief This file defines AST data structures related to concepts.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/ASTConcept.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/TemplateBase.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/FoldingSet.h"
20 using namespace clang;
21 
22 ASTConstraintSatisfaction::ASTConstraintSatisfaction(const ASTContext &C,
23     const ConstraintSatisfaction &Satisfaction):
24     NumRecords{Satisfaction.Details.size()},
25     IsSatisfied{Satisfaction.IsSatisfied} {
26   for (unsigned I = 0; I < NumRecords; ++I) {
27     auto &Detail = Satisfaction.Details[I];
28     if (Detail.second.is<Expr *>())
29       new (getTrailingObjects<UnsatisfiedConstraintRecord>() + I)
30          UnsatisfiedConstraintRecord{Detail.first,
31                                      UnsatisfiedConstraintRecord::second_type(
32                                          Detail.second.get<Expr *>())};
33     else {
34       auto &SubstitutionDiagnostic =
35           *Detail.second.get<std::pair<SourceLocation, StringRef> *>();
36       unsigned MessageSize = SubstitutionDiagnostic.second.size();
37       char *Mem = new (C) char[MessageSize];
38       memcpy(Mem, SubstitutionDiagnostic.second.data(), MessageSize);
39       auto *NewSubstDiag = new (C) std::pair<SourceLocation, StringRef>(
40           SubstitutionDiagnostic.first, StringRef(Mem, MessageSize));
41       new (getTrailingObjects<UnsatisfiedConstraintRecord>() + I)
42          UnsatisfiedConstraintRecord{Detail.first,
43                                      UnsatisfiedConstraintRecord::second_type(
44                                          NewSubstDiag)};
45     }
46   }
47 }
48 
49 
50 ASTConstraintSatisfaction *
51 ASTConstraintSatisfaction::Create(const ASTContext &C,
52                                   const ConstraintSatisfaction &Satisfaction) {
53   std::size_t size =
54       totalSizeToAlloc<UnsatisfiedConstraintRecord>(
55           Satisfaction.Details.size());
56   void *Mem = C.Allocate(size, alignof(ASTConstraintSatisfaction));
57   return new (Mem) ASTConstraintSatisfaction(C, Satisfaction);
58 }
59 
60 void ConstraintSatisfaction::Profile(
61     llvm::FoldingSetNodeID &ID, const ASTContext &C,
62     const NamedDecl *ConstraintOwner, ArrayRef<TemplateArgument> TemplateArgs) {
63   ID.AddPointer(ConstraintOwner);
64   ID.AddInteger(TemplateArgs.size());
65   for (auto &Arg : TemplateArgs)
66     Arg.Profile(ID, C);
67 }
68