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