clang API Documentation

CXXFieldCollector.h
Go to the documentation of this file.
00001 //===- CXXFieldCollector.h - Utility class for C++ class semantic analysis ===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file provides CXXFieldCollector that is used during parsing & semantic
00011 //  analysis of C++ classes.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_SEMA_CXXFIELDCOLLECTOR_H
00016 #define LLVM_CLANG_SEMA_CXXFIELDCOLLECTOR_H
00017 
00018 #include "clang/Basic/LLVM.h"
00019 #include "llvm/ADT/SmallVector.h"
00020 
00021 namespace clang {
00022   class FieldDecl;
00023 
00024 /// CXXFieldCollector - Used to keep track of CXXFieldDecls during parsing of
00025 /// C++ classes.
00026 class CXXFieldCollector {
00027   /// Fields - Contains all FieldDecls collected during parsing of a C++
00028   /// class. When a nested class is entered, its fields are appended to the
00029   /// fields of its parent class, when it is exited its fields are removed.
00030   SmallVector<FieldDecl*, 32> Fields;
00031 
00032   /// FieldCount - Each entry represents the number of fields collected during
00033   /// the parsing of a C++ class. When a nested class is entered, a new field
00034   /// count is pushed, when it is exited, the field count is popped.
00035   SmallVector<size_t, 4> FieldCount;
00036 
00037   // Example:
00038   //
00039   // class C {
00040   //   int x,y;
00041   //   class NC {
00042   //     int q;
00043   //     // At this point, Fields contains [x,y,q] decls and FieldCount contains
00044   //     // [2,1].
00045   //   };
00046   //   int z;
00047   //   // At this point, Fields contains [x,y,z] decls and FieldCount contains
00048   //   // [3].
00049   // };
00050 
00051 public:
00052   /// StartClass - Called by Sema::ActOnStartCXXClassDef.
00053   void StartClass() { FieldCount.push_back(0); }
00054 
00055   /// Add - Called by Sema::ActOnCXXMemberDeclarator.
00056   void Add(FieldDecl *D) {
00057     Fields.push_back(D);
00058     ++FieldCount.back();
00059   }
00060 
00061   /// getCurNumField - The number of fields added to the currently parsed class.
00062   size_t getCurNumFields() const {
00063     assert(!FieldCount.empty() && "no currently-parsed class");
00064     return FieldCount.back();
00065   }
00066 
00067   /// getCurFields - Pointer to array of fields added to the currently parsed
00068   /// class.
00069   FieldDecl **getCurFields() { return &*(Fields.end() - getCurNumFields()); }
00070 
00071   /// FinishClass - Called by Sema::ActOnFinishCXXClassDef.
00072   void FinishClass() {
00073     Fields.resize(Fields.size() - getCurNumFields());
00074     FieldCount.pop_back();
00075   }
00076 };
00077 
00078 } // end namespace clang
00079 
00080 #endif