2008-11-09 01:17:31 +08:00
|
|
|
//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements C++ semantic analysis for scope specifiers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/Parse/DeclSpec.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-12-12 04:41:00 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-11-09 01:17:31 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
Decl *LookupNestedName(DeclContext *LookupCtx, bool LookInParentCtx,
|
2008-12-12 00:49:14 +08:00
|
|
|
DeclarationName Name, bool &IdIsUndeclared,
|
2008-12-12 04:41:00 +08:00
|
|
|
ASTContext &Context) {
|
2008-12-12 00:49:14 +08:00
|
|
|
if (LookupCtx && !LookInParentCtx) {
|
|
|
|
IdIsUndeclared = true;
|
2008-12-12 04:41:00 +08:00
|
|
|
DeclContext::lookup_const_iterator I, E;
|
2009-01-09 01:28:14 +08:00
|
|
|
for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I) {
|
2008-12-12 00:49:14 +08:00
|
|
|
IdIsUndeclared = false;
|
Unify the code for defining tags in C and C++, so that we always
introduce a Scope for the body of a tag. This reduces the number of
semantic differences between C and C++ structs and unions, and will
help with other features (e.g., anonymous unions) in C. Some important
points:
- Fields are now in the "member" namespace (IDNS_Member), to keep
them separate from tags and ordinary names in C. See the new test
in Sema/member-reference.c for an example of why this matters. In
C++, ordinary and member name lookup will find members in both the
ordinary and member namespace, so the difference between
IDNS_Member and IDNS_Ordinary is erased by Sema::LookupDecl (but
only in C++!).
- We always introduce a Scope and push a DeclContext when we're
defining a tag, in both C and C++. Previously, we had different
actions and different Scope/CurContext behavior for enums, C
structs/unions, and C++ structs/unions/classes. Now, it's one pair
of actions. (Yay!)
There's still some fuzziness in the handling of struct/union/enum
definitions within other struct/union/enum definitions in C. We'll
need to do some more cleanup to eliminate some reliance on CurContext
before we can solve this issue for real. What we want is for something
like this:
struct X {
struct T { int x; } t;
};
to introduce T into translation unit scope (placing it at the
appropriate point in the IdentifierResolver chain, too), but it should
still have struct X as its lexical declaration
context. PushOnScopeChains isn't smart enough to do that yet, though,
so there's a FIXME test in nested-redef.c
llvm-svn: 61940
2009-01-09 04:45:30 +08:00
|
|
|
if (((*I)->isInIdentifierNamespace(Decl::IDNS_Tag)) ||
|
2008-12-16 14:37:47 +08:00
|
|
|
isa<TypedefDecl>(*I))
|
2008-12-12 04:41:00 +08:00
|
|
|
return *I;
|
2008-12-12 00:49:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Decouple this from the IdentifierResolver so that we can
|
|
|
|
// deal with lookups into the semantic parent contexts that aren't
|
|
|
|
// lexical parent contexts.
|
|
|
|
|
2008-11-09 01:17:31 +08:00
|
|
|
IdentifierResolver::iterator
|
2008-11-18 04:34:05 +08:00
|
|
|
I = IdentifierResolver::begin(Name, LookupCtx, LookInParentCtx),
|
2008-11-09 01:17:31 +08:00
|
|
|
E = IdentifierResolver::end();
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
IdIsUndeclared = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
IdIsUndeclared = false;
|
|
|
|
|
|
|
|
// C++ 3.4.3p1 :
|
|
|
|
// During the lookup for a name preceding the :: scope resolution operator,
|
|
|
|
// object, function, and enumerator names are ignored. If the name found is
|
|
|
|
// not a class-name or namespace-name, the program is ill-formed.
|
|
|
|
|
|
|
|
for (; I != E; ++I) {
|
2008-12-16 14:37:47 +08:00
|
|
|
if (isa<TypedefDecl>(*I)) {
|
|
|
|
break;
|
2008-11-09 01:17:31 +08:00
|
|
|
}
|
2008-12-16 14:37:47 +08:00
|
|
|
if (((*I)->getIdentifierNamespace() & Decl::IDNS_Tag))
|
2008-11-09 01:17:31 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (I != E ? *I : 0);
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
|
|
|
|
/// global scope ('::').
|
|
|
|
Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
|
|
|
|
SourceLocation CCLoc) {
|
|
|
|
return cast<DeclContext>(Context.getTranslationUnitDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ActOnCXXNestedNameSpecifier - Called during parsing of a
|
|
|
|
/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
|
|
|
|
/// we want to resolve "bar::". 'SS' is empty or the previously parsed
|
|
|
|
/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
|
|
|
|
/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
|
|
|
|
/// Returns a CXXScopeTy* object representing the C++ scope.
|
|
|
|
Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
|
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
SourceLocation IdLoc,
|
|
|
|
SourceLocation CCLoc,
|
2008-11-18 04:34:05 +08:00
|
|
|
IdentifierInfo &II) {
|
2008-11-09 01:17:31 +08:00
|
|
|
DeclContext *DC = static_cast<DeclContext*>(SS.getScopeRep());
|
|
|
|
Decl *SD;
|
|
|
|
bool IdIsUndeclared;
|
|
|
|
|
|
|
|
if (DC)
|
2008-12-12 00:49:14 +08:00
|
|
|
SD = LookupNestedName(DC, false/*LookInParentCtx*/, &II, IdIsUndeclared,
|
|
|
|
Context);
|
2008-11-09 01:17:31 +08:00
|
|
|
else
|
2008-11-18 04:34:05 +08:00
|
|
|
SD = LookupNestedName(CurContext, true/*LookInParent*/, &II,
|
2008-12-12 00:49:14 +08:00
|
|
|
IdIsUndeclared, Context);
|
2008-11-09 01:17:31 +08:00
|
|
|
|
|
|
|
if (SD) {
|
|
|
|
if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
|
2008-12-16 14:37:47 +08:00
|
|
|
if (const RecordType* Record = TD->getUnderlyingType()->getAsRecordType())
|
|
|
|
return cast<DeclContext>(Record->getDecl());
|
|
|
|
} else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) {
|
|
|
|
return cast<DeclContext>(SD);
|
2008-11-09 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
2008-12-16 14:37:47 +08:00
|
|
|
// Fall through to produce an error: we found something that isn't
|
|
|
|
// a class or a namespace.
|
2008-11-09 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DiagID;
|
|
|
|
if (!IdIsUndeclared)
|
|
|
|
DiagID = diag::err_expected_class_or_namespace;
|
|
|
|
else if (DC)
|
|
|
|
DiagID = diag::err_typecheck_no_member;
|
|
|
|
else
|
|
|
|
DiagID = diag::err_undeclared_var_use;
|
|
|
|
|
|
|
|
if (DC)
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(IdLoc, DiagID) << &II << SS.getRange();
|
2008-11-09 01:17:31 +08:00
|
|
|
else
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(IdLoc, DiagID) << &II;
|
2008-11-09 01:17:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
|
|
|
|
/// scope or nested-name-specifier) is parsed, part of a declarator-id.
|
|
|
|
/// After this method is called, according to [C++ 3.4.3p3], names should be
|
|
|
|
/// looked up in the declarator-id's scope, until the declarator is parsed and
|
|
|
|
/// ActOnCXXExitDeclaratorScope is called.
|
|
|
|
/// The 'SS' should be a non-empty valid CXXScopeSpec.
|
|
|
|
void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
|
|
|
|
assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
|
|
|
assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
|
2008-12-16 08:38:16 +08:00
|
|
|
PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
|
2008-12-17 05:30:33 +08:00
|
|
|
CurContext = static_cast<DeclContext*>(SS.getScopeRep());
|
|
|
|
S->setEntity(CurContext);
|
2008-11-09 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
|
|
|
|
/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
|
|
|
|
/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
|
|
|
|
/// Used to indicate that names should revert to being looked up in the
|
|
|
|
/// defining scope.
|
2008-12-16 08:38:16 +08:00
|
|
|
void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
|
2008-11-09 01:17:31 +08:00
|
|
|
assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
2008-12-16 08:38:16 +08:00
|
|
|
assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!");
|
|
|
|
S->setEntity(PreDeclaratorDC);
|
2008-11-09 01:17:31 +08:00
|
|
|
PreDeclaratorDC = 0;
|
2008-12-17 05:30:33 +08:00
|
|
|
|
|
|
|
// Reset CurContext to the nearest enclosing context.
|
|
|
|
while (!S->getEntity() && S->getParent())
|
|
|
|
S = S->getParent();
|
|
|
|
CurContext = static_cast<DeclContext*>(S->getEntity());
|
2008-11-09 01:17:31 +08:00
|
|
|
}
|