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