2008-06-08 00:52:53 +08:00
|
|
|
//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Decl and DeclContext classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/DeclBase.h"
|
2008-08-11 12:54:23 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-06-10 05:05:31 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-06-08 00:52:53 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-12-12 00:49:14 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2008-06-08 00:52:53 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2008-12-24 05:05:05 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
2008-12-23 08:26:44 +08:00
|
|
|
#include <vector>
|
2008-06-08 00:52:53 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Statistics
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// temporary statistics gathering
|
|
|
|
static unsigned nFuncs = 0;
|
|
|
|
static unsigned nVars = 0;
|
|
|
|
static unsigned nParmVars = 0;
|
2008-12-21 05:06:28 +08:00
|
|
|
static unsigned nOriginalParmVars = 0;
|
2008-06-08 00:52:53 +08:00
|
|
|
static unsigned nSUC = 0;
|
2008-08-10 09:47:31 +08:00
|
|
|
static unsigned nCXXSUC = 0;
|
2008-06-08 00:52:53 +08:00
|
|
|
static unsigned nEnumConst = 0;
|
|
|
|
static unsigned nEnumDecls = 0;
|
|
|
|
static unsigned nNamespaces = 0;
|
2008-10-22 00:13:35 +08:00
|
|
|
static unsigned nOverFuncs = 0;
|
2008-06-08 00:52:53 +08:00
|
|
|
static unsigned nTypedef = 0;
|
|
|
|
static unsigned nFieldDecls = 0;
|
|
|
|
static unsigned nInterfaceDecls = 0;
|
|
|
|
static unsigned nClassDecls = 0;
|
|
|
|
static unsigned nMethodDecls = 0;
|
|
|
|
static unsigned nProtocolDecls = 0;
|
|
|
|
static unsigned nForwardProtocolDecls = 0;
|
|
|
|
static unsigned nCategoryDecls = 0;
|
|
|
|
static unsigned nIvarDecls = 0;
|
2008-08-20 11:26:33 +08:00
|
|
|
static unsigned nAtDefsFieldDecls = 0;
|
2008-06-08 00:52:53 +08:00
|
|
|
static unsigned nObjCImplementationDecls = 0;
|
|
|
|
static unsigned nObjCCategoryImpl = 0;
|
|
|
|
static unsigned nObjCCompatibleAlias = 0;
|
|
|
|
static unsigned nObjCPropertyDecl = 0;
|
|
|
|
static unsigned nObjCPropertyImplDecl = 0;
|
|
|
|
static unsigned nLinkageSpecDecl = 0;
|
|
|
|
static unsigned nFileScopeAsmDecl = 0;
|
2008-10-09 01:01:13 +08:00
|
|
|
static unsigned nBlockDecls = 0;
|
2008-06-08 00:52:53 +08:00
|
|
|
|
|
|
|
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!");
|
|
|
|
case Namespace: return "Namespace";
|
2008-10-22 00:13:35 +08:00
|
|
|
case OverloadedFunction: return "OverloadedFunction";
|
2008-06-08 00:52:53 +08:00
|
|
|
case Typedef: return "Typedef";
|
|
|
|
case Function: return "Function";
|
|
|
|
case Var: return "Var";
|
|
|
|
case ParmVar: return "ParmVar";
|
2008-12-21 05:06:28 +08:00
|
|
|
case OriginalParmVar: return "OriginalParmVar";
|
2008-06-08 00:52:53 +08:00
|
|
|
case EnumConstant: return "EnumConstant";
|
|
|
|
case ObjCIvar: return "ObjCIvar";
|
|
|
|
case ObjCInterface: return "ObjCInterface";
|
2008-12-02 04:33:01 +08:00
|
|
|
case ObjCImplementation: return "ObjCImplementation";
|
2008-06-08 00:52:53 +08:00
|
|
|
case ObjCClass: return "ObjCClass";
|
|
|
|
case ObjCMethod: return "ObjCMethod";
|
|
|
|
case ObjCProtocol: return "ObjCProtocol";
|
2008-12-02 04:33:01 +08:00
|
|
|
case ObjCProperty: return "ObjCProperty";
|
|
|
|
case ObjCPropertyImpl: return "ObjCPropertyImpl";
|
2008-06-08 00:52:53 +08:00
|
|
|
case ObjCForwardProtocol: return "ObjCForwardProtocol";
|
2008-10-15 08:42:39 +08:00
|
|
|
case Record: return "Record";
|
|
|
|
case CXXRecord: return "CXXRecord";
|
2008-06-08 00:52:53 +08:00
|
|
|
case Enum: return "Enum";
|
2008-10-09 01:01:13 +08:00
|
|
|
case Block: return "Block";
|
2008-06-08 00:52:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Decl::CollectingStats(bool Enable) {
|
|
|
|
if (Enable)
|
|
|
|
StatSwitch = true;
|
|
|
|
return StatSwitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Decl::PrintStats() {
|
|
|
|
fprintf(stderr, "*** Decl Stats:\n");
|
|
|
|
fprintf(stderr, " %d decls total.\n",
|
2008-12-21 05:06:28 +08:00
|
|
|
int(nFuncs+nVars+nParmVars+nOriginalParmVars+nFieldDecls+nSUC+nCXXSUC+
|
2008-06-08 00:52:53 +08:00
|
|
|
nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
|
|
|
|
nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
|
2008-10-22 00:13:35 +08:00
|
|
|
nAtDefsFieldDecls+nNamespaces+nOverFuncs));
|
2008-06-08 00:52:53 +08:00
|
|
|
fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
|
|
|
|
nNamespaces, (int)sizeof(NamespaceDecl),
|
|
|
|
int(nNamespaces*sizeof(NamespaceDecl)));
|
2008-10-22 00:13:35 +08:00
|
|
|
fprintf(stderr, " %d overloaded function decls, %d each (%d bytes)\n",
|
|
|
|
nOverFuncs, (int)sizeof(OverloadedFunctionDecl),
|
|
|
|
int(nOverFuncs*sizeof(OverloadedFunctionDecl)));
|
2008-06-08 00:52:53 +08:00
|
|
|
fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
|
|
|
|
nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
|
|
|
|
fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
|
|
|
|
nVars, (int)sizeof(VarDecl),
|
|
|
|
int(nVars*sizeof(VarDecl)));
|
|
|
|
fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
|
|
|
|
nParmVars, (int)sizeof(ParmVarDecl),
|
|
|
|
int(nParmVars*sizeof(ParmVarDecl)));
|
2008-12-21 05:06:28 +08:00
|
|
|
fprintf(stderr, " %d original parameter variable decls, %d each (%d bytes)\n",
|
|
|
|
nOriginalParmVars, (int)sizeof(ParmVarWithOriginalTypeDecl),
|
|
|
|
int(nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)));
|
2008-06-08 00:52:53 +08:00
|
|
|
fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
|
|
|
|
nFieldDecls, (int)sizeof(FieldDecl),
|
|
|
|
int(nFieldDecls*sizeof(FieldDecl)));
|
2008-08-20 11:26:33 +08:00
|
|
|
fprintf(stderr, " %d @defs generated field decls, %d each (%d bytes)\n",
|
|
|
|
nAtDefsFieldDecls, (int)sizeof(ObjCAtDefsFieldDecl),
|
|
|
|
int(nAtDefsFieldDecls*sizeof(ObjCAtDefsFieldDecl)));
|
2008-06-08 00:52:53 +08:00
|
|
|
fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
|
|
|
|
nSUC, (int)sizeof(RecordDecl),
|
|
|
|
int(nSUC*sizeof(RecordDecl)));
|
2008-08-10 09:47:31 +08:00
|
|
|
fprintf(stderr, " %d C++ struct/union/class decls, %d each (%d bytes)\n",
|
|
|
|
nCXXSUC, (int)sizeof(CXXRecordDecl),
|
|
|
|
int(nCXXSUC*sizeof(CXXRecordDecl)));
|
2008-06-08 00:52:53 +08:00
|
|
|
fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
|
|
|
|
nEnumDecls, (int)sizeof(EnumDecl),
|
|
|
|
int(nEnumDecls*sizeof(EnumDecl)));
|
|
|
|
fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
|
|
|
|
nEnumConst, (int)sizeof(EnumConstantDecl),
|
|
|
|
int(nEnumConst*sizeof(EnumConstantDecl)));
|
|
|
|
fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
|
|
|
|
nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
|
|
|
|
// Objective-C decls...
|
|
|
|
fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
|
|
|
|
nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
|
|
|
|
int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
|
|
|
|
fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
|
|
|
|
nIvarDecls, (int)sizeof(ObjCIvarDecl),
|
|
|
|
int(nIvarDecls*sizeof(ObjCIvarDecl)));
|
|
|
|
fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
|
|
|
|
nClassDecls, (int)sizeof(ObjCClassDecl),
|
|
|
|
int(nClassDecls*sizeof(ObjCClassDecl)));
|
|
|
|
fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
|
|
|
|
nMethodDecls, (int)sizeof(ObjCMethodDecl),
|
|
|
|
int(nMethodDecls*sizeof(ObjCMethodDecl)));
|
|
|
|
fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
|
|
|
|
nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
|
|
|
|
int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
|
|
|
|
fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
|
|
|
|
nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
|
|
|
|
int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
|
|
|
|
fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
|
|
|
|
nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
|
|
|
|
int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
|
|
|
|
|
|
|
|
fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
|
|
|
|
nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
|
|
|
|
int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
|
|
|
|
|
|
|
|
fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
|
|
|
|
nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
|
|
|
|
int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
|
|
|
|
|
|
|
|
fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
|
|
|
|
nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
|
|
|
|
int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
|
|
|
|
|
|
|
|
fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
|
|
|
|
nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
|
|
|
|
int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
|
|
|
|
|
|
|
|
fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
|
|
|
|
nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
|
|
|
|
int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
|
|
|
|
|
|
|
|
fprintf(stderr, "Total bytes = %d\n",
|
|
|
|
int(nFuncs*sizeof(FunctionDecl)+
|
|
|
|
nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
|
2008-12-21 05:06:28 +08:00
|
|
|
nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)+
|
2008-06-08 00:52:53 +08:00
|
|
|
nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
|
2008-12-12 00:49:14 +08:00
|
|
|
nCXXSUC*sizeof(CXXRecordDecl)+
|
2008-06-08 00:52:53 +08:00
|
|
|
nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
|
|
|
|
nTypedef*sizeof(TypedefDecl)+
|
|
|
|
nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
|
|
|
|
nIvarDecls*sizeof(ObjCIvarDecl)+
|
|
|
|
nClassDecls*sizeof(ObjCClassDecl)+
|
|
|
|
nMethodDecls*sizeof(ObjCMethodDecl)+
|
|
|
|
nProtocolDecls*sizeof(ObjCProtocolDecl)+
|
|
|
|
nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
|
|
|
|
nCategoryDecls*sizeof(ObjCCategoryDecl)+
|
|
|
|
nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
|
|
|
|
nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
|
|
|
|
nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
|
|
|
|
nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
|
|
|
|
nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
|
|
|
|
nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
|
|
|
|
nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
|
2008-10-22 00:13:35 +08:00
|
|
|
nNamespaces*sizeof(NamespaceDecl)+
|
|
|
|
nOverFuncs*sizeof(OverloadedFunctionDecl)));
|
2008-06-08 00:52:53 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Decl::addDeclKind(Kind k) {
|
|
|
|
switch (k) {
|
|
|
|
case Namespace: nNamespaces++; break;
|
2008-10-22 00:13:35 +08:00
|
|
|
case OverloadedFunction: nOverFuncs++; break;
|
2008-06-08 00:52:53 +08:00
|
|
|
case Typedef: nTypedef++; break;
|
|
|
|
case Function: nFuncs++; break;
|
|
|
|
case Var: nVars++; break;
|
|
|
|
case ParmVar: nParmVars++; break;
|
2008-12-21 05:06:28 +08:00
|
|
|
case OriginalParmVar: nOriginalParmVars++; break;
|
2008-06-08 00:52:53 +08:00
|
|
|
case EnumConstant: nEnumConst++; break;
|
|
|
|
case Field: nFieldDecls++; break;
|
2008-10-15 08:42:39 +08:00
|
|
|
case Record: nSUC++; break;
|
2008-06-08 00:52:53 +08:00
|
|
|
case Enum: nEnumDecls++; break;
|
|
|
|
case ObjCInterface: nInterfaceDecls++; break;
|
|
|
|
case ObjCClass: nClassDecls++; break;
|
|
|
|
case ObjCMethod: nMethodDecls++; break;
|
|
|
|
case ObjCProtocol: nProtocolDecls++; break;
|
|
|
|
case ObjCForwardProtocol: nForwardProtocolDecls++; break;
|
|
|
|
case ObjCCategory: nCategoryDecls++; break;
|
|
|
|
case ObjCIvar: nIvarDecls++; break;
|
2008-08-20 11:26:33 +08:00
|
|
|
case ObjCAtDefsField: nAtDefsFieldDecls++; break;
|
2008-06-08 00:52:53 +08:00
|
|
|
case ObjCImplementation: nObjCImplementationDecls++; break;
|
|
|
|
case ObjCCategoryImpl: nObjCCategoryImpl++; break;
|
|
|
|
case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
|
|
|
|
case ObjCProperty: nObjCPropertyDecl++; break;
|
|
|
|
case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
|
|
|
|
case LinkageSpec: nLinkageSpecDecl++; break;
|
|
|
|
case FileScopeAsm: nFileScopeAsmDecl++; break;
|
2008-10-09 01:01:13 +08:00
|
|
|
case Block: nBlockDecls++; break;
|
2008-06-18 02:05:57 +08:00
|
|
|
case ImplicitParam:
|
2008-06-08 00:52:53 +08:00
|
|
|
case TranslationUnit: break;
|
2008-06-10 05:05:31 +08:00
|
|
|
|
2008-10-15 08:42:39 +08:00
|
|
|
case CXXRecord: nCXXSUC++; break;
|
2008-06-10 05:05:31 +08:00
|
|
|
// FIXME: Statistics for C++ decls.
|
2008-12-06 02:15:24 +08:00
|
|
|
case TemplateTypeParm:
|
|
|
|
case NonTypeTemplateParm:
|
2008-06-10 05:05:31 +08:00
|
|
|
case CXXMethod:
|
2008-10-31 17:07:45 +08:00
|
|
|
case CXXConstructor:
|
2008-11-06 04:51:48 +08:00
|
|
|
case CXXDestructor:
|
2008-11-08 04:08:42 +08:00
|
|
|
case CXXConversion:
|
2008-06-10 05:05:31 +08:00
|
|
|
case CXXClassVar:
|
|
|
|
break;
|
2008-06-08 00:52:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Decl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Out-of-line virtual method providing a home for Decl.
|
|
|
|
Decl::~Decl() {
|
|
|
|
if (!HasAttrs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DeclAttrMapTy::iterator it = DeclAttrs->find(this);
|
|
|
|
assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
|
|
|
|
|
|
|
|
// release attributes.
|
|
|
|
delete it->second;
|
|
|
|
invalidateAttrs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Decl::addAttr(Attr *NewAttr) {
|
|
|
|
if (!DeclAttrs)
|
|
|
|
DeclAttrs = new DeclAttrMapTy();
|
|
|
|
|
|
|
|
Attr *&ExistingAttr = (*DeclAttrs)[this];
|
|
|
|
|
|
|
|
NewAttr->setNext(ExistingAttr);
|
|
|
|
ExistingAttr = NewAttr;
|
|
|
|
|
|
|
|
HasAttrs = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Decl::invalidateAttrs() {
|
|
|
|
if (!HasAttrs) return;
|
|
|
|
|
|
|
|
HasAttrs = false;
|
|
|
|
(*DeclAttrs)[this] = 0;
|
|
|
|
DeclAttrs->erase(this);
|
|
|
|
|
|
|
|
if (DeclAttrs->empty()) {
|
|
|
|
delete DeclAttrs;
|
|
|
|
DeclAttrs = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Attr *Decl::getAttrs() const {
|
|
|
|
if (!HasAttrs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (*DeclAttrs)[this];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Decl::swapAttrs(Decl *RHS) {
|
|
|
|
bool HasLHSAttr = this->HasAttrs;
|
|
|
|
bool HasRHSAttr = RHS->HasAttrs;
|
|
|
|
|
|
|
|
// Usually, neither decl has attrs, nothing to do.
|
|
|
|
if (!HasLHSAttr && !HasRHSAttr) return;
|
|
|
|
|
|
|
|
// If 'this' has no attrs, swap the other way.
|
|
|
|
if (!HasLHSAttr)
|
|
|
|
return RHS->swapAttrs(this);
|
|
|
|
|
|
|
|
// Handle the case when both decls have attrs.
|
|
|
|
if (HasRHSAttr) {
|
|
|
|
std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, LHS has an attr and RHS doesn't.
|
|
|
|
(*DeclAttrs)[RHS] = (*DeclAttrs)[this];
|
|
|
|
(*DeclAttrs).erase(this);
|
|
|
|
this->HasAttrs = false;
|
|
|
|
RHS->HasAttrs = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Decl::Destroy(ASTContext& C) {
|
|
|
|
if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
|
|
|
|
|
|
|
|
// Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
|
|
|
|
// within the loop, only the Destroy method for the first ScopedDecl
|
|
|
|
// will deallocate all of the ScopedDecls in a chain.
|
|
|
|
|
|
|
|
ScopedDecl* N = SD->getNextDeclarator();
|
|
|
|
|
|
|
|
while (N) {
|
|
|
|
ScopedDecl* Tmp = N->getNextDeclarator();
|
|
|
|
N->NextDeclarator = 0x0;
|
|
|
|
N->Destroy(C);
|
|
|
|
N = Tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->~Decl();
|
|
|
|
C.getAllocator().Deallocate((void *)this);
|
|
|
|
}
|
|
|
|
|
2008-10-13 00:14:48 +08:00
|
|
|
Decl *Decl::castFromDeclContext (const DeclContext *D) {
|
|
|
|
return DeclContext::CastTo<Decl>(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclContext *Decl::castToDeclContext(const Decl *D) {
|
|
|
|
return DeclContext::CastTo<DeclContext>(D);
|
|
|
|
}
|
|
|
|
|
2008-06-08 00:52:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DeclContext Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-11-20 01:36:39 +08:00
|
|
|
const DeclContext *DeclContext::getParent() const {
|
|
|
|
if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
|
2008-06-08 00:52:53 +08:00
|
|
|
return SD->getDeclContext();
|
2008-11-20 01:36:39 +08:00
|
|
|
else if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
|
2008-10-10 09:28:17 +08:00
|
|
|
return BD->getParentContext();
|
2008-06-08 00:52:53 +08:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-20 02:01:13 +08:00
|
|
|
|
|
|
|
const DeclContext *DeclContext::getLexicalParent() const {
|
|
|
|
if (const ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
|
|
|
|
return SD->getLexicalDeclContext();
|
2008-11-20 02:07:24 +08:00
|
|
|
return getParent();
|
2008-11-20 02:01:13 +08:00
|
|
|
}
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
|
|
// FIXME: We really want to use a DenseSet here to eliminate the
|
|
|
|
// redundant storage of the declaration names, but (1) it doesn't give
|
|
|
|
// us the ability to search based on DeclarationName, (2) we really
|
|
|
|
// need something more like a DenseMultiSet, and (3) it's
|
2008-12-23 08:26:44 +08:00
|
|
|
// implemented in terms of DenseMap anyway. However, this data
|
|
|
|
// structure is really space-inefficient, so we'll have to do
|
|
|
|
// something.
|
|
|
|
typedef llvm::DenseMap<DeclarationName, std::vector<ScopedDecl*> >
|
|
|
|
StoredDeclsMap;
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
|
|
DeclContext::~DeclContext() {
|
|
|
|
unsigned Size = LookupPtr.getInt();
|
|
|
|
if (Size == LookupIsMap) {
|
|
|
|
StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
|
|
|
|
delete Map;
|
|
|
|
} else {
|
2008-12-23 08:26:44 +08:00
|
|
|
ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
|
2008-12-12 00:49:14 +08:00
|
|
|
delete [] Array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclContext::DestroyDecls(ASTContext &C) {
|
|
|
|
for (decl_iterator D = Decls.begin(); D != Decls.end(); ++D) {
|
|
|
|
if ((*D)->getLexicalDeclContext() == this)
|
|
|
|
(*D)->Destroy(C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-06 03:45:36 +08:00
|
|
|
bool DeclContext::isTransparentContext() const {
|
|
|
|
if (DeclKind == Decl::Enum)
|
|
|
|
return true; // FIXME: Check for C++0x scoped enums
|
|
|
|
else if (DeclKind == Decl::LinkageSpec)
|
|
|
|
return true;
|
|
|
|
else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord)
|
|
|
|
return false; // FIXME: need to know about anonymous unions/structs
|
|
|
|
else if (DeclKind == Decl::Namespace)
|
|
|
|
return false; // FIXME: Check for C++0x inline namespaces
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-12 00:49:14 +08:00
|
|
|
DeclContext *DeclContext::getPrimaryContext(ASTContext &Context) {
|
|
|
|
switch (DeclKind) {
|
|
|
|
case Decl::TranslationUnit:
|
2009-01-06 03:45:36 +08:00
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::Block:
|
2008-12-12 00:49:14 +08:00
|
|
|
// There is only one DeclContext for these entities.
|
|
|
|
return this;
|
|
|
|
|
|
|
|
case Decl::Namespace:
|
|
|
|
// The original namespace is our primary context.
|
|
|
|
return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
|
|
|
|
|
|
|
|
case Decl::Enum:
|
2009-01-06 03:45:36 +08:00
|
|
|
#if 0
|
|
|
|
// FIXME: See the comment for CXXRecord, below.
|
2008-12-12 00:49:14 +08:00
|
|
|
// The declaration associated with the enumeration type is our
|
|
|
|
// primary context.
|
|
|
|
return Context.getTypeDeclType(static_cast<EnumDecl*>(this))
|
|
|
|
->getAsEnumType()->getDecl();
|
2009-01-06 03:45:36 +08:00
|
|
|
#else
|
|
|
|
return this;
|
|
|
|
#endif
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
|
|
case Decl::Record:
|
|
|
|
case Decl::CXXRecord: {
|
|
|
|
// The declaration associated with the type is be our primary
|
|
|
|
// context.
|
|
|
|
#if 0
|
|
|
|
// FIXME: This is what we expect to do. However, it doesn't work
|
|
|
|
// because ASTContext::setTagDefinition changes the result of
|
|
|
|
// Context.getTypeDeclType, meaning that our "primary" declaration
|
|
|
|
// of a RecordDecl/CXXRecordDecl will change, and we won't be able
|
|
|
|
// to find any values inserted into the earlier "primary"
|
|
|
|
// declaration. We need better tracking of redeclarations and
|
|
|
|
// definitions.
|
|
|
|
QualType Type = Context.getTypeDeclType(static_cast<RecordDecl*>(this));
|
|
|
|
return Type->getAsRecordType()->getDecl();
|
|
|
|
#else
|
|
|
|
// FIXME: This hack will work for now, because the declaration we
|
|
|
|
// create when we're defining the record is the one we'll use as
|
|
|
|
// the definition later.
|
|
|
|
return this;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
return this;
|
|
|
|
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
// FIXME: Can Objective-C interfaces be forward-declared?
|
|
|
|
return this;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
|
|
|
|
"Unknown DeclContext kind");
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclContext *DeclContext::getNextContext() {
|
|
|
|
switch (DeclKind) {
|
|
|
|
case Decl::TranslationUnit:
|
|
|
|
case Decl::Enum:
|
|
|
|
case Decl::Record:
|
|
|
|
case Decl::CXXRecord:
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
case Decl::ObjCInterface:
|
2009-01-06 03:45:36 +08:00
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::Block:
|
2008-12-12 00:49:14 +08:00
|
|
|
// There is only one DeclContext for these entities.
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case Decl::Namespace:
|
|
|
|
// Return the next namespace
|
|
|
|
return static_cast<NamespaceDecl*>(this)->getNextNamespace();
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
|
|
|
|
"Unknown DeclContext kind");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclContext::addDecl(ASTContext &Context, ScopedDecl *D, bool AllowLookup) {
|
|
|
|
Decls.push_back(D);
|
|
|
|
if (AllowLookup)
|
|
|
|
D->getDeclContext()->insert(Context, D);
|
|
|
|
}
|
|
|
|
|
2009-01-06 03:45:36 +08:00
|
|
|
/// buildLookup - Build the lookup data structure with all of the
|
|
|
|
/// declarations in DCtx (and any other contexts linked to it or
|
|
|
|
/// transparent contexts nested within it).
|
|
|
|
void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
|
|
|
|
for (; DCtx; DCtx = DCtx->getNextContext()) {
|
|
|
|
for (decl_iterator D = DCtx->decls_begin(); D != DCtx->decls_end(); ++D) {
|
|
|
|
// Insert this declaration into the lookup structure
|
|
|
|
insertImpl(*D);
|
|
|
|
|
|
|
|
// If this declaration is itself a transparent declaration context,
|
|
|
|
// add its members (recursively).
|
|
|
|
if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
|
|
|
|
if (InnerCtx->isTransparentContext())
|
|
|
|
buildLookup(Context, InnerCtx->getPrimaryContext(Context));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-12 00:49:14 +08:00
|
|
|
DeclContext::lookup_result
|
|
|
|
DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
|
|
|
|
DeclContext *PrimaryContext = getPrimaryContext(Context);
|
|
|
|
if (PrimaryContext != this)
|
|
|
|
return PrimaryContext->lookup(Context, Name);
|
|
|
|
|
2008-12-23 08:26:44 +08:00
|
|
|
/// If there is no lookup data structure, build one now by walking
|
2008-12-12 00:49:14 +08:00
|
|
|
/// all of the linked DeclContexts (in declaration order!) and
|
|
|
|
/// inserting their values.
|
2009-01-06 03:45:36 +08:00
|
|
|
if (LookupPtr.getPointer() == 0)
|
|
|
|
buildLookup(Context, this);
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
|
|
if (isLookupMap()) {
|
|
|
|
StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
|
|
|
|
StoredDeclsMap::iterator Pos = Map->find(Name);
|
2008-12-23 08:26:44 +08:00
|
|
|
if (Pos != Map->end())
|
|
|
|
return lookup_result(&Pos->second.front(),
|
|
|
|
&Pos->second.front() + Pos->second.size());
|
|
|
|
return lookup_result(0, 0);
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have a small array. Look into it.
|
|
|
|
unsigned Size = LookupPtr.getInt();
|
2008-12-23 08:26:44 +08:00
|
|
|
ScopedDecl **Array = static_cast<ScopedDecl**>(LookupPtr.getPointer());
|
2008-12-12 04:41:00 +08:00
|
|
|
for (unsigned Idx = 0; Idx != Size; ++Idx)
|
2008-12-12 00:49:14 +08:00
|
|
|
if (Array[Idx]->getDeclName() == Name) {
|
2008-12-23 08:26:44 +08:00
|
|
|
unsigned Last = Idx + 1;
|
|
|
|
while (Last != Size && Array[Last]->getDeclName() == Name)
|
|
|
|
++Last;
|
|
|
|
return lookup_result(&Array[Idx], &Array[Last]);
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
|
2008-12-23 08:26:44 +08:00
|
|
|
return lookup_result(0, 0);
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DeclContext::lookup_const_result
|
|
|
|
DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
|
|
|
|
return const_cast<DeclContext*>(this)->lookup(Context, Name);
|
|
|
|
}
|
|
|
|
|
2008-12-23 08:26:44 +08:00
|
|
|
void DeclContext::insert(ASTContext &Context, ScopedDecl *D) {
|
2008-12-12 00:49:14 +08:00
|
|
|
DeclContext *PrimaryContext = getPrimaryContext(Context);
|
|
|
|
if (PrimaryContext != this) {
|
|
|
|
PrimaryContext->insert(Context, D);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we already have a lookup data structure, perform the insertion
|
|
|
|
// into it. Otherwise, be lazy and don't build that structure until
|
|
|
|
// someone asks for it.
|
|
|
|
if (LookupPtr.getPointer())
|
|
|
|
insertImpl(D);
|
2009-01-06 03:45:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
// If we are a transparent context, insert into our parent context,
|
|
|
|
// too. This operation is recursive.
|
|
|
|
if (isTransparentContext())
|
|
|
|
getParent()->insert(Context, D);
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
|
2008-12-23 08:26:44 +08:00
|
|
|
void DeclContext::insertImpl(ScopedDecl *D) {
|
2009-01-06 03:45:36 +08:00
|
|
|
// Skip unnamed declarations.
|
|
|
|
if (!D->getDeclName())
|
|
|
|
return;
|
|
|
|
|
2008-12-23 08:26:44 +08:00
|
|
|
bool MayBeRedeclaration = true;
|
|
|
|
|
2008-12-12 00:49:14 +08:00
|
|
|
if (!isLookupMap()) {
|
|
|
|
unsigned Size = LookupPtr.getInt();
|
|
|
|
|
|
|
|
// The lookup data is stored as an array. Search through the array
|
|
|
|
// to find the insertion location.
|
2008-12-23 08:26:44 +08:00
|
|
|
ScopedDecl **Array;
|
2008-12-12 00:49:14 +08:00
|
|
|
if (Size == 0) {
|
2008-12-23 08:26:44 +08:00
|
|
|
Array = new ScopedDecl*[LookupIsMap - 1];
|
2008-12-12 00:49:14 +08:00
|
|
|
LookupPtr.setPointer(Array);
|
|
|
|
} else {
|
2008-12-23 08:26:44 +08:00
|
|
|
Array = static_cast<ScopedDecl **>(LookupPtr.getPointer());
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We always keep declarations of the same name next to each other
|
|
|
|
// in the array, so that it is easy to return multiple results
|
2008-12-23 08:26:44 +08:00
|
|
|
// from lookup().
|
|
|
|
unsigned FirstMatch;
|
|
|
|
for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
|
|
|
|
if (Array[FirstMatch]->getDeclName() == D->getDeclName())
|
2008-12-12 04:41:00 +08:00
|
|
|
break;
|
2008-12-12 00:49:14 +08:00
|
|
|
|
2008-12-23 08:26:44 +08:00
|
|
|
unsigned InsertPos = FirstMatch;
|
|
|
|
if (FirstMatch != Size) {
|
|
|
|
// We found another declaration with the same name. First
|
|
|
|
// determine whether this is a redeclaration of an existing
|
|
|
|
// declaration in this scope, in which case we will replace the
|
|
|
|
// existing declaration.
|
|
|
|
unsigned LastMatch = FirstMatch;
|
|
|
|
for (; LastMatch != Size; ++LastMatch) {
|
|
|
|
if (Array[LastMatch]->getDeclName() != D->getDeclName())
|
|
|
|
break;
|
|
|
|
|
2008-12-24 05:05:05 +08:00
|
|
|
if (D->declarationReplaces(Array[LastMatch])) {
|
2008-12-23 08:26:44 +08:00
|
|
|
// D is a redeclaration of an existing element in the
|
|
|
|
// array. Replace that element with D.
|
|
|
|
Array[LastMatch] = D;
|
|
|
|
return;
|
|
|
|
}
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
|
2008-12-23 08:26:44 +08:00
|
|
|
// [FirstMatch, LastMatch) contains the set of declarations that
|
|
|
|
// have the same name as this declaration. Determine where the
|
|
|
|
// declaration D will be inserted into this range.
|
|
|
|
if (D->getIdentifierNamespace() == Decl::IDNS_Tag)
|
|
|
|
InsertPos = LastMatch;
|
|
|
|
else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
|
|
|
|
InsertPos = LastMatch - 1;
|
|
|
|
else
|
|
|
|
InsertPos = LastMatch;
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Size < LookupIsMap - 1) {
|
|
|
|
// The new declaration will fit in the array. Insert the new
|
|
|
|
// declaration at the position Match in the array.
|
2008-12-23 08:26:44 +08:00
|
|
|
for (unsigned Idx = Size; Idx > InsertPos; --Idx)
|
2008-12-12 00:49:14 +08:00
|
|
|
Array[Idx] = Array[Idx-1];
|
|
|
|
|
2008-12-23 08:26:44 +08:00
|
|
|
Array[InsertPos] = D;
|
2008-12-12 00:49:14 +08:00
|
|
|
LookupPtr.setInt(Size + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've reached capacity in this array. Create a map and copy in
|
|
|
|
// all of the declarations that were stored in the array.
|
|
|
|
StoredDeclsMap *Map = new StoredDeclsMap(16);
|
|
|
|
LookupPtr.setPointer(Map);
|
|
|
|
LookupPtr.setInt(LookupIsMap);
|
2008-12-12 04:41:00 +08:00
|
|
|
for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
|
2008-12-12 00:49:14 +08:00
|
|
|
insertImpl(Array[Idx]);
|
|
|
|
delete [] Array;
|
|
|
|
|
|
|
|
// Fall through to perform insertion into the map.
|
2008-12-23 08:26:44 +08:00
|
|
|
MayBeRedeclaration = false;
|
|
|
|
}
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
|
|
// Insert this declaration into the map.
|
|
|
|
StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
|
|
|
|
StoredDeclsMap::iterator Pos = Map->find(D->getDeclName());
|
2008-12-23 08:26:44 +08:00
|
|
|
if (Pos != Map->end()) {
|
|
|
|
if (MayBeRedeclaration) {
|
|
|
|
// Determine if this declaration is actually a redeclaration.
|
2008-12-24 05:05:05 +08:00
|
|
|
std::vector<ScopedDecl *>::iterator Redecl
|
|
|
|
= std::find_if(Pos->second.begin(), Pos->second.end(),
|
|
|
|
std::bind1st(std::mem_fun(&ScopedDecl::declarationReplaces),
|
|
|
|
D));
|
|
|
|
if (Redecl != Pos->second.end()) {
|
|
|
|
*Redecl = D;
|
|
|
|
return;
|
2008-12-23 08:26:44 +08:00
|
|
|
}
|
|
|
|
}
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
|
|
// Put this declaration into the appropriate slot.
|
2008-12-23 08:26:44 +08:00
|
|
|
if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty())
|
|
|
|
Pos->second.push_back(D);
|
|
|
|
else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
|
|
|
|
ScopedDecl *TagD = Pos->second.back();
|
|
|
|
Pos->second.back() = D;
|
|
|
|
Pos->second.push_back(TagD);
|
|
|
|
} else
|
|
|
|
Pos->second.push_back(D);
|
2008-12-12 00:49:14 +08:00
|
|
|
} else {
|
2008-12-23 08:26:44 +08:00
|
|
|
(*Map)[D->getDeclName()].push_back(D);
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
}
|