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