Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1 | //===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===//
|
| 2 | //
|
| 3 | // The LLVM Compiler Infrastructure
|
| 4 | //
|
| 5 | // This file is distributed under the University of Illinois Open Source
|
| 6 | // License. See LICENSE.TXT for details.
|
| 7 | //
|
| 8 | //===----------------------------------------------------------------------===//
|
| 9 | //
|
| 10 | // This file implements C++ semantic analysis for scope specifiers.
|
| 11 | //
|
| 12 | //===----------------------------------------------------------------------===//
|
| 13 |
|
| 14 | #include "Sema.h"
|
| 15 | #include "clang/AST/ASTContext.h"
|
| 16 | #include "clang/Parse/DeclSpec.h"
|
| 17 | #include "clang/Basic/Diagnostic.h"
|
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h"
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 19 | using namespace clang;
|
| 20 |
|
| 21 |
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 22 | /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
|
| 23 | /// global scope ('::').
|
| 24 | Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
|
| 25 | SourceLocation CCLoc) {
|
| 26 | return cast<DeclContext>(Context.getTranslationUnitDecl());
|
| 27 | }
|
| 28 |
|
| 29 | /// ActOnCXXNestedNameSpecifier - Called during parsing of a
|
| 30 | /// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
|
| 31 | /// we want to resolve "bar::". 'SS' is empty or the previously parsed
|
| 32 | /// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
|
| 33 | /// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
|
| 34 | /// Returns a CXXScopeTy* object representing the C++ scope.
|
| 35 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
|
| 36 | const CXXScopeSpec &SS,
|
| 37 | SourceLocation IdLoc,
|
| 38 | SourceLocation CCLoc,
|
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 39 | IdentifierInfo &II) {
|
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 40 | Decl *SD = LookupParsedName(S, SS, &II,
|
| 41 | LookupCriteria(LookupCriteria::NestedNameSpecifier,
|
| 42 | /*RedeclarationOnly=*/false,
|
| 43 | /*CPlusPlus=*/true));
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 44 |
|
| 45 | if (SD) {
|
| 46 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
|
Douglas Gregor | 3dde5a3 | 2008-12-16 06:37:47 +0000 | [diff] [blame] | 47 | if (const RecordType* Record = TD->getUnderlyingType()->getAsRecordType())
|
| 48 | return cast<DeclContext>(Record->getDecl());
|
| 49 | } else if (isa<NamespaceDecl>(SD) || isa<RecordDecl>(SD)) {
|
| 50 | return cast<DeclContext>(SD);
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 51 | }
|
| 52 |
|
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 53 | // FIXME: C++0x scoped enums
|
| 54 |
|
Douglas Gregor | 3dde5a3 | 2008-12-16 06:37:47 +0000 | [diff] [blame] | 55 | // Fall through to produce an error: we found something that isn't
|
| 56 | // a class or a namespace.
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 57 | }
|
| 58 |
|
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 59 | // If we didn't find anything during our lookup, try again with
|
| 60 | // ordinary name lookup, which can help us produce better error
|
| 61 | // messages.
|
| 62 | if (!SD)
|
| 63 | SD = LookupParsedName(S, SS, &II,
|
| 64 | LookupCriteria(LookupCriteria::Ordinary,
|
| 65 | /*RedeclarationOnly=*/false,
|
| 66 | /*CPlusPlus=*/true));
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 67 | unsigned DiagID;
|
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 68 | if (SD)
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 69 | DiagID = diag::err_expected_class_or_namespace;
|
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 70 | else if (SS.isSet())
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 71 | DiagID = diag::err_typecheck_no_member;
|
| 72 | else
|
| 73 | DiagID = diag::err_undeclared_var_use;
|
| 74 |
|
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 75 | if (SS.isSet())
|
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 76 | Diag(IdLoc, DiagID) << &II << SS.getRange();
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 77 | else
|
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 78 | Diag(IdLoc, DiagID) << &II;
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 79 |
|
| 80 | return 0;
|
| 81 | }
|
| 82 |
|
| 83 | /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
|
| 84 | /// scope or nested-name-specifier) is parsed, part of a declarator-id.
|
| 85 | /// After this method is called, according to [C++ 3.4.3p3], names should be
|
| 86 | /// looked up in the declarator-id's scope, until the declarator is parsed and
|
| 87 | /// ActOnCXXExitDeclaratorScope is called.
|
| 88 | /// The 'SS' should be a non-empty valid CXXScopeSpec.
|
| 89 | void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
|
| 90 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
| 91 | assert(PreDeclaratorDC == 0 && "Previous declarator context not popped?");
|
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 92 | PreDeclaratorDC = static_cast<DeclContext*>(S->getEntity());
|
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 93 | CurContext = static_cast<DeclContext*>(SS.getScopeRep());
|
| 94 | S->setEntity(CurContext);
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 95 | }
|
| 96 |
|
| 97 | /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
|
| 98 | /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
|
| 99 | /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
|
| 100 | /// Used to indicate that names should revert to being looked up in the
|
| 101 | /// defining scope.
|
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 102 | void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 103 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
Douglas Gregor | 0a59acb | 2008-12-16 00:38:16 +0000 | [diff] [blame] | 104 | assert(S->getEntity() == SS.getScopeRep() && "Context imbalance!");
|
| 105 | S->setEntity(PreDeclaratorDC);
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 106 | PreDeclaratorDC = 0;
|
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 107 |
|
| 108 | // Reset CurContext to the nearest enclosing context.
|
| 109 | while (!S->getEntity() && S->getParent())
|
| 110 | S = S->getParent();
|
| 111 | CurContext = static_cast<DeclContext*>(S->getEntity());
|
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 112 | }
|