Record.h 3.8 KB

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