summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/AST/DeclBase.cpp1
-rw-r--r--clang/lib/AST/DeclCXX.cpp14
-rw-r--r--clang/lib/Parse/ParseDecl.cpp49
-rw-r--r--clang/lib/Parse/ParseDeclCXX.cpp53
-rw-r--r--clang/lib/Sema/Sema.h8
-rw-r--r--clang/lib/Sema/SemaDecl.cpp100
-rw-r--r--clang/lib/Sema/SemaDeclCXX.cpp232
7 files changed, 359 insertions, 98 deletions
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 3c6c91ea0ab..de7a1d3ca43 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -238,6 +238,7 @@ void Decl::addDeclKind(Kind k) {
// FIXME: Statistics for C++ decls.
case CXXMethod:
case CXXConstructor:
+ case CXXDestructor:
case CXXClassVar:
break;
}
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index cf59d1af360..b0df75b404d 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -44,6 +44,10 @@ void CXXRecordDecl::Destroy(ASTContext &C) {
= Constructors.function_begin();
func != Constructors.function_end(); ++func)
(*func)->Destroy(C);
+
+ if (isDefinition())
+ Destructor->Destroy(C);
+
RecordDecl::Destroy(C);
}
@@ -218,6 +222,16 @@ bool CXXConstructorDecl::isConvertingConstructor() const {
(getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0);
}
+CXXDestructorDecl *
+CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
+ SourceLocation L, IdentifierInfo *Id,
+ QualType T, bool isInline,
+ bool isImplicitlyDeclared) {
+ void *Mem = C.getAllocator().Allocate<CXXDestructorDecl>();
+ return new (Mem) CXXDestructorDecl(RD, L, Id, T, isInline,
+ isImplicitlyDeclared);
+}
+
CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
SourceLocation L, IdentifierInfo *Id,
QualType T, ScopedDecl *PrevDecl) {
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index f414f16af73..e1fbb6dfcab 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -1243,7 +1243,7 @@ void Parser::ParseDeclaratorInternal(Declarator &D) {
/// ParseDirectDeclarator
/// direct-declarator: [C99 6.7.5]
-/// identifier
+/// [C99] identifier
/// '(' declarator ')'
/// [GNU] '(' attributes declarator ')'
/// [C90] direct-declarator '[' constant-expression[opt] ']'
@@ -1258,16 +1258,53 @@ void Parser::ParseDeclaratorInternal(Declarator &D) {
/// [C++] direct-declarator '(' parameter-declaration-clause ')'
/// cv-qualifier-seq[opt] exception-specification[opt]
/// [C++] declarator-id
-//
-// declarator-id: [C++ 8]
-// id-expression
-// '::'[opt] nested-name-specifier[opt] type-name
+///
+/// declarator-id: [C++ 8]
+/// id-expression
+/// '::'[opt] nested-name-specifier[opt] type-name
+///
+/// id-expression: [C++ 5.1]
+/// unqualified-id
+/// qualified-id [TODO]
+///
+/// unqualified-id: [C++ 5.1]
+/// identifier
+/// operator-function-id [TODO]
+/// conversion-function-id [TODO]
+/// '~' class-name
+/// template-id [TODO]
void Parser::ParseDirectDeclarator(Declarator &D) {
// Parse the first direct-declarator seen.
if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
assert(Tok.getIdentifierInfo() && "Not an identifier?");
- D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
+ // Determine whether this identifier is a C++ constructor name or
+ // a normal identifier.
+ if (getLang().CPlusPlus &&
+ CurScope->isCXXClassScope() &&
+ Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
+ D.SetConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope),
+ Tok.getIdentifierInfo(), Tok.getLocation());
+ else
+ D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
ConsumeToken();
+ } else if (getLang().CPlusPlus && Tok.is(tok::tilde) &&
+ CurScope->isCXXClassScope() && D.mayHaveIdentifier()) {
+ // This should be a C++ destructor.
+ SourceLocation TildeLoc = ConsumeToken();
+
+ // Use the next identifier and "~" to form a name for the
+ // destructor. This is useful both for diagnostics and for
+ // correctness of the parser, since we use presence/absence of the
+ // identifier to determine what we parsed.
+ // FIXME: We could end up with a template-id here, once we parse
+ // templates, and will have to do something different to form the
+ // name of the destructor.
+ assert(Tok.is(tok::identifier) && "Expected identifier");
+ IdentifierInfo *II = Tok.getIdentifierInfo();
+ II = &PP.getIdentifierTable().get(std::string("~") + II->getName());
+
+ if (TypeTy *Type = ParseClassName())
+ D.SetDestructor(Type, II, TildeLoc);
} else if (Tok.is(tok::l_paren)) {
// direct-declarator: '(' declarator ')'
// direct-declarator: '(' attributes declarator ')'
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 57fa193e756..f90469acacb 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -128,6 +128,37 @@ Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, D);
}
+/// ParseClassName - Parse a C++ class-name, which names a class. Note
+/// that we only check that the result names a type; semantic analysis
+/// will need to verify that the type names a class. The result is
+/// either a type or NULL, dependending on whether a type name was
+/// found.
+///
+/// class-name: [C++ 9.1]
+/// identifier
+/// template-id [TODO]
+///
+Parser::TypeTy *Parser::ParseClassName() {
+ // Parse the class-name.
+ // FIXME: Alternatively, parse a simple-template-id.
+ if (Tok.isNot(tok::identifier)) {
+ Diag(Tok.getLocation(), diag::err_expected_class_name);
+ return 0;
+ }
+
+ // We have an identifier; check whether it is actually a type.
+ TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
+ if (!Type) {
+ Diag(Tok.getLocation(), diag::err_expected_class_name);
+ return 0;
+ }
+
+ // Consume the identifier.
+ ConsumeToken();
+
+ return Type;
+}
+
/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
/// until we reach the start of a definition or see a token that
@@ -325,29 +356,17 @@ Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
// FIXME: Parse optional '::' and optional nested-name-specifier.
- // Parse the class-name.
- // FIXME: Alternatively, parse a simple-template-id.
- if (Tok.isNot(tok::identifier)) {
- Diag(Tok.getLocation(), diag::err_expected_class_name);
- return true;
- }
-
- // We have an identifier; check whether it is actually a type.
- TypeTy *BaseType = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
- if (!BaseType) {
- Diag(Tok.getLocation(), diag::err_expected_class_name);
- return true;
- }
-
// The location of the base class itself.
SourceLocation BaseLoc = Tok.getLocation();
+
+ // Parse the class-name.
+ TypeTy *BaseType = ParseClassName();
+ if (!BaseType)
+ return true;
// Find the complete source range for the base-specifier.
SourceRange Range(StartLoc, BaseLoc);
- // Consume the identifier token (finally!).
- ConsumeToken();
-
// Notify semantic analysis that we have parsed a complete
// base-specifier.
return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, BaseType,
diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h
index d14c5ffa200..68d2eeefc50 100644
--- a/clang/lib/Sema/Sema.h
+++ b/clang/lib/Sema/Sema.h
@@ -836,7 +836,13 @@ public:
virtual void ActOnFinishCXXClassDef(DeclTy *TagDecl);
- virtual DeclTy *ActOnConstructorDeclarator(CXXConstructorDecl *ConDecl);
+
+ bool CheckConstructorDeclarator(Declarator &D, QualType &R,
+ FunctionDecl::StorageClass& SC);
+ bool CheckDestructorDeclarator(Declarator &D, QualType &R,
+ FunctionDecl::StorageClass& SC);
+ DeclTy *ActOnConstructorDeclarator(CXXConstructorDecl *Constructor);
+ DeclTy *ActOnDestructorDeclarator(CXXDestructorDecl *Destructor);
//===--------------------------------------------------------------------===//
// C++ Derived Classes
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c8c253a3fba..38a5c3780b7 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -800,79 +800,16 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
}
bool isInline = D.getDeclSpec().isInlineSpecified();
- bool isVirtual = D.getDeclSpec().isVirtualSpecified();
+ // bool isVirtual = D.getDeclSpec().isVirtualSpecified();
bool isExplicit = D.getDeclSpec().isExplicitSpecified();
FunctionDecl *NewFD;
- if (isCurrentClassName(*II, S)) {
+ if (D.getKind() == Declarator::DK_Constructor) {
// This is a C++ constructor declaration.
assert(D.getContext() == Declarator::MemberContext &&
"Constructors can only be declared in a member context");
- // C++ [class.ctor]p3:
- // A constructor shall not be virtual (10.3) or static (9.4). A
- // constructor can be invoked for a const, volatile or const
- // volatile object. A constructor shall not be declared const,
- // volatile, or const volatile (9.3.2).
- if (isVirtual) {
- Diag(D.getIdentifierLoc(),
- diag::err_constructor_cannot_be,
- "virtual",
- SourceRange(D.getDeclSpec().getVirtualSpecLoc()),
- SourceRange(D.getIdentifierLoc()));
- isVirtual = false;
- }
- if (SC == FunctionDecl::Static) {
- Diag(D.getIdentifierLoc(),
- diag::err_constructor_cannot_be,
- "static",
- SourceRange(D.getDeclSpec().getStorageClassSpecLoc()),
- SourceRange(D.getIdentifierLoc()));
- isVirtual = false;
- }
- if (D.getDeclSpec().hasTypeSpecifier()) {
- // Constructors don't have return types, but the parser will
- // happily parse something like:
- //
- // class X {
- // float X(float);
- // };
- //
- // The return type will be eliminated later.
- Diag(D.getIdentifierLoc(),
- diag::err_constructor_return_type,
- SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()),
- SourceRange(D.getIdentifierLoc()));
- }
- if (R->getAsFunctionTypeProto()->getTypeQuals() != 0) {
- DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
- if (FTI.TypeQuals & QualType::Const)
- Diag(D.getIdentifierLoc(),
- diag::err_invalid_qualified_constructor,
- "const",
- SourceRange(D.getIdentifierLoc()));
- if (FTI.TypeQuals & QualType::Volatile)
- Diag(D.getIdentifierLoc(),
- diag::err_invalid_qualified_constructor,
- "volatile",
- SourceRange(D.getIdentifierLoc()));
- if (FTI.TypeQuals & QualType::Restrict)
- Diag(D.getIdentifierLoc(),
- diag::err_invalid_qualified_constructor,
- "restrict",
- SourceRange(D.getIdentifierLoc()));
- }
-
- // Rebuild the function type "R" without any type qualifiers (in
- // case any of the errors above fired) and with "void" as the
- // return type, since constructors don't have return types. We
- // *always* have to do this, because GetTypeForDeclarator will
- // put in a result type of "int" when none was specified.
- const FunctionTypeProto *Proto = R->getAsFunctionTypeProto();
- R = Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(),
- Proto->getNumArgs(),
- Proto->isVariadic(),
- 0);
+ bool isInvalidDecl = CheckConstructorDeclarator(D, R, SC);
// Create the new declaration
NewFD = CXXConstructorDecl::Create(Context,
@@ -881,6 +818,23 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
isExplicit, isInline,
/*isImplicitlyDeclared=*/false);
+ if (isInvalidDecl)
+ NewFD->setInvalidDecl();
+ } else if (D.getKind() == Declarator::DK_Destructor) {
+ // This is a C++ destructor declaration.
+ assert(D.getContext() == Declarator::MemberContext &&
+ "Destructor can only be declared in a member context");
+
+ bool isInvalidDecl = CheckDestructorDeclarator(D, R, SC);
+
+ NewFD = CXXDestructorDecl::Create(Context,
+ cast<CXXRecordDecl>(CurContext),
+ D.getIdentifierLoc(), II, R,
+ isInline,
+ /*isImplicitlyDeclared=*/false);
+
+ if (isInvalidDecl)
+ NewFD->setInvalidDecl();
} else if (D.getContext() == Declarator::MemberContext) {
// This is a C++ method declaration.
NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(CurContext),
@@ -969,12 +923,14 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
}
}
- // C++ constructors are handled by a separate routine, since they
- // don't require any declaration merging (C++ [class.mfct]p2) and
- // they aren't ever pushed into scope, because they can't be found
- // by name lookup anyway (C++ [class.ctor]p2).
- if (CXXConstructorDecl *ConDecl = dyn_cast<CXXConstructorDecl>(NewFD))
- return ActOnConstructorDeclarator(ConDecl);
+ // C++ constructors and destructors are handled by separate
+ // routines, since they don't require any declaration merging (C++
+ // [class.mfct]p2) and they aren't ever pushed into scope, because
+ // they can't be found by name lookup anyway (C++ [class.ctor]p2).
+ if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(NewFD))
+ return ActOnConstructorDeclarator(Constructor);
+ else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(NewFD))
+ return ActOnDestructorDeclarator(Destructor);
// Merge the decl with the existing one if appropriate. Since C functions
// are in a flat namespace, make sure we consider decls in outer scopes.
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 53051ff57fb..ea3c175f162 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -383,6 +383,7 @@ void Sema::ActOnStartCXXClassDef(Scope *S, DeclTy *D, SourceLocation LBrace) {
// class itself; this is known as the injected-class-name. For
// purposes of access checking, the injected-class-name is treated
// as if it were a public member name.
+ // FIXME: this should probably have its own kind of type node.
TypedefDecl *InjectedClassName
= TypedefDecl::Create(Context, Dcl, LBrace, Dcl->getIdentifier(),
Context.getTypeDeclType(Dcl), /*PrevDecl=*/0);
@@ -768,7 +769,25 @@ void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) {
ClassDecl->addConstructor(Context, CopyConstructor);
}
- // FIXME: Implicit destructor
+ if (!ClassDecl->getDestructor()) {
+ // C++ [class.dtor]p2:
+ // If a class has no user-declared destructor, a destructor is
+ // declared implicitly. An implicitly-declared destructor is an
+ // inline public member of its class.
+ std::string DestructorName = "~";
+ DestructorName += ClassDecl->getName();
+ CXXDestructorDecl *Destructor
+ = CXXDestructorDecl::Create(Context, ClassDecl,
+ ClassDecl->getLocation(),
+ &PP.getIdentifierTable().get(DestructorName),
+ Context.getFunctionType(Context.VoidTy,
+ 0, 0, false, 0),
+ /*isInline=*/true,
+ /*isImplicitlyDeclared=*/true);
+ Destructor->setAccess(AS_public);
+ ClassDecl->setDestructor(Destructor);
+ }
+
// FIXME: Implicit copy assignment operator
}
@@ -783,6 +802,191 @@ void Sema::ActOnFinishCXXClassDef(DeclTy *D) {
Consumer.HandleTagDeclDefinition(Rec);
}
+/// CheckConstructorDeclarator - Called by ActOnDeclarator to check
+/// the well-formednes of the constructor declarator @p D with type @p
+/// R. If there are any errors in the declarator, this routine will
+/// emit diagnostics and return true. Otherwise, it will return
+/// false. Either way, the type @p R will be updated to reflect a
+/// well-formed type for the constructor.
+bool Sema::CheckConstructorDeclarator(Declarator &D, QualType &R,
+ FunctionDecl::StorageClass& SC) {
+ bool isVirtual = D.getDeclSpec().isVirtualSpecified();
+ bool isInvalid = false;
+
+ // C++ [class.ctor]p3:
+ // A constructor shall not be virtual (10.3) or static (9.4). A
+ // constructor can be invoked for a const, volatile or const
+ // volatile object. A constructor shall not be declared const,
+ // volatile, or const volatile (9.3.2).
+ if (isVirtual) {
+ Diag(D.getIdentifierLoc(),
+ diag::err_constructor_cannot_be,
+ "virtual",
+ SourceRange(D.getDeclSpec().getVirtualSpecLoc()),
+ SourceRange(D.getIdentifierLoc()));
+ isInvalid = true;
+ }
+ if (SC == FunctionDecl::Static) {
+ Diag(D.getIdentifierLoc(),
+ diag::err_constructor_cannot_be,
+ "static",
+ SourceRange(D.getDeclSpec().getStorageClassSpecLoc()),
+ SourceRange(D.getIdentifierLoc()));
+ isInvalid = true;
+ SC = FunctionDecl::None;
+ }
+ if (D.getDeclSpec().hasTypeSpecifier()) {
+ // Constructors don't have return types, but the parser will
+ // happily parse something like:
+ //
+ // class X {
+ // float X(float);
+ // };
+ //
+ // The return type will be eliminated later.
+ Diag(D.getIdentifierLoc(),
+ diag::err_constructor_return_type,
+ SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()),
+ SourceRange(D.getIdentifierLoc()));
+ }
+ if (R->getAsFunctionTypeProto()->getTypeQuals() != 0) {
+ DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
+ if (FTI.TypeQuals & QualType::Const)
+ Diag(D.getIdentifierLoc(),
+ diag::err_invalid_qualified_constructor,
+ "const",
+ SourceRange(D.getIdentifierLoc()));
+ if (FTI.TypeQuals & QualType::Volatile)
+ Diag(D.getIdentifierLoc(),
+ diag::err_invalid_qualified_constructor,
+ "volatile",
+ SourceRange(D.getIdentifierLoc()));
+ if (FTI.TypeQuals & QualType::Restrict)
+ Diag(D.getIdentifierLoc(),
+ diag::err_invalid_qualified_constructor,
+ "restrict",
+ SourceRange(D.getIdentifierLoc()));
+ }
+
+ // Rebuild the function type "R" without any type qualifiers (in
+ // case any of the errors above fired) and with "void" as the
+ // return type, since constructors don't have return types. We
+ // *always* have to do this, because GetTypeForDeclarator will
+ // put in a result type of "int" when none was specified.
+ const FunctionTypeProto *Proto = R->getAsFunctionTypeProto();
+ R = Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(),
+ Proto->getNumArgs(),
+ Proto->isVariadic(),
+ 0);
+
+ return isInvalid;
+}
+
+/// CheckDestructorDeclarator - Called by ActOnDeclarator to check
+/// the well-formednes of the destructor declarator @p D with type @p
+/// R. If there are any errors in the declarator, this routine will
+/// emit diagnostics and return true. Otherwise, it will return
+/// false. Either way, the type @p R will be updated to reflect a
+/// well-formed type for the destructor.
+bool Sema::CheckDestructorDeclarator(Declarator &D, QualType &R,
+ FunctionDecl::StorageClass& SC) {
+ bool isInvalid = false;
+
+ // C++ [class.dtor]p1:
+ // [...] A typedef-name that names a class is a class-name
+ // (7.1.3); however, a typedef-name that names a class shall not
+ // be used as the identifier in the declarator for a destructor
+ // declaration.
+ TypeDecl *DeclaratorTypeD = (TypeDecl *)D.getDeclaratorIdType();
+ if (const TypedefDecl *TypedefD = dyn_cast<TypedefDecl>(DeclaratorTypeD)) {
+ if (TypedefD->getIdentifier() !=
+ cast<CXXRecordDecl>(CurContext)->getIdentifier()) {
+ // FIXME: This would be easier if we could just look at whether
+ // we found the injected-class-name.
+ Diag(D.getIdentifierLoc(),
+ diag::err_destructor_typedef_name,
+ TypedefD->getName());
+ isInvalid = true;
+ }
+ }
+
+ // C++ [class.dtor]p2:
+ // A destructor is used to destroy objects of its class type. A
+ // destructor takes no parameters, and no return type can be
+ // specified for it (not even void). The address of a destructor
+ // shall not be taken. A destructor shall not be static. A
+ // destructor can be invoked for a const, volatile or const
+ // volatile object. A destructor shall not be declared const,
+ // volatile or const volatile (9.3.2).
+ if (SC == FunctionDecl::Static) {
+ Diag(D.getIdentifierLoc(),
+ diag::err_destructor_cannot_be,
+ "static",
+ SourceRange(D.getDeclSpec().getStorageClassSpecLoc()),
+ SourceRange(D.getIdentifierLoc()));
+ isInvalid = true;
+ SC = FunctionDecl::None;
+ }
+ if (D.getDeclSpec().hasTypeSpecifier()) {
+ // Destructors don't have return types, but the parser will
+ // happily parse something like:
+ //
+ // class X {
+ // float ~X();
+ // };
+ //
+ // The return type will be eliminated later.
+ Diag(D.getIdentifierLoc(),
+ diag::err_destructor_return_type,
+ SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()),
+ SourceRange(D.getIdentifierLoc()));
+ }
+ if (R->getAsFunctionTypeProto()->getTypeQuals() != 0) {
+ DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
+ if (FTI.TypeQuals & QualType::Const)
+ Diag(D.getIdentifierLoc(),
+ diag::err_invalid_qualified_destructor,
+ "const",
+ SourceRange(D.getIdentifierLoc()));
+ if (FTI.TypeQuals & QualType::Volatile)
+ Diag(D.getIdentifierLoc(),
+ diag::err_invalid_qualified_destructor,
+ "volatile",
+ SourceRange(D.getIdentifierLoc()));
+ if (FTI.TypeQuals & QualType::Restrict)
+ Diag(D.getIdentifierLoc(),
+ diag::err_invalid_qualified_destructor,
+ "restrict",
+ SourceRange(D.getIdentifierLoc()));
+ }
+
+ // Make sure we don't have any parameters.
+ if (R->getAsFunctionTypeProto()->getNumArgs() > 0) {
+ Diag(D.getIdentifierLoc(), diag::err_destructor_with_params);
+
+ // Delete the parameters.
+ DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
+ if (FTI.NumArgs) {
+ delete [] FTI.ArgInfo;
+ FTI.NumArgs = 0;
+ FTI.ArgInfo = 0;
+ }
+ }
+
+ // Make sure the destructor isn't variadic.
+ if (R->getAsFunctionTypeProto()->isVariadic())
+ Diag(D.getIdentifierLoc(), diag::err_destructor_variadic);
+
+ // Rebuild the function type "R" without any type qualifiers or
+ // parameters (in case any of the errors above fired) and with
+ // "void" as the return type, since destructors don't have return
+ // types. We *always* have to do this, because GetTypeForDeclarator
+ // will put in a result type of "int" when none was specified.
+ R = Context.getFunctionType(Context.VoidTy, 0, 0, false, 0);
+
+ return isInvalid;
+}
+
/// ActOnConstructorDeclarator - Called by ActOnDeclarator to complete
/// the declaration of the given C++ constructor ConDecl that was
/// built from declarator D. This routine is responsible for checking
@@ -837,7 +1041,7 @@ Sema::DeclTy *Sema::ActOnConstructorDeclarator(CXXConstructorDecl *ConDecl) {
diag::err_constructor_byvalue_arg,
SourceRange(ConDecl->getParamDecl(0)->getLocation()));
ConDecl->setInvalidDecl();
- return 0;
+ return ConDecl;
}
}
@@ -847,6 +1051,30 @@ Sema::DeclTy *Sema::ActOnConstructorDeclarator(CXXConstructorDecl *ConDecl) {
return (DeclTy *)ConDecl;
}
+/// ActOnDestructorDeclarator - Called by ActOnDeclarator to complete
+/// the declaration of the given C++ @p Destructor. This routine is
+/// responsible for recording the destructor in the C++ class, if
+/// possible.
+Sema::DeclTy *Sema::ActOnDestructorDeclarator(CXXDestructorDecl *Destructor) {
+ assert(Destructor && "Expected to receive a destructor declaration");
+
+ CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CurContext);
+
+ // Make sure we aren't redeclaring the destructor.
+ if (CXXDestructorDecl *PrevDestructor = ClassDecl->getDestructor()) {
+ Diag(Destructor->getLocation(), diag::err_destructor_redeclared);
+ Diag(PrevDestructor->getLocation(),
+ PrevDestructor->isThisDeclarationADefinition()?
+ diag::err_previous_definition
+ : diag::err_previous_declaration);
+ Destructor->setInvalidDecl();
+ return Destructor;
+ }
+
+ ClassDecl->setDestructor(Destructor);
+ return (DeclTy *)Destructor;
+}
+
//===----------------------------------------------------------------------===//
// Namespace Handling
//===----------------------------------------------------------------------===//
OpenPOWER on IntegriCloud