CXXFieldCollector.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CXXFieldCollector.h - Utility class for C++ class semantic analysis ===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file provides CXXFieldCollector that is used during parsing & semantic
  15. // analysis of C++ classes.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_SEMA_CXXFIELDCOLLECTOR_H
  19. #define LLVM_CLANG_SEMA_CXXFIELDCOLLECTOR_H
  20. #include "clang/Basic/LLVM.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. namespace clang {
  23. class FieldDecl;
  24. /// CXXFieldCollector - Used to keep track of CXXFieldDecls during parsing of
  25. /// C++ classes.
  26. class CXXFieldCollector {
  27. /// Fields - Contains all FieldDecls collected during parsing of a C++
  28. /// class. When a nested class is entered, its fields are appended to the
  29. /// fields of its parent class, when it is exited its fields are removed.
  30. SmallVector<FieldDecl*, 32> Fields;
  31. /// FieldCount - Each entry represents the number of fields collected during
  32. /// the parsing of a C++ class. When a nested class is entered, a new field
  33. /// count is pushed, when it is exited, the field count is popped.
  34. SmallVector<size_t, 4> FieldCount;
  35. // Example:
  36. //
  37. // class C {
  38. // int x,y;
  39. // class NC {
  40. // int q;
  41. // // At this point, Fields contains [x,y,q] decls and FieldCount contains
  42. // // [2,1].
  43. // };
  44. // int z;
  45. // // At this point, Fields contains [x,y,z] decls and FieldCount contains
  46. // // [3].
  47. // };
  48. public:
  49. /// StartClass - Called by Sema::ActOnStartCXXClassDef.
  50. void StartClass() { FieldCount.push_back(0); }
  51. /// Add - Called by Sema::ActOnCXXMemberDeclarator.
  52. void Add(FieldDecl *D) {
  53. Fields.push_back(D);
  54. ++FieldCount.back();
  55. }
  56. /// getCurNumField - The number of fields added to the currently parsed class.
  57. size_t getCurNumFields() const {
  58. assert(!FieldCount.empty() && "no currently-parsed class");
  59. return FieldCount.back();
  60. }
  61. /// getCurFields - Pointer to array of fields added to the currently parsed
  62. /// class.
  63. FieldDecl **getCurFields() { return &*(Fields.end() - getCurNumFields()); }
  64. /// FinishClass - Called by Sema::ActOnFinishCXXClassDef.
  65. void FinishClass() {
  66. Fields.resize(Fields.size() - getCurNumFields());
  67. FieldCount.pop_back();
  68. }
  69. };
  70. } // end namespace clang
  71. #endif
  72. #ifdef __GNUC__
  73. #pragma GCC diagnostic pop
  74. #endif