Improve handling of base initializers. We now parse initializers in out of line decls, such as:

class C {
    C() { }
    
    int a;
};

C::C() : a(10) { }

We also diagnose when initializers are used on declarations that aren't constructors:

t.cpp:1:10: error: only constructors take base initializers
void f() : a(10) { }
         ^

Doug and/or Sebastian: I'd appreciate a review, especially the nested-name-spec test results (from the looks of it we now match gcc in that test.)

llvm-svn: 67672
This commit is contained in:
Anders Carlsson 2009-03-25 02:58:17 +00:00
parent 3cfc2e214a
commit 75fdaa465f
6 changed files with 33 additions and 2 deletions

View File

@ -1219,6 +1219,9 @@ def err_overload_multiple_match : Error<
"more than one matching function found in __builtin_overload">; "more than one matching function found in __builtin_overload">;
// C++ member initializers. // C++ member initializers.
def err_only_constructors_take_base_inits : Error<
"only constructors take base initializers">;
def err_mem_init_not_member_or_class : Error< def err_mem_init_not_member_or_class : Error<
"member initializer %0 does not name a non-static data member or base " "member initializer %0 does not name a non-static data member or base "
"class">; "class">;

View File

@ -505,7 +505,9 @@ Parser::ParseDeclarationOrFunctionDefinition(
} else if (DeclaratorInfo.isFunctionDeclarator() && } else if (DeclaratorInfo.isFunctionDeclarator() &&
(Tok.is(tok::l_brace) || // int X() {} (Tok.is(tok::l_brace) || // int X() {}
(!getLang().CPlusPlus && (!getLang().CPlusPlus &&
isDeclarationSpecifier()))) { // int X(f) int f; {} isDeclarationSpecifier()) || // int X(f) int f; {}
(getLang().CPlusPlus &&
Tok.is(tok::colon)))) { // X() : Base() {} (used for ctors)
if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Diag(Tok, diag::err_function_declared_typedef); Diag(Tok, diag::err_function_declared_typedef);

View File

@ -1588,6 +1588,10 @@ public:
void AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl); void AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl);
virtual void ActOnMemInitializers(DeclTy *ConstructorDecl,
SourceLocation ColonLoc,
MemInitTy **MemInits, unsigned NumMemInits);
virtual void ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, virtual void ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc,
DeclTy *TagDecl, DeclTy *TagDecl,
SourceLocation LBrac, SourceLocation LBrac,

View File

@ -706,6 +706,18 @@ Sema::ActOnMemInitializer(DeclTy *ConstructorD,
return new CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, NumArgs); return new CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, NumArgs);
} }
void Sema::ActOnMemInitializers(DeclTy *ConstructorDecl,
SourceLocation ColonLoc,
MemInitTy **MemInits, unsigned NumMemInits) {
CXXConstructorDecl *Constructor =
dyn_cast<CXXConstructorDecl>((Decl *)ConstructorDecl);
if (!Constructor) {
Diag(ColonLoc, diag::err_only_constructors_take_base_inits);
return;
}
}
namespace { namespace {
/// PureVirtualMethodCollector - traverses a class and its superclasses /// PureVirtualMethodCollector - traverses a class and its superclasses
/// and determines if it has any pure virtual methods. /// and determines if it has any pure virtual methods.

View File

@ -45,3 +45,12 @@ public:
class G : A { class G : A {
G() : A(10); // expected-error{{expected '{'}} G() : A(10); // expected-error{{expected '{'}}
}; };
void f() : a(242) { } // expected-error{{only constructors take base initializers}}
class H : A {
H();
};
H::H() : A(10) { }

View File

@ -168,5 +168,6 @@ Y::foo y; // expected-error{{incomplete type 'struct Y' named in nested name spe
// FIXME: ugly: expected-error{{invalid token after top level declarator}} // FIXME: ugly: expected-error{{invalid token after top level declarator}}
X::X() : a(5) { } // expected-error{{use of undeclared identifier 'X'}} \ X::X() : a(5) { } // expected-error{{use of undeclared identifier 'X'}} \
// expected-error{{expected function body after function declarator}} // expected-error{{C++ requires a type specifier for all declarations}} \
// expected-error{{only constructors take base initializers}}