Record.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===--- Record.h - struct and class metadata for the VM --------*- 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. // A record is part of a program to describe the layout and methods of a struct.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_AST_INTERP_RECORD_H
  13. #define LLVM_CLANG_AST_INTERP_RECORD_H
  14. #include "Descriptor.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclCXX.h"
  17. namespace clang {
  18. namespace interp {
  19. class Program;
  20. /// Structure/Class descriptor.
  21. class Record final {
  22. public:
  23. /// Describes a record field.
  24. struct Field {
  25. const FieldDecl *Decl;
  26. unsigned Offset;
  27. Descriptor *Desc;
  28. };
  29. /// Describes a base class.
  30. struct Base {
  31. const RecordDecl *Decl;
  32. unsigned Offset;
  33. Descriptor *Desc;
  34. Record *R;
  35. };
  36. /// Mapping from identifiers to field descriptors.
  37. using FieldList = llvm::SmallVector<Field, 8>;
  38. /// Mapping from identifiers to base classes.
  39. using BaseList = llvm::SmallVector<Base, 8>;
  40. /// List of virtual base classes.
  41. using VirtualBaseList = llvm::SmallVector<Base, 2>;
  42. public:
  43. /// Returns the underlying declaration.
  44. const RecordDecl *getDecl() const { return Decl; }
  45. /// Returns the name of the underlying declaration.
  46. const std::string getName() const { return Decl->getNameAsString(); }
  47. /// Checks if the record is a union.
  48. bool isUnion() const { return getDecl()->isUnion(); }
  49. /// Returns the size of the record.
  50. unsigned getSize() const { return BaseSize; }
  51. /// Returns the full size of the record, including records.
  52. unsigned getFullSize() const { return BaseSize + VirtualSize; }
  53. /// Returns a field.
  54. const Field *getField(const FieldDecl *FD) const;
  55. /// Returns a base descriptor.
  56. const Base *getBase(const RecordDecl *FD) const;
  57. /// Returns a virtual base descriptor.
  58. const Base *getVirtualBase(const RecordDecl *RD) const;
  59. // Returns the destructor of the record, if any.
  60. const CXXDestructorDecl *getDestructor() const {
  61. if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(Decl))
  62. return CXXDecl->getDestructor();
  63. return nullptr;
  64. }
  65. using const_field_iter = FieldList::const_iterator;
  66. llvm::iterator_range<const_field_iter> fields() const {
  67. return llvm::make_range(Fields.begin(), Fields.end());
  68. }
  69. unsigned getNumFields() const { return Fields.size(); }
  70. const Field *getField(unsigned I) const { return &Fields[I]; }
  71. Field *getField(unsigned I) { return &Fields[I]; }
  72. using const_base_iter = BaseList::const_iterator;
  73. llvm::iterator_range<const_base_iter> bases() const {
  74. return llvm::make_range(Bases.begin(), Bases.end());
  75. }
  76. unsigned getNumBases() const { return Bases.size(); }
  77. Base *getBase(unsigned I) { return &Bases[I]; }
  78. using const_virtual_iter = VirtualBaseList::const_iterator;
  79. llvm::iterator_range<const_virtual_iter> virtual_bases() const {
  80. return llvm::make_range(VirtualBases.begin(), VirtualBases.end());
  81. }
  82. unsigned getNumVirtualBases() const { return VirtualBases.size(); }
  83. Base *getVirtualBase(unsigned I) { return &VirtualBases[I]; }
  84. private:
  85. /// Constructor used by Program to create record descriptors.
  86. Record(const RecordDecl *, BaseList &&Bases, FieldList &&Fields,
  87. VirtualBaseList &&VirtualBases, unsigned VirtualSize,
  88. unsigned BaseSize);
  89. private:
  90. friend class Program;
  91. /// Original declaration.
  92. const RecordDecl *Decl;
  93. /// List of all base classes.
  94. BaseList Bases;
  95. /// List of all the fields in the record.
  96. FieldList Fields;
  97. /// List o fall virtual bases.
  98. VirtualBaseList VirtualBases;
  99. /// Mapping from declarations to bases.
  100. llvm::DenseMap<const RecordDecl *, Base *> BaseMap;
  101. /// Mapping from field identifiers to descriptors.
  102. llvm::DenseMap<const FieldDecl *, Field *> FieldMap;
  103. /// Mapping from declarations to virtual bases.
  104. llvm::DenseMap<const RecordDecl *, Base *> VirtualBaseMap;
  105. /// Size of the structure.
  106. unsigned BaseSize;
  107. /// Size of all virtual bases.
  108. unsigned VirtualSize;
  109. };
  110. } // namespace interp
  111. } // namespace clang
  112. #endif