Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +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" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 19 | #include "clang/AST/NestedNameSpecifier.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 20 | #include "clang/Basic/PartialDiagnostic.h" |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 21 | #include "clang/Parse/DeclSpec.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 7551c18 | 2009-07-22 00:28:09 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 26 | /// \brief Find the current instantiation that associated with the given type. |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 27 | static CXXRecordDecl *getCurrentInstantiationOf(QualType T) { |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 28 | if (T.isNull()) |
| 29 | return 0; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 30 | |
| 31 | const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); |
| 32 | if (isa<RecordType>(Ty)) |
| 33 | return cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); |
| 34 | else if (isa<InjectedClassNameType>(Ty)) |
| 35 | return cast<InjectedClassNameType>(Ty)->getDecl(); |
| 36 | else |
| 37 | return 0; |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 40 | /// \brief Compute the DeclContext that is associated with the given type. |
| 41 | /// |
| 42 | /// \param T the type for which we are attempting to find a DeclContext. |
| 43 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | /// \returns the declaration context represented by the type T, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 45 | /// or NULL if the declaration context cannot be computed (e.g., because it is |
| 46 | /// dependent and not the current instantiation). |
| 47 | DeclContext *Sema::computeDeclContext(QualType T) { |
| 48 | if (const TagType *Tag = T->getAs<TagType>()) |
| 49 | return Tag->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 51 | return ::getCurrentInstantiationOf(T); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 54 | /// \brief Compute the DeclContext that is associated with the given |
| 55 | /// scope specifier. |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 56 | /// |
| 57 | /// \param SS the C++ scope specifier as it appears in the source |
| 58 | /// |
| 59 | /// \param EnteringContext when true, we will be entering the context of |
| 60 | /// this scope specifier, so we can retrieve the declaration context of a |
| 61 | /// class template or class template partial specialization even if it is |
| 62 | /// not the current instantiation. |
| 63 | /// |
| 64 | /// \returns the declaration context represented by the scope specifier @p SS, |
| 65 | /// or NULL if the declaration context cannot be computed (e.g., because it is |
| 66 | /// dependent and not the current instantiation). |
| 67 | DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, |
| 68 | bool EnteringContext) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 69 | if (!SS.isSet() || SS.isInvalid()) |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 70 | return 0; |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 71 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 73 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 74 | if (NNS->isDependent()) { |
| 75 | // If this nested-name-specifier refers to the current |
| 76 | // instantiation, return its DeclContext. |
| 77 | if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) |
| 78 | return Record; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 80 | if (EnteringContext) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 81 | const Type *NNSType = NNS->getAsType(); |
| 82 | if (!NNSType) { |
| 83 | // do nothing, fall out |
| 84 | } else if (const TemplateSpecializationType *SpecType |
| 85 | = NNSType->getAs<TemplateSpecializationType>()) { |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 86 | // We are entering the context of the nested name specifier, so try to |
| 87 | // match the nested name specifier to either a primary class template |
| 88 | // or a class template partial specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 90 | = dyn_cast_or_null<ClassTemplateDecl>( |
| 91 | SpecType->getTemplateName().getAsTemplateDecl())) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 92 | QualType ContextType |
| 93 | = Context.getCanonicalType(QualType(SpecType, 0)); |
| 94 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 95 | // If the type of the nested name specifier is the same as the |
| 96 | // injected class name of the named class template, we're entering |
| 97 | // into that class template definition. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 98 | QualType Injected |
| 99 | = ClassTemplate->getInjectedClassNameSpecialization(Context); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 100 | if (Context.hasSameType(Injected, ContextType)) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 101 | return ClassTemplate->getTemplatedDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 103 | // If the type of the nested name specifier is the same as the |
| 104 | // type of one of the class template's class template partial |
| 105 | // specializations, we're entering into the definition of that |
| 106 | // class template partial specialization. |
| 107 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 108 | = ClassTemplate->findPartialSpecialization(ContextType)) |
| 109 | return PartialSpec; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 110 | } |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 111 | } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) { |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 112 | // The nested name specifier refers to a member of a class template. |
| 113 | return RecordT->getDecl(); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 117 | return 0; |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 118 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 119 | |
| 120 | switch (NNS->getKind()) { |
| 121 | case NestedNameSpecifier::Identifier: |
| 122 | assert(false && "Dependent nested-name-specifier has no DeclContext"); |
| 123 | break; |
| 124 | |
| 125 | case NestedNameSpecifier::Namespace: |
| 126 | return NNS->getAsNamespace(); |
| 127 | |
| 128 | case NestedNameSpecifier::TypeSpec: |
| 129 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 130 | const TagType *Tag = NNS->getAsType()->getAs<TagType>(); |
| 131 | assert(Tag && "Non-tag type in nested-name-specifier"); |
| 132 | return Tag->getDecl(); |
| 133 | } break; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 134 | |
| 135 | case NestedNameSpecifier::Global: |
| 136 | return Context.getTranslationUnitDecl(); |
| 137 | } |
| 138 | |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 139 | // Required to silence a GCC warning. |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 140 | return 0; |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 143 | bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { |
| 144 | if (!SS.isSet() || SS.isInvalid()) |
| 145 | return false; |
| 146 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 148 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 149 | return NNS->isDependent(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 152 | // \brief Determine whether this C++ scope specifier refers to an |
| 153 | // unknown specialization, i.e., a dependent type that is not the |
| 154 | // current instantiation. |
| 155 | bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) { |
| 156 | if (!isDependentScopeSpecifier(SS)) |
| 157 | return false; |
| 158 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | NestedNameSpecifier *NNS |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 160 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 161 | return getCurrentInstantiationOf(NNS) == 0; |
| 162 | } |
| 163 | |
| 164 | /// \brief If the given nested name specifier refers to the current |
| 165 | /// instantiation, return the declaration that corresponds to that |
| 166 | /// current instantiation (C++0x [temp.dep.type]p1). |
| 167 | /// |
| 168 | /// \param NNS a dependent nested name specifier. |
| 169 | CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { |
| 170 | assert(getLangOptions().CPlusPlus && "Only callable in C++"); |
| 171 | assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); |
| 172 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 173 | if (!NNS->getAsType()) |
| 174 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | 1560def | 2009-07-31 18:32:42 +0000 | [diff] [blame] | 176 | QualType T = QualType(NNS->getAsType(), 0); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 177 | return ::getCurrentInstantiationOf(T); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 180 | /// \brief Require that the context specified by SS be complete. |
| 181 | /// |
| 182 | /// If SS refers to a type, this routine checks whether the type is |
| 183 | /// complete enough (or can be made complete enough) for name lookup |
| 184 | /// into the DeclContext. A type that is not yet completed can be |
| 185 | /// considered "complete enough" if it is a class/struct/union/enum |
| 186 | /// that is currently being defined. Or, if we have a type that names |
| 187 | /// a class template specialization that is not a complete type, we |
| 188 | /// will attempt to instantiate that class template. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame^] | 189 | bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, |
| 190 | DeclContext *DC) { |
| 191 | assert(DC != 0 && "given null context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 193 | if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { |
Douglas Gregor | a4e8c2a | 2010-02-05 04:39:02 +0000 | [diff] [blame] | 194 | // If this is a dependent type, then we consider it complete. |
| 195 | if (Tag->isDependentContext()) |
| 196 | return false; |
| 197 | |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 198 | // If we're currently defining this type, then lookup into the |
| 199 | // type is okay: don't complain that it isn't complete yet. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 200 | const TagType *TagT = Context.getTypeDeclType(Tag)->getAs<TagType>(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 201 | if (TagT && TagT->isBeingDefined()) |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 202 | return false; |
| 203 | |
| 204 | // The type must be complete. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 205 | if (RequireCompleteType(SS.getRange().getBegin(), |
| 206 | Context.getTypeDeclType(Tag), |
| 207 | PDiag(diag::err_incomplete_nested_name_spec) |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame^] | 208 | << SS.getRange())) { |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 209 | SS.setScopeRep(0); // Mark the ScopeSpec invalid. |
| 210 | return true; |
| 211 | } |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | return false; |
| 215 | } |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 216 | |
| 217 | /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the |
| 218 | /// global scope ('::'). |
| 219 | Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, |
| 220 | SourceLocation CCLoc) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 221 | return NestedNameSpecifier::GlobalSpecifier(Context); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 224 | /// \brief Determines whether the given declaration is an valid acceptable |
| 225 | /// result for name lookup of a nested-name-specifier. |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 226 | bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 227 | if (!SD) |
| 228 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 230 | // Namespace and namespace aliases are fine. |
| 231 | if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD)) |
| 232 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 234 | if (!isa<TypeDecl>(SD)) |
| 235 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 237 | // Determine whether we have a class (or, in C++0x, an enum) or |
| 238 | // a typedef thereof. If so, build the nested-name-specifier. |
| 239 | QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); |
| 240 | if (T->isDependentType()) |
| 241 | return true; |
| 242 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
| 243 | if (TD->getUnderlyingType()->isRecordType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | (Context.getLangOptions().CPlusPlus0x && |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 245 | TD->getUnderlyingType()->isEnumeralType())) |
| 246 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | } else if (isa<RecordDecl>(SD) || |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 248 | (Context.getLangOptions().CPlusPlus0x && isa<EnumDecl>(SD))) |
| 249 | return true; |
| 250 | |
| 251 | return false; |
| 252 | } |
| 253 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 254 | /// \brief If the given nested-name-specifier begins with a bare identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | /// (e.g., Base::), perform name lookup for that identifier as a |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 256 | /// nested-name-specifier within the given scope, and return the result of that |
| 257 | /// name lookup. |
| 258 | NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) { |
| 259 | if (!S || !NNS) |
| 260 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 262 | while (NNS->getPrefix()) |
| 263 | NNS = NNS->getPrefix(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 265 | if (NNS->getKind() != NestedNameSpecifier::Identifier) |
| 266 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 268 | LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(), |
| 269 | LookupNestedNameSpecifierName); |
| 270 | LookupName(Found, S); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 271 | assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet"); |
| 272 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 273 | if (!Found.isSingleResult()) |
| 274 | return 0; |
| 275 | |
| 276 | NamedDecl *Result = Found.getFoundDecl(); |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 277 | if (isAcceptableNestedNameSpecifier(Result)) |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 278 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 283 | bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 284 | SourceLocation IdLoc, |
| 285 | IdentifierInfo &II, |
| 286 | TypeTy *ObjectTypePtr) { |
| 287 | QualType ObjectType = GetTypeFromParser(ObjectTypePtr); |
| 288 | LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); |
| 289 | |
| 290 | // Determine where to perform name lookup |
| 291 | DeclContext *LookupCtx = 0; |
| 292 | bool isDependent = false; |
| 293 | if (!ObjectType.isNull()) { |
| 294 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 295 | // x->B::f, and we are looking into the type of the object. |
| 296 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 297 | LookupCtx = computeDeclContext(ObjectType); |
| 298 | isDependent = ObjectType->isDependentType(); |
| 299 | } else if (SS.isSet()) { |
| 300 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 301 | // so long into the context associated with the prior nested-name-specifier. |
| 302 | LookupCtx = computeDeclContext(SS, false); |
| 303 | isDependent = isDependentScopeSpecifier(SS); |
| 304 | Found.setContextRange(SS.getRange()); |
| 305 | } |
| 306 | |
| 307 | if (LookupCtx) { |
| 308 | // Perform "qualified" name lookup into the declaration context we |
| 309 | // computed, which is either the type of the base of a member access |
| 310 | // expression or the declaration context associated with a prior |
| 311 | // nested-name-specifier. |
| 312 | |
| 313 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame^] | 314 | if (!LookupCtx->isDependentContext() && |
| 315 | RequireCompleteDeclContext(SS, LookupCtx)) |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 316 | return false; |
| 317 | |
| 318 | LookupQualifiedName(Found, LookupCtx); |
| 319 | } else if (isDependent) { |
| 320 | return false; |
| 321 | } else { |
| 322 | LookupName(Found, S); |
| 323 | } |
| 324 | Found.suppressDiagnostics(); |
| 325 | |
| 326 | if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) |
| 327 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 328 | |
| 329 | return false; |
| 330 | } |
| 331 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 332 | /// \brief Build a new nested-name-specifier for "identifier::", as described |
| 333 | /// by ActOnCXXNestedNameSpecifier. |
| 334 | /// |
| 335 | /// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in |
| 336 | /// that it contains an extra parameter \p ScopeLookupResult, which provides |
| 337 | /// the result of name lookup within the scope of the nested-name-specifier |
Douglas Gregor | a6e5199 | 2009-12-30 16:01:52 +0000 | [diff] [blame] | 338 | /// that was computed at template definition time. |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 339 | /// |
| 340 | /// If ErrorRecoveryLookup is true, then this call is used to improve error |
| 341 | /// recovery. This means that it should not emit diagnostics, it should |
| 342 | /// just return null on failure. It also means it should only return a valid |
| 343 | /// scope if it *knows* that the result is correct. It should not return in a |
| 344 | /// dependent context, for example. |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 345 | Sema::CXXScopeTy *Sema::BuildCXXNestedNameSpecifier(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 346 | CXXScopeSpec &SS, |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 347 | SourceLocation IdLoc, |
| 348 | SourceLocation CCLoc, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 349 | IdentifierInfo &II, |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 350 | QualType ObjectType, |
| 351 | NamedDecl *ScopeLookupResult, |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 352 | bool EnteringContext, |
| 353 | bool ErrorRecoveryLookup) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | NestedNameSpecifier *Prefix |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 355 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 357 | LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); |
| 358 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 359 | // Determine where to perform name lookup |
| 360 | DeclContext *LookupCtx = 0; |
| 361 | bool isDependent = false; |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 362 | if (!ObjectType.isNull()) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 363 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 364 | // x->B::f, and we are looking into the type of the object. |
| 365 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 366 | LookupCtx = computeDeclContext(ObjectType); |
| 367 | isDependent = ObjectType->isDependentType(); |
| 368 | } else if (SS.isSet()) { |
| 369 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 370 | // so long into the context associated with the prior nested-name-specifier. |
| 371 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 372 | isDependent = isDependentScopeSpecifier(SS); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 373 | Found.setContextRange(SS.getRange()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 374 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 377 | bool ObjectTypeSearchedInScope = false; |
| 378 | if (LookupCtx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | // Perform "qualified" name lookup into the declaration context we |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 380 | // computed, which is either the type of the base of a member access |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | // expression or the declaration context associated with a prior |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 382 | // nested-name-specifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 384 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame^] | 385 | if (!LookupCtx->isDependentContext() && |
| 386 | RequireCompleteDeclContext(SS, LookupCtx)) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 387 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 389 | LookupQualifiedName(Found, LookupCtx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 391 | if (!ObjectType.isNull() && Found.empty()) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 392 | // C++ [basic.lookup.classref]p4: |
| 393 | // If the id-expression in a class member access is a qualified-id of |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | // the form |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 395 | // |
| 396 | // class-name-or-namespace-name::... |
| 397 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | // the class-name-or-namespace-name following the . or -> operator is |
| 399 | // looked up both in the context of the entire postfix-expression and in |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 400 | // the scope of the class of the object expression. If the name is found |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | // only in the scope of the class of the object expression, the name |
| 402 | // shall refer to a class-name. If the name is found only in the |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 403 | // context of the entire postfix-expression, the name shall refer to a |
| 404 | // class-name or namespace-name. [...] |
| 405 | // |
| 406 | // Qualified name lookup into a class will not find a namespace-name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | // so we do not need to diagnoste that case specifically. However, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 408 | // this qualified name lookup may find nothing. In that case, perform |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | // unqualified name lookup in the given scope (if available) or |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 410 | // reconstruct the result from when name lookup was performed at template |
| 411 | // definition time. |
| 412 | if (S) |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 413 | LookupName(Found, S); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 414 | else if (ScopeLookupResult) |
| 415 | Found.addDecl(ScopeLookupResult); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 417 | ObjectTypeSearchedInScope = true; |
| 418 | } |
| 419 | } else if (isDependent) { |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 420 | // Don't speculate if we're just trying to improve error recovery. |
| 421 | if (ErrorRecoveryLookup) |
| 422 | return 0; |
| 423 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 424 | // We were not able to compute the declaration context for a dependent |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | // base object type or prior nested-name-specifier, so this |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 426 | // nested-name-specifier refers to an unknown specialization. Just build |
| 427 | // a dependent nested-name-specifier. |
Douglas Gregor | 2700dcd | 2009-09-02 23:58:38 +0000 | [diff] [blame] | 428 | if (!Prefix) |
| 429 | return NestedNameSpecifier::Create(Context, &II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 431 | return NestedNameSpecifier::Create(Context, Prefix, &II); |
| 432 | } else { |
| 433 | // Perform unqualified name lookup in the current scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 434 | LookupName(Found, S); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 435 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 437 | // FIXME: Deal with ambiguities cleanly. |
Douglas Gregor | 175a656 | 2009-12-31 08:26:35 +0000 | [diff] [blame] | 438 | |
| 439 | if (Found.empty() && !ErrorRecoveryLookup) { |
| 440 | // We haven't found anything, and we're not recovering from a |
| 441 | // different kind of error, so look for typos. |
| 442 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 443 | if (CorrectTypo(Found, S, &SS, LookupCtx, EnteringContext, |
| 444 | CTC_NoKeywords) && |
Douglas Gregor | 175a656 | 2009-12-31 08:26:35 +0000 | [diff] [blame] | 445 | Found.isSingleResult() && |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 446 | isAcceptableNestedNameSpecifier(Found.getAsSingle<NamedDecl>())) { |
Douglas Gregor | 175a656 | 2009-12-31 08:26:35 +0000 | [diff] [blame] | 447 | if (LookupCtx) |
| 448 | Diag(Found.getNameLoc(), diag::err_no_member_suggest) |
| 449 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 450 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
| 451 | Found.getLookupName().getAsString()); |
Douglas Gregor | 175a656 | 2009-12-31 08:26:35 +0000 | [diff] [blame] | 452 | else |
| 453 | Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest) |
| 454 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 455 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
| 456 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 457 | |
| 458 | if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) |
| 459 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 460 | << ND->getDeclName(); |
Douglas Gregor | 175a656 | 2009-12-31 08:26:35 +0000 | [diff] [blame] | 461 | } else |
| 462 | Found.clear(); |
| 463 | } |
| 464 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 465 | NamedDecl *SD = Found.getAsSingle<NamedDecl>(); |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 466 | if (isAcceptableNestedNameSpecifier(SD)) { |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 467 | if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 468 | // C++ [basic.lookup.classref]p4: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | // [...] If the name is found in both contexts, the |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 470 | // class-name-or-namespace-name shall refer to the same entity. |
| 471 | // |
| 472 | // We already found the name in the scope of the object. Now, look |
| 473 | // into the current scope (the scope of the postfix-expression) to |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 474 | // see if we can find the same name there. As above, if there is no |
| 475 | // scope, reconstruct the result from the template instantiation itself. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 476 | NamedDecl *OuterDecl; |
| 477 | if (S) { |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 478 | LookupResult FoundOuter(*this, &II, IdLoc, LookupNestedNameSpecifierName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 479 | LookupName(FoundOuter, S); |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 480 | OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 481 | } else |
| 482 | OuterDecl = ScopeLookupResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 484 | if (isAcceptableNestedNameSpecifier(OuterDecl) && |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 485 | OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && |
| 486 | (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) || |
| 487 | !Context.hasSameType( |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 488 | Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)), |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 489 | Context.getTypeDeclType(cast<TypeDecl>(SD))))) { |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 490 | if (ErrorRecoveryLookup) |
| 491 | return 0; |
| 492 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 493 | Diag(IdLoc, diag::err_nested_name_member_ref_lookup_ambiguous) |
| 494 | << &II; |
| 495 | Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type) |
| 496 | << ObjectType; |
| 497 | Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 499 | // Fall through so that we'll pick the name we found in the object |
| 500 | // type, since that's probably what the user wanted anyway. |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 501 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 502 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 504 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) |
| 505 | return NestedNameSpecifier::Create(Context, Prefix, Namespace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 507 | // FIXME: It would be nice to maintain the namespace alias name, then |
| 508 | // see through that alias when resolving the nested-name-specifier down to |
| 509 | // a declaration context. |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 510 | if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) |
| 511 | return NestedNameSpecifier::Create(Context, Prefix, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 513 | Alias->getNamespace()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 515 | QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); |
| 516 | return NestedNameSpecifier::Create(Context, Prefix, false, |
| 517 | T.getTypePtr()); |
| 518 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 520 | // Otherwise, we have an error case. If we don't want diagnostics, just |
| 521 | // return an error now. |
| 522 | if (ErrorRecoveryLookup) |
| 523 | return 0; |
| 524 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 525 | // If we didn't find anything during our lookup, try again with |
| 526 | // ordinary name lookup, which can help us produce better error |
| 527 | // messages. |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 528 | if (Found.empty()) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 529 | Found.clear(LookupOrdinaryName); |
| 530 | LookupName(Found, S); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 533 | unsigned DiagID; |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 534 | if (!Found.empty()) |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 535 | DiagID = diag::err_expected_class_or_namespace; |
Anders Carlsson | a31d5f7 | 2009-08-30 07:09:50 +0000 | [diff] [blame] | 536 | else if (SS.isSet()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 537 | Diag(IdLoc, diag::err_no_member) << &II << LookupCtx << SS.getRange(); |
Anders Carlsson | a31d5f7 | 2009-08-30 07:09:50 +0000 | [diff] [blame] | 538 | return 0; |
| 539 | } else |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 540 | DiagID = diag::err_undeclared_var_use; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 542 | if (SS.isSet()) |
| 543 | Diag(IdLoc, DiagID) << &II << SS.getRange(); |
| 544 | else |
| 545 | Diag(IdLoc, DiagID) << &II; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 550 | /// ActOnCXXNestedNameSpecifier - Called during parsing of a |
| 551 | /// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now |
| 552 | /// we want to resolve "bar::". 'SS' is empty or the previously parsed |
| 553 | /// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar', |
| 554 | /// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'. |
| 555 | /// Returns a CXXScopeTy* object representing the C++ scope. |
| 556 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 557 | CXXScopeSpec &SS, |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 558 | SourceLocation IdLoc, |
| 559 | SourceLocation CCLoc, |
| 560 | IdentifierInfo &II, |
| 561 | TypeTy *ObjectTypePtr, |
| 562 | bool EnteringContext) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | return BuildCXXNestedNameSpecifier(S, SS, IdLoc, CCLoc, II, |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 564 | QualType::getFromOpaquePtr(ObjectTypePtr), |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 565 | /*ScopeLookupResult=*/0, EnteringContext, |
| 566 | false); |
| 567 | } |
| 568 | |
| 569 | /// IsInvalidUnlessNestedName - This method is used for error recovery |
| 570 | /// purposes to determine whether the specified identifier is only valid as |
| 571 | /// a nested name specifier, for example a namespace name. It is |
| 572 | /// conservatively correct to always return false from this method. |
| 573 | /// |
| 574 | /// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 575 | bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 576 | IdentifierInfo &II, TypeTy *ObjectType, |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 577 | bool EnteringContext) { |
| 578 | return BuildCXXNestedNameSpecifier(S, SS, SourceLocation(), SourceLocation(), |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 579 | II, QualType::getFromOpaquePtr(ObjectType), |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 580 | /*ScopeLookupResult=*/0, EnteringContext, |
| 581 | true); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 584 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
| 585 | const CXXScopeSpec &SS, |
| 586 | TypeTy *Ty, |
| 587 | SourceRange TypeRange, |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 588 | SourceLocation CCLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | NestedNameSpecifier *Prefix |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 590 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 591 | QualType T = GetTypeFromParser(Ty); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 592 | return NestedNameSpecifier::Create(Context, Prefix, /*FIXME:*/false, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 593 | T.getTypePtr()); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 594 | } |
| 595 | |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 596 | bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 597 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
| 598 | |
| 599 | NestedNameSpecifier *Qualifier = |
| 600 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 601 | |
| 602 | // There are only two places a well-formed program may qualify a |
| 603 | // declarator: first, when defining a namespace or class member |
| 604 | // out-of-line, and second, when naming an explicitly-qualified |
| 605 | // friend function. The latter case is governed by |
| 606 | // C++03 [basic.lookup.unqual]p10: |
| 607 | // In a friend declaration naming a member function, a name used |
| 608 | // in the function declarator and not part of a template-argument |
| 609 | // in a template-id is first looked up in the scope of the member |
| 610 | // function's class. If it is not found, or if the name is part of |
| 611 | // a template-argument in a template-id, the look up is as |
| 612 | // described for unqualified names in the definition of the class |
| 613 | // granting friendship. |
| 614 | // i.e. we don't push a scope unless it's a class member. |
| 615 | |
| 616 | switch (Qualifier->getKind()) { |
| 617 | case NestedNameSpecifier::Global: |
| 618 | case NestedNameSpecifier::Namespace: |
| 619 | // These are always namespace scopes. We never want to enter a |
| 620 | // namespace scope from anything but a file context. |
| 621 | return CurContext->getLookupContext()->isFileContext(); |
| 622 | |
| 623 | case NestedNameSpecifier::Identifier: |
| 624 | case NestedNameSpecifier::TypeSpec: |
| 625 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 626 | // These are never namespace scopes. |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | // Silence bogus warning. |
| 631 | return false; |
| 632 | } |
| 633 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 634 | /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global |
| 635 | /// scope or nested-name-specifier) is parsed, part of a declarator-id. |
| 636 | /// After this method is called, according to [C++ 3.4.3p3], names should be |
| 637 | /// looked up in the declarator-id's scope, until the declarator is parsed and |
| 638 | /// ActOnCXXExitDeclaratorScope is called. |
| 639 | /// The 'SS' should be a non-empty valid CXXScopeSpec. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 640 | bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 641 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 642 | |
| 643 | if (SS.isInvalid()) return true; |
| 644 | |
| 645 | DeclContext *DC = computeDeclContext(SS, true); |
| 646 | if (!DC) return true; |
| 647 | |
| 648 | // Before we enter a declarator's context, we need to make sure that |
| 649 | // it is a complete declaration context. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame^] | 650 | if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 651 | return true; |
| 652 | |
| 653 | EnterDeclaratorContext(S, DC); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 654 | |
| 655 | // Rebuild the nested name specifier for the new scope. |
| 656 | if (DC->isDependentContext()) |
| 657 | RebuildNestedNameSpecifierInCurrentInstantiation(SS); |
| 658 | |
Douglas Gregor | 7dfd0fb | 2009-09-24 23:39:01 +0000 | [diff] [blame] | 659 | return false; |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously |
| 663 | /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same |
| 664 | /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. |
| 665 | /// Used to indicate that names should revert to being looked up in the |
| 666 | /// defining scope. |
| 667 | void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 668 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 669 | if (SS.isInvalid()) |
| 670 | return; |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 671 | assert(!SS.isInvalid() && computeDeclContext(SS, true) && |
| 672 | "exiting declarator scope we never really entered"); |
| 673 | ExitDeclaratorContext(S); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 674 | } |