aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema/SemaDeclCXX.cpp
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@mac.com>2009-08-28 03:16:11 +0000
committerAnders Carlsson <andersca@mac.com>2009-08-28 03:16:11 +0000
commitcf9f921268e67703d9cddcf9f2d6ac057c4c3cc8 (patch)
treefc54994b08b485f66e8546fdb2815586c7646ee5 /lib/Sema/SemaDeclCXX.cpp
parentdd46adef4e839686bf9f22c2f861d1f29c9095d4 (diff)
Many improvements to using declarations.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80332 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaDeclCXX.cpp')
-rw-r--r--lib/Sema/SemaDeclCXX.cpp87
1 files changed, 68 insertions, 19 deletions
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index ed01a7f2d0..b8384ee780 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -2094,13 +2094,13 @@ void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) {
Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S,
- SourceLocation UsingLoc,
- const CXXScopeSpec &SS,
- SourceLocation IdentLoc,
- IdentifierInfo *TargetName,
- OverloadedOperatorKind Op,
- AttributeList *AttrList,
- bool IsTypeName) {
+ SourceLocation UsingLoc,
+ const CXXScopeSpec &SS,
+ SourceLocation IdentLoc,
+ IdentifierInfo *TargetName,
+ OverloadedOperatorKind Op,
+ AttributeList *AttrList,
+ bool IsTypeName) {
assert(!SS.isInvalid() && "Invalid CXXScopeSpec.");
assert((TargetName || Op) && "Invalid TargetName.");
assert(IdentLoc.isValid() && "Invalid TargetName location.");
@@ -2118,25 +2118,74 @@ Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S,
if (isUnknownSpecialization(SS)) {
Diag(IdentLoc, diag::err_using_dependent_unsupported);
delete AttrList;
- return DeclPtrTy::make((UsingDecl*)0);
+ return DeclPtrTy();
}
- // Lookup target name.
- LookupResult R = LookupParsedName(S, &SS, Name, LookupOrdinaryName, false);
+ if (SS.isEmpty()) {
+ Diag(IdentLoc, diag::err_using_requires_qualname);
+ return DeclPtrTy();
+ }
+
+ NestedNameSpecifier *NNS =
+ static_cast<NestedNameSpecifier *>(SS.getScopeRep());
- if (NamedDecl *NS = R) {
- if (IsTypeName && !isa<TypeDecl>(NS)) {
- Diag(IdentLoc, diag::err_using_typename_non_type);
+ DeclContext *LookupContext = 0;
+
+ if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
+ // C++0x N2914 [namespace.udecl]p3:
+ // A using-declaration used as a member-declaration shall refer to a member
+ // of a base class of the class being defined, shall refer to a member of an
+ // anonymous union that is a member of a base class of the class being
+ // defined, or shall refer to an enumerator for an enumeration type that is
+ // a member of a base class of the class being defined.
+ const Type *Ty = NNS->getAsType();
+ if (!Ty || !IsDerivedFrom(Context.getTagDeclType(RD), QualType(Ty, 0))) {
+ Diag(SS.getRange().getBegin(),
+ diag::err_using_decl_nested_name_specifier_is_not_a_base_class)
+ << NNS << RD->getDeclName();
+ return DeclPtrTy();
}
- UsingAlias = UsingDecl::Create(Context, CurContext, IdentLoc, SS.getRange(),
- NS->getLocation(), UsingLoc, NS,
- static_cast<NestedNameSpecifier *>(SS.getScopeRep()),
- IsTypeName);
- PushOnScopeChains(UsingAlias, S);
+
+ LookupContext = cast<RecordType>(Ty)->getDecl();
} else {
- Diag(IdentLoc, diag::err_using_requires_qualname) << SS.getRange();
+ // C++0x N2914 [namespace.udecl]p8:
+ // A using-declaration for a class member shall be a member-declaration.
+ if (NNS->getKind() == NestedNameSpecifier::TypeSpec) {
+ Diag(IdentLoc, diag::err_using_decl_refers_to_class_member)
+ << SS.getRange();
+ return DeclPtrTy();
+ }
+
+ // C++0x N2914 [namespace.udecl]p9:
+ // In a using-declaration, a prefix :: refers to the global namespace.
+ if (NNS->getKind() == NestedNameSpecifier::Global)
+ LookupContext = Context.getTranslationUnitDecl();
+ else
+ LookupContext = NNS->getAsNamespace();
+ }
+
+
+ // Lookup target name.
+ LookupResult R = LookupQualifiedName(LookupContext,
+ Name, LookupOrdinaryName);
+
+ if (!R) {
+ Diag(IdentLoc, diag::err_typecheck_no_member) << Name << SS.getRange();
+ return DeclPtrTy();
}
+ NamedDecl *ND = R.getAsDecl();
+
+ if (IsTypeName && !isa<TypeDecl>(ND)) {
+ Diag(IdentLoc, diag::err_using_typename_non_type);
+ return DeclPtrTy();
+ }
+
+ UsingAlias =
+ UsingDecl::Create(Context, CurContext, IdentLoc, SS.getRange(),
+ ND->getLocation(), UsingLoc, ND, NNS, IsTypeName);
+ PushOnScopeChains(UsingAlias, S);
+
// FIXME: We ignore attributes for now.
delete AttrList;
return DeclPtrTy::make(UsingAlias);