Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1 | //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
James Dennett | 84053fb | 2012-06-22 05:14:59 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11 | /// Implements semantic analysis for C++ expressions. |
James Dennett | 84053fb | 2012-06-22 05:14:59 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 14 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 16 | #include "TreeTransform.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "TypeLocBuilder.h" |
Steve Naroff | aac9415 | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Faisal Vali | 47d9ed4 | 2014-05-30 04:39:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTLambda.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Fariborz Jahanian | 1d44608 | 2010-06-16 18:56:04 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 25 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Akira Hatanaka | 3e40c30 | 2017-07-19 17:17:50 +0000 | [diff] [blame] | 27 | #include "clang/Basic/AlignedAllocation.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 30 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Sema/DeclSpec.h" |
| 32 | #include "clang/Sema/Initialization.h" |
| 33 | #include "clang/Sema/Lookup.h" |
| 34 | #include "clang/Sema/ParsedTemplate.h" |
| 35 | #include "clang/Sema/Scope.h" |
| 36 | #include "clang/Sema/ScopeInfo.h" |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 37 | #include "clang/Sema/SemaLambda.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 38 | #include "clang/Sema/TemplateDeduction.h" |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/APInt.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8b0cf1d | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 42 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 43 | using namespace sema; |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 44 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 45 | /// Handle the result of the special case name lookup for inheriting |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 46 | /// constructor declarations. 'NS::X::X' and 'NS::X<...>::X' are treated as |
| 47 | /// constructor names in member using declarations, even if 'X' is not the |
| 48 | /// name of the corresponding type. |
| 49 | ParsedType Sema::getInheritingConstructorName(CXXScopeSpec &SS, |
| 50 | SourceLocation NameLoc, |
| 51 | IdentifierInfo &Name) { |
| 52 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
| 53 | |
| 54 | // Convert the nested-name-specifier into a type. |
| 55 | QualType Type; |
| 56 | switch (NNS->getKind()) { |
| 57 | case NestedNameSpecifier::TypeSpec: |
| 58 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 59 | Type = QualType(NNS->getAsType(), 0); |
| 60 | break; |
| 61 | |
| 62 | case NestedNameSpecifier::Identifier: |
| 63 | // Strip off the last layer of the nested-name-specifier and build a |
| 64 | // typename type for it. |
| 65 | assert(NNS->getAsIdentifier() == &Name && "not a constructor name"); |
| 66 | Type = Context.getDependentNameType(ETK_None, NNS->getPrefix(), |
| 67 | NNS->getAsIdentifier()); |
| 68 | break; |
| 69 | |
| 70 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 71 | case NestedNameSpecifier::Super: |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 72 | case NestedNameSpecifier::Namespace: |
| 73 | case NestedNameSpecifier::NamespaceAlias: |
| 74 | llvm_unreachable("Nested name specifier is not a type for inheriting ctor"); |
| 75 | } |
| 76 | |
| 77 | // This reference to the type is located entirely at the location of the |
| 78 | // final identifier in the qualified-id. |
| 79 | return CreateParsedType(Type, |
| 80 | Context.getTrivialTypeSourceInfo(Type, NameLoc)); |
| 81 | } |
| 82 | |
Richard Smith | 715ee07 | 2018-06-20 21:58:20 +0000 | [diff] [blame] | 83 | ParsedType Sema::getConstructorName(IdentifierInfo &II, |
| 84 | SourceLocation NameLoc, |
Richard Smith | 69bc9aa | 2018-06-22 19:50:19 +0000 | [diff] [blame] | 85 | Scope *S, CXXScopeSpec &SS, |
| 86 | bool EnteringContext) { |
Richard Smith | 715ee07 | 2018-06-20 21:58:20 +0000 | [diff] [blame] | 87 | CXXRecordDecl *CurClass = getCurrentClass(S, &SS); |
| 88 | assert(CurClass && &II == CurClass->getIdentifier() && |
| 89 | "not a constructor name"); |
| 90 | |
Richard Smith | 69bc9aa | 2018-06-22 19:50:19 +0000 | [diff] [blame] | 91 | // When naming a constructor as a member of a dependent context (eg, in a |
| 92 | // friend declaration or an inherited constructor declaration), form an |
| 93 | // unresolved "typename" type. |
| 94 | if (CurClass->isDependentContext() && !EnteringContext) { |
| 95 | QualType T = Context.getDependentNameType(ETK_None, SS.getScopeRep(), &II); |
| 96 | return ParsedType::make(T); |
| 97 | } |
| 98 | |
Richard Smith | 715ee07 | 2018-06-20 21:58:20 +0000 | [diff] [blame] | 99 | if (SS.isNotEmpty() && RequireCompleteDeclContext(SS, CurClass)) |
| 100 | return ParsedType(); |
| 101 | |
| 102 | // Find the injected-class-name declaration. Note that we make no attempt to |
| 103 | // diagnose cases where the injected-class-name is shadowed: the only |
| 104 | // declaration that can validly shadow the injected-class-name is a |
| 105 | // non-static data member, and if the class contains both a non-static data |
| 106 | // member and a constructor then it is ill-formed (we check that in |
| 107 | // CheckCompletedCXXClass). |
| 108 | CXXRecordDecl *InjectedClassName = nullptr; |
| 109 | for (NamedDecl *ND : CurClass->lookup(&II)) { |
| 110 | auto *RD = dyn_cast<CXXRecordDecl>(ND); |
| 111 | if (RD && RD->isInjectedClassName()) { |
| 112 | InjectedClassName = RD; |
| 113 | break; |
| 114 | } |
| 115 | } |
Ilya Biryukov | a2d5825 | 2018-07-04 08:50:12 +0000 | [diff] [blame] | 116 | if (!InjectedClassName && CurClass->isInvalidDecl()) |
| 117 | return ParsedType(); |
Richard Smith | 715ee07 | 2018-06-20 21:58:20 +0000 | [diff] [blame] | 118 | assert(InjectedClassName && "couldn't find injected class name"); |
| 119 | |
| 120 | QualType T = Context.getTypeDeclType(InjectedClassName); |
| 121 | DiagnoseUseOfDecl(InjectedClassName, NameLoc); |
| 122 | MarkAnyDeclReferenced(NameLoc, InjectedClassName, /*OdrUse=*/false); |
| 123 | |
| 124 | return ParsedType::make(T); |
| 125 | } |
| 126 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 127 | ParsedType Sema::getDestructorName(SourceLocation TildeLoc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 128 | IdentifierInfo &II, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 129 | SourceLocation NameLoc, |
| 130 | Scope *S, CXXScopeSpec &SS, |
| 131 | ParsedType ObjectTypePtr, |
| 132 | bool EnteringContext) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 133 | // Determine where to perform name lookup. |
| 134 | |
| 135 | // FIXME: This area of the standard is very messy, and the current |
| 136 | // wording is rather unclear about which scopes we search for the |
| 137 | // destructor name; see core issues 399 and 555. Issue 399 in |
| 138 | // particular shows where the current description of destructor name |
| 139 | // lookup is completely out of line with existing practice, e.g., |
| 140 | // this appears to be ill-formed: |
| 141 | // |
| 142 | // namespace N { |
| 143 | // template <typename T> struct S { |
| 144 | // ~S(); |
| 145 | // }; |
| 146 | // } |
| 147 | // |
| 148 | // void f(N::S<int>* s) { |
| 149 | // s->N::S<int>::~S(); |
| 150 | // } |
| 151 | // |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 152 | // See also PR6358 and PR6359. |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 153 | // For this reason, we're currently only doing the C++03 version of this |
| 154 | // code; the C++0x version has to wait until we get a proper spec. |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 155 | QualType SearchType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 156 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 157 | bool isDependent = false; |
| 158 | bool LookInScope = false; |
| 159 | |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 160 | if (SS.isInvalid()) |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 161 | return nullptr; |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 162 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 163 | // If we have an object type, it's because we are in a |
| 164 | // pseudo-destructor-expression or a member access expression, and |
| 165 | // we know what type we're looking for. |
| 166 | if (ObjectTypePtr) |
| 167 | SearchType = GetTypeFromParser(ObjectTypePtr); |
| 168 | |
| 169 | if (SS.isSet()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 170 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 171 | |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 172 | bool AlreadySearched = false; |
| 173 | bool LookAtPrefix = true; |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 174 | // C++11 [basic.lookup.qual]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 175 | // If a pseudo-destructor-name (5.2.4) contains a nested-name-specifier, |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 176 | // the type-names are looked up as types in the scope designated by the |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 177 | // nested-name-specifier. Similarly, in a qualified-id of the form: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 178 | // |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 179 | // nested-name-specifier[opt] class-name :: ~ class-name |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 180 | // |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 181 | // the second class-name is looked up in the same scope as the first. |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 182 | // |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 183 | // Here, we determine whether the code below is permitted to look at the |
| 184 | // prefix of the nested-name-specifier. |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 185 | DeclContext *DC = computeDeclContext(SS, EnteringContext); |
| 186 | if (DC && DC->isFileContext()) { |
| 187 | AlreadySearched = true; |
| 188 | LookupCtx = DC; |
| 189 | isDependent = false; |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 190 | } else if (DC && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 191 | LookAtPrefix = false; |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 192 | LookInScope = true; |
| 193 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 194 | |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 195 | // The second case from the C++03 rules quoted further above. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 196 | NestedNameSpecifier *Prefix = nullptr; |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 197 | if (AlreadySearched) { |
| 198 | // Nothing left to do. |
| 199 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { |
| 200 | CXXScopeSpec PrefixSS; |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 201 | PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data())); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 202 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); |
| 203 | isDependent = isDependentScopeSpecifier(PrefixSS); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 204 | } else if (ObjectTypePtr) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 205 | LookupCtx = computeDeclContext(SearchType); |
| 206 | isDependent = SearchType->isDependentType(); |
| 207 | } else { |
| 208 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 209 | isDependent = LookupCtx && LookupCtx->isDependentContext(); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 210 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 211 | } else if (ObjectTypePtr) { |
| 212 | // C++ [basic.lookup.classref]p3: |
| 213 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 214 | // in the context of the entire postfix-expression. If the type T |
| 215 | // of the object expression is of a class type C, the type-name is |
| 216 | // also looked up in the scope of class C. At least one of the |
| 217 | // lookups shall find a name that refers to (possibly |
| 218 | // cv-qualified) T. |
| 219 | LookupCtx = computeDeclContext(SearchType); |
| 220 | isDependent = SearchType->isDependentType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 221 | assert((isDependent || !SearchType->isIncompleteType()) && |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 222 | "Caller should have completed object type"); |
| 223 | |
| 224 | LookInScope = true; |
| 225 | } else { |
| 226 | // Perform lookup into the current scope (only). |
| 227 | LookInScope = true; |
| 228 | } |
| 229 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 230 | TypeDecl *NonMatchingTypeDecl = nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 231 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); |
| 232 | for (unsigned Step = 0; Step != 2; ++Step) { |
| 233 | // Look for the name first in the computed lookup context (if we |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 234 | // have one) and, if that fails to find a match, in the scope (if |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 235 | // we're allowed to look there). |
| 236 | Found.clear(); |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame] | 237 | if (Step == 0 && LookupCtx) { |
| 238 | if (RequireCompleteDeclContext(SS, LookupCtx)) |
| 239 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 240 | LookupQualifiedName(Found, LookupCtx); |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame] | 241 | } else if (Step == 1 && LookInScope && S) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 242 | LookupName(Found, S); |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame] | 243 | } else { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 244 | continue; |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame] | 245 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 246 | |
| 247 | // FIXME: Should we be suppressing ambiguities here? |
| 248 | if (Found.isAmbiguous()) |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 249 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 250 | |
| 251 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
| 252 | QualType T = Context.getTypeDeclType(Type); |
Nico Weber | 83a6387 | 2014-11-12 04:33:52 +0000 | [diff] [blame] | 253 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 254 | |
| 255 | if (SearchType.isNull() || SearchType->isDependentType() || |
| 256 | Context.hasSameUnqualifiedType(T, SearchType)) { |
| 257 | // We found our type! |
| 258 | |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 259 | return CreateParsedType(T, |
| 260 | Context.getTrivialTypeSourceInfo(T, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 261 | } |
John Wiegley | b4a9e51 | 2011-03-08 08:13:22 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 263 | if (!SearchType.isNull()) |
| 264 | NonMatchingTypeDecl = Type; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // If the name that we found is a class template name, and it is |
| 268 | // the same name as the template name in the last part of the |
| 269 | // nested-name-specifier (if present) or the object type, then |
| 270 | // this is the destructor for that class. |
| 271 | // FIXME: This is a workaround until we get real drafting for core |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 272 | // issue 399, for which there isn't even an obvious direction. |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 273 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
| 274 | QualType MemberOfType; |
| 275 | if (SS.isSet()) { |
| 276 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
| 277 | // Figure out the type of the context, if it has one. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 278 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) |
| 279 | MemberOfType = Context.getTypeDeclType(Record); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | if (MemberOfType.isNull()) |
| 283 | MemberOfType = SearchType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 285 | if (MemberOfType.isNull()) |
| 286 | continue; |
| 287 | |
| 288 | // We're referring into a class template specialization. If the |
| 289 | // class template we found is the same as the template being |
| 290 | // specialized, we found what we are looking for. |
| 291 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
| 292 | if (ClassTemplateSpecializationDecl *Spec |
| 293 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
| 294 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
| 295 | Template->getCanonicalDecl()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 296 | return CreateParsedType( |
| 297 | MemberOfType, |
| 298 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | continue; |
| 302 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 303 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 304 | // We're referring to an unresolved class template |
| 305 | // specialization. Determine whether we class template we found |
| 306 | // is the same as the template being specialized or, if we don't |
| 307 | // know which template is being specialized, that it at least |
| 308 | // has the same name. |
| 309 | if (const TemplateSpecializationType *SpecType |
| 310 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
| 311 | TemplateName SpecName = SpecType->getTemplateName(); |
| 312 | |
| 313 | // The class template we found is the same template being |
| 314 | // specialized. |
| 315 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
| 316 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 317 | return CreateParsedType( |
| 318 | MemberOfType, |
| 319 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 320 | |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | // The class template we found has the same name as the |
| 325 | // (dependent) template name being specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 326 | if (DependentTemplateName *DepTemplate |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 327 | = SpecName.getAsDependentTemplateName()) { |
| 328 | if (DepTemplate->isIdentifier() && |
| 329 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 330 | return CreateParsedType( |
| 331 | MemberOfType, |
| 332 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 333 | |
| 334 | continue; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if (isDependent) { |
| 341 | // We didn't find our type, but that's okay: it's dependent |
| 342 | // anyway. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 343 | |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 344 | // FIXME: What if we have no nested-name-specifier? |
| 345 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), |
| 346 | SS.getWithLocInContext(Context), |
| 347 | II, NameLoc); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 348 | return ParsedType::make(T); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 351 | if (NonMatchingTypeDecl) { |
| 352 | QualType T = Context.getTypeDeclType(NonMatchingTypeDecl); |
| 353 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) |
| 354 | << T << SearchType; |
| 355 | Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here) |
| 356 | << T; |
| 357 | } else if (ObjectTypePtr) |
| 358 | Diag(NameLoc, diag::err_ident_in_dtor_not_a_type) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 359 | << &II; |
David Blaikie | 5e026f5 | 2013-03-20 17:42:13 +0000 | [diff] [blame] | 360 | else { |
| 361 | SemaDiagnosticBuilder DtorDiag = Diag(NameLoc, |
| 362 | diag::err_destructor_class_name); |
| 363 | if (S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 364 | const DeclContext *Ctx = S->getEntity(); |
David Blaikie | 5e026f5 | 2013-03-20 17:42:13 +0000 | [diff] [blame] | 365 | if (const CXXRecordDecl *Class = dyn_cast_or_null<CXXRecordDecl>(Ctx)) |
| 366 | DtorDiag << FixItHint::CreateReplacement(SourceRange(NameLoc), |
| 367 | Class->getNameAsString()); |
| 368 | } |
| 369 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 370 | |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 371 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 374 | ParsedType Sema::getDestructorTypeForDecltype(const DeclSpec &DS, |
| 375 | ParsedType ObjectType) { |
| 376 | if (DS.getTypeSpecType() == DeclSpec::TST_error) |
| 377 | return nullptr; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 378 | |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 379 | if (DS.getTypeSpecType() == DeclSpec::TST_decltype_auto) { |
| 380 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decltype_auto_invalid); |
| 381 | return nullptr; |
| 382 | } |
| 383 | |
| 384 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype && |
| 385 | "unexpected type in getDestructorType"); |
| 386 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 387 | |
| 388 | // If we know the type of the object, check that the correct destructor |
| 389 | // type was named now; we can give better diagnostics this way. |
| 390 | QualType SearchType = GetTypeFromParser(ObjectType); |
| 391 | if (!SearchType.isNull() && !SearchType->isDependentType() && |
| 392 | !Context.hasSameUnqualifiedType(T, SearchType)) { |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 393 | Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch) |
| 394 | << T << SearchType; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 395 | return nullptr; |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | return ParsedType::make(T); |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 401 | bool Sema::checkLiteralOperatorId(const CXXScopeSpec &SS, |
| 402 | const UnqualifiedId &Name) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 403 | assert(Name.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId); |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 404 | |
| 405 | if (!SS.isValid()) |
| 406 | return false; |
| 407 | |
| 408 | switch (SS.getScopeRep()->getKind()) { |
| 409 | case NestedNameSpecifier::Identifier: |
| 410 | case NestedNameSpecifier::TypeSpec: |
| 411 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 412 | // Per C++11 [over.literal]p2, literal operators can only be declared at |
| 413 | // namespace scope. Therefore, this unqualified-id cannot name anything. |
| 414 | // Reject it early, because we have no AST representation for this in the |
| 415 | // case where the scope is dependent. |
| 416 | Diag(Name.getLocStart(), diag::err_literal_operator_id_outside_namespace) |
| 417 | << SS.getScopeRep(); |
| 418 | return true; |
| 419 | |
| 420 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 421 | case NestedNameSpecifier::Super: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 422 | case NestedNameSpecifier::Namespace: |
| 423 | case NestedNameSpecifier::NamespaceAlias: |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | llvm_unreachable("unknown nested name specifier kind"); |
| 428 | } |
| 429 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 430 | /// Build a C++ typeid expression with a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 431 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 432 | SourceLocation TypeidLoc, |
| 433 | TypeSourceInfo *Operand, |
| 434 | SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 435 | // C++ [expr.typeid]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 436 | // The top-level cv-qualifiers of the lvalue expression or the type-id |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 437 | // that is the operand of typeid are always ignored. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 438 | // If the type of the type-id is a class type or a reference to a class |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 439 | // type, the class shall be completely-defined. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 440 | Qualifiers Quals; |
| 441 | QualType T |
| 442 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
| 443 | Quals); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 444 | if (T->getAs<RecordType>() && |
| 445 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 446 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 447 | |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 448 | if (T->isVariablyModifiedType()) |
| 449 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) << T); |
| 450 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 451 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), Operand, |
| 452 | SourceRange(TypeidLoc, RParenLoc)); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 455 | /// Build a C++ typeid expression with an expression operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 456 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 457 | SourceLocation TypeidLoc, |
| 458 | Expr *E, |
| 459 | SourceLocation RParenLoc) { |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 460 | bool WasEvaluated = false; |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 461 | if (E && !E->isTypeDependent()) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 462 | if (E->getType()->isPlaceholderType()) { |
| 463 | ExprResult result = CheckPlaceholderExpr(E); |
| 464 | if (result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 465 | E = result.get(); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 468 | QualType T = E->getType(); |
| 469 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
| 470 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 471 | // C++ [expr.typeid]p3: |
| 472 | // [...] If the type of the expression is a class type, the class |
| 473 | // shall be completely-defined. |
| 474 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 475 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 476 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 477 | // C++ [expr.typeid]p3: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 478 | // When typeid is applied to an expression other than an glvalue of a |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 479 | // polymorphic class type [...] [the] expression is an unevaluated |
| 480 | // operand. [...] |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 481 | if (RecordD->isPolymorphic() && E->isGLValue()) { |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 482 | // The subexpression is potentially evaluated; switch the context |
| 483 | // and recheck the subexpression. |
Benjamin Kramer | d81108f | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 484 | ExprResult Result = TransformToPotentiallyEvaluated(E); |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 485 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 486 | E = Result.get(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 487 | |
| 488 | // We require a vtable to query the type at run time. |
| 489 | MarkVTableUsed(TypeidLoc, RecordD); |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 490 | WasEvaluated = true; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 491 | } |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 492 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 494 | // C++ [expr.typeid]p4: |
| 495 | // [...] If the type of the type-id is a reference to a possibly |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 496 | // cv-qualified type, the result of the typeid expression refers to a |
| 497 | // std::type_info object representing the cv-unqualified referenced |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 498 | // type. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 499 | Qualifiers Quals; |
| 500 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
| 501 | if (!Context.hasSameType(T, UnqualT)) { |
| 502 | T = UnqualT; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 503 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).get(); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 506 | |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 507 | if (E->getType()->isVariablyModifiedType()) |
| 508 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) |
| 509 | << E->getType()); |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 510 | else if (!inTemplateInstantiation() && |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 511 | E->HasSideEffects(Context, WasEvaluated)) { |
| 512 | // The expression operand for typeid is in an unevaluated expression |
| 513 | // context, so side effects could result in unintended consequences. |
| 514 | Diag(E->getExprLoc(), WasEvaluated |
| 515 | ? diag::warn_side_effects_typeid |
| 516 | : diag::warn_side_effects_unevaluated_context); |
| 517 | } |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 518 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 519 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), E, |
| 520 | SourceRange(TypeidLoc, RParenLoc)); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 524 | ExprResult |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 525 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 526 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Sven van Haastregt | 2ca6ba1 | 2018-05-09 13:16:17 +0000 | [diff] [blame] | 527 | // OpenCL C++ 1.0 s2.9: typeid is not supported. |
| 528 | if (getLangOpts().OpenCLCPlusPlus) { |
| 529 | return ExprError(Diag(OpLoc, diag::err_openclcxx_not_supported) |
| 530 | << "typeid"); |
| 531 | } |
| 532 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 533 | // Find the std::type_info type. |
Sebastian Redl | 7ac9741 | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 534 | if (!getStdNamespace()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 535 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 536 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 537 | if (!CXXTypeInfoDecl) { |
| 538 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 539 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 540 | LookupQualifiedName(R, getStdNamespace()); |
| 541 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
Nico Weber | 5f96883 | 2012-06-19 23:58:27 +0000 | [diff] [blame] | 542 | // Microsoft's typeinfo doesn't have type_info in std but in the global |
| 543 | // namespace if _HAS_EXCEPTIONS is defined to 0. See PR13153. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 544 | if (!CXXTypeInfoDecl && LangOpts.MSVCCompat) { |
Nico Weber | 5f96883 | 2012-06-19 23:58:27 +0000 | [diff] [blame] | 545 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 546 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 547 | } |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 548 | if (!CXXTypeInfoDecl) |
| 549 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 550 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 551 | |
Nico Weber | 1b7f39d | 2012-05-20 01:27:21 +0000 | [diff] [blame] | 552 | if (!getLangOpts().RTTI) { |
| 553 | return ExprError(Diag(OpLoc, diag::err_no_typeid_with_fno_rtti)); |
| 554 | } |
| 555 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 556 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 557 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 558 | if (isType) { |
| 559 | // The operand is a type; handle it as such. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 560 | TypeSourceInfo *TInfo = nullptr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 561 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 562 | &TInfo); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 563 | if (T.isNull()) |
| 564 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 565 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 566 | if (!TInfo) |
| 567 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 568 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 569 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 570 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 572 | // The operand is an expression. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 573 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 574 | } |
| 575 | |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 576 | /// Grabs __declspec(uuid()) off a type, or returns 0 if we cannot resolve to |
| 577 | /// a single GUID. |
| 578 | static void |
| 579 | getUuidAttrOfType(Sema &SemaRef, QualType QT, |
| 580 | llvm::SmallSetVector<const UuidAttr *, 1> &UuidAttrs) { |
| 581 | // Optionally remove one level of pointer, reference or array indirection. |
| 582 | const Type *Ty = QT.getTypePtr(); |
| 583 | if (QT->isPointerType() || QT->isReferenceType()) |
| 584 | Ty = QT->getPointeeType().getTypePtr(); |
| 585 | else if (QT->isArrayType()) |
| 586 | Ty = Ty->getBaseElementTypeUnsafe(); |
| 587 | |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 588 | const auto *TD = Ty->getAsTagDecl(); |
| 589 | if (!TD) |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 590 | return; |
| 591 | |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 592 | if (const auto *Uuid = TD->getMostRecentDecl()->getAttr<UuidAttr>()) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 593 | UuidAttrs.insert(Uuid); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | // __uuidof can grab UUIDs from template arguments. |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 598 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(TD)) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 599 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
| 600 | for (const TemplateArgument &TA : TAL.asArray()) { |
| 601 | const UuidAttr *UuidForTA = nullptr; |
| 602 | if (TA.getKind() == TemplateArgument::Type) |
| 603 | getUuidAttrOfType(SemaRef, TA.getAsType(), UuidAttrs); |
| 604 | else if (TA.getKind() == TemplateArgument::Declaration) |
| 605 | getUuidAttrOfType(SemaRef, TA.getAsDecl()->getType(), UuidAttrs); |
| 606 | |
| 607 | if (UuidForTA) |
| 608 | UuidAttrs.insert(UuidForTA); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 613 | /// Build a Microsoft __uuidof expression with a type operand. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 614 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 615 | SourceLocation TypeidLoc, |
| 616 | TypeSourceInfo *Operand, |
| 617 | SourceLocation RParenLoc) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 618 | StringRef UuidStr; |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 619 | if (!Operand->getType()->isDependentType()) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 620 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
| 621 | getUuidAttrOfType(*this, Operand->getType(), UuidAttrs); |
| 622 | if (UuidAttrs.empty()) |
| 623 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 624 | if (UuidAttrs.size() > 1) |
| 625 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 626 | UuidStr = UuidAttrs.back()->getGuid(); |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 627 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 628 | |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 629 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), Operand, UuidStr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 630 | SourceRange(TypeidLoc, RParenLoc)); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 633 | /// Build a Microsoft __uuidof expression with an expression operand. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 634 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 635 | SourceLocation TypeidLoc, |
| 636 | Expr *E, |
| 637 | SourceLocation RParenLoc) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 638 | StringRef UuidStr; |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 639 | if (!E->getType()->isDependentType()) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 640 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 641 | UuidStr = "00000000-0000-0000-0000-000000000000"; |
| 642 | } else { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 643 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
| 644 | getUuidAttrOfType(*this, E->getType(), UuidAttrs); |
| 645 | if (UuidAttrs.empty()) |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 646 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 647 | if (UuidAttrs.size() > 1) |
| 648 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 649 | UuidStr = UuidAttrs.back()->getGuid(); |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 650 | } |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 651 | } |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 652 | |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 653 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), E, UuidStr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 654 | SourceRange(TypeidLoc, RParenLoc)); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 658 | ExprResult |
| 659 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 660 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 661 | // If MSVCGuidDecl has not been cached, do the lookup. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 662 | if (!MSVCGuidDecl) { |
| 663 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 664 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 665 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 666 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 667 | if (!MSVCGuidDecl) |
| 668 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 671 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 672 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 673 | if (isType) { |
| 674 | // The operand is a type; handle it as such. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 675 | TypeSourceInfo *TInfo = nullptr; |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 676 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 677 | &TInfo); |
| 678 | if (T.isNull()) |
| 679 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 680 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 681 | if (!TInfo) |
| 682 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 683 | |
| 684 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 685 | } |
| 686 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 687 | // The operand is an expression. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 688 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 689 | } |
| 690 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 691 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 692 | ExprResult |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 693 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 694 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Bill Wendling | bf313b0 | 2007-02-13 20:09:46 +0000 | [diff] [blame] | 695 | "Unknown C++ Boolean value!"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 696 | return new (Context) |
| 697 | CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 698 | } |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 699 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 700 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 701 | ExprResult |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 702 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 703 | return new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 706 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 707 | ExprResult |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 708 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
| 709 | bool IsThrownVarInScope = false; |
| 710 | if (Ex) { |
| 711 | // C++0x [class.copymove]p31: |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 712 | // When certain criteria are met, an implementation is allowed to omit the |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 713 | // copy/move construction of a class object [...] |
| 714 | // |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 715 | // - in a throw-expression, when the operand is the name of a |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 716 | // non-volatile automatic object (other than a function or catch- |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 717 | // clause parameter) whose scope does not extend beyond the end of the |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 718 | // innermost enclosing try-block (if there is one), the copy/move |
| 719 | // operation from the operand to the exception object (15.1) can be |
| 720 | // omitted by constructing the automatic object directly into the |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 721 | // exception object |
| 722 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) |
| 723 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 724 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { |
| 725 | for( ; S; S = S->getParent()) { |
| 726 | if (S->isDeclScope(Var)) { |
| 727 | IsThrownVarInScope = true; |
| 728 | break; |
| 729 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 730 | |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 731 | if (S->getFlags() & |
| 732 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
| 733 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | |
| 734 | Scope::TryScope)) |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 740 | |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 741 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
| 742 | } |
| 743 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 744 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 745 | bool IsThrownVarInScope) { |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 746 | // Don't report an error if 'throw' is used in system headers. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 747 | if (!getLangOpts().CXXExceptions && |
Alexey Bataev | 1ab3457 | 2018-05-02 16:52:07 +0000 | [diff] [blame] | 748 | !getSourceManager().isInSystemHeader(OpLoc) && |
| 749 | (!getLangOpts().OpenMPIsDevice || |
| 750 | !getLangOpts().OpenMPHostCXXExceptions || |
| 751 | isInOpenMPTargetExecutionDirective() || |
| 752 | isInOpenMPDeclareTargetContext())) |
Anders Carlsson | b94ad3e | 2011-02-19 21:53:09 +0000 | [diff] [blame] | 753 | Diag(OpLoc, diag::err_exceptions_disabled) << "throw"; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 754 | |
Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 755 | // Exceptions aren't allowed in CUDA device code. |
| 756 | if (getLangOpts().CUDA) |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 757 | CUDADiagIfDeviceCode(OpLoc, diag::err_cuda_device_exceptions) |
| 758 | << "throw" << CurrentCUDATarget(); |
Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 759 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 760 | if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope()) |
| 761 | Diag(OpLoc, diag::err_omp_simd_region_cannot_use_stmt) << "throw"; |
| 762 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 763 | if (Ex && !Ex->isTypeDependent()) { |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 764 | QualType ExceptionObjectTy = Context.getExceptionObjectType(Ex->getType()); |
| 765 | if (CheckCXXThrowOperand(OpLoc, ExceptionObjectTy, Ex)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 766 | return ExprError(); |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 767 | |
| 768 | // Initialize the exception result. This implicitly weeds out |
| 769 | // abstract types or types with inaccessible copy constructors. |
| 770 | |
| 771 | // C++0x [class.copymove]p31: |
| 772 | // When certain criteria are met, an implementation is allowed to omit the |
| 773 | // copy/move construction of a class object [...] |
| 774 | // |
| 775 | // - in a throw-expression, when the operand is the name of a |
| 776 | // non-volatile automatic object (other than a function or |
| 777 | // catch-clause |
| 778 | // parameter) whose scope does not extend beyond the end of the |
| 779 | // innermost enclosing try-block (if there is one), the copy/move |
| 780 | // operation from the operand to the exception object (15.1) can be |
| 781 | // omitted by constructing the automatic object directly into the |
| 782 | // exception object |
| 783 | const VarDecl *NRVOVariable = nullptr; |
| 784 | if (IsThrownVarInScope) |
Richard Trieu | 09c163b | 2018-03-15 03:00:55 +0000 | [diff] [blame] | 785 | NRVOVariable = getCopyElisionCandidate(QualType(), Ex, CES_Strict); |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 786 | |
| 787 | InitializedEntity Entity = InitializedEntity::InitializeException( |
| 788 | OpLoc, ExceptionObjectTy, |
| 789 | /*NRVO=*/NRVOVariable != nullptr); |
| 790 | ExprResult Res = PerformMoveOrCopyInitialization( |
| 791 | Entity, NRVOVariable, QualType(), Ex, IsThrownVarInScope); |
| 792 | if (Res.isInvalid()) |
| 793 | return ExprError(); |
| 794 | Ex = Res.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 795 | } |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 796 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 797 | return new (Context) |
| 798 | CXXThrowExpr(Ex, Context.VoidTy, OpLoc, IsThrownVarInScope); |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 799 | } |
| 800 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 801 | static void |
| 802 | collectPublicBases(CXXRecordDecl *RD, |
| 803 | llvm::DenseMap<CXXRecordDecl *, unsigned> &SubobjectsSeen, |
| 804 | llvm::SmallPtrSetImpl<CXXRecordDecl *> &VBases, |
| 805 | llvm::SetVector<CXXRecordDecl *> &PublicSubobjectsSeen, |
| 806 | bool ParentIsPublic) { |
| 807 | for (const CXXBaseSpecifier &BS : RD->bases()) { |
| 808 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
| 809 | bool NewSubobject; |
| 810 | // Virtual bases constitute the same subobject. Non-virtual bases are |
| 811 | // always distinct subobjects. |
| 812 | if (BS.isVirtual()) |
| 813 | NewSubobject = VBases.insert(BaseDecl).second; |
| 814 | else |
| 815 | NewSubobject = true; |
| 816 | |
| 817 | if (NewSubobject) |
| 818 | ++SubobjectsSeen[BaseDecl]; |
| 819 | |
| 820 | // Only add subobjects which have public access throughout the entire chain. |
| 821 | bool PublicPath = ParentIsPublic && BS.getAccessSpecifier() == AS_public; |
| 822 | if (PublicPath) |
| 823 | PublicSubobjectsSeen.insert(BaseDecl); |
| 824 | |
| 825 | // Recurse on to each base subobject. |
| 826 | collectPublicBases(BaseDecl, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
| 827 | PublicPath); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | static void getUnambiguousPublicSubobjects( |
| 832 | CXXRecordDecl *RD, llvm::SmallVectorImpl<CXXRecordDecl *> &Objects) { |
| 833 | llvm::DenseMap<CXXRecordDecl *, unsigned> SubobjectsSeen; |
| 834 | llvm::SmallSet<CXXRecordDecl *, 2> VBases; |
| 835 | llvm::SetVector<CXXRecordDecl *> PublicSubobjectsSeen; |
| 836 | SubobjectsSeen[RD] = 1; |
| 837 | PublicSubobjectsSeen.insert(RD); |
| 838 | collectPublicBases(RD, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
| 839 | /*ParentIsPublic=*/true); |
| 840 | |
| 841 | for (CXXRecordDecl *PublicSubobject : PublicSubobjectsSeen) { |
| 842 | // Skip ambiguous objects. |
| 843 | if (SubobjectsSeen[PublicSubobject] > 1) |
| 844 | continue; |
| 845 | |
| 846 | Objects.push_back(PublicSubobject); |
| 847 | } |
| 848 | } |
| 849 | |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 850 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 851 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, |
| 852 | QualType ExceptionObjectTy, Expr *E) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 853 | // If the type of the exception would be an incomplete type or a pointer |
| 854 | // to an incomplete type other than (cv) void the program is ill-formed. |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 855 | QualType Ty = ExceptionObjectTy; |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 856 | bool isPointer = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 857 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 858 | Ty = Ptr->getPointeeType(); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 859 | isPointer = true; |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 860 | } |
| 861 | if (!isPointer || !Ty->isVoidType()) { |
| 862 | if (RequireCompleteType(ThrowLoc, Ty, |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 863 | isPointer ? diag::err_throw_incomplete_ptr |
| 864 | : diag::err_throw_incomplete, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 865 | E->getSourceRange())) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 866 | return true; |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 867 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 868 | if (RequireNonAbstractType(ThrowLoc, ExceptionObjectTy, |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 869 | diag::err_throw_abstract_type, E)) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 870 | return true; |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 873 | // If the exception has class type, we need additional handling. |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 874 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 875 | if (!RD) |
| 876 | return false; |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 877 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 878 | // If we are throwing a polymorphic class type or pointer thereof, |
| 879 | // exception handling will make use of the vtable. |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 880 | MarkVTableUsed(ThrowLoc, RD); |
| 881 | |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 882 | // If a pointer is thrown, the referenced object will not be destroyed. |
| 883 | if (isPointer) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 884 | return false; |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 885 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 886 | // If the class has a destructor, we must be able to call it. |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 887 | if (!RD->hasIrrelevantDestructor()) { |
| 888 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
| 889 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
| 890 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 891 | PDiag(diag::err_access_dtor_exception) << Ty); |
| 892 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 893 | return true; |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 896 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 897 | // The MSVC ABI creates a list of all types which can catch the exception |
| 898 | // object. This list also references the appropriate copy constructor to call |
| 899 | // if the object is caught by value and has a non-trivial copy constructor. |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 900 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 901 | // We are only interested in the public, unambiguous bases contained within |
| 902 | // the exception object. Bases which are ambiguous or otherwise |
| 903 | // inaccessible are not catchable types. |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 904 | llvm::SmallVector<CXXRecordDecl *, 2> UnambiguousPublicSubobjects; |
| 905 | getUnambiguousPublicSubobjects(RD, UnambiguousPublicSubobjects); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 906 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 907 | for (CXXRecordDecl *Subobject : UnambiguousPublicSubobjects) { |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 908 | // Attempt to lookup the copy constructor. Various pieces of machinery |
| 909 | // will spring into action, like template instantiation, which means this |
| 910 | // cannot be a simple walk of the class's decls. Instead, we must perform |
| 911 | // lookup and overload resolution. |
| 912 | CXXConstructorDecl *CD = LookupCopyingConstructor(Subobject, 0); |
| 913 | if (!CD) |
| 914 | continue; |
| 915 | |
| 916 | // Mark the constructor referenced as it is used by this throw expression. |
| 917 | MarkFunctionReferenced(E->getExprLoc(), CD); |
| 918 | |
| 919 | // Skip this copy constructor if it is trivial, we don't need to record it |
| 920 | // in the catchable type data. |
| 921 | if (CD->isTrivial()) |
| 922 | continue; |
| 923 | |
| 924 | // The copy constructor is non-trivial, create a mapping from this class |
| 925 | // type to this constructor. |
| 926 | // N.B. The selection of copy constructor is not sensitive to this |
| 927 | // particular throw-site. Lookup will be performed at the catch-site to |
| 928 | // ensure that the copy constructor is, in fact, accessible (via |
| 929 | // friendship or any other means). |
| 930 | Context.addCopyConstructorForExceptionObject(Subobject, CD); |
| 931 | |
| 932 | // We don't keep the instantiated default argument expressions around so |
| 933 | // we must rebuild them here. |
| 934 | for (unsigned I = 1, E = CD->getNumParams(); I != E; ++I) { |
Reid Kleckner | c01ee75 | 2016-11-23 16:51:30 +0000 | [diff] [blame] | 935 | if (CheckCXXDefaultArgExpr(ThrowLoc, CD, CD->getParamDecl(I))) |
| 936 | return true; |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | } |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 940 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 941 | return false; |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 942 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 943 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 944 | static QualType adjustCVQualifiersForCXXThisWithinLambda( |
| 945 | ArrayRef<FunctionScopeInfo *> FunctionScopes, QualType ThisTy, |
| 946 | DeclContext *CurSemaContext, ASTContext &ASTCtx) { |
| 947 | |
| 948 | QualType ClassType = ThisTy->getPointeeType(); |
| 949 | LambdaScopeInfo *CurLSI = nullptr; |
| 950 | DeclContext *CurDC = CurSemaContext; |
| 951 | |
| 952 | // Iterate through the stack of lambdas starting from the innermost lambda to |
| 953 | // the outermost lambda, checking if '*this' is ever captured by copy - since |
| 954 | // that could change the cv-qualifiers of the '*this' object. |
| 955 | // The object referred to by '*this' starts out with the cv-qualifiers of its |
| 956 | // member function. We then start with the innermost lambda and iterate |
| 957 | // outward checking to see if any lambda performs a by-copy capture of '*this' |
| 958 | // - and if so, any nested lambda must respect the 'constness' of that |
| 959 | // capturing lamdbda's call operator. |
| 960 | // |
| 961 | |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 962 | // Since the FunctionScopeInfo stack is representative of the lexical |
| 963 | // nesting of the lambda expressions during initial parsing (and is the best |
| 964 | // place for querying information about captures about lambdas that are |
| 965 | // partially processed) and perhaps during instantiation of function templates |
| 966 | // that contain lambda expressions that need to be transformed BUT not |
| 967 | // necessarily during instantiation of a nested generic lambda's function call |
| 968 | // operator (which might even be instantiated at the end of the TU) - at which |
| 969 | // time the DeclContext tree is mature enough to query capture information |
| 970 | // reliably - we use a two pronged approach to walk through all the lexically |
| 971 | // enclosing lambda expressions: |
| 972 | // |
| 973 | // 1) Climb down the FunctionScopeInfo stack as long as each item represents |
| 974 | // a Lambda (i.e. LambdaScopeInfo) AND each LSI's 'closure-type' is lexically |
| 975 | // enclosed by the call-operator of the LSI below it on the stack (while |
| 976 | // tracking the enclosing DC for step 2 if needed). Note the topmost LSI on |
| 977 | // the stack represents the innermost lambda. |
| 978 | // |
| 979 | // 2) If we run out of enclosing LSI's, check if the enclosing DeclContext |
| 980 | // represents a lambda's call operator. If it does, we must be instantiating |
| 981 | // a generic lambda's call operator (represented by the Current LSI, and |
| 982 | // should be the only scenario where an inconsistency between the LSI and the |
| 983 | // DeclContext should occur), so climb out the DeclContexts if they |
| 984 | // represent lambdas, while querying the corresponding closure types |
| 985 | // regarding capture information. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 986 | |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 987 | // 1) Climb down the function scope info stack. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 988 | for (int I = FunctionScopes.size(); |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 989 | I-- && isa<LambdaScopeInfo>(FunctionScopes[I]) && |
| 990 | (!CurLSI || !CurLSI->Lambda || CurLSI->Lambda->getDeclContext() == |
| 991 | cast<LambdaScopeInfo>(FunctionScopes[I])->CallOperator); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 992 | CurDC = getLambdaAwareParentOfDeclContext(CurDC)) { |
| 993 | CurLSI = cast<LambdaScopeInfo>(FunctionScopes[I]); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 994 | |
| 995 | if (!CurLSI->isCXXThisCaptured()) |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 996 | continue; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 997 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 998 | auto C = CurLSI->getCXXThisCapture(); |
| 999 | |
| 1000 | if (C.isCopyCapture()) { |
| 1001 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 1002 | if (CurLSI->CallOperator->isConst()) |
| 1003 | ClassType.addConst(); |
| 1004 | return ASTCtx.getPointerType(ClassType); |
| 1005 | } |
| 1006 | } |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 1007 | |
| 1008 | // 2) We've run out of ScopeInfos but check if CurDC is a lambda (which can |
| 1009 | // happen during instantiation of its nested generic lambda call operator) |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1010 | if (isLambdaCallOperator(CurDC)) { |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 1011 | assert(CurLSI && "While computing 'this' capture-type for a generic " |
| 1012 | "lambda, we must have a corresponding LambdaScopeInfo"); |
| 1013 | assert(isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && |
| 1014 | "While computing 'this' capture-type for a generic lambda, when we " |
| 1015 | "run out of enclosing LSI's, yet the enclosing DC is a " |
| 1016 | "lambda-call-operator we must be (i.e. Current LSI) in a generic " |
| 1017 | "lambda call oeprator"); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1018 | assert(CurDC == getLambdaAwareParentOfDeclContext(CurLSI->CallOperator)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1019 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1020 | auto IsThisCaptured = |
| 1021 | [](CXXRecordDecl *Closure, bool &IsByCopy, bool &IsConst) { |
| 1022 | IsConst = false; |
| 1023 | IsByCopy = false; |
| 1024 | for (auto &&C : Closure->captures()) { |
| 1025 | if (C.capturesThis()) { |
| 1026 | if (C.getCaptureKind() == LCK_StarThis) |
| 1027 | IsByCopy = true; |
| 1028 | if (Closure->getLambdaCallOperator()->isConst()) |
| 1029 | IsConst = true; |
| 1030 | return true; |
| 1031 | } |
| 1032 | } |
| 1033 | return false; |
| 1034 | }; |
| 1035 | |
| 1036 | bool IsByCopyCapture = false; |
| 1037 | bool IsConstCapture = false; |
| 1038 | CXXRecordDecl *Closure = cast<CXXRecordDecl>(CurDC->getParent()); |
| 1039 | while (Closure && |
| 1040 | IsThisCaptured(Closure, IsByCopyCapture, IsConstCapture)) { |
| 1041 | if (IsByCopyCapture) { |
| 1042 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 1043 | if (IsConstCapture) |
| 1044 | ClassType.addConst(); |
| 1045 | return ASTCtx.getPointerType(ClassType); |
| 1046 | } |
| 1047 | Closure = isLambdaCallOperator(Closure->getParent()) |
| 1048 | ? cast<CXXRecordDecl>(Closure->getParent()->getParent()) |
| 1049 | : nullptr; |
| 1050 | } |
| 1051 | } |
| 1052 | return ASTCtx.getPointerType(ClassType); |
| 1053 | } |
| 1054 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1055 | QualType Sema::getCurrentThisType() { |
| 1056 | DeclContext *DC = getFunctionLevelDeclContext(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1057 | QualType ThisTy = CXXThisTypeOverride; |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 1058 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1059 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { |
| 1060 | if (method && method->isInstance()) |
| 1061 | ThisTy = method->getThisType(Context); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1062 | } |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1063 | |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 1064 | if (ThisTy.isNull() && isLambdaCallOperator(CurContext) && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1065 | inTemplateInstantiation()) { |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1066 | |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 1067 | assert(isa<CXXRecordDecl>(DC) && |
| 1068 | "Trying to get 'this' type from static method?"); |
| 1069 | |
| 1070 | // This is a lambda call operator that is being instantiated as a default |
| 1071 | // initializer. DC must point to the enclosing class type, so we can recover |
| 1072 | // the 'this' type from it. |
| 1073 | |
| 1074 | QualType ClassTy = Context.getTypeDeclType(cast<CXXRecordDecl>(DC)); |
| 1075 | // There are no cv-qualifiers for 'this' within default initializers, |
| 1076 | // per [expr.prim.general]p4. |
| 1077 | ThisTy = Context.getPointerType(ClassTy); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1078 | } |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1079 | |
| 1080 | // If we are within a lambda's call operator, the cv-qualifiers of 'this' |
| 1081 | // might need to be adjusted if the lambda or any of its enclosing lambda's |
| 1082 | // captures '*this' by copy. |
| 1083 | if (!ThisTy.isNull() && isLambdaCallOperator(CurContext)) |
| 1084 | return adjustCVQualifiersForCXXThisWithinLambda(FunctionScopes, ThisTy, |
| 1085 | CurContext, Context); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1086 | return ThisTy; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1089 | Sema::CXXThisScopeRAII::CXXThisScopeRAII(Sema &S, |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1090 | Decl *ContextDecl, |
| 1091 | unsigned CXXThisTypeQuals, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1092 | bool Enabled) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1093 | : S(S), OldCXXThisTypeOverride(S.CXXThisTypeOverride), Enabled(false) |
| 1094 | { |
| 1095 | if (!Enabled || !ContextDecl) |
| 1096 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1097 | |
| 1098 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1099 | if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(ContextDecl)) |
| 1100 | Record = Template->getTemplatedDecl(); |
| 1101 | else |
| 1102 | Record = cast<CXXRecordDecl>(ContextDecl); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1103 | |
Andrey Bokhanko | 67a4186 | 2016-05-26 10:06:01 +0000 | [diff] [blame] | 1104 | // We care only for CVR qualifiers here, so cut everything else. |
| 1105 | CXXThisTypeQuals &= Qualifiers::FastMask; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1106 | S.CXXThisTypeOverride |
| 1107 | = S.Context.getPointerType( |
| 1108 | S.Context.getRecordType(Record).withCVRQualifiers(CXXThisTypeQuals)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1109 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1110 | this->Enabled = true; |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | Sema::CXXThisScopeRAII::~CXXThisScopeRAII() { |
| 1115 | if (Enabled) { |
| 1116 | S.CXXThisTypeOverride = OldCXXThisTypeOverride; |
| 1117 | } |
| 1118 | } |
| 1119 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1120 | static Expr *captureThis(Sema &S, ASTContext &Context, RecordDecl *RD, |
| 1121 | QualType ThisTy, SourceLocation Loc, |
| 1122 | const bool ByCopy) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1123 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1124 | QualType AdjustedThisTy = ThisTy; |
| 1125 | // The type of the corresponding data member (not a 'this' pointer if 'by |
| 1126 | // copy'). |
| 1127 | QualType CaptureThisFieldTy = ThisTy; |
| 1128 | if (ByCopy) { |
| 1129 | // If we are capturing the object referred to by '*this' by copy, ignore any |
| 1130 | // cv qualifiers inherited from the type of the member function for the type |
| 1131 | // of the closure-type's corresponding data member and any use of 'this'. |
| 1132 | CaptureThisFieldTy = ThisTy->getPointeeType(); |
| 1133 | CaptureThisFieldTy.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 1134 | AdjustedThisTy = Context.getPointerType(CaptureThisFieldTy); |
| 1135 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1136 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1137 | FieldDecl *Field = FieldDecl::Create( |
| 1138 | Context, RD, Loc, Loc, nullptr, CaptureThisFieldTy, |
| 1139 | Context.getTrivialTypeSourceInfo(CaptureThisFieldTy, Loc), nullptr, false, |
| 1140 | ICIS_NoInit); |
| 1141 | |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1142 | Field->setImplicit(true); |
| 1143 | Field->setAccess(AS_private); |
| 1144 | RD->addDecl(Field); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1145 | Expr *This = |
| 1146 | new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit*/ true); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1147 | if (ByCopy) { |
| 1148 | Expr *StarThis = S.CreateBuiltinUnaryOp(Loc, |
| 1149 | UO_Deref, |
| 1150 | This).get(); |
| 1151 | InitializedEntity Entity = InitializedEntity::InitializeLambdaCapture( |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1152 | nullptr, CaptureThisFieldTy, Loc); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1153 | InitializationKind InitKind = InitializationKind::CreateDirect(Loc, Loc, Loc); |
| 1154 | InitializationSequence Init(S, Entity, InitKind, StarThis); |
| 1155 | ExprResult ER = Init.Perform(S, Entity, InitKind, StarThis); |
| 1156 | if (ER.isInvalid()) return nullptr; |
| 1157 | return ER.get(); |
| 1158 | } |
| 1159 | return This; |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1162 | bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit, |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1163 | bool BuildAndDiagnose, const unsigned *const FunctionScopeIndexToStopAt, |
| 1164 | const bool ByCopy) { |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1165 | // We don't need to capture this in an unevaluated context. |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1166 | if (isUnevaluatedContext() && !Explicit) |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1167 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1168 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1169 | assert((!ByCopy || Explicit) && "cannot implicitly capture *this by value"); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1170 | |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 1171 | const int MaxFunctionScopesIndex = FunctionScopeIndexToStopAt |
| 1172 | ? *FunctionScopeIndexToStopAt |
| 1173 | : FunctionScopes.size() - 1; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1174 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1175 | // Check that we can capture the *enclosing object* (referred to by '*this') |
| 1176 | // by the capturing-entity/closure (lambda/block/etc) at |
| 1177 | // MaxFunctionScopesIndex-deep on the FunctionScopes stack. |
| 1178 | |
| 1179 | // Note: The *enclosing object* can only be captured by-value by a |
| 1180 | // closure that is a lambda, using the explicit notation: |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1181 | // [*this] { ... }. |
| 1182 | // Every other capture of the *enclosing object* results in its by-reference |
| 1183 | // capture. |
| 1184 | |
| 1185 | // For a closure 'L' (at MaxFunctionScopesIndex in the FunctionScopes |
| 1186 | // stack), we can capture the *enclosing object* only if: |
| 1187 | // - 'L' has an explicit byref or byval capture of the *enclosing object* |
| 1188 | // - or, 'L' has an implicit capture. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1189 | // AND |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1190 | // -- there is no enclosing closure |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1191 | // -- or, there is some enclosing closure 'E' that has already captured the |
| 1192 | // *enclosing object*, and every intervening closure (if any) between 'E' |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1193 | // and 'L' can implicitly capture the *enclosing object*. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1194 | // -- or, every enclosing closure can implicitly capture the |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1195 | // *enclosing object* |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1196 | |
| 1197 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1198 | unsigned NumCapturingClosures = 0; |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 1199 | for (int idx = MaxFunctionScopesIndex; idx >= 0; idx--) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1200 | if (CapturingScopeInfo *CSI = |
| 1201 | dyn_cast<CapturingScopeInfo>(FunctionScopes[idx])) { |
| 1202 | if (CSI->CXXThisCaptureIndex != 0) { |
| 1203 | // 'this' is already being captured; there isn't anything more to do. |
Malcolm Parsons | 87a0362 | 2017-01-13 15:01:06 +0000 | [diff] [blame] | 1204 | CSI->Captures[CSI->CXXThisCaptureIndex - 1].markUsed(BuildAndDiagnose); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1205 | break; |
| 1206 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1207 | LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI); |
| 1208 | if (LSI && isGenericLambdaCallOperatorSpecialization(LSI->CallOperator)) { |
| 1209 | // This context can't implicitly capture 'this'; fail out. |
| 1210 | if (BuildAndDiagnose) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1211 | Diag(Loc, diag::err_this_capture) |
| 1212 | << (Explicit && idx == MaxFunctionScopesIndex); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1213 | return true; |
| 1214 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1215 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref || |
Douglas Gregor | a1bffa2 | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 1216 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval || |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 1217 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block || |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1218 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_CapturedRegion || |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1219 | (Explicit && idx == MaxFunctionScopesIndex)) { |
| 1220 | // Regarding (Explicit && idx == MaxFunctionScopesIndex): only the first |
| 1221 | // iteration through can be an explicit capture, all enclosing closures, |
| 1222 | // if any, must perform implicit captures. |
| 1223 | |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 1224 | // This closure can capture 'this'; continue looking upwards. |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1225 | NumCapturingClosures++; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1226 | continue; |
| 1227 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1228 | // This context can't implicitly capture 'this'; fail out. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1229 | if (BuildAndDiagnose) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1230 | Diag(Loc, diag::err_this_capture) |
| 1231 | << (Explicit && idx == MaxFunctionScopesIndex); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1232 | return true; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1233 | } |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1236 | if (!BuildAndDiagnose) return false; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1237 | |
| 1238 | // If we got here, then the closure at MaxFunctionScopesIndex on the |
| 1239 | // FunctionScopes stack, can capture the *enclosing object*, so capture it |
| 1240 | // (including implicit by-reference captures in any enclosing closures). |
| 1241 | |
| 1242 | // In the loop below, respect the ByCopy flag only for the closure requesting |
| 1243 | // the capture (i.e. first iteration through the loop below). Ignore it for |
Simon Pilgrim | b17efcb | 2016-11-15 18:28:07 +0000 | [diff] [blame] | 1244 | // all enclosing closure's up to NumCapturingClosures (since they must be |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1245 | // implicitly capturing the *enclosing object* by reference (see loop |
| 1246 | // above)). |
| 1247 | assert((!ByCopy || |
| 1248 | dyn_cast<LambdaScopeInfo>(FunctionScopes[MaxFunctionScopesIndex])) && |
| 1249 | "Only a lambda can capture the enclosing object (referred to by " |
| 1250 | "*this) by copy"); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1251 | // FIXME: We need to delay this marking in PotentiallyPotentiallyEvaluated |
| 1252 | // contexts. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1253 | QualType ThisTy = getCurrentThisType(); |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 1254 | for (int idx = MaxFunctionScopesIndex; NumCapturingClosures; |
| 1255 | --idx, --NumCapturingClosures) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1256 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1257 | Expr *ThisExpr = nullptr; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1258 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1259 | if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI)) { |
| 1260 | // For lambda expressions, build a field and an initializing expression, |
| 1261 | // and capture the *enclosing object* by copy only if this is the first |
| 1262 | // iteration. |
| 1263 | ThisExpr = captureThis(*this, Context, LSI->Lambda, ThisTy, Loc, |
| 1264 | ByCopy && idx == MaxFunctionScopesIndex); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1265 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1266 | } else if (CapturedRegionScopeInfo *RSI |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1267 | = dyn_cast<CapturedRegionScopeInfo>(FunctionScopes[idx])) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1268 | ThisExpr = |
| 1269 | captureThis(*this, Context, RSI->TheRecordDecl, ThisTy, Loc, |
| 1270 | false/*ByCopy*/); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1271 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1272 | bool isNested = NumCapturingClosures > 1; |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1273 | CSI->addThisCapture(isNested, Loc, ThisExpr, ByCopy); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1274 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1275 | return false; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1278 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1279 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 1280 | /// is a non-lvalue expression whose value is the address of the object for |
| 1281 | /// which the function is called. |
| 1282 | |
Douglas Gregor | 09deffa | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 1283 | QualType ThisTy = getCurrentThisType(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1284 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1285 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1286 | CheckCXXThisCapture(Loc); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1287 | return new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1288 | } |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1289 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1290 | bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) { |
| 1291 | // If we're outside the body of a member function, then we'll have a specified |
| 1292 | // type for 'this'. |
| 1293 | if (CXXThisTypeOverride.isNull()) |
| 1294 | return false; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1295 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1296 | // Determine whether we're looking into a class that's currently being |
| 1297 | // defined. |
| 1298 | CXXRecordDecl *Class = BaseType->getAsCXXRecordDecl(); |
| 1299 | return Class && Class->isBeingDefined(); |
| 1300 | } |
| 1301 | |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1302 | /// Parse construction of a specified type. |
| 1303 | /// Can be interpreted either as function-style casting ("int(x)") |
| 1304 | /// or class type construction ("ClassType(x,y,z)") |
| 1305 | /// or creation of a value-initialized type ("int()"). |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1306 | ExprResult |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1307 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1308 | SourceLocation LParenOrBraceLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1309 | MultiExprArg exprs, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1310 | SourceLocation RParenOrBraceLoc, |
| 1311 | bool ListInitialization) { |
Douglas Gregor | 7df89f5 | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 1312 | if (!TypeRep) |
| 1313 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1314 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1315 | TypeSourceInfo *TInfo; |
| 1316 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 1317 | if (!TInfo) |
| 1318 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1319 | |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1320 | auto Result = BuildCXXTypeConstructExpr(TInfo, LParenOrBraceLoc, exprs, |
| 1321 | RParenOrBraceLoc, ListInitialization); |
Richard Smith | b8c414c | 2016-06-30 20:24:30 +0000 | [diff] [blame] | 1322 | // Avoid creating a non-type-dependent expression that contains typos. |
| 1323 | // Non-type-dependent expressions are liable to be discarded without |
| 1324 | // checking for embedded typos. |
| 1325 | if (!Result.isInvalid() && Result.get()->isInstantiationDependent() && |
| 1326 | !Result.get()->isTypeDependent()) |
| 1327 | Result = CorrectDelayedTyposInExpr(Result.get()); |
| 1328 | return Result; |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1331 | ExprResult |
| 1332 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1333 | SourceLocation LParenOrBraceLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1334 | MultiExprArg Exprs, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1335 | SourceLocation RParenOrBraceLoc, |
| 1336 | bool ListInitialization) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1337 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1338 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1339 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1340 | if (Ty->isDependentType() || CallExpr::hasAnyTypeDependentArguments(Exprs)) { |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1341 | // FIXME: CXXUnresolvedConstructExpr does not model list-initialization |
| 1342 | // directly. We work around this by dropping the locations of the braces. |
| 1343 | SourceRange Locs = ListInitialization |
| 1344 | ? SourceRange() |
| 1345 | : SourceRange(LParenOrBraceLoc, RParenOrBraceLoc); |
| 1346 | return CXXUnresolvedConstructExpr::Create(Context, TInfo, Locs.getBegin(), |
| 1347 | Exprs, Locs.getEnd()); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1350 | assert((!ListInitialization || |
| 1351 | (Exprs.size() == 1 && isa<InitListExpr>(Exprs[0]))) && |
| 1352 | "List initialization must have initializer list as expression."); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1353 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenOrBraceLoc); |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1354 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1355 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 1356 | InitializationKind Kind = |
| 1357 | Exprs.size() |
| 1358 | ? ListInitialization |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1359 | ? InitializationKind::CreateDirectList( |
| 1360 | TyBeginLoc, LParenOrBraceLoc, RParenOrBraceLoc) |
| 1361 | : InitializationKind::CreateDirect(TyBeginLoc, LParenOrBraceLoc, |
| 1362 | RParenOrBraceLoc) |
| 1363 | : InitializationKind::CreateValue(TyBeginLoc, LParenOrBraceLoc, |
| 1364 | RParenOrBraceLoc); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1365 | |
| 1366 | // C++1z [expr.type.conv]p1: |
| 1367 | // If the type is a placeholder for a deduced class type, [...perform class |
| 1368 | // template argument deduction...] |
| 1369 | DeducedType *Deduced = Ty->getContainedDeducedType(); |
| 1370 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
| 1371 | Ty = DeduceTemplateSpecializationFromInitializer(TInfo, Entity, |
| 1372 | Kind, Exprs); |
| 1373 | if (Ty.isNull()) |
| 1374 | return ExprError(); |
| 1375 | Entity = InitializedEntity::InitializeTemporary(TInfo, Ty); |
| 1376 | } |
| 1377 | |
Douglas Gregor | dd04d33 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 1378 | // C++ [expr.type.conv]p1: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1379 | // If the expression list is a parenthesized single expression, the type |
| 1380 | // conversion expression is equivalent (in definedness, and if defined in |
| 1381 | // meaning) to the corresponding cast expression. |
| 1382 | if (Exprs.size() == 1 && !ListInitialization && |
| 1383 | !isa<InitListExpr>(Exprs[0])) { |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1384 | Expr *Arg = Exprs[0]; |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1385 | return BuildCXXFunctionalCastExpr(TInfo, Ty, LParenOrBraceLoc, Arg, |
| 1386 | RParenOrBraceLoc); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1389 | // For an expression of the form T(), T shall not be an array type. |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1390 | QualType ElemTy = Ty; |
| 1391 | if (Ty->isArrayType()) { |
| 1392 | if (!ListInitialization) |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1393 | return ExprError(Diag(TyBeginLoc, diag::err_value_init_for_array_type) |
| 1394 | << FullRange); |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1395 | ElemTy = Context.getBaseElementType(Ty); |
| 1396 | } |
| 1397 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1398 | // There doesn't seem to be an explicit rule against this but sanity demands |
| 1399 | // we only construct objects with object types. |
| 1400 | if (Ty->isFunctionType()) |
| 1401 | return ExprError(Diag(TyBeginLoc, diag::err_init_for_function_type) |
| 1402 | << Ty << FullRange); |
David Majnemer | 7eddcff | 2015-09-14 07:05:00 +0000 | [diff] [blame] | 1403 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1404 | // C++17 [expr.type.conv]p2: |
| 1405 | // If the type is cv void and the initializer is (), the expression is a |
| 1406 | // prvalue of the specified type that performs no initialization. |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1407 | if (!Ty->isVoidType() && |
| 1408 | RequireCompleteType(TyBeginLoc, ElemTy, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1409 | diag::err_invalid_incomplete_type_use, FullRange)) |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1410 | return ExprError(); |
| 1411 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1412 | // Otherwise, the expression is a prvalue of the specified type whose |
| 1413 | // result object is direct-initialized (11.6) with the initializer. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1414 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs); |
| 1415 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Exprs); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1416 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1417 | if (Result.isInvalid()) |
Richard Smith | 9006190 | 2013-09-23 02:20:00 +0000 | [diff] [blame] | 1418 | return Result; |
| 1419 | |
| 1420 | Expr *Inner = Result.get(); |
| 1421 | if (CXXBindTemporaryExpr *BTE = dyn_cast_or_null<CXXBindTemporaryExpr>(Inner)) |
| 1422 | Inner = BTE->getSubExpr(); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1423 | if (!isa<CXXTemporaryObjectExpr>(Inner) && |
| 1424 | !isa<CXXScalarValueInitExpr>(Inner)) { |
Richard Smith | 1ae689c | 2015-01-28 22:06:01 +0000 | [diff] [blame] | 1425 | // If we created a CXXTemporaryObjectExpr, that node also represents the |
| 1426 | // functional cast. Otherwise, create an explicit cast to represent |
| 1427 | // the syntactic form of a functional-style cast that was used here. |
| 1428 | // |
| 1429 | // FIXME: Creating a CXXFunctionalCastExpr around a CXXConstructExpr |
| 1430 | // would give a more consistent AST representation than using a |
| 1431 | // CXXTemporaryObjectExpr. It's also weird that the functional cast |
| 1432 | // is sometimes handled by initialization and sometimes not. |
Richard Smith | 9006190 | 2013-09-23 02:20:00 +0000 | [diff] [blame] | 1433 | QualType ResultType = Result.get()->getType(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1434 | SourceRange Locs = ListInitialization |
| 1435 | ? SourceRange() |
| 1436 | : SourceRange(LParenOrBraceLoc, RParenOrBraceLoc); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1437 | Result = CXXFunctionalCastExpr::Create( |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1438 | Context, ResultType, Expr::getValueKindForType(Ty), TInfo, CK_NoOp, |
| 1439 | Result.get(), /*Path=*/nullptr, Locs.getBegin(), Locs.getEnd()); |
Sebastian Redl | 2b80af4 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1442 | return Result; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1443 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1444 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1445 | /// Determine whether the given function is a non-placement |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1446 | /// deallocation function. |
| 1447 | static bool isNonPlacementDeallocationFunction(Sema &S, FunctionDecl *FD) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1448 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 1449 | return Method->isUsualDeallocationFunction(); |
| 1450 | |
| 1451 | if (FD->getOverloadedOperator() != OO_Delete && |
| 1452 | FD->getOverloadedOperator() != OO_Array_Delete) |
| 1453 | return false; |
| 1454 | |
| 1455 | unsigned UsualParams = 1; |
| 1456 | |
| 1457 | if (S.getLangOpts().SizedDeallocation && UsualParams < FD->getNumParams() && |
| 1458 | S.Context.hasSameUnqualifiedType( |
| 1459 | FD->getParamDecl(UsualParams)->getType(), |
| 1460 | S.Context.getSizeType())) |
| 1461 | ++UsualParams; |
| 1462 | |
| 1463 | if (S.getLangOpts().AlignedAllocation && UsualParams < FD->getNumParams() && |
| 1464 | S.Context.hasSameUnqualifiedType( |
| 1465 | FD->getParamDecl(UsualParams)->getType(), |
| 1466 | S.Context.getTypeDeclType(S.getStdAlignValT()))) |
| 1467 | ++UsualParams; |
| 1468 | |
| 1469 | return UsualParams == FD->getNumParams(); |
| 1470 | } |
| 1471 | |
| 1472 | namespace { |
| 1473 | struct UsualDeallocFnInfo { |
| 1474 | UsualDeallocFnInfo() : Found(), FD(nullptr) {} |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1475 | UsualDeallocFnInfo(Sema &S, DeclAccessPair Found) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1476 | : Found(Found), FD(dyn_cast<FunctionDecl>(Found->getUnderlyingDecl())), |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1477 | Destroying(false), HasSizeT(false), HasAlignValT(false), |
| 1478 | CUDAPref(Sema::CFP_Native) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1479 | // A function template declaration is never a usual deallocation function. |
| 1480 | if (!FD) |
| 1481 | return; |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1482 | unsigned NumBaseParams = 1; |
| 1483 | if (FD->isDestroyingOperatorDelete()) { |
| 1484 | Destroying = true; |
| 1485 | ++NumBaseParams; |
| 1486 | } |
| 1487 | if (FD->getNumParams() == NumBaseParams + 2) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1488 | HasAlignValT = HasSizeT = true; |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1489 | else if (FD->getNumParams() == NumBaseParams + 1) { |
| 1490 | HasSizeT = FD->getParamDecl(NumBaseParams)->getType()->isIntegerType(); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1491 | HasAlignValT = !HasSizeT; |
| 1492 | } |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1493 | |
| 1494 | // In CUDA, determine how much we'd like / dislike to call this. |
| 1495 | if (S.getLangOpts().CUDA) |
| 1496 | if (auto *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 1497 | CUDAPref = S.IdentifyCUDAPreference(Caller, FD); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Eric Fiselier | fa752f2 | 2018-03-21 19:19:48 +0000 | [diff] [blame] | 1500 | explicit operator bool() const { return FD; } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1501 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1502 | bool isBetterThan(const UsualDeallocFnInfo &Other, bool WantSize, |
| 1503 | bool WantAlign) const { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1504 | // C++ P0722: |
| 1505 | // A destroying operator delete is preferred over a non-destroying |
| 1506 | // operator delete. |
| 1507 | if (Destroying != Other.Destroying) |
| 1508 | return Destroying; |
| 1509 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1510 | // C++17 [expr.delete]p10: |
| 1511 | // If the type has new-extended alignment, a function with a parameter |
| 1512 | // of type std::align_val_t is preferred; otherwise a function without |
| 1513 | // such a parameter is preferred |
| 1514 | if (HasAlignValT != Other.HasAlignValT) |
| 1515 | return HasAlignValT == WantAlign; |
| 1516 | |
| 1517 | if (HasSizeT != Other.HasSizeT) |
| 1518 | return HasSizeT == WantSize; |
| 1519 | |
| 1520 | // Use CUDA call preference as a tiebreaker. |
| 1521 | return CUDAPref > Other.CUDAPref; |
| 1522 | } |
| 1523 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1524 | DeclAccessPair Found; |
| 1525 | FunctionDecl *FD; |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1526 | bool Destroying, HasSizeT, HasAlignValT; |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1527 | Sema::CUDAFunctionPreference CUDAPref; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1528 | }; |
| 1529 | } |
| 1530 | |
| 1531 | /// Determine whether a type has new-extended alignment. This may be called when |
| 1532 | /// the type is incomplete (for a delete-expression with an incomplete pointee |
| 1533 | /// type), in which case it will conservatively return false if the alignment is |
| 1534 | /// not known. |
| 1535 | static bool hasNewExtendedAlignment(Sema &S, QualType AllocType) { |
| 1536 | return S.getLangOpts().AlignedAllocation && |
| 1537 | S.getASTContext().getTypeAlignIfKnown(AllocType) > |
| 1538 | S.getASTContext().getTargetInfo().getNewAlign(); |
| 1539 | } |
| 1540 | |
| 1541 | /// Select the correct "usual" deallocation function to use from a selection of |
| 1542 | /// deallocation functions (either global or class-scope). |
| 1543 | static UsualDeallocFnInfo resolveDeallocationOverload( |
| 1544 | Sema &S, LookupResult &R, bool WantSize, bool WantAlign, |
| 1545 | llvm::SmallVectorImpl<UsualDeallocFnInfo> *BestFns = nullptr) { |
| 1546 | UsualDeallocFnInfo Best; |
| 1547 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1548 | for (auto I = R.begin(), E = R.end(); I != E; ++I) { |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1549 | UsualDeallocFnInfo Info(S, I.getPair()); |
| 1550 | if (!Info || !isNonPlacementDeallocationFunction(S, Info.FD) || |
| 1551 | Info.CUDAPref == Sema::CFP_Never) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1552 | continue; |
| 1553 | |
| 1554 | if (!Best) { |
| 1555 | Best = Info; |
| 1556 | if (BestFns) |
| 1557 | BestFns->push_back(Info); |
| 1558 | continue; |
| 1559 | } |
| 1560 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1561 | if (Best.isBetterThan(Info, WantSize, WantAlign)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1562 | continue; |
| 1563 | |
| 1564 | // If more than one preferred function is found, all non-preferred |
| 1565 | // functions are eliminated from further consideration. |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1566 | if (BestFns && Info.isBetterThan(Best, WantSize, WantAlign)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1567 | BestFns->clear(); |
| 1568 | |
| 1569 | Best = Info; |
| 1570 | if (BestFns) |
| 1571 | BestFns->push_back(Info); |
| 1572 | } |
| 1573 | |
| 1574 | return Best; |
| 1575 | } |
| 1576 | |
| 1577 | /// Determine whether a given type is a class for which 'delete[]' would call |
| 1578 | /// a member 'operator delete[]' with a 'size_t' parameter. This implies that |
| 1579 | /// we need to store the array size (even if the type is |
| 1580 | /// trivially-destructible). |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1581 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
| 1582 | QualType allocType) { |
| 1583 | const RecordType *record = |
| 1584 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 1585 | if (!record) return false; |
| 1586 | |
| 1587 | // Try to find an operator delete[] in class scope. |
| 1588 | |
| 1589 | DeclarationName deleteName = |
| 1590 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
| 1591 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
| 1592 | S.LookupQualifiedName(ops, record->getDecl()); |
| 1593 | |
| 1594 | // We're just doing this for information. |
| 1595 | ops.suppressDiagnostics(); |
| 1596 | |
| 1597 | // Very likely: there's no operator delete[]. |
| 1598 | if (ops.empty()) return false; |
| 1599 | |
| 1600 | // If it's ambiguous, it should be illegal to call operator delete[] |
| 1601 | // on this thing, so it doesn't matter if we allocate extra space or not. |
| 1602 | if (ops.isAmbiguous()) return false; |
| 1603 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1604 | // C++17 [expr.delete]p10: |
| 1605 | // If the deallocation functions have class scope, the one without a |
| 1606 | // parameter of type std::size_t is selected. |
| 1607 | auto Best = resolveDeallocationOverload( |
| 1608 | S, ops, /*WantSize*/false, |
| 1609 | /*WantAlign*/hasNewExtendedAlignment(S, allocType)); |
| 1610 | return Best && Best.HasSizeT; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1611 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1612 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1613 | /// Parsed a C++ 'new' expression (C++ 5.3.4). |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 1614 | /// |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1615 | /// E.g.: |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1616 | /// @code new (memory) int[size][4] @endcode |
| 1617 | /// or |
| 1618 | /// @code ::new Foo(23, "hello") @endcode |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1619 | /// |
| 1620 | /// \param StartLoc The first location of the expression. |
| 1621 | /// \param UseGlobal True if 'new' was prefixed with '::'. |
| 1622 | /// \param PlacementLParen Opening paren of the placement arguments. |
| 1623 | /// \param PlacementArgs Placement new arguments. |
| 1624 | /// \param PlacementRParen Closing paren of the placement arguments. |
| 1625 | /// \param TypeIdParens If the type is in parens, the source range. |
| 1626 | /// \param D The type to be allocated, as well as array dimensions. |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 1627 | /// \param Initializer The initializing expression or initializer-list, or null |
| 1628 | /// if there is none. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1629 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1630 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1631 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1632 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1633 | Declarator &D, Expr *Initializer) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1634 | Expr *ArraySize = nullptr; |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1635 | // If the specified type is an array, unwrap it and save the expression. |
| 1636 | if (D.getNumTypeObjects() > 0 && |
| 1637 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1638 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
Richard Smith | bfbff07 | 2017-02-10 22:35:37 +0000 | [diff] [blame] | 1639 | if (D.getDeclSpec().hasAutoTypeSpec()) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1640 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
| 1641 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1642 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1643 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 1644 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1645 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1646 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 1647 | << D.getSourceRange()); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1648 | |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1649 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1650 | D.DropFirstTypeObject(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1653 | // Every dimension shall be of constant size. |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1654 | if (ArraySize) { |
| 1655 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1656 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 1657 | break; |
| 1658 | |
| 1659 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 1660 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1661 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent()) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1662 | if (getLangOpts().CPlusPlus14) { |
Fangrui Song | 99337e2 | 2018-07-20 08:19:20 +0000 | [diff] [blame^] | 1663 | // C++1y [expr.new]p6: Every constant-expression in a noptr-new-declarator |
| 1664 | // shall be a converted constant expression (5.19) of type std::size_t |
| 1665 | // and shall evaluate to a strictly positive value. |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1666 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
| 1667 | assert(IntWidth && "Builtin type of size 0?"); |
| 1668 | llvm::APSInt Value(IntWidth); |
| 1669 | Array.NumElts |
| 1670 | = CheckConvertedConstantExpression(NumElts, Context.getSizeType(), Value, |
| 1671 | CCEK_NewExpr) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1672 | .get(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1673 | } else { |
| 1674 | Array.NumElts |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1675 | = VerifyIntegerConstantExpression(NumElts, nullptr, |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1676 | diag::err_new_array_nonconst) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1677 | .get(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1678 | } |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1679 | if (!Array.NumElts) |
| 1680 | return ExprError(); |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | } |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1685 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1686 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/nullptr); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1687 | QualType AllocType = TInfo->getType(); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1688 | if (D.isInvalidType()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1689 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1690 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1691 | SourceRange DirectInitRange; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1692 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1693 | DirectInitRange = List->getSourceRange(); |
| 1694 | |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1695 | return BuildCXXNew(SourceRange(StartLoc, D.getLocEnd()), UseGlobal, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1696 | PlacementLParen, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1697 | PlacementArgs, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1698 | PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1699 | TypeIdParens, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1701 | TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1702 | ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1703 | DirectInitRange, |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1704 | Initializer); |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1707 | static bool isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style, |
| 1708 | Expr *Init) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1709 | if (!Init) |
| 1710 | return true; |
Sebastian Redl | eb54f08 | 2012-02-17 08:42:32 +0000 | [diff] [blame] | 1711 | if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) |
| 1712 | return PLE->getNumExprs() == 0; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1713 | if (isa<ImplicitValueInitExpr>(Init)) |
| 1714 | return true; |
| 1715 | else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) |
| 1716 | return !CCE->isListInitialization() && |
| 1717 | CCE->getConstructor()->isDefaultConstructor(); |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1718 | else if (Style == CXXNewExpr::ListInit) { |
| 1719 | assert(isa<InitListExpr>(Init) && |
| 1720 | "Shouldn't create list CXXConstructExprs for arrays."); |
| 1721 | return true; |
| 1722 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1723 | return false; |
| 1724 | } |
| 1725 | |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 1726 | // Emit a diagnostic if an aligned allocation/deallocation function that is not |
| 1727 | // implemented in the standard library is selected. |
| 1728 | static void diagnoseUnavailableAlignedAllocation(const FunctionDecl &FD, |
| 1729 | SourceLocation Loc, bool IsDelete, |
| 1730 | Sema &S) { |
| 1731 | if (!S.getLangOpts().AlignedAllocationUnavailable) |
| 1732 | return; |
| 1733 | |
| 1734 | // Return if there is a definition. |
| 1735 | if (FD.isDefined()) |
| 1736 | return; |
| 1737 | |
| 1738 | bool IsAligned = false; |
| 1739 | if (FD.isReplaceableGlobalAllocationFunction(&IsAligned) && IsAligned) { |
Akira Hatanaka | 3e40c30 | 2017-07-19 17:17:50 +0000 | [diff] [blame] | 1740 | const llvm::Triple &T = S.getASTContext().getTargetInfo().getTriple(); |
| 1741 | StringRef OSName = AvailabilityAttr::getPlatformNameSourceSpelling( |
| 1742 | S.getASTContext().getTargetInfo().getPlatformName()); |
| 1743 | |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 1744 | S.Diag(Loc, diag::warn_aligned_allocation_unavailable) |
Akira Hatanaka | 3e40c30 | 2017-07-19 17:17:50 +0000 | [diff] [blame] | 1745 | << IsDelete << FD.getType().getAsString() << OSName |
| 1746 | << alignedAllocMinVersion(T.getOS()).getAsString(); |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 1747 | S.Diag(Loc, diag::note_silence_unligned_allocation_unavailable); |
| 1748 | } |
| 1749 | } |
| 1750 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1751 | ExprResult |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1752 | Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1753 | SourceLocation PlacementLParen, |
| 1754 | MultiExprArg PlacementArgs, |
| 1755 | SourceLocation PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1756 | SourceRange TypeIdParens, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1757 | QualType AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1758 | TypeSourceInfo *AllocTypeInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1759 | Expr *ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1760 | SourceRange DirectInitRange, |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1761 | Expr *Initializer) { |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1762 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1763 | SourceLocation StartLoc = Range.getBegin(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1764 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1765 | CXXNewExpr::InitializationStyle initStyle; |
| 1766 | if (DirectInitRange.isValid()) { |
| 1767 | assert(Initializer && "Have parens but no initializer."); |
| 1768 | initStyle = CXXNewExpr::CallInit; |
| 1769 | } else if (Initializer && isa<InitListExpr>(Initializer)) |
| 1770 | initStyle = CXXNewExpr::ListInit; |
| 1771 | else { |
| 1772 | assert((!Initializer || isa<ImplicitValueInitExpr>(Initializer) || |
| 1773 | isa<CXXConstructExpr>(Initializer)) && |
| 1774 | "Initializer expression that cannot have been implicitly created."); |
| 1775 | initStyle = CXXNewExpr::NoInit; |
| 1776 | } |
| 1777 | |
| 1778 | Expr **Inits = &Initializer; |
| 1779 | unsigned NumInits = Initializer ? 1 : 0; |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 1780 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) { |
| 1781 | assert(initStyle == CXXNewExpr::CallInit && "paren init for non-call init"); |
| 1782 | Inits = List->getExprs(); |
| 1783 | NumInits = List->getNumExprs(); |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1786 | // C++11 [expr.new]p15: |
| 1787 | // A new-expression that creates an object of type T initializes that |
| 1788 | // object as follows: |
| 1789 | InitializationKind Kind |
| 1790 | // - If the new-initializer is omitted, the object is default- |
| 1791 | // initialized (8.5); if no initialization is performed, |
| 1792 | // the object has indeterminate value |
| 1793 | = initStyle == CXXNewExpr::NoInit |
| 1794 | ? InitializationKind::CreateDefault(TypeRange.getBegin()) |
| 1795 | // - Otherwise, the new-initializer is interpreted according to the |
| 1796 | // initialization rules of 8.5 for direct-initialization. |
| 1797 | : initStyle == CXXNewExpr::ListInit |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1798 | ? InitializationKind::CreateDirectList(TypeRange.getBegin(), |
| 1799 | Initializer->getLocStart(), |
| 1800 | Initializer->getLocEnd()) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1801 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
| 1802 | DirectInitRange.getBegin(), |
| 1803 | DirectInitRange.getEnd()); |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1804 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1805 | // C++11 [dcl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 1806 | auto *Deduced = AllocType->getContainedDeducedType(); |
| 1807 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
| 1808 | if (ArraySize) |
| 1809 | return ExprError(Diag(ArraySize->getExprLoc(), |
| 1810 | diag::err_deduced_class_template_compound_type) |
| 1811 | << /*array*/ 2 << ArraySize->getSourceRange()); |
| 1812 | |
| 1813 | InitializedEntity Entity |
| 1814 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
| 1815 | AllocType = DeduceTemplateSpecializationFromInitializer( |
| 1816 | AllocTypeInfo, Entity, Kind, MultiExprArg(Inits, NumInits)); |
| 1817 | if (AllocType.isNull()) |
| 1818 | return ExprError(); |
| 1819 | } else if (Deduced) { |
Zhihao Yuan | 00c9dfd | 2017-12-11 18:29:54 +0000 | [diff] [blame] | 1820 | bool Braced = (initStyle == CXXNewExpr::ListInit); |
| 1821 | if (NumInits == 1) { |
| 1822 | if (auto p = dyn_cast_or_null<InitListExpr>(Inits[0])) { |
| 1823 | Inits = p->getInits(); |
| 1824 | NumInits = p->getNumInits(); |
| 1825 | Braced = true; |
| 1826 | } |
| 1827 | } |
| 1828 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1829 | if (initStyle == CXXNewExpr::NoInit || NumInits == 0) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1830 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
| 1831 | << AllocType << TypeRange); |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1832 | if (NumInits > 1) { |
| 1833 | Expr *FirstBad = Inits[1]; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1834 | return ExprError(Diag(FirstBad->getLocStart(), |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1835 | diag::err_auto_new_ctor_multiple_expressions) |
| 1836 | << AllocType << TypeRange); |
| 1837 | } |
Zhihao Yuan | 00c9dfd | 2017-12-11 18:29:54 +0000 | [diff] [blame] | 1838 | if (Braced && !getLangOpts().CPlusPlus17) |
| 1839 | Diag(Initializer->getLocStart(), diag::ext_auto_new_list_init) |
| 1840 | << AllocType << TypeRange; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1841 | Expr *Deduce = Inits[0]; |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1842 | QualType DeducedType; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 1843 | if (DeduceAutoType(AllocTypeInfo, Deduce, DeducedType) == DAR_Failed) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1844 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1845 | << AllocType << Deduce->getType() |
| 1846 | << TypeRange << Deduce->getSourceRange()); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1847 | if (DeducedType.isNull()) |
Richard Smith | 9647d3c | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 1848 | return ExprError(); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1849 | AllocType = DeducedType; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1850 | } |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1851 | |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1852 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 1853 | // typedef of an array type. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1854 | if (!ArraySize) { |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1855 | if (const ConstantArrayType *Array |
| 1856 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1857 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 1858 | Context.getSizeType(), |
| 1859 | TypeRange.getEnd()); |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1860 | AllocType = Array->getElementType(); |
| 1861 | } |
| 1862 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1863 | |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1864 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 1865 | return ExprError(); |
| 1866 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1867 | // In ARC, infer 'retaining' for the allocated |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1868 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1869 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 1870 | AllocType->isObjCLifetimeType()) { |
| 1871 | AllocType = Context.getLifetimeQualifiedType(AllocType, |
| 1872 | AllocType->getObjCARCImplicitLifetime()); |
| 1873 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1874 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1875 | QualType ResultType = Context.getPointerType(AllocType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1876 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1877 | if (ArraySize && ArraySize->getType()->isNonOverloadPlaceholderType()) { |
| 1878 | ExprResult result = CheckPlaceholderExpr(ArraySize); |
| 1879 | if (result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1880 | ArraySize = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1881 | } |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 1882 | // C++98 5.3.4p6: "The expression in a direct-new-declarator shall have |
| 1883 | // integral or enumeration type with a non-negative value." |
| 1884 | // C++11 [expr.new]p6: The expression [...] shall be of integral or unscoped |
| 1885 | // enumeration type, or a class type for which a single non-explicit |
| 1886 | // conversion function to integral or unscoped enumeration type exists. |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1887 | // C++1y [expr.new]p6: The expression [...] is implicitly converted to |
Larisse Voufo | bf4aa57 | 2013-06-18 03:08:53 +0000 | [diff] [blame] | 1888 | // std::size_t. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1889 | llvm::Optional<uint64_t> KnownArraySize; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1890 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1891 | ExprResult ConvertedSize; |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1892 | if (getLangOpts().CPlusPlus14) { |
Alp Toker | 965f882 | 2013-11-27 05:22:15 +0000 | [diff] [blame] | 1893 | assert(Context.getTargetInfo().getIntWidth() && "Builtin type of size 0?"); |
| 1894 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1895 | ConvertedSize = PerformImplicitConversion(ArraySize, Context.getSizeType(), |
Fangrui Song | 99337e2 | 2018-07-20 08:19:20 +0000 | [diff] [blame^] | 1896 | AA_Converting); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1897 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1898 | if (!ConvertedSize.isInvalid() && |
Larisse Voufo | bf4aa57 | 2013-06-18 03:08:53 +0000 | [diff] [blame] | 1899 | ArraySize->getType()->getAs<RecordType>()) |
Larisse Voufo | 9f380c5 | 2013-06-18 01:27:47 +0000 | [diff] [blame] | 1900 | // Diagnose the compatibility of this conversion. |
| 1901 | Diag(StartLoc, diag::warn_cxx98_compat_array_size_conversion) |
| 1902 | << ArraySize->getType() << 0 << "'size_t'"; |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1903 | } else { |
| 1904 | class SizeConvertDiagnoser : public ICEConvertDiagnoser { |
| 1905 | protected: |
| 1906 | Expr *ArraySize; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1907 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1908 | public: |
| 1909 | SizeConvertDiagnoser(Expr *ArraySize) |
| 1910 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false, false, false), |
| 1911 | ArraySize(ArraySize) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1912 | |
| 1913 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 1914 | QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1915 | return S.Diag(Loc, diag::err_array_size_not_integral) |
| 1916 | << S.getLangOpts().CPlusPlus11 << T; |
| 1917 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1918 | |
| 1919 | SemaDiagnosticBuilder diagnoseIncomplete( |
| 1920 | Sema &S, SourceLocation Loc, QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1921 | return S.Diag(Loc, diag::err_array_size_incomplete_type) |
| 1922 | << T << ArraySize->getSourceRange(); |
| 1923 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1924 | |
| 1925 | SemaDiagnosticBuilder diagnoseExplicitConv( |
| 1926 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1927 | return S.Diag(Loc, diag::err_array_size_explicit_conversion) << T << ConvTy; |
| 1928 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1929 | |
| 1930 | SemaDiagnosticBuilder noteExplicitConv( |
| 1931 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1932 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
| 1933 | << ConvTy->isEnumeralType() << ConvTy; |
| 1934 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1935 | |
| 1936 | SemaDiagnosticBuilder diagnoseAmbiguous( |
| 1937 | Sema &S, SourceLocation Loc, QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1938 | return S.Diag(Loc, diag::err_array_size_ambiguous_conversion) << T; |
| 1939 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1940 | |
| 1941 | SemaDiagnosticBuilder noteAmbiguous( |
| 1942 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1943 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
| 1944 | << ConvTy->isEnumeralType() << ConvTy; |
| 1945 | } |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1946 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1947 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
| 1948 | QualType T, |
| 1949 | QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1950 | return S.Diag(Loc, |
| 1951 | S.getLangOpts().CPlusPlus11 |
| 1952 | ? diag::warn_cxx98_compat_array_size_conversion |
| 1953 | : diag::ext_array_size_conversion) |
| 1954 | << T << ConvTy->isEnumeralType() << ConvTy; |
| 1955 | } |
| 1956 | } SizeDiagnoser(ArraySize); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1957 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1958 | ConvertedSize = PerformContextualImplicitConversion(StartLoc, ArraySize, |
| 1959 | SizeDiagnoser); |
| 1960 | } |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1961 | if (ConvertedSize.isInvalid()) |
| 1962 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1963 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1964 | ArraySize = ConvertedSize.get(); |
John McCall | 9b80c21 | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 1965 | QualType SizeType = ArraySize->getType(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1966 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1967 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1968 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1969 | |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1970 | // C++98 [expr.new]p7: |
| 1971 | // The expression in a direct-new-declarator shall have integral type |
| 1972 | // with a non-negative value. |
| 1973 | // |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1974 | // Let's see if this is a constant < 0. If so, we reject it out of hand, |
| 1975 | // per CWG1464. Otherwise, if it's not a constant, we must have an |
| 1976 | // unparenthesized array type. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1977 | if (!ArraySize->isValueDependent()) { |
| 1978 | llvm::APSInt Value; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1979 | // We've already performed any required implicit conversion to integer or |
| 1980 | // unscoped enumeration type. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1981 | // FIXME: Per CWG1464, we are required to check the value prior to |
| 1982 | // converting to size_t. This will never find a negative array size in |
| 1983 | // C++14 onwards, because Value is always unsigned here! |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1984 | if (ArraySize->isIntegerConstantExpr(Value, Context)) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1985 | if (Value.isSigned() && Value.isNegative()) { |
| 1986 | return ExprError(Diag(ArraySize->getLocStart(), |
| 1987 | diag::err_typecheck_negative_array_size) |
| 1988 | << ArraySize->getSourceRange()); |
| 1989 | } |
| 1990 | |
| 1991 | if (!AllocType->isDependentType()) { |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1992 | unsigned ActiveSizeBits = |
| 1993 | ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1994 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) |
| 1995 | return ExprError(Diag(ArraySize->getLocStart(), |
| 1996 | diag::err_array_too_large) |
| 1997 | << Value.toString(10) |
| 1998 | << ArraySize->getSourceRange()); |
Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1999 | } |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2000 | |
| 2001 | KnownArraySize = Value.getZExtValue(); |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 2002 | } else if (TypeIdParens.isValid()) { |
| 2003 | // Can't have dynamic array size when the type-id is in parentheses. |
| 2004 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 2005 | << ArraySize->getSourceRange() |
| 2006 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 2007 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2008 | |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 2009 | TypeIdParens = SourceRange(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2010 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2011 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2012 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 2013 | // Note that we do *not* convert the argument in any way. It can |
| 2014 | // be signed, larger than size_t, whatever. |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2015 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2016 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2017 | FunctionDecl *OperatorNew = nullptr; |
| 2018 | FunctionDecl *OperatorDelete = nullptr; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2019 | unsigned Alignment = |
| 2020 | AllocType->isDependentType() ? 0 : Context.getTypeAlign(AllocType); |
| 2021 | unsigned NewAlignment = Context.getTargetInfo().getNewAlign(); |
| 2022 | bool PassAlignment = getLangOpts().AlignedAllocation && |
| 2023 | Alignment > NewAlignment; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2024 | |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 2025 | AllocationFunctionScope Scope = UseGlobal ? AFS_Global : AFS_Both; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2026 | if (!AllocType->isDependentType() && |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 2027 | !Expr::hasAnyTypeDependentArguments(PlacementArgs) && |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2028 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 2029 | SourceRange(PlacementLParen, PlacementRParen), |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 2030 | Scope, Scope, AllocType, ArraySize, PassAlignment, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2031 | PlacementArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2032 | return ExprError(); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 2033 | |
| 2034 | // If this is an array allocation, compute whether the usual array |
| 2035 | // deallocation function for the type has a size_t parameter. |
| 2036 | bool UsualArrayDeleteWantsSize = false; |
| 2037 | if (ArraySize && !AllocType->isDependentType()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2038 | UsualArrayDeleteWantsSize = |
| 2039 | doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 2040 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2041 | SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 1eab66c | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 2042 | if (OperatorNew) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2043 | const FunctionProtoType *Proto = |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 2044 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
| 2045 | VariadicCallType CallType = Proto->isVariadic() ? VariadicFunction |
| 2046 | : VariadicDoesNotApply; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2047 | |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 2048 | // We've already converted the placement args, just fill in any default |
| 2049 | // arguments. Skip the first parameter because we don't have a corresponding |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2050 | // argument. Skip the second parameter too if we're passing in the |
| 2051 | // alignment; we've already filled it in. |
| 2052 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, Proto, |
| 2053 | PassAlignment ? 2 : 1, PlacementArgs, |
| 2054 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 2055 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2056 | |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 2057 | if (!AllPlaceArgs.empty()) |
| 2058 | PlacementArgs = AllPlaceArgs; |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 2059 | |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 2060 | // FIXME: This is wrong: PlacementArgs misses out the first (size) argument. |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 2061 | DiagnoseSentinelCalls(OperatorNew, PlacementLParen, PlacementArgs); |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 2062 | |
| 2063 | // FIXME: Missing call to CheckFunctionCall or equivalent |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2064 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2065 | // Warn if the type is over-aligned and is being allocated by (unaligned) |
| 2066 | // global operator new. |
| 2067 | if (PlacementArgs.empty() && !PassAlignment && |
| 2068 | (OperatorNew->isImplicit() || |
| 2069 | (OperatorNew->getLocStart().isValid() && |
| 2070 | getSourceManager().isInSystemHeader(OperatorNew->getLocStart())))) { |
| 2071 | if (Alignment > NewAlignment) |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 2072 | Diag(StartLoc, diag::warn_overaligned_type) |
| 2073 | << AllocType |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2074 | << unsigned(Alignment / Context.getCharWidth()) |
| 2075 | << unsigned(NewAlignment / Context.getCharWidth()); |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2079 | // Array 'new' can't have any initializers except empty parentheses. |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 2080 | // Initializer lists are also allowed, in C++11. Rely on the parser for the |
| 2081 | // dialect distinction. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2082 | if (ArraySize && !isLegalArrayNewInitializer(initStyle, Initializer)) { |
| 2083 | SourceRange InitRange(Inits[0]->getLocStart(), |
| 2084 | Inits[NumInits - 1]->getLocEnd()); |
| 2085 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 2086 | return ExprError(); |
Anders Carlsson | c6bb0e1 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 2089 | // If we can perform the initialization, and we've not already done so, |
| 2090 | // do it now. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2091 | if (!AllocType->isDependentType() && |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2092 | !Expr::hasAnyTypeDependentArguments( |
Richard Smith | c6abd96 | 2014-07-25 01:12:44 +0000 | [diff] [blame] | 2093 | llvm::makeArrayRef(Inits, NumInits))) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2094 | // The type we initialize is the complete type, including the array bound. |
| 2095 | QualType InitType; |
| 2096 | if (KnownArraySize) |
| 2097 | InitType = Context.getConstantArrayType( |
| 2098 | AllocType, llvm::APInt(Context.getTypeSize(Context.getSizeType()), |
| 2099 | *KnownArraySize), |
| 2100 | ArrayType::Normal, 0); |
| 2101 | else if (ArraySize) |
| 2102 | InitType = |
| 2103 | Context.getIncompleteArrayType(AllocType, ArrayType::Normal, 0); |
| 2104 | else |
| 2105 | InitType = AllocType; |
| 2106 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2107 | InitializedEntity Entity |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 2108 | = InitializedEntity::InitializeNew(StartLoc, InitType); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2109 | InitializationSequence InitSeq(*this, Entity, Kind, |
| 2110 | MultiExprArg(Inits, NumInits)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2111 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2112 | MultiExprArg(Inits, NumInits)); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2113 | if (FullInit.isInvalid()) |
| 2114 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2115 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2116 | // FullInit is our initializer; strip off CXXBindTemporaryExprs, because |
| 2117 | // we don't want the initialized object to be destructed. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2118 | // FIXME: We should not create these in the first place. |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2119 | if (CXXBindTemporaryExpr *Binder = |
| 2120 | dyn_cast_or_null<CXXBindTemporaryExpr>(FullInit.get())) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2121 | FullInit = Binder->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2122 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2123 | Initializer = FullInit.get(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2124 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2125 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2126 | // Mark the new and delete operators as referenced. |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2127 | if (OperatorNew) { |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2128 | if (DiagnoseUseOfDecl(OperatorNew, StartLoc)) |
| 2129 | return ExprError(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2130 | MarkFunctionReferenced(StartLoc, OperatorNew); |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2131 | diagnoseUnavailableAlignedAllocation(*OperatorNew, StartLoc, false, *this); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2132 | } |
| 2133 | if (OperatorDelete) { |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2134 | if (DiagnoseUseOfDecl(OperatorDelete, StartLoc)) |
| 2135 | return ExprError(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2136 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2137 | diagnoseUnavailableAlignedAllocation(*OperatorDelete, StartLoc, true, *this); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2138 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2139 | |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 2140 | // C++0x [expr.new]p17: |
| 2141 | // If the new expression creates an array of objects of class type, |
| 2142 | // access and ambiguity control are done for the destructor. |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2143 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
| 2144 | if (ArraySize && !BaseAllocType->isDependentType()) { |
| 2145 | if (const RecordType *BaseRecordType = BaseAllocType->getAs<RecordType>()) { |
| 2146 | if (CXXDestructorDecl *dtor = LookupDestructor( |
| 2147 | cast<CXXRecordDecl>(BaseRecordType->getDecl()))) { |
| 2148 | MarkFunctionReferenced(StartLoc, dtor); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2149 | CheckDestructorAccess(StartLoc, dtor, |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2150 | PDiag(diag::err_access_dtor) |
| 2151 | << BaseAllocType); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2152 | if (DiagnoseUseOfDecl(dtor, StartLoc)) |
| 2153 | return ExprError(); |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2154 | } |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 2155 | } |
| 2156 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2157 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2158 | return new (Context) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2159 | CXXNewExpr(Context, UseGlobal, OperatorNew, OperatorDelete, PassAlignment, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2160 | UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens, |
| 2161 | ArraySize, initStyle, Initializer, ResultType, AllocTypeInfo, |
| 2162 | Range, DirectInitRange); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2165 | /// Checks that a type is suitable as the allocated type |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2166 | /// in a new-expression. |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2167 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | SourceRange R) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2169 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 2170 | // abstract class type or array thereof. |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2171 | if (AllocType->isFunctionType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2172 | return Diag(Loc, diag::err_bad_new_type) |
| 2173 | << AllocType << 0 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2174 | else if (AllocType->isReferenceType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2175 | return Diag(Loc, diag::err_bad_new_type) |
| 2176 | << AllocType << 1 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2177 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2178 | RequireCompleteType(Loc, AllocType, diag::err_new_incomplete_type,R)) |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2179 | return true; |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2180 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2181 | diag::err_allocation_of_abstract_type)) |
| 2182 | return true; |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 2183 | else if (AllocType->isVariablyModifiedType()) |
| 2184 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 2185 | << AllocType; |
Sven van Haastregt | e6e76fd | 2018-06-14 09:51:54 +0000 | [diff] [blame] | 2186 | else if (AllocType.getAddressSpace() != LangAS::Default && |
| 2187 | !getLangOpts().OpenCLCPlusPlus) |
Douglas Gregor | 39d1a09 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 2188 | return Diag(Loc, diag::err_address_space_qualified_new) |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 2189 | << AllocType.getUnqualifiedType() |
| 2190 | << AllocType.getQualifiers().getAddressSpaceAttributePrintValue(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2191 | else if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2192 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { |
| 2193 | QualType BaseAllocType = Context.getBaseElementType(AT); |
| 2194 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 2195 | BaseAllocType->isObjCLifetimeType()) |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 2196 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2197 | << BaseAllocType; |
| 2198 | } |
| 2199 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2200 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2201 | return false; |
| 2202 | } |
| 2203 | |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2204 | static bool resolveAllocationOverload( |
| 2205 | Sema &S, LookupResult &R, SourceRange Range, SmallVectorImpl<Expr *> &Args, |
| 2206 | bool &PassAlignment, FunctionDecl *&Operator, |
| 2207 | OverloadCandidateSet *AlignedCandidates, Expr *AlignArg, bool Diagnose) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2208 | OverloadCandidateSet Candidates(R.getNameLoc(), |
| 2209 | OverloadCandidateSet::CSK_Normal); |
| 2210 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 2211 | Alloc != AllocEnd; ++Alloc) { |
| 2212 | // Even member operator new/delete are implicitly treated as |
| 2213 | // static, so don't use AddMemberCandidate. |
| 2214 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
| 2215 | |
| 2216 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 2217 | S.AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
| 2218 | /*ExplicitTemplateArgs=*/nullptr, Args, |
| 2219 | Candidates, |
| 2220 | /*SuppressUserConversions=*/false); |
| 2221 | continue; |
| 2222 | } |
| 2223 | |
| 2224 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 2225 | S.AddOverloadCandidate(Fn, Alloc.getPair(), Args, Candidates, |
| 2226 | /*SuppressUserConversions=*/false); |
| 2227 | } |
| 2228 | |
| 2229 | // Do the resolution. |
| 2230 | OverloadCandidateSet::iterator Best; |
| 2231 | switch (Candidates.BestViableFunction(S, R.getNameLoc(), Best)) { |
| 2232 | case OR_Success: { |
| 2233 | // Got one! |
| 2234 | FunctionDecl *FnDecl = Best->Function; |
| 2235 | if (S.CheckAllocationAccess(R.getNameLoc(), Range, R.getNamingClass(), |
| 2236 | Best->FoundDecl) == Sema::AR_inaccessible) |
| 2237 | return true; |
| 2238 | |
| 2239 | Operator = FnDecl; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2240 | return false; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2241 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2242 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2243 | case OR_No_Viable_Function: |
| 2244 | // C++17 [expr.new]p13: |
| 2245 | // If no matching function is found and the allocated object type has |
| 2246 | // new-extended alignment, the alignment argument is removed from the |
| 2247 | // argument list, and overload resolution is performed again. |
| 2248 | if (PassAlignment) { |
| 2249 | PassAlignment = false; |
| 2250 | AlignArg = Args[1]; |
| 2251 | Args.erase(Args.begin() + 1); |
| 2252 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2253 | Operator, &Candidates, AlignArg, |
| 2254 | Diagnose); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2255 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2256 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2257 | // MSVC will fall back on trying to find a matching global operator new |
| 2258 | // if operator new[] cannot be found. Also, MSVC will leak by not |
| 2259 | // generating a call to operator delete or operator delete[], but we |
| 2260 | // will not replicate that bug. |
| 2261 | // FIXME: Find out how this interacts with the std::align_val_t fallback |
| 2262 | // once MSVC implements it. |
| 2263 | if (R.getLookupName().getCXXOverloadedOperator() == OO_Array_New && |
| 2264 | S.Context.getLangOpts().MSVCCompat) { |
| 2265 | R.clear(); |
| 2266 | R.setLookupName(S.Context.DeclarationNames.getCXXOperatorName(OO_New)); |
| 2267 | S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); |
| 2268 | // FIXME: This will give bad diagnostics pointing at the wrong functions. |
| 2269 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2270 | Operator, /*Candidates=*/nullptr, |
| 2271 | /*AlignArg=*/nullptr, Diagnose); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2272 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2273 | |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2274 | if (Diagnose) { |
| 2275 | S.Diag(R.getNameLoc(), diag::err_ovl_no_viable_function_in_call) |
| 2276 | << R.getLookupName() << Range; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2277 | |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2278 | // If we have aligned candidates, only note the align_val_t candidates |
| 2279 | // from AlignedCandidates and the non-align_val_t candidates from |
| 2280 | // Candidates. |
| 2281 | if (AlignedCandidates) { |
| 2282 | auto IsAligned = [](OverloadCandidate &C) { |
| 2283 | return C.Function->getNumParams() > 1 && |
| 2284 | C.Function->getParamDecl(1)->getType()->isAlignValT(); |
| 2285 | }; |
| 2286 | auto IsUnaligned = [&](OverloadCandidate &C) { return !IsAligned(C); }; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2287 | |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2288 | // This was an overaligned allocation, so list the aligned candidates |
| 2289 | // first. |
| 2290 | Args.insert(Args.begin() + 1, AlignArg); |
| 2291 | AlignedCandidates->NoteCandidates(S, OCD_AllCandidates, Args, "", |
| 2292 | R.getNameLoc(), IsAligned); |
| 2293 | Args.erase(Args.begin() + 1); |
| 2294 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args, "", R.getNameLoc(), |
| 2295 | IsUnaligned); |
| 2296 | } else { |
| 2297 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 2298 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2299 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2300 | return true; |
| 2301 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2302 | case OR_Ambiguous: |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2303 | if (Diagnose) { |
| 2304 | S.Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) |
| 2305 | << R.getLookupName() << Range; |
| 2306 | Candidates.NoteCandidates(S, OCD_ViableCandidates, Args); |
| 2307 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2308 | return true; |
| 2309 | |
| 2310 | case OR_Deleted: { |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2311 | if (Diagnose) { |
| 2312 | S.Diag(R.getNameLoc(), diag::err_ovl_deleted_call) |
| 2313 | << Best->Function->isDeleted() << R.getLookupName() |
| 2314 | << S.getDeletedOrUnavailableSuffix(Best->Function) << Range; |
| 2315 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 2316 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2317 | return true; |
| 2318 | } |
| 2319 | } |
| 2320 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 2323 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 2324 | AllocationFunctionScope NewScope, |
| 2325 | AllocationFunctionScope DeleteScope, |
| 2326 | QualType AllocType, bool IsArray, |
| 2327 | bool &PassAlignment, MultiExprArg PlaceArgs, |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2328 | FunctionDecl *&OperatorNew, |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2329 | FunctionDecl *&OperatorDelete, |
| 2330 | bool Diagnose) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2331 | // --- Choosing an allocation function --- |
| 2332 | // C++ 5.3.4p8 - 14 & 18 |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 2333 | // 1) If looking in AFS_Global scope for allocation functions, only look in |
| 2334 | // the global scope. Else, if AFS_Class, only look in the scope of the |
| 2335 | // allocated class. If AFS_Both, look in both. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2336 | // 2) If an array size is given, look for operator new[], else look for |
| 2337 | // operator new. |
| 2338 | // 3) The first argument is always size_t. Append the arguments from the |
| 2339 | // placement form. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2340 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2341 | SmallVector<Expr*, 8> AllocArgs; |
| 2342 | AllocArgs.reserve((PassAlignment ? 2 : 1) + PlaceArgs.size()); |
| 2343 | |
| 2344 | // We don't care about the actual value of these arguments. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2345 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 2346 | // tree? Or should the consumer just recalculate the value? |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2347 | // FIXME: Using a dummy value will interact poorly with attribute enable_if. |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2348 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2349 | Context.getTargetInfo().getPointerWidth(0)), |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2350 | Context.getSizeType(), |
| 2351 | SourceLocation()); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2352 | AllocArgs.push_back(&Size); |
| 2353 | |
| 2354 | QualType AlignValT = Context.VoidTy; |
| 2355 | if (PassAlignment) { |
| 2356 | DeclareGlobalNewDelete(); |
| 2357 | AlignValT = Context.getTypeDeclType(getStdAlignValT()); |
| 2358 | } |
| 2359 | CXXScalarValueInitExpr Align(AlignValT, nullptr, SourceLocation()); |
| 2360 | if (PassAlignment) |
| 2361 | AllocArgs.push_back(&Align); |
| 2362 | |
| 2363 | AllocArgs.insert(AllocArgs.end(), PlaceArgs.begin(), PlaceArgs.end()); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2364 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2365 | // C++ [expr.new]p8: |
| 2366 | // If the allocated type is a non-array type, the allocation |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2367 | // function's name is operator new and the deallocation function's |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2368 | // name is operator delete. If the allocated type is an array |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2369 | // type, the allocation function's name is operator new[] and the |
| 2370 | // deallocation function's name is operator delete[]. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2371 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2372 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2373 | |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2374 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 2375 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2376 | // Find the allocation function. |
| 2377 | { |
| 2378 | LookupResult R(*this, NewName, StartLoc, LookupOrdinaryName); |
| 2379 | |
| 2380 | // C++1z [expr.new]p9: |
| 2381 | // If the new-expression begins with a unary :: operator, the allocation |
| 2382 | // function's name is looked up in the global scope. Otherwise, if the |
| 2383 | // allocated type is a class type T or array thereof, the allocation |
| 2384 | // function's name is looked up in the scope of T. |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 2385 | if (AllocElemType->isRecordType() && NewScope != AFS_Global) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2386 | LookupQualifiedName(R, AllocElemType->getAsCXXRecordDecl()); |
| 2387 | |
| 2388 | // We can see ambiguity here if the allocation function is found in |
| 2389 | // multiple base classes. |
| 2390 | if (R.isAmbiguous()) |
| 2391 | return true; |
| 2392 | |
| 2393 | // If this lookup fails to find the name, or if the allocated type is not |
| 2394 | // a class type, the allocation function's name is looked up in the |
| 2395 | // global scope. |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 2396 | if (R.empty()) { |
| 2397 | if (NewScope == AFS_Class) |
| 2398 | return true; |
| 2399 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2400 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 2401 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2402 | |
Sven van Haastregt | e6e76fd | 2018-06-14 09:51:54 +0000 | [diff] [blame] | 2403 | if (getLangOpts().OpenCLCPlusPlus && R.empty()) { |
| 2404 | Diag(StartLoc, diag::err_openclcxx_not_supported) << "default new"; |
| 2405 | return true; |
| 2406 | } |
| 2407 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2408 | assert(!R.empty() && "implicitly declared allocation functions not found"); |
| 2409 | assert(!R.isAmbiguous() && "global allocation functions are ambiguous"); |
| 2410 | |
| 2411 | // We do our own custom access checks below. |
| 2412 | R.suppressDiagnostics(); |
| 2413 | |
| 2414 | if (resolveAllocationOverload(*this, R, Range, AllocArgs, PassAlignment, |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2415 | OperatorNew, /*Candidates=*/nullptr, |
| 2416 | /*AlignArg=*/nullptr, Diagnose)) |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2417 | return true; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2418 | } |
Aaron Ballman | 324fbee | 2013-05-30 01:55:39 +0000 | [diff] [blame] | 2419 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2420 | // We don't need an operator delete if we're running under -fno-exceptions. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2421 | if (!getLangOpts().Exceptions) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2422 | OperatorDelete = nullptr; |
John McCall | 0f55a03 | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 2423 | return false; |
| 2424 | } |
| 2425 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2426 | // Note, the name of OperatorNew might have been changed from array to |
| 2427 | // non-array by resolveAllocationOverload. |
| 2428 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 2429 | OperatorNew->getDeclName().getCXXOverloadedOperator() == OO_Array_New |
| 2430 | ? OO_Array_Delete |
| 2431 | : OO_Delete); |
| 2432 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2433 | // C++ [expr.new]p19: |
| 2434 | // |
| 2435 | // If the new-expression begins with a unary :: operator, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2436 | // deallocation function's name is looked up in the global |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2437 | // scope. Otherwise, if the allocated type is a class type T or an |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2438 | // array thereof, the deallocation function's name is looked up in |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2439 | // the scope of T. If this lookup fails to find the name, or if |
| 2440 | // the allocated type is not a class type or array thereof, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2441 | // deallocation function's name is looked up in the global scope. |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2442 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 2443 | if (AllocElemType->isRecordType() && DeleteScope != AFS_Global) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2444 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2445 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2446 | LookupQualifiedName(FoundDelete, RD); |
| 2447 | } |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 2448 | if (FoundDelete.isAmbiguous()) |
| 2449 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2450 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2451 | bool FoundGlobalDelete = FoundDelete.empty(); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2452 | if (FoundDelete.empty()) { |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 2453 | if (DeleteScope == AFS_Class) |
| 2454 | return true; |
| 2455 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2456 | DeclareGlobalNewDelete(); |
| 2457 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 2458 | } |
| 2459 | |
| 2460 | FoundDelete.suppressDiagnostics(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2461 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2462 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2463 | |
John McCall | d3be2c8 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 2464 | // Whether we're looking for a placement operator delete is dictated |
| 2465 | // by whether we selected a placement operator new, not by whether |
| 2466 | // we had explicit placement arguments. This matters for things like |
| 2467 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 2468 | // A *a = new A() |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2469 | // |
| 2470 | // We don't have any definition for what a "placement allocation function" |
| 2471 | // is, but we assume it's any allocation function whose |
| 2472 | // parameter-declaration-clause is anything other than (size_t). |
| 2473 | // |
| 2474 | // FIXME: Should (size_t, std::align_val_t) also be considered non-placement? |
| 2475 | // This affects whether an exception from the constructor of an overaligned |
| 2476 | // type uses the sized or non-sized form of aligned operator delete. |
| 2477 | bool isPlacementNew = !PlaceArgs.empty() || OperatorNew->param_size() != 1 || |
| 2478 | OperatorNew->isVariadic(); |
John McCall | d3be2c8 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 2479 | |
| 2480 | if (isPlacementNew) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2481 | // C++ [expr.new]p20: |
| 2482 | // A declaration of a placement deallocation function matches the |
| 2483 | // declaration of a placement allocation function if it has the |
| 2484 | // same number of parameters and, after parameter transformations |
| 2485 | // (8.3.5), all parameter types except the first are |
| 2486 | // identical. [...] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2487 | // |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2488 | // To perform this comparison, we compute the function type that |
| 2489 | // the deallocation function should have, and use that type both |
| 2490 | // for template argument deduction and for comparison purposes. |
| 2491 | QualType ExpectedFunctionType; |
| 2492 | { |
| 2493 | const FunctionProtoType *Proto |
| 2494 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2495 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2496 | SmallVector<QualType, 4> ArgTypes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2497 | ArgTypes.push_back(Context.VoidPtrTy); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2498 | for (unsigned I = 1, N = Proto->getNumParams(); I < N; ++I) |
| 2499 | ArgTypes.push_back(Proto->getParamType(I)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2500 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2501 | FunctionProtoType::ExtProtoInfo EPI; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2502 | // FIXME: This is not part of the standard's rule. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2503 | EPI.Variadic = Proto->isVariadic(); |
| 2504 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2505 | ExpectedFunctionType |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2506 | = Context.getFunctionType(Context.VoidTy, ArgTypes, EPI); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2509 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2510 | DEnd = FoundDelete.end(); |
| 2511 | D != DEnd; ++D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2512 | FunctionDecl *Fn = nullptr; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 2513 | if (FunctionTemplateDecl *FnTmpl = |
| 2514 | dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2515 | // Perform template argument deduction to try to match the |
| 2516 | // expected function type. |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 2517 | TemplateDeductionInfo Info(StartLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2518 | if (DeduceTemplateArguments(FnTmpl, nullptr, ExpectedFunctionType, Fn, |
| 2519 | Info)) |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2520 | continue; |
| 2521 | } else |
| 2522 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 2523 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 2524 | if (Context.hasSameType(adjustCCAndNoReturn(Fn->getType(), |
| 2525 | ExpectedFunctionType, |
| 2526 | /*AdjustExcpetionSpec*/true), |
| 2527 | ExpectedFunctionType)) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2528 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2529 | } |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 2530 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2531 | if (getLangOpts().CUDA) |
| 2532 | EraseUnwantedCUDAMatches(dyn_cast<FunctionDecl>(CurContext), Matches); |
| 2533 | } else { |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2534 | // C++1y [expr.new]p22: |
| 2535 | // For a non-placement allocation function, the normal deallocation |
| 2536 | // function lookup is used |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2537 | // |
| 2538 | // Per [expr.delete]p10, this lookup prefers a member operator delete |
| 2539 | // without a size_t argument, but prefers a non-member operator delete |
| 2540 | // with a size_t where possible (which it always is in this case). |
| 2541 | llvm::SmallVector<UsualDeallocFnInfo, 4> BestDeallocFns; |
| 2542 | UsualDeallocFnInfo Selected = resolveDeallocationOverload( |
| 2543 | *this, FoundDelete, /*WantSize*/ FoundGlobalDelete, |
| 2544 | /*WantAlign*/ hasNewExtendedAlignment(*this, AllocElemType), |
| 2545 | &BestDeallocFns); |
| 2546 | if (Selected) |
| 2547 | Matches.push_back(std::make_pair(Selected.Found, Selected.FD)); |
| 2548 | else { |
| 2549 | // If we failed to select an operator, all remaining functions are viable |
| 2550 | // but ambiguous. |
| 2551 | for (auto Fn : BestDeallocFns) |
| 2552 | Matches.push_back(std::make_pair(Fn.Found, Fn.FD)); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2553 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | // C++ [expr.new]p20: |
| 2557 | // [...] If the lookup finds a single matching deallocation |
| 2558 | // function, that function will be called; otherwise, no |
| 2559 | // deallocation function will be called. |
| 2560 | if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2561 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2562 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2563 | // C++1z [expr.new]p23: |
| 2564 | // If the lookup finds a usual deallocation function (3.7.4.2) |
| 2565 | // with a parameter of type std::size_t and that function, considered |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2566 | // as a placement deallocation function, would have been |
| 2567 | // selected as a match for the allocation function, the program |
| 2568 | // is ill-formed. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2569 | if (getLangOpts().CPlusPlus11 && isPlacementNew && |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2570 | isNonPlacementDeallocationFunction(*this, OperatorDelete)) { |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 2571 | UsualDeallocFnInfo Info(*this, |
| 2572 | DeclAccessPair::make(OperatorDelete, AS_public)); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2573 | // Core issue, per mail to core reflector, 2016-10-09: |
| 2574 | // If this is a member operator delete, and there is a corresponding |
| 2575 | // non-sized member operator delete, this isn't /really/ a sized |
| 2576 | // deallocation function, it just happens to have a size_t parameter. |
| 2577 | bool IsSizedDelete = Info.HasSizeT; |
| 2578 | if (IsSizedDelete && !FoundGlobalDelete) { |
| 2579 | auto NonSizedDelete = |
| 2580 | resolveDeallocationOverload(*this, FoundDelete, /*WantSize*/false, |
| 2581 | /*WantAlign*/Info.HasAlignValT); |
| 2582 | if (NonSizedDelete && !NonSizedDelete.HasSizeT && |
| 2583 | NonSizedDelete.HasAlignValT == Info.HasAlignValT) |
| 2584 | IsSizedDelete = false; |
| 2585 | } |
| 2586 | |
| 2587 | if (IsSizedDelete) { |
| 2588 | SourceRange R = PlaceArgs.empty() |
| 2589 | ? SourceRange() |
| 2590 | : SourceRange(PlaceArgs.front()->getLocStart(), |
| 2591 | PlaceArgs.back()->getLocEnd()); |
| 2592 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) << R; |
| 2593 | if (!OperatorDelete->isImplicit()) |
| 2594 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 2595 | << DeleteName; |
| 2596 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2597 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2598 | |
| 2599 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
| 2600 | Matches[0].first); |
| 2601 | } else if (!Matches.empty()) { |
| 2602 | // We found multiple suitable operators. Per [expr.new]p20, that means we |
| 2603 | // call no 'operator delete' function, but we should at least warn the user. |
| 2604 | // FIXME: Suppress this warning if the construction cannot throw. |
| 2605 | Diag(StartLoc, diag::warn_ambiguous_suitable_delete_function_found) |
| 2606 | << DeleteName << AllocElemType; |
| 2607 | |
| 2608 | for (auto &Match : Matches) |
| 2609 | Diag(Match.second->getLocation(), |
| 2610 | diag::note_member_declared_here) << DeleteName; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2613 | return false; |
| 2614 | } |
| 2615 | |
| 2616 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 2617 | /// delete. These are: |
| 2618 | /// @code |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2619 | /// // C++03: |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2620 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 2621 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 2622 | /// void operator delete(void *) throw(); |
| 2623 | /// void operator delete[](void *) throw(); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2624 | /// // C++11: |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2625 | /// void* operator new(std::size_t); |
| 2626 | /// void* operator new[](std::size_t); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2627 | /// void operator delete(void *) noexcept; |
| 2628 | /// void operator delete[](void *) noexcept; |
| 2629 | /// // C++1y: |
| 2630 | /// void* operator new(std::size_t); |
| 2631 | /// void* operator new[](std::size_t); |
| 2632 | /// void operator delete(void *) noexcept; |
| 2633 | /// void operator delete[](void *) noexcept; |
| 2634 | /// void operator delete(void *, std::size_t) noexcept; |
| 2635 | /// void operator delete[](void *, std::size_t) noexcept; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2636 | /// @endcode |
| 2637 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 2638 | /// declared. Their use requires including \<new\>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2640 | if (GlobalNewDeleteDeclared) |
| 2641 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2642 | |
Sven van Haastregt | e6e76fd | 2018-06-14 09:51:54 +0000 | [diff] [blame] | 2643 | // OpenCL C++ 1.0 s2.9: the implicitly declared new and delete operators |
| 2644 | // are not supported. |
| 2645 | if (getLangOpts().OpenCLCPlusPlus) |
| 2646 | return; |
| 2647 | |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2648 | // C++ [basic.std.dynamic]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2649 | // [...] The following allocation and deallocation functions (18.4) are |
| 2650 | // implicitly declared in global scope in each translation unit of a |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2651 | // program |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2652 | // |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2653 | // C++03: |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2654 | // void* operator new(std::size_t) throw(std::bad_alloc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2655 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 2656 | // void operator delete(void*) throw(); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2657 | // void operator delete[](void*) throw(); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2658 | // C++11: |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2659 | // void* operator new(std::size_t); |
| 2660 | // void* operator new[](std::size_t); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2661 | // void operator delete(void*) noexcept; |
| 2662 | // void operator delete[](void*) noexcept; |
| 2663 | // C++1y: |
| 2664 | // void* operator new(std::size_t); |
| 2665 | // void* operator new[](std::size_t); |
| 2666 | // void operator delete(void*) noexcept; |
| 2667 | // void operator delete[](void*) noexcept; |
| 2668 | // void operator delete(void*, std::size_t) noexcept; |
| 2669 | // void operator delete[](void*, std::size_t) noexcept; |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2670 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2671 | // These implicit declarations introduce only the function names operator |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2672 | // new, operator new[], operator delete, operator delete[]. |
| 2673 | // |
| 2674 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 2675 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 2676 | // However, we do not make these implicit declarations visible to name |
| 2677 | // lookup. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2678 | if (!StdBadAlloc && !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2679 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 2680 | // implicitly. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2681 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
| 2682 | getOrCreateStdNamespace(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2683 | SourceLocation(), SourceLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2684 | &PP.getIdentifierTable().get("bad_alloc"), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2685 | nullptr); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 2686 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2687 | } |
Richard Smith | 5913902 | 2016-09-30 22:41:36 +0000 | [diff] [blame] | 2688 | if (!StdAlignValT && getLangOpts().AlignedAllocation) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2689 | // The "std::align_val_t" enum class has not yet been declared, so build it |
| 2690 | // implicitly. |
| 2691 | auto *AlignValT = EnumDecl::Create( |
| 2692 | Context, getOrCreateStdNamespace(), SourceLocation(), SourceLocation(), |
| 2693 | &PP.getIdentifierTable().get("align_val_t"), nullptr, true, true, true); |
| 2694 | AlignValT->setIntegerType(Context.getSizeType()); |
| 2695 | AlignValT->setPromotionType(Context.getSizeType()); |
| 2696 | AlignValT->setImplicit(true); |
| 2697 | StdAlignValT = AlignValT; |
| 2698 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2699 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2700 | GlobalNewDeleteDeclared = true; |
| 2701 | |
| 2702 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 2703 | QualType SizeT = Context.getSizeType(); |
| 2704 | |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2705 | auto DeclareGlobalAllocationFunctions = [&](OverloadedOperatorKind Kind, |
| 2706 | QualType Return, QualType Param) { |
| 2707 | llvm::SmallVector<QualType, 3> Params; |
| 2708 | Params.push_back(Param); |
| 2709 | |
| 2710 | // Create up to four variants of the function (sized/aligned). |
| 2711 | bool HasSizedVariant = getLangOpts().SizedDeallocation && |
| 2712 | (Kind == OO_Delete || Kind == OO_Array_Delete); |
Richard Smith | 5913902 | 2016-09-30 22:41:36 +0000 | [diff] [blame] | 2713 | bool HasAlignedVariant = getLangOpts().AlignedAllocation; |
Simon Pilgrim | d69fc8e | 2016-09-30 14:18:06 +0000 | [diff] [blame] | 2714 | |
| 2715 | int NumSizeVariants = (HasSizedVariant ? 2 : 1); |
| 2716 | int NumAlignVariants = (HasAlignedVariant ? 2 : 1); |
| 2717 | for (int Sized = 0; Sized < NumSizeVariants; ++Sized) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2718 | if (Sized) |
| 2719 | Params.push_back(SizeT); |
| 2720 | |
Simon Pilgrim | d69fc8e | 2016-09-30 14:18:06 +0000 | [diff] [blame] | 2721 | for (int Aligned = 0; Aligned < NumAlignVariants; ++Aligned) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2722 | if (Aligned) |
| 2723 | Params.push_back(Context.getTypeDeclType(getStdAlignValT())); |
| 2724 | |
| 2725 | DeclareGlobalAllocationFunction( |
| 2726 | Context.DeclarationNames.getCXXOperatorName(Kind), Return, Params); |
| 2727 | |
| 2728 | if (Aligned) |
| 2729 | Params.pop_back(); |
| 2730 | } |
| 2731 | } |
| 2732 | }; |
| 2733 | |
| 2734 | DeclareGlobalAllocationFunctions(OO_New, VoidPtr, SizeT); |
| 2735 | DeclareGlobalAllocationFunctions(OO_Array_New, VoidPtr, SizeT); |
| 2736 | DeclareGlobalAllocationFunctions(OO_Delete, Context.VoidTy, VoidPtr); |
| 2737 | DeclareGlobalAllocationFunctions(OO_Array_Delete, Context.VoidTy, VoidPtr); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 2741 | /// allocation function if it doesn't already exist. |
| 2742 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2743 | QualType Return, |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2744 | ArrayRef<QualType> Params) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2745 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 2746 | |
| 2747 | // Check if this function is already declared. |
Serge Pavlov | d548907 | 2013-09-14 12:00:01 +0000 | [diff] [blame] | 2748 | DeclContext::lookup_result R = GlobalCtx->lookup(Name); |
| 2749 | for (DeclContext::lookup_iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 2750 | Alloc != AllocEnd; ++Alloc) { |
| 2751 | // Only look at non-template functions, as it is the predefined, |
| 2752 | // non-templated allocation function we are trying to declare here. |
| 2753 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2754 | if (Func->getNumParams() == Params.size()) { |
| 2755 | llvm::SmallVector<QualType, 3> FuncParams; |
| 2756 | for (auto *P : Func->parameters()) |
| 2757 | FuncParams.push_back( |
| 2758 | Context.getCanonicalType(P->getType().getUnqualifiedType())); |
| 2759 | if (llvm::makeArrayRef(FuncParams) == Params) { |
Serge Pavlov | d548907 | 2013-09-14 12:00:01 +0000 | [diff] [blame] | 2760 | // Make the function visible to name lookup, even if we found it in |
| 2761 | // an unimported module. It either is an implicitly-declared global |
Richard Smith | 42713d7 | 2013-07-14 02:01:48 +0000 | [diff] [blame] | 2762 | // allocation function, or is suppressing that function. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 2763 | Func->setVisibleDespiteOwningModule(); |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 2764 | return; |
Douglas Gregor | c1a42fd | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 2765 | } |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 2766 | } |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2767 | } |
| 2768 | } |
Reid Kleckner | 7270ef5 | 2015-03-19 17:03:58 +0000 | [diff] [blame] | 2769 | |
Richard Smith | c015bc2 | 2014-02-07 22:39:53 +0000 | [diff] [blame] | 2770 | FunctionProtoType::ExtProtoInfo EPI; |
| 2771 | |
Richard Smith | f8b417c | 2014-02-08 00:42:45 +0000 | [diff] [blame] | 2772 | QualType BadAllocType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | bool HasBadAllocExceptionSpec |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2774 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 2775 | Name.getCXXOverloadedOperator() == OO_Array_New); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2776 | if (HasBadAllocExceptionSpec) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2777 | if (!getLangOpts().CPlusPlus11) { |
Richard Smith | f8b417c | 2014-02-08 00:42:45 +0000 | [diff] [blame] | 2778 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Richard Smith | c015bc2 | 2014-02-07 22:39:53 +0000 | [diff] [blame] | 2779 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2780 | EPI.ExceptionSpec.Type = EST_Dynamic; |
| 2781 | EPI.ExceptionSpec.Exceptions = llvm::makeArrayRef(BadAllocType); |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2782 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2783 | } else { |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2784 | EPI.ExceptionSpec = |
| 2785 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2786 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2787 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2788 | auto CreateAllocationFunctionDecl = [&](Attr *ExtraAttr) { |
| 2789 | QualType FnType = Context.getFunctionType(Return, Params, EPI); |
| 2790 | FunctionDecl *Alloc = FunctionDecl::Create( |
| 2791 | Context, GlobalCtx, SourceLocation(), SourceLocation(), Name, |
| 2792 | FnType, /*TInfo=*/nullptr, SC_None, false, true); |
| 2793 | Alloc->setImplicit(); |
Vassil Vassilev | 41cafcd | 2017-06-09 21:36:28 +0000 | [diff] [blame] | 2794 | // Global allocation functions should always be visible. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 2795 | Alloc->setVisibleDespiteOwningModule(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2796 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2797 | // Implicit sized deallocation functions always have default visibility. |
| 2798 | Alloc->addAttr( |
| 2799 | VisibilityAttr::CreateImplicit(Context, VisibilityAttr::Default)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2800 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2801 | llvm::SmallVector<ParmVarDecl *, 3> ParamDecls; |
| 2802 | for (QualType T : Params) { |
| 2803 | ParamDecls.push_back(ParmVarDecl::Create( |
| 2804 | Context, Alloc, SourceLocation(), SourceLocation(), nullptr, T, |
| 2805 | /*TInfo=*/nullptr, SC_None, nullptr)); |
| 2806 | ParamDecls.back()->setImplicit(); |
| 2807 | } |
| 2808 | Alloc->setParams(ParamDecls); |
| 2809 | if (ExtraAttr) |
| 2810 | Alloc->addAttr(ExtraAttr); |
| 2811 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
| 2812 | IdResolver.tryAddTopLevelDecl(Alloc, Name); |
| 2813 | }; |
| 2814 | |
| 2815 | if (!LangOpts.CUDA) |
| 2816 | CreateAllocationFunctionDecl(nullptr); |
| 2817 | else { |
| 2818 | // Host and device get their own declaration so each can be |
| 2819 | // defined or re-declared independently. |
| 2820 | CreateAllocationFunctionDecl(CUDAHostAttr::CreateImplicit(Context)); |
| 2821 | CreateAllocationFunctionDecl(CUDADeviceAttr::CreateImplicit(Context)); |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2822 | } |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2825 | FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc, |
| 2826 | bool CanProvideSize, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2827 | bool Overaligned, |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2828 | DeclarationName Name) { |
| 2829 | DeclareGlobalNewDelete(); |
| 2830 | |
| 2831 | LookupResult FoundDelete(*this, Name, StartLoc, LookupOrdinaryName); |
| 2832 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 2833 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2834 | // FIXME: It's possible for this to result in ambiguity, through a |
| 2835 | // user-declared variadic operator delete or the enable_if attribute. We |
| 2836 | // should probably not consider those cases to be usual deallocation |
| 2837 | // functions. But for now we just make an arbitrary choice in that case. |
| 2838 | auto Result = resolveDeallocationOverload(*this, FoundDelete, CanProvideSize, |
| 2839 | Overaligned); |
| 2840 | assert(Result.FD && "operator delete missing from global scope?"); |
| 2841 | return Result.FD; |
| 2842 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2843 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2844 | FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc, |
| 2845 | CXXRecordDecl *RD) { |
| 2846 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2847 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2848 | FunctionDecl *OperatorDelete = nullptr; |
| 2849 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
| 2850 | return nullptr; |
| 2851 | if (OperatorDelete) |
| 2852 | return OperatorDelete; |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 2853 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2854 | // If there's no class-specific operator delete, look up the global |
| 2855 | // non-array delete. |
| 2856 | return FindUsualDeallocationFunction( |
| 2857 | Loc, true, hasNewExtendedAlignment(*this, Context.getRecordType(RD)), |
| 2858 | Name); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2861 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 2862 | DeclarationName Name, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2863 | FunctionDecl *&Operator, bool Diagnose) { |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2864 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2865 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2866 | LookupQualifiedName(Found, RD); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2867 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2868 | if (Found.isAmbiguous()) |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2869 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2870 | |
Chandler Carruth | b6f9917 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 2871 | Found.suppressDiagnostics(); |
| 2872 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2873 | bool Overaligned = hasNewExtendedAlignment(*this, Context.getRecordType(RD)); |
Chandler Carruth | 9b41823 | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 2874 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2875 | // C++17 [expr.delete]p10: |
| 2876 | // If the deallocation functions have class scope, the one without a |
| 2877 | // parameter of type std::size_t is selected. |
| 2878 | llvm::SmallVector<UsualDeallocFnInfo, 4> Matches; |
| 2879 | resolveDeallocationOverload(*this, Found, /*WantSize*/ false, |
| 2880 | /*WantAlign*/ Overaligned, &Matches); |
Chandler Carruth | 9b41823 | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 2881 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2882 | // If we could find an overload, use it. |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2883 | if (Matches.size() == 1) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2884 | Operator = cast<CXXMethodDecl>(Matches[0].FD); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2885 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2886 | // FIXME: DiagnoseUseOfDecl? |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2887 | if (Operator->isDeleted()) { |
| 2888 | if (Diagnose) { |
| 2889 | Diag(StartLoc, diag::err_deleted_function_use); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 2890 | NoteDeletedFunction(Operator); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2891 | } |
| 2892 | return true; |
| 2893 | } |
| 2894 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 2895 | if (CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2896 | Matches[0].Found, Diagnose) == AR_inaccessible) |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 2897 | return true; |
| 2898 | |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2899 | return false; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2900 | } |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2901 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2902 | // We found multiple suitable operators; complain about the ambiguity. |
| 2903 | // FIXME: The standard doesn't say to do this; it appears that the intent |
| 2904 | // is that this should never happen. |
| 2905 | if (!Matches.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2906 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2907 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 2908 | << Name << RD; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2909 | for (auto &Match : Matches) |
| 2910 | Diag(Match.FD->getLocation(), diag::note_member_declared_here) << Name; |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2911 | } |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2912 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | // We did find operator delete/operator delete[] declarations, but |
| 2916 | // none of them were suitable. |
| 2917 | if (!Found.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2918 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2919 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 2920 | << Name << RD; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2921 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2922 | for (NamedDecl *D : Found) |
| 2923 | Diag(D->getUnderlyingDecl()->getLocation(), |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2924 | diag::note_member_declared_here) << Name; |
| 2925 | } |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2926 | return true; |
| 2927 | } |
| 2928 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2929 | Operator = nullptr; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2930 | return false; |
| 2931 | } |
| 2932 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2933 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2934 | /// Checks whether delete-expression, and new-expression used for |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2935 | /// initializing deletee have the same array form. |
| 2936 | class MismatchingNewDeleteDetector { |
| 2937 | public: |
| 2938 | enum MismatchResult { |
| 2939 | /// Indicates that there is no mismatch or a mismatch cannot be proven. |
| 2940 | NoMismatch, |
| 2941 | /// Indicates that variable is initialized with mismatching form of \a new. |
| 2942 | VarInitMismatches, |
| 2943 | /// Indicates that member is initialized with mismatching form of \a new. |
| 2944 | MemberInitMismatches, |
| 2945 | /// Indicates that 1 or more constructors' definitions could not been |
| 2946 | /// analyzed, and they will be checked again at the end of translation unit. |
| 2947 | AnalyzeLater |
| 2948 | }; |
| 2949 | |
| 2950 | /// \param EndOfTU True, if this is the final analysis at the end of |
| 2951 | /// translation unit. False, if this is the initial analysis at the point |
| 2952 | /// delete-expression was encountered. |
| 2953 | explicit MismatchingNewDeleteDetector(bool EndOfTU) |
Alexander Shaposhnikov | 3087b2c | 2016-09-01 23:18:00 +0000 | [diff] [blame] | 2954 | : Field(nullptr), IsArrayForm(false), EndOfTU(EndOfTU), |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2955 | HasUndefinedConstructors(false) {} |
| 2956 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2957 | /// Checks whether pointee of a delete-expression is initialized with |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2958 | /// matching form of new-expression. |
| 2959 | /// |
| 2960 | /// If return value is \c VarInitMismatches or \c MemberInitMismatches at the |
| 2961 | /// point where delete-expression is encountered, then a warning will be |
| 2962 | /// issued immediately. If return value is \c AnalyzeLater at the point where |
| 2963 | /// delete-expression is seen, then member will be analyzed at the end of |
| 2964 | /// translation unit. \c AnalyzeLater is returned iff at least one constructor |
| 2965 | /// couldn't be analyzed. If at least one constructor initializes the member |
| 2966 | /// with matching type of new, the return value is \c NoMismatch. |
| 2967 | MismatchResult analyzeDeleteExpr(const CXXDeleteExpr *DE); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2968 | /// Analyzes a class member. |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2969 | /// \param Field Class member to analyze. |
| 2970 | /// \param DeleteWasArrayForm Array form-ness of the delete-expression used |
| 2971 | /// for deleting the \p Field. |
| 2972 | MismatchResult analyzeField(FieldDecl *Field, bool DeleteWasArrayForm); |
Alexander Shaposhnikov | 3087b2c | 2016-09-01 23:18:00 +0000 | [diff] [blame] | 2973 | FieldDecl *Field; |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2974 | /// List of mismatching new-expressions used for initialization of the pointee |
| 2975 | llvm::SmallVector<const CXXNewExpr *, 4> NewExprs; |
| 2976 | /// Indicates whether delete-expression was in array form. |
| 2977 | bool IsArrayForm; |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2978 | |
| 2979 | private: |
| 2980 | const bool EndOfTU; |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2981 | /// Indicates that there is at least one constructor without body. |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2982 | bool HasUndefinedConstructors; |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2983 | /// Returns \c CXXNewExpr from given initialization expression. |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2984 | /// \param E Expression used for initializing pointee in delete-expression. |
NAKAMURA Takumi | 4bd67d8 | 2015-05-19 06:47:23 +0000 | [diff] [blame] | 2985 | /// E can be a single-element \c InitListExpr consisting of new-expression. |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2986 | const CXXNewExpr *getNewExprFromInitListOrExpr(const Expr *E); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2987 | /// Returns whether member is initialized with mismatching form of |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2988 | /// \c new either by the member initializer or in-class initialization. |
| 2989 | /// |
| 2990 | /// If bodies of all constructors are not visible at the end of translation |
| 2991 | /// unit or at least one constructor initializes member with the matching |
| 2992 | /// form of \c new, mismatch cannot be proven, and this function will return |
| 2993 | /// \c NoMismatch. |
| 2994 | MismatchResult analyzeMemberExpr(const MemberExpr *ME); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2995 | /// Returns whether variable is initialized with mismatching form of |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2996 | /// \c new. |
| 2997 | /// |
| 2998 | /// If variable is initialized with matching form of \c new or variable is not |
| 2999 | /// initialized with a \c new expression, this function will return true. |
| 3000 | /// If variable is initialized with mismatching form of \c new, returns false. |
| 3001 | /// \param D Variable to analyze. |
| 3002 | bool hasMatchingVarInit(const DeclRefExpr *D); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3003 | /// Checks whether the constructor initializes pointee with mismatching |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3004 | /// form of \c new. |
| 3005 | /// |
| 3006 | /// Returns true, if member is initialized with matching form of \c new in |
| 3007 | /// member initializer list. Returns false, if member is initialized with the |
| 3008 | /// matching form of \c new in this constructor's initializer or given |
| 3009 | /// constructor isn't defined at the point where delete-expression is seen, or |
| 3010 | /// member isn't initialized by the constructor. |
| 3011 | bool hasMatchingNewInCtor(const CXXConstructorDecl *CD); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3012 | /// Checks whether member is initialized with matching form of |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3013 | /// \c new in member initializer list. |
| 3014 | bool hasMatchingNewInCtorInit(const CXXCtorInitializer *CI); |
| 3015 | /// Checks whether member is initialized with mismatching form of \c new by |
| 3016 | /// in-class initializer. |
| 3017 | MismatchResult analyzeInClassInitializer(); |
| 3018 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3019 | } |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3020 | |
| 3021 | MismatchingNewDeleteDetector::MismatchResult |
| 3022 | MismatchingNewDeleteDetector::analyzeDeleteExpr(const CXXDeleteExpr *DE) { |
| 3023 | NewExprs.clear(); |
| 3024 | assert(DE && "Expected delete-expression"); |
| 3025 | IsArrayForm = DE->isArrayForm(); |
| 3026 | const Expr *E = DE->getArgument()->IgnoreParenImpCasts(); |
| 3027 | if (const MemberExpr *ME = dyn_cast<const MemberExpr>(E)) { |
| 3028 | return analyzeMemberExpr(ME); |
| 3029 | } else if (const DeclRefExpr *D = dyn_cast<const DeclRefExpr>(E)) { |
| 3030 | if (!hasMatchingVarInit(D)) |
| 3031 | return VarInitMismatches; |
| 3032 | } |
| 3033 | return NoMismatch; |
| 3034 | } |
| 3035 | |
| 3036 | const CXXNewExpr * |
| 3037 | MismatchingNewDeleteDetector::getNewExprFromInitListOrExpr(const Expr *E) { |
| 3038 | assert(E != nullptr && "Expected a valid initializer expression"); |
| 3039 | E = E->IgnoreParenImpCasts(); |
| 3040 | if (const InitListExpr *ILE = dyn_cast<const InitListExpr>(E)) { |
| 3041 | if (ILE->getNumInits() == 1) |
| 3042 | E = dyn_cast<const CXXNewExpr>(ILE->getInit(0)->IgnoreParenImpCasts()); |
| 3043 | } |
| 3044 | |
| 3045 | return dyn_cast_or_null<const CXXNewExpr>(E); |
| 3046 | } |
| 3047 | |
| 3048 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtorInit( |
| 3049 | const CXXCtorInitializer *CI) { |
| 3050 | const CXXNewExpr *NE = nullptr; |
| 3051 | if (Field == CI->getMember() && |
| 3052 | (NE = getNewExprFromInitListOrExpr(CI->getInit()))) { |
| 3053 | if (NE->isArray() == IsArrayForm) |
| 3054 | return true; |
| 3055 | else |
| 3056 | NewExprs.push_back(NE); |
| 3057 | } |
| 3058 | return false; |
| 3059 | } |
| 3060 | |
| 3061 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtor( |
| 3062 | const CXXConstructorDecl *CD) { |
| 3063 | if (CD->isImplicit()) |
| 3064 | return false; |
| 3065 | const FunctionDecl *Definition = CD; |
| 3066 | if (!CD->isThisDeclarationADefinition() && !CD->isDefined(Definition)) { |
| 3067 | HasUndefinedConstructors = true; |
| 3068 | return EndOfTU; |
| 3069 | } |
| 3070 | for (const auto *CI : cast<const CXXConstructorDecl>(Definition)->inits()) { |
| 3071 | if (hasMatchingNewInCtorInit(CI)) |
| 3072 | return true; |
| 3073 | } |
| 3074 | return false; |
| 3075 | } |
| 3076 | |
| 3077 | MismatchingNewDeleteDetector::MismatchResult |
| 3078 | MismatchingNewDeleteDetector::analyzeInClassInitializer() { |
| 3079 | assert(Field != nullptr && "This should be called only for members"); |
Ismail Pazarbasi | 7ff1836 | 2015-10-26 19:20:24 +0000 | [diff] [blame] | 3080 | const Expr *InitExpr = Field->getInClassInitializer(); |
| 3081 | if (!InitExpr) |
| 3082 | return EndOfTU ? NoMismatch : AnalyzeLater; |
| 3083 | if (const CXXNewExpr *NE = getNewExprFromInitListOrExpr(InitExpr)) { |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3084 | if (NE->isArray() != IsArrayForm) { |
| 3085 | NewExprs.push_back(NE); |
| 3086 | return MemberInitMismatches; |
| 3087 | } |
| 3088 | } |
| 3089 | return NoMismatch; |
| 3090 | } |
| 3091 | |
| 3092 | MismatchingNewDeleteDetector::MismatchResult |
| 3093 | MismatchingNewDeleteDetector::analyzeField(FieldDecl *Field, |
| 3094 | bool DeleteWasArrayForm) { |
| 3095 | assert(Field != nullptr && "Analysis requires a valid class member."); |
| 3096 | this->Field = Field; |
| 3097 | IsArrayForm = DeleteWasArrayForm; |
| 3098 | const CXXRecordDecl *RD = cast<const CXXRecordDecl>(Field->getParent()); |
| 3099 | for (const auto *CD : RD->ctors()) { |
| 3100 | if (hasMatchingNewInCtor(CD)) |
| 3101 | return NoMismatch; |
| 3102 | } |
| 3103 | if (HasUndefinedConstructors) |
| 3104 | return EndOfTU ? NoMismatch : AnalyzeLater; |
| 3105 | if (!NewExprs.empty()) |
| 3106 | return MemberInitMismatches; |
| 3107 | return Field->hasInClassInitializer() ? analyzeInClassInitializer() |
| 3108 | : NoMismatch; |
| 3109 | } |
| 3110 | |
| 3111 | MismatchingNewDeleteDetector::MismatchResult |
| 3112 | MismatchingNewDeleteDetector::analyzeMemberExpr(const MemberExpr *ME) { |
| 3113 | assert(ME != nullptr && "Expected a member expression"); |
| 3114 | if (FieldDecl *F = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 3115 | return analyzeField(F, IsArrayForm); |
| 3116 | return NoMismatch; |
| 3117 | } |
| 3118 | |
| 3119 | bool MismatchingNewDeleteDetector::hasMatchingVarInit(const DeclRefExpr *D) { |
| 3120 | const CXXNewExpr *NE = nullptr; |
| 3121 | if (const VarDecl *VD = dyn_cast<const VarDecl>(D->getDecl())) { |
| 3122 | if (VD->hasInit() && (NE = getNewExprFromInitListOrExpr(VD->getInit())) && |
| 3123 | NE->isArray() != IsArrayForm) { |
| 3124 | NewExprs.push_back(NE); |
| 3125 | } |
| 3126 | } |
| 3127 | return NewExprs.empty(); |
| 3128 | } |
| 3129 | |
| 3130 | static void |
| 3131 | DiagnoseMismatchedNewDelete(Sema &SemaRef, SourceLocation DeleteLoc, |
| 3132 | const MismatchingNewDeleteDetector &Detector) { |
| 3133 | SourceLocation EndOfDelete = SemaRef.getLocForEndOfToken(DeleteLoc); |
| 3134 | FixItHint H; |
| 3135 | if (!Detector.IsArrayForm) |
| 3136 | H = FixItHint::CreateInsertion(EndOfDelete, "[]"); |
| 3137 | else { |
| 3138 | SourceLocation RSquare = Lexer::findLocationAfterToken( |
| 3139 | DeleteLoc, tok::l_square, SemaRef.getSourceManager(), |
| 3140 | SemaRef.getLangOpts(), true); |
| 3141 | if (RSquare.isValid()) |
| 3142 | H = FixItHint::CreateRemoval(SourceRange(EndOfDelete, RSquare)); |
| 3143 | } |
| 3144 | SemaRef.Diag(DeleteLoc, diag::warn_mismatched_delete_new) |
| 3145 | << Detector.IsArrayForm << H; |
| 3146 | |
| 3147 | for (const auto *NE : Detector.NewExprs) |
| 3148 | SemaRef.Diag(NE->getExprLoc(), diag::note_allocated_here) |
| 3149 | << Detector.IsArrayForm; |
| 3150 | } |
| 3151 | |
| 3152 | void Sema::AnalyzeDeleteExprMismatch(const CXXDeleteExpr *DE) { |
| 3153 | if (Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) |
| 3154 | return; |
| 3155 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/false); |
| 3156 | switch (Detector.analyzeDeleteExpr(DE)) { |
| 3157 | case MismatchingNewDeleteDetector::VarInitMismatches: |
| 3158 | case MismatchingNewDeleteDetector::MemberInitMismatches: { |
| 3159 | DiagnoseMismatchedNewDelete(*this, DE->getLocStart(), Detector); |
| 3160 | break; |
| 3161 | } |
| 3162 | case MismatchingNewDeleteDetector::AnalyzeLater: { |
| 3163 | DeleteExprs[Detector.Field].push_back( |
| 3164 | std::make_pair(DE->getLocStart(), DE->isArrayForm())); |
| 3165 | break; |
| 3166 | } |
| 3167 | case MismatchingNewDeleteDetector::NoMismatch: |
| 3168 | break; |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | void Sema::AnalyzeDeleteExprMismatch(FieldDecl *Field, SourceLocation DeleteLoc, |
| 3173 | bool DeleteWasArrayForm) { |
| 3174 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/true); |
| 3175 | switch (Detector.analyzeField(Field, DeleteWasArrayForm)) { |
| 3176 | case MismatchingNewDeleteDetector::VarInitMismatches: |
| 3177 | llvm_unreachable("This analysis should have been done for class members."); |
| 3178 | case MismatchingNewDeleteDetector::AnalyzeLater: |
| 3179 | llvm_unreachable("Analysis cannot be postponed any point beyond end of " |
| 3180 | "translation unit."); |
| 3181 | case MismatchingNewDeleteDetector::MemberInitMismatches: |
| 3182 | DiagnoseMismatchedNewDelete(*this, DeleteLoc, Detector); |
| 3183 | break; |
| 3184 | case MismatchingNewDeleteDetector::NoMismatch: |
| 3185 | break; |
| 3186 | } |
| 3187 | } |
| 3188 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3189 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 3190 | /// @code ::delete ptr; @endcode |
| 3191 | /// or |
| 3192 | /// @code delete [] ptr; @endcode |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3193 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3194 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3195 | bool ArrayForm, Expr *ExE) { |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3196 | // C++ [expr.delete]p1: |
| 3197 | // The operand shall have a pointer type, or a class type having a single |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3198 | // non-explicit conversion function to a pointer type. The result has type |
| 3199 | // void. |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3200 | // |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3201 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 3202 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3203 | ExprResult Ex = ExE; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3204 | FunctionDecl *OperatorDelete = nullptr; |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3205 | bool ArrayFormAsWritten = ArrayForm; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3206 | bool UsualArrayDeleteWantsSize = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3208 | if (!Ex.get()->isTypeDependent()) { |
John McCall | ef42902 | 2012-03-09 04:08:29 +0000 | [diff] [blame] | 3209 | // Perform lvalue-to-rvalue cast, if needed. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3210 | Ex = DefaultLvalueConversion(Ex.get()); |
Eli Friedman | 89a4a2c | 2012-12-13 00:37:17 +0000 | [diff] [blame] | 3211 | if (Ex.isInvalid()) |
| 3212 | return ExprError(); |
John McCall | ef42902 | 2012-03-09 04:08:29 +0000 | [diff] [blame] | 3213 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3214 | QualType Type = Ex.get()->getType(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3215 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3216 | class DeleteConverter : public ContextualImplicitConverter { |
| 3217 | public: |
| 3218 | DeleteConverter() : ContextualImplicitConverter(false, true) {} |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3219 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3220 | bool match(QualType ConvType) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3221 | // FIXME: If we have an operator T* and an operator void*, we must pick |
| 3222 | // the operator T*. |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3223 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3224 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3225 | return true; |
| 3226 | return false; |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3227 | } |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3228 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3229 | SemaDiagnosticBuilder diagnoseNoMatch(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3230 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3231 | return S.Diag(Loc, diag::err_delete_operand) << T; |
| 3232 | } |
| 3233 | |
| 3234 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3235 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3236 | return S.Diag(Loc, diag::err_delete_incomplete_class_type) << T; |
| 3237 | } |
| 3238 | |
| 3239 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3240 | QualType T, |
| 3241 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3242 | return S.Diag(Loc, diag::err_delete_explicit_conversion) << T << ConvTy; |
| 3243 | } |
| 3244 | |
| 3245 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3246 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3247 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
| 3248 | << ConvTy; |
| 3249 | } |
| 3250 | |
| 3251 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3252 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3253 | return S.Diag(Loc, diag::err_ambiguous_delete_operand) << T; |
| 3254 | } |
| 3255 | |
| 3256 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3257 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3258 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
| 3259 | << ConvTy; |
| 3260 | } |
| 3261 | |
| 3262 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3263 | QualType T, |
| 3264 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3265 | llvm_unreachable("conversion functions are permitted"); |
| 3266 | } |
| 3267 | } Converter; |
| 3268 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3269 | Ex = PerformContextualImplicitConversion(StartLoc, Ex.get(), Converter); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3270 | if (Ex.isInvalid()) |
| 3271 | return ExprError(); |
| 3272 | Type = Ex.get()->getType(); |
| 3273 | if (!Converter.match(Type)) |
| 3274 | // FIXME: PerformContextualImplicitConversion should return ExprError |
| 3275 | // itself in this case. |
| 3276 | return ExprError(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3277 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3278 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3279 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 3280 | |
Sven van Haastregt | e6e76fd | 2018-06-14 09:51:54 +0000 | [diff] [blame] | 3281 | if (Pointee.getAddressSpace() != LangAS::Default && |
| 3282 | !getLangOpts().OpenCLCPlusPlus) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3283 | return Diag(Ex.get()->getLocStart(), |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3284 | diag::err_address_space_qualified_delete) |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 3285 | << Pointee.getUnqualifiedType() |
| 3286 | << Pointee.getQualifiers().getAddressSpaceAttributePrintValue(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3287 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3288 | CXXRecordDecl *PointeeRD = nullptr; |
Douglas Gregor | bb3348e | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 3289 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | // The C++ standard bans deleting a pointer to a non-object type, which |
Douglas Gregor | bb3348e | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 3291 | // effectively bans deletion of "void*". However, most compilers support |
| 3292 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 3293 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3294 | << Type << Ex.get()->getSourceRange(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3295 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3296 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3297 | << Type << Ex.get()->getSourceRange()); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3298 | } else if (!Pointee->isDependentType()) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3299 | // FIXME: This can result in errors if the definition was imported from a |
| 3300 | // module but is hidden. |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3301 | if (!RequireCompleteType(StartLoc, Pointee, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3302 | diag::warn_delete_incomplete, Ex.get())) { |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3303 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
| 3304 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3305 | } |
| 3306 | } |
| 3307 | |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3308 | if (Pointee->isArrayType() && !ArrayForm) { |
| 3309 | Diag(StartLoc, diag::warn_delete_array_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3310 | << Type << Ex.get()->getSourceRange() |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 3311 | << FixItHint::CreateInsertion(getLocForEndOfToken(StartLoc), "[]"); |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3312 | ArrayForm = true; |
| 3313 | } |
| 3314 | |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 3315 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 3316 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 3317 | |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3318 | if (PointeeRD) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3319 | if (!UseGlobal && |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3320 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, |
| 3321 | OperatorDelete)) |
Anders Carlsson | 654e5c7 | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 3322 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3323 | |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3324 | // If we're allocating an array of records, check whether the |
| 3325 | // usual operator delete[] has a size_t parameter. |
| 3326 | if (ArrayForm) { |
| 3327 | // If the user specifically asked to use the global allocator, |
| 3328 | // we'll need to do the lookup into the class. |
| 3329 | if (UseGlobal) |
| 3330 | UsualArrayDeleteWantsSize = |
| 3331 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); |
| 3332 | |
| 3333 | // Otherwise, the usual operator delete[] should be the |
| 3334 | // function we just found. |
Richard Smith | f03bd30 | 2013-12-05 08:30:59 +0000 | [diff] [blame] | 3335 | else if (OperatorDelete && isa<CXXMethodDecl>(OperatorDelete)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3336 | UsualArrayDeleteWantsSize = |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 3337 | UsualDeallocFnInfo(*this, |
| 3338 | DeclAccessPair::make(OperatorDelete, AS_public)) |
| 3339 | .HasSizeT; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3342 | if (!PointeeRD->hasIrrelevantDestructor()) |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3343 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3344 | MarkFunctionReferenced(StartLoc, |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3345 | const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 3346 | if (DiagnoseUseOfDecl(Dtor, StartLoc)) |
| 3347 | return ExprError(); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3348 | } |
Argyrios Kyrtzidis | 8bd4285 | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 3349 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3350 | CheckVirtualDtorCall(PointeeRD->getDestructor(), StartLoc, |
| 3351 | /*IsDelete=*/true, /*CallCanBeVirtual=*/true, |
| 3352 | /*WarnOnNonAbstractTypes=*/!ArrayForm, |
| 3353 | SourceLocation()); |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 3354 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3356 | if (!OperatorDelete) { |
Sven van Haastregt | e6e76fd | 2018-06-14 09:51:54 +0000 | [diff] [blame] | 3357 | if (getLangOpts().OpenCLCPlusPlus) { |
| 3358 | Diag(StartLoc, diag::err_openclcxx_not_supported) << "default delete"; |
| 3359 | return ExprError(); |
| 3360 | } |
| 3361 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3362 | bool IsComplete = isCompleteType(StartLoc, Pointee); |
| 3363 | bool CanProvideSize = |
| 3364 | IsComplete && (!ArrayForm || UsualArrayDeleteWantsSize || |
| 3365 | Pointee.isDestructedType()); |
| 3366 | bool Overaligned = hasNewExtendedAlignment(*this, Pointee); |
| 3367 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 3368 | // Look for a global declaration. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3369 | OperatorDelete = FindUsualDeallocationFunction(StartLoc, CanProvideSize, |
| 3370 | Overaligned, DeleteName); |
| 3371 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3372 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3373 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3374 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 3375 | // Check access and ambiguity of destructor if we're going to call it. |
| 3376 | // Note that this is required even for a virtual delete. |
| 3377 | bool IsVirtualDelete = false; |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3378 | if (PointeeRD) { |
| 3379 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 3380 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
| 3381 | PDiag(diag::err_access_dtor) << PointeeElem); |
| 3382 | IsVirtualDelete = Dtor->isVirtual(); |
Douglas Gregor | fa77813 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 3383 | } |
| 3384 | } |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 3385 | |
| 3386 | diagnoseUnavailableAlignedAllocation(*OperatorDelete, StartLoc, true, |
| 3387 | *this); |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 3388 | |
| 3389 | // Convert the operand to the type of the first parameter of operator |
| 3390 | // delete. This is only necessary if we selected a destroying operator |
| 3391 | // delete that we are going to call (non-virtually); converting to void* |
| 3392 | // is trivial and left to AST consumers to handle. |
| 3393 | QualType ParamType = OperatorDelete->getParamDecl(0)->getType(); |
| 3394 | if (!IsVirtualDelete && !ParamType->getPointeeType()->isVoidType()) { |
Richard Smith | 2517201 | 2017-12-05 23:54:25 +0000 | [diff] [blame] | 3395 | Qualifiers Qs = Pointee.getQualifiers(); |
| 3396 | if (Qs.hasCVRQualifiers()) { |
| 3397 | // Qualifiers are irrelevant to this conversion; we're only looking |
| 3398 | // for access and ambiguity. |
| 3399 | Qs.removeCVRQualifiers(); |
| 3400 | QualType Unqual = Context.getPointerType( |
| 3401 | Context.getQualifiedType(Pointee.getUnqualifiedType(), Qs)); |
| 3402 | Ex = ImpCastExprToType(Ex.get(), Unqual, CK_NoOp); |
| 3403 | } |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 3404 | Ex = PerformImplicitConversion(Ex.get(), ParamType, AA_Passing); |
| 3405 | if (Ex.isInvalid()) |
| 3406 | return ExprError(); |
| 3407 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3410 | CXXDeleteExpr *Result = new (Context) CXXDeleteExpr( |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3411 | Context.VoidTy, UseGlobal, ArrayForm, ArrayFormAsWritten, |
| 3412 | UsualArrayDeleteWantsSize, OperatorDelete, Ex.get(), StartLoc); |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3413 | AnalyzeDeleteExprMismatch(Result); |
| 3414 | return Result; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
Eric Fiselier | fa752f2 | 2018-03-21 19:19:48 +0000 | [diff] [blame] | 3417 | static bool resolveBuiltinNewDeleteOverload(Sema &S, CallExpr *TheCall, |
| 3418 | bool IsDelete, |
| 3419 | FunctionDecl *&Operator) { |
| 3420 | |
| 3421 | DeclarationName NewName = S.Context.DeclarationNames.getCXXOperatorName( |
| 3422 | IsDelete ? OO_Delete : OO_New); |
| 3423 | |
| 3424 | LookupResult R(S, NewName, TheCall->getLocStart(), Sema::LookupOrdinaryName); |
| 3425 | S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); |
| 3426 | assert(!R.empty() && "implicitly declared allocation functions not found"); |
| 3427 | assert(!R.isAmbiguous() && "global allocation functions are ambiguous"); |
| 3428 | |
| 3429 | // We do our own custom access checks below. |
| 3430 | R.suppressDiagnostics(); |
| 3431 | |
| 3432 | SmallVector<Expr *, 8> Args(TheCall->arg_begin(), TheCall->arg_end()); |
| 3433 | OverloadCandidateSet Candidates(R.getNameLoc(), |
| 3434 | OverloadCandidateSet::CSK_Normal); |
| 3435 | for (LookupResult::iterator FnOvl = R.begin(), FnOvlEnd = R.end(); |
| 3436 | FnOvl != FnOvlEnd; ++FnOvl) { |
| 3437 | // Even member operator new/delete are implicitly treated as |
| 3438 | // static, so don't use AddMemberCandidate. |
| 3439 | NamedDecl *D = (*FnOvl)->getUnderlyingDecl(); |
| 3440 | |
| 3441 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3442 | S.AddTemplateOverloadCandidate(FnTemplate, FnOvl.getPair(), |
| 3443 | /*ExplicitTemplateArgs=*/nullptr, Args, |
| 3444 | Candidates, |
| 3445 | /*SuppressUserConversions=*/false); |
| 3446 | continue; |
| 3447 | } |
| 3448 | |
| 3449 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 3450 | S.AddOverloadCandidate(Fn, FnOvl.getPair(), Args, Candidates, |
| 3451 | /*SuppressUserConversions=*/false); |
| 3452 | } |
| 3453 | |
| 3454 | SourceRange Range = TheCall->getSourceRange(); |
| 3455 | |
| 3456 | // Do the resolution. |
| 3457 | OverloadCandidateSet::iterator Best; |
| 3458 | switch (Candidates.BestViableFunction(S, R.getNameLoc(), Best)) { |
| 3459 | case OR_Success: { |
| 3460 | // Got one! |
| 3461 | FunctionDecl *FnDecl = Best->Function; |
| 3462 | assert(R.getNamingClass() == nullptr && |
| 3463 | "class members should not be considered"); |
| 3464 | |
| 3465 | if (!FnDecl->isReplaceableGlobalAllocationFunction()) { |
| 3466 | S.Diag(R.getNameLoc(), diag::err_builtin_operator_new_delete_not_usual) |
| 3467 | << (IsDelete ? 1 : 0) << Range; |
| 3468 | S.Diag(FnDecl->getLocation(), diag::note_non_usual_function_declared_here) |
| 3469 | << R.getLookupName() << FnDecl->getSourceRange(); |
| 3470 | return true; |
| 3471 | } |
| 3472 | |
| 3473 | Operator = FnDecl; |
| 3474 | return false; |
| 3475 | } |
| 3476 | |
| 3477 | case OR_No_Viable_Function: |
| 3478 | S.Diag(R.getNameLoc(), diag::err_ovl_no_viable_function_in_call) |
| 3479 | << R.getLookupName() << Range; |
| 3480 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 3481 | return true; |
| 3482 | |
| 3483 | case OR_Ambiguous: |
| 3484 | S.Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) |
| 3485 | << R.getLookupName() << Range; |
| 3486 | Candidates.NoteCandidates(S, OCD_ViableCandidates, Args); |
| 3487 | return true; |
| 3488 | |
| 3489 | case OR_Deleted: { |
| 3490 | S.Diag(R.getNameLoc(), diag::err_ovl_deleted_call) |
| 3491 | << Best->Function->isDeleted() << R.getLookupName() |
| 3492 | << S.getDeletedOrUnavailableSuffix(Best->Function) << Range; |
| 3493 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 3494 | return true; |
| 3495 | } |
| 3496 | } |
| 3497 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
| 3498 | } |
| 3499 | |
| 3500 | ExprResult |
| 3501 | Sema::SemaBuiltinOperatorNewDeleteOverloaded(ExprResult TheCallResult, |
| 3502 | bool IsDelete) { |
| 3503 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 3504 | if (!getLangOpts().CPlusPlus) { |
| 3505 | Diag(TheCall->getExprLoc(), diag::err_builtin_requires_language) |
| 3506 | << (IsDelete ? "__builtin_operator_delete" : "__builtin_operator_new") |
| 3507 | << "C++"; |
| 3508 | return ExprError(); |
| 3509 | } |
| 3510 | // CodeGen assumes it can find the global new and delete to call, |
| 3511 | // so ensure that they are declared. |
| 3512 | DeclareGlobalNewDelete(); |
| 3513 | |
| 3514 | FunctionDecl *OperatorNewOrDelete = nullptr; |
| 3515 | if (resolveBuiltinNewDeleteOverload(*this, TheCall, IsDelete, |
| 3516 | OperatorNewOrDelete)) |
| 3517 | return ExprError(); |
| 3518 | assert(OperatorNewOrDelete && "should be found"); |
| 3519 | |
| 3520 | TheCall->setType(OperatorNewOrDelete->getReturnType()); |
| 3521 | for (unsigned i = 0; i != TheCall->getNumArgs(); ++i) { |
| 3522 | QualType ParamTy = OperatorNewOrDelete->getParamDecl(i)->getType(); |
| 3523 | InitializedEntity Entity = |
| 3524 | InitializedEntity::InitializeParameter(Context, ParamTy, false); |
| 3525 | ExprResult Arg = PerformCopyInitialization( |
| 3526 | Entity, TheCall->getArg(i)->getLocStart(), TheCall->getArg(i)); |
| 3527 | if (Arg.isInvalid()) |
| 3528 | return ExprError(); |
| 3529 | TheCall->setArg(i, Arg.get()); |
| 3530 | } |
| 3531 | auto Callee = dyn_cast<ImplicitCastExpr>(TheCall->getCallee()); |
| 3532 | assert(Callee && Callee->getCastKind() == CK_BuiltinFnToFnPtr && |
| 3533 | "Callee expected to be implicit cast to a builtin function pointer"); |
| 3534 | Callee->setType(OperatorNewOrDelete->getType()); |
| 3535 | |
| 3536 | return TheCallResult; |
| 3537 | } |
| 3538 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3539 | void Sema::CheckVirtualDtorCall(CXXDestructorDecl *dtor, SourceLocation Loc, |
| 3540 | bool IsDelete, bool CallCanBeVirtual, |
| 3541 | bool WarnOnNonAbstractTypes, |
| 3542 | SourceLocation DtorLoc) { |
Nico Weber | 955bb84 | 2017-08-30 20:25:22 +0000 | [diff] [blame] | 3543 | if (!dtor || dtor->isVirtual() || !CallCanBeVirtual || isUnevaluatedContext()) |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3544 | return; |
| 3545 | |
| 3546 | // C++ [expr.delete]p3: |
| 3547 | // In the first alternative (delete object), if the static type of the |
| 3548 | // object to be deleted is different from its dynamic type, the static |
| 3549 | // type shall be a base class of the dynamic type of the object to be |
| 3550 | // deleted and the static type shall have a virtual destructor or the |
| 3551 | // behavior is undefined. |
| 3552 | // |
| 3553 | const CXXRecordDecl *PointeeRD = dtor->getParent(); |
| 3554 | // Note: a final class cannot be derived from, no issue there |
| 3555 | if (!PointeeRD->isPolymorphic() || PointeeRD->hasAttr<FinalAttr>()) |
| 3556 | return; |
| 3557 | |
Nico Weber | bf2260c | 2017-08-31 06:17:08 +0000 | [diff] [blame] | 3558 | // If the superclass is in a system header, there's nothing that can be done. |
| 3559 | // The `delete` (where we emit the warning) can be in a system header, |
| 3560 | // what matters for this warning is where the deleted type is defined. |
| 3561 | if (getSourceManager().isInSystemHeader(PointeeRD->getLocation())) |
| 3562 | return; |
| 3563 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3564 | QualType ClassType = dtor->getThisType(Context)->getPointeeType(); |
| 3565 | if (PointeeRD->isAbstract()) { |
| 3566 | // If the class is abstract, we warn by default, because we're |
| 3567 | // sure the code has undefined behavior. |
| 3568 | Diag(Loc, diag::warn_delete_abstract_non_virtual_dtor) << (IsDelete ? 0 : 1) |
| 3569 | << ClassType; |
| 3570 | } else if (WarnOnNonAbstractTypes) { |
| 3571 | // Otherwise, if this is not an array delete, it's a bit suspect, |
| 3572 | // but not necessarily wrong. |
| 3573 | Diag(Loc, diag::warn_delete_non_virtual_dtor) << (IsDelete ? 0 : 1) |
| 3574 | << ClassType; |
| 3575 | } |
| 3576 | if (!IsDelete) { |
| 3577 | std::string TypeStr; |
| 3578 | ClassType.getAsStringInternal(TypeStr, getPrintingPolicy()); |
| 3579 | Diag(DtorLoc, diag::note_delete_non_virtual) |
| 3580 | << FixItHint::CreateInsertion(DtorLoc, TypeStr + "::"); |
| 3581 | } |
| 3582 | } |
| 3583 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3584 | Sema::ConditionResult Sema::ActOnConditionVariable(Decl *ConditionVar, |
| 3585 | SourceLocation StmtLoc, |
| 3586 | ConditionKind CK) { |
| 3587 | ExprResult E = |
| 3588 | CheckConditionVariable(cast<VarDecl>(ConditionVar), StmtLoc, CK); |
| 3589 | if (E.isInvalid()) |
| 3590 | return ConditionError(); |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3591 | return ConditionResult(*this, ConditionVar, MakeFullExpr(E.get(), StmtLoc), |
| 3592 | CK == ConditionKind::ConstexprIf); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3595 | /// Check the use of the given variable as a C++ condition in an if, |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3596 | /// while, do-while, or switch statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3597 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3598 | SourceLocation StmtLoc, |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3599 | ConditionKind CK) { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3600 | if (ConditionVar->isInvalidDecl()) |
| 3601 | return ExprError(); |
| 3602 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3603 | QualType T = ConditionVar->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3604 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3605 | // C++ [stmt.select]p2: |
| 3606 | // The declarator shall not specify a function or an array. |
| 3607 | if (T->isFunctionType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3608 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3609 | diag::err_invalid_use_of_function_type) |
| 3610 | << ConditionVar->getSourceRange()); |
| 3611 | else if (T->isArrayType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3612 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3613 | diag::err_invalid_use_of_array_type) |
| 3614 | << ConditionVar->getSourceRange()); |
Douglas Gregor | 0156d1c | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 3615 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3616 | ExprResult Condition = DeclRefExpr::Create( |
| 3617 | Context, NestedNameSpecifierLoc(), SourceLocation(), ConditionVar, |
| 3618 | /*enclosing*/ false, ConditionVar->getLocation(), |
| 3619 | ConditionVar->getType().getNonReferenceType(), VK_LValue); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3620 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3621 | MarkDeclRefReferenced(cast<DeclRefExpr>(Condition.get())); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3622 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3623 | switch (CK) { |
| 3624 | case ConditionKind::Boolean: |
| 3625 | return CheckBooleanCondition(StmtLoc, Condition.get()); |
| 3626 | |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3627 | case ConditionKind::ConstexprIf: |
| 3628 | return CheckBooleanCondition(StmtLoc, Condition.get(), true); |
| 3629 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3630 | case ConditionKind::Switch: |
| 3631 | return CheckSwitchCondition(StmtLoc, Condition.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3632 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3633 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3634 | llvm_unreachable("unexpected condition kind"); |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3637 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3638 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr, bool IsConstexpr) { |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3639 | // C++ 6.4p4: |
| 3640 | // The value of a condition that is an initialized declaration in a statement |
| 3641 | // other than a switch statement is the value of the declared variable |
| 3642 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 3643 | // program is ill-formed. |
| 3644 | // The value of a condition that is an expression is the value of the |
| 3645 | // expression, implicitly converted to bool. |
| 3646 | // |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3647 | // FIXME: Return this value to the caller so they don't need to recompute it. |
| 3648 | llvm::APSInt Value(/*BitWidth*/1); |
| 3649 | return (IsConstexpr && !CondExpr->isValueDependent()) |
| 3650 | ? CheckConvertedConstantExpression(CondExpr, Context.BoolTy, Value, |
| 3651 | CCEK_ConstexprIf) |
| 3652 | : PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3653 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3654 | |
| 3655 | /// Helper function to determine whether this is the (deprecated) C++ |
| 3656 | /// conversion from a string literal to a pointer to non-const char or |
| 3657 | /// non-const wchar_t (for narrow and wide string literals, |
| 3658 | /// respectively). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | bool |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3660 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 3661 | // Look inside the implicit cast, if it exists. |
| 3662 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 3663 | From = Cast->getSubExpr(); |
| 3664 | |
| 3665 | // A string literal (2.13.4) that is not a wide string literal can |
| 3666 | // be converted to an rvalue of type "pointer to char"; a wide |
| 3667 | // string literal can be converted to an rvalue of type "pointer |
| 3668 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 689999d | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 3669 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3670 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | if (const BuiltinType *ToPointeeType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3672 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3673 | // This conversion is considered only when there is an |
| 3674 | // explicit appropriate pointer target type (C++ 4.2p2). |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3675 | if (!ToPtrType->getPointeeType().hasQualifiers()) { |
| 3676 | switch (StrLit->getKind()) { |
| 3677 | case StringLiteral::UTF8: |
| 3678 | case StringLiteral::UTF16: |
| 3679 | case StringLiteral::UTF32: |
| 3680 | // We don't allow UTF literals to be implicitly converted |
| 3681 | break; |
| 3682 | case StringLiteral::Ascii: |
| 3683 | return (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 3684 | ToPointeeType->getKind() == BuiltinType::Char_S); |
| 3685 | case StringLiteral::Wide: |
Dmitry Polukhin | 9d64f72 | 2016-04-14 09:52:06 +0000 | [diff] [blame] | 3686 | return Context.typesAreCompatible(Context.getWideCharType(), |
| 3687 | QualType(ToPointeeType, 0)); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3688 | } |
| 3689 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | return false; |
| 3693 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3694 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3695 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3696 | SourceLocation CastLoc, |
| 3697 | QualType Ty, |
| 3698 | CastKind Kind, |
| 3699 | CXXMethodDecl *Method, |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3700 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3701 | bool HadMultipleCandidates, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3702 | Expr *From) { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3703 | switch (Kind) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3704 | default: llvm_unreachable("Unhandled cast kind!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3705 | case CK_ConstructorConversion: { |
Douglas Gregor | c7a3107 | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 3706 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3707 | SmallVector<Expr*, 8> ConstructorArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3708 | |
Richard Smith | 72d7405 | 2013-07-20 19:41:36 +0000 | [diff] [blame] | 3709 | if (S.RequireNonAbstractType(CastLoc, Ty, |
| 3710 | diag::err_allocation_of_abstract_type)) |
| 3711 | return ExprError(); |
| 3712 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3713 | if (S.CompleteConstructorCall(Constructor, From, CastLoc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3714 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3715 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 3716 | S.CheckConstructorAccess(CastLoc, Constructor, FoundDecl, |
| 3717 | InitializedEntity::InitializeTemporary(Ty)); |
Richard Smith | 7c9442a | 2015-02-24 21:44:43 +0000 | [diff] [blame] | 3718 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3719 | return ExprError(); |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 3720 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3721 | ExprResult Result = S.BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3722 | CastLoc, Ty, FoundDecl, cast<CXXConstructorDecl>(Method), |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3723 | ConstructorArgs, HadMultipleCandidates, |
| 3724 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3725 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3726 | if (Result.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3727 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3728 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3729 | return S.MaybeBindToTemporary(Result.getAs<Expr>()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3730 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3731 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3732 | case CK_UserDefinedConversion: { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3733 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3734 | |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3735 | S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ nullptr, FoundDecl); |
Richard Smith | 7c9442a | 2015-02-24 21:44:43 +0000 | [diff] [blame] | 3736 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3737 | return ExprError(); |
| 3738 | |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3739 | // Create an implicit call expr that calls it. |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 3740 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(Method); |
| 3741 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Conv, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3742 | HadMultipleCandidates); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3743 | if (Result.isInvalid()) |
| 3744 | return ExprError(); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 3745 | // Record usage of conversion in an implicit cast. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3746 | Result = ImplicitCastExpr::Create(S.Context, Result.get()->getType(), |
| 3747 | CK_UserDefinedConversion, Result.get(), |
| 3748 | nullptr, Result.get()->getValueKind()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3749 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3750 | return S.MaybeBindToTemporary(Result.get()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3751 | } |
| 3752 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3753 | } |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3754 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3755 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 3756 | /// expression From to the type ToType using the pre-computed implicit |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3757 | /// conversion sequence ICS. Returns the converted |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 3758 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3759 | /// used in the error message. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3760 | ExprResult |
| 3761 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3762 | const ImplicitConversionSequence &ICS, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3763 | AssignmentAction Action, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3764 | CheckedConversionKind CCK) { |
Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 3765 | // C++ [over.match.oper]p7: [...] operands of class type are converted [...] |
| 3766 | if (CCK == CCK_ForBuiltinOverloadedOp && !From->getType()->isRecordType()) |
| 3767 | return From; |
| 3768 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3769 | switch (ICS.getKind()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3770 | case ImplicitConversionSequence::StandardConversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3771 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, |
| 3772 | Action, CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3773 | if (Res.isInvalid()) |
| 3774 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3775 | From = Res.get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3776 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3777 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3778 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3779 | case ImplicitConversionSequence::UserDefinedConversion: { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3780 | |
Fariborz Jahanian | 2fee79a | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 3781 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3782 | CastKind CastKind; |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3783 | QualType BeforeToType; |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3784 | assert(FD && "no conversion function for user-defined conversion seq"); |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3785 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3786 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3787 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3788 | // If the user-defined conversion is specified by a conversion function, |
| 3789 | // the initial standard conversion sequence converts the source type to |
| 3790 | // the implicit object parameter of the conversion function. |
| 3791 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
John McCall | a03edda | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 3792 | } else { |
| 3793 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3794 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3795 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 3796 | if (!ICS.UserDefined.EllipsisConversion) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3797 | // If the user-defined conversion is specified by a constructor, the |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 3798 | // initial standard conversion sequence converts the source type to |
| 3799 | // the type required by the argument of the constructor |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 3800 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3801 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3802 | } |
Richard Smith | 72d7405 | 2013-07-20 19:41:36 +0000 | [diff] [blame] | 3803 | // Watch out for ellipsis conversion. |
Fariborz Jahanian | eec642f | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 3804 | if (!ICS.UserDefined.EllipsisConversion) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3805 | ExprResult Res = |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3806 | PerformImplicitConversion(From, BeforeToType, |
| 3807 | ICS.UserDefined.Before, AA_Converting, |
| 3808 | CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3809 | if (Res.isInvalid()) |
| 3810 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3811 | From = Res.get(); |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3813 | |
| 3814 | ExprResult CastArg |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3815 | = BuildCXXCastArgument(*this, |
| 3816 | From->getLocStart(), |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3817 | ToType.getNonReferenceType(), |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 3818 | CastKind, cast<CXXMethodDecl>(FD), |
| 3819 | ICS.UserDefined.FoundConversionFunction, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3820 | ICS.UserDefined.HadMultipleCandidates, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3821 | From); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3822 | |
| 3823 | if (CastArg.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3824 | return ExprError(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 3825 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3826 | From = CastArg.get(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 3827 | |
Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 3828 | // C++ [over.match.oper]p7: |
| 3829 | // [...] the second standard conversion sequence of a user-defined |
| 3830 | // conversion sequence is not applied. |
| 3831 | if (CCK == CCK_ForBuiltinOverloadedOp) |
| 3832 | return From; |
| 3833 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3834 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
| 3835 | AA_Converting, CCK); |
Fariborz Jahanian | da21efb | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 3836 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3837 | |
| 3838 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3839 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3840 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 3841 | << From->getSourceRange()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3842 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3843 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3844 | case ImplicitConversionSequence::EllipsisConversion: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3845 | llvm_unreachable("Cannot perform an ellipsis conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3846 | |
| 3847 | case ImplicitConversionSequence::BadConversion: |
Richard Smith | e15a370 | 2016-10-06 23:12:58 +0000 | [diff] [blame] | 3848 | bool Diagnosed = |
| 3849 | DiagnoseAssignmentResult(Incompatible, From->getExprLoc(), ToType, |
| 3850 | From->getType(), From, Action); |
| 3851 | assert(Diagnosed && "failed to diagnose bad conversion"); (void)Diagnosed; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3852 | return ExprError(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
| 3855 | // Everything went well. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3856 | return From; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3859 | /// PerformImplicitConversion - Perform an implicit conversion of the |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3860 | /// expression From to the type ToType by following the standard |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3861 | /// conversion sequence SCS. Returns the converted |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3862 | /// expression. Flavor is the context in which we're performing this |
| 3863 | /// conversion, for use in error messages. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3864 | ExprResult |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3865 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3866 | const StandardConversionSequence& SCS, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3867 | AssignmentAction Action, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3868 | CheckedConversionKind CCK) { |
| 3869 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3870 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3871 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 3872 | // much extra work. What this means is that we need to keep track of more |
| 3873 | // information that is computed when we try the implicit conversion initially, |
| 3874 | // so that we don't need to recompute anything here. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3875 | QualType FromType = From->getType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3876 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3877 | if (SCS.CopyConstructor) { |
Anders Carlsson | 549c5bd | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 3878 | // FIXME: When can ToType be a reference type? |
| 3879 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3880 | if (SCS.Second == ICK_Derived_To_Base) { |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3881 | SmallVector<Expr*, 8> ConstructorArgs; |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3882 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3883 | From, /*FIXME:ConstructLoc*/SourceLocation(), |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3884 | ConstructorArgs)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3885 | return ExprError(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3886 | return BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3887 | /*FIXME:ConstructLoc*/ SourceLocation(), ToType, |
| 3888 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3889 | ConstructorArgs, /*HadMultipleCandidates*/ false, |
| 3890 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3891 | CXXConstructExpr::CK_Complete, SourceRange()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3892 | } |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3893 | return BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3894 | /*FIXME:ConstructLoc*/ SourceLocation(), ToType, |
| 3895 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3896 | From, /*HadMultipleCandidates*/ false, |
| 3897 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3898 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3901 | // Resolve overloaded function references. |
| 3902 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 3903 | DeclAccessPair Found; |
| 3904 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 3905 | true, Found); |
| 3906 | if (!Fn) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3907 | return ExprError(); |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3908 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3909 | if (DiagnoseUseOfDecl(Fn, From->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3910 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3911 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3912 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 3913 | FromType = From->getType(); |
| 3914 | } |
| 3915 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3916 | // If we're converting to an atomic type, first convert to the corresponding |
| 3917 | // non-atomic type. |
| 3918 | QualType ToAtomicType; |
| 3919 | if (const AtomicType *ToAtomic = ToType->getAs<AtomicType>()) { |
| 3920 | ToAtomicType = ToType; |
| 3921 | ToType = ToAtomic->getValueType(); |
| 3922 | } |
| 3923 | |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 3924 | QualType InitialFromType = FromType; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3925 | // Perform the first implicit conversion. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3926 | switch (SCS.First) { |
| 3927 | case ICK_Identity: |
David Majnemer | 3087a2b | 2014-12-28 21:47:31 +0000 | [diff] [blame] | 3928 | if (const AtomicType *FromAtomic = FromType->getAs<AtomicType>()) { |
| 3929 | FromType = FromAtomic->getValueType().getUnqualifiedType(); |
| 3930 | From = ImplicitCastExpr::Create(Context, FromType, CK_AtomicToNonAtomic, |
| 3931 | From, /*BasePath=*/nullptr, VK_RValue); |
| 3932 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3933 | break; |
| 3934 | |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3935 | case ICK_Lvalue_To_Rvalue: { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 3936 | assert(From->getObjectKind() != OK_ObjCProperty); |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3937 | ExprResult FromRes = DefaultLvalueConversion(From); |
| 3938 | assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!"); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3939 | From = FromRes.get(); |
David Majnemer | e7029bc | 2014-12-16 06:31:17 +0000 | [diff] [blame] | 3940 | FromType = From->getType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3941 | break; |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3942 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3943 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3944 | case ICK_Array_To_Pointer: |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3945 | FromType = Context.getArrayDecayedType(FromType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3946 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3947 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3948 | break; |
| 3949 | |
| 3950 | case ICK_Function_To_Pointer: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3951 | FromType = Context.getPointerType(FromType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3952 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3953 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3954 | break; |
| 3955 | |
| 3956 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3957 | llvm_unreachable("Improper first standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3960 | // Perform the second implicit conversion |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3961 | switch (SCS.Second) { |
| 3962 | case ICK_Identity: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 3963 | // C++ [except.spec]p5: |
| 3964 | // [For] assignment to and initialization of pointers to functions, |
| 3965 | // pointers to member functions, and references to functions: the |
| 3966 | // target entity shall allow at least the exceptions allowed by the |
| 3967 | // source value in the assignment or initialization. |
| 3968 | switch (Action) { |
| 3969 | case AA_Assigning: |
| 3970 | case AA_Initializing: |
| 3971 | // Note, function argument passing and returning are initialization. |
| 3972 | case AA_Passing: |
| 3973 | case AA_Returning: |
| 3974 | case AA_Sending: |
| 3975 | case AA_Passing_CFAudited: |
| 3976 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 3977 | return ExprError(); |
| 3978 | break; |
| 3979 | |
| 3980 | case AA_Casting: |
| 3981 | case AA_Converting: |
| 3982 | // Casts and implicit conversions are not initialization, so are not |
| 3983 | // checked for exception specification mismatches. |
| 3984 | break; |
| 3985 | } |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3986 | // Nothing else to do. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3987 | break; |
| 3988 | |
| 3989 | case ICK_Integral_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3990 | case ICK_Integral_Conversion: |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3991 | if (ToType->isBooleanType()) { |
| 3992 | assert(FromType->castAs<EnumType>()->getDecl()->isFixed() && |
| 3993 | SCS.Second == ICK_Integral_Promotion && |
| 3994 | "only enums with fixed underlying type can promote to bool"); |
| 3995 | From = ImpCastExprToType(From, ToType, CK_IntegralToBoolean, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3996 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3997 | } else { |
| 3998 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3999 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 4000 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4001 | break; |
| 4002 | |
| 4003 | case ICK_Floating_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4004 | case ICK_Floating_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4005 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4006 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4007 | break; |
| 4008 | |
| 4009 | case ICK_Complex_Promotion: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4010 | case ICK_Complex_Conversion: { |
| 4011 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
| 4012 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
| 4013 | CastKind CK; |
| 4014 | if (FromEl->isRealFloatingType()) { |
| 4015 | if (ToEl->isRealFloatingType()) |
| 4016 | CK = CK_FloatingComplexCast; |
| 4017 | else |
| 4018 | CK = CK_FloatingComplexToIntegralComplex; |
| 4019 | } else if (ToEl->isRealFloatingType()) { |
| 4020 | CK = CK_IntegralComplexToFloatingComplex; |
| 4021 | } else { |
| 4022 | CK = CK_IntegralComplexCast; |
| 4023 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4024 | From = ImpCastExprToType(From, ToType, CK, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4025 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4026 | break; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4027 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4028 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4029 | case ICK_Floating_Integral: |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 4030 | if (ToType->isRealFloatingType()) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4031 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4032 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4033 | else |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4034 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4035 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4036 | break; |
| 4037 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 4038 | case ICK_Compatible_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4039 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4040 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4041 | break; |
| 4042 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4043 | case ICK_Writeback_Conversion: |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 4044 | case ICK_Pointer_Conversion: { |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4045 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 4046 | // Diagnose incompatible Objective-C conversions |
Douglas Gregor | 2720dc6 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 4047 | if (Action == AA_Initializing || Action == AA_Assigning) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4048 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 4049 | diag::ext_typecheck_convert_incompatible_pointer) |
| 4050 | << ToType << From->getType() << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 4051 | << From->getSourceRange() << 0; |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 4052 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4053 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 4054 | diag::ext_typecheck_convert_incompatible_pointer) |
| 4055 | << From->getType() << ToType << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 4056 | << From->getSourceRange() << 0; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4057 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4058 | if (From->getType()->isObjCObjectPointerType() && |
| 4059 | ToType->isObjCObjectPointerType()) |
| 4060 | EmitRelatedResultTypeNote(From); |
Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame] | 4061 | } else if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && |
| 4062 | !CheckObjCARCUnavailableWeakConversion(ToType, |
| 4063 | From->getType())) { |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 4064 | if (Action == AA_Initializing) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4065 | Diag(From->getLocStart(), |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 4066 | diag::err_arc_weak_unavailable_assign); |
| 4067 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4068 | Diag(From->getLocStart(), |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4069 | diag::err_arc_convesion_of_weak_unavailable) |
| 4070 | << (Action == AA_Casting) << From->getType() << ToType |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 4071 | << From->getSourceRange(); |
| 4072 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4073 | |
Richard Smith | 354abec | 2017-12-08 23:29:59 +0000 | [diff] [blame] | 4074 | CastKind Kind; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4075 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4076 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4077 | return ExprError(); |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4078 | |
| 4079 | // Make sure we extend blocks if necessary. |
| 4080 | // FIXME: doing this here is really ugly. |
| 4081 | if (Kind == CK_BlockPointerToObjCPointerCast) { |
| 4082 | ExprResult E = From; |
| 4083 | (void) PrepareCastToObjCObjectPointer(E); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4084 | From = E.get(); |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4085 | } |
Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame] | 4086 | if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) |
| 4087 | CheckObjCConversion(SourceRange(), ToType, From, CCK); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4088 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4089 | .get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4090 | break; |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 4091 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4092 | |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 4093 | case ICK_Pointer_Member: { |
Richard Smith | 354abec | 2017-12-08 23:29:59 +0000 | [diff] [blame] | 4094 | CastKind Kind; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4095 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4096 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4097 | return ExprError(); |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 4098 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4099 | return ExprError(); |
David Majnemer | d96b997 | 2014-08-08 00:10:39 +0000 | [diff] [blame] | 4100 | |
| 4101 | // We may not have been able to figure out what this member pointer resolved |
| 4102 | // to up until this exact point. Attempt to lock-in it's inheritance model. |
David Majnemer | fc22e47 | 2015-06-12 17:55:44 +0000 | [diff] [blame] | 4103 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4104 | (void)isCompleteType(From->getExprLoc(), From->getType()); |
| 4105 | (void)isCompleteType(From->getExprLoc(), ToType); |
David Majnemer | fc22e47 | 2015-06-12 17:55:44 +0000 | [diff] [blame] | 4106 | } |
David Majnemer | d96b997 | 2014-08-08 00:10:39 +0000 | [diff] [blame] | 4107 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4108 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4109 | .get(); |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 4110 | break; |
| 4111 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4112 | |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 4113 | case ICK_Boolean_Conversion: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4114 | // Perform half-to-boolean conversion via float. |
| 4115 | if (From->getType()->isHalfType()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4116 | From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).get(); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4117 | FromType = Context.FloatTy; |
| 4118 | } |
| 4119 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4120 | From = ImpCastExprToType(From, Context.BoolTy, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4121 | ScalarTypeToBooleanCastKind(FromType), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4122 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4123 | break; |
| 4124 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4125 | case ICK_Derived_To_Base: { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4126 | CXXCastPath BasePath; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4127 | if (CheckDerivedToBaseConversion(From->getType(), |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 4128 | ToType.getNonReferenceType(), |
| 4129 | From->getLocStart(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4130 | From->getSourceRange(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4131 | &BasePath, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4132 | CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4133 | return ExprError(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4134 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4135 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 4136 | CK_DerivedToBase, From->getValueKind(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4137 | &BasePath, CCK).get(); |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 4138 | break; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4141 | case ICK_Vector_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4142 | From = ImpCastExprToType(From, ToType, CK_BitCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4143 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4144 | break; |
| 4145 | |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 4146 | case ICK_Vector_Splat: { |
Fariborz Jahanian | 28d94b1 | 2015-03-05 23:06:09 +0000 | [diff] [blame] | 4147 | // Vector splat from any arithmetic type to a vector. |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 4148 | Expr *Elem = prepareVectorSplat(ToType, From).get(); |
| 4149 | From = ImpCastExprToType(Elem, ToType, CK_VectorSplat, VK_RValue, |
| 4150 | /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4151 | break; |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 4152 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4153 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4154 | case ICK_Complex_Real: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4155 | // Case 1. x -> _Complex y |
| 4156 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
| 4157 | QualType ElType = ToComplex->getElementType(); |
| 4158 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 4159 | |
| 4160 | // x -> y |
| 4161 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
| 4162 | // do nothing |
| 4163 | } else if (From->getType()->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4164 | From = ImpCastExprToType(From, ElType, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4165 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4166 | } else { |
| 4167 | assert(From->getType()->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4168 | From = ImpCastExprToType(From, ElType, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4169 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4170 | } |
| 4171 | // y -> _Complex y |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4172 | From = ImpCastExprToType(From, ToType, |
| 4173 | isFloatingComplex ? CK_FloatingRealToComplex |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4174 | : CK_IntegralRealToComplex).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4175 | |
| 4176 | // Case 2. _Complex x -> y |
| 4177 | } else { |
| 4178 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
| 4179 | assert(FromComplex); |
| 4180 | |
| 4181 | QualType ElType = FromComplex->getElementType(); |
| 4182 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 4183 | |
| 4184 | // _Complex x -> x |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4185 | From = ImpCastExprToType(From, ElType, |
| 4186 | isFloatingComplex ? CK_FloatingComplexToReal |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4187 | : CK_IntegralComplexToReal, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4188 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4189 | |
| 4190 | // x -> y |
| 4191 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
| 4192 | // do nothing |
| 4193 | } else if (ToType->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4194 | From = ImpCastExprToType(From, ToType, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4195 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4196 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4197 | } else { |
| 4198 | assert(ToType->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4199 | From = ImpCastExprToType(From, ToType, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4200 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4201 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4202 | } |
| 4203 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4204 | break; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4205 | |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 4206 | case ICK_Block_Pointer_Conversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4207 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4208 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4209 | break; |
| 4210 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4211 | |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 4212 | case ICK_TransparentUnionConversion: { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4213 | ExprResult FromRes = From; |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 4214 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4215 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); |
| 4216 | if (FromRes.isInvalid()) |
| 4217 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4218 | From = FromRes.get(); |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 4219 | assert ((ConvTy == Sema::Compatible) && |
| 4220 | "Improper transparent union conversion"); |
| 4221 | (void)ConvTy; |
| 4222 | break; |
| 4223 | } |
| 4224 | |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4225 | case ICK_Zero_Event_Conversion: |
| 4226 | From = ImpCastExprToType(From, ToType, |
| 4227 | CK_ZeroToOCLEvent, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4228 | From->getValueKind()).get(); |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4229 | break; |
| 4230 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 4231 | case ICK_Zero_Queue_Conversion: |
| 4232 | From = ImpCastExprToType(From, ToType, |
| 4233 | CK_ZeroToOCLQueue, |
| 4234 | From->getValueKind()).get(); |
| 4235 | break; |
| 4236 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4237 | case ICK_Lvalue_To_Rvalue: |
| 4238 | case ICK_Array_To_Pointer: |
| 4239 | case ICK_Function_To_Pointer: |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 4240 | case ICK_Function_Conversion: |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4241 | case ICK_Qualification: |
| 4242 | case ICK_Num_Conversion_Kinds: |
George Burgess IV | 78ed9b4 | 2015-10-11 20:37:14 +0000 | [diff] [blame] | 4243 | case ICK_C_Only_Conversion: |
George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 4244 | case ICK_Incompatible_Pointer_Conversion: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4245 | llvm_unreachable("Improper second standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | switch (SCS.Third) { |
| 4249 | case ICK_Identity: |
| 4250 | // Nothing to do. |
| 4251 | break; |
| 4252 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 4253 | case ICK_Function_Conversion: |
| 4254 | // If both sides are functions (or pointers/references to them), there could |
| 4255 | // be incompatible exception declarations. |
| 4256 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 4257 | return ExprError(); |
| 4258 | |
| 4259 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 4260 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
| 4261 | break; |
| 4262 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4263 | case ICK_Qualification: { |
| 4264 | // The qualification keeps the category of the inner expression, unless the |
| 4265 | // target type isn't a reference. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4266 | ExprValueKind VK = ToType->isReferenceType() ? |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4267 | From->getValueKind() : VK_RValue; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4268 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4269 | CK_NoOp, VK, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 4270 | |
Douglas Gregor | e981bb0 | 2011-03-14 16:13:32 +0000 | [diff] [blame] | 4271 | if (SCS.DeprecatedStringLiteralToCharPtr && |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 4272 | !getLangOpts().WritableStrings) { |
| 4273 | Diag(From->getLocStart(), getLangOpts().CPlusPlus11 |
| 4274 | ? diag::ext_deprecated_string_literal_conversion |
| 4275 | : diag::warn_deprecated_string_literal_conversion) |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 4276 | << ToType.getNonReferenceType(); |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 4277 | } |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 4278 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4279 | break; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4280 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4282 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4283 | llvm_unreachable("Improper third standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
Douglas Gregor | 298f43d | 2012-04-12 20:42:30 +0000 | [diff] [blame] | 4286 | // If this conversion sequence involved a scalar -> atomic conversion, perform |
| 4287 | // that conversion now. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4288 | if (!ToAtomicType.isNull()) { |
| 4289 | assert(Context.hasSameType( |
| 4290 | ToAtomicType->castAs<AtomicType>()->getValueType(), From->getType())); |
| 4291 | From = ImpCastExprToType(From, ToAtomicType, CK_NonAtomicToAtomic, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4292 | VK_RValue, nullptr, CCK).get(); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 4295 | // If this conversion sequence succeeded and involved implicitly converting a |
| 4296 | // _Nullable type to a _Nonnull one, complain. |
Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 4297 | if (!isCast(CCK)) |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 4298 | diagnoseNullableToNonnullConversion(ToType, InitialFromType, |
| 4299 | From->getLocStart()); |
| 4300 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4301 | return From; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4304 | /// Check the completeness of a type in a unary type trait. |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4305 | /// |
| 4306 | /// If the particular type trait requires a complete type, tries to complete |
| 4307 | /// it. If completing the type fails, a diagnostic is emitted and false |
| 4308 | /// returned. If completing the type succeeds or no completion was required, |
| 4309 | /// returns true. |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4310 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT, |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4311 | SourceLocation Loc, |
| 4312 | QualType ArgTy) { |
| 4313 | // C++0x [meta.unary.prop]p3: |
| 4314 | // For all of the class templates X declared in this Clause, instantiating |
| 4315 | // that template with a template argument that is a class template |
| 4316 | // specialization may result in the implicit instantiation of the template |
| 4317 | // argument if and only if the semantics of X require that the argument |
| 4318 | // must be a complete type. |
| 4319 | // We apply this rule to all the type trait expressions used to implement |
| 4320 | // these class templates. We also try to follow any GCC documented behavior |
| 4321 | // in these expressions to ensure portability of standard libraries. |
| 4322 | switch (UTT) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4323 | default: llvm_unreachable("not a UTT"); |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4324 | // is_complete_type somewhat obviously cannot require a complete type. |
| 4325 | case UTT_IsCompleteType: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 4326 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4327 | |
| 4328 | // These traits are modeled on the type predicates in C++0x |
| 4329 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as |
| 4330 | // requiring a complete type, as whether or not they return true cannot be |
| 4331 | // impacted by the completeness of the type. |
| 4332 | case UTT_IsVoid: |
| 4333 | case UTT_IsIntegral: |
| 4334 | case UTT_IsFloatingPoint: |
| 4335 | case UTT_IsArray: |
| 4336 | case UTT_IsPointer: |
| 4337 | case UTT_IsLvalueReference: |
| 4338 | case UTT_IsRvalueReference: |
| 4339 | case UTT_IsMemberFunctionPointer: |
| 4340 | case UTT_IsMemberObjectPointer: |
| 4341 | case UTT_IsEnum: |
| 4342 | case UTT_IsUnion: |
| 4343 | case UTT_IsClass: |
| 4344 | case UTT_IsFunction: |
| 4345 | case UTT_IsReference: |
| 4346 | case UTT_IsArithmetic: |
| 4347 | case UTT_IsFundamental: |
| 4348 | case UTT_IsObject: |
| 4349 | case UTT_IsScalar: |
| 4350 | case UTT_IsCompound: |
| 4351 | case UTT_IsMemberPointer: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 4352 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4353 | |
| 4354 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] |
| 4355 | // which requires some of its traits to have the complete type. However, |
| 4356 | // the completeness of the type cannot impact these traits' semantics, and |
| 4357 | // so they don't require it. This matches the comments on these traits in |
| 4358 | // Table 49. |
| 4359 | case UTT_IsConst: |
| 4360 | case UTT_IsVolatile: |
| 4361 | case UTT_IsSigned: |
| 4362 | case UTT_IsUnsigned: |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4363 | |
| 4364 | // This type trait always returns false, checking the type is moot. |
| 4365 | case UTT_IsInterfaceClass: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4366 | return true; |
| 4367 | |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4368 | // C++14 [meta.unary.prop]: |
| 4369 | // If T is a non-union class type, T shall be a complete type. |
| 4370 | case UTT_IsEmpty: |
| 4371 | case UTT_IsPolymorphic: |
| 4372 | case UTT_IsAbstract: |
| 4373 | if (const auto *RD = ArgTy->getAsCXXRecordDecl()) |
| 4374 | if (!RD->isUnion()) |
| 4375 | return !S.RequireCompleteType( |
| 4376 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
| 4377 | return true; |
| 4378 | |
| 4379 | // C++14 [meta.unary.prop]: |
| 4380 | // If T is a class type, T shall be a complete type. |
| 4381 | case UTT_IsFinal: |
| 4382 | case UTT_IsSealed: |
| 4383 | if (ArgTy->getAsCXXRecordDecl()) |
| 4384 | return !S.RequireCompleteType( |
| 4385 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
| 4386 | return true; |
| 4387 | |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4388 | // C++1z [meta.unary.prop]: |
| 4389 | // remove_all_extents_t<T> shall be a complete type or cv void. |
Eric Fiselier | 0736066 | 2017-04-12 22:12:15 +0000 | [diff] [blame] | 4390 | case UTT_IsAggregate: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4391 | case UTT_IsTrivial: |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 4392 | case UTT_IsTriviallyCopyable: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4393 | case UTT_IsStandardLayout: |
| 4394 | case UTT_IsPOD: |
| 4395 | case UTT_IsLiteral: |
Karthik Bhat | e1ae1b2 | 2017-06-28 08:52:08 +0000 | [diff] [blame] | 4396 | // Per the GCC type traits documentation, T shall be a complete type, cv void, |
| 4397 | // or an array of unknown bound. But GCC actually imposes the same constraints |
| 4398 | // as above. |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4399 | case UTT_HasNothrowAssign: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4400 | case UTT_HasNothrowMoveAssign: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4401 | case UTT_HasNothrowConstructor: |
| 4402 | case UTT_HasNothrowCopy: |
| 4403 | case UTT_HasTrivialAssign: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4404 | case UTT_HasTrivialMoveAssign: |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4405 | case UTT_HasTrivialDefaultConstructor: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4406 | case UTT_HasTrivialMoveConstructor: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4407 | case UTT_HasTrivialCopy: |
| 4408 | case UTT_HasTrivialDestructor: |
| 4409 | case UTT_HasVirtualDestructor: |
Karthik Bhat | e1ae1b2 | 2017-06-28 08:52:08 +0000 | [diff] [blame] | 4410 | ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0); |
| 4411 | LLVM_FALLTHROUGH; |
| 4412 | |
| 4413 | // C++1z [meta.unary.prop]: |
| 4414 | // T shall be a complete type, cv void, or an array of unknown bound. |
| 4415 | case UTT_IsDestructible: |
| 4416 | case UTT_IsNothrowDestructible: |
| 4417 | case UTT_IsTriviallyDestructible: |
Erich Keane | e63e9d7 | 2017-10-24 21:31:50 +0000 | [diff] [blame] | 4418 | case UTT_HasUniqueObjectRepresentations: |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4419 | if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType()) |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4420 | return true; |
| 4421 | |
| 4422 | return !S.RequireCompleteType( |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4423 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4424 | } |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4427 | static bool HasNoThrowOperator(const RecordType *RT, OverloadedOperatorKind Op, |
| 4428 | Sema &Self, SourceLocation KeyLoc, ASTContext &C, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4429 | bool (CXXRecordDecl::*HasTrivial)() const, |
| 4430 | bool (CXXRecordDecl::*HasNonTrivial)() const, |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4431 | bool (CXXMethodDecl::*IsDesiredOp)() const) |
| 4432 | { |
| 4433 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 4434 | if ((RD->*HasTrivial)() && !(RD->*HasNonTrivial)()) |
| 4435 | return true; |
| 4436 | |
| 4437 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(Op); |
| 4438 | DeclarationNameInfo NameInfo(Name, KeyLoc); |
| 4439 | LookupResult Res(Self, NameInfo, Sema::LookupOrdinaryName); |
| 4440 | if (Self.LookupQualifiedName(Res, RD)) { |
| 4441 | bool FoundOperator = false; |
| 4442 | Res.suppressDiagnostics(); |
| 4443 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 4444 | Op != OpEnd; ++Op) { |
| 4445 | if (isa<FunctionTemplateDecl>(*Op)) |
| 4446 | continue; |
| 4447 | |
| 4448 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 4449 | if((Operator->*IsDesiredOp)()) { |
| 4450 | FoundOperator = true; |
| 4451 | const FunctionProtoType *CPT = |
| 4452 | Operator->getType()->getAs<FunctionProtoType>(); |
| 4453 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 4454 | if (!CPT || !CPT->isNothrow()) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4455 | return false; |
| 4456 | } |
| 4457 | } |
| 4458 | return FoundOperator; |
| 4459 | } |
| 4460 | return false; |
| 4461 | } |
| 4462 | |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4463 | static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT, |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4464 | SourceLocation KeyLoc, QualType T) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4465 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4466 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4467 | ASTContext &C = Self.Context; |
| 4468 | switch(UTT) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4469 | default: llvm_unreachable("not a UTT"); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4470 | // Type trait expressions corresponding to the primary type category |
| 4471 | // predicates in C++0x [meta.unary.cat]. |
| 4472 | case UTT_IsVoid: |
| 4473 | return T->isVoidType(); |
| 4474 | case UTT_IsIntegral: |
| 4475 | return T->isIntegralType(C); |
| 4476 | case UTT_IsFloatingPoint: |
| 4477 | return T->isFloatingType(); |
| 4478 | case UTT_IsArray: |
| 4479 | return T->isArrayType(); |
| 4480 | case UTT_IsPointer: |
| 4481 | return T->isPointerType(); |
| 4482 | case UTT_IsLvalueReference: |
| 4483 | return T->isLValueReferenceType(); |
| 4484 | case UTT_IsRvalueReference: |
| 4485 | return T->isRValueReferenceType(); |
| 4486 | case UTT_IsMemberFunctionPointer: |
| 4487 | return T->isMemberFunctionPointerType(); |
| 4488 | case UTT_IsMemberObjectPointer: |
| 4489 | return T->isMemberDataPointerType(); |
| 4490 | case UTT_IsEnum: |
| 4491 | return T->isEnumeralType(); |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4492 | case UTT_IsUnion: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4493 | return T->isUnionType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4494 | case UTT_IsClass: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4495 | return T->isClassType() || T->isStructureType() || T->isInterfaceType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4496 | case UTT_IsFunction: |
| 4497 | return T->isFunctionType(); |
| 4498 | |
| 4499 | // Type trait expressions which correspond to the convenient composition |
| 4500 | // predicates in C++0x [meta.unary.comp]. |
| 4501 | case UTT_IsReference: |
| 4502 | return T->isReferenceType(); |
| 4503 | case UTT_IsArithmetic: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4504 | return T->isArithmeticType() && !T->isEnumeralType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4505 | case UTT_IsFundamental: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4506 | return T->isFundamentalType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4507 | case UTT_IsObject: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4508 | return T->isObjectType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4509 | case UTT_IsScalar: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4510 | // Note: semantic analysis depends on Objective-C lifetime types to be |
| 4511 | // considered scalar types. However, such types do not actually behave |
| 4512 | // like scalar types at run time (since they may require retain/release |
| 4513 | // operations), so we report them as non-scalar. |
| 4514 | if (T->isObjCLifetimeType()) { |
| 4515 | switch (T.getObjCLifetime()) { |
| 4516 | case Qualifiers::OCL_None: |
| 4517 | case Qualifiers::OCL_ExplicitNone: |
| 4518 | return true; |
| 4519 | |
| 4520 | case Qualifiers::OCL_Strong: |
| 4521 | case Qualifiers::OCL_Weak: |
| 4522 | case Qualifiers::OCL_Autoreleasing: |
| 4523 | return false; |
| 4524 | } |
| 4525 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4526 | |
Chandler Carruth | 7ba7bd3 | 2011-05-01 09:29:55 +0000 | [diff] [blame] | 4527 | return T->isScalarType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4528 | case UTT_IsCompound: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4529 | return T->isCompoundType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4530 | case UTT_IsMemberPointer: |
| 4531 | return T->isMemberPointerType(); |
| 4532 | |
| 4533 | // Type trait expressions which correspond to the type property predicates |
| 4534 | // in C++0x [meta.unary.prop]. |
| 4535 | case UTT_IsConst: |
| 4536 | return T.isConstQualified(); |
| 4537 | case UTT_IsVolatile: |
| 4538 | return T.isVolatileQualified(); |
| 4539 | case UTT_IsTrivial: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4540 | return T.isTrivialType(C); |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 4541 | case UTT_IsTriviallyCopyable: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4542 | return T.isTriviallyCopyableType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4543 | case UTT_IsStandardLayout: |
| 4544 | return T->isStandardLayoutType(); |
| 4545 | case UTT_IsPOD: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4546 | return T.isPODType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4547 | case UTT_IsLiteral: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4548 | return T->isLiteralType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4549 | case UTT_IsEmpty: |
| 4550 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4551 | return !RD->isUnion() && RD->isEmpty(); |
| 4552 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4553 | case UTT_IsPolymorphic: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4554 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4555 | return !RD->isUnion() && RD->isPolymorphic(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4556 | return false; |
| 4557 | case UTT_IsAbstract: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4558 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4559 | return !RD->isUnion() && RD->isAbstract(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4560 | return false; |
Eric Fiselier | 0736066 | 2017-04-12 22:12:15 +0000 | [diff] [blame] | 4561 | case UTT_IsAggregate: |
| 4562 | // Report vector extensions and complex types as aggregates because they |
| 4563 | // support aggregate initialization. GCC mirrors this behavior for vectors |
| 4564 | // but not _Complex. |
| 4565 | return T->isAggregateType() || T->isVectorType() || T->isExtVectorType() || |
| 4566 | T->isAnyComplexType(); |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4567 | // __is_interface_class only returns true when CL is invoked in /CLR mode and |
| 4568 | // even then only when it is used with the 'interface struct ...' syntax |
| 4569 | // Clang doesn't support /CLR which makes this type trait moot. |
John McCall | bf4a7d7 | 2012-09-25 07:32:49 +0000 | [diff] [blame] | 4570 | case UTT_IsInterfaceClass: |
John McCall | bf4a7d7 | 2012-09-25 07:32:49 +0000 | [diff] [blame] | 4571 | return false; |
Douglas Gregor | dca70af | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 4572 | case UTT_IsFinal: |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4573 | case UTT_IsSealed: |
| 4574 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4575 | return RD->hasAttr<FinalAttr>(); |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4576 | return false; |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4577 | case UTT_IsSigned: |
| 4578 | return T->isSignedIntegerType(); |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4579 | case UTT_IsUnsigned: |
| 4580 | return T->isUnsignedIntegerType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4581 | |
| 4582 | // Type trait expressions which query classes regarding their construction, |
| 4583 | // destruction, and copying. Rather than being based directly on the |
| 4584 | // related type predicates in the standard, they are specified by both |
| 4585 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those |
| 4586 | // specifications. |
| 4587 | // |
| 4588 | // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html |
| 4589 | // 2: http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4590 | // |
| 4591 | // Note that these builtins do not behave as documented in g++: if a class |
| 4592 | // has both a trivial and a non-trivial special member of a particular kind, |
| 4593 | // they return false! For now, we emulate this behavior. |
| 4594 | // FIXME: This appears to be a g++ bug: more complex cases reveal that it |
| 4595 | // does not correctly compute triviality in the presence of multiple special |
| 4596 | // members of the same kind. Revisit this once the g++ bug is fixed. |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4597 | case UTT_HasTrivialDefaultConstructor: |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4598 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4599 | // If __is_pod (type) is true then the trait is true, else if type is |
| 4600 | // a cv class or union type (or array thereof) with a trivial default |
| 4601 | // constructor ([class.ctor]) then the trait is true, else it is false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4602 | if (T.isPODType(C)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4603 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4604 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4605 | return RD->hasTrivialDefaultConstructor() && |
| 4606 | !RD->hasNonTrivialDefaultConstructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4607 | return false; |
Akira Hatanaka | 367b1a8 | 2018-04-09 19:39:27 +0000 | [diff] [blame] | 4608 | case UTT_HasTrivialMoveConstructor: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4609 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4610 | // standard library headers. Specifically this is used as the logic |
| 4611 | // behind std::is_trivially_move_constructible (20.9.4.3). |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4612 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4613 | return true; |
| 4614 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4615 | return RD->hasTrivialMoveConstructor() && !RD->hasNonTrivialMoveConstructor(); |
| 4616 | return false; |
Akira Hatanaka | 367b1a8 | 2018-04-09 19:39:27 +0000 | [diff] [blame] | 4617 | case UTT_HasTrivialCopy: |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4618 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4619 | // If __is_pod (type) is true or type is a reference type then |
| 4620 | // the trait is true, else if type is a cv class or union type |
| 4621 | // with a trivial copy constructor ([class.copy]) then the trait |
| 4622 | // is true, else it is false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4623 | if (T.isPODType(C) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4624 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4625 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4626 | return RD->hasTrivialCopyConstructor() && |
| 4627 | !RD->hasNonTrivialCopyConstructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4628 | return false; |
Akira Hatanaka | 367b1a8 | 2018-04-09 19:39:27 +0000 | [diff] [blame] | 4629 | case UTT_HasTrivialMoveAssign: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4630 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4631 | // standard library headers. Specifically it is used as the logic |
| 4632 | // behind std::is_trivially_move_assignable (20.9.4.3) |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4633 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4634 | return true; |
| 4635 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4636 | return RD->hasTrivialMoveAssignment() && !RD->hasNonTrivialMoveAssignment(); |
| 4637 | return false; |
Akira Hatanaka | 367b1a8 | 2018-04-09 19:39:27 +0000 | [diff] [blame] | 4638 | case UTT_HasTrivialAssign: |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4639 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4640 | // If type is const qualified or is a reference type then the |
| 4641 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 4642 | // trait is true, else if type is a cv class or union type with |
| 4643 | // a trivial copy assignment ([class.copy]) then the trait is |
| 4644 | // true, else it is false. |
| 4645 | // Note: the const and reference restrictions are interesting, |
| 4646 | // given that const and reference members don't prevent a class |
| 4647 | // from having a trivial copy assignment operator (but do cause |
| 4648 | // errors if the copy assignment operator is actually used, q.v. |
| 4649 | // [class.copy]p12). |
| 4650 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4651 | if (T.isConstQualified()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4652 | return false; |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4653 | if (T.isPODType(C)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4654 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4655 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4656 | return RD->hasTrivialCopyAssignment() && |
| 4657 | !RD->hasNonTrivialCopyAssignment(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4658 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4659 | case UTT_IsDestructible: |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4660 | case UTT_IsTriviallyDestructible: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4661 | case UTT_IsNothrowDestructible: |
David Majnemer | ac73de9 | 2015-08-11 03:03:28 +0000 | [diff] [blame] | 4662 | // C++14 [meta.unary.prop]: |
| 4663 | // For reference types, is_destructible<T>::value is true. |
| 4664 | if (T->isReferenceType()) |
| 4665 | return true; |
| 4666 | |
| 4667 | // Objective-C++ ARC: autorelease types don't require destruction. |
| 4668 | if (T->isObjCLifetimeType() && |
| 4669 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 4670 | return true; |
| 4671 | |
| 4672 | // C++14 [meta.unary.prop]: |
| 4673 | // For incomplete types and function types, is_destructible<T>::value is |
| 4674 | // false. |
| 4675 | if (T->isIncompleteType() || T->isFunctionType()) |
| 4676 | return false; |
| 4677 | |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4678 | // A type that requires destruction (via a non-trivial destructor or ARC |
| 4679 | // lifetime semantics) is not trivially-destructible. |
| 4680 | if (UTT == UTT_IsTriviallyDestructible && T.isDestructedType()) |
| 4681 | return false; |
| 4682 | |
David Majnemer | ac73de9 | 2015-08-11 03:03:28 +0000 | [diff] [blame] | 4683 | // C++14 [meta.unary.prop]: |
| 4684 | // For object types and given U equal to remove_all_extents_t<T>, if the |
| 4685 | // expression std::declval<U&>().~U() is well-formed when treated as an |
| 4686 | // unevaluated operand (Clause 5), then is_destructible<T>::value is true |
| 4687 | if (auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
| 4688 | CXXDestructorDecl *Destructor = Self.LookupDestructor(RD); |
| 4689 | if (!Destructor) |
| 4690 | return false; |
| 4691 | // C++14 [dcl.fct.def.delete]p2: |
| 4692 | // A program that refers to a deleted function implicitly or |
| 4693 | // explicitly, other than to declare it, is ill-formed. |
| 4694 | if (Destructor->isDeleted()) |
| 4695 | return false; |
| 4696 | if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public) |
| 4697 | return false; |
| 4698 | if (UTT == UTT_IsNothrowDestructible) { |
| 4699 | const FunctionProtoType *CPT = |
| 4700 | Destructor->getType()->getAs<FunctionProtoType>(); |
| 4701 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 4702 | if (!CPT || !CPT->isNothrow()) |
David Majnemer | ac73de9 | 2015-08-11 03:03:28 +0000 | [diff] [blame] | 4703 | return false; |
| 4704 | } |
| 4705 | } |
| 4706 | return true; |
| 4707 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4708 | case UTT_HasTrivialDestructor: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4709 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4710 | // If __is_pod (type) is true or type is a reference type |
| 4711 | // then the trait is true, else if type is a cv class or union |
| 4712 | // type (or array thereof) with a trivial destructor |
| 4713 | // ([class.dtor]) then the trait is true, else it is |
| 4714 | // false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4715 | if (T.isPODType(C) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4716 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4717 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4718 | // Objective-C++ ARC: autorelease types don't require destruction. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4719 | if (T->isObjCLifetimeType() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4720 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 4721 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4722 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4723 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4724 | return RD->hasTrivialDestructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4725 | return false; |
| 4726 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 4727 | case UTT_HasNothrowAssign: |
| 4728 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4729 | // If type is const qualified or is a reference type then the |
| 4730 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 4731 | // is true then the trait is true, else if type is a cv class |
| 4732 | // or union type with copy assignment operators that are known |
| 4733 | // not to throw an exception then the trait is true, else it is |
| 4734 | // false. |
| 4735 | if (C.getBaseElementType(T).isConstQualified()) |
| 4736 | return false; |
| 4737 | if (T->isReferenceType()) |
| 4738 | return false; |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4739 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4740 | return true; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4741 | |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4742 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 4743 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
| 4744 | &CXXRecordDecl::hasTrivialCopyAssignment, |
| 4745 | &CXXRecordDecl::hasNonTrivialCopyAssignment, |
| 4746 | &CXXMethodDecl::isCopyAssignmentOperator); |
| 4747 | return false; |
| 4748 | case UTT_HasNothrowMoveAssign: |
| 4749 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4750 | // standard library headers. Specifically this is used as the logic |
| 4751 | // behind std::is_nothrow_move_assignable (20.9.4.3). |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4752 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4753 | return true; |
| 4754 | |
| 4755 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) |
| 4756 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
| 4757 | &CXXRecordDecl::hasTrivialMoveAssignment, |
| 4758 | &CXXRecordDecl::hasNonTrivialMoveAssignment, |
| 4759 | &CXXMethodDecl::isMoveAssignmentOperator); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4760 | return false; |
| 4761 | case UTT_HasNothrowCopy: |
| 4762 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4763 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 4764 | // if type is a cv class or union type with copy constructors that are |
| 4765 | // known not to throw an exception then the trait is true, else it is |
| 4766 | // false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4767 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4768 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4769 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
| 4770 | if (RD->hasTrivialCopyConstructor() && |
| 4771 | !RD->hasNonTrivialCopyConstructor()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4772 | return true; |
| 4773 | |
| 4774 | bool FoundConstructor = false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4775 | unsigned FoundTQs; |
Aaron Ballman | e4de1a51 | 2015-07-21 22:33:52 +0000 | [diff] [blame] | 4776 | for (const auto *ND : Self.LookupConstructors(RD)) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4777 | // A template constructor is never a copy constructor. |
| 4778 | // FIXME: However, it may actually be selected at the actual overload |
| 4779 | // resolution point. |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4780 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4781 | continue; |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4782 | // UsingDecl itself is not a constructor |
| 4783 | if (isa<UsingDecl>(ND)) |
| 4784 | continue; |
| 4785 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4786 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 4787 | FoundConstructor = true; |
| 4788 | const FunctionProtoType *CPT |
| 4789 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4790 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4791 | if (!CPT) |
| 4792 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4793 | // TODO: check whether evaluating default arguments can throw. |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4794 | // For now, we'll be conservative and assume that they can throw. |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 4795 | if (!CPT->isNothrow() || CPT->getNumParams() > 1) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4796 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4797 | } |
| 4798 | } |
| 4799 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4800 | return FoundConstructor; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4801 | } |
| 4802 | return false; |
| 4803 | case UTT_HasNothrowConstructor: |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4804 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4805 | // If __has_trivial_constructor (type) is true then the trait is |
| 4806 | // true, else if type is a cv class or union type (or array |
| 4807 | // thereof) with a default constructor that is known not to |
| 4808 | // throw an exception then the trait is true, else it is false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4809 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4810 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4811 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
| 4812 | if (RD->hasTrivialDefaultConstructor() && |
| 4813 | !RD->hasNonTrivialDefaultConstructor()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4814 | return true; |
| 4815 | |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4816 | bool FoundConstructor = false; |
Aaron Ballman | e4de1a51 | 2015-07-21 22:33:52 +0000 | [diff] [blame] | 4817 | for (const auto *ND : Self.LookupConstructors(RD)) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4818 | // FIXME: In C++0x, a constructor template can be a default constructor. |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4819 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4820 | continue; |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4821 | // UsingDecl itself is not a constructor |
| 4822 | if (isa<UsingDecl>(ND)) |
| 4823 | continue; |
| 4824 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4825 | if (Constructor->isDefaultConstructor()) { |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4826 | FoundConstructor = true; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4827 | const FunctionProtoType *CPT |
| 4828 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4829 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4830 | if (!CPT) |
| 4831 | return false; |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4832 | // FIXME: check whether evaluating default arguments can throw. |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4833 | // For now, we'll be conservative and assume that they can throw. |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 4834 | if (!CPT->isNothrow() || CPT->getNumParams() > 0) |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4835 | return false; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4836 | } |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4837 | } |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4838 | return FoundConstructor; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4839 | } |
| 4840 | return false; |
| 4841 | case UTT_HasVirtualDestructor: |
| 4842 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4843 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 4844 | // then the trait is true, else it is false. |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4845 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
Sebastian Redl | 058fc82 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 4846 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4847 | return Destructor->isVirtual(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4848 | return false; |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4849 | |
| 4850 | // These type trait expressions are modeled on the specifications for the |
| 4851 | // Embarcadero C++0x type trait functions: |
| 4852 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
| 4853 | case UTT_IsCompleteType: |
| 4854 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): |
| 4855 | // Returns True if and only if T is a complete type at the point of the |
| 4856 | // function call. |
| 4857 | return !T->isIncompleteType(); |
Erich Keane | e63e9d7 | 2017-10-24 21:31:50 +0000 | [diff] [blame] | 4858 | case UTT_HasUniqueObjectRepresentations: |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 4859 | return C.hasUniqueObjectRepresentations(T); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4860 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4861 | } |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 4862 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4863 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
| 4864 | QualType RhsT, SourceLocation KeyLoc); |
| 4865 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4866 | static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc, |
| 4867 | ArrayRef<TypeSourceInfo *> Args, |
| 4868 | SourceLocation RParenLoc) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4869 | if (Kind <= UTT_Last) |
| 4870 | return EvaluateUnaryTypeTrait(S, Kind, KWLoc, Args[0]->getType()); |
| 4871 | |
Eric Fiselier | 1af6c11 | 2018-01-12 00:09:37 +0000 | [diff] [blame] | 4872 | // Evaluate BTT_ReferenceBindsToTemporary alongside the IsConstructible |
| 4873 | // traits to avoid duplication. |
| 4874 | if (Kind <= BTT_Last && Kind != BTT_ReferenceBindsToTemporary) |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4875 | return EvaluateBinaryTypeTrait(S, Kind, Args[0]->getType(), |
| 4876 | Args[1]->getType(), RParenLoc); |
| 4877 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4878 | switch (Kind) { |
Eric Fiselier | 1af6c11 | 2018-01-12 00:09:37 +0000 | [diff] [blame] | 4879 | case clang::BTT_ReferenceBindsToTemporary: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4880 | case clang::TT_IsConstructible: |
| 4881 | case clang::TT_IsNothrowConstructible: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4882 | case clang::TT_IsTriviallyConstructible: { |
| 4883 | // C++11 [meta.unary.prop]: |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 4884 | // is_trivially_constructible is defined as: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4885 | // |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 4886 | // is_constructible<T, Args...>::value is true and the variable |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 4887 | // definition for is_constructible, as defined below, is known to call |
| 4888 | // no operation that is not trivial. |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4889 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4890 | // The predicate condition for a template specialization |
| 4891 | // is_constructible<T, Args...> shall be satisfied if and only if the |
| 4892 | // following variable definition would be well-formed for some invented |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4893 | // variable t: |
| 4894 | // |
| 4895 | // T t(create<Args>()...); |
Alp Toker | 40f9b1c | 2013-12-12 21:23:03 +0000 | [diff] [blame] | 4896 | assert(!Args.empty()); |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4897 | |
| 4898 | // Precondition: T and all types in the parameter pack Args shall be |
| 4899 | // complete types, (possibly cv-qualified) void, or arrays of |
| 4900 | // unknown bound. |
Aaron Ballman | 2bf2cad | 2015-07-21 21:18:29 +0000 | [diff] [blame] | 4901 | for (const auto *TSI : Args) { |
| 4902 | QualType ArgTy = TSI->getType(); |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4903 | if (ArgTy->isVoidType() || ArgTy->isIncompleteArrayType()) |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4904 | continue; |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4905 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4906 | if (S.RequireCompleteType(KWLoc, ArgTy, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4907 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4908 | return false; |
| 4909 | } |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4910 | |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4911 | // Make sure the first argument is not incomplete nor a function type. |
| 4912 | QualType T = Args[0]->getType(); |
| 4913 | if (T->isIncompleteType() || T->isFunctionType()) |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4914 | return false; |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4915 | |
Nikola Smiljanic | 1b4b6ba | 2014-04-15 11:30:15 +0000 | [diff] [blame] | 4916 | // Make sure the first argument is not an abstract type. |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4917 | CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
Nikola Smiljanic | 1b4b6ba | 2014-04-15 11:30:15 +0000 | [diff] [blame] | 4918 | if (RD && RD->isAbstract()) |
| 4919 | return false; |
| 4920 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4921 | SmallVector<OpaqueValueExpr, 2> OpaqueArgExprs; |
| 4922 | SmallVector<Expr *, 2> ArgExprs; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4923 | ArgExprs.reserve(Args.size() - 1); |
| 4924 | for (unsigned I = 1, N = Args.size(); I != N; ++I) { |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4925 | QualType ArgTy = Args[I]->getType(); |
| 4926 | if (ArgTy->isObjectType() || ArgTy->isFunctionType()) |
| 4927 | ArgTy = S.Context.getRValueReferenceType(ArgTy); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4928 | OpaqueArgExprs.push_back( |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4929 | OpaqueValueExpr(Args[I]->getTypeLoc().getLocStart(), |
| 4930 | ArgTy.getNonLValueExprType(S.Context), |
| 4931 | Expr::getValueKindForType(ArgTy))); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4932 | } |
Richard Smith | a507bfc | 2014-07-23 20:07:08 +0000 | [diff] [blame] | 4933 | for (Expr &E : OpaqueArgExprs) |
| 4934 | ArgExprs.push_back(&E); |
| 4935 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4936 | // Perform the initialization in an unevaluated context within a SFINAE |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4937 | // trap at translation unit scope. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4938 | EnterExpressionEvaluationContext Unevaluated( |
| 4939 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4940 | Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true); |
| 4941 | Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl()); |
| 4942 | InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0])); |
| 4943 | InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc, |
| 4944 | RParenLoc)); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4945 | InitializationSequence Init(S, To, InitKind, ArgExprs); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4946 | if (Init.Failed()) |
| 4947 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4948 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4949 | ExprResult Result = Init.Perform(S, To, InitKind, ArgExprs); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4950 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 4951 | return false; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4952 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4953 | if (Kind == clang::TT_IsConstructible) |
| 4954 | return true; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4955 | |
Eric Fiselier | 1af6c11 | 2018-01-12 00:09:37 +0000 | [diff] [blame] | 4956 | if (Kind == clang::BTT_ReferenceBindsToTemporary) { |
| 4957 | if (!T->isReferenceType()) |
| 4958 | return false; |
| 4959 | |
| 4960 | return !Init.isDirectReferenceBinding(); |
| 4961 | } |
| 4962 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4963 | if (Kind == clang::TT_IsNothrowConstructible) |
| 4964 | return S.canThrow(Result.get()) == CT_Cannot; |
| 4965 | |
| 4966 | if (Kind == clang::TT_IsTriviallyConstructible) { |
Brian Kelley | 93c640b | 2017-03-29 17:40:35 +0000 | [diff] [blame] | 4967 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
| 4968 | // Objective-C lifetime, this is a non-trivial construction. |
| 4969 | if (T.getNonReferenceType().hasNonTrivialObjCLifetime()) |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4970 | return false; |
| 4971 | |
| 4972 | // The initialization succeeded; now make sure there are no non-trivial |
| 4973 | // calls. |
| 4974 | return !Result.get()->hasNonTrivialCall(S.Context); |
| 4975 | } |
| 4976 | |
| 4977 | llvm_unreachable("unhandled type trait"); |
| 4978 | return false; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4979 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4980 | default: llvm_unreachable("not a TT"); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4981 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4982 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4983 | return false; |
| 4984 | } |
| 4985 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4986 | ExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 4987 | ArrayRef<TypeSourceInfo *> Args, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4988 | SourceLocation RParenLoc) { |
Alp Toker | 5294e6e | 2013-12-25 01:47:02 +0000 | [diff] [blame] | 4989 | QualType ResultType = Context.getLogicalOperationType(); |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4990 | |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4991 | if (Kind <= UTT_Last && !CheckUnaryTypeTraitTypeCompleteness( |
| 4992 | *this, Kind, KWLoc, Args[0]->getType())) |
| 4993 | return ExprError(); |
| 4994 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4995 | bool Dependent = false; |
| 4996 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 4997 | if (Args[I]->getType()->isDependentType()) { |
| 4998 | Dependent = true; |
| 4999 | break; |
| 5000 | } |
| 5001 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 5002 | |
| 5003 | bool Result = false; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5004 | if (!Dependent) |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 5005 | Result = evaluateTypeTrait(*this, Kind, KWLoc, Args, RParenLoc); |
| 5006 | |
| 5007 | return TypeTraitExpr::Create(Context, ResultType, KWLoc, Kind, Args, |
| 5008 | RParenLoc, Result); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
Alp Toker | 88f64e6 | 2013-12-13 21:19:30 +0000 | [diff] [blame] | 5011 | ExprResult Sema::ActOnTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 5012 | ArrayRef<ParsedType> Args, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5013 | SourceLocation RParenLoc) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5014 | SmallVector<TypeSourceInfo *, 4> ConvertedArgs; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5015 | ConvertedArgs.reserve(Args.size()); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5016 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5017 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 5018 | TypeSourceInfo *TInfo; |
| 5019 | QualType T = GetTypeFromParser(Args[I], &TInfo); |
| 5020 | if (!TInfo) |
| 5021 | TInfo = Context.getTrivialTypeSourceInfo(T, KWLoc); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5022 | |
| 5023 | ConvertedArgs.push_back(TInfo); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5024 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 5025 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5026 | return BuildTypeTrait(Kind, KWLoc, ConvertedArgs, RParenLoc); |
| 5027 | } |
| 5028 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 5029 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
| 5030 | QualType RhsT, SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 5031 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
| 5032 | "Cannot evaluate traits of dependent types"); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 5033 | |
| 5034 | switch(BTT) { |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 5035 | case BTT_IsBaseOf: { |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 5036 | // C++0x [meta.rel]p2 |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 5037 | // Base is a base class of Derived without regard to cv-qualifiers or |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 5038 | // Base and Derived are not unions and name the same class type without |
| 5039 | // regard to cv-qualifiers. |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 5040 | |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 5041 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 5042 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
Erik Pilkington | 07f8c43 | 2017-05-10 17:18:56 +0000 | [diff] [blame] | 5043 | if (!rhsRecord || !lhsRecord) { |
| 5044 | const ObjCObjectType *LHSObjTy = LhsT->getAs<ObjCObjectType>(); |
| 5045 | const ObjCObjectType *RHSObjTy = RhsT->getAs<ObjCObjectType>(); |
| 5046 | if (!LHSObjTy || !RHSObjTy) |
| 5047 | return false; |
| 5048 | |
| 5049 | ObjCInterfaceDecl *BaseInterface = LHSObjTy->getInterface(); |
| 5050 | ObjCInterfaceDecl *DerivedInterface = RHSObjTy->getInterface(); |
| 5051 | if (!BaseInterface || !DerivedInterface) |
| 5052 | return false; |
| 5053 | |
| 5054 | if (Self.RequireCompleteType( |
| 5055 | KeyLoc, RhsT, diag::err_incomplete_type_used_in_type_trait_expr)) |
| 5056 | return false; |
| 5057 | |
| 5058 | return BaseInterface->isSuperClassOf(DerivedInterface); |
| 5059 | } |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 5060 | |
| 5061 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
| 5062 | == (lhsRecord == rhsRecord)); |
| 5063 | |
| 5064 | if (lhsRecord == rhsRecord) |
| 5065 | return !lhsRecord->getDecl()->isUnion(); |
| 5066 | |
| 5067 | // C++0x [meta.rel]p2: |
| 5068 | // If Base and Derived are class types and are different types |
| 5069 | // (ignoring possible cv-qualifiers) then Derived shall be a |
| 5070 | // complete type. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5071 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 5072 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 5073 | return false; |
| 5074 | |
| 5075 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) |
| 5076 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); |
| 5077 | } |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 5078 | case BTT_IsSame: |
| 5079 | return Self.Context.hasSameType(LhsT, RhsT); |
George Burgess IV | 31ac1fa | 2017-10-16 22:58:37 +0000 | [diff] [blame] | 5080 | case BTT_TypeCompatible: { |
| 5081 | // GCC ignores cv-qualifiers on arrays for this builtin. |
| 5082 | Qualifiers LhsQuals, RhsQuals; |
| 5083 | QualType Lhs = Self.getASTContext().getUnqualifiedArrayType(LhsT, LhsQuals); |
| 5084 | QualType Rhs = Self.getASTContext().getUnqualifiedArrayType(RhsT, RhsQuals); |
| 5085 | return Self.Context.typesAreCompatible(Lhs, Rhs); |
| 5086 | } |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 5087 | case BTT_IsConvertible: |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5088 | case BTT_IsConvertibleTo: { |
| 5089 | // C++0x [meta.rel]p4: |
| 5090 | // Given the following function prototype: |
| 5091 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5092 | // template <class T> |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5093 | // typename add_rvalue_reference<T>::type create(); |
| 5094 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5095 | // the predicate condition for a template specialization |
| 5096 | // is_convertible<From, To> shall be satisfied if and only if |
| 5097 | // the return expression in the following code would be |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5098 | // well-formed, including any implicit conversions to the return |
| 5099 | // type of the function: |
| 5100 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5101 | // To test() { |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5102 | // return create<From>(); |
| 5103 | // } |
| 5104 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5105 | // Access checking is performed as if in a context unrelated to To and |
| 5106 | // From. Only the validity of the immediate context of the expression |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5107 | // of the return-statement (including conversions to the return type) |
| 5108 | // is considered. |
| 5109 | // |
| 5110 | // We model the initialization as a copy-initialization of a temporary |
| 5111 | // of the appropriate type, which for this expression is identical to the |
| 5112 | // return statement (since NRVO doesn't apply). |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 5113 | |
| 5114 | // Functions aren't allowed to return function or array types. |
| 5115 | if (RhsT->isFunctionType() || RhsT->isArrayType()) |
| 5116 | return false; |
| 5117 | |
| 5118 | // A return statement in a void function must have void type. |
| 5119 | if (RhsT->isVoidType()) |
| 5120 | return LhsT->isVoidType(); |
| 5121 | |
| 5122 | // A function definition requires a complete, non-abstract return type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5123 | if (!Self.isCompleteType(KeyLoc, RhsT) || Self.isAbstractType(KeyLoc, RhsT)) |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 5124 | return false; |
| 5125 | |
| 5126 | // Compute the result of add_rvalue_reference. |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5127 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 5128 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 5129 | |
| 5130 | // Build a fake source and destination for initialization. |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5131 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5132 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5133 | Expr::getValueKindForType(LhsT)); |
| 5134 | Expr *FromPtr = &From; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5135 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5136 | SourceLocation())); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5137 | |
| 5138 | // Perform the initialization in an unevaluated context within a SFINAE |
Eli Friedman | a59b190 | 2012-01-25 01:05:57 +0000 | [diff] [blame] | 5139 | // trap at translation unit scope. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 5140 | EnterExpressionEvaluationContext Unevaluated( |
| 5141 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 5142 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 5143 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5144 | InitializationSequence Init(Self, To, Kind, FromPtr); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5145 | if (Init.Failed()) |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5146 | return false; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 5147 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5148 | ExprResult Result = Init.Perform(Self, To, Kind, FromPtr); |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 5149 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); |
| 5150 | } |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 5151 | |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 5152 | case BTT_IsAssignable: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 5153 | case BTT_IsNothrowAssignable: |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5154 | case BTT_IsTriviallyAssignable: { |
| 5155 | // C++11 [meta.unary.prop]p3: |
| 5156 | // is_trivially_assignable is defined as: |
| 5157 | // is_assignable<T, U>::value is true and the assignment, as defined by |
| 5158 | // is_assignable, is known to call no operation that is not trivial |
| 5159 | // |
| 5160 | // is_assignable is defined as: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5161 | // The expression declval<T>() = declval<U>() is well-formed when |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5162 | // treated as an unevaluated operand (Clause 5). |
| 5163 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5164 | // For both, T and U shall be complete types, (possibly cv-qualified) |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5165 | // void, or arrays of unknown bound. |
| 5166 | if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() && |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5167 | Self.RequireCompleteType(KeyLoc, LhsT, |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5168 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 5169 | return false; |
| 5170 | if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() && |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5171 | Self.RequireCompleteType(KeyLoc, RhsT, |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5172 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 5173 | return false; |
| 5174 | |
| 5175 | // cv void is never assignable. |
| 5176 | if (LhsT->isVoidType() || RhsT->isVoidType()) |
| 5177 | return false; |
| 5178 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5179 | // Build expressions that emulate the effect of declval<T>() and |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5180 | // declval<U>(). |
| 5181 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 5182 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
| 5183 | if (RhsT->isObjectType() || RhsT->isFunctionType()) |
| 5184 | RhsT = Self.Context.getRValueReferenceType(RhsT); |
| 5185 | OpaqueValueExpr Lhs(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
| 5186 | Expr::getValueKindForType(LhsT)); |
| 5187 | OpaqueValueExpr Rhs(KeyLoc, RhsT.getNonLValueExprType(Self.Context), |
| 5188 | Expr::getValueKindForType(RhsT)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5189 | |
| 5190 | // Attempt the assignment in an unevaluated context within a SFINAE |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5191 | // trap at translation unit scope. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 5192 | EnterExpressionEvaluationContext Unevaluated( |
| 5193 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5194 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
Erich Keane | 1a3b8fd | 2017-12-12 16:22:31 +0000 | [diff] [blame] | 5195 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5196 | ExprResult Result = Self.BuildBinOp(/*S=*/nullptr, KeyLoc, BO_Assign, &Lhs, |
| 5197 | &Rhs); |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5198 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 5199 | return false; |
| 5200 | |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 5201 | if (BTT == BTT_IsAssignable) |
| 5202 | return true; |
| 5203 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 5204 | if (BTT == BTT_IsNothrowAssignable) |
| 5205 | return Self.canThrow(Result.get()) == CT_Cannot; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 5206 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 5207 | if (BTT == BTT_IsTriviallyAssignable) { |
Brian Kelley | 93c640b | 2017-03-29 17:40:35 +0000 | [diff] [blame] | 5208 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
| 5209 | // Objective-C lifetime, this is a non-trivial assignment. |
| 5210 | if (LhsT.getNonReferenceType().hasNonTrivialObjCLifetime()) |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 5211 | return false; |
| 5212 | |
| 5213 | return !Result.get()->hasNonTrivialCall(Self.Context); |
| 5214 | } |
| 5215 | |
| 5216 | llvm_unreachable("unhandled type trait"); |
| 5217 | return false; |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5218 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 5219 | default: llvm_unreachable("not a BTT"); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 5220 | } |
| 5221 | llvm_unreachable("Unknown type trait or not implemented"); |
| 5222 | } |
| 5223 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5224 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
| 5225 | SourceLocation KWLoc, |
| 5226 | ParsedType Ty, |
| 5227 | Expr* DimExpr, |
| 5228 | SourceLocation RParen) { |
| 5229 | TypeSourceInfo *TSInfo; |
| 5230 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
| 5231 | if (!TSInfo) |
| 5232 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
| 5233 | |
| 5234 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
| 5235 | } |
| 5236 | |
| 5237 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
| 5238 | QualType T, Expr *DimExpr, |
| 5239 | SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 5240 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5241 | |
| 5242 | switch(ATT) { |
| 5243 | case ATT_ArrayRank: |
| 5244 | if (T->isArrayType()) { |
| 5245 | unsigned Dim = 0; |
| 5246 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 5247 | ++Dim; |
| 5248 | T = AT->getElementType(); |
| 5249 | } |
| 5250 | return Dim; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5251 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 5252 | return 0; |
| 5253 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5254 | case ATT_ArrayExtent: { |
| 5255 | llvm::APSInt Value; |
| 5256 | uint64_t Dim; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5257 | if (Self.VerifyIntegerConstantExpression(DimExpr, &Value, |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5258 | diag::err_dimension_expr_not_constant_integer, |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5259 | false).isInvalid()) |
| 5260 | return 0; |
| 5261 | if (Value.isSigned() && Value.isNegative()) { |
Daniel Dunbar | 900cead | 2012-03-09 21:38:22 +0000 | [diff] [blame] | 5262 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) |
| 5263 | << DimExpr->getSourceRange(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5264 | return 0; |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 5265 | } |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5266 | Dim = Value.getLimitedValue(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5267 | |
| 5268 | if (T->isArrayType()) { |
| 5269 | unsigned D = 0; |
| 5270 | bool Matched = false; |
| 5271 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 5272 | if (Dim == D) { |
| 5273 | Matched = true; |
| 5274 | break; |
| 5275 | } |
| 5276 | ++D; |
| 5277 | T = AT->getElementType(); |
| 5278 | } |
| 5279 | |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 5280 | if (Matched && T->isArrayType()) { |
| 5281 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
| 5282 | return CAT->getSize().getLimitedValue(); |
| 5283 | } |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5284 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 5285 | return 0; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5286 | } |
| 5287 | } |
| 5288 | llvm_unreachable("Unknown type trait or not implemented"); |
| 5289 | } |
| 5290 | |
| 5291 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
| 5292 | SourceLocation KWLoc, |
| 5293 | TypeSourceInfo *TSInfo, |
| 5294 | Expr* DimExpr, |
| 5295 | SourceLocation RParen) { |
| 5296 | QualType T = TSInfo->getType(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5297 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 5298 | // FIXME: This should likely be tracked as an APInt to remove any host |
| 5299 | // assumptions about the width of size_t on the target. |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 5300 | uint64_t Value = 0; |
| 5301 | if (!T->isDependentType()) |
| 5302 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); |
| 5303 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 5304 | // While the specification for these traits from the Embarcadero C++ |
| 5305 | // compiler's documentation says the return type is 'unsigned int', Clang |
| 5306 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero |
| 5307 | // compiler, there is no difference. On several other platforms this is an |
| 5308 | // important distinction. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5309 | return new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, DimExpr, |
| 5310 | RParen, Context.getSizeType()); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5313 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5314 | SourceLocation KWLoc, |
| 5315 | Expr *Queried, |
| 5316 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5317 | // If error parsing the expression, ignore. |
| 5318 | if (!Queried) |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5319 | return ExprError(); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5320 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5321 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5322 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5323 | return Result; |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5326 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
| 5327 | switch (ET) { |
| 5328 | case ET_IsLValueExpr: return E->isLValue(); |
| 5329 | case ET_IsRValueExpr: return E->isRValue(); |
| 5330 | } |
| 5331 | llvm_unreachable("Expression trait not covered by switch"); |
| 5332 | } |
| 5333 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5334 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5335 | SourceLocation KWLoc, |
| 5336 | Expr *Queried, |
| 5337 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5338 | if (Queried->isTypeDependent()) { |
| 5339 | // Delay type-checking for type-dependent expressions. |
| 5340 | } else if (Queried->getType()->isPlaceholderType()) { |
| 5341 | ExprResult PE = CheckPlaceholderExpr(Queried); |
| 5342 | if (PE.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5343 | return BuildExpressionTrait(ET, KWLoc, PE.get(), RParen); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5346 | bool Value = EvaluateExpressionTrait(ET, Queried); |
Chandler Carruth | f57eba3 | 2011-05-01 08:48:19 +0000 | [diff] [blame] | 5347 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5348 | return new (Context) |
| 5349 | ExpressionTraitExpr(KWLoc, ET, Queried, Value, RParen, Context.BoolTy); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5352 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5353 | ExprValueKind &VK, |
| 5354 | SourceLocation Loc, |
| 5355 | bool isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5356 | assert(!LHS.get()->getType()->isPlaceholderType() && |
| 5357 | !RHS.get()->getType()->isPlaceholderType() && |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 5358 | "placeholders should have been weeded out by now"); |
| 5359 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 5360 | // The LHS undergoes lvalue conversions if this is ->*, and undergoes the |
| 5361 | // temporary materialization conversion otherwise. |
| 5362 | if (isIndirect) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5363 | LHS = DefaultLvalueConversion(LHS.get()); |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 5364 | else if (LHS.get()->isRValue()) |
| 5365 | LHS = TemporaryMaterializationConversion(LHS.get()); |
| 5366 | if (LHS.isInvalid()) |
| 5367 | return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 5368 | |
| 5369 | // The RHS always undergoes lvalue conversions. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5370 | RHS = DefaultLvalueConversion(RHS.get()); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5371 | if (RHS.isInvalid()) return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 5372 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5373 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 5374 | // C++ 5.5p2 |
| 5375 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 5376 | // be of type "pointer to member of T" (where T is a completely-defined |
| 5377 | // class type) [...] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5378 | QualType RHSType = RHS.get()->getType(); |
| 5379 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5380 | if (!MemPtr) { |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5381 | Diag(Loc, diag::err_bad_memptr_rhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5382 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5383 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5384 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5385 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5386 | QualType Class(MemPtr->getClass(), 0); |
| 5387 | |
Douglas Gregor | d07ba34 | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 5388 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
| 5389 | // member pointer points must be completely-defined. However, there is no |
| 5390 | // reason for this semantic distinction, and the rule is not enforced by |
| 5391 | // other compilers. Therefore, we do not check this property, as it is |
| 5392 | // likely to be considered a defect. |
Sebastian Redl | c72350e | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 5393 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5394 | // C++ 5.5p2 |
| 5395 | // [...] to its first operand, which shall be of class T or of a class of |
| 5396 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 5397 | // such a class] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5398 | QualType LHSType = LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5399 | if (isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5400 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) |
| 5401 | LHSType = Ptr->getPointeeType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5402 | else { |
| 5403 | Diag(Loc, diag::err_bad_memptr_lhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5404 | << OpSpelling << 1 << LHSType |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5405 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5406 | return QualType(); |
| 5407 | } |
| 5408 | } |
| 5409 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5410 | if (!Context.hasSameUnqualifiedType(Class, LHSType)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 5411 | // If we want to check the hierarchy, we need a complete type. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5412 | if (RequireCompleteType(Loc, LHSType, diag::err_bad_memptr_lhs, |
| 5413 | OpSpelling, (int)isIndirect)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 5414 | return QualType(); |
| 5415 | } |
Richard Smith | db05cd3 | 2013-12-12 03:40:18 +0000 | [diff] [blame] | 5416 | |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5417 | if (!IsDerivedFrom(Loc, LHSType, Class)) { |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5418 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5419 | << (int)isIndirect << LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5420 | return QualType(); |
| 5421 | } |
Richard Smith | db05cd3 | 2013-12-12 03:40:18 +0000 | [diff] [blame] | 5422 | |
| 5423 | CXXCastPath BasePath; |
| 5424 | if (CheckDerivedToBaseConversion(LHSType, Class, Loc, |
| 5425 | SourceRange(LHS.get()->getLocStart(), |
| 5426 | RHS.get()->getLocEnd()), |
| 5427 | &BasePath)) |
| 5428 | return QualType(); |
| 5429 | |
Eli Friedman | 1fcf66b | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 5430 | // Cast LHS to type of use. |
Richard Smith | 01e4a7f2 | 2017-06-09 22:25:28 +0000 | [diff] [blame] | 5431 | QualType UseType = Context.getQualifiedType(Class, LHSType.getQualifiers()); |
| 5432 | if (isIndirect) |
| 5433 | UseType = Context.getPointerType(UseType); |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5434 | ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5435 | LHS = ImpCastExprToType(LHS.get(), UseType, CK_DerivedToBase, VK, |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5436 | &BasePath); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5439 | if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) { |
Fariborz Jahanian | 1bc0f9a | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 5440 | // Diagnose use of pointer-to-member type which when used as |
| 5441 | // the functional cast in a pointer-to-member expression. |
| 5442 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 5443 | return QualType(); |
| 5444 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5445 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5446 | // C++ 5.5p2 |
| 5447 | // The result is an object or a function of the type specified by the |
| 5448 | // second operand. |
| 5449 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 5450 | // in accordance with 5.5p5 and 5.2.5. |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5451 | QualType Result = MemPtr->getPointeeType(); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5452 | Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5453 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5454 | // C++0x [expr.mptr.oper]p6: |
| 5455 | // In a .* expression whose object expression is an rvalue, the program is |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5456 | // ill-formed if the second operand is a pointer to member function with |
| 5457 | // ref-qualifier &. In a ->* expression or in a .* expression whose object |
| 5458 | // expression is an lvalue, the program is ill-formed if the second operand |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5459 | // is a pointer to member function with ref-qualifier &&. |
| 5460 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
| 5461 | switch (Proto->getRefQualifier()) { |
| 5462 | case RQ_None: |
| 5463 | // Do nothing |
| 5464 | break; |
| 5465 | |
| 5466 | case RQ_LValue: |
Richard Smith | 2592327 | 2017-08-25 01:47:55 +0000 | [diff] [blame] | 5467 | if (!isIndirect && !LHS.get()->Classify(Context).isLValue()) { |
Nicolas Lesser | 1ad0e9f | 2018-07-13 16:27:45 +0000 | [diff] [blame] | 5468 | // C++2a allows functions with ref-qualifier & if their cv-qualifier-seq |
| 5469 | // is (exactly) 'const'. |
| 5470 | if (Proto->isConst() && !Proto->isVolatile()) |
Richard Smith | 2592327 | 2017-08-25 01:47:55 +0000 | [diff] [blame] | 5471 | Diag(Loc, getLangOpts().CPlusPlus2a |
| 5472 | ? diag::warn_cxx17_compat_pointer_to_const_ref_member_on_rvalue |
| 5473 | : diag::ext_pointer_to_const_ref_member_on_rvalue); |
| 5474 | else |
| 5475 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
| 5476 | << RHSType << 1 << LHS.get()->getSourceRange(); |
| 5477 | } |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5478 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5479 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5480 | case RQ_RValue: |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5481 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5482 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5483 | << RHSType << 0 << LHS.get()->getSourceRange(); |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5484 | break; |
| 5485 | } |
| 5486 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5487 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5488 | // C++ [expr.mptr.oper]p6: |
| 5489 | // The result of a .* expression whose second operand is a pointer |
| 5490 | // to a data member is of the same value category as its |
| 5491 | // first operand. The result of a .* expression whose second |
| 5492 | // operand is a pointer to a member function is a prvalue. The |
| 5493 | // result of an ->* expression is an lvalue if its second operand |
| 5494 | // is a pointer to data member and a prvalue otherwise. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5495 | if (Result->isFunctionType()) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5496 | VK = VK_RValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5497 | return Context.BoundMemberTy; |
| 5498 | } else if (isIndirect) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5499 | VK = VK_LValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5500 | } else { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5501 | VK = LHS.get()->getValueKind(); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5502 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5503 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5504 | return Result; |
| 5505 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5506 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5507 | /// Try to convert a type to another according to C++11 5.16p3. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5508 | /// |
| 5509 | /// This is part of the parameter validation for the ? operator. If either |
| 5510 | /// value operand is a class type, the two operands are attempted to be |
| 5511 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5512 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 5513 | /// as such. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5514 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 5515 | SourceLocation QuestionLoc, |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5516 | bool &HaveConversion, |
| 5517 | QualType &ToType) { |
| 5518 | HaveConversion = false; |
| 5519 | ToType = To->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5520 | |
| 5521 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5522 | SourceLocation()); |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5523 | // C++11 5.16p3 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5524 | // The process for determining whether an operand expression E1 of type T1 |
| 5525 | // can be converted to match an operand expression E2 of type T2 is defined |
| 5526 | // as follows: |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5527 | // -- If E2 is an lvalue: E1 can be converted to match E2 if E1 can be |
| 5528 | // implicitly converted to type "lvalue reference to T2", subject to the |
| 5529 | // constraint that in the conversion the reference must bind directly to |
| 5530 | // an lvalue. |
| 5531 | // -- If E2 is an xvalue: E1 can be converted to match E2 if E1 can be |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5532 | // implicitly converted to the type "rvalue reference to R2", subject to |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5533 | // the constraint that the reference must bind directly. |
| 5534 | if (To->isLValue() || To->isXValue()) { |
| 5535 | QualType T = To->isLValue() ? Self.Context.getLValueReferenceType(ToType) |
| 5536 | : Self.Context.getRValueReferenceType(ToType); |
| 5537 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5538 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5539 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5540 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5541 | if (InitSeq.isDirectReferenceBinding()) { |
| 5542 | ToType = T; |
| 5543 | HaveConversion = true; |
| 5544 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5545 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5546 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5547 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5548 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5549 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5550 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5551 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 5552 | // -- if E1 and E2 have class type, and the underlying class types are |
| 5553 | // the same or one is a base class of the other: |
| 5554 | QualType FTy = From->getType(); |
| 5555 | QualType TTy = To->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5556 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 5557 | const RecordType *TRec = TTy->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5558 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5559 | Self.IsDerivedFrom(QuestionLoc, FTy, TTy); |
| 5560 | if (FRec && TRec && (FRec == TRec || FDerivedFromT || |
| 5561 | Self.IsDerivedFrom(QuestionLoc, TTy, FTy))) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5562 | // E1 can be converted to match E2 if the class of T2 is the |
| 5563 | // same type as, or a base class of, the class of T1, and |
| 5564 | // [cv2 > cv1]. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5565 | if (FRec == TRec || FDerivedFromT) { |
| 5566 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5567 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5568 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5569 | if (InitSeq) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5570 | HaveConversion = true; |
| 5571 | return false; |
| 5572 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5573 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5574 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5575 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5576 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5577 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5578 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5579 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5580 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5581 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5582 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 5583 | // implicitly converted to the type that expression E2 would have |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5584 | // if E2 were converted to an rvalue (or the type it has, if E2 is |
Douglas Gregor | f9edf80 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 5585 | // an rvalue). |
| 5586 | // |
| 5587 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 5588 | // to the array-to-pointer or function-to-pointer conversions. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5589 | TTy = TTy.getNonLValueExprType(Self.Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5590 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5591 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5592 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5593 | HaveConversion = !InitSeq.Failed(); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5594 | ToType = TTy; |
| 5595 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5596 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5597 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5598 | return false; |
| 5599 | } |
| 5600 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5601 | /// Try to find a common type for two according to C++0x 5.16p5. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5602 | /// |
| 5603 | /// This is part of the parameter validation for the ? operator. If either |
| 5604 | /// value operand is a class type, overload resolution is used to find a |
| 5605 | /// conversion to a common type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5606 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5607 | SourceLocation QuestionLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5608 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5609 | OverloadCandidateSet CandidateSet(QuestionLoc, |
| 5610 | OverloadCandidateSet::CSK_Operator); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 5611 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5612 | CandidateSet); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5613 | |
| 5614 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5615 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5616 | case OR_Success: { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5617 | // We found a match. Perform the conversions on the arguments and move on. |
George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 5618 | ExprResult LHSRes = Self.PerformImplicitConversion( |
| 5619 | LHS.get(), Best->BuiltinParamTypes[0], Best->Conversions[0], |
| 5620 | Sema::AA_Converting); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5621 | if (LHSRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5622 | break; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5623 | LHS = LHSRes; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5624 | |
George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 5625 | ExprResult RHSRes = Self.PerformImplicitConversion( |
| 5626 | RHS.get(), Best->BuiltinParamTypes[1], Best->Conversions[1], |
| 5627 | Sema::AA_Converting); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5628 | if (RHSRes.isInvalid()) |
| 5629 | break; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5630 | RHS = RHSRes; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 5631 | if (Best->Function) |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5632 | Self.MarkFunctionReferenced(QuestionLoc, Best->Function); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5633 | return false; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5634 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5635 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5636 | case OR_No_Viable_Function: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5637 | |
| 5638 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5639 | // constant and the other is a pointer type. In this case, the user most |
| 5640 | // likely forgot to take the address of the other expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5641 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5642 | return true; |
| 5643 | |
| 5644 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5645 | << LHS.get()->getType() << RHS.get()->getType() |
| 5646 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5647 | return true; |
| 5648 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5649 | case OR_Ambiguous: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5650 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5651 | << LHS.get()->getType() << RHS.get()->getType() |
| 5652 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5653 | // FIXME: Print the possible common types by printing the return types of |
| 5654 | // the viable candidates. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5655 | break; |
| 5656 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5657 | case OR_Deleted: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5658 | llvm_unreachable("Conditional operator has only built-in overloads"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5659 | } |
| 5660 | return true; |
| 5661 | } |
| 5662 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5663 | /// Perform an "extended" implicit conversion as returned by |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5664 | /// TryClassUnification. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5665 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5666 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5667 | InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5668 | SourceLocation()); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5669 | Expr *Arg = E.get(); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5670 | InitializationSequence InitSeq(Self, Entity, Kind, Arg); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5671 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, Arg); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5672 | if (Result.isInvalid()) |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5673 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5674 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5675 | E = Result; |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5676 | return false; |
| 5677 | } |
| 5678 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5679 | /// Check the operands of ?: under C++ semantics. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5680 | /// |
| 5681 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 5682 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5683 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 5684 | ExprResult &RHS, ExprValueKind &VK, |
| 5685 | ExprObjectKind &OK, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5686 | SourceLocation QuestionLoc) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5687 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 5688 | // interface pointers. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5689 | |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5690 | // C++11 [expr.cond]p1 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5691 | // The first expression is contextually converted to bool. |
Simon Dardis | 7cd5876 | 2017-05-12 19:11:06 +0000 | [diff] [blame] | 5692 | // |
| 5693 | // FIXME; GCC's vector extension permits the use of a?b:c where the type of |
| 5694 | // a is that of a integer vector with the same number of elements and |
| 5695 | // size as the vectors of b and c. If one of either b or c is a scalar |
| 5696 | // it is implicitly converted to match the type of the vector. |
| 5697 | // Otherwise the expression is ill-formed. If both b and c are scalars, |
| 5698 | // then b and c are checked and converted to the type of a if possible. |
| 5699 | // Unlike the OpenCL ?: operator, the expression is evaluated as |
| 5700 | // (a[0] != 0 ? b[0] : c[0], .. , a[n] != 0 ? b[n] : c[n]). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5701 | if (!Cond.get()->isTypeDependent()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5702 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5703 | if (CondRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5704 | return QualType(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5705 | Cond = CondRes; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5706 | } |
| 5707 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5708 | // Assume r-value. |
| 5709 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5710 | OK = OK_Ordinary; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5711 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5712 | // Either of the arguments dependent? |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5713 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5714 | return Context.DependentTy; |
| 5715 | |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5716 | // C++11 [expr.cond]p2 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5717 | // If either the second or the third operand has type (cv) void, ... |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5718 | QualType LTy = LHS.get()->getType(); |
| 5719 | QualType RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5720 | bool LVoid = LTy->isVoidType(); |
| 5721 | bool RVoid = RTy->isVoidType(); |
| 5722 | if (LVoid || RVoid) { |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 5723 | // ... one of the following shall hold: |
| 5724 | // -- The second or the third operand (but not both) is a (possibly |
| 5725 | // parenthesized) throw-expression; the result is of the type |
| 5726 | // and value category of the other. |
| 5727 | bool LThrow = isa<CXXThrowExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 5728 | bool RThrow = isa<CXXThrowExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 5729 | if (LThrow != RThrow) { |
| 5730 | Expr *NonThrow = LThrow ? RHS.get() : LHS.get(); |
| 5731 | VK = NonThrow->getValueKind(); |
| 5732 | // DR (no number yet): the result is a bit-field if the |
| 5733 | // non-throw-expression operand is a bit-field. |
| 5734 | OK = NonThrow->getObjectKind(); |
| 5735 | return NonThrow->getType(); |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5736 | } |
| 5737 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5738 | // -- Both the second and third operands have type void; the result is of |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5739 | // type void and is a prvalue. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5740 | if (LVoid && RVoid) |
| 5741 | return Context.VoidTy; |
| 5742 | |
| 5743 | // Neither holds, error. |
| 5744 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 5745 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5746 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5747 | return QualType(); |
| 5748 | } |
| 5749 | |
| 5750 | // Neither is void. |
| 5751 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5752 | // C++11 [expr.cond]p3 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5753 | // Otherwise, if the second and third operand have different types, and |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5754 | // either has (cv) class type [...] an attempt is made to convert each of |
| 5755 | // those operands to the type of the other. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5756 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5757 | (LTy->isRecordType() || RTy->isRecordType())) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5758 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5759 | QualType L2RType, R2LType; |
| 5760 | bool HaveL2R, HaveR2L; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5761 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5762 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5763 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5764 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5765 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5766 | // If both can be converted, [...] the program is ill-formed. |
| 5767 | if (HaveL2R && HaveR2L) { |
| 5768 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5769 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5770 | return QualType(); |
| 5771 | } |
| 5772 | |
| 5773 | // If exactly one conversion is possible, that conversion is applied to |
| 5774 | // the chosen operand and the converted operands are used in place of the |
| 5775 | // original operands for the remainder of this section. |
| 5776 | if (HaveL2R) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5777 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5778 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5779 | LTy = LHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5780 | } else if (HaveR2L) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5781 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5782 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5783 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5784 | } |
| 5785 | } |
| 5786 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5787 | // C++11 [expr.cond]p3 |
| 5788 | // if both are glvalues of the same value category and the same type except |
| 5789 | // for cv-qualification, an attempt is made to convert each of those |
| 5790 | // operands to the type of the other. |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5791 | // FIXME: |
| 5792 | // Resolving a defect in P0012R1: we extend this to cover all cases where |
| 5793 | // one of the operands is reference-compatible with the other, in order |
| 5794 | // to support conditionals between functions differing in noexcept. |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5795 | ExprValueKind LVK = LHS.get()->getValueKind(); |
| 5796 | ExprValueKind RVK = RHS.get()->getValueKind(); |
| 5797 | if (!Context.hasSameType(LTy, RTy) && |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5798 | LVK == RVK && LVK != VK_RValue) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5799 | // DerivedToBase was already handled by the class-specific case above. |
| 5800 | // FIXME: Should we allow ObjC conversions here? |
| 5801 | bool DerivedToBase, ObjCConversion, ObjCLifetimeConversion; |
| 5802 | if (CompareReferenceRelationship( |
| 5803 | QuestionLoc, LTy, RTy, DerivedToBase, |
| 5804 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5805 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
| 5806 | // [...] subject to the constraint that the reference must bind |
| 5807 | // directly [...] |
| 5808 | !RHS.get()->refersToBitField() && |
| 5809 | !RHS.get()->refersToVectorElement()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5810 | RHS = ImpCastExprToType(RHS.get(), LTy, CK_NoOp, RVK); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5811 | RTy = RHS.get()->getType(); |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5812 | } else if (CompareReferenceRelationship( |
| 5813 | QuestionLoc, RTy, LTy, DerivedToBase, |
| 5814 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5815 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
| 5816 | !LHS.get()->refersToBitField() && |
| 5817 | !LHS.get()->refersToVectorElement()) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5818 | LHS = ImpCastExprToType(LHS.get(), RTy, CK_NoOp, LVK); |
| 5819 | LTy = LHS.get()->getType(); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5820 | } |
| 5821 | } |
| 5822 | |
| 5823 | // C++11 [expr.cond]p4 |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5824 | // If the second and third operands are glvalues of the same value |
| 5825 | // category and have the same type, the result is of that type and |
| 5826 | // value category and it is a bit-field if the second or the third |
| 5827 | // operand is a bit-field, or if both are bit-fields. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5828 | // We only extend this to bitfields, not to the crazy other kinds of |
| 5829 | // l-values. |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 5830 | bool Same = Context.hasSameType(LTy, RTy); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5831 | if (Same && LVK == RVK && LVK != VK_RValue && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5832 | LHS.get()->isOrdinaryOrBitFieldObject() && |
| 5833 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
| 5834 | VK = LHS.get()->getValueKind(); |
| 5835 | if (LHS.get()->getObjectKind() == OK_BitField || |
| 5836 | RHS.get()->getObjectKind() == OK_BitField) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5837 | OK = OK_BitField; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5838 | |
| 5839 | // If we have function pointer types, unify them anyway to unify their |
| 5840 | // exception specifications, if any. |
| 5841 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
| 5842 | Qualifiers Qs = LTy.getQualifiers(); |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5843 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5844 | /*ConvertArgs*/false); |
| 5845 | LTy = Context.getQualifiedType(LTy, Qs); |
| 5846 | |
| 5847 | assert(!LTy.isNull() && "failed to find composite pointer type for " |
| 5848 | "canonically equivalent function ptr types"); |
| 5849 | assert(Context.hasSameType(LTy, RTy) && "bad composite pointer type"); |
| 5850 | } |
| 5851 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5852 | return LTy; |
Fariborz Jahanian | c60da03 | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 5853 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5854 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5855 | // C++11 [expr.cond]p5 |
| 5856 | // Otherwise, the result is a prvalue. If the second and third operands |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5857 | // do not have the same type, and either has (cv) class type, ... |
| 5858 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 5859 | // ... overload resolution is used to determine the conversions (if any) |
| 5860 | // to be applied to the operands. If the overload resolution fails, the |
| 5861 | // program is ill-formed. |
| 5862 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 5863 | return QualType(); |
| 5864 | } |
| 5865 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5866 | // C++11 [expr.cond]p6 |
| 5867 | // Lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5868 | // conversions are performed on the second and third operands. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5869 | LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); |
| 5870 | RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5871 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5872 | return QualType(); |
| 5873 | LTy = LHS.get()->getType(); |
| 5874 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5875 | |
| 5876 | // After those conversions, one of the following shall hold: |
| 5877 | // -- The second and third operands have the same type; the result |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5878 | // is of that type. If the operands have class type, the result |
| 5879 | // is a prvalue temporary of the result type, which is |
| 5880 | // copy-initialized from either the second operand or the third |
| 5881 | // operand depending on the value of the first operand. |
| 5882 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 5883 | if (LTy->isRecordType()) { |
| 5884 | // The operands have class type. Make a temporary copy. |
| 5885 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
David Blaikie | 6154ef9 | 2012-09-10 22:05:41 +0000 | [diff] [blame] | 5886 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5887 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
| 5888 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5889 | LHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5890 | if (LHSCopy.isInvalid()) |
| 5891 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5892 | |
| 5893 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
| 5894 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5895 | RHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5896 | if (RHSCopy.isInvalid()) |
| 5897 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5898 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5899 | LHS = LHSCopy; |
| 5900 | RHS = RHSCopy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5901 | } |
| 5902 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5903 | // If we have function pointer types, unify them anyway to unify their |
| 5904 | // exception specifications, if any. |
| 5905 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
| 5906 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS); |
| 5907 | assert(!LTy.isNull() && "failed to find composite pointer type for " |
| 5908 | "canonically equivalent function ptr types"); |
| 5909 | } |
| 5910 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5911 | return LTy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5912 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5913 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 5914 | // Extension: conditional operator involving vector types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5915 | if (LTy->isVectorType() || RTy->isVectorType()) |
Ulrich Weigand | 3c5038a | 2015-07-30 14:08:36 +0000 | [diff] [blame] | 5916 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false, |
| 5917 | /*AllowBothBool*/true, |
| 5918 | /*AllowBoolConversions*/false); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 5919 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5920 | // -- The second and third operands have arithmetic or enumeration type; |
| 5921 | // the usual arithmetic conversions are performed to bring them to a |
| 5922 | // common type, and the result is of that type. |
| 5923 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 5924 | QualType ResTy = UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5925 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5926 | return QualType(); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5927 | if (ResTy.isNull()) { |
| 5928 | Diag(QuestionLoc, |
| 5929 | diag::err_typecheck_cond_incompatible_operands) << LTy << RTy |
| 5930 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5931 | return QualType(); |
| 5932 | } |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 5933 | |
| 5934 | LHS = ImpCastExprToType(LHS.get(), ResTy, PrepareScalarCast(LHS, ResTy)); |
| 5935 | RHS = ImpCastExprToType(RHS.get(), ResTy, PrepareScalarCast(RHS, ResTy)); |
| 5936 | |
| 5937 | return ResTy; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5938 | } |
| 5939 | |
| 5940 | // -- The second and third operands have pointer type, or one has pointer |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5941 | // type and the other is a null pointer constant, or both are null |
| 5942 | // pointer constants, at least one of which is non-integral; pointer |
| 5943 | // conversions and qualification conversions are performed to bring them |
| 5944 | // to their composite pointer type. The result is of the composite |
| 5945 | // pointer type. |
Eli Friedman | 81390df | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 5946 | // -- The second and third operands have pointer to member type, or one has |
| 5947 | // pointer to member type and the other is a null pointer constant; |
| 5948 | // pointer to member conversions and qualification conversions are |
| 5949 | // performed to bring them to a common type, whose cv-qualification |
| 5950 | // shall match the cv-qualification of either the second or the third |
| 5951 | // operand. The result is of the common type. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5952 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS); |
| 5953 | if (!Composite.isNull()) |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5954 | return Composite; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5955 | |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 5956 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 798d2bd | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 5957 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 5958 | if (!Composite.isNull()) |
| 5959 | return Composite; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5960 | |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 5961 | // Check if we are using a null with a non-pointer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5962 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 5963 | return QualType(); |
| 5964 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5965 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5966 | << LHS.get()->getType() << RHS.get()->getType() |
| 5967 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5968 | return QualType(); |
| 5969 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5970 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5971 | static FunctionProtoType::ExceptionSpecInfo |
| 5972 | mergeExceptionSpecs(Sema &S, FunctionProtoType::ExceptionSpecInfo ESI1, |
| 5973 | FunctionProtoType::ExceptionSpecInfo ESI2, |
| 5974 | SmallVectorImpl<QualType> &ExceptionTypeStorage) { |
| 5975 | ExceptionSpecificationType EST1 = ESI1.Type; |
| 5976 | ExceptionSpecificationType EST2 = ESI2.Type; |
| 5977 | |
| 5978 | // If either of them can throw anything, that is the result. |
| 5979 | if (EST1 == EST_None) return ESI1; |
| 5980 | if (EST2 == EST_None) return ESI2; |
| 5981 | if (EST1 == EST_MSAny) return ESI1; |
| 5982 | if (EST2 == EST_MSAny) return ESI2; |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 5983 | if (EST1 == EST_NoexceptFalse) return ESI1; |
| 5984 | if (EST2 == EST_NoexceptFalse) return ESI2; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5985 | |
| 5986 | // If either of them is non-throwing, the result is the other. |
| 5987 | if (EST1 == EST_DynamicNone) return ESI2; |
| 5988 | if (EST2 == EST_DynamicNone) return ESI1; |
| 5989 | if (EST1 == EST_BasicNoexcept) return ESI2; |
| 5990 | if (EST2 == EST_BasicNoexcept) return ESI1; |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 5991 | if (EST1 == EST_NoexceptTrue) return ESI2; |
| 5992 | if (EST2 == EST_NoexceptTrue) return ESI1; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5993 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5994 | // If we're left with value-dependent computed noexcept expressions, we're |
| 5995 | // stuck. Before C++17, we can just drop the exception specification entirely, |
| 5996 | // since it's not actually part of the canonical type. And this should never |
| 5997 | // happen in C++17, because it would mean we were computing the composite |
| 5998 | // pointer type of dependent types, which should never happen. |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 5999 | if (EST1 == EST_DependentNoexcept || EST2 == EST_DependentNoexcept) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6000 | assert(!S.getLangOpts().CPlusPlus17 && |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6001 | "computing composite pointer type of dependent types"); |
| 6002 | return FunctionProtoType::ExceptionSpecInfo(); |
| 6003 | } |
| 6004 | |
| 6005 | // Switch over the possibilities so that people adding new values know to |
| 6006 | // update this function. |
| 6007 | switch (EST1) { |
| 6008 | case EST_None: |
| 6009 | case EST_DynamicNone: |
| 6010 | case EST_MSAny: |
| 6011 | case EST_BasicNoexcept: |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 6012 | case EST_DependentNoexcept: |
| 6013 | case EST_NoexceptFalse: |
| 6014 | case EST_NoexceptTrue: |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6015 | llvm_unreachable("handled above"); |
| 6016 | |
| 6017 | case EST_Dynamic: { |
| 6018 | // This is the fun case: both exception specifications are dynamic. Form |
| 6019 | // the union of the two lists. |
| 6020 | assert(EST2 == EST_Dynamic && "other cases should already be handled"); |
| 6021 | llvm::SmallPtrSet<QualType, 8> Found; |
| 6022 | for (auto &Exceptions : {ESI1.Exceptions, ESI2.Exceptions}) |
| 6023 | for (QualType E : Exceptions) |
| 6024 | if (Found.insert(S.Context.getCanonicalType(E)).second) |
| 6025 | ExceptionTypeStorage.push_back(E); |
| 6026 | |
| 6027 | FunctionProtoType::ExceptionSpecInfo Result(EST_Dynamic); |
| 6028 | Result.Exceptions = ExceptionTypeStorage; |
| 6029 | return Result; |
| 6030 | } |
| 6031 | |
| 6032 | case EST_Unevaluated: |
| 6033 | case EST_Uninstantiated: |
| 6034 | case EST_Unparsed: |
| 6035 | llvm_unreachable("shouldn't see unresolved exception specifications here"); |
| 6036 | } |
| 6037 | |
| 6038 | llvm_unreachable("invalid ExceptionSpecificationType"); |
| 6039 | } |
| 6040 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6041 | /// Find a merged pointer type and convert the two expressions to it. |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6042 | /// |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6043 | /// This finds the composite pointer type (or member pointer type) for @p E1 |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6044 | /// and @p E2 according to C++1z 5p14. It converts both expressions to this |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6045 | /// type and returns it. |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6046 | /// It does not emit diagnostics. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6047 | /// |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6048 | /// \param Loc The location of the operator requiring these two expressions to |
| 6049 | /// be converted to the composite pointer type. |
| 6050 | /// |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6051 | /// \param ConvertArgs If \c false, do not convert E1 and E2 to the target type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6052 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6053 | Expr *&E1, Expr *&E2, |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6054 | bool ConvertArgs) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6055 | assert(getLangOpts().CPlusPlus && "This function assumes C++"); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6056 | |
| 6057 | // C++1z [expr]p14: |
| 6058 | // The composite pointer type of two operands p1 and p2 having types T1 |
| 6059 | // and T2 |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6060 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6061 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6062 | // where at least one is a pointer or pointer to member type or |
| 6063 | // std::nullptr_t is: |
| 6064 | bool T1IsPointerLike = T1->isAnyPointerType() || T1->isMemberPointerType() || |
| 6065 | T1->isNullPtrType(); |
| 6066 | bool T2IsPointerLike = T2->isAnyPointerType() || T2->isMemberPointerType() || |
| 6067 | T2->isNullPtrType(); |
| 6068 | if (!T1IsPointerLike && !T2IsPointerLike) |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 6069 | return QualType(); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 6070 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6071 | // - if both p1 and p2 are null pointer constants, std::nullptr_t; |
| 6072 | // This can't actually happen, following the standard, but we also use this |
| 6073 | // to implement the end of [expr.conv], which hits this case. |
| 6074 | // |
| 6075 | // - if either p1 or p2 is a null pointer constant, T2 or T1, respectively; |
| 6076 | if (T1IsPointerLike && |
| 6077 | E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6078 | if (ConvertArgs) |
| 6079 | E2 = ImpCastExprToType(E2, T1, T1->isMemberPointerType() |
| 6080 | ? CK_NullToMemberPointer |
| 6081 | : CK_NullToPointer).get(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6082 | return T1; |
| 6083 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6084 | if (T2IsPointerLike && |
| 6085 | E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6086 | if (ConvertArgs) |
| 6087 | E1 = ImpCastExprToType(E1, T2, T2->isMemberPointerType() |
| 6088 | ? CK_NullToMemberPointer |
| 6089 | : CK_NullToPointer).get(); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6090 | return T2; |
| 6091 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6092 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6093 | // Now both have to be pointers or member pointers. |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6094 | if (!T1IsPointerLike || !T2IsPointerLike) |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6095 | return QualType(); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6096 | assert(!T1->isNullPtrType() && !T2->isNullPtrType() && |
| 6097 | "nullptr_t should be a null pointer constant"); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6098 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6099 | // - if T1 or T2 is "pointer to cv1 void" and the other type is |
| 6100 | // "pointer to cv2 T", "pointer to cv12 void", where cv12 is |
| 6101 | // the union of cv1 and cv2; |
| 6102 | // - if T1 or T2 is "pointer to noexcept function" and the other type is |
| 6103 | // "pointer to function", where the function types are otherwise the same, |
| 6104 | // "pointer to function"; |
| 6105 | // FIXME: This rule is defective: it should also permit removing noexcept |
| 6106 | // from a pointer to member function. As a Clang extension, we also |
| 6107 | // permit removing 'noreturn', so we generalize this rule to; |
| 6108 | // - [Clang] If T1 and T2 are both of type "pointer to function" or |
| 6109 | // "pointer to member function" and the pointee types can be unified |
| 6110 | // by a function pointer conversion, that conversion is applied |
| 6111 | // before checking the following rules. |
| 6112 | // - if T1 is "pointer to cv1 C1" and T2 is "pointer to cv2 C2", where C1 |
| 6113 | // is reference-related to C2 or C2 is reference-related to C1 (8.6.3), |
| 6114 | // the cv-combined type of T1 and T2 or the cv-combined type of T2 and T1, |
| 6115 | // respectively; |
| 6116 | // - if T1 is "pointer to member of C1 of type cv1 U1" and T2 is "pointer |
| 6117 | // to member of C2 of type cv2 U2" where C1 is reference-related to C2 or |
| 6118 | // C2 is reference-related to C1 (8.6.3), the cv-combined type of T2 and |
| 6119 | // T1 or the cv-combined type of T1 and T2, respectively; |
| 6120 | // - if T1 and T2 are similar types (4.5), the cv-combined type of T1 and |
| 6121 | // T2; |
| 6122 | // |
| 6123 | // If looked at in the right way, these bullets all do the same thing. |
| 6124 | // What we do here is, we build the two possible cv-combined types, and try |
| 6125 | // the conversions in both directions. If only one works, or if the two |
| 6126 | // composite types are the same, we have succeeded. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6127 | // FIXME: extended qualifiers? |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6128 | // |
| 6129 | // Note that this will fail to find a composite pointer type for "pointer |
| 6130 | // to void" and "pointer to function". We can't actually perform the final |
| 6131 | // conversion in this case, even though a composite pointer type formally |
| 6132 | // exists. |
| 6133 | SmallVector<unsigned, 4> QualifierUnion; |
| 6134 | SmallVector<std::pair<const Type *, const Type *>, 4> MemberOfClass; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6135 | QualType Composite1 = T1; |
| 6136 | QualType Composite2 = T2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6137 | unsigned NeedConstBefore = 0; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6138 | while (true) { |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6139 | const PointerType *Ptr1, *Ptr2; |
| 6140 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 6141 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 6142 | Composite1 = Ptr1->getPointeeType(); |
| 6143 | Composite2 = Ptr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6144 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6145 | // If we're allowed to create a non-standard composite type, keep track |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6146 | // of where we need to fill in additional 'const' qualifiers. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 6147 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6148 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6149 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6150 | QualifierUnion.push_back( |
| 6151 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6152 | MemberOfClass.push_back(std::make_pair(nullptr, nullptr)); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6153 | continue; |
| 6154 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6155 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6156 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 6157 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 6158 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 6159 | Composite1 = MemPtr1->getPointeeType(); |
| 6160 | Composite2 = MemPtr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6161 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6162 | // If we're allowed to create a non-standard composite type, keep track |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6163 | // of where we need to fill in additional 'const' qualifiers. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 6164 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6165 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6166 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6167 | QualifierUnion.push_back( |
| 6168 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 6169 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 6170 | MemPtr2->getClass())); |
| 6171 | continue; |
| 6172 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6173 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6174 | // FIXME: block pointer types? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6175 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6176 | // Cannot unwrap any more types. |
| 6177 | break; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6178 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6179 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6180 | // Apply the function pointer conversion to unify the types. We've already |
| 6181 | // unwrapped down to the function types, and we want to merge rather than |
| 6182 | // just convert, so do this ourselves rather than calling |
| 6183 | // IsFunctionConversion. |
| 6184 | // |
| 6185 | // FIXME: In order to match the standard wording as closely as possible, we |
| 6186 | // currently only do this under a single level of pointers. Ideally, we would |
| 6187 | // allow this in general, and set NeedConstBefore to the relevant depth on |
| 6188 | // the side(s) where we changed anything. |
| 6189 | if (QualifierUnion.size() == 1) { |
| 6190 | if (auto *FPT1 = Composite1->getAs<FunctionProtoType>()) { |
| 6191 | if (auto *FPT2 = Composite2->getAs<FunctionProtoType>()) { |
| 6192 | FunctionProtoType::ExtProtoInfo EPI1 = FPT1->getExtProtoInfo(); |
| 6193 | FunctionProtoType::ExtProtoInfo EPI2 = FPT2->getExtProtoInfo(); |
| 6194 | |
| 6195 | // The result is noreturn if both operands are. |
| 6196 | bool Noreturn = |
| 6197 | EPI1.ExtInfo.getNoReturn() && EPI2.ExtInfo.getNoReturn(); |
| 6198 | EPI1.ExtInfo = EPI1.ExtInfo.withNoReturn(Noreturn); |
| 6199 | EPI2.ExtInfo = EPI2.ExtInfo.withNoReturn(Noreturn); |
| 6200 | |
| 6201 | // The result is nothrow if both operands are. |
| 6202 | SmallVector<QualType, 8> ExceptionTypeStorage; |
| 6203 | EPI1.ExceptionSpec = EPI2.ExceptionSpec = |
| 6204 | mergeExceptionSpecs(*this, EPI1.ExceptionSpec, EPI2.ExceptionSpec, |
| 6205 | ExceptionTypeStorage); |
| 6206 | |
| 6207 | Composite1 = Context.getFunctionType(FPT1->getReturnType(), |
| 6208 | FPT1->getParamTypes(), EPI1); |
| 6209 | Composite2 = Context.getFunctionType(FPT2->getReturnType(), |
| 6210 | FPT2->getParamTypes(), EPI2); |
| 6211 | } |
| 6212 | } |
| 6213 | } |
| 6214 | |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 6215 | if (NeedConstBefore) { |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6216 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6217 | // mismatch, so that our (non-standard!) composite type meets the |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6218 | // requirements of C++ [conv.qual]p4 bullet 3. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 6219 | for (unsigned I = 0; I != NeedConstBefore; ++I) |
| 6220 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6221 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6222 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6223 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6224 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6225 | auto MOC = MemberOfClass.rbegin(); |
| 6226 | for (unsigned CVR : llvm::reverse(QualifierUnion)) { |
| 6227 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 6228 | auto Classes = *MOC++; |
| 6229 | if (Classes.first && Classes.second) { |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6230 | // Rebuild member pointer type |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6231 | Composite1 = Context.getMemberPointerType( |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6232 | Context.getQualifiedType(Composite1, Quals), Classes.first); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6233 | Composite2 = Context.getMemberPointerType( |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6234 | Context.getQualifiedType(Composite2, Quals), Classes.second); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6235 | } else { |
| 6236 | // Rebuild pointer type |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6237 | Composite1 = |
| 6238 | Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 6239 | Composite2 = |
| 6240 | Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6241 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6242 | } |
| 6243 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6244 | struct Conversion { |
| 6245 | Sema &S; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6246 | Expr *&E1, *&E2; |
| 6247 | QualType Composite; |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 6248 | InitializedEntity Entity; |
| 6249 | InitializationKind Kind; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6250 | InitializationSequence E1ToC, E2ToC; |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 6251 | bool Viable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6252 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6253 | Conversion(Sema &S, SourceLocation Loc, Expr *&E1, Expr *&E2, |
| 6254 | QualType Composite) |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 6255 | : S(S), E1(E1), E2(E2), Composite(Composite), |
| 6256 | Entity(InitializedEntity::InitializeTemporary(Composite)), |
| 6257 | Kind(InitializationKind::CreateCopy(Loc, SourceLocation())), |
| 6258 | E1ToC(S, Entity, Kind, E1), E2ToC(S, Entity, Kind, E2), |
| 6259 | Viable(E1ToC && E2ToC) {} |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6260 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6261 | bool perform() { |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6262 | ExprResult E1Result = E1ToC.Perform(S, Entity, Kind, E1); |
| 6263 | if (E1Result.isInvalid()) |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6264 | return true; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6265 | E1 = E1Result.getAs<Expr>(); |
| 6266 | |
| 6267 | ExprResult E2Result = E2ToC.Perform(S, Entity, Kind, E2); |
| 6268 | if (E2Result.isInvalid()) |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6269 | return true; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6270 | E2 = E2Result.getAs<Expr>(); |
| 6271 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6272 | return false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6273 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6274 | }; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6275 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6276 | // Try to convert to each composite pointer type. |
| 6277 | Conversion C1(*this, Loc, E1, E2, Composite1); |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6278 | if (C1.Viable && Context.hasSameType(Composite1, Composite2)) { |
| 6279 | if (ConvertArgs && C1.perform()) |
| 6280 | return QualType(); |
| 6281 | return C1.Composite; |
| 6282 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6283 | Conversion C2(*this, Loc, E1, E2, Composite2); |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6284 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6285 | if (C1.Viable == C2.Viable) { |
| 6286 | // Either Composite1 and Composite2 are viable and are different, or |
| 6287 | // neither is viable. |
| 6288 | // FIXME: How both be viable and different? |
| 6289 | return QualType(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6290 | } |
| 6291 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6292 | // Convert to the chosen type. |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6293 | if (ConvertArgs && (C1.Viable ? C1 : C2).perform()) |
| 6294 | return QualType(); |
| 6295 | |
| 6296 | return C1.Viable ? C1.Composite : C2.Composite; |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6297 | } |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 6298 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6299 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Douglas Gregor | 298087b | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 6300 | if (!E) |
| 6301 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6302 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6303 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 6304 | |
| 6305 | // If the result is a glvalue, we shouldn't bind it. |
| 6306 | if (!E->isRValue()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6307 | return E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6308 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6309 | // In ARC, calls that return a retainable type can return retained, |
| 6310 | // in which case we have to insert a consuming cast. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6311 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6312 | E->getType()->isObjCRetainableType()) { |
| 6313 | |
| 6314 | bool ReturnsRetained; |
| 6315 | |
| 6316 | // For actual calls, we compute this by examining the type of the |
| 6317 | // called value. |
| 6318 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { |
| 6319 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
| 6320 | QualType T = Callee->getType(); |
| 6321 | |
| 6322 | if (T == Context.BoundMemberTy) { |
| 6323 | // Handle pointer-to-members. |
| 6324 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) |
| 6325 | T = BinOp->getRHS()->getType(); |
| 6326 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) |
| 6327 | T = Mem->getMemberDecl()->getType(); |
| 6328 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6329 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6330 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
| 6331 | T = Ptr->getPointeeType(); |
| 6332 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
| 6333 | T = Ptr->getPointeeType(); |
| 6334 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
| 6335 | T = MemPtr->getPointeeType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6336 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6337 | const FunctionType *FTy = T->getAs<FunctionType>(); |
| 6338 | assert(FTy && "call to value not of function type?"); |
| 6339 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
| 6340 | |
| 6341 | // ActOnStmtExpr arranges things so that StmtExprs of retainable |
| 6342 | // type always produce a +1 object. |
| 6343 | } else if (isa<StmtExpr>(E)) { |
| 6344 | ReturnsRetained = true; |
| 6345 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6346 | // We hit this case with the lambda conversion-to-block optimization; |
| 6347 | // we don't want any extra casts here. |
| 6348 | } else if (isa<CastExpr>(E) && |
| 6349 | isa<BlockExpr>(cast<CastExpr>(E)->getSubExpr())) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6350 | return E; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6351 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6352 | // For message sends and property references, we try to find an |
| 6353 | // actual method. FIXME: we should infer retention by selector in |
| 6354 | // cases where we don't have an actual method. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6355 | } else { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6356 | ObjCMethodDecl *D = nullptr; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6357 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { |
| 6358 | D = Send->getMethodDecl(); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 6359 | } else if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(E)) { |
| 6360 | D = BoxedExpr->getBoxingMethod(); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6361 | } else if (ObjCArrayLiteral *ArrayLit = dyn_cast<ObjCArrayLiteral>(E)) { |
Akira Hatanaka | 4d53a1c | 2017-04-15 06:42:00 +0000 | [diff] [blame] | 6362 | // Don't do reclaims if we're using the zero-element array |
| 6363 | // constant. |
| 6364 | if (ArrayLit->getNumElements() == 0 && |
| 6365 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
| 6366 | return E; |
| 6367 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6368 | D = ArrayLit->getArrayWithObjectsMethod(); |
| 6369 | } else if (ObjCDictionaryLiteral *DictLit |
| 6370 | = dyn_cast<ObjCDictionaryLiteral>(E)) { |
Akira Hatanaka | 4d53a1c | 2017-04-15 06:42:00 +0000 | [diff] [blame] | 6371 | // Don't do reclaims if we're using the zero-element dictionary |
| 6372 | // constant. |
| 6373 | if (DictLit->getNumElements() == 0 && |
| 6374 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
| 6375 | return E; |
| 6376 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6377 | D = DictLit->getDictWithObjectsMethod(); |
| 6378 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6379 | |
| 6380 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
John McCall | 32a4da0 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 6381 | |
| 6382 | // Don't do reclaims on performSelector calls; despite their |
| 6383 | // return type, the invoked method doesn't necessarily actually |
| 6384 | // return an object. |
| 6385 | if (!ReturnsRetained && |
| 6386 | D && D->getMethodFamily() == OMF_performSelector) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6387 | return E; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6388 | } |
| 6389 | |
John McCall | 16de4d2 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 6390 | // Don't reclaim an object of Class type. |
| 6391 | if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6392 | return E; |
John McCall | 16de4d2 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 6393 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6394 | Cleanup.setExprNeedsCleanups(true); |
John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 6395 | |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6396 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject |
| 6397 | : CK_ARCReclaimReturnedObject); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6398 | return ImplicitCastExpr::Create(Context, E->getType(), ck, E, nullptr, |
| 6399 | VK_RValue); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6402 | if (!getLangOpts().CPlusPlus) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6403 | return E; |
Douglas Gregor | 363b151 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 6404 | |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6405 | // Search for the base element type (cf. ASTContext::getBaseElementType) with |
| 6406 | // a fast path for the common case that the type is directly a RecordType. |
| 6407 | const Type *T = Context.getCanonicalType(E->getType().getTypePtr()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6408 | const RecordType *RT = nullptr; |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6409 | while (!RT) { |
| 6410 | switch (T->getTypeClass()) { |
| 6411 | case Type::Record: |
| 6412 | RT = cast<RecordType>(T); |
| 6413 | break; |
| 6414 | case Type::ConstantArray: |
| 6415 | case Type::IncompleteArray: |
| 6416 | case Type::VariableArray: |
| 6417 | case Type::DependentSizedArray: |
| 6418 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
| 6419 | break; |
| 6420 | default: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6421 | return E; |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6422 | } |
| 6423 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6424 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6425 | // That should be enough to guarantee that this type is complete, if we're |
| 6426 | // not processing a decltype expression. |
Jeffrey Yasskin | bbc4eea | 2011-01-27 19:17:54 +0000 | [diff] [blame] | 6427 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6428 | if (RD->isInvalidDecl() || RD->isDependentContext()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6429 | return E; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6430 | |
Nicolas Lesser | b6d5c58 | 2018-07-12 18:45:41 +0000 | [diff] [blame] | 6431 | bool IsDecltype = ExprEvalContexts.back().ExprContext == |
| 6432 | ExpressionEvaluationContextRecord::EK_Decltype; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6433 | CXXDestructorDecl *Destructor = IsDecltype ? nullptr : LookupDestructor(RD); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6434 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6435 | if (Destructor) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6436 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
John McCall | 8e36d53 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 6437 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 6438 | PDiag(diag::err_access_dtor_temp) |
| 6439 | << E->getType()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6440 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
| 6441 | return ExprError(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6442 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6443 | // If destructor is trivial, we can avoid the extra copy. |
| 6444 | if (Destructor->isTrivial()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6445 | return E; |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6446 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6447 | // We need a cleanup, but we don't need to remember the temporary. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6448 | Cleanup.setExprNeedsCleanups(true); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6449 | } |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6450 | |
| 6451 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6452 | CXXBindTemporaryExpr *Bind = CXXBindTemporaryExpr::Create(Context, Temp, E); |
| 6453 | |
| 6454 | if (IsDecltype) |
| 6455 | ExprEvalContexts.back().DelayedDecltypeBinds.push_back(Bind); |
| 6456 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6457 | return Bind; |
Anders Carlsson | 2d4cada | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 6458 | } |
| 6459 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6460 | ExprResult |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6461 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6462 | if (SubExpr.isInvalid()) |
| 6463 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6464 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6465 | return MaybeCreateExprWithCleanups(SubExpr.get()); |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6468 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 6469 | assert(SubExpr && "subexpression can't be null!"); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6470 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 6471 | CleanupVarDeclMarking(); |
| 6472 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6473 | unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects; |
| 6474 | assert(ExprCleanupObjects.size() >= FirstCleanup); |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6475 | assert(Cleanup.exprNeedsCleanups() || |
| 6476 | ExprCleanupObjects.size() == FirstCleanup); |
| 6477 | if (!Cleanup.exprNeedsCleanups()) |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6478 | return SubExpr; |
| 6479 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6480 | auto Cleanups = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup, |
| 6481 | ExprCleanupObjects.size() - FirstCleanup); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6482 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6483 | auto *E = ExprWithCleanups::Create( |
| 6484 | Context, SubExpr, Cleanup.cleanupsHaveSideEffects(), Cleanups); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6485 | DiscardCleanupsInEvaluationContext(); |
| 6486 | |
| 6487 | return E; |
| 6488 | } |
| 6489 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6490 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 6491 | assert(SubStmt && "sub-statement can't be null!"); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6492 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 6493 | CleanupVarDeclMarking(); |
| 6494 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6495 | if (!Cleanup.exprNeedsCleanups()) |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6496 | return SubStmt; |
| 6497 | |
| 6498 | // FIXME: In order to attach the temporaries, wrap the statement into |
| 6499 | // a StmtExpr; currently this is only used for asm statements. |
| 6500 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
| 6501 | // a new AsmStmtWithTemporaries. |
Benjamin Kramer | 0742090 | 2017-12-24 16:24:20 +0000 | [diff] [blame] | 6502 | CompoundStmt *CompStmt = CompoundStmt::Create( |
| 6503 | Context, SubStmt, SourceLocation(), SourceLocation()); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6504 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
| 6505 | SourceLocation()); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6506 | return MaybeCreateExprWithCleanups(E); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6509 | /// Process the expression contained within a decltype. For such expressions, |
| 6510 | /// certain semantic checks on temporaries are delayed until this point, and |
| 6511 | /// are omitted for the 'topmost' call in the decltype expression. If the |
| 6512 | /// topmost call bound a temporary, strip that temporary off the expression. |
| 6513 | ExprResult Sema::ActOnDecltypeExpression(Expr *E) { |
Nicolas Lesser | b6d5c58 | 2018-07-12 18:45:41 +0000 | [diff] [blame] | 6514 | assert(ExprEvalContexts.back().ExprContext == |
| 6515 | ExpressionEvaluationContextRecord::EK_Decltype && |
| 6516 | "not in a decltype expression"); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6517 | |
| 6518 | // C++11 [expr.call]p11: |
| 6519 | // If a function call is a prvalue of object type, |
| 6520 | // -- if the function call is either |
| 6521 | // -- the operand of a decltype-specifier, or |
| 6522 | // -- the right operand of a comma operator that is the operand of a |
| 6523 | // decltype-specifier, |
| 6524 | // a temporary object is not introduced for the prvalue. |
| 6525 | |
| 6526 | // Recursively rebuild ParenExprs and comma expressions to strip out the |
| 6527 | // outermost CXXBindTemporaryExpr, if any. |
| 6528 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 6529 | ExprResult SubExpr = ActOnDecltypeExpression(PE->getSubExpr()); |
| 6530 | if (SubExpr.isInvalid()) |
| 6531 | return ExprError(); |
| 6532 | if (SubExpr.get() == PE->getSubExpr()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6533 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6534 | return ActOnParenExpr(PE->getLParen(), PE->getRParen(), SubExpr.get()); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6535 | } |
| 6536 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 6537 | if (BO->getOpcode() == BO_Comma) { |
| 6538 | ExprResult RHS = ActOnDecltypeExpression(BO->getRHS()); |
| 6539 | if (RHS.isInvalid()) |
| 6540 | return ExprError(); |
| 6541 | if (RHS.get() == BO->getRHS()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6542 | return E; |
| 6543 | return new (Context) BinaryOperator( |
| 6544 | BO->getLHS(), RHS.get(), BO_Comma, BO->getType(), BO->getValueKind(), |
Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 6545 | BO->getObjectKind(), BO->getOperatorLoc(), BO->getFPFeatures()); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6546 | } |
| 6547 | } |
| 6548 | |
| 6549 | CXXBindTemporaryExpr *TopBind = dyn_cast<CXXBindTemporaryExpr>(E); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6550 | CallExpr *TopCall = TopBind ? dyn_cast<CallExpr>(TopBind->getSubExpr()) |
| 6551 | : nullptr; |
Richard Smith | 202dc13 | 2014-02-18 03:51:47 +0000 | [diff] [blame] | 6552 | if (TopCall) |
| 6553 | E = TopCall; |
| 6554 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6555 | TopBind = nullptr; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6556 | |
| 6557 | // Disable the special decltype handling now. |
Nicolas Lesser | b6d5c58 | 2018-07-12 18:45:41 +0000 | [diff] [blame] | 6558 | ExprEvalContexts.back().ExprContext = |
| 6559 | ExpressionEvaluationContextRecord::EK_Other; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6560 | |
Richard Smith | f86b0ae | 2012-07-28 19:54:11 +0000 | [diff] [blame] | 6561 | // In MS mode, don't perform any extra checking of call return types within a |
| 6562 | // decltype expression. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 6563 | if (getLangOpts().MSVCCompat) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6564 | return E; |
Richard Smith | f86b0ae | 2012-07-28 19:54:11 +0000 | [diff] [blame] | 6565 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6566 | // Perform the semantic checks we delayed until this point. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6567 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeCalls.size(); |
| 6568 | I != N; ++I) { |
| 6569 | CallExpr *Call = ExprEvalContexts.back().DelayedDecltypeCalls[I]; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6570 | if (Call == TopCall) |
| 6571 | continue; |
| 6572 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6573 | if (CheckCallReturnType(Call->getCallReturnType(Context), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6574 | Call->getLocStart(), |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6575 | Call, Call->getDirectCallee())) |
| 6576 | return ExprError(); |
| 6577 | } |
| 6578 | |
| 6579 | // Now all relevant types are complete, check the destructors are accessible |
| 6580 | // and non-deleted, and annotate them on the temporaries. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6581 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeBinds.size(); |
| 6582 | I != N; ++I) { |
| 6583 | CXXBindTemporaryExpr *Bind = |
| 6584 | ExprEvalContexts.back().DelayedDecltypeBinds[I]; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6585 | if (Bind == TopBind) |
| 6586 | continue; |
| 6587 | |
| 6588 | CXXTemporary *Temp = Bind->getTemporary(); |
| 6589 | |
| 6590 | CXXRecordDecl *RD = |
| 6591 | Bind->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6592 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
| 6593 | Temp->setDestructor(Destructor); |
| 6594 | |
Richard Smith | 7d847b1 | 2012-05-11 22:20:10 +0000 | [diff] [blame] | 6595 | MarkFunctionReferenced(Bind->getExprLoc(), Destructor); |
| 6596 | CheckDestructorAccess(Bind->getExprLoc(), Destructor, |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6597 | PDiag(diag::err_access_dtor_temp) |
Richard Smith | 7d847b1 | 2012-05-11 22:20:10 +0000 | [diff] [blame] | 6598 | << Bind->getType()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6599 | if (DiagnoseUseOfDecl(Destructor, Bind->getExprLoc())) |
| 6600 | return ExprError(); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6601 | |
| 6602 | // We need a cleanup, but we don't need to remember the temporary. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6603 | Cleanup.setExprNeedsCleanups(true); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6604 | } |
| 6605 | |
| 6606 | // Possibly strip off the top CXXBindTemporaryExpr. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6607 | return E; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6608 | } |
| 6609 | |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6610 | /// Note a set of 'operator->' functions that were used for a member access. |
| 6611 | static void noteOperatorArrows(Sema &S, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 6612 | ArrayRef<FunctionDecl *> OperatorArrows) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6613 | unsigned SkipStart = OperatorArrows.size(), SkipCount = 0; |
| 6614 | // FIXME: Make this configurable? |
| 6615 | unsigned Limit = 9; |
| 6616 | if (OperatorArrows.size() > Limit) { |
| 6617 | // Produce Limit-1 normal notes and one 'skipping' note. |
| 6618 | SkipStart = (Limit - 1) / 2 + (Limit - 1) % 2; |
| 6619 | SkipCount = OperatorArrows.size() - (Limit - 1); |
| 6620 | } |
| 6621 | |
| 6622 | for (unsigned I = 0; I < OperatorArrows.size(); /**/) { |
| 6623 | if (I == SkipStart) { |
| 6624 | S.Diag(OperatorArrows[I]->getLocation(), |
| 6625 | diag::note_operator_arrows_suppressed) |
| 6626 | << SkipCount; |
| 6627 | I += SkipCount; |
| 6628 | } else { |
| 6629 | S.Diag(OperatorArrows[I]->getLocation(), diag::note_operator_arrow_here) |
| 6630 | << OperatorArrows[I]->getCallResultType(); |
| 6631 | ++I; |
| 6632 | } |
| 6633 | } |
| 6634 | } |
| 6635 | |
Nico Weber | 964d332 | 2015-02-16 22:35:45 +0000 | [diff] [blame] | 6636 | ExprResult Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, |
| 6637 | SourceLocation OpLoc, |
| 6638 | tok::TokenKind OpKind, |
| 6639 | ParsedType &ObjectType, |
| 6640 | bool &MayBePseudoDestructor) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6641 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6642 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6643 | if (Result.isInvalid()) return ExprError(); |
| 6644 | Base = Result.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6645 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6646 | Result = CheckPlaceholderExpr(Base); |
| 6647 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6648 | Base = Result.get(); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6649 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6650 | QualType BaseType = Base->getType(); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6651 | MayBePseudoDestructor = false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6652 | if (BaseType->isDependentType()) { |
Douglas Gregor | 4112718 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 6653 | // If we have a pointer to a dependent type and are using the -> operator, |
| 6654 | // the object type is the type that the pointer points to. We might still |
| 6655 | // have enough information about that type to do something useful. |
| 6656 | if (OpKind == tok::arrow) |
| 6657 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 6658 | BaseType = Ptr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6659 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6660 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6661 | MayBePseudoDestructor = true; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6662 | return Base; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6663 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6664 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6665 | // C++ [over.match.oper]p8: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6666 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6667 | // returned, with the original second operand. |
| 6668 | if (OpKind == tok::arrow) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6669 | QualType StartingType = BaseType; |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6670 | bool NoArrowOperatorFound = false; |
| 6671 | bool FirstIteration = true; |
| 6672 | FunctionDecl *CurFD = dyn_cast<FunctionDecl>(CurContext); |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 6673 | // The set of types we've considered so far. |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 6674 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6675 | SmallVector<FunctionDecl*, 8> OperatorArrows; |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 6676 | CTypes.insert(Context.getCanonicalType(BaseType)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6677 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6678 | while (BaseType->isRecordType()) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6679 | if (OperatorArrows.size() >= getLangOpts().ArrowDepth) { |
| 6680 | Diag(OpLoc, diag::err_operator_arrow_depth_exceeded) |
Richard Smith | 9dbc574 | 2013-11-06 19:43:09 +0000 | [diff] [blame] | 6681 | << StartingType << getLangOpts().ArrowDepth << Base->getSourceRange(); |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6682 | noteOperatorArrows(*this, OperatorArrows); |
| 6683 | Diag(OpLoc, diag::note_operator_arrow_depth) |
| 6684 | << getLangOpts().ArrowDepth; |
| 6685 | return ExprError(); |
| 6686 | } |
| 6687 | |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6688 | Result = BuildOverloadedArrowExpr( |
| 6689 | S, Base, OpLoc, |
| 6690 | // When in a template specialization and on the first loop iteration, |
| 6691 | // potentially give the default diagnostic (with the fixit in a |
| 6692 | // separate note) instead of having the error reported back to here |
| 6693 | // and giving a diagnostic with a fixit attached to the error itself. |
| 6694 | (FirstIteration && CurFD && CurFD->isFunctionTemplateSpecialization()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6695 | ? nullptr |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6696 | : &NoArrowOperatorFound); |
| 6697 | if (Result.isInvalid()) { |
| 6698 | if (NoArrowOperatorFound) { |
| 6699 | if (FirstIteration) { |
| 6700 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Richard Smith | 9dbc574 | 2013-11-06 19:43:09 +0000 | [diff] [blame] | 6701 | << BaseType << 1 << Base->getSourceRange() |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6702 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 6703 | OpKind = tok::period; |
| 6704 | break; |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 6705 | } |
| 6706 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 6707 | << BaseType << Base->getSourceRange(); |
| 6708 | CallExpr *CE = dyn_cast<CallExpr>(Base); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6709 | if (Decl *CD = (CE ? CE->getCalleeDecl() : nullptr)) { |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 6710 | Diag(CD->getLocStart(), |
| 6711 | diag::note_member_reference_arrow_from_operator_arrow); |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6712 | } |
| 6713 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6714 | return ExprError(); |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6715 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6716 | Base = Result.get(); |
| 6717 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6718 | OperatorArrows.push_back(OpCall->getDirectCallee()); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6719 | BaseType = Base->getType(); |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 6720 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6721 | if (!CTypes.insert(CBaseType).second) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6722 | Diag(OpLoc, diag::err_operator_arrow_circular) << StartingType; |
| 6723 | noteOperatorArrows(*this, OperatorArrows); |
Fariborz Jahanian | 10ce958 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 6724 | return ExprError(); |
| 6725 | } |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6726 | FirstIteration = false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6727 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6728 | |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6729 | if (OpKind == tok::arrow && |
| 6730 | (BaseType->isPointerType() || BaseType->isObjCObjectPointerType())) |
Douglas Gregor | e4f764f | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 6731 | BaseType = BaseType->getPointeeType(); |
| 6732 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6733 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6734 | // Objective-C properties allow "." access on Objective-C pointer types, |
| 6735 | // so adjust the base type to the object type itself. |
| 6736 | if (BaseType->isObjCObjectPointerType()) |
| 6737 | BaseType = BaseType->getPointeeType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6738 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6739 | // C++ [basic.lookup.classref]p2: |
| 6740 | // [...] If the type of the object expression is of pointer to scalar |
| 6741 | // type, the unqualified-id is looked up in the context of the complete |
| 6742 | // postfix-expression. |
| 6743 | // |
| 6744 | // This also indicates that we could be parsing a pseudo-destructor-name. |
| 6745 | // Note that Objective-C class and object types can be pseudo-destructor |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6746 | // expressions or normal member (ivar or property) access expressions, and |
| 6747 | // it's legal for the type to be incomplete if this is a pseudo-destructor |
| 6748 | // call. We'll do more incomplete-type checks later in the lookup process, |
| 6749 | // so just skip this check for ObjC types. |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6750 | if (BaseType->isObjCObjectOrInterfaceType()) { |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6751 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6752 | MayBePseudoDestructor = true; |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6753 | return Base; |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6754 | } else if (!BaseType->isRecordType()) { |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 6755 | ObjectType = nullptr; |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6756 | MayBePseudoDestructor = true; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6757 | return Base; |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 6758 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6759 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6760 | // The object type must be complete (or dependent), or |
| 6761 | // C++11 [expr.prim.general]p3: |
| 6762 | // Unlike the object expression in other contexts, *this is not required to |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6763 | // be of complete type for purposes of class member access (5.2.5) outside |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6764 | // the member function body. |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 6765 | if (!BaseType->isDependentType() && |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6766 | !isThisOutsideMemberFunctionBody(BaseType) && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 6767 | RequireCompleteType(OpLoc, BaseType, diag::err_incomplete_member_access)) |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 6768 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6769 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 6770 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6771 | // If the id-expression in a class member access (5.2.5) is an |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 6772 | // unqualified-id, and the type of the object expression is of a class |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 6773 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 6774 | // up in the scope of class C. [...] |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6775 | ObjectType = ParsedType::make(BaseType); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6776 | return Base; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6777 | } |
| 6778 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6779 | static bool CheckArrow(Sema& S, QualType& ObjectType, Expr *&Base, |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6780 | tok::TokenKind& OpKind, SourceLocation OpLoc) { |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6781 | if (Base->hasPlaceholderType()) { |
| 6782 | ExprResult result = S.CheckPlaceholderExpr(Base); |
| 6783 | if (result.isInvalid()) return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6784 | Base = result.get(); |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6785 | } |
| 6786 | ObjectType = Base->getType(); |
| 6787 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6788 | // C++ [expr.pseudo]p2: |
| 6789 | // The left-hand side of the dot operator shall be of scalar type. The |
| 6790 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 6791 | // This scalar type is the object type. |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6792 | // Note that this is rather different from the normal handling for the |
| 6793 | // arrow operator. |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6794 | if (OpKind == tok::arrow) { |
| 6795 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 6796 | ObjectType = Ptr->getPointeeType(); |
| 6797 | } else if (!Base->isTypeDependent()) { |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 6798 | // The user wrote "p->" when they probably meant "p."; fix it. |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6799 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 6800 | << ObjectType << true |
| 6801 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 6802 | if (S.isSFINAEContext()) |
| 6803 | return true; |
| 6804 | |
| 6805 | OpKind = tok::period; |
| 6806 | } |
| 6807 | } |
| 6808 | |
| 6809 | return false; |
| 6810 | } |
| 6811 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6812 | /// Check if it's ok to try and recover dot pseudo destructor calls on |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6813 | /// pointer objects. |
| 6814 | static bool |
| 6815 | canRecoverDotPseudoDestructorCallsOnPointerObjects(Sema &SemaRef, |
| 6816 | QualType DestructedType) { |
| 6817 | // If this is a record type, check if its destructor is callable. |
| 6818 | if (auto *RD = DestructedType->getAsCXXRecordDecl()) { |
| 6819 | if (CXXDestructorDecl *D = SemaRef.LookupDestructor(RD)) |
| 6820 | return SemaRef.CanUseDecl(D, /*TreatUnavailableAsInvalid=*/false); |
| 6821 | return false; |
| 6822 | } |
| 6823 | |
| 6824 | // Otherwise, check if it's a type for which it's valid to use a pseudo-dtor. |
| 6825 | return DestructedType->isDependentType() || DestructedType->isScalarType() || |
| 6826 | DestructedType->isVectorType(); |
| 6827 | } |
| 6828 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6829 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6830 | SourceLocation OpLoc, |
| 6831 | tok::TokenKind OpKind, |
| 6832 | const CXXScopeSpec &SS, |
| 6833 | TypeSourceInfo *ScopeTypeInfo, |
| 6834 | SourceLocation CCLoc, |
| 6835 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6836 | PseudoDestructorTypeStorage Destructed) { |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6837 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6838 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6839 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6840 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6841 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6842 | |
Douglas Gregor | c5c5734 | 2012-09-10 14:57:06 +0000 | [diff] [blame] | 6843 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType() && |
| 6844 | !ObjectType->isVectorType()) { |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 6845 | if (getLangOpts().MSVCCompat && ObjectType->isVoidType()) |
Nico Weber | 4bc6499 | 2012-01-23 06:08:16 +0000 | [diff] [blame] | 6846 | Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange(); |
Reid Kleckner | e502507 | 2014-05-01 16:50:23 +0000 | [diff] [blame] | 6847 | else { |
Nico Weber | 5882927 | 2012-01-23 05:50:57 +0000 | [diff] [blame] | 6848 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 6849 | << ObjectType << Base->getSourceRange(); |
Reid Kleckner | e502507 | 2014-05-01 16:50:23 +0000 | [diff] [blame] | 6850 | return ExprError(); |
| 6851 | } |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
| 6854 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6855 | // [...] The cv-unqualified versions of the object type and of the type |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6856 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6857 | if (DestructedTypeInfo) { |
| 6858 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 6859 | SourceLocation DestructedTypeStart |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6860 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6861 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
| 6862 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6863 | // Detect dot pseudo destructor calls on pointer objects, e.g.: |
| 6864 | // Foo *foo; |
| 6865 | // foo.~Foo(); |
| 6866 | if (OpKind == tok::period && ObjectType->isPointerType() && |
| 6867 | Context.hasSameUnqualifiedType(DestructedType, |
| 6868 | ObjectType->getPointeeType())) { |
| 6869 | auto Diagnostic = |
| 6870 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 6871 | << ObjectType << /*IsArrow=*/0 << Base->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6872 | |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6873 | // Issue a fixit only when the destructor is valid. |
| 6874 | if (canRecoverDotPseudoDestructorCallsOnPointerObjects( |
| 6875 | *this, DestructedType)) |
| 6876 | Diagnostic << FixItHint::CreateReplacement(OpLoc, "->"); |
| 6877 | |
| 6878 | // Recover by setting the object type to the destructed type and the |
| 6879 | // operator to '->'. |
| 6880 | ObjectType = DestructedType; |
| 6881 | OpKind = tok::arrow; |
| 6882 | } else { |
| 6883 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
| 6884 | << ObjectType << DestructedType << Base->getSourceRange() |
| 6885 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 6886 | |
| 6887 | // Recover by setting the destructed type to the object type. |
| 6888 | DestructedType = ObjectType; |
| 6889 | DestructedTypeInfo = |
| 6890 | Context.getTrivialTypeSourceInfo(ObjectType, DestructedTypeStart); |
| 6891 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6892 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6893 | } else if (DestructedType.getObjCLifetime() != |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6894 | ObjectType.getObjCLifetime()) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6895 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6896 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
| 6897 | // Okay: just pretend that the user provided the correctly-qualified |
| 6898 | // type. |
| 6899 | } else { |
| 6900 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
| 6901 | << ObjectType << DestructedType << Base->getSourceRange() |
| 6902 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 6903 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6904 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6905 | // Recover by setting the destructed type to the object type. |
| 6906 | DestructedType = ObjectType; |
| 6907 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 6908 | DestructedTypeStart); |
| 6909 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6910 | } |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6911 | } |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6912 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6913 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6914 | // C++ [expr.pseudo]p2: |
| 6915 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 6916 | // form |
| 6917 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6918 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6919 | // |
| 6920 | // shall designate the same scalar type. |
| 6921 | if (ScopeTypeInfo) { |
| 6922 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 6923 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | b86a6b8 | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 6924 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6925 | |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6926 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6927 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6928 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6929 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6930 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6931 | ScopeType = QualType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6932 | ScopeTypeInfo = nullptr; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6933 | } |
| 6934 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6935 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6936 | Expr *Result |
| 6937 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 6938 | OpKind == tok::arrow, OpLoc, |
Douglas Gregor | a6ce608 | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 6939 | SS.getWithLocInContext(Context), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6940 | ScopeTypeInfo, |
| 6941 | CCLoc, |
| 6942 | TildeLoc, |
| 6943 | Destructed); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6944 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6945 | return Result; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6946 | } |
| 6947 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6948 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6949 | SourceLocation OpLoc, |
| 6950 | tok::TokenKind OpKind, |
| 6951 | CXXScopeSpec &SS, |
| 6952 | UnqualifiedId &FirstTypeName, |
| 6953 | SourceLocation CCLoc, |
| 6954 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6955 | UnqualifiedId &SecondTypeName) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 6956 | assert((FirstTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
| 6957 | FirstTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) && |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6958 | "Invalid first type name in pseudo-destructor"); |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 6959 | assert((SecondTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
| 6960 | SecondTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) && |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6961 | "Invalid second type name in pseudo-destructor"); |
| 6962 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6963 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6964 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6965 | return ExprError(); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6966 | |
| 6967 | // Compute the object type that we should use for name lookup purposes. Only |
| 6968 | // record types and dependent types matter. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6969 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6970 | if (!SS.isSet()) { |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6971 | if (ObjectType->isRecordType()) |
| 6972 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6973 | else if (ObjectType->isDependentType()) |
| 6974 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6975 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6976 | |
| 6977 | // Convert the name of the type being destructed (following the ~) into a |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6978 | // type (with source-location information). |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6979 | QualType DestructedType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6980 | TypeSourceInfo *DestructedTypeInfo = nullptr; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6981 | PseudoDestructorTypeStorage Destructed; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 6982 | if (SecondTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6983 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6984 | SecondTypeName.StartLocation, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6985 | S, &SS, true, false, ObjectTypePtrForLookup, |
| 6986 | /*IsCtorOrDtorName*/true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6987 | if (!T && |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6988 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 6989 | (!SS.isSet() && ObjectType->isDependentType()))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6990 | // The name of the type being destroyed is a dependent name, and we |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6991 | // couldn't find anything useful in scope. Just store the identifier and |
| 6992 | // it's location, and we'll perform (qualified) name lookup again at |
| 6993 | // template instantiation time. |
| 6994 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 6995 | SecondTypeName.StartLocation); |
| 6996 | } else if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6997 | Diag(SecondTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6998 | diag::err_pseudo_dtor_destructor_non_type) |
| 6999 | << SecondTypeName.Identifier << ObjectType; |
| 7000 | if (isSFINAEContext()) |
| 7001 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7002 | |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7003 | // Recover by assuming we had the right type all along. |
| 7004 | DestructedType = ObjectType; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7005 | } else |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7006 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7007 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7008 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7009 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7010 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7011 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7012 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7013 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7014 | TemplateId->Template, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 7015 | TemplateId->Name, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7016 | TemplateId->TemplateNameLoc, |
| 7017 | TemplateId->LAngleLoc, |
| 7018 | TemplateArgsPtr, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 7019 | TemplateId->RAngleLoc, |
| 7020 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7021 | if (T.isInvalid() || !T.get()) { |
| 7022 | // Recover by assuming we had the right type all along. |
| 7023 | DestructedType = ObjectType; |
| 7024 | } else |
| 7025 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7026 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7027 | |
| 7028 | // If we've performed some kind of recovery, (re-)build the type source |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7029 | // information. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 7030 | if (!DestructedType.isNull()) { |
| 7031 | if (!DestructedTypeInfo) |
| 7032 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7033 | SecondTypeName.StartLocation); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 7034 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 7035 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7036 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7037 | // Convert the name of the scope type (the type prior to '::') into a type. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7038 | TypeSourceInfo *ScopeTypeInfo = nullptr; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7039 | QualType ScopeType; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 7040 | if (FirstTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7041 | FirstTypeName.Identifier) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 7042 | if (FirstTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7043 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7044 | FirstTypeName.StartLocation, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 7045 | S, &SS, true, false, ObjectTypePtrForLookup, |
| 7046 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7047 | if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7048 | Diag(FirstTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7049 | diag::err_pseudo_dtor_destructor_non_type) |
| 7050 | << FirstTypeName.Identifier << ObjectType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7051 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7052 | if (isSFINAEContext()) |
| 7053 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7054 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7055 | // Just drop this type. It's unnecessary anyway. |
| 7056 | ScopeType = QualType(); |
| 7057 | } else |
| 7058 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7059 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7060 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7061 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7062 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7063 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7064 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7065 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7066 | TemplateId->Template, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 7067 | TemplateId->Name, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7068 | TemplateId->TemplateNameLoc, |
| 7069 | TemplateId->LAngleLoc, |
| 7070 | TemplateArgsPtr, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 7071 | TemplateId->RAngleLoc, |
| 7072 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 7073 | if (T.isInvalid() || !T.get()) { |
| 7074 | // Recover by dropping this type. |
| 7075 | ScopeType = QualType(); |
| 7076 | } else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7077 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 7078 | } |
| 7079 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7080 | |
Douglas Gregor | 90ad922 | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 7081 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 7082 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 7083 | FirstTypeName.StartLocation); |
| 7084 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7085 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7086 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | cdbd515 | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 7087 | ScopeTypeInfo, CCLoc, TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 7088 | Destructed); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 7089 | } |
| 7090 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 7091 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
| 7092 | SourceLocation OpLoc, |
| 7093 | tok::TokenKind OpKind, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7094 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 7095 | const DeclSpec& DS) { |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 7096 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 7097 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 7098 | return ExprError(); |
| 7099 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 7100 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc(), |
| 7101 | false); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 7102 | |
| 7103 | TypeLocBuilder TLB; |
| 7104 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
| 7105 | DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 7106 | TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T); |
| 7107 | PseudoDestructorTypeStorage Destructed(DestructedTypeInfo); |
| 7108 | |
| 7109 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, CXXScopeSpec(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7110 | nullptr, SourceLocation(), TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 7111 | Destructed); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 7112 | } |
| 7113 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7114 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 7115 | CXXConversionDecl *Method, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7116 | bool HadMultipleCandidates) { |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 7117 | if (Method->getParent()->isLambda() && |
| 7118 | Method->getConversionType()->isBlockPointerType()) { |
| 7119 | // This is a lambda coversion to block pointer; check if the argument |
| 7120 | // is a LambdaExpr. |
| 7121 | Expr *SubE = E; |
| 7122 | CastExpr *CE = dyn_cast<CastExpr>(SubE); |
| 7123 | if (CE && CE->getCastKind() == CK_NoOp) |
| 7124 | SubE = CE->getSubExpr(); |
| 7125 | SubE = SubE->IgnoreParens(); |
| 7126 | if (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(SubE)) |
| 7127 | SubE = BE->getSubExpr(); |
| 7128 | if (isa<LambdaExpr>(SubE)) { |
| 7129 | // For the conversion to block pointer on a lambda expression, we |
| 7130 | // construct a special BlockLiteral instead; this doesn't really make |
| 7131 | // a difference in ARC, but outside of ARC the resulting block literal |
| 7132 | // follows the normal lifetime rules for block literals instead of being |
| 7133 | // autoreleased. |
| 7134 | DiagnosticErrorTrap Trap(Diags); |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 7135 | PushExpressionEvaluationContext( |
| 7136 | ExpressionEvaluationContext::PotentiallyEvaluated); |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 7137 | ExprResult Exp = BuildBlockForLambdaConversion(E->getExprLoc(), |
| 7138 | E->getExprLoc(), |
| 7139 | Method, E); |
Akira Hatanaka | c482acd | 2016-05-04 18:07:20 +0000 | [diff] [blame] | 7140 | PopExpressionEvaluationContext(); |
| 7141 | |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 7142 | if (Exp.isInvalid()) |
| 7143 | Diag(E->getExprLoc(), diag::note_lambda_to_block_conv); |
| 7144 | return Exp; |
| 7145 | } |
| 7146 | } |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 7147 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7148 | ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/nullptr, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7149 | FoundDecl, Method); |
| 7150 | if (Exp.isInvalid()) |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 7151 | return true; |
Eli Friedman | f719553 | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 7152 | |
Aaron Ballman | f4cb2be | 2015-03-24 15:07:53 +0000 | [diff] [blame] | 7153 | MemberExpr *ME = new (Context) MemberExpr( |
| 7154 | Exp.get(), /*IsArrow=*/false, SourceLocation(), Method, SourceLocation(), |
| 7155 | Context.BoundMemberTy, VK_RValue, OK_Ordinary); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7156 | if (HadMultipleCandidates) |
| 7157 | ME->setHadMultipleCandidates(true); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 7158 | MarkMemberReferenced(ME); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7159 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7160 | QualType ResultType = Method->getReturnType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7161 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 7162 | ResultType = ResultType.getNonLValueExprType(Context); |
| 7163 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 7164 | CXXMemberCallExpr *CE = |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 7165 | new (Context) CXXMemberCallExpr(Context, ME, None, ResultType, VK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7166 | Exp.get()->getLocEnd()); |
George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 7167 | |
| 7168 | if (CheckFunctionCall(Method, CE, |
| 7169 | Method->getType()->castAs<FunctionProtoType>())) |
| 7170 | return ExprError(); |
| 7171 | |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7172 | return CE; |
| 7173 | } |
| 7174 | |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 7175 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 7176 | SourceLocation RParen) { |
Aaron Ballman | edc8084 | 2015-04-27 22:31:12 +0000 | [diff] [blame] | 7177 | // If the operand is an unresolved lookup expression, the expression is ill- |
| 7178 | // formed per [over.over]p1, because overloaded function names cannot be used |
| 7179 | // without arguments except in explicit contexts. |
| 7180 | ExprResult R = CheckPlaceholderExpr(Operand); |
| 7181 | if (R.isInvalid()) |
| 7182 | return R; |
| 7183 | |
| 7184 | // The operand may have been modified when checking the placeholder type. |
| 7185 | Operand = R.get(); |
| 7186 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 7187 | if (!inTemplateInstantiation() && Operand->HasSideEffects(Context, false)) { |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 7188 | // The expression operand for noexcept is in an unevaluated expression |
| 7189 | // context, so side effects could result in unintended consequences. |
| 7190 | Diag(Operand->getExprLoc(), diag::warn_side_effects_unevaluated_context); |
| 7191 | } |
| 7192 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7193 | CanThrowResult CanThrow = canThrow(Operand); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7194 | return new (Context) |
| 7195 | CXXNoexceptExpr(Context.BoolTy, Operand, CanThrow, KeyLoc, RParen); |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 7196 | } |
| 7197 | |
| 7198 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 7199 | Expr *Operand, SourceLocation RParen) { |
| 7200 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 22e3a93 | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 7201 | } |
| 7202 | |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7203 | static bool IsSpecialDiscardedValue(Expr *E) { |
| 7204 | // In C++11, discarded-value expressions of a certain form are special, |
| 7205 | // according to [expr]p10: |
| 7206 | // The lvalue-to-rvalue conversion (4.1) is applied only if the |
| 7207 | // expression is an lvalue of volatile-qualified type and it has |
| 7208 | // one of the following forms: |
| 7209 | E = E->IgnoreParens(); |
| 7210 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7211 | // - id-expression (5.1.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7212 | if (isa<DeclRefExpr>(E)) |
| 7213 | return true; |
| 7214 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7215 | // - subscripting (5.2.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7216 | if (isa<ArraySubscriptExpr>(E)) |
| 7217 | return true; |
| 7218 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7219 | // - class member access (5.2.5), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7220 | if (isa<MemberExpr>(E)) |
| 7221 | return true; |
| 7222 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7223 | // - indirection (5.3.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7224 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 7225 | if (UO->getOpcode() == UO_Deref) |
| 7226 | return true; |
| 7227 | |
| 7228 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7229 | // - pointer-to-member operation (5.5), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7230 | if (BO->isPtrMemOp()) |
| 7231 | return true; |
| 7232 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7233 | // - comma expression (5.18) where the right operand is one of the above. |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7234 | if (BO->getOpcode() == BO_Comma) |
| 7235 | return IsSpecialDiscardedValue(BO->getRHS()); |
| 7236 | } |
| 7237 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7238 | // - conditional expression (5.16) where both the second and the third |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7239 | // operands are one of the above, or |
| 7240 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) |
| 7241 | return IsSpecialDiscardedValue(CO->getTrueExpr()) && |
| 7242 | IsSpecialDiscardedValue(CO->getFalseExpr()); |
| 7243 | // The related edge case of "*x ?: *x". |
| 7244 | if (BinaryConditionalOperator *BCO = |
| 7245 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 7246 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(BCO->getTrueExpr())) |
| 7247 | return IsSpecialDiscardedValue(OVE->getSourceExpr()) && |
| 7248 | IsSpecialDiscardedValue(BCO->getFalseExpr()); |
| 7249 | } |
| 7250 | |
| 7251 | // Objective-C++ extensions to the rule. |
| 7252 | if (isa<PseudoObjectExpr>(E) || isa<ObjCIvarRefExpr>(E)) |
| 7253 | return true; |
| 7254 | |
| 7255 | return false; |
| 7256 | } |
| 7257 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7258 | /// Perform the conversions required for an expression used in a |
| 7259 | /// context that ignores the result. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7260 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7261 | if (E->hasPlaceholderType()) { |
| 7262 | ExprResult result = CheckPlaceholderExpr(E); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7263 | if (result.isInvalid()) return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7264 | E = result.get(); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7265 | } |
| 7266 | |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 7267 | // C99 6.3.2.1: |
| 7268 | // [Except in specific positions,] an lvalue that does not have |
| 7269 | // array type is converted to the value stored in the |
| 7270 | // designated object (and is no longer an lvalue). |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 7271 | if (E->isRValue()) { |
| 7272 | // In C, function designators (i.e. expressions of function type) |
| 7273 | // are r-values, but we still want to do function-to-pointer decay |
| 7274 | // on them. This is both technically correct and convenient for |
| 7275 | // some clients. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7276 | if (!getLangOpts().CPlusPlus && E->getType()->isFunctionType()) |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 7277 | return DefaultFunctionArrayConversion(E); |
| 7278 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7279 | return E; |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 7280 | } |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 7281 | |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7282 | if (getLangOpts().CPlusPlus) { |
| 7283 | // The C++11 standard defines the notion of a discarded-value expression; |
| 7284 | // normally, we don't need to do anything to handle it, but if it is a |
| 7285 | // volatile lvalue with a special form, we perform an lvalue-to-rvalue |
| 7286 | // conversion. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7287 | if (getLangOpts().CPlusPlus11 && E->isGLValue() && |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7288 | E->getType().isVolatileQualified() && |
| 7289 | IsSpecialDiscardedValue(E)) { |
| 7290 | ExprResult Res = DefaultLvalueConversion(E); |
| 7291 | if (Res.isInvalid()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7292 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7293 | E = Res.get(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7294 | } |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 7295 | |
| 7296 | // C++1z: |
| 7297 | // If the expression is a prvalue after this optional conversion, the |
| 7298 | // temporary materialization conversion is applied. |
| 7299 | // |
| 7300 | // We skip this step: IR generation is able to synthesize the storage for |
| 7301 | // itself in the aggregate case, and adding the extra node to the AST is |
| 7302 | // just clutter. |
| 7303 | // FIXME: We don't emit lifetime markers for the temporaries due to this. |
| 7304 | // FIXME: Do any other AST consumers care about this? |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7305 | return E; |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7306 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7307 | |
| 7308 | // GCC seems to also exclude expressions of incomplete enum type. |
| 7309 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
| 7310 | if (!T->getDecl()->isComplete()) { |
| 7311 | // FIXME: stupid workaround for a codegen bug! |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7312 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).get(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7313 | return E; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7314 | } |
| 7315 | } |
| 7316 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7317 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 7318 | if (Res.isInvalid()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7319 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7320 | E = Res.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7321 | |
John McCall | ca61b65 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 7322 | if (!E->getType()->isVoidType()) |
| 7323 | RequireCompleteType(E->getExprLoc(), E->getType(), |
| 7324 | diag::err_incomplete_type); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7325 | return E; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7326 | } |
| 7327 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7328 | // If we can unambiguously determine whether Var can never be used |
| 7329 | // in a constant expression, return true. |
| 7330 | // - if the variable and its initializer are non-dependent, then |
| 7331 | // we can unambiguously check if the variable is a constant expression. |
| 7332 | // - if the initializer is not value dependent - we can determine whether |
| 7333 | // it can be used to initialize a constant expression. If Init can not |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7334 | // be used to initialize a constant expression we conclude that Var can |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7335 | // never be a constant expression. |
| 7336 | // - FXIME: if the initializer is dependent, we can still do some analysis and |
| 7337 | // identify certain cases unambiguously as non-const by using a Visitor: |
| 7338 | // - such as those that involve odr-use of a ParmVarDecl, involve a new |
| 7339 | // delete, lambda-expr, dynamic-cast, reinterpret-cast etc... |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7340 | static inline bool VariableCanNeverBeAConstantExpression(VarDecl *Var, |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7341 | ASTContext &Context) { |
| 7342 | if (isa<ParmVarDecl>(Var)) return true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7343 | const VarDecl *DefVD = nullptr; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7344 | |
| 7345 | // If there is no initializer - this can not be a constant expression. |
| 7346 | if (!Var->getAnyInitializer(DefVD)) return true; |
| 7347 | assert(DefVD); |
| 7348 | if (DefVD->isWeak()) return false; |
| 7349 | EvaluatedStmt *Eval = DefVD->ensureEvaluatedStmt(); |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 7350 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7351 | Expr *Init = cast<Expr>(Eval->Value); |
| 7352 | |
| 7353 | if (Var->getType()->isDependentType() || Init->isValueDependent()) { |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 7354 | // FIXME: Teach the constant evaluator to deal with the non-dependent parts |
| 7355 | // of value-dependent expressions, and use it here to determine whether the |
| 7356 | // initializer is a potential constant expression. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7357 | return false; |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 7358 | } |
| 7359 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7360 | return !IsVariableAConstantExpression(Var, Context); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7361 | } |
| 7362 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7363 | /// Check if the current lambda has any potential captures |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7364 | /// that must be captured by any of its enclosing lambdas that are ready to |
| 7365 | /// capture. If there is a lambda that can capture a nested |
| 7366 | /// potential-capture, go ahead and do so. Also, check to see if any |
| 7367 | /// variables are uncaptureable or do not involve an odr-use so do not |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7368 | /// need to be captured. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7369 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7370 | static void CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures( |
| 7371 | Expr *const FE, LambdaScopeInfo *const CurrentLSI, Sema &S) { |
| 7372 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7373 | assert(!S.isUnevaluatedContext()); |
| 7374 | assert(S.CurContext->isDependentContext()); |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7375 | #ifndef NDEBUG |
| 7376 | DeclContext *DC = S.CurContext; |
| 7377 | while (DC && isa<CapturedDecl>(DC)) |
| 7378 | DC = DC->getParent(); |
| 7379 | assert( |
| 7380 | CurrentLSI->CallOperator == DC && |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7381 | "The current call operator must be synchronized with Sema's CurContext"); |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7382 | #endif // NDEBUG |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7383 | |
| 7384 | const bool IsFullExprInstantiationDependent = FE->isInstantiationDependent(); |
| 7385 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7386 | // All the potentially captureable variables in the current nested |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7387 | // lambda (within a generic outer lambda), must be captured by an |
| 7388 | // outer lambda that is enclosed within a non-dependent context. |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7389 | const unsigned NumPotentialCaptures = |
| 7390 | CurrentLSI->getNumPotentialVariableCaptures(); |
| 7391 | for (unsigned I = 0; I != NumPotentialCaptures; ++I) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7392 | Expr *VarExpr = nullptr; |
| 7393 | VarDecl *Var = nullptr; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7394 | CurrentLSI->getPotentialVariableCapture(I, Var, VarExpr); |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7395 | // If the variable is clearly identified as non-odr-used and the full |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7396 | // expression is not instantiation dependent, only then do we not |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7397 | // need to check enclosing lambda's for speculative captures. |
| 7398 | // For e.g.: |
| 7399 | // Even though 'x' is not odr-used, it should be captured. |
| 7400 | // int test() { |
| 7401 | // const int x = 10; |
| 7402 | // auto L = [=](auto a) { |
| 7403 | // (void) +x + a; |
| 7404 | // }; |
| 7405 | // } |
| 7406 | if (CurrentLSI->isVariableExprMarkedAsNonODRUsed(VarExpr) && |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7407 | !IsFullExprInstantiationDependent) |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7408 | continue; |
| 7409 | |
| 7410 | // If we have a capture-capable lambda for the variable, go ahead and |
| 7411 | // capture the variable in that lambda (and all its enclosing lambdas). |
| 7412 | if (const Optional<unsigned> Index = |
| 7413 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 7414 | S.FunctionScopes, Var, S)) { |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7415 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
| 7416 | MarkVarDeclODRUsed(Var, VarExpr->getExprLoc(), S, |
| 7417 | &FunctionScopeIndexOfCapturableLambda); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7418 | } |
| 7419 | const bool IsVarNeverAConstantExpression = |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7420 | VariableCanNeverBeAConstantExpression(Var, S.Context); |
| 7421 | if (!IsFullExprInstantiationDependent || IsVarNeverAConstantExpression) { |
| 7422 | // This full expression is not instantiation dependent or the variable |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7423 | // can not be used in a constant expression - which means |
| 7424 | // this variable must be odr-used here, so diagnose a |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7425 | // capture violation early, if the variable is un-captureable. |
| 7426 | // This is purely for diagnosing errors early. Otherwise, this |
| 7427 | // error would get diagnosed when the lambda becomes capture ready. |
| 7428 | QualType CaptureType, DeclRefType; |
| 7429 | SourceLocation ExprLoc = VarExpr->getExprLoc(); |
| 7430 | if (S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7431 | /*EllipsisLoc*/ SourceLocation(), |
| 7432 | /*BuildAndDiagnose*/false, CaptureType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7433 | DeclRefType, nullptr)) { |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7434 | // We will never be able to capture this variable, and we need |
| 7435 | // to be able to in any and all instantiations, so diagnose it. |
| 7436 | S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7437 | /*EllipsisLoc*/ SourceLocation(), |
| 7438 | /*BuildAndDiagnose*/true, CaptureType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7439 | DeclRefType, nullptr); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7440 | } |
| 7441 | } |
| 7442 | } |
| 7443 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7444 | // Check if 'this' needs to be captured. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7445 | if (CurrentLSI->hasPotentialThisCapture()) { |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7446 | // If we have a capture-capable lambda for 'this', go ahead and capture |
| 7447 | // 'this' in that lambda (and all its enclosing lambdas). |
| 7448 | if (const Optional<unsigned> Index = |
| 7449 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 7450 | S.FunctionScopes, /*0 is 'this'*/ nullptr, S)) { |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7451 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
| 7452 | S.CheckCXXThisCapture(CurrentLSI->PotentialThisCaptureLocation, |
| 7453 | /*Explicit*/ false, /*BuildAndDiagnose*/ true, |
| 7454 | &FunctionScopeIndexOfCapturableLambda); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7455 | } |
| 7456 | } |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7457 | |
| 7458 | // Reset all the potential captures at the end of each full-expression. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7459 | CurrentLSI->clearPotentialCaptures(); |
| 7460 | } |
| 7461 | |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7462 | static ExprResult attemptRecovery(Sema &SemaRef, |
| 7463 | const TypoCorrectionConsumer &Consumer, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 7464 | const TypoCorrection &TC) { |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7465 | LookupResult R(SemaRef, Consumer.getLookupResult().getLookupNameInfo(), |
| 7466 | Consumer.getLookupResult().getLookupKind()); |
| 7467 | const CXXScopeSpec *SS = Consumer.getSS(); |
| 7468 | CXXScopeSpec NewSS; |
| 7469 | |
| 7470 | // Use an approprate CXXScopeSpec for building the expr. |
| 7471 | if (auto *NNS = TC.getCorrectionSpecifier()) |
| 7472 | NewSS.MakeTrivial(SemaRef.Context, NNS, TC.getCorrectionRange()); |
| 7473 | else if (SS && !TC.WillReplaceSpecifier()) |
| 7474 | NewSS = *SS; |
| 7475 | |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7476 | if (auto *ND = TC.getFoundDecl()) { |
Nick Lewycky | 01ad4ae | 2014-12-13 02:54:28 +0000 | [diff] [blame] | 7477 | R.setLookupName(ND->getDeclName()); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7478 | R.addDecl(ND); |
| 7479 | if (ND->isCXXClassMember()) { |
Nick Lewycky | 01ad4ae | 2014-12-13 02:54:28 +0000 | [diff] [blame] | 7480 | // Figure out the correct naming class to add to the LookupResult. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7481 | CXXRecordDecl *Record = nullptr; |
| 7482 | if (auto *NNS = TC.getCorrectionSpecifier()) |
| 7483 | Record = NNS->getAsType()->getAsCXXRecordDecl(); |
| 7484 | if (!Record) |
Olivier Goffart | ed13fab | 2015-01-09 09:37:26 +0000 | [diff] [blame] | 7485 | Record = |
| 7486 | dyn_cast<CXXRecordDecl>(ND->getDeclContext()->getRedeclContext()); |
| 7487 | if (Record) |
| 7488 | R.setNamingClass(Record); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7489 | |
| 7490 | // Detect and handle the case where the decl might be an implicit |
| 7491 | // member. |
| 7492 | bool MightBeImplicitMember; |
| 7493 | if (!Consumer.isAddressOfOperand()) |
| 7494 | MightBeImplicitMember = true; |
| 7495 | else if (!NewSS.isEmpty()) |
| 7496 | MightBeImplicitMember = false; |
| 7497 | else if (R.isOverloadedResult()) |
| 7498 | MightBeImplicitMember = false; |
| 7499 | else if (R.isUnresolvableResult()) |
| 7500 | MightBeImplicitMember = true; |
| 7501 | else |
| 7502 | MightBeImplicitMember = isa<FieldDecl>(ND) || |
| 7503 | isa<IndirectFieldDecl>(ND) || |
| 7504 | isa<MSPropertyDecl>(ND); |
| 7505 | |
| 7506 | if (MightBeImplicitMember) |
| 7507 | return SemaRef.BuildPossibleImplicitMemberExpr( |
| 7508 | NewSS, /*TemplateKWLoc*/ SourceLocation(), R, |
Aaron Ballman | 6924dcd | 2015-09-01 14:49:24 +0000 | [diff] [blame] | 7509 | /*TemplateArgs*/ nullptr, /*S*/ nullptr); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7510 | } else if (auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
| 7511 | return SemaRef.LookupInObjCMethod(R, Consumer.getScope(), |
| 7512 | Ivar->getIdentifier()); |
| 7513 | } |
| 7514 | } |
| 7515 | |
Kaelyn Takata | 6f71ce2 | 2014-11-20 22:06:33 +0000 | [diff] [blame] | 7516 | return SemaRef.BuildDeclarationNameExpr(NewSS, R, /*NeedsADL*/ false, |
| 7517 | /*AcceptInvalidDecl*/ true); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7518 | } |
| 7519 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7520 | namespace { |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7521 | class FindTypoExprs : public RecursiveASTVisitor<FindTypoExprs> { |
| 7522 | llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs; |
| 7523 | |
| 7524 | public: |
| 7525 | explicit FindTypoExprs(llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs) |
| 7526 | : TypoExprs(TypoExprs) {} |
| 7527 | bool VisitTypoExpr(TypoExpr *TE) { |
| 7528 | TypoExprs.insert(TE); |
| 7529 | return true; |
| 7530 | } |
| 7531 | }; |
| 7532 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7533 | class TransformTypos : public TreeTransform<TransformTypos> { |
| 7534 | typedef TreeTransform<TransformTypos> BaseTransform; |
| 7535 | |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7536 | VarDecl *InitDecl; // A decl to avoid as a correction because it is in the |
| 7537 | // process of being initialized. |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7538 | llvm::function_ref<ExprResult(Expr *)> ExprFilter; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7539 | llvm::SmallSetVector<TypoExpr *, 2> TypoExprs, AmbiguousTypoExprs; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7540 | llvm::SmallDenseMap<TypoExpr *, ExprResult, 2> TransformCache; |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7541 | llvm::SmallDenseMap<OverloadExpr *, Expr *, 4> OverloadResolution; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7542 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7543 | /// Emit diagnostics for all of the TypoExprs encountered. |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7544 | /// If the TypoExprs were successfully corrected, then the diagnostics should |
| 7545 | /// suggest the corrections. Otherwise the diagnostics will not suggest |
| 7546 | /// anything (having been passed an empty TypoCorrection). |
| 7547 | void EmitAllDiagnostics() { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 7548 | for (TypoExpr *TE : TypoExprs) { |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7549 | auto &State = SemaRef.getTypoExprState(TE); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7550 | if (State.DiagHandler) { |
| 7551 | TypoCorrection TC = State.Consumer->getCurrentCorrection(); |
| 7552 | ExprResult Replacement = TransformCache[TE]; |
| 7553 | |
| 7554 | // Extract the NamedDecl from the transformed TypoExpr and add it to the |
| 7555 | // TypoCorrection, replacing the existing decls. This ensures the right |
| 7556 | // NamedDecl is used in diagnostics e.g. in the case where overload |
| 7557 | // resolution was used to select one from several possible decls that |
| 7558 | // had been stored in the TypoCorrection. |
| 7559 | if (auto *ND = getDeclFromExpr( |
| 7560 | Replacement.isInvalid() ? nullptr : Replacement.get())) |
| 7561 | TC.setCorrectionDecl(ND); |
| 7562 | |
| 7563 | State.DiagHandler(TC); |
| 7564 | } |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7565 | SemaRef.clearDelayedTypo(TE); |
| 7566 | } |
| 7567 | } |
| 7568 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7569 | /// If corrections for the first TypoExpr have been exhausted for a |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7570 | /// given combination of the other TypoExprs, retry those corrections against |
| 7571 | /// the next combination of substitutions for the other TypoExprs by advancing |
| 7572 | /// to the next potential correction of the second TypoExpr. For the second |
| 7573 | /// and subsequent TypoExprs, if its stream of corrections has been exhausted, |
| 7574 | /// the stream is reset and the next TypoExpr's stream is advanced by one (a |
| 7575 | /// TypoExpr's correction stream is advanced by removing the TypoExpr from the |
| 7576 | /// TransformCache). Returns true if there is still any untried combinations |
| 7577 | /// of corrections. |
| 7578 | bool CheckAndAdvanceTypoExprCorrectionStreams() { |
| 7579 | for (auto TE : TypoExprs) { |
| 7580 | auto &State = SemaRef.getTypoExprState(TE); |
| 7581 | TransformCache.erase(TE); |
| 7582 | if (!State.Consumer->finished()) |
| 7583 | return true; |
| 7584 | State.Consumer->resetCorrectionStream(); |
| 7585 | } |
| 7586 | return false; |
| 7587 | } |
| 7588 | |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7589 | NamedDecl *getDeclFromExpr(Expr *E) { |
| 7590 | if (auto *OE = dyn_cast_or_null<OverloadExpr>(E)) |
| 7591 | E = OverloadResolution[OE]; |
| 7592 | |
| 7593 | if (!E) |
| 7594 | return nullptr; |
| 7595 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7596 | return DRE->getFoundDecl(); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7597 | if (auto *ME = dyn_cast<MemberExpr>(E)) |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7598 | return ME->getFoundDecl(); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7599 | // FIXME: Add any other expr types that could be be seen by the delayed typo |
| 7600 | // correction TreeTransform for which the corresponding TypoCorrection could |
Nick Lewycky | 39f9dbc | 2014-12-16 21:48:39 +0000 | [diff] [blame] | 7601 | // contain multiple decls. |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7602 | return nullptr; |
| 7603 | } |
| 7604 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7605 | ExprResult TryTransform(Expr *E) { |
| 7606 | Sema::SFINAETrap Trap(SemaRef); |
| 7607 | ExprResult Res = TransformExpr(E); |
| 7608 | if (Trap.hasErrorOccurred() || Res.isInvalid()) |
| 7609 | return ExprError(); |
| 7610 | |
| 7611 | return ExprFilter(Res.get()); |
| 7612 | } |
| 7613 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7614 | public: |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7615 | TransformTypos(Sema &SemaRef, VarDecl *InitDecl, llvm::function_ref<ExprResult(Expr *)> Filter) |
| 7616 | : BaseTransform(SemaRef), InitDecl(InitDecl), ExprFilter(Filter) {} |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7617 | |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7618 | ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc, |
| 7619 | MultiExprArg Args, |
| 7620 | SourceLocation RParenLoc, |
| 7621 | Expr *ExecConfig = nullptr) { |
| 7622 | auto Result = BaseTransform::RebuildCallExpr(Callee, LParenLoc, Args, |
| 7623 | RParenLoc, ExecConfig); |
| 7624 | if (auto *OE = dyn_cast<OverloadExpr>(Callee)) { |
Reid Kleckner | a9e65ba | 2014-12-13 01:11:23 +0000 | [diff] [blame] | 7625 | if (Result.isUsable()) { |
Reid Kleckner | a7fe33e | 2014-12-13 00:53:10 +0000 | [diff] [blame] | 7626 | Expr *ResultCall = Result.get(); |
| 7627 | if (auto *BE = dyn_cast<CXXBindTemporaryExpr>(ResultCall)) |
| 7628 | ResultCall = BE->getSubExpr(); |
| 7629 | if (auto *CE = dyn_cast<CallExpr>(ResultCall)) |
| 7630 | OverloadResolution[OE] = CE->getCallee(); |
| 7631 | } |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7632 | } |
| 7633 | return Result; |
| 7634 | } |
| 7635 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7636 | ExprResult TransformLambdaExpr(LambdaExpr *E) { return Owned(E); } |
| 7637 | |
Saleem Abdulrasool | a174241 | 2015-10-31 00:39:15 +0000 | [diff] [blame] | 7638 | ExprResult TransformBlockExpr(BlockExpr *E) { return Owned(E); } |
| 7639 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7640 | ExprResult Transform(Expr *E) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7641 | ExprResult Res; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7642 | while (true) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7643 | Res = TryTransform(E); |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7644 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7645 | // Exit if either the transform was valid or if there were no TypoExprs |
| 7646 | // to transform that still have any untried correction candidates.. |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7647 | if (!Res.isInvalid() || |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7648 | !CheckAndAdvanceTypoExprCorrectionStreams()) |
| 7649 | break; |
| 7650 | } |
| 7651 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7652 | // Ensure none of the TypoExprs have multiple typo correction candidates |
| 7653 | // with the same edit length that pass all the checks and filters. |
| 7654 | // TODO: Properly handle various permutations of possible corrections when |
| 7655 | // there is more than one potentially ambiguous typo correction. |
Kaelyn Takata | 26ffc5f | 2015-06-25 23:47:39 +0000 | [diff] [blame] | 7656 | // Also, disable typo correction while attempting the transform when |
| 7657 | // handling potentially ambiguous typo corrections as any new TypoExprs will |
| 7658 | // have been introduced by the application of one of the correction |
| 7659 | // candidates and add little to no value if corrected. |
| 7660 | SemaRef.DisableTypoCorrection = true; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7661 | while (!AmbiguousTypoExprs.empty()) { |
| 7662 | auto TE = AmbiguousTypoExprs.back(); |
| 7663 | auto Cached = TransformCache[TE]; |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7664 | auto &State = SemaRef.getTypoExprState(TE); |
| 7665 | State.Consumer->saveCurrentPosition(); |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7666 | TransformCache.erase(TE); |
| 7667 | if (!TryTransform(E).isInvalid()) { |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7668 | State.Consumer->resetCorrectionStream(); |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7669 | TransformCache.erase(TE); |
| 7670 | Res = ExprError(); |
| 7671 | break; |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7672 | } |
| 7673 | AmbiguousTypoExprs.remove(TE); |
| 7674 | State.Consumer->restoreSavedPosition(); |
| 7675 | TransformCache[TE] = Cached; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7676 | } |
Kaelyn Takata | 26ffc5f | 2015-06-25 23:47:39 +0000 | [diff] [blame] | 7677 | SemaRef.DisableTypoCorrection = false; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7678 | |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7679 | // Ensure that all of the TypoExprs within the current Expr have been found. |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7680 | if (!Res.isUsable()) |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7681 | FindTypoExprs(TypoExprs).TraverseStmt(E); |
| 7682 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7683 | EmitAllDiagnostics(); |
| 7684 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7685 | return Res; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7686 | } |
| 7687 | |
| 7688 | ExprResult TransformTypoExpr(TypoExpr *E) { |
| 7689 | // If the TypoExpr hasn't been seen before, record it. Otherwise, return the |
| 7690 | // cached transformation result if there is one and the TypoExpr isn't the |
| 7691 | // first one that was encountered. |
| 7692 | auto &CacheEntry = TransformCache[E]; |
| 7693 | if (!TypoExprs.insert(E) && !CacheEntry.isUnset()) { |
| 7694 | return CacheEntry; |
| 7695 | } |
| 7696 | |
| 7697 | auto &State = SemaRef.getTypoExprState(E); |
| 7698 | assert(State.Consumer && "Cannot transform a cleared TypoExpr"); |
| 7699 | |
| 7700 | // For the first TypoExpr and an uncached TypoExpr, find the next likely |
| 7701 | // typo correction and return it. |
| 7702 | while (TypoCorrection TC = State.Consumer->getNextCorrection()) { |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7703 | if (InitDecl && TC.getFoundDecl() == InitDecl) |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7704 | continue; |
Richard Smith | 1cf4541 | 2017-01-04 23:14:16 +0000 | [diff] [blame] | 7705 | // FIXME: If we would typo-correct to an invalid declaration, it's |
| 7706 | // probably best to just suppress all errors from this typo correction. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7707 | ExprResult NE = State.RecoveryHandler ? |
| 7708 | State.RecoveryHandler(SemaRef, E, TC) : |
| 7709 | attemptRecovery(SemaRef, *State.Consumer, TC); |
| 7710 | if (!NE.isInvalid()) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7711 | // Check whether there may be a second viable correction with the same |
| 7712 | // edit distance; if so, remember this TypoExpr may have an ambiguous |
| 7713 | // correction so it can be more thoroughly vetted later. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7714 | TypoCorrection Next; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7715 | if ((Next = State.Consumer->peekNextCorrection()) && |
| 7716 | Next.getEditDistance(false) == TC.getEditDistance(false)) { |
| 7717 | AmbiguousTypoExprs.insert(E); |
| 7718 | } else { |
| 7719 | AmbiguousTypoExprs.remove(E); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7720 | } |
| 7721 | assert(!NE.isUnset() && |
| 7722 | "Typo was transformed into a valid-but-null ExprResult"); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7723 | return CacheEntry = NE; |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7724 | } |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7725 | } |
| 7726 | return CacheEntry = ExprError(); |
| 7727 | } |
| 7728 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7729 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7730 | |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7731 | ExprResult |
| 7732 | Sema::CorrectDelayedTyposInExpr(Expr *E, VarDecl *InitDecl, |
| 7733 | llvm::function_ref<ExprResult(Expr *)> Filter) { |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7734 | // If the current evaluation context indicates there are uncorrected typos |
| 7735 | // and the current expression isn't guaranteed to not have typos, try to |
| 7736 | // resolve any TypoExpr nodes that might be in the expression. |
Kaelyn Takata | c71dda2 | 2014-12-02 22:05:35 +0000 | [diff] [blame] | 7737 | if (E && !ExprEvalContexts.empty() && ExprEvalContexts.back().NumTypos && |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7738 | (E->isTypeDependent() || E->isValueDependent() || |
| 7739 | E->isInstantiationDependent())) { |
| 7740 | auto TyposResolved = DelayedTypos.size(); |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7741 | auto Result = TransformTypos(*this, InitDecl, Filter).Transform(E); |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7742 | TyposResolved -= DelayedTypos.size(); |
Nick Lewycky | 4d59b77 | 2014-12-16 22:02:06 +0000 | [diff] [blame] | 7743 | if (Result.isInvalid() || Result.get() != E) { |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7744 | ExprEvalContexts.back().NumTypos -= TyposResolved; |
| 7745 | return Result; |
| 7746 | } |
Nick Lewycky | 4d59b77 | 2014-12-16 22:02:06 +0000 | [diff] [blame] | 7747 | assert(TyposResolved == 0 && "Corrected typo but got same Expr back?"); |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7748 | } |
| 7749 | return E; |
| 7750 | } |
| 7751 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7752 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC, |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7753 | bool DiscardedValue, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7754 | bool IsConstexpr, |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7755 | bool IsLambdaInitCaptureInitializer) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7756 | ExprResult FullExpr = FE; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7757 | |
| 7758 | if (!FullExpr.get()) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 7759 | return ExprError(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7760 | |
| 7761 | // If we are an init-expression in a lambdas init-capture, we should not |
| 7762 | // diagnose an unexpanded pack now (will be diagnosed once lambda-expr |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7763 | // containing full-expression is done). |
| 7764 | // template<class ... Ts> void test(Ts ... t) { |
| 7765 | // test([&a(t)]() { <-- (t) is an init-expr that shouldn't be diagnosed now. |
| 7766 | // return a; |
| 7767 | // }() ...); |
| 7768 | // } |
| 7769 | // FIXME: This is a hack. It would be better if we pushed the lambda scope |
| 7770 | // when we parse the lambda introducer, and teach capturing (but not |
| 7771 | // unexpanded pack detection) to walk over LambdaScopeInfos which don't have a |
| 7772 | // corresponding class yet (that is, have LambdaScopeInfo either represent a |
| 7773 | // lambda where we've entered the introducer but not the body, or represent a |
| 7774 | // lambda where we've entered the body, depending on where the |
| 7775 | // parser/instantiation has got to). |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7776 | if (!IsLambdaInitCaptureInitializer && |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7777 | DiagnoseUnexpandedParameterPack(FullExpr.get())) |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 7778 | return ExprError(); |
| 7779 | |
Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7780 | // Top-level expressions default to 'id' when we're in a debugger. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7781 | if (DiscardedValue && getLangOpts().DebuggerCastResultToId && |
Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7782 | FullExpr.get()->getType() == Context.UnknownAnyTy) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7783 | FullExpr = forceUnknownAnyToType(FullExpr.get(), Context.getObjCIdType()); |
Douglas Gregor | 95715f9 | 2011-12-15 00:53:32 +0000 | [diff] [blame] | 7784 | if (FullExpr.isInvalid()) |
| 7785 | return ExprError(); |
| 7786 | } |
Douglas Gregor | 0ec210b | 2011-03-07 02:05:23 +0000 | [diff] [blame] | 7787 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7788 | if (DiscardedValue) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7789 | FullExpr = CheckPlaceholderExpr(FullExpr.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7790 | if (FullExpr.isInvalid()) |
| 7791 | return ExprError(); |
| 7792 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7793 | FullExpr = IgnoredValueConversions(FullExpr.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7794 | if (FullExpr.isInvalid()) |
| 7795 | return ExprError(); |
| 7796 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7797 | |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7798 | FullExpr = CorrectDelayedTyposInExpr(FullExpr.get()); |
| 7799 | if (FullExpr.isInvalid()) |
| 7800 | return ExprError(); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7801 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7802 | CheckCompletedExpr(FullExpr.get(), CC, IsConstexpr); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7803 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7804 | // At the end of this full expression (which could be a deeply nested |
| 7805 | // lambda), if there is a potential capture within the nested lambda, |
Faisal Vali | 218e94b | 2013-11-12 03:56:08 +0000 | [diff] [blame] | 7806 | // have the outer capture-able lambda try and capture it. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7807 | // Consider the following code: |
| 7808 | // void f(int, int); |
| 7809 | // void f(const int&, double); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7810 | // void foo() { |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7811 | // const int x = 10, y = 20; |
| 7812 | // auto L = [=](auto a) { |
| 7813 | // auto M = [=](auto b) { |
| 7814 | // f(x, b); <-- requires x to be captured by L and M |
| 7815 | // f(y, a); <-- requires y to be captured by L, but not all Ms |
| 7816 | // }; |
| 7817 | // }; |
| 7818 | // } |
| 7819 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7820 | // FIXME: Also consider what happens for something like this that involves |
| 7821 | // the gnu-extension statement-expressions or even lambda-init-captures: |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7822 | // void f() { |
| 7823 | // const int n = 0; |
| 7824 | // auto L = [&](auto a) { |
| 7825 | // +n + ({ 0; a; }); |
| 7826 | // }; |
| 7827 | // } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7828 | // |
| 7829 | // Here, we see +n, and then the full-expression 0; ends, so we don't |
| 7830 | // capture n (and instead remove it from our list of potential captures), |
| 7831 | // and then the full-expression +n + ({ 0; }); ends, but it's too late |
Faisal Vali | 218e94b | 2013-11-12 03:56:08 +0000 | [diff] [blame] | 7832 | // for us to see that we need to capture n after all. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7833 | |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7834 | LambdaScopeInfo *const CurrentLSI = |
| 7835 | getCurLambda(/*IgnoreCapturedRegions=*/true); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7836 | // FIXME: PR 17877 showed that getCurLambda() can return a valid pointer |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7837 | // even if CurContext is not a lambda call operator. Refer to that Bug Report |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7838 | // for an example of the code that might cause this asynchrony. |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7839 | // By ensuring we are in the context of a lambda's call operator |
| 7840 | // we can fix the bug (we only need to check whether we need to capture |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7841 | // if we are within a lambda's body); but per the comments in that |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7842 | // PR, a proper fix would entail : |
| 7843 | // "Alternative suggestion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7844 | // - Add to Sema an integer holding the smallest (outermost) scope |
| 7845 | // index that we are *lexically* within, and save/restore/set to |
| 7846 | // FunctionScopes.size() in InstantiatingTemplate's |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7847 | // constructor/destructor. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7848 | // - Teach the handful of places that iterate over FunctionScopes to |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7849 | // stop at the outermost enclosing lexical scope." |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7850 | DeclContext *DC = CurContext; |
| 7851 | while (DC && isa<CapturedDecl>(DC)) |
| 7852 | DC = DC->getParent(); |
| 7853 | const bool IsInLambdaDeclContext = isLambdaCallOperator(DC); |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7854 | if (IsInLambdaDeclContext && CurrentLSI && |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7855 | CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid()) |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7856 | CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(FE, CurrentLSI, |
| 7857 | *this); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7858 | return MaybeCreateExprWithCleanups(FullExpr); |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 7859 | } |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 7860 | |
| 7861 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
| 7862 | if (!FullStmt) return StmtError(); |
| 7863 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7864 | return MaybeCreateStmtWithCleanups(FullStmt); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 7865 | } |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7866 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7867 | Sema::IfExistsResult |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7868 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, |
| 7869 | CXXScopeSpec &SS, |
| 7870 | const DeclarationNameInfo &TargetNameInfo) { |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7871 | DeclarationName TargetName = TargetNameInfo.getName(); |
| 7872 | if (!TargetName) |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7873 | return IER_DoesNotExist; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7874 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7875 | // If the name itself is dependent, then the result is dependent. |
| 7876 | if (TargetName.isDependentName()) |
| 7877 | return IER_Dependent; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7878 | |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7879 | // Do the redeclaration lookup in the current scope. |
| 7880 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
| 7881 | Sema::NotForRedeclaration); |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7882 | LookupParsedName(R, S, &SS); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7883 | R.suppressDiagnostics(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7884 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7885 | switch (R.getResultKind()) { |
| 7886 | case LookupResult::Found: |
| 7887 | case LookupResult::FoundOverloaded: |
| 7888 | case LookupResult::FoundUnresolvedValue: |
| 7889 | case LookupResult::Ambiguous: |
| 7890 | return IER_Exists; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7891 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7892 | case LookupResult::NotFound: |
| 7893 | return IER_DoesNotExist; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7894 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7895 | case LookupResult::NotFoundInCurrentInstantiation: |
| 7896 | return IER_Dependent; |
| 7897 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7898 | |
| 7899 | llvm_unreachable("Invalid LookupResult Kind!"); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7900 | } |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7901 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7902 | Sema::IfExistsResult |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 7903 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc, |
| 7904 | bool IsIfExists, CXXScopeSpec &SS, |
| 7905 | UnqualifiedId &Name) { |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7906 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7907 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 7908 | // Check for an unexpanded parameter pack. |
| 7909 | auto UPPC = IsIfExists ? UPPC_IfExists : UPPC_IfNotExists; |
| 7910 | if (DiagnoseUnexpandedParameterPack(SS, UPPC) || |
| 7911 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC)) |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 7912 | return IER_Error; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7913 | |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7914 | return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo); |
| 7915 | } |