Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1 | //===--- SemaExprMember.cpp - Semantic Analysis for Expressions -----------===// |
| 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 semantic analysis member access expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "clang/Sema/SemaInternal.h" |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTLambda.h" |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
| 16 | #include "clang/AST/DeclObjC.h" |
| 17 | #include "clang/AST/DeclTemplate.h" |
| 18 | #include "clang/AST/ExprCXX.h" |
| 19 | #include "clang/AST/ExprObjC.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Lookup.h" |
| 22 | #include "clang/Sema/Scope.h" |
| 23 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace sema; |
| 27 | |
Richard Smith | d80b2d5 | 2012-11-22 00:24:47 +0000 | [diff] [blame] | 28 | typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> BaseSet; |
| 29 | static bool BaseIsNotInSet(const CXXRecordDecl *Base, void *BasesPtr) { |
| 30 | const BaseSet &Bases = *reinterpret_cast<const BaseSet*>(BasesPtr); |
| 31 | return !Bases.count(Base->getCanonicalDecl()); |
| 32 | } |
| 33 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 34 | /// Determines if the given class is provably not derived from all of |
| 35 | /// the prospective base classes. |
Richard Smith | d80b2d5 | 2012-11-22 00:24:47 +0000 | [diff] [blame] | 36 | static bool isProvablyNotDerivedFrom(Sema &SemaRef, CXXRecordDecl *Record, |
| 37 | const BaseSet &Bases) { |
| 38 | void *BasesPtr = const_cast<void*>(reinterpret_cast<const void*>(&Bases)); |
| 39 | return BaseIsNotInSet(Record, BasesPtr) && |
| 40 | Record->forallBases(BaseIsNotInSet, BasesPtr); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | enum IMAKind { |
| 44 | /// The reference is definitely not an instance member access. |
| 45 | IMA_Static, |
| 46 | |
| 47 | /// The reference may be an implicit instance member access. |
| 48 | IMA_Mixed, |
| 49 | |
Eli Friedman | 7bda7f7 | 2012-01-18 03:53:45 +0000 | [diff] [blame] | 50 | /// The reference may be to an instance member, but it might be invalid if |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 51 | /// so, because the context is not an instance method. |
| 52 | IMA_Mixed_StaticContext, |
| 53 | |
| 54 | /// The reference may be to an instance member, but it is invalid if |
| 55 | /// so, because the context is from an unrelated class. |
| 56 | IMA_Mixed_Unrelated, |
| 57 | |
| 58 | /// The reference is definitely an implicit instance member access. |
| 59 | IMA_Instance, |
| 60 | |
| 61 | /// The reference may be to an unresolved using declaration. |
| 62 | IMA_Unresolved, |
| 63 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 64 | /// The reference is a contextually-permitted abstract member reference. |
| 65 | IMA_Abstract, |
| 66 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 67 | /// The reference may be to an unresolved using declaration and the |
| 68 | /// context is not an instance method. |
| 69 | IMA_Unresolved_StaticContext, |
| 70 | |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 71 | // The reference refers to a field which is not a member of the containing |
| 72 | // class, which is allowed because we're in C++11 mode and the context is |
| 73 | // unevaluated. |
| 74 | IMA_Field_Uneval_Context, |
Eli Friedman | 7bda7f7 | 2012-01-18 03:53:45 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 76 | /// All possible referrents are instance members and the current |
| 77 | /// context is not an instance method. |
| 78 | IMA_Error_StaticContext, |
| 79 | |
| 80 | /// All possible referrents are instance members of an unrelated |
| 81 | /// class. |
| 82 | IMA_Error_Unrelated |
| 83 | }; |
| 84 | |
| 85 | /// The given lookup names class member(s) and is not being used for |
| 86 | /// an address-of-member expression. Classify the type of access |
| 87 | /// according to whether it's possible that this reference names an |
Eli Friedman | 7bda7f7 | 2012-01-18 03:53:45 +0000 | [diff] [blame] | 88 | /// instance member. This is best-effort in dependent contexts; it is okay to |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 89 | /// conservatively answer "yes", in which case some errors will simply |
| 90 | /// not be caught until template-instantiation. |
| 91 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 92 | Scope *CurScope, |
| 93 | const LookupResult &R) { |
| 94 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
| 95 | |
| 96 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
| 97 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 98 | bool isStaticContext = SemaRef.CXXThisTypeOverride.isNull() && |
| 99 | (!isa<CXXMethodDecl>(DC) || cast<CXXMethodDecl>(DC)->isStatic()); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 100 | |
| 101 | if (R.isUnresolvableResult()) |
| 102 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 103 | |
| 104 | // Collect all the declaring classes of instance members we find. |
| 105 | bool hasNonInstance = false; |
Eli Friedman | 7bda7f7 | 2012-01-18 03:53:45 +0000 | [diff] [blame] | 106 | bool isField = false; |
Richard Smith | d80b2d5 | 2012-11-22 00:24:47 +0000 | [diff] [blame] | 107 | BaseSet Classes; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 108 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 109 | NamedDecl *D = *I; |
| 110 | |
| 111 | if (D->isCXXInstanceMember()) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 112 | if (dyn_cast<FieldDecl>(D) || dyn_cast<MSPropertyDecl>(D) |
| 113 | || dyn_cast<IndirectFieldDecl>(D)) |
Eli Friedman | 7bda7f7 | 2012-01-18 03:53:45 +0000 | [diff] [blame] | 114 | isField = true; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 115 | |
| 116 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
| 117 | Classes.insert(R->getCanonicalDecl()); |
| 118 | } |
| 119 | else |
| 120 | hasNonInstance = true; |
| 121 | } |
| 122 | |
| 123 | // If we didn't find any instance members, it can't be an implicit |
| 124 | // member reference. |
| 125 | if (Classes.empty()) |
| 126 | return IMA_Static; |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 127 | |
| 128 | // C++11 [expr.prim.general]p12: |
| 129 | // An id-expression that denotes a non-static data member or non-static |
| 130 | // member function of a class can only be used: |
| 131 | // (...) |
| 132 | // - if that id-expression denotes a non-static data member and it |
| 133 | // appears in an unevaluated operand. |
| 134 | // |
| 135 | // This rule is specific to C++11. However, we also permit this form |
| 136 | // in unevaluated inline assembly operands, like the operand to a SIZE. |
| 137 | IMAKind AbstractInstanceResult = IMA_Static; // happens to be 'false' |
| 138 | assert(!AbstractInstanceResult); |
| 139 | switch (SemaRef.ExprEvalContexts.back().Context) { |
| 140 | case Sema::Unevaluated: |
| 141 | if (isField && SemaRef.getLangOpts().CPlusPlus11) |
| 142 | AbstractInstanceResult = IMA_Field_Uneval_Context; |
| 143 | break; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 144 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 145 | case Sema::UnevaluatedAbstract: |
| 146 | AbstractInstanceResult = IMA_Abstract; |
| 147 | break; |
| 148 | |
| 149 | case Sema::ConstantEvaluated: |
| 150 | case Sema::PotentiallyEvaluated: |
| 151 | case Sema::PotentiallyEvaluatedIfUsed: |
| 152 | break; |
Richard Smith | eae9968 | 2012-02-25 10:04:07 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 155 | // If the current context is not an instance method, it can't be |
| 156 | // an implicit member reference. |
| 157 | if (isStaticContext) { |
| 158 | if (hasNonInstance) |
Richard Smith | eae9968 | 2012-02-25 10:04:07 +0000 | [diff] [blame] | 159 | return IMA_Mixed_StaticContext; |
| 160 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 161 | return AbstractInstanceResult ? AbstractInstanceResult |
| 162 | : IMA_Error_StaticContext; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | CXXRecordDecl *contextClass; |
| 166 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) |
| 167 | contextClass = MD->getParent()->getCanonicalDecl(); |
| 168 | else |
| 169 | contextClass = cast<CXXRecordDecl>(DC); |
| 170 | |
| 171 | // [class.mfct.non-static]p3: |
| 172 | // ...is used in the body of a non-static member function of class X, |
| 173 | // if name lookup (3.4.1) resolves the name in the id-expression to a |
| 174 | // non-static non-type member of some class C [...] |
| 175 | // ...if C is not X or a base class of X, the class member access expression |
| 176 | // is ill-formed. |
| 177 | if (R.getNamingClass() && |
DeLesley Hutchins | 5b330db | 2012-02-25 00:11:55 +0000 | [diff] [blame] | 178 | contextClass->getCanonicalDecl() != |
Richard Smith | d80b2d5 | 2012-11-22 00:24:47 +0000 | [diff] [blame] | 179 | R.getNamingClass()->getCanonicalDecl()) { |
| 180 | // If the naming class is not the current context, this was a qualified |
| 181 | // member name lookup, and it's sufficient to check that we have the naming |
| 182 | // class as a base class. |
| 183 | Classes.clear(); |
Richard Smith | b2c5f96 | 2012-11-22 00:40:54 +0000 | [diff] [blame] | 184 | Classes.insert(R.getNamingClass()->getCanonicalDecl()); |
Richard Smith | d80b2d5 | 2012-11-22 00:24:47 +0000 | [diff] [blame] | 185 | } |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 186 | |
| 187 | // If we can prove that the current context is unrelated to all the |
| 188 | // declaring classes, it can't be an implicit member reference (in |
| 189 | // which case it's an error if any of those members are selected). |
Richard Smith | d80b2d5 | 2012-11-22 00:24:47 +0000 | [diff] [blame] | 190 | if (isProvablyNotDerivedFrom(SemaRef, contextClass, Classes)) |
Richard Smith | 2a98611 | 2012-02-25 10:20:59 +0000 | [diff] [blame] | 191 | return hasNonInstance ? IMA_Mixed_Unrelated : |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 192 | AbstractInstanceResult ? AbstractInstanceResult : |
| 193 | IMA_Error_Unrelated; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 194 | |
| 195 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 196 | } |
| 197 | |
| 198 | /// Diagnose a reference to a field with no object available. |
Richard Smith | fa0a1f5 | 2012-04-05 01:13:04 +0000 | [diff] [blame] | 199 | static void diagnoseInstanceReference(Sema &SemaRef, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 200 | const CXXScopeSpec &SS, |
Richard Smith | fa0a1f5 | 2012-04-05 01:13:04 +0000 | [diff] [blame] | 201 | NamedDecl *Rep, |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 202 | const DeclarationNameInfo &nameInfo) { |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 203 | SourceLocation Loc = nameInfo.getLoc(); |
| 204 | SourceRange Range(Loc); |
| 205 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
Eli Friedman | 7bda7f7 | 2012-01-18 03:53:45 +0000 | [diff] [blame] | 206 | |
Richard Smith | fa0a1f5 | 2012-04-05 01:13:04 +0000 | [diff] [blame] | 207 | DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext(); |
| 208 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FunctionLevelDC); |
| 209 | CXXRecordDecl *ContextClass = Method ? Method->getParent() : 0; |
| 210 | CXXRecordDecl *RepClass = dyn_cast<CXXRecordDecl>(Rep->getDeclContext()); |
| 211 | |
| 212 | bool InStaticMethod = Method && Method->isStatic(); |
| 213 | bool IsField = isa<FieldDecl>(Rep) || isa<IndirectFieldDecl>(Rep); |
| 214 | |
| 215 | if (IsField && InStaticMethod) |
| 216 | // "invalid use of member 'x' in static member function" |
| 217 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| 218 | << Range << nameInfo.getName(); |
| 219 | else if (ContextClass && RepClass && SS.isEmpty() && !InStaticMethod && |
| 220 | !RepClass->Equals(ContextClass) && RepClass->Encloses(ContextClass)) |
| 221 | // Unqualified lookup in a non-static member function found a member of an |
| 222 | // enclosing class. |
| 223 | SemaRef.Diag(Loc, diag::err_nested_non_static_member_use) |
| 224 | << IsField << RepClass << nameInfo.getName() << ContextClass << Range; |
| 225 | else if (IsField) |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 226 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
Richard Smith | fa0a1f5 | 2012-04-05 01:13:04 +0000 | [diff] [blame] | 227 | << nameInfo.getName() << Range; |
| 228 | else |
| 229 | SemaRef.Diag(Loc, diag::err_member_call_without_object) |
| 230 | << Range; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /// Builds an expression which might be an implicit member expression. |
| 234 | ExprResult |
| 235 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 236 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 237 | LookupResult &R, |
| 238 | const TemplateArgumentListInfo *TemplateArgs) { |
| 239 | switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) { |
| 240 | case IMA_Instance: |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 241 | return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 242 | |
| 243 | case IMA_Mixed: |
| 244 | case IMA_Mixed_Unrelated: |
| 245 | case IMA_Unresolved: |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 246 | return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, false); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 247 | |
Richard Smith | 2a98611 | 2012-02-25 10:20:59 +0000 | [diff] [blame] | 248 | case IMA_Field_Uneval_Context: |
| 249 | Diag(R.getNameLoc(), diag::warn_cxx98_compat_non_static_member_use) |
| 250 | << R.getLookupNameInfo().getName(); |
| 251 | // Fall through. |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 252 | case IMA_Static: |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 253 | case IMA_Abstract: |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 254 | case IMA_Mixed_StaticContext: |
| 255 | case IMA_Unresolved_StaticContext: |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 256 | if (TemplateArgs || TemplateKWLoc.isValid()) |
| 257 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 258 | return BuildDeclarationNameExpr(SS, R, false); |
| 259 | |
| 260 | case IMA_Error_StaticContext: |
| 261 | case IMA_Error_Unrelated: |
Richard Smith | fa0a1f5 | 2012-04-05 01:13:04 +0000 | [diff] [blame] | 262 | diagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 263 | R.getLookupNameInfo()); |
| 264 | return ExprError(); |
| 265 | } |
| 266 | |
| 267 | llvm_unreachable("unexpected instance member access kind"); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /// Check an ext-vector component access expression. |
| 271 | /// |
| 272 | /// VK should be set in advance to the value kind of the base |
| 273 | /// expression. |
| 274 | static QualType |
| 275 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 276 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
| 277 | SourceLocation CompLoc) { |
| 278 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 279 | // see FIXME there. |
| 280 | // |
| 281 | // FIXME: This logic can be greatly simplified by splitting it along |
| 282 | // halving/not halving and reworking the component checking. |
| 283 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
| 284 | |
| 285 | // The vector accessor can't exceed the number of elements. |
| 286 | const char *compStr = CompName->getNameStart(); |
| 287 | |
| 288 | // This flag determines whether or not the component is one of the four |
| 289 | // special names that indicate a subset of exactly half the elements are |
| 290 | // to be selected. |
| 291 | bool HalvingSwizzle = false; |
| 292 | |
| 293 | // This flag determines whether or not CompName has an 's' char prefix, |
| 294 | // indicating that it is a string of hex values to be used as vector indices. |
| 295 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
| 296 | |
| 297 | bool HasRepeated = false; |
| 298 | bool HasIndex[16] = {}; |
| 299 | |
| 300 | int Idx; |
| 301 | |
| 302 | // Check that we've found one of the special components, or that the component |
| 303 | // names must come from the same set. |
| 304 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
| 305 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 306 | HalvingSwizzle = true; |
| 307 | } else if (!HexSwizzle && |
| 308 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 309 | do { |
| 310 | if (HasIndex[Idx]) HasRepeated = true; |
| 311 | HasIndex[Idx] = true; |
| 312 | compStr++; |
| 313 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 314 | } else { |
| 315 | if (HexSwizzle) compStr++; |
| 316 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 317 | if (HasIndex[Idx]) HasRepeated = true; |
| 318 | HasIndex[Idx] = true; |
| 319 | compStr++; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (!HalvingSwizzle && *compStr) { |
| 324 | // We didn't get to the end of the string. This means the component names |
| 325 | // didn't come from the same set *or* we encountered an illegal name. |
| 326 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 327 | << StringRef(compStr, 1) << SourceRange(CompLoc); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 328 | return QualType(); |
| 329 | } |
| 330 | |
| 331 | // Ensure no component accessor exceeds the width of the vector type it |
| 332 | // operates on. |
| 333 | if (!HalvingSwizzle) { |
| 334 | compStr = CompName->getNameStart(); |
| 335 | |
| 336 | if (HexSwizzle) |
| 337 | compStr++; |
| 338 | |
| 339 | while (*compStr) { |
| 340 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 341 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 342 | << baseType << SourceRange(CompLoc); |
| 343 | return QualType(); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // The component accessor looks fine - now we need to compute the actual type. |
| 349 | // The vector type is implied by the component accessor. For example, |
| 350 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
| 351 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
| 352 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
| 353 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
| 354 | : CompName->getLength(); |
| 355 | if (HexSwizzle) |
| 356 | CompSize--; |
| 357 | |
| 358 | if (CompSize == 1) |
| 359 | return vecType->getElementType(); |
| 360 | |
| 361 | if (HasRepeated) VK = VK_RValue; |
| 362 | |
| 363 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
| 364 | // Now look up the TypeDefDecl from the vector type. Without this, |
| 365 | // diagostics look bad. We want extended vector types to appear built-in. |
Douglas Gregor | b7098a3 | 2011-07-28 00:39:29 +0000 | [diff] [blame] | 366 | for (Sema::ExtVectorDeclsType::iterator |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 367 | I = S.ExtVectorDecls.begin(S.getExternalSource()), |
Douglas Gregor | b7098a3 | 2011-07-28 00:39:29 +0000 | [diff] [blame] | 368 | E = S.ExtVectorDecls.end(); |
| 369 | I != E; ++I) { |
| 370 | if ((*I)->getUnderlyingType() == VT) |
| 371 | return S.Context.getTypedefType(*I); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 372 | } |
Douglas Gregor | b7098a3 | 2011-07-28 00:39:29 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 374 | return VT; // should never get here (a typedef type should always be found). |
| 375 | } |
| 376 | |
| 377 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
| 378 | IdentifierInfo *Member, |
| 379 | const Selector &Sel, |
| 380 | ASTContext &Context) { |
| 381 | if (Member) |
| 382 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 383 | return PD; |
| 384 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
| 385 | return OMD; |
| 386 | |
| 387 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 388 | E = PDecl->protocol_end(); I != E; ++I) { |
| 389 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 390 | Context)) |
| 391 | return D; |
| 392 | } |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 397 | IdentifierInfo *Member, |
| 398 | const Selector &Sel, |
| 399 | ASTContext &Context) { |
| 400 | // Check protocols on qualified interfaces. |
| 401 | Decl *GDecl = 0; |
| 402 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
| 403 | E = QIdTy->qual_end(); I != E; ++I) { |
| 404 | if (Member) |
| 405 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 406 | GDecl = PD; |
| 407 | break; |
| 408 | } |
| 409 | // Also must look for a getter or setter name which uses property syntax. |
| 410 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
| 411 | GDecl = OMD; |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | if (!GDecl) { |
| 416 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
| 417 | E = QIdTy->qual_end(); I != E; ++I) { |
| 418 | // Search in the protocol-qualifier list of current protocol. |
| 419 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 420 | Context); |
| 421 | if (GDecl) |
| 422 | return GDecl; |
| 423 | } |
| 424 | } |
| 425 | return GDecl; |
| 426 | } |
| 427 | |
| 428 | ExprResult |
| 429 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
| 430 | bool IsArrow, SourceLocation OpLoc, |
| 431 | const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 432 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 433 | NamedDecl *FirstQualifierInScope, |
| 434 | const DeclarationNameInfo &NameInfo, |
| 435 | const TemplateArgumentListInfo *TemplateArgs) { |
| 436 | // Even in dependent contexts, try to diagnose base expressions with |
| 437 | // obviously wrong types, e.g.: |
| 438 | // |
| 439 | // T* t; |
| 440 | // t.f; |
| 441 | // |
| 442 | // In Obj-C++, however, the above expression is valid, since it could be |
| 443 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 444 | // allows this, while still reporting an error if T is a struct pointer. |
| 445 | if (!IsArrow) { |
| 446 | const PointerType *PT = BaseType->getAs<PointerType>(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 447 | if (PT && (!getLangOpts().ObjC1 || |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 448 | PT->getPointeeType()->isRecordType())) { |
| 449 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Matt Beaumont-Gay | d9f244af | 2012-04-21 01:12:48 +0000 | [diff] [blame] | 450 | Diag(OpLoc, diag::err_typecheck_member_reference_struct_union) |
Matt Beaumont-Gay | 025321b | 2012-04-21 02:13:04 +0000 | [diff] [blame] | 451 | << BaseType << BaseExpr->getSourceRange() << NameInfo.getSourceRange(); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 452 | return ExprError(); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | assert(BaseType->isDependentType() || |
| 457 | NameInfo.getName().isDependentName() || |
| 458 | isDependentScopeSpecifier(SS)); |
| 459 | |
| 460 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 461 | // must have pointer type, and the accessed type is the pointee. |
| 462 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
| 463 | IsArrow, OpLoc, |
| 464 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 465 | TemplateKWLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 466 | FirstQualifierInScope, |
| 467 | NameInfo, TemplateArgs)); |
| 468 | } |
| 469 | |
| 470 | /// We know that the given qualified member reference points only to |
| 471 | /// declarations which do not belong to the static type of the base |
| 472 | /// expression. Diagnose the problem. |
| 473 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 474 | Expr *BaseExpr, |
| 475 | QualType BaseType, |
| 476 | const CXXScopeSpec &SS, |
| 477 | NamedDecl *rep, |
| 478 | const DeclarationNameInfo &nameInfo) { |
| 479 | // If this is an implicit member access, use a different set of |
| 480 | // diagnostics. |
| 481 | if (!BaseExpr) |
Richard Smith | fa0a1f5 | 2012-04-05 01:13:04 +0000 | [diff] [blame] | 482 | return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 483 | |
| 484 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 485 | << SS.getRange() << rep << BaseType; |
| 486 | } |
| 487 | |
| 488 | // Check whether the declarations we found through a nested-name |
| 489 | // specifier in a member expression are actually members of the base |
| 490 | // type. The restriction here is: |
| 491 | // |
| 492 | // C++ [expr.ref]p2: |
| 493 | // ... In these cases, the id-expression shall name a |
| 494 | // member of the class or of one of its base classes. |
| 495 | // |
| 496 | // So it's perfectly legitimate for the nested-name specifier to name |
| 497 | // an unrelated class, and for us to find an overload set including |
| 498 | // decls from classes which are not superclasses, as long as the decl |
| 499 | // we actually pick through overload resolution is from a superclass. |
| 500 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 501 | QualType BaseType, |
| 502 | const CXXScopeSpec &SS, |
| 503 | const LookupResult &R) { |
Richard Smith | d80b2d5 | 2012-11-22 00:24:47 +0000 | [diff] [blame] | 504 | CXXRecordDecl *BaseRecord = |
| 505 | cast_or_null<CXXRecordDecl>(computeDeclContext(BaseType)); |
| 506 | if (!BaseRecord) { |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 507 | // We can't check this yet because the base type is still |
| 508 | // dependent. |
| 509 | assert(BaseType->isDependentType()); |
| 510 | return false; |
| 511 | } |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 512 | |
| 513 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 514 | // If this is an implicit member reference and we find a |
| 515 | // non-instance member, it's not an error. |
| 516 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
| 517 | return false; |
| 518 | |
| 519 | // Note that we use the DC of the decl, not the underlying decl. |
| 520 | DeclContext *DC = (*I)->getDeclContext(); |
| 521 | while (DC->isTransparentContext()) |
| 522 | DC = DC->getParent(); |
| 523 | |
| 524 | if (!DC->isRecord()) |
| 525 | continue; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 526 | |
Richard Smith | d80b2d5 | 2012-11-22 00:24:47 +0000 | [diff] [blame] | 527 | CXXRecordDecl *MemberRecord = cast<CXXRecordDecl>(DC)->getCanonicalDecl(); |
| 528 | if (BaseRecord->getCanonicalDecl() == MemberRecord || |
| 529 | !BaseRecord->isProvablyNotDerivedFrom(MemberRecord)) |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | |
| 533 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 534 | R.getRepresentativeDecl(), |
| 535 | R.getLookupNameInfo()); |
| 536 | return true; |
| 537 | } |
| 538 | |
Kaelyn Uhrain | 3658e6a | 2012-01-13 21:28:55 +0000 | [diff] [blame] | 539 | namespace { |
| 540 | |
| 541 | // Callback to only accept typo corrections that are either a ValueDecl or a |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 542 | // FunctionTemplateDecl and are declared in the current record or, for a C++ |
| 543 | // classes, one of its base classes. |
Kaelyn Uhrain | 3658e6a | 2012-01-13 21:28:55 +0000 | [diff] [blame] | 544 | class RecordMemberExprValidatorCCC : public CorrectionCandidateCallback { |
| 545 | public: |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 546 | explicit RecordMemberExprValidatorCCC(const RecordType *RTy) |
| 547 | : Record(RTy->getDecl()) {} |
| 548 | |
Kaelyn Uhrain | 3658e6a | 2012-01-13 21:28:55 +0000 | [diff] [blame] | 549 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 550 | NamedDecl *ND = candidate.getCorrectionDecl(); |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 551 | // Don't accept candidates that cannot be member functions, constants, |
| 552 | // variables, or templates. |
| 553 | if (!ND || !(isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND))) |
| 554 | return false; |
| 555 | |
| 556 | // Accept candidates that occur in the current record. |
| 557 | if (Record->containsDecl(ND)) |
| 558 | return true; |
| 559 | |
| 560 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Record)) { |
| 561 | // Accept candidates that occur in any of the current class' base classes. |
| 562 | for (CXXRecordDecl::base_class_const_iterator BS = RD->bases_begin(), |
| 563 | BSEnd = RD->bases_end(); |
| 564 | BS != BSEnd; ++BS) { |
| 565 | if (const RecordType *BSTy = dyn_cast_or_null<RecordType>( |
| 566 | BS->getType().getTypePtrOrNull())) { |
| 567 | if (BSTy->getDecl()->containsDecl(ND)) |
| 568 | return true; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | return false; |
Kaelyn Uhrain | 3658e6a | 2012-01-13 21:28:55 +0000 | [diff] [blame] | 574 | } |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 575 | |
| 576 | private: |
| 577 | const RecordDecl *const Record; |
Kaelyn Uhrain | 3658e6a | 2012-01-13 21:28:55 +0000 | [diff] [blame] | 578 | }; |
| 579 | |
| 580 | } |
| 581 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 582 | static bool |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 583 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 584 | SourceRange BaseRange, const RecordType *RTy, |
| 585 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 586 | bool HasTemplateArgs) { |
| 587 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 588 | if (!SemaRef.isThisOutsideMemberFunctionBody(QualType(RTy, 0)) && |
| 589 | SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 590 | diag::err_typecheck_incomplete_tag, |
| 591 | BaseRange)) |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 592 | return true; |
| 593 | |
| 594 | if (HasTemplateArgs) { |
| 595 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 596 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 597 | |
| 598 | bool MOUS; |
| 599 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | DeclContext *DC = RDecl; |
| 604 | if (SS.isSet()) { |
| 605 | // If the member name was a qualified-id, look into the |
| 606 | // nested-name-specifier. |
| 607 | DC = SemaRef.computeDeclContext(SS, false); |
| 608 | |
| 609 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
| 610 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 611 | << SS.getRange() << DC; |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 616 | |
| 617 | if (!isa<TypeDecl>(DC)) { |
| 618 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 619 | << DC << SS.getRange(); |
| 620 | return true; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | // The record definition is complete, now look up the member. |
| 625 | SemaRef.LookupQualifiedName(R, DC); |
| 626 | |
| 627 | if (!R.empty()) |
| 628 | return false; |
| 629 | |
| 630 | // We didn't find anything with the given name, so try to correct |
| 631 | // for typos. |
| 632 | DeclarationName Name = R.getLookupName(); |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 633 | RecordMemberExprValidatorCCC Validator(RTy); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 634 | TypoCorrection Corrected = SemaRef.CorrectTypo(R.getLookupNameInfo(), |
| 635 | R.getLookupKind(), NULL, |
Kaelyn Uhrain | 4e8942c | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 636 | &SS, Validator, DC); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 637 | R.clear(); |
Nick Lewycky | d1b0df4 | 2013-05-07 22:14:37 +0000 | [diff] [blame] | 638 | if (Corrected.isResolved() && !Corrected.isKeyword()) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 639 | R.setLookupName(Corrected.getCorrection()); |
Nick Lewycky | d1b0df4 | 2013-05-07 22:14:37 +0000 | [diff] [blame] | 640 | for (TypoCorrection::decl_iterator DI = Corrected.begin(), |
| 641 | DIEnd = Corrected.end(); |
| 642 | DI != DIEnd; ++DI) { |
| 643 | R.addDecl(*DI); |
| 644 | } |
| 645 | R.resolveKind(); |
| 646 | |
Nick Lewycky | d1b0df4 | 2013-05-07 22:14:37 +0000 | [diff] [blame] | 647 | // If we're typo-correcting to an overloaded name, we don't yet have enough |
| 648 | // information to do overload resolution, so we don't know which previous |
| 649 | // declaration to point to. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 650 | if (Corrected.isOverloaded()) |
| 651 | Corrected.setCorrectionDecl(0); |
| 652 | bool DroppedSpecifier = |
| 653 | Corrected.WillReplaceSpecifier() && |
| 654 | Name.getAsString() == Corrected.getAsString(SemaRef.getLangOpts()); |
| 655 | SemaRef.diagnoseTypo(Corrected, |
| 656 | SemaRef.PDiag(diag::err_no_member_suggest) |
| 657 | << Name << DC << DroppedSpecifier << SS.getRange()); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | return false; |
| 661 | } |
| 662 | |
| 663 | ExprResult |
| 664 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
| 665 | SourceLocation OpLoc, bool IsArrow, |
| 666 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 667 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 668 | NamedDecl *FirstQualifierInScope, |
| 669 | const DeclarationNameInfo &NameInfo, |
| 670 | const TemplateArgumentListInfo *TemplateArgs) { |
| 671 | if (BaseType->isDependentType() || |
| 672 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
| 673 | return ActOnDependentMemberExpr(Base, BaseType, |
| 674 | IsArrow, OpLoc, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 675 | SS, TemplateKWLoc, FirstQualifierInScope, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 676 | NameInfo, TemplateArgs); |
| 677 | |
| 678 | LookupResult R(*this, NameInfo, LookupMemberName); |
| 679 | |
| 680 | // Implicit member accesses. |
| 681 | if (!Base) { |
| 682 | QualType RecordTy = BaseType; |
| 683 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 684 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 685 | RecordTy->getAs<RecordType>(), |
| 686 | OpLoc, SS, TemplateArgs != 0)) |
| 687 | return ExprError(); |
| 688 | |
| 689 | // Explicit member accesses. |
| 690 | } else { |
| 691 | ExprResult BaseResult = Owned(Base); |
| 692 | ExprResult Result = |
| 693 | LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
| 694 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
| 695 | |
| 696 | if (BaseResult.isInvalid()) |
| 697 | return ExprError(); |
| 698 | Base = BaseResult.take(); |
| 699 | |
| 700 | if (Result.isInvalid()) { |
| 701 | Owned(Base); |
| 702 | return ExprError(); |
| 703 | } |
| 704 | |
| 705 | if (Result.get()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 706 | return Result; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 707 | |
| 708 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 709 | BaseType = Base->getType(); |
| 710 | } |
| 711 | |
| 712 | return BuildMemberReferenceExpr(Base, BaseType, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 713 | OpLoc, IsArrow, SS, TemplateKWLoc, |
| 714 | FirstQualifierInScope, R, TemplateArgs); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static ExprResult |
| 718 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 719 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 720 | DeclAccessPair FoundDecl, |
| 721 | const DeclarationNameInfo &MemberNameInfo); |
| 722 | |
| 723 | ExprResult |
| 724 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 725 | SourceLocation loc, |
| 726 | IndirectFieldDecl *indirectField, |
Eli Friedman | cccd064 | 2013-07-16 00:01:31 +0000 | [diff] [blame] | 727 | DeclAccessPair foundDecl, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 728 | Expr *baseObjectExpr, |
| 729 | SourceLocation opLoc) { |
| 730 | // First, build the expression that refers to the base object. |
| 731 | |
| 732 | bool baseObjectIsPointer = false; |
| 733 | Qualifiers baseQuals; |
| 734 | |
| 735 | // Case 1: the base of the indirect field is not a field. |
| 736 | VarDecl *baseVariable = indirectField->getVarDecl(); |
| 737 | CXXScopeSpec EmptySS; |
| 738 | if (baseVariable) { |
| 739 | assert(baseVariable->getType()->isRecordType()); |
| 740 | |
| 741 | // In principle we could have a member access expression that |
| 742 | // accesses an anonymous struct/union that's a static member of |
| 743 | // the base object's class. However, under the current standard, |
| 744 | // static data members cannot be anonymous structs or unions. |
| 745 | // Supporting this is as easy as building a MemberExpr here. |
| 746 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 747 | |
| 748 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 749 | |
| 750 | ExprResult result |
| 751 | = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
| 752 | if (result.isInvalid()) return ExprError(); |
| 753 | |
| 754 | baseObjectExpr = result.take(); |
| 755 | baseObjectIsPointer = false; |
| 756 | baseQuals = baseObjectExpr->getType().getQualifiers(); |
| 757 | |
| 758 | // Case 2: the base of the indirect field is a field and the user |
| 759 | // wrote a member expression. |
| 760 | } else if (baseObjectExpr) { |
| 761 | // The caller provided the base object expression. Determine |
| 762 | // whether its a pointer and whether it adds any qualifiers to the |
| 763 | // anonymous struct/union fields we're looking into. |
| 764 | QualType objectType = baseObjectExpr->getType(); |
| 765 | |
| 766 | if (const PointerType *ptr = objectType->getAs<PointerType>()) { |
| 767 | baseObjectIsPointer = true; |
| 768 | objectType = ptr->getPointeeType(); |
| 769 | } else { |
| 770 | baseObjectIsPointer = false; |
| 771 | } |
| 772 | baseQuals = objectType.getQualifiers(); |
| 773 | |
| 774 | // Case 3: the base of the indirect field is a field and we should |
| 775 | // build an implicit member access. |
| 776 | } else { |
| 777 | // We've found a member of an anonymous struct/union that is |
| 778 | // inside a non-anonymous struct/union, so in a well-formed |
| 779 | // program our base object expression is "this". |
Douglas Gregor | 09deffa | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 780 | QualType ThisTy = getCurrentThisType(); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 781 | if (ThisTy.isNull()) { |
| 782 | Diag(loc, diag::err_invalid_member_use_in_static_method) |
| 783 | << indirectField->getDeclName(); |
| 784 | return ExprError(); |
| 785 | } |
| 786 | |
| 787 | // Our base object expression is "this". |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 788 | CheckCXXThisCapture(loc); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 789 | baseObjectExpr |
| 790 | = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true); |
| 791 | baseObjectIsPointer = true; |
| 792 | baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers(); |
| 793 | } |
| 794 | |
| 795 | // Build the implicit member references to the field of the |
| 796 | // anonymous struct/union. |
| 797 | Expr *result = baseObjectExpr; |
| 798 | IndirectFieldDecl::chain_iterator |
| 799 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
| 800 | |
| 801 | // Build the first member access in the chain with full information. |
| 802 | if (!baseVariable) { |
| 803 | FieldDecl *field = cast<FieldDecl>(*FI); |
| 804 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 805 | // Make a nameInfo that properly uses the anonymous name. |
| 806 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 807 | |
| 808 | result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, |
| 809 | EmptySS, field, foundDecl, |
| 810 | memberNameInfo).take(); |
Eli Friedman | cccd064 | 2013-07-16 00:01:31 +0000 | [diff] [blame] | 811 | if (!result) |
| 812 | return ExprError(); |
| 813 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 814 | baseObjectIsPointer = false; |
| 815 | |
| 816 | // FIXME: check qualified member access |
| 817 | } |
| 818 | |
| 819 | // In all cases, we should now skip the first declaration in the chain. |
| 820 | ++FI; |
| 821 | |
| 822 | while (FI != FEnd) { |
| 823 | FieldDecl *field = cast<FieldDecl>(*FI++); |
Eli Friedman | cccd064 | 2013-07-16 00:01:31 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 825 | // FIXME: these are somewhat meaningless |
| 826 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
Eli Friedman | cccd064 | 2013-07-16 00:01:31 +0000 | [diff] [blame] | 827 | DeclAccessPair fakeFoundDecl = |
| 828 | DeclAccessPair::make(field, field->getAccess()); |
| 829 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 830 | result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, |
Eli Friedman | cccd064 | 2013-07-16 00:01:31 +0000 | [diff] [blame] | 831 | (FI == FEnd? SS : EmptySS), field, |
| 832 | fakeFoundDecl, memberNameInfo).take(); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | return Owned(result); |
| 836 | } |
| 837 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 838 | static ExprResult |
| 839 | BuildMSPropertyRefExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 840 | const CXXScopeSpec &SS, |
| 841 | MSPropertyDecl *PD, |
| 842 | const DeclarationNameInfo &NameInfo) { |
| 843 | // Property names are always simple identifiers and therefore never |
| 844 | // require any interesting additional storage. |
| 845 | return new (S.Context) MSPropertyRefExpr(BaseExpr, PD, IsArrow, |
| 846 | S.Context.PseudoObjectTy, VK_LValue, |
| 847 | SS.getWithLocInContext(S.Context), |
| 848 | NameInfo.getLoc()); |
| 849 | } |
| 850 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 851 | /// \brief Build a MemberExpr AST node. |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 852 | static MemberExpr *BuildMemberExpr(Sema &SemaRef, |
| 853 | ASTContext &C, Expr *Base, bool isArrow, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 854 | const CXXScopeSpec &SS, |
| 855 | SourceLocation TemplateKWLoc, |
| 856 | ValueDecl *Member, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 857 | DeclAccessPair FoundDecl, |
| 858 | const DeclarationNameInfo &MemberNameInfo, |
| 859 | QualType Ty, |
| 860 | ExprValueKind VK, ExprObjectKind OK, |
| 861 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 862 | assert((!isArrow || Base->isRValue()) && "-> base must be a pointer rvalue"); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 863 | MemberExpr *E = |
| 864 | MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C), |
| 865 | TemplateKWLoc, Member, FoundDecl, MemberNameInfo, |
| 866 | TemplateArgs, Ty, VK, OK); |
| 867 | SemaRef.MarkMemberReferenced(E); |
| 868 | return E; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | ExprResult |
| 872 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
| 873 | SourceLocation OpLoc, bool IsArrow, |
| 874 | const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 875 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 876 | NamedDecl *FirstQualifierInScope, |
| 877 | LookupResult &R, |
Kaelyn Uhrain | 76e0734 | 2012-04-25 19:49:54 +0000 | [diff] [blame] | 878 | const TemplateArgumentListInfo *TemplateArgs, |
| 879 | bool SuppressQualifierCheck, |
| 880 | ActOnMemberAccessExtraArgs *ExtraArgs) { |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 881 | QualType BaseType = BaseExprType; |
| 882 | if (IsArrow) { |
| 883 | assert(BaseType->isPointerType()); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 884 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 885 | } |
| 886 | R.setBaseObjectType(BaseType); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 887 | |
| 888 | LambdaScopeInfo *const CurLSI = getCurLambda(); |
| 889 | // If this is an implicit member reference and the overloaded |
| 890 | // name refers to both static and non-static member functions |
| 891 | // (i.e. BaseExpr is null) and if we are currently processing a lambda, |
| 892 | // check if we should/can capture 'this'... |
| 893 | // Keep this example in mind: |
| 894 | // struct X { |
| 895 | // void f(int) { } |
| 896 | // static void f(double) { } |
| 897 | // |
| 898 | // int g() { |
| 899 | // auto L = [=](auto a) { |
| 900 | // return [](int i) { |
| 901 | // return [=](auto b) { |
| 902 | // f(b); |
| 903 | // //f(decltype(a){}); |
| 904 | // }; |
| 905 | // }; |
| 906 | // }; |
| 907 | // auto M = L(0.0); |
| 908 | // auto N = M(3); |
| 909 | // N(5.32); // OK, must not error. |
| 910 | // return 0; |
| 911 | // } |
| 912 | // }; |
| 913 | // |
| 914 | if (!BaseExpr && CurLSI) { |
| 915 | SourceLocation Loc = R.getNameLoc(); |
| 916 | if (SS.getRange().isValid()) |
| 917 | Loc = SS.getRange().getBegin(); |
| 918 | DeclContext *EnclosingFunctionCtx = CurContext->getParent()->getParent(); |
| 919 | // If the enclosing function is not dependent, then this lambda is |
| 920 | // capture ready, so if we can capture this, do so. |
| 921 | if (!EnclosingFunctionCtx->isDependentContext()) { |
| 922 | // If the current lambda and all enclosing lambdas can capture 'this' - |
| 923 | // then go ahead and capture 'this' (since our unresolved overload set |
| 924 | // contains both static and non-static member functions). |
| 925 | if (!CheckCXXThisCapture(Loc, /*Explcit*/false, /*Diagnose*/false)) |
| 926 | CheckCXXThisCapture(Loc); |
| 927 | } else if (CurContext->isDependentContext()) { |
| 928 | // ... since this is an implicit member reference, that might potentially |
| 929 | // involve a 'this' capture, mark 'this' for potential capture in |
| 930 | // enclosing lambdas. |
| 931 | if (CurLSI->ImpCaptureStyle != CurLSI->ImpCap_None) |
| 932 | CurLSI->addPotentialThisCapture(Loc); |
| 933 | } |
| 934 | } |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 935 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 936 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 937 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
| 938 | |
| 939 | if (R.isAmbiguous()) |
| 940 | return ExprError(); |
| 941 | |
| 942 | if (R.empty()) { |
| 943 | // Rederive where we looked up. |
| 944 | DeclContext *DC = (SS.isSet() |
| 945 | ? computeDeclContext(SS, false) |
| 946 | : BaseType->getAs<RecordType>()->getDecl()); |
| 947 | |
Kaelyn Uhrain | 76e0734 | 2012-04-25 19:49:54 +0000 | [diff] [blame] | 948 | if (ExtraArgs) { |
| 949 | ExprResult RetryExpr; |
| 950 | if (!IsArrow && BaseExpr) { |
Kaelyn Uhrain | d4ea98a | 2012-05-01 01:17:53 +0000 | [diff] [blame] | 951 | SFINAETrap Trap(*this, true); |
Kaelyn Uhrain | 76e0734 | 2012-04-25 19:49:54 +0000 | [diff] [blame] | 952 | ParsedType ObjectType; |
| 953 | bool MayBePseudoDestructor = false; |
| 954 | RetryExpr = ActOnStartCXXMemberReference(getCurScope(), BaseExpr, |
| 955 | OpLoc, tok::arrow, ObjectType, |
| 956 | MayBePseudoDestructor); |
| 957 | if (RetryExpr.isUsable() && !Trap.hasErrorOccurred()) { |
| 958 | CXXScopeSpec TempSS(SS); |
| 959 | RetryExpr = ActOnMemberAccessExpr( |
| 960 | ExtraArgs->S, RetryExpr.get(), OpLoc, tok::arrow, TempSS, |
| 961 | TemplateKWLoc, ExtraArgs->Id, ExtraArgs->ObjCImpDecl, |
| 962 | ExtraArgs->HasTrailingLParen); |
| 963 | } |
| 964 | if (Trap.hasErrorOccurred()) |
| 965 | RetryExpr = ExprError(); |
| 966 | } |
| 967 | if (RetryExpr.isUsable()) { |
| 968 | Diag(OpLoc, diag::err_no_member_overloaded_arrow) |
| 969 | << MemberName << DC << FixItHint::CreateReplacement(OpLoc, "->"); |
| 970 | return RetryExpr; |
| 971 | } |
| 972 | } |
| 973 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 974 | Diag(R.getNameLoc(), diag::err_no_member) |
| 975 | << MemberName << DC |
| 976 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
| 977 | return ExprError(); |
| 978 | } |
| 979 | |
| 980 | // Diagnose lookups that find only declarations from a non-base |
| 981 | // type. This is possible for either qualified lookups (which may |
| 982 | // have been qualified with an unrelated type) or implicit member |
| 983 | // expressions (which were found with unqualified lookup and thus |
| 984 | // may have come from an enclosing scope). Note that it's okay for |
| 985 | // lookup to find declarations from a non-base type as long as those |
| 986 | // aren't the ones picked by overload resolution. |
| 987 | if ((SS.isSet() || !BaseExpr || |
| 988 | (isa<CXXThisExpr>(BaseExpr) && |
| 989 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
| 990 | !SuppressQualifierCheck && |
| 991 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
| 992 | return ExprError(); |
Fariborz Jahanian | 502d2ee | 2011-10-17 21:00:22 +0000 | [diff] [blame] | 993 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 994 | // Construct an unresolved result if we in fact got an unresolved |
| 995 | // result. |
| 996 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
| 997 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 998 | // pick a member. |
| 999 | R.suppressDiagnostics(); |
| 1000 | |
| 1001 | UnresolvedMemberExpr *MemExpr |
| 1002 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
| 1003 | BaseExpr, BaseExprType, |
| 1004 | IsArrow, OpLoc, |
| 1005 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1006 | TemplateKWLoc, MemberNameInfo, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1007 | TemplateArgs, R.begin(), R.end()); |
| 1008 | |
| 1009 | return Owned(MemExpr); |
| 1010 | } |
| 1011 | |
| 1012 | assert(R.isSingleResult()); |
| 1013 | DeclAccessPair FoundDecl = R.begin().getPair(); |
| 1014 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 1015 | |
| 1016 | // FIXME: diagnose the presence of template arguments now. |
| 1017 | |
| 1018 | // If the decl being referenced had an error, return an error for this |
| 1019 | // sub-expr without emitting another error, in order to avoid cascading |
| 1020 | // error cases. |
| 1021 | if (MemberDecl->isInvalidDecl()) |
| 1022 | return ExprError(); |
| 1023 | |
| 1024 | // Handle the implicit-member-access case. |
| 1025 | if (!BaseExpr) { |
| 1026 | // If this is not an instance member, convert to a non-member access. |
| 1027 | if (!MemberDecl->isCXXInstanceMember()) |
| 1028 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
| 1029 | |
| 1030 | SourceLocation Loc = R.getNameLoc(); |
| 1031 | if (SS.getRange().isValid()) |
| 1032 | Loc = SS.getRange().getBegin(); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1033 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1034 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
| 1035 | } |
| 1036 | |
| 1037 | bool ShouldCheckUse = true; |
| 1038 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 1039 | // Don't diagnose the use of a virtual member function unless it's |
| 1040 | // explicitly qualified. |
| 1041 | if (MD->isVirtual() && !SS.isSet()) |
| 1042 | ShouldCheckUse = false; |
| 1043 | } |
| 1044 | |
| 1045 | // Check the use of this member. |
| 1046 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 1047 | Owned(BaseExpr); |
| 1048 | return ExprError(); |
| 1049 | } |
| 1050 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1051 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 1052 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 1053 | SS, FD, FoundDecl, MemberNameInfo); |
| 1054 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1055 | if (MSPropertyDecl *PD = dyn_cast<MSPropertyDecl>(MemberDecl)) |
| 1056 | return BuildMSPropertyRefExpr(*this, BaseExpr, IsArrow, SS, PD, |
| 1057 | MemberNameInfo); |
| 1058 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1059 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 1060 | // We may have found a field within an anonymous union or struct |
| 1061 | // (C++ [class.union]). |
| 1062 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
Eli Friedman | cccd064 | 2013-07-16 00:01:31 +0000 | [diff] [blame] | 1063 | FoundDecl, BaseExpr, |
| 1064 | OpLoc); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1065 | |
| 1066 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1067 | return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS, |
| 1068 | TemplateKWLoc, Var, FoundDecl, MemberNameInfo, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1069 | Var->getType().getNonReferenceType(), |
| 1070 | VK_LValue, OK_Ordinary)); |
| 1071 | } |
| 1072 | |
| 1073 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 1074 | ExprValueKind valueKind; |
| 1075 | QualType type; |
| 1076 | if (MemberFn->isInstance()) { |
| 1077 | valueKind = VK_RValue; |
| 1078 | type = Context.BoundMemberTy; |
| 1079 | } else { |
| 1080 | valueKind = VK_LValue; |
| 1081 | type = MemberFn->getType(); |
| 1082 | } |
| 1083 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1084 | return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS, |
| 1085 | TemplateKWLoc, MemberFn, FoundDecl, |
| 1086 | MemberNameInfo, type, valueKind, |
| 1087 | OK_Ordinary)); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1088 | } |
| 1089 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
| 1090 | |
| 1091 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1092 | return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS, |
| 1093 | TemplateKWLoc, Enum, FoundDecl, MemberNameInfo, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1094 | Enum->getType(), VK_RValue, OK_Ordinary)); |
| 1095 | } |
| 1096 | |
| 1097 | Owned(BaseExpr); |
| 1098 | |
| 1099 | // We found something that we didn't expect. Complain. |
| 1100 | if (isa<TypeDecl>(MemberDecl)) |
| 1101 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
| 1102 | << MemberName << BaseType << int(IsArrow); |
| 1103 | else |
| 1104 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 1105 | << MemberName << BaseType << int(IsArrow); |
| 1106 | |
| 1107 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 1108 | << MemberName; |
| 1109 | R.suppressDiagnostics(); |
| 1110 | return ExprError(); |
| 1111 | } |
| 1112 | |
| 1113 | /// Given that normal member access failed on the given expression, |
| 1114 | /// and given that the expression's type involves builtin-id or |
| 1115 | /// builtin-Class, decide whether substituting in the redefinition |
| 1116 | /// types would be profitable. The redefinition type is whatever |
| 1117 | /// this translation unit tried to typedef to id/Class; we store |
| 1118 | /// it to the side and then re-use it in places like this. |
| 1119 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) { |
| 1120 | const ObjCObjectPointerType *opty |
| 1121 | = base.get()->getType()->getAs<ObjCObjectPointerType>(); |
| 1122 | if (!opty) return false; |
| 1123 | |
| 1124 | const ObjCObjectType *ty = opty->getObjectType(); |
| 1125 | |
| 1126 | QualType redef; |
| 1127 | if (ty->isObjCId()) { |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1128 | redef = S.Context.getObjCIdRedefinitionType(); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1129 | } else if (ty->isObjCClass()) { |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1130 | redef = S.Context.getObjCClassRedefinitionType(); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1131 | } else { |
| 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | // Do the substitution as long as the redefinition type isn't just a |
| 1136 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 1137 | opty = redef->getAs<ObjCObjectPointerType>(); |
Richard Trieu | f20d905 | 2012-10-12 17:48:40 +0000 | [diff] [blame] | 1138 | if (opty && !opty->getObjectType()->getInterface()) |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1139 | return false; |
| 1140 | |
| 1141 | base = S.ImpCastExprToType(base.take(), redef, CK_BitCast); |
| 1142 | return true; |
| 1143 | } |
| 1144 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1145 | static bool isRecordType(QualType T) { |
| 1146 | return T->isRecordType(); |
| 1147 | } |
| 1148 | static bool isPointerToRecordType(QualType T) { |
| 1149 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 1150 | return PT->getPointeeType()->isRecordType(); |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
Richard Smith | cab9a7d | 2011-10-26 19:06:56 +0000 | [diff] [blame] | 1154 | /// Perform conversions on the LHS of a member access expression. |
| 1155 | ExprResult |
| 1156 | Sema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) { |
Eli Friedman | 9a766c4 | 2012-01-13 02:20:01 +0000 | [diff] [blame] | 1157 | if (IsArrow && !Base->getType()->isFunctionType()) |
| 1158 | return DefaultFunctionArrayLvalueConversion(Base); |
Richard Smith | cab9a7d | 2011-10-26 19:06:56 +0000 | [diff] [blame] | 1159 | |
Eli Friedman | 9a766c4 | 2012-01-13 02:20:01 +0000 | [diff] [blame] | 1160 | return CheckPlaceholderExpr(Base); |
Richard Smith | cab9a7d | 2011-10-26 19:06:56 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1163 | /// Look up the given member of the given non-type-dependent |
| 1164 | /// expression. This can return in one of two ways: |
| 1165 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 1166 | /// assume that lookup was performed and the results written into |
| 1167 | /// the provided structure. It will take over from there. |
| 1168 | /// * Otherwise, the returned expression will be produced in place of |
| 1169 | /// an ordinary member expression. |
| 1170 | /// |
| 1171 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 1172 | /// fixed for ObjC++. |
| 1173 | ExprResult |
| 1174 | Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, |
| 1175 | bool &IsArrow, SourceLocation OpLoc, |
| 1176 | CXXScopeSpec &SS, |
| 1177 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
| 1178 | assert(BaseExpr.get() && "no base expression"); |
| 1179 | |
| 1180 | // Perform default conversions. |
Richard Smith | cab9a7d | 2011-10-26 19:06:56 +0000 | [diff] [blame] | 1181 | BaseExpr = PerformMemberExprBaseConversion(BaseExpr.take(), IsArrow); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1182 | if (BaseExpr.isInvalid()) |
| 1183 | return ExprError(); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1184 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1185 | QualType BaseType = BaseExpr.get()->getType(); |
| 1186 | assert(!BaseType->isDependentType()); |
| 1187 | |
| 1188 | DeclarationName MemberName = R.getLookupName(); |
| 1189 | SourceLocation MemberLoc = R.getNameLoc(); |
| 1190 | |
| 1191 | // For later type-checking purposes, turn arrow accesses into dot |
| 1192 | // accesses. The only access type we support that doesn't follow |
| 1193 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 1194 | // and those never use arrows, so this is unaffected. |
| 1195 | if (IsArrow) { |
| 1196 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 1197 | BaseType = Ptr->getPointeeType(); |
| 1198 | else if (const ObjCObjectPointerType *Ptr |
| 1199 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 1200 | BaseType = Ptr->getPointeeType(); |
| 1201 | else if (BaseType->isRecordType()) { |
| 1202 | // Recover from arrow accesses to records, e.g.: |
| 1203 | // struct MyRecord foo; |
| 1204 | // foo->bar |
| 1205 | // This is actually well-formed in C++ if MyRecord has an |
| 1206 | // overloaded operator->, but that should have been dealt with |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 1207 | // by now--or a diagnostic message already issued if a problem |
| 1208 | // was encountered while looking for the overloaded operator->. |
Kaelyn Uhrain | bd6ddaa | 2013-10-31 20:32:56 +0000 | [diff] [blame] | 1209 | if (!getLangOpts().CPlusPlus) { |
| 1210 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 1211 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
| 1212 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 1213 | } |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1214 | IsArrow = false; |
Eli Friedman | 9a766c4 | 2012-01-13 02:20:01 +0000 | [diff] [blame] | 1215 | } else if (BaseType->isFunctionType()) { |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1216 | goto fail; |
| 1217 | } else { |
| 1218 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 1219 | << BaseType << BaseExpr.get()->getSourceRange(); |
| 1220 | return ExprError(); |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | // Handle field access to simple records. |
| 1225 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
| 1226 | if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(), |
| 1227 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 1228 | return ExprError(); |
| 1229 | |
| 1230 | // Returning valid-but-null is how we indicate to the caller that |
| 1231 | // the lookup result was filled in. |
| 1232 | return Owned((Expr*) 0); |
| 1233 | } |
| 1234 | |
| 1235 | // Handle ivar access to Objective-C objects. |
| 1236 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
Douglas Gregor | bcc9539 | 2011-10-10 16:09:49 +0000 | [diff] [blame] | 1237 | if (!SS.isEmpty() && !SS.isInvalid()) { |
Douglas Gregor | 12340e5 | 2011-10-09 23:22:49 +0000 | [diff] [blame] | 1238 | Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access) |
| 1239 | << 1 << SS.getScopeRep() |
| 1240 | << FixItHint::CreateRemoval(SS.getRange()); |
| 1241 | SS.clear(); |
| 1242 | } |
| 1243 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1244 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 1245 | |
| 1246 | // There are three cases for the base type: |
| 1247 | // - builtin id (qualified or unqualified) |
| 1248 | // - builtin Class (qualified or unqualified) |
| 1249 | // - an interface |
| 1250 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 1251 | if (!IDecl) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1252 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1253 | (OTy->isObjCId() || OTy->isObjCClass())) |
| 1254 | goto fail; |
| 1255 | // There's an implicit 'isa' ivar on all objects. |
| 1256 | // But we only actually find it this way on objects of type 'id', |
Eric Christopher | ae6b9d2 | 2012-08-16 23:50:37 +0000 | [diff] [blame] | 1257 | // apparently. |
Fariborz Jahanian | 8451074 | 2013-03-27 21:19:25 +0000 | [diff] [blame] | 1258 | if (OTy->isObjCId() && Member->isStr("isa")) |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1259 | return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc, |
Fariborz Jahanian | 06bb7f7 | 2013-03-28 19:50:55 +0000 | [diff] [blame] | 1260 | OpLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1261 | Context.getObjCClassType())); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1262 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 1263 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 1264 | ObjCImpDecl, HasTemplateArgs); |
| 1265 | goto fail; |
| 1266 | } |
Fariborz Jahanian | 25cb4ac | 2012-06-21 21:35:15 +0000 | [diff] [blame] | 1267 | |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1268 | if (RequireCompleteType(OpLoc, BaseType, diag::err_typecheck_incomplete_tag, |
| 1269 | BaseExpr.get())) |
Douglas Gregor | 5dbf4eb | 2012-01-02 17:18:37 +0000 | [diff] [blame] | 1270 | return ExprError(); |
| 1271 | |
Ted Kremenek | 679b478 | 2012-03-17 00:53:39 +0000 | [diff] [blame] | 1272 | ObjCInterfaceDecl *ClassDeclared = 0; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1273 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 1274 | |
| 1275 | if (!IV) { |
| 1276 | // Attempt to correct for typos in ivar names. |
Kaelyn Uhrain | 3658e6a | 2012-01-13 21:28:55 +0000 | [diff] [blame] | 1277 | DeclFilterCCC<ObjCIvarDecl> Validator; |
| 1278 | Validator.IsObjCIvarLookup = IsArrow; |
| 1279 | if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(), |
| 1280 | LookupMemberName, NULL, NULL, |
Kaelyn Uhrain | 4e8942c | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1281 | Validator, IDecl)) { |
Kaelyn Uhrain | 3658e6a | 2012-01-13 21:28:55 +0000 | [diff] [blame] | 1282 | IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>(); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1283 | diagnoseTypo(Corrected, |
| 1284 | PDiag(diag::err_typecheck_member_reference_ivar_suggest) |
| 1285 | << IDecl->getDeclName() << MemberName); |
| 1286 | |
Ted Kremenek | 679b478 | 2012-03-17 00:53:39 +0000 | [diff] [blame] | 1287 | // Figure out the class that declares the ivar. |
| 1288 | assert(!ClassDeclared); |
| 1289 | Decl *D = cast<Decl>(IV->getDeclContext()); |
| 1290 | if (ObjCCategoryDecl *CAT = dyn_cast<ObjCCategoryDecl>(D)) |
| 1291 | D = CAT->getClassInterface(); |
| 1292 | ClassDeclared = cast<ObjCInterfaceDecl>(D); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1293 | } else { |
Fariborz Jahanian | c297cd8 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1294 | if (IsArrow && IDecl->FindPropertyDeclaration(Member)) { |
| 1295 | Diag(MemberLoc, |
| 1296 | diag::err_property_found_suggest) |
| 1297 | << Member << BaseExpr.get()->getType() |
| 1298 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 1299 | return ExprError(); |
| 1300 | } |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1301 | |
| 1302 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 1303 | << IDecl->getDeclName() << MemberName |
| 1304 | << BaseExpr.get()->getSourceRange(); |
| 1305 | return ExprError(); |
| 1306 | } |
| 1307 | } |
Ted Kremenek | 679b478 | 2012-03-17 00:53:39 +0000 | [diff] [blame] | 1308 | |
| 1309 | assert(ClassDeclared); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1310 | |
| 1311 | // If the decl being referenced had an error, return an error for this |
| 1312 | // sub-expr without emitting another error, in order to avoid cascading |
| 1313 | // error cases. |
| 1314 | if (IV->isInvalidDecl()) |
| 1315 | return ExprError(); |
| 1316 | |
| 1317 | // Check whether we can reference this field. |
| 1318 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 1319 | return ExprError(); |
| 1320 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 1321 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 1322 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 1323 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 1324 | ClassOfMethodDecl = MD->getClassInterface(); |
| 1325 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 1326 | // Case of a c-function declared inside an objc implementation. |
| 1327 | // FIXME: For a c-style function nested inside an objc implementation |
| 1328 | // class, there is no implementation context available, so we pass |
| 1329 | // down the context as argument to this routine. Ideally, this context |
| 1330 | // need be passed down in the AST node and somehow calculated from the |
| 1331 | // AST for a function decl. |
| 1332 | if (ObjCImplementationDecl *IMPD = |
| 1333 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 1334 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 1335 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 1336 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 1337 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 1338 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1339 | if (!getLangOpts().DebuggerSupport) { |
Fariborz Jahanian | d6cb4a8 | 2012-03-07 00:58:41 +0000 | [diff] [blame] | 1340 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 1341 | if (!declaresSameEntity(ClassDeclared, IDecl) || |
| 1342 | !declaresSameEntity(ClassOfMethodDecl, ClassDeclared)) |
| 1343 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 1344 | << IV->getDeclName(); |
| 1345 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 1346 | // @protected |
| 1347 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1348 | << IV->getDeclName(); |
Fariborz Jahanian | d6cb4a8 | 2012-03-07 00:58:41 +0000 | [diff] [blame] | 1349 | } |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1350 | } |
Fariborz Jahanian | 285a7cc | 2012-08-07 23:48:10 +0000 | [diff] [blame] | 1351 | bool warn = true; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1352 | if (getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1353 | Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts(); |
| 1354 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp)) |
| 1355 | if (UO->getOpcode() == UO_Deref) |
| 1356 | BaseExp = UO->getSubExpr()->IgnoreParenCasts(); |
| 1357 | |
| 1358 | if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp)) |
Fariborz Jahanian | 285a7cc | 2012-08-07 23:48:10 +0000 | [diff] [blame] | 1359 | if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1360 | Diag(DE->getLocation(), diag::error_arc_weak_ivar_access); |
Fariborz Jahanian | 285a7cc | 2012-08-07 23:48:10 +0000 | [diff] [blame] | 1361 | warn = false; |
| 1362 | } |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1363 | } |
Fariborz Jahanian | a5063a6 | 2012-08-08 16:41:04 +0000 | [diff] [blame] | 1364 | if (warn) { |
Fariborz Jahanian | a7c9f88 | 2012-08-07 16:38:44 +0000 | [diff] [blame] | 1365 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 1366 | ObjCMethodFamily MF = MD->getMethodFamily(); |
| 1367 | warn = (MF != OMF_init && MF != OMF_dealloc && |
Fariborz Jahanian | a934a02 | 2013-02-14 19:07:19 +0000 | [diff] [blame] | 1368 | MF != OMF_finalize && |
| 1369 | !IvarBacksCurrentMethodAccessor(IDecl, MD, IV)); |
Fariborz Jahanian | a7c9f88 | 2012-08-07 16:38:44 +0000 | [diff] [blame] | 1370 | } |
| 1371 | if (warn) |
| 1372 | Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName(); |
| 1373 | } |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1374 | |
| 1375 | ObjCIvarRefExpr *Result = new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
Fariborz Jahanian | f12ff4df | 2013-04-02 18:57:54 +0000 | [diff] [blame] | 1376 | MemberLoc, OpLoc, |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1377 | BaseExpr.take(), |
| 1378 | IsArrow); |
| 1379 | |
| 1380 | if (getLangOpts().ObjCAutoRefCount) { |
| 1381 | if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 1382 | DiagnosticsEngine::Level Level = |
| 1383 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, |
| 1384 | MemberLoc); |
| 1385 | if (Level != DiagnosticsEngine::Ignored) |
Fariborz Jahanian | 6f829e3 | 2013-05-21 21:20:26 +0000 | [diff] [blame] | 1386 | recordUseOfEvaluatedWeak(Result); |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | return Owned(Result); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | // Objective-C property access. |
| 1394 | const ObjCObjectPointerType *OPT; |
| 1395 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
Douglas Gregor | bcc9539 | 2011-10-10 16:09:49 +0000 | [diff] [blame] | 1396 | if (!SS.isEmpty() && !SS.isInvalid()) { |
Douglas Gregor | 12340e5 | 2011-10-09 23:22:49 +0000 | [diff] [blame] | 1397 | Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access) |
| 1398 | << 0 << SS.getScopeRep() |
| 1399 | << FixItHint::CreateRemoval(SS.getRange()); |
| 1400 | SS.clear(); |
| 1401 | } |
| 1402 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1403 | // This actually uses the base as an r-value. |
| 1404 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 1405 | if (BaseExpr.isInvalid()) |
| 1406 | return ExprError(); |
| 1407 | |
| 1408 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType())); |
| 1409 | |
| 1410 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 1411 | |
| 1412 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 1413 | |
| 1414 | // id, with and without qualifiers. |
| 1415 | if (OT->isObjCId()) { |
| 1416 | // Check protocols on qualified interfaces. |
| 1417 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 1418 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 1419 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 1420 | // Check the use of this declaration |
| 1421 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1422 | return ExprError(); |
| 1423 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1424 | return Owned(new (Context) ObjCPropertyRefExpr(PD, |
| 1425 | Context.PseudoObjectTy, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1426 | VK_LValue, |
| 1427 | OK_ObjCProperty, |
| 1428 | MemberLoc, |
| 1429 | BaseExpr.take())); |
| 1430 | } |
| 1431 | |
| 1432 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 1433 | // Check the use of this method. |
| 1434 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 1435 | return ExprError(); |
| 1436 | Selector SetterSel = |
Adrian Prantl | a4ce906 | 2013-06-07 22:29:12 +0000 | [diff] [blame] | 1437 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 1438 | PP.getSelectorTable(), |
| 1439 | Member); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1440 | ObjCMethodDecl *SMD = 0; |
| 1441 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 1442 | SetterSel, Context)) |
| 1443 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1444 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1445 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, |
| 1446 | Context.PseudoObjectTy, |
| 1447 | VK_LValue, OK_ObjCProperty, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1448 | MemberLoc, BaseExpr.take())); |
| 1449 | } |
| 1450 | } |
| 1451 | // Use of id.member can only be for a property reference. Do not |
| 1452 | // use the 'id' redefinition in this case. |
| 1453 | if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 1454 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 1455 | ObjCImpDecl, HasTemplateArgs); |
| 1456 | |
| 1457 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1458 | << MemberName << BaseType); |
| 1459 | } |
| 1460 | |
| 1461 | // 'Class', unqualified only. |
| 1462 | if (OT->isObjCClass()) { |
| 1463 | // Only works in a method declaration (??!). |
| 1464 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 1465 | if (!MD) { |
| 1466 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 1467 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 1468 | ObjCImpDecl, HasTemplateArgs); |
| 1469 | |
| 1470 | goto fail; |
| 1471 | } |
| 1472 | |
| 1473 | // Also must look for a getter name which uses property syntax. |
| 1474 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 1475 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 1476 | ObjCMethodDecl *Getter; |
| 1477 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 1478 | // Check the use of this method. |
| 1479 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1480 | return ExprError(); |
| 1481 | } else |
| 1482 | Getter = IFace->lookupPrivateMethod(Sel, false); |
| 1483 | // If we found a getter then this may be a valid dot-reference, we |
| 1484 | // will look for the matching setter, in case it is needed. |
| 1485 | Selector SetterSel = |
Adrian Prantl | a4ce906 | 2013-06-07 22:29:12 +0000 | [diff] [blame] | 1486 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 1487 | PP.getSelectorTable(), |
| 1488 | Member); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1489 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 1490 | if (!Setter) { |
| 1491 | // If this reference is in an @implementation, also check for 'private' |
| 1492 | // methods. |
| 1493 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
| 1494 | } |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1495 | |
| 1496 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1497 | return ExprError(); |
| 1498 | |
| 1499 | if (Getter || Setter) { |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1500 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1501 | Context.PseudoObjectTy, |
| 1502 | VK_LValue, OK_ObjCProperty, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1503 | MemberLoc, BaseExpr.take())); |
| 1504 | } |
| 1505 | |
| 1506 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 1507 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 1508 | ObjCImpDecl, HasTemplateArgs); |
| 1509 | |
| 1510 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1511 | << MemberName << BaseType); |
| 1512 | } |
| 1513 | |
| 1514 | // Normal property access. |
Fariborz Jahanian | c297cd8 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1515 | return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc, |
| 1516 | MemberName, MemberLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1517 | SourceLocation(), QualType(), false); |
| 1518 | } |
| 1519 | |
| 1520 | // Handle 'field access' to vectors, such as 'V.xx'. |
| 1521 | if (BaseType->isExtVectorType()) { |
| 1522 | // FIXME: this expr should store IsArrow. |
| 1523 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 1524 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind()); |
| 1525 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 1526 | Member, MemberLoc); |
| 1527 | if (ret.isNull()) |
| 1528 | return ExprError(); |
| 1529 | |
| 1530 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(), |
| 1531 | *Member, MemberLoc)); |
| 1532 | } |
| 1533 | |
| 1534 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 1535 | // not just a pointer to builtin-sel again. |
| 1536 | if (IsArrow && |
| 1537 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1538 | !Context.getObjCSelRedefinitionType()->isObjCSelType()) { |
| 1539 | BaseExpr = ImpCastExprToType(BaseExpr.take(), |
| 1540 | Context.getObjCSelRedefinitionType(), |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1541 | CK_BitCast); |
| 1542 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 1543 | ObjCImpDecl, HasTemplateArgs); |
| 1544 | } |
| 1545 | |
| 1546 | // Failure cases. |
| 1547 | fail: |
| 1548 | |
| 1549 | // Recover from dot accesses to pointers, e.g.: |
| 1550 | // type *foo; |
| 1551 | // foo.bar |
| 1552 | // This is actually well-formed in two cases: |
| 1553 | // - 'type' is an Objective C type |
| 1554 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 1555 | // the appropriate pointer type |
| 1556 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 1557 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 1558 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| 1559 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 1560 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
| 1561 | << FixItHint::CreateReplacement(OpLoc, "->"); |
| 1562 | |
| 1563 | // Recurse as an -> access. |
| 1564 | IsArrow = true; |
| 1565 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 1566 | ObjCImpDecl, HasTemplateArgs); |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | // If the user is trying to apply -> or . to a function name, it's probably |
| 1571 | // because they forgot parentheses to call that function. |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1572 | if (tryToRecoverWithCall(BaseExpr, |
| 1573 | PDiag(diag::err_member_reference_needs_call), |
| 1574 | /*complain*/ false, |
Eli Friedman | 9a766c4 | 2012-01-13 02:20:01 +0000 | [diff] [blame] | 1575 | IsArrow ? &isPointerToRecordType : &isRecordType)) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1576 | if (BaseExpr.isInvalid()) |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1577 | return ExprError(); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1578 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
| 1579 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 1580 | ObjCImpDecl, HasTemplateArgs); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Matt Beaumont-Gay | d9f244af | 2012-04-21 01:12:48 +0000 | [diff] [blame] | 1583 | Diag(OpLoc, diag::err_typecheck_member_reference_struct_union) |
Matt Beaumont-Gay | 025321b | 2012-04-21 02:13:04 +0000 | [diff] [blame] | 1584 | << BaseType << BaseExpr.get()->getSourceRange() << MemberLoc; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1585 | |
| 1586 | return ExprError(); |
| 1587 | } |
| 1588 | |
| 1589 | /// The main callback when the parser finds something like |
| 1590 | /// expression . [nested-name-specifier] identifier |
| 1591 | /// expression -> [nested-name-specifier] identifier |
| 1592 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 1593 | /// possibilities, including destructor and operator references. |
| 1594 | /// |
| 1595 | /// \param OpKind either tok::arrow or tok::period |
| 1596 | /// \param HasTrailingLParen whether the next token is '(', which |
| 1597 | /// is used to diagnose mis-uses of special members that can |
| 1598 | /// only be called |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1599 | /// \param ObjCImpDecl the current Objective-C \@implementation |
| 1600 | /// decl; this is an ugly hack around the fact that Objective-C |
| 1601 | /// \@implementations aren't properly put in the context chain |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1602 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
| 1603 | SourceLocation OpLoc, |
| 1604 | tok::TokenKind OpKind, |
| 1605 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1606 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1607 | UnqualifiedId &Id, |
| 1608 | Decl *ObjCImpDecl, |
| 1609 | bool HasTrailingLParen) { |
| 1610 | if (SS.isSet() && SS.isInvalid()) |
| 1611 | return ExprError(); |
| 1612 | |
| 1613 | // Warn about the explicit constructor calls Microsoft extension. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1614 | if (getLangOpts().MicrosoftExt && |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1615 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 1616 | Diag(Id.getSourceRange().getBegin(), |
| 1617 | diag::ext_ms_explicit_constructor_call); |
| 1618 | |
| 1619 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 1620 | |
| 1621 | // Decompose the name into its component parts. |
| 1622 | DeclarationNameInfo NameInfo; |
| 1623 | const TemplateArgumentListInfo *TemplateArgs; |
| 1624 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, |
| 1625 | NameInfo, TemplateArgs); |
| 1626 | |
| 1627 | DeclarationName Name = NameInfo.getName(); |
| 1628 | bool IsArrow = (OpKind == tok::arrow); |
| 1629 | |
| 1630 | NamedDecl *FirstQualifierInScope |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 1631 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, SS.getScopeRep())); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1632 | |
| 1633 | // This is a postfix expression, so get rid of ParenListExprs. |
| 1634 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
| 1635 | if (Result.isInvalid()) return ExprError(); |
| 1636 | Base = Result.take(); |
| 1637 | |
| 1638 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 1639 | isDependentScopeSpecifier(SS)) { |
| 1640 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
| 1641 | IsArrow, OpLoc, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1642 | SS, TemplateKWLoc, FirstQualifierInScope, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1643 | NameInfo, TemplateArgs); |
| 1644 | } else { |
| 1645 | LookupResult R(*this, NameInfo, LookupMemberName); |
| 1646 | ExprResult BaseResult = Owned(Base); |
| 1647 | Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
| 1648 | SS, ObjCImpDecl, TemplateArgs != 0); |
| 1649 | if (BaseResult.isInvalid()) |
| 1650 | return ExprError(); |
| 1651 | Base = BaseResult.take(); |
| 1652 | |
| 1653 | if (Result.isInvalid()) { |
| 1654 | Owned(Base); |
| 1655 | return ExprError(); |
| 1656 | } |
| 1657 | |
| 1658 | if (Result.get()) { |
| 1659 | // The only way a reference to a destructor can be used is to |
| 1660 | // immediately call it, which falls into this case. If the |
| 1661 | // next token is not a '(', produce a diagnostic and build the |
| 1662 | // call now. |
| 1663 | if (!HasTrailingLParen && |
| 1664 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
| 1665 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
| 1666 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1667 | return Result; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Kaelyn Uhrain | 76e0734 | 2012-04-25 19:49:54 +0000 | [diff] [blame] | 1670 | ActOnMemberAccessExtraArgs ExtraArgs = {S, Id, ObjCImpDecl, HasTrailingLParen}; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1671 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1672 | OpLoc, IsArrow, SS, TemplateKWLoc, |
Kaelyn Uhrain | 76e0734 | 2012-04-25 19:49:54 +0000 | [diff] [blame] | 1673 | FirstQualifierInScope, R, TemplateArgs, |
| 1674 | false, &ExtraArgs); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1677 | return Result; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | static ExprResult |
| 1681 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 1682 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 1683 | DeclAccessPair FoundDecl, |
| 1684 | const DeclarationNameInfo &MemberNameInfo) { |
| 1685 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 1686 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 1687 | // that *x is always an l-value), except that if the base isn't |
| 1688 | // an ordinary object then we must have an rvalue. |
| 1689 | ExprValueKind VK = VK_LValue; |
| 1690 | ExprObjectKind OK = OK_Ordinary; |
| 1691 | if (!IsArrow) { |
| 1692 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 1693 | VK = BaseExpr->getValueKind(); |
| 1694 | else |
| 1695 | VK = VK_RValue; |
| 1696 | } |
| 1697 | if (VK != VK_RValue && Field->isBitField()) |
| 1698 | OK = OK_BitField; |
| 1699 | |
| 1700 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 1701 | QualType MemberType = Field->getType(); |
| 1702 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 1703 | MemberType = Ref->getPointeeType(); |
| 1704 | VK = VK_LValue; |
| 1705 | } else { |
| 1706 | QualType BaseType = BaseExpr->getType(); |
| 1707 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 1708 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1709 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 1710 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1711 | // GC attributes are never picked up by members. |
| 1712 | BaseQuals.removeObjCGCAttr(); |
Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 1713 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1714 | // CVR attributes from the base are picked up by members, |
| 1715 | // except that 'mutable' members don't pick up 'const'. |
| 1716 | if (Field->isMutable()) BaseQuals.removeConst(); |
Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1718 | Qualifiers MemberQuals |
| 1719 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 1720 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1721 | assert(!MemberQuals.hasAddressSpace()); |
Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 1722 | |
| 1723 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1724 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 1725 | if (Combined != MemberQuals) |
| 1726 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 1727 | } |
Matt Arsenault | 376f720 | 2013-02-26 21:16:00 +0000 | [diff] [blame] | 1728 | |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1729 | S.UnusedPrivateFields.remove(Field); |
| 1730 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1731 | ExprResult Base = |
| 1732 | S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 1733 | FoundDecl, Field); |
| 1734 | if (Base.isInvalid()) |
| 1735 | return ExprError(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1736 | return S.Owned(BuildMemberExpr(S, S.Context, Base.take(), IsArrow, SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1737 | /*TemplateKWLoc=*/SourceLocation(), |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1738 | Field, FoundDecl, MemberNameInfo, |
| 1739 | MemberType, VK, OK)); |
| 1740 | } |
| 1741 | |
| 1742 | /// Builds an implicit member access expression. The current context |
| 1743 | /// is known to be an instance method, and the given unqualified lookup |
| 1744 | /// set is known to contain only instance members, at least one of which |
| 1745 | /// is from an appropriate type. |
| 1746 | ExprResult |
| 1747 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1748 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1749 | LookupResult &R, |
| 1750 | const TemplateArgumentListInfo *TemplateArgs, |
| 1751 | bool IsKnownInstance) { |
| 1752 | assert(!R.empty() && !R.isAmbiguous()); |
| 1753 | |
| 1754 | SourceLocation loc = R.getNameLoc(); |
Richard Smith | 59d26d2 | 2014-01-17 22:29:43 +0000 | [diff] [blame^] | 1755 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1756 | // If this is known to be an instance access, go ahead and build an |
| 1757 | // implicit 'this' expression now. |
| 1758 | // 'this' expression now. |
Douglas Gregor | 09deffa | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 1759 | QualType ThisTy = getCurrentThisType(); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1760 | assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'"); |
| 1761 | |
| 1762 | Expr *baseExpr = 0; // null signifies implicit access |
| 1763 | if (IsKnownInstance) { |
| 1764 | SourceLocation Loc = R.getNameLoc(); |
| 1765 | if (SS.getRange().isValid()) |
| 1766 | Loc = SS.getRange().getBegin(); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1767 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1768 | baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true); |
| 1769 | } |
| 1770 | |
| 1771 | return BuildMemberReferenceExpr(baseExpr, ThisTy, |
| 1772 | /*OpLoc*/ SourceLocation(), |
| 1773 | /*IsArrow*/ true, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1774 | SS, TemplateKWLoc, |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1775 | /*FirstQualifierInScope*/ 0, |
| 1776 | R, TemplateArgs); |
| 1777 | } |