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 |
| 11 | /// \brief Implements semantic analysis for C++ expressions. |
| 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 | |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 45 | /// \brief Handle the result of the special case name lookup for inheriting |
| 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 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 83 | ParsedType Sema::getDestructorName(SourceLocation TildeLoc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 84 | IdentifierInfo &II, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 85 | SourceLocation NameLoc, |
| 86 | Scope *S, CXXScopeSpec &SS, |
| 87 | ParsedType ObjectTypePtr, |
| 88 | bool EnteringContext) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 89 | // Determine where to perform name lookup. |
| 90 | |
| 91 | // FIXME: This area of the standard is very messy, and the current |
| 92 | // wording is rather unclear about which scopes we search for the |
| 93 | // destructor name; see core issues 399 and 555. Issue 399 in |
| 94 | // particular shows where the current description of destructor name |
| 95 | // lookup is completely out of line with existing practice, e.g., |
| 96 | // this appears to be ill-formed: |
| 97 | // |
| 98 | // namespace N { |
| 99 | // template <typename T> struct S { |
| 100 | // ~S(); |
| 101 | // }; |
| 102 | // } |
| 103 | // |
| 104 | // void f(N::S<int>* s) { |
| 105 | // s->N::S<int>::~S(); |
| 106 | // } |
| 107 | // |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 108 | // See also PR6358 and PR6359. |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 109 | // For this reason, we're currently only doing the C++03 version of this |
| 110 | // 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] | 111 | QualType SearchType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 112 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 113 | bool isDependent = false; |
| 114 | bool LookInScope = false; |
| 115 | |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 116 | if (SS.isInvalid()) |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 117 | return nullptr; |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 118 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 119 | // If we have an object type, it's because we are in a |
| 120 | // pseudo-destructor-expression or a member access expression, and |
| 121 | // we know what type we're looking for. |
| 122 | if (ObjectTypePtr) |
| 123 | SearchType = GetTypeFromParser(ObjectTypePtr); |
| 124 | |
| 125 | if (SS.isSet()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 126 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 127 | |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 128 | bool AlreadySearched = false; |
| 129 | bool LookAtPrefix = true; |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 130 | // C++11 [basic.lookup.qual]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 131 | // 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] | 132 | // 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] | 133 | // nested-name-specifier. Similarly, in a qualified-id of the form: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 134 | // |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 135 | // nested-name-specifier[opt] class-name :: ~ class-name |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 136 | // |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 137 | // 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] | 138 | // |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 139 | // Here, we determine whether the code below is permitted to look at the |
| 140 | // prefix of the nested-name-specifier. |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 141 | DeclContext *DC = computeDeclContext(SS, EnteringContext); |
| 142 | if (DC && DC->isFileContext()) { |
| 143 | AlreadySearched = true; |
| 144 | LookupCtx = DC; |
| 145 | isDependent = false; |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 146 | } else if (DC && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 147 | LookAtPrefix = false; |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 148 | LookInScope = true; |
| 149 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 150 | |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 151 | // The second case from the C++03 rules quoted further above. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 152 | NestedNameSpecifier *Prefix = nullptr; |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 153 | if (AlreadySearched) { |
| 154 | // Nothing left to do. |
| 155 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { |
| 156 | CXXScopeSpec PrefixSS; |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 157 | PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data())); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 158 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); |
| 159 | isDependent = isDependentScopeSpecifier(PrefixSS); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 160 | } else if (ObjectTypePtr) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 161 | LookupCtx = computeDeclContext(SearchType); |
| 162 | isDependent = SearchType->isDependentType(); |
| 163 | } else { |
| 164 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 165 | isDependent = LookupCtx && LookupCtx->isDependentContext(); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 166 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 167 | } else if (ObjectTypePtr) { |
| 168 | // C++ [basic.lookup.classref]p3: |
| 169 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 170 | // in the context of the entire postfix-expression. If the type T |
| 171 | // of the object expression is of a class type C, the type-name is |
| 172 | // also looked up in the scope of class C. At least one of the |
| 173 | // lookups shall find a name that refers to (possibly |
| 174 | // cv-qualified) T. |
| 175 | LookupCtx = computeDeclContext(SearchType); |
| 176 | isDependent = SearchType->isDependentType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 177 | assert((isDependent || !SearchType->isIncompleteType()) && |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 178 | "Caller should have completed object type"); |
| 179 | |
| 180 | LookInScope = true; |
| 181 | } else { |
| 182 | // Perform lookup into the current scope (only). |
| 183 | LookInScope = true; |
| 184 | } |
| 185 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 186 | TypeDecl *NonMatchingTypeDecl = nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 187 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); |
| 188 | for (unsigned Step = 0; Step != 2; ++Step) { |
| 189 | // Look for the name first in the computed lookup context (if we |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 190 | // 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] | 191 | // we're allowed to look there). |
| 192 | Found.clear(); |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame] | 193 | if (Step == 0 && LookupCtx) { |
| 194 | if (RequireCompleteDeclContext(SS, LookupCtx)) |
| 195 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 196 | LookupQualifiedName(Found, LookupCtx); |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame] | 197 | } else if (Step == 1 && LookInScope && S) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 198 | LookupName(Found, S); |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame] | 199 | } else { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 200 | continue; |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame] | 201 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 202 | |
| 203 | // FIXME: Should we be suppressing ambiguities here? |
| 204 | if (Found.isAmbiguous()) |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 205 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 206 | |
| 207 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
| 208 | QualType T = Context.getTypeDeclType(Type); |
Nico Weber | 83a6387 | 2014-11-12 04:33:52 +0000 | [diff] [blame] | 209 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 210 | |
| 211 | if (SearchType.isNull() || SearchType->isDependentType() || |
| 212 | Context.hasSameUnqualifiedType(T, SearchType)) { |
| 213 | // We found our type! |
| 214 | |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 215 | return CreateParsedType(T, |
| 216 | Context.getTrivialTypeSourceInfo(T, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 217 | } |
John Wiegley | b4a9e51 | 2011-03-08 08:13:22 +0000 | [diff] [blame] | 218 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 219 | if (!SearchType.isNull()) |
| 220 | NonMatchingTypeDecl = Type; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // If the name that we found is a class template name, and it is |
| 224 | // the same name as the template name in the last part of the |
| 225 | // nested-name-specifier (if present) or the object type, then |
| 226 | // this is the destructor for that class. |
| 227 | // FIXME: This is a workaround until we get real drafting for core |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 228 | // issue 399, for which there isn't even an obvious direction. |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 229 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
| 230 | QualType MemberOfType; |
| 231 | if (SS.isSet()) { |
| 232 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
| 233 | // Figure out the type of the context, if it has one. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 234 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) |
| 235 | MemberOfType = Context.getTypeDeclType(Record); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | if (MemberOfType.isNull()) |
| 239 | MemberOfType = SearchType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 240 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 241 | if (MemberOfType.isNull()) |
| 242 | continue; |
| 243 | |
| 244 | // We're referring into a class template specialization. If the |
| 245 | // class template we found is the same as the template being |
| 246 | // specialized, we found what we are looking for. |
| 247 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
| 248 | if (ClassTemplateSpecializationDecl *Spec |
| 249 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
| 250 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
| 251 | Template->getCanonicalDecl()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 252 | return CreateParsedType( |
| 253 | MemberOfType, |
| 254 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | continue; |
| 258 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 259 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 260 | // We're referring to an unresolved class template |
| 261 | // specialization. Determine whether we class template we found |
| 262 | // is the same as the template being specialized or, if we don't |
| 263 | // know which template is being specialized, that it at least |
| 264 | // has the same name. |
| 265 | if (const TemplateSpecializationType *SpecType |
| 266 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
| 267 | TemplateName SpecName = SpecType->getTemplateName(); |
| 268 | |
| 269 | // The class template we found is the same template being |
| 270 | // specialized. |
| 271 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
| 272 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 273 | return CreateParsedType( |
| 274 | MemberOfType, |
| 275 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 276 | |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | // The class template we found has the same name as the |
| 281 | // (dependent) template name being specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 282 | if (DependentTemplateName *DepTemplate |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 283 | = SpecName.getAsDependentTemplateName()) { |
| 284 | if (DepTemplate->isIdentifier() && |
| 285 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 286 | return CreateParsedType( |
| 287 | MemberOfType, |
| 288 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 289 | |
| 290 | continue; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (isDependent) { |
| 297 | // We didn't find our type, but that's okay: it's dependent |
| 298 | // anyway. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 299 | |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 300 | // FIXME: What if we have no nested-name-specifier? |
| 301 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), |
| 302 | SS.getWithLocInContext(Context), |
| 303 | II, NameLoc); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 304 | return ParsedType::make(T); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 307 | if (NonMatchingTypeDecl) { |
| 308 | QualType T = Context.getTypeDeclType(NonMatchingTypeDecl); |
| 309 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) |
| 310 | << T << SearchType; |
| 311 | Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here) |
| 312 | << T; |
| 313 | } else if (ObjectTypePtr) |
| 314 | Diag(NameLoc, diag::err_ident_in_dtor_not_a_type) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 315 | << &II; |
David Blaikie | 5e026f5 | 2013-03-20 17:42:13 +0000 | [diff] [blame] | 316 | else { |
| 317 | SemaDiagnosticBuilder DtorDiag = Diag(NameLoc, |
| 318 | diag::err_destructor_class_name); |
| 319 | if (S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 320 | const DeclContext *Ctx = S->getEntity(); |
David Blaikie | 5e026f5 | 2013-03-20 17:42:13 +0000 | [diff] [blame] | 321 | if (const CXXRecordDecl *Class = dyn_cast_or_null<CXXRecordDecl>(Ctx)) |
| 322 | DtorDiag << FixItHint::CreateReplacement(SourceRange(NameLoc), |
| 323 | Class->getNameAsString()); |
| 324 | } |
| 325 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 326 | |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 327 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 330 | ParsedType Sema::getDestructorTypeForDecltype(const DeclSpec &DS, |
| 331 | ParsedType ObjectType) { |
| 332 | if (DS.getTypeSpecType() == DeclSpec::TST_error) |
| 333 | return nullptr; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 334 | |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 335 | if (DS.getTypeSpecType() == DeclSpec::TST_decltype_auto) { |
| 336 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decltype_auto_invalid); |
| 337 | return nullptr; |
| 338 | } |
| 339 | |
| 340 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype && |
| 341 | "unexpected type in getDestructorType"); |
| 342 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 343 | |
| 344 | // If we know the type of the object, check that the correct destructor |
| 345 | // type was named now; we can give better diagnostics this way. |
| 346 | QualType SearchType = GetTypeFromParser(ObjectType); |
| 347 | if (!SearchType.isNull() && !SearchType->isDependentType() && |
| 348 | !Context.hasSameUnqualifiedType(T, SearchType)) { |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 349 | Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch) |
| 350 | << T << SearchType; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 351 | return nullptr; |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | return ParsedType::make(T); |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 357 | bool Sema::checkLiteralOperatorId(const CXXScopeSpec &SS, |
| 358 | const UnqualifiedId &Name) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 359 | assert(Name.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId); |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 360 | |
| 361 | if (!SS.isValid()) |
| 362 | return false; |
| 363 | |
| 364 | switch (SS.getScopeRep()->getKind()) { |
| 365 | case NestedNameSpecifier::Identifier: |
| 366 | case NestedNameSpecifier::TypeSpec: |
| 367 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 368 | // Per C++11 [over.literal]p2, literal operators can only be declared at |
| 369 | // namespace scope. Therefore, this unqualified-id cannot name anything. |
| 370 | // Reject it early, because we have no AST representation for this in the |
| 371 | // case where the scope is dependent. |
| 372 | Diag(Name.getLocStart(), diag::err_literal_operator_id_outside_namespace) |
| 373 | << SS.getScopeRep(); |
| 374 | return true; |
| 375 | |
| 376 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 377 | case NestedNameSpecifier::Super: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 378 | case NestedNameSpecifier::Namespace: |
| 379 | case NestedNameSpecifier::NamespaceAlias: |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | llvm_unreachable("unknown nested name specifier kind"); |
| 384 | } |
| 385 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 386 | /// \brief Build a C++ typeid expression with a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 387 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 388 | SourceLocation TypeidLoc, |
| 389 | TypeSourceInfo *Operand, |
| 390 | SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 391 | // C++ [expr.typeid]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 392 | // 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] | 393 | // that is the operand of typeid are always ignored. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 394 | // 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] | 395 | // type, the class shall be completely-defined. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 396 | Qualifiers Quals; |
| 397 | QualType T |
| 398 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
| 399 | Quals); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 400 | if (T->getAs<RecordType>() && |
| 401 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 402 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 403 | |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 404 | if (T->isVariablyModifiedType()) |
| 405 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) << T); |
| 406 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 407 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), Operand, |
| 408 | SourceRange(TypeidLoc, RParenLoc)); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /// \brief Build a C++ typeid expression with an expression operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 412 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 413 | SourceLocation TypeidLoc, |
| 414 | Expr *E, |
| 415 | SourceLocation RParenLoc) { |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 416 | bool WasEvaluated = false; |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 417 | if (E && !E->isTypeDependent()) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 418 | if (E->getType()->isPlaceholderType()) { |
| 419 | ExprResult result = CheckPlaceholderExpr(E); |
| 420 | if (result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 421 | E = result.get(); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 424 | QualType T = E->getType(); |
| 425 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
| 426 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 427 | // C++ [expr.typeid]p3: |
| 428 | // [...] If the type of the expression is a class type, the class |
| 429 | // shall be completely-defined. |
| 430 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 431 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 433 | // C++ [expr.typeid]p3: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 434 | // 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] | 435 | // polymorphic class type [...] [the] expression is an unevaluated |
| 436 | // operand. [...] |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 437 | if (RecordD->isPolymorphic() && E->isGLValue()) { |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 438 | // The subexpression is potentially evaluated; switch the context |
| 439 | // and recheck the subexpression. |
Benjamin Kramer | d81108f | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 440 | ExprResult Result = TransformToPotentiallyEvaluated(E); |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 441 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 442 | E = Result.get(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 443 | |
| 444 | // We require a vtable to query the type at run time. |
| 445 | MarkVTableUsed(TypeidLoc, RecordD); |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 446 | WasEvaluated = true; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 447 | } |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 448 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 449 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 450 | // C++ [expr.typeid]p4: |
| 451 | // [...] 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] | 452 | // cv-qualified type, the result of the typeid expression refers to a |
| 453 | // std::type_info object representing the cv-unqualified referenced |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 454 | // type. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 455 | Qualifiers Quals; |
| 456 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
| 457 | if (!Context.hasSameType(T, UnqualT)) { |
| 458 | T = UnqualT; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 459 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).get(); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 462 | |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 463 | if (E->getType()->isVariablyModifiedType()) |
| 464 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) |
| 465 | << E->getType()); |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 466 | else if (!inTemplateInstantiation() && |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 467 | E->HasSideEffects(Context, WasEvaluated)) { |
| 468 | // The expression operand for typeid is in an unevaluated expression |
| 469 | // context, so side effects could result in unintended consequences. |
| 470 | Diag(E->getExprLoc(), WasEvaluated |
| 471 | ? diag::warn_side_effects_typeid |
| 472 | : diag::warn_side_effects_unevaluated_context); |
| 473 | } |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 474 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 475 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), E, |
| 476 | SourceRange(TypeidLoc, RParenLoc)); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 480 | ExprResult |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 481 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 482 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 483 | // Find the std::type_info type. |
Sebastian Redl | 7ac9741 | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 484 | if (!getStdNamespace()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 485 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 487 | if (!CXXTypeInfoDecl) { |
| 488 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 489 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 490 | LookupQualifiedName(R, getStdNamespace()); |
| 491 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
Nico Weber | 5f96883 | 2012-06-19 23:58:27 +0000 | [diff] [blame] | 492 | // Microsoft's typeinfo doesn't have type_info in std but in the global |
| 493 | // namespace if _HAS_EXCEPTIONS is defined to 0. See PR13153. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 494 | if (!CXXTypeInfoDecl && LangOpts.MSVCCompat) { |
Nico Weber | 5f96883 | 2012-06-19 23:58:27 +0000 | [diff] [blame] | 495 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 496 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 497 | } |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 498 | if (!CXXTypeInfoDecl) |
| 499 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 500 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 501 | |
Nico Weber | 1b7f39d | 2012-05-20 01:27:21 +0000 | [diff] [blame] | 502 | if (!getLangOpts().RTTI) { |
| 503 | return ExprError(Diag(OpLoc, diag::err_no_typeid_with_fno_rtti)); |
| 504 | } |
| 505 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 506 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 507 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 508 | if (isType) { |
| 509 | // The operand is a type; handle it as such. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 510 | TypeSourceInfo *TInfo = nullptr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 511 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 512 | &TInfo); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 513 | if (T.isNull()) |
| 514 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 515 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 516 | if (!TInfo) |
| 517 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 518 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 519 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 520 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 522 | // The operand is an expression. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 523 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 524 | } |
| 525 | |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 526 | /// Grabs __declspec(uuid()) off a type, or returns 0 if we cannot resolve to |
| 527 | /// a single GUID. |
| 528 | static void |
| 529 | getUuidAttrOfType(Sema &SemaRef, QualType QT, |
| 530 | llvm::SmallSetVector<const UuidAttr *, 1> &UuidAttrs) { |
| 531 | // Optionally remove one level of pointer, reference or array indirection. |
| 532 | const Type *Ty = QT.getTypePtr(); |
| 533 | if (QT->isPointerType() || QT->isReferenceType()) |
| 534 | Ty = QT->getPointeeType().getTypePtr(); |
| 535 | else if (QT->isArrayType()) |
| 536 | Ty = Ty->getBaseElementTypeUnsafe(); |
| 537 | |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 538 | const auto *TD = Ty->getAsTagDecl(); |
| 539 | if (!TD) |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 540 | return; |
| 541 | |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 542 | if (const auto *Uuid = TD->getMostRecentDecl()->getAttr<UuidAttr>()) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 543 | UuidAttrs.insert(Uuid); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | // __uuidof can grab UUIDs from template arguments. |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 548 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(TD)) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 549 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
| 550 | for (const TemplateArgument &TA : TAL.asArray()) { |
| 551 | const UuidAttr *UuidForTA = nullptr; |
| 552 | if (TA.getKind() == TemplateArgument::Type) |
| 553 | getUuidAttrOfType(SemaRef, TA.getAsType(), UuidAttrs); |
| 554 | else if (TA.getKind() == TemplateArgument::Declaration) |
| 555 | getUuidAttrOfType(SemaRef, TA.getAsDecl()->getType(), UuidAttrs); |
| 556 | |
| 557 | if (UuidForTA) |
| 558 | UuidAttrs.insert(UuidForTA); |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 563 | /// \brief Build a Microsoft __uuidof expression with a type operand. |
| 564 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 565 | SourceLocation TypeidLoc, |
| 566 | TypeSourceInfo *Operand, |
| 567 | SourceLocation RParenLoc) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 568 | StringRef UuidStr; |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 569 | if (!Operand->getType()->isDependentType()) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 570 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
| 571 | getUuidAttrOfType(*this, Operand->getType(), UuidAttrs); |
| 572 | if (UuidAttrs.empty()) |
| 573 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 574 | if (UuidAttrs.size() > 1) |
| 575 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 576 | UuidStr = UuidAttrs.back()->getGuid(); |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 577 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 578 | |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 579 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), Operand, UuidStr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 580 | SourceRange(TypeidLoc, RParenLoc)); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /// \brief Build a Microsoft __uuidof expression with an expression operand. |
| 584 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 585 | SourceLocation TypeidLoc, |
| 586 | Expr *E, |
| 587 | SourceLocation RParenLoc) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 588 | StringRef UuidStr; |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 589 | if (!E->getType()->isDependentType()) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 590 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 591 | UuidStr = "00000000-0000-0000-0000-000000000000"; |
| 592 | } else { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 593 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
| 594 | getUuidAttrOfType(*this, E->getType(), UuidAttrs); |
| 595 | if (UuidAttrs.empty()) |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 596 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 597 | if (UuidAttrs.size() > 1) |
| 598 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 599 | UuidStr = UuidAttrs.back()->getGuid(); |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 600 | } |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 601 | } |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 602 | |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 603 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), E, UuidStr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 604 | SourceRange(TypeidLoc, RParenLoc)); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 608 | ExprResult |
| 609 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 610 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 611 | // If MSVCGuidDecl has not been cached, do the lookup. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 612 | if (!MSVCGuidDecl) { |
| 613 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 614 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 615 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 616 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 617 | if (!MSVCGuidDecl) |
| 618 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 621 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 622 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 623 | if (isType) { |
| 624 | // The operand is a type; handle it as such. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 625 | TypeSourceInfo *TInfo = nullptr; |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 626 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 627 | &TInfo); |
| 628 | if (T.isNull()) |
| 629 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 630 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 631 | if (!TInfo) |
| 632 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 633 | |
| 634 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 635 | } |
| 636 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 637 | // The operand is an expression. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 638 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 639 | } |
| 640 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 641 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 642 | ExprResult |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 643 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 644 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Bill Wendling | bf313b0 | 2007-02-13 20:09:46 +0000 | [diff] [blame] | 645 | "Unknown C++ Boolean value!"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 646 | return new (Context) |
| 647 | CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 649 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 650 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 651 | ExprResult |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 652 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 653 | return new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 656 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 657 | ExprResult |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 658 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
| 659 | bool IsThrownVarInScope = false; |
| 660 | if (Ex) { |
| 661 | // C++0x [class.copymove]p31: |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 662 | // When certain criteria are met, an implementation is allowed to omit the |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 663 | // copy/move construction of a class object [...] |
| 664 | // |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 665 | // - in a throw-expression, when the operand is the name of a |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 666 | // non-volatile automatic object (other than a function or catch- |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 667 | // clause parameter) whose scope does not extend beyond the end of the |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 668 | // innermost enclosing try-block (if there is one), the copy/move |
| 669 | // operation from the operand to the exception object (15.1) can be |
| 670 | // omitted by constructing the automatic object directly into the |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 671 | // exception object |
| 672 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) |
| 673 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 674 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { |
| 675 | for( ; S; S = S->getParent()) { |
| 676 | if (S->isDeclScope(Var)) { |
| 677 | IsThrownVarInScope = true; |
| 678 | break; |
| 679 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 680 | |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 681 | if (S->getFlags() & |
| 682 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
| 683 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | |
| 684 | Scope::TryScope)) |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 691 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
| 692 | } |
| 693 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 694 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 695 | bool IsThrownVarInScope) { |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 696 | // Don't report an error if 'throw' is used in system headers. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 697 | if (!getLangOpts().CXXExceptions && |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 698 | !getSourceManager().isInSystemHeader(OpLoc)) |
Anders Carlsson | b94ad3e | 2011-02-19 21:53:09 +0000 | [diff] [blame] | 699 | Diag(OpLoc, diag::err_exceptions_disabled) << "throw"; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 700 | |
Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 701 | // Exceptions aren't allowed in CUDA device code. |
| 702 | if (getLangOpts().CUDA) |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 703 | CUDADiagIfDeviceCode(OpLoc, diag::err_cuda_device_exceptions) |
| 704 | << "throw" << CurrentCUDATarget(); |
Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 705 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 706 | if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope()) |
| 707 | Diag(OpLoc, diag::err_omp_simd_region_cannot_use_stmt) << "throw"; |
| 708 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 709 | if (Ex && !Ex->isTypeDependent()) { |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 710 | QualType ExceptionObjectTy = Context.getExceptionObjectType(Ex->getType()); |
| 711 | if (CheckCXXThrowOperand(OpLoc, ExceptionObjectTy, Ex)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 712 | return ExprError(); |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 713 | |
| 714 | // Initialize the exception result. This implicitly weeds out |
| 715 | // abstract types or types with inaccessible copy constructors. |
| 716 | |
| 717 | // C++0x [class.copymove]p31: |
| 718 | // When certain criteria are met, an implementation is allowed to omit the |
| 719 | // copy/move construction of a class object [...] |
| 720 | // |
| 721 | // - in a throw-expression, when the operand is the name of a |
| 722 | // non-volatile automatic object (other than a function or |
| 723 | // catch-clause |
| 724 | // parameter) whose scope does not extend beyond the end of the |
| 725 | // innermost enclosing try-block (if there is one), the copy/move |
| 726 | // operation from the operand to the exception object (15.1) can be |
| 727 | // omitted by constructing the automatic object directly into the |
| 728 | // exception object |
| 729 | const VarDecl *NRVOVariable = nullptr; |
| 730 | if (IsThrownVarInScope) |
| 731 | NRVOVariable = getCopyElisionCandidate(QualType(), Ex, false); |
| 732 | |
| 733 | InitializedEntity Entity = InitializedEntity::InitializeException( |
| 734 | OpLoc, ExceptionObjectTy, |
| 735 | /*NRVO=*/NRVOVariable != nullptr); |
| 736 | ExprResult Res = PerformMoveOrCopyInitialization( |
| 737 | Entity, NRVOVariable, QualType(), Ex, IsThrownVarInScope); |
| 738 | if (Res.isInvalid()) |
| 739 | return ExprError(); |
| 740 | Ex = Res.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 741 | } |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 742 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 743 | return new (Context) |
| 744 | CXXThrowExpr(Ex, Context.VoidTy, OpLoc, IsThrownVarInScope); |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 745 | } |
| 746 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 747 | static void |
| 748 | collectPublicBases(CXXRecordDecl *RD, |
| 749 | llvm::DenseMap<CXXRecordDecl *, unsigned> &SubobjectsSeen, |
| 750 | llvm::SmallPtrSetImpl<CXXRecordDecl *> &VBases, |
| 751 | llvm::SetVector<CXXRecordDecl *> &PublicSubobjectsSeen, |
| 752 | bool ParentIsPublic) { |
| 753 | for (const CXXBaseSpecifier &BS : RD->bases()) { |
| 754 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
| 755 | bool NewSubobject; |
| 756 | // Virtual bases constitute the same subobject. Non-virtual bases are |
| 757 | // always distinct subobjects. |
| 758 | if (BS.isVirtual()) |
| 759 | NewSubobject = VBases.insert(BaseDecl).second; |
| 760 | else |
| 761 | NewSubobject = true; |
| 762 | |
| 763 | if (NewSubobject) |
| 764 | ++SubobjectsSeen[BaseDecl]; |
| 765 | |
| 766 | // Only add subobjects which have public access throughout the entire chain. |
| 767 | bool PublicPath = ParentIsPublic && BS.getAccessSpecifier() == AS_public; |
| 768 | if (PublicPath) |
| 769 | PublicSubobjectsSeen.insert(BaseDecl); |
| 770 | |
| 771 | // Recurse on to each base subobject. |
| 772 | collectPublicBases(BaseDecl, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
| 773 | PublicPath); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | static void getUnambiguousPublicSubobjects( |
| 778 | CXXRecordDecl *RD, llvm::SmallVectorImpl<CXXRecordDecl *> &Objects) { |
| 779 | llvm::DenseMap<CXXRecordDecl *, unsigned> SubobjectsSeen; |
| 780 | llvm::SmallSet<CXXRecordDecl *, 2> VBases; |
| 781 | llvm::SetVector<CXXRecordDecl *> PublicSubobjectsSeen; |
| 782 | SubobjectsSeen[RD] = 1; |
| 783 | PublicSubobjectsSeen.insert(RD); |
| 784 | collectPublicBases(RD, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
| 785 | /*ParentIsPublic=*/true); |
| 786 | |
| 787 | for (CXXRecordDecl *PublicSubobject : PublicSubobjectsSeen) { |
| 788 | // Skip ambiguous objects. |
| 789 | if (SubobjectsSeen[PublicSubobject] > 1) |
| 790 | continue; |
| 791 | |
| 792 | Objects.push_back(PublicSubobject); |
| 793 | } |
| 794 | } |
| 795 | |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 796 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 797 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, |
| 798 | QualType ExceptionObjectTy, Expr *E) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 799 | // If the type of the exception would be an incomplete type or a pointer |
| 800 | // 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] | 801 | QualType Ty = ExceptionObjectTy; |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 802 | bool isPointer = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 803 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 804 | Ty = Ptr->getPointeeType(); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 805 | isPointer = true; |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 806 | } |
| 807 | if (!isPointer || !Ty->isVoidType()) { |
| 808 | if (RequireCompleteType(ThrowLoc, Ty, |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 809 | isPointer ? diag::err_throw_incomplete_ptr |
| 810 | : diag::err_throw_incomplete, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 811 | E->getSourceRange())) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 812 | return true; |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 813 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 814 | if (RequireNonAbstractType(ThrowLoc, ExceptionObjectTy, |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 815 | diag::err_throw_abstract_type, E)) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 816 | return true; |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 819 | // If the exception has class type, we need additional handling. |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 820 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 821 | if (!RD) |
| 822 | return false; |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 823 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 824 | // If we are throwing a polymorphic class type or pointer thereof, |
| 825 | // exception handling will make use of the vtable. |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 826 | MarkVTableUsed(ThrowLoc, RD); |
| 827 | |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 828 | // If a pointer is thrown, the referenced object will not be destroyed. |
| 829 | if (isPointer) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 830 | return false; |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 831 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 832 | // 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] | 833 | if (!RD->hasIrrelevantDestructor()) { |
| 834 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
| 835 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
| 836 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 837 | PDiag(diag::err_access_dtor_exception) << Ty); |
| 838 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 839 | return true; |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 842 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 843 | // The MSVC ABI creates a list of all types which can catch the exception |
| 844 | // object. This list also references the appropriate copy constructor to call |
| 845 | // 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] | 846 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 847 | // We are only interested in the public, unambiguous bases contained within |
| 848 | // the exception object. Bases which are ambiguous or otherwise |
| 849 | // inaccessible are not catchable types. |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 850 | llvm::SmallVector<CXXRecordDecl *, 2> UnambiguousPublicSubobjects; |
| 851 | getUnambiguousPublicSubobjects(RD, UnambiguousPublicSubobjects); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 852 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 853 | for (CXXRecordDecl *Subobject : UnambiguousPublicSubobjects) { |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 854 | // Attempt to lookup the copy constructor. Various pieces of machinery |
| 855 | // will spring into action, like template instantiation, which means this |
| 856 | // cannot be a simple walk of the class's decls. Instead, we must perform |
| 857 | // lookup and overload resolution. |
| 858 | CXXConstructorDecl *CD = LookupCopyingConstructor(Subobject, 0); |
| 859 | if (!CD) |
| 860 | continue; |
| 861 | |
| 862 | // Mark the constructor referenced as it is used by this throw expression. |
| 863 | MarkFunctionReferenced(E->getExprLoc(), CD); |
| 864 | |
| 865 | // Skip this copy constructor if it is trivial, we don't need to record it |
| 866 | // in the catchable type data. |
| 867 | if (CD->isTrivial()) |
| 868 | continue; |
| 869 | |
| 870 | // The copy constructor is non-trivial, create a mapping from this class |
| 871 | // type to this constructor. |
| 872 | // N.B. The selection of copy constructor is not sensitive to this |
| 873 | // particular throw-site. Lookup will be performed at the catch-site to |
| 874 | // ensure that the copy constructor is, in fact, accessible (via |
| 875 | // friendship or any other means). |
| 876 | Context.addCopyConstructorForExceptionObject(Subobject, CD); |
| 877 | |
| 878 | // We don't keep the instantiated default argument expressions around so |
| 879 | // we must rebuild them here. |
| 880 | for (unsigned I = 1, E = CD->getNumParams(); I != E; ++I) { |
Reid Kleckner | c01ee75 | 2016-11-23 16:51:30 +0000 | [diff] [blame] | 881 | if (CheckCXXDefaultArgExpr(ThrowLoc, CD, CD->getParamDecl(I))) |
| 882 | return true; |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | } |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 886 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 887 | return false; |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 888 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 889 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 890 | static QualType adjustCVQualifiersForCXXThisWithinLambda( |
| 891 | ArrayRef<FunctionScopeInfo *> FunctionScopes, QualType ThisTy, |
| 892 | DeclContext *CurSemaContext, ASTContext &ASTCtx) { |
| 893 | |
| 894 | QualType ClassType = ThisTy->getPointeeType(); |
| 895 | LambdaScopeInfo *CurLSI = nullptr; |
| 896 | DeclContext *CurDC = CurSemaContext; |
| 897 | |
| 898 | // Iterate through the stack of lambdas starting from the innermost lambda to |
| 899 | // the outermost lambda, checking if '*this' is ever captured by copy - since |
| 900 | // that could change the cv-qualifiers of the '*this' object. |
| 901 | // The object referred to by '*this' starts out with the cv-qualifiers of its |
| 902 | // member function. We then start with the innermost lambda and iterate |
| 903 | // outward checking to see if any lambda performs a by-copy capture of '*this' |
| 904 | // - and if so, any nested lambda must respect the 'constness' of that |
| 905 | // capturing lamdbda's call operator. |
| 906 | // |
| 907 | |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 908 | // Since the FunctionScopeInfo stack is representative of the lexical |
| 909 | // nesting of the lambda expressions during initial parsing (and is the best |
| 910 | // place for querying information about captures about lambdas that are |
| 911 | // partially processed) and perhaps during instantiation of function templates |
| 912 | // that contain lambda expressions that need to be transformed BUT not |
| 913 | // necessarily during instantiation of a nested generic lambda's function call |
| 914 | // operator (which might even be instantiated at the end of the TU) - at which |
| 915 | // time the DeclContext tree is mature enough to query capture information |
| 916 | // reliably - we use a two pronged approach to walk through all the lexically |
| 917 | // enclosing lambda expressions: |
| 918 | // |
| 919 | // 1) Climb down the FunctionScopeInfo stack as long as each item represents |
| 920 | // a Lambda (i.e. LambdaScopeInfo) AND each LSI's 'closure-type' is lexically |
| 921 | // enclosed by the call-operator of the LSI below it on the stack (while |
| 922 | // tracking the enclosing DC for step 2 if needed). Note the topmost LSI on |
| 923 | // the stack represents the innermost lambda. |
| 924 | // |
| 925 | // 2) If we run out of enclosing LSI's, check if the enclosing DeclContext |
| 926 | // represents a lambda's call operator. If it does, we must be instantiating |
| 927 | // a generic lambda's call operator (represented by the Current LSI, and |
| 928 | // should be the only scenario where an inconsistency between the LSI and the |
| 929 | // DeclContext should occur), so climb out the DeclContexts if they |
| 930 | // represent lambdas, while querying the corresponding closure types |
| 931 | // regarding capture information. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 932 | |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 933 | // 1) Climb down the function scope info stack. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 934 | for (int I = FunctionScopes.size(); |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 935 | I-- && isa<LambdaScopeInfo>(FunctionScopes[I]) && |
| 936 | (!CurLSI || !CurLSI->Lambda || CurLSI->Lambda->getDeclContext() == |
| 937 | cast<LambdaScopeInfo>(FunctionScopes[I])->CallOperator); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 938 | CurDC = getLambdaAwareParentOfDeclContext(CurDC)) { |
| 939 | CurLSI = cast<LambdaScopeInfo>(FunctionScopes[I]); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 940 | |
| 941 | if (!CurLSI->isCXXThisCaptured()) |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 942 | continue; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 943 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 944 | auto C = CurLSI->getCXXThisCapture(); |
| 945 | |
| 946 | if (C.isCopyCapture()) { |
| 947 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 948 | if (CurLSI->CallOperator->isConst()) |
| 949 | ClassType.addConst(); |
| 950 | return ASTCtx.getPointerType(ClassType); |
| 951 | } |
| 952 | } |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 953 | |
| 954 | // 2) We've run out of ScopeInfos but check if CurDC is a lambda (which can |
| 955 | // happen during instantiation of its nested generic lambda call operator) |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 956 | if (isLambdaCallOperator(CurDC)) { |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 957 | assert(CurLSI && "While computing 'this' capture-type for a generic " |
| 958 | "lambda, we must have a corresponding LambdaScopeInfo"); |
| 959 | assert(isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && |
| 960 | "While computing 'this' capture-type for a generic lambda, when we " |
| 961 | "run out of enclosing LSI's, yet the enclosing DC is a " |
| 962 | "lambda-call-operator we must be (i.e. Current LSI) in a generic " |
| 963 | "lambda call oeprator"); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 964 | assert(CurDC == getLambdaAwareParentOfDeclContext(CurLSI->CallOperator)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 965 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 966 | auto IsThisCaptured = |
| 967 | [](CXXRecordDecl *Closure, bool &IsByCopy, bool &IsConst) { |
| 968 | IsConst = false; |
| 969 | IsByCopy = false; |
| 970 | for (auto &&C : Closure->captures()) { |
| 971 | if (C.capturesThis()) { |
| 972 | if (C.getCaptureKind() == LCK_StarThis) |
| 973 | IsByCopy = true; |
| 974 | if (Closure->getLambdaCallOperator()->isConst()) |
| 975 | IsConst = true; |
| 976 | return true; |
| 977 | } |
| 978 | } |
| 979 | return false; |
| 980 | }; |
| 981 | |
| 982 | bool IsByCopyCapture = false; |
| 983 | bool IsConstCapture = false; |
| 984 | CXXRecordDecl *Closure = cast<CXXRecordDecl>(CurDC->getParent()); |
| 985 | while (Closure && |
| 986 | IsThisCaptured(Closure, IsByCopyCapture, IsConstCapture)) { |
| 987 | if (IsByCopyCapture) { |
| 988 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 989 | if (IsConstCapture) |
| 990 | ClassType.addConst(); |
| 991 | return ASTCtx.getPointerType(ClassType); |
| 992 | } |
| 993 | Closure = isLambdaCallOperator(Closure->getParent()) |
| 994 | ? cast<CXXRecordDecl>(Closure->getParent()->getParent()) |
| 995 | : nullptr; |
| 996 | } |
| 997 | } |
| 998 | return ASTCtx.getPointerType(ClassType); |
| 999 | } |
| 1000 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1001 | QualType Sema::getCurrentThisType() { |
| 1002 | DeclContext *DC = getFunctionLevelDeclContext(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1003 | QualType ThisTy = CXXThisTypeOverride; |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 1004 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1005 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { |
| 1006 | if (method && method->isInstance()) |
| 1007 | ThisTy = method->getThisType(Context); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1008 | } |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1009 | |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 1010 | if (ThisTy.isNull() && isLambdaCallOperator(CurContext) && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1011 | inTemplateInstantiation()) { |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1012 | |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 1013 | assert(isa<CXXRecordDecl>(DC) && |
| 1014 | "Trying to get 'this' type from static method?"); |
| 1015 | |
| 1016 | // This is a lambda call operator that is being instantiated as a default |
| 1017 | // initializer. DC must point to the enclosing class type, so we can recover |
| 1018 | // the 'this' type from it. |
| 1019 | |
| 1020 | QualType ClassTy = Context.getTypeDeclType(cast<CXXRecordDecl>(DC)); |
| 1021 | // There are no cv-qualifiers for 'this' within default initializers, |
| 1022 | // per [expr.prim.general]p4. |
| 1023 | ThisTy = Context.getPointerType(ClassTy); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1024 | } |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1025 | |
| 1026 | // If we are within a lambda's call operator, the cv-qualifiers of 'this' |
| 1027 | // might need to be adjusted if the lambda or any of its enclosing lambda's |
| 1028 | // captures '*this' by copy. |
| 1029 | if (!ThisTy.isNull() && isLambdaCallOperator(CurContext)) |
| 1030 | return adjustCVQualifiersForCXXThisWithinLambda(FunctionScopes, ThisTy, |
| 1031 | CurContext, Context); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1032 | return ThisTy; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1035 | Sema::CXXThisScopeRAII::CXXThisScopeRAII(Sema &S, |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1036 | Decl *ContextDecl, |
| 1037 | unsigned CXXThisTypeQuals, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1038 | bool Enabled) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1039 | : S(S), OldCXXThisTypeOverride(S.CXXThisTypeOverride), Enabled(false) |
| 1040 | { |
| 1041 | if (!Enabled || !ContextDecl) |
| 1042 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1043 | |
| 1044 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1045 | if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(ContextDecl)) |
| 1046 | Record = Template->getTemplatedDecl(); |
| 1047 | else |
| 1048 | Record = cast<CXXRecordDecl>(ContextDecl); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1049 | |
Andrey Bokhanko | 67a4186 | 2016-05-26 10:06:01 +0000 | [diff] [blame] | 1050 | // We care only for CVR qualifiers here, so cut everything else. |
| 1051 | CXXThisTypeQuals &= Qualifiers::FastMask; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1052 | S.CXXThisTypeOverride |
| 1053 | = S.Context.getPointerType( |
| 1054 | S.Context.getRecordType(Record).withCVRQualifiers(CXXThisTypeQuals)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1056 | this->Enabled = true; |
| 1057 | } |
| 1058 | |
| 1059 | |
| 1060 | Sema::CXXThisScopeRAII::~CXXThisScopeRAII() { |
| 1061 | if (Enabled) { |
| 1062 | S.CXXThisTypeOverride = OldCXXThisTypeOverride; |
| 1063 | } |
| 1064 | } |
| 1065 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1066 | static Expr *captureThis(Sema &S, ASTContext &Context, RecordDecl *RD, |
| 1067 | QualType ThisTy, SourceLocation Loc, |
| 1068 | const bool ByCopy) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1069 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1070 | QualType AdjustedThisTy = ThisTy; |
| 1071 | // The type of the corresponding data member (not a 'this' pointer if 'by |
| 1072 | // copy'). |
| 1073 | QualType CaptureThisFieldTy = ThisTy; |
| 1074 | if (ByCopy) { |
| 1075 | // If we are capturing the object referred to by '*this' by copy, ignore any |
| 1076 | // cv qualifiers inherited from the type of the member function for the type |
| 1077 | // of the closure-type's corresponding data member and any use of 'this'. |
| 1078 | CaptureThisFieldTy = ThisTy->getPointeeType(); |
| 1079 | CaptureThisFieldTy.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 1080 | AdjustedThisTy = Context.getPointerType(CaptureThisFieldTy); |
| 1081 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1082 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1083 | FieldDecl *Field = FieldDecl::Create( |
| 1084 | Context, RD, Loc, Loc, nullptr, CaptureThisFieldTy, |
| 1085 | Context.getTrivialTypeSourceInfo(CaptureThisFieldTy, Loc), nullptr, false, |
| 1086 | ICIS_NoInit); |
| 1087 | |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1088 | Field->setImplicit(true); |
| 1089 | Field->setAccess(AS_private); |
| 1090 | RD->addDecl(Field); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1091 | Expr *This = |
| 1092 | new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit*/ true); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1093 | if (ByCopy) { |
| 1094 | Expr *StarThis = S.CreateBuiltinUnaryOp(Loc, |
| 1095 | UO_Deref, |
| 1096 | This).get(); |
| 1097 | InitializedEntity Entity = InitializedEntity::InitializeLambdaCapture( |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1098 | nullptr, CaptureThisFieldTy, Loc); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1099 | InitializationKind InitKind = InitializationKind::CreateDirect(Loc, Loc, Loc); |
| 1100 | InitializationSequence Init(S, Entity, InitKind, StarThis); |
| 1101 | ExprResult ER = Init.Perform(S, Entity, InitKind, StarThis); |
| 1102 | if (ER.isInvalid()) return nullptr; |
| 1103 | return ER.get(); |
| 1104 | } |
| 1105 | return This; |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1108 | bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit, |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1109 | bool BuildAndDiagnose, const unsigned *const FunctionScopeIndexToStopAt, |
| 1110 | const bool ByCopy) { |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1111 | // We don't need to capture this in an unevaluated context. |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1112 | if (isUnevaluatedContext() && !Explicit) |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1113 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1114 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1115 | assert((!ByCopy || Explicit) && "cannot implicitly capture *this by value"); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1116 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1117 | const unsigned MaxFunctionScopesIndex = FunctionScopeIndexToStopAt ? |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1118 | *FunctionScopeIndexToStopAt : FunctionScopes.size() - 1; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1119 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1120 | // Check that we can capture the *enclosing object* (referred to by '*this') |
| 1121 | // by the capturing-entity/closure (lambda/block/etc) at |
| 1122 | // MaxFunctionScopesIndex-deep on the FunctionScopes stack. |
| 1123 | |
| 1124 | // Note: The *enclosing object* can only be captured by-value by a |
| 1125 | // closure that is a lambda, using the explicit notation: |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1126 | // [*this] { ... }. |
| 1127 | // Every other capture of the *enclosing object* results in its by-reference |
| 1128 | // capture. |
| 1129 | |
| 1130 | // For a closure 'L' (at MaxFunctionScopesIndex in the FunctionScopes |
| 1131 | // stack), we can capture the *enclosing object* only if: |
| 1132 | // - 'L' has an explicit byref or byval capture of the *enclosing object* |
| 1133 | // - or, 'L' has an implicit capture. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1134 | // AND |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1135 | // -- there is no enclosing closure |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1136 | // -- or, there is some enclosing closure 'E' that has already captured the |
| 1137 | // *enclosing object*, and every intervening closure (if any) between 'E' |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1138 | // and 'L' can implicitly capture the *enclosing object*. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1139 | // -- or, every enclosing closure can implicitly capture the |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1140 | // *enclosing object* |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1141 | |
| 1142 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1143 | unsigned NumCapturingClosures = 0; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1144 | for (unsigned idx = MaxFunctionScopesIndex; idx != 0; idx--) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1145 | if (CapturingScopeInfo *CSI = |
| 1146 | dyn_cast<CapturingScopeInfo>(FunctionScopes[idx])) { |
| 1147 | if (CSI->CXXThisCaptureIndex != 0) { |
| 1148 | // 'this' is already being captured; there isn't anything more to do. |
Malcolm Parsons | 87a0362 | 2017-01-13 15:01:06 +0000 | [diff] [blame] | 1149 | CSI->Captures[CSI->CXXThisCaptureIndex - 1].markUsed(BuildAndDiagnose); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1150 | break; |
| 1151 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1152 | LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI); |
| 1153 | if (LSI && isGenericLambdaCallOperatorSpecialization(LSI->CallOperator)) { |
| 1154 | // This context can't implicitly capture 'this'; fail out. |
| 1155 | if (BuildAndDiagnose) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1156 | Diag(Loc, diag::err_this_capture) |
| 1157 | << (Explicit && idx == MaxFunctionScopesIndex); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1158 | return true; |
| 1159 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1160 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref || |
Douglas Gregor | a1bffa2 | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 1161 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval || |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 1162 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block || |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1163 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_CapturedRegion || |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1164 | (Explicit && idx == MaxFunctionScopesIndex)) { |
| 1165 | // Regarding (Explicit && idx == MaxFunctionScopesIndex): only the first |
| 1166 | // iteration through can be an explicit capture, all enclosing closures, |
| 1167 | // if any, must perform implicit captures. |
| 1168 | |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 1169 | // This closure can capture 'this'; continue looking upwards. |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1170 | NumCapturingClosures++; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1171 | continue; |
| 1172 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1173 | // This context can't implicitly capture 'this'; fail out. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1174 | if (BuildAndDiagnose) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1175 | Diag(Loc, diag::err_this_capture) |
| 1176 | << (Explicit && idx == MaxFunctionScopesIndex); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1177 | return true; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1178 | } |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1179 | break; |
| 1180 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1181 | if (!BuildAndDiagnose) return false; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1182 | |
| 1183 | // If we got here, then the closure at MaxFunctionScopesIndex on the |
| 1184 | // FunctionScopes stack, can capture the *enclosing object*, so capture it |
| 1185 | // (including implicit by-reference captures in any enclosing closures). |
| 1186 | |
| 1187 | // In the loop below, respect the ByCopy flag only for the closure requesting |
| 1188 | // 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] | 1189 | // all enclosing closure's up to NumCapturingClosures (since they must be |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1190 | // implicitly capturing the *enclosing object* by reference (see loop |
| 1191 | // above)). |
| 1192 | assert((!ByCopy || |
| 1193 | dyn_cast<LambdaScopeInfo>(FunctionScopes[MaxFunctionScopesIndex])) && |
| 1194 | "Only a lambda can capture the enclosing object (referred to by " |
| 1195 | "*this) by copy"); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1196 | // FIXME: We need to delay this marking in PotentiallyPotentiallyEvaluated |
| 1197 | // contexts. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1198 | QualType ThisTy = getCurrentThisType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1199 | for (unsigned idx = MaxFunctionScopesIndex; NumCapturingClosures; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1200 | --idx, --NumCapturingClosures) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1201 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1202 | Expr *ThisExpr = nullptr; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1203 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1204 | if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI)) { |
| 1205 | // For lambda expressions, build a field and an initializing expression, |
| 1206 | // and capture the *enclosing object* by copy only if this is the first |
| 1207 | // iteration. |
| 1208 | ThisExpr = captureThis(*this, Context, LSI->Lambda, ThisTy, Loc, |
| 1209 | ByCopy && idx == MaxFunctionScopesIndex); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1210 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1211 | } else if (CapturedRegionScopeInfo *RSI |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1212 | = dyn_cast<CapturedRegionScopeInfo>(FunctionScopes[idx])) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1213 | ThisExpr = |
| 1214 | captureThis(*this, Context, RSI->TheRecordDecl, ThisTy, Loc, |
| 1215 | false/*ByCopy*/); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1216 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1217 | bool isNested = NumCapturingClosures > 1; |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1218 | CSI->addThisCapture(isNested, Loc, ThisExpr, ByCopy); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1219 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1220 | return false; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1223 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1224 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 1225 | /// is a non-lvalue expression whose value is the address of the object for |
| 1226 | /// which the function is called. |
| 1227 | |
Douglas Gregor | 09deffa | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 1228 | QualType ThisTy = getCurrentThisType(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1229 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1230 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1231 | CheckCXXThisCapture(Loc); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1232 | return new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1233 | } |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1234 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1235 | bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) { |
| 1236 | // If we're outside the body of a member function, then we'll have a specified |
| 1237 | // type for 'this'. |
| 1238 | if (CXXThisTypeOverride.isNull()) |
| 1239 | return false; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1240 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1241 | // Determine whether we're looking into a class that's currently being |
| 1242 | // defined. |
| 1243 | CXXRecordDecl *Class = BaseType->getAsCXXRecordDecl(); |
| 1244 | return Class && Class->isBeingDefined(); |
| 1245 | } |
| 1246 | |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1247 | /// Parse construction of a specified type. |
| 1248 | /// Can be interpreted either as function-style casting ("int(x)") |
| 1249 | /// or class type construction ("ClassType(x,y,z)") |
| 1250 | /// or creation of a value-initialized type ("int()"). |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1251 | ExprResult |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1252 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1253 | SourceLocation LParenOrBraceLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1254 | MultiExprArg exprs, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1255 | SourceLocation RParenOrBraceLoc, |
| 1256 | bool ListInitialization) { |
Douglas Gregor | 7df89f5 | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 1257 | if (!TypeRep) |
| 1258 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1259 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1260 | TypeSourceInfo *TInfo; |
| 1261 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 1262 | if (!TInfo) |
| 1263 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1264 | |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1265 | auto Result = BuildCXXTypeConstructExpr(TInfo, LParenOrBraceLoc, exprs, |
| 1266 | RParenOrBraceLoc, ListInitialization); |
Richard Smith | b8c414c | 2016-06-30 20:24:30 +0000 | [diff] [blame] | 1267 | // Avoid creating a non-type-dependent expression that contains typos. |
| 1268 | // Non-type-dependent expressions are liable to be discarded without |
| 1269 | // checking for embedded typos. |
| 1270 | if (!Result.isInvalid() && Result.get()->isInstantiationDependent() && |
| 1271 | !Result.get()->isTypeDependent()) |
| 1272 | Result = CorrectDelayedTyposInExpr(Result.get()); |
| 1273 | return Result; |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1276 | ExprResult |
| 1277 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1278 | SourceLocation LParenOrBraceLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1279 | MultiExprArg Exprs, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1280 | SourceLocation RParenOrBraceLoc, |
| 1281 | bool ListInitialization) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1282 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1283 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1284 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1285 | if (Ty->isDependentType() || CallExpr::hasAnyTypeDependentArguments(Exprs)) { |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1286 | // FIXME: CXXUnresolvedConstructExpr does not model list-initialization |
| 1287 | // directly. We work around this by dropping the locations of the braces. |
| 1288 | SourceRange Locs = ListInitialization |
| 1289 | ? SourceRange() |
| 1290 | : SourceRange(LParenOrBraceLoc, RParenOrBraceLoc); |
| 1291 | return CXXUnresolvedConstructExpr::Create(Context, TInfo, Locs.getBegin(), |
| 1292 | Exprs, Locs.getEnd()); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1295 | assert((!ListInitialization || |
| 1296 | (Exprs.size() == 1 && isa<InitListExpr>(Exprs[0]))) && |
| 1297 | "List initialization must have initializer list as expression."); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1298 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenOrBraceLoc); |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1299 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1300 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 1301 | InitializationKind Kind = |
| 1302 | Exprs.size() |
| 1303 | ? ListInitialization |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1304 | ? InitializationKind::CreateDirectList( |
| 1305 | TyBeginLoc, LParenOrBraceLoc, RParenOrBraceLoc) |
| 1306 | : InitializationKind::CreateDirect(TyBeginLoc, LParenOrBraceLoc, |
| 1307 | RParenOrBraceLoc) |
| 1308 | : InitializationKind::CreateValue(TyBeginLoc, LParenOrBraceLoc, |
| 1309 | RParenOrBraceLoc); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1310 | |
| 1311 | // C++1z [expr.type.conv]p1: |
| 1312 | // If the type is a placeholder for a deduced class type, [...perform class |
| 1313 | // template argument deduction...] |
| 1314 | DeducedType *Deduced = Ty->getContainedDeducedType(); |
| 1315 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
| 1316 | Ty = DeduceTemplateSpecializationFromInitializer(TInfo, Entity, |
| 1317 | Kind, Exprs); |
| 1318 | if (Ty.isNull()) |
| 1319 | return ExprError(); |
| 1320 | Entity = InitializedEntity::InitializeTemporary(TInfo, Ty); |
| 1321 | } |
| 1322 | |
Douglas Gregor | dd04d33 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 1323 | // C++ [expr.type.conv]p1: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1324 | // If the expression list is a parenthesized single expression, the type |
| 1325 | // conversion expression is equivalent (in definedness, and if defined in |
| 1326 | // meaning) to the corresponding cast expression. |
| 1327 | if (Exprs.size() == 1 && !ListInitialization && |
| 1328 | !isa<InitListExpr>(Exprs[0])) { |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1329 | Expr *Arg = Exprs[0]; |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1330 | return BuildCXXFunctionalCastExpr(TInfo, Ty, LParenOrBraceLoc, Arg, |
| 1331 | RParenOrBraceLoc); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1334 | // 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] | 1335 | QualType ElemTy = Ty; |
| 1336 | if (Ty->isArrayType()) { |
| 1337 | if (!ListInitialization) |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1338 | return ExprError(Diag(TyBeginLoc, diag::err_value_init_for_array_type) |
| 1339 | << FullRange); |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1340 | ElemTy = Context.getBaseElementType(Ty); |
| 1341 | } |
| 1342 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1343 | // There doesn't seem to be an explicit rule against this but sanity demands |
| 1344 | // we only construct objects with object types. |
| 1345 | if (Ty->isFunctionType()) |
| 1346 | return ExprError(Diag(TyBeginLoc, diag::err_init_for_function_type) |
| 1347 | << Ty << FullRange); |
David Majnemer | 7eddcff | 2015-09-14 07:05:00 +0000 | [diff] [blame] | 1348 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1349 | // C++17 [expr.type.conv]p2: |
| 1350 | // If the type is cv void and the initializer is (), the expression is a |
| 1351 | // prvalue of the specified type that performs no initialization. |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1352 | if (!Ty->isVoidType() && |
| 1353 | RequireCompleteType(TyBeginLoc, ElemTy, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1354 | diag::err_invalid_incomplete_type_use, FullRange)) |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1355 | return ExprError(); |
| 1356 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1357 | // Otherwise, the expression is a prvalue of the specified type whose |
| 1358 | // result object is direct-initialized (11.6) with the initializer. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1359 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs); |
| 1360 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Exprs); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1361 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1362 | if (Result.isInvalid()) |
Richard Smith | 9006190 | 2013-09-23 02:20:00 +0000 | [diff] [blame] | 1363 | return Result; |
| 1364 | |
| 1365 | Expr *Inner = Result.get(); |
| 1366 | if (CXXBindTemporaryExpr *BTE = dyn_cast_or_null<CXXBindTemporaryExpr>(Inner)) |
| 1367 | Inner = BTE->getSubExpr(); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1368 | if (!isa<CXXTemporaryObjectExpr>(Inner) && |
| 1369 | !isa<CXXScalarValueInitExpr>(Inner)) { |
Richard Smith | 1ae689c | 2015-01-28 22:06:01 +0000 | [diff] [blame] | 1370 | // If we created a CXXTemporaryObjectExpr, that node also represents the |
| 1371 | // functional cast. Otherwise, create an explicit cast to represent |
| 1372 | // the syntactic form of a functional-style cast that was used here. |
| 1373 | // |
| 1374 | // FIXME: Creating a CXXFunctionalCastExpr around a CXXConstructExpr |
| 1375 | // would give a more consistent AST representation than using a |
| 1376 | // CXXTemporaryObjectExpr. It's also weird that the functional cast |
| 1377 | // is sometimes handled by initialization and sometimes not. |
Richard Smith | 9006190 | 2013-09-23 02:20:00 +0000 | [diff] [blame] | 1378 | QualType ResultType = Result.get()->getType(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1379 | SourceRange Locs = ListInitialization |
| 1380 | ? SourceRange() |
| 1381 | : SourceRange(LParenOrBraceLoc, RParenOrBraceLoc); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1382 | Result = CXXFunctionalCastExpr::Create( |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1383 | Context, ResultType, Expr::getValueKindForType(Ty), TInfo, CK_NoOp, |
| 1384 | Result.get(), /*Path=*/nullptr, Locs.getBegin(), Locs.getEnd()); |
Sebastian Redl | 2b80af4 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1387 | return Result; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1388 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1389 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1390 | /// \brief Determine whether the given function is a non-placement |
| 1391 | /// deallocation function. |
| 1392 | static bool isNonPlacementDeallocationFunction(Sema &S, FunctionDecl *FD) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1393 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 1394 | return Method->isUsualDeallocationFunction(); |
| 1395 | |
| 1396 | if (FD->getOverloadedOperator() != OO_Delete && |
| 1397 | FD->getOverloadedOperator() != OO_Array_Delete) |
| 1398 | return false; |
| 1399 | |
| 1400 | unsigned UsualParams = 1; |
| 1401 | |
| 1402 | if (S.getLangOpts().SizedDeallocation && UsualParams < FD->getNumParams() && |
| 1403 | S.Context.hasSameUnqualifiedType( |
| 1404 | FD->getParamDecl(UsualParams)->getType(), |
| 1405 | S.Context.getSizeType())) |
| 1406 | ++UsualParams; |
| 1407 | |
| 1408 | if (S.getLangOpts().AlignedAllocation && UsualParams < FD->getNumParams() && |
| 1409 | S.Context.hasSameUnqualifiedType( |
| 1410 | FD->getParamDecl(UsualParams)->getType(), |
| 1411 | S.Context.getTypeDeclType(S.getStdAlignValT()))) |
| 1412 | ++UsualParams; |
| 1413 | |
| 1414 | return UsualParams == FD->getNumParams(); |
| 1415 | } |
| 1416 | |
| 1417 | namespace { |
| 1418 | struct UsualDeallocFnInfo { |
| 1419 | UsualDeallocFnInfo() : Found(), FD(nullptr) {} |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1420 | UsualDeallocFnInfo(Sema &S, DeclAccessPair Found) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1421 | : Found(Found), FD(dyn_cast<FunctionDecl>(Found->getUnderlyingDecl())), |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1422 | Destroying(false), HasSizeT(false), HasAlignValT(false), |
| 1423 | CUDAPref(Sema::CFP_Native) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1424 | // A function template declaration is never a usual deallocation function. |
| 1425 | if (!FD) |
| 1426 | return; |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1427 | unsigned NumBaseParams = 1; |
| 1428 | if (FD->isDestroyingOperatorDelete()) { |
| 1429 | Destroying = true; |
| 1430 | ++NumBaseParams; |
| 1431 | } |
| 1432 | if (FD->getNumParams() == NumBaseParams + 2) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1433 | HasAlignValT = HasSizeT = true; |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1434 | else if (FD->getNumParams() == NumBaseParams + 1) { |
| 1435 | HasSizeT = FD->getParamDecl(NumBaseParams)->getType()->isIntegerType(); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1436 | HasAlignValT = !HasSizeT; |
| 1437 | } |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1438 | |
| 1439 | // In CUDA, determine how much we'd like / dislike to call this. |
| 1440 | if (S.getLangOpts().CUDA) |
| 1441 | if (auto *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 1442 | CUDAPref = S.IdentifyCUDAPreference(Caller, FD); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | operator bool() const { return FD; } |
| 1446 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1447 | bool isBetterThan(const UsualDeallocFnInfo &Other, bool WantSize, |
| 1448 | bool WantAlign) const { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1449 | // C++ P0722: |
| 1450 | // A destroying operator delete is preferred over a non-destroying |
| 1451 | // operator delete. |
| 1452 | if (Destroying != Other.Destroying) |
| 1453 | return Destroying; |
| 1454 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1455 | // C++17 [expr.delete]p10: |
| 1456 | // If the type has new-extended alignment, a function with a parameter |
| 1457 | // of type std::align_val_t is preferred; otherwise a function without |
| 1458 | // such a parameter is preferred |
| 1459 | if (HasAlignValT != Other.HasAlignValT) |
| 1460 | return HasAlignValT == WantAlign; |
| 1461 | |
| 1462 | if (HasSizeT != Other.HasSizeT) |
| 1463 | return HasSizeT == WantSize; |
| 1464 | |
| 1465 | // Use CUDA call preference as a tiebreaker. |
| 1466 | return CUDAPref > Other.CUDAPref; |
| 1467 | } |
| 1468 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1469 | DeclAccessPair Found; |
| 1470 | FunctionDecl *FD; |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1471 | bool Destroying, HasSizeT, HasAlignValT; |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1472 | Sema::CUDAFunctionPreference CUDAPref; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1473 | }; |
| 1474 | } |
| 1475 | |
| 1476 | /// Determine whether a type has new-extended alignment. This may be called when |
| 1477 | /// the type is incomplete (for a delete-expression with an incomplete pointee |
| 1478 | /// type), in which case it will conservatively return false if the alignment is |
| 1479 | /// not known. |
| 1480 | static bool hasNewExtendedAlignment(Sema &S, QualType AllocType) { |
| 1481 | return S.getLangOpts().AlignedAllocation && |
| 1482 | S.getASTContext().getTypeAlignIfKnown(AllocType) > |
| 1483 | S.getASTContext().getTargetInfo().getNewAlign(); |
| 1484 | } |
| 1485 | |
| 1486 | /// Select the correct "usual" deallocation function to use from a selection of |
| 1487 | /// deallocation functions (either global or class-scope). |
| 1488 | static UsualDeallocFnInfo resolveDeallocationOverload( |
| 1489 | Sema &S, LookupResult &R, bool WantSize, bool WantAlign, |
| 1490 | llvm::SmallVectorImpl<UsualDeallocFnInfo> *BestFns = nullptr) { |
| 1491 | UsualDeallocFnInfo Best; |
| 1492 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1493 | for (auto I = R.begin(), E = R.end(); I != E; ++I) { |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1494 | UsualDeallocFnInfo Info(S, I.getPair()); |
| 1495 | if (!Info || !isNonPlacementDeallocationFunction(S, Info.FD) || |
| 1496 | Info.CUDAPref == Sema::CFP_Never) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1497 | continue; |
| 1498 | |
| 1499 | if (!Best) { |
| 1500 | Best = Info; |
| 1501 | if (BestFns) |
| 1502 | BestFns->push_back(Info); |
| 1503 | continue; |
| 1504 | } |
| 1505 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1506 | if (Best.isBetterThan(Info, WantSize, WantAlign)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1507 | continue; |
| 1508 | |
| 1509 | // If more than one preferred function is found, all non-preferred |
| 1510 | // functions are eliminated from further consideration. |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1511 | if (BestFns && Info.isBetterThan(Best, WantSize, WantAlign)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1512 | BestFns->clear(); |
| 1513 | |
| 1514 | Best = Info; |
| 1515 | if (BestFns) |
| 1516 | BestFns->push_back(Info); |
| 1517 | } |
| 1518 | |
| 1519 | return Best; |
| 1520 | } |
| 1521 | |
| 1522 | /// Determine whether a given type is a class for which 'delete[]' would call |
| 1523 | /// a member 'operator delete[]' with a 'size_t' parameter. This implies that |
| 1524 | /// we need to store the array size (even if the type is |
| 1525 | /// trivially-destructible). |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1526 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
| 1527 | QualType allocType) { |
| 1528 | const RecordType *record = |
| 1529 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 1530 | if (!record) return false; |
| 1531 | |
| 1532 | // Try to find an operator delete[] in class scope. |
| 1533 | |
| 1534 | DeclarationName deleteName = |
| 1535 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
| 1536 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
| 1537 | S.LookupQualifiedName(ops, record->getDecl()); |
| 1538 | |
| 1539 | // We're just doing this for information. |
| 1540 | ops.suppressDiagnostics(); |
| 1541 | |
| 1542 | // Very likely: there's no operator delete[]. |
| 1543 | if (ops.empty()) return false; |
| 1544 | |
| 1545 | // If it's ambiguous, it should be illegal to call operator delete[] |
| 1546 | // on this thing, so it doesn't matter if we allocate extra space or not. |
| 1547 | if (ops.isAmbiguous()) return false; |
| 1548 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1549 | // C++17 [expr.delete]p10: |
| 1550 | // If the deallocation functions have class scope, the one without a |
| 1551 | // parameter of type std::size_t is selected. |
| 1552 | auto Best = resolveDeallocationOverload( |
| 1553 | S, ops, /*WantSize*/false, |
| 1554 | /*WantAlign*/hasNewExtendedAlignment(S, allocType)); |
| 1555 | return Best && Best.HasSizeT; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1556 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1557 | |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1558 | /// \brief Parsed a C++ 'new' expression (C++ 5.3.4). |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 1559 | /// |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1560 | /// E.g.: |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1561 | /// @code new (memory) int[size][4] @endcode |
| 1562 | /// or |
| 1563 | /// @code ::new Foo(23, "hello") @endcode |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1564 | /// |
| 1565 | /// \param StartLoc The first location of the expression. |
| 1566 | /// \param UseGlobal True if 'new' was prefixed with '::'. |
| 1567 | /// \param PlacementLParen Opening paren of the placement arguments. |
| 1568 | /// \param PlacementArgs Placement new arguments. |
| 1569 | /// \param PlacementRParen Closing paren of the placement arguments. |
| 1570 | /// \param TypeIdParens If the type is in parens, the source range. |
| 1571 | /// \param D The type to be allocated, as well as array dimensions. |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 1572 | /// \param Initializer The initializing expression or initializer-list, or null |
| 1573 | /// if there is none. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1574 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1575 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1576 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1577 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1578 | Declarator &D, Expr *Initializer) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1579 | Expr *ArraySize = nullptr; |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1580 | // If the specified type is an array, unwrap it and save the expression. |
| 1581 | if (D.getNumTypeObjects() > 0 && |
| 1582 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1583 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
Richard Smith | bfbff07 | 2017-02-10 22:35:37 +0000 | [diff] [blame] | 1584 | if (D.getDeclSpec().hasAutoTypeSpec()) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1585 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
| 1586 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1587 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1588 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 1589 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1590 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1591 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 1592 | << D.getSourceRange()); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1593 | |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1594 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1595 | D.DropFirstTypeObject(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1598 | // Every dimension shall be of constant size. |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1599 | if (ArraySize) { |
| 1600 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1601 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 1602 | break; |
| 1603 | |
| 1604 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 1605 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1606 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent()) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1607 | if (getLangOpts().CPlusPlus14) { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1608 | // C++1y [expr.new]p6: Every constant-expression in a noptr-new-declarator |
| 1609 | // shall be a converted constant expression (5.19) of type std::size_t |
| 1610 | // and shall evaluate to a strictly positive value. |
| 1611 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
| 1612 | assert(IntWidth && "Builtin type of size 0?"); |
| 1613 | llvm::APSInt Value(IntWidth); |
| 1614 | Array.NumElts |
| 1615 | = CheckConvertedConstantExpression(NumElts, Context.getSizeType(), Value, |
| 1616 | CCEK_NewExpr) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1617 | .get(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1618 | } else { |
| 1619 | Array.NumElts |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1620 | = VerifyIntegerConstantExpression(NumElts, nullptr, |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1621 | diag::err_new_array_nonconst) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1622 | .get(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1623 | } |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1624 | if (!Array.NumElts) |
| 1625 | return ExprError(); |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | } |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1630 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1631 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/nullptr); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1632 | QualType AllocType = TInfo->getType(); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1633 | if (D.isInvalidType()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1634 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1635 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1636 | SourceRange DirectInitRange; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1637 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1638 | DirectInitRange = List->getSourceRange(); |
| 1639 | |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1640 | return BuildCXXNew(SourceRange(StartLoc, D.getLocEnd()), UseGlobal, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1641 | PlacementLParen, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1642 | PlacementArgs, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1643 | PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1644 | TypeIdParens, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1646 | TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1647 | ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1648 | DirectInitRange, |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1649 | Initializer); |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1652 | static bool isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style, |
| 1653 | Expr *Init) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1654 | if (!Init) |
| 1655 | return true; |
Sebastian Redl | eb54f08 | 2012-02-17 08:42:32 +0000 | [diff] [blame] | 1656 | if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) |
| 1657 | return PLE->getNumExprs() == 0; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1658 | if (isa<ImplicitValueInitExpr>(Init)) |
| 1659 | return true; |
| 1660 | else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) |
| 1661 | return !CCE->isListInitialization() && |
| 1662 | CCE->getConstructor()->isDefaultConstructor(); |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1663 | else if (Style == CXXNewExpr::ListInit) { |
| 1664 | assert(isa<InitListExpr>(Init) && |
| 1665 | "Shouldn't create list CXXConstructExprs for arrays."); |
| 1666 | return true; |
| 1667 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1668 | return false; |
| 1669 | } |
| 1670 | |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 1671 | // Emit a diagnostic if an aligned allocation/deallocation function that is not |
| 1672 | // implemented in the standard library is selected. |
| 1673 | static void diagnoseUnavailableAlignedAllocation(const FunctionDecl &FD, |
| 1674 | SourceLocation Loc, bool IsDelete, |
| 1675 | Sema &S) { |
| 1676 | if (!S.getLangOpts().AlignedAllocationUnavailable) |
| 1677 | return; |
| 1678 | |
| 1679 | // Return if there is a definition. |
| 1680 | if (FD.isDefined()) |
| 1681 | return; |
| 1682 | |
| 1683 | bool IsAligned = false; |
| 1684 | if (FD.isReplaceableGlobalAllocationFunction(&IsAligned) && IsAligned) { |
Akira Hatanaka | 3e40c30 | 2017-07-19 17:17:50 +0000 | [diff] [blame] | 1685 | const llvm::Triple &T = S.getASTContext().getTargetInfo().getTriple(); |
| 1686 | StringRef OSName = AvailabilityAttr::getPlatformNameSourceSpelling( |
| 1687 | S.getASTContext().getTargetInfo().getPlatformName()); |
| 1688 | |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 1689 | S.Diag(Loc, diag::warn_aligned_allocation_unavailable) |
Akira Hatanaka | 3e40c30 | 2017-07-19 17:17:50 +0000 | [diff] [blame] | 1690 | << IsDelete << FD.getType().getAsString() << OSName |
| 1691 | << alignedAllocMinVersion(T.getOS()).getAsString(); |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 1692 | S.Diag(Loc, diag::note_silence_unligned_allocation_unavailable); |
| 1693 | } |
| 1694 | } |
| 1695 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1696 | ExprResult |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1697 | Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1698 | SourceLocation PlacementLParen, |
| 1699 | MultiExprArg PlacementArgs, |
| 1700 | SourceLocation PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1701 | SourceRange TypeIdParens, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1702 | QualType AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1703 | TypeSourceInfo *AllocTypeInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1704 | Expr *ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1705 | SourceRange DirectInitRange, |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1706 | Expr *Initializer) { |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1707 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1708 | SourceLocation StartLoc = Range.getBegin(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1709 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1710 | CXXNewExpr::InitializationStyle initStyle; |
| 1711 | if (DirectInitRange.isValid()) { |
| 1712 | assert(Initializer && "Have parens but no initializer."); |
| 1713 | initStyle = CXXNewExpr::CallInit; |
| 1714 | } else if (Initializer && isa<InitListExpr>(Initializer)) |
| 1715 | initStyle = CXXNewExpr::ListInit; |
| 1716 | else { |
| 1717 | assert((!Initializer || isa<ImplicitValueInitExpr>(Initializer) || |
| 1718 | isa<CXXConstructExpr>(Initializer)) && |
| 1719 | "Initializer expression that cannot have been implicitly created."); |
| 1720 | initStyle = CXXNewExpr::NoInit; |
| 1721 | } |
| 1722 | |
| 1723 | Expr **Inits = &Initializer; |
| 1724 | unsigned NumInits = Initializer ? 1 : 0; |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 1725 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) { |
| 1726 | assert(initStyle == CXXNewExpr::CallInit && "paren init for non-call init"); |
| 1727 | Inits = List->getExprs(); |
| 1728 | NumInits = List->getNumExprs(); |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1731 | // C++11 [expr.new]p15: |
| 1732 | // A new-expression that creates an object of type T initializes that |
| 1733 | // object as follows: |
| 1734 | InitializationKind Kind |
| 1735 | // - If the new-initializer is omitted, the object is default- |
| 1736 | // initialized (8.5); if no initialization is performed, |
| 1737 | // the object has indeterminate value |
| 1738 | = initStyle == CXXNewExpr::NoInit |
| 1739 | ? InitializationKind::CreateDefault(TypeRange.getBegin()) |
| 1740 | // - Otherwise, the new-initializer is interpreted according to the |
| 1741 | // initialization rules of 8.5 for direct-initialization. |
| 1742 | : initStyle == CXXNewExpr::ListInit |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1743 | ? InitializationKind::CreateDirectList(TypeRange.getBegin(), |
| 1744 | Initializer->getLocStart(), |
| 1745 | Initializer->getLocEnd()) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1746 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
| 1747 | DirectInitRange.getBegin(), |
| 1748 | DirectInitRange.getEnd()); |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1749 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1750 | // C++11 [dcl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 1751 | auto *Deduced = AllocType->getContainedDeducedType(); |
| 1752 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
| 1753 | if (ArraySize) |
| 1754 | return ExprError(Diag(ArraySize->getExprLoc(), |
| 1755 | diag::err_deduced_class_template_compound_type) |
| 1756 | << /*array*/ 2 << ArraySize->getSourceRange()); |
| 1757 | |
| 1758 | InitializedEntity Entity |
| 1759 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
| 1760 | AllocType = DeduceTemplateSpecializationFromInitializer( |
| 1761 | AllocTypeInfo, Entity, Kind, MultiExprArg(Inits, NumInits)); |
| 1762 | if (AllocType.isNull()) |
| 1763 | return ExprError(); |
| 1764 | } else if (Deduced) { |
Zhihao Yuan | 00c9dfd | 2017-12-11 18:29:54 +0000 | [diff] [blame] | 1765 | bool Braced = (initStyle == CXXNewExpr::ListInit); |
| 1766 | if (NumInits == 1) { |
| 1767 | if (auto p = dyn_cast_or_null<InitListExpr>(Inits[0])) { |
| 1768 | Inits = p->getInits(); |
| 1769 | NumInits = p->getNumInits(); |
| 1770 | Braced = true; |
| 1771 | } |
| 1772 | } |
| 1773 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1774 | if (initStyle == CXXNewExpr::NoInit || NumInits == 0) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1775 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
| 1776 | << AllocType << TypeRange); |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1777 | if (NumInits > 1) { |
| 1778 | Expr *FirstBad = Inits[1]; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1779 | return ExprError(Diag(FirstBad->getLocStart(), |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1780 | diag::err_auto_new_ctor_multiple_expressions) |
| 1781 | << AllocType << TypeRange); |
| 1782 | } |
Zhihao Yuan | 00c9dfd | 2017-12-11 18:29:54 +0000 | [diff] [blame] | 1783 | if (Braced && !getLangOpts().CPlusPlus17) |
| 1784 | Diag(Initializer->getLocStart(), diag::ext_auto_new_list_init) |
| 1785 | << AllocType << TypeRange; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1786 | Expr *Deduce = Inits[0]; |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1787 | QualType DeducedType; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 1788 | if (DeduceAutoType(AllocTypeInfo, Deduce, DeducedType) == DAR_Failed) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1789 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1790 | << AllocType << Deduce->getType() |
| 1791 | << TypeRange << Deduce->getSourceRange()); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1792 | if (DeducedType.isNull()) |
Richard Smith | 9647d3c | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 1793 | return ExprError(); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1794 | AllocType = DeducedType; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1795 | } |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1796 | |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1797 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 1798 | // typedef of an array type. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1799 | if (!ArraySize) { |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1800 | if (const ConstantArrayType *Array |
| 1801 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1802 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 1803 | Context.getSizeType(), |
| 1804 | TypeRange.getEnd()); |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1805 | AllocType = Array->getElementType(); |
| 1806 | } |
| 1807 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1809 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 1810 | return ExprError(); |
| 1811 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1812 | if (initStyle == CXXNewExpr::ListInit && |
| 1813 | isStdInitializerList(AllocType, nullptr)) { |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1814 | Diag(AllocTypeInfo->getTypeLoc().getBeginLoc(), |
| 1815 | diag::warn_dangling_std_initializer_list) |
Sebastian Redl | 73cfbeb | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1816 | << /*at end of FE*/0 << Inits[0]->getSourceRange(); |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1819 | // In ARC, infer 'retaining' for the allocated |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1820 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1821 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 1822 | AllocType->isObjCLifetimeType()) { |
| 1823 | AllocType = Context.getLifetimeQualifiedType(AllocType, |
| 1824 | AllocType->getObjCARCImplicitLifetime()); |
| 1825 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1826 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1827 | QualType ResultType = Context.getPointerType(AllocType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1828 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1829 | if (ArraySize && ArraySize->getType()->isNonOverloadPlaceholderType()) { |
| 1830 | ExprResult result = CheckPlaceholderExpr(ArraySize); |
| 1831 | if (result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1832 | ArraySize = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1833 | } |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 1834 | // C++98 5.3.4p6: "The expression in a direct-new-declarator shall have |
| 1835 | // integral or enumeration type with a non-negative value." |
| 1836 | // C++11 [expr.new]p6: The expression [...] shall be of integral or unscoped |
| 1837 | // enumeration type, or a class type for which a single non-explicit |
| 1838 | // conversion function to integral or unscoped enumeration type exists. |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1839 | // C++1y [expr.new]p6: The expression [...] is implicitly converted to |
Larisse Voufo | bf4aa57 | 2013-06-18 03:08:53 +0000 | [diff] [blame] | 1840 | // std::size_t. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1841 | llvm::Optional<uint64_t> KnownArraySize; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1842 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1843 | ExprResult ConvertedSize; |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1844 | if (getLangOpts().CPlusPlus14) { |
Alp Toker | 965f882 | 2013-11-27 05:22:15 +0000 | [diff] [blame] | 1845 | assert(Context.getTargetInfo().getIntWidth() && "Builtin type of size 0?"); |
| 1846 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1847 | ConvertedSize = PerformImplicitConversion(ArraySize, Context.getSizeType(), |
| 1848 | AA_Converting); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1849 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1850 | if (!ConvertedSize.isInvalid() && |
Larisse Voufo | bf4aa57 | 2013-06-18 03:08:53 +0000 | [diff] [blame] | 1851 | ArraySize->getType()->getAs<RecordType>()) |
Larisse Voufo | 9f380c5 | 2013-06-18 01:27:47 +0000 | [diff] [blame] | 1852 | // Diagnose the compatibility of this conversion. |
| 1853 | Diag(StartLoc, diag::warn_cxx98_compat_array_size_conversion) |
| 1854 | << ArraySize->getType() << 0 << "'size_t'"; |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1855 | } else { |
| 1856 | class SizeConvertDiagnoser : public ICEConvertDiagnoser { |
| 1857 | protected: |
| 1858 | Expr *ArraySize; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1859 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1860 | public: |
| 1861 | SizeConvertDiagnoser(Expr *ArraySize) |
| 1862 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false, false, false), |
| 1863 | ArraySize(ArraySize) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1864 | |
| 1865 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 1866 | QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1867 | return S.Diag(Loc, diag::err_array_size_not_integral) |
| 1868 | << S.getLangOpts().CPlusPlus11 << T; |
| 1869 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1870 | |
| 1871 | SemaDiagnosticBuilder diagnoseIncomplete( |
| 1872 | Sema &S, SourceLocation Loc, QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1873 | return S.Diag(Loc, diag::err_array_size_incomplete_type) |
| 1874 | << T << ArraySize->getSourceRange(); |
| 1875 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1876 | |
| 1877 | SemaDiagnosticBuilder diagnoseExplicitConv( |
| 1878 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1879 | return S.Diag(Loc, diag::err_array_size_explicit_conversion) << T << ConvTy; |
| 1880 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1881 | |
| 1882 | SemaDiagnosticBuilder noteExplicitConv( |
| 1883 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1884 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
| 1885 | << ConvTy->isEnumeralType() << ConvTy; |
| 1886 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1887 | |
| 1888 | SemaDiagnosticBuilder diagnoseAmbiguous( |
| 1889 | Sema &S, SourceLocation Loc, QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1890 | return S.Diag(Loc, diag::err_array_size_ambiguous_conversion) << T; |
| 1891 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1892 | |
| 1893 | SemaDiagnosticBuilder noteAmbiguous( |
| 1894 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1895 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
| 1896 | << ConvTy->isEnumeralType() << ConvTy; |
| 1897 | } |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1898 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1899 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
| 1900 | QualType T, |
| 1901 | QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1902 | return S.Diag(Loc, |
| 1903 | S.getLangOpts().CPlusPlus11 |
| 1904 | ? diag::warn_cxx98_compat_array_size_conversion |
| 1905 | : diag::ext_array_size_conversion) |
| 1906 | << T << ConvTy->isEnumeralType() << ConvTy; |
| 1907 | } |
| 1908 | } SizeDiagnoser(ArraySize); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1909 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1910 | ConvertedSize = PerformContextualImplicitConversion(StartLoc, ArraySize, |
| 1911 | SizeDiagnoser); |
| 1912 | } |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1913 | if (ConvertedSize.isInvalid()) |
| 1914 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1915 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1916 | ArraySize = ConvertedSize.get(); |
John McCall | 9b80c21 | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 1917 | QualType SizeType = ArraySize->getType(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1918 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1919 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1920 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1921 | |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1922 | // C++98 [expr.new]p7: |
| 1923 | // The expression in a direct-new-declarator shall have integral type |
| 1924 | // with a non-negative value. |
| 1925 | // |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1926 | // Let's see if this is a constant < 0. If so, we reject it out of hand, |
| 1927 | // per CWG1464. Otherwise, if it's not a constant, we must have an |
| 1928 | // unparenthesized array type. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1929 | if (!ArraySize->isValueDependent()) { |
| 1930 | llvm::APSInt Value; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1931 | // We've already performed any required implicit conversion to integer or |
| 1932 | // unscoped enumeration type. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1933 | // FIXME: Per CWG1464, we are required to check the value prior to |
| 1934 | // converting to size_t. This will never find a negative array size in |
| 1935 | // C++14 onwards, because Value is always unsigned here! |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1936 | if (ArraySize->isIntegerConstantExpr(Value, Context)) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1937 | if (Value.isSigned() && Value.isNegative()) { |
| 1938 | return ExprError(Diag(ArraySize->getLocStart(), |
| 1939 | diag::err_typecheck_negative_array_size) |
| 1940 | << ArraySize->getSourceRange()); |
| 1941 | } |
| 1942 | |
| 1943 | if (!AllocType->isDependentType()) { |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1944 | unsigned ActiveSizeBits = |
| 1945 | ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1946 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) |
| 1947 | return ExprError(Diag(ArraySize->getLocStart(), |
| 1948 | diag::err_array_too_large) |
| 1949 | << Value.toString(10) |
| 1950 | << ArraySize->getSourceRange()); |
Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1951 | } |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1952 | |
| 1953 | KnownArraySize = Value.getZExtValue(); |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1954 | } else if (TypeIdParens.isValid()) { |
| 1955 | // Can't have dynamic array size when the type-id is in parentheses. |
| 1956 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 1957 | << ArraySize->getSourceRange() |
| 1958 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 1959 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1960 | |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1961 | TypeIdParens = SourceRange(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1962 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1963 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1964 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 1965 | // Note that we do *not* convert the argument in any way. It can |
| 1966 | // be signed, larger than size_t, whatever. |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1967 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1968 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1969 | FunctionDecl *OperatorNew = nullptr; |
| 1970 | FunctionDecl *OperatorDelete = nullptr; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1971 | unsigned Alignment = |
| 1972 | AllocType->isDependentType() ? 0 : Context.getTypeAlign(AllocType); |
| 1973 | unsigned NewAlignment = Context.getTargetInfo().getNewAlign(); |
| 1974 | bool PassAlignment = getLangOpts().AlignedAllocation && |
| 1975 | Alignment > NewAlignment; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1976 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1977 | if (!AllocType->isDependentType() && |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 1978 | !Expr::hasAnyTypeDependentArguments(PlacementArgs) && |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1979 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1980 | SourceRange(PlacementLParen, PlacementRParen), |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1981 | UseGlobal, AllocType, ArraySize, PassAlignment, |
| 1982 | PlacementArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1983 | return ExprError(); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1984 | |
| 1985 | // If this is an array allocation, compute whether the usual array |
| 1986 | // deallocation function for the type has a size_t parameter. |
| 1987 | bool UsualArrayDeleteWantsSize = false; |
| 1988 | if (ArraySize && !AllocType->isDependentType()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1989 | UsualArrayDeleteWantsSize = |
| 1990 | doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1991 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1992 | SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 1eab66c | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1993 | if (OperatorNew) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1994 | const FunctionProtoType *Proto = |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 1995 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
| 1996 | VariadicCallType CallType = Proto->isVariadic() ? VariadicFunction |
| 1997 | : VariadicDoesNotApply; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1998 | |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 1999 | // We've already converted the placement args, just fill in any default |
| 2000 | // arguments. Skip the first parameter because we don't have a corresponding |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2001 | // argument. Skip the second parameter too if we're passing in the |
| 2002 | // alignment; we've already filled it in. |
| 2003 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, Proto, |
| 2004 | PassAlignment ? 2 : 1, PlacementArgs, |
| 2005 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 2006 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2007 | |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 2008 | if (!AllPlaceArgs.empty()) |
| 2009 | PlacementArgs = AllPlaceArgs; |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 2010 | |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 2011 | // FIXME: This is wrong: PlacementArgs misses out the first (size) argument. |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 2012 | DiagnoseSentinelCalls(OperatorNew, PlacementLParen, PlacementArgs); |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 2013 | |
| 2014 | // FIXME: Missing call to CheckFunctionCall or equivalent |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2015 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2016 | // Warn if the type is over-aligned and is being allocated by (unaligned) |
| 2017 | // global operator new. |
| 2018 | if (PlacementArgs.empty() && !PassAlignment && |
| 2019 | (OperatorNew->isImplicit() || |
| 2020 | (OperatorNew->getLocStart().isValid() && |
| 2021 | getSourceManager().isInSystemHeader(OperatorNew->getLocStart())))) { |
| 2022 | if (Alignment > NewAlignment) |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 2023 | Diag(StartLoc, diag::warn_overaligned_type) |
| 2024 | << AllocType |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2025 | << unsigned(Alignment / Context.getCharWidth()) |
| 2026 | << unsigned(NewAlignment / Context.getCharWidth()); |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 2027 | } |
| 2028 | } |
| 2029 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2030 | // Array 'new' can't have any initializers except empty parentheses. |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 2031 | // Initializer lists are also allowed, in C++11. Rely on the parser for the |
| 2032 | // dialect distinction. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2033 | if (ArraySize && !isLegalArrayNewInitializer(initStyle, Initializer)) { |
| 2034 | SourceRange InitRange(Inits[0]->getLocStart(), |
| 2035 | Inits[NumInits - 1]->getLocEnd()); |
| 2036 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 2037 | return ExprError(); |
Anders Carlsson | c6bb0e1 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 2040 | // If we can perform the initialization, and we've not already done so, |
| 2041 | // do it now. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2042 | if (!AllocType->isDependentType() && |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2043 | !Expr::hasAnyTypeDependentArguments( |
Richard Smith | c6abd96 | 2014-07-25 01:12:44 +0000 | [diff] [blame] | 2044 | llvm::makeArrayRef(Inits, NumInits))) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2045 | // The type we initialize is the complete type, including the array bound. |
| 2046 | QualType InitType; |
| 2047 | if (KnownArraySize) |
| 2048 | InitType = Context.getConstantArrayType( |
| 2049 | AllocType, llvm::APInt(Context.getTypeSize(Context.getSizeType()), |
| 2050 | *KnownArraySize), |
| 2051 | ArrayType::Normal, 0); |
| 2052 | else if (ArraySize) |
| 2053 | InitType = |
| 2054 | Context.getIncompleteArrayType(AllocType, ArrayType::Normal, 0); |
| 2055 | else |
| 2056 | InitType = AllocType; |
| 2057 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2058 | InitializedEntity Entity |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 2059 | = InitializedEntity::InitializeNew(StartLoc, InitType); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2060 | InitializationSequence InitSeq(*this, Entity, Kind, |
| 2061 | MultiExprArg(Inits, NumInits)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2062 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2063 | MultiExprArg(Inits, NumInits)); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2064 | if (FullInit.isInvalid()) |
| 2065 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2066 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2067 | // FullInit is our initializer; strip off CXXBindTemporaryExprs, because |
| 2068 | // we don't want the initialized object to be destructed. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2069 | // FIXME: We should not create these in the first place. |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2070 | if (CXXBindTemporaryExpr *Binder = |
| 2071 | dyn_cast_or_null<CXXBindTemporaryExpr>(FullInit.get())) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2072 | FullInit = Binder->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2073 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2074 | Initializer = FullInit.get(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2075 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2076 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2077 | // Mark the new and delete operators as referenced. |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2078 | if (OperatorNew) { |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2079 | if (DiagnoseUseOfDecl(OperatorNew, StartLoc)) |
| 2080 | return ExprError(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2081 | MarkFunctionReferenced(StartLoc, OperatorNew); |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2082 | diagnoseUnavailableAlignedAllocation(*OperatorNew, StartLoc, false, *this); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2083 | } |
| 2084 | if (OperatorDelete) { |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2085 | if (DiagnoseUseOfDecl(OperatorDelete, StartLoc)) |
| 2086 | return ExprError(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2087 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2088 | diagnoseUnavailableAlignedAllocation(*OperatorDelete, StartLoc, true, *this); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2089 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2090 | |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 2091 | // C++0x [expr.new]p17: |
| 2092 | // If the new expression creates an array of objects of class type, |
| 2093 | // access and ambiguity control are done for the destructor. |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2094 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
| 2095 | if (ArraySize && !BaseAllocType->isDependentType()) { |
| 2096 | if (const RecordType *BaseRecordType = BaseAllocType->getAs<RecordType>()) { |
| 2097 | if (CXXDestructorDecl *dtor = LookupDestructor( |
| 2098 | cast<CXXRecordDecl>(BaseRecordType->getDecl()))) { |
| 2099 | MarkFunctionReferenced(StartLoc, dtor); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2100 | CheckDestructorAccess(StartLoc, dtor, |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2101 | PDiag(diag::err_access_dtor) |
| 2102 | << BaseAllocType); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2103 | if (DiagnoseUseOfDecl(dtor, StartLoc)) |
| 2104 | return ExprError(); |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2105 | } |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 2106 | } |
| 2107 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2108 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2109 | return new (Context) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2110 | CXXNewExpr(Context, UseGlobal, OperatorNew, OperatorDelete, PassAlignment, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2111 | UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens, |
| 2112 | ArraySize, initStyle, Initializer, ResultType, AllocTypeInfo, |
| 2113 | Range, DirectInitRange); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2116 | /// \brief Checks that a type is suitable as the allocated type |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2117 | /// in a new-expression. |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2118 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | SourceRange R) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2120 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 2121 | // abstract class type or array thereof. |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2122 | if (AllocType->isFunctionType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2123 | return Diag(Loc, diag::err_bad_new_type) |
| 2124 | << AllocType << 0 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2125 | else if (AllocType->isReferenceType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2126 | return Diag(Loc, diag::err_bad_new_type) |
| 2127 | << AllocType << 1 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2128 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2129 | RequireCompleteType(Loc, AllocType, diag::err_new_incomplete_type,R)) |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2130 | return true; |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2131 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2132 | diag::err_allocation_of_abstract_type)) |
| 2133 | return true; |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 2134 | else if (AllocType->isVariablyModifiedType()) |
| 2135 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 2136 | << AllocType; |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 2137 | else if (AllocType.getAddressSpace() != LangAS::Default) |
Douglas Gregor | 39d1a09 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 2138 | return Diag(Loc, diag::err_address_space_qualified_new) |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 2139 | << AllocType.getUnqualifiedType() |
| 2140 | << AllocType.getQualifiers().getAddressSpaceAttributePrintValue(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2141 | else if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2142 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { |
| 2143 | QualType BaseAllocType = Context.getBaseElementType(AT); |
| 2144 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 2145 | BaseAllocType->isObjCLifetimeType()) |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 2146 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2147 | << BaseAllocType; |
| 2148 | } |
| 2149 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2150 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2151 | return false; |
| 2152 | } |
| 2153 | |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2154 | static bool resolveAllocationOverload( |
| 2155 | Sema &S, LookupResult &R, SourceRange Range, SmallVectorImpl<Expr *> &Args, |
| 2156 | bool &PassAlignment, FunctionDecl *&Operator, |
| 2157 | OverloadCandidateSet *AlignedCandidates, Expr *AlignArg, bool Diagnose) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2158 | OverloadCandidateSet Candidates(R.getNameLoc(), |
| 2159 | OverloadCandidateSet::CSK_Normal); |
| 2160 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 2161 | Alloc != AllocEnd; ++Alloc) { |
| 2162 | // Even member operator new/delete are implicitly treated as |
| 2163 | // static, so don't use AddMemberCandidate. |
| 2164 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
| 2165 | |
| 2166 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 2167 | S.AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
| 2168 | /*ExplicitTemplateArgs=*/nullptr, Args, |
| 2169 | Candidates, |
| 2170 | /*SuppressUserConversions=*/false); |
| 2171 | continue; |
| 2172 | } |
| 2173 | |
| 2174 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 2175 | S.AddOverloadCandidate(Fn, Alloc.getPair(), Args, Candidates, |
| 2176 | /*SuppressUserConversions=*/false); |
| 2177 | } |
| 2178 | |
| 2179 | // Do the resolution. |
| 2180 | OverloadCandidateSet::iterator Best; |
| 2181 | switch (Candidates.BestViableFunction(S, R.getNameLoc(), Best)) { |
| 2182 | case OR_Success: { |
| 2183 | // Got one! |
| 2184 | FunctionDecl *FnDecl = Best->Function; |
| 2185 | if (S.CheckAllocationAccess(R.getNameLoc(), Range, R.getNamingClass(), |
| 2186 | Best->FoundDecl) == Sema::AR_inaccessible) |
| 2187 | return true; |
| 2188 | |
| 2189 | Operator = FnDecl; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2190 | return false; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2191 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2192 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2193 | case OR_No_Viable_Function: |
| 2194 | // C++17 [expr.new]p13: |
| 2195 | // If no matching function is found and the allocated object type has |
| 2196 | // new-extended alignment, the alignment argument is removed from the |
| 2197 | // argument list, and overload resolution is performed again. |
| 2198 | if (PassAlignment) { |
| 2199 | PassAlignment = false; |
| 2200 | AlignArg = Args[1]; |
| 2201 | Args.erase(Args.begin() + 1); |
| 2202 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2203 | Operator, &Candidates, AlignArg, |
| 2204 | Diagnose); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2205 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2206 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2207 | // MSVC will fall back on trying to find a matching global operator new |
| 2208 | // if operator new[] cannot be found. Also, MSVC will leak by not |
| 2209 | // generating a call to operator delete or operator delete[], but we |
| 2210 | // will not replicate that bug. |
| 2211 | // FIXME: Find out how this interacts with the std::align_val_t fallback |
| 2212 | // once MSVC implements it. |
| 2213 | if (R.getLookupName().getCXXOverloadedOperator() == OO_Array_New && |
| 2214 | S.Context.getLangOpts().MSVCCompat) { |
| 2215 | R.clear(); |
| 2216 | R.setLookupName(S.Context.DeclarationNames.getCXXOperatorName(OO_New)); |
| 2217 | S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); |
| 2218 | // FIXME: This will give bad diagnostics pointing at the wrong functions. |
| 2219 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2220 | Operator, /*Candidates=*/nullptr, |
| 2221 | /*AlignArg=*/nullptr, Diagnose); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2222 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2223 | |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2224 | if (Diagnose) { |
| 2225 | S.Diag(R.getNameLoc(), diag::err_ovl_no_viable_function_in_call) |
| 2226 | << R.getLookupName() << Range; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2227 | |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2228 | // If we have aligned candidates, only note the align_val_t candidates |
| 2229 | // from AlignedCandidates and the non-align_val_t candidates from |
| 2230 | // Candidates. |
| 2231 | if (AlignedCandidates) { |
| 2232 | auto IsAligned = [](OverloadCandidate &C) { |
| 2233 | return C.Function->getNumParams() > 1 && |
| 2234 | C.Function->getParamDecl(1)->getType()->isAlignValT(); |
| 2235 | }; |
| 2236 | auto IsUnaligned = [&](OverloadCandidate &C) { return !IsAligned(C); }; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2237 | |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2238 | // This was an overaligned allocation, so list the aligned candidates |
| 2239 | // first. |
| 2240 | Args.insert(Args.begin() + 1, AlignArg); |
| 2241 | AlignedCandidates->NoteCandidates(S, OCD_AllCandidates, Args, "", |
| 2242 | R.getNameLoc(), IsAligned); |
| 2243 | Args.erase(Args.begin() + 1); |
| 2244 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args, "", R.getNameLoc(), |
| 2245 | IsUnaligned); |
| 2246 | } else { |
| 2247 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 2248 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2249 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2250 | return true; |
| 2251 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2252 | case OR_Ambiguous: |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2253 | if (Diagnose) { |
| 2254 | S.Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) |
| 2255 | << R.getLookupName() << Range; |
| 2256 | Candidates.NoteCandidates(S, OCD_ViableCandidates, Args); |
| 2257 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2258 | return true; |
| 2259 | |
| 2260 | case OR_Deleted: { |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2261 | if (Diagnose) { |
| 2262 | S.Diag(R.getNameLoc(), diag::err_ovl_deleted_call) |
| 2263 | << Best->Function->isDeleted() << R.getLookupName() |
| 2264 | << S.getDeletedOrUnavailableSuffix(Best->Function) << Range; |
| 2265 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 2266 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2267 | return true; |
| 2268 | } |
| 2269 | } |
| 2270 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2273 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2274 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 2275 | /// that are appropriate for the allocation. |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 2276 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 2277 | bool UseGlobal, QualType AllocType, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2278 | bool IsArray, bool &PassAlignment, |
| 2279 | MultiExprArg PlaceArgs, |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2280 | FunctionDecl *&OperatorNew, |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2281 | FunctionDecl *&OperatorDelete, |
| 2282 | bool Diagnose) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2283 | // --- Choosing an allocation function --- |
| 2284 | // C++ 5.3.4p8 - 14 & 18 |
| 2285 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 2286 | // in the scope of the allocated class. |
| 2287 | // 2) If an array size is given, look for operator new[], else look for |
| 2288 | // operator new. |
| 2289 | // 3) The first argument is always size_t. Append the arguments from the |
| 2290 | // placement form. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2291 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2292 | SmallVector<Expr*, 8> AllocArgs; |
| 2293 | AllocArgs.reserve((PassAlignment ? 2 : 1) + PlaceArgs.size()); |
| 2294 | |
| 2295 | // We don't care about the actual value of these arguments. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2296 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 2297 | // tree? Or should the consumer just recalculate the value? |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2298 | // FIXME: Using a dummy value will interact poorly with attribute enable_if. |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2299 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2300 | Context.getTargetInfo().getPointerWidth(0)), |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2301 | Context.getSizeType(), |
| 2302 | SourceLocation()); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2303 | AllocArgs.push_back(&Size); |
| 2304 | |
| 2305 | QualType AlignValT = Context.VoidTy; |
| 2306 | if (PassAlignment) { |
| 2307 | DeclareGlobalNewDelete(); |
| 2308 | AlignValT = Context.getTypeDeclType(getStdAlignValT()); |
| 2309 | } |
| 2310 | CXXScalarValueInitExpr Align(AlignValT, nullptr, SourceLocation()); |
| 2311 | if (PassAlignment) |
| 2312 | AllocArgs.push_back(&Align); |
| 2313 | |
| 2314 | AllocArgs.insert(AllocArgs.end(), PlaceArgs.begin(), PlaceArgs.end()); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2315 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2316 | // C++ [expr.new]p8: |
| 2317 | // If the allocated type is a non-array type, the allocation |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2318 | // function's name is operator new and the deallocation function's |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2319 | // name is operator delete. If the allocated type is an array |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2320 | // type, the allocation function's name is operator new[] and the |
| 2321 | // deallocation function's name is operator delete[]. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2322 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2323 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2324 | |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2325 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 2326 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2327 | // Find the allocation function. |
| 2328 | { |
| 2329 | LookupResult R(*this, NewName, StartLoc, LookupOrdinaryName); |
| 2330 | |
| 2331 | // C++1z [expr.new]p9: |
| 2332 | // If the new-expression begins with a unary :: operator, the allocation |
| 2333 | // function's name is looked up in the global scope. Otherwise, if the |
| 2334 | // allocated type is a class type T or array thereof, the allocation |
| 2335 | // function's name is looked up in the scope of T. |
| 2336 | if (AllocElemType->isRecordType() && !UseGlobal) |
| 2337 | LookupQualifiedName(R, AllocElemType->getAsCXXRecordDecl()); |
| 2338 | |
| 2339 | // We can see ambiguity here if the allocation function is found in |
| 2340 | // multiple base classes. |
| 2341 | if (R.isAmbiguous()) |
| 2342 | return true; |
| 2343 | |
| 2344 | // If this lookup fails to find the name, or if the allocated type is not |
| 2345 | // a class type, the allocation function's name is looked up in the |
| 2346 | // global scope. |
| 2347 | if (R.empty()) |
| 2348 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 2349 | |
| 2350 | assert(!R.empty() && "implicitly declared allocation functions not found"); |
| 2351 | assert(!R.isAmbiguous() && "global allocation functions are ambiguous"); |
| 2352 | |
| 2353 | // We do our own custom access checks below. |
| 2354 | R.suppressDiagnostics(); |
| 2355 | |
| 2356 | if (resolveAllocationOverload(*this, R, Range, AllocArgs, PassAlignment, |
Brian Gesiak | 87412d9 | 2018-02-15 20:09:25 +0000 | [diff] [blame] | 2357 | OperatorNew, /*Candidates=*/nullptr, |
| 2358 | /*AlignArg=*/nullptr, Diagnose)) |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2359 | return true; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2360 | } |
Aaron Ballman | 324fbee | 2013-05-30 01:55:39 +0000 | [diff] [blame] | 2361 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2362 | // 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] | 2363 | if (!getLangOpts().Exceptions) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2364 | OperatorDelete = nullptr; |
John McCall | 0f55a03 | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 2365 | return false; |
| 2366 | } |
| 2367 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2368 | // Note, the name of OperatorNew might have been changed from array to |
| 2369 | // non-array by resolveAllocationOverload. |
| 2370 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 2371 | OperatorNew->getDeclName().getCXXOverloadedOperator() == OO_Array_New |
| 2372 | ? OO_Array_Delete |
| 2373 | : OO_Delete); |
| 2374 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2375 | // C++ [expr.new]p19: |
| 2376 | // |
| 2377 | // If the new-expression begins with a unary :: operator, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2378 | // deallocation function's name is looked up in the global |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2379 | // 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] | 2380 | // array thereof, the deallocation function's name is looked up in |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2381 | // the scope of T. If this lookup fails to find the name, or if |
| 2382 | // the allocated type is not a class type or array thereof, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2383 | // deallocation function's name is looked up in the global scope. |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2384 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2385 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2386 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2387 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2388 | LookupQualifiedName(FoundDelete, RD); |
| 2389 | } |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 2390 | if (FoundDelete.isAmbiguous()) |
| 2391 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2392 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2393 | bool FoundGlobalDelete = FoundDelete.empty(); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2394 | if (FoundDelete.empty()) { |
| 2395 | DeclareGlobalNewDelete(); |
| 2396 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 2397 | } |
| 2398 | |
| 2399 | FoundDelete.suppressDiagnostics(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2400 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2401 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2402 | |
John McCall | d3be2c8 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 2403 | // Whether we're looking for a placement operator delete is dictated |
| 2404 | // by whether we selected a placement operator new, not by whether |
| 2405 | // we had explicit placement arguments. This matters for things like |
| 2406 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 2407 | // A *a = new A() |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2408 | // |
| 2409 | // We don't have any definition for what a "placement allocation function" |
| 2410 | // is, but we assume it's any allocation function whose |
| 2411 | // parameter-declaration-clause is anything other than (size_t). |
| 2412 | // |
| 2413 | // FIXME: Should (size_t, std::align_val_t) also be considered non-placement? |
| 2414 | // This affects whether an exception from the constructor of an overaligned |
| 2415 | // type uses the sized or non-sized form of aligned operator delete. |
| 2416 | bool isPlacementNew = !PlaceArgs.empty() || OperatorNew->param_size() != 1 || |
| 2417 | OperatorNew->isVariadic(); |
John McCall | d3be2c8 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 2418 | |
| 2419 | if (isPlacementNew) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2420 | // C++ [expr.new]p20: |
| 2421 | // A declaration of a placement deallocation function matches the |
| 2422 | // declaration of a placement allocation function if it has the |
| 2423 | // same number of parameters and, after parameter transformations |
| 2424 | // (8.3.5), all parameter types except the first are |
| 2425 | // identical. [...] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2426 | // |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2427 | // To perform this comparison, we compute the function type that |
| 2428 | // the deallocation function should have, and use that type both |
| 2429 | // for template argument deduction and for comparison purposes. |
| 2430 | QualType ExpectedFunctionType; |
| 2431 | { |
| 2432 | const FunctionProtoType *Proto |
| 2433 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2434 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2435 | SmallVector<QualType, 4> ArgTypes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2436 | ArgTypes.push_back(Context.VoidPtrTy); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2437 | for (unsigned I = 1, N = Proto->getNumParams(); I < N; ++I) |
| 2438 | ArgTypes.push_back(Proto->getParamType(I)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2439 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2440 | FunctionProtoType::ExtProtoInfo EPI; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2441 | // FIXME: This is not part of the standard's rule. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2442 | EPI.Variadic = Proto->isVariadic(); |
| 2443 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2444 | ExpectedFunctionType |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2445 | = Context.getFunctionType(Context.VoidTy, ArgTypes, EPI); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2448 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2449 | DEnd = FoundDelete.end(); |
| 2450 | D != DEnd; ++D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2451 | FunctionDecl *Fn = nullptr; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 2452 | if (FunctionTemplateDecl *FnTmpl = |
| 2453 | dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2454 | // Perform template argument deduction to try to match the |
| 2455 | // expected function type. |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 2456 | TemplateDeductionInfo Info(StartLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2457 | if (DeduceTemplateArguments(FnTmpl, nullptr, ExpectedFunctionType, Fn, |
| 2458 | Info)) |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2459 | continue; |
| 2460 | } else |
| 2461 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 2462 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 2463 | if (Context.hasSameType(adjustCCAndNoReturn(Fn->getType(), |
| 2464 | ExpectedFunctionType, |
| 2465 | /*AdjustExcpetionSpec*/true), |
| 2466 | ExpectedFunctionType)) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2467 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2468 | } |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 2469 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2470 | if (getLangOpts().CUDA) |
| 2471 | EraseUnwantedCUDAMatches(dyn_cast<FunctionDecl>(CurContext), Matches); |
| 2472 | } else { |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2473 | // C++1y [expr.new]p22: |
| 2474 | // For a non-placement allocation function, the normal deallocation |
| 2475 | // function lookup is used |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2476 | // |
| 2477 | // Per [expr.delete]p10, this lookup prefers a member operator delete |
| 2478 | // without a size_t argument, but prefers a non-member operator delete |
| 2479 | // with a size_t where possible (which it always is in this case). |
| 2480 | llvm::SmallVector<UsualDeallocFnInfo, 4> BestDeallocFns; |
| 2481 | UsualDeallocFnInfo Selected = resolveDeallocationOverload( |
| 2482 | *this, FoundDelete, /*WantSize*/ FoundGlobalDelete, |
| 2483 | /*WantAlign*/ hasNewExtendedAlignment(*this, AllocElemType), |
| 2484 | &BestDeallocFns); |
| 2485 | if (Selected) |
| 2486 | Matches.push_back(std::make_pair(Selected.Found, Selected.FD)); |
| 2487 | else { |
| 2488 | // If we failed to select an operator, all remaining functions are viable |
| 2489 | // but ambiguous. |
| 2490 | for (auto Fn : BestDeallocFns) |
| 2491 | Matches.push_back(std::make_pair(Fn.Found, Fn.FD)); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2492 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | // C++ [expr.new]p20: |
| 2496 | // [...] If the lookup finds a single matching deallocation |
| 2497 | // function, that function will be called; otherwise, no |
| 2498 | // deallocation function will be called. |
| 2499 | if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2500 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2501 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2502 | // C++1z [expr.new]p23: |
| 2503 | // If the lookup finds a usual deallocation function (3.7.4.2) |
| 2504 | // with a parameter of type std::size_t and that function, considered |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2505 | // as a placement deallocation function, would have been |
| 2506 | // selected as a match for the allocation function, the program |
| 2507 | // is ill-formed. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2508 | if (getLangOpts().CPlusPlus11 && isPlacementNew && |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2509 | isNonPlacementDeallocationFunction(*this, OperatorDelete)) { |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 2510 | UsualDeallocFnInfo Info(*this, |
| 2511 | DeclAccessPair::make(OperatorDelete, AS_public)); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2512 | // Core issue, per mail to core reflector, 2016-10-09: |
| 2513 | // If this is a member operator delete, and there is a corresponding |
| 2514 | // non-sized member operator delete, this isn't /really/ a sized |
| 2515 | // deallocation function, it just happens to have a size_t parameter. |
| 2516 | bool IsSizedDelete = Info.HasSizeT; |
| 2517 | if (IsSizedDelete && !FoundGlobalDelete) { |
| 2518 | auto NonSizedDelete = |
| 2519 | resolveDeallocationOverload(*this, FoundDelete, /*WantSize*/false, |
| 2520 | /*WantAlign*/Info.HasAlignValT); |
| 2521 | if (NonSizedDelete && !NonSizedDelete.HasSizeT && |
| 2522 | NonSizedDelete.HasAlignValT == Info.HasAlignValT) |
| 2523 | IsSizedDelete = false; |
| 2524 | } |
| 2525 | |
| 2526 | if (IsSizedDelete) { |
| 2527 | SourceRange R = PlaceArgs.empty() |
| 2528 | ? SourceRange() |
| 2529 | : SourceRange(PlaceArgs.front()->getLocStart(), |
| 2530 | PlaceArgs.back()->getLocEnd()); |
| 2531 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) << R; |
| 2532 | if (!OperatorDelete->isImplicit()) |
| 2533 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 2534 | << DeleteName; |
| 2535 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2536 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2537 | |
| 2538 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
| 2539 | Matches[0].first); |
| 2540 | } else if (!Matches.empty()) { |
| 2541 | // We found multiple suitable operators. Per [expr.new]p20, that means we |
| 2542 | // call no 'operator delete' function, but we should at least warn the user. |
| 2543 | // FIXME: Suppress this warning if the construction cannot throw. |
| 2544 | Diag(StartLoc, diag::warn_ambiguous_suitable_delete_function_found) |
| 2545 | << DeleteName << AllocElemType; |
| 2546 | |
| 2547 | for (auto &Match : Matches) |
| 2548 | Diag(Match.second->getLocation(), |
| 2549 | diag::note_member_declared_here) << DeleteName; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2552 | return false; |
| 2553 | } |
| 2554 | |
| 2555 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 2556 | /// delete. These are: |
| 2557 | /// @code |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2558 | /// // C++03: |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2559 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 2560 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 2561 | /// void operator delete(void *) throw(); |
| 2562 | /// void operator delete[](void *) throw(); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2563 | /// // C++11: |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2564 | /// void* operator new(std::size_t); |
| 2565 | /// void* operator new[](std::size_t); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2566 | /// void operator delete(void *) noexcept; |
| 2567 | /// void operator delete[](void *) noexcept; |
| 2568 | /// // C++1y: |
| 2569 | /// void* operator new(std::size_t); |
| 2570 | /// void* operator new[](std::size_t); |
| 2571 | /// void operator delete(void *) noexcept; |
| 2572 | /// void operator delete[](void *) noexcept; |
| 2573 | /// void operator delete(void *, std::size_t) noexcept; |
| 2574 | /// void operator delete[](void *, std::size_t) noexcept; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2575 | /// @endcode |
| 2576 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 2577 | /// declared. Their use requires including \<new\>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2579 | if (GlobalNewDeleteDeclared) |
| 2580 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2581 | |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2582 | // C++ [basic.std.dynamic]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2583 | // [...] The following allocation and deallocation functions (18.4) are |
| 2584 | // implicitly declared in global scope in each translation unit of a |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2585 | // program |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2586 | // |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2587 | // C++03: |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2588 | // void* operator new(std::size_t) throw(std::bad_alloc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2589 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 2590 | // void operator delete(void*) throw(); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2591 | // void operator delete[](void*) throw(); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2592 | // C++11: |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2593 | // void* operator new(std::size_t); |
| 2594 | // void* operator new[](std::size_t); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2595 | // void operator delete(void*) noexcept; |
| 2596 | // void operator delete[](void*) noexcept; |
| 2597 | // C++1y: |
| 2598 | // void* operator new(std::size_t); |
| 2599 | // void* operator new[](std::size_t); |
| 2600 | // void operator delete(void*) noexcept; |
| 2601 | // void operator delete[](void*) noexcept; |
| 2602 | // void operator delete(void*, std::size_t) noexcept; |
| 2603 | // void operator delete[](void*, std::size_t) noexcept; |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2604 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2605 | // These implicit declarations introduce only the function names operator |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2606 | // new, operator new[], operator delete, operator delete[]. |
| 2607 | // |
| 2608 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 2609 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 2610 | // However, we do not make these implicit declarations visible to name |
| 2611 | // lookup. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2612 | if (!StdBadAlloc && !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2613 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 2614 | // implicitly. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2615 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
| 2616 | getOrCreateStdNamespace(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2617 | SourceLocation(), SourceLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2618 | &PP.getIdentifierTable().get("bad_alloc"), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2619 | nullptr); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 2620 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2621 | } |
Richard Smith | 5913902 | 2016-09-30 22:41:36 +0000 | [diff] [blame] | 2622 | if (!StdAlignValT && getLangOpts().AlignedAllocation) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2623 | // The "std::align_val_t" enum class has not yet been declared, so build it |
| 2624 | // implicitly. |
| 2625 | auto *AlignValT = EnumDecl::Create( |
| 2626 | Context, getOrCreateStdNamespace(), SourceLocation(), SourceLocation(), |
| 2627 | &PP.getIdentifierTable().get("align_val_t"), nullptr, true, true, true); |
| 2628 | AlignValT->setIntegerType(Context.getSizeType()); |
| 2629 | AlignValT->setPromotionType(Context.getSizeType()); |
| 2630 | AlignValT->setImplicit(true); |
| 2631 | StdAlignValT = AlignValT; |
| 2632 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2633 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2634 | GlobalNewDeleteDeclared = true; |
| 2635 | |
| 2636 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 2637 | QualType SizeT = Context.getSizeType(); |
| 2638 | |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2639 | auto DeclareGlobalAllocationFunctions = [&](OverloadedOperatorKind Kind, |
| 2640 | QualType Return, QualType Param) { |
| 2641 | llvm::SmallVector<QualType, 3> Params; |
| 2642 | Params.push_back(Param); |
| 2643 | |
| 2644 | // Create up to four variants of the function (sized/aligned). |
| 2645 | bool HasSizedVariant = getLangOpts().SizedDeallocation && |
| 2646 | (Kind == OO_Delete || Kind == OO_Array_Delete); |
Richard Smith | 5913902 | 2016-09-30 22:41:36 +0000 | [diff] [blame] | 2647 | bool HasAlignedVariant = getLangOpts().AlignedAllocation; |
Simon Pilgrim | d69fc8e | 2016-09-30 14:18:06 +0000 | [diff] [blame] | 2648 | |
| 2649 | int NumSizeVariants = (HasSizedVariant ? 2 : 1); |
| 2650 | int NumAlignVariants = (HasAlignedVariant ? 2 : 1); |
| 2651 | for (int Sized = 0; Sized < NumSizeVariants; ++Sized) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2652 | if (Sized) |
| 2653 | Params.push_back(SizeT); |
| 2654 | |
Simon Pilgrim | d69fc8e | 2016-09-30 14:18:06 +0000 | [diff] [blame] | 2655 | for (int Aligned = 0; Aligned < NumAlignVariants; ++Aligned) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2656 | if (Aligned) |
| 2657 | Params.push_back(Context.getTypeDeclType(getStdAlignValT())); |
| 2658 | |
| 2659 | DeclareGlobalAllocationFunction( |
| 2660 | Context.DeclarationNames.getCXXOperatorName(Kind), Return, Params); |
| 2661 | |
| 2662 | if (Aligned) |
| 2663 | Params.pop_back(); |
| 2664 | } |
| 2665 | } |
| 2666 | }; |
| 2667 | |
| 2668 | DeclareGlobalAllocationFunctions(OO_New, VoidPtr, SizeT); |
| 2669 | DeclareGlobalAllocationFunctions(OO_Array_New, VoidPtr, SizeT); |
| 2670 | DeclareGlobalAllocationFunctions(OO_Delete, Context.VoidTy, VoidPtr); |
| 2671 | DeclareGlobalAllocationFunctions(OO_Array_Delete, Context.VoidTy, VoidPtr); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 2675 | /// allocation function if it doesn't already exist. |
| 2676 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2677 | QualType Return, |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2678 | ArrayRef<QualType> Params) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2679 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 2680 | |
| 2681 | // Check if this function is already declared. |
Serge Pavlov | d548907 | 2013-09-14 12:00:01 +0000 | [diff] [blame] | 2682 | DeclContext::lookup_result R = GlobalCtx->lookup(Name); |
| 2683 | for (DeclContext::lookup_iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 2684 | Alloc != AllocEnd; ++Alloc) { |
| 2685 | // Only look at non-template functions, as it is the predefined, |
| 2686 | // non-templated allocation function we are trying to declare here. |
| 2687 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2688 | if (Func->getNumParams() == Params.size()) { |
| 2689 | llvm::SmallVector<QualType, 3> FuncParams; |
| 2690 | for (auto *P : Func->parameters()) |
| 2691 | FuncParams.push_back( |
| 2692 | Context.getCanonicalType(P->getType().getUnqualifiedType())); |
| 2693 | if (llvm::makeArrayRef(FuncParams) == Params) { |
Serge Pavlov | d548907 | 2013-09-14 12:00:01 +0000 | [diff] [blame] | 2694 | // Make the function visible to name lookup, even if we found it in |
| 2695 | // an unimported module. It either is an implicitly-declared global |
Richard Smith | 42713d7 | 2013-07-14 02:01:48 +0000 | [diff] [blame] | 2696 | // allocation function, or is suppressing that function. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 2697 | Func->setVisibleDespiteOwningModule(); |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 2698 | return; |
Douglas Gregor | c1a42fd | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 2699 | } |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 2700 | } |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2701 | } |
| 2702 | } |
Reid Kleckner | 7270ef5 | 2015-03-19 17:03:58 +0000 | [diff] [blame] | 2703 | |
Richard Smith | c015bc2 | 2014-02-07 22:39:53 +0000 | [diff] [blame] | 2704 | FunctionProtoType::ExtProtoInfo EPI; |
| 2705 | |
Richard Smith | f8b417c | 2014-02-08 00:42:45 +0000 | [diff] [blame] | 2706 | QualType BadAllocType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2707 | bool HasBadAllocExceptionSpec |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2708 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 2709 | Name.getCXXOverloadedOperator() == OO_Array_New); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2710 | if (HasBadAllocExceptionSpec) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2711 | if (!getLangOpts().CPlusPlus11) { |
Richard Smith | f8b417c | 2014-02-08 00:42:45 +0000 | [diff] [blame] | 2712 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Richard Smith | c015bc2 | 2014-02-07 22:39:53 +0000 | [diff] [blame] | 2713 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2714 | EPI.ExceptionSpec.Type = EST_Dynamic; |
| 2715 | EPI.ExceptionSpec.Exceptions = llvm::makeArrayRef(BadAllocType); |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2716 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2717 | } else { |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2718 | EPI.ExceptionSpec = |
| 2719 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2720 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2721 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2722 | auto CreateAllocationFunctionDecl = [&](Attr *ExtraAttr) { |
| 2723 | QualType FnType = Context.getFunctionType(Return, Params, EPI); |
| 2724 | FunctionDecl *Alloc = FunctionDecl::Create( |
| 2725 | Context, GlobalCtx, SourceLocation(), SourceLocation(), Name, |
| 2726 | FnType, /*TInfo=*/nullptr, SC_None, false, true); |
| 2727 | Alloc->setImplicit(); |
Vassil Vassilev | 41cafcd | 2017-06-09 21:36:28 +0000 | [diff] [blame] | 2728 | // Global allocation functions should always be visible. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 2729 | Alloc->setVisibleDespiteOwningModule(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2730 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2731 | // Implicit sized deallocation functions always have default visibility. |
| 2732 | Alloc->addAttr( |
| 2733 | VisibilityAttr::CreateImplicit(Context, VisibilityAttr::Default)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2734 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2735 | llvm::SmallVector<ParmVarDecl *, 3> ParamDecls; |
| 2736 | for (QualType T : Params) { |
| 2737 | ParamDecls.push_back(ParmVarDecl::Create( |
| 2738 | Context, Alloc, SourceLocation(), SourceLocation(), nullptr, T, |
| 2739 | /*TInfo=*/nullptr, SC_None, nullptr)); |
| 2740 | ParamDecls.back()->setImplicit(); |
| 2741 | } |
| 2742 | Alloc->setParams(ParamDecls); |
| 2743 | if (ExtraAttr) |
| 2744 | Alloc->addAttr(ExtraAttr); |
| 2745 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
| 2746 | IdResolver.tryAddTopLevelDecl(Alloc, Name); |
| 2747 | }; |
| 2748 | |
| 2749 | if (!LangOpts.CUDA) |
| 2750 | CreateAllocationFunctionDecl(nullptr); |
| 2751 | else { |
| 2752 | // Host and device get their own declaration so each can be |
| 2753 | // defined or re-declared independently. |
| 2754 | CreateAllocationFunctionDecl(CUDAHostAttr::CreateImplicit(Context)); |
| 2755 | CreateAllocationFunctionDecl(CUDADeviceAttr::CreateImplicit(Context)); |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2756 | } |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2759 | FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc, |
| 2760 | bool CanProvideSize, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2761 | bool Overaligned, |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2762 | DeclarationName Name) { |
| 2763 | DeclareGlobalNewDelete(); |
| 2764 | |
| 2765 | LookupResult FoundDelete(*this, Name, StartLoc, LookupOrdinaryName); |
| 2766 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 2767 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2768 | // FIXME: It's possible for this to result in ambiguity, through a |
| 2769 | // user-declared variadic operator delete or the enable_if attribute. We |
| 2770 | // should probably not consider those cases to be usual deallocation |
| 2771 | // functions. But for now we just make an arbitrary choice in that case. |
| 2772 | auto Result = resolveDeallocationOverload(*this, FoundDelete, CanProvideSize, |
| 2773 | Overaligned); |
| 2774 | assert(Result.FD && "operator delete missing from global scope?"); |
| 2775 | return Result.FD; |
| 2776 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2777 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2778 | FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc, |
| 2779 | CXXRecordDecl *RD) { |
| 2780 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2781 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2782 | FunctionDecl *OperatorDelete = nullptr; |
| 2783 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
| 2784 | return nullptr; |
| 2785 | if (OperatorDelete) |
| 2786 | return OperatorDelete; |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 2787 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2788 | // If there's no class-specific operator delete, look up the global |
| 2789 | // non-array delete. |
| 2790 | return FindUsualDeallocationFunction( |
| 2791 | Loc, true, hasNewExtendedAlignment(*this, Context.getRecordType(RD)), |
| 2792 | Name); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2795 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 2796 | DeclarationName Name, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2797 | FunctionDecl *&Operator, bool Diagnose) { |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2798 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2799 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2800 | LookupQualifiedName(Found, RD); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2801 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2802 | if (Found.isAmbiguous()) |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2803 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2804 | |
Chandler Carruth | b6f9917 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 2805 | Found.suppressDiagnostics(); |
| 2806 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2807 | bool Overaligned = hasNewExtendedAlignment(*this, Context.getRecordType(RD)); |
Chandler Carruth | 9b41823 | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 2808 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2809 | // C++17 [expr.delete]p10: |
| 2810 | // If the deallocation functions have class scope, the one without a |
| 2811 | // parameter of type std::size_t is selected. |
| 2812 | llvm::SmallVector<UsualDeallocFnInfo, 4> Matches; |
| 2813 | resolveDeallocationOverload(*this, Found, /*WantSize*/ false, |
| 2814 | /*WantAlign*/ Overaligned, &Matches); |
Chandler Carruth | 9b41823 | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 2815 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2816 | // If we could find an overload, use it. |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2817 | if (Matches.size() == 1) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2818 | Operator = cast<CXXMethodDecl>(Matches[0].FD); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2819 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2820 | // FIXME: DiagnoseUseOfDecl? |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2821 | if (Operator->isDeleted()) { |
| 2822 | if (Diagnose) { |
| 2823 | Diag(StartLoc, diag::err_deleted_function_use); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 2824 | NoteDeletedFunction(Operator); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2825 | } |
| 2826 | return true; |
| 2827 | } |
| 2828 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 2829 | if (CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2830 | Matches[0].Found, Diagnose) == AR_inaccessible) |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 2831 | return true; |
| 2832 | |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2833 | return false; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2834 | } |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2835 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2836 | // We found multiple suitable operators; complain about the ambiguity. |
| 2837 | // FIXME: The standard doesn't say to do this; it appears that the intent |
| 2838 | // is that this should never happen. |
| 2839 | if (!Matches.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2840 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2841 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 2842 | << Name << RD; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2843 | for (auto &Match : Matches) |
| 2844 | Diag(Match.FD->getLocation(), diag::note_member_declared_here) << Name; |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2845 | } |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2846 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | // We did find operator delete/operator delete[] declarations, but |
| 2850 | // none of them were suitable. |
| 2851 | if (!Found.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2852 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2853 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 2854 | << Name << RD; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2855 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2856 | for (NamedDecl *D : Found) |
| 2857 | Diag(D->getUnderlyingDecl()->getLocation(), |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2858 | diag::note_member_declared_here) << Name; |
| 2859 | } |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2860 | return true; |
| 2861 | } |
| 2862 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2863 | Operator = nullptr; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2864 | return false; |
| 2865 | } |
| 2866 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2867 | namespace { |
| 2868 | /// \brief Checks whether delete-expression, and new-expression used for |
| 2869 | /// initializing deletee have the same array form. |
| 2870 | class MismatchingNewDeleteDetector { |
| 2871 | public: |
| 2872 | enum MismatchResult { |
| 2873 | /// Indicates that there is no mismatch or a mismatch cannot be proven. |
| 2874 | NoMismatch, |
| 2875 | /// Indicates that variable is initialized with mismatching form of \a new. |
| 2876 | VarInitMismatches, |
| 2877 | /// Indicates that member is initialized with mismatching form of \a new. |
| 2878 | MemberInitMismatches, |
| 2879 | /// Indicates that 1 or more constructors' definitions could not been |
| 2880 | /// analyzed, and they will be checked again at the end of translation unit. |
| 2881 | AnalyzeLater |
| 2882 | }; |
| 2883 | |
| 2884 | /// \param EndOfTU True, if this is the final analysis at the end of |
| 2885 | /// translation unit. False, if this is the initial analysis at the point |
| 2886 | /// delete-expression was encountered. |
| 2887 | explicit MismatchingNewDeleteDetector(bool EndOfTU) |
Alexander Shaposhnikov | 3087b2c | 2016-09-01 23:18:00 +0000 | [diff] [blame] | 2888 | : Field(nullptr), IsArrayForm(false), EndOfTU(EndOfTU), |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2889 | HasUndefinedConstructors(false) {} |
| 2890 | |
| 2891 | /// \brief Checks whether pointee of a delete-expression is initialized with |
| 2892 | /// matching form of new-expression. |
| 2893 | /// |
| 2894 | /// If return value is \c VarInitMismatches or \c MemberInitMismatches at the |
| 2895 | /// point where delete-expression is encountered, then a warning will be |
| 2896 | /// issued immediately. If return value is \c AnalyzeLater at the point where |
| 2897 | /// delete-expression is seen, then member will be analyzed at the end of |
| 2898 | /// translation unit. \c AnalyzeLater is returned iff at least one constructor |
| 2899 | /// couldn't be analyzed. If at least one constructor initializes the member |
| 2900 | /// with matching type of new, the return value is \c NoMismatch. |
| 2901 | MismatchResult analyzeDeleteExpr(const CXXDeleteExpr *DE); |
| 2902 | /// \brief Analyzes a class member. |
| 2903 | /// \param Field Class member to analyze. |
| 2904 | /// \param DeleteWasArrayForm Array form-ness of the delete-expression used |
| 2905 | /// for deleting the \p Field. |
| 2906 | MismatchResult analyzeField(FieldDecl *Field, bool DeleteWasArrayForm); |
Alexander Shaposhnikov | 3087b2c | 2016-09-01 23:18:00 +0000 | [diff] [blame] | 2907 | FieldDecl *Field; |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2908 | /// List of mismatching new-expressions used for initialization of the pointee |
| 2909 | llvm::SmallVector<const CXXNewExpr *, 4> NewExprs; |
| 2910 | /// Indicates whether delete-expression was in array form. |
| 2911 | bool IsArrayForm; |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2912 | |
| 2913 | private: |
| 2914 | const bool EndOfTU; |
| 2915 | /// \brief Indicates that there is at least one constructor without body. |
| 2916 | bool HasUndefinedConstructors; |
| 2917 | /// \brief Returns \c CXXNewExpr from given initialization expression. |
| 2918 | /// \param E Expression used for initializing pointee in delete-expression. |
NAKAMURA Takumi | 4bd67d8 | 2015-05-19 06:47:23 +0000 | [diff] [blame] | 2919 | /// E can be a single-element \c InitListExpr consisting of new-expression. |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2920 | const CXXNewExpr *getNewExprFromInitListOrExpr(const Expr *E); |
| 2921 | /// \brief Returns whether member is initialized with mismatching form of |
| 2922 | /// \c new either by the member initializer or in-class initialization. |
| 2923 | /// |
| 2924 | /// If bodies of all constructors are not visible at the end of translation |
| 2925 | /// unit or at least one constructor initializes member with the matching |
| 2926 | /// form of \c new, mismatch cannot be proven, and this function will return |
| 2927 | /// \c NoMismatch. |
| 2928 | MismatchResult analyzeMemberExpr(const MemberExpr *ME); |
| 2929 | /// \brief Returns whether variable is initialized with mismatching form of |
| 2930 | /// \c new. |
| 2931 | /// |
| 2932 | /// If variable is initialized with matching form of \c new or variable is not |
| 2933 | /// initialized with a \c new expression, this function will return true. |
| 2934 | /// If variable is initialized with mismatching form of \c new, returns false. |
| 2935 | /// \param D Variable to analyze. |
| 2936 | bool hasMatchingVarInit(const DeclRefExpr *D); |
| 2937 | /// \brief Checks whether the constructor initializes pointee with mismatching |
| 2938 | /// form of \c new. |
| 2939 | /// |
| 2940 | /// Returns true, if member is initialized with matching form of \c new in |
| 2941 | /// member initializer list. Returns false, if member is initialized with the |
| 2942 | /// matching form of \c new in this constructor's initializer or given |
| 2943 | /// constructor isn't defined at the point where delete-expression is seen, or |
| 2944 | /// member isn't initialized by the constructor. |
| 2945 | bool hasMatchingNewInCtor(const CXXConstructorDecl *CD); |
| 2946 | /// \brief Checks whether member is initialized with matching form of |
| 2947 | /// \c new in member initializer list. |
| 2948 | bool hasMatchingNewInCtorInit(const CXXCtorInitializer *CI); |
| 2949 | /// Checks whether member is initialized with mismatching form of \c new by |
| 2950 | /// in-class initializer. |
| 2951 | MismatchResult analyzeInClassInitializer(); |
| 2952 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2953 | } |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2954 | |
| 2955 | MismatchingNewDeleteDetector::MismatchResult |
| 2956 | MismatchingNewDeleteDetector::analyzeDeleteExpr(const CXXDeleteExpr *DE) { |
| 2957 | NewExprs.clear(); |
| 2958 | assert(DE && "Expected delete-expression"); |
| 2959 | IsArrayForm = DE->isArrayForm(); |
| 2960 | const Expr *E = DE->getArgument()->IgnoreParenImpCasts(); |
| 2961 | if (const MemberExpr *ME = dyn_cast<const MemberExpr>(E)) { |
| 2962 | return analyzeMemberExpr(ME); |
| 2963 | } else if (const DeclRefExpr *D = dyn_cast<const DeclRefExpr>(E)) { |
| 2964 | if (!hasMatchingVarInit(D)) |
| 2965 | return VarInitMismatches; |
| 2966 | } |
| 2967 | return NoMismatch; |
| 2968 | } |
| 2969 | |
| 2970 | const CXXNewExpr * |
| 2971 | MismatchingNewDeleteDetector::getNewExprFromInitListOrExpr(const Expr *E) { |
| 2972 | assert(E != nullptr && "Expected a valid initializer expression"); |
| 2973 | E = E->IgnoreParenImpCasts(); |
| 2974 | if (const InitListExpr *ILE = dyn_cast<const InitListExpr>(E)) { |
| 2975 | if (ILE->getNumInits() == 1) |
| 2976 | E = dyn_cast<const CXXNewExpr>(ILE->getInit(0)->IgnoreParenImpCasts()); |
| 2977 | } |
| 2978 | |
| 2979 | return dyn_cast_or_null<const CXXNewExpr>(E); |
| 2980 | } |
| 2981 | |
| 2982 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtorInit( |
| 2983 | const CXXCtorInitializer *CI) { |
| 2984 | const CXXNewExpr *NE = nullptr; |
| 2985 | if (Field == CI->getMember() && |
| 2986 | (NE = getNewExprFromInitListOrExpr(CI->getInit()))) { |
| 2987 | if (NE->isArray() == IsArrayForm) |
| 2988 | return true; |
| 2989 | else |
| 2990 | NewExprs.push_back(NE); |
| 2991 | } |
| 2992 | return false; |
| 2993 | } |
| 2994 | |
| 2995 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtor( |
| 2996 | const CXXConstructorDecl *CD) { |
| 2997 | if (CD->isImplicit()) |
| 2998 | return false; |
| 2999 | const FunctionDecl *Definition = CD; |
| 3000 | if (!CD->isThisDeclarationADefinition() && !CD->isDefined(Definition)) { |
| 3001 | HasUndefinedConstructors = true; |
| 3002 | return EndOfTU; |
| 3003 | } |
| 3004 | for (const auto *CI : cast<const CXXConstructorDecl>(Definition)->inits()) { |
| 3005 | if (hasMatchingNewInCtorInit(CI)) |
| 3006 | return true; |
| 3007 | } |
| 3008 | return false; |
| 3009 | } |
| 3010 | |
| 3011 | MismatchingNewDeleteDetector::MismatchResult |
| 3012 | MismatchingNewDeleteDetector::analyzeInClassInitializer() { |
| 3013 | assert(Field != nullptr && "This should be called only for members"); |
Ismail Pazarbasi | 7ff1836 | 2015-10-26 19:20:24 +0000 | [diff] [blame] | 3014 | const Expr *InitExpr = Field->getInClassInitializer(); |
| 3015 | if (!InitExpr) |
| 3016 | return EndOfTU ? NoMismatch : AnalyzeLater; |
| 3017 | if (const CXXNewExpr *NE = getNewExprFromInitListOrExpr(InitExpr)) { |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3018 | if (NE->isArray() != IsArrayForm) { |
| 3019 | NewExprs.push_back(NE); |
| 3020 | return MemberInitMismatches; |
| 3021 | } |
| 3022 | } |
| 3023 | return NoMismatch; |
| 3024 | } |
| 3025 | |
| 3026 | MismatchingNewDeleteDetector::MismatchResult |
| 3027 | MismatchingNewDeleteDetector::analyzeField(FieldDecl *Field, |
| 3028 | bool DeleteWasArrayForm) { |
| 3029 | assert(Field != nullptr && "Analysis requires a valid class member."); |
| 3030 | this->Field = Field; |
| 3031 | IsArrayForm = DeleteWasArrayForm; |
| 3032 | const CXXRecordDecl *RD = cast<const CXXRecordDecl>(Field->getParent()); |
| 3033 | for (const auto *CD : RD->ctors()) { |
| 3034 | if (hasMatchingNewInCtor(CD)) |
| 3035 | return NoMismatch; |
| 3036 | } |
| 3037 | if (HasUndefinedConstructors) |
| 3038 | return EndOfTU ? NoMismatch : AnalyzeLater; |
| 3039 | if (!NewExprs.empty()) |
| 3040 | return MemberInitMismatches; |
| 3041 | return Field->hasInClassInitializer() ? analyzeInClassInitializer() |
| 3042 | : NoMismatch; |
| 3043 | } |
| 3044 | |
| 3045 | MismatchingNewDeleteDetector::MismatchResult |
| 3046 | MismatchingNewDeleteDetector::analyzeMemberExpr(const MemberExpr *ME) { |
| 3047 | assert(ME != nullptr && "Expected a member expression"); |
| 3048 | if (FieldDecl *F = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 3049 | return analyzeField(F, IsArrayForm); |
| 3050 | return NoMismatch; |
| 3051 | } |
| 3052 | |
| 3053 | bool MismatchingNewDeleteDetector::hasMatchingVarInit(const DeclRefExpr *D) { |
| 3054 | const CXXNewExpr *NE = nullptr; |
| 3055 | if (const VarDecl *VD = dyn_cast<const VarDecl>(D->getDecl())) { |
| 3056 | if (VD->hasInit() && (NE = getNewExprFromInitListOrExpr(VD->getInit())) && |
| 3057 | NE->isArray() != IsArrayForm) { |
| 3058 | NewExprs.push_back(NE); |
| 3059 | } |
| 3060 | } |
| 3061 | return NewExprs.empty(); |
| 3062 | } |
| 3063 | |
| 3064 | static void |
| 3065 | DiagnoseMismatchedNewDelete(Sema &SemaRef, SourceLocation DeleteLoc, |
| 3066 | const MismatchingNewDeleteDetector &Detector) { |
| 3067 | SourceLocation EndOfDelete = SemaRef.getLocForEndOfToken(DeleteLoc); |
| 3068 | FixItHint H; |
| 3069 | if (!Detector.IsArrayForm) |
| 3070 | H = FixItHint::CreateInsertion(EndOfDelete, "[]"); |
| 3071 | else { |
| 3072 | SourceLocation RSquare = Lexer::findLocationAfterToken( |
| 3073 | DeleteLoc, tok::l_square, SemaRef.getSourceManager(), |
| 3074 | SemaRef.getLangOpts(), true); |
| 3075 | if (RSquare.isValid()) |
| 3076 | H = FixItHint::CreateRemoval(SourceRange(EndOfDelete, RSquare)); |
| 3077 | } |
| 3078 | SemaRef.Diag(DeleteLoc, diag::warn_mismatched_delete_new) |
| 3079 | << Detector.IsArrayForm << H; |
| 3080 | |
| 3081 | for (const auto *NE : Detector.NewExprs) |
| 3082 | SemaRef.Diag(NE->getExprLoc(), diag::note_allocated_here) |
| 3083 | << Detector.IsArrayForm; |
| 3084 | } |
| 3085 | |
| 3086 | void Sema::AnalyzeDeleteExprMismatch(const CXXDeleteExpr *DE) { |
| 3087 | if (Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) |
| 3088 | return; |
| 3089 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/false); |
| 3090 | switch (Detector.analyzeDeleteExpr(DE)) { |
| 3091 | case MismatchingNewDeleteDetector::VarInitMismatches: |
| 3092 | case MismatchingNewDeleteDetector::MemberInitMismatches: { |
| 3093 | DiagnoseMismatchedNewDelete(*this, DE->getLocStart(), Detector); |
| 3094 | break; |
| 3095 | } |
| 3096 | case MismatchingNewDeleteDetector::AnalyzeLater: { |
| 3097 | DeleteExprs[Detector.Field].push_back( |
| 3098 | std::make_pair(DE->getLocStart(), DE->isArrayForm())); |
| 3099 | break; |
| 3100 | } |
| 3101 | case MismatchingNewDeleteDetector::NoMismatch: |
| 3102 | break; |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | void Sema::AnalyzeDeleteExprMismatch(FieldDecl *Field, SourceLocation DeleteLoc, |
| 3107 | bool DeleteWasArrayForm) { |
| 3108 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/true); |
| 3109 | switch (Detector.analyzeField(Field, DeleteWasArrayForm)) { |
| 3110 | case MismatchingNewDeleteDetector::VarInitMismatches: |
| 3111 | llvm_unreachable("This analysis should have been done for class members."); |
| 3112 | case MismatchingNewDeleteDetector::AnalyzeLater: |
| 3113 | llvm_unreachable("Analysis cannot be postponed any point beyond end of " |
| 3114 | "translation unit."); |
| 3115 | case MismatchingNewDeleteDetector::MemberInitMismatches: |
| 3116 | DiagnoseMismatchedNewDelete(*this, DeleteLoc, Detector); |
| 3117 | break; |
| 3118 | case MismatchingNewDeleteDetector::NoMismatch: |
| 3119 | break; |
| 3120 | } |
| 3121 | } |
| 3122 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3123 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 3124 | /// @code ::delete ptr; @endcode |
| 3125 | /// or |
| 3126 | /// @code delete [] ptr; @endcode |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3127 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3128 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3129 | bool ArrayForm, Expr *ExE) { |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3130 | // C++ [expr.delete]p1: |
| 3131 | // 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] | 3132 | // non-explicit conversion function to a pointer type. The result has type |
| 3133 | // void. |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3134 | // |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3135 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 3136 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3137 | ExprResult Ex = ExE; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3138 | FunctionDecl *OperatorDelete = nullptr; |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3139 | bool ArrayFormAsWritten = ArrayForm; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3140 | bool UsualArrayDeleteWantsSize = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3142 | if (!Ex.get()->isTypeDependent()) { |
John McCall | ef42902 | 2012-03-09 04:08:29 +0000 | [diff] [blame] | 3143 | // Perform lvalue-to-rvalue cast, if needed. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3144 | Ex = DefaultLvalueConversion(Ex.get()); |
Eli Friedman | 89a4a2c | 2012-12-13 00:37:17 +0000 | [diff] [blame] | 3145 | if (Ex.isInvalid()) |
| 3146 | return ExprError(); |
John McCall | ef42902 | 2012-03-09 04:08:29 +0000 | [diff] [blame] | 3147 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3148 | QualType Type = Ex.get()->getType(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3149 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3150 | class DeleteConverter : public ContextualImplicitConverter { |
| 3151 | public: |
| 3152 | DeleteConverter() : ContextualImplicitConverter(false, true) {} |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3153 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3154 | bool match(QualType ConvType) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3155 | // FIXME: If we have an operator T* and an operator void*, we must pick |
| 3156 | // the operator T*. |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3157 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3158 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3159 | return true; |
| 3160 | return false; |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3161 | } |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3162 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3163 | SemaDiagnosticBuilder diagnoseNoMatch(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3164 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3165 | return S.Diag(Loc, diag::err_delete_operand) << T; |
| 3166 | } |
| 3167 | |
| 3168 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3169 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3170 | return S.Diag(Loc, diag::err_delete_incomplete_class_type) << T; |
| 3171 | } |
| 3172 | |
| 3173 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3174 | QualType T, |
| 3175 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3176 | return S.Diag(Loc, diag::err_delete_explicit_conversion) << T << ConvTy; |
| 3177 | } |
| 3178 | |
| 3179 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3180 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3181 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
| 3182 | << ConvTy; |
| 3183 | } |
| 3184 | |
| 3185 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3186 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3187 | return S.Diag(Loc, diag::err_ambiguous_delete_operand) << T; |
| 3188 | } |
| 3189 | |
| 3190 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3191 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3192 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
| 3193 | << ConvTy; |
| 3194 | } |
| 3195 | |
| 3196 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3197 | QualType T, |
| 3198 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3199 | llvm_unreachable("conversion functions are permitted"); |
| 3200 | } |
| 3201 | } Converter; |
| 3202 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3203 | Ex = PerformContextualImplicitConversion(StartLoc, Ex.get(), Converter); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3204 | if (Ex.isInvalid()) |
| 3205 | return ExprError(); |
| 3206 | Type = Ex.get()->getType(); |
| 3207 | if (!Converter.match(Type)) |
| 3208 | // FIXME: PerformContextualImplicitConversion should return ExprError |
| 3209 | // itself in this case. |
| 3210 | return ExprError(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3211 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3212 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3213 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 3214 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 3215 | if (Pointee.getAddressSpace() != LangAS::Default) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3216 | return Diag(Ex.get()->getLocStart(), |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3217 | diag::err_address_space_qualified_delete) |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 3218 | << Pointee.getUnqualifiedType() |
| 3219 | << Pointee.getQualifiers().getAddressSpaceAttributePrintValue(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3220 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3221 | CXXRecordDecl *PointeeRD = nullptr; |
Douglas Gregor | bb3348e | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 3222 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3223 | // 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] | 3224 | // effectively bans deletion of "void*". However, most compilers support |
| 3225 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 3226 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3227 | << Type << Ex.get()->getSourceRange(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3228 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3229 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3230 | << Type << Ex.get()->getSourceRange()); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3231 | } else if (!Pointee->isDependentType()) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3232 | // FIXME: This can result in errors if the definition was imported from a |
| 3233 | // module but is hidden. |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3234 | if (!RequireCompleteType(StartLoc, Pointee, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3235 | diag::warn_delete_incomplete, Ex.get())) { |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3236 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
| 3237 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3238 | } |
| 3239 | } |
| 3240 | |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3241 | if (Pointee->isArrayType() && !ArrayForm) { |
| 3242 | Diag(StartLoc, diag::warn_delete_array_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3243 | << Type << Ex.get()->getSourceRange() |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 3244 | << FixItHint::CreateInsertion(getLocForEndOfToken(StartLoc), "[]"); |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3245 | ArrayForm = true; |
| 3246 | } |
| 3247 | |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 3248 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 3249 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 3250 | |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3251 | if (PointeeRD) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3252 | if (!UseGlobal && |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3253 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, |
| 3254 | OperatorDelete)) |
Anders Carlsson | 654e5c7 | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 3255 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3256 | |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3257 | // If we're allocating an array of records, check whether the |
| 3258 | // usual operator delete[] has a size_t parameter. |
| 3259 | if (ArrayForm) { |
| 3260 | // If the user specifically asked to use the global allocator, |
| 3261 | // we'll need to do the lookup into the class. |
| 3262 | if (UseGlobal) |
| 3263 | UsualArrayDeleteWantsSize = |
| 3264 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); |
| 3265 | |
| 3266 | // Otherwise, the usual operator delete[] should be the |
| 3267 | // function we just found. |
Richard Smith | f03bd30 | 2013-12-05 08:30:59 +0000 | [diff] [blame] | 3268 | else if (OperatorDelete && isa<CXXMethodDecl>(OperatorDelete)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3269 | UsualArrayDeleteWantsSize = |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 3270 | UsualDeallocFnInfo(*this, |
| 3271 | DeclAccessPair::make(OperatorDelete, AS_public)) |
| 3272 | .HasSizeT; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3275 | if (!PointeeRD->hasIrrelevantDestructor()) |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3276 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3277 | MarkFunctionReferenced(StartLoc, |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3278 | const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 3279 | if (DiagnoseUseOfDecl(Dtor, StartLoc)) |
| 3280 | return ExprError(); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3281 | } |
Argyrios Kyrtzidis | 8bd4285 | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 3282 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3283 | CheckVirtualDtorCall(PointeeRD->getDestructor(), StartLoc, |
| 3284 | /*IsDelete=*/true, /*CallCanBeVirtual=*/true, |
| 3285 | /*WarnOnNonAbstractTypes=*/!ArrayForm, |
| 3286 | SourceLocation()); |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 3287 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3288 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3289 | if (!OperatorDelete) { |
| 3290 | bool IsComplete = isCompleteType(StartLoc, Pointee); |
| 3291 | bool CanProvideSize = |
| 3292 | IsComplete && (!ArrayForm || UsualArrayDeleteWantsSize || |
| 3293 | Pointee.isDestructedType()); |
| 3294 | bool Overaligned = hasNewExtendedAlignment(*this, Pointee); |
| 3295 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 3296 | // Look for a global declaration. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3297 | OperatorDelete = FindUsualDeallocationFunction(StartLoc, CanProvideSize, |
| 3298 | Overaligned, DeleteName); |
| 3299 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3300 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3301 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3302 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 3303 | // Check access and ambiguity of destructor if we're going to call it. |
| 3304 | // Note that this is required even for a virtual delete. |
| 3305 | bool IsVirtualDelete = false; |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3306 | if (PointeeRD) { |
| 3307 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 3308 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
| 3309 | PDiag(diag::err_access_dtor) << PointeeElem); |
| 3310 | IsVirtualDelete = Dtor->isVirtual(); |
Douglas Gregor | fa77813 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 3311 | } |
| 3312 | } |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 3313 | |
| 3314 | diagnoseUnavailableAlignedAllocation(*OperatorDelete, StartLoc, true, |
| 3315 | *this); |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 3316 | |
| 3317 | // Convert the operand to the type of the first parameter of operator |
| 3318 | // delete. This is only necessary if we selected a destroying operator |
| 3319 | // delete that we are going to call (non-virtually); converting to void* |
| 3320 | // is trivial and left to AST consumers to handle. |
| 3321 | QualType ParamType = OperatorDelete->getParamDecl(0)->getType(); |
| 3322 | if (!IsVirtualDelete && !ParamType->getPointeeType()->isVoidType()) { |
Richard Smith | 2517201 | 2017-12-05 23:54:25 +0000 | [diff] [blame] | 3323 | Qualifiers Qs = Pointee.getQualifiers(); |
| 3324 | if (Qs.hasCVRQualifiers()) { |
| 3325 | // Qualifiers are irrelevant to this conversion; we're only looking |
| 3326 | // for access and ambiguity. |
| 3327 | Qs.removeCVRQualifiers(); |
| 3328 | QualType Unqual = Context.getPointerType( |
| 3329 | Context.getQualifiedType(Pointee.getUnqualifiedType(), Qs)); |
| 3330 | Ex = ImpCastExprToType(Ex.get(), Unqual, CK_NoOp); |
| 3331 | } |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 3332 | Ex = PerformImplicitConversion(Ex.get(), ParamType, AA_Passing); |
| 3333 | if (Ex.isInvalid()) |
| 3334 | return ExprError(); |
| 3335 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3338 | CXXDeleteExpr *Result = new (Context) CXXDeleteExpr( |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3339 | Context.VoidTy, UseGlobal, ArrayForm, ArrayFormAsWritten, |
| 3340 | UsualArrayDeleteWantsSize, OperatorDelete, Ex.get(), StartLoc); |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3341 | AnalyzeDeleteExprMismatch(Result); |
| 3342 | return Result; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3345 | void Sema::CheckVirtualDtorCall(CXXDestructorDecl *dtor, SourceLocation Loc, |
| 3346 | bool IsDelete, bool CallCanBeVirtual, |
| 3347 | bool WarnOnNonAbstractTypes, |
| 3348 | SourceLocation DtorLoc) { |
Nico Weber | 955bb84 | 2017-08-30 20:25:22 +0000 | [diff] [blame] | 3349 | if (!dtor || dtor->isVirtual() || !CallCanBeVirtual || isUnevaluatedContext()) |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3350 | return; |
| 3351 | |
| 3352 | // C++ [expr.delete]p3: |
| 3353 | // In the first alternative (delete object), if the static type of the |
| 3354 | // object to be deleted is different from its dynamic type, the static |
| 3355 | // type shall be a base class of the dynamic type of the object to be |
| 3356 | // deleted and the static type shall have a virtual destructor or the |
| 3357 | // behavior is undefined. |
| 3358 | // |
| 3359 | const CXXRecordDecl *PointeeRD = dtor->getParent(); |
| 3360 | // Note: a final class cannot be derived from, no issue there |
| 3361 | if (!PointeeRD->isPolymorphic() || PointeeRD->hasAttr<FinalAttr>()) |
| 3362 | return; |
| 3363 | |
Nico Weber | bf2260c | 2017-08-31 06:17:08 +0000 | [diff] [blame] | 3364 | // If the superclass is in a system header, there's nothing that can be done. |
| 3365 | // The `delete` (where we emit the warning) can be in a system header, |
| 3366 | // what matters for this warning is where the deleted type is defined. |
| 3367 | if (getSourceManager().isInSystemHeader(PointeeRD->getLocation())) |
| 3368 | return; |
| 3369 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3370 | QualType ClassType = dtor->getThisType(Context)->getPointeeType(); |
| 3371 | if (PointeeRD->isAbstract()) { |
| 3372 | // If the class is abstract, we warn by default, because we're |
| 3373 | // sure the code has undefined behavior. |
| 3374 | Diag(Loc, diag::warn_delete_abstract_non_virtual_dtor) << (IsDelete ? 0 : 1) |
| 3375 | << ClassType; |
| 3376 | } else if (WarnOnNonAbstractTypes) { |
| 3377 | // Otherwise, if this is not an array delete, it's a bit suspect, |
| 3378 | // but not necessarily wrong. |
| 3379 | Diag(Loc, diag::warn_delete_non_virtual_dtor) << (IsDelete ? 0 : 1) |
| 3380 | << ClassType; |
| 3381 | } |
| 3382 | if (!IsDelete) { |
| 3383 | std::string TypeStr; |
| 3384 | ClassType.getAsStringInternal(TypeStr, getPrintingPolicy()); |
| 3385 | Diag(DtorLoc, diag::note_delete_non_virtual) |
| 3386 | << FixItHint::CreateInsertion(DtorLoc, TypeStr + "::"); |
| 3387 | } |
| 3388 | } |
| 3389 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3390 | Sema::ConditionResult Sema::ActOnConditionVariable(Decl *ConditionVar, |
| 3391 | SourceLocation StmtLoc, |
| 3392 | ConditionKind CK) { |
| 3393 | ExprResult E = |
| 3394 | CheckConditionVariable(cast<VarDecl>(ConditionVar), StmtLoc, CK); |
| 3395 | if (E.isInvalid()) |
| 3396 | return ConditionError(); |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3397 | return ConditionResult(*this, ConditionVar, MakeFullExpr(E.get(), StmtLoc), |
| 3398 | CK == ConditionKind::ConstexprIf); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3401 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 3402 | /// while, do-while, or switch statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3403 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3404 | SourceLocation StmtLoc, |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3405 | ConditionKind CK) { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3406 | if (ConditionVar->isInvalidDecl()) |
| 3407 | return ExprError(); |
| 3408 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3409 | QualType T = ConditionVar->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3410 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3411 | // C++ [stmt.select]p2: |
| 3412 | // The declarator shall not specify a function or an array. |
| 3413 | if (T->isFunctionType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3414 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3415 | diag::err_invalid_use_of_function_type) |
| 3416 | << ConditionVar->getSourceRange()); |
| 3417 | else if (T->isArrayType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3418 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3419 | diag::err_invalid_use_of_array_type) |
| 3420 | << ConditionVar->getSourceRange()); |
Douglas Gregor | 0156d1c | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 3421 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3422 | ExprResult Condition = DeclRefExpr::Create( |
| 3423 | Context, NestedNameSpecifierLoc(), SourceLocation(), ConditionVar, |
| 3424 | /*enclosing*/ false, ConditionVar->getLocation(), |
| 3425 | ConditionVar->getType().getNonReferenceType(), VK_LValue); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3426 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3427 | MarkDeclRefReferenced(cast<DeclRefExpr>(Condition.get())); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3428 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3429 | switch (CK) { |
| 3430 | case ConditionKind::Boolean: |
| 3431 | return CheckBooleanCondition(StmtLoc, Condition.get()); |
| 3432 | |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3433 | case ConditionKind::ConstexprIf: |
| 3434 | return CheckBooleanCondition(StmtLoc, Condition.get(), true); |
| 3435 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3436 | case ConditionKind::Switch: |
| 3437 | return CheckSwitchCondition(StmtLoc, Condition.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3438 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3439 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3440 | llvm_unreachable("unexpected condition kind"); |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3443 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3444 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr, bool IsConstexpr) { |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3445 | // C++ 6.4p4: |
| 3446 | // The value of a condition that is an initialized declaration in a statement |
| 3447 | // other than a switch statement is the value of the declared variable |
| 3448 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 3449 | // program is ill-formed. |
| 3450 | // The value of a condition that is an expression is the value of the |
| 3451 | // expression, implicitly converted to bool. |
| 3452 | // |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3453 | // FIXME: Return this value to the caller so they don't need to recompute it. |
| 3454 | llvm::APSInt Value(/*BitWidth*/1); |
| 3455 | return (IsConstexpr && !CondExpr->isValueDependent()) |
| 3456 | ? CheckConvertedConstantExpression(CondExpr, Context.BoolTy, Value, |
| 3457 | CCEK_ConstexprIf) |
| 3458 | : PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3459 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3460 | |
| 3461 | /// Helper function to determine whether this is the (deprecated) C++ |
| 3462 | /// conversion from a string literal to a pointer to non-const char or |
| 3463 | /// non-const wchar_t (for narrow and wide string literals, |
| 3464 | /// respectively). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3465 | bool |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3466 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 3467 | // Look inside the implicit cast, if it exists. |
| 3468 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 3469 | From = Cast->getSubExpr(); |
| 3470 | |
| 3471 | // A string literal (2.13.4) that is not a wide string literal can |
| 3472 | // be converted to an rvalue of type "pointer to char"; a wide |
| 3473 | // string literal can be converted to an rvalue of type "pointer |
| 3474 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 689999d | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 3475 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3476 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | if (const BuiltinType *ToPointeeType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3478 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3479 | // This conversion is considered only when there is an |
| 3480 | // explicit appropriate pointer target type (C++ 4.2p2). |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3481 | if (!ToPtrType->getPointeeType().hasQualifiers()) { |
| 3482 | switch (StrLit->getKind()) { |
| 3483 | case StringLiteral::UTF8: |
| 3484 | case StringLiteral::UTF16: |
| 3485 | case StringLiteral::UTF32: |
| 3486 | // We don't allow UTF literals to be implicitly converted |
| 3487 | break; |
| 3488 | case StringLiteral::Ascii: |
| 3489 | return (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 3490 | ToPointeeType->getKind() == BuiltinType::Char_S); |
| 3491 | case StringLiteral::Wide: |
Dmitry Polukhin | 9d64f72 | 2016-04-14 09:52:06 +0000 | [diff] [blame] | 3492 | return Context.typesAreCompatible(Context.getWideCharType(), |
| 3493 | QualType(ToPointeeType, 0)); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3494 | } |
| 3495 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | return false; |
| 3499 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3500 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3502 | SourceLocation CastLoc, |
| 3503 | QualType Ty, |
| 3504 | CastKind Kind, |
| 3505 | CXXMethodDecl *Method, |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3506 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3507 | bool HadMultipleCandidates, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3508 | Expr *From) { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3509 | switch (Kind) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3510 | default: llvm_unreachable("Unhandled cast kind!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3511 | case CK_ConstructorConversion: { |
Douglas Gregor | c7a3107 | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 3512 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3513 | SmallVector<Expr*, 8> ConstructorArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3514 | |
Richard Smith | 72d7405 | 2013-07-20 19:41:36 +0000 | [diff] [blame] | 3515 | if (S.RequireNonAbstractType(CastLoc, Ty, |
| 3516 | diag::err_allocation_of_abstract_type)) |
| 3517 | return ExprError(); |
| 3518 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3519 | if (S.CompleteConstructorCall(Constructor, From, CastLoc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3520 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3521 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 3522 | S.CheckConstructorAccess(CastLoc, Constructor, FoundDecl, |
| 3523 | InitializedEntity::InitializeTemporary(Ty)); |
Richard Smith | 7c9442a | 2015-02-24 21:44:43 +0000 | [diff] [blame] | 3524 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3525 | return ExprError(); |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 3526 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3527 | ExprResult Result = S.BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3528 | CastLoc, Ty, FoundDecl, cast<CXXConstructorDecl>(Method), |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3529 | ConstructorArgs, HadMultipleCandidates, |
| 3530 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3531 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3532 | if (Result.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3533 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3534 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3535 | return S.MaybeBindToTemporary(Result.getAs<Expr>()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3536 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3537 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3538 | case CK_UserDefinedConversion: { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3539 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3540 | |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3541 | S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ nullptr, FoundDecl); |
Richard Smith | 7c9442a | 2015-02-24 21:44:43 +0000 | [diff] [blame] | 3542 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3543 | return ExprError(); |
| 3544 | |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3545 | // Create an implicit call expr that calls it. |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 3546 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(Method); |
| 3547 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Conv, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3548 | HadMultipleCandidates); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3549 | if (Result.isInvalid()) |
| 3550 | return ExprError(); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 3551 | // Record usage of conversion in an implicit cast. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3552 | Result = ImplicitCastExpr::Create(S.Context, Result.get()->getType(), |
| 3553 | CK_UserDefinedConversion, Result.get(), |
| 3554 | nullptr, Result.get()->getValueKind()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3555 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3556 | return S.MaybeBindToTemporary(Result.get()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3557 | } |
| 3558 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | } |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3560 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3561 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 3562 | /// expression From to the type ToType using the pre-computed implicit |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3563 | /// conversion sequence ICS. Returns the converted |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 3564 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3565 | /// used in the error message. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3566 | ExprResult |
| 3567 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3568 | const ImplicitConversionSequence &ICS, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3569 | AssignmentAction Action, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3570 | CheckedConversionKind CCK) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3571 | switch (ICS.getKind()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3572 | case ImplicitConversionSequence::StandardConversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3573 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, |
| 3574 | Action, CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3575 | if (Res.isInvalid()) |
| 3576 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3577 | From = Res.get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3578 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3579 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3580 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3581 | case ImplicitConversionSequence::UserDefinedConversion: { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3582 | |
Fariborz Jahanian | 2fee79a | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 3583 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3584 | CastKind CastKind; |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3585 | QualType BeforeToType; |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3586 | assert(FD && "no conversion function for user-defined conversion seq"); |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3587 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3588 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3589 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3590 | // If the user-defined conversion is specified by a conversion function, |
| 3591 | // the initial standard conversion sequence converts the source type to |
| 3592 | // the implicit object parameter of the conversion function. |
| 3593 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
John McCall | a03edda | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 3594 | } else { |
| 3595 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3596 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3597 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 3598 | if (!ICS.UserDefined.EllipsisConversion) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3599 | // If the user-defined conversion is specified by a constructor, the |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 3600 | // initial standard conversion sequence converts the source type to |
| 3601 | // the type required by the argument of the constructor |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 3602 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3603 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3604 | } |
Richard Smith | 72d7405 | 2013-07-20 19:41:36 +0000 | [diff] [blame] | 3605 | // Watch out for ellipsis conversion. |
Fariborz Jahanian | eec642f | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 3606 | if (!ICS.UserDefined.EllipsisConversion) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3607 | ExprResult Res = |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3608 | PerformImplicitConversion(From, BeforeToType, |
| 3609 | ICS.UserDefined.Before, AA_Converting, |
| 3610 | CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3611 | if (Res.isInvalid()) |
| 3612 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3613 | From = Res.get(); |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3614 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3615 | |
| 3616 | ExprResult CastArg |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3617 | = BuildCXXCastArgument(*this, |
| 3618 | From->getLocStart(), |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3619 | ToType.getNonReferenceType(), |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 3620 | CastKind, cast<CXXMethodDecl>(FD), |
| 3621 | ICS.UserDefined.FoundConversionFunction, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3622 | ICS.UserDefined.HadMultipleCandidates, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3623 | From); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3624 | |
| 3625 | if (CastArg.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3626 | return ExprError(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 3627 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3628 | From = CastArg.get(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 3629 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3630 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
| 3631 | AA_Converting, CCK); |
Fariborz Jahanian | da21efb | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 3632 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3633 | |
| 3634 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3635 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3636 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 3637 | << From->getSourceRange()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3638 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3639 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3640 | case ImplicitConversionSequence::EllipsisConversion: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3641 | llvm_unreachable("Cannot perform an ellipsis conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3642 | |
| 3643 | case ImplicitConversionSequence::BadConversion: |
Richard Smith | e15a370 | 2016-10-06 23:12:58 +0000 | [diff] [blame] | 3644 | bool Diagnosed = |
| 3645 | DiagnoseAssignmentResult(Incompatible, From->getExprLoc(), ToType, |
| 3646 | From->getType(), From, Action); |
| 3647 | assert(Diagnosed && "failed to diagnose bad conversion"); (void)Diagnosed; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3648 | return ExprError(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | // Everything went well. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3652 | return From; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3655 | /// PerformImplicitConversion - Perform an implicit conversion of the |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3656 | /// expression From to the type ToType by following the standard |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3657 | /// conversion sequence SCS. Returns the converted |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3658 | /// expression. Flavor is the context in which we're performing this |
| 3659 | /// conversion, for use in error messages. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3660 | ExprResult |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3661 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3662 | const StandardConversionSequence& SCS, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3663 | AssignmentAction Action, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3664 | CheckedConversionKind CCK) { |
| 3665 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3666 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3667 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 3668 | // much extra work. What this means is that we need to keep track of more |
| 3669 | // information that is computed when we try the implicit conversion initially, |
| 3670 | // so that we don't need to recompute anything here. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3671 | QualType FromType = From->getType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3672 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3673 | if (SCS.CopyConstructor) { |
Anders Carlsson | 549c5bd | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 3674 | // FIXME: When can ToType be a reference type? |
| 3675 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3676 | if (SCS.Second == ICK_Derived_To_Base) { |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3677 | SmallVector<Expr*, 8> ConstructorArgs; |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3678 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3679 | From, /*FIXME:ConstructLoc*/SourceLocation(), |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3680 | ConstructorArgs)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3681 | return ExprError(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3682 | return BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3683 | /*FIXME:ConstructLoc*/ SourceLocation(), ToType, |
| 3684 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3685 | ConstructorArgs, /*HadMultipleCandidates*/ false, |
| 3686 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3687 | CXXConstructExpr::CK_Complete, SourceRange()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3688 | } |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3689 | return BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3690 | /*FIXME:ConstructLoc*/ SourceLocation(), ToType, |
| 3691 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3692 | From, /*HadMultipleCandidates*/ false, |
| 3693 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3694 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3697 | // Resolve overloaded function references. |
| 3698 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 3699 | DeclAccessPair Found; |
| 3700 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 3701 | true, Found); |
| 3702 | if (!Fn) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3703 | return ExprError(); |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3704 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3705 | if (DiagnoseUseOfDecl(Fn, From->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3706 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3707 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3708 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 3709 | FromType = From->getType(); |
| 3710 | } |
| 3711 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3712 | // If we're converting to an atomic type, first convert to the corresponding |
| 3713 | // non-atomic type. |
| 3714 | QualType ToAtomicType; |
| 3715 | if (const AtomicType *ToAtomic = ToType->getAs<AtomicType>()) { |
| 3716 | ToAtomicType = ToType; |
| 3717 | ToType = ToAtomic->getValueType(); |
| 3718 | } |
| 3719 | |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 3720 | QualType InitialFromType = FromType; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3721 | // Perform the first implicit conversion. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3722 | switch (SCS.First) { |
| 3723 | case ICK_Identity: |
David Majnemer | 3087a2b | 2014-12-28 21:47:31 +0000 | [diff] [blame] | 3724 | if (const AtomicType *FromAtomic = FromType->getAs<AtomicType>()) { |
| 3725 | FromType = FromAtomic->getValueType().getUnqualifiedType(); |
| 3726 | From = ImplicitCastExpr::Create(Context, FromType, CK_AtomicToNonAtomic, |
| 3727 | From, /*BasePath=*/nullptr, VK_RValue); |
| 3728 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3729 | break; |
| 3730 | |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3731 | case ICK_Lvalue_To_Rvalue: { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 3732 | assert(From->getObjectKind() != OK_ObjCProperty); |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3733 | ExprResult FromRes = DefaultLvalueConversion(From); |
| 3734 | assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!"); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3735 | From = FromRes.get(); |
David Majnemer | e7029bc | 2014-12-16 06:31:17 +0000 | [diff] [blame] | 3736 | FromType = From->getType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3737 | break; |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3738 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3739 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3740 | case ICK_Array_To_Pointer: |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3741 | FromType = Context.getArrayDecayedType(FromType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3742 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3743 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3744 | break; |
| 3745 | |
| 3746 | case ICK_Function_To_Pointer: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3747 | FromType = Context.getPointerType(FromType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3748 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3749 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3750 | break; |
| 3751 | |
| 3752 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3753 | llvm_unreachable("Improper first standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3756 | // Perform the second implicit conversion |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3757 | switch (SCS.Second) { |
| 3758 | case ICK_Identity: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 3759 | // C++ [except.spec]p5: |
| 3760 | // [For] assignment to and initialization of pointers to functions, |
| 3761 | // pointers to member functions, and references to functions: the |
| 3762 | // target entity shall allow at least the exceptions allowed by the |
| 3763 | // source value in the assignment or initialization. |
| 3764 | switch (Action) { |
| 3765 | case AA_Assigning: |
| 3766 | case AA_Initializing: |
| 3767 | // Note, function argument passing and returning are initialization. |
| 3768 | case AA_Passing: |
| 3769 | case AA_Returning: |
| 3770 | case AA_Sending: |
| 3771 | case AA_Passing_CFAudited: |
| 3772 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 3773 | return ExprError(); |
| 3774 | break; |
| 3775 | |
| 3776 | case AA_Casting: |
| 3777 | case AA_Converting: |
| 3778 | // Casts and implicit conversions are not initialization, so are not |
| 3779 | // checked for exception specification mismatches. |
| 3780 | break; |
| 3781 | } |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3782 | // Nothing else to do. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3783 | break; |
| 3784 | |
| 3785 | case ICK_Integral_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3786 | case ICK_Integral_Conversion: |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3787 | if (ToType->isBooleanType()) { |
| 3788 | assert(FromType->castAs<EnumType>()->getDecl()->isFixed() && |
| 3789 | SCS.Second == ICK_Integral_Promotion && |
| 3790 | "only enums with fixed underlying type can promote to bool"); |
| 3791 | From = ImpCastExprToType(From, ToType, CK_IntegralToBoolean, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3792 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3793 | } else { |
| 3794 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3795 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3796 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3797 | break; |
| 3798 | |
| 3799 | case ICK_Floating_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3800 | case ICK_Floating_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3801 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3802 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3803 | break; |
| 3804 | |
| 3805 | case ICK_Complex_Promotion: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3806 | case ICK_Complex_Conversion: { |
| 3807 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
| 3808 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
| 3809 | CastKind CK; |
| 3810 | if (FromEl->isRealFloatingType()) { |
| 3811 | if (ToEl->isRealFloatingType()) |
| 3812 | CK = CK_FloatingComplexCast; |
| 3813 | else |
| 3814 | CK = CK_FloatingComplexToIntegralComplex; |
| 3815 | } else if (ToEl->isRealFloatingType()) { |
| 3816 | CK = CK_IntegralComplexToFloatingComplex; |
| 3817 | } else { |
| 3818 | CK = CK_IntegralComplexCast; |
| 3819 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3820 | From = ImpCastExprToType(From, ToType, CK, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3821 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3822 | break; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3823 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3824 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3825 | case ICK_Floating_Integral: |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 3826 | if (ToType->isRealFloatingType()) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3827 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3828 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3829 | else |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3830 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3831 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3832 | break; |
| 3833 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 3834 | case ICK_Compatible_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3835 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3836 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3837 | break; |
| 3838 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3839 | case ICK_Writeback_Conversion: |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3840 | case ICK_Pointer_Conversion: { |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3841 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3842 | // Diagnose incompatible Objective-C conversions |
Douglas Gregor | 2720dc6 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 3843 | if (Action == AA_Initializing || Action == AA_Assigning) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3844 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 3845 | diag::ext_typecheck_convert_incompatible_pointer) |
| 3846 | << ToType << From->getType() << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 3847 | << From->getSourceRange() << 0; |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 3848 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3849 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 3850 | diag::ext_typecheck_convert_incompatible_pointer) |
| 3851 | << From->getType() << ToType << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 3852 | << From->getSourceRange() << 0; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3853 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3854 | if (From->getType()->isObjCObjectPointerType() && |
| 3855 | ToType->isObjCObjectPointerType()) |
| 3856 | EmitRelatedResultTypeNote(From); |
Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame] | 3857 | } else if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && |
| 3858 | !CheckObjCARCUnavailableWeakConversion(ToType, |
| 3859 | From->getType())) { |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 3860 | if (Action == AA_Initializing) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3861 | Diag(From->getLocStart(), |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 3862 | diag::err_arc_weak_unavailable_assign); |
| 3863 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3864 | Diag(From->getLocStart(), |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3865 | diag::err_arc_convesion_of_weak_unavailable) |
| 3866 | << (Action == AA_Casting) << From->getType() << ToType |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 3867 | << From->getSourceRange(); |
| 3868 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3869 | |
Richard Smith | 354abec | 2017-12-08 23:29:59 +0000 | [diff] [blame] | 3870 | CastKind Kind; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3871 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3872 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3873 | return ExprError(); |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3874 | |
| 3875 | // Make sure we extend blocks if necessary. |
| 3876 | // FIXME: doing this here is really ugly. |
| 3877 | if (Kind == CK_BlockPointerToObjCPointerCast) { |
| 3878 | ExprResult E = From; |
| 3879 | (void) PrepareCastToObjCObjectPointer(E); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3880 | From = E.get(); |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3881 | } |
Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame] | 3882 | if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) |
| 3883 | CheckObjCConversion(SourceRange(), ToType, From, CCK); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3884 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3885 | .get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3886 | break; |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3887 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3888 | |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3889 | case ICK_Pointer_Member: { |
Richard Smith | 354abec | 2017-12-08 23:29:59 +0000 | [diff] [blame] | 3890 | CastKind Kind; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3891 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3892 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3893 | return ExprError(); |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3894 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3895 | return ExprError(); |
David Majnemer | d96b997 | 2014-08-08 00:10:39 +0000 | [diff] [blame] | 3896 | |
| 3897 | // We may not have been able to figure out what this member pointer resolved |
| 3898 | // 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] | 3899 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3900 | (void)isCompleteType(From->getExprLoc(), From->getType()); |
| 3901 | (void)isCompleteType(From->getExprLoc(), ToType); |
David Majnemer | fc22e47 | 2015-06-12 17:55:44 +0000 | [diff] [blame] | 3902 | } |
David Majnemer | d96b997 | 2014-08-08 00:10:39 +0000 | [diff] [blame] | 3903 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3904 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3905 | .get(); |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3906 | break; |
| 3907 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3908 | |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 3909 | case ICK_Boolean_Conversion: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3910 | // Perform half-to-boolean conversion via float. |
| 3911 | if (From->getType()->isHalfType()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3912 | From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).get(); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3913 | FromType = Context.FloatTy; |
| 3914 | } |
| 3915 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3916 | From = ImpCastExprToType(From, Context.BoolTy, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3917 | ScalarTypeToBooleanCastKind(FromType), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3918 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3919 | break; |
| 3920 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3921 | case ICK_Derived_To_Base: { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3922 | CXXCastPath BasePath; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3923 | if (CheckDerivedToBaseConversion(From->getType(), |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 3924 | ToType.getNonReferenceType(), |
| 3925 | From->getLocStart(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3926 | From->getSourceRange(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3927 | &BasePath, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3928 | CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3929 | return ExprError(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3930 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3931 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 3932 | CK_DerivedToBase, From->getValueKind(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3933 | &BasePath, CCK).get(); |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 3934 | break; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3937 | case ICK_Vector_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3938 | From = ImpCastExprToType(From, ToType, CK_BitCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3939 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3940 | break; |
| 3941 | |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3942 | case ICK_Vector_Splat: { |
Fariborz Jahanian | 28d94b1 | 2015-03-05 23:06:09 +0000 | [diff] [blame] | 3943 | // Vector splat from any arithmetic type to a vector. |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3944 | Expr *Elem = prepareVectorSplat(ToType, From).get(); |
| 3945 | From = ImpCastExprToType(Elem, ToType, CK_VectorSplat, VK_RValue, |
| 3946 | /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3947 | break; |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3948 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3950 | case ICK_Complex_Real: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3951 | // Case 1. x -> _Complex y |
| 3952 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
| 3953 | QualType ElType = ToComplex->getElementType(); |
| 3954 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 3955 | |
| 3956 | // x -> y |
| 3957 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
| 3958 | // do nothing |
| 3959 | } else if (From->getType()->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3960 | From = ImpCastExprToType(From, ElType, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3961 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3962 | } else { |
| 3963 | assert(From->getType()->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3964 | From = ImpCastExprToType(From, ElType, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3965 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3966 | } |
| 3967 | // y -> _Complex y |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3968 | From = ImpCastExprToType(From, ToType, |
| 3969 | isFloatingComplex ? CK_FloatingRealToComplex |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3970 | : CK_IntegralRealToComplex).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3971 | |
| 3972 | // Case 2. _Complex x -> y |
| 3973 | } else { |
| 3974 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
| 3975 | assert(FromComplex); |
| 3976 | |
| 3977 | QualType ElType = FromComplex->getElementType(); |
| 3978 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 3979 | |
| 3980 | // _Complex x -> x |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3981 | From = ImpCastExprToType(From, ElType, |
| 3982 | isFloatingComplex ? CK_FloatingComplexToReal |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3983 | : CK_IntegralComplexToReal, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3984 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3985 | |
| 3986 | // x -> y |
| 3987 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
| 3988 | // do nothing |
| 3989 | } else if (ToType->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3990 | From = ImpCastExprToType(From, ToType, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3991 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3992 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3993 | } else { |
| 3994 | assert(ToType->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3995 | From = ImpCastExprToType(From, ToType, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3996 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3997 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4000 | break; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4001 | |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 4002 | case ICK_Block_Pointer_Conversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4003 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4004 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4005 | break; |
| 4006 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4007 | |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 4008 | case ICK_TransparentUnionConversion: { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4009 | ExprResult FromRes = From; |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 4010 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4011 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); |
| 4012 | if (FromRes.isInvalid()) |
| 4013 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4014 | From = FromRes.get(); |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 4015 | assert ((ConvTy == Sema::Compatible) && |
| 4016 | "Improper transparent union conversion"); |
| 4017 | (void)ConvTy; |
| 4018 | break; |
| 4019 | } |
| 4020 | |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4021 | case ICK_Zero_Event_Conversion: |
| 4022 | From = ImpCastExprToType(From, ToType, |
| 4023 | CK_ZeroToOCLEvent, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4024 | From->getValueKind()).get(); |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4025 | break; |
| 4026 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 4027 | case ICK_Zero_Queue_Conversion: |
| 4028 | From = ImpCastExprToType(From, ToType, |
| 4029 | CK_ZeroToOCLQueue, |
| 4030 | From->getValueKind()).get(); |
| 4031 | break; |
| 4032 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4033 | case ICK_Lvalue_To_Rvalue: |
| 4034 | case ICK_Array_To_Pointer: |
| 4035 | case ICK_Function_To_Pointer: |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 4036 | case ICK_Function_Conversion: |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4037 | case ICK_Qualification: |
| 4038 | case ICK_Num_Conversion_Kinds: |
George Burgess IV | 78ed9b4 | 2015-10-11 20:37:14 +0000 | [diff] [blame] | 4039 | case ICK_C_Only_Conversion: |
George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 4040 | case ICK_Incompatible_Pointer_Conversion: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4041 | llvm_unreachable("Improper second standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | switch (SCS.Third) { |
| 4045 | case ICK_Identity: |
| 4046 | // Nothing to do. |
| 4047 | break; |
| 4048 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 4049 | case ICK_Function_Conversion: |
| 4050 | // If both sides are functions (or pointers/references to them), there could |
| 4051 | // be incompatible exception declarations. |
| 4052 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 4053 | return ExprError(); |
| 4054 | |
| 4055 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 4056 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
| 4057 | break; |
| 4058 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4059 | case ICK_Qualification: { |
| 4060 | // The qualification keeps the category of the inner expression, unless the |
| 4061 | // target type isn't a reference. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4062 | ExprValueKind VK = ToType->isReferenceType() ? |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4063 | From->getValueKind() : VK_RValue; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 4064 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4065 | CK_NoOp, VK, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 4066 | |
Douglas Gregor | e981bb0 | 2011-03-14 16:13:32 +0000 | [diff] [blame] | 4067 | if (SCS.DeprecatedStringLiteralToCharPtr && |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 4068 | !getLangOpts().WritableStrings) { |
| 4069 | Diag(From->getLocStart(), getLangOpts().CPlusPlus11 |
| 4070 | ? diag::ext_deprecated_string_literal_conversion |
| 4071 | : diag::warn_deprecated_string_literal_conversion) |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 4072 | << ToType.getNonReferenceType(); |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 4073 | } |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 4074 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4075 | break; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4076 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4077 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4078 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4079 | llvm_unreachable("Improper third standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Douglas Gregor | 298f43d | 2012-04-12 20:42:30 +0000 | [diff] [blame] | 4082 | // If this conversion sequence involved a scalar -> atomic conversion, perform |
| 4083 | // that conversion now. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4084 | if (!ToAtomicType.isNull()) { |
| 4085 | assert(Context.hasSameType( |
| 4086 | ToAtomicType->castAs<AtomicType>()->getValueType(), From->getType())); |
| 4087 | From = ImpCastExprToType(From, ToAtomicType, CK_NonAtomicToAtomic, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4088 | VK_RValue, nullptr, CCK).get(); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 4091 | // If this conversion sequence succeeded and involved implicitly converting a |
| 4092 | // _Nullable type to a _Nonnull one, complain. |
| 4093 | if (CCK == CCK_ImplicitConversion) |
| 4094 | diagnoseNullableToNonnullConversion(ToType, InitialFromType, |
| 4095 | From->getLocStart()); |
| 4096 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4097 | return From; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4100 | /// \brief Check the completeness of a type in a unary type trait. |
| 4101 | /// |
| 4102 | /// If the particular type trait requires a complete type, tries to complete |
| 4103 | /// it. If completing the type fails, a diagnostic is emitted and false |
| 4104 | /// returned. If completing the type succeeds or no completion was required, |
| 4105 | /// returns true. |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4106 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT, |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4107 | SourceLocation Loc, |
| 4108 | QualType ArgTy) { |
| 4109 | // C++0x [meta.unary.prop]p3: |
| 4110 | // For all of the class templates X declared in this Clause, instantiating |
| 4111 | // that template with a template argument that is a class template |
| 4112 | // specialization may result in the implicit instantiation of the template |
| 4113 | // argument if and only if the semantics of X require that the argument |
| 4114 | // must be a complete type. |
| 4115 | // We apply this rule to all the type trait expressions used to implement |
| 4116 | // these class templates. We also try to follow any GCC documented behavior |
| 4117 | // in these expressions to ensure portability of standard libraries. |
| 4118 | switch (UTT) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4119 | default: llvm_unreachable("not a UTT"); |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4120 | // is_complete_type somewhat obviously cannot require a complete type. |
| 4121 | case UTT_IsCompleteType: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 4122 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4123 | |
| 4124 | // These traits are modeled on the type predicates in C++0x |
| 4125 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as |
| 4126 | // requiring a complete type, as whether or not they return true cannot be |
| 4127 | // impacted by the completeness of the type. |
| 4128 | case UTT_IsVoid: |
| 4129 | case UTT_IsIntegral: |
| 4130 | case UTT_IsFloatingPoint: |
| 4131 | case UTT_IsArray: |
| 4132 | case UTT_IsPointer: |
| 4133 | case UTT_IsLvalueReference: |
| 4134 | case UTT_IsRvalueReference: |
| 4135 | case UTT_IsMemberFunctionPointer: |
| 4136 | case UTT_IsMemberObjectPointer: |
| 4137 | case UTT_IsEnum: |
| 4138 | case UTT_IsUnion: |
| 4139 | case UTT_IsClass: |
| 4140 | case UTT_IsFunction: |
| 4141 | case UTT_IsReference: |
| 4142 | case UTT_IsArithmetic: |
| 4143 | case UTT_IsFundamental: |
| 4144 | case UTT_IsObject: |
| 4145 | case UTT_IsScalar: |
| 4146 | case UTT_IsCompound: |
| 4147 | case UTT_IsMemberPointer: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 4148 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4149 | |
| 4150 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] |
| 4151 | // which requires some of its traits to have the complete type. However, |
| 4152 | // the completeness of the type cannot impact these traits' semantics, and |
| 4153 | // so they don't require it. This matches the comments on these traits in |
| 4154 | // Table 49. |
| 4155 | case UTT_IsConst: |
| 4156 | case UTT_IsVolatile: |
| 4157 | case UTT_IsSigned: |
| 4158 | case UTT_IsUnsigned: |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4159 | |
| 4160 | // This type trait always returns false, checking the type is moot. |
| 4161 | case UTT_IsInterfaceClass: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4162 | return true; |
| 4163 | |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4164 | // C++14 [meta.unary.prop]: |
| 4165 | // If T is a non-union class type, T shall be a complete type. |
| 4166 | case UTT_IsEmpty: |
| 4167 | case UTT_IsPolymorphic: |
| 4168 | case UTT_IsAbstract: |
| 4169 | if (const auto *RD = ArgTy->getAsCXXRecordDecl()) |
| 4170 | if (!RD->isUnion()) |
| 4171 | return !S.RequireCompleteType( |
| 4172 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
| 4173 | return true; |
| 4174 | |
| 4175 | // C++14 [meta.unary.prop]: |
| 4176 | // If T is a class type, T shall be a complete type. |
| 4177 | case UTT_IsFinal: |
| 4178 | case UTT_IsSealed: |
| 4179 | if (ArgTy->getAsCXXRecordDecl()) |
| 4180 | return !S.RequireCompleteType( |
| 4181 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
| 4182 | return true; |
| 4183 | |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4184 | // C++1z [meta.unary.prop]: |
| 4185 | // 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] | 4186 | case UTT_IsAggregate: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4187 | case UTT_IsTrivial: |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 4188 | case UTT_IsTriviallyCopyable: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4189 | case UTT_IsStandardLayout: |
| 4190 | case UTT_IsPOD: |
| 4191 | case UTT_IsLiteral: |
Karthik Bhat | e1ae1b2 | 2017-06-28 08:52:08 +0000 | [diff] [blame] | 4192 | // Per the GCC type traits documentation, T shall be a complete type, cv void, |
| 4193 | // or an array of unknown bound. But GCC actually imposes the same constraints |
| 4194 | // as above. |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4195 | case UTT_HasNothrowAssign: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4196 | case UTT_HasNothrowMoveAssign: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4197 | case UTT_HasNothrowConstructor: |
| 4198 | case UTT_HasNothrowCopy: |
| 4199 | case UTT_HasTrivialAssign: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4200 | case UTT_HasTrivialMoveAssign: |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4201 | case UTT_HasTrivialDefaultConstructor: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4202 | case UTT_HasTrivialMoveConstructor: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4203 | case UTT_HasTrivialCopy: |
| 4204 | case UTT_HasTrivialDestructor: |
| 4205 | case UTT_HasVirtualDestructor: |
Karthik Bhat | e1ae1b2 | 2017-06-28 08:52:08 +0000 | [diff] [blame] | 4206 | ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0); |
| 4207 | LLVM_FALLTHROUGH; |
| 4208 | |
| 4209 | // C++1z [meta.unary.prop]: |
| 4210 | // T shall be a complete type, cv void, or an array of unknown bound. |
| 4211 | case UTT_IsDestructible: |
| 4212 | case UTT_IsNothrowDestructible: |
| 4213 | case UTT_IsTriviallyDestructible: |
Erich Keane | e63e9d7 | 2017-10-24 21:31:50 +0000 | [diff] [blame] | 4214 | case UTT_HasUniqueObjectRepresentations: |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4215 | if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType()) |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4216 | return true; |
| 4217 | |
| 4218 | return !S.RequireCompleteType( |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4219 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4220 | } |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4223 | static bool HasNoThrowOperator(const RecordType *RT, OverloadedOperatorKind Op, |
| 4224 | Sema &Self, SourceLocation KeyLoc, ASTContext &C, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4225 | bool (CXXRecordDecl::*HasTrivial)() const, |
| 4226 | bool (CXXRecordDecl::*HasNonTrivial)() const, |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4227 | bool (CXXMethodDecl::*IsDesiredOp)() const) |
| 4228 | { |
| 4229 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 4230 | if ((RD->*HasTrivial)() && !(RD->*HasNonTrivial)()) |
| 4231 | return true; |
| 4232 | |
| 4233 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(Op); |
| 4234 | DeclarationNameInfo NameInfo(Name, KeyLoc); |
| 4235 | LookupResult Res(Self, NameInfo, Sema::LookupOrdinaryName); |
| 4236 | if (Self.LookupQualifiedName(Res, RD)) { |
| 4237 | bool FoundOperator = false; |
| 4238 | Res.suppressDiagnostics(); |
| 4239 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 4240 | Op != OpEnd; ++Op) { |
| 4241 | if (isa<FunctionTemplateDecl>(*Op)) |
| 4242 | continue; |
| 4243 | |
| 4244 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 4245 | if((Operator->*IsDesiredOp)()) { |
| 4246 | FoundOperator = true; |
| 4247 | const FunctionProtoType *CPT = |
| 4248 | Operator->getType()->getAs<FunctionProtoType>(); |
| 4249 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4250 | if (!CPT || !CPT->isNothrow(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4251 | return false; |
| 4252 | } |
| 4253 | } |
| 4254 | return FoundOperator; |
| 4255 | } |
| 4256 | return false; |
| 4257 | } |
| 4258 | |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4259 | static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT, |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4260 | SourceLocation KeyLoc, QualType T) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4261 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4262 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4263 | ASTContext &C = Self.Context; |
| 4264 | switch(UTT) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4265 | default: llvm_unreachable("not a UTT"); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4266 | // Type trait expressions corresponding to the primary type category |
| 4267 | // predicates in C++0x [meta.unary.cat]. |
| 4268 | case UTT_IsVoid: |
| 4269 | return T->isVoidType(); |
| 4270 | case UTT_IsIntegral: |
| 4271 | return T->isIntegralType(C); |
| 4272 | case UTT_IsFloatingPoint: |
| 4273 | return T->isFloatingType(); |
| 4274 | case UTT_IsArray: |
| 4275 | return T->isArrayType(); |
| 4276 | case UTT_IsPointer: |
| 4277 | return T->isPointerType(); |
| 4278 | case UTT_IsLvalueReference: |
| 4279 | return T->isLValueReferenceType(); |
| 4280 | case UTT_IsRvalueReference: |
| 4281 | return T->isRValueReferenceType(); |
| 4282 | case UTT_IsMemberFunctionPointer: |
| 4283 | return T->isMemberFunctionPointerType(); |
| 4284 | case UTT_IsMemberObjectPointer: |
| 4285 | return T->isMemberDataPointerType(); |
| 4286 | case UTT_IsEnum: |
| 4287 | return T->isEnumeralType(); |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4288 | case UTT_IsUnion: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4289 | return T->isUnionType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4290 | case UTT_IsClass: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4291 | return T->isClassType() || T->isStructureType() || T->isInterfaceType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4292 | case UTT_IsFunction: |
| 4293 | return T->isFunctionType(); |
| 4294 | |
| 4295 | // Type trait expressions which correspond to the convenient composition |
| 4296 | // predicates in C++0x [meta.unary.comp]. |
| 4297 | case UTT_IsReference: |
| 4298 | return T->isReferenceType(); |
| 4299 | case UTT_IsArithmetic: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4300 | return T->isArithmeticType() && !T->isEnumeralType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4301 | case UTT_IsFundamental: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4302 | return T->isFundamentalType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4303 | case UTT_IsObject: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4304 | return T->isObjectType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4305 | case UTT_IsScalar: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4306 | // Note: semantic analysis depends on Objective-C lifetime types to be |
| 4307 | // considered scalar types. However, such types do not actually behave |
| 4308 | // like scalar types at run time (since they may require retain/release |
| 4309 | // operations), so we report them as non-scalar. |
| 4310 | if (T->isObjCLifetimeType()) { |
| 4311 | switch (T.getObjCLifetime()) { |
| 4312 | case Qualifiers::OCL_None: |
| 4313 | case Qualifiers::OCL_ExplicitNone: |
| 4314 | return true; |
| 4315 | |
| 4316 | case Qualifiers::OCL_Strong: |
| 4317 | case Qualifiers::OCL_Weak: |
| 4318 | case Qualifiers::OCL_Autoreleasing: |
| 4319 | return false; |
| 4320 | } |
| 4321 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4322 | |
Chandler Carruth | 7ba7bd3 | 2011-05-01 09:29:55 +0000 | [diff] [blame] | 4323 | return T->isScalarType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4324 | case UTT_IsCompound: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4325 | return T->isCompoundType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4326 | case UTT_IsMemberPointer: |
| 4327 | return T->isMemberPointerType(); |
| 4328 | |
| 4329 | // Type trait expressions which correspond to the type property predicates |
| 4330 | // in C++0x [meta.unary.prop]. |
| 4331 | case UTT_IsConst: |
| 4332 | return T.isConstQualified(); |
| 4333 | case UTT_IsVolatile: |
| 4334 | return T.isVolatileQualified(); |
| 4335 | case UTT_IsTrivial: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4336 | return T.isTrivialType(C); |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 4337 | case UTT_IsTriviallyCopyable: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4338 | return T.isTriviallyCopyableType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4339 | case UTT_IsStandardLayout: |
| 4340 | return T->isStandardLayoutType(); |
| 4341 | case UTT_IsPOD: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4342 | return T.isPODType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4343 | case UTT_IsLiteral: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4344 | return T->isLiteralType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4345 | case UTT_IsEmpty: |
| 4346 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4347 | return !RD->isUnion() && RD->isEmpty(); |
| 4348 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4349 | case UTT_IsPolymorphic: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4350 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4351 | return !RD->isUnion() && RD->isPolymorphic(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4352 | return false; |
| 4353 | case UTT_IsAbstract: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4354 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4355 | return !RD->isUnion() && RD->isAbstract(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4356 | return false; |
Eric Fiselier | 0736066 | 2017-04-12 22:12:15 +0000 | [diff] [blame] | 4357 | case UTT_IsAggregate: |
| 4358 | // Report vector extensions and complex types as aggregates because they |
| 4359 | // support aggregate initialization. GCC mirrors this behavior for vectors |
| 4360 | // but not _Complex. |
| 4361 | return T->isAggregateType() || T->isVectorType() || T->isExtVectorType() || |
| 4362 | T->isAnyComplexType(); |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4363 | // __is_interface_class only returns true when CL is invoked in /CLR mode and |
| 4364 | // even then only when it is used with the 'interface struct ...' syntax |
| 4365 | // Clang doesn't support /CLR which makes this type trait moot. |
John McCall | bf4a7d7 | 2012-09-25 07:32:49 +0000 | [diff] [blame] | 4366 | case UTT_IsInterfaceClass: |
John McCall | bf4a7d7 | 2012-09-25 07:32:49 +0000 | [diff] [blame] | 4367 | return false; |
Douglas Gregor | dca70af | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 4368 | case UTT_IsFinal: |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4369 | case UTT_IsSealed: |
| 4370 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4371 | return RD->hasAttr<FinalAttr>(); |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4372 | return false; |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4373 | case UTT_IsSigned: |
| 4374 | return T->isSignedIntegerType(); |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4375 | case UTT_IsUnsigned: |
| 4376 | return T->isUnsignedIntegerType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4377 | |
| 4378 | // Type trait expressions which query classes regarding their construction, |
| 4379 | // destruction, and copying. Rather than being based directly on the |
| 4380 | // related type predicates in the standard, they are specified by both |
| 4381 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those |
| 4382 | // specifications. |
| 4383 | // |
| 4384 | // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html |
| 4385 | // 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] | 4386 | // |
| 4387 | // Note that these builtins do not behave as documented in g++: if a class |
| 4388 | // has both a trivial and a non-trivial special member of a particular kind, |
| 4389 | // they return false! For now, we emulate this behavior. |
| 4390 | // FIXME: This appears to be a g++ bug: more complex cases reveal that it |
| 4391 | // does not correctly compute triviality in the presence of multiple special |
| 4392 | // 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] | 4393 | case UTT_HasTrivialDefaultConstructor: |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4394 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4395 | // If __is_pod (type) is true then the trait is true, else if type is |
| 4396 | // a cv class or union type (or array thereof) with a trivial default |
| 4397 | // constructor ([class.ctor]) then the trait is true, else it is false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4398 | if (T.isPODType(C)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4399 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4400 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4401 | return RD->hasTrivialDefaultConstructor() && |
| 4402 | !RD->hasNonTrivialDefaultConstructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4403 | return false; |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4404 | case UTT_HasTrivialMoveConstructor: |
| 4405 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4406 | // standard library headers. Specifically this is used as the logic |
| 4407 | // behind std::is_trivially_move_constructible (20.9.4.3). |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4408 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4409 | return true; |
| 4410 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4411 | return RD->hasTrivialMoveConstructor() && !RD->hasNonTrivialMoveConstructor(); |
| 4412 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4413 | case UTT_HasTrivialCopy: |
| 4414 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4415 | // If __is_pod (type) is true or type is a reference type then |
| 4416 | // the trait is true, else if type is a cv class or union type |
| 4417 | // with a trivial copy constructor ([class.copy]) then the trait |
| 4418 | // is true, else it is false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4419 | if (T.isPODType(C) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4420 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4421 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4422 | return RD->hasTrivialCopyConstructor() && |
| 4423 | !RD->hasNonTrivialCopyConstructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4424 | return false; |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4425 | case UTT_HasTrivialMoveAssign: |
| 4426 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4427 | // standard library headers. Specifically it is used as the logic |
| 4428 | // behind std::is_trivially_move_assignable (20.9.4.3) |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4429 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4430 | return true; |
| 4431 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4432 | return RD->hasTrivialMoveAssignment() && !RD->hasNonTrivialMoveAssignment(); |
| 4433 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4434 | case UTT_HasTrivialAssign: |
| 4435 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4436 | // If type is const qualified or is a reference type then the |
| 4437 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 4438 | // trait is true, else if type is a cv class or union type with |
| 4439 | // a trivial copy assignment ([class.copy]) then the trait is |
| 4440 | // true, else it is false. |
| 4441 | // Note: the const and reference restrictions are interesting, |
| 4442 | // given that const and reference members don't prevent a class |
| 4443 | // from having a trivial copy assignment operator (but do cause |
| 4444 | // errors if the copy assignment operator is actually used, q.v. |
| 4445 | // [class.copy]p12). |
| 4446 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4447 | if (T.isConstQualified()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4448 | return false; |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4449 | if (T.isPODType(C)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4450 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4451 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4452 | return RD->hasTrivialCopyAssignment() && |
| 4453 | !RD->hasNonTrivialCopyAssignment(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4454 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4455 | case UTT_IsDestructible: |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4456 | case UTT_IsTriviallyDestructible: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4457 | case UTT_IsNothrowDestructible: |
David Majnemer | ac73de9 | 2015-08-11 03:03:28 +0000 | [diff] [blame] | 4458 | // C++14 [meta.unary.prop]: |
| 4459 | // For reference types, is_destructible<T>::value is true. |
| 4460 | if (T->isReferenceType()) |
| 4461 | return true; |
| 4462 | |
| 4463 | // Objective-C++ ARC: autorelease types don't require destruction. |
| 4464 | if (T->isObjCLifetimeType() && |
| 4465 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 4466 | return true; |
| 4467 | |
| 4468 | // C++14 [meta.unary.prop]: |
| 4469 | // For incomplete types and function types, is_destructible<T>::value is |
| 4470 | // false. |
| 4471 | if (T->isIncompleteType() || T->isFunctionType()) |
| 4472 | return false; |
| 4473 | |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4474 | // A type that requires destruction (via a non-trivial destructor or ARC |
| 4475 | // lifetime semantics) is not trivially-destructible. |
| 4476 | if (UTT == UTT_IsTriviallyDestructible && T.isDestructedType()) |
| 4477 | return false; |
| 4478 | |
David Majnemer | ac73de9 | 2015-08-11 03:03:28 +0000 | [diff] [blame] | 4479 | // C++14 [meta.unary.prop]: |
| 4480 | // For object types and given U equal to remove_all_extents_t<T>, if the |
| 4481 | // expression std::declval<U&>().~U() is well-formed when treated as an |
| 4482 | // unevaluated operand (Clause 5), then is_destructible<T>::value is true |
| 4483 | if (auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
| 4484 | CXXDestructorDecl *Destructor = Self.LookupDestructor(RD); |
| 4485 | if (!Destructor) |
| 4486 | return false; |
| 4487 | // C++14 [dcl.fct.def.delete]p2: |
| 4488 | // A program that refers to a deleted function implicitly or |
| 4489 | // explicitly, other than to declare it, is ill-formed. |
| 4490 | if (Destructor->isDeleted()) |
| 4491 | return false; |
| 4492 | if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public) |
| 4493 | return false; |
| 4494 | if (UTT == UTT_IsNothrowDestructible) { |
| 4495 | const FunctionProtoType *CPT = |
| 4496 | Destructor->getType()->getAs<FunctionProtoType>(); |
| 4497 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4498 | if (!CPT || !CPT->isNothrow(C)) |
| 4499 | return false; |
| 4500 | } |
| 4501 | } |
| 4502 | return true; |
| 4503 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4504 | case UTT_HasTrivialDestructor: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4505 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4506 | // If __is_pod (type) is true or type is a reference type |
| 4507 | // then the trait is true, else if type is a cv class or union |
| 4508 | // type (or array thereof) with a trivial destructor |
| 4509 | // ([class.dtor]) then the trait is true, else it is |
| 4510 | // false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4511 | if (T.isPODType(C) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4512 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4513 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4514 | // Objective-C++ ARC: autorelease types don't require destruction. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4515 | if (T->isObjCLifetimeType() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4516 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 4517 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4518 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4519 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4520 | return RD->hasTrivialDestructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4521 | return false; |
| 4522 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 4523 | case UTT_HasNothrowAssign: |
| 4524 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4525 | // If type is const qualified or is a reference type then the |
| 4526 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 4527 | // is true then the trait is true, else if type is a cv class |
| 4528 | // or union type with copy assignment operators that are known |
| 4529 | // not to throw an exception then the trait is true, else it is |
| 4530 | // false. |
| 4531 | if (C.getBaseElementType(T).isConstQualified()) |
| 4532 | return false; |
| 4533 | if (T->isReferenceType()) |
| 4534 | return false; |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4535 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4536 | return true; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4537 | |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4538 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 4539 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
| 4540 | &CXXRecordDecl::hasTrivialCopyAssignment, |
| 4541 | &CXXRecordDecl::hasNonTrivialCopyAssignment, |
| 4542 | &CXXMethodDecl::isCopyAssignmentOperator); |
| 4543 | return false; |
| 4544 | case UTT_HasNothrowMoveAssign: |
| 4545 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4546 | // standard library headers. Specifically this is used as the logic |
| 4547 | // behind std::is_nothrow_move_assignable (20.9.4.3). |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4548 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4549 | return true; |
| 4550 | |
| 4551 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) |
| 4552 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
| 4553 | &CXXRecordDecl::hasTrivialMoveAssignment, |
| 4554 | &CXXRecordDecl::hasNonTrivialMoveAssignment, |
| 4555 | &CXXMethodDecl::isMoveAssignmentOperator); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4556 | return false; |
| 4557 | case UTT_HasNothrowCopy: |
| 4558 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4559 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 4560 | // if type is a cv class or union type with copy constructors that are |
| 4561 | // known not to throw an exception then the trait is true, else it is |
| 4562 | // false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4563 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4564 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4565 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
| 4566 | if (RD->hasTrivialCopyConstructor() && |
| 4567 | !RD->hasNonTrivialCopyConstructor()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4568 | return true; |
| 4569 | |
| 4570 | bool FoundConstructor = false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4571 | unsigned FoundTQs; |
Aaron Ballman | e4de1a51 | 2015-07-21 22:33:52 +0000 | [diff] [blame] | 4572 | for (const auto *ND : Self.LookupConstructors(RD)) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4573 | // A template constructor is never a copy constructor. |
| 4574 | // FIXME: However, it may actually be selected at the actual overload |
| 4575 | // resolution point. |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4576 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4577 | continue; |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4578 | // UsingDecl itself is not a constructor |
| 4579 | if (isa<UsingDecl>(ND)) |
| 4580 | continue; |
| 4581 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4582 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 4583 | FoundConstructor = true; |
| 4584 | const FunctionProtoType *CPT |
| 4585 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4586 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4587 | if (!CPT) |
| 4588 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4589 | // TODO: check whether evaluating default arguments can throw. |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4590 | // For now, we'll be conservative and assume that they can throw. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4591 | if (!CPT->isNothrow(C) || CPT->getNumParams() > 1) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4592 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4593 | } |
| 4594 | } |
| 4595 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4596 | return FoundConstructor; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4597 | } |
| 4598 | return false; |
| 4599 | case UTT_HasNothrowConstructor: |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4600 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4601 | // If __has_trivial_constructor (type) is true then the trait is |
| 4602 | // true, else if type is a cv class or union type (or array |
| 4603 | // thereof) with a default constructor that is known not to |
| 4604 | // throw an exception then the trait is true, else it is false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4605 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4606 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4607 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
| 4608 | if (RD->hasTrivialDefaultConstructor() && |
| 4609 | !RD->hasNonTrivialDefaultConstructor()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4610 | return true; |
| 4611 | |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4612 | bool FoundConstructor = false; |
Aaron Ballman | e4de1a51 | 2015-07-21 22:33:52 +0000 | [diff] [blame] | 4613 | for (const auto *ND : Self.LookupConstructors(RD)) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4614 | // FIXME: In C++0x, a constructor template can be a default constructor. |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4615 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4616 | continue; |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4617 | // UsingDecl itself is not a constructor |
| 4618 | if (isa<UsingDecl>(ND)) |
| 4619 | continue; |
| 4620 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4621 | if (Constructor->isDefaultConstructor()) { |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4622 | FoundConstructor = true; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4623 | const FunctionProtoType *CPT |
| 4624 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4625 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4626 | if (!CPT) |
| 4627 | return false; |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4628 | // FIXME: check whether evaluating default arguments can throw. |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4629 | // For now, we'll be conservative and assume that they can throw. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4630 | if (!CPT->isNothrow(C) || CPT->getNumParams() > 0) |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4631 | return false; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4632 | } |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4633 | } |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4634 | return FoundConstructor; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4635 | } |
| 4636 | return false; |
| 4637 | case UTT_HasVirtualDestructor: |
| 4638 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4639 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 4640 | // then the trait is true, else it is false. |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4641 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
Sebastian Redl | 058fc82 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 4642 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4643 | return Destructor->isVirtual(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4644 | return false; |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4645 | |
| 4646 | // These type trait expressions are modeled on the specifications for the |
| 4647 | // Embarcadero C++0x type trait functions: |
| 4648 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
| 4649 | case UTT_IsCompleteType: |
| 4650 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): |
| 4651 | // Returns True if and only if T is a complete type at the point of the |
| 4652 | // function call. |
| 4653 | return !T->isIncompleteType(); |
Erich Keane | e63e9d7 | 2017-10-24 21:31:50 +0000 | [diff] [blame] | 4654 | case UTT_HasUniqueObjectRepresentations: |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 4655 | return C.hasUniqueObjectRepresentations(T); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4656 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4657 | } |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 4658 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4659 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
| 4660 | QualType RhsT, SourceLocation KeyLoc); |
| 4661 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4662 | static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc, |
| 4663 | ArrayRef<TypeSourceInfo *> Args, |
| 4664 | SourceLocation RParenLoc) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4665 | if (Kind <= UTT_Last) |
| 4666 | return EvaluateUnaryTypeTrait(S, Kind, KWLoc, Args[0]->getType()); |
| 4667 | |
Eric Fiselier | 1af6c11 | 2018-01-12 00:09:37 +0000 | [diff] [blame] | 4668 | // Evaluate BTT_ReferenceBindsToTemporary alongside the IsConstructible |
| 4669 | // traits to avoid duplication. |
| 4670 | if (Kind <= BTT_Last && Kind != BTT_ReferenceBindsToTemporary) |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4671 | return EvaluateBinaryTypeTrait(S, Kind, Args[0]->getType(), |
| 4672 | Args[1]->getType(), RParenLoc); |
| 4673 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4674 | switch (Kind) { |
Eric Fiselier | 1af6c11 | 2018-01-12 00:09:37 +0000 | [diff] [blame] | 4675 | case clang::BTT_ReferenceBindsToTemporary: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4676 | case clang::TT_IsConstructible: |
| 4677 | case clang::TT_IsNothrowConstructible: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4678 | case clang::TT_IsTriviallyConstructible: { |
| 4679 | // C++11 [meta.unary.prop]: |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 4680 | // is_trivially_constructible is defined as: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4681 | // |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 4682 | // is_constructible<T, Args...>::value is true and the variable |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 4683 | // definition for is_constructible, as defined below, is known to call |
| 4684 | // no operation that is not trivial. |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4685 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4686 | // The predicate condition for a template specialization |
| 4687 | // is_constructible<T, Args...> shall be satisfied if and only if the |
| 4688 | // following variable definition would be well-formed for some invented |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4689 | // variable t: |
| 4690 | // |
| 4691 | // T t(create<Args>()...); |
Alp Toker | 40f9b1c | 2013-12-12 21:23:03 +0000 | [diff] [blame] | 4692 | assert(!Args.empty()); |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4693 | |
| 4694 | // Precondition: T and all types in the parameter pack Args shall be |
| 4695 | // complete types, (possibly cv-qualified) void, or arrays of |
| 4696 | // unknown bound. |
Aaron Ballman | 2bf2cad | 2015-07-21 21:18:29 +0000 | [diff] [blame] | 4697 | for (const auto *TSI : Args) { |
| 4698 | QualType ArgTy = TSI->getType(); |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4699 | if (ArgTy->isVoidType() || ArgTy->isIncompleteArrayType()) |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4700 | continue; |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4701 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4702 | if (S.RequireCompleteType(KWLoc, ArgTy, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4703 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4704 | return false; |
| 4705 | } |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4706 | |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4707 | // Make sure the first argument is not incomplete nor a function type. |
| 4708 | QualType T = Args[0]->getType(); |
| 4709 | if (T->isIncompleteType() || T->isFunctionType()) |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4710 | return false; |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4711 | |
Nikola Smiljanic | 1b4b6ba | 2014-04-15 11:30:15 +0000 | [diff] [blame] | 4712 | // Make sure the first argument is not an abstract type. |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4713 | CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
Nikola Smiljanic | 1b4b6ba | 2014-04-15 11:30:15 +0000 | [diff] [blame] | 4714 | if (RD && RD->isAbstract()) |
| 4715 | return false; |
| 4716 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4717 | SmallVector<OpaqueValueExpr, 2> OpaqueArgExprs; |
| 4718 | SmallVector<Expr *, 2> ArgExprs; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4719 | ArgExprs.reserve(Args.size() - 1); |
| 4720 | for (unsigned I = 1, N = Args.size(); I != N; ++I) { |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4721 | QualType ArgTy = Args[I]->getType(); |
| 4722 | if (ArgTy->isObjectType() || ArgTy->isFunctionType()) |
| 4723 | ArgTy = S.Context.getRValueReferenceType(ArgTy); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4724 | OpaqueArgExprs.push_back( |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4725 | OpaqueValueExpr(Args[I]->getTypeLoc().getLocStart(), |
| 4726 | ArgTy.getNonLValueExprType(S.Context), |
| 4727 | Expr::getValueKindForType(ArgTy))); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4728 | } |
Richard Smith | a507bfc | 2014-07-23 20:07:08 +0000 | [diff] [blame] | 4729 | for (Expr &E : OpaqueArgExprs) |
| 4730 | ArgExprs.push_back(&E); |
| 4731 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4732 | // Perform the initialization in an unevaluated context within a SFINAE |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4733 | // trap at translation unit scope. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4734 | EnterExpressionEvaluationContext Unevaluated( |
| 4735 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4736 | Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true); |
| 4737 | Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl()); |
| 4738 | InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0])); |
| 4739 | InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc, |
| 4740 | RParenLoc)); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4741 | InitializationSequence Init(S, To, InitKind, ArgExprs); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4742 | if (Init.Failed()) |
| 4743 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4744 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4745 | ExprResult Result = Init.Perform(S, To, InitKind, ArgExprs); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4746 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 4747 | return false; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4748 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4749 | if (Kind == clang::TT_IsConstructible) |
| 4750 | return true; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4751 | |
Eric Fiselier | 1af6c11 | 2018-01-12 00:09:37 +0000 | [diff] [blame] | 4752 | if (Kind == clang::BTT_ReferenceBindsToTemporary) { |
| 4753 | if (!T->isReferenceType()) |
| 4754 | return false; |
| 4755 | |
| 4756 | return !Init.isDirectReferenceBinding(); |
| 4757 | } |
| 4758 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4759 | if (Kind == clang::TT_IsNothrowConstructible) |
| 4760 | return S.canThrow(Result.get()) == CT_Cannot; |
| 4761 | |
| 4762 | if (Kind == clang::TT_IsTriviallyConstructible) { |
Brian Kelley | 93c640b | 2017-03-29 17:40:35 +0000 | [diff] [blame] | 4763 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
| 4764 | // Objective-C lifetime, this is a non-trivial construction. |
| 4765 | if (T.getNonReferenceType().hasNonTrivialObjCLifetime()) |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4766 | return false; |
| 4767 | |
| 4768 | // The initialization succeeded; now make sure there are no non-trivial |
| 4769 | // calls. |
| 4770 | return !Result.get()->hasNonTrivialCall(S.Context); |
| 4771 | } |
| 4772 | |
| 4773 | llvm_unreachable("unhandled type trait"); |
| 4774 | return false; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4775 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4776 | default: llvm_unreachable("not a TT"); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4777 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4778 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4779 | return false; |
| 4780 | } |
| 4781 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4782 | ExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 4783 | ArrayRef<TypeSourceInfo *> Args, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4784 | SourceLocation RParenLoc) { |
Alp Toker | 5294e6e | 2013-12-25 01:47:02 +0000 | [diff] [blame] | 4785 | QualType ResultType = Context.getLogicalOperationType(); |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4786 | |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4787 | if (Kind <= UTT_Last && !CheckUnaryTypeTraitTypeCompleteness( |
| 4788 | *this, Kind, KWLoc, Args[0]->getType())) |
| 4789 | return ExprError(); |
| 4790 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4791 | bool Dependent = false; |
| 4792 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 4793 | if (Args[I]->getType()->isDependentType()) { |
| 4794 | Dependent = true; |
| 4795 | break; |
| 4796 | } |
| 4797 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4798 | |
| 4799 | bool Result = false; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4800 | if (!Dependent) |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4801 | Result = evaluateTypeTrait(*this, Kind, KWLoc, Args, RParenLoc); |
| 4802 | |
| 4803 | return TypeTraitExpr::Create(Context, ResultType, KWLoc, Kind, Args, |
| 4804 | RParenLoc, Result); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
Alp Toker | 88f64e6 | 2013-12-13 21:19:30 +0000 | [diff] [blame] | 4807 | ExprResult Sema::ActOnTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 4808 | ArrayRef<ParsedType> Args, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4809 | SourceLocation RParenLoc) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4810 | SmallVector<TypeSourceInfo *, 4> ConvertedArgs; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4811 | ConvertedArgs.reserve(Args.size()); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4812 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4813 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 4814 | TypeSourceInfo *TInfo; |
| 4815 | QualType T = GetTypeFromParser(Args[I], &TInfo); |
| 4816 | if (!TInfo) |
| 4817 | TInfo = Context.getTrivialTypeSourceInfo(T, KWLoc); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4818 | |
| 4819 | ConvertedArgs.push_back(TInfo); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4820 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4821 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4822 | return BuildTypeTrait(Kind, KWLoc, ConvertedArgs, RParenLoc); |
| 4823 | } |
| 4824 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4825 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
| 4826 | QualType RhsT, SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4827 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
| 4828 | "Cannot evaluate traits of dependent types"); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4829 | |
| 4830 | switch(BTT) { |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4831 | case BTT_IsBaseOf: { |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4832 | // C++0x [meta.rel]p2 |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4833 | // 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] | 4834 | // Base and Derived are not unions and name the same class type without |
| 4835 | // regard to cv-qualifiers. |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4836 | |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4837 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4838 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
Erik Pilkington | 07f8c43 | 2017-05-10 17:18:56 +0000 | [diff] [blame] | 4839 | if (!rhsRecord || !lhsRecord) { |
| 4840 | const ObjCObjectType *LHSObjTy = LhsT->getAs<ObjCObjectType>(); |
| 4841 | const ObjCObjectType *RHSObjTy = RhsT->getAs<ObjCObjectType>(); |
| 4842 | if (!LHSObjTy || !RHSObjTy) |
| 4843 | return false; |
| 4844 | |
| 4845 | ObjCInterfaceDecl *BaseInterface = LHSObjTy->getInterface(); |
| 4846 | ObjCInterfaceDecl *DerivedInterface = RHSObjTy->getInterface(); |
| 4847 | if (!BaseInterface || !DerivedInterface) |
| 4848 | return false; |
| 4849 | |
| 4850 | if (Self.RequireCompleteType( |
| 4851 | KeyLoc, RhsT, diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4852 | return false; |
| 4853 | |
| 4854 | return BaseInterface->isSuperClassOf(DerivedInterface); |
| 4855 | } |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4856 | |
| 4857 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
| 4858 | == (lhsRecord == rhsRecord)); |
| 4859 | |
| 4860 | if (lhsRecord == rhsRecord) |
| 4861 | return !lhsRecord->getDecl()->isUnion(); |
| 4862 | |
| 4863 | // C++0x [meta.rel]p2: |
| 4864 | // If Base and Derived are class types and are different types |
| 4865 | // (ignoring possible cv-qualifiers) then Derived shall be a |
| 4866 | // complete type. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4867 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4868 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4869 | return false; |
| 4870 | |
| 4871 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) |
| 4872 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); |
| 4873 | } |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4874 | case BTT_IsSame: |
| 4875 | return Self.Context.hasSameType(LhsT, RhsT); |
George Burgess IV | 31ac1fa | 2017-10-16 22:58:37 +0000 | [diff] [blame] | 4876 | case BTT_TypeCompatible: { |
| 4877 | // GCC ignores cv-qualifiers on arrays for this builtin. |
| 4878 | Qualifiers LhsQuals, RhsQuals; |
| 4879 | QualType Lhs = Self.getASTContext().getUnqualifiedArrayType(LhsT, LhsQuals); |
| 4880 | QualType Rhs = Self.getASTContext().getUnqualifiedArrayType(RhsT, RhsQuals); |
| 4881 | return Self.Context.typesAreCompatible(Lhs, Rhs); |
| 4882 | } |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4883 | case BTT_IsConvertible: |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4884 | case BTT_IsConvertibleTo: { |
| 4885 | // C++0x [meta.rel]p4: |
| 4886 | // Given the following function prototype: |
| 4887 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4888 | // template <class T> |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4889 | // typename add_rvalue_reference<T>::type create(); |
| 4890 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4891 | // the predicate condition for a template specialization |
| 4892 | // is_convertible<From, To> shall be satisfied if and only if |
| 4893 | // the return expression in the following code would be |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4894 | // well-formed, including any implicit conversions to the return |
| 4895 | // type of the function: |
| 4896 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4897 | // To test() { |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4898 | // return create<From>(); |
| 4899 | // } |
| 4900 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4901 | // Access checking is performed as if in a context unrelated to To and |
| 4902 | // From. Only the validity of the immediate context of the expression |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4903 | // of the return-statement (including conversions to the return type) |
| 4904 | // is considered. |
| 4905 | // |
| 4906 | // We model the initialization as a copy-initialization of a temporary |
| 4907 | // of the appropriate type, which for this expression is identical to the |
| 4908 | // return statement (since NRVO doesn't apply). |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4909 | |
| 4910 | // Functions aren't allowed to return function or array types. |
| 4911 | if (RhsT->isFunctionType() || RhsT->isArrayType()) |
| 4912 | return false; |
| 4913 | |
| 4914 | // A return statement in a void function must have void type. |
| 4915 | if (RhsT->isVoidType()) |
| 4916 | return LhsT->isVoidType(); |
| 4917 | |
| 4918 | // A function definition requires a complete, non-abstract return type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4919 | if (!Self.isCompleteType(KeyLoc, RhsT) || Self.isAbstractType(KeyLoc, RhsT)) |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4920 | return false; |
| 4921 | |
| 4922 | // Compute the result of add_rvalue_reference. |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4923 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 4924 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4925 | |
| 4926 | // Build a fake source and destination for initialization. |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4927 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 4928 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4929 | Expr::getValueKindForType(LhsT)); |
| 4930 | Expr *FromPtr = &From; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4931 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4932 | SourceLocation())); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4933 | |
| 4934 | // Perform the initialization in an unevaluated context within a SFINAE |
Eli Friedman | a59b190 | 2012-01-25 01:05:57 +0000 | [diff] [blame] | 4935 | // trap at translation unit scope. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4936 | EnterExpressionEvaluationContext Unevaluated( |
| 4937 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 4938 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 4939 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4940 | InitializationSequence Init(Self, To, Kind, FromPtr); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 4941 | if (Init.Failed()) |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4942 | return false; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 4943 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4944 | ExprResult Result = Init.Perform(Self, To, Kind, FromPtr); |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4945 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); |
| 4946 | } |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4947 | |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 4948 | case BTT_IsAssignable: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4949 | case BTT_IsNothrowAssignable: |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4950 | case BTT_IsTriviallyAssignable: { |
| 4951 | // C++11 [meta.unary.prop]p3: |
| 4952 | // is_trivially_assignable is defined as: |
| 4953 | // is_assignable<T, U>::value is true and the assignment, as defined by |
| 4954 | // is_assignable, is known to call no operation that is not trivial |
| 4955 | // |
| 4956 | // is_assignable is defined as: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4957 | // The expression declval<T>() = declval<U>() is well-formed when |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4958 | // treated as an unevaluated operand (Clause 5). |
| 4959 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4960 | // For both, T and U shall be complete types, (possibly cv-qualified) |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4961 | // void, or arrays of unknown bound. |
| 4962 | if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() && |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4963 | Self.RequireCompleteType(KeyLoc, LhsT, |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4964 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4965 | return false; |
| 4966 | if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() && |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4967 | Self.RequireCompleteType(KeyLoc, RhsT, |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4968 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4969 | return false; |
| 4970 | |
| 4971 | // cv void is never assignable. |
| 4972 | if (LhsT->isVoidType() || RhsT->isVoidType()) |
| 4973 | return false; |
| 4974 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4975 | // Build expressions that emulate the effect of declval<T>() and |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4976 | // declval<U>(). |
| 4977 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 4978 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
| 4979 | if (RhsT->isObjectType() || RhsT->isFunctionType()) |
| 4980 | RhsT = Self.Context.getRValueReferenceType(RhsT); |
| 4981 | OpaqueValueExpr Lhs(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
| 4982 | Expr::getValueKindForType(LhsT)); |
| 4983 | OpaqueValueExpr Rhs(KeyLoc, RhsT.getNonLValueExprType(Self.Context), |
| 4984 | Expr::getValueKindForType(RhsT)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4985 | |
| 4986 | // Attempt the assignment in an unevaluated context within a SFINAE |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4987 | // trap at translation unit scope. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4988 | EnterExpressionEvaluationContext Unevaluated( |
| 4989 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4990 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
Erich Keane | 1a3b8fd | 2017-12-12 16:22:31 +0000 | [diff] [blame] | 4991 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4992 | ExprResult Result = Self.BuildBinOp(/*S=*/nullptr, KeyLoc, BO_Assign, &Lhs, |
| 4993 | &Rhs); |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4994 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 4995 | return false; |
| 4996 | |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 4997 | if (BTT == BTT_IsAssignable) |
| 4998 | return true; |
| 4999 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 5000 | if (BTT == BTT_IsNothrowAssignable) |
| 5001 | return Self.canThrow(Result.get()) == CT_Cannot; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 5002 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 5003 | if (BTT == BTT_IsTriviallyAssignable) { |
Brian Kelley | 93c640b | 2017-03-29 17:40:35 +0000 | [diff] [blame] | 5004 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
| 5005 | // Objective-C lifetime, this is a non-trivial assignment. |
| 5006 | if (LhsT.getNonReferenceType().hasNonTrivialObjCLifetime()) |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 5007 | return false; |
| 5008 | |
| 5009 | return !Result.get()->hasNonTrivialCall(Self.Context); |
| 5010 | } |
| 5011 | |
| 5012 | llvm_unreachable("unhandled type trait"); |
| 5013 | return false; |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 5014 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 5015 | default: llvm_unreachable("not a BTT"); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 5016 | } |
| 5017 | llvm_unreachable("Unknown type trait or not implemented"); |
| 5018 | } |
| 5019 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5020 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
| 5021 | SourceLocation KWLoc, |
| 5022 | ParsedType Ty, |
| 5023 | Expr* DimExpr, |
| 5024 | SourceLocation RParen) { |
| 5025 | TypeSourceInfo *TSInfo; |
| 5026 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
| 5027 | if (!TSInfo) |
| 5028 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
| 5029 | |
| 5030 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
| 5031 | } |
| 5032 | |
| 5033 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
| 5034 | QualType T, Expr *DimExpr, |
| 5035 | SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 5036 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5037 | |
| 5038 | switch(ATT) { |
| 5039 | case ATT_ArrayRank: |
| 5040 | if (T->isArrayType()) { |
| 5041 | unsigned Dim = 0; |
| 5042 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 5043 | ++Dim; |
| 5044 | T = AT->getElementType(); |
| 5045 | } |
| 5046 | return Dim; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5047 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 5048 | return 0; |
| 5049 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5050 | case ATT_ArrayExtent: { |
| 5051 | llvm::APSInt Value; |
| 5052 | uint64_t Dim; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5053 | if (Self.VerifyIntegerConstantExpression(DimExpr, &Value, |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5054 | diag::err_dimension_expr_not_constant_integer, |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5055 | false).isInvalid()) |
| 5056 | return 0; |
| 5057 | if (Value.isSigned() && Value.isNegative()) { |
Daniel Dunbar | 900cead | 2012-03-09 21:38:22 +0000 | [diff] [blame] | 5058 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) |
| 5059 | << DimExpr->getSourceRange(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5060 | return 0; |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 5061 | } |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5062 | Dim = Value.getLimitedValue(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5063 | |
| 5064 | if (T->isArrayType()) { |
| 5065 | unsigned D = 0; |
| 5066 | bool Matched = false; |
| 5067 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 5068 | if (Dim == D) { |
| 5069 | Matched = true; |
| 5070 | break; |
| 5071 | } |
| 5072 | ++D; |
| 5073 | T = AT->getElementType(); |
| 5074 | } |
| 5075 | |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 5076 | if (Matched && T->isArrayType()) { |
| 5077 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
| 5078 | return CAT->getSize().getLimitedValue(); |
| 5079 | } |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5080 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 5081 | return 0; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5082 | } |
| 5083 | } |
| 5084 | llvm_unreachable("Unknown type trait or not implemented"); |
| 5085 | } |
| 5086 | |
| 5087 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
| 5088 | SourceLocation KWLoc, |
| 5089 | TypeSourceInfo *TSInfo, |
| 5090 | Expr* DimExpr, |
| 5091 | SourceLocation RParen) { |
| 5092 | QualType T = TSInfo->getType(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5093 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 5094 | // FIXME: This should likely be tracked as an APInt to remove any host |
| 5095 | // assumptions about the width of size_t on the target. |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 5096 | uint64_t Value = 0; |
| 5097 | if (!T->isDependentType()) |
| 5098 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); |
| 5099 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 5100 | // While the specification for these traits from the Embarcadero C++ |
| 5101 | // compiler's documentation says the return type is 'unsigned int', Clang |
| 5102 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero |
| 5103 | // compiler, there is no difference. On several other platforms this is an |
| 5104 | // important distinction. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5105 | return new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, DimExpr, |
| 5106 | RParen, Context.getSizeType()); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5109 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5110 | SourceLocation KWLoc, |
| 5111 | Expr *Queried, |
| 5112 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5113 | // If error parsing the expression, ignore. |
| 5114 | if (!Queried) |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5115 | return ExprError(); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5116 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5117 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5118 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5119 | return Result; |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5122 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
| 5123 | switch (ET) { |
| 5124 | case ET_IsLValueExpr: return E->isLValue(); |
| 5125 | case ET_IsRValueExpr: return E->isRValue(); |
| 5126 | } |
| 5127 | llvm_unreachable("Expression trait not covered by switch"); |
| 5128 | } |
| 5129 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5130 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5131 | SourceLocation KWLoc, |
| 5132 | Expr *Queried, |
| 5133 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5134 | if (Queried->isTypeDependent()) { |
| 5135 | // Delay type-checking for type-dependent expressions. |
| 5136 | } else if (Queried->getType()->isPlaceholderType()) { |
| 5137 | ExprResult PE = CheckPlaceholderExpr(Queried); |
| 5138 | if (PE.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5139 | return BuildExpressionTrait(ET, KWLoc, PE.get(), RParen); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5142 | bool Value = EvaluateExpressionTrait(ET, Queried); |
Chandler Carruth | f57eba3 | 2011-05-01 08:48:19 +0000 | [diff] [blame] | 5143 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5144 | return new (Context) |
| 5145 | ExpressionTraitExpr(KWLoc, ET, Queried, Value, RParen, Context.BoolTy); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5148 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5149 | ExprValueKind &VK, |
| 5150 | SourceLocation Loc, |
| 5151 | bool isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5152 | assert(!LHS.get()->getType()->isPlaceholderType() && |
| 5153 | !RHS.get()->getType()->isPlaceholderType() && |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 5154 | "placeholders should have been weeded out by now"); |
| 5155 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 5156 | // The LHS undergoes lvalue conversions if this is ->*, and undergoes the |
| 5157 | // temporary materialization conversion otherwise. |
| 5158 | if (isIndirect) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5159 | LHS = DefaultLvalueConversion(LHS.get()); |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 5160 | else if (LHS.get()->isRValue()) |
| 5161 | LHS = TemporaryMaterializationConversion(LHS.get()); |
| 5162 | if (LHS.isInvalid()) |
| 5163 | return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 5164 | |
| 5165 | // The RHS always undergoes lvalue conversions. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5166 | RHS = DefaultLvalueConversion(RHS.get()); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5167 | if (RHS.isInvalid()) return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 5168 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5169 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 5170 | // C++ 5.5p2 |
| 5171 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 5172 | // be of type "pointer to member of T" (where T is a completely-defined |
| 5173 | // class type) [...] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5174 | QualType RHSType = RHS.get()->getType(); |
| 5175 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5176 | if (!MemPtr) { |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5177 | Diag(Loc, diag::err_bad_memptr_rhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5178 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5179 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5180 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5181 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5182 | QualType Class(MemPtr->getClass(), 0); |
| 5183 | |
Douglas Gregor | d07ba34 | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 5184 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
| 5185 | // member pointer points must be completely-defined. However, there is no |
| 5186 | // reason for this semantic distinction, and the rule is not enforced by |
| 5187 | // other compilers. Therefore, we do not check this property, as it is |
| 5188 | // likely to be considered a defect. |
Sebastian Redl | c72350e | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 5189 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5190 | // C++ 5.5p2 |
| 5191 | // [...] to its first operand, which shall be of class T or of a class of |
| 5192 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 5193 | // such a class] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5194 | QualType LHSType = LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5195 | if (isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5196 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) |
| 5197 | LHSType = Ptr->getPointeeType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5198 | else { |
| 5199 | Diag(Loc, diag::err_bad_memptr_lhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5200 | << OpSpelling << 1 << LHSType |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5201 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5202 | return QualType(); |
| 5203 | } |
| 5204 | } |
| 5205 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5206 | if (!Context.hasSameUnqualifiedType(Class, LHSType)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 5207 | // If we want to check the hierarchy, we need a complete type. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5208 | if (RequireCompleteType(Loc, LHSType, diag::err_bad_memptr_lhs, |
| 5209 | OpSpelling, (int)isIndirect)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 5210 | return QualType(); |
| 5211 | } |
Richard Smith | db05cd3 | 2013-12-12 03:40:18 +0000 | [diff] [blame] | 5212 | |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5213 | if (!IsDerivedFrom(Loc, LHSType, Class)) { |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5214 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5215 | << (int)isIndirect << LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5216 | return QualType(); |
| 5217 | } |
Richard Smith | db05cd3 | 2013-12-12 03:40:18 +0000 | [diff] [blame] | 5218 | |
| 5219 | CXXCastPath BasePath; |
| 5220 | if (CheckDerivedToBaseConversion(LHSType, Class, Loc, |
| 5221 | SourceRange(LHS.get()->getLocStart(), |
| 5222 | RHS.get()->getLocEnd()), |
| 5223 | &BasePath)) |
| 5224 | return QualType(); |
| 5225 | |
Eli Friedman | 1fcf66b | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 5226 | // Cast LHS to type of use. |
Richard Smith | 01e4a7f2 | 2017-06-09 22:25:28 +0000 | [diff] [blame] | 5227 | QualType UseType = Context.getQualifiedType(Class, LHSType.getQualifiers()); |
| 5228 | if (isIndirect) |
| 5229 | UseType = Context.getPointerType(UseType); |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5230 | ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5231 | LHS = ImpCastExprToType(LHS.get(), UseType, CK_DerivedToBase, VK, |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5232 | &BasePath); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5235 | if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) { |
Fariborz Jahanian | 1bc0f9a | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 5236 | // Diagnose use of pointer-to-member type which when used as |
| 5237 | // the functional cast in a pointer-to-member expression. |
| 5238 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 5239 | return QualType(); |
| 5240 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5241 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5242 | // C++ 5.5p2 |
| 5243 | // The result is an object or a function of the type specified by the |
| 5244 | // second operand. |
| 5245 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 5246 | // in accordance with 5.5p5 and 5.2.5. |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5247 | QualType Result = MemPtr->getPointeeType(); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5248 | Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5249 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5250 | // C++0x [expr.mptr.oper]p6: |
| 5251 | // In a .* expression whose object expression is an rvalue, the program is |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5252 | // ill-formed if the second operand is a pointer to member function with |
| 5253 | // ref-qualifier &. In a ->* expression or in a .* expression whose object |
| 5254 | // 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] | 5255 | // is a pointer to member function with ref-qualifier &&. |
| 5256 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
| 5257 | switch (Proto->getRefQualifier()) { |
| 5258 | case RQ_None: |
| 5259 | // Do nothing |
| 5260 | break; |
| 5261 | |
| 5262 | case RQ_LValue: |
Richard Smith | 2592327 | 2017-08-25 01:47:55 +0000 | [diff] [blame] | 5263 | if (!isIndirect && !LHS.get()->Classify(Context).isLValue()) { |
| 5264 | // C++2a allows functions with ref-qualifier & if they are also 'const'. |
| 5265 | if (Proto->isConst()) |
| 5266 | Diag(Loc, getLangOpts().CPlusPlus2a |
| 5267 | ? diag::warn_cxx17_compat_pointer_to_const_ref_member_on_rvalue |
| 5268 | : diag::ext_pointer_to_const_ref_member_on_rvalue); |
| 5269 | else |
| 5270 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
| 5271 | << RHSType << 1 << LHS.get()->getSourceRange(); |
| 5272 | } |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5273 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5274 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5275 | case RQ_RValue: |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5276 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5277 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5278 | << RHSType << 0 << LHS.get()->getSourceRange(); |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5279 | break; |
| 5280 | } |
| 5281 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5282 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5283 | // C++ [expr.mptr.oper]p6: |
| 5284 | // The result of a .* expression whose second operand is a pointer |
| 5285 | // to a data member is of the same value category as its |
| 5286 | // first operand. The result of a .* expression whose second |
| 5287 | // operand is a pointer to a member function is a prvalue. The |
| 5288 | // result of an ->* expression is an lvalue if its second operand |
| 5289 | // is a pointer to data member and a prvalue otherwise. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5290 | if (Result->isFunctionType()) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5291 | VK = VK_RValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5292 | return Context.BoundMemberTy; |
| 5293 | } else if (isIndirect) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5294 | VK = VK_LValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5295 | } else { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5296 | VK = LHS.get()->getValueKind(); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5297 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5298 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5299 | return Result; |
| 5300 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5301 | |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5302 | /// \brief 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] | 5303 | /// |
| 5304 | /// This is part of the parameter validation for the ? operator. If either |
| 5305 | /// value operand is a class type, the two operands are attempted to be |
| 5306 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5307 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 5308 | /// as such. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5309 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 5310 | SourceLocation QuestionLoc, |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5311 | bool &HaveConversion, |
| 5312 | QualType &ToType) { |
| 5313 | HaveConversion = false; |
| 5314 | ToType = To->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5315 | |
| 5316 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5317 | SourceLocation()); |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5318 | // C++11 5.16p3 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5319 | // The process for determining whether an operand expression E1 of type T1 |
| 5320 | // can be converted to match an operand expression E2 of type T2 is defined |
| 5321 | // as follows: |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5322 | // -- If E2 is an lvalue: E1 can be converted to match E2 if E1 can be |
| 5323 | // implicitly converted to type "lvalue reference to T2", subject to the |
| 5324 | // constraint that in the conversion the reference must bind directly to |
| 5325 | // an lvalue. |
| 5326 | // -- If E2 is an xvalue: E1 can be converted to match E2 if E1 can be |
| 5327 | // implicitly conveted to the type "rvalue reference to R2", subject to |
| 5328 | // the constraint that the reference must bind directly. |
| 5329 | if (To->isLValue() || To->isXValue()) { |
| 5330 | QualType T = To->isLValue() ? Self.Context.getLValueReferenceType(ToType) |
| 5331 | : Self.Context.getRValueReferenceType(ToType); |
| 5332 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5333 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5334 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5335 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5336 | if (InitSeq.isDirectReferenceBinding()) { |
| 5337 | ToType = T; |
| 5338 | HaveConversion = true; |
| 5339 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5340 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5341 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5342 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5343 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5344 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5345 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5346 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 5347 | // -- if E1 and E2 have class type, and the underlying class types are |
| 5348 | // the same or one is a base class of the other: |
| 5349 | QualType FTy = From->getType(); |
| 5350 | QualType TTy = To->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5351 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 5352 | const RecordType *TRec = TTy->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5353 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5354 | Self.IsDerivedFrom(QuestionLoc, FTy, TTy); |
| 5355 | if (FRec && TRec && (FRec == TRec || FDerivedFromT || |
| 5356 | Self.IsDerivedFrom(QuestionLoc, TTy, FTy))) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5357 | // E1 can be converted to match E2 if the class of T2 is the |
| 5358 | // same type as, or a base class of, the class of T1, and |
| 5359 | // [cv2 > cv1]. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5360 | if (FRec == TRec || FDerivedFromT) { |
| 5361 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5362 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5363 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5364 | if (InitSeq) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5365 | HaveConversion = true; |
| 5366 | return false; |
| 5367 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5368 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5369 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5370 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5371 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5372 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5373 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5374 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5375 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5376 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5377 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 5378 | // implicitly converted to the type that expression E2 would have |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5379 | // 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] | 5380 | // an rvalue). |
| 5381 | // |
| 5382 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 5383 | // to the array-to-pointer or function-to-pointer conversions. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5384 | TTy = TTy.getNonLValueExprType(Self.Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5385 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5386 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5387 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5388 | HaveConversion = !InitSeq.Failed(); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5389 | ToType = TTy; |
| 5390 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5391 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5392 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5393 | return false; |
| 5394 | } |
| 5395 | |
| 5396 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 5397 | /// |
| 5398 | /// This is part of the parameter validation for the ? operator. If either |
| 5399 | /// value operand is a class type, overload resolution is used to find a |
| 5400 | /// conversion to a common type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5401 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5402 | SourceLocation QuestionLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5403 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5404 | OverloadCandidateSet CandidateSet(QuestionLoc, |
| 5405 | OverloadCandidateSet::CSK_Operator); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 5406 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5407 | CandidateSet); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5408 | |
| 5409 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5410 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5411 | case OR_Success: { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5412 | // 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] | 5413 | ExprResult LHSRes = Self.PerformImplicitConversion( |
| 5414 | LHS.get(), Best->BuiltinParamTypes[0], Best->Conversions[0], |
| 5415 | Sema::AA_Converting); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5416 | if (LHSRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5417 | break; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5418 | LHS = LHSRes; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5419 | |
George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 5420 | ExprResult RHSRes = Self.PerformImplicitConversion( |
| 5421 | RHS.get(), Best->BuiltinParamTypes[1], Best->Conversions[1], |
| 5422 | Sema::AA_Converting); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5423 | if (RHSRes.isInvalid()) |
| 5424 | break; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5425 | RHS = RHSRes; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 5426 | if (Best->Function) |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5427 | Self.MarkFunctionReferenced(QuestionLoc, Best->Function); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5428 | return false; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5429 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5430 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5431 | case OR_No_Viable_Function: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5432 | |
| 5433 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5434 | // constant and the other is a pointer type. In this case, the user most |
| 5435 | // likely forgot to take the address of the other expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5436 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5437 | return true; |
| 5438 | |
| 5439 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5440 | << LHS.get()->getType() << RHS.get()->getType() |
| 5441 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5442 | return true; |
| 5443 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5444 | case OR_Ambiguous: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5445 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5446 | << LHS.get()->getType() << RHS.get()->getType() |
| 5447 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5448 | // FIXME: Print the possible common types by printing the return types of |
| 5449 | // the viable candidates. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5450 | break; |
| 5451 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5452 | case OR_Deleted: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5453 | llvm_unreachable("Conditional operator has only built-in overloads"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5454 | } |
| 5455 | return true; |
| 5456 | } |
| 5457 | |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5458 | /// \brief Perform an "extended" implicit conversion as returned by |
| 5459 | /// TryClassUnification. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5460 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5461 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5462 | InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5463 | SourceLocation()); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5464 | Expr *Arg = E.get(); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5465 | InitializationSequence InitSeq(Self, Entity, Kind, Arg); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5466 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, Arg); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5467 | if (Result.isInvalid()) |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5468 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5469 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5470 | E = Result; |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5471 | return false; |
| 5472 | } |
| 5473 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5474 | /// \brief Check the operands of ?: under C++ semantics. |
| 5475 | /// |
| 5476 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 5477 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5478 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 5479 | ExprResult &RHS, ExprValueKind &VK, |
| 5480 | ExprObjectKind &OK, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5481 | SourceLocation QuestionLoc) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5482 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 5483 | // interface pointers. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5484 | |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5485 | // C++11 [expr.cond]p1 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5486 | // The first expression is contextually converted to bool. |
Simon Dardis | 7cd5876 | 2017-05-12 19:11:06 +0000 | [diff] [blame] | 5487 | // |
| 5488 | // FIXME; GCC's vector extension permits the use of a?b:c where the type of |
| 5489 | // a is that of a integer vector with the same number of elements and |
| 5490 | // size as the vectors of b and c. If one of either b or c is a scalar |
| 5491 | // it is implicitly converted to match the type of the vector. |
| 5492 | // Otherwise the expression is ill-formed. If both b and c are scalars, |
| 5493 | // then b and c are checked and converted to the type of a if possible. |
| 5494 | // Unlike the OpenCL ?: operator, the expression is evaluated as |
| 5495 | // (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] | 5496 | if (!Cond.get()->isTypeDependent()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5497 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5498 | if (CondRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5499 | return QualType(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5500 | Cond = CondRes; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5503 | // Assume r-value. |
| 5504 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5505 | OK = OK_Ordinary; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5506 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5507 | // Either of the arguments dependent? |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5508 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5509 | return Context.DependentTy; |
| 5510 | |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5511 | // C++11 [expr.cond]p2 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5512 | // If either the second or the third operand has type (cv) void, ... |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5513 | QualType LTy = LHS.get()->getType(); |
| 5514 | QualType RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5515 | bool LVoid = LTy->isVoidType(); |
| 5516 | bool RVoid = RTy->isVoidType(); |
| 5517 | if (LVoid || RVoid) { |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 5518 | // ... one of the following shall hold: |
| 5519 | // -- The second or the third operand (but not both) is a (possibly |
| 5520 | // parenthesized) throw-expression; the result is of the type |
| 5521 | // and value category of the other. |
| 5522 | bool LThrow = isa<CXXThrowExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 5523 | bool RThrow = isa<CXXThrowExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 5524 | if (LThrow != RThrow) { |
| 5525 | Expr *NonThrow = LThrow ? RHS.get() : LHS.get(); |
| 5526 | VK = NonThrow->getValueKind(); |
| 5527 | // DR (no number yet): the result is a bit-field if the |
| 5528 | // non-throw-expression operand is a bit-field. |
| 5529 | OK = NonThrow->getObjectKind(); |
| 5530 | return NonThrow->getType(); |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5533 | // -- 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] | 5534 | // type void and is a prvalue. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5535 | if (LVoid && RVoid) |
| 5536 | return Context.VoidTy; |
| 5537 | |
| 5538 | // Neither holds, error. |
| 5539 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 5540 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5541 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5542 | return QualType(); |
| 5543 | } |
| 5544 | |
| 5545 | // Neither is void. |
| 5546 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5547 | // C++11 [expr.cond]p3 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5548 | // Otherwise, if the second and third operand have different types, and |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5549 | // either has (cv) class type [...] an attempt is made to convert each of |
| 5550 | // those operands to the type of the other. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5551 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5552 | (LTy->isRecordType() || RTy->isRecordType())) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5553 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5554 | QualType L2RType, R2LType; |
| 5555 | bool HaveL2R, HaveR2L; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5556 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5557 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5558 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5559 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5560 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5561 | // If both can be converted, [...] the program is ill-formed. |
| 5562 | if (HaveL2R && HaveR2L) { |
| 5563 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5564 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5565 | return QualType(); |
| 5566 | } |
| 5567 | |
| 5568 | // If exactly one conversion is possible, that conversion is applied to |
| 5569 | // the chosen operand and the converted operands are used in place of the |
| 5570 | // original operands for the remainder of this section. |
| 5571 | if (HaveL2R) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5572 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5573 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5574 | LTy = LHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5575 | } else if (HaveR2L) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5576 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5577 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5578 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5579 | } |
| 5580 | } |
| 5581 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5582 | // C++11 [expr.cond]p3 |
| 5583 | // if both are glvalues of the same value category and the same type except |
| 5584 | // for cv-qualification, an attempt is made to convert each of those |
| 5585 | // operands to the type of the other. |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5586 | // FIXME: |
| 5587 | // Resolving a defect in P0012R1: we extend this to cover all cases where |
| 5588 | // one of the operands is reference-compatible with the other, in order |
| 5589 | // to support conditionals between functions differing in noexcept. |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5590 | ExprValueKind LVK = LHS.get()->getValueKind(); |
| 5591 | ExprValueKind RVK = RHS.get()->getValueKind(); |
| 5592 | if (!Context.hasSameType(LTy, RTy) && |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5593 | LVK == RVK && LVK != VK_RValue) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5594 | // DerivedToBase was already handled by the class-specific case above. |
| 5595 | // FIXME: Should we allow ObjC conversions here? |
| 5596 | bool DerivedToBase, ObjCConversion, ObjCLifetimeConversion; |
| 5597 | if (CompareReferenceRelationship( |
| 5598 | QuestionLoc, LTy, RTy, DerivedToBase, |
| 5599 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5600 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
| 5601 | // [...] subject to the constraint that the reference must bind |
| 5602 | // directly [...] |
| 5603 | !RHS.get()->refersToBitField() && |
| 5604 | !RHS.get()->refersToVectorElement()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5605 | RHS = ImpCastExprToType(RHS.get(), LTy, CK_NoOp, RVK); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5606 | RTy = RHS.get()->getType(); |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5607 | } else if (CompareReferenceRelationship( |
| 5608 | QuestionLoc, RTy, LTy, DerivedToBase, |
| 5609 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5610 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
| 5611 | !LHS.get()->refersToBitField() && |
| 5612 | !LHS.get()->refersToVectorElement()) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5613 | LHS = ImpCastExprToType(LHS.get(), RTy, CK_NoOp, LVK); |
| 5614 | LTy = LHS.get()->getType(); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5615 | } |
| 5616 | } |
| 5617 | |
| 5618 | // C++11 [expr.cond]p4 |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5619 | // If the second and third operands are glvalues of the same value |
| 5620 | // category and have the same type, the result is of that type and |
| 5621 | // value category and it is a bit-field if the second or the third |
| 5622 | // operand is a bit-field, or if both are bit-fields. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5623 | // We only extend this to bitfields, not to the crazy other kinds of |
| 5624 | // l-values. |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 5625 | bool Same = Context.hasSameType(LTy, RTy); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5626 | if (Same && LVK == RVK && LVK != VK_RValue && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5627 | LHS.get()->isOrdinaryOrBitFieldObject() && |
| 5628 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
| 5629 | VK = LHS.get()->getValueKind(); |
| 5630 | if (LHS.get()->getObjectKind() == OK_BitField || |
| 5631 | RHS.get()->getObjectKind() == OK_BitField) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5632 | OK = OK_BitField; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5633 | |
| 5634 | // If we have function pointer types, unify them anyway to unify their |
| 5635 | // exception specifications, if any. |
| 5636 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
| 5637 | Qualifiers Qs = LTy.getQualifiers(); |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5638 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5639 | /*ConvertArgs*/false); |
| 5640 | LTy = Context.getQualifiedType(LTy, Qs); |
| 5641 | |
| 5642 | assert(!LTy.isNull() && "failed to find composite pointer type for " |
| 5643 | "canonically equivalent function ptr types"); |
| 5644 | assert(Context.hasSameType(LTy, RTy) && "bad composite pointer type"); |
| 5645 | } |
| 5646 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5647 | return LTy; |
Fariborz Jahanian | c60da03 | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 5648 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5649 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5650 | // C++11 [expr.cond]p5 |
| 5651 | // Otherwise, the result is a prvalue. If the second and third operands |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5652 | // do not have the same type, and either has (cv) class type, ... |
| 5653 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 5654 | // ... overload resolution is used to determine the conversions (if any) |
| 5655 | // to be applied to the operands. If the overload resolution fails, the |
| 5656 | // program is ill-formed. |
| 5657 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 5658 | return QualType(); |
| 5659 | } |
| 5660 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5661 | // C++11 [expr.cond]p6 |
| 5662 | // Lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5663 | // conversions are performed on the second and third operands. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5664 | LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); |
| 5665 | RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5666 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5667 | return QualType(); |
| 5668 | LTy = LHS.get()->getType(); |
| 5669 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5670 | |
| 5671 | // After those conversions, one of the following shall hold: |
| 5672 | // -- The second and third operands have the same type; the result |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5673 | // is of that type. If the operands have class type, the result |
| 5674 | // is a prvalue temporary of the result type, which is |
| 5675 | // copy-initialized from either the second operand or the third |
| 5676 | // operand depending on the value of the first operand. |
| 5677 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 5678 | if (LTy->isRecordType()) { |
| 5679 | // The operands have class type. Make a temporary copy. |
| 5680 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
David Blaikie | 6154ef9 | 2012-09-10 22:05:41 +0000 | [diff] [blame] | 5681 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5682 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
| 5683 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5684 | LHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5685 | if (LHSCopy.isInvalid()) |
| 5686 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | |
| 5688 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
| 5689 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5690 | RHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5691 | if (RHSCopy.isInvalid()) |
| 5692 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5693 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5694 | LHS = LHSCopy; |
| 5695 | RHS = RHSCopy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5698 | // If we have function pointer types, unify them anyway to unify their |
| 5699 | // exception specifications, if any. |
| 5700 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
| 5701 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS); |
| 5702 | assert(!LTy.isNull() && "failed to find composite pointer type for " |
| 5703 | "canonically equivalent function ptr types"); |
| 5704 | } |
| 5705 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5706 | return LTy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5707 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5708 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 5709 | // Extension: conditional operator involving vector types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5710 | if (LTy->isVectorType() || RTy->isVectorType()) |
Ulrich Weigand | 3c5038a | 2015-07-30 14:08:36 +0000 | [diff] [blame] | 5711 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false, |
| 5712 | /*AllowBothBool*/true, |
| 5713 | /*AllowBoolConversions*/false); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 5714 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5715 | // -- The second and third operands have arithmetic or enumeration type; |
| 5716 | // the usual arithmetic conversions are performed to bring them to a |
| 5717 | // common type, and the result is of that type. |
| 5718 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 5719 | QualType ResTy = UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5720 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5721 | return QualType(); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5722 | if (ResTy.isNull()) { |
| 5723 | Diag(QuestionLoc, |
| 5724 | diag::err_typecheck_cond_incompatible_operands) << LTy << RTy |
| 5725 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5726 | return QualType(); |
| 5727 | } |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 5728 | |
| 5729 | LHS = ImpCastExprToType(LHS.get(), ResTy, PrepareScalarCast(LHS, ResTy)); |
| 5730 | RHS = ImpCastExprToType(RHS.get(), ResTy, PrepareScalarCast(RHS, ResTy)); |
| 5731 | |
| 5732 | return ResTy; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5733 | } |
| 5734 | |
| 5735 | // -- The second and third operands have pointer type, or one has pointer |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5736 | // type and the other is a null pointer constant, or both are null |
| 5737 | // pointer constants, at least one of which is non-integral; pointer |
| 5738 | // conversions and qualification conversions are performed to bring them |
| 5739 | // to their composite pointer type. The result is of the composite |
| 5740 | // pointer type. |
Eli Friedman | 81390df | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 5741 | // -- The second and third operands have pointer to member type, or one has |
| 5742 | // pointer to member type and the other is a null pointer constant; |
| 5743 | // pointer to member conversions and qualification conversions are |
| 5744 | // performed to bring them to a common type, whose cv-qualification |
| 5745 | // shall match the cv-qualification of either the second or the third |
| 5746 | // operand. The result is of the common type. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5747 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS); |
| 5748 | if (!Composite.isNull()) |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5749 | return Composite; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5750 | |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 5751 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 798d2bd | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 5752 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 5753 | if (!Composite.isNull()) |
| 5754 | return Composite; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5755 | |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 5756 | // Check if we are using a null with a non-pointer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5757 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 5758 | return QualType(); |
| 5759 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5760 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5761 | << LHS.get()->getType() << RHS.get()->getType() |
| 5762 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5763 | return QualType(); |
| 5764 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5765 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5766 | static FunctionProtoType::ExceptionSpecInfo |
| 5767 | mergeExceptionSpecs(Sema &S, FunctionProtoType::ExceptionSpecInfo ESI1, |
| 5768 | FunctionProtoType::ExceptionSpecInfo ESI2, |
| 5769 | SmallVectorImpl<QualType> &ExceptionTypeStorage) { |
| 5770 | ExceptionSpecificationType EST1 = ESI1.Type; |
| 5771 | ExceptionSpecificationType EST2 = ESI2.Type; |
| 5772 | |
| 5773 | // If either of them can throw anything, that is the result. |
| 5774 | if (EST1 == EST_None) return ESI1; |
| 5775 | if (EST2 == EST_None) return ESI2; |
| 5776 | if (EST1 == EST_MSAny) return ESI1; |
| 5777 | if (EST2 == EST_MSAny) return ESI2; |
| 5778 | |
| 5779 | // If either of them is non-throwing, the result is the other. |
| 5780 | if (EST1 == EST_DynamicNone) return ESI2; |
| 5781 | if (EST2 == EST_DynamicNone) return ESI1; |
| 5782 | if (EST1 == EST_BasicNoexcept) return ESI2; |
| 5783 | if (EST2 == EST_BasicNoexcept) return ESI1; |
| 5784 | |
| 5785 | // If either of them is a non-value-dependent computed noexcept, that |
| 5786 | // determines the result. |
| 5787 | if (EST2 == EST_ComputedNoexcept && ESI2.NoexceptExpr && |
| 5788 | !ESI2.NoexceptExpr->isValueDependent()) |
| 5789 | return !ESI2.NoexceptExpr->EvaluateKnownConstInt(S.Context) ? ESI2 : ESI1; |
| 5790 | if (EST1 == EST_ComputedNoexcept && ESI1.NoexceptExpr && |
| 5791 | !ESI1.NoexceptExpr->isValueDependent()) |
| 5792 | return !ESI1.NoexceptExpr->EvaluateKnownConstInt(S.Context) ? ESI1 : ESI2; |
| 5793 | // If we're left with value-dependent computed noexcept expressions, we're |
| 5794 | // stuck. Before C++17, we can just drop the exception specification entirely, |
| 5795 | // since it's not actually part of the canonical type. And this should never |
| 5796 | // happen in C++17, because it would mean we were computing the composite |
| 5797 | // pointer type of dependent types, which should never happen. |
| 5798 | if (EST1 == EST_ComputedNoexcept || EST2 == EST_ComputedNoexcept) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5799 | assert(!S.getLangOpts().CPlusPlus17 && |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5800 | "computing composite pointer type of dependent types"); |
| 5801 | return FunctionProtoType::ExceptionSpecInfo(); |
| 5802 | } |
| 5803 | |
| 5804 | // Switch over the possibilities so that people adding new values know to |
| 5805 | // update this function. |
| 5806 | switch (EST1) { |
| 5807 | case EST_None: |
| 5808 | case EST_DynamicNone: |
| 5809 | case EST_MSAny: |
| 5810 | case EST_BasicNoexcept: |
| 5811 | case EST_ComputedNoexcept: |
| 5812 | llvm_unreachable("handled above"); |
| 5813 | |
| 5814 | case EST_Dynamic: { |
| 5815 | // This is the fun case: both exception specifications are dynamic. Form |
| 5816 | // the union of the two lists. |
| 5817 | assert(EST2 == EST_Dynamic && "other cases should already be handled"); |
| 5818 | llvm::SmallPtrSet<QualType, 8> Found; |
| 5819 | for (auto &Exceptions : {ESI1.Exceptions, ESI2.Exceptions}) |
| 5820 | for (QualType E : Exceptions) |
| 5821 | if (Found.insert(S.Context.getCanonicalType(E)).second) |
| 5822 | ExceptionTypeStorage.push_back(E); |
| 5823 | |
| 5824 | FunctionProtoType::ExceptionSpecInfo Result(EST_Dynamic); |
| 5825 | Result.Exceptions = ExceptionTypeStorage; |
| 5826 | return Result; |
| 5827 | } |
| 5828 | |
| 5829 | case EST_Unevaluated: |
| 5830 | case EST_Uninstantiated: |
| 5831 | case EST_Unparsed: |
| 5832 | llvm_unreachable("shouldn't see unresolved exception specifications here"); |
| 5833 | } |
| 5834 | |
| 5835 | llvm_unreachable("invalid ExceptionSpecificationType"); |
| 5836 | } |
| 5837 | |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5838 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 5839 | /// |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5840 | /// 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] | 5841 | /// 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] | 5842 | /// type and returns it. |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5843 | /// It does not emit diagnostics. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5844 | /// |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5845 | /// \param Loc The location of the operator requiring these two expressions to |
| 5846 | /// be converted to the composite pointer type. |
| 5847 | /// |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5848 | /// \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] | 5849 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5850 | Expr *&E1, Expr *&E2, |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5851 | bool ConvertArgs) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5852 | assert(getLangOpts().CPlusPlus && "This function assumes C++"); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5853 | |
| 5854 | // C++1z [expr]p14: |
| 5855 | // The composite pointer type of two operands p1 and p2 having types T1 |
| 5856 | // and T2 |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5857 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5858 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5859 | // where at least one is a pointer or pointer to member type or |
| 5860 | // std::nullptr_t is: |
| 5861 | bool T1IsPointerLike = T1->isAnyPointerType() || T1->isMemberPointerType() || |
| 5862 | T1->isNullPtrType(); |
| 5863 | bool T2IsPointerLike = T2->isAnyPointerType() || T2->isMemberPointerType() || |
| 5864 | T2->isNullPtrType(); |
| 5865 | if (!T1IsPointerLike && !T2IsPointerLike) |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5866 | return QualType(); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5867 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5868 | // - if both p1 and p2 are null pointer constants, std::nullptr_t; |
| 5869 | // This can't actually happen, following the standard, but we also use this |
| 5870 | // to implement the end of [expr.conv], which hits this case. |
| 5871 | // |
| 5872 | // - if either p1 or p2 is a null pointer constant, T2 or T1, respectively; |
| 5873 | if (T1IsPointerLike && |
| 5874 | E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5875 | if (ConvertArgs) |
| 5876 | E2 = ImpCastExprToType(E2, T1, T1->isMemberPointerType() |
| 5877 | ? CK_NullToMemberPointer |
| 5878 | : CK_NullToPointer).get(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5879 | return T1; |
| 5880 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5881 | if (T2IsPointerLike && |
| 5882 | E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5883 | if (ConvertArgs) |
| 5884 | E1 = ImpCastExprToType(E1, T2, T2->isMemberPointerType() |
| 5885 | ? CK_NullToMemberPointer |
| 5886 | : CK_NullToPointer).get(); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5887 | return T2; |
| 5888 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5889 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5890 | // Now both have to be pointers or member pointers. |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5891 | if (!T1IsPointerLike || !T2IsPointerLike) |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5892 | return QualType(); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5893 | assert(!T1->isNullPtrType() && !T2->isNullPtrType() && |
| 5894 | "nullptr_t should be a null pointer constant"); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5895 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5896 | // - if T1 or T2 is "pointer to cv1 void" and the other type is |
| 5897 | // "pointer to cv2 T", "pointer to cv12 void", where cv12 is |
| 5898 | // the union of cv1 and cv2; |
| 5899 | // - if T1 or T2 is "pointer to noexcept function" and the other type is |
| 5900 | // "pointer to function", where the function types are otherwise the same, |
| 5901 | // "pointer to function"; |
| 5902 | // FIXME: This rule is defective: it should also permit removing noexcept |
| 5903 | // from a pointer to member function. As a Clang extension, we also |
| 5904 | // permit removing 'noreturn', so we generalize this rule to; |
| 5905 | // - [Clang] If T1 and T2 are both of type "pointer to function" or |
| 5906 | // "pointer to member function" and the pointee types can be unified |
| 5907 | // by a function pointer conversion, that conversion is applied |
| 5908 | // before checking the following rules. |
| 5909 | // - if T1 is "pointer to cv1 C1" and T2 is "pointer to cv2 C2", where C1 |
| 5910 | // is reference-related to C2 or C2 is reference-related to C1 (8.6.3), |
| 5911 | // the cv-combined type of T1 and T2 or the cv-combined type of T2 and T1, |
| 5912 | // respectively; |
| 5913 | // - if T1 is "pointer to member of C1 of type cv1 U1" and T2 is "pointer |
| 5914 | // to member of C2 of type cv2 U2" where C1 is reference-related to C2 or |
| 5915 | // C2 is reference-related to C1 (8.6.3), the cv-combined type of T2 and |
| 5916 | // T1 or the cv-combined type of T1 and T2, respectively; |
| 5917 | // - if T1 and T2 are similar types (4.5), the cv-combined type of T1 and |
| 5918 | // T2; |
| 5919 | // |
| 5920 | // If looked at in the right way, these bullets all do the same thing. |
| 5921 | // What we do here is, we build the two possible cv-combined types, and try |
| 5922 | // the conversions in both directions. If only one works, or if the two |
| 5923 | // composite types are the same, we have succeeded. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5924 | // FIXME: extended qualifiers? |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5925 | // |
| 5926 | // Note that this will fail to find a composite pointer type for "pointer |
| 5927 | // to void" and "pointer to function". We can't actually perform the final |
| 5928 | // conversion in this case, even though a composite pointer type formally |
| 5929 | // exists. |
| 5930 | SmallVector<unsigned, 4> QualifierUnion; |
| 5931 | SmallVector<std::pair<const Type *, const Type *>, 4> MemberOfClass; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5932 | QualType Composite1 = T1; |
| 5933 | QualType Composite2 = T2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5934 | unsigned NeedConstBefore = 0; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5935 | while (true) { |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5936 | const PointerType *Ptr1, *Ptr2; |
| 5937 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 5938 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 5939 | Composite1 = Ptr1->getPointeeType(); |
| 5940 | Composite2 = Ptr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5941 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5942 | // 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] | 5943 | // of where we need to fill in additional 'const' qualifiers. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5944 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5945 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5946 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5947 | QualifierUnion.push_back( |
| 5948 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5949 | MemberOfClass.push_back(std::make_pair(nullptr, nullptr)); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5950 | continue; |
| 5951 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5952 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5953 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 5954 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 5955 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 5956 | Composite1 = MemPtr1->getPointeeType(); |
| 5957 | Composite2 = MemPtr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5958 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5959 | // 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] | 5960 | // of where we need to fill in additional 'const' qualifiers. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5961 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5962 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5963 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5964 | QualifierUnion.push_back( |
| 5965 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 5966 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 5967 | MemPtr2->getClass())); |
| 5968 | continue; |
| 5969 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5970 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5971 | // FIXME: block pointer types? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5972 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5973 | // Cannot unwrap any more types. |
| 5974 | break; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5975 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5976 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5977 | // Apply the function pointer conversion to unify the types. We've already |
| 5978 | // unwrapped down to the function types, and we want to merge rather than |
| 5979 | // just convert, so do this ourselves rather than calling |
| 5980 | // IsFunctionConversion. |
| 5981 | // |
| 5982 | // FIXME: In order to match the standard wording as closely as possible, we |
| 5983 | // currently only do this under a single level of pointers. Ideally, we would |
| 5984 | // allow this in general, and set NeedConstBefore to the relevant depth on |
| 5985 | // the side(s) where we changed anything. |
| 5986 | if (QualifierUnion.size() == 1) { |
| 5987 | if (auto *FPT1 = Composite1->getAs<FunctionProtoType>()) { |
| 5988 | if (auto *FPT2 = Composite2->getAs<FunctionProtoType>()) { |
| 5989 | FunctionProtoType::ExtProtoInfo EPI1 = FPT1->getExtProtoInfo(); |
| 5990 | FunctionProtoType::ExtProtoInfo EPI2 = FPT2->getExtProtoInfo(); |
| 5991 | |
| 5992 | // The result is noreturn if both operands are. |
| 5993 | bool Noreturn = |
| 5994 | EPI1.ExtInfo.getNoReturn() && EPI2.ExtInfo.getNoReturn(); |
| 5995 | EPI1.ExtInfo = EPI1.ExtInfo.withNoReturn(Noreturn); |
| 5996 | EPI2.ExtInfo = EPI2.ExtInfo.withNoReturn(Noreturn); |
| 5997 | |
| 5998 | // The result is nothrow if both operands are. |
| 5999 | SmallVector<QualType, 8> ExceptionTypeStorage; |
| 6000 | EPI1.ExceptionSpec = EPI2.ExceptionSpec = |
| 6001 | mergeExceptionSpecs(*this, EPI1.ExceptionSpec, EPI2.ExceptionSpec, |
| 6002 | ExceptionTypeStorage); |
| 6003 | |
| 6004 | Composite1 = Context.getFunctionType(FPT1->getReturnType(), |
| 6005 | FPT1->getParamTypes(), EPI1); |
| 6006 | Composite2 = Context.getFunctionType(FPT2->getReturnType(), |
| 6007 | FPT2->getParamTypes(), EPI2); |
| 6008 | } |
| 6009 | } |
| 6010 | } |
| 6011 | |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 6012 | if (NeedConstBefore) { |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6013 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6014 | // mismatch, so that our (non-standard!) composite type meets the |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6015 | // requirements of C++ [conv.qual]p4 bullet 3. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 6016 | for (unsigned I = 0; I != NeedConstBefore; ++I) |
| 6017 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6018 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6019 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6020 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6021 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6022 | auto MOC = MemberOfClass.rbegin(); |
| 6023 | for (unsigned CVR : llvm::reverse(QualifierUnion)) { |
| 6024 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 6025 | auto Classes = *MOC++; |
| 6026 | if (Classes.first && Classes.second) { |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6027 | // Rebuild member pointer type |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6028 | Composite1 = Context.getMemberPointerType( |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6029 | Context.getQualifiedType(Composite1, Quals), Classes.first); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6030 | Composite2 = Context.getMemberPointerType( |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6031 | Context.getQualifiedType(Composite2, Quals), Classes.second); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6032 | } else { |
| 6033 | // Rebuild pointer type |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6034 | Composite1 = |
| 6035 | Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 6036 | Composite2 = |
| 6037 | Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6038 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6039 | } |
| 6040 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6041 | struct Conversion { |
| 6042 | Sema &S; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6043 | Expr *&E1, *&E2; |
| 6044 | QualType Composite; |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 6045 | InitializedEntity Entity; |
| 6046 | InitializationKind Kind; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6047 | InitializationSequence E1ToC, E2ToC; |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 6048 | bool Viable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6049 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6050 | Conversion(Sema &S, SourceLocation Loc, Expr *&E1, Expr *&E2, |
| 6051 | QualType Composite) |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 6052 | : S(S), E1(E1), E2(E2), Composite(Composite), |
| 6053 | Entity(InitializedEntity::InitializeTemporary(Composite)), |
| 6054 | Kind(InitializationKind::CreateCopy(Loc, SourceLocation())), |
| 6055 | E1ToC(S, Entity, Kind, E1), E2ToC(S, Entity, Kind, E2), |
| 6056 | Viable(E1ToC && E2ToC) {} |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6057 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6058 | bool perform() { |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6059 | ExprResult E1Result = E1ToC.Perform(S, Entity, Kind, E1); |
| 6060 | if (E1Result.isInvalid()) |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6061 | return true; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6062 | E1 = E1Result.getAs<Expr>(); |
| 6063 | |
| 6064 | ExprResult E2Result = E2ToC.Perform(S, Entity, Kind, E2); |
| 6065 | if (E2Result.isInvalid()) |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6066 | return true; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6067 | E2 = E2Result.getAs<Expr>(); |
| 6068 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6069 | return false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6070 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6071 | }; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6072 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6073 | // Try to convert to each composite pointer type. |
| 6074 | Conversion C1(*this, Loc, E1, E2, Composite1); |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6075 | if (C1.Viable && Context.hasSameType(Composite1, Composite2)) { |
| 6076 | if (ConvertArgs && C1.perform()) |
| 6077 | return QualType(); |
| 6078 | return C1.Composite; |
| 6079 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6080 | Conversion C2(*this, Loc, E1, E2, Composite2); |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6081 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6082 | if (C1.Viable == C2.Viable) { |
| 6083 | // Either Composite1 and Composite2 are viable and are different, or |
| 6084 | // neither is viable. |
| 6085 | // FIXME: How both be viable and different? |
| 6086 | return QualType(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 6089 | // Convert to the chosen type. |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 6090 | if (ConvertArgs && (C1.Viable ? C1 : C2).perform()) |
| 6091 | return QualType(); |
| 6092 | |
| 6093 | return C1.Viable ? C1.Composite : C2.Composite; |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 6094 | } |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 6095 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6096 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Douglas Gregor | 298087b | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 6097 | if (!E) |
| 6098 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6099 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6100 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 6101 | |
| 6102 | // If the result is a glvalue, we shouldn't bind it. |
| 6103 | if (!E->isRValue()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6104 | return E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6105 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6106 | // In ARC, calls that return a retainable type can return retained, |
| 6107 | // in which case we have to insert a consuming cast. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6108 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6109 | E->getType()->isObjCRetainableType()) { |
| 6110 | |
| 6111 | bool ReturnsRetained; |
| 6112 | |
| 6113 | // For actual calls, we compute this by examining the type of the |
| 6114 | // called value. |
| 6115 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { |
| 6116 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
| 6117 | QualType T = Callee->getType(); |
| 6118 | |
| 6119 | if (T == Context.BoundMemberTy) { |
| 6120 | // Handle pointer-to-members. |
| 6121 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) |
| 6122 | T = BinOp->getRHS()->getType(); |
| 6123 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) |
| 6124 | T = Mem->getMemberDecl()->getType(); |
| 6125 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6126 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6127 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
| 6128 | T = Ptr->getPointeeType(); |
| 6129 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
| 6130 | T = Ptr->getPointeeType(); |
| 6131 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
| 6132 | T = MemPtr->getPointeeType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6133 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6134 | const FunctionType *FTy = T->getAs<FunctionType>(); |
| 6135 | assert(FTy && "call to value not of function type?"); |
| 6136 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
| 6137 | |
| 6138 | // ActOnStmtExpr arranges things so that StmtExprs of retainable |
| 6139 | // type always produce a +1 object. |
| 6140 | } else if (isa<StmtExpr>(E)) { |
| 6141 | ReturnsRetained = true; |
| 6142 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6143 | // We hit this case with the lambda conversion-to-block optimization; |
| 6144 | // we don't want any extra casts here. |
| 6145 | } else if (isa<CastExpr>(E) && |
| 6146 | isa<BlockExpr>(cast<CastExpr>(E)->getSubExpr())) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6147 | return E; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6148 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6149 | // For message sends and property references, we try to find an |
| 6150 | // actual method. FIXME: we should infer retention by selector in |
| 6151 | // cases where we don't have an actual method. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6152 | } else { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6153 | ObjCMethodDecl *D = nullptr; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6154 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { |
| 6155 | D = Send->getMethodDecl(); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 6156 | } else if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(E)) { |
| 6157 | D = BoxedExpr->getBoxingMethod(); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6158 | } else if (ObjCArrayLiteral *ArrayLit = dyn_cast<ObjCArrayLiteral>(E)) { |
Akira Hatanaka | 4d53a1c | 2017-04-15 06:42:00 +0000 | [diff] [blame] | 6159 | // Don't do reclaims if we're using the zero-element array |
| 6160 | // constant. |
| 6161 | if (ArrayLit->getNumElements() == 0 && |
| 6162 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
| 6163 | return E; |
| 6164 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6165 | D = ArrayLit->getArrayWithObjectsMethod(); |
| 6166 | } else if (ObjCDictionaryLiteral *DictLit |
| 6167 | = dyn_cast<ObjCDictionaryLiteral>(E)) { |
Akira Hatanaka | 4d53a1c | 2017-04-15 06:42:00 +0000 | [diff] [blame] | 6168 | // Don't do reclaims if we're using the zero-element dictionary |
| 6169 | // constant. |
| 6170 | if (DictLit->getNumElements() == 0 && |
| 6171 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
| 6172 | return E; |
| 6173 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6174 | D = DictLit->getDictWithObjectsMethod(); |
| 6175 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6176 | |
| 6177 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
John McCall | 32a4da0 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 6178 | |
| 6179 | // Don't do reclaims on performSelector calls; despite their |
| 6180 | // return type, the invoked method doesn't necessarily actually |
| 6181 | // return an object. |
| 6182 | if (!ReturnsRetained && |
| 6183 | D && D->getMethodFamily() == OMF_performSelector) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6184 | return E; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6185 | } |
| 6186 | |
John McCall | 16de4d2 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 6187 | // Don't reclaim an object of Class type. |
| 6188 | if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6189 | return E; |
John McCall | 16de4d2 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 6190 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6191 | Cleanup.setExprNeedsCleanups(true); |
John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 6192 | |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6193 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject |
| 6194 | : CK_ARCReclaimReturnedObject); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6195 | return ImplicitCastExpr::Create(Context, E->getType(), ck, E, nullptr, |
| 6196 | VK_RValue); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6197 | } |
| 6198 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6199 | if (!getLangOpts().CPlusPlus) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6200 | return E; |
Douglas Gregor | 363b151 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 6201 | |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6202 | // Search for the base element type (cf. ASTContext::getBaseElementType) with |
| 6203 | // a fast path for the common case that the type is directly a RecordType. |
| 6204 | const Type *T = Context.getCanonicalType(E->getType().getTypePtr()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6205 | const RecordType *RT = nullptr; |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6206 | while (!RT) { |
| 6207 | switch (T->getTypeClass()) { |
| 6208 | case Type::Record: |
| 6209 | RT = cast<RecordType>(T); |
| 6210 | break; |
| 6211 | case Type::ConstantArray: |
| 6212 | case Type::IncompleteArray: |
| 6213 | case Type::VariableArray: |
| 6214 | case Type::DependentSizedArray: |
| 6215 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
| 6216 | break; |
| 6217 | default: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6218 | return E; |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6219 | } |
| 6220 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6221 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6222 | // That should be enough to guarantee that this type is complete, if we're |
| 6223 | // not processing a decltype expression. |
Jeffrey Yasskin | bbc4eea | 2011-01-27 19:17:54 +0000 | [diff] [blame] | 6224 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6225 | if (RD->isInvalidDecl() || RD->isDependentContext()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6226 | return E; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6227 | |
| 6228 | bool IsDecltype = ExprEvalContexts.back().IsDecltype; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6229 | CXXDestructorDecl *Destructor = IsDecltype ? nullptr : LookupDestructor(RD); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6230 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6231 | if (Destructor) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6232 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
John McCall | 8e36d53 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 6233 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 6234 | PDiag(diag::err_access_dtor_temp) |
| 6235 | << E->getType()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6236 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
| 6237 | return ExprError(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6238 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6239 | // If destructor is trivial, we can avoid the extra copy. |
| 6240 | if (Destructor->isTrivial()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6241 | return E; |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6242 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6243 | // 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] | 6244 | Cleanup.setExprNeedsCleanups(true); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6245 | } |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6246 | |
| 6247 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6248 | CXXBindTemporaryExpr *Bind = CXXBindTemporaryExpr::Create(Context, Temp, E); |
| 6249 | |
| 6250 | if (IsDecltype) |
| 6251 | ExprEvalContexts.back().DelayedDecltypeBinds.push_back(Bind); |
| 6252 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6253 | return Bind; |
Anders Carlsson | 2d4cada | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 6254 | } |
| 6255 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6256 | ExprResult |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6257 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6258 | if (SubExpr.isInvalid()) |
| 6259 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6260 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6261 | return MaybeCreateExprWithCleanups(SubExpr.get()); |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6262 | } |
| 6263 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6264 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 6265 | assert(SubExpr && "subexpression can't be null!"); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6266 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 6267 | CleanupVarDeclMarking(); |
| 6268 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6269 | unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects; |
| 6270 | assert(ExprCleanupObjects.size() >= FirstCleanup); |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6271 | assert(Cleanup.exprNeedsCleanups() || |
| 6272 | ExprCleanupObjects.size() == FirstCleanup); |
| 6273 | if (!Cleanup.exprNeedsCleanups()) |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6274 | return SubExpr; |
| 6275 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6276 | auto Cleanups = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup, |
| 6277 | ExprCleanupObjects.size() - FirstCleanup); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6278 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6279 | auto *E = ExprWithCleanups::Create( |
| 6280 | Context, SubExpr, Cleanup.cleanupsHaveSideEffects(), Cleanups); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6281 | DiscardCleanupsInEvaluationContext(); |
| 6282 | |
| 6283 | return E; |
| 6284 | } |
| 6285 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6286 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 6287 | assert(SubStmt && "sub-statement can't be null!"); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6288 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 6289 | CleanupVarDeclMarking(); |
| 6290 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6291 | if (!Cleanup.exprNeedsCleanups()) |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6292 | return SubStmt; |
| 6293 | |
| 6294 | // FIXME: In order to attach the temporaries, wrap the statement into |
| 6295 | // a StmtExpr; currently this is only used for asm statements. |
| 6296 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
| 6297 | // a new AsmStmtWithTemporaries. |
Benjamin Kramer | 0742090 | 2017-12-24 16:24:20 +0000 | [diff] [blame] | 6298 | CompoundStmt *CompStmt = CompoundStmt::Create( |
| 6299 | Context, SubStmt, SourceLocation(), SourceLocation()); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6300 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
| 6301 | SourceLocation()); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6302 | return MaybeCreateExprWithCleanups(E); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6303 | } |
| 6304 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6305 | /// Process the expression contained within a decltype. For such expressions, |
| 6306 | /// certain semantic checks on temporaries are delayed until this point, and |
| 6307 | /// are omitted for the 'topmost' call in the decltype expression. If the |
| 6308 | /// topmost call bound a temporary, strip that temporary off the expression. |
| 6309 | ExprResult Sema::ActOnDecltypeExpression(Expr *E) { |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6310 | assert(ExprEvalContexts.back().IsDecltype && "not in a decltype expression"); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6311 | |
| 6312 | // C++11 [expr.call]p11: |
| 6313 | // If a function call is a prvalue of object type, |
| 6314 | // -- if the function call is either |
| 6315 | // -- the operand of a decltype-specifier, or |
| 6316 | // -- the right operand of a comma operator that is the operand of a |
| 6317 | // decltype-specifier, |
| 6318 | // a temporary object is not introduced for the prvalue. |
| 6319 | |
| 6320 | // Recursively rebuild ParenExprs and comma expressions to strip out the |
| 6321 | // outermost CXXBindTemporaryExpr, if any. |
| 6322 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 6323 | ExprResult SubExpr = ActOnDecltypeExpression(PE->getSubExpr()); |
| 6324 | if (SubExpr.isInvalid()) |
| 6325 | return ExprError(); |
| 6326 | if (SubExpr.get() == PE->getSubExpr()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6327 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6328 | return ActOnParenExpr(PE->getLParen(), PE->getRParen(), SubExpr.get()); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6329 | } |
| 6330 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 6331 | if (BO->getOpcode() == BO_Comma) { |
| 6332 | ExprResult RHS = ActOnDecltypeExpression(BO->getRHS()); |
| 6333 | if (RHS.isInvalid()) |
| 6334 | return ExprError(); |
| 6335 | if (RHS.get() == BO->getRHS()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6336 | return E; |
| 6337 | return new (Context) BinaryOperator( |
| 6338 | BO->getLHS(), RHS.get(), BO_Comma, BO->getType(), BO->getValueKind(), |
Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 6339 | BO->getObjectKind(), BO->getOperatorLoc(), BO->getFPFeatures()); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6340 | } |
| 6341 | } |
| 6342 | |
| 6343 | CXXBindTemporaryExpr *TopBind = dyn_cast<CXXBindTemporaryExpr>(E); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6344 | CallExpr *TopCall = TopBind ? dyn_cast<CallExpr>(TopBind->getSubExpr()) |
| 6345 | : nullptr; |
Richard Smith | 202dc13 | 2014-02-18 03:51:47 +0000 | [diff] [blame] | 6346 | if (TopCall) |
| 6347 | E = TopCall; |
| 6348 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6349 | TopBind = nullptr; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6350 | |
| 6351 | // Disable the special decltype handling now. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6352 | ExprEvalContexts.back().IsDecltype = false; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6353 | |
Richard Smith | f86b0ae | 2012-07-28 19:54:11 +0000 | [diff] [blame] | 6354 | // In MS mode, don't perform any extra checking of call return types within a |
| 6355 | // decltype expression. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 6356 | if (getLangOpts().MSVCCompat) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6357 | return E; |
Richard Smith | f86b0ae | 2012-07-28 19:54:11 +0000 | [diff] [blame] | 6358 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6359 | // Perform the semantic checks we delayed until this point. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6360 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeCalls.size(); |
| 6361 | I != N; ++I) { |
| 6362 | CallExpr *Call = ExprEvalContexts.back().DelayedDecltypeCalls[I]; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6363 | if (Call == TopCall) |
| 6364 | continue; |
| 6365 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6366 | if (CheckCallReturnType(Call->getCallReturnType(Context), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6367 | Call->getLocStart(), |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6368 | Call, Call->getDirectCallee())) |
| 6369 | return ExprError(); |
| 6370 | } |
| 6371 | |
| 6372 | // Now all relevant types are complete, check the destructors are accessible |
| 6373 | // and non-deleted, and annotate them on the temporaries. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6374 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeBinds.size(); |
| 6375 | I != N; ++I) { |
| 6376 | CXXBindTemporaryExpr *Bind = |
| 6377 | ExprEvalContexts.back().DelayedDecltypeBinds[I]; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6378 | if (Bind == TopBind) |
| 6379 | continue; |
| 6380 | |
| 6381 | CXXTemporary *Temp = Bind->getTemporary(); |
| 6382 | |
| 6383 | CXXRecordDecl *RD = |
| 6384 | Bind->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6385 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
| 6386 | Temp->setDestructor(Destructor); |
| 6387 | |
Richard Smith | 7d847b1 | 2012-05-11 22:20:10 +0000 | [diff] [blame] | 6388 | MarkFunctionReferenced(Bind->getExprLoc(), Destructor); |
| 6389 | CheckDestructorAccess(Bind->getExprLoc(), Destructor, |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6390 | PDiag(diag::err_access_dtor_temp) |
Richard Smith | 7d847b1 | 2012-05-11 22:20:10 +0000 | [diff] [blame] | 6391 | << Bind->getType()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6392 | if (DiagnoseUseOfDecl(Destructor, Bind->getExprLoc())) |
| 6393 | return ExprError(); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6394 | |
| 6395 | // 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] | 6396 | Cleanup.setExprNeedsCleanups(true); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
| 6399 | // Possibly strip off the top CXXBindTemporaryExpr. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6400 | return E; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6403 | /// Note a set of 'operator->' functions that were used for a member access. |
| 6404 | static void noteOperatorArrows(Sema &S, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 6405 | ArrayRef<FunctionDecl *> OperatorArrows) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6406 | unsigned SkipStart = OperatorArrows.size(), SkipCount = 0; |
| 6407 | // FIXME: Make this configurable? |
| 6408 | unsigned Limit = 9; |
| 6409 | if (OperatorArrows.size() > Limit) { |
| 6410 | // Produce Limit-1 normal notes and one 'skipping' note. |
| 6411 | SkipStart = (Limit - 1) / 2 + (Limit - 1) % 2; |
| 6412 | SkipCount = OperatorArrows.size() - (Limit - 1); |
| 6413 | } |
| 6414 | |
| 6415 | for (unsigned I = 0; I < OperatorArrows.size(); /**/) { |
| 6416 | if (I == SkipStart) { |
| 6417 | S.Diag(OperatorArrows[I]->getLocation(), |
| 6418 | diag::note_operator_arrows_suppressed) |
| 6419 | << SkipCount; |
| 6420 | I += SkipCount; |
| 6421 | } else { |
| 6422 | S.Diag(OperatorArrows[I]->getLocation(), diag::note_operator_arrow_here) |
| 6423 | << OperatorArrows[I]->getCallResultType(); |
| 6424 | ++I; |
| 6425 | } |
| 6426 | } |
| 6427 | } |
| 6428 | |
Nico Weber | 964d332 | 2015-02-16 22:35:45 +0000 | [diff] [blame] | 6429 | ExprResult Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, |
| 6430 | SourceLocation OpLoc, |
| 6431 | tok::TokenKind OpKind, |
| 6432 | ParsedType &ObjectType, |
| 6433 | bool &MayBePseudoDestructor) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6434 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6435 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6436 | if (Result.isInvalid()) return ExprError(); |
| 6437 | Base = Result.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6438 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6439 | Result = CheckPlaceholderExpr(Base); |
| 6440 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6441 | Base = Result.get(); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6442 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6443 | QualType BaseType = Base->getType(); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6444 | MayBePseudoDestructor = false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6445 | if (BaseType->isDependentType()) { |
Douglas Gregor | 4112718 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 6446 | // If we have a pointer to a dependent type and are using the -> operator, |
| 6447 | // the object type is the type that the pointer points to. We might still |
| 6448 | // have enough information about that type to do something useful. |
| 6449 | if (OpKind == tok::arrow) |
| 6450 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 6451 | BaseType = Ptr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6452 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6453 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6454 | MayBePseudoDestructor = true; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6455 | return Base; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6456 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6457 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6458 | // C++ [over.match.oper]p8: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6459 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6460 | // returned, with the original second operand. |
| 6461 | if (OpKind == tok::arrow) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6462 | QualType StartingType = BaseType; |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6463 | bool NoArrowOperatorFound = false; |
| 6464 | bool FirstIteration = true; |
| 6465 | FunctionDecl *CurFD = dyn_cast<FunctionDecl>(CurContext); |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 6466 | // The set of types we've considered so far. |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 6467 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6468 | SmallVector<FunctionDecl*, 8> OperatorArrows; |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 6469 | CTypes.insert(Context.getCanonicalType(BaseType)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6470 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6471 | while (BaseType->isRecordType()) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6472 | if (OperatorArrows.size() >= getLangOpts().ArrowDepth) { |
| 6473 | Diag(OpLoc, diag::err_operator_arrow_depth_exceeded) |
Richard Smith | 9dbc574 | 2013-11-06 19:43:09 +0000 | [diff] [blame] | 6474 | << StartingType << getLangOpts().ArrowDepth << Base->getSourceRange(); |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6475 | noteOperatorArrows(*this, OperatorArrows); |
| 6476 | Diag(OpLoc, diag::note_operator_arrow_depth) |
| 6477 | << getLangOpts().ArrowDepth; |
| 6478 | return ExprError(); |
| 6479 | } |
| 6480 | |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6481 | Result = BuildOverloadedArrowExpr( |
| 6482 | S, Base, OpLoc, |
| 6483 | // When in a template specialization and on the first loop iteration, |
| 6484 | // potentially give the default diagnostic (with the fixit in a |
| 6485 | // separate note) instead of having the error reported back to here |
| 6486 | // and giving a diagnostic with a fixit attached to the error itself. |
| 6487 | (FirstIteration && CurFD && CurFD->isFunctionTemplateSpecialization()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6488 | ? nullptr |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6489 | : &NoArrowOperatorFound); |
| 6490 | if (Result.isInvalid()) { |
| 6491 | if (NoArrowOperatorFound) { |
| 6492 | if (FirstIteration) { |
| 6493 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Richard Smith | 9dbc574 | 2013-11-06 19:43:09 +0000 | [diff] [blame] | 6494 | << BaseType << 1 << Base->getSourceRange() |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6495 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 6496 | OpKind = tok::period; |
| 6497 | break; |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 6498 | } |
| 6499 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 6500 | << BaseType << Base->getSourceRange(); |
| 6501 | CallExpr *CE = dyn_cast<CallExpr>(Base); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6502 | if (Decl *CD = (CE ? CE->getCalleeDecl() : nullptr)) { |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 6503 | Diag(CD->getLocStart(), |
| 6504 | diag::note_member_reference_arrow_from_operator_arrow); |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6505 | } |
| 6506 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6507 | return ExprError(); |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6508 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6509 | Base = Result.get(); |
| 6510 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6511 | OperatorArrows.push_back(OpCall->getDirectCallee()); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6512 | BaseType = Base->getType(); |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 6513 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6514 | if (!CTypes.insert(CBaseType).second) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6515 | Diag(OpLoc, diag::err_operator_arrow_circular) << StartingType; |
| 6516 | noteOperatorArrows(*this, OperatorArrows); |
Fariborz Jahanian | 10ce958 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 6517 | return ExprError(); |
| 6518 | } |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6519 | FirstIteration = false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6520 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6521 | |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6522 | if (OpKind == tok::arrow && |
| 6523 | (BaseType->isPointerType() || BaseType->isObjCObjectPointerType())) |
Douglas Gregor | e4f764f | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 6524 | BaseType = BaseType->getPointeeType(); |
| 6525 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6526 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6527 | // Objective-C properties allow "." access on Objective-C pointer types, |
| 6528 | // so adjust the base type to the object type itself. |
| 6529 | if (BaseType->isObjCObjectPointerType()) |
| 6530 | BaseType = BaseType->getPointeeType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6531 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6532 | // C++ [basic.lookup.classref]p2: |
| 6533 | // [...] If the type of the object expression is of pointer to scalar |
| 6534 | // type, the unqualified-id is looked up in the context of the complete |
| 6535 | // postfix-expression. |
| 6536 | // |
| 6537 | // This also indicates that we could be parsing a pseudo-destructor-name. |
| 6538 | // Note that Objective-C class and object types can be pseudo-destructor |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6539 | // expressions or normal member (ivar or property) access expressions, and |
| 6540 | // it's legal for the type to be incomplete if this is a pseudo-destructor |
| 6541 | // call. We'll do more incomplete-type checks later in the lookup process, |
| 6542 | // so just skip this check for ObjC types. |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6543 | if (BaseType->isObjCObjectOrInterfaceType()) { |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6544 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6545 | MayBePseudoDestructor = true; |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6546 | return Base; |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6547 | } else if (!BaseType->isRecordType()) { |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 6548 | ObjectType = nullptr; |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6549 | MayBePseudoDestructor = true; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6550 | return Base; |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 6551 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6552 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6553 | // The object type must be complete (or dependent), or |
| 6554 | // C++11 [expr.prim.general]p3: |
| 6555 | // Unlike the object expression in other contexts, *this is not required to |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6556 | // 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] | 6557 | // the member function body. |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 6558 | if (!BaseType->isDependentType() && |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6559 | !isThisOutsideMemberFunctionBody(BaseType) && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 6560 | RequireCompleteType(OpLoc, BaseType, diag::err_incomplete_member_access)) |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 6561 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6562 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 6563 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6564 | // 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] | 6565 | // 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] | 6566 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 6567 | // up in the scope of class C. [...] |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6568 | ObjectType = ParsedType::make(BaseType); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6569 | return Base; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6570 | } |
| 6571 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6572 | static bool CheckArrow(Sema& S, QualType& ObjectType, Expr *&Base, |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6573 | tok::TokenKind& OpKind, SourceLocation OpLoc) { |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6574 | if (Base->hasPlaceholderType()) { |
| 6575 | ExprResult result = S.CheckPlaceholderExpr(Base); |
| 6576 | if (result.isInvalid()) return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6577 | Base = result.get(); |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6578 | } |
| 6579 | ObjectType = Base->getType(); |
| 6580 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6581 | // C++ [expr.pseudo]p2: |
| 6582 | // The left-hand side of the dot operator shall be of scalar type. The |
| 6583 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 6584 | // This scalar type is the object type. |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6585 | // Note that this is rather different from the normal handling for the |
| 6586 | // arrow operator. |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6587 | if (OpKind == tok::arrow) { |
| 6588 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 6589 | ObjectType = Ptr->getPointeeType(); |
| 6590 | } else if (!Base->isTypeDependent()) { |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 6591 | // The user wrote "p->" when they probably meant "p."; fix it. |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6592 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 6593 | << ObjectType << true |
| 6594 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 6595 | if (S.isSFINAEContext()) |
| 6596 | return true; |
| 6597 | |
| 6598 | OpKind = tok::period; |
| 6599 | } |
| 6600 | } |
| 6601 | |
| 6602 | return false; |
| 6603 | } |
| 6604 | |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6605 | /// \brief Check if it's ok to try and recover dot pseudo destructor calls on |
| 6606 | /// pointer objects. |
| 6607 | static bool |
| 6608 | canRecoverDotPseudoDestructorCallsOnPointerObjects(Sema &SemaRef, |
| 6609 | QualType DestructedType) { |
| 6610 | // If this is a record type, check if its destructor is callable. |
| 6611 | if (auto *RD = DestructedType->getAsCXXRecordDecl()) { |
| 6612 | if (CXXDestructorDecl *D = SemaRef.LookupDestructor(RD)) |
| 6613 | return SemaRef.CanUseDecl(D, /*TreatUnavailableAsInvalid=*/false); |
| 6614 | return false; |
| 6615 | } |
| 6616 | |
| 6617 | // Otherwise, check if it's a type for which it's valid to use a pseudo-dtor. |
| 6618 | return DestructedType->isDependentType() || DestructedType->isScalarType() || |
| 6619 | DestructedType->isVectorType(); |
| 6620 | } |
| 6621 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6622 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6623 | SourceLocation OpLoc, |
| 6624 | tok::TokenKind OpKind, |
| 6625 | const CXXScopeSpec &SS, |
| 6626 | TypeSourceInfo *ScopeTypeInfo, |
| 6627 | SourceLocation CCLoc, |
| 6628 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6629 | PseudoDestructorTypeStorage Destructed) { |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6630 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6631 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6632 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6633 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6634 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6635 | |
Douglas Gregor | c5c5734 | 2012-09-10 14:57:06 +0000 | [diff] [blame] | 6636 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType() && |
| 6637 | !ObjectType->isVectorType()) { |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 6638 | if (getLangOpts().MSVCCompat && ObjectType->isVoidType()) |
Nico Weber | 4bc6499 | 2012-01-23 06:08:16 +0000 | [diff] [blame] | 6639 | Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange(); |
Reid Kleckner | e502507 | 2014-05-01 16:50:23 +0000 | [diff] [blame] | 6640 | else { |
Nico Weber | 5882927 | 2012-01-23 05:50:57 +0000 | [diff] [blame] | 6641 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 6642 | << ObjectType << Base->getSourceRange(); |
Reid Kleckner | e502507 | 2014-05-01 16:50:23 +0000 | [diff] [blame] | 6643 | return ExprError(); |
| 6644 | } |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6645 | } |
| 6646 | |
| 6647 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6648 | // [...] The cv-unqualified versions of the object type and of the type |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6649 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6650 | if (DestructedTypeInfo) { |
| 6651 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 6652 | SourceLocation DestructedTypeStart |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6653 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6654 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
| 6655 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6656 | // Detect dot pseudo destructor calls on pointer objects, e.g.: |
| 6657 | // Foo *foo; |
| 6658 | // foo.~Foo(); |
| 6659 | if (OpKind == tok::period && ObjectType->isPointerType() && |
| 6660 | Context.hasSameUnqualifiedType(DestructedType, |
| 6661 | ObjectType->getPointeeType())) { |
| 6662 | auto Diagnostic = |
| 6663 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 6664 | << ObjectType << /*IsArrow=*/0 << Base->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6665 | |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6666 | // Issue a fixit only when the destructor is valid. |
| 6667 | if (canRecoverDotPseudoDestructorCallsOnPointerObjects( |
| 6668 | *this, DestructedType)) |
| 6669 | Diagnostic << FixItHint::CreateReplacement(OpLoc, "->"); |
| 6670 | |
| 6671 | // Recover by setting the object type to the destructed type and the |
| 6672 | // operator to '->'. |
| 6673 | ObjectType = DestructedType; |
| 6674 | OpKind = tok::arrow; |
| 6675 | } else { |
| 6676 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
| 6677 | << ObjectType << DestructedType << Base->getSourceRange() |
| 6678 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 6679 | |
| 6680 | // Recover by setting the destructed type to the object type. |
| 6681 | DestructedType = ObjectType; |
| 6682 | DestructedTypeInfo = |
| 6683 | Context.getTrivialTypeSourceInfo(ObjectType, DestructedTypeStart); |
| 6684 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6685 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6686 | } else if (DestructedType.getObjCLifetime() != |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6687 | ObjectType.getObjCLifetime()) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6688 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6689 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
| 6690 | // Okay: just pretend that the user provided the correctly-qualified |
| 6691 | // type. |
| 6692 | } else { |
| 6693 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
| 6694 | << ObjectType << DestructedType << Base->getSourceRange() |
| 6695 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 6696 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6697 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6698 | // Recover by setting the destructed type to the object type. |
| 6699 | DestructedType = ObjectType; |
| 6700 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 6701 | DestructedTypeStart); |
| 6702 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6703 | } |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6704 | } |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6705 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6706 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6707 | // C++ [expr.pseudo]p2: |
| 6708 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 6709 | // form |
| 6710 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6711 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6712 | // |
| 6713 | // shall designate the same scalar type. |
| 6714 | if (ScopeTypeInfo) { |
| 6715 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 6716 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | b86a6b8 | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 6717 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6718 | |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6719 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6720 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6721 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6722 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6723 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6724 | ScopeType = QualType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6725 | ScopeTypeInfo = nullptr; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6726 | } |
| 6727 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6728 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6729 | Expr *Result |
| 6730 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 6731 | OpKind == tok::arrow, OpLoc, |
Douglas Gregor | a6ce608 | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 6732 | SS.getWithLocInContext(Context), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6733 | ScopeTypeInfo, |
| 6734 | CCLoc, |
| 6735 | TildeLoc, |
| 6736 | Destructed); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6737 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6738 | return Result; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6739 | } |
| 6740 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6741 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6742 | SourceLocation OpLoc, |
| 6743 | tok::TokenKind OpKind, |
| 6744 | CXXScopeSpec &SS, |
| 6745 | UnqualifiedId &FirstTypeName, |
| 6746 | SourceLocation CCLoc, |
| 6747 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6748 | UnqualifiedId &SecondTypeName) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 6749 | assert((FirstTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
| 6750 | FirstTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) && |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6751 | "Invalid first type name in pseudo-destructor"); |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 6752 | assert((SecondTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
| 6753 | SecondTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) && |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6754 | "Invalid second type name in pseudo-destructor"); |
| 6755 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6756 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6757 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6758 | return ExprError(); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6759 | |
| 6760 | // Compute the object type that we should use for name lookup purposes. Only |
| 6761 | // record types and dependent types matter. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6762 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6763 | if (!SS.isSet()) { |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6764 | if (ObjectType->isRecordType()) |
| 6765 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6766 | else if (ObjectType->isDependentType()) |
| 6767 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6768 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6769 | |
| 6770 | // Convert the name of the type being destructed (following the ~) into a |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6771 | // type (with source-location information). |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6772 | QualType DestructedType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6773 | TypeSourceInfo *DestructedTypeInfo = nullptr; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6774 | PseudoDestructorTypeStorage Destructed; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 6775 | if (SecondTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6776 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6777 | SecondTypeName.StartLocation, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6778 | S, &SS, true, false, ObjectTypePtrForLookup, |
| 6779 | /*IsCtorOrDtorName*/true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6780 | if (!T && |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6781 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 6782 | (!SS.isSet() && ObjectType->isDependentType()))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6783 | // 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] | 6784 | // couldn't find anything useful in scope. Just store the identifier and |
| 6785 | // it's location, and we'll perform (qualified) name lookup again at |
| 6786 | // template instantiation time. |
| 6787 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 6788 | SecondTypeName.StartLocation); |
| 6789 | } else if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6790 | Diag(SecondTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6791 | diag::err_pseudo_dtor_destructor_non_type) |
| 6792 | << SecondTypeName.Identifier << ObjectType; |
| 6793 | if (isSFINAEContext()) |
| 6794 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6795 | |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6796 | // Recover by assuming we had the right type all along. |
| 6797 | DestructedType = ObjectType; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6798 | } else |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6799 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6800 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6801 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6802 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6803 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6804 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6805 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6806 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6807 | TemplateId->Template, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6808 | TemplateId->Name, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6809 | TemplateId->TemplateNameLoc, |
| 6810 | TemplateId->LAngleLoc, |
| 6811 | TemplateArgsPtr, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6812 | TemplateId->RAngleLoc, |
| 6813 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6814 | if (T.isInvalid() || !T.get()) { |
| 6815 | // Recover by assuming we had the right type all along. |
| 6816 | DestructedType = ObjectType; |
| 6817 | } else |
| 6818 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6819 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6820 | |
| 6821 | // 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] | 6822 | // information. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6823 | if (!DestructedType.isNull()) { |
| 6824 | if (!DestructedTypeInfo) |
| 6825 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6826 | SecondTypeName.StartLocation); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6827 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6828 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6829 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6830 | // 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] | 6831 | TypeSourceInfo *ScopeTypeInfo = nullptr; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6832 | QualType ScopeType; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 6833 | if (FirstTypeName.getKind() == UnqualifiedIdKind::IK_TemplateId || |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6834 | FirstTypeName.Identifier) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 6835 | if (FirstTypeName.getKind() == UnqualifiedIdKind::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6836 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6837 | FirstTypeName.StartLocation, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6838 | S, &SS, true, false, ObjectTypePtrForLookup, |
| 6839 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6840 | if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6841 | Diag(FirstTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6842 | diag::err_pseudo_dtor_destructor_non_type) |
| 6843 | << FirstTypeName.Identifier << ObjectType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6844 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6845 | if (isSFINAEContext()) |
| 6846 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6847 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6848 | // Just drop this type. It's unnecessary anyway. |
| 6849 | ScopeType = QualType(); |
| 6850 | } else |
| 6851 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6852 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6853 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6854 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6855 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6856 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6857 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6858 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6859 | TemplateId->Template, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6860 | TemplateId->Name, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6861 | TemplateId->TemplateNameLoc, |
| 6862 | TemplateId->LAngleLoc, |
| 6863 | TemplateArgsPtr, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6864 | TemplateId->RAngleLoc, |
| 6865 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6866 | if (T.isInvalid() || !T.get()) { |
| 6867 | // Recover by dropping this type. |
| 6868 | ScopeType = QualType(); |
| 6869 | } else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6870 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6871 | } |
| 6872 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6873 | |
Douglas Gregor | 90ad922 | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 6874 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 6875 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 6876 | FirstTypeName.StartLocation); |
| 6877 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6878 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6879 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | cdbd515 | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 6880 | ScopeTypeInfo, CCLoc, TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6881 | Destructed); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6882 | } |
| 6883 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6884 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
| 6885 | SourceLocation OpLoc, |
| 6886 | tok::TokenKind OpKind, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6887 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6888 | const DeclSpec& DS) { |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6889 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6890 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6891 | return ExprError(); |
| 6892 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 6893 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc(), |
| 6894 | false); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6895 | |
| 6896 | TypeLocBuilder TLB; |
| 6897 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
| 6898 | DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 6899 | TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T); |
| 6900 | PseudoDestructorTypeStorage Destructed(DestructedTypeInfo); |
| 6901 | |
| 6902 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, CXXScopeSpec(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6903 | nullptr, SourceLocation(), TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6904 | Destructed); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6905 | } |
| 6906 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6907 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 6908 | CXXConversionDecl *Method, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6909 | bool HadMultipleCandidates) { |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6910 | if (Method->getParent()->isLambda() && |
| 6911 | Method->getConversionType()->isBlockPointerType()) { |
| 6912 | // This is a lambda coversion to block pointer; check if the argument |
| 6913 | // is a LambdaExpr. |
| 6914 | Expr *SubE = E; |
| 6915 | CastExpr *CE = dyn_cast<CastExpr>(SubE); |
| 6916 | if (CE && CE->getCastKind() == CK_NoOp) |
| 6917 | SubE = CE->getSubExpr(); |
| 6918 | SubE = SubE->IgnoreParens(); |
| 6919 | if (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(SubE)) |
| 6920 | SubE = BE->getSubExpr(); |
| 6921 | if (isa<LambdaExpr>(SubE)) { |
| 6922 | // For the conversion to block pointer on a lambda expression, we |
| 6923 | // construct a special BlockLiteral instead; this doesn't really make |
| 6924 | // a difference in ARC, but outside of ARC the resulting block literal |
| 6925 | // follows the normal lifetime rules for block literals instead of being |
| 6926 | // autoreleased. |
| 6927 | DiagnosticErrorTrap Trap(Diags); |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6928 | PushExpressionEvaluationContext( |
| 6929 | ExpressionEvaluationContext::PotentiallyEvaluated); |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6930 | ExprResult Exp = BuildBlockForLambdaConversion(E->getExprLoc(), |
| 6931 | E->getExprLoc(), |
| 6932 | Method, E); |
Akira Hatanaka | c482acd | 2016-05-04 18:07:20 +0000 | [diff] [blame] | 6933 | PopExpressionEvaluationContext(); |
| 6934 | |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6935 | if (Exp.isInvalid()) |
| 6936 | Diag(E->getExprLoc(), diag::note_lambda_to_block_conv); |
| 6937 | return Exp; |
| 6938 | } |
| 6939 | } |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6940 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6941 | ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/nullptr, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6942 | FoundDecl, Method); |
| 6943 | if (Exp.isInvalid()) |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 6944 | return true; |
Eli Friedman | f719553 | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 6945 | |
Aaron Ballman | f4cb2be | 2015-03-24 15:07:53 +0000 | [diff] [blame] | 6946 | MemberExpr *ME = new (Context) MemberExpr( |
| 6947 | Exp.get(), /*IsArrow=*/false, SourceLocation(), Method, SourceLocation(), |
| 6948 | Context.BoundMemberTy, VK_RValue, OK_Ordinary); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6949 | if (HadMultipleCandidates) |
| 6950 | ME->setHadMultipleCandidates(true); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 6951 | MarkMemberReferenced(ME); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6952 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6953 | QualType ResultType = Method->getReturnType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6954 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 6955 | ResultType = ResultType.getNonLValueExprType(Context); |
| 6956 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6957 | CXXMemberCallExpr *CE = |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 6958 | new (Context) CXXMemberCallExpr(Context, ME, None, ResultType, VK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6959 | Exp.get()->getLocEnd()); |
George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6960 | |
| 6961 | if (CheckFunctionCall(Method, CE, |
| 6962 | Method->getType()->castAs<FunctionProtoType>())) |
| 6963 | return ExprError(); |
| 6964 | |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6965 | return CE; |
| 6966 | } |
| 6967 | |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6968 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 6969 | SourceLocation RParen) { |
Aaron Ballman | edc8084 | 2015-04-27 22:31:12 +0000 | [diff] [blame] | 6970 | // If the operand is an unresolved lookup expression, the expression is ill- |
| 6971 | // formed per [over.over]p1, because overloaded function names cannot be used |
| 6972 | // without arguments except in explicit contexts. |
| 6973 | ExprResult R = CheckPlaceholderExpr(Operand); |
| 6974 | if (R.isInvalid()) |
| 6975 | return R; |
| 6976 | |
| 6977 | // The operand may have been modified when checking the placeholder type. |
| 6978 | Operand = R.get(); |
| 6979 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 6980 | if (!inTemplateInstantiation() && Operand->HasSideEffects(Context, false)) { |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 6981 | // The expression operand for noexcept is in an unevaluated expression |
| 6982 | // context, so side effects could result in unintended consequences. |
| 6983 | Diag(Operand->getExprLoc(), diag::warn_side_effects_unevaluated_context); |
| 6984 | } |
| 6985 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 6986 | CanThrowResult CanThrow = canThrow(Operand); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6987 | return new (Context) |
| 6988 | CXXNoexceptExpr(Context.BoolTy, Operand, CanThrow, KeyLoc, RParen); |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6989 | } |
| 6990 | |
| 6991 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 6992 | Expr *Operand, SourceLocation RParen) { |
| 6993 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 22e3a93 | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6996 | static bool IsSpecialDiscardedValue(Expr *E) { |
| 6997 | // In C++11, discarded-value expressions of a certain form are special, |
| 6998 | // according to [expr]p10: |
| 6999 | // The lvalue-to-rvalue conversion (4.1) is applied only if the |
| 7000 | // expression is an lvalue of volatile-qualified type and it has |
| 7001 | // one of the following forms: |
| 7002 | E = E->IgnoreParens(); |
| 7003 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7004 | // - id-expression (5.1.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7005 | if (isa<DeclRefExpr>(E)) |
| 7006 | return true; |
| 7007 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7008 | // - subscripting (5.2.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7009 | if (isa<ArraySubscriptExpr>(E)) |
| 7010 | return true; |
| 7011 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7012 | // - class member access (5.2.5), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7013 | if (isa<MemberExpr>(E)) |
| 7014 | return true; |
| 7015 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7016 | // - indirection (5.3.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7017 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 7018 | if (UO->getOpcode() == UO_Deref) |
| 7019 | return true; |
| 7020 | |
| 7021 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7022 | // - pointer-to-member operation (5.5), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7023 | if (BO->isPtrMemOp()) |
| 7024 | return true; |
| 7025 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7026 | // - 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] | 7027 | if (BO->getOpcode() == BO_Comma) |
| 7028 | return IsSpecialDiscardedValue(BO->getRHS()); |
| 7029 | } |
| 7030 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 7031 | // - conditional expression (5.16) where both the second and the third |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7032 | // operands are one of the above, or |
| 7033 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) |
| 7034 | return IsSpecialDiscardedValue(CO->getTrueExpr()) && |
| 7035 | IsSpecialDiscardedValue(CO->getFalseExpr()); |
| 7036 | // The related edge case of "*x ?: *x". |
| 7037 | if (BinaryConditionalOperator *BCO = |
| 7038 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 7039 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(BCO->getTrueExpr())) |
| 7040 | return IsSpecialDiscardedValue(OVE->getSourceExpr()) && |
| 7041 | IsSpecialDiscardedValue(BCO->getFalseExpr()); |
| 7042 | } |
| 7043 | |
| 7044 | // Objective-C++ extensions to the rule. |
| 7045 | if (isa<PseudoObjectExpr>(E) || isa<ObjCIvarRefExpr>(E)) |
| 7046 | return true; |
| 7047 | |
| 7048 | return false; |
| 7049 | } |
| 7050 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7051 | /// Perform the conversions required for an expression used in a |
| 7052 | /// context that ignores the result. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7053 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7054 | if (E->hasPlaceholderType()) { |
| 7055 | ExprResult result = CheckPlaceholderExpr(E); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7056 | if (result.isInvalid()) return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7057 | E = result.get(); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7058 | } |
| 7059 | |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 7060 | // C99 6.3.2.1: |
| 7061 | // [Except in specific positions,] an lvalue that does not have |
| 7062 | // array type is converted to the value stored in the |
| 7063 | // designated object (and is no longer an lvalue). |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 7064 | if (E->isRValue()) { |
| 7065 | // In C, function designators (i.e. expressions of function type) |
| 7066 | // are r-values, but we still want to do function-to-pointer decay |
| 7067 | // on them. This is both technically correct and convenient for |
| 7068 | // some clients. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7069 | if (!getLangOpts().CPlusPlus && E->getType()->isFunctionType()) |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 7070 | return DefaultFunctionArrayConversion(E); |
| 7071 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7072 | return E; |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 7073 | } |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 7074 | |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7075 | if (getLangOpts().CPlusPlus) { |
| 7076 | // The C++11 standard defines the notion of a discarded-value expression; |
| 7077 | // normally, we don't need to do anything to handle it, but if it is a |
| 7078 | // volatile lvalue with a special form, we perform an lvalue-to-rvalue |
| 7079 | // conversion. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7080 | if (getLangOpts().CPlusPlus11 && E->isGLValue() && |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7081 | E->getType().isVolatileQualified() && |
| 7082 | IsSpecialDiscardedValue(E)) { |
| 7083 | ExprResult Res = DefaultLvalueConversion(E); |
| 7084 | if (Res.isInvalid()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7085 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7086 | E = Res.get(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7087 | } |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 7088 | |
| 7089 | // C++1z: |
| 7090 | // If the expression is a prvalue after this optional conversion, the |
| 7091 | // temporary materialization conversion is applied. |
| 7092 | // |
| 7093 | // We skip this step: IR generation is able to synthesize the storage for |
| 7094 | // itself in the aggregate case, and adding the extra node to the AST is |
| 7095 | // just clutter. |
| 7096 | // FIXME: We don't emit lifetime markers for the temporaries due to this. |
| 7097 | // FIXME: Do any other AST consumers care about this? |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7098 | return E; |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 7099 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7100 | |
| 7101 | // GCC seems to also exclude expressions of incomplete enum type. |
| 7102 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
| 7103 | if (!T->getDecl()->isComplete()) { |
| 7104 | // FIXME: stupid workaround for a codegen bug! |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7105 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).get(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7106 | return E; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7107 | } |
| 7108 | } |
| 7109 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7110 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 7111 | if (Res.isInvalid()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7112 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7113 | E = Res.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7114 | |
John McCall | ca61b65 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 7115 | if (!E->getType()->isVoidType()) |
| 7116 | RequireCompleteType(E->getExprLoc(), E->getType(), |
| 7117 | diag::err_incomplete_type); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7118 | return E; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7119 | } |
| 7120 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7121 | // If we can unambiguously determine whether Var can never be used |
| 7122 | // in a constant expression, return true. |
| 7123 | // - if the variable and its initializer are non-dependent, then |
| 7124 | // we can unambiguously check if the variable is a constant expression. |
| 7125 | // - if the initializer is not value dependent - we can determine whether |
| 7126 | // 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] | 7127 | // be used to initialize a constant expression we conclude that Var can |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7128 | // never be a constant expression. |
| 7129 | // - FXIME: if the initializer is dependent, we can still do some analysis and |
| 7130 | // identify certain cases unambiguously as non-const by using a Visitor: |
| 7131 | // - such as those that involve odr-use of a ParmVarDecl, involve a new |
| 7132 | // delete, lambda-expr, dynamic-cast, reinterpret-cast etc... |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7133 | static inline bool VariableCanNeverBeAConstantExpression(VarDecl *Var, |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7134 | ASTContext &Context) { |
| 7135 | if (isa<ParmVarDecl>(Var)) return true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7136 | const VarDecl *DefVD = nullptr; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7137 | |
| 7138 | // If there is no initializer - this can not be a constant expression. |
| 7139 | if (!Var->getAnyInitializer(DefVD)) return true; |
| 7140 | assert(DefVD); |
| 7141 | if (DefVD->isWeak()) return false; |
| 7142 | EvaluatedStmt *Eval = DefVD->ensureEvaluatedStmt(); |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 7143 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7144 | Expr *Init = cast<Expr>(Eval->Value); |
| 7145 | |
| 7146 | if (Var->getType()->isDependentType() || Init->isValueDependent()) { |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 7147 | // FIXME: Teach the constant evaluator to deal with the non-dependent parts |
| 7148 | // of value-dependent expressions, and use it here to determine whether the |
| 7149 | // initializer is a potential constant expression. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7150 | return false; |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 7151 | } |
| 7152 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7153 | return !IsVariableAConstantExpression(Var, Context); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7154 | } |
| 7155 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7156 | /// \brief Check if the current lambda has any potential captures |
| 7157 | /// that must be captured by any of its enclosing lambdas that are ready to |
| 7158 | /// capture. If there is a lambda that can capture a nested |
| 7159 | /// potential-capture, go ahead and do so. Also, check to see if any |
| 7160 | /// 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] | 7161 | /// need to be captured. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7162 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7163 | static void CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures( |
| 7164 | Expr *const FE, LambdaScopeInfo *const CurrentLSI, Sema &S) { |
| 7165 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7166 | assert(!S.isUnevaluatedContext()); |
| 7167 | assert(S.CurContext->isDependentContext()); |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7168 | #ifndef NDEBUG |
| 7169 | DeclContext *DC = S.CurContext; |
| 7170 | while (DC && isa<CapturedDecl>(DC)) |
| 7171 | DC = DC->getParent(); |
| 7172 | assert( |
| 7173 | CurrentLSI->CallOperator == DC && |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7174 | "The current call operator must be synchronized with Sema's CurContext"); |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7175 | #endif // NDEBUG |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7176 | |
| 7177 | const bool IsFullExprInstantiationDependent = FE->isInstantiationDependent(); |
| 7178 | |
| 7179 | ArrayRef<const FunctionScopeInfo *> FunctionScopesArrayRef( |
| 7180 | S.FunctionScopes.data(), S.FunctionScopes.size()); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7181 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7182 | // All the potentially captureable variables in the current nested |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7183 | // lambda (within a generic outer lambda), must be captured by an |
| 7184 | // outer lambda that is enclosed within a non-dependent context. |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7185 | const unsigned NumPotentialCaptures = |
| 7186 | CurrentLSI->getNumPotentialVariableCaptures(); |
| 7187 | for (unsigned I = 0; I != NumPotentialCaptures; ++I) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7188 | Expr *VarExpr = nullptr; |
| 7189 | VarDecl *Var = nullptr; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7190 | CurrentLSI->getPotentialVariableCapture(I, Var, VarExpr); |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7191 | // 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] | 7192 | // expression is not instantiation dependent, only then do we not |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7193 | // need to check enclosing lambda's for speculative captures. |
| 7194 | // For e.g.: |
| 7195 | // Even though 'x' is not odr-used, it should be captured. |
| 7196 | // int test() { |
| 7197 | // const int x = 10; |
| 7198 | // auto L = [=](auto a) { |
| 7199 | // (void) +x + a; |
| 7200 | // }; |
| 7201 | // } |
| 7202 | if (CurrentLSI->isVariableExprMarkedAsNonODRUsed(VarExpr) && |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7203 | !IsFullExprInstantiationDependent) |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7204 | continue; |
| 7205 | |
| 7206 | // If we have a capture-capable lambda for the variable, go ahead and |
| 7207 | // capture the variable in that lambda (and all its enclosing lambdas). |
| 7208 | if (const Optional<unsigned> Index = |
| 7209 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
| 7210 | FunctionScopesArrayRef, Var, S)) { |
| 7211 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
| 7212 | MarkVarDeclODRUsed(Var, VarExpr->getExprLoc(), S, |
| 7213 | &FunctionScopeIndexOfCapturableLambda); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7214 | } |
| 7215 | const bool IsVarNeverAConstantExpression = |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7216 | VariableCanNeverBeAConstantExpression(Var, S.Context); |
| 7217 | if (!IsFullExprInstantiationDependent || IsVarNeverAConstantExpression) { |
| 7218 | // This full expression is not instantiation dependent or the variable |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7219 | // can not be used in a constant expression - which means |
| 7220 | // this variable must be odr-used here, so diagnose a |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7221 | // capture violation early, if the variable is un-captureable. |
| 7222 | // This is purely for diagnosing errors early. Otherwise, this |
| 7223 | // error would get diagnosed when the lambda becomes capture ready. |
| 7224 | QualType CaptureType, DeclRefType; |
| 7225 | SourceLocation ExprLoc = VarExpr->getExprLoc(); |
| 7226 | if (S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7227 | /*EllipsisLoc*/ SourceLocation(), |
| 7228 | /*BuildAndDiagnose*/false, CaptureType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7229 | DeclRefType, nullptr)) { |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7230 | // We will never be able to capture this variable, and we need |
| 7231 | // to be able to in any and all instantiations, so diagnose it. |
| 7232 | S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7233 | /*EllipsisLoc*/ SourceLocation(), |
| 7234 | /*BuildAndDiagnose*/true, CaptureType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7235 | DeclRefType, nullptr); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7236 | } |
| 7237 | } |
| 7238 | } |
| 7239 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7240 | // Check if 'this' needs to be captured. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7241 | if (CurrentLSI->hasPotentialThisCapture()) { |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7242 | // If we have a capture-capable lambda for 'this', go ahead and capture |
| 7243 | // 'this' in that lambda (and all its enclosing lambdas). |
| 7244 | if (const Optional<unsigned> Index = |
| 7245 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7246 | FunctionScopesArrayRef, /*0 is 'this'*/ nullptr, S)) { |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7247 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
| 7248 | S.CheckCXXThisCapture(CurrentLSI->PotentialThisCaptureLocation, |
| 7249 | /*Explicit*/ false, /*BuildAndDiagnose*/ true, |
| 7250 | &FunctionScopeIndexOfCapturableLambda); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7251 | } |
| 7252 | } |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7253 | |
| 7254 | // Reset all the potential captures at the end of each full-expression. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7255 | CurrentLSI->clearPotentialCaptures(); |
| 7256 | } |
| 7257 | |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7258 | static ExprResult attemptRecovery(Sema &SemaRef, |
| 7259 | const TypoCorrectionConsumer &Consumer, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 7260 | const TypoCorrection &TC) { |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7261 | LookupResult R(SemaRef, Consumer.getLookupResult().getLookupNameInfo(), |
| 7262 | Consumer.getLookupResult().getLookupKind()); |
| 7263 | const CXXScopeSpec *SS = Consumer.getSS(); |
| 7264 | CXXScopeSpec NewSS; |
| 7265 | |
| 7266 | // Use an approprate CXXScopeSpec for building the expr. |
| 7267 | if (auto *NNS = TC.getCorrectionSpecifier()) |
| 7268 | NewSS.MakeTrivial(SemaRef.Context, NNS, TC.getCorrectionRange()); |
| 7269 | else if (SS && !TC.WillReplaceSpecifier()) |
| 7270 | NewSS = *SS; |
| 7271 | |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7272 | if (auto *ND = TC.getFoundDecl()) { |
Nick Lewycky | 01ad4ae | 2014-12-13 02:54:28 +0000 | [diff] [blame] | 7273 | R.setLookupName(ND->getDeclName()); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7274 | R.addDecl(ND); |
| 7275 | if (ND->isCXXClassMember()) { |
Nick Lewycky | 01ad4ae | 2014-12-13 02:54:28 +0000 | [diff] [blame] | 7276 | // Figure out the correct naming class to add to the LookupResult. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7277 | CXXRecordDecl *Record = nullptr; |
| 7278 | if (auto *NNS = TC.getCorrectionSpecifier()) |
| 7279 | Record = NNS->getAsType()->getAsCXXRecordDecl(); |
| 7280 | if (!Record) |
Olivier Goffart | ed13fab | 2015-01-09 09:37:26 +0000 | [diff] [blame] | 7281 | Record = |
| 7282 | dyn_cast<CXXRecordDecl>(ND->getDeclContext()->getRedeclContext()); |
| 7283 | if (Record) |
| 7284 | R.setNamingClass(Record); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7285 | |
| 7286 | // Detect and handle the case where the decl might be an implicit |
| 7287 | // member. |
| 7288 | bool MightBeImplicitMember; |
| 7289 | if (!Consumer.isAddressOfOperand()) |
| 7290 | MightBeImplicitMember = true; |
| 7291 | else if (!NewSS.isEmpty()) |
| 7292 | MightBeImplicitMember = false; |
| 7293 | else if (R.isOverloadedResult()) |
| 7294 | MightBeImplicitMember = false; |
| 7295 | else if (R.isUnresolvableResult()) |
| 7296 | MightBeImplicitMember = true; |
| 7297 | else |
| 7298 | MightBeImplicitMember = isa<FieldDecl>(ND) || |
| 7299 | isa<IndirectFieldDecl>(ND) || |
| 7300 | isa<MSPropertyDecl>(ND); |
| 7301 | |
| 7302 | if (MightBeImplicitMember) |
| 7303 | return SemaRef.BuildPossibleImplicitMemberExpr( |
| 7304 | NewSS, /*TemplateKWLoc*/ SourceLocation(), R, |
Aaron Ballman | 6924dcd | 2015-09-01 14:49:24 +0000 | [diff] [blame] | 7305 | /*TemplateArgs*/ nullptr, /*S*/ nullptr); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7306 | } else if (auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
| 7307 | return SemaRef.LookupInObjCMethod(R, Consumer.getScope(), |
| 7308 | Ivar->getIdentifier()); |
| 7309 | } |
| 7310 | } |
| 7311 | |
Kaelyn Takata | 6f71ce2 | 2014-11-20 22:06:33 +0000 | [diff] [blame] | 7312 | return SemaRef.BuildDeclarationNameExpr(NewSS, R, /*NeedsADL*/ false, |
| 7313 | /*AcceptInvalidDecl*/ true); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7314 | } |
| 7315 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7316 | namespace { |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7317 | class FindTypoExprs : public RecursiveASTVisitor<FindTypoExprs> { |
| 7318 | llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs; |
| 7319 | |
| 7320 | public: |
| 7321 | explicit FindTypoExprs(llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs) |
| 7322 | : TypoExprs(TypoExprs) {} |
| 7323 | bool VisitTypoExpr(TypoExpr *TE) { |
| 7324 | TypoExprs.insert(TE); |
| 7325 | return true; |
| 7326 | } |
| 7327 | }; |
| 7328 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7329 | class TransformTypos : public TreeTransform<TransformTypos> { |
| 7330 | typedef TreeTransform<TransformTypos> BaseTransform; |
| 7331 | |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7332 | VarDecl *InitDecl; // A decl to avoid as a correction because it is in the |
| 7333 | // process of being initialized. |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7334 | llvm::function_ref<ExprResult(Expr *)> ExprFilter; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7335 | llvm::SmallSetVector<TypoExpr *, 2> TypoExprs, AmbiguousTypoExprs; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7336 | llvm::SmallDenseMap<TypoExpr *, ExprResult, 2> TransformCache; |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7337 | llvm::SmallDenseMap<OverloadExpr *, Expr *, 4> OverloadResolution; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7338 | |
| 7339 | /// \brief Emit diagnostics for all of the TypoExprs encountered. |
| 7340 | /// If the TypoExprs were successfully corrected, then the diagnostics should |
| 7341 | /// suggest the corrections. Otherwise the diagnostics will not suggest |
| 7342 | /// anything (having been passed an empty TypoCorrection). |
| 7343 | void EmitAllDiagnostics() { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame^] | 7344 | for (TypoExpr *TE : TypoExprs) { |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7345 | auto &State = SemaRef.getTypoExprState(TE); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7346 | if (State.DiagHandler) { |
| 7347 | TypoCorrection TC = State.Consumer->getCurrentCorrection(); |
| 7348 | ExprResult Replacement = TransformCache[TE]; |
| 7349 | |
| 7350 | // Extract the NamedDecl from the transformed TypoExpr and add it to the |
| 7351 | // TypoCorrection, replacing the existing decls. This ensures the right |
| 7352 | // NamedDecl is used in diagnostics e.g. in the case where overload |
| 7353 | // resolution was used to select one from several possible decls that |
| 7354 | // had been stored in the TypoCorrection. |
| 7355 | if (auto *ND = getDeclFromExpr( |
| 7356 | Replacement.isInvalid() ? nullptr : Replacement.get())) |
| 7357 | TC.setCorrectionDecl(ND); |
| 7358 | |
| 7359 | State.DiagHandler(TC); |
| 7360 | } |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7361 | SemaRef.clearDelayedTypo(TE); |
| 7362 | } |
| 7363 | } |
| 7364 | |
| 7365 | /// \brief If corrections for the first TypoExpr have been exhausted for a |
| 7366 | /// given combination of the other TypoExprs, retry those corrections against |
| 7367 | /// the next combination of substitutions for the other TypoExprs by advancing |
| 7368 | /// to the next potential correction of the second TypoExpr. For the second |
| 7369 | /// and subsequent TypoExprs, if its stream of corrections has been exhausted, |
| 7370 | /// the stream is reset and the next TypoExpr's stream is advanced by one (a |
| 7371 | /// TypoExpr's correction stream is advanced by removing the TypoExpr from the |
| 7372 | /// TransformCache). Returns true if there is still any untried combinations |
| 7373 | /// of corrections. |
| 7374 | bool CheckAndAdvanceTypoExprCorrectionStreams() { |
| 7375 | for (auto TE : TypoExprs) { |
| 7376 | auto &State = SemaRef.getTypoExprState(TE); |
| 7377 | TransformCache.erase(TE); |
| 7378 | if (!State.Consumer->finished()) |
| 7379 | return true; |
| 7380 | State.Consumer->resetCorrectionStream(); |
| 7381 | } |
| 7382 | return false; |
| 7383 | } |
| 7384 | |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7385 | NamedDecl *getDeclFromExpr(Expr *E) { |
| 7386 | if (auto *OE = dyn_cast_or_null<OverloadExpr>(E)) |
| 7387 | E = OverloadResolution[OE]; |
| 7388 | |
| 7389 | if (!E) |
| 7390 | return nullptr; |
| 7391 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7392 | return DRE->getFoundDecl(); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7393 | if (auto *ME = dyn_cast<MemberExpr>(E)) |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7394 | return ME->getFoundDecl(); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7395 | // FIXME: Add any other expr types that could be be seen by the delayed typo |
| 7396 | // correction TreeTransform for which the corresponding TypoCorrection could |
Nick Lewycky | 39f9dbc | 2014-12-16 21:48:39 +0000 | [diff] [blame] | 7397 | // contain multiple decls. |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7398 | return nullptr; |
| 7399 | } |
| 7400 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7401 | ExprResult TryTransform(Expr *E) { |
| 7402 | Sema::SFINAETrap Trap(SemaRef); |
| 7403 | ExprResult Res = TransformExpr(E); |
| 7404 | if (Trap.hasErrorOccurred() || Res.isInvalid()) |
| 7405 | return ExprError(); |
| 7406 | |
| 7407 | return ExprFilter(Res.get()); |
| 7408 | } |
| 7409 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7410 | public: |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7411 | TransformTypos(Sema &SemaRef, VarDecl *InitDecl, llvm::function_ref<ExprResult(Expr *)> Filter) |
| 7412 | : BaseTransform(SemaRef), InitDecl(InitDecl), ExprFilter(Filter) {} |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7413 | |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7414 | ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc, |
| 7415 | MultiExprArg Args, |
| 7416 | SourceLocation RParenLoc, |
| 7417 | Expr *ExecConfig = nullptr) { |
| 7418 | auto Result = BaseTransform::RebuildCallExpr(Callee, LParenLoc, Args, |
| 7419 | RParenLoc, ExecConfig); |
| 7420 | if (auto *OE = dyn_cast<OverloadExpr>(Callee)) { |
Reid Kleckner | a9e65ba | 2014-12-13 01:11:23 +0000 | [diff] [blame] | 7421 | if (Result.isUsable()) { |
Reid Kleckner | a7fe33e | 2014-12-13 00:53:10 +0000 | [diff] [blame] | 7422 | Expr *ResultCall = Result.get(); |
| 7423 | if (auto *BE = dyn_cast<CXXBindTemporaryExpr>(ResultCall)) |
| 7424 | ResultCall = BE->getSubExpr(); |
| 7425 | if (auto *CE = dyn_cast<CallExpr>(ResultCall)) |
| 7426 | OverloadResolution[OE] = CE->getCallee(); |
| 7427 | } |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7428 | } |
| 7429 | return Result; |
| 7430 | } |
| 7431 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7432 | ExprResult TransformLambdaExpr(LambdaExpr *E) { return Owned(E); } |
| 7433 | |
Saleem Abdulrasool | a174241 | 2015-10-31 00:39:15 +0000 | [diff] [blame] | 7434 | ExprResult TransformBlockExpr(BlockExpr *E) { return Owned(E); } |
| 7435 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7436 | ExprResult Transform(Expr *E) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7437 | ExprResult Res; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7438 | while (true) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7439 | Res = TryTransform(E); |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7440 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7441 | // Exit if either the transform was valid or if there were no TypoExprs |
| 7442 | // to transform that still have any untried correction candidates.. |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7443 | if (!Res.isInvalid() || |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7444 | !CheckAndAdvanceTypoExprCorrectionStreams()) |
| 7445 | break; |
| 7446 | } |
| 7447 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7448 | // Ensure none of the TypoExprs have multiple typo correction candidates |
| 7449 | // with the same edit length that pass all the checks and filters. |
| 7450 | // TODO: Properly handle various permutations of possible corrections when |
| 7451 | // there is more than one potentially ambiguous typo correction. |
Kaelyn Takata | 26ffc5f | 2015-06-25 23:47:39 +0000 | [diff] [blame] | 7452 | // Also, disable typo correction while attempting the transform when |
| 7453 | // handling potentially ambiguous typo corrections as any new TypoExprs will |
| 7454 | // have been introduced by the application of one of the correction |
| 7455 | // candidates and add little to no value if corrected. |
| 7456 | SemaRef.DisableTypoCorrection = true; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7457 | while (!AmbiguousTypoExprs.empty()) { |
| 7458 | auto TE = AmbiguousTypoExprs.back(); |
| 7459 | auto Cached = TransformCache[TE]; |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7460 | auto &State = SemaRef.getTypoExprState(TE); |
| 7461 | State.Consumer->saveCurrentPosition(); |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7462 | TransformCache.erase(TE); |
| 7463 | if (!TryTransform(E).isInvalid()) { |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7464 | State.Consumer->resetCorrectionStream(); |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7465 | TransformCache.erase(TE); |
| 7466 | Res = ExprError(); |
| 7467 | break; |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7468 | } |
| 7469 | AmbiguousTypoExprs.remove(TE); |
| 7470 | State.Consumer->restoreSavedPosition(); |
| 7471 | TransformCache[TE] = Cached; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7472 | } |
Kaelyn Takata | 26ffc5f | 2015-06-25 23:47:39 +0000 | [diff] [blame] | 7473 | SemaRef.DisableTypoCorrection = false; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7474 | |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7475 | // 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] | 7476 | if (!Res.isUsable()) |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7477 | FindTypoExprs(TypoExprs).TraverseStmt(E); |
| 7478 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7479 | EmitAllDiagnostics(); |
| 7480 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7481 | return Res; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7482 | } |
| 7483 | |
| 7484 | ExprResult TransformTypoExpr(TypoExpr *E) { |
| 7485 | // If the TypoExpr hasn't been seen before, record it. Otherwise, return the |
| 7486 | // cached transformation result if there is one and the TypoExpr isn't the |
| 7487 | // first one that was encountered. |
| 7488 | auto &CacheEntry = TransformCache[E]; |
| 7489 | if (!TypoExprs.insert(E) && !CacheEntry.isUnset()) { |
| 7490 | return CacheEntry; |
| 7491 | } |
| 7492 | |
| 7493 | auto &State = SemaRef.getTypoExprState(E); |
| 7494 | assert(State.Consumer && "Cannot transform a cleared TypoExpr"); |
| 7495 | |
| 7496 | // For the first TypoExpr and an uncached TypoExpr, find the next likely |
| 7497 | // typo correction and return it. |
| 7498 | while (TypoCorrection TC = State.Consumer->getNextCorrection()) { |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7499 | if (InitDecl && TC.getFoundDecl() == InitDecl) |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7500 | continue; |
Richard Smith | 1cf4541 | 2017-01-04 23:14:16 +0000 | [diff] [blame] | 7501 | // FIXME: If we would typo-correct to an invalid declaration, it's |
| 7502 | // probably best to just suppress all errors from this typo correction. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7503 | ExprResult NE = State.RecoveryHandler ? |
| 7504 | State.RecoveryHandler(SemaRef, E, TC) : |
| 7505 | attemptRecovery(SemaRef, *State.Consumer, TC); |
| 7506 | if (!NE.isInvalid()) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7507 | // Check whether there may be a second viable correction with the same |
| 7508 | // edit distance; if so, remember this TypoExpr may have an ambiguous |
| 7509 | // correction so it can be more thoroughly vetted later. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7510 | TypoCorrection Next; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7511 | if ((Next = State.Consumer->peekNextCorrection()) && |
| 7512 | Next.getEditDistance(false) == TC.getEditDistance(false)) { |
| 7513 | AmbiguousTypoExprs.insert(E); |
| 7514 | } else { |
| 7515 | AmbiguousTypoExprs.remove(E); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7516 | } |
| 7517 | assert(!NE.isUnset() && |
| 7518 | "Typo was transformed into a valid-but-null ExprResult"); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7519 | return CacheEntry = NE; |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7520 | } |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7521 | } |
| 7522 | return CacheEntry = ExprError(); |
| 7523 | } |
| 7524 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7525 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7526 | |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7527 | ExprResult |
| 7528 | Sema::CorrectDelayedTyposInExpr(Expr *E, VarDecl *InitDecl, |
| 7529 | llvm::function_ref<ExprResult(Expr *)> Filter) { |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7530 | // If the current evaluation context indicates there are uncorrected typos |
| 7531 | // and the current expression isn't guaranteed to not have typos, try to |
| 7532 | // resolve any TypoExpr nodes that might be in the expression. |
Kaelyn Takata | c71dda2 | 2014-12-02 22:05:35 +0000 | [diff] [blame] | 7533 | if (E && !ExprEvalContexts.empty() && ExprEvalContexts.back().NumTypos && |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7534 | (E->isTypeDependent() || E->isValueDependent() || |
| 7535 | E->isInstantiationDependent())) { |
Kaelyn Takata | c49838b | 2015-01-28 21:10:46 +0000 | [diff] [blame] | 7536 | auto TyposInContext = ExprEvalContexts.back().NumTypos; |
| 7537 | assert(TyposInContext < ~0U && "Recursive call of CorrectDelayedTyposInExpr"); |
| 7538 | ExprEvalContexts.back().NumTypos = ~0U; |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7539 | auto TyposResolved = DelayedTypos.size(); |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7540 | auto Result = TransformTypos(*this, InitDecl, Filter).Transform(E); |
Kaelyn Takata | c49838b | 2015-01-28 21:10:46 +0000 | [diff] [blame] | 7541 | ExprEvalContexts.back().NumTypos = TyposInContext; |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7542 | TyposResolved -= DelayedTypos.size(); |
Nick Lewycky | 4d59b77 | 2014-12-16 22:02:06 +0000 | [diff] [blame] | 7543 | if (Result.isInvalid() || Result.get() != E) { |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7544 | ExprEvalContexts.back().NumTypos -= TyposResolved; |
| 7545 | return Result; |
| 7546 | } |
Nick Lewycky | 4d59b77 | 2014-12-16 22:02:06 +0000 | [diff] [blame] | 7547 | assert(TyposResolved == 0 && "Corrected typo but got same Expr back?"); |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7548 | } |
| 7549 | return E; |
| 7550 | } |
| 7551 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7552 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC, |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7553 | bool DiscardedValue, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7554 | bool IsConstexpr, |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7555 | bool IsLambdaInitCaptureInitializer) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7556 | ExprResult FullExpr = FE; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7557 | |
| 7558 | if (!FullExpr.get()) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 7559 | return ExprError(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7560 | |
| 7561 | // If we are an init-expression in a lambdas init-capture, we should not |
| 7562 | // diagnose an unexpanded pack now (will be diagnosed once lambda-expr |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7563 | // containing full-expression is done). |
| 7564 | // template<class ... Ts> void test(Ts ... t) { |
| 7565 | // test([&a(t)]() { <-- (t) is an init-expr that shouldn't be diagnosed now. |
| 7566 | // return a; |
| 7567 | // }() ...); |
| 7568 | // } |
| 7569 | // FIXME: This is a hack. It would be better if we pushed the lambda scope |
| 7570 | // when we parse the lambda introducer, and teach capturing (but not |
| 7571 | // unexpanded pack detection) to walk over LambdaScopeInfos which don't have a |
| 7572 | // corresponding class yet (that is, have LambdaScopeInfo either represent a |
| 7573 | // lambda where we've entered the introducer but not the body, or represent a |
| 7574 | // lambda where we've entered the body, depending on where the |
| 7575 | // parser/instantiation has got to). |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7576 | if (!IsLambdaInitCaptureInitializer && |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7577 | DiagnoseUnexpandedParameterPack(FullExpr.get())) |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 7578 | return ExprError(); |
| 7579 | |
Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7580 | // Top-level expressions default to 'id' when we're in a debugger. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7581 | if (DiscardedValue && getLangOpts().DebuggerCastResultToId && |
Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7582 | FullExpr.get()->getType() == Context.UnknownAnyTy) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7583 | FullExpr = forceUnknownAnyToType(FullExpr.get(), Context.getObjCIdType()); |
Douglas Gregor | 95715f9 | 2011-12-15 00:53:32 +0000 | [diff] [blame] | 7584 | if (FullExpr.isInvalid()) |
| 7585 | return ExprError(); |
| 7586 | } |
Douglas Gregor | 0ec210b | 2011-03-07 02:05:23 +0000 | [diff] [blame] | 7587 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7588 | if (DiscardedValue) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7589 | FullExpr = CheckPlaceholderExpr(FullExpr.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7590 | if (FullExpr.isInvalid()) |
| 7591 | return ExprError(); |
| 7592 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7593 | FullExpr = IgnoredValueConversions(FullExpr.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7594 | if (FullExpr.isInvalid()) |
| 7595 | return ExprError(); |
| 7596 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7597 | |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7598 | FullExpr = CorrectDelayedTyposInExpr(FullExpr.get()); |
| 7599 | if (FullExpr.isInvalid()) |
| 7600 | return ExprError(); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7601 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7602 | CheckCompletedExpr(FullExpr.get(), CC, IsConstexpr); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7603 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7604 | // At the end of this full expression (which could be a deeply nested |
| 7605 | // lambda), if there is a potential capture within the nested lambda, |
Faisal Vali | 218e94b | 2013-11-12 03:56:08 +0000 | [diff] [blame] | 7606 | // have the outer capture-able lambda try and capture it. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7607 | // Consider the following code: |
| 7608 | // void f(int, int); |
| 7609 | // void f(const int&, double); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7610 | // void foo() { |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7611 | // const int x = 10, y = 20; |
| 7612 | // auto L = [=](auto a) { |
| 7613 | // auto M = [=](auto b) { |
| 7614 | // f(x, b); <-- requires x to be captured by L and M |
| 7615 | // f(y, a); <-- requires y to be captured by L, but not all Ms |
| 7616 | // }; |
| 7617 | // }; |
| 7618 | // } |
| 7619 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7620 | // FIXME: Also consider what happens for something like this that involves |
| 7621 | // the gnu-extension statement-expressions or even lambda-init-captures: |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7622 | // void f() { |
| 7623 | // const int n = 0; |
| 7624 | // auto L = [&](auto a) { |
| 7625 | // +n + ({ 0; a; }); |
| 7626 | // }; |
| 7627 | // } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7628 | // |
| 7629 | // Here, we see +n, and then the full-expression 0; ends, so we don't |
| 7630 | // capture n (and instead remove it from our list of potential captures), |
| 7631 | // 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] | 7632 | // for us to see that we need to capture n after all. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7633 | |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7634 | LambdaScopeInfo *const CurrentLSI = |
| 7635 | getCurLambda(/*IgnoreCapturedRegions=*/true); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7636 | // FIXME: PR 17877 showed that getCurLambda() can return a valid pointer |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7637 | // 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] | 7638 | // for an example of the code that might cause this asynchrony. |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7639 | // By ensuring we are in the context of a lambda's call operator |
| 7640 | // 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] | 7641 | // 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] | 7642 | // PR, a proper fix would entail : |
| 7643 | // "Alternative suggestion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7644 | // - Add to Sema an integer holding the smallest (outermost) scope |
| 7645 | // index that we are *lexically* within, and save/restore/set to |
| 7646 | // FunctionScopes.size() in InstantiatingTemplate's |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7647 | // constructor/destructor. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7648 | // - Teach the handful of places that iterate over FunctionScopes to |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7649 | // stop at the outermost enclosing lexical scope." |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7650 | DeclContext *DC = CurContext; |
| 7651 | while (DC && isa<CapturedDecl>(DC)) |
| 7652 | DC = DC->getParent(); |
| 7653 | const bool IsInLambdaDeclContext = isLambdaCallOperator(DC); |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7654 | if (IsInLambdaDeclContext && CurrentLSI && |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7655 | CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid()) |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7656 | CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(FE, CurrentLSI, |
| 7657 | *this); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7658 | return MaybeCreateExprWithCleanups(FullExpr); |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 7659 | } |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 7660 | |
| 7661 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
| 7662 | if (!FullStmt) return StmtError(); |
| 7663 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7664 | return MaybeCreateStmtWithCleanups(FullStmt); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 7665 | } |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7666 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7667 | Sema::IfExistsResult |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7668 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, |
| 7669 | CXXScopeSpec &SS, |
| 7670 | const DeclarationNameInfo &TargetNameInfo) { |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7671 | DeclarationName TargetName = TargetNameInfo.getName(); |
| 7672 | if (!TargetName) |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7673 | return IER_DoesNotExist; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7674 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7675 | // If the name itself is dependent, then the result is dependent. |
| 7676 | if (TargetName.isDependentName()) |
| 7677 | return IER_Dependent; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7678 | |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7679 | // Do the redeclaration lookup in the current scope. |
| 7680 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
| 7681 | Sema::NotForRedeclaration); |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7682 | LookupParsedName(R, S, &SS); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7683 | R.suppressDiagnostics(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7684 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7685 | switch (R.getResultKind()) { |
| 7686 | case LookupResult::Found: |
| 7687 | case LookupResult::FoundOverloaded: |
| 7688 | case LookupResult::FoundUnresolvedValue: |
| 7689 | case LookupResult::Ambiguous: |
| 7690 | return IER_Exists; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7691 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7692 | case LookupResult::NotFound: |
| 7693 | return IER_DoesNotExist; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7694 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7695 | case LookupResult::NotFoundInCurrentInstantiation: |
| 7696 | return IER_Dependent; |
| 7697 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7698 | |
| 7699 | llvm_unreachable("Invalid LookupResult Kind!"); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7700 | } |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7701 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7702 | Sema::IfExistsResult |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 7703 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc, |
| 7704 | bool IsIfExists, CXXScopeSpec &SS, |
| 7705 | UnqualifiedId &Name) { |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7706 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7707 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 7708 | // Check for an unexpanded parameter pack. |
| 7709 | auto UPPC = IsIfExists ? UPPC_IfExists : UPPC_IfNotExists; |
| 7710 | if (DiagnoseUnexpandedParameterPack(SS, UPPC) || |
| 7711 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC)) |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 7712 | return IER_Error; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7713 | |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7714 | return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo); |
| 7715 | } |