Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1 | //===- CXXFieldCollector.h - Utility class for C++ class semantic analysis ===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides CXXFieldCollector that is used during parsing & semantic |
| 11 | // analysis of C++ classes. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_SEMA_CXXFIELDCOLLECTOR_H |
| 16 | #define LLVM_CLANG_SEMA_CXXFIELDCOLLECTOR_H |
| 17 | |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | |
| 20 | namespace clang { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 21 | class FieldDecl; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 22 | |
| 23 | /// CXXFieldCollector - Used to keep track of CXXFieldDecls during parsing of |
| 24 | /// C++ classes. |
| 25 | class CXXFieldCollector { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 26 | /// Fields - Contains all FieldDecls collected during parsing of a C++ |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 27 | /// class. When a nested class is entered, its fields are appended to the |
| 28 | /// fields of its parent class, when it is exited its fields are removed. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 29 | llvm::SmallVector<FieldDecl*, 32> Fields; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 30 | |
| 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 | llvm::SmallVector<size_t, 4> FieldCount; |
| 35 | |
| 36 | // Example: |
| 37 | // |
| 38 | // class C { |
| 39 | // int x,y; |
| 40 | // class NC { |
| 41 | // int q; |
| 42 | // // At this point, Fields contains [x,y,q] decls and FieldCount contains |
| 43 | // // [2,1]. |
| 44 | // }; |
| 45 | // int z; |
| 46 | // // At this point, Fields contains [x,y,z] decls and FieldCount contains |
| 47 | // // [3]. |
| 48 | // }; |
| 49 | |
| 50 | public: |
| 51 | /// StartClass - Called by Sema::ActOnStartCXXClassDef. |
| 52 | void StartClass() { FieldCount.push_back(0); } |
| 53 | |
| 54 | /// Add - Called by Sema::ActOnCXXMemberDeclarator. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 55 | void Add(FieldDecl *D) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 56 | Fields.push_back(D); |
| 57 | ++FieldCount.back(); |
| 58 | } |
| 59 | |
Argyrios Kyrtzidis | 7bfa291 | 2008-07-01 11:22:40 +0000 | [diff] [blame] | 60 | /// getCurNumField - The number of fields added to the currently parsed class. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 61 | size_t getCurNumFields() const { return FieldCount.back(); } |
| 62 | |
Argyrios Kyrtzidis | 7bfa291 | 2008-07-01 11:22:40 +0000 | [diff] [blame] | 63 | /// getCurFields - Pointer to array of fields added to the currently parsed |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 64 | /// class. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 65 | FieldDecl **getCurFields() { return &*(Fields.end() - getCurNumFields()); } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 66 | |
| 67 | /// FinishClass - Called by Sema::ActOnFinishCXXClassDef. |
| 68 | void FinishClass() { |
| 69 | Fields.resize(Fields.size() - getCurNumFields()); |
| 70 | FieldCount.pop_back(); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | } // end namespace clang |
| 75 | |
| 76 | #endif |