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