Add a FriendClassDecl type for holding declarations of friend types in

the AST, and create such declarations.

llvm-svn: 78719
This commit is contained in:
John McCall 2009-08-11 21:13:21 +00:00
parent cc9ca3500d
commit 2658c4e2d9
5 changed files with 128 additions and 54 deletions

View File

@ -1273,6 +1273,42 @@ public:
static bool classof(const FriendFunctionDecl *D) { return true; } static bool classof(const FriendFunctionDecl *D) { return true; }
}; };
/// FriendClassDecl - Represents the declaration of a friend class.
class FriendClassDecl : public Decl {
// The friended type. In C++0x, this can be an arbitrary type,
// which we simply ignore if it's not a record type.
const QualType FriendType;
// Location of the 'friend' specifier.
const SourceLocation FriendLoc;
FriendClassDecl(DeclContext *DC, SourceLocation L,
QualType T, SourceLocation FriendL)
: Decl(FriendClass, DC, L),
FriendType(T),
FriendLoc(FriendL)
{}
public:
static FriendClassDecl *Create(ASTContext &C, DeclContext *DC,
SourceLocation L, QualType T,
SourceLocation FriendL);
QualType getFriendType() const {
return FriendType;
}
SourceLocation getFriendLoc() const {
return FriendLoc;
}
// Implement isa/cast/dyncast/etc.
static bool classof(const Decl *D) {
return D->getKind() == FriendClass;
}
static bool classof(const FriendClassDecl *D) { return true; }
};
/// LinkageSpecDecl - This represents a linkage specification. For example: /// LinkageSpecDecl - This represents a linkage specification. For example:
/// extern "C" void foo(); /// extern "C" void foo();
/// ///

View File

@ -125,6 +125,7 @@ DECL(ObjCPropertyImpl, Decl)
DECL(ObjCForwardProtocol, Decl) DECL(ObjCForwardProtocol, Decl)
DECL(ObjCClass, Decl) DECL(ObjCClass, Decl)
DECL(FileScopeAsm, Decl) DECL(FileScopeAsm, Decl)
DECL(FriendClass, Decl)
DECL(StaticAssert, Decl) DECL(StaticAssert, Decl)
LAST_DECL(Block, Decl) LAST_DECL(Block, Decl)

View File

@ -199,6 +199,7 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
case ObjCCompatibleAlias: case ObjCCompatibleAlias:
return IDNS_Ordinary; return IDNS_Ordinary;
case FriendClass:
case FriendFunction: case FriendFunction:
return IDNS_Friend; return IDNS_Friend;

View File

@ -743,7 +743,8 @@ bool OverloadIterator::Equals(const OverloadIterator &Other) const {
return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter; return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
} }
FriendFunctionDecl *FriendFunctionDecl::Create(ASTContext &C,DeclContext *DC, FriendFunctionDecl *FriendFunctionDecl::Create(ASTContext &C,
DeclContext *DC,
SourceLocation L, SourceLocation L,
DeclarationName N, QualType T, DeclarationName N, QualType T,
bool isInline, bool isInline,
@ -751,6 +752,11 @@ FriendFunctionDecl *FriendFunctionDecl::Create(ASTContext &C,DeclContext *DC,
return new (C) FriendFunctionDecl(DC, L, N, T, isInline, FriendL); return new (C) FriendFunctionDecl(DC, L, N, T, isInline, FriendL);
} }
FriendClassDecl *FriendClassDecl::Create(ASTContext &C, DeclContext *DC,
SourceLocation L, QualType T,
SourceLocation FriendL) {
return new (C) FriendClassDecl(DC, L, T, FriendL);
}
LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
DeclContext *DC, DeclContext *DC,

View File

@ -3320,9 +3320,29 @@ Sema::DeclPtrTy Sema::ActOnFriendDecl(Scope *S,
assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified);
// If there's no declarator, then this can only be a friend class // If there's no declarator, then this can only be a friend class
// declaration (or else it's just invalid). // declaration (or else it's just syntactically invalid).
if (!D) { if (!D) {
SourceLocation Loc = DS.getSourceRange().getBegin();
QualType T;
DeclContext *DC;
// In C++0x, we just accept any old type.
if (getLangOptions().CPlusPlus0x) {
bool invalid = false;
QualType T = ConvertDeclSpecToType(DS, Loc, invalid);
if (invalid)
return DeclPtrTy();
// The semantic context in which to create the decl. If it's not
// a record decl (or we don't yet know if it is), create it in the
// current context.
DC = CurContext;
if (const RecordType *RT = T->getAs<RecordType>())
DC = RT->getDecl()->getDeclContext();
// The C++98 rules are somewhat more complex.
} else {
// C++ [class.friend]p2: // C++ [class.friend]p2:
// An elaborated-type-specifier shall be used in a friend declaration // An elaborated-type-specifier shall be used in a friend declaration
// for a class.* // for a class.*
@ -3333,7 +3353,7 @@ Sema::DeclPtrTy Sema::ActOnFriendDecl(Scope *S,
case DeclSpec::TST_class: case DeclSpec::TST_class:
case DeclSpec::TST_struct: case DeclSpec::TST_struct:
case DeclSpec::TST_union: case DeclSpec::TST_union:
RD = dyn_cast_or_null<CXXRecordDecl>(static_cast<Decl*>(DS.getTypeRep())); RD = dyn_cast_or_null<CXXRecordDecl>((Decl*) DS.getTypeRep());
if (!RD) return DeclPtrTy(); if (!RD) return DeclPtrTy();
break; break;
@ -3370,10 +3390,7 @@ Sema::DeclPtrTy Sema::ActOnFriendDecl(Scope *S,
// or class that is not a member of the class . . . // or class that is not a member of the class . . .
// But that's a silly restriction which nobody implements for // But that's a silly restriction which nobody implements for
// inner classes, and C++0x removes it anyway, so we only report // inner classes, and C++0x removes it anyway, so we only report
// this // this (as a warning) if we're being pedantic.
// But no-one implements it that way, and C++0x removes this
// restriction, so we only report it (as a warning) if we're being
// pedantic. Ideally this would real -pedantic mode
// //
// Also, definitions currently get treated in a way that causes // Also, definitions currently get treated in a way that causes
// this error, so only report it if we didn't see a definition. // this error, so only report it if we didn't see a definition.
@ -3381,7 +3398,20 @@ Sema::DeclPtrTy Sema::ActOnFriendDecl(Scope *S,
!getLangOptions().CPlusPlus0x) !getLangOptions().CPlusPlus0x)
Diag(DS.getFriendSpecLoc(), diag::ext_friend_inner_class); Diag(DS.getFriendSpecLoc(), diag::ext_friend_inner_class);
return DeclPtrTy::make(RD); T = QualType(RD->getTypeForDecl(), 0);
DC = RD->getDeclContext();
}
FriendClassDecl *FCD = FriendClassDecl::Create(Context, DC, Loc, T,
DS.getFriendSpecLoc());
FCD->setLexicalDeclContext(CurContext);
if (CurContext->isDependentContext())
CurContext->addHiddenDecl(FCD);
else
CurContext->addDecl(FCD);
return DeclPtrTy::make(FCD);
} }
// We have a declarator. // We have a declarator.