ASTConcept.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "clang/AST/ASTConcept.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/TemplateBase.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/FoldingSet.h"
  19. using namespace clang;
  20. namespace {
  21. void CreatUnsatisfiedConstraintRecord(
  22. const ASTContext &C, const UnsatisfiedConstraintRecord &Detail,
  23. UnsatisfiedConstraintRecord *TrailingObject) {
  24. if (Detail.second.is<Expr *>())
  25. new (TrailingObject) UnsatisfiedConstraintRecord{
  26. Detail.first,
  27. UnsatisfiedConstraintRecord::second_type(Detail.second.get<Expr *>())};
  28. else {
  29. auto &SubstitutionDiagnostic =
  30. *Detail.second.get<std::pair<SourceLocation, StringRef> *>();
  31. unsigned MessageSize = SubstitutionDiagnostic.second.size();
  32. char *Mem = new (C) char[MessageSize];
  33. memcpy(Mem, SubstitutionDiagnostic.second.data(), MessageSize);
  34. auto *NewSubstDiag = new (C) std::pair<SourceLocation, StringRef>(
  35. SubstitutionDiagnostic.first, StringRef(Mem, MessageSize));
  36. new (TrailingObject) UnsatisfiedConstraintRecord{
  37. Detail.first, UnsatisfiedConstraintRecord::second_type(NewSubstDiag)};
  38. }
  39. }
  40. } // namespace
  41. ASTConstraintSatisfaction::ASTConstraintSatisfaction(
  42. const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
  43. : NumRecords{Satisfaction.Details.size()},
  44. IsSatisfied{Satisfaction.IsSatisfied}, ContainsErrors{
  45. Satisfaction.ContainsErrors} {
  46. for (unsigned I = 0; I < NumRecords; ++I)
  47. CreatUnsatisfiedConstraintRecord(
  48. C, Satisfaction.Details[I],
  49. getTrailingObjects<UnsatisfiedConstraintRecord>() + I);
  50. }
  51. ASTConstraintSatisfaction::ASTConstraintSatisfaction(
  52. const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction)
  53. : NumRecords{Satisfaction.NumRecords},
  54. IsSatisfied{Satisfaction.IsSatisfied},
  55. ContainsErrors{Satisfaction.ContainsErrors} {
  56. for (unsigned I = 0; I < NumRecords; ++I)
  57. CreatUnsatisfiedConstraintRecord(
  58. C, *(Satisfaction.begin() + I),
  59. getTrailingObjects<UnsatisfiedConstraintRecord>() + I);
  60. }
  61. ASTConstraintSatisfaction *
  62. ASTConstraintSatisfaction::Create(const ASTContext &C,
  63. const ConstraintSatisfaction &Satisfaction) {
  64. std::size_t size =
  65. totalSizeToAlloc<UnsatisfiedConstraintRecord>(
  66. Satisfaction.Details.size());
  67. void *Mem = C.Allocate(size, alignof(ASTConstraintSatisfaction));
  68. return new (Mem) ASTConstraintSatisfaction(C, Satisfaction);
  69. }
  70. ASTConstraintSatisfaction *ASTConstraintSatisfaction::Rebuild(
  71. const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction) {
  72. std::size_t size =
  73. totalSizeToAlloc<UnsatisfiedConstraintRecord>(Satisfaction.NumRecords);
  74. void *Mem = C.Allocate(size, alignof(ASTConstraintSatisfaction));
  75. return new (Mem) ASTConstraintSatisfaction(C, Satisfaction);
  76. }
  77. void ConstraintSatisfaction::Profile(
  78. llvm::FoldingSetNodeID &ID, const ASTContext &C,
  79. const NamedDecl *ConstraintOwner, ArrayRef<TemplateArgument> TemplateArgs) {
  80. ID.AddPointer(ConstraintOwner);
  81. ID.AddInteger(TemplateArgs.size());
  82. for (auto &Arg : TemplateArgs)
  83. Arg.Profile(ID, C);
  84. }