2006-10-16 06:34:45 +08:00
|
|
|
//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-10-16 06:34:45 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-06-04 21:04:04 +08:00
|
|
|
// This file implements the Decl subclasses.
|
2006-10-16 06:34:45 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/Decl.h"
|
2008-03-15 14:12:44 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 12:54:23 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2008-05-20 08:43:19 +08:00
|
|
|
|
2006-10-25 13:11:20 +08:00
|
|
|
using namespace clang;
|
2006-10-16 06:34:45 +08:00
|
|
|
|
2008-03-15 14:12:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Decl Allocation/Deallocation Method Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-27 21:50:30 +08:00
|
|
|
|
2008-04-17 22:40:12 +08:00
|
|
|
TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
|
|
|
|
void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
|
|
|
|
return new (Mem) TranslationUnitDecl();
|
|
|
|
}
|
|
|
|
|
2008-04-27 21:50:30 +08:00
|
|
|
NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
|
|
|
|
SourceLocation L, IdentifierInfo *Id) {
|
|
|
|
void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
|
|
|
|
return new (Mem) NamespaceDecl(DC, L, Id);
|
|
|
|
}
|
|
|
|
|
2008-05-20 12:49:55 +08:00
|
|
|
void NamespaceDecl::Destroy(ASTContext& C) {
|
|
|
|
// NamespaceDecl uses "NextDeclarator" to chain namespace declarations
|
|
|
|
// together. They are all top-level Decls.
|
|
|
|
|
2008-05-24 23:09:56 +08:00
|
|
|
this->~NamespaceDecl();
|
2008-05-20 12:49:55 +08:00
|
|
|
C.getAllocator().Deallocate((void *)this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 02:05:57 +08:00
|
|
|
ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
|
|
|
|
SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) {
|
|
|
|
void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>();
|
|
|
|
return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl);
|
|
|
|
}
|
|
|
|
|
2008-04-23 02:39:57 +08:00
|
|
|
VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
|
2008-04-16 06:42:06 +08:00
|
|
|
SourceLocation L,
|
|
|
|
IdentifierInfo *Id, QualType T,
|
|
|
|
StorageClass S, ScopedDecl *PrevDecl) {
|
|
|
|
void *Mem = C.getAllocator().Allocate<VarDecl>();
|
2008-04-23 02:39:57 +08:00
|
|
|
return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
|
2008-03-16 05:10:16 +08:00
|
|
|
}
|
|
|
|
|
2008-04-23 02:39:57 +08:00
|
|
|
ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation L, IdentifierInfo *Id,
|
|
|
|
QualType T, StorageClass S,
|
2008-04-08 12:40:51 +08:00
|
|
|
Expr *DefArg, ScopedDecl *PrevDecl) {
|
2008-03-16 05:10:16 +08:00
|
|
|
void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
|
2008-04-23 02:39:57 +08:00
|
|
|
return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
|
2008-03-16 05:10:16 +08:00
|
|
|
}
|
|
|
|
|
2008-04-23 02:39:57 +08:00
|
|
|
FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation L,
|
2008-03-16 05:24:04 +08:00
|
|
|
IdentifierInfo *Id, QualType T,
|
|
|
|
StorageClass S, bool isInline,
|
|
|
|
ScopedDecl *PrevDecl) {
|
|
|
|
void *Mem = C.getAllocator().Allocate<FunctionDecl>();
|
2008-06-10 05:05:31 +08:00
|
|
|
return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl);
|
2008-03-16 05:24:04 +08:00
|
|
|
}
|
|
|
|
|
2008-04-06 12:47:34 +08:00
|
|
|
FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
|
2008-03-16 08:16:02 +08:00
|
|
|
IdentifierInfo *Id, QualType T, Expr *BW) {
|
|
|
|
void *Mem = C.getAllocator().Allocate<FieldDecl>();
|
2008-04-06 12:47:34 +08:00
|
|
|
return new (Mem) FieldDecl(L, Id, T, BW);
|
2008-03-16 08:16:02 +08:00
|
|
|
}
|
|
|
|
|
2008-03-16 05:24:04 +08:00
|
|
|
|
2008-04-04 14:12:32 +08:00
|
|
|
EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
|
|
|
|
SourceLocation L,
|
2008-03-16 05:32:50 +08:00
|
|
|
IdentifierInfo *Id, QualType T,
|
|
|
|
Expr *E, const llvm::APSInt &V,
|
|
|
|
ScopedDecl *PrevDecl){
|
2008-03-15 14:12:44 +08:00
|
|
|
void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
|
2008-04-04 14:12:32 +08:00
|
|
|
return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
|
2008-03-15 14:12:44 +08:00
|
|
|
}
|
|
|
|
|
2008-05-20 12:49:55 +08:00
|
|
|
void EnumConstantDecl::Destroy(ASTContext& C) {
|
|
|
|
if (Init) Init->Destroy(C);
|
|
|
|
Decl::Destroy(C);
|
|
|
|
}
|
|
|
|
|
2008-04-23 02:39:57 +08:00
|
|
|
TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation L,
|
2008-03-16 05:32:50 +08:00
|
|
|
IdentifierInfo *Id, QualType T,
|
|
|
|
ScopedDecl *PD) {
|
2008-03-15 14:12:44 +08:00
|
|
|
void *Mem = C.getAllocator().Allocate<TypedefDecl>();
|
2008-04-23 02:39:57 +08:00
|
|
|
return new (Mem) TypedefDecl(DC, L, Id, T, PD);
|
2008-03-15 14:12:44 +08:00
|
|
|
}
|
|
|
|
|
2008-04-23 02:39:57 +08:00
|
|
|
EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
|
2008-04-04 14:12:32 +08:00
|
|
|
IdentifierInfo *Id,
|
2008-03-16 05:32:50 +08:00
|
|
|
ScopedDecl *PrevDecl) {
|
2008-03-15 14:12:44 +08:00
|
|
|
void *Mem = C.getAllocator().Allocate<EnumDecl>();
|
2008-04-23 02:39:57 +08:00
|
|
|
return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
|
2008-03-15 14:12:44 +08:00
|
|
|
}
|
|
|
|
|
2008-09-03 04:13:32 +08:00
|
|
|
void EnumDecl::Destroy(ASTContext& C) {
|
|
|
|
if (getEnumConstantList()) getEnumConstantList()->Destroy(C);
|
|
|
|
Decl::Destroy(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==------------------------------------------------------------------------==//
|
|
|
|
// RecordDecl methods.
|
|
|
|
//==------------------------------------------------------------------------==//
|
|
|
|
|
|
|
|
RecordDecl::RecordDecl(Kind DK, DeclContext *DC, SourceLocation L,
|
|
|
|
IdentifierInfo *Id, RecordDecl *PrevDecl)
|
|
|
|
: TagDecl(DK, DC, L, Id, 0), NextDecl(0) {
|
|
|
|
|
|
|
|
HasFlexibleArrayMember = false;
|
|
|
|
assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
|
|
|
|
Members = 0;
|
|
|
|
NumMembers = -1;
|
|
|
|
|
|
|
|
// Hook up the RecordDecl chain.
|
|
|
|
if (PrevDecl) {
|
|
|
|
RecordDecl* Tmp = PrevDecl->NextDecl;
|
|
|
|
// 'Tmp' might be non-NULL if it is the RecordDecl that provides the
|
|
|
|
// definition of the struct/union. By construction, the last RecordDecl
|
|
|
|
// in the chain is the 'defining' RecordDecl.
|
|
|
|
if (Tmp) {
|
|
|
|
assert (Tmp->NextDecl == 0);
|
|
|
|
assert (Tmp->Members && "Previous RecordDecl has a NextDecl that is "
|
|
|
|
"not the 'defining' RecordDecl");
|
|
|
|
|
|
|
|
NextDecl = Tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrevDecl->NextDecl = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-10 07:19:58 +08:00
|
|
|
RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
|
2008-04-04 14:12:32 +08:00
|
|
|
SourceLocation L, IdentifierInfo *Id,
|
2008-09-03 04:13:32 +08:00
|
|
|
RecordDecl *PrevDecl) {
|
2008-03-15 14:12:44 +08:00
|
|
|
void *Mem = C.getAllocator().Allocate<RecordDecl>();
|
2008-06-10 07:19:58 +08:00
|
|
|
Kind DK;
|
|
|
|
switch (TK) {
|
2008-06-17 07:45:12 +08:00
|
|
|
default: assert(0 && "Invalid TagKind!");
|
|
|
|
case TK_enum: assert(0 && "Enum TagKind passed for Record!");
|
|
|
|
case TK_struct: DK = Struct; break;
|
|
|
|
case TK_union: DK = Union; break;
|
|
|
|
case TK_class: DK = Class; break;
|
2008-06-10 07:19:58 +08:00
|
|
|
}
|
2008-04-23 02:39:57 +08:00
|
|
|
return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
|
2008-03-15 14:12:44 +08:00
|
|
|
}
|
|
|
|
|
2008-09-03 04:25:22 +08:00
|
|
|
/// getDefinitionDecl - Returns the RecordDecl for the struct/union that
|
|
|
|
/// represents the actual definition (i.e., not a forward declaration).
|
|
|
|
/// This method returns NULL if no such RecordDecl exists.
|
|
|
|
const RecordDecl* RecordDecl::getDefinitionDecl() const {
|
|
|
|
const RecordDecl* R = this;
|
|
|
|
|
|
|
|
for (RecordDecl* N = R->NextDecl; N; N = R->NextDecl)
|
|
|
|
R = N;
|
|
|
|
|
|
|
|
return R->Members ? R : 0;
|
|
|
|
}
|
|
|
|
|
2008-05-20 12:49:55 +08:00
|
|
|
|
2008-04-04 14:12:32 +08:00
|
|
|
FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
|
|
|
|
SourceLocation L,
|
2008-03-16 08:16:02 +08:00
|
|
|
StringLiteral *Str) {
|
|
|
|
void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
|
|
|
|
return new (Mem) FileScopeAsmDecl(L, Str);
|
|
|
|
}
|
|
|
|
|
2008-04-04 14:12:32 +08:00
|
|
|
LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
|
|
|
|
SourceLocation L,
|
2008-03-16 08:16:02 +08:00
|
|
|
LanguageIDs Lang, Decl *D) {
|
|
|
|
void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
|
|
|
|
return new (Mem) LinkageSpecDecl(L, Lang, D);
|
|
|
|
}
|
2008-03-15 14:12:44 +08:00
|
|
|
|
2008-03-31 08:36:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-04 14:12:32 +08:00
|
|
|
// NamedDecl Implementation
|
2008-03-31 08:36:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-04 14:12:32 +08:00
|
|
|
const char *NamedDecl::getName() const {
|
|
|
|
if (const IdentifierInfo *II = getIdentifier())
|
|
|
|
return II->getName();
|
|
|
|
return "";
|
2008-03-31 08:36:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FunctionDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-01-21 15:42:07 +08:00
|
|
|
FunctionDecl::~FunctionDecl() {
|
|
|
|
delete[] ParamInfo;
|
2008-04-21 10:02:58 +08:00
|
|
|
}
|
|
|
|
|
2008-05-20 08:43:19 +08:00
|
|
|
void FunctionDecl::Destroy(ASTContext& C) {
|
2008-05-20 11:56:00 +08:00
|
|
|
if (Body)
|
|
|
|
Body->Destroy(C);
|
|
|
|
|
|
|
|
for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
|
|
|
|
(*I)->Destroy(C);
|
|
|
|
|
2008-05-20 08:43:19 +08:00
|
|
|
Decl::Destroy(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-21 10:02:58 +08:00
|
|
|
Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
|
|
|
|
for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
|
|
|
|
if (FD->Body) {
|
|
|
|
Definition = FD;
|
|
|
|
return FD->Body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-01-21 15:42:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FunctionDecl::getNumParams() const {
|
2008-04-07 07:09:52 +08:00
|
|
|
const FunctionType *FT = getType()->getAsFunctionType();
|
|
|
|
if (isa<FunctionTypeNoProto>(FT))
|
2008-03-15 13:43:15 +08:00
|
|
|
return 0;
|
2008-04-07 07:09:52 +08:00
|
|
|
return cast<FunctionTypeProto>(FT)->getNumArgs();
|
2007-01-21 15:42:07 +08:00
|
|
|
}
|
|
|
|
|
2007-06-14 04:44:40 +08:00
|
|
|
void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
|
2007-01-21 15:42:07 +08:00
|
|
|
assert(ParamInfo == 0 && "Already has param info!");
|
|
|
|
assert(NumParams == getNumParams() && "Parameter count mismatch!");
|
|
|
|
|
2007-01-22 03:04:10 +08:00
|
|
|
// Zero params -> null pointer.
|
|
|
|
if (NumParams) {
|
2007-06-14 04:44:40 +08:00
|
|
|
ParamInfo = new ParmVarDecl*[NumParams];
|
|
|
|
memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
|
2007-01-22 03:04:10 +08:00
|
|
|
}
|
2007-01-21 15:42:07 +08:00
|
|
|
}
|
2007-01-25 12:52:46 +08:00
|
|
|
|
2008-04-10 10:22:51 +08:00
|
|
|
/// getMinRequiredArguments - Returns the minimum number of arguments
|
|
|
|
/// needed to call this function. This may be fewer than the number of
|
|
|
|
/// function parameters, if some of the parameters have default
|
2008-04-13 07:52:44 +08:00
|
|
|
/// arguments (in C++).
|
2008-04-10 10:22:51 +08:00
|
|
|
unsigned FunctionDecl::getMinRequiredArguments() const {
|
|
|
|
unsigned NumRequiredArgs = getNumParams();
|
|
|
|
while (NumRequiredArgs > 0
|
|
|
|
&& getParamDecl(NumRequiredArgs-1)->getDefaultArg())
|
|
|
|
--NumRequiredArgs;
|
|
|
|
|
|
|
|
return NumRequiredArgs;
|
|
|
|
}
|
|
|
|
|
2008-03-31 08:36:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RecordDecl Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-01-25 12:52:46 +08:00
|
|
|
|
2008-08-08 22:08:55 +08:00
|
|
|
RecordDecl::~RecordDecl() {
|
|
|
|
delete[] Members;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordDecl::Destroy(ASTContext& C) {
|
|
|
|
if (isDefinition())
|
|
|
|
for (field_iterator I=field_begin(), E=field_end(); I!=E; ++I)
|
|
|
|
(*I)->Destroy(C);
|
|
|
|
|
|
|
|
TagDecl::Destroy(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-25 12:52:46 +08:00
|
|
|
/// defineBody - When created, RecordDecl's correspond to a forward declared
|
|
|
|
/// record. This method is used to mark the decl as being defined, with the
|
|
|
|
/// specified contents.
|
2007-03-27 07:09:51 +08:00
|
|
|
void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
|
2007-01-25 12:52:46 +08:00
|
|
|
assert(!isDefinition() && "Cannot redefine record!");
|
|
|
|
setDefinition(true);
|
2007-01-25 14:27:24 +08:00
|
|
|
NumMembers = numMembers;
|
|
|
|
if (numMembers) {
|
2007-03-27 07:09:51 +08:00
|
|
|
Members = new FieldDecl*[numMembers];
|
2007-01-25 14:27:24 +08:00
|
|
|
memcpy(Members, members, numMembers*sizeof(Decl*));
|
2007-01-25 12:52:46 +08:00
|
|
|
}
|
|
|
|
}
|
2007-03-27 07:09:51 +08:00
|
|
|
|
2008-03-31 08:36:02 +08:00
|
|
|
FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
|
2007-03-27 07:09:51 +08:00
|
|
|
if (Members == 0 || NumMembers < 0)
|
|
|
|
return 0;
|
2007-10-04 08:45:27 +08:00
|
|
|
|
2008-03-31 08:36:02 +08:00
|
|
|
// Linear search. When C++ classes come along, will likely need to revisit.
|
|
|
|
for (int i = 0; i != NumMembers; ++i)
|
|
|
|
if (Members[i]->getIdentifier() == II)
|
2007-03-27 07:09:51 +08:00
|
|
|
return Members[i];
|
|
|
|
return 0;
|
2007-07-12 23:43:07 +08:00
|
|
|
}
|