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" |
| 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 18 | #include "clang/AST/NestedNameSpecifier.h" |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 19 | #include "clang/Parse/DeclSpec.h" |
| 20 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 7551c18 | 2009-07-22 00:28:09 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 24 | /// \brief Compute the DeclContext that is associated with the given |
| 25 | /// scope specifier. |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 26 | /// |
| 27 | /// \param SS the C++ scope specifier as it appears in the source |
| 28 | /// |
| 29 | /// \param EnteringContext when true, we will be entering the context of |
| 30 | /// this scope specifier, so we can retrieve the declaration context of a |
| 31 | /// class template or class template partial specialization even if it is |
| 32 | /// not the current instantiation. |
| 33 | /// |
| 34 | /// \returns the declaration context represented by the scope specifier @p SS, |
| 35 | /// or NULL if the declaration context cannot be computed (e.g., because it is |
| 36 | /// dependent and not the current instantiation). |
| 37 | DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, |
| 38 | bool EnteringContext) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 39 | if (!SS.isSet() || SS.isInvalid()) |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 40 | return 0; |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 42 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 43 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 44 | if (NNS->isDependent()) { |
| 45 | // If this nested-name-specifier refers to the current |
| 46 | // instantiation, return its DeclContext. |
| 47 | if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) |
| 48 | return Record; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 49 | |
| 50 | if (EnteringContext) { |
| 51 | // We are entering the context of the nested name specifier, so try to |
| 52 | // match the nested name specifier to either a primary class template |
Douglas Gregor | 7551c18 | 2009-07-22 00:28:09 +0000 | [diff] [blame] | 53 | // or a class template partial specialization. |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 54 | if (const TemplateSpecializationType *SpecType |
| 55 | = dyn_cast_or_null<TemplateSpecializationType>(NNS->getAsType())) { |
| 56 | if (ClassTemplateDecl *ClassTemplate |
| 57 | = dyn_cast_or_null<ClassTemplateDecl>( |
| 58 | SpecType->getTemplateName().getAsTemplateDecl())) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 59 | QualType ContextType |
| 60 | = Context.getCanonicalType(QualType(SpecType, 0)); |
| 61 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 62 | // If the type of the nested name specifier is the same as the |
| 63 | // injected class name of the named class template, we're entering |
| 64 | // into that class template definition. |
| 65 | QualType Injected = ClassTemplate->getInjectedClassNameType(Context); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 66 | if (Context.hasSameType(Injected, ContextType)) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 67 | return ClassTemplate->getTemplatedDecl(); |
| 68 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 69 | // If the type of the nested name specifier is the same as the |
| 70 | // type of one of the class template's class template partial |
| 71 | // specializations, we're entering into the definition of that |
| 72 | // class template partial specialization. |
| 73 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 74 | = ClassTemplate->findPartialSpecialization(ContextType)) |
| 75 | return PartialSpec; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
Douglas Gregor | 7551c18 | 2009-07-22 00:28:09 +0000 | [diff] [blame] | 78 | |
| 79 | std::string NNSString; |
| 80 | { |
| 81 | llvm::raw_string_ostream OS(NNSString); |
| 82 | NNS->print(OS, Context.PrintingPolicy); |
| 83 | } |
| 84 | |
| 85 | // FIXME: Allow us to pass a nested-name-specifier to Diag? |
| 86 | Diag(SS.getRange().getBegin(), |
| 87 | diag::err_template_qualified_declarator_no_match) |
| 88 | << NNSString << SS.getRange(); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return 0; |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 92 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 93 | |
| 94 | switch (NNS->getKind()) { |
| 95 | case NestedNameSpecifier::Identifier: |
| 96 | assert(false && "Dependent nested-name-specifier has no DeclContext"); |
| 97 | break; |
| 98 | |
| 99 | case NestedNameSpecifier::Namespace: |
| 100 | return NNS->getAsNamespace(); |
| 101 | |
| 102 | case NestedNameSpecifier::TypeSpec: |
| 103 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 104 | const TagType *Tag = NNS->getAsType()->getAs<TagType>(); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 105 | assert(Tag && "Non-tag type in nested-name-specifier"); |
| 106 | return Tag->getDecl(); |
| 107 | } break; |
| 108 | |
| 109 | case NestedNameSpecifier::Global: |
| 110 | return Context.getTranslationUnitDecl(); |
| 111 | } |
| 112 | |
| 113 | // Required to silence a GCC warning. |
| 114 | return 0; |
Douglas Gregor | ca5e77f | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 117 | bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { |
| 118 | if (!SS.isSet() || SS.isInvalid()) |
| 119 | return false; |
| 120 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 121 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 122 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 123 | return NNS->isDependent(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 126 | // \brief Determine whether this C++ scope specifier refers to an |
| 127 | // unknown specialization, i.e., a dependent type that is not the |
| 128 | // current instantiation. |
| 129 | bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) { |
| 130 | if (!isDependentScopeSpecifier(SS)) |
| 131 | return false; |
| 132 | |
| 133 | NestedNameSpecifier *NNS |
| 134 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 135 | return getCurrentInstantiationOf(NNS) == 0; |
| 136 | } |
| 137 | |
| 138 | /// \brief If the given nested name specifier refers to the current |
| 139 | /// instantiation, return the declaration that corresponds to that |
| 140 | /// current instantiation (C++0x [temp.dep.type]p1). |
| 141 | /// |
| 142 | /// \param NNS a dependent nested name specifier. |
| 143 | CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { |
| 144 | assert(getLangOptions().CPlusPlus && "Only callable in C++"); |
| 145 | assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); |
| 146 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 147 | if (!NNS->getAsType()) |
| 148 | return 0; |
| 149 | |
Douglas Gregor | 1560def | 2009-07-31 18:32:42 +0000 | [diff] [blame] | 150 | QualType T = QualType(NNS->getAsType(), 0); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 151 | // If the nested name specifier does not refer to a type, then it |
| 152 | // does not refer to the current instantiation. |
| 153 | if (T.isNull()) |
| 154 | return 0; |
| 155 | |
| 156 | T = Context.getCanonicalType(T); |
| 157 | |
| 158 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getParent()) { |
| 159 | // If we've hit a namespace or the global scope, then the |
| 160 | // nested-name-specifier can't refer to the current instantiation. |
| 161 | if (Ctx->isFileContext()) |
| 162 | return 0; |
| 163 | |
| 164 | // Skip non-class contexts. |
| 165 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 166 | if (!Record) |
| 167 | continue; |
| 168 | |
| 169 | // If this record type is not dependent, |
| 170 | if (!Record->isDependentType()) |
| 171 | return 0; |
| 172 | |
| 173 | // C++ [temp.dep.type]p1: |
| 174 | // |
| 175 | // In the definition of a class template, a nested class of a |
| 176 | // class template, a member of a class template, or a member of a |
| 177 | // nested class of a class template, a name refers to the current |
| 178 | // instantiation if it is |
| 179 | // -- the injected-class-name (9) of the class template or |
| 180 | // nested class, |
| 181 | // -- in the definition of a primary class template, the name |
| 182 | // of the class template followed by the template argument |
| 183 | // list of the primary template (as described below) |
| 184 | // enclosed in <>, |
| 185 | // -- in the definition of a nested class of a class template, |
| 186 | // the name of the nested class referenced as a member of |
| 187 | // the current instantiation, or |
| 188 | // -- in the definition of a partial specialization, the name |
| 189 | // of the class template followed by the template argument |
| 190 | // list of the partial specialization enclosed in <>. If |
| 191 | // the nth template parameter is a parameter pack, the nth |
| 192 | // template argument is a pack expansion (14.6.3) whose |
Douglas Gregor | c5b8c9b | 2009-07-30 17:50:56 +0000 | [diff] [blame] | 193 | // pattern is the name of the parameter pack. |
| 194 | // (FIXME: parameter packs) |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 195 | // |
| 196 | // All of these options come down to having the |
| 197 | // nested-name-specifier type that is equivalent to the |
| 198 | // injected-class-name of one of the types that is currently in |
| 199 | // our context. |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 200 | if (Context.getCanonicalType(Context.getTypeDeclType(Record)) == T) |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 201 | return Record; |
| 202 | |
| 203 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
| 204 | QualType InjectedClassName |
| 205 | = Template->getInjectedClassNameType(Context); |
| 206 | if (T == Context.getCanonicalType(InjectedClassName)) |
| 207 | return Template->getTemplatedDecl(); |
| 208 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 209 | // FIXME: check for class template partial specializations |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 215 | /// \brief Require that the context specified by SS be complete. |
| 216 | /// |
| 217 | /// If SS refers to a type, this routine checks whether the type is |
| 218 | /// complete enough (or can be made complete enough) for name lookup |
| 219 | /// into the DeclContext. A type that is not yet completed can be |
| 220 | /// considered "complete enough" if it is a class/struct/union/enum |
| 221 | /// that is currently being defined. Or, if we have a type that names |
| 222 | /// a class template specialization that is not a complete type, we |
| 223 | /// will attempt to instantiate that class template. |
| 224 | bool Sema::RequireCompleteDeclContext(const CXXScopeSpec &SS) { |
| 225 | if (!SS.isSet() || SS.isInvalid()) |
| 226 | return false; |
| 227 | |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 228 | DeclContext *DC = computeDeclContext(SS, true); |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 229 | if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { |
| 230 | // If we're currently defining this type, then lookup into the |
| 231 | // type is okay: don't complain that it isn't complete yet. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 232 | const TagType *TagT = Context.getTypeDeclType(Tag)->getAs<TagType>(); |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 233 | if (TagT->isBeingDefined()) |
| 234 | return false; |
| 235 | |
| 236 | // The type must be complete. |
| 237 | return RequireCompleteType(SS.getRange().getBegin(), |
| 238 | Context.getTypeDeclType(Tag), |
| 239 | diag::err_incomplete_nested_name_spec, |
| 240 | SS.getRange()); |
| 241 | } |
| 242 | |
| 243 | return false; |
| 244 | } |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 245 | |
| 246 | /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the |
| 247 | /// global scope ('::'). |
| 248 | Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, |
| 249 | SourceLocation CCLoc) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 250 | return NestedNameSpecifier::GlobalSpecifier(Context); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /// ActOnCXXNestedNameSpecifier - Called during parsing of a |
| 254 | /// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now |
| 255 | /// we want to resolve "bar::". 'SS' is empty or the previously parsed |
| 256 | /// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar', |
| 257 | /// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'. |
| 258 | /// Returns a CXXScopeTy* object representing the C++ scope. |
| 259 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
| 260 | const CXXScopeSpec &SS, |
| 261 | SourceLocation IdLoc, |
| 262 | SourceLocation CCLoc, |
| 263 | IdentifierInfo &II) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 264 | NestedNameSpecifier *Prefix |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 265 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 266 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 267 | // If the prefix already refers to an unknown specialization, there |
| 268 | // is no name lookup to perform. Just build the resulting |
| 269 | // nested-name-specifier. |
| 270 | if (Prefix && isUnknownSpecialization(SS)) |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 271 | return NestedNameSpecifier::Create(Context, Prefix, &II); |
| 272 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 273 | NamedDecl *SD = LookupParsedName(S, &SS, &II, LookupNestedNameSpecifierName); |
| 274 | |
| 275 | if (SD) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 276 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) |
| 277 | return NestedNameSpecifier::Create(Context, Prefix, Namespace); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 278 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 279 | if (TypeDecl *Type = dyn_cast<TypeDecl>(SD)) { |
| 280 | // Determine whether we have a class (or, in C++0x, an enum) or |
| 281 | // a typedef thereof. If so, build the nested-name-specifier. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 282 | QualType T = Context.getTypeDeclType(Type); |
| 283 | bool AcceptableType = false; |
| 284 | if (T->isDependentType()) |
| 285 | AcceptableType = true; |
| 286 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 287 | if (TD->getUnderlyingType()->isRecordType() || |
| 288 | (getLangOptions().CPlusPlus0x && |
| 289 | TD->getUnderlyingType()->isEnumeralType())) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 290 | AcceptableType = true; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 291 | } else if (isa<RecordDecl>(Type) || |
| 292 | (getLangOptions().CPlusPlus0x && isa<EnumDecl>(Type))) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 293 | AcceptableType = true; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 295 | if (AcceptableType) |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 296 | return NestedNameSpecifier::Create(Context, Prefix, false, |
| 297 | T.getTypePtr()); |
| 298 | } |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 299 | |
| 300 | if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) |
| 301 | return NestedNameSpecifier::Create(Context, Prefix, |
| 302 | Alias->getNamespace()); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 303 | |
| 304 | // Fall through to produce an error: we found something that isn't |
| 305 | // a class or a namespace. |
| 306 | } |
| 307 | |
| 308 | // If we didn't find anything during our lookup, try again with |
| 309 | // ordinary name lookup, which can help us produce better error |
| 310 | // messages. |
| 311 | if (!SD) |
| 312 | SD = LookupParsedName(S, &SS, &II, LookupOrdinaryName); |
| 313 | unsigned DiagID; |
| 314 | if (SD) |
| 315 | DiagID = diag::err_expected_class_or_namespace; |
| 316 | else if (SS.isSet()) |
| 317 | DiagID = diag::err_typecheck_no_member; |
| 318 | else |
| 319 | DiagID = diag::err_undeclared_var_use; |
| 320 | |
| 321 | if (SS.isSet()) |
| 322 | Diag(IdLoc, DiagID) << &II << SS.getRange(); |
| 323 | else |
| 324 | Diag(IdLoc, DiagID) << &II; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 329 | Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
| 330 | const CXXScopeSpec &SS, |
| 331 | TypeTy *Ty, |
| 332 | SourceRange TypeRange, |
| 333 | SourceLocation CCLoc) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 334 | NestedNameSpecifier *Prefix |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 335 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 336 | QualType T = GetTypeFromParser(Ty); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 337 | return NestedNameSpecifier::Create(Context, Prefix, /*FIXME:*/false, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 338 | T.getTypePtr()); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 341 | Action::OwningExprResult |
| 342 | Sema::ActOnCXXEnterMemberScope(Scope *S, CXXScopeSpec &SS, ExprArg Base, |
| 343 | tok::TokenKind OpKind) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 344 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 345 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 346 | |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 347 | Expr *BaseExpr = (Expr*)Base.get(); |
| 348 | assert(BaseExpr && "no record expansion"); |
| 349 | |
| 350 | QualType BaseType = BaseExpr->getType(); |
| 351 | // FIXME: handle dependent types |
| 352 | if (BaseType->isDependentType()) |
| 353 | return move(Base); |
| 354 | |
| 355 | // C++ [over.match.oper]p8: |
| 356 | // [...] When operator->returns, the operator-> is applied to the value |
| 357 | // returned, with the original second operand. |
| 358 | if (OpKind == tok::arrow) { |
| 359 | while (BaseType->isRecordType()) { |
| 360 | Base = BuildOverloadedArrowExpr(S, move(Base), BaseExpr->getExprLoc()); |
| 361 | BaseExpr = (Expr*)Base.get(); |
| 362 | if (BaseExpr == NULL) |
| 363 | return ExprError(); |
| 364 | BaseType = BaseExpr->getType(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (BaseType->isPointerType()) |
| 369 | BaseType = BaseType->getPointeeType(); |
| 370 | |
| 371 | // We could end up with various non-record types here, such as extended |
| 372 | // vector types or Objective-C interfaces. Just return early and let |
| 373 | // ActOnMemberReferenceExpr do the work. |
| 374 | if (!BaseType->isRecordType()) |
| 375 | return move(Base); |
| 376 | |
| 377 | SS.setRange(BaseExpr->getSourceRange()); |
| 378 | SS.setScopeRep( |
| 379 | NestedNameSpecifier::Create(Context, 0, false, BaseType.getTypePtr()) |
| 380 | ); |
| 381 | |
| 382 | if (S) |
| 383 | ActOnCXXEnterDeclaratorScope(S,SS); |
| 384 | return move(Base); |
| 385 | } |
| 386 | |
| 387 | void Sema::ActOnCXXExitMemberScope(Scope *S, const CXXScopeSpec &SS) { |
| 388 | if (S && SS.isSet()) |
| 389 | ActOnCXXExitDeclaratorScope(S,SS); |
| 390 | } |
| 391 | |
| 392 | |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 393 | /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global |
| 394 | /// scope or nested-name-specifier) is parsed, part of a declarator-id. |
| 395 | /// After this method is called, according to [C++ 3.4.3p3], names should be |
| 396 | /// looked up in the declarator-id's scope, until the declarator is parsed and |
| 397 | /// ActOnCXXExitDeclaratorScope is called. |
| 398 | /// The 'SS' should be a non-empty valid CXXScopeSpec. |
| 399 | void Sema::ActOnCXXEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 400 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 401 | if (DeclContext *DC = computeDeclContext(SS, true)) |
Douglas Gregor | 3fdbed5 | 2009-07-21 18:59:28 +0000 | [diff] [blame] | 402 | EnterDeclaratorContext(S, DC); |
| 403 | else |
| 404 | const_cast<CXXScopeSpec&>(SS).setScopeRep(0); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously |
| 408 | /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same |
| 409 | /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. |
| 410 | /// Used to indicate that names should revert to being looked up in the |
| 411 | /// defining scope. |
| 412 | void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 413 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 414 | assert((SS.isInvalid() || S->getEntity() == computeDeclContext(SS, true)) && |
Douglas Gregor | 3fdbed5 | 2009-07-21 18:59:28 +0000 | [diff] [blame] | 415 | "Context imbalance!"); |
| 416 | if (!SS.isInvalid()) |
| 417 | ExitDeclaratorContext(S); |
Cedric Venet | 3d65864 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 418 | } |