Store attributes in a global hash map instead.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@47191 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/AST/Decl.cpp b/AST/Decl.cpp
index 0ed028c..04d0d3c 100644
--- a/AST/Decl.cpp
+++ b/AST/Decl.cpp
@@ -15,6 +15,8 @@
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Attr.h"
#include "clang/Basic/IdentifierTable.h"
+#include "llvm/ADT/DenseMap.h"
+
using namespace clang;
// temporary statistics gathering
@@ -43,6 +45,12 @@
static bool StatSwitch = false;
+// This keeps track of all decl attributes. Since so few decls have attrs, we
+// keep them in a hash map instead of wasting space in the Decl class.
+typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
+
+static DeclAttrMapTy *DeclAttrs = 0;
+
const char *Decl::getDeclKindName() const {
switch (DeclKind) {
default: assert(0 && "Unknown decl kind!");
@@ -251,6 +259,39 @@
// Out-of-line virtual method providing a home for Decl.
Decl::~Decl() {
+ if (!DeclAttrs)
+ return;
+
+ DeclAttrMapTy::iterator it = DeclAttrs->find(this);
+ if (it != DeclAttrs->end()) {
+ delete it->second;
+ DeclAttrs->erase(it);
+ if (DeclAttrs->empty()) {
+ delete DeclAttrs;
+ DeclAttrs = 0;
+ }
+ }
+}
+
+void Decl::addAttr(Attr *newattr)
+{
+ if (!DeclAttrs)
+ DeclAttrs = new llvm::DenseMap<const Decl*, Attr*>;
+
+ Attr *&attr = DeclAttrs->FindAndConstruct(this).second;
+
+ newattr->setNext(attr);
+ attr = newattr;
+
+ HasAttrs = true;
+}
+
+const Attr *Decl::getAttrs() const
+{
+ if (!HasAttrs || !DeclAttrs)
+ return 0;
+
+ return DeclAttrs->find(this)->second;
}
const char *NamedDecl::getName() const {
@@ -259,12 +300,6 @@
return "";
}
-void ValueDecl::addAttr(Attr *attr)
-{
- attr->setNext(Attrs);
- Attrs = attr;
-}
-
FunctionDecl::~FunctionDecl() {
delete[] ParamInfo;
}