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" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 27 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 29 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 30 | #include "clang/Sema/DeclSpec.h" |
| 31 | #include "clang/Sema/Initialization.h" |
| 32 | #include "clang/Sema/Lookup.h" |
| 33 | #include "clang/Sema/ParsedTemplate.h" |
| 34 | #include "clang/Sema/Scope.h" |
| 35 | #include "clang/Sema/ScopeInfo.h" |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 36 | #include "clang/Sema/SemaLambda.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 37 | #include "clang/Sema/TemplateDeduction.h" |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/APInt.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8b0cf1d | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 41 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 42 | using namespace sema; |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 43 | |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 44 | /// \brief Handle the result of the special case name lookup for inheriting |
| 45 | /// constructor declarations. 'NS::X::X' and 'NS::X<...>::X' are treated as |
| 46 | /// constructor names in member using declarations, even if 'X' is not the |
| 47 | /// name of the corresponding type. |
| 48 | ParsedType Sema::getInheritingConstructorName(CXXScopeSpec &SS, |
| 49 | SourceLocation NameLoc, |
| 50 | IdentifierInfo &Name) { |
| 51 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
| 52 | |
| 53 | // Convert the nested-name-specifier into a type. |
| 54 | QualType Type; |
| 55 | switch (NNS->getKind()) { |
| 56 | case NestedNameSpecifier::TypeSpec: |
| 57 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 58 | Type = QualType(NNS->getAsType(), 0); |
| 59 | break; |
| 60 | |
| 61 | case NestedNameSpecifier::Identifier: |
| 62 | // Strip off the last layer of the nested-name-specifier and build a |
| 63 | // typename type for it. |
| 64 | assert(NNS->getAsIdentifier() == &Name && "not a constructor name"); |
| 65 | Type = Context.getDependentNameType(ETK_None, NNS->getPrefix(), |
| 66 | NNS->getAsIdentifier()); |
| 67 | break; |
| 68 | |
| 69 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 70 | case NestedNameSpecifier::Super: |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 71 | case NestedNameSpecifier::Namespace: |
| 72 | case NestedNameSpecifier::NamespaceAlias: |
| 73 | llvm_unreachable("Nested name specifier is not a type for inheriting ctor"); |
| 74 | } |
| 75 | |
| 76 | // This reference to the type is located entirely at the location of the |
| 77 | // final identifier in the qualified-id. |
| 78 | return CreateParsedType(Type, |
| 79 | Context.getTrivialTypeSourceInfo(Type, NameLoc)); |
| 80 | } |
| 81 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 82 | ParsedType Sema::getDestructorName(SourceLocation TildeLoc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 83 | IdentifierInfo &II, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 84 | SourceLocation NameLoc, |
| 85 | Scope *S, CXXScopeSpec &SS, |
| 86 | ParsedType ObjectTypePtr, |
| 87 | bool EnteringContext) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 88 | // Determine where to perform name lookup. |
| 89 | |
| 90 | // FIXME: This area of the standard is very messy, and the current |
| 91 | // wording is rather unclear about which scopes we search for the |
| 92 | // destructor name; see core issues 399 and 555. Issue 399 in |
| 93 | // particular shows where the current description of destructor name |
| 94 | // lookup is completely out of line with existing practice, e.g., |
| 95 | // this appears to be ill-formed: |
| 96 | // |
| 97 | // namespace N { |
| 98 | // template <typename T> struct S { |
| 99 | // ~S(); |
| 100 | // }; |
| 101 | // } |
| 102 | // |
| 103 | // void f(N::S<int>* s) { |
| 104 | // s->N::S<int>::~S(); |
| 105 | // } |
| 106 | // |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 107 | // See also PR6358 and PR6359. |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 108 | // For this reason, we're currently only doing the C++03 version of this |
| 109 | // 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] | 110 | QualType SearchType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 111 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 112 | bool isDependent = false; |
| 113 | bool LookInScope = false; |
| 114 | |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 115 | if (SS.isInvalid()) |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 116 | return nullptr; |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 117 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 118 | // If we have an object type, it's because we are in a |
| 119 | // pseudo-destructor-expression or a member access expression, and |
| 120 | // we know what type we're looking for. |
| 121 | if (ObjectTypePtr) |
| 122 | SearchType = GetTypeFromParser(ObjectTypePtr); |
| 123 | |
| 124 | if (SS.isSet()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 125 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 126 | |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 127 | bool AlreadySearched = false; |
| 128 | bool LookAtPrefix = true; |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 129 | // C++11 [basic.lookup.qual]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 130 | // 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] | 131 | // 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] | 132 | // nested-name-specifier. Similarly, in a qualified-id of the form: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 133 | // |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 134 | // nested-name-specifier[opt] class-name :: ~ class-name |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 135 | // |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 136 | // 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] | 137 | // |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 138 | // Here, we determine whether the code below is permitted to look at the |
| 139 | // prefix of the nested-name-specifier. |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 140 | DeclContext *DC = computeDeclContext(SS, EnteringContext); |
| 141 | if (DC && DC->isFileContext()) { |
| 142 | AlreadySearched = true; |
| 143 | LookupCtx = DC; |
| 144 | isDependent = false; |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 145 | } else if (DC && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 146 | LookAtPrefix = false; |
David Majnemer | e37a6ce | 2014-05-21 20:19:59 +0000 | [diff] [blame] | 147 | LookInScope = true; |
| 148 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 149 | |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 150 | // The second case from the C++03 rules quoted further above. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 151 | NestedNameSpecifier *Prefix = nullptr; |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 152 | if (AlreadySearched) { |
| 153 | // Nothing left to do. |
| 154 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { |
| 155 | CXXScopeSpec PrefixSS; |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 156 | PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data())); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 157 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); |
| 158 | isDependent = isDependentScopeSpecifier(PrefixSS); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 159 | } else if (ObjectTypePtr) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 160 | LookupCtx = computeDeclContext(SearchType); |
| 161 | isDependent = SearchType->isDependentType(); |
| 162 | } else { |
| 163 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 164 | isDependent = LookupCtx && LookupCtx->isDependentContext(); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 165 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 166 | } else if (ObjectTypePtr) { |
| 167 | // C++ [basic.lookup.classref]p3: |
| 168 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 169 | // in the context of the entire postfix-expression. If the type T |
| 170 | // of the object expression is of a class type C, the type-name is |
| 171 | // also looked up in the scope of class C. At least one of the |
| 172 | // lookups shall find a name that refers to (possibly |
| 173 | // cv-qualified) T. |
| 174 | LookupCtx = computeDeclContext(SearchType); |
| 175 | isDependent = SearchType->isDependentType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 176 | assert((isDependent || !SearchType->isIncompleteType()) && |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 177 | "Caller should have completed object type"); |
| 178 | |
| 179 | LookInScope = true; |
| 180 | } else { |
| 181 | // Perform lookup into the current scope (only). |
| 182 | LookInScope = true; |
| 183 | } |
| 184 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 185 | TypeDecl *NonMatchingTypeDecl = nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 186 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); |
| 187 | for (unsigned Step = 0; Step != 2; ++Step) { |
| 188 | // Look for the name first in the computed lookup context (if we |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 189 | // 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] | 190 | // we're allowed to look there). |
| 191 | Found.clear(); |
| 192 | if (Step == 0 && LookupCtx) |
| 193 | LookupQualifiedName(Found, LookupCtx); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 194 | else if (Step == 1 && LookInScope && S) |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 195 | LookupName(Found, S); |
| 196 | else |
| 197 | continue; |
| 198 | |
| 199 | // FIXME: Should we be suppressing ambiguities here? |
| 200 | if (Found.isAmbiguous()) |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 201 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 202 | |
| 203 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
| 204 | QualType T = Context.getTypeDeclType(Type); |
Nico Weber | 83a6387 | 2014-11-12 04:33:52 +0000 | [diff] [blame] | 205 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 206 | |
| 207 | if (SearchType.isNull() || SearchType->isDependentType() || |
| 208 | Context.hasSameUnqualifiedType(T, SearchType)) { |
| 209 | // We found our type! |
| 210 | |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 211 | return CreateParsedType(T, |
| 212 | Context.getTrivialTypeSourceInfo(T, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 213 | } |
John Wiegley | b4a9e51 | 2011-03-08 08:13:22 +0000 | [diff] [blame] | 214 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 215 | if (!SearchType.isNull()) |
| 216 | NonMatchingTypeDecl = Type; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // If the name that we found is a class template name, and it is |
| 220 | // the same name as the template name in the last part of the |
| 221 | // nested-name-specifier (if present) or the object type, then |
| 222 | // this is the destructor for that class. |
| 223 | // FIXME: This is a workaround until we get real drafting for core |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 224 | // issue 399, for which there isn't even an obvious direction. |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 225 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
| 226 | QualType MemberOfType; |
| 227 | if (SS.isSet()) { |
| 228 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
| 229 | // Figure out the type of the context, if it has one. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 230 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) |
| 231 | MemberOfType = Context.getTypeDeclType(Record); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | if (MemberOfType.isNull()) |
| 235 | MemberOfType = SearchType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 237 | if (MemberOfType.isNull()) |
| 238 | continue; |
| 239 | |
| 240 | // We're referring into a class template specialization. If the |
| 241 | // class template we found is the same as the template being |
| 242 | // specialized, we found what we are looking for. |
| 243 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
| 244 | if (ClassTemplateSpecializationDecl *Spec |
| 245 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
| 246 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
| 247 | Template->getCanonicalDecl()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 248 | return CreateParsedType( |
| 249 | MemberOfType, |
| 250 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | continue; |
| 254 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 256 | // We're referring to an unresolved class template |
| 257 | // specialization. Determine whether we class template we found |
| 258 | // is the same as the template being specialized or, if we don't |
| 259 | // know which template is being specialized, that it at least |
| 260 | // has the same name. |
| 261 | if (const TemplateSpecializationType *SpecType |
| 262 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
| 263 | TemplateName SpecName = SpecType->getTemplateName(); |
| 264 | |
| 265 | // The class template we found is the same template being |
| 266 | // specialized. |
| 267 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
| 268 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 269 | return CreateParsedType( |
| 270 | MemberOfType, |
| 271 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 272 | |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | // The class template we found has the same name as the |
| 277 | // (dependent) template name being specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 278 | if (DependentTemplateName *DepTemplate |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 279 | = SpecName.getAsDependentTemplateName()) { |
| 280 | if (DepTemplate->isIdentifier() && |
| 281 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 282 | return CreateParsedType( |
| 283 | MemberOfType, |
| 284 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 285 | |
| 286 | continue; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if (isDependent) { |
| 293 | // We didn't find our type, but that's okay: it's dependent |
| 294 | // anyway. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 296 | // FIXME: What if we have no nested-name-specifier? |
| 297 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), |
| 298 | SS.getWithLocInContext(Context), |
| 299 | II, NameLoc); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 300 | return ParsedType::make(T); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 303 | if (NonMatchingTypeDecl) { |
| 304 | QualType T = Context.getTypeDeclType(NonMatchingTypeDecl); |
| 305 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) |
| 306 | << T << SearchType; |
| 307 | Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here) |
| 308 | << T; |
| 309 | } else if (ObjectTypePtr) |
| 310 | Diag(NameLoc, diag::err_ident_in_dtor_not_a_type) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 311 | << &II; |
David Blaikie | 5e026f5 | 2013-03-20 17:42:13 +0000 | [diff] [blame] | 312 | else { |
| 313 | SemaDiagnosticBuilder DtorDiag = Diag(NameLoc, |
| 314 | diag::err_destructor_class_name); |
| 315 | if (S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 316 | const DeclContext *Ctx = S->getEntity(); |
David Blaikie | 5e026f5 | 2013-03-20 17:42:13 +0000 | [diff] [blame] | 317 | if (const CXXRecordDecl *Class = dyn_cast_or_null<CXXRecordDecl>(Ctx)) |
| 318 | DtorDiag << FixItHint::CreateReplacement(SourceRange(NameLoc), |
| 319 | Class->getNameAsString()); |
| 320 | } |
| 321 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 322 | |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 323 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 326 | ParsedType Sema::getDestructorTypeForDecltype(const DeclSpec &DS, |
| 327 | ParsedType ObjectType) { |
| 328 | if (DS.getTypeSpecType() == DeclSpec::TST_error) |
| 329 | return nullptr; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 330 | |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 331 | if (DS.getTypeSpecType() == DeclSpec::TST_decltype_auto) { |
| 332 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decltype_auto_invalid); |
| 333 | return nullptr; |
| 334 | } |
| 335 | |
| 336 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype && |
| 337 | "unexpected type in getDestructorType"); |
| 338 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 339 | |
| 340 | // If we know the type of the object, check that the correct destructor |
| 341 | // type was named now; we can give better diagnostics this way. |
| 342 | QualType SearchType = GetTypeFromParser(ObjectType); |
| 343 | if (!SearchType.isNull() && !SearchType->isDependentType() && |
| 344 | !Context.hasSameUnqualifiedType(T, SearchType)) { |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 345 | Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch) |
| 346 | << T << SearchType; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 347 | return nullptr; |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return ParsedType::make(T); |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 353 | bool Sema::checkLiteralOperatorId(const CXXScopeSpec &SS, |
| 354 | const UnqualifiedId &Name) { |
| 355 | assert(Name.getKind() == UnqualifiedId::IK_LiteralOperatorId); |
| 356 | |
| 357 | if (!SS.isValid()) |
| 358 | return false; |
| 359 | |
| 360 | switch (SS.getScopeRep()->getKind()) { |
| 361 | case NestedNameSpecifier::Identifier: |
| 362 | case NestedNameSpecifier::TypeSpec: |
| 363 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 364 | // Per C++11 [over.literal]p2, literal operators can only be declared at |
| 365 | // namespace scope. Therefore, this unqualified-id cannot name anything. |
| 366 | // Reject it early, because we have no AST representation for this in the |
| 367 | // case where the scope is dependent. |
| 368 | Diag(Name.getLocStart(), diag::err_literal_operator_id_outside_namespace) |
| 369 | << SS.getScopeRep(); |
| 370 | return true; |
| 371 | |
| 372 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 373 | case NestedNameSpecifier::Super: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 374 | case NestedNameSpecifier::Namespace: |
| 375 | case NestedNameSpecifier::NamespaceAlias: |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | llvm_unreachable("unknown nested name specifier kind"); |
| 380 | } |
| 381 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 382 | /// \brief Build a C++ typeid expression with a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 383 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 384 | SourceLocation TypeidLoc, |
| 385 | TypeSourceInfo *Operand, |
| 386 | SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 387 | // C++ [expr.typeid]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 388 | // 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] | 389 | // that is the operand of typeid are always ignored. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 390 | // 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] | 391 | // type, the class shall be completely-defined. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 392 | Qualifiers Quals; |
| 393 | QualType T |
| 394 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
| 395 | Quals); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 396 | if (T->getAs<RecordType>() && |
| 397 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 398 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 399 | |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 400 | if (T->isVariablyModifiedType()) |
| 401 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) << T); |
| 402 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 403 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), Operand, |
| 404 | SourceRange(TypeidLoc, RParenLoc)); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /// \brief Build a C++ typeid expression with an expression operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 408 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 409 | SourceLocation TypeidLoc, |
| 410 | Expr *E, |
| 411 | SourceLocation RParenLoc) { |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 412 | bool WasEvaluated = false; |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 413 | if (E && !E->isTypeDependent()) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 414 | if (E->getType()->isPlaceholderType()) { |
| 415 | ExprResult result = CheckPlaceholderExpr(E); |
| 416 | if (result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 417 | E = result.get(); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 420 | QualType T = E->getType(); |
| 421 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
| 422 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 423 | // C++ [expr.typeid]p3: |
| 424 | // [...] If the type of the expression is a class type, the class |
| 425 | // shall be completely-defined. |
| 426 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 427 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 428 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 429 | // C++ [expr.typeid]p3: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 430 | // 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] | 431 | // polymorphic class type [...] [the] expression is an unevaluated |
| 432 | // operand. [...] |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 433 | if (RecordD->isPolymorphic() && E->isGLValue()) { |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 434 | // The subexpression is potentially evaluated; switch the context |
| 435 | // and recheck the subexpression. |
Benjamin Kramer | d81108f | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 436 | ExprResult Result = TransformToPotentiallyEvaluated(E); |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 437 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 438 | E = Result.get(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 439 | |
| 440 | // We require a vtable to query the type at run time. |
| 441 | MarkVTableUsed(TypeidLoc, RecordD); |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 442 | WasEvaluated = true; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 443 | } |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 444 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 446 | // C++ [expr.typeid]p4: |
| 447 | // [...] 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] | 448 | // cv-qualified type, the result of the typeid expression refers to a |
| 449 | // std::type_info object representing the cv-unqualified referenced |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 450 | // type. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 451 | Qualifiers Quals; |
| 452 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
| 453 | if (!Context.hasSameType(T, UnqualT)) { |
| 454 | T = UnqualT; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 455 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).get(); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 458 | |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 459 | if (E->getType()->isVariablyModifiedType()) |
| 460 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) |
| 461 | << E->getType()); |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 462 | else if (!inTemplateInstantiation() && |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 463 | E->HasSideEffects(Context, WasEvaluated)) { |
| 464 | // The expression operand for typeid is in an unevaluated expression |
| 465 | // context, so side effects could result in unintended consequences. |
| 466 | Diag(E->getExprLoc(), WasEvaluated |
| 467 | ? diag::warn_side_effects_typeid |
| 468 | : diag::warn_side_effects_unevaluated_context); |
| 469 | } |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 470 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 471 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), E, |
| 472 | SourceRange(TypeidLoc, RParenLoc)); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 476 | ExprResult |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 477 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 478 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 479 | // Find the std::type_info type. |
Sebastian Redl | 7ac9741 | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 480 | if (!getStdNamespace()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 481 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 483 | if (!CXXTypeInfoDecl) { |
| 484 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 485 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 486 | LookupQualifiedName(R, getStdNamespace()); |
| 487 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
Nico Weber | 5f96883 | 2012-06-19 23:58:27 +0000 | [diff] [blame] | 488 | // Microsoft's typeinfo doesn't have type_info in std but in the global |
| 489 | // namespace if _HAS_EXCEPTIONS is defined to 0. See PR13153. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 490 | if (!CXXTypeInfoDecl && LangOpts.MSVCCompat) { |
Nico Weber | 5f96883 | 2012-06-19 23:58:27 +0000 | [diff] [blame] | 491 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 492 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 493 | } |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 494 | if (!CXXTypeInfoDecl) |
| 495 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 496 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 497 | |
Nico Weber | 1b7f39d | 2012-05-20 01:27:21 +0000 | [diff] [blame] | 498 | if (!getLangOpts().RTTI) { |
| 499 | return ExprError(Diag(OpLoc, diag::err_no_typeid_with_fno_rtti)); |
| 500 | } |
| 501 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 502 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 504 | if (isType) { |
| 505 | // The operand is a type; handle it as such. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 506 | TypeSourceInfo *TInfo = nullptr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 507 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 508 | &TInfo); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 509 | if (T.isNull()) |
| 510 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 511 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 512 | if (!TInfo) |
| 513 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 514 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 515 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 516 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 518 | // The operand is an expression. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 519 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 520 | } |
| 521 | |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 522 | /// Grabs __declspec(uuid()) off a type, or returns 0 if we cannot resolve to |
| 523 | /// a single GUID. |
| 524 | static void |
| 525 | getUuidAttrOfType(Sema &SemaRef, QualType QT, |
| 526 | llvm::SmallSetVector<const UuidAttr *, 1> &UuidAttrs) { |
| 527 | // Optionally remove one level of pointer, reference or array indirection. |
| 528 | const Type *Ty = QT.getTypePtr(); |
| 529 | if (QT->isPointerType() || QT->isReferenceType()) |
| 530 | Ty = QT->getPointeeType().getTypePtr(); |
| 531 | else if (QT->isArrayType()) |
| 532 | Ty = Ty->getBaseElementTypeUnsafe(); |
| 533 | |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 534 | const auto *TD = Ty->getAsTagDecl(); |
| 535 | if (!TD) |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 536 | return; |
| 537 | |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 538 | if (const auto *Uuid = TD->getMostRecentDecl()->getAttr<UuidAttr>()) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 539 | UuidAttrs.insert(Uuid); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | // __uuidof can grab UUIDs from template arguments. |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 544 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(TD)) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 545 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
| 546 | for (const TemplateArgument &TA : TAL.asArray()) { |
| 547 | const UuidAttr *UuidForTA = nullptr; |
| 548 | if (TA.getKind() == TemplateArgument::Type) |
| 549 | getUuidAttrOfType(SemaRef, TA.getAsType(), UuidAttrs); |
| 550 | else if (TA.getKind() == TemplateArgument::Declaration) |
| 551 | getUuidAttrOfType(SemaRef, TA.getAsDecl()->getType(), UuidAttrs); |
| 552 | |
| 553 | if (UuidForTA) |
| 554 | UuidAttrs.insert(UuidForTA); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 559 | /// \brief Build a Microsoft __uuidof expression with a type operand. |
| 560 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 561 | SourceLocation TypeidLoc, |
| 562 | TypeSourceInfo *Operand, |
| 563 | SourceLocation RParenLoc) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 564 | StringRef UuidStr; |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 565 | if (!Operand->getType()->isDependentType()) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 566 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
| 567 | getUuidAttrOfType(*this, Operand->getType(), UuidAttrs); |
| 568 | if (UuidAttrs.empty()) |
| 569 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 570 | if (UuidAttrs.size() > 1) |
| 571 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 572 | UuidStr = UuidAttrs.back()->getGuid(); |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 573 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 574 | |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 575 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), Operand, UuidStr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 576 | SourceRange(TypeidLoc, RParenLoc)); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /// \brief Build a Microsoft __uuidof expression with an expression operand. |
| 580 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 581 | SourceLocation TypeidLoc, |
| 582 | Expr *E, |
| 583 | SourceLocation RParenLoc) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 584 | StringRef UuidStr; |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 585 | if (!E->getType()->isDependentType()) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 586 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 587 | UuidStr = "00000000-0000-0000-0000-000000000000"; |
| 588 | } else { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 589 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
| 590 | getUuidAttrOfType(*this, E->getType(), UuidAttrs); |
| 591 | if (UuidAttrs.empty()) |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 592 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 593 | if (UuidAttrs.size() > 1) |
| 594 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 595 | UuidStr = UuidAttrs.back()->getGuid(); |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 596 | } |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 597 | } |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 598 | |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 599 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), E, UuidStr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 600 | SourceRange(TypeidLoc, RParenLoc)); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 604 | ExprResult |
| 605 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 606 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | // If MSVCGuidDecl has not been cached, do the lookup. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 608 | if (!MSVCGuidDecl) { |
| 609 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 610 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 611 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 612 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 613 | if (!MSVCGuidDecl) |
| 614 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 617 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 618 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 619 | if (isType) { |
| 620 | // The operand is a type; handle it as such. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 621 | TypeSourceInfo *TInfo = nullptr; |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 622 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 623 | &TInfo); |
| 624 | if (T.isNull()) |
| 625 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 626 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 627 | if (!TInfo) |
| 628 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 629 | |
| 630 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 631 | } |
| 632 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 633 | // The operand is an expression. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 634 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 635 | } |
| 636 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 637 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 638 | ExprResult |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 639 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 640 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Bill Wendling | bf313b0 | 2007-02-13 20:09:46 +0000 | [diff] [blame] | 641 | "Unknown C++ Boolean value!"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 642 | return new (Context) |
| 643 | CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 644 | } |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 645 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 646 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 647 | ExprResult |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 648 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 649 | return new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 652 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 653 | ExprResult |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 654 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
| 655 | bool IsThrownVarInScope = false; |
| 656 | if (Ex) { |
| 657 | // C++0x [class.copymove]p31: |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 658 | // When certain criteria are met, an implementation is allowed to omit the |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 659 | // copy/move construction of a class object [...] |
| 660 | // |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 661 | // - in a throw-expression, when the operand is the name of a |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 662 | // non-volatile automatic object (other than a function or catch- |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 663 | // clause parameter) whose scope does not extend beyond the end of the |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 664 | // innermost enclosing try-block (if there is one), the copy/move |
| 665 | // operation from the operand to the exception object (15.1) can be |
| 666 | // omitted by constructing the automatic object directly into the |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 667 | // exception object |
| 668 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) |
| 669 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 670 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { |
| 671 | for( ; S; S = S->getParent()) { |
| 672 | if (S->isDeclScope(Var)) { |
| 673 | IsThrownVarInScope = true; |
| 674 | break; |
| 675 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 676 | |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 677 | if (S->getFlags() & |
| 678 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
| 679 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | |
| 680 | Scope::TryScope)) |
| 681 | break; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 686 | |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 687 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
| 688 | } |
| 689 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 690 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 691 | bool IsThrownVarInScope) { |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 692 | // Don't report an error if 'throw' is used in system headers. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 693 | if (!getLangOpts().CXXExceptions && |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 694 | !getSourceManager().isInSystemHeader(OpLoc)) |
Anders Carlsson | b94ad3e | 2011-02-19 21:53:09 +0000 | [diff] [blame] | 695 | Diag(OpLoc, diag::err_exceptions_disabled) << "throw"; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 696 | |
Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 697 | // Exceptions aren't allowed in CUDA device code. |
| 698 | if (getLangOpts().CUDA) |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 699 | CUDADiagIfDeviceCode(OpLoc, diag::err_cuda_device_exceptions) |
| 700 | << "throw" << CurrentCUDATarget(); |
Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 701 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 702 | if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope()) |
| 703 | Diag(OpLoc, diag::err_omp_simd_region_cannot_use_stmt) << "throw"; |
| 704 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 705 | if (Ex && !Ex->isTypeDependent()) { |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 706 | QualType ExceptionObjectTy = Context.getExceptionObjectType(Ex->getType()); |
| 707 | if (CheckCXXThrowOperand(OpLoc, ExceptionObjectTy, Ex)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 708 | return ExprError(); |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 709 | |
| 710 | // Initialize the exception result. This implicitly weeds out |
| 711 | // abstract types or types with inaccessible copy constructors. |
| 712 | |
| 713 | // C++0x [class.copymove]p31: |
| 714 | // When certain criteria are met, an implementation is allowed to omit the |
| 715 | // copy/move construction of a class object [...] |
| 716 | // |
| 717 | // - in a throw-expression, when the operand is the name of a |
| 718 | // non-volatile automatic object (other than a function or |
| 719 | // catch-clause |
| 720 | // parameter) whose scope does not extend beyond the end of the |
| 721 | // innermost enclosing try-block (if there is one), the copy/move |
| 722 | // operation from the operand to the exception object (15.1) can be |
| 723 | // omitted by constructing the automatic object directly into the |
| 724 | // exception object |
| 725 | const VarDecl *NRVOVariable = nullptr; |
| 726 | if (IsThrownVarInScope) |
| 727 | NRVOVariable = getCopyElisionCandidate(QualType(), Ex, false); |
| 728 | |
| 729 | InitializedEntity Entity = InitializedEntity::InitializeException( |
| 730 | OpLoc, ExceptionObjectTy, |
| 731 | /*NRVO=*/NRVOVariable != nullptr); |
| 732 | ExprResult Res = PerformMoveOrCopyInitialization( |
| 733 | Entity, NRVOVariable, QualType(), Ex, IsThrownVarInScope); |
| 734 | if (Res.isInvalid()) |
| 735 | return ExprError(); |
| 736 | Ex = Res.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 737 | } |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 738 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 739 | return new (Context) |
| 740 | CXXThrowExpr(Ex, Context.VoidTy, OpLoc, IsThrownVarInScope); |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 741 | } |
| 742 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 743 | static void |
| 744 | collectPublicBases(CXXRecordDecl *RD, |
| 745 | llvm::DenseMap<CXXRecordDecl *, unsigned> &SubobjectsSeen, |
| 746 | llvm::SmallPtrSetImpl<CXXRecordDecl *> &VBases, |
| 747 | llvm::SetVector<CXXRecordDecl *> &PublicSubobjectsSeen, |
| 748 | bool ParentIsPublic) { |
| 749 | for (const CXXBaseSpecifier &BS : RD->bases()) { |
| 750 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
| 751 | bool NewSubobject; |
| 752 | // Virtual bases constitute the same subobject. Non-virtual bases are |
| 753 | // always distinct subobjects. |
| 754 | if (BS.isVirtual()) |
| 755 | NewSubobject = VBases.insert(BaseDecl).second; |
| 756 | else |
| 757 | NewSubobject = true; |
| 758 | |
| 759 | if (NewSubobject) |
| 760 | ++SubobjectsSeen[BaseDecl]; |
| 761 | |
| 762 | // Only add subobjects which have public access throughout the entire chain. |
| 763 | bool PublicPath = ParentIsPublic && BS.getAccessSpecifier() == AS_public; |
| 764 | if (PublicPath) |
| 765 | PublicSubobjectsSeen.insert(BaseDecl); |
| 766 | |
| 767 | // Recurse on to each base subobject. |
| 768 | collectPublicBases(BaseDecl, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
| 769 | PublicPath); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | static void getUnambiguousPublicSubobjects( |
| 774 | CXXRecordDecl *RD, llvm::SmallVectorImpl<CXXRecordDecl *> &Objects) { |
| 775 | llvm::DenseMap<CXXRecordDecl *, unsigned> SubobjectsSeen; |
| 776 | llvm::SmallSet<CXXRecordDecl *, 2> VBases; |
| 777 | llvm::SetVector<CXXRecordDecl *> PublicSubobjectsSeen; |
| 778 | SubobjectsSeen[RD] = 1; |
| 779 | PublicSubobjectsSeen.insert(RD); |
| 780 | collectPublicBases(RD, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
| 781 | /*ParentIsPublic=*/true); |
| 782 | |
| 783 | for (CXXRecordDecl *PublicSubobject : PublicSubobjectsSeen) { |
| 784 | // Skip ambiguous objects. |
| 785 | if (SubobjectsSeen[PublicSubobject] > 1) |
| 786 | continue; |
| 787 | |
| 788 | Objects.push_back(PublicSubobject); |
| 789 | } |
| 790 | } |
| 791 | |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 792 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 793 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, |
| 794 | QualType ExceptionObjectTy, Expr *E) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 795 | // If the type of the exception would be an incomplete type or a pointer |
| 796 | // 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] | 797 | QualType Ty = ExceptionObjectTy; |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 798 | bool isPointer = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 799 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 800 | Ty = Ptr->getPointeeType(); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 801 | isPointer = true; |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 802 | } |
| 803 | if (!isPointer || !Ty->isVoidType()) { |
| 804 | if (RequireCompleteType(ThrowLoc, Ty, |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 805 | isPointer ? diag::err_throw_incomplete_ptr |
| 806 | : diag::err_throw_incomplete, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 807 | E->getSourceRange())) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 808 | return true; |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 809 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 810 | if (RequireNonAbstractType(ThrowLoc, ExceptionObjectTy, |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 811 | diag::err_throw_abstract_type, E)) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 812 | return true; |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 815 | // If the exception has class type, we need additional handling. |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 816 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 817 | if (!RD) |
| 818 | return false; |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 820 | // If we are throwing a polymorphic class type or pointer thereof, |
| 821 | // exception handling will make use of the vtable. |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 822 | MarkVTableUsed(ThrowLoc, RD); |
| 823 | |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 824 | // If a pointer is thrown, the referenced object will not be destroyed. |
| 825 | if (isPointer) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 826 | return false; |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 827 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 828 | // 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] | 829 | if (!RD->hasIrrelevantDestructor()) { |
| 830 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
| 831 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
| 832 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 833 | PDiag(diag::err_access_dtor_exception) << Ty); |
| 834 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 835 | return true; |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 838 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 839 | // The MSVC ABI creates a list of all types which can catch the exception |
| 840 | // object. This list also references the appropriate copy constructor to call |
| 841 | // 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] | 842 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 843 | // We are only interested in the public, unambiguous bases contained within |
| 844 | // the exception object. Bases which are ambiguous or otherwise |
| 845 | // inaccessible are not catchable types. |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 846 | llvm::SmallVector<CXXRecordDecl *, 2> UnambiguousPublicSubobjects; |
| 847 | getUnambiguousPublicSubobjects(RD, UnambiguousPublicSubobjects); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 848 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 849 | for (CXXRecordDecl *Subobject : UnambiguousPublicSubobjects) { |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 850 | // Attempt to lookup the copy constructor. Various pieces of machinery |
| 851 | // will spring into action, like template instantiation, which means this |
| 852 | // cannot be a simple walk of the class's decls. Instead, we must perform |
| 853 | // lookup and overload resolution. |
| 854 | CXXConstructorDecl *CD = LookupCopyingConstructor(Subobject, 0); |
| 855 | if (!CD) |
| 856 | continue; |
| 857 | |
| 858 | // Mark the constructor referenced as it is used by this throw expression. |
| 859 | MarkFunctionReferenced(E->getExprLoc(), CD); |
| 860 | |
| 861 | // Skip this copy constructor if it is trivial, we don't need to record it |
| 862 | // in the catchable type data. |
| 863 | if (CD->isTrivial()) |
| 864 | continue; |
| 865 | |
| 866 | // The copy constructor is non-trivial, create a mapping from this class |
| 867 | // type to this constructor. |
| 868 | // N.B. The selection of copy constructor is not sensitive to this |
| 869 | // particular throw-site. Lookup will be performed at the catch-site to |
| 870 | // ensure that the copy constructor is, in fact, accessible (via |
| 871 | // friendship or any other means). |
| 872 | Context.addCopyConstructorForExceptionObject(Subobject, CD); |
| 873 | |
| 874 | // We don't keep the instantiated default argument expressions around so |
| 875 | // we must rebuild them here. |
| 876 | for (unsigned I = 1, E = CD->getNumParams(); I != E; ++I) { |
Reid Kleckner | c01ee75 | 2016-11-23 16:51:30 +0000 | [diff] [blame] | 877 | if (CheckCXXDefaultArgExpr(ThrowLoc, CD, CD->getParamDecl(I))) |
| 878 | return true; |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | } |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 882 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 883 | return false; |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 884 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 885 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 886 | static QualType adjustCVQualifiersForCXXThisWithinLambda( |
| 887 | ArrayRef<FunctionScopeInfo *> FunctionScopes, QualType ThisTy, |
| 888 | DeclContext *CurSemaContext, ASTContext &ASTCtx) { |
| 889 | |
| 890 | QualType ClassType = ThisTy->getPointeeType(); |
| 891 | LambdaScopeInfo *CurLSI = nullptr; |
| 892 | DeclContext *CurDC = CurSemaContext; |
| 893 | |
| 894 | // Iterate through the stack of lambdas starting from the innermost lambda to |
| 895 | // the outermost lambda, checking if '*this' is ever captured by copy - since |
| 896 | // that could change the cv-qualifiers of the '*this' object. |
| 897 | // The object referred to by '*this' starts out with the cv-qualifiers of its |
| 898 | // member function. We then start with the innermost lambda and iterate |
| 899 | // outward checking to see if any lambda performs a by-copy capture of '*this' |
| 900 | // - and if so, any nested lambda must respect the 'constness' of that |
| 901 | // capturing lamdbda's call operator. |
| 902 | // |
| 903 | |
| 904 | // The issue is that we cannot rely entirely on the FunctionScopeInfo stack |
| 905 | // since ScopeInfos are pushed on during parsing and treetransforming. But |
| 906 | // since a generic lambda's call operator can be instantiated anywhere (even |
| 907 | // end of the TU) we need to be able to examine its enclosing lambdas and so |
| 908 | // we use the DeclContext to get a hold of the closure-class and query it for |
| 909 | // capture information. The reason we don't just resort to always using the |
| 910 | // DeclContext chain is that it is only mature for lambda expressions |
| 911 | // enclosing generic lambda's call operators that are being instantiated. |
| 912 | |
| 913 | for (int I = FunctionScopes.size(); |
| 914 | I-- && isa<LambdaScopeInfo>(FunctionScopes[I]); |
| 915 | CurDC = getLambdaAwareParentOfDeclContext(CurDC)) { |
| 916 | CurLSI = cast<LambdaScopeInfo>(FunctionScopes[I]); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 917 | |
| 918 | if (!CurLSI->isCXXThisCaptured()) |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 919 | continue; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 920 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 921 | auto C = CurLSI->getCXXThisCapture(); |
| 922 | |
| 923 | if (C.isCopyCapture()) { |
| 924 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 925 | if (CurLSI->CallOperator->isConst()) |
| 926 | ClassType.addConst(); |
| 927 | return ASTCtx.getPointerType(ClassType); |
| 928 | } |
| 929 | } |
| 930 | // We've run out of ScopeInfos but check if CurDC is a lambda (which can |
| 931 | // happen during instantiation of generic lambdas) |
| 932 | if (isLambdaCallOperator(CurDC)) { |
| 933 | assert(CurLSI); |
| 934 | assert(isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator)); |
| 935 | assert(CurDC == getLambdaAwareParentOfDeclContext(CurLSI->CallOperator)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 936 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 937 | auto IsThisCaptured = |
| 938 | [](CXXRecordDecl *Closure, bool &IsByCopy, bool &IsConst) { |
| 939 | IsConst = false; |
| 940 | IsByCopy = false; |
| 941 | for (auto &&C : Closure->captures()) { |
| 942 | if (C.capturesThis()) { |
| 943 | if (C.getCaptureKind() == LCK_StarThis) |
| 944 | IsByCopy = true; |
| 945 | if (Closure->getLambdaCallOperator()->isConst()) |
| 946 | IsConst = true; |
| 947 | return true; |
| 948 | } |
| 949 | } |
| 950 | return false; |
| 951 | }; |
| 952 | |
| 953 | bool IsByCopyCapture = false; |
| 954 | bool IsConstCapture = false; |
| 955 | CXXRecordDecl *Closure = cast<CXXRecordDecl>(CurDC->getParent()); |
| 956 | while (Closure && |
| 957 | IsThisCaptured(Closure, IsByCopyCapture, IsConstCapture)) { |
| 958 | if (IsByCopyCapture) { |
| 959 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 960 | if (IsConstCapture) |
| 961 | ClassType.addConst(); |
| 962 | return ASTCtx.getPointerType(ClassType); |
| 963 | } |
| 964 | Closure = isLambdaCallOperator(Closure->getParent()) |
| 965 | ? cast<CXXRecordDecl>(Closure->getParent()->getParent()) |
| 966 | : nullptr; |
| 967 | } |
| 968 | } |
| 969 | return ASTCtx.getPointerType(ClassType); |
| 970 | } |
| 971 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 972 | QualType Sema::getCurrentThisType() { |
| 973 | DeclContext *DC = getFunctionLevelDeclContext(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 974 | QualType ThisTy = CXXThisTypeOverride; |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 975 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 976 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { |
| 977 | if (method && method->isInstance()) |
| 978 | ThisTy = method->getThisType(Context); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 979 | } |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 980 | |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 981 | if (ThisTy.isNull() && isLambdaCallOperator(CurContext) && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 982 | inTemplateInstantiation()) { |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 983 | |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 984 | assert(isa<CXXRecordDecl>(DC) && |
| 985 | "Trying to get 'this' type from static method?"); |
| 986 | |
| 987 | // This is a lambda call operator that is being instantiated as a default |
| 988 | // initializer. DC must point to the enclosing class type, so we can recover |
| 989 | // the 'this' type from it. |
| 990 | |
| 991 | QualType ClassTy = Context.getTypeDeclType(cast<CXXRecordDecl>(DC)); |
| 992 | // There are no cv-qualifiers for 'this' within default initializers, |
| 993 | // per [expr.prim.general]p4. |
| 994 | ThisTy = Context.getPointerType(ClassTy); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 995 | } |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 996 | |
| 997 | // If we are within a lambda's call operator, the cv-qualifiers of 'this' |
| 998 | // might need to be adjusted if the lambda or any of its enclosing lambda's |
| 999 | // captures '*this' by copy. |
| 1000 | if (!ThisTy.isNull() && isLambdaCallOperator(CurContext)) |
| 1001 | return adjustCVQualifiersForCXXThisWithinLambda(FunctionScopes, ThisTy, |
| 1002 | CurContext, Context); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1003 | return ThisTy; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1006 | Sema::CXXThisScopeRAII::CXXThisScopeRAII(Sema &S, |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1007 | Decl *ContextDecl, |
| 1008 | unsigned CXXThisTypeQuals, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1009 | bool Enabled) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1010 | : S(S), OldCXXThisTypeOverride(S.CXXThisTypeOverride), Enabled(false) |
| 1011 | { |
| 1012 | if (!Enabled || !ContextDecl) |
| 1013 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1014 | |
| 1015 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1016 | if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(ContextDecl)) |
| 1017 | Record = Template->getTemplatedDecl(); |
| 1018 | else |
| 1019 | Record = cast<CXXRecordDecl>(ContextDecl); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1020 | |
Andrey Bokhanko | 67a4186 | 2016-05-26 10:06:01 +0000 | [diff] [blame] | 1021 | // We care only for CVR qualifiers here, so cut everything else. |
| 1022 | CXXThisTypeQuals &= Qualifiers::FastMask; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1023 | S.CXXThisTypeOverride |
| 1024 | = S.Context.getPointerType( |
| 1025 | S.Context.getRecordType(Record).withCVRQualifiers(CXXThisTypeQuals)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1026 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1027 | this->Enabled = true; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | Sema::CXXThisScopeRAII::~CXXThisScopeRAII() { |
| 1032 | if (Enabled) { |
| 1033 | S.CXXThisTypeOverride = OldCXXThisTypeOverride; |
| 1034 | } |
| 1035 | } |
| 1036 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1037 | static Expr *captureThis(Sema &S, ASTContext &Context, RecordDecl *RD, |
| 1038 | QualType ThisTy, SourceLocation Loc, |
| 1039 | const bool ByCopy) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1040 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1041 | QualType AdjustedThisTy = ThisTy; |
| 1042 | // The type of the corresponding data member (not a 'this' pointer if 'by |
| 1043 | // copy'). |
| 1044 | QualType CaptureThisFieldTy = ThisTy; |
| 1045 | if (ByCopy) { |
| 1046 | // If we are capturing the object referred to by '*this' by copy, ignore any |
| 1047 | // cv qualifiers inherited from the type of the member function for the type |
| 1048 | // of the closure-type's corresponding data member and any use of 'this'. |
| 1049 | CaptureThisFieldTy = ThisTy->getPointeeType(); |
| 1050 | CaptureThisFieldTy.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 1051 | AdjustedThisTy = Context.getPointerType(CaptureThisFieldTy); |
| 1052 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1053 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1054 | FieldDecl *Field = FieldDecl::Create( |
| 1055 | Context, RD, Loc, Loc, nullptr, CaptureThisFieldTy, |
| 1056 | Context.getTrivialTypeSourceInfo(CaptureThisFieldTy, Loc), nullptr, false, |
| 1057 | ICIS_NoInit); |
| 1058 | |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1059 | Field->setImplicit(true); |
| 1060 | Field->setAccess(AS_private); |
| 1061 | RD->addDecl(Field); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1062 | Expr *This = |
| 1063 | new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit*/ true); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1064 | if (ByCopy) { |
| 1065 | Expr *StarThis = S.CreateBuiltinUnaryOp(Loc, |
| 1066 | UO_Deref, |
| 1067 | This).get(); |
| 1068 | InitializedEntity Entity = InitializedEntity::InitializeLambdaCapture( |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1069 | nullptr, CaptureThisFieldTy, Loc); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1070 | InitializationKind InitKind = InitializationKind::CreateDirect(Loc, Loc, Loc); |
| 1071 | InitializationSequence Init(S, Entity, InitKind, StarThis); |
| 1072 | ExprResult ER = Init.Perform(S, Entity, InitKind, StarThis); |
| 1073 | if (ER.isInvalid()) return nullptr; |
| 1074 | return ER.get(); |
| 1075 | } |
| 1076 | return This; |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1079 | bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit, |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1080 | bool BuildAndDiagnose, const unsigned *const FunctionScopeIndexToStopAt, |
| 1081 | const bool ByCopy) { |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1082 | // We don't need to capture this in an unevaluated context. |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1083 | if (isUnevaluatedContext() && !Explicit) |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1084 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1085 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1086 | assert((!ByCopy || Explicit) && "cannot implicitly capture *this by value"); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1087 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1088 | const unsigned MaxFunctionScopesIndex = FunctionScopeIndexToStopAt ? |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1089 | *FunctionScopeIndexToStopAt : FunctionScopes.size() - 1; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1090 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1091 | // Check that we can capture the *enclosing object* (referred to by '*this') |
| 1092 | // by the capturing-entity/closure (lambda/block/etc) at |
| 1093 | // MaxFunctionScopesIndex-deep on the FunctionScopes stack. |
| 1094 | |
| 1095 | // Note: The *enclosing object* can only be captured by-value by a |
| 1096 | // closure that is a lambda, using the explicit notation: |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1097 | // [*this] { ... }. |
| 1098 | // Every other capture of the *enclosing object* results in its by-reference |
| 1099 | // capture. |
| 1100 | |
| 1101 | // For a closure 'L' (at MaxFunctionScopesIndex in the FunctionScopes |
| 1102 | // stack), we can capture the *enclosing object* only if: |
| 1103 | // - 'L' has an explicit byref or byval capture of the *enclosing object* |
| 1104 | // - or, 'L' has an implicit capture. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1105 | // AND |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1106 | // -- there is no enclosing closure |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1107 | // -- or, there is some enclosing closure 'E' that has already captured the |
| 1108 | // *enclosing object*, and every intervening closure (if any) between 'E' |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1109 | // and 'L' can implicitly capture the *enclosing object*. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1110 | // -- or, every enclosing closure can implicitly capture the |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1111 | // *enclosing object* |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1112 | |
| 1113 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1114 | unsigned NumCapturingClosures = 0; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1115 | for (unsigned idx = MaxFunctionScopesIndex; idx != 0; idx--) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1116 | if (CapturingScopeInfo *CSI = |
| 1117 | dyn_cast<CapturingScopeInfo>(FunctionScopes[idx])) { |
| 1118 | if (CSI->CXXThisCaptureIndex != 0) { |
| 1119 | // 'this' is already being captured; there isn't anything more to do. |
Malcolm Parsons | 87a0362 | 2017-01-13 15:01:06 +0000 | [diff] [blame] | 1120 | CSI->Captures[CSI->CXXThisCaptureIndex - 1].markUsed(BuildAndDiagnose); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1121 | break; |
| 1122 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1123 | LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI); |
| 1124 | if (LSI && isGenericLambdaCallOperatorSpecialization(LSI->CallOperator)) { |
| 1125 | // This context can't implicitly capture 'this'; fail out. |
| 1126 | if (BuildAndDiagnose) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1127 | Diag(Loc, diag::err_this_capture) |
| 1128 | << (Explicit && idx == MaxFunctionScopesIndex); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1129 | return true; |
| 1130 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1131 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref || |
Douglas Gregor | a1bffa2 | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 1132 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval || |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 1133 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block || |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1134 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_CapturedRegion || |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1135 | (Explicit && idx == MaxFunctionScopesIndex)) { |
| 1136 | // Regarding (Explicit && idx == MaxFunctionScopesIndex): only the first |
| 1137 | // iteration through can be an explicit capture, all enclosing closures, |
| 1138 | // if any, must perform implicit captures. |
| 1139 | |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 1140 | // This closure can capture 'this'; continue looking upwards. |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1141 | NumCapturingClosures++; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1142 | continue; |
| 1143 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1144 | // This context can't implicitly capture 'this'; fail out. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1145 | if (BuildAndDiagnose) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1146 | Diag(Loc, diag::err_this_capture) |
| 1147 | << (Explicit && idx == MaxFunctionScopesIndex); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1148 | return true; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1149 | } |
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 | if (!BuildAndDiagnose) return false; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1153 | |
| 1154 | // If we got here, then the closure at MaxFunctionScopesIndex on the |
| 1155 | // FunctionScopes stack, can capture the *enclosing object*, so capture it |
| 1156 | // (including implicit by-reference captures in any enclosing closures). |
| 1157 | |
| 1158 | // In the loop below, respect the ByCopy flag only for the closure requesting |
| 1159 | // 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] | 1160 | // all enclosing closure's up to NumCapturingClosures (since they must be |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1161 | // implicitly capturing the *enclosing object* by reference (see loop |
| 1162 | // above)). |
| 1163 | assert((!ByCopy || |
| 1164 | dyn_cast<LambdaScopeInfo>(FunctionScopes[MaxFunctionScopesIndex])) && |
| 1165 | "Only a lambda can capture the enclosing object (referred to by " |
| 1166 | "*this) by copy"); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1167 | // FIXME: We need to delay this marking in PotentiallyPotentiallyEvaluated |
| 1168 | // contexts. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1169 | QualType ThisTy = getCurrentThisType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1170 | for (unsigned idx = MaxFunctionScopesIndex; NumCapturingClosures; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1171 | --idx, --NumCapturingClosures) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1172 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1173 | Expr *ThisExpr = nullptr; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1174 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1175 | if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI)) { |
| 1176 | // For lambda expressions, build a field and an initializing expression, |
| 1177 | // and capture the *enclosing object* by copy only if this is the first |
| 1178 | // iteration. |
| 1179 | ThisExpr = captureThis(*this, Context, LSI->Lambda, ThisTy, Loc, |
| 1180 | ByCopy && idx == MaxFunctionScopesIndex); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1181 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1182 | } else if (CapturedRegionScopeInfo *RSI |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1183 | = dyn_cast<CapturedRegionScopeInfo>(FunctionScopes[idx])) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1184 | ThisExpr = |
| 1185 | captureThis(*this, Context, RSI->TheRecordDecl, ThisTy, Loc, |
| 1186 | false/*ByCopy*/); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1187 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1188 | bool isNested = NumCapturingClosures > 1; |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1189 | CSI->addThisCapture(isNested, Loc, ThisExpr, ByCopy); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1190 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1191 | return false; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1194 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1195 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 1196 | /// is a non-lvalue expression whose value is the address of the object for |
| 1197 | /// which the function is called. |
| 1198 | |
Douglas Gregor | 09deffa | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 1199 | QualType ThisTy = getCurrentThisType(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1200 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1201 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1202 | CheckCXXThisCapture(Loc); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1203 | return new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1204 | } |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1205 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1206 | bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) { |
| 1207 | // If we're outside the body of a member function, then we'll have a specified |
| 1208 | // type for 'this'. |
| 1209 | if (CXXThisTypeOverride.isNull()) |
| 1210 | return false; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1211 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1212 | // Determine whether we're looking into a class that's currently being |
| 1213 | // defined. |
| 1214 | CXXRecordDecl *Class = BaseType->getAsCXXRecordDecl(); |
| 1215 | return Class && Class->isBeingDefined(); |
| 1216 | } |
| 1217 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1218 | ExprResult |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1219 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1220 | SourceLocation LParenLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1221 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1222 | SourceLocation RParenLoc) { |
Douglas Gregor | 7df89f5 | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 1223 | if (!TypeRep) |
| 1224 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1225 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1226 | TypeSourceInfo *TInfo; |
| 1227 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 1228 | if (!TInfo) |
| 1229 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1230 | |
Richard Smith | b8c414c | 2016-06-30 20:24:30 +0000 | [diff] [blame] | 1231 | auto Result = BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); |
| 1232 | // Avoid creating a non-type-dependent expression that contains typos. |
| 1233 | // Non-type-dependent expressions are liable to be discarded without |
| 1234 | // checking for embedded typos. |
| 1235 | if (!Result.isInvalid() && Result.get()->isInstantiationDependent() && |
| 1236 | !Result.get()->isTypeDependent()) |
| 1237 | Result = CorrectDelayedTyposInExpr(Result.get()); |
| 1238 | return Result; |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 1242 | /// Can be interpreted either as function-style casting ("int(x)") |
| 1243 | /// or class type construction ("ClassType(x,y,z)") |
| 1244 | /// or creation of a value-initialized type ("int()"). |
| 1245 | ExprResult |
| 1246 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
| 1247 | SourceLocation LParenLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1248 | MultiExprArg Exprs, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1249 | SourceLocation RParenLoc) { |
| 1250 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1251 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1252 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1253 | if (Ty->isDependentType() || CallExpr::hasAnyTypeDependentArguments(Exprs)) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1254 | return CXXUnresolvedConstructExpr::Create(Context, TInfo, LParenLoc, Exprs, |
| 1255 | RParenLoc); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1258 | bool ListInitialization = LParenLoc.isInvalid(); |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1259 | assert((!ListInitialization || |
| 1260 | (Exprs.size() == 1 && isa<InitListExpr>(Exprs[0]))) && |
| 1261 | "List initialization must have initializer list as expression."); |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1262 | SourceRange FullRange = SourceRange(TyBeginLoc, |
| 1263 | ListInitialization ? Exprs[0]->getSourceRange().getEnd() : RParenLoc); |
| 1264 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1265 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 1266 | InitializationKind Kind = |
| 1267 | Exprs.size() |
| 1268 | ? ListInitialization |
| 1269 | ? InitializationKind::CreateDirectList(TyBeginLoc) |
| 1270 | : InitializationKind::CreateDirect(TyBeginLoc, LParenLoc, |
| 1271 | RParenLoc) |
| 1272 | : InitializationKind::CreateValue(TyBeginLoc, LParenLoc, RParenLoc); |
| 1273 | |
| 1274 | // C++1z [expr.type.conv]p1: |
| 1275 | // If the type is a placeholder for a deduced class type, [...perform class |
| 1276 | // template argument deduction...] |
| 1277 | DeducedType *Deduced = Ty->getContainedDeducedType(); |
| 1278 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
| 1279 | Ty = DeduceTemplateSpecializationFromInitializer(TInfo, Entity, |
| 1280 | Kind, Exprs); |
| 1281 | if (Ty.isNull()) |
| 1282 | return ExprError(); |
| 1283 | Entity = InitializedEntity::InitializeTemporary(TInfo, Ty); |
| 1284 | } |
| 1285 | |
Douglas Gregor | dd04d33 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 1286 | // C++ [expr.type.conv]p1: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1287 | // If the expression list is a parenthesized single expression, the type |
| 1288 | // conversion expression is equivalent (in definedness, and if defined in |
| 1289 | // meaning) to the corresponding cast expression. |
| 1290 | if (Exprs.size() == 1 && !ListInitialization && |
| 1291 | !isa<InitListExpr>(Exprs[0])) { |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1292 | Expr *Arg = Exprs[0]; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1293 | return BuildCXXFunctionalCastExpr(TInfo, Ty, LParenLoc, Arg, RParenLoc); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1296 | // 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] | 1297 | QualType ElemTy = Ty; |
| 1298 | if (Ty->isArrayType()) { |
| 1299 | if (!ListInitialization) |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1300 | return ExprError(Diag(TyBeginLoc, diag::err_value_init_for_array_type) |
| 1301 | << FullRange); |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1302 | ElemTy = Context.getBaseElementType(Ty); |
| 1303 | } |
| 1304 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1305 | // There doesn't seem to be an explicit rule against this but sanity demands |
| 1306 | // we only construct objects with object types. |
| 1307 | if (Ty->isFunctionType()) |
| 1308 | return ExprError(Diag(TyBeginLoc, diag::err_init_for_function_type) |
| 1309 | << Ty << FullRange); |
David Majnemer | 7eddcff | 2015-09-14 07:05:00 +0000 | [diff] [blame] | 1310 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1311 | // C++17 [expr.type.conv]p2: |
| 1312 | // If the type is cv void and the initializer is (), the expression is a |
| 1313 | // prvalue of the specified type that performs no initialization. |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1314 | if (!Ty->isVoidType() && |
| 1315 | RequireCompleteType(TyBeginLoc, ElemTy, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1316 | diag::err_invalid_incomplete_type_use, FullRange)) |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1317 | return ExprError(); |
| 1318 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1319 | // Otherwise, the expression is a prvalue of the specified type whose |
| 1320 | // result object is direct-initialized (11.6) with the initializer. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1321 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs); |
| 1322 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Exprs); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1323 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1324 | if (Result.isInvalid()) |
Richard Smith | 9006190 | 2013-09-23 02:20:00 +0000 | [diff] [blame] | 1325 | return Result; |
| 1326 | |
| 1327 | Expr *Inner = Result.get(); |
| 1328 | if (CXXBindTemporaryExpr *BTE = dyn_cast_or_null<CXXBindTemporaryExpr>(Inner)) |
| 1329 | Inner = BTE->getSubExpr(); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1330 | if (!isa<CXXTemporaryObjectExpr>(Inner) && |
| 1331 | !isa<CXXScalarValueInitExpr>(Inner)) { |
Richard Smith | 1ae689c | 2015-01-28 22:06:01 +0000 | [diff] [blame] | 1332 | // If we created a CXXTemporaryObjectExpr, that node also represents the |
| 1333 | // functional cast. Otherwise, create an explicit cast to represent |
| 1334 | // the syntactic form of a functional-style cast that was used here. |
| 1335 | // |
| 1336 | // FIXME: Creating a CXXFunctionalCastExpr around a CXXConstructExpr |
| 1337 | // would give a more consistent AST representation than using a |
| 1338 | // CXXTemporaryObjectExpr. It's also weird that the functional cast |
| 1339 | // is sometimes handled by initialization and sometimes not. |
Richard Smith | 9006190 | 2013-09-23 02:20:00 +0000 | [diff] [blame] | 1340 | QualType ResultType = Result.get()->getType(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1341 | Result = CXXFunctionalCastExpr::Create( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1342 | Context, ResultType, Expr::getValueKindForType(Ty), TInfo, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1343 | CK_NoOp, Result.get(), /*Path=*/nullptr, LParenLoc, RParenLoc); |
Sebastian Redl | 2b80af4 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1346 | return Result; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1347 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1348 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1349 | /// \brief Determine whether the given function is a non-placement |
| 1350 | /// deallocation function. |
| 1351 | static bool isNonPlacementDeallocationFunction(Sema &S, FunctionDecl *FD) { |
| 1352 | if (FD->isInvalidDecl()) |
| 1353 | return false; |
| 1354 | |
| 1355 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 1356 | return Method->isUsualDeallocationFunction(); |
| 1357 | |
| 1358 | if (FD->getOverloadedOperator() != OO_Delete && |
| 1359 | FD->getOverloadedOperator() != OO_Array_Delete) |
| 1360 | return false; |
| 1361 | |
| 1362 | unsigned UsualParams = 1; |
| 1363 | |
| 1364 | if (S.getLangOpts().SizedDeallocation && UsualParams < FD->getNumParams() && |
| 1365 | S.Context.hasSameUnqualifiedType( |
| 1366 | FD->getParamDecl(UsualParams)->getType(), |
| 1367 | S.Context.getSizeType())) |
| 1368 | ++UsualParams; |
| 1369 | |
| 1370 | if (S.getLangOpts().AlignedAllocation && UsualParams < FD->getNumParams() && |
| 1371 | S.Context.hasSameUnqualifiedType( |
| 1372 | FD->getParamDecl(UsualParams)->getType(), |
| 1373 | S.Context.getTypeDeclType(S.getStdAlignValT()))) |
| 1374 | ++UsualParams; |
| 1375 | |
| 1376 | return UsualParams == FD->getNumParams(); |
| 1377 | } |
| 1378 | |
| 1379 | namespace { |
| 1380 | struct UsualDeallocFnInfo { |
| 1381 | UsualDeallocFnInfo() : Found(), FD(nullptr) {} |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1382 | UsualDeallocFnInfo(Sema &S, DeclAccessPair Found) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1383 | : Found(Found), FD(dyn_cast<FunctionDecl>(Found->getUnderlyingDecl())), |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1384 | HasSizeT(false), HasAlignValT(false), CUDAPref(Sema::CFP_Native) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1385 | // A function template declaration is never a usual deallocation function. |
| 1386 | if (!FD) |
| 1387 | return; |
| 1388 | if (FD->getNumParams() == 3) |
| 1389 | HasAlignValT = HasSizeT = true; |
| 1390 | else if (FD->getNumParams() == 2) { |
| 1391 | HasSizeT = FD->getParamDecl(1)->getType()->isIntegerType(); |
| 1392 | HasAlignValT = !HasSizeT; |
| 1393 | } |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1394 | |
| 1395 | // In CUDA, determine how much we'd like / dislike to call this. |
| 1396 | if (S.getLangOpts().CUDA) |
| 1397 | if (auto *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 1398 | CUDAPref = S.IdentifyCUDAPreference(Caller, FD); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | operator bool() const { return FD; } |
| 1402 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1403 | bool isBetterThan(const UsualDeallocFnInfo &Other, bool WantSize, |
| 1404 | bool WantAlign) const { |
| 1405 | // C++17 [expr.delete]p10: |
| 1406 | // If the type has new-extended alignment, a function with a parameter |
| 1407 | // of type std::align_val_t is preferred; otherwise a function without |
| 1408 | // such a parameter is preferred |
| 1409 | if (HasAlignValT != Other.HasAlignValT) |
| 1410 | return HasAlignValT == WantAlign; |
| 1411 | |
| 1412 | if (HasSizeT != Other.HasSizeT) |
| 1413 | return HasSizeT == WantSize; |
| 1414 | |
| 1415 | // Use CUDA call preference as a tiebreaker. |
| 1416 | return CUDAPref > Other.CUDAPref; |
| 1417 | } |
| 1418 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1419 | DeclAccessPair Found; |
| 1420 | FunctionDecl *FD; |
| 1421 | bool HasSizeT, HasAlignValT; |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1422 | Sema::CUDAFunctionPreference CUDAPref; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1423 | }; |
| 1424 | } |
| 1425 | |
| 1426 | /// Determine whether a type has new-extended alignment. This may be called when |
| 1427 | /// the type is incomplete (for a delete-expression with an incomplete pointee |
| 1428 | /// type), in which case it will conservatively return false if the alignment is |
| 1429 | /// not known. |
| 1430 | static bool hasNewExtendedAlignment(Sema &S, QualType AllocType) { |
| 1431 | return S.getLangOpts().AlignedAllocation && |
| 1432 | S.getASTContext().getTypeAlignIfKnown(AllocType) > |
| 1433 | S.getASTContext().getTargetInfo().getNewAlign(); |
| 1434 | } |
| 1435 | |
| 1436 | /// Select the correct "usual" deallocation function to use from a selection of |
| 1437 | /// deallocation functions (either global or class-scope). |
| 1438 | static UsualDeallocFnInfo resolveDeallocationOverload( |
| 1439 | Sema &S, LookupResult &R, bool WantSize, bool WantAlign, |
| 1440 | llvm::SmallVectorImpl<UsualDeallocFnInfo> *BestFns = nullptr) { |
| 1441 | UsualDeallocFnInfo Best; |
| 1442 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1443 | for (auto I = R.begin(), E = R.end(); I != E; ++I) { |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1444 | UsualDeallocFnInfo Info(S, I.getPair()); |
| 1445 | if (!Info || !isNonPlacementDeallocationFunction(S, Info.FD) || |
| 1446 | Info.CUDAPref == Sema::CFP_Never) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1447 | continue; |
| 1448 | |
| 1449 | if (!Best) { |
| 1450 | Best = Info; |
| 1451 | if (BestFns) |
| 1452 | BestFns->push_back(Info); |
| 1453 | continue; |
| 1454 | } |
| 1455 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1456 | if (Best.isBetterThan(Info, WantSize, WantAlign)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1457 | continue; |
| 1458 | |
| 1459 | // If more than one preferred function is found, all non-preferred |
| 1460 | // functions are eliminated from further consideration. |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1461 | if (BestFns && Info.isBetterThan(Best, WantSize, WantAlign)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1462 | BestFns->clear(); |
| 1463 | |
| 1464 | Best = Info; |
| 1465 | if (BestFns) |
| 1466 | BestFns->push_back(Info); |
| 1467 | } |
| 1468 | |
| 1469 | return Best; |
| 1470 | } |
| 1471 | |
| 1472 | /// Determine whether a given type is a class for which 'delete[]' would call |
| 1473 | /// a member 'operator delete[]' with a 'size_t' parameter. This implies that |
| 1474 | /// we need to store the array size (even if the type is |
| 1475 | /// trivially-destructible). |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1476 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
| 1477 | QualType allocType) { |
| 1478 | const RecordType *record = |
| 1479 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 1480 | if (!record) return false; |
| 1481 | |
| 1482 | // Try to find an operator delete[] in class scope. |
| 1483 | |
| 1484 | DeclarationName deleteName = |
| 1485 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
| 1486 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
| 1487 | S.LookupQualifiedName(ops, record->getDecl()); |
| 1488 | |
| 1489 | // We're just doing this for information. |
| 1490 | ops.suppressDiagnostics(); |
| 1491 | |
| 1492 | // Very likely: there's no operator delete[]. |
| 1493 | if (ops.empty()) return false; |
| 1494 | |
| 1495 | // If it's ambiguous, it should be illegal to call operator delete[] |
| 1496 | // on this thing, so it doesn't matter if we allocate extra space or not. |
| 1497 | if (ops.isAmbiguous()) return false; |
| 1498 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1499 | // C++17 [expr.delete]p10: |
| 1500 | // If the deallocation functions have class scope, the one without a |
| 1501 | // parameter of type std::size_t is selected. |
| 1502 | auto Best = resolveDeallocationOverload( |
| 1503 | S, ops, /*WantSize*/false, |
| 1504 | /*WantAlign*/hasNewExtendedAlignment(S, allocType)); |
| 1505 | return Best && Best.HasSizeT; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1506 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1507 | |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1508 | /// \brief Parsed a C++ 'new' expression (C++ 5.3.4). |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 1509 | /// |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1510 | /// E.g.: |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1511 | /// @code new (memory) int[size][4] @endcode |
| 1512 | /// or |
| 1513 | /// @code ::new Foo(23, "hello") @endcode |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1514 | /// |
| 1515 | /// \param StartLoc The first location of the expression. |
| 1516 | /// \param UseGlobal True if 'new' was prefixed with '::'. |
| 1517 | /// \param PlacementLParen Opening paren of the placement arguments. |
| 1518 | /// \param PlacementArgs Placement new arguments. |
| 1519 | /// \param PlacementRParen Closing paren of the placement arguments. |
| 1520 | /// \param TypeIdParens If the type is in parens, the source range. |
| 1521 | /// \param D The type to be allocated, as well as array dimensions. |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 1522 | /// \param Initializer The initializing expression or initializer-list, or null |
| 1523 | /// if there is none. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1524 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1525 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1526 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1527 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1528 | Declarator &D, Expr *Initializer) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1529 | Expr *ArraySize = nullptr; |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1530 | // If the specified type is an array, unwrap it and save the expression. |
| 1531 | if (D.getNumTypeObjects() > 0 && |
| 1532 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1533 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
Richard Smith | bfbff07 | 2017-02-10 22:35:37 +0000 | [diff] [blame] | 1534 | if (D.getDeclSpec().hasAutoTypeSpec()) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1535 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
| 1536 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1537 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1538 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 1539 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1540 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1541 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 1542 | << D.getSourceRange()); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1543 | |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1544 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1545 | D.DropFirstTypeObject(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1548 | // Every dimension shall be of constant size. |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1549 | if (ArraySize) { |
| 1550 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1551 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 1552 | break; |
| 1553 | |
| 1554 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 1555 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1556 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent()) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1557 | if (getLangOpts().CPlusPlus14) { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1558 | // C++1y [expr.new]p6: Every constant-expression in a noptr-new-declarator |
| 1559 | // shall be a converted constant expression (5.19) of type std::size_t |
| 1560 | // and shall evaluate to a strictly positive value. |
| 1561 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
| 1562 | assert(IntWidth && "Builtin type of size 0?"); |
| 1563 | llvm::APSInt Value(IntWidth); |
| 1564 | Array.NumElts |
| 1565 | = CheckConvertedConstantExpression(NumElts, Context.getSizeType(), Value, |
| 1566 | CCEK_NewExpr) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1567 | .get(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1568 | } else { |
| 1569 | Array.NumElts |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1570 | = VerifyIntegerConstantExpression(NumElts, nullptr, |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1571 | diag::err_new_array_nonconst) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1572 | .get(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1573 | } |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1574 | if (!Array.NumElts) |
| 1575 | return ExprError(); |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | } |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1580 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1581 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/nullptr); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1582 | QualType AllocType = TInfo->getType(); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1583 | if (D.isInvalidType()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1584 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1585 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1586 | SourceRange DirectInitRange; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1587 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1588 | DirectInitRange = List->getSourceRange(); |
| 1589 | |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1590 | return BuildCXXNew(SourceRange(StartLoc, D.getLocEnd()), UseGlobal, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1591 | PlacementLParen, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1592 | PlacementArgs, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1593 | PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1594 | TypeIdParens, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1596 | TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1597 | ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1598 | DirectInitRange, |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1599 | Initializer); |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1602 | static bool isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style, |
| 1603 | Expr *Init) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1604 | if (!Init) |
| 1605 | return true; |
Sebastian Redl | eb54f08 | 2012-02-17 08:42:32 +0000 | [diff] [blame] | 1606 | if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) |
| 1607 | return PLE->getNumExprs() == 0; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1608 | if (isa<ImplicitValueInitExpr>(Init)) |
| 1609 | return true; |
| 1610 | else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) |
| 1611 | return !CCE->isListInitialization() && |
| 1612 | CCE->getConstructor()->isDefaultConstructor(); |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1613 | else if (Style == CXXNewExpr::ListInit) { |
| 1614 | assert(isa<InitListExpr>(Init) && |
| 1615 | "Shouldn't create list CXXConstructExprs for arrays."); |
| 1616 | return true; |
| 1617 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1618 | return false; |
| 1619 | } |
| 1620 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1621 | ExprResult |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1622 | Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1623 | SourceLocation PlacementLParen, |
| 1624 | MultiExprArg PlacementArgs, |
| 1625 | SourceLocation PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1626 | SourceRange TypeIdParens, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1627 | QualType AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1628 | TypeSourceInfo *AllocTypeInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1629 | Expr *ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1630 | SourceRange DirectInitRange, |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1631 | Expr *Initializer) { |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1632 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1633 | SourceLocation StartLoc = Range.getBegin(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1634 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1635 | CXXNewExpr::InitializationStyle initStyle; |
| 1636 | if (DirectInitRange.isValid()) { |
| 1637 | assert(Initializer && "Have parens but no initializer."); |
| 1638 | initStyle = CXXNewExpr::CallInit; |
| 1639 | } else if (Initializer && isa<InitListExpr>(Initializer)) |
| 1640 | initStyle = CXXNewExpr::ListInit; |
| 1641 | else { |
| 1642 | assert((!Initializer || isa<ImplicitValueInitExpr>(Initializer) || |
| 1643 | isa<CXXConstructExpr>(Initializer)) && |
| 1644 | "Initializer expression that cannot have been implicitly created."); |
| 1645 | initStyle = CXXNewExpr::NoInit; |
| 1646 | } |
| 1647 | |
| 1648 | Expr **Inits = &Initializer; |
| 1649 | unsigned NumInits = Initializer ? 1 : 0; |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 1650 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) { |
| 1651 | assert(initStyle == CXXNewExpr::CallInit && "paren init for non-call init"); |
| 1652 | Inits = List->getExprs(); |
| 1653 | NumInits = List->getNumExprs(); |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1656 | // C++11 [expr.new]p15: |
| 1657 | // A new-expression that creates an object of type T initializes that |
| 1658 | // object as follows: |
| 1659 | InitializationKind Kind |
| 1660 | // - If the new-initializer is omitted, the object is default- |
| 1661 | // initialized (8.5); if no initialization is performed, |
| 1662 | // the object has indeterminate value |
| 1663 | = initStyle == CXXNewExpr::NoInit |
| 1664 | ? InitializationKind::CreateDefault(TypeRange.getBegin()) |
| 1665 | // - Otherwise, the new-initializer is interpreted according to the |
| 1666 | // initialization rules of 8.5 for direct-initialization. |
| 1667 | : initStyle == CXXNewExpr::ListInit |
| 1668 | ? InitializationKind::CreateDirectList(TypeRange.getBegin()) |
| 1669 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
| 1670 | DirectInitRange.getBegin(), |
| 1671 | DirectInitRange.getEnd()); |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1672 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1673 | // C++11 [dcl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 1674 | auto *Deduced = AllocType->getContainedDeducedType(); |
| 1675 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
| 1676 | if (ArraySize) |
| 1677 | return ExprError(Diag(ArraySize->getExprLoc(), |
| 1678 | diag::err_deduced_class_template_compound_type) |
| 1679 | << /*array*/ 2 << ArraySize->getSourceRange()); |
| 1680 | |
| 1681 | InitializedEntity Entity |
| 1682 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
| 1683 | AllocType = DeduceTemplateSpecializationFromInitializer( |
| 1684 | AllocTypeInfo, Entity, Kind, MultiExprArg(Inits, NumInits)); |
| 1685 | if (AllocType.isNull()) |
| 1686 | return ExprError(); |
| 1687 | } else if (Deduced) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1688 | if (initStyle == CXXNewExpr::NoInit || NumInits == 0) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1689 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
| 1690 | << AllocType << TypeRange); |
Richard Smith | 66204ec | 2014-03-12 17:42:45 +0000 | [diff] [blame] | 1691 | if (initStyle == CXXNewExpr::ListInit || |
| 1692 | (NumInits == 1 && isa<InitListExpr>(Inits[0]))) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1693 | return ExprError(Diag(Inits[0]->getLocStart(), |
Richard Smith | 66204ec | 2014-03-12 17:42:45 +0000 | [diff] [blame] | 1694 | diag::err_auto_new_list_init) |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1695 | << AllocType << TypeRange); |
| 1696 | if (NumInits > 1) { |
| 1697 | Expr *FirstBad = Inits[1]; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1698 | return ExprError(Diag(FirstBad->getLocStart(), |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1699 | diag::err_auto_new_ctor_multiple_expressions) |
| 1700 | << AllocType << TypeRange); |
| 1701 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1702 | Expr *Deduce = Inits[0]; |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1703 | QualType DeducedType; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 1704 | if (DeduceAutoType(AllocTypeInfo, Deduce, DeducedType) == DAR_Failed) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1705 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1706 | << AllocType << Deduce->getType() |
| 1707 | << TypeRange << Deduce->getSourceRange()); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1708 | if (DeducedType.isNull()) |
Richard Smith | 9647d3c | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 1709 | return ExprError(); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1710 | AllocType = DeducedType; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1711 | } |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1713 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 1714 | // typedef of an array type. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1715 | if (!ArraySize) { |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1716 | if (const ConstantArrayType *Array |
| 1717 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1718 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 1719 | Context.getSizeType(), |
| 1720 | TypeRange.getEnd()); |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1721 | AllocType = Array->getElementType(); |
| 1722 | } |
| 1723 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1724 | |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1725 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 1726 | return ExprError(); |
| 1727 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1728 | if (initStyle == CXXNewExpr::ListInit && |
| 1729 | isStdInitializerList(AllocType, nullptr)) { |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1730 | Diag(AllocTypeInfo->getTypeLoc().getBeginLoc(), |
| 1731 | diag::warn_dangling_std_initializer_list) |
Sebastian Redl | 73cfbeb | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1732 | << /*at end of FE*/0 << Inits[0]->getSourceRange(); |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1735 | // In ARC, infer 'retaining' for the allocated |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1736 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1737 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 1738 | AllocType->isObjCLifetimeType()) { |
| 1739 | AllocType = Context.getLifetimeQualifiedType(AllocType, |
| 1740 | AllocType->getObjCARCImplicitLifetime()); |
| 1741 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1742 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1743 | QualType ResultType = Context.getPointerType(AllocType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1744 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1745 | if (ArraySize && ArraySize->getType()->isNonOverloadPlaceholderType()) { |
| 1746 | ExprResult result = CheckPlaceholderExpr(ArraySize); |
| 1747 | if (result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1748 | ArraySize = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1749 | } |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 1750 | // C++98 5.3.4p6: "The expression in a direct-new-declarator shall have |
| 1751 | // integral or enumeration type with a non-negative value." |
| 1752 | // C++11 [expr.new]p6: The expression [...] shall be of integral or unscoped |
| 1753 | // enumeration type, or a class type for which a single non-explicit |
| 1754 | // conversion function to integral or unscoped enumeration type exists. |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1755 | // C++1y [expr.new]p6: The expression [...] is implicitly converted to |
Larisse Voufo | bf4aa57 | 2013-06-18 03:08:53 +0000 | [diff] [blame] | 1756 | // std::size_t. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1757 | llvm::Optional<uint64_t> KnownArraySize; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1758 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1759 | ExprResult ConvertedSize; |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1760 | if (getLangOpts().CPlusPlus14) { |
Alp Toker | 965f882 | 2013-11-27 05:22:15 +0000 | [diff] [blame] | 1761 | assert(Context.getTargetInfo().getIntWidth() && "Builtin type of size 0?"); |
| 1762 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1763 | ConvertedSize = PerformImplicitConversion(ArraySize, Context.getSizeType(), |
| 1764 | AA_Converting); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1765 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1766 | if (!ConvertedSize.isInvalid() && |
Larisse Voufo | bf4aa57 | 2013-06-18 03:08:53 +0000 | [diff] [blame] | 1767 | ArraySize->getType()->getAs<RecordType>()) |
Larisse Voufo | 9f380c5 | 2013-06-18 01:27:47 +0000 | [diff] [blame] | 1768 | // Diagnose the compatibility of this conversion. |
| 1769 | Diag(StartLoc, diag::warn_cxx98_compat_array_size_conversion) |
| 1770 | << ArraySize->getType() << 0 << "'size_t'"; |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1771 | } else { |
| 1772 | class SizeConvertDiagnoser : public ICEConvertDiagnoser { |
| 1773 | protected: |
| 1774 | Expr *ArraySize; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1775 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1776 | public: |
| 1777 | SizeConvertDiagnoser(Expr *ArraySize) |
| 1778 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false, false, false), |
| 1779 | ArraySize(ArraySize) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1780 | |
| 1781 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 1782 | QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1783 | return S.Diag(Loc, diag::err_array_size_not_integral) |
| 1784 | << S.getLangOpts().CPlusPlus11 << T; |
| 1785 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1786 | |
| 1787 | SemaDiagnosticBuilder diagnoseIncomplete( |
| 1788 | Sema &S, SourceLocation Loc, QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1789 | return S.Diag(Loc, diag::err_array_size_incomplete_type) |
| 1790 | << T << ArraySize->getSourceRange(); |
| 1791 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1792 | |
| 1793 | SemaDiagnosticBuilder diagnoseExplicitConv( |
| 1794 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1795 | return S.Diag(Loc, diag::err_array_size_explicit_conversion) << T << ConvTy; |
| 1796 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1797 | |
| 1798 | SemaDiagnosticBuilder noteExplicitConv( |
| 1799 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1800 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
| 1801 | << ConvTy->isEnumeralType() << ConvTy; |
| 1802 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1803 | |
| 1804 | SemaDiagnosticBuilder diagnoseAmbiguous( |
| 1805 | Sema &S, SourceLocation Loc, QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1806 | return S.Diag(Loc, diag::err_array_size_ambiguous_conversion) << T; |
| 1807 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1808 | |
| 1809 | SemaDiagnosticBuilder noteAmbiguous( |
| 1810 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1811 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
| 1812 | << ConvTy->isEnumeralType() << ConvTy; |
| 1813 | } |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1814 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1815 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
| 1816 | QualType T, |
| 1817 | QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1818 | return S.Diag(Loc, |
| 1819 | S.getLangOpts().CPlusPlus11 |
| 1820 | ? diag::warn_cxx98_compat_array_size_conversion |
| 1821 | : diag::ext_array_size_conversion) |
| 1822 | << T << ConvTy->isEnumeralType() << ConvTy; |
| 1823 | } |
| 1824 | } SizeDiagnoser(ArraySize); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1825 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1826 | ConvertedSize = PerformContextualImplicitConversion(StartLoc, ArraySize, |
| 1827 | SizeDiagnoser); |
| 1828 | } |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1829 | if (ConvertedSize.isInvalid()) |
| 1830 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1831 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1832 | ArraySize = ConvertedSize.get(); |
John McCall | 9b80c21 | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 1833 | QualType SizeType = ArraySize->getType(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1834 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1835 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1836 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1837 | |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1838 | // C++98 [expr.new]p7: |
| 1839 | // The expression in a direct-new-declarator shall have integral type |
| 1840 | // with a non-negative value. |
| 1841 | // |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1842 | // Let's see if this is a constant < 0. If so, we reject it out of hand, |
| 1843 | // per CWG1464. Otherwise, if it's not a constant, we must have an |
| 1844 | // unparenthesized array type. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1845 | if (!ArraySize->isValueDependent()) { |
| 1846 | llvm::APSInt Value; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1847 | // We've already performed any required implicit conversion to integer or |
| 1848 | // unscoped enumeration type. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1849 | // FIXME: Per CWG1464, we are required to check the value prior to |
| 1850 | // converting to size_t. This will never find a negative array size in |
| 1851 | // C++14 onwards, because Value is always unsigned here! |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1852 | if (ArraySize->isIntegerConstantExpr(Value, Context)) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1853 | if (Value.isSigned() && Value.isNegative()) { |
| 1854 | return ExprError(Diag(ArraySize->getLocStart(), |
| 1855 | diag::err_typecheck_negative_array_size) |
| 1856 | << ArraySize->getSourceRange()); |
| 1857 | } |
| 1858 | |
| 1859 | if (!AllocType->isDependentType()) { |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1860 | unsigned ActiveSizeBits = |
| 1861 | ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1862 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) |
| 1863 | return ExprError(Diag(ArraySize->getLocStart(), |
| 1864 | diag::err_array_too_large) |
| 1865 | << Value.toString(10) |
| 1866 | << ArraySize->getSourceRange()); |
Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1867 | } |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1868 | |
| 1869 | KnownArraySize = Value.getZExtValue(); |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1870 | } else if (TypeIdParens.isValid()) { |
| 1871 | // Can't have dynamic array size when the type-id is in parentheses. |
| 1872 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 1873 | << ArraySize->getSourceRange() |
| 1874 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 1875 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1876 | |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1877 | TypeIdParens = SourceRange(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1878 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1879 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1880 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 1881 | // Note that we do *not* convert the argument in any way. It can |
| 1882 | // be signed, larger than size_t, whatever. |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1883 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1884 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1885 | FunctionDecl *OperatorNew = nullptr; |
| 1886 | FunctionDecl *OperatorDelete = nullptr; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1887 | unsigned Alignment = |
| 1888 | AllocType->isDependentType() ? 0 : Context.getTypeAlign(AllocType); |
| 1889 | unsigned NewAlignment = Context.getTargetInfo().getNewAlign(); |
| 1890 | bool PassAlignment = getLangOpts().AlignedAllocation && |
| 1891 | Alignment > NewAlignment; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1892 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1893 | if (!AllocType->isDependentType() && |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 1894 | !Expr::hasAnyTypeDependentArguments(PlacementArgs) && |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1895 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1896 | SourceRange(PlacementLParen, PlacementRParen), |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1897 | UseGlobal, AllocType, ArraySize, PassAlignment, |
| 1898 | PlacementArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1899 | return ExprError(); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1900 | |
| 1901 | // If this is an array allocation, compute whether the usual array |
| 1902 | // deallocation function for the type has a size_t parameter. |
| 1903 | bool UsualArrayDeleteWantsSize = false; |
| 1904 | if (ArraySize && !AllocType->isDependentType()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1905 | UsualArrayDeleteWantsSize = |
| 1906 | doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1907 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1908 | SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 1eab66c | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1909 | if (OperatorNew) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1910 | const FunctionProtoType *Proto = |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 1911 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
| 1912 | VariadicCallType CallType = Proto->isVariadic() ? VariadicFunction |
| 1913 | : VariadicDoesNotApply; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1914 | |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 1915 | // We've already converted the placement args, just fill in any default |
| 1916 | // arguments. Skip the first parameter because we don't have a corresponding |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1917 | // argument. Skip the second parameter too if we're passing in the |
| 1918 | // alignment; we've already filled it in. |
| 1919 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, Proto, |
| 1920 | PassAlignment ? 2 : 1, PlacementArgs, |
| 1921 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1922 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1923 | |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 1924 | if (!AllPlaceArgs.empty()) |
| 1925 | PlacementArgs = AllPlaceArgs; |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 1926 | |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 1927 | // FIXME: This is wrong: PlacementArgs misses out the first (size) argument. |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 1928 | DiagnoseSentinelCalls(OperatorNew, PlacementLParen, PlacementArgs); |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 1929 | |
| 1930 | // FIXME: Missing call to CheckFunctionCall or equivalent |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1931 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1932 | // Warn if the type is over-aligned and is being allocated by (unaligned) |
| 1933 | // global operator new. |
| 1934 | if (PlacementArgs.empty() && !PassAlignment && |
| 1935 | (OperatorNew->isImplicit() || |
| 1936 | (OperatorNew->getLocStart().isValid() && |
| 1937 | getSourceManager().isInSystemHeader(OperatorNew->getLocStart())))) { |
| 1938 | if (Alignment > NewAlignment) |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 1939 | Diag(StartLoc, diag::warn_overaligned_type) |
| 1940 | << AllocType |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1941 | << unsigned(Alignment / Context.getCharWidth()) |
| 1942 | << unsigned(NewAlignment / Context.getCharWidth()); |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 1943 | } |
| 1944 | } |
| 1945 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1946 | // Array 'new' can't have any initializers except empty parentheses. |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1947 | // Initializer lists are also allowed, in C++11. Rely on the parser for the |
| 1948 | // dialect distinction. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1949 | if (ArraySize && !isLegalArrayNewInitializer(initStyle, Initializer)) { |
| 1950 | SourceRange InitRange(Inits[0]->getLocStart(), |
| 1951 | Inits[NumInits - 1]->getLocEnd()); |
| 1952 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 1953 | return ExprError(); |
Anders Carlsson | c6bb0e1 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 1956 | // If we can perform the initialization, and we've not already done so, |
| 1957 | // do it now. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1958 | if (!AllocType->isDependentType() && |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1959 | !Expr::hasAnyTypeDependentArguments( |
Richard Smith | c6abd96 | 2014-07-25 01:12:44 +0000 | [diff] [blame] | 1960 | llvm::makeArrayRef(Inits, NumInits))) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1961 | // The type we initialize is the complete type, including the array bound. |
| 1962 | QualType InitType; |
| 1963 | if (KnownArraySize) |
| 1964 | InitType = Context.getConstantArrayType( |
| 1965 | AllocType, llvm::APInt(Context.getTypeSize(Context.getSizeType()), |
| 1966 | *KnownArraySize), |
| 1967 | ArrayType::Normal, 0); |
| 1968 | else if (ArraySize) |
| 1969 | InitType = |
| 1970 | Context.getIncompleteArrayType(AllocType, ArrayType::Normal, 0); |
| 1971 | else |
| 1972 | InitType = AllocType; |
| 1973 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1974 | InitializedEntity Entity |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1975 | = InitializedEntity::InitializeNew(StartLoc, InitType); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1976 | InitializationSequence InitSeq(*this, Entity, Kind, |
| 1977 | MultiExprArg(Inits, NumInits)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1978 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1979 | MultiExprArg(Inits, NumInits)); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1980 | if (FullInit.isInvalid()) |
| 1981 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1982 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1983 | // FullInit is our initializer; strip off CXXBindTemporaryExprs, because |
| 1984 | // we don't want the initialized object to be destructed. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1985 | // FIXME: We should not create these in the first place. |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1986 | if (CXXBindTemporaryExpr *Binder = |
| 1987 | dyn_cast_or_null<CXXBindTemporaryExpr>(FullInit.get())) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1988 | FullInit = Binder->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1989 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1990 | Initializer = FullInit.get(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1991 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1992 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1993 | // Mark the new and delete operators as referenced. |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 1994 | if (OperatorNew) { |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 1995 | if (DiagnoseUseOfDecl(OperatorNew, StartLoc)) |
| 1996 | return ExprError(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1997 | MarkFunctionReferenced(StartLoc, OperatorNew); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 1998 | } |
| 1999 | if (OperatorDelete) { |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2000 | if (DiagnoseUseOfDecl(OperatorDelete, StartLoc)) |
| 2001 | return ExprError(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2002 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2003 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2004 | |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 2005 | // C++0x [expr.new]p17: |
| 2006 | // If the new expression creates an array of objects of class type, |
| 2007 | // access and ambiguity control are done for the destructor. |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2008 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
| 2009 | if (ArraySize && !BaseAllocType->isDependentType()) { |
| 2010 | if (const RecordType *BaseRecordType = BaseAllocType->getAs<RecordType>()) { |
| 2011 | if (CXXDestructorDecl *dtor = LookupDestructor( |
| 2012 | cast<CXXRecordDecl>(BaseRecordType->getDecl()))) { |
| 2013 | MarkFunctionReferenced(StartLoc, dtor); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2014 | CheckDestructorAccess(StartLoc, dtor, |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2015 | PDiag(diag::err_access_dtor) |
| 2016 | << BaseAllocType); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2017 | if (DiagnoseUseOfDecl(dtor, StartLoc)) |
| 2018 | return ExprError(); |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2019 | } |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 2020 | } |
| 2021 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2022 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2023 | return new (Context) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2024 | CXXNewExpr(Context, UseGlobal, OperatorNew, OperatorDelete, PassAlignment, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2025 | UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens, |
| 2026 | ArraySize, initStyle, Initializer, ResultType, AllocTypeInfo, |
| 2027 | Range, DirectInitRange); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2030 | /// \brief Checks that a type is suitable as the allocated type |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2031 | /// in a new-expression. |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2032 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | SourceRange R) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2034 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 2035 | // abstract class type or array thereof. |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2036 | if (AllocType->isFunctionType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2037 | return Diag(Loc, diag::err_bad_new_type) |
| 2038 | << AllocType << 0 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2039 | else if (AllocType->isReferenceType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2040 | return Diag(Loc, diag::err_bad_new_type) |
| 2041 | << AllocType << 1 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2042 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2043 | RequireCompleteType(Loc, AllocType, diag::err_new_incomplete_type,R)) |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2044 | return true; |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2045 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2046 | diag::err_allocation_of_abstract_type)) |
| 2047 | return true; |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 2048 | else if (AllocType->isVariablyModifiedType()) |
| 2049 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 2050 | << AllocType; |
Douglas Gregor | 39d1a09 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 2051 | else if (unsigned AddressSpace = AllocType.getAddressSpace()) |
| 2052 | return Diag(Loc, diag::err_address_space_qualified_new) |
| 2053 | << AllocType.getUnqualifiedType() << AddressSpace; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2054 | else if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2055 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { |
| 2056 | QualType BaseAllocType = Context.getBaseElementType(AT); |
| 2057 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 2058 | BaseAllocType->isObjCLifetimeType()) |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 2059 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2060 | << BaseAllocType; |
| 2061 | } |
| 2062 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2063 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2064 | return false; |
| 2065 | } |
| 2066 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2067 | static bool |
| 2068 | resolveAllocationOverload(Sema &S, LookupResult &R, SourceRange Range, |
| 2069 | SmallVectorImpl<Expr *> &Args, bool &PassAlignment, |
| 2070 | FunctionDecl *&Operator, |
| 2071 | OverloadCandidateSet *AlignedCandidates = nullptr, |
| 2072 | Expr *AlignArg = nullptr) { |
| 2073 | OverloadCandidateSet Candidates(R.getNameLoc(), |
| 2074 | OverloadCandidateSet::CSK_Normal); |
| 2075 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 2076 | Alloc != AllocEnd; ++Alloc) { |
| 2077 | // Even member operator new/delete are implicitly treated as |
| 2078 | // static, so don't use AddMemberCandidate. |
| 2079 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
| 2080 | |
| 2081 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 2082 | S.AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
| 2083 | /*ExplicitTemplateArgs=*/nullptr, Args, |
| 2084 | Candidates, |
| 2085 | /*SuppressUserConversions=*/false); |
| 2086 | continue; |
| 2087 | } |
| 2088 | |
| 2089 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 2090 | S.AddOverloadCandidate(Fn, Alloc.getPair(), Args, Candidates, |
| 2091 | /*SuppressUserConversions=*/false); |
| 2092 | } |
| 2093 | |
| 2094 | // Do the resolution. |
| 2095 | OverloadCandidateSet::iterator Best; |
| 2096 | switch (Candidates.BestViableFunction(S, R.getNameLoc(), Best)) { |
| 2097 | case OR_Success: { |
| 2098 | // Got one! |
| 2099 | FunctionDecl *FnDecl = Best->Function; |
| 2100 | if (S.CheckAllocationAccess(R.getNameLoc(), Range, R.getNamingClass(), |
| 2101 | Best->FoundDecl) == Sema::AR_inaccessible) |
| 2102 | return true; |
| 2103 | |
| 2104 | Operator = FnDecl; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2105 | return false; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2106 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2107 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2108 | case OR_No_Viable_Function: |
| 2109 | // C++17 [expr.new]p13: |
| 2110 | // If no matching function is found and the allocated object type has |
| 2111 | // new-extended alignment, the alignment argument is removed from the |
| 2112 | // argument list, and overload resolution is performed again. |
| 2113 | if (PassAlignment) { |
| 2114 | PassAlignment = false; |
| 2115 | AlignArg = Args[1]; |
| 2116 | Args.erase(Args.begin() + 1); |
| 2117 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
| 2118 | Operator, &Candidates, AlignArg); |
| 2119 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2120 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2121 | // MSVC will fall back on trying to find a matching global operator new |
| 2122 | // if operator new[] cannot be found. Also, MSVC will leak by not |
| 2123 | // generating a call to operator delete or operator delete[], but we |
| 2124 | // will not replicate that bug. |
| 2125 | // FIXME: Find out how this interacts with the std::align_val_t fallback |
| 2126 | // once MSVC implements it. |
| 2127 | if (R.getLookupName().getCXXOverloadedOperator() == OO_Array_New && |
| 2128 | S.Context.getLangOpts().MSVCCompat) { |
| 2129 | R.clear(); |
| 2130 | R.setLookupName(S.Context.DeclarationNames.getCXXOperatorName(OO_New)); |
| 2131 | S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); |
| 2132 | // FIXME: This will give bad diagnostics pointing at the wrong functions. |
| 2133 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
| 2134 | Operator, nullptr); |
| 2135 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2136 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2137 | S.Diag(R.getNameLoc(), diag::err_ovl_no_viable_function_in_call) |
| 2138 | << R.getLookupName() << Range; |
| 2139 | |
| 2140 | // If we have aligned candidates, only note the align_val_t candidates |
| 2141 | // from AlignedCandidates and the non-align_val_t candidates from |
| 2142 | // Candidates. |
| 2143 | if (AlignedCandidates) { |
| 2144 | auto IsAligned = [](OverloadCandidate &C) { |
| 2145 | return C.Function->getNumParams() > 1 && |
| 2146 | C.Function->getParamDecl(1)->getType()->isAlignValT(); |
| 2147 | }; |
| 2148 | auto IsUnaligned = [&](OverloadCandidate &C) { return !IsAligned(C); }; |
| 2149 | |
| 2150 | // This was an overaligned allocation, so list the aligned candidates |
| 2151 | // first. |
| 2152 | Args.insert(Args.begin() + 1, AlignArg); |
| 2153 | AlignedCandidates->NoteCandidates(S, OCD_AllCandidates, Args, "", |
| 2154 | R.getNameLoc(), IsAligned); |
| 2155 | Args.erase(Args.begin() + 1); |
| 2156 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args, "", R.getNameLoc(), |
| 2157 | IsUnaligned); |
| 2158 | } else { |
| 2159 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 2160 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2161 | return true; |
| 2162 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2163 | case OR_Ambiguous: |
| 2164 | S.Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) |
| 2165 | << R.getLookupName() << Range; |
| 2166 | Candidates.NoteCandidates(S, OCD_ViableCandidates, Args); |
| 2167 | return true; |
| 2168 | |
| 2169 | case OR_Deleted: { |
| 2170 | S.Diag(R.getNameLoc(), diag::err_ovl_deleted_call) |
| 2171 | << Best->Function->isDeleted() |
| 2172 | << R.getLookupName() |
| 2173 | << S.getDeletedOrUnavailableSuffix(Best->Function) |
| 2174 | << Range; |
| 2175 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 2176 | return true; |
| 2177 | } |
| 2178 | } |
| 2179 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2182 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2183 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 2184 | /// that are appropriate for the allocation. |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 2185 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 2186 | bool UseGlobal, QualType AllocType, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2187 | bool IsArray, bool &PassAlignment, |
| 2188 | MultiExprArg PlaceArgs, |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2189 | FunctionDecl *&OperatorNew, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2190 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2191 | // --- Choosing an allocation function --- |
| 2192 | // C++ 5.3.4p8 - 14 & 18 |
| 2193 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 2194 | // in the scope of the allocated class. |
| 2195 | // 2) If an array size is given, look for operator new[], else look for |
| 2196 | // operator new. |
| 2197 | // 3) The first argument is always size_t. Append the arguments from the |
| 2198 | // placement form. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2199 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2200 | SmallVector<Expr*, 8> AllocArgs; |
| 2201 | AllocArgs.reserve((PassAlignment ? 2 : 1) + PlaceArgs.size()); |
| 2202 | |
| 2203 | // We don't care about the actual value of these arguments. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2204 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 2205 | // tree? Or should the consumer just recalculate the value? |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2206 | // FIXME: Using a dummy value will interact poorly with attribute enable_if. |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2207 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2208 | Context.getTargetInfo().getPointerWidth(0)), |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2209 | Context.getSizeType(), |
| 2210 | SourceLocation()); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2211 | AllocArgs.push_back(&Size); |
| 2212 | |
| 2213 | QualType AlignValT = Context.VoidTy; |
| 2214 | if (PassAlignment) { |
| 2215 | DeclareGlobalNewDelete(); |
| 2216 | AlignValT = Context.getTypeDeclType(getStdAlignValT()); |
| 2217 | } |
| 2218 | CXXScalarValueInitExpr Align(AlignValT, nullptr, SourceLocation()); |
| 2219 | if (PassAlignment) |
| 2220 | AllocArgs.push_back(&Align); |
| 2221 | |
| 2222 | AllocArgs.insert(AllocArgs.end(), PlaceArgs.begin(), PlaceArgs.end()); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2224 | // C++ [expr.new]p8: |
| 2225 | // If the allocated type is a non-array type, the allocation |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2226 | // function's name is operator new and the deallocation function's |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2227 | // name is operator delete. If the allocated type is an array |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2228 | // type, the allocation function's name is operator new[] and the |
| 2229 | // deallocation function's name is operator delete[]. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2230 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2231 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2232 | |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2233 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 2234 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2235 | // Find the allocation function. |
| 2236 | { |
| 2237 | LookupResult R(*this, NewName, StartLoc, LookupOrdinaryName); |
| 2238 | |
| 2239 | // C++1z [expr.new]p9: |
| 2240 | // If the new-expression begins with a unary :: operator, the allocation |
| 2241 | // function's name is looked up in the global scope. Otherwise, if the |
| 2242 | // allocated type is a class type T or array thereof, the allocation |
| 2243 | // function's name is looked up in the scope of T. |
| 2244 | if (AllocElemType->isRecordType() && !UseGlobal) |
| 2245 | LookupQualifiedName(R, AllocElemType->getAsCXXRecordDecl()); |
| 2246 | |
| 2247 | // We can see ambiguity here if the allocation function is found in |
| 2248 | // multiple base classes. |
| 2249 | if (R.isAmbiguous()) |
| 2250 | return true; |
| 2251 | |
| 2252 | // If this lookup fails to find the name, or if the allocated type is not |
| 2253 | // a class type, the allocation function's name is looked up in the |
| 2254 | // global scope. |
| 2255 | if (R.empty()) |
| 2256 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 2257 | |
| 2258 | assert(!R.empty() && "implicitly declared allocation functions not found"); |
| 2259 | assert(!R.isAmbiguous() && "global allocation functions are ambiguous"); |
| 2260 | |
| 2261 | // We do our own custom access checks below. |
| 2262 | R.suppressDiagnostics(); |
| 2263 | |
| 2264 | if (resolveAllocationOverload(*this, R, Range, AllocArgs, PassAlignment, |
| 2265 | OperatorNew)) |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2266 | return true; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2267 | } |
Aaron Ballman | 324fbee | 2013-05-30 01:55:39 +0000 | [diff] [blame] | 2268 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2269 | // 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] | 2270 | if (!getLangOpts().Exceptions) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2271 | OperatorDelete = nullptr; |
John McCall | 0f55a03 | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 2272 | return false; |
| 2273 | } |
| 2274 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2275 | // Note, the name of OperatorNew might have been changed from array to |
| 2276 | // non-array by resolveAllocationOverload. |
| 2277 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 2278 | OperatorNew->getDeclName().getCXXOverloadedOperator() == OO_Array_New |
| 2279 | ? OO_Array_Delete |
| 2280 | : OO_Delete); |
| 2281 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2282 | // C++ [expr.new]p19: |
| 2283 | // |
| 2284 | // If the new-expression begins with a unary :: operator, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2285 | // deallocation function's name is looked up in the global |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2286 | // 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] | 2287 | // array thereof, the deallocation function's name is looked up in |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2288 | // the scope of T. If this lookup fails to find the name, or if |
| 2289 | // the allocated type is not a class type or array thereof, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2290 | // deallocation function's name is looked up in the global scope. |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2291 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2292 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2293 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2294 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2295 | LookupQualifiedName(FoundDelete, RD); |
| 2296 | } |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 2297 | if (FoundDelete.isAmbiguous()) |
| 2298 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2299 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2300 | bool FoundGlobalDelete = FoundDelete.empty(); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2301 | if (FoundDelete.empty()) { |
| 2302 | DeclareGlobalNewDelete(); |
| 2303 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 2304 | } |
| 2305 | |
| 2306 | FoundDelete.suppressDiagnostics(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2307 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2308 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2309 | |
John McCall | d3be2c8 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 2310 | // Whether we're looking for a placement operator delete is dictated |
| 2311 | // by whether we selected a placement operator new, not by whether |
| 2312 | // we had explicit placement arguments. This matters for things like |
| 2313 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 2314 | // A *a = new A() |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2315 | // |
| 2316 | // We don't have any definition for what a "placement allocation function" |
| 2317 | // is, but we assume it's any allocation function whose |
| 2318 | // parameter-declaration-clause is anything other than (size_t). |
| 2319 | // |
| 2320 | // FIXME: Should (size_t, std::align_val_t) also be considered non-placement? |
| 2321 | // This affects whether an exception from the constructor of an overaligned |
| 2322 | // type uses the sized or non-sized form of aligned operator delete. |
| 2323 | bool isPlacementNew = !PlaceArgs.empty() || OperatorNew->param_size() != 1 || |
| 2324 | OperatorNew->isVariadic(); |
John McCall | d3be2c8 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 2325 | |
| 2326 | if (isPlacementNew) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2327 | // C++ [expr.new]p20: |
| 2328 | // A declaration of a placement deallocation function matches the |
| 2329 | // declaration of a placement allocation function if it has the |
| 2330 | // same number of parameters and, after parameter transformations |
| 2331 | // (8.3.5), all parameter types except the first are |
| 2332 | // identical. [...] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2333 | // |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2334 | // To perform this comparison, we compute the function type that |
| 2335 | // the deallocation function should have, and use that type both |
| 2336 | // for template argument deduction and for comparison purposes. |
| 2337 | QualType ExpectedFunctionType; |
| 2338 | { |
| 2339 | const FunctionProtoType *Proto |
| 2340 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2341 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2342 | SmallVector<QualType, 4> ArgTypes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2343 | ArgTypes.push_back(Context.VoidPtrTy); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2344 | for (unsigned I = 1, N = Proto->getNumParams(); I < N; ++I) |
| 2345 | ArgTypes.push_back(Proto->getParamType(I)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2346 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2347 | FunctionProtoType::ExtProtoInfo EPI; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2348 | // FIXME: This is not part of the standard's rule. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2349 | EPI.Variadic = Proto->isVariadic(); |
| 2350 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2351 | ExpectedFunctionType |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2352 | = Context.getFunctionType(Context.VoidTy, ArgTypes, EPI); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2355 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2356 | DEnd = FoundDelete.end(); |
| 2357 | D != DEnd; ++D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2358 | FunctionDecl *Fn = nullptr; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 2359 | if (FunctionTemplateDecl *FnTmpl = |
| 2360 | dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2361 | // Perform template argument deduction to try to match the |
| 2362 | // expected function type. |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 2363 | TemplateDeductionInfo Info(StartLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2364 | if (DeduceTemplateArguments(FnTmpl, nullptr, ExpectedFunctionType, Fn, |
| 2365 | Info)) |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2366 | continue; |
| 2367 | } else |
| 2368 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 2369 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 2370 | if (Context.hasSameType(adjustCCAndNoReturn(Fn->getType(), |
| 2371 | ExpectedFunctionType, |
| 2372 | /*AdjustExcpetionSpec*/true), |
| 2373 | ExpectedFunctionType)) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2374 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2375 | } |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 2376 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2377 | if (getLangOpts().CUDA) |
| 2378 | EraseUnwantedCUDAMatches(dyn_cast<FunctionDecl>(CurContext), Matches); |
| 2379 | } else { |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2380 | // C++1y [expr.new]p22: |
| 2381 | // For a non-placement allocation function, the normal deallocation |
| 2382 | // function lookup is used |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2383 | // |
| 2384 | // Per [expr.delete]p10, this lookup prefers a member operator delete |
| 2385 | // without a size_t argument, but prefers a non-member operator delete |
| 2386 | // with a size_t where possible (which it always is in this case). |
| 2387 | llvm::SmallVector<UsualDeallocFnInfo, 4> BestDeallocFns; |
| 2388 | UsualDeallocFnInfo Selected = resolveDeallocationOverload( |
| 2389 | *this, FoundDelete, /*WantSize*/ FoundGlobalDelete, |
| 2390 | /*WantAlign*/ hasNewExtendedAlignment(*this, AllocElemType), |
| 2391 | &BestDeallocFns); |
| 2392 | if (Selected) |
| 2393 | Matches.push_back(std::make_pair(Selected.Found, Selected.FD)); |
| 2394 | else { |
| 2395 | // If we failed to select an operator, all remaining functions are viable |
| 2396 | // but ambiguous. |
| 2397 | for (auto Fn : BestDeallocFns) |
| 2398 | Matches.push_back(std::make_pair(Fn.Found, Fn.FD)); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2399 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | // C++ [expr.new]p20: |
| 2403 | // [...] If the lookup finds a single matching deallocation |
| 2404 | // function, that function will be called; otherwise, no |
| 2405 | // deallocation function will be called. |
| 2406 | if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2407 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2408 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2409 | // C++1z [expr.new]p23: |
| 2410 | // If the lookup finds a usual deallocation function (3.7.4.2) |
| 2411 | // with a parameter of type std::size_t and that function, considered |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2412 | // as a placement deallocation function, would have been |
| 2413 | // selected as a match for the allocation function, the program |
| 2414 | // is ill-formed. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2415 | if (getLangOpts().CPlusPlus11 && isPlacementNew && |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2416 | isNonPlacementDeallocationFunction(*this, OperatorDelete)) { |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 2417 | UsualDeallocFnInfo Info(*this, |
| 2418 | DeclAccessPair::make(OperatorDelete, AS_public)); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2419 | // Core issue, per mail to core reflector, 2016-10-09: |
| 2420 | // If this is a member operator delete, and there is a corresponding |
| 2421 | // non-sized member operator delete, this isn't /really/ a sized |
| 2422 | // deallocation function, it just happens to have a size_t parameter. |
| 2423 | bool IsSizedDelete = Info.HasSizeT; |
| 2424 | if (IsSizedDelete && !FoundGlobalDelete) { |
| 2425 | auto NonSizedDelete = |
| 2426 | resolveDeallocationOverload(*this, FoundDelete, /*WantSize*/false, |
| 2427 | /*WantAlign*/Info.HasAlignValT); |
| 2428 | if (NonSizedDelete && !NonSizedDelete.HasSizeT && |
| 2429 | NonSizedDelete.HasAlignValT == Info.HasAlignValT) |
| 2430 | IsSizedDelete = false; |
| 2431 | } |
| 2432 | |
| 2433 | if (IsSizedDelete) { |
| 2434 | SourceRange R = PlaceArgs.empty() |
| 2435 | ? SourceRange() |
| 2436 | : SourceRange(PlaceArgs.front()->getLocStart(), |
| 2437 | PlaceArgs.back()->getLocEnd()); |
| 2438 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) << R; |
| 2439 | if (!OperatorDelete->isImplicit()) |
| 2440 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 2441 | << DeleteName; |
| 2442 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2443 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2444 | |
| 2445 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
| 2446 | Matches[0].first); |
| 2447 | } else if (!Matches.empty()) { |
| 2448 | // We found multiple suitable operators. Per [expr.new]p20, that means we |
| 2449 | // call no 'operator delete' function, but we should at least warn the user. |
| 2450 | // FIXME: Suppress this warning if the construction cannot throw. |
| 2451 | Diag(StartLoc, diag::warn_ambiguous_suitable_delete_function_found) |
| 2452 | << DeleteName << AllocElemType; |
| 2453 | |
| 2454 | for (auto &Match : Matches) |
| 2455 | Diag(Match.second->getLocation(), |
| 2456 | diag::note_member_declared_here) << DeleteName; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2459 | return false; |
| 2460 | } |
| 2461 | |
| 2462 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 2463 | /// delete. These are: |
| 2464 | /// @code |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2465 | /// // C++03: |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2466 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 2467 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 2468 | /// void operator delete(void *) throw(); |
| 2469 | /// void operator delete[](void *) throw(); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2470 | /// // C++11: |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2471 | /// void* operator new(std::size_t); |
| 2472 | /// void* operator new[](std::size_t); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2473 | /// void operator delete(void *) noexcept; |
| 2474 | /// void operator delete[](void *) noexcept; |
| 2475 | /// // C++1y: |
| 2476 | /// void* operator new(std::size_t); |
| 2477 | /// void* operator new[](std::size_t); |
| 2478 | /// void operator delete(void *) noexcept; |
| 2479 | /// void operator delete[](void *) noexcept; |
| 2480 | /// void operator delete(void *, std::size_t) noexcept; |
| 2481 | /// void operator delete[](void *, std::size_t) noexcept; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2482 | /// @endcode |
| 2483 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 2484 | /// declared. Their use requires including \<new\>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2485 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2486 | if (GlobalNewDeleteDeclared) |
| 2487 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2488 | |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2489 | // C++ [basic.std.dynamic]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2490 | // [...] The following allocation and deallocation functions (18.4) are |
| 2491 | // implicitly declared in global scope in each translation unit of a |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2492 | // program |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2493 | // |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2494 | // C++03: |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2495 | // void* operator new(std::size_t) throw(std::bad_alloc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2496 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 2497 | // void operator delete(void*) throw(); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2498 | // void operator delete[](void*) throw(); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2499 | // C++11: |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2500 | // void* operator new(std::size_t); |
| 2501 | // void* operator new[](std::size_t); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2502 | // void operator delete(void*) noexcept; |
| 2503 | // void operator delete[](void*) noexcept; |
| 2504 | // C++1y: |
| 2505 | // void* operator new(std::size_t); |
| 2506 | // void* operator new[](std::size_t); |
| 2507 | // void operator delete(void*) noexcept; |
| 2508 | // void operator delete[](void*) noexcept; |
| 2509 | // void operator delete(void*, std::size_t) noexcept; |
| 2510 | // void operator delete[](void*, std::size_t) noexcept; |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2511 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2512 | // These implicit declarations introduce only the function names operator |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2513 | // new, operator new[], operator delete, operator delete[]. |
| 2514 | // |
| 2515 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 2516 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 2517 | // However, we do not make these implicit declarations visible to name |
| 2518 | // lookup. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2519 | if (!StdBadAlloc && !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2520 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 2521 | // implicitly. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2522 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
| 2523 | getOrCreateStdNamespace(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2524 | SourceLocation(), SourceLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2525 | &PP.getIdentifierTable().get("bad_alloc"), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2526 | nullptr); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 2527 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2528 | } |
Richard Smith | 5913902 | 2016-09-30 22:41:36 +0000 | [diff] [blame] | 2529 | if (!StdAlignValT && getLangOpts().AlignedAllocation) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2530 | // The "std::align_val_t" enum class has not yet been declared, so build it |
| 2531 | // implicitly. |
| 2532 | auto *AlignValT = EnumDecl::Create( |
| 2533 | Context, getOrCreateStdNamespace(), SourceLocation(), SourceLocation(), |
| 2534 | &PP.getIdentifierTable().get("align_val_t"), nullptr, true, true, true); |
| 2535 | AlignValT->setIntegerType(Context.getSizeType()); |
| 2536 | AlignValT->setPromotionType(Context.getSizeType()); |
| 2537 | AlignValT->setImplicit(true); |
| 2538 | StdAlignValT = AlignValT; |
| 2539 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2540 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2541 | GlobalNewDeleteDeclared = true; |
| 2542 | |
| 2543 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 2544 | QualType SizeT = Context.getSizeType(); |
| 2545 | |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2546 | auto DeclareGlobalAllocationFunctions = [&](OverloadedOperatorKind Kind, |
| 2547 | QualType Return, QualType Param) { |
| 2548 | llvm::SmallVector<QualType, 3> Params; |
| 2549 | Params.push_back(Param); |
| 2550 | |
| 2551 | // Create up to four variants of the function (sized/aligned). |
| 2552 | bool HasSizedVariant = getLangOpts().SizedDeallocation && |
| 2553 | (Kind == OO_Delete || Kind == OO_Array_Delete); |
Richard Smith | 5913902 | 2016-09-30 22:41:36 +0000 | [diff] [blame] | 2554 | bool HasAlignedVariant = getLangOpts().AlignedAllocation; |
Simon Pilgrim | d69fc8e | 2016-09-30 14:18:06 +0000 | [diff] [blame] | 2555 | |
| 2556 | int NumSizeVariants = (HasSizedVariant ? 2 : 1); |
| 2557 | int NumAlignVariants = (HasAlignedVariant ? 2 : 1); |
| 2558 | for (int Sized = 0; Sized < NumSizeVariants; ++Sized) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2559 | if (Sized) |
| 2560 | Params.push_back(SizeT); |
| 2561 | |
Simon Pilgrim | d69fc8e | 2016-09-30 14:18:06 +0000 | [diff] [blame] | 2562 | for (int Aligned = 0; Aligned < NumAlignVariants; ++Aligned) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2563 | if (Aligned) |
| 2564 | Params.push_back(Context.getTypeDeclType(getStdAlignValT())); |
| 2565 | |
| 2566 | DeclareGlobalAllocationFunction( |
| 2567 | Context.DeclarationNames.getCXXOperatorName(Kind), Return, Params); |
| 2568 | |
| 2569 | if (Aligned) |
| 2570 | Params.pop_back(); |
| 2571 | } |
| 2572 | } |
| 2573 | }; |
| 2574 | |
| 2575 | DeclareGlobalAllocationFunctions(OO_New, VoidPtr, SizeT); |
| 2576 | DeclareGlobalAllocationFunctions(OO_Array_New, VoidPtr, SizeT); |
| 2577 | DeclareGlobalAllocationFunctions(OO_Delete, Context.VoidTy, VoidPtr); |
| 2578 | DeclareGlobalAllocationFunctions(OO_Array_Delete, Context.VoidTy, VoidPtr); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 2582 | /// allocation function if it doesn't already exist. |
| 2583 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2584 | QualType Return, |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2585 | ArrayRef<QualType> Params) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2586 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 2587 | |
| 2588 | // Check if this function is already declared. |
Serge Pavlov | d548907 | 2013-09-14 12:00:01 +0000 | [diff] [blame] | 2589 | DeclContext::lookup_result R = GlobalCtx->lookup(Name); |
| 2590 | for (DeclContext::lookup_iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 2591 | Alloc != AllocEnd; ++Alloc) { |
| 2592 | // Only look at non-template functions, as it is the predefined, |
| 2593 | // non-templated allocation function we are trying to declare here. |
| 2594 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2595 | if (Func->getNumParams() == Params.size()) { |
| 2596 | llvm::SmallVector<QualType, 3> FuncParams; |
| 2597 | for (auto *P : Func->parameters()) |
| 2598 | FuncParams.push_back( |
| 2599 | Context.getCanonicalType(P->getType().getUnqualifiedType())); |
| 2600 | if (llvm::makeArrayRef(FuncParams) == Params) { |
Serge Pavlov | d548907 | 2013-09-14 12:00:01 +0000 | [diff] [blame] | 2601 | // Make the function visible to name lookup, even if we found it in |
| 2602 | // an unimported module. It either is an implicitly-declared global |
Richard Smith | 42713d7 | 2013-07-14 02:01:48 +0000 | [diff] [blame] | 2603 | // allocation function, or is suppressing that function. |
| 2604 | Func->setHidden(false); |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 2605 | return; |
Douglas Gregor | c1a42fd | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 2606 | } |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 2607 | } |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2608 | } |
| 2609 | } |
Reid Kleckner | 7270ef5 | 2015-03-19 17:03:58 +0000 | [diff] [blame] | 2610 | |
Richard Smith | c015bc2 | 2014-02-07 22:39:53 +0000 | [diff] [blame] | 2611 | FunctionProtoType::ExtProtoInfo EPI; |
| 2612 | |
Richard Smith | f8b417c | 2014-02-08 00:42:45 +0000 | [diff] [blame] | 2613 | QualType BadAllocType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2614 | bool HasBadAllocExceptionSpec |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2615 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 2616 | Name.getCXXOverloadedOperator() == OO_Array_New); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2617 | if (HasBadAllocExceptionSpec) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2618 | if (!getLangOpts().CPlusPlus11) { |
Richard Smith | f8b417c | 2014-02-08 00:42:45 +0000 | [diff] [blame] | 2619 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Richard Smith | c015bc2 | 2014-02-07 22:39:53 +0000 | [diff] [blame] | 2620 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2621 | EPI.ExceptionSpec.Type = EST_Dynamic; |
| 2622 | EPI.ExceptionSpec.Exceptions = llvm::makeArrayRef(BadAllocType); |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2623 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2624 | } else { |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2625 | EPI.ExceptionSpec = |
| 2626 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2627 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2628 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2629 | auto CreateAllocationFunctionDecl = [&](Attr *ExtraAttr) { |
| 2630 | QualType FnType = Context.getFunctionType(Return, Params, EPI); |
| 2631 | FunctionDecl *Alloc = FunctionDecl::Create( |
| 2632 | Context, GlobalCtx, SourceLocation(), SourceLocation(), Name, |
| 2633 | FnType, /*TInfo=*/nullptr, SC_None, false, true); |
| 2634 | Alloc->setImplicit(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2635 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2636 | // Implicit sized deallocation functions always have default visibility. |
| 2637 | Alloc->addAttr( |
| 2638 | VisibilityAttr::CreateImplicit(Context, VisibilityAttr::Default)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2639 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2640 | llvm::SmallVector<ParmVarDecl *, 3> ParamDecls; |
| 2641 | for (QualType T : Params) { |
| 2642 | ParamDecls.push_back(ParmVarDecl::Create( |
| 2643 | Context, Alloc, SourceLocation(), SourceLocation(), nullptr, T, |
| 2644 | /*TInfo=*/nullptr, SC_None, nullptr)); |
| 2645 | ParamDecls.back()->setImplicit(); |
| 2646 | } |
| 2647 | Alloc->setParams(ParamDecls); |
| 2648 | if (ExtraAttr) |
| 2649 | Alloc->addAttr(ExtraAttr); |
| 2650 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
| 2651 | IdResolver.tryAddTopLevelDecl(Alloc, Name); |
| 2652 | }; |
| 2653 | |
| 2654 | if (!LangOpts.CUDA) |
| 2655 | CreateAllocationFunctionDecl(nullptr); |
| 2656 | else { |
| 2657 | // Host and device get their own declaration so each can be |
| 2658 | // defined or re-declared independently. |
| 2659 | CreateAllocationFunctionDecl(CUDAHostAttr::CreateImplicit(Context)); |
| 2660 | CreateAllocationFunctionDecl(CUDADeviceAttr::CreateImplicit(Context)); |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2661 | } |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2664 | FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc, |
| 2665 | bool CanProvideSize, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2666 | bool Overaligned, |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2667 | DeclarationName Name) { |
| 2668 | DeclareGlobalNewDelete(); |
| 2669 | |
| 2670 | LookupResult FoundDelete(*this, Name, StartLoc, LookupOrdinaryName); |
| 2671 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 2672 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2673 | // FIXME: It's possible for this to result in ambiguity, through a |
| 2674 | // user-declared variadic operator delete or the enable_if attribute. We |
| 2675 | // should probably not consider those cases to be usual deallocation |
| 2676 | // functions. But for now we just make an arbitrary choice in that case. |
| 2677 | auto Result = resolveDeallocationOverload(*this, FoundDelete, CanProvideSize, |
| 2678 | Overaligned); |
| 2679 | assert(Result.FD && "operator delete missing from global scope?"); |
| 2680 | return Result.FD; |
| 2681 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2682 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2683 | FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc, |
| 2684 | CXXRecordDecl *RD) { |
| 2685 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2686 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2687 | FunctionDecl *OperatorDelete = nullptr; |
| 2688 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
| 2689 | return nullptr; |
| 2690 | if (OperatorDelete) |
| 2691 | return OperatorDelete; |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 2692 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2693 | // If there's no class-specific operator delete, look up the global |
| 2694 | // non-array delete. |
| 2695 | return FindUsualDeallocationFunction( |
| 2696 | Loc, true, hasNewExtendedAlignment(*this, Context.getRecordType(RD)), |
| 2697 | Name); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2700 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 2701 | DeclarationName Name, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2702 | FunctionDecl *&Operator, bool Diagnose) { |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2703 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2704 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2705 | LookupQualifiedName(Found, RD); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2706 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2707 | if (Found.isAmbiguous()) |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2708 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2709 | |
Chandler Carruth | b6f9917 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 2710 | Found.suppressDiagnostics(); |
| 2711 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2712 | bool Overaligned = hasNewExtendedAlignment(*this, Context.getRecordType(RD)); |
Chandler Carruth | 9b41823 | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 2713 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2714 | // C++17 [expr.delete]p10: |
| 2715 | // If the deallocation functions have class scope, the one without a |
| 2716 | // parameter of type std::size_t is selected. |
| 2717 | llvm::SmallVector<UsualDeallocFnInfo, 4> Matches; |
| 2718 | resolveDeallocationOverload(*this, Found, /*WantSize*/ false, |
| 2719 | /*WantAlign*/ Overaligned, &Matches); |
Chandler Carruth | 9b41823 | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 2720 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2721 | // If we could find an overload, use it. |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2722 | if (Matches.size() == 1) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2723 | Operator = cast<CXXMethodDecl>(Matches[0].FD); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2724 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2725 | // FIXME: DiagnoseUseOfDecl? |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2726 | if (Operator->isDeleted()) { |
| 2727 | if (Diagnose) { |
| 2728 | Diag(StartLoc, diag::err_deleted_function_use); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 2729 | NoteDeletedFunction(Operator); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2730 | } |
| 2731 | return true; |
| 2732 | } |
| 2733 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 2734 | if (CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2735 | Matches[0].Found, Diagnose) == AR_inaccessible) |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 2736 | return true; |
| 2737 | |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2738 | return false; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2739 | } |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2740 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2741 | // We found multiple suitable operators; complain about the ambiguity. |
| 2742 | // FIXME: The standard doesn't say to do this; it appears that the intent |
| 2743 | // is that this should never happen. |
| 2744 | if (!Matches.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2745 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2746 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 2747 | << Name << RD; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2748 | for (auto &Match : Matches) |
| 2749 | Diag(Match.FD->getLocation(), diag::note_member_declared_here) << Name; |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2750 | } |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2751 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | // We did find operator delete/operator delete[] declarations, but |
| 2755 | // none of them were suitable. |
| 2756 | if (!Found.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2757 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2758 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 2759 | << Name << RD; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2760 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2761 | for (NamedDecl *D : Found) |
| 2762 | Diag(D->getUnderlyingDecl()->getLocation(), |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2763 | diag::note_member_declared_here) << Name; |
| 2764 | } |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2765 | return true; |
| 2766 | } |
| 2767 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2768 | Operator = nullptr; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2769 | return false; |
| 2770 | } |
| 2771 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2772 | namespace { |
| 2773 | /// \brief Checks whether delete-expression, and new-expression used for |
| 2774 | /// initializing deletee have the same array form. |
| 2775 | class MismatchingNewDeleteDetector { |
| 2776 | public: |
| 2777 | enum MismatchResult { |
| 2778 | /// Indicates that there is no mismatch or a mismatch cannot be proven. |
| 2779 | NoMismatch, |
| 2780 | /// Indicates that variable is initialized with mismatching form of \a new. |
| 2781 | VarInitMismatches, |
| 2782 | /// Indicates that member is initialized with mismatching form of \a new. |
| 2783 | MemberInitMismatches, |
| 2784 | /// Indicates that 1 or more constructors' definitions could not been |
| 2785 | /// analyzed, and they will be checked again at the end of translation unit. |
| 2786 | AnalyzeLater |
| 2787 | }; |
| 2788 | |
| 2789 | /// \param EndOfTU True, if this is the final analysis at the end of |
| 2790 | /// translation unit. False, if this is the initial analysis at the point |
| 2791 | /// delete-expression was encountered. |
| 2792 | explicit MismatchingNewDeleteDetector(bool EndOfTU) |
Alexander Shaposhnikov | 3087b2c | 2016-09-01 23:18:00 +0000 | [diff] [blame] | 2793 | : Field(nullptr), IsArrayForm(false), EndOfTU(EndOfTU), |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2794 | HasUndefinedConstructors(false) {} |
| 2795 | |
| 2796 | /// \brief Checks whether pointee of a delete-expression is initialized with |
| 2797 | /// matching form of new-expression. |
| 2798 | /// |
| 2799 | /// If return value is \c VarInitMismatches or \c MemberInitMismatches at the |
| 2800 | /// point where delete-expression is encountered, then a warning will be |
| 2801 | /// issued immediately. If return value is \c AnalyzeLater at the point where |
| 2802 | /// delete-expression is seen, then member will be analyzed at the end of |
| 2803 | /// translation unit. \c AnalyzeLater is returned iff at least one constructor |
| 2804 | /// couldn't be analyzed. If at least one constructor initializes the member |
| 2805 | /// with matching type of new, the return value is \c NoMismatch. |
| 2806 | MismatchResult analyzeDeleteExpr(const CXXDeleteExpr *DE); |
| 2807 | /// \brief Analyzes a class member. |
| 2808 | /// \param Field Class member to analyze. |
| 2809 | /// \param DeleteWasArrayForm Array form-ness of the delete-expression used |
| 2810 | /// for deleting the \p Field. |
| 2811 | MismatchResult analyzeField(FieldDecl *Field, bool DeleteWasArrayForm); |
Alexander Shaposhnikov | 3087b2c | 2016-09-01 23:18:00 +0000 | [diff] [blame] | 2812 | FieldDecl *Field; |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2813 | /// List of mismatching new-expressions used for initialization of the pointee |
| 2814 | llvm::SmallVector<const CXXNewExpr *, 4> NewExprs; |
| 2815 | /// Indicates whether delete-expression was in array form. |
| 2816 | bool IsArrayForm; |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2817 | |
| 2818 | private: |
| 2819 | const bool EndOfTU; |
| 2820 | /// \brief Indicates that there is at least one constructor without body. |
| 2821 | bool HasUndefinedConstructors; |
| 2822 | /// \brief Returns \c CXXNewExpr from given initialization expression. |
| 2823 | /// \param E Expression used for initializing pointee in delete-expression. |
NAKAMURA Takumi | 4bd67d8 | 2015-05-19 06:47:23 +0000 | [diff] [blame] | 2824 | /// E can be a single-element \c InitListExpr consisting of new-expression. |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2825 | const CXXNewExpr *getNewExprFromInitListOrExpr(const Expr *E); |
| 2826 | /// \brief Returns whether member is initialized with mismatching form of |
| 2827 | /// \c new either by the member initializer or in-class initialization. |
| 2828 | /// |
| 2829 | /// If bodies of all constructors are not visible at the end of translation |
| 2830 | /// unit or at least one constructor initializes member with the matching |
| 2831 | /// form of \c new, mismatch cannot be proven, and this function will return |
| 2832 | /// \c NoMismatch. |
| 2833 | MismatchResult analyzeMemberExpr(const MemberExpr *ME); |
| 2834 | /// \brief Returns whether variable is initialized with mismatching form of |
| 2835 | /// \c new. |
| 2836 | /// |
| 2837 | /// If variable is initialized with matching form of \c new or variable is not |
| 2838 | /// initialized with a \c new expression, this function will return true. |
| 2839 | /// If variable is initialized with mismatching form of \c new, returns false. |
| 2840 | /// \param D Variable to analyze. |
| 2841 | bool hasMatchingVarInit(const DeclRefExpr *D); |
| 2842 | /// \brief Checks whether the constructor initializes pointee with mismatching |
| 2843 | /// form of \c new. |
| 2844 | /// |
| 2845 | /// Returns true, if member is initialized with matching form of \c new in |
| 2846 | /// member initializer list. Returns false, if member is initialized with the |
| 2847 | /// matching form of \c new in this constructor's initializer or given |
| 2848 | /// constructor isn't defined at the point where delete-expression is seen, or |
| 2849 | /// member isn't initialized by the constructor. |
| 2850 | bool hasMatchingNewInCtor(const CXXConstructorDecl *CD); |
| 2851 | /// \brief Checks whether member is initialized with matching form of |
| 2852 | /// \c new in member initializer list. |
| 2853 | bool hasMatchingNewInCtorInit(const CXXCtorInitializer *CI); |
| 2854 | /// Checks whether member is initialized with mismatching form of \c new by |
| 2855 | /// in-class initializer. |
| 2856 | MismatchResult analyzeInClassInitializer(); |
| 2857 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2858 | } |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2859 | |
| 2860 | MismatchingNewDeleteDetector::MismatchResult |
| 2861 | MismatchingNewDeleteDetector::analyzeDeleteExpr(const CXXDeleteExpr *DE) { |
| 2862 | NewExprs.clear(); |
| 2863 | assert(DE && "Expected delete-expression"); |
| 2864 | IsArrayForm = DE->isArrayForm(); |
| 2865 | const Expr *E = DE->getArgument()->IgnoreParenImpCasts(); |
| 2866 | if (const MemberExpr *ME = dyn_cast<const MemberExpr>(E)) { |
| 2867 | return analyzeMemberExpr(ME); |
| 2868 | } else if (const DeclRefExpr *D = dyn_cast<const DeclRefExpr>(E)) { |
| 2869 | if (!hasMatchingVarInit(D)) |
| 2870 | return VarInitMismatches; |
| 2871 | } |
| 2872 | return NoMismatch; |
| 2873 | } |
| 2874 | |
| 2875 | const CXXNewExpr * |
| 2876 | MismatchingNewDeleteDetector::getNewExprFromInitListOrExpr(const Expr *E) { |
| 2877 | assert(E != nullptr && "Expected a valid initializer expression"); |
| 2878 | E = E->IgnoreParenImpCasts(); |
| 2879 | if (const InitListExpr *ILE = dyn_cast<const InitListExpr>(E)) { |
| 2880 | if (ILE->getNumInits() == 1) |
| 2881 | E = dyn_cast<const CXXNewExpr>(ILE->getInit(0)->IgnoreParenImpCasts()); |
| 2882 | } |
| 2883 | |
| 2884 | return dyn_cast_or_null<const CXXNewExpr>(E); |
| 2885 | } |
| 2886 | |
| 2887 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtorInit( |
| 2888 | const CXXCtorInitializer *CI) { |
| 2889 | const CXXNewExpr *NE = nullptr; |
| 2890 | if (Field == CI->getMember() && |
| 2891 | (NE = getNewExprFromInitListOrExpr(CI->getInit()))) { |
| 2892 | if (NE->isArray() == IsArrayForm) |
| 2893 | return true; |
| 2894 | else |
| 2895 | NewExprs.push_back(NE); |
| 2896 | } |
| 2897 | return false; |
| 2898 | } |
| 2899 | |
| 2900 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtor( |
| 2901 | const CXXConstructorDecl *CD) { |
| 2902 | if (CD->isImplicit()) |
| 2903 | return false; |
| 2904 | const FunctionDecl *Definition = CD; |
| 2905 | if (!CD->isThisDeclarationADefinition() && !CD->isDefined(Definition)) { |
| 2906 | HasUndefinedConstructors = true; |
| 2907 | return EndOfTU; |
| 2908 | } |
| 2909 | for (const auto *CI : cast<const CXXConstructorDecl>(Definition)->inits()) { |
| 2910 | if (hasMatchingNewInCtorInit(CI)) |
| 2911 | return true; |
| 2912 | } |
| 2913 | return false; |
| 2914 | } |
| 2915 | |
| 2916 | MismatchingNewDeleteDetector::MismatchResult |
| 2917 | MismatchingNewDeleteDetector::analyzeInClassInitializer() { |
| 2918 | assert(Field != nullptr && "This should be called only for members"); |
Ismail Pazarbasi | 7ff1836 | 2015-10-26 19:20:24 +0000 | [diff] [blame] | 2919 | const Expr *InitExpr = Field->getInClassInitializer(); |
| 2920 | if (!InitExpr) |
| 2921 | return EndOfTU ? NoMismatch : AnalyzeLater; |
| 2922 | if (const CXXNewExpr *NE = getNewExprFromInitListOrExpr(InitExpr)) { |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2923 | if (NE->isArray() != IsArrayForm) { |
| 2924 | NewExprs.push_back(NE); |
| 2925 | return MemberInitMismatches; |
| 2926 | } |
| 2927 | } |
| 2928 | return NoMismatch; |
| 2929 | } |
| 2930 | |
| 2931 | MismatchingNewDeleteDetector::MismatchResult |
| 2932 | MismatchingNewDeleteDetector::analyzeField(FieldDecl *Field, |
| 2933 | bool DeleteWasArrayForm) { |
| 2934 | assert(Field != nullptr && "Analysis requires a valid class member."); |
| 2935 | this->Field = Field; |
| 2936 | IsArrayForm = DeleteWasArrayForm; |
| 2937 | const CXXRecordDecl *RD = cast<const CXXRecordDecl>(Field->getParent()); |
| 2938 | for (const auto *CD : RD->ctors()) { |
| 2939 | if (hasMatchingNewInCtor(CD)) |
| 2940 | return NoMismatch; |
| 2941 | } |
| 2942 | if (HasUndefinedConstructors) |
| 2943 | return EndOfTU ? NoMismatch : AnalyzeLater; |
| 2944 | if (!NewExprs.empty()) |
| 2945 | return MemberInitMismatches; |
| 2946 | return Field->hasInClassInitializer() ? analyzeInClassInitializer() |
| 2947 | : NoMismatch; |
| 2948 | } |
| 2949 | |
| 2950 | MismatchingNewDeleteDetector::MismatchResult |
| 2951 | MismatchingNewDeleteDetector::analyzeMemberExpr(const MemberExpr *ME) { |
| 2952 | assert(ME != nullptr && "Expected a member expression"); |
| 2953 | if (FieldDecl *F = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2954 | return analyzeField(F, IsArrayForm); |
| 2955 | return NoMismatch; |
| 2956 | } |
| 2957 | |
| 2958 | bool MismatchingNewDeleteDetector::hasMatchingVarInit(const DeclRefExpr *D) { |
| 2959 | const CXXNewExpr *NE = nullptr; |
| 2960 | if (const VarDecl *VD = dyn_cast<const VarDecl>(D->getDecl())) { |
| 2961 | if (VD->hasInit() && (NE = getNewExprFromInitListOrExpr(VD->getInit())) && |
| 2962 | NE->isArray() != IsArrayForm) { |
| 2963 | NewExprs.push_back(NE); |
| 2964 | } |
| 2965 | } |
| 2966 | return NewExprs.empty(); |
| 2967 | } |
| 2968 | |
| 2969 | static void |
| 2970 | DiagnoseMismatchedNewDelete(Sema &SemaRef, SourceLocation DeleteLoc, |
| 2971 | const MismatchingNewDeleteDetector &Detector) { |
| 2972 | SourceLocation EndOfDelete = SemaRef.getLocForEndOfToken(DeleteLoc); |
| 2973 | FixItHint H; |
| 2974 | if (!Detector.IsArrayForm) |
| 2975 | H = FixItHint::CreateInsertion(EndOfDelete, "[]"); |
| 2976 | else { |
| 2977 | SourceLocation RSquare = Lexer::findLocationAfterToken( |
| 2978 | DeleteLoc, tok::l_square, SemaRef.getSourceManager(), |
| 2979 | SemaRef.getLangOpts(), true); |
| 2980 | if (RSquare.isValid()) |
| 2981 | H = FixItHint::CreateRemoval(SourceRange(EndOfDelete, RSquare)); |
| 2982 | } |
| 2983 | SemaRef.Diag(DeleteLoc, diag::warn_mismatched_delete_new) |
| 2984 | << Detector.IsArrayForm << H; |
| 2985 | |
| 2986 | for (const auto *NE : Detector.NewExprs) |
| 2987 | SemaRef.Diag(NE->getExprLoc(), diag::note_allocated_here) |
| 2988 | << Detector.IsArrayForm; |
| 2989 | } |
| 2990 | |
| 2991 | void Sema::AnalyzeDeleteExprMismatch(const CXXDeleteExpr *DE) { |
| 2992 | if (Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) |
| 2993 | return; |
| 2994 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/false); |
| 2995 | switch (Detector.analyzeDeleteExpr(DE)) { |
| 2996 | case MismatchingNewDeleteDetector::VarInitMismatches: |
| 2997 | case MismatchingNewDeleteDetector::MemberInitMismatches: { |
| 2998 | DiagnoseMismatchedNewDelete(*this, DE->getLocStart(), Detector); |
| 2999 | break; |
| 3000 | } |
| 3001 | case MismatchingNewDeleteDetector::AnalyzeLater: { |
| 3002 | DeleteExprs[Detector.Field].push_back( |
| 3003 | std::make_pair(DE->getLocStart(), DE->isArrayForm())); |
| 3004 | break; |
| 3005 | } |
| 3006 | case MismatchingNewDeleteDetector::NoMismatch: |
| 3007 | break; |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | void Sema::AnalyzeDeleteExprMismatch(FieldDecl *Field, SourceLocation DeleteLoc, |
| 3012 | bool DeleteWasArrayForm) { |
| 3013 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/true); |
| 3014 | switch (Detector.analyzeField(Field, DeleteWasArrayForm)) { |
| 3015 | case MismatchingNewDeleteDetector::VarInitMismatches: |
| 3016 | llvm_unreachable("This analysis should have been done for class members."); |
| 3017 | case MismatchingNewDeleteDetector::AnalyzeLater: |
| 3018 | llvm_unreachable("Analysis cannot be postponed any point beyond end of " |
| 3019 | "translation unit."); |
| 3020 | case MismatchingNewDeleteDetector::MemberInitMismatches: |
| 3021 | DiagnoseMismatchedNewDelete(*this, DeleteLoc, Detector); |
| 3022 | break; |
| 3023 | case MismatchingNewDeleteDetector::NoMismatch: |
| 3024 | break; |
| 3025 | } |
| 3026 | } |
| 3027 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3028 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 3029 | /// @code ::delete ptr; @endcode |
| 3030 | /// or |
| 3031 | /// @code delete [] ptr; @endcode |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3032 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3033 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3034 | bool ArrayForm, Expr *ExE) { |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3035 | // C++ [expr.delete]p1: |
| 3036 | // 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] | 3037 | // non-explicit conversion function to a pointer type. The result has type |
| 3038 | // void. |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3039 | // |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3040 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 3041 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3042 | ExprResult Ex = ExE; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3043 | FunctionDecl *OperatorDelete = nullptr; |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3044 | bool ArrayFormAsWritten = ArrayForm; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3045 | bool UsualArrayDeleteWantsSize = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3047 | if (!Ex.get()->isTypeDependent()) { |
John McCall | ef42902 | 2012-03-09 04:08:29 +0000 | [diff] [blame] | 3048 | // Perform lvalue-to-rvalue cast, if needed. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3049 | Ex = DefaultLvalueConversion(Ex.get()); |
Eli Friedman | 89a4a2c | 2012-12-13 00:37:17 +0000 | [diff] [blame] | 3050 | if (Ex.isInvalid()) |
| 3051 | return ExprError(); |
John McCall | ef42902 | 2012-03-09 04:08:29 +0000 | [diff] [blame] | 3052 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3053 | QualType Type = Ex.get()->getType(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3054 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3055 | class DeleteConverter : public ContextualImplicitConverter { |
| 3056 | public: |
| 3057 | DeleteConverter() : ContextualImplicitConverter(false, true) {} |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3058 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3059 | bool match(QualType ConvType) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3060 | // FIXME: If we have an operator T* and an operator void*, we must pick |
| 3061 | // the operator T*. |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3062 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3063 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3064 | return true; |
| 3065 | return false; |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3066 | } |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3067 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3068 | SemaDiagnosticBuilder diagnoseNoMatch(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3069 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3070 | return S.Diag(Loc, diag::err_delete_operand) << T; |
| 3071 | } |
| 3072 | |
| 3073 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3074 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3075 | return S.Diag(Loc, diag::err_delete_incomplete_class_type) << T; |
| 3076 | } |
| 3077 | |
| 3078 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3079 | QualType T, |
| 3080 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3081 | return S.Diag(Loc, diag::err_delete_explicit_conversion) << T << ConvTy; |
| 3082 | } |
| 3083 | |
| 3084 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3085 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3086 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
| 3087 | << ConvTy; |
| 3088 | } |
| 3089 | |
| 3090 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3091 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3092 | return S.Diag(Loc, diag::err_ambiguous_delete_operand) << T; |
| 3093 | } |
| 3094 | |
| 3095 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3096 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3097 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
| 3098 | << ConvTy; |
| 3099 | } |
| 3100 | |
| 3101 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3102 | QualType T, |
| 3103 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3104 | llvm_unreachable("conversion functions are permitted"); |
| 3105 | } |
| 3106 | } Converter; |
| 3107 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3108 | Ex = PerformContextualImplicitConversion(StartLoc, Ex.get(), Converter); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3109 | if (Ex.isInvalid()) |
| 3110 | return ExprError(); |
| 3111 | Type = Ex.get()->getType(); |
| 3112 | if (!Converter.match(Type)) |
| 3113 | // FIXME: PerformContextualImplicitConversion should return ExprError |
| 3114 | // itself in this case. |
| 3115 | return ExprError(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3116 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3117 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3118 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 3119 | |
| 3120 | if (unsigned AddressSpace = Pointee.getAddressSpace()) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3121 | return Diag(Ex.get()->getLocStart(), |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3122 | diag::err_address_space_qualified_delete) |
| 3123 | << Pointee.getUnqualifiedType() << AddressSpace; |
| 3124 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3125 | CXXRecordDecl *PointeeRD = nullptr; |
Douglas Gregor | bb3348e | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 3126 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3127 | // 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] | 3128 | // effectively bans deletion of "void*". However, most compilers support |
| 3129 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 3130 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3131 | << Type << Ex.get()->getSourceRange(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3132 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3133 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3134 | << Type << Ex.get()->getSourceRange()); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3135 | } else if (!Pointee->isDependentType()) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3136 | // FIXME: This can result in errors if the definition was imported from a |
| 3137 | // module but is hidden. |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3138 | if (!RequireCompleteType(StartLoc, Pointee, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3139 | diag::warn_delete_incomplete, Ex.get())) { |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3140 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
| 3141 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3142 | } |
| 3143 | } |
| 3144 | |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3145 | if (Pointee->isArrayType() && !ArrayForm) { |
| 3146 | Diag(StartLoc, diag::warn_delete_array_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3147 | << Type << Ex.get()->getSourceRange() |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 3148 | << FixItHint::CreateInsertion(getLocForEndOfToken(StartLoc), "[]"); |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3149 | ArrayForm = true; |
| 3150 | } |
| 3151 | |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 3152 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 3153 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 3154 | |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3155 | if (PointeeRD) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3156 | if (!UseGlobal && |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3157 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, |
| 3158 | OperatorDelete)) |
Anders Carlsson | 654e5c7 | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 3159 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3160 | |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3161 | // If we're allocating an array of records, check whether the |
| 3162 | // usual operator delete[] has a size_t parameter. |
| 3163 | if (ArrayForm) { |
| 3164 | // If the user specifically asked to use the global allocator, |
| 3165 | // we'll need to do the lookup into the class. |
| 3166 | if (UseGlobal) |
| 3167 | UsualArrayDeleteWantsSize = |
| 3168 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); |
| 3169 | |
| 3170 | // Otherwise, the usual operator delete[] should be the |
| 3171 | // function we just found. |
Richard Smith | f03bd30 | 2013-12-05 08:30:59 +0000 | [diff] [blame] | 3172 | else if (OperatorDelete && isa<CXXMethodDecl>(OperatorDelete)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3173 | UsualArrayDeleteWantsSize = |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 3174 | UsualDeallocFnInfo(*this, |
| 3175 | DeclAccessPair::make(OperatorDelete, AS_public)) |
| 3176 | .HasSizeT; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3179 | if (!PointeeRD->hasIrrelevantDestructor()) |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3180 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3181 | MarkFunctionReferenced(StartLoc, |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3182 | const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 3183 | if (DiagnoseUseOfDecl(Dtor, StartLoc)) |
| 3184 | return ExprError(); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3185 | } |
Argyrios Kyrtzidis | 8bd4285 | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 3186 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3187 | CheckVirtualDtorCall(PointeeRD->getDestructor(), StartLoc, |
| 3188 | /*IsDelete=*/true, /*CallCanBeVirtual=*/true, |
| 3189 | /*WarnOnNonAbstractTypes=*/!ArrayForm, |
| 3190 | SourceLocation()); |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 3191 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3192 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3193 | if (!OperatorDelete) { |
| 3194 | bool IsComplete = isCompleteType(StartLoc, Pointee); |
| 3195 | bool CanProvideSize = |
| 3196 | IsComplete && (!ArrayForm || UsualArrayDeleteWantsSize || |
| 3197 | Pointee.isDestructedType()); |
| 3198 | bool Overaligned = hasNewExtendedAlignment(*this, Pointee); |
| 3199 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 3200 | // Look for a global declaration. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3201 | OperatorDelete = FindUsualDeallocationFunction(StartLoc, CanProvideSize, |
| 3202 | Overaligned, DeleteName); |
| 3203 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3205 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3206 | |
Douglas Gregor | fa77813 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 3207 | // Check access and ambiguity of operator delete and destructor. |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3208 | if (PointeeRD) { |
| 3209 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3210 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
Douglas Gregor | fa77813 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 3211 | PDiag(diag::err_access_dtor) << PointeeElem); |
| 3212 | } |
| 3213 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3216 | CXXDeleteExpr *Result = new (Context) CXXDeleteExpr( |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3217 | Context.VoidTy, UseGlobal, ArrayForm, ArrayFormAsWritten, |
| 3218 | UsualArrayDeleteWantsSize, OperatorDelete, Ex.get(), StartLoc); |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3219 | AnalyzeDeleteExprMismatch(Result); |
| 3220 | return Result; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3223 | void Sema::CheckVirtualDtorCall(CXXDestructorDecl *dtor, SourceLocation Loc, |
| 3224 | bool IsDelete, bool CallCanBeVirtual, |
| 3225 | bool WarnOnNonAbstractTypes, |
| 3226 | SourceLocation DtorLoc) { |
| 3227 | if (!dtor || dtor->isVirtual() || !CallCanBeVirtual) |
| 3228 | return; |
| 3229 | |
| 3230 | // C++ [expr.delete]p3: |
| 3231 | // In the first alternative (delete object), if the static type of the |
| 3232 | // object to be deleted is different from its dynamic type, the static |
| 3233 | // type shall be a base class of the dynamic type of the object to be |
| 3234 | // deleted and the static type shall have a virtual destructor or the |
| 3235 | // behavior is undefined. |
| 3236 | // |
| 3237 | const CXXRecordDecl *PointeeRD = dtor->getParent(); |
| 3238 | // Note: a final class cannot be derived from, no issue there |
| 3239 | if (!PointeeRD->isPolymorphic() || PointeeRD->hasAttr<FinalAttr>()) |
| 3240 | return; |
| 3241 | |
| 3242 | QualType ClassType = dtor->getThisType(Context)->getPointeeType(); |
| 3243 | if (PointeeRD->isAbstract()) { |
| 3244 | // If the class is abstract, we warn by default, because we're |
| 3245 | // sure the code has undefined behavior. |
| 3246 | Diag(Loc, diag::warn_delete_abstract_non_virtual_dtor) << (IsDelete ? 0 : 1) |
| 3247 | << ClassType; |
| 3248 | } else if (WarnOnNonAbstractTypes) { |
| 3249 | // Otherwise, if this is not an array delete, it's a bit suspect, |
| 3250 | // but not necessarily wrong. |
| 3251 | Diag(Loc, diag::warn_delete_non_virtual_dtor) << (IsDelete ? 0 : 1) |
| 3252 | << ClassType; |
| 3253 | } |
| 3254 | if (!IsDelete) { |
| 3255 | std::string TypeStr; |
| 3256 | ClassType.getAsStringInternal(TypeStr, getPrintingPolicy()); |
| 3257 | Diag(DtorLoc, diag::note_delete_non_virtual) |
| 3258 | << FixItHint::CreateInsertion(DtorLoc, TypeStr + "::"); |
| 3259 | } |
| 3260 | } |
| 3261 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3262 | Sema::ConditionResult Sema::ActOnConditionVariable(Decl *ConditionVar, |
| 3263 | SourceLocation StmtLoc, |
| 3264 | ConditionKind CK) { |
| 3265 | ExprResult E = |
| 3266 | CheckConditionVariable(cast<VarDecl>(ConditionVar), StmtLoc, CK); |
| 3267 | if (E.isInvalid()) |
| 3268 | return ConditionError(); |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3269 | return ConditionResult(*this, ConditionVar, MakeFullExpr(E.get(), StmtLoc), |
| 3270 | CK == ConditionKind::ConstexprIf); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3273 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 3274 | /// while, do-while, or switch statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3275 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3276 | SourceLocation StmtLoc, |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3277 | ConditionKind CK) { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3278 | if (ConditionVar->isInvalidDecl()) |
| 3279 | return ExprError(); |
| 3280 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3281 | QualType T = ConditionVar->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3282 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3283 | // C++ [stmt.select]p2: |
| 3284 | // The declarator shall not specify a function or an array. |
| 3285 | if (T->isFunctionType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3286 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3287 | diag::err_invalid_use_of_function_type) |
| 3288 | << ConditionVar->getSourceRange()); |
| 3289 | else if (T->isArrayType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3291 | diag::err_invalid_use_of_array_type) |
| 3292 | << ConditionVar->getSourceRange()); |
Douglas Gregor | 0156d1c | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 3293 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3294 | ExprResult Condition = DeclRefExpr::Create( |
| 3295 | Context, NestedNameSpecifierLoc(), SourceLocation(), ConditionVar, |
| 3296 | /*enclosing*/ false, ConditionVar->getLocation(), |
| 3297 | ConditionVar->getType().getNonReferenceType(), VK_LValue); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3298 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3299 | MarkDeclRefReferenced(cast<DeclRefExpr>(Condition.get())); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3300 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3301 | switch (CK) { |
| 3302 | case ConditionKind::Boolean: |
| 3303 | return CheckBooleanCondition(StmtLoc, Condition.get()); |
| 3304 | |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3305 | case ConditionKind::ConstexprIf: |
| 3306 | return CheckBooleanCondition(StmtLoc, Condition.get(), true); |
| 3307 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3308 | case ConditionKind::Switch: |
| 3309 | return CheckSwitchCondition(StmtLoc, Condition.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3310 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3311 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3312 | llvm_unreachable("unexpected condition kind"); |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3315 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3316 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr, bool IsConstexpr) { |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3317 | // C++ 6.4p4: |
| 3318 | // The value of a condition that is an initialized declaration in a statement |
| 3319 | // other than a switch statement is the value of the declared variable |
| 3320 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 3321 | // program is ill-formed. |
| 3322 | // The value of a condition that is an expression is the value of the |
| 3323 | // expression, implicitly converted to bool. |
| 3324 | // |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3325 | // FIXME: Return this value to the caller so they don't need to recompute it. |
| 3326 | llvm::APSInt Value(/*BitWidth*/1); |
| 3327 | return (IsConstexpr && !CondExpr->isValueDependent()) |
| 3328 | ? CheckConvertedConstantExpression(CondExpr, Context.BoolTy, Value, |
| 3329 | CCEK_ConstexprIf) |
| 3330 | : PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3331 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3332 | |
| 3333 | /// Helper function to determine whether this is the (deprecated) C++ |
| 3334 | /// conversion from a string literal to a pointer to non-const char or |
| 3335 | /// non-const wchar_t (for narrow and wide string literals, |
| 3336 | /// respectively). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | bool |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3338 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 3339 | // Look inside the implicit cast, if it exists. |
| 3340 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 3341 | From = Cast->getSubExpr(); |
| 3342 | |
| 3343 | // A string literal (2.13.4) that is not a wide string literal can |
| 3344 | // be converted to an rvalue of type "pointer to char"; a wide |
| 3345 | // string literal can be converted to an rvalue of type "pointer |
| 3346 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 689999d | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 3347 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3348 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | if (const BuiltinType *ToPointeeType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3350 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3351 | // This conversion is considered only when there is an |
| 3352 | // explicit appropriate pointer target type (C++ 4.2p2). |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3353 | if (!ToPtrType->getPointeeType().hasQualifiers()) { |
| 3354 | switch (StrLit->getKind()) { |
| 3355 | case StringLiteral::UTF8: |
| 3356 | case StringLiteral::UTF16: |
| 3357 | case StringLiteral::UTF32: |
| 3358 | // We don't allow UTF literals to be implicitly converted |
| 3359 | break; |
| 3360 | case StringLiteral::Ascii: |
| 3361 | return (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 3362 | ToPointeeType->getKind() == BuiltinType::Char_S); |
| 3363 | case StringLiteral::Wide: |
Dmitry Polukhin | 9d64f72 | 2016-04-14 09:52:06 +0000 | [diff] [blame] | 3364 | return Context.typesAreCompatible(Context.getWideCharType(), |
| 3365 | QualType(ToPointeeType, 0)); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3366 | } |
| 3367 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | return false; |
| 3371 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3372 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3373 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3374 | SourceLocation CastLoc, |
| 3375 | QualType Ty, |
| 3376 | CastKind Kind, |
| 3377 | CXXMethodDecl *Method, |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3378 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3379 | bool HadMultipleCandidates, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3380 | Expr *From) { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3381 | switch (Kind) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3382 | default: llvm_unreachable("Unhandled cast kind!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3383 | case CK_ConstructorConversion: { |
Douglas Gregor | c7a3107 | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 3384 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3385 | SmallVector<Expr*, 8> ConstructorArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3386 | |
Richard Smith | 72d7405 | 2013-07-20 19:41:36 +0000 | [diff] [blame] | 3387 | if (S.RequireNonAbstractType(CastLoc, Ty, |
| 3388 | diag::err_allocation_of_abstract_type)) |
| 3389 | return ExprError(); |
| 3390 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3391 | if (S.CompleteConstructorCall(Constructor, From, CastLoc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3392 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3393 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 3394 | S.CheckConstructorAccess(CastLoc, Constructor, FoundDecl, |
| 3395 | InitializedEntity::InitializeTemporary(Ty)); |
Richard Smith | 7c9442a | 2015-02-24 21:44:43 +0000 | [diff] [blame] | 3396 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3397 | return ExprError(); |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 3398 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3399 | ExprResult Result = S.BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3400 | CastLoc, Ty, FoundDecl, cast<CXXConstructorDecl>(Method), |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3401 | ConstructorArgs, HadMultipleCandidates, |
| 3402 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3403 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3404 | if (Result.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3405 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3406 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3407 | return S.MaybeBindToTemporary(Result.getAs<Expr>()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3408 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3409 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3410 | case CK_UserDefinedConversion: { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3411 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3412 | |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3413 | S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ nullptr, FoundDecl); |
Richard Smith | 7c9442a | 2015-02-24 21:44:43 +0000 | [diff] [blame] | 3414 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3415 | return ExprError(); |
| 3416 | |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3417 | // Create an implicit call expr that calls it. |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 3418 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(Method); |
| 3419 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Conv, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3420 | HadMultipleCandidates); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3421 | if (Result.isInvalid()) |
| 3422 | return ExprError(); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 3423 | // Record usage of conversion in an implicit cast. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3424 | Result = ImplicitCastExpr::Create(S.Context, Result.get()->getType(), |
| 3425 | CK_UserDefinedConversion, Result.get(), |
| 3426 | nullptr, Result.get()->getValueKind()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3427 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3428 | return S.MaybeBindToTemporary(Result.get()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3429 | } |
| 3430 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3431 | } |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3432 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3433 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 3434 | /// expression From to the type ToType using the pre-computed implicit |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3435 | /// conversion sequence ICS. Returns the converted |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 3436 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3437 | /// used in the error message. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3438 | ExprResult |
| 3439 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3440 | const ImplicitConversionSequence &ICS, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3441 | AssignmentAction Action, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3442 | CheckedConversionKind CCK) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3443 | switch (ICS.getKind()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3444 | case ImplicitConversionSequence::StandardConversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3445 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, |
| 3446 | Action, CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3447 | if (Res.isInvalid()) |
| 3448 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3449 | From = Res.get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3450 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3451 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3452 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3453 | case ImplicitConversionSequence::UserDefinedConversion: { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3454 | |
Fariborz Jahanian | 2fee79a | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 3455 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3456 | CastKind CastKind; |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3457 | QualType BeforeToType; |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3458 | assert(FD && "no conversion function for user-defined conversion seq"); |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3459 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3460 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3461 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3462 | // If the user-defined conversion is specified by a conversion function, |
| 3463 | // the initial standard conversion sequence converts the source type to |
| 3464 | // the implicit object parameter of the conversion function. |
| 3465 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
John McCall | a03edda | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 3466 | } else { |
| 3467 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3468 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3469 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 3470 | if (!ICS.UserDefined.EllipsisConversion) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3471 | // If the user-defined conversion is specified by a constructor, the |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 3472 | // initial standard conversion sequence converts the source type to |
| 3473 | // the type required by the argument of the constructor |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 3474 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3475 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3476 | } |
Richard Smith | 72d7405 | 2013-07-20 19:41:36 +0000 | [diff] [blame] | 3477 | // Watch out for ellipsis conversion. |
Fariborz Jahanian | eec642f | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 3478 | if (!ICS.UserDefined.EllipsisConversion) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3479 | ExprResult Res = |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3480 | PerformImplicitConversion(From, BeforeToType, |
| 3481 | ICS.UserDefined.Before, AA_Converting, |
| 3482 | CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3483 | if (Res.isInvalid()) |
| 3484 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3485 | From = Res.get(); |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3486 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3487 | |
| 3488 | ExprResult CastArg |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3489 | = BuildCXXCastArgument(*this, |
| 3490 | From->getLocStart(), |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3491 | ToType.getNonReferenceType(), |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 3492 | CastKind, cast<CXXMethodDecl>(FD), |
| 3493 | ICS.UserDefined.FoundConversionFunction, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3494 | ICS.UserDefined.HadMultipleCandidates, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3495 | From); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3496 | |
| 3497 | if (CastArg.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3498 | return ExprError(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 3499 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3500 | From = CastArg.get(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 3501 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3502 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
| 3503 | AA_Converting, CCK); |
Fariborz Jahanian | da21efb | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 3504 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3505 | |
| 3506 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3507 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3508 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 3509 | << From->getSourceRange()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3510 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3511 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3512 | case ImplicitConversionSequence::EllipsisConversion: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3513 | llvm_unreachable("Cannot perform an ellipsis conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3514 | |
| 3515 | case ImplicitConversionSequence::BadConversion: |
Richard Smith | e15a370 | 2016-10-06 23:12:58 +0000 | [diff] [blame] | 3516 | bool Diagnosed = |
| 3517 | DiagnoseAssignmentResult(Incompatible, From->getExprLoc(), ToType, |
| 3518 | From->getType(), From, Action); |
| 3519 | assert(Diagnosed && "failed to diagnose bad conversion"); (void)Diagnosed; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3520 | return ExprError(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | // Everything went well. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3524 | return From; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3527 | /// PerformImplicitConversion - Perform an implicit conversion of the |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3528 | /// expression From to the type ToType by following the standard |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3529 | /// conversion sequence SCS. Returns the converted |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3530 | /// expression. Flavor is the context in which we're performing this |
| 3531 | /// conversion, for use in error messages. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3532 | ExprResult |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3533 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3534 | const StandardConversionSequence& SCS, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3535 | AssignmentAction Action, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3536 | CheckedConversionKind CCK) { |
| 3537 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3538 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3539 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 3540 | // much extra work. What this means is that we need to keep track of more |
| 3541 | // information that is computed when we try the implicit conversion initially, |
| 3542 | // so that we don't need to recompute anything here. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3543 | QualType FromType = From->getType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3544 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3545 | if (SCS.CopyConstructor) { |
Anders Carlsson | 549c5bd | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 3546 | // FIXME: When can ToType be a reference type? |
| 3547 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3548 | if (SCS.Second == ICK_Derived_To_Base) { |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3549 | SmallVector<Expr*, 8> ConstructorArgs; |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3550 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3551 | From, /*FIXME:ConstructLoc*/SourceLocation(), |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3552 | ConstructorArgs)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3553 | return ExprError(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3554 | return BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3555 | /*FIXME:ConstructLoc*/ SourceLocation(), ToType, |
| 3556 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3557 | ConstructorArgs, /*HadMultipleCandidates*/ false, |
| 3558 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3559 | CXXConstructExpr::CK_Complete, SourceRange()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3560 | } |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3561 | return BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3562 | /*FIXME:ConstructLoc*/ SourceLocation(), ToType, |
| 3563 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3564 | From, /*HadMultipleCandidates*/ false, |
| 3565 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3566 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3569 | // Resolve overloaded function references. |
| 3570 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 3571 | DeclAccessPair Found; |
| 3572 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 3573 | true, Found); |
| 3574 | if (!Fn) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3575 | return ExprError(); |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3576 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3577 | if (DiagnoseUseOfDecl(Fn, From->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3578 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3579 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3580 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 3581 | FromType = From->getType(); |
| 3582 | } |
| 3583 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3584 | // If we're converting to an atomic type, first convert to the corresponding |
| 3585 | // non-atomic type. |
| 3586 | QualType ToAtomicType; |
| 3587 | if (const AtomicType *ToAtomic = ToType->getAs<AtomicType>()) { |
| 3588 | ToAtomicType = ToType; |
| 3589 | ToType = ToAtomic->getValueType(); |
| 3590 | } |
| 3591 | |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 3592 | QualType InitialFromType = FromType; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3593 | // Perform the first implicit conversion. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3594 | switch (SCS.First) { |
| 3595 | case ICK_Identity: |
David Majnemer | 3087a2b | 2014-12-28 21:47:31 +0000 | [diff] [blame] | 3596 | if (const AtomicType *FromAtomic = FromType->getAs<AtomicType>()) { |
| 3597 | FromType = FromAtomic->getValueType().getUnqualifiedType(); |
| 3598 | From = ImplicitCastExpr::Create(Context, FromType, CK_AtomicToNonAtomic, |
| 3599 | From, /*BasePath=*/nullptr, VK_RValue); |
| 3600 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3601 | break; |
| 3602 | |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3603 | case ICK_Lvalue_To_Rvalue: { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 3604 | assert(From->getObjectKind() != OK_ObjCProperty); |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3605 | ExprResult FromRes = DefaultLvalueConversion(From); |
| 3606 | assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!"); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3607 | From = FromRes.get(); |
David Majnemer | e7029bc | 2014-12-16 06:31:17 +0000 | [diff] [blame] | 3608 | FromType = From->getType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3609 | break; |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3610 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3611 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3612 | case ICK_Array_To_Pointer: |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3613 | FromType = Context.getArrayDecayedType(FromType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3614 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3615 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3616 | break; |
| 3617 | |
| 3618 | case ICK_Function_To_Pointer: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3619 | FromType = Context.getPointerType(FromType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3620 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3621 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3622 | break; |
| 3623 | |
| 3624 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3625 | llvm_unreachable("Improper first standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3628 | // Perform the second implicit conversion |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3629 | switch (SCS.Second) { |
| 3630 | case ICK_Identity: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 3631 | // C++ [except.spec]p5: |
| 3632 | // [For] assignment to and initialization of pointers to functions, |
| 3633 | // pointers to member functions, and references to functions: the |
| 3634 | // target entity shall allow at least the exceptions allowed by the |
| 3635 | // source value in the assignment or initialization. |
| 3636 | switch (Action) { |
| 3637 | case AA_Assigning: |
| 3638 | case AA_Initializing: |
| 3639 | // Note, function argument passing and returning are initialization. |
| 3640 | case AA_Passing: |
| 3641 | case AA_Returning: |
| 3642 | case AA_Sending: |
| 3643 | case AA_Passing_CFAudited: |
| 3644 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 3645 | return ExprError(); |
| 3646 | break; |
| 3647 | |
| 3648 | case AA_Casting: |
| 3649 | case AA_Converting: |
| 3650 | // Casts and implicit conversions are not initialization, so are not |
| 3651 | // checked for exception specification mismatches. |
| 3652 | break; |
| 3653 | } |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3654 | // Nothing else to do. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3655 | break; |
| 3656 | |
| 3657 | case ICK_Integral_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3658 | case ICK_Integral_Conversion: |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3659 | if (ToType->isBooleanType()) { |
| 3660 | assert(FromType->castAs<EnumType>()->getDecl()->isFixed() && |
| 3661 | SCS.Second == ICK_Integral_Promotion && |
| 3662 | "only enums with fixed underlying type can promote to bool"); |
| 3663 | From = ImpCastExprToType(From, ToType, CK_IntegralToBoolean, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3664 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3665 | } else { |
| 3666 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3667 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3668 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3669 | break; |
| 3670 | |
| 3671 | case ICK_Floating_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3672 | case ICK_Floating_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3673 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3674 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3675 | break; |
| 3676 | |
| 3677 | case ICK_Complex_Promotion: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3678 | case ICK_Complex_Conversion: { |
| 3679 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
| 3680 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
| 3681 | CastKind CK; |
| 3682 | if (FromEl->isRealFloatingType()) { |
| 3683 | if (ToEl->isRealFloatingType()) |
| 3684 | CK = CK_FloatingComplexCast; |
| 3685 | else |
| 3686 | CK = CK_FloatingComplexToIntegralComplex; |
| 3687 | } else if (ToEl->isRealFloatingType()) { |
| 3688 | CK = CK_IntegralComplexToFloatingComplex; |
| 3689 | } else { |
| 3690 | CK = CK_IntegralComplexCast; |
| 3691 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3692 | From = ImpCastExprToType(From, ToType, CK, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3693 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3694 | break; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3695 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3696 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3697 | case ICK_Floating_Integral: |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 3698 | if (ToType->isRealFloatingType()) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3699 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3700 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3701 | else |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3702 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3703 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3704 | break; |
| 3705 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 3706 | case ICK_Compatible_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3707 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3708 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3709 | break; |
| 3710 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3711 | case ICK_Writeback_Conversion: |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3712 | case ICK_Pointer_Conversion: { |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3713 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3714 | // Diagnose incompatible Objective-C conversions |
Douglas Gregor | 2720dc6 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 3715 | if (Action == AA_Initializing || Action == AA_Assigning) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3716 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 3717 | diag::ext_typecheck_convert_incompatible_pointer) |
| 3718 | << ToType << From->getType() << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 3719 | << From->getSourceRange() << 0; |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 3720 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3721 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 3722 | diag::ext_typecheck_convert_incompatible_pointer) |
| 3723 | << From->getType() << ToType << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 3724 | << From->getSourceRange() << 0; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3725 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3726 | if (From->getType()->isObjCObjectPointerType() && |
| 3727 | ToType->isObjCObjectPointerType()) |
| 3728 | EmitRelatedResultTypeNote(From); |
Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame^] | 3729 | } else if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && |
| 3730 | !CheckObjCARCUnavailableWeakConversion(ToType, |
| 3731 | From->getType())) { |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 3732 | if (Action == AA_Initializing) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3733 | Diag(From->getLocStart(), |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 3734 | diag::err_arc_weak_unavailable_assign); |
| 3735 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3736 | Diag(From->getLocStart(), |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3737 | diag::err_arc_convesion_of_weak_unavailable) |
| 3738 | << (Action == AA_Casting) << From->getType() << ToType |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 3739 | << From->getSourceRange(); |
| 3740 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3741 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3742 | CastKind Kind = CK_Invalid; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3743 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3744 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3745 | return ExprError(); |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3746 | |
| 3747 | // Make sure we extend blocks if necessary. |
| 3748 | // FIXME: doing this here is really ugly. |
| 3749 | if (Kind == CK_BlockPointerToObjCPointerCast) { |
| 3750 | ExprResult E = From; |
| 3751 | (void) PrepareCastToObjCObjectPointer(E); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3752 | From = E.get(); |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3753 | } |
Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame^] | 3754 | if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) |
| 3755 | CheckObjCConversion(SourceRange(), ToType, From, CCK); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3756 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3757 | .get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3758 | break; |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3759 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3760 | |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3761 | case ICK_Pointer_Member: { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3762 | CastKind Kind = CK_Invalid; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3763 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3764 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3765 | return ExprError(); |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3766 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3767 | return ExprError(); |
David Majnemer | d96b997 | 2014-08-08 00:10:39 +0000 | [diff] [blame] | 3768 | |
| 3769 | // We may not have been able to figure out what this member pointer resolved |
| 3770 | // 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] | 3771 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3772 | (void)isCompleteType(From->getExprLoc(), From->getType()); |
| 3773 | (void)isCompleteType(From->getExprLoc(), ToType); |
David Majnemer | fc22e47 | 2015-06-12 17:55:44 +0000 | [diff] [blame] | 3774 | } |
David Majnemer | d96b997 | 2014-08-08 00:10:39 +0000 | [diff] [blame] | 3775 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3776 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3777 | .get(); |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3778 | break; |
| 3779 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3780 | |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 3781 | case ICK_Boolean_Conversion: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3782 | // Perform half-to-boolean conversion via float. |
| 3783 | if (From->getType()->isHalfType()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3784 | From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).get(); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3785 | FromType = Context.FloatTy; |
| 3786 | } |
| 3787 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3788 | From = ImpCastExprToType(From, Context.BoolTy, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3789 | ScalarTypeToBooleanCastKind(FromType), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3790 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3791 | break; |
| 3792 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3793 | case ICK_Derived_To_Base: { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3794 | CXXCastPath BasePath; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3795 | if (CheckDerivedToBaseConversion(From->getType(), |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 3796 | ToType.getNonReferenceType(), |
| 3797 | From->getLocStart(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3798 | From->getSourceRange(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3799 | &BasePath, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3800 | CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3801 | return ExprError(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3802 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3803 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 3804 | CK_DerivedToBase, From->getValueKind(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3805 | &BasePath, CCK).get(); |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 3806 | break; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3809 | case ICK_Vector_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3810 | From = ImpCastExprToType(From, ToType, CK_BitCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3811 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3812 | break; |
| 3813 | |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3814 | case ICK_Vector_Splat: { |
Fariborz Jahanian | 28d94b1 | 2015-03-05 23:06:09 +0000 | [diff] [blame] | 3815 | // Vector splat from any arithmetic type to a vector. |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3816 | Expr *Elem = prepareVectorSplat(ToType, From).get(); |
| 3817 | From = ImpCastExprToType(Elem, ToType, CK_VectorSplat, VK_RValue, |
| 3818 | /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3819 | break; |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3820 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3821 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3822 | case ICK_Complex_Real: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3823 | // Case 1. x -> _Complex y |
| 3824 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
| 3825 | QualType ElType = ToComplex->getElementType(); |
| 3826 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 3827 | |
| 3828 | // x -> y |
| 3829 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
| 3830 | // do nothing |
| 3831 | } else if (From->getType()->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3832 | From = ImpCastExprToType(From, ElType, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3833 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3834 | } else { |
| 3835 | assert(From->getType()->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3836 | From = ImpCastExprToType(From, ElType, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3837 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3838 | } |
| 3839 | // y -> _Complex y |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3840 | From = ImpCastExprToType(From, ToType, |
| 3841 | isFloatingComplex ? CK_FloatingRealToComplex |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3842 | : CK_IntegralRealToComplex).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3843 | |
| 3844 | // Case 2. _Complex x -> y |
| 3845 | } else { |
| 3846 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
| 3847 | assert(FromComplex); |
| 3848 | |
| 3849 | QualType ElType = FromComplex->getElementType(); |
| 3850 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 3851 | |
| 3852 | // _Complex x -> x |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3853 | From = ImpCastExprToType(From, ElType, |
| 3854 | isFloatingComplex ? CK_FloatingComplexToReal |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3855 | : CK_IntegralComplexToReal, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3856 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3857 | |
| 3858 | // x -> y |
| 3859 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
| 3860 | // do nothing |
| 3861 | } else if (ToType->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3862 | From = ImpCastExprToType(From, ToType, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3863 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3864 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3865 | } else { |
| 3866 | assert(ToType->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3867 | From = ImpCastExprToType(From, ToType, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3868 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3869 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3870 | } |
| 3871 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3872 | break; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3873 | |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 3874 | case ICK_Block_Pointer_Conversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3875 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3876 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3877 | break; |
| 3878 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3879 | |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 3880 | case ICK_TransparentUnionConversion: { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3881 | ExprResult FromRes = From; |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 3882 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3883 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); |
| 3884 | if (FromRes.isInvalid()) |
| 3885 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3886 | From = FromRes.get(); |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 3887 | assert ((ConvTy == Sema::Compatible) && |
| 3888 | "Improper transparent union conversion"); |
| 3889 | (void)ConvTy; |
| 3890 | break; |
| 3891 | } |
| 3892 | |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 3893 | case ICK_Zero_Event_Conversion: |
| 3894 | From = ImpCastExprToType(From, ToType, |
| 3895 | CK_ZeroToOCLEvent, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3896 | From->getValueKind()).get(); |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 3897 | break; |
| 3898 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3899 | case ICK_Zero_Queue_Conversion: |
| 3900 | From = ImpCastExprToType(From, ToType, |
| 3901 | CK_ZeroToOCLQueue, |
| 3902 | From->getValueKind()).get(); |
| 3903 | break; |
| 3904 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3905 | case ICK_Lvalue_To_Rvalue: |
| 3906 | case ICK_Array_To_Pointer: |
| 3907 | case ICK_Function_To_Pointer: |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 3908 | case ICK_Function_Conversion: |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3909 | case ICK_Qualification: |
| 3910 | case ICK_Num_Conversion_Kinds: |
George Burgess IV | 78ed9b4 | 2015-10-11 20:37:14 +0000 | [diff] [blame] | 3911 | case ICK_C_Only_Conversion: |
George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 3912 | case ICK_Incompatible_Pointer_Conversion: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3913 | llvm_unreachable("Improper second standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | switch (SCS.Third) { |
| 3917 | case ICK_Identity: |
| 3918 | // Nothing to do. |
| 3919 | break; |
| 3920 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 3921 | case ICK_Function_Conversion: |
| 3922 | // If both sides are functions (or pointers/references to them), there could |
| 3923 | // be incompatible exception declarations. |
| 3924 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 3925 | return ExprError(); |
| 3926 | |
| 3927 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 3928 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
| 3929 | break; |
| 3930 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3931 | case ICK_Qualification: { |
| 3932 | // The qualification keeps the category of the inner expression, unless the |
| 3933 | // target type isn't a reference. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3934 | ExprValueKind VK = ToType->isReferenceType() ? |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 3935 | From->getValueKind() : VK_RValue; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3936 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3937 | CK_NoOp, VK, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3938 | |
Douglas Gregor | e981bb0 | 2011-03-14 16:13:32 +0000 | [diff] [blame] | 3939 | if (SCS.DeprecatedStringLiteralToCharPtr && |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3940 | !getLangOpts().WritableStrings) { |
| 3941 | Diag(From->getLocStart(), getLangOpts().CPlusPlus11 |
| 3942 | ? diag::ext_deprecated_string_literal_conversion |
| 3943 | : diag::warn_deprecated_string_literal_conversion) |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3944 | << ToType.getNonReferenceType(); |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3945 | } |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3946 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3947 | break; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3948 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3950 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3951 | llvm_unreachable("Improper third standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
Douglas Gregor | 298f43d | 2012-04-12 20:42:30 +0000 | [diff] [blame] | 3954 | // If this conversion sequence involved a scalar -> atomic conversion, perform |
| 3955 | // that conversion now. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3956 | if (!ToAtomicType.isNull()) { |
| 3957 | assert(Context.hasSameType( |
| 3958 | ToAtomicType->castAs<AtomicType>()->getValueType(), From->getType())); |
| 3959 | From = ImpCastExprToType(From, ToAtomicType, CK_NonAtomicToAtomic, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3960 | VK_RValue, nullptr, CCK).get(); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 3963 | // If this conversion sequence succeeded and involved implicitly converting a |
| 3964 | // _Nullable type to a _Nonnull one, complain. |
| 3965 | if (CCK == CCK_ImplicitConversion) |
| 3966 | diagnoseNullableToNonnullConversion(ToType, InitialFromType, |
| 3967 | From->getLocStart()); |
| 3968 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3969 | return From; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 3972 | /// \brief Check the completeness of a type in a unary type trait. |
| 3973 | /// |
| 3974 | /// If the particular type trait requires a complete type, tries to complete |
| 3975 | /// it. If completing the type fails, a diagnostic is emitted and false |
| 3976 | /// returned. If completing the type succeeds or no completion was required, |
| 3977 | /// returns true. |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 3978 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT, |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 3979 | SourceLocation Loc, |
| 3980 | QualType ArgTy) { |
| 3981 | // C++0x [meta.unary.prop]p3: |
| 3982 | // For all of the class templates X declared in this Clause, instantiating |
| 3983 | // that template with a template argument that is a class template |
| 3984 | // specialization may result in the implicit instantiation of the template |
| 3985 | // argument if and only if the semantics of X require that the argument |
| 3986 | // must be a complete type. |
| 3987 | // We apply this rule to all the type trait expressions used to implement |
| 3988 | // these class templates. We also try to follow any GCC documented behavior |
| 3989 | // in these expressions to ensure portability of standard libraries. |
| 3990 | switch (UTT) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 3991 | default: llvm_unreachable("not a UTT"); |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 3992 | // is_complete_type somewhat obviously cannot require a complete type. |
| 3993 | case UTT_IsCompleteType: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 3994 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 3995 | |
| 3996 | // These traits are modeled on the type predicates in C++0x |
| 3997 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as |
| 3998 | // requiring a complete type, as whether or not they return true cannot be |
| 3999 | // impacted by the completeness of the type. |
| 4000 | case UTT_IsVoid: |
| 4001 | case UTT_IsIntegral: |
| 4002 | case UTT_IsFloatingPoint: |
| 4003 | case UTT_IsArray: |
| 4004 | case UTT_IsPointer: |
| 4005 | case UTT_IsLvalueReference: |
| 4006 | case UTT_IsRvalueReference: |
| 4007 | case UTT_IsMemberFunctionPointer: |
| 4008 | case UTT_IsMemberObjectPointer: |
| 4009 | case UTT_IsEnum: |
| 4010 | case UTT_IsUnion: |
| 4011 | case UTT_IsClass: |
| 4012 | case UTT_IsFunction: |
| 4013 | case UTT_IsReference: |
| 4014 | case UTT_IsArithmetic: |
| 4015 | case UTT_IsFundamental: |
| 4016 | case UTT_IsObject: |
| 4017 | case UTT_IsScalar: |
| 4018 | case UTT_IsCompound: |
| 4019 | case UTT_IsMemberPointer: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 4020 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4021 | |
| 4022 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] |
| 4023 | // which requires some of its traits to have the complete type. However, |
| 4024 | // the completeness of the type cannot impact these traits' semantics, and |
| 4025 | // so they don't require it. This matches the comments on these traits in |
| 4026 | // Table 49. |
| 4027 | case UTT_IsConst: |
| 4028 | case UTT_IsVolatile: |
| 4029 | case UTT_IsSigned: |
| 4030 | case UTT_IsUnsigned: |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4031 | |
| 4032 | // This type trait always returns false, checking the type is moot. |
| 4033 | case UTT_IsInterfaceClass: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4034 | return true; |
| 4035 | |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4036 | // C++14 [meta.unary.prop]: |
| 4037 | // If T is a non-union class type, T shall be a complete type. |
| 4038 | case UTT_IsEmpty: |
| 4039 | case UTT_IsPolymorphic: |
| 4040 | case UTT_IsAbstract: |
| 4041 | if (const auto *RD = ArgTy->getAsCXXRecordDecl()) |
| 4042 | if (!RD->isUnion()) |
| 4043 | return !S.RequireCompleteType( |
| 4044 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
| 4045 | return true; |
| 4046 | |
| 4047 | // C++14 [meta.unary.prop]: |
| 4048 | // If T is a class type, T shall be a complete type. |
| 4049 | case UTT_IsFinal: |
| 4050 | case UTT_IsSealed: |
| 4051 | if (ArgTy->getAsCXXRecordDecl()) |
| 4052 | return !S.RequireCompleteType( |
| 4053 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
| 4054 | return true; |
| 4055 | |
| 4056 | // C++0x [meta.unary.prop] Table 49 requires the following traits to be |
| 4057 | // applied to a complete type. |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4058 | case UTT_IsTrivial: |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 4059 | case UTT_IsTriviallyCopyable: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4060 | case UTT_IsStandardLayout: |
| 4061 | case UTT_IsPOD: |
| 4062 | case UTT_IsLiteral: |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4063 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4064 | case UTT_IsDestructible: |
| 4065 | case UTT_IsNothrowDestructible: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 4066 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4067 | |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 4068 | // These trait expressions are designed to help implement predicates in |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4069 | // [meta.unary.prop] despite not being named the same. They are specified |
| 4070 | // by both GCC and the Embarcadero C++ compiler, and require the complete |
| 4071 | // type due to the overarching C++0x type predicates being implemented |
| 4072 | // requiring the complete type. |
| 4073 | case UTT_HasNothrowAssign: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4074 | case UTT_HasNothrowMoveAssign: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4075 | case UTT_HasNothrowConstructor: |
| 4076 | case UTT_HasNothrowCopy: |
| 4077 | case UTT_HasTrivialAssign: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4078 | case UTT_HasTrivialMoveAssign: |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4079 | case UTT_HasTrivialDefaultConstructor: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4080 | case UTT_HasTrivialMoveConstructor: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4081 | case UTT_HasTrivialCopy: |
| 4082 | case UTT_HasTrivialDestructor: |
| 4083 | case UTT_HasVirtualDestructor: |
| 4084 | // Arrays of unknown bound are expressly allowed. |
| 4085 | QualType ElTy = ArgTy; |
| 4086 | if (ArgTy->isIncompleteArrayType()) |
| 4087 | ElTy = S.Context.getAsArrayType(ArgTy)->getElementType(); |
| 4088 | |
| 4089 | // The void type is expressly allowed. |
| 4090 | if (ElTy->isVoidType()) |
| 4091 | return true; |
| 4092 | |
| 4093 | return !S.RequireCompleteType( |
| 4094 | Loc, ElTy, diag::err_incomplete_type_used_in_type_trait_expr); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4095 | } |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4098 | static bool HasNoThrowOperator(const RecordType *RT, OverloadedOperatorKind Op, |
| 4099 | Sema &Self, SourceLocation KeyLoc, ASTContext &C, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4100 | bool (CXXRecordDecl::*HasTrivial)() const, |
| 4101 | bool (CXXRecordDecl::*HasNonTrivial)() const, |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4102 | bool (CXXMethodDecl::*IsDesiredOp)() const) |
| 4103 | { |
| 4104 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 4105 | if ((RD->*HasTrivial)() && !(RD->*HasNonTrivial)()) |
| 4106 | return true; |
| 4107 | |
| 4108 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(Op); |
| 4109 | DeclarationNameInfo NameInfo(Name, KeyLoc); |
| 4110 | LookupResult Res(Self, NameInfo, Sema::LookupOrdinaryName); |
| 4111 | if (Self.LookupQualifiedName(Res, RD)) { |
| 4112 | bool FoundOperator = false; |
| 4113 | Res.suppressDiagnostics(); |
| 4114 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 4115 | Op != OpEnd; ++Op) { |
| 4116 | if (isa<FunctionTemplateDecl>(*Op)) |
| 4117 | continue; |
| 4118 | |
| 4119 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 4120 | if((Operator->*IsDesiredOp)()) { |
| 4121 | FoundOperator = true; |
| 4122 | const FunctionProtoType *CPT = |
| 4123 | Operator->getType()->getAs<FunctionProtoType>(); |
| 4124 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4125 | if (!CPT || !CPT->isNothrow(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4126 | return false; |
| 4127 | } |
| 4128 | } |
| 4129 | return FoundOperator; |
| 4130 | } |
| 4131 | return false; |
| 4132 | } |
| 4133 | |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4134 | static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT, |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4135 | SourceLocation KeyLoc, QualType T) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4136 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4137 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4138 | ASTContext &C = Self.Context; |
| 4139 | switch(UTT) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4140 | default: llvm_unreachable("not a UTT"); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4141 | // Type trait expressions corresponding to the primary type category |
| 4142 | // predicates in C++0x [meta.unary.cat]. |
| 4143 | case UTT_IsVoid: |
| 4144 | return T->isVoidType(); |
| 4145 | case UTT_IsIntegral: |
| 4146 | return T->isIntegralType(C); |
| 4147 | case UTT_IsFloatingPoint: |
| 4148 | return T->isFloatingType(); |
| 4149 | case UTT_IsArray: |
| 4150 | return T->isArrayType(); |
| 4151 | case UTT_IsPointer: |
| 4152 | return T->isPointerType(); |
| 4153 | case UTT_IsLvalueReference: |
| 4154 | return T->isLValueReferenceType(); |
| 4155 | case UTT_IsRvalueReference: |
| 4156 | return T->isRValueReferenceType(); |
| 4157 | case UTT_IsMemberFunctionPointer: |
| 4158 | return T->isMemberFunctionPointerType(); |
| 4159 | case UTT_IsMemberObjectPointer: |
| 4160 | return T->isMemberDataPointerType(); |
| 4161 | case UTT_IsEnum: |
| 4162 | return T->isEnumeralType(); |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4163 | case UTT_IsUnion: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4164 | return T->isUnionType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4165 | case UTT_IsClass: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4166 | return T->isClassType() || T->isStructureType() || T->isInterfaceType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4167 | case UTT_IsFunction: |
| 4168 | return T->isFunctionType(); |
| 4169 | |
| 4170 | // Type trait expressions which correspond to the convenient composition |
| 4171 | // predicates in C++0x [meta.unary.comp]. |
| 4172 | case UTT_IsReference: |
| 4173 | return T->isReferenceType(); |
| 4174 | case UTT_IsArithmetic: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4175 | return T->isArithmeticType() && !T->isEnumeralType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4176 | case UTT_IsFundamental: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4177 | return T->isFundamentalType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4178 | case UTT_IsObject: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4179 | return T->isObjectType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4180 | case UTT_IsScalar: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4181 | // Note: semantic analysis depends on Objective-C lifetime types to be |
| 4182 | // considered scalar types. However, such types do not actually behave |
| 4183 | // like scalar types at run time (since they may require retain/release |
| 4184 | // operations), so we report them as non-scalar. |
| 4185 | if (T->isObjCLifetimeType()) { |
| 4186 | switch (T.getObjCLifetime()) { |
| 4187 | case Qualifiers::OCL_None: |
| 4188 | case Qualifiers::OCL_ExplicitNone: |
| 4189 | return true; |
| 4190 | |
| 4191 | case Qualifiers::OCL_Strong: |
| 4192 | case Qualifiers::OCL_Weak: |
| 4193 | case Qualifiers::OCL_Autoreleasing: |
| 4194 | return false; |
| 4195 | } |
| 4196 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4197 | |
Chandler Carruth | 7ba7bd3 | 2011-05-01 09:29:55 +0000 | [diff] [blame] | 4198 | return T->isScalarType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4199 | case UTT_IsCompound: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4200 | return T->isCompoundType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4201 | case UTT_IsMemberPointer: |
| 4202 | return T->isMemberPointerType(); |
| 4203 | |
| 4204 | // Type trait expressions which correspond to the type property predicates |
| 4205 | // in C++0x [meta.unary.prop]. |
| 4206 | case UTT_IsConst: |
| 4207 | return T.isConstQualified(); |
| 4208 | case UTT_IsVolatile: |
| 4209 | return T.isVolatileQualified(); |
| 4210 | case UTT_IsTrivial: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4211 | return T.isTrivialType(C); |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 4212 | case UTT_IsTriviallyCopyable: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4213 | return T.isTriviallyCopyableType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4214 | case UTT_IsStandardLayout: |
| 4215 | return T->isStandardLayoutType(); |
| 4216 | case UTT_IsPOD: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4217 | return T.isPODType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4218 | case UTT_IsLiteral: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4219 | return T->isLiteralType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4220 | case UTT_IsEmpty: |
| 4221 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4222 | return !RD->isUnion() && RD->isEmpty(); |
| 4223 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4224 | case UTT_IsPolymorphic: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4225 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4226 | return !RD->isUnion() && RD->isPolymorphic(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4227 | return false; |
| 4228 | case UTT_IsAbstract: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4229 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4230 | return !RD->isUnion() && RD->isAbstract(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4231 | return false; |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4232 | // __is_interface_class only returns true when CL is invoked in /CLR mode and |
| 4233 | // even then only when it is used with the 'interface struct ...' syntax |
| 4234 | // Clang doesn't support /CLR which makes this type trait moot. |
John McCall | bf4a7d7 | 2012-09-25 07:32:49 +0000 | [diff] [blame] | 4235 | case UTT_IsInterfaceClass: |
John McCall | bf4a7d7 | 2012-09-25 07:32:49 +0000 | [diff] [blame] | 4236 | return false; |
Douglas Gregor | dca70af | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 4237 | case UTT_IsFinal: |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4238 | case UTT_IsSealed: |
| 4239 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4240 | return RD->hasAttr<FinalAttr>(); |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4241 | return false; |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4242 | case UTT_IsSigned: |
| 4243 | return T->isSignedIntegerType(); |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4244 | case UTT_IsUnsigned: |
| 4245 | return T->isUnsignedIntegerType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4246 | |
| 4247 | // Type trait expressions which query classes regarding their construction, |
| 4248 | // destruction, and copying. Rather than being based directly on the |
| 4249 | // related type predicates in the standard, they are specified by both |
| 4250 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those |
| 4251 | // specifications. |
| 4252 | // |
| 4253 | // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html |
| 4254 | // 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] | 4255 | // |
| 4256 | // Note that these builtins do not behave as documented in g++: if a class |
| 4257 | // has both a trivial and a non-trivial special member of a particular kind, |
| 4258 | // they return false! For now, we emulate this behavior. |
| 4259 | // FIXME: This appears to be a g++ bug: more complex cases reveal that it |
| 4260 | // does not correctly compute triviality in the presence of multiple special |
| 4261 | // 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] | 4262 | case UTT_HasTrivialDefaultConstructor: |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4263 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4264 | // If __is_pod (type) is true then the trait is true, else if type is |
| 4265 | // a cv class or union type (or array thereof) with a trivial default |
| 4266 | // constructor ([class.ctor]) then the trait is true, else it is false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4267 | if (T.isPODType(C)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4268 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4269 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4270 | return RD->hasTrivialDefaultConstructor() && |
| 4271 | !RD->hasNonTrivialDefaultConstructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4272 | return false; |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4273 | case UTT_HasTrivialMoveConstructor: |
| 4274 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4275 | // standard library headers. Specifically this is used as the logic |
| 4276 | // behind std::is_trivially_move_constructible (20.9.4.3). |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4277 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4278 | return true; |
| 4279 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4280 | return RD->hasTrivialMoveConstructor() && !RD->hasNonTrivialMoveConstructor(); |
| 4281 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4282 | case UTT_HasTrivialCopy: |
| 4283 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4284 | // If __is_pod (type) is true or type is a reference type then |
| 4285 | // the trait is true, else if type is a cv class or union type |
| 4286 | // with a trivial copy constructor ([class.copy]) then the trait |
| 4287 | // is true, else it is false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4288 | if (T.isPODType(C) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4289 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4290 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4291 | return RD->hasTrivialCopyConstructor() && |
| 4292 | !RD->hasNonTrivialCopyConstructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4293 | return false; |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4294 | case UTT_HasTrivialMoveAssign: |
| 4295 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4296 | // standard library headers. Specifically it is used as the logic |
| 4297 | // behind std::is_trivially_move_assignable (20.9.4.3) |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4298 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4299 | return true; |
| 4300 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4301 | return RD->hasTrivialMoveAssignment() && !RD->hasNonTrivialMoveAssignment(); |
| 4302 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4303 | case UTT_HasTrivialAssign: |
| 4304 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4305 | // If type is const qualified or is a reference type then the |
| 4306 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 4307 | // trait is true, else if type is a cv class or union type with |
| 4308 | // a trivial copy assignment ([class.copy]) then the trait is |
| 4309 | // true, else it is false. |
| 4310 | // Note: the const and reference restrictions are interesting, |
| 4311 | // given that const and reference members don't prevent a class |
| 4312 | // from having a trivial copy assignment operator (but do cause |
| 4313 | // errors if the copy assignment operator is actually used, q.v. |
| 4314 | // [class.copy]p12). |
| 4315 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4316 | if (T.isConstQualified()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4317 | return false; |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4318 | if (T.isPODType(C)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4319 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4320 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4321 | return RD->hasTrivialCopyAssignment() && |
| 4322 | !RD->hasNonTrivialCopyAssignment(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4323 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4324 | case UTT_IsDestructible: |
| 4325 | case UTT_IsNothrowDestructible: |
David Majnemer | ac73de9 | 2015-08-11 03:03:28 +0000 | [diff] [blame] | 4326 | // C++14 [meta.unary.prop]: |
| 4327 | // For reference types, is_destructible<T>::value is true. |
| 4328 | if (T->isReferenceType()) |
| 4329 | return true; |
| 4330 | |
| 4331 | // Objective-C++ ARC: autorelease types don't require destruction. |
| 4332 | if (T->isObjCLifetimeType() && |
| 4333 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 4334 | return true; |
| 4335 | |
| 4336 | // C++14 [meta.unary.prop]: |
| 4337 | // For incomplete types and function types, is_destructible<T>::value is |
| 4338 | // false. |
| 4339 | if (T->isIncompleteType() || T->isFunctionType()) |
| 4340 | return false; |
| 4341 | |
| 4342 | // C++14 [meta.unary.prop]: |
| 4343 | // For object types and given U equal to remove_all_extents_t<T>, if the |
| 4344 | // expression std::declval<U&>().~U() is well-formed when treated as an |
| 4345 | // unevaluated operand (Clause 5), then is_destructible<T>::value is true |
| 4346 | if (auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
| 4347 | CXXDestructorDecl *Destructor = Self.LookupDestructor(RD); |
| 4348 | if (!Destructor) |
| 4349 | return false; |
| 4350 | // C++14 [dcl.fct.def.delete]p2: |
| 4351 | // A program that refers to a deleted function implicitly or |
| 4352 | // explicitly, other than to declare it, is ill-formed. |
| 4353 | if (Destructor->isDeleted()) |
| 4354 | return false; |
| 4355 | if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public) |
| 4356 | return false; |
| 4357 | if (UTT == UTT_IsNothrowDestructible) { |
| 4358 | const FunctionProtoType *CPT = |
| 4359 | Destructor->getType()->getAs<FunctionProtoType>(); |
| 4360 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4361 | if (!CPT || !CPT->isNothrow(C)) |
| 4362 | return false; |
| 4363 | } |
| 4364 | } |
| 4365 | return true; |
| 4366 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4367 | case UTT_HasTrivialDestructor: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4368 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4369 | // If __is_pod (type) is true or type is a reference type |
| 4370 | // then the trait is true, else if type is a cv class or union |
| 4371 | // type (or array thereof) with a trivial destructor |
| 4372 | // ([class.dtor]) then the trait is true, else it is |
| 4373 | // false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4374 | if (T.isPODType(C) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4375 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4376 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4377 | // Objective-C++ ARC: autorelease types don't require destruction. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4378 | if (T->isObjCLifetimeType() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4379 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 4380 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4381 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4382 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4383 | return RD->hasTrivialDestructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4384 | return false; |
| 4385 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 4386 | case UTT_HasNothrowAssign: |
| 4387 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4388 | // If type is const qualified or is a reference type then the |
| 4389 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 4390 | // is true then the trait is true, else if type is a cv class |
| 4391 | // or union type with copy assignment operators that are known |
| 4392 | // not to throw an exception then the trait is true, else it is |
| 4393 | // false. |
| 4394 | if (C.getBaseElementType(T).isConstQualified()) |
| 4395 | return false; |
| 4396 | if (T->isReferenceType()) |
| 4397 | return false; |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4398 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4399 | return true; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4400 | |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4401 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 4402 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
| 4403 | &CXXRecordDecl::hasTrivialCopyAssignment, |
| 4404 | &CXXRecordDecl::hasNonTrivialCopyAssignment, |
| 4405 | &CXXMethodDecl::isCopyAssignmentOperator); |
| 4406 | return false; |
| 4407 | case UTT_HasNothrowMoveAssign: |
| 4408 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4409 | // standard library headers. Specifically this is used as the logic |
| 4410 | // behind std::is_nothrow_move_assignable (20.9.4.3). |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4411 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4412 | return true; |
| 4413 | |
| 4414 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) |
| 4415 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
| 4416 | &CXXRecordDecl::hasTrivialMoveAssignment, |
| 4417 | &CXXRecordDecl::hasNonTrivialMoveAssignment, |
| 4418 | &CXXMethodDecl::isMoveAssignmentOperator); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4419 | return false; |
| 4420 | case UTT_HasNothrowCopy: |
| 4421 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4422 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 4423 | // if type is a cv class or union type with copy constructors that are |
| 4424 | // known not to throw an exception then the trait is true, else it is |
| 4425 | // false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4426 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4427 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4428 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
| 4429 | if (RD->hasTrivialCopyConstructor() && |
| 4430 | !RD->hasNonTrivialCopyConstructor()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4431 | return true; |
| 4432 | |
| 4433 | bool FoundConstructor = false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4434 | unsigned FoundTQs; |
Aaron Ballman | e4de1a51 | 2015-07-21 22:33:52 +0000 | [diff] [blame] | 4435 | for (const auto *ND : Self.LookupConstructors(RD)) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4436 | // A template constructor is never a copy constructor. |
| 4437 | // FIXME: However, it may actually be selected at the actual overload |
| 4438 | // resolution point. |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4439 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4440 | continue; |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4441 | // UsingDecl itself is not a constructor |
| 4442 | if (isa<UsingDecl>(ND)) |
| 4443 | continue; |
| 4444 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4445 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 4446 | FoundConstructor = true; |
| 4447 | const FunctionProtoType *CPT |
| 4448 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4449 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4450 | if (!CPT) |
| 4451 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4452 | // TODO: check whether evaluating default arguments can throw. |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4453 | // For now, we'll be conservative and assume that they can throw. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4454 | if (!CPT->isNothrow(C) || CPT->getNumParams() > 1) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4455 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4456 | } |
| 4457 | } |
| 4458 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4459 | return FoundConstructor; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4460 | } |
| 4461 | return false; |
| 4462 | case UTT_HasNothrowConstructor: |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4463 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4464 | // If __has_trivial_constructor (type) is true then the trait is |
| 4465 | // true, else if type is a cv class or union type (or array |
| 4466 | // thereof) with a default constructor that is known not to |
| 4467 | // throw an exception then the trait is true, else it is false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4468 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4469 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4470 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
| 4471 | if (RD->hasTrivialDefaultConstructor() && |
| 4472 | !RD->hasNonTrivialDefaultConstructor()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4473 | return true; |
| 4474 | |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4475 | bool FoundConstructor = false; |
Aaron Ballman | e4de1a51 | 2015-07-21 22:33:52 +0000 | [diff] [blame] | 4476 | for (const auto *ND : Self.LookupConstructors(RD)) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4477 | // FIXME: In C++0x, a constructor template can be a default constructor. |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4478 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4479 | continue; |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4480 | // UsingDecl itself is not a constructor |
| 4481 | if (isa<UsingDecl>(ND)) |
| 4482 | continue; |
| 4483 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4484 | if (Constructor->isDefaultConstructor()) { |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4485 | FoundConstructor = true; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4486 | const FunctionProtoType *CPT |
| 4487 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4488 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4489 | if (!CPT) |
| 4490 | return false; |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4491 | // FIXME: check whether evaluating default arguments can throw. |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4492 | // For now, we'll be conservative and assume that they can throw. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4493 | if (!CPT->isNothrow(C) || CPT->getNumParams() > 0) |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4494 | return false; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4495 | } |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4496 | } |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4497 | return FoundConstructor; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4498 | } |
| 4499 | return false; |
| 4500 | case UTT_HasVirtualDestructor: |
| 4501 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4502 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 4503 | // then the trait is true, else it is false. |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4504 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
Sebastian Redl | 058fc82 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 4505 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4506 | return Destructor->isVirtual(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4507 | return false; |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4508 | |
| 4509 | // These type trait expressions are modeled on the specifications for the |
| 4510 | // Embarcadero C++0x type trait functions: |
| 4511 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
| 4512 | case UTT_IsCompleteType: |
| 4513 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): |
| 4514 | // Returns True if and only if T is a complete type at the point of the |
| 4515 | // function call. |
| 4516 | return !T->isIncompleteType(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4517 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4518 | } |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 4519 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4520 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
| 4521 | QualType RhsT, SourceLocation KeyLoc); |
| 4522 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4523 | static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc, |
| 4524 | ArrayRef<TypeSourceInfo *> Args, |
| 4525 | SourceLocation RParenLoc) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4526 | if (Kind <= UTT_Last) |
| 4527 | return EvaluateUnaryTypeTrait(S, Kind, KWLoc, Args[0]->getType()); |
| 4528 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4529 | if (Kind <= BTT_Last) |
| 4530 | return EvaluateBinaryTypeTrait(S, Kind, Args[0]->getType(), |
| 4531 | Args[1]->getType(), RParenLoc); |
| 4532 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4533 | switch (Kind) { |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4534 | case clang::TT_IsConstructible: |
| 4535 | case clang::TT_IsNothrowConstructible: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4536 | case clang::TT_IsTriviallyConstructible: { |
| 4537 | // C++11 [meta.unary.prop]: |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 4538 | // is_trivially_constructible is defined as: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4539 | // |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 4540 | // is_constructible<T, Args...>::value is true and the variable |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 4541 | // definition for is_constructible, as defined below, is known to call |
| 4542 | // no operation that is not trivial. |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4543 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4544 | // The predicate condition for a template specialization |
| 4545 | // is_constructible<T, Args...> shall be satisfied if and only if the |
| 4546 | // following variable definition would be well-formed for some invented |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4547 | // variable t: |
| 4548 | // |
| 4549 | // T t(create<Args>()...); |
Alp Toker | 40f9b1c | 2013-12-12 21:23:03 +0000 | [diff] [blame] | 4550 | assert(!Args.empty()); |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4551 | |
| 4552 | // Precondition: T and all types in the parameter pack Args shall be |
| 4553 | // complete types, (possibly cv-qualified) void, or arrays of |
| 4554 | // unknown bound. |
Aaron Ballman | 2bf2cad | 2015-07-21 21:18:29 +0000 | [diff] [blame] | 4555 | for (const auto *TSI : Args) { |
| 4556 | QualType ArgTy = TSI->getType(); |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4557 | if (ArgTy->isVoidType() || ArgTy->isIncompleteArrayType()) |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4558 | continue; |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4559 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4560 | if (S.RequireCompleteType(KWLoc, ArgTy, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4561 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4562 | return false; |
| 4563 | } |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4564 | |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4565 | // Make sure the first argument is not incomplete nor a function type. |
| 4566 | QualType T = Args[0]->getType(); |
| 4567 | if (T->isIncompleteType() || T->isFunctionType()) |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4568 | return false; |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4569 | |
Nikola Smiljanic | 1b4b6ba | 2014-04-15 11:30:15 +0000 | [diff] [blame] | 4570 | // Make sure the first argument is not an abstract type. |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4571 | CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
Nikola Smiljanic | 1b4b6ba | 2014-04-15 11:30:15 +0000 | [diff] [blame] | 4572 | if (RD && RD->isAbstract()) |
| 4573 | return false; |
| 4574 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4575 | SmallVector<OpaqueValueExpr, 2> OpaqueArgExprs; |
| 4576 | SmallVector<Expr *, 2> ArgExprs; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4577 | ArgExprs.reserve(Args.size() - 1); |
| 4578 | for (unsigned I = 1, N = Args.size(); I != N; ++I) { |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4579 | QualType ArgTy = Args[I]->getType(); |
| 4580 | if (ArgTy->isObjectType() || ArgTy->isFunctionType()) |
| 4581 | ArgTy = S.Context.getRValueReferenceType(ArgTy); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4582 | OpaqueArgExprs.push_back( |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4583 | OpaqueValueExpr(Args[I]->getTypeLoc().getLocStart(), |
| 4584 | ArgTy.getNonLValueExprType(S.Context), |
| 4585 | Expr::getValueKindForType(ArgTy))); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4586 | } |
Richard Smith | a507bfc | 2014-07-23 20:07:08 +0000 | [diff] [blame] | 4587 | for (Expr &E : OpaqueArgExprs) |
| 4588 | ArgExprs.push_back(&E); |
| 4589 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4590 | // Perform the initialization in an unevaluated context within a SFINAE |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4591 | // trap at translation unit scope. |
| 4592 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 4593 | Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true); |
| 4594 | Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl()); |
| 4595 | InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0])); |
| 4596 | InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc, |
| 4597 | RParenLoc)); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4598 | InitializationSequence Init(S, To, InitKind, ArgExprs); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4599 | if (Init.Failed()) |
| 4600 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4601 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4602 | ExprResult Result = Init.Perform(S, To, InitKind, ArgExprs); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4603 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 4604 | return false; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4605 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4606 | if (Kind == clang::TT_IsConstructible) |
| 4607 | return true; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4608 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4609 | if (Kind == clang::TT_IsNothrowConstructible) |
| 4610 | return S.canThrow(Result.get()) == CT_Cannot; |
| 4611 | |
| 4612 | if (Kind == clang::TT_IsTriviallyConstructible) { |
Brian Kelley | 93c640b | 2017-03-29 17:40:35 +0000 | [diff] [blame] | 4613 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
| 4614 | // Objective-C lifetime, this is a non-trivial construction. |
| 4615 | if (T.getNonReferenceType().hasNonTrivialObjCLifetime()) |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4616 | return false; |
| 4617 | |
| 4618 | // The initialization succeeded; now make sure there are no non-trivial |
| 4619 | // calls. |
| 4620 | return !Result.get()->hasNonTrivialCall(S.Context); |
| 4621 | } |
| 4622 | |
| 4623 | llvm_unreachable("unhandled type trait"); |
| 4624 | return false; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4625 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4626 | default: llvm_unreachable("not a TT"); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4627 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4628 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4629 | return false; |
| 4630 | } |
| 4631 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4632 | ExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 4633 | ArrayRef<TypeSourceInfo *> Args, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4634 | SourceLocation RParenLoc) { |
Alp Toker | 5294e6e | 2013-12-25 01:47:02 +0000 | [diff] [blame] | 4635 | QualType ResultType = Context.getLogicalOperationType(); |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4636 | |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4637 | if (Kind <= UTT_Last && !CheckUnaryTypeTraitTypeCompleteness( |
| 4638 | *this, Kind, KWLoc, Args[0]->getType())) |
| 4639 | return ExprError(); |
| 4640 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4641 | bool Dependent = false; |
| 4642 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 4643 | if (Args[I]->getType()->isDependentType()) { |
| 4644 | Dependent = true; |
| 4645 | break; |
| 4646 | } |
| 4647 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4648 | |
| 4649 | bool Result = false; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4650 | if (!Dependent) |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4651 | Result = evaluateTypeTrait(*this, Kind, KWLoc, Args, RParenLoc); |
| 4652 | |
| 4653 | return TypeTraitExpr::Create(Context, ResultType, KWLoc, Kind, Args, |
| 4654 | RParenLoc, Result); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
Alp Toker | 88f64e6 | 2013-12-13 21:19:30 +0000 | [diff] [blame] | 4657 | ExprResult Sema::ActOnTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 4658 | ArrayRef<ParsedType> Args, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4659 | SourceLocation RParenLoc) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4660 | SmallVector<TypeSourceInfo *, 4> ConvertedArgs; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4661 | ConvertedArgs.reserve(Args.size()); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4662 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4663 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 4664 | TypeSourceInfo *TInfo; |
| 4665 | QualType T = GetTypeFromParser(Args[I], &TInfo); |
| 4666 | if (!TInfo) |
| 4667 | TInfo = Context.getTrivialTypeSourceInfo(T, KWLoc); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4668 | |
| 4669 | ConvertedArgs.push_back(TInfo); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4670 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4671 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4672 | return BuildTypeTrait(Kind, KWLoc, ConvertedArgs, RParenLoc); |
| 4673 | } |
| 4674 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4675 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
| 4676 | QualType RhsT, SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4677 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
| 4678 | "Cannot evaluate traits of dependent types"); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4679 | |
| 4680 | switch(BTT) { |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4681 | case BTT_IsBaseOf: { |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4682 | // C++0x [meta.rel]p2 |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4683 | // 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] | 4684 | // Base and Derived are not unions and name the same class type without |
| 4685 | // regard to cv-qualifiers. |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4686 | |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4687 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
| 4688 | if (!lhsRecord) return false; |
| 4689 | |
| 4690 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
| 4691 | if (!rhsRecord) return false; |
| 4692 | |
| 4693 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
| 4694 | == (lhsRecord == rhsRecord)); |
| 4695 | |
| 4696 | if (lhsRecord == rhsRecord) |
| 4697 | return !lhsRecord->getDecl()->isUnion(); |
| 4698 | |
| 4699 | // C++0x [meta.rel]p2: |
| 4700 | // If Base and Derived are class types and are different types |
| 4701 | // (ignoring possible cv-qualifiers) then Derived shall be a |
| 4702 | // complete type. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4703 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4704 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4705 | return false; |
| 4706 | |
| 4707 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) |
| 4708 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); |
| 4709 | } |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4710 | case BTT_IsSame: |
| 4711 | return Self.Context.hasSameType(LhsT, RhsT); |
Francois Pichet | 34b2113 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 4712 | case BTT_TypeCompatible: |
| 4713 | return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(), |
| 4714 | RhsT.getUnqualifiedType()); |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4715 | case BTT_IsConvertible: |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4716 | case BTT_IsConvertibleTo: { |
| 4717 | // C++0x [meta.rel]p4: |
| 4718 | // Given the following function prototype: |
| 4719 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4720 | // template <class T> |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4721 | // typename add_rvalue_reference<T>::type create(); |
| 4722 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4723 | // the predicate condition for a template specialization |
| 4724 | // is_convertible<From, To> shall be satisfied if and only if |
| 4725 | // the return expression in the following code would be |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4726 | // well-formed, including any implicit conversions to the return |
| 4727 | // type of the function: |
| 4728 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4729 | // To test() { |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4730 | // return create<From>(); |
| 4731 | // } |
| 4732 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4733 | // Access checking is performed as if in a context unrelated to To and |
| 4734 | // From. Only the validity of the immediate context of the expression |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4735 | // of the return-statement (including conversions to the return type) |
| 4736 | // is considered. |
| 4737 | // |
| 4738 | // We model the initialization as a copy-initialization of a temporary |
| 4739 | // of the appropriate type, which for this expression is identical to the |
| 4740 | // return statement (since NRVO doesn't apply). |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4741 | |
| 4742 | // Functions aren't allowed to return function or array types. |
| 4743 | if (RhsT->isFunctionType() || RhsT->isArrayType()) |
| 4744 | return false; |
| 4745 | |
| 4746 | // A return statement in a void function must have void type. |
| 4747 | if (RhsT->isVoidType()) |
| 4748 | return LhsT->isVoidType(); |
| 4749 | |
| 4750 | // A function definition requires a complete, non-abstract return type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4751 | if (!Self.isCompleteType(KeyLoc, RhsT) || Self.isAbstractType(KeyLoc, RhsT)) |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4752 | return false; |
| 4753 | |
| 4754 | // Compute the result of add_rvalue_reference. |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4755 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 4756 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4757 | |
| 4758 | // Build a fake source and destination for initialization. |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4759 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 4760 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4761 | Expr::getValueKindForType(LhsT)); |
| 4762 | Expr *FromPtr = &From; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4763 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4764 | SourceLocation())); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4765 | |
| 4766 | // Perform the initialization in an unevaluated context within a SFINAE |
Eli Friedman | a59b190 | 2012-01-25 01:05:57 +0000 | [diff] [blame] | 4767 | // trap at translation unit scope. |
| 4768 | EnterExpressionEvaluationContext Unevaluated(Self, Sema::Unevaluated); |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 4769 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 4770 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4771 | InitializationSequence Init(Self, To, Kind, FromPtr); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 4772 | if (Init.Failed()) |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4773 | return false; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 4774 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4775 | ExprResult Result = Init.Perform(Self, To, Kind, FromPtr); |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4776 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); |
| 4777 | } |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4778 | |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 4779 | case BTT_IsAssignable: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4780 | case BTT_IsNothrowAssignable: |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4781 | case BTT_IsTriviallyAssignable: { |
| 4782 | // C++11 [meta.unary.prop]p3: |
| 4783 | // is_trivially_assignable is defined as: |
| 4784 | // is_assignable<T, U>::value is true and the assignment, as defined by |
| 4785 | // is_assignable, is known to call no operation that is not trivial |
| 4786 | // |
| 4787 | // is_assignable is defined as: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4788 | // The expression declval<T>() = declval<U>() is well-formed when |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4789 | // treated as an unevaluated operand (Clause 5). |
| 4790 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4791 | // For both, T and U shall be complete types, (possibly cv-qualified) |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4792 | // void, or arrays of unknown bound. |
| 4793 | if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() && |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4794 | Self.RequireCompleteType(KeyLoc, LhsT, |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4795 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4796 | return false; |
| 4797 | if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() && |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4798 | Self.RequireCompleteType(KeyLoc, RhsT, |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4799 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4800 | return false; |
| 4801 | |
| 4802 | // cv void is never assignable. |
| 4803 | if (LhsT->isVoidType() || RhsT->isVoidType()) |
| 4804 | return false; |
| 4805 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4806 | // Build expressions that emulate the effect of declval<T>() and |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4807 | // declval<U>(). |
| 4808 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 4809 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
| 4810 | if (RhsT->isObjectType() || RhsT->isFunctionType()) |
| 4811 | RhsT = Self.Context.getRValueReferenceType(RhsT); |
| 4812 | OpaqueValueExpr Lhs(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
| 4813 | Expr::getValueKindForType(LhsT)); |
| 4814 | OpaqueValueExpr Rhs(KeyLoc, RhsT.getNonLValueExprType(Self.Context), |
| 4815 | Expr::getValueKindForType(RhsT)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4816 | |
| 4817 | // Attempt the assignment in an unevaluated context within a SFINAE |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4818 | // trap at translation unit scope. |
| 4819 | EnterExpressionEvaluationContext Unevaluated(Self, Sema::Unevaluated); |
| 4820 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 4821 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4822 | ExprResult Result = Self.BuildBinOp(/*S=*/nullptr, KeyLoc, BO_Assign, &Lhs, |
| 4823 | &Rhs); |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4824 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 4825 | return false; |
| 4826 | |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 4827 | if (BTT == BTT_IsAssignable) |
| 4828 | return true; |
| 4829 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4830 | if (BTT == BTT_IsNothrowAssignable) |
| 4831 | return Self.canThrow(Result.get()) == CT_Cannot; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4832 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4833 | if (BTT == BTT_IsTriviallyAssignable) { |
Brian Kelley | 93c640b | 2017-03-29 17:40:35 +0000 | [diff] [blame] | 4834 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
| 4835 | // Objective-C lifetime, this is a non-trivial assignment. |
| 4836 | if (LhsT.getNonReferenceType().hasNonTrivialObjCLifetime()) |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4837 | return false; |
| 4838 | |
| 4839 | return !Result.get()->hasNonTrivialCall(Self.Context); |
| 4840 | } |
| 4841 | |
| 4842 | llvm_unreachable("unhandled type trait"); |
| 4843 | return false; |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4844 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4845 | default: llvm_unreachable("not a BTT"); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4846 | } |
| 4847 | llvm_unreachable("Unknown type trait or not implemented"); |
| 4848 | } |
| 4849 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4850 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
| 4851 | SourceLocation KWLoc, |
| 4852 | ParsedType Ty, |
| 4853 | Expr* DimExpr, |
| 4854 | SourceLocation RParen) { |
| 4855 | TypeSourceInfo *TSInfo; |
| 4856 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
| 4857 | if (!TSInfo) |
| 4858 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
| 4859 | |
| 4860 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
| 4861 | } |
| 4862 | |
| 4863 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
| 4864 | QualType T, Expr *DimExpr, |
| 4865 | SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4866 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4867 | |
| 4868 | switch(ATT) { |
| 4869 | case ATT_ArrayRank: |
| 4870 | if (T->isArrayType()) { |
| 4871 | unsigned Dim = 0; |
| 4872 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 4873 | ++Dim; |
| 4874 | T = AT->getElementType(); |
| 4875 | } |
| 4876 | return Dim; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4877 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4878 | return 0; |
| 4879 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4880 | case ATT_ArrayExtent: { |
| 4881 | llvm::APSInt Value; |
| 4882 | uint64_t Dim; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4883 | if (Self.VerifyIntegerConstantExpression(DimExpr, &Value, |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4884 | diag::err_dimension_expr_not_constant_integer, |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4885 | false).isInvalid()) |
| 4886 | return 0; |
| 4887 | if (Value.isSigned() && Value.isNegative()) { |
Daniel Dunbar | 900cead | 2012-03-09 21:38:22 +0000 | [diff] [blame] | 4888 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) |
| 4889 | << DimExpr->getSourceRange(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4890 | return 0; |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4891 | } |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4892 | Dim = Value.getLimitedValue(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4893 | |
| 4894 | if (T->isArrayType()) { |
| 4895 | unsigned D = 0; |
| 4896 | bool Matched = false; |
| 4897 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 4898 | if (Dim == D) { |
| 4899 | Matched = true; |
| 4900 | break; |
| 4901 | } |
| 4902 | ++D; |
| 4903 | T = AT->getElementType(); |
| 4904 | } |
| 4905 | |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4906 | if (Matched && T->isArrayType()) { |
| 4907 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
| 4908 | return CAT->getSize().getLimitedValue(); |
| 4909 | } |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4910 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4911 | return 0; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4912 | } |
| 4913 | } |
| 4914 | llvm_unreachable("Unknown type trait or not implemented"); |
| 4915 | } |
| 4916 | |
| 4917 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
| 4918 | SourceLocation KWLoc, |
| 4919 | TypeSourceInfo *TSInfo, |
| 4920 | Expr* DimExpr, |
| 4921 | SourceLocation RParen) { |
| 4922 | QualType T = TSInfo->getType(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4923 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 4924 | // FIXME: This should likely be tracked as an APInt to remove any host |
| 4925 | // assumptions about the width of size_t on the target. |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4926 | uint64_t Value = 0; |
| 4927 | if (!T->isDependentType()) |
| 4928 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); |
| 4929 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 4930 | // While the specification for these traits from the Embarcadero C++ |
| 4931 | // compiler's documentation says the return type is 'unsigned int', Clang |
| 4932 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero |
| 4933 | // compiler, there is no difference. On several other platforms this is an |
| 4934 | // important distinction. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4935 | return new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, DimExpr, |
| 4936 | RParen, Context.getSizeType()); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4939 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 4940 | SourceLocation KWLoc, |
| 4941 | Expr *Queried, |
| 4942 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4943 | // If error parsing the expression, ignore. |
| 4944 | if (!Queried) |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 4945 | return ExprError(); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4946 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 4947 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4948 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4949 | return Result; |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 4952 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
| 4953 | switch (ET) { |
| 4954 | case ET_IsLValueExpr: return E->isLValue(); |
| 4955 | case ET_IsRValueExpr: return E->isRValue(); |
| 4956 | } |
| 4957 | llvm_unreachable("Expression trait not covered by switch"); |
| 4958 | } |
| 4959 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4960 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 4961 | SourceLocation KWLoc, |
| 4962 | Expr *Queried, |
| 4963 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4964 | if (Queried->isTypeDependent()) { |
| 4965 | // Delay type-checking for type-dependent expressions. |
| 4966 | } else if (Queried->getType()->isPlaceholderType()) { |
| 4967 | ExprResult PE = CheckPlaceholderExpr(Queried); |
| 4968 | if (PE.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4969 | return BuildExpressionTrait(ET, KWLoc, PE.get(), RParen); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 4972 | bool Value = EvaluateExpressionTrait(ET, Queried); |
Chandler Carruth | f57eba3 | 2011-05-01 08:48:19 +0000 | [diff] [blame] | 4973 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4974 | return new (Context) |
| 4975 | ExpressionTraitExpr(KWLoc, ET, Queried, Value, RParen, Context.BoolTy); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4978 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4979 | ExprValueKind &VK, |
| 4980 | SourceLocation Loc, |
| 4981 | bool isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4982 | assert(!LHS.get()->getType()->isPlaceholderType() && |
| 4983 | !RHS.get()->getType()->isPlaceholderType() && |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 4984 | "placeholders should have been weeded out by now"); |
| 4985 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 4986 | // The LHS undergoes lvalue conversions if this is ->*, and undergoes the |
| 4987 | // temporary materialization conversion otherwise. |
| 4988 | if (isIndirect) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4989 | LHS = DefaultLvalueConversion(LHS.get()); |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 4990 | else if (LHS.get()->isRValue()) |
| 4991 | LHS = TemporaryMaterializationConversion(LHS.get()); |
| 4992 | if (LHS.isInvalid()) |
| 4993 | return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 4994 | |
| 4995 | // The RHS always undergoes lvalue conversions. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4996 | RHS = DefaultLvalueConversion(RHS.get()); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4997 | if (RHS.isInvalid()) return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 4998 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 4999 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 5000 | // C++ 5.5p2 |
| 5001 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 5002 | // be of type "pointer to member of T" (where T is a completely-defined |
| 5003 | // class type) [...] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5004 | QualType RHSType = RHS.get()->getType(); |
| 5005 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5006 | if (!MemPtr) { |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5007 | Diag(Loc, diag::err_bad_memptr_rhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5008 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5009 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5010 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5011 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5012 | QualType Class(MemPtr->getClass(), 0); |
| 5013 | |
Douglas Gregor | d07ba34 | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 5014 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
| 5015 | // member pointer points must be completely-defined. However, there is no |
| 5016 | // reason for this semantic distinction, and the rule is not enforced by |
| 5017 | // other compilers. Therefore, we do not check this property, as it is |
| 5018 | // likely to be considered a defect. |
Sebastian Redl | c72350e | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 5019 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5020 | // C++ 5.5p2 |
| 5021 | // [...] to its first operand, which shall be of class T or of a class of |
| 5022 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 5023 | // such a class] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5024 | QualType LHSType = LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5025 | if (isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5026 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) |
| 5027 | LHSType = Ptr->getPointeeType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5028 | else { |
| 5029 | Diag(Loc, diag::err_bad_memptr_lhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5030 | << OpSpelling << 1 << LHSType |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5031 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5032 | return QualType(); |
| 5033 | } |
| 5034 | } |
| 5035 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5036 | if (!Context.hasSameUnqualifiedType(Class, LHSType)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 5037 | // If we want to check the hierarchy, we need a complete type. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5038 | if (RequireCompleteType(Loc, LHSType, diag::err_bad_memptr_lhs, |
| 5039 | OpSpelling, (int)isIndirect)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 5040 | return QualType(); |
| 5041 | } |
Richard Smith | db05cd3 | 2013-12-12 03:40:18 +0000 | [diff] [blame] | 5042 | |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5043 | if (!IsDerivedFrom(Loc, LHSType, Class)) { |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5044 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5045 | << (int)isIndirect << LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5046 | return QualType(); |
| 5047 | } |
Richard Smith | db05cd3 | 2013-12-12 03:40:18 +0000 | [diff] [blame] | 5048 | |
| 5049 | CXXCastPath BasePath; |
| 5050 | if (CheckDerivedToBaseConversion(LHSType, Class, Loc, |
| 5051 | SourceRange(LHS.get()->getLocStart(), |
| 5052 | RHS.get()->getLocEnd()), |
| 5053 | &BasePath)) |
| 5054 | return QualType(); |
| 5055 | |
Eli Friedman | 1fcf66b | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 5056 | // Cast LHS to type of use. |
| 5057 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5058 | ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5059 | LHS = ImpCastExprToType(LHS.get(), UseType, CK_DerivedToBase, VK, |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5060 | &BasePath); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5063 | if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) { |
Fariborz Jahanian | 1bc0f9a | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 5064 | // Diagnose use of pointer-to-member type which when used as |
| 5065 | // the functional cast in a pointer-to-member expression. |
| 5066 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 5067 | return QualType(); |
| 5068 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5069 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5070 | // C++ 5.5p2 |
| 5071 | // The result is an object or a function of the type specified by the |
| 5072 | // second operand. |
| 5073 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 5074 | // in accordance with 5.5p5 and 5.2.5. |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5075 | QualType Result = MemPtr->getPointeeType(); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5076 | Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5077 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5078 | // C++0x [expr.mptr.oper]p6: |
| 5079 | // In a .* expression whose object expression is an rvalue, the program is |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5080 | // ill-formed if the second operand is a pointer to member function with |
| 5081 | // ref-qualifier &. In a ->* expression or in a .* expression whose object |
| 5082 | // 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] | 5083 | // is a pointer to member function with ref-qualifier &&. |
| 5084 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
| 5085 | switch (Proto->getRefQualifier()) { |
| 5086 | case RQ_None: |
| 5087 | // Do nothing |
| 5088 | break; |
| 5089 | |
| 5090 | case RQ_LValue: |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5091 | if (!isIndirect && !LHS.get()->Classify(Context).isLValue()) |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5092 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5093 | << RHSType << 1 << LHS.get()->getSourceRange(); |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5094 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5095 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5096 | case RQ_RValue: |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5097 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5098 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5099 | << RHSType << 0 << LHS.get()->getSourceRange(); |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5100 | break; |
| 5101 | } |
| 5102 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5103 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5104 | // C++ [expr.mptr.oper]p6: |
| 5105 | // The result of a .* expression whose second operand is a pointer |
| 5106 | // to a data member is of the same value category as its |
| 5107 | // first operand. The result of a .* expression whose second |
| 5108 | // operand is a pointer to a member function is a prvalue. The |
| 5109 | // result of an ->* expression is an lvalue if its second operand |
| 5110 | // is a pointer to data member and a prvalue otherwise. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5111 | if (Result->isFunctionType()) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5112 | VK = VK_RValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5113 | return Context.BoundMemberTy; |
| 5114 | } else if (isIndirect) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5115 | VK = VK_LValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5116 | } else { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5117 | VK = LHS.get()->getValueKind(); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5118 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5119 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5120 | return Result; |
| 5121 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5122 | |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5123 | /// \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] | 5124 | /// |
| 5125 | /// This is part of the parameter validation for the ? operator. If either |
| 5126 | /// value operand is a class type, the two operands are attempted to be |
| 5127 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5128 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 5129 | /// as such. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5130 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 5131 | SourceLocation QuestionLoc, |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5132 | bool &HaveConversion, |
| 5133 | QualType &ToType) { |
| 5134 | HaveConversion = false; |
| 5135 | ToType = To->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5136 | |
| 5137 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5138 | SourceLocation()); |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5139 | // C++11 5.16p3 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5140 | // The process for determining whether an operand expression E1 of type T1 |
| 5141 | // can be converted to match an operand expression E2 of type T2 is defined |
| 5142 | // as follows: |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5143 | // -- If E2 is an lvalue: E1 can be converted to match E2 if E1 can be |
| 5144 | // implicitly converted to type "lvalue reference to T2", subject to the |
| 5145 | // constraint that in the conversion the reference must bind directly to |
| 5146 | // an lvalue. |
| 5147 | // -- If E2 is an xvalue: E1 can be converted to match E2 if E1 can be |
| 5148 | // implicitly conveted to the type "rvalue reference to R2", subject to |
| 5149 | // the constraint that the reference must bind directly. |
| 5150 | if (To->isLValue() || To->isXValue()) { |
| 5151 | QualType T = To->isLValue() ? Self.Context.getLValueReferenceType(ToType) |
| 5152 | : Self.Context.getRValueReferenceType(ToType); |
| 5153 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5154 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5155 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5156 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5157 | if (InitSeq.isDirectReferenceBinding()) { |
| 5158 | ToType = T; |
| 5159 | HaveConversion = true; |
| 5160 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5161 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5162 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5163 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5164 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5165 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5166 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5167 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 5168 | // -- if E1 and E2 have class type, and the underlying class types are |
| 5169 | // the same or one is a base class of the other: |
| 5170 | QualType FTy = From->getType(); |
| 5171 | QualType TTy = To->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5172 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 5173 | const RecordType *TRec = TTy->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5174 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5175 | Self.IsDerivedFrom(QuestionLoc, FTy, TTy); |
| 5176 | if (FRec && TRec && (FRec == TRec || FDerivedFromT || |
| 5177 | Self.IsDerivedFrom(QuestionLoc, TTy, FTy))) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5178 | // E1 can be converted to match E2 if the class of T2 is the |
| 5179 | // same type as, or a base class of, the class of T1, and |
| 5180 | // [cv2 > cv1]. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5181 | if (FRec == TRec || FDerivedFromT) { |
| 5182 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5183 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5184 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5185 | if (InitSeq) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5186 | HaveConversion = true; |
| 5187 | return false; |
| 5188 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5189 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5190 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5191 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5192 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5193 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5194 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5195 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5196 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5197 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5198 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 5199 | // implicitly converted to the type that expression E2 would have |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5200 | // 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] | 5201 | // an rvalue). |
| 5202 | // |
| 5203 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 5204 | // to the array-to-pointer or function-to-pointer conversions. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5205 | TTy = TTy.getNonLValueExprType(Self.Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5206 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5207 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5208 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5209 | HaveConversion = !InitSeq.Failed(); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5210 | ToType = TTy; |
| 5211 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5212 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5213 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5214 | return false; |
| 5215 | } |
| 5216 | |
| 5217 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 5218 | /// |
| 5219 | /// This is part of the parameter validation for the ? operator. If either |
| 5220 | /// value operand is a class type, overload resolution is used to find a |
| 5221 | /// conversion to a common type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5222 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5223 | SourceLocation QuestionLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5224 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5225 | OverloadCandidateSet CandidateSet(QuestionLoc, |
| 5226 | OverloadCandidateSet::CSK_Operator); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 5227 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5228 | CandidateSet); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5229 | |
| 5230 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5231 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5232 | case OR_Success: { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5233 | // We found a match. Perform the conversions on the arguments and move on. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5234 | ExprResult LHSRes = |
| 5235 | Self.PerformImplicitConversion(LHS.get(), Best->BuiltinTypes.ParamTypes[0], |
| 5236 | Best->Conversions[0], Sema::AA_Converting); |
| 5237 | if (LHSRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5238 | break; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5239 | LHS = LHSRes; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5240 | |
| 5241 | ExprResult RHSRes = |
| 5242 | Self.PerformImplicitConversion(RHS.get(), Best->BuiltinTypes.ParamTypes[1], |
| 5243 | Best->Conversions[1], Sema::AA_Converting); |
| 5244 | if (RHSRes.isInvalid()) |
| 5245 | break; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5246 | RHS = RHSRes; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 5247 | if (Best->Function) |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5248 | Self.MarkFunctionReferenced(QuestionLoc, Best->Function); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5249 | return false; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5250 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5251 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5252 | case OR_No_Viable_Function: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5253 | |
| 5254 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5255 | // constant and the other is a pointer type. In this case, the user most |
| 5256 | // likely forgot to take the address of the other expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5257 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5258 | return true; |
| 5259 | |
| 5260 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5261 | << LHS.get()->getType() << RHS.get()->getType() |
| 5262 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5263 | return true; |
| 5264 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5265 | case OR_Ambiguous: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5266 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5267 | << LHS.get()->getType() << RHS.get()->getType() |
| 5268 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5269 | // FIXME: Print the possible common types by printing the return types of |
| 5270 | // the viable candidates. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5271 | break; |
| 5272 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5273 | case OR_Deleted: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5274 | llvm_unreachable("Conditional operator has only built-in overloads"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5275 | } |
| 5276 | return true; |
| 5277 | } |
| 5278 | |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5279 | /// \brief Perform an "extended" implicit conversion as returned by |
| 5280 | /// TryClassUnification. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5281 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5282 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5283 | InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5284 | SourceLocation()); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5285 | Expr *Arg = E.get(); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5286 | InitializationSequence InitSeq(Self, Entity, Kind, Arg); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5287 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, Arg); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5288 | if (Result.isInvalid()) |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5289 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5290 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5291 | E = Result; |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5292 | return false; |
| 5293 | } |
| 5294 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5295 | /// \brief Check the operands of ?: under C++ semantics. |
| 5296 | /// |
| 5297 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 5298 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5299 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 5300 | ExprResult &RHS, ExprValueKind &VK, |
| 5301 | ExprObjectKind &OK, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5302 | SourceLocation QuestionLoc) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5303 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 5304 | // interface pointers. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5305 | |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5306 | // C++11 [expr.cond]p1 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5307 | // The first expression is contextually converted to bool. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5308 | if (!Cond.get()->isTypeDependent()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5309 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5310 | if (CondRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5311 | return QualType(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5312 | Cond = CondRes; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5313 | } |
| 5314 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5315 | // Assume r-value. |
| 5316 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5317 | OK = OK_Ordinary; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5318 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5319 | // Either of the arguments dependent? |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5320 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5321 | return Context.DependentTy; |
| 5322 | |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5323 | // C++11 [expr.cond]p2 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5324 | // If either the second or the third operand has type (cv) void, ... |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5325 | QualType LTy = LHS.get()->getType(); |
| 5326 | QualType RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5327 | bool LVoid = LTy->isVoidType(); |
| 5328 | bool RVoid = RTy->isVoidType(); |
| 5329 | if (LVoid || RVoid) { |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 5330 | // ... one of the following shall hold: |
| 5331 | // -- The second or the third operand (but not both) is a (possibly |
| 5332 | // parenthesized) throw-expression; the result is of the type |
| 5333 | // and value category of the other. |
| 5334 | bool LThrow = isa<CXXThrowExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 5335 | bool RThrow = isa<CXXThrowExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 5336 | if (LThrow != RThrow) { |
| 5337 | Expr *NonThrow = LThrow ? RHS.get() : LHS.get(); |
| 5338 | VK = NonThrow->getValueKind(); |
| 5339 | // DR (no number yet): the result is a bit-field if the |
| 5340 | // non-throw-expression operand is a bit-field. |
| 5341 | OK = NonThrow->getObjectKind(); |
| 5342 | return NonThrow->getType(); |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5343 | } |
| 5344 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5345 | // -- 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] | 5346 | // type void and is a prvalue. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5347 | if (LVoid && RVoid) |
| 5348 | return Context.VoidTy; |
| 5349 | |
| 5350 | // Neither holds, error. |
| 5351 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 5352 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5353 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5354 | return QualType(); |
| 5355 | } |
| 5356 | |
| 5357 | // Neither is void. |
| 5358 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5359 | // C++11 [expr.cond]p3 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5360 | // Otherwise, if the second and third operand have different types, and |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5361 | // either has (cv) class type [...] an attempt is made to convert each of |
| 5362 | // those operands to the type of the other. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5363 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5364 | (LTy->isRecordType() || RTy->isRecordType())) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5365 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5366 | QualType L2RType, R2LType; |
| 5367 | bool HaveL2R, HaveR2L; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5368 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5369 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5370 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5371 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5372 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5373 | // If both can be converted, [...] the program is ill-formed. |
| 5374 | if (HaveL2R && HaveR2L) { |
| 5375 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5376 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5377 | return QualType(); |
| 5378 | } |
| 5379 | |
| 5380 | // If exactly one conversion is possible, that conversion is applied to |
| 5381 | // the chosen operand and the converted operands are used in place of the |
| 5382 | // original operands for the remainder of this section. |
| 5383 | if (HaveL2R) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5384 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5385 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5386 | LTy = LHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5387 | } else if (HaveR2L) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5388 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5389 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5390 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5391 | } |
| 5392 | } |
| 5393 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5394 | // C++11 [expr.cond]p3 |
| 5395 | // if both are glvalues of the same value category and the same type except |
| 5396 | // for cv-qualification, an attempt is made to convert each of those |
| 5397 | // operands to the type of the other. |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5398 | // FIXME: |
| 5399 | // Resolving a defect in P0012R1: we extend this to cover all cases where |
| 5400 | // one of the operands is reference-compatible with the other, in order |
| 5401 | // to support conditionals between functions differing in noexcept. |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5402 | ExprValueKind LVK = LHS.get()->getValueKind(); |
| 5403 | ExprValueKind RVK = RHS.get()->getValueKind(); |
| 5404 | if (!Context.hasSameType(LTy, RTy) && |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5405 | LVK == RVK && LVK != VK_RValue) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5406 | // DerivedToBase was already handled by the class-specific case above. |
| 5407 | // FIXME: Should we allow ObjC conversions here? |
| 5408 | bool DerivedToBase, ObjCConversion, ObjCLifetimeConversion; |
| 5409 | if (CompareReferenceRelationship( |
| 5410 | QuestionLoc, LTy, RTy, DerivedToBase, |
| 5411 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5412 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
| 5413 | // [...] subject to the constraint that the reference must bind |
| 5414 | // directly [...] |
| 5415 | !RHS.get()->refersToBitField() && |
| 5416 | !RHS.get()->refersToVectorElement()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5417 | RHS = ImpCastExprToType(RHS.get(), LTy, CK_NoOp, RVK); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5418 | RTy = RHS.get()->getType(); |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5419 | } else if (CompareReferenceRelationship( |
| 5420 | QuestionLoc, RTy, LTy, DerivedToBase, |
| 5421 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5422 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
| 5423 | !LHS.get()->refersToBitField() && |
| 5424 | !LHS.get()->refersToVectorElement()) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5425 | LHS = ImpCastExprToType(LHS.get(), RTy, CK_NoOp, LVK); |
| 5426 | LTy = LHS.get()->getType(); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5427 | } |
| 5428 | } |
| 5429 | |
| 5430 | // C++11 [expr.cond]p4 |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5431 | // If the second and third operands are glvalues of the same value |
| 5432 | // category and have the same type, the result is of that type and |
| 5433 | // value category and it is a bit-field if the second or the third |
| 5434 | // operand is a bit-field, or if both are bit-fields. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5435 | // We only extend this to bitfields, not to the crazy other kinds of |
| 5436 | // l-values. |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 5437 | bool Same = Context.hasSameType(LTy, RTy); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5438 | if (Same && LVK == RVK && LVK != VK_RValue && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5439 | LHS.get()->isOrdinaryOrBitFieldObject() && |
| 5440 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
| 5441 | VK = LHS.get()->getValueKind(); |
| 5442 | if (LHS.get()->getObjectKind() == OK_BitField || |
| 5443 | RHS.get()->getObjectKind() == OK_BitField) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5444 | OK = OK_BitField; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5445 | |
| 5446 | // If we have function pointer types, unify them anyway to unify their |
| 5447 | // exception specifications, if any. |
| 5448 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
| 5449 | Qualifiers Qs = LTy.getQualifiers(); |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5450 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5451 | /*ConvertArgs*/false); |
| 5452 | LTy = Context.getQualifiedType(LTy, Qs); |
| 5453 | |
| 5454 | assert(!LTy.isNull() && "failed to find composite pointer type for " |
| 5455 | "canonically equivalent function ptr types"); |
| 5456 | assert(Context.hasSameType(LTy, RTy) && "bad composite pointer type"); |
| 5457 | } |
| 5458 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5459 | return LTy; |
Fariborz Jahanian | c60da03 | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 5460 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5461 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5462 | // C++11 [expr.cond]p5 |
| 5463 | // Otherwise, the result is a prvalue. If the second and third operands |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5464 | // do not have the same type, and either has (cv) class type, ... |
| 5465 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 5466 | // ... overload resolution is used to determine the conversions (if any) |
| 5467 | // to be applied to the operands. If the overload resolution fails, the |
| 5468 | // program is ill-formed. |
| 5469 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 5470 | return QualType(); |
| 5471 | } |
| 5472 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5473 | // C++11 [expr.cond]p6 |
| 5474 | // Lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5475 | // conversions are performed on the second and third operands. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5476 | LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); |
| 5477 | RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5478 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5479 | return QualType(); |
| 5480 | LTy = LHS.get()->getType(); |
| 5481 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5482 | |
| 5483 | // After those conversions, one of the following shall hold: |
| 5484 | // -- The second and third operands have the same type; the result |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5485 | // is of that type. If the operands have class type, the result |
| 5486 | // is a prvalue temporary of the result type, which is |
| 5487 | // copy-initialized from either the second operand or the third |
| 5488 | // operand depending on the value of the first operand. |
| 5489 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 5490 | if (LTy->isRecordType()) { |
| 5491 | // The operands have class type. Make a temporary copy. |
| 5492 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
David Blaikie | 6154ef9 | 2012-09-10 22:05:41 +0000 | [diff] [blame] | 5493 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5494 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
| 5495 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5496 | LHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5497 | if (LHSCopy.isInvalid()) |
| 5498 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5499 | |
| 5500 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
| 5501 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5502 | RHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5503 | if (RHSCopy.isInvalid()) |
| 5504 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5505 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5506 | LHS = LHSCopy; |
| 5507 | RHS = RHSCopy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5508 | } |
| 5509 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5510 | // If we have function pointer types, unify them anyway to unify their |
| 5511 | // exception specifications, if any. |
| 5512 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
| 5513 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS); |
| 5514 | assert(!LTy.isNull() && "failed to find composite pointer type for " |
| 5515 | "canonically equivalent function ptr types"); |
| 5516 | } |
| 5517 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5518 | return LTy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5519 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5520 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 5521 | // Extension: conditional operator involving vector types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5522 | if (LTy->isVectorType() || RTy->isVectorType()) |
Ulrich Weigand | 3c5038a | 2015-07-30 14:08:36 +0000 | [diff] [blame] | 5523 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false, |
| 5524 | /*AllowBothBool*/true, |
| 5525 | /*AllowBoolConversions*/false); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 5526 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5527 | // -- The second and third operands have arithmetic or enumeration type; |
| 5528 | // the usual arithmetic conversions are performed to bring them to a |
| 5529 | // common type, and the result is of that type. |
| 5530 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 5531 | QualType ResTy = UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5532 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5533 | return QualType(); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5534 | if (ResTy.isNull()) { |
| 5535 | Diag(QuestionLoc, |
| 5536 | diag::err_typecheck_cond_incompatible_operands) << LTy << RTy |
| 5537 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5538 | return QualType(); |
| 5539 | } |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 5540 | |
| 5541 | LHS = ImpCastExprToType(LHS.get(), ResTy, PrepareScalarCast(LHS, ResTy)); |
| 5542 | RHS = ImpCastExprToType(RHS.get(), ResTy, PrepareScalarCast(RHS, ResTy)); |
| 5543 | |
| 5544 | return ResTy; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
| 5547 | // -- The second and third operands have pointer type, or one has pointer |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5548 | // type and the other is a null pointer constant, or both are null |
| 5549 | // pointer constants, at least one of which is non-integral; pointer |
| 5550 | // conversions and qualification conversions are performed to bring them |
| 5551 | // to their composite pointer type. The result is of the composite |
| 5552 | // pointer type. |
Eli Friedman | 81390df | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 5553 | // -- The second and third operands have pointer to member type, or one has |
| 5554 | // pointer to member type and the other is a null pointer constant; |
| 5555 | // pointer to member conversions and qualification conversions are |
| 5556 | // performed to bring them to a common type, whose cv-qualification |
| 5557 | // shall match the cv-qualification of either the second or the third |
| 5558 | // operand. The result is of the common type. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5559 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS); |
| 5560 | if (!Composite.isNull()) |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5561 | return Composite; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5562 | |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 5563 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 798d2bd | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 5564 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 5565 | if (!Composite.isNull()) |
| 5566 | return Composite; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5567 | |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 5568 | // Check if we are using a null with a non-pointer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5569 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 5570 | return QualType(); |
| 5571 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5572 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5573 | << LHS.get()->getType() << RHS.get()->getType() |
| 5574 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5575 | return QualType(); |
| 5576 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5577 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5578 | static FunctionProtoType::ExceptionSpecInfo |
| 5579 | mergeExceptionSpecs(Sema &S, FunctionProtoType::ExceptionSpecInfo ESI1, |
| 5580 | FunctionProtoType::ExceptionSpecInfo ESI2, |
| 5581 | SmallVectorImpl<QualType> &ExceptionTypeStorage) { |
| 5582 | ExceptionSpecificationType EST1 = ESI1.Type; |
| 5583 | ExceptionSpecificationType EST2 = ESI2.Type; |
| 5584 | |
| 5585 | // If either of them can throw anything, that is the result. |
| 5586 | if (EST1 == EST_None) return ESI1; |
| 5587 | if (EST2 == EST_None) return ESI2; |
| 5588 | if (EST1 == EST_MSAny) return ESI1; |
| 5589 | if (EST2 == EST_MSAny) return ESI2; |
| 5590 | |
| 5591 | // If either of them is non-throwing, the result is the other. |
| 5592 | if (EST1 == EST_DynamicNone) return ESI2; |
| 5593 | if (EST2 == EST_DynamicNone) return ESI1; |
| 5594 | if (EST1 == EST_BasicNoexcept) return ESI2; |
| 5595 | if (EST2 == EST_BasicNoexcept) return ESI1; |
| 5596 | |
| 5597 | // If either of them is a non-value-dependent computed noexcept, that |
| 5598 | // determines the result. |
| 5599 | if (EST2 == EST_ComputedNoexcept && ESI2.NoexceptExpr && |
| 5600 | !ESI2.NoexceptExpr->isValueDependent()) |
| 5601 | return !ESI2.NoexceptExpr->EvaluateKnownConstInt(S.Context) ? ESI2 : ESI1; |
| 5602 | if (EST1 == EST_ComputedNoexcept && ESI1.NoexceptExpr && |
| 5603 | !ESI1.NoexceptExpr->isValueDependent()) |
| 5604 | return !ESI1.NoexceptExpr->EvaluateKnownConstInt(S.Context) ? ESI1 : ESI2; |
| 5605 | // If we're left with value-dependent computed noexcept expressions, we're |
| 5606 | // stuck. Before C++17, we can just drop the exception specification entirely, |
| 5607 | // since it's not actually part of the canonical type. And this should never |
| 5608 | // happen in C++17, because it would mean we were computing the composite |
| 5609 | // pointer type of dependent types, which should never happen. |
| 5610 | if (EST1 == EST_ComputedNoexcept || EST2 == EST_ComputedNoexcept) { |
| 5611 | assert(!S.getLangOpts().CPlusPlus1z && |
| 5612 | "computing composite pointer type of dependent types"); |
| 5613 | return FunctionProtoType::ExceptionSpecInfo(); |
| 5614 | } |
| 5615 | |
| 5616 | // Switch over the possibilities so that people adding new values know to |
| 5617 | // update this function. |
| 5618 | switch (EST1) { |
| 5619 | case EST_None: |
| 5620 | case EST_DynamicNone: |
| 5621 | case EST_MSAny: |
| 5622 | case EST_BasicNoexcept: |
| 5623 | case EST_ComputedNoexcept: |
| 5624 | llvm_unreachable("handled above"); |
| 5625 | |
| 5626 | case EST_Dynamic: { |
| 5627 | // This is the fun case: both exception specifications are dynamic. Form |
| 5628 | // the union of the two lists. |
| 5629 | assert(EST2 == EST_Dynamic && "other cases should already be handled"); |
| 5630 | llvm::SmallPtrSet<QualType, 8> Found; |
| 5631 | for (auto &Exceptions : {ESI1.Exceptions, ESI2.Exceptions}) |
| 5632 | for (QualType E : Exceptions) |
| 5633 | if (Found.insert(S.Context.getCanonicalType(E)).second) |
| 5634 | ExceptionTypeStorage.push_back(E); |
| 5635 | |
| 5636 | FunctionProtoType::ExceptionSpecInfo Result(EST_Dynamic); |
| 5637 | Result.Exceptions = ExceptionTypeStorage; |
| 5638 | return Result; |
| 5639 | } |
| 5640 | |
| 5641 | case EST_Unevaluated: |
| 5642 | case EST_Uninstantiated: |
| 5643 | case EST_Unparsed: |
| 5644 | llvm_unreachable("shouldn't see unresolved exception specifications here"); |
| 5645 | } |
| 5646 | |
| 5647 | llvm_unreachable("invalid ExceptionSpecificationType"); |
| 5648 | } |
| 5649 | |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5650 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 5651 | /// |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5652 | /// 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] | 5653 | /// 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] | 5654 | /// type and returns it. |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5655 | /// It does not emit diagnostics. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5656 | /// |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5657 | /// \param Loc The location of the operator requiring these two expressions to |
| 5658 | /// be converted to the composite pointer type. |
| 5659 | /// |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5660 | /// \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] | 5661 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5662 | Expr *&E1, Expr *&E2, |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5663 | bool ConvertArgs) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5664 | assert(getLangOpts().CPlusPlus && "This function assumes C++"); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5665 | |
| 5666 | // C++1z [expr]p14: |
| 5667 | // The composite pointer type of two operands p1 and p2 having types T1 |
| 5668 | // and T2 |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5669 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5670 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5671 | // where at least one is a pointer or pointer to member type or |
| 5672 | // std::nullptr_t is: |
| 5673 | bool T1IsPointerLike = T1->isAnyPointerType() || T1->isMemberPointerType() || |
| 5674 | T1->isNullPtrType(); |
| 5675 | bool T2IsPointerLike = T2->isAnyPointerType() || T2->isMemberPointerType() || |
| 5676 | T2->isNullPtrType(); |
| 5677 | if (!T1IsPointerLike && !T2IsPointerLike) |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5678 | return QualType(); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5679 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5680 | // - if both p1 and p2 are null pointer constants, std::nullptr_t; |
| 5681 | // This can't actually happen, following the standard, but we also use this |
| 5682 | // to implement the end of [expr.conv], which hits this case. |
| 5683 | // |
| 5684 | // - if either p1 or p2 is a null pointer constant, T2 or T1, respectively; |
| 5685 | if (T1IsPointerLike && |
| 5686 | E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5687 | if (ConvertArgs) |
| 5688 | E2 = ImpCastExprToType(E2, T1, T1->isMemberPointerType() |
| 5689 | ? CK_NullToMemberPointer |
| 5690 | : CK_NullToPointer).get(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5691 | return T1; |
| 5692 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5693 | if (T2IsPointerLike && |
| 5694 | E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5695 | if (ConvertArgs) |
| 5696 | E1 = ImpCastExprToType(E1, T2, T2->isMemberPointerType() |
| 5697 | ? CK_NullToMemberPointer |
| 5698 | : CK_NullToPointer).get(); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5699 | return T2; |
| 5700 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5701 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5702 | // Now both have to be pointers or member pointers. |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5703 | if (!T1IsPointerLike || !T2IsPointerLike) |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5704 | return QualType(); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5705 | assert(!T1->isNullPtrType() && !T2->isNullPtrType() && |
| 5706 | "nullptr_t should be a null pointer constant"); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5707 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5708 | // - if T1 or T2 is "pointer to cv1 void" and the other type is |
| 5709 | // "pointer to cv2 T", "pointer to cv12 void", where cv12 is |
| 5710 | // the union of cv1 and cv2; |
| 5711 | // - if T1 or T2 is "pointer to noexcept function" and the other type is |
| 5712 | // "pointer to function", where the function types are otherwise the same, |
| 5713 | // "pointer to function"; |
| 5714 | // FIXME: This rule is defective: it should also permit removing noexcept |
| 5715 | // from a pointer to member function. As a Clang extension, we also |
| 5716 | // permit removing 'noreturn', so we generalize this rule to; |
| 5717 | // - [Clang] If T1 and T2 are both of type "pointer to function" or |
| 5718 | // "pointer to member function" and the pointee types can be unified |
| 5719 | // by a function pointer conversion, that conversion is applied |
| 5720 | // before checking the following rules. |
| 5721 | // - if T1 is "pointer to cv1 C1" and T2 is "pointer to cv2 C2", where C1 |
| 5722 | // is reference-related to C2 or C2 is reference-related to C1 (8.6.3), |
| 5723 | // the cv-combined type of T1 and T2 or the cv-combined type of T2 and T1, |
| 5724 | // respectively; |
| 5725 | // - if T1 is "pointer to member of C1 of type cv1 U1" and T2 is "pointer |
| 5726 | // to member of C2 of type cv2 U2" where C1 is reference-related to C2 or |
| 5727 | // C2 is reference-related to C1 (8.6.3), the cv-combined type of T2 and |
| 5728 | // T1 or the cv-combined type of T1 and T2, respectively; |
| 5729 | // - if T1 and T2 are similar types (4.5), the cv-combined type of T1 and |
| 5730 | // T2; |
| 5731 | // |
| 5732 | // If looked at in the right way, these bullets all do the same thing. |
| 5733 | // What we do here is, we build the two possible cv-combined types, and try |
| 5734 | // the conversions in both directions. If only one works, or if the two |
| 5735 | // composite types are the same, we have succeeded. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5736 | // FIXME: extended qualifiers? |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5737 | // |
| 5738 | // Note that this will fail to find a composite pointer type for "pointer |
| 5739 | // to void" and "pointer to function". We can't actually perform the final |
| 5740 | // conversion in this case, even though a composite pointer type formally |
| 5741 | // exists. |
| 5742 | SmallVector<unsigned, 4> QualifierUnion; |
| 5743 | SmallVector<std::pair<const Type *, const Type *>, 4> MemberOfClass; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5744 | QualType Composite1 = T1; |
| 5745 | QualType Composite2 = T2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | unsigned NeedConstBefore = 0; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5747 | while (true) { |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5748 | const PointerType *Ptr1, *Ptr2; |
| 5749 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 5750 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 5751 | Composite1 = Ptr1->getPointeeType(); |
| 5752 | Composite2 = Ptr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5753 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5754 | // 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] | 5755 | // of where we need to fill in additional 'const' qualifiers. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5756 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5757 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5758 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5759 | QualifierUnion.push_back( |
| 5760 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5761 | MemberOfClass.push_back(std::make_pair(nullptr, nullptr)); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5762 | continue; |
| 5763 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5764 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5765 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 5766 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 5767 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 5768 | Composite1 = MemPtr1->getPointeeType(); |
| 5769 | Composite2 = MemPtr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5770 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5771 | // 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] | 5772 | // of where we need to fill in additional 'const' qualifiers. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5773 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5774 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5775 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5776 | QualifierUnion.push_back( |
| 5777 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 5778 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 5779 | MemPtr2->getClass())); |
| 5780 | continue; |
| 5781 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5782 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5783 | // FIXME: block pointer types? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5784 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5785 | // Cannot unwrap any more types. |
| 5786 | break; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5787 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5788 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5789 | // Apply the function pointer conversion to unify the types. We've already |
| 5790 | // unwrapped down to the function types, and we want to merge rather than |
| 5791 | // just convert, so do this ourselves rather than calling |
| 5792 | // IsFunctionConversion. |
| 5793 | // |
| 5794 | // FIXME: In order to match the standard wording as closely as possible, we |
| 5795 | // currently only do this under a single level of pointers. Ideally, we would |
| 5796 | // allow this in general, and set NeedConstBefore to the relevant depth on |
| 5797 | // the side(s) where we changed anything. |
| 5798 | if (QualifierUnion.size() == 1) { |
| 5799 | if (auto *FPT1 = Composite1->getAs<FunctionProtoType>()) { |
| 5800 | if (auto *FPT2 = Composite2->getAs<FunctionProtoType>()) { |
| 5801 | FunctionProtoType::ExtProtoInfo EPI1 = FPT1->getExtProtoInfo(); |
| 5802 | FunctionProtoType::ExtProtoInfo EPI2 = FPT2->getExtProtoInfo(); |
| 5803 | |
| 5804 | // The result is noreturn if both operands are. |
| 5805 | bool Noreturn = |
| 5806 | EPI1.ExtInfo.getNoReturn() && EPI2.ExtInfo.getNoReturn(); |
| 5807 | EPI1.ExtInfo = EPI1.ExtInfo.withNoReturn(Noreturn); |
| 5808 | EPI2.ExtInfo = EPI2.ExtInfo.withNoReturn(Noreturn); |
| 5809 | |
| 5810 | // The result is nothrow if both operands are. |
| 5811 | SmallVector<QualType, 8> ExceptionTypeStorage; |
| 5812 | EPI1.ExceptionSpec = EPI2.ExceptionSpec = |
| 5813 | mergeExceptionSpecs(*this, EPI1.ExceptionSpec, EPI2.ExceptionSpec, |
| 5814 | ExceptionTypeStorage); |
| 5815 | |
| 5816 | Composite1 = Context.getFunctionType(FPT1->getReturnType(), |
| 5817 | FPT1->getParamTypes(), EPI1); |
| 5818 | Composite2 = Context.getFunctionType(FPT2->getReturnType(), |
| 5819 | FPT2->getParamTypes(), EPI2); |
| 5820 | } |
| 5821 | } |
| 5822 | } |
| 5823 | |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5824 | if (NeedConstBefore) { |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5825 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5826 | // mismatch, so that our (non-standard!) composite type meets the |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5827 | // requirements of C++ [conv.qual]p4 bullet 3. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5828 | for (unsigned I = 0; I != NeedConstBefore; ++I) |
| 5829 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5830 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5831 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5832 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5833 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5834 | auto MOC = MemberOfClass.rbegin(); |
| 5835 | for (unsigned CVR : llvm::reverse(QualifierUnion)) { |
| 5836 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 5837 | auto Classes = *MOC++; |
| 5838 | if (Classes.first && Classes.second) { |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5839 | // Rebuild member pointer type |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5840 | Composite1 = Context.getMemberPointerType( |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5841 | Context.getQualifiedType(Composite1, Quals), Classes.first); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5842 | Composite2 = Context.getMemberPointerType( |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5843 | Context.getQualifiedType(Composite2, Quals), Classes.second); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5844 | } else { |
| 5845 | // Rebuild pointer type |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5846 | Composite1 = |
| 5847 | Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 5848 | Composite2 = |
| 5849 | Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5850 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5851 | } |
| 5852 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5853 | struct Conversion { |
| 5854 | Sema &S; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5855 | Expr *&E1, *&E2; |
| 5856 | QualType Composite; |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 5857 | InitializedEntity Entity; |
| 5858 | InitializationKind Kind; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5859 | InitializationSequence E1ToC, E2ToC; |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 5860 | bool Viable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5861 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5862 | Conversion(Sema &S, SourceLocation Loc, Expr *&E1, Expr *&E2, |
| 5863 | QualType Composite) |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 5864 | : S(S), E1(E1), E2(E2), Composite(Composite), |
| 5865 | Entity(InitializedEntity::InitializeTemporary(Composite)), |
| 5866 | Kind(InitializationKind::CreateCopy(Loc, SourceLocation())), |
| 5867 | E1ToC(S, Entity, Kind, E1), E2ToC(S, Entity, Kind, E2), |
| 5868 | Viable(E1ToC && E2ToC) {} |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5869 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5870 | bool perform() { |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5871 | ExprResult E1Result = E1ToC.Perform(S, Entity, Kind, E1); |
| 5872 | if (E1Result.isInvalid()) |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5873 | return true; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5874 | E1 = E1Result.getAs<Expr>(); |
| 5875 | |
| 5876 | ExprResult E2Result = E2ToC.Perform(S, Entity, Kind, E2); |
| 5877 | if (E2Result.isInvalid()) |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5878 | return true; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5879 | E2 = E2Result.getAs<Expr>(); |
| 5880 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5881 | return false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5882 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5883 | }; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5884 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5885 | // Try to convert to each composite pointer type. |
| 5886 | Conversion C1(*this, Loc, E1, E2, Composite1); |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5887 | if (C1.Viable && Context.hasSameType(Composite1, Composite2)) { |
| 5888 | if (ConvertArgs && C1.perform()) |
| 5889 | return QualType(); |
| 5890 | return C1.Composite; |
| 5891 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5892 | Conversion C2(*this, Loc, E1, E2, Composite2); |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5893 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5894 | if (C1.Viable == C2.Viable) { |
| 5895 | // Either Composite1 and Composite2 are viable and are different, or |
| 5896 | // neither is viable. |
| 5897 | // FIXME: How both be viable and different? |
| 5898 | return QualType(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5899 | } |
| 5900 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5901 | // Convert to the chosen type. |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5902 | if (ConvertArgs && (C1.Viable ? C1 : C2).perform()) |
| 5903 | return QualType(); |
| 5904 | |
| 5905 | return C1.Viable ? C1.Composite : C2.Composite; |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5906 | } |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 5907 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5908 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Douglas Gregor | 298087b | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 5909 | if (!E) |
| 5910 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5911 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5912 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 5913 | |
| 5914 | // If the result is a glvalue, we shouldn't bind it. |
| 5915 | if (!E->isRValue()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5916 | return E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5917 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5918 | // In ARC, calls that return a retainable type can return retained, |
| 5919 | // in which case we have to insert a consuming cast. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5920 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5921 | E->getType()->isObjCRetainableType()) { |
| 5922 | |
| 5923 | bool ReturnsRetained; |
| 5924 | |
| 5925 | // For actual calls, we compute this by examining the type of the |
| 5926 | // called value. |
| 5927 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { |
| 5928 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
| 5929 | QualType T = Callee->getType(); |
| 5930 | |
| 5931 | if (T == Context.BoundMemberTy) { |
| 5932 | // Handle pointer-to-members. |
| 5933 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) |
| 5934 | T = BinOp->getRHS()->getType(); |
| 5935 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) |
| 5936 | T = Mem->getMemberDecl()->getType(); |
| 5937 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5938 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5939 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
| 5940 | T = Ptr->getPointeeType(); |
| 5941 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
| 5942 | T = Ptr->getPointeeType(); |
| 5943 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
| 5944 | T = MemPtr->getPointeeType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5945 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5946 | const FunctionType *FTy = T->getAs<FunctionType>(); |
| 5947 | assert(FTy && "call to value not of function type?"); |
| 5948 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
| 5949 | |
| 5950 | // ActOnStmtExpr arranges things so that StmtExprs of retainable |
| 5951 | // type always produce a +1 object. |
| 5952 | } else if (isa<StmtExpr>(E)) { |
| 5953 | ReturnsRetained = true; |
| 5954 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5955 | // We hit this case with the lambda conversion-to-block optimization; |
| 5956 | // we don't want any extra casts here. |
| 5957 | } else if (isa<CastExpr>(E) && |
| 5958 | isa<BlockExpr>(cast<CastExpr>(E)->getSubExpr())) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5959 | return E; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5960 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5961 | // For message sends and property references, we try to find an |
| 5962 | // actual method. FIXME: we should infer retention by selector in |
| 5963 | // cases where we don't have an actual method. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5964 | } else { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5965 | ObjCMethodDecl *D = nullptr; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5966 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { |
| 5967 | D = Send->getMethodDecl(); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 5968 | } else if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(E)) { |
| 5969 | D = BoxedExpr->getBoxingMethod(); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5970 | } else if (ObjCArrayLiteral *ArrayLit = dyn_cast<ObjCArrayLiteral>(E)) { |
| 5971 | D = ArrayLit->getArrayWithObjectsMethod(); |
| 5972 | } else if (ObjCDictionaryLiteral *DictLit |
| 5973 | = dyn_cast<ObjCDictionaryLiteral>(E)) { |
| 5974 | D = DictLit->getDictWithObjectsMethod(); |
| 5975 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5976 | |
| 5977 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
John McCall | 32a4da0 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 5978 | |
| 5979 | // Don't do reclaims on performSelector calls; despite their |
| 5980 | // return type, the invoked method doesn't necessarily actually |
| 5981 | // return an object. |
| 5982 | if (!ReturnsRetained && |
| 5983 | D && D->getMethodFamily() == OMF_performSelector) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5984 | return E; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5985 | } |
| 5986 | |
John McCall | 16de4d2 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 5987 | // Don't reclaim an object of Class type. |
| 5988 | if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5989 | return E; |
John McCall | 16de4d2 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 5990 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 5991 | Cleanup.setExprNeedsCleanups(true); |
John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 5992 | |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5993 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject |
| 5994 | : CK_ARCReclaimReturnedObject); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5995 | return ImplicitCastExpr::Create(Context, E->getType(), ck, E, nullptr, |
| 5996 | VK_RValue); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5997 | } |
| 5998 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5999 | if (!getLangOpts().CPlusPlus) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6000 | return E; |
Douglas Gregor | 363b151 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 6001 | |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6002 | // Search for the base element type (cf. ASTContext::getBaseElementType) with |
| 6003 | // a fast path for the common case that the type is directly a RecordType. |
| 6004 | const Type *T = Context.getCanonicalType(E->getType().getTypePtr()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6005 | const RecordType *RT = nullptr; |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6006 | while (!RT) { |
| 6007 | switch (T->getTypeClass()) { |
| 6008 | case Type::Record: |
| 6009 | RT = cast<RecordType>(T); |
| 6010 | break; |
| 6011 | case Type::ConstantArray: |
| 6012 | case Type::IncompleteArray: |
| 6013 | case Type::VariableArray: |
| 6014 | case Type::DependentSizedArray: |
| 6015 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
| 6016 | break; |
| 6017 | default: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6018 | return E; |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6019 | } |
| 6020 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6021 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6022 | // That should be enough to guarantee that this type is complete, if we're |
| 6023 | // not processing a decltype expression. |
Jeffrey Yasskin | bbc4eea | 2011-01-27 19:17:54 +0000 | [diff] [blame] | 6024 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6025 | if (RD->isInvalidDecl() || RD->isDependentContext()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6026 | return E; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6027 | |
| 6028 | bool IsDecltype = ExprEvalContexts.back().IsDecltype; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6029 | CXXDestructorDecl *Destructor = IsDecltype ? nullptr : LookupDestructor(RD); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6030 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6031 | if (Destructor) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6032 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
John McCall | 8e36d53 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 6033 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 6034 | PDiag(diag::err_access_dtor_temp) |
| 6035 | << E->getType()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6036 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
| 6037 | return ExprError(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6038 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6039 | // If destructor is trivial, we can avoid the extra copy. |
| 6040 | if (Destructor->isTrivial()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6041 | return E; |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6042 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6043 | // 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] | 6044 | Cleanup.setExprNeedsCleanups(true); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6045 | } |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6046 | |
| 6047 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6048 | CXXBindTemporaryExpr *Bind = CXXBindTemporaryExpr::Create(Context, Temp, E); |
| 6049 | |
| 6050 | if (IsDecltype) |
| 6051 | ExprEvalContexts.back().DelayedDecltypeBinds.push_back(Bind); |
| 6052 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6053 | return Bind; |
Anders Carlsson | 2d4cada | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6056 | ExprResult |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6057 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6058 | if (SubExpr.isInvalid()) |
| 6059 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6060 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6061 | return MaybeCreateExprWithCleanups(SubExpr.get()); |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6062 | } |
| 6063 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6064 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 6065 | assert(SubExpr && "subexpression can't be null!"); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6066 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 6067 | CleanupVarDeclMarking(); |
| 6068 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6069 | unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects; |
| 6070 | assert(ExprCleanupObjects.size() >= FirstCleanup); |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6071 | assert(Cleanup.exprNeedsCleanups() || |
| 6072 | ExprCleanupObjects.size() == FirstCleanup); |
| 6073 | if (!Cleanup.exprNeedsCleanups()) |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6074 | return SubExpr; |
| 6075 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6076 | auto Cleanups = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup, |
| 6077 | ExprCleanupObjects.size() - FirstCleanup); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6078 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6079 | auto *E = ExprWithCleanups::Create( |
| 6080 | Context, SubExpr, Cleanup.cleanupsHaveSideEffects(), Cleanups); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6081 | DiscardCleanupsInEvaluationContext(); |
| 6082 | |
| 6083 | return E; |
| 6084 | } |
| 6085 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6086 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 6087 | assert(SubStmt && "sub-statement can't be null!"); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6088 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 6089 | CleanupVarDeclMarking(); |
| 6090 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6091 | if (!Cleanup.exprNeedsCleanups()) |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6092 | return SubStmt; |
| 6093 | |
| 6094 | // FIXME: In order to attach the temporaries, wrap the statement into |
| 6095 | // a StmtExpr; currently this is only used for asm statements. |
| 6096 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
| 6097 | // a new AsmStmtWithTemporaries. |
Nico Weber | a2a0eb9 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 6098 | CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, SubStmt, |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6099 | SourceLocation(), |
| 6100 | SourceLocation()); |
| 6101 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
| 6102 | SourceLocation()); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6103 | return MaybeCreateExprWithCleanups(E); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6104 | } |
| 6105 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6106 | /// Process the expression contained within a decltype. For such expressions, |
| 6107 | /// certain semantic checks on temporaries are delayed until this point, and |
| 6108 | /// are omitted for the 'topmost' call in the decltype expression. If the |
| 6109 | /// topmost call bound a temporary, strip that temporary off the expression. |
| 6110 | ExprResult Sema::ActOnDecltypeExpression(Expr *E) { |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6111 | assert(ExprEvalContexts.back().IsDecltype && "not in a decltype expression"); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6112 | |
| 6113 | // C++11 [expr.call]p11: |
| 6114 | // If a function call is a prvalue of object type, |
| 6115 | // -- if the function call is either |
| 6116 | // -- the operand of a decltype-specifier, or |
| 6117 | // -- the right operand of a comma operator that is the operand of a |
| 6118 | // decltype-specifier, |
| 6119 | // a temporary object is not introduced for the prvalue. |
| 6120 | |
| 6121 | // Recursively rebuild ParenExprs and comma expressions to strip out the |
| 6122 | // outermost CXXBindTemporaryExpr, if any. |
| 6123 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 6124 | ExprResult SubExpr = ActOnDecltypeExpression(PE->getSubExpr()); |
| 6125 | if (SubExpr.isInvalid()) |
| 6126 | return ExprError(); |
| 6127 | if (SubExpr.get() == PE->getSubExpr()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6128 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6129 | return ActOnParenExpr(PE->getLParen(), PE->getRParen(), SubExpr.get()); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6130 | } |
| 6131 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 6132 | if (BO->getOpcode() == BO_Comma) { |
| 6133 | ExprResult RHS = ActOnDecltypeExpression(BO->getRHS()); |
| 6134 | if (RHS.isInvalid()) |
| 6135 | return ExprError(); |
| 6136 | if (RHS.get() == BO->getRHS()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6137 | return E; |
| 6138 | return new (Context) BinaryOperator( |
| 6139 | BO->getLHS(), RHS.get(), BO_Comma, BO->getType(), BO->getValueKind(), |
Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 6140 | BO->getObjectKind(), BO->getOperatorLoc(), BO->getFPFeatures()); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6141 | } |
| 6142 | } |
| 6143 | |
| 6144 | CXXBindTemporaryExpr *TopBind = dyn_cast<CXXBindTemporaryExpr>(E); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6145 | CallExpr *TopCall = TopBind ? dyn_cast<CallExpr>(TopBind->getSubExpr()) |
| 6146 | : nullptr; |
Richard Smith | 202dc13 | 2014-02-18 03:51:47 +0000 | [diff] [blame] | 6147 | if (TopCall) |
| 6148 | E = TopCall; |
| 6149 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6150 | TopBind = nullptr; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6151 | |
| 6152 | // Disable the special decltype handling now. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6153 | ExprEvalContexts.back().IsDecltype = false; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6154 | |
Richard Smith | f86b0ae | 2012-07-28 19:54:11 +0000 | [diff] [blame] | 6155 | // In MS mode, don't perform any extra checking of call return types within a |
| 6156 | // decltype expression. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 6157 | if (getLangOpts().MSVCCompat) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6158 | return E; |
Richard Smith | f86b0ae | 2012-07-28 19:54:11 +0000 | [diff] [blame] | 6159 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6160 | // Perform the semantic checks we delayed until this point. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6161 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeCalls.size(); |
| 6162 | I != N; ++I) { |
| 6163 | CallExpr *Call = ExprEvalContexts.back().DelayedDecltypeCalls[I]; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6164 | if (Call == TopCall) |
| 6165 | continue; |
| 6166 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6167 | if (CheckCallReturnType(Call->getCallReturnType(Context), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6168 | Call->getLocStart(), |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6169 | Call, Call->getDirectCallee())) |
| 6170 | return ExprError(); |
| 6171 | } |
| 6172 | |
| 6173 | // Now all relevant types are complete, check the destructors are accessible |
| 6174 | // and non-deleted, and annotate them on the temporaries. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6175 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeBinds.size(); |
| 6176 | I != N; ++I) { |
| 6177 | CXXBindTemporaryExpr *Bind = |
| 6178 | ExprEvalContexts.back().DelayedDecltypeBinds[I]; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6179 | if (Bind == TopBind) |
| 6180 | continue; |
| 6181 | |
| 6182 | CXXTemporary *Temp = Bind->getTemporary(); |
| 6183 | |
| 6184 | CXXRecordDecl *RD = |
| 6185 | Bind->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6186 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
| 6187 | Temp->setDestructor(Destructor); |
| 6188 | |
Richard Smith | 7d847b1 | 2012-05-11 22:20:10 +0000 | [diff] [blame] | 6189 | MarkFunctionReferenced(Bind->getExprLoc(), Destructor); |
| 6190 | CheckDestructorAccess(Bind->getExprLoc(), Destructor, |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6191 | PDiag(diag::err_access_dtor_temp) |
Richard Smith | 7d847b1 | 2012-05-11 22:20:10 +0000 | [diff] [blame] | 6192 | << Bind->getType()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6193 | if (DiagnoseUseOfDecl(Destructor, Bind->getExprLoc())) |
| 6194 | return ExprError(); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6195 | |
| 6196 | // 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] | 6197 | Cleanup.setExprNeedsCleanups(true); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | // Possibly strip off the top CXXBindTemporaryExpr. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6201 | return E; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6202 | } |
| 6203 | |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6204 | /// Note a set of 'operator->' functions that were used for a member access. |
| 6205 | static void noteOperatorArrows(Sema &S, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 6206 | ArrayRef<FunctionDecl *> OperatorArrows) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6207 | unsigned SkipStart = OperatorArrows.size(), SkipCount = 0; |
| 6208 | // FIXME: Make this configurable? |
| 6209 | unsigned Limit = 9; |
| 6210 | if (OperatorArrows.size() > Limit) { |
| 6211 | // Produce Limit-1 normal notes and one 'skipping' note. |
| 6212 | SkipStart = (Limit - 1) / 2 + (Limit - 1) % 2; |
| 6213 | SkipCount = OperatorArrows.size() - (Limit - 1); |
| 6214 | } |
| 6215 | |
| 6216 | for (unsigned I = 0; I < OperatorArrows.size(); /**/) { |
| 6217 | if (I == SkipStart) { |
| 6218 | S.Diag(OperatorArrows[I]->getLocation(), |
| 6219 | diag::note_operator_arrows_suppressed) |
| 6220 | << SkipCount; |
| 6221 | I += SkipCount; |
| 6222 | } else { |
| 6223 | S.Diag(OperatorArrows[I]->getLocation(), diag::note_operator_arrow_here) |
| 6224 | << OperatorArrows[I]->getCallResultType(); |
| 6225 | ++I; |
| 6226 | } |
| 6227 | } |
| 6228 | } |
| 6229 | |
Nico Weber | 964d332 | 2015-02-16 22:35:45 +0000 | [diff] [blame] | 6230 | ExprResult Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, |
| 6231 | SourceLocation OpLoc, |
| 6232 | tok::TokenKind OpKind, |
| 6233 | ParsedType &ObjectType, |
| 6234 | bool &MayBePseudoDestructor) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6235 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6236 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6237 | if (Result.isInvalid()) return ExprError(); |
| 6238 | Base = Result.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6239 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6240 | Result = CheckPlaceholderExpr(Base); |
| 6241 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6242 | Base = Result.get(); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6243 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6244 | QualType BaseType = Base->getType(); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6245 | MayBePseudoDestructor = false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6246 | if (BaseType->isDependentType()) { |
Douglas Gregor | 4112718 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 6247 | // If we have a pointer to a dependent type and are using the -> operator, |
| 6248 | // the object type is the type that the pointer points to. We might still |
| 6249 | // have enough information about that type to do something useful. |
| 6250 | if (OpKind == tok::arrow) |
| 6251 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 6252 | BaseType = Ptr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6253 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6254 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6255 | MayBePseudoDestructor = true; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6256 | return Base; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6257 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6258 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6259 | // C++ [over.match.oper]p8: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6260 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6261 | // returned, with the original second operand. |
| 6262 | if (OpKind == tok::arrow) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6263 | QualType StartingType = BaseType; |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6264 | bool NoArrowOperatorFound = false; |
| 6265 | bool FirstIteration = true; |
| 6266 | FunctionDecl *CurFD = dyn_cast<FunctionDecl>(CurContext); |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 6267 | // The set of types we've considered so far. |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 6268 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6269 | SmallVector<FunctionDecl*, 8> OperatorArrows; |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 6270 | CTypes.insert(Context.getCanonicalType(BaseType)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6271 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6272 | while (BaseType->isRecordType()) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6273 | if (OperatorArrows.size() >= getLangOpts().ArrowDepth) { |
| 6274 | Diag(OpLoc, diag::err_operator_arrow_depth_exceeded) |
Richard Smith | 9dbc574 | 2013-11-06 19:43:09 +0000 | [diff] [blame] | 6275 | << StartingType << getLangOpts().ArrowDepth << Base->getSourceRange(); |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6276 | noteOperatorArrows(*this, OperatorArrows); |
| 6277 | Diag(OpLoc, diag::note_operator_arrow_depth) |
| 6278 | << getLangOpts().ArrowDepth; |
| 6279 | return ExprError(); |
| 6280 | } |
| 6281 | |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6282 | Result = BuildOverloadedArrowExpr( |
| 6283 | S, Base, OpLoc, |
| 6284 | // When in a template specialization and on the first loop iteration, |
| 6285 | // potentially give the default diagnostic (with the fixit in a |
| 6286 | // separate note) instead of having the error reported back to here |
| 6287 | // and giving a diagnostic with a fixit attached to the error itself. |
| 6288 | (FirstIteration && CurFD && CurFD->isFunctionTemplateSpecialization()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6289 | ? nullptr |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6290 | : &NoArrowOperatorFound); |
| 6291 | if (Result.isInvalid()) { |
| 6292 | if (NoArrowOperatorFound) { |
| 6293 | if (FirstIteration) { |
| 6294 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Richard Smith | 9dbc574 | 2013-11-06 19:43:09 +0000 | [diff] [blame] | 6295 | << BaseType << 1 << Base->getSourceRange() |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6296 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 6297 | OpKind = tok::period; |
| 6298 | break; |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 6299 | } |
| 6300 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 6301 | << BaseType << Base->getSourceRange(); |
| 6302 | CallExpr *CE = dyn_cast<CallExpr>(Base); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6303 | if (Decl *CD = (CE ? CE->getCalleeDecl() : nullptr)) { |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 6304 | Diag(CD->getLocStart(), |
| 6305 | diag::note_member_reference_arrow_from_operator_arrow); |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6306 | } |
| 6307 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6308 | return ExprError(); |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6309 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6310 | Base = Result.get(); |
| 6311 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6312 | OperatorArrows.push_back(OpCall->getDirectCallee()); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6313 | BaseType = Base->getType(); |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 6314 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6315 | if (!CTypes.insert(CBaseType).second) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6316 | Diag(OpLoc, diag::err_operator_arrow_circular) << StartingType; |
| 6317 | noteOperatorArrows(*this, OperatorArrows); |
Fariborz Jahanian | 10ce958 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 6318 | return ExprError(); |
| 6319 | } |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6320 | FirstIteration = false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6321 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6322 | |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6323 | if (OpKind == tok::arrow && |
| 6324 | (BaseType->isPointerType() || BaseType->isObjCObjectPointerType())) |
Douglas Gregor | e4f764f | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 6325 | BaseType = BaseType->getPointeeType(); |
| 6326 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6327 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6328 | // Objective-C properties allow "." access on Objective-C pointer types, |
| 6329 | // so adjust the base type to the object type itself. |
| 6330 | if (BaseType->isObjCObjectPointerType()) |
| 6331 | BaseType = BaseType->getPointeeType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6332 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6333 | // C++ [basic.lookup.classref]p2: |
| 6334 | // [...] If the type of the object expression is of pointer to scalar |
| 6335 | // type, the unqualified-id is looked up in the context of the complete |
| 6336 | // postfix-expression. |
| 6337 | // |
| 6338 | // This also indicates that we could be parsing a pseudo-destructor-name. |
| 6339 | // Note that Objective-C class and object types can be pseudo-destructor |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6340 | // expressions or normal member (ivar or property) access expressions, and |
| 6341 | // it's legal for the type to be incomplete if this is a pseudo-destructor |
| 6342 | // call. We'll do more incomplete-type checks later in the lookup process, |
| 6343 | // so just skip this check for ObjC types. |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6344 | if (BaseType->isObjCObjectOrInterfaceType()) { |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6345 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6346 | MayBePseudoDestructor = true; |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6347 | return Base; |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6348 | } else if (!BaseType->isRecordType()) { |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 6349 | ObjectType = nullptr; |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6350 | MayBePseudoDestructor = true; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6351 | return Base; |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 6352 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6353 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6354 | // The object type must be complete (or dependent), or |
| 6355 | // C++11 [expr.prim.general]p3: |
| 6356 | // Unlike the object expression in other contexts, *this is not required to |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6357 | // 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] | 6358 | // the member function body. |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 6359 | if (!BaseType->isDependentType() && |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6360 | !isThisOutsideMemberFunctionBody(BaseType) && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 6361 | RequireCompleteType(OpLoc, BaseType, diag::err_incomplete_member_access)) |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 6362 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6363 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 6364 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6365 | // 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] | 6366 | // 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] | 6367 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 6368 | // up in the scope of class C. [...] |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6369 | ObjectType = ParsedType::make(BaseType); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6370 | return Base; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6373 | static bool CheckArrow(Sema& S, QualType& ObjectType, Expr *&Base, |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6374 | tok::TokenKind& OpKind, SourceLocation OpLoc) { |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6375 | if (Base->hasPlaceholderType()) { |
| 6376 | ExprResult result = S.CheckPlaceholderExpr(Base); |
| 6377 | if (result.isInvalid()) return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6378 | Base = result.get(); |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6379 | } |
| 6380 | ObjectType = Base->getType(); |
| 6381 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6382 | // C++ [expr.pseudo]p2: |
| 6383 | // The left-hand side of the dot operator shall be of scalar type. The |
| 6384 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 6385 | // This scalar type is the object type. |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6386 | // Note that this is rather different from the normal handling for the |
| 6387 | // arrow operator. |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6388 | if (OpKind == tok::arrow) { |
| 6389 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 6390 | ObjectType = Ptr->getPointeeType(); |
| 6391 | } else if (!Base->isTypeDependent()) { |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 6392 | // The user wrote "p->" when they probably meant "p."; fix it. |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6393 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 6394 | << ObjectType << true |
| 6395 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 6396 | if (S.isSFINAEContext()) |
| 6397 | return true; |
| 6398 | |
| 6399 | OpKind = tok::period; |
| 6400 | } |
| 6401 | } |
| 6402 | |
| 6403 | return false; |
| 6404 | } |
| 6405 | |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6406 | /// \brief Check if it's ok to try and recover dot pseudo destructor calls on |
| 6407 | /// pointer objects. |
| 6408 | static bool |
| 6409 | canRecoverDotPseudoDestructorCallsOnPointerObjects(Sema &SemaRef, |
| 6410 | QualType DestructedType) { |
| 6411 | // If this is a record type, check if its destructor is callable. |
| 6412 | if (auto *RD = DestructedType->getAsCXXRecordDecl()) { |
| 6413 | if (CXXDestructorDecl *D = SemaRef.LookupDestructor(RD)) |
| 6414 | return SemaRef.CanUseDecl(D, /*TreatUnavailableAsInvalid=*/false); |
| 6415 | return false; |
| 6416 | } |
| 6417 | |
| 6418 | // Otherwise, check if it's a type for which it's valid to use a pseudo-dtor. |
| 6419 | return DestructedType->isDependentType() || DestructedType->isScalarType() || |
| 6420 | DestructedType->isVectorType(); |
| 6421 | } |
| 6422 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6423 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6424 | SourceLocation OpLoc, |
| 6425 | tok::TokenKind OpKind, |
| 6426 | const CXXScopeSpec &SS, |
| 6427 | TypeSourceInfo *ScopeTypeInfo, |
| 6428 | SourceLocation CCLoc, |
| 6429 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6430 | PseudoDestructorTypeStorage Destructed) { |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6431 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6432 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6433 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6434 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6435 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6436 | |
Douglas Gregor | c5c5734 | 2012-09-10 14:57:06 +0000 | [diff] [blame] | 6437 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType() && |
| 6438 | !ObjectType->isVectorType()) { |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 6439 | if (getLangOpts().MSVCCompat && ObjectType->isVoidType()) |
Nico Weber | 4bc6499 | 2012-01-23 06:08:16 +0000 | [diff] [blame] | 6440 | Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange(); |
Reid Kleckner | e502507 | 2014-05-01 16:50:23 +0000 | [diff] [blame] | 6441 | else { |
Nico Weber | 5882927 | 2012-01-23 05:50:57 +0000 | [diff] [blame] | 6442 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 6443 | << ObjectType << Base->getSourceRange(); |
Reid Kleckner | e502507 | 2014-05-01 16:50:23 +0000 | [diff] [blame] | 6444 | return ExprError(); |
| 6445 | } |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6446 | } |
| 6447 | |
| 6448 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6449 | // [...] The cv-unqualified versions of the object type and of the type |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6450 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6451 | if (DestructedTypeInfo) { |
| 6452 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 6453 | SourceLocation DestructedTypeStart |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6454 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6455 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
| 6456 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6457 | // Detect dot pseudo destructor calls on pointer objects, e.g.: |
| 6458 | // Foo *foo; |
| 6459 | // foo.~Foo(); |
| 6460 | if (OpKind == tok::period && ObjectType->isPointerType() && |
| 6461 | Context.hasSameUnqualifiedType(DestructedType, |
| 6462 | ObjectType->getPointeeType())) { |
| 6463 | auto Diagnostic = |
| 6464 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 6465 | << ObjectType << /*IsArrow=*/0 << Base->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6466 | |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6467 | // Issue a fixit only when the destructor is valid. |
| 6468 | if (canRecoverDotPseudoDestructorCallsOnPointerObjects( |
| 6469 | *this, DestructedType)) |
| 6470 | Diagnostic << FixItHint::CreateReplacement(OpLoc, "->"); |
| 6471 | |
| 6472 | // Recover by setting the object type to the destructed type and the |
| 6473 | // operator to '->'. |
| 6474 | ObjectType = DestructedType; |
| 6475 | OpKind = tok::arrow; |
| 6476 | } else { |
| 6477 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
| 6478 | << ObjectType << DestructedType << Base->getSourceRange() |
| 6479 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 6480 | |
| 6481 | // Recover by setting the destructed type to the object type. |
| 6482 | DestructedType = ObjectType; |
| 6483 | DestructedTypeInfo = |
| 6484 | Context.getTrivialTypeSourceInfo(ObjectType, DestructedTypeStart); |
| 6485 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6486 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6487 | } else if (DestructedType.getObjCLifetime() != |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6488 | ObjectType.getObjCLifetime()) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6489 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6490 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
| 6491 | // Okay: just pretend that the user provided the correctly-qualified |
| 6492 | // type. |
| 6493 | } else { |
| 6494 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
| 6495 | << ObjectType << DestructedType << Base->getSourceRange() |
| 6496 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 6497 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6498 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6499 | // Recover by setting the destructed type to the object type. |
| 6500 | DestructedType = ObjectType; |
| 6501 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 6502 | DestructedTypeStart); |
| 6503 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6504 | } |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6505 | } |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6506 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6507 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6508 | // C++ [expr.pseudo]p2: |
| 6509 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 6510 | // form |
| 6511 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6512 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6513 | // |
| 6514 | // shall designate the same scalar type. |
| 6515 | if (ScopeTypeInfo) { |
| 6516 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 6517 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | b86a6b8 | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 6518 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6519 | |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6520 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6521 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6522 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6523 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6524 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6525 | ScopeType = QualType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6526 | ScopeTypeInfo = nullptr; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6527 | } |
| 6528 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6529 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6530 | Expr *Result |
| 6531 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 6532 | OpKind == tok::arrow, OpLoc, |
Douglas Gregor | a6ce608 | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 6533 | SS.getWithLocInContext(Context), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6534 | ScopeTypeInfo, |
| 6535 | CCLoc, |
| 6536 | TildeLoc, |
| 6537 | Destructed); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6538 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6539 | return Result; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6542 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6543 | SourceLocation OpLoc, |
| 6544 | tok::TokenKind OpKind, |
| 6545 | CXXScopeSpec &SS, |
| 6546 | UnqualifiedId &FirstTypeName, |
| 6547 | SourceLocation CCLoc, |
| 6548 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6549 | UnqualifiedId &SecondTypeName) { |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6550 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 6551 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 6552 | "Invalid first type name in pseudo-destructor"); |
| 6553 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 6554 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 6555 | "Invalid second type name in pseudo-destructor"); |
| 6556 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6557 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6558 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6559 | return ExprError(); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6560 | |
| 6561 | // Compute the object type that we should use for name lookup purposes. Only |
| 6562 | // record types and dependent types matter. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6563 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6564 | if (!SS.isSet()) { |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6565 | if (ObjectType->isRecordType()) |
| 6566 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6567 | else if (ObjectType->isDependentType()) |
| 6568 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6569 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6570 | |
| 6571 | // Convert the name of the type being destructed (following the ~) into a |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6572 | // type (with source-location information). |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6573 | QualType DestructedType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6574 | TypeSourceInfo *DestructedTypeInfo = nullptr; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6575 | PseudoDestructorTypeStorage Destructed; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6576 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6577 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6578 | SecondTypeName.StartLocation, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6579 | S, &SS, true, false, ObjectTypePtrForLookup, |
| 6580 | /*IsCtorOrDtorName*/true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6581 | if (!T && |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6582 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 6583 | (!SS.isSet() && ObjectType->isDependentType()))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6584 | // 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] | 6585 | // couldn't find anything useful in scope. Just store the identifier and |
| 6586 | // it's location, and we'll perform (qualified) name lookup again at |
| 6587 | // template instantiation time. |
| 6588 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 6589 | SecondTypeName.StartLocation); |
| 6590 | } else if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6591 | Diag(SecondTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6592 | diag::err_pseudo_dtor_destructor_non_type) |
| 6593 | << SecondTypeName.Identifier << ObjectType; |
| 6594 | if (isSFINAEContext()) |
| 6595 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6596 | |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6597 | // Recover by assuming we had the right type all along. |
| 6598 | DestructedType = ObjectType; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6599 | } else |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6600 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6601 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6602 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6603 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6604 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6605 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6606 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6607 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6608 | TemplateId->Template, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6609 | TemplateId->Name, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6610 | TemplateId->TemplateNameLoc, |
| 6611 | TemplateId->LAngleLoc, |
| 6612 | TemplateArgsPtr, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6613 | TemplateId->RAngleLoc, |
| 6614 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6615 | if (T.isInvalid() || !T.get()) { |
| 6616 | // Recover by assuming we had the right type all along. |
| 6617 | DestructedType = ObjectType; |
| 6618 | } else |
| 6619 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6620 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6621 | |
| 6622 | // 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] | 6623 | // information. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6624 | if (!DestructedType.isNull()) { |
| 6625 | if (!DestructedTypeInfo) |
| 6626 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6627 | SecondTypeName.StartLocation); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6628 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6629 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6630 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6631 | // 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] | 6632 | TypeSourceInfo *ScopeTypeInfo = nullptr; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6633 | QualType ScopeType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6634 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6635 | FirstTypeName.Identifier) { |
| 6636 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6637 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6638 | FirstTypeName.StartLocation, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6639 | S, &SS, true, false, ObjectTypePtrForLookup, |
| 6640 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6641 | if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6642 | Diag(FirstTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6643 | diag::err_pseudo_dtor_destructor_non_type) |
| 6644 | << FirstTypeName.Identifier << ObjectType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6645 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6646 | if (isSFINAEContext()) |
| 6647 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6648 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6649 | // Just drop this type. It's unnecessary anyway. |
| 6650 | ScopeType = QualType(); |
| 6651 | } else |
| 6652 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6653 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6654 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6655 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6656 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6657 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6658 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6659 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6660 | TemplateId->Template, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6661 | TemplateId->Name, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6662 | TemplateId->TemplateNameLoc, |
| 6663 | TemplateId->LAngleLoc, |
| 6664 | TemplateArgsPtr, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6665 | TemplateId->RAngleLoc, |
| 6666 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6667 | if (T.isInvalid() || !T.get()) { |
| 6668 | // Recover by dropping this type. |
| 6669 | ScopeType = QualType(); |
| 6670 | } else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6671 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6672 | } |
| 6673 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6674 | |
Douglas Gregor | 90ad922 | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 6675 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 6676 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 6677 | FirstTypeName.StartLocation); |
| 6678 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6679 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6680 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | cdbd515 | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 6681 | ScopeTypeInfo, CCLoc, TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6682 | Destructed); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6685 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
| 6686 | SourceLocation OpLoc, |
| 6687 | tok::TokenKind OpKind, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6688 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6689 | const DeclSpec& DS) { |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6690 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6691 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6692 | return ExprError(); |
| 6693 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 6694 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc(), |
| 6695 | false); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6696 | |
| 6697 | TypeLocBuilder TLB; |
| 6698 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
| 6699 | DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 6700 | TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T); |
| 6701 | PseudoDestructorTypeStorage Destructed(DestructedTypeInfo); |
| 6702 | |
| 6703 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, CXXScopeSpec(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6704 | nullptr, SourceLocation(), TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6705 | Destructed); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6706 | } |
| 6707 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6708 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 6709 | CXXConversionDecl *Method, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6710 | bool HadMultipleCandidates) { |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6711 | if (Method->getParent()->isLambda() && |
| 6712 | Method->getConversionType()->isBlockPointerType()) { |
| 6713 | // This is a lambda coversion to block pointer; check if the argument |
| 6714 | // is a LambdaExpr. |
| 6715 | Expr *SubE = E; |
| 6716 | CastExpr *CE = dyn_cast<CastExpr>(SubE); |
| 6717 | if (CE && CE->getCastKind() == CK_NoOp) |
| 6718 | SubE = CE->getSubExpr(); |
| 6719 | SubE = SubE->IgnoreParens(); |
| 6720 | if (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(SubE)) |
| 6721 | SubE = BE->getSubExpr(); |
| 6722 | if (isa<LambdaExpr>(SubE)) { |
| 6723 | // For the conversion to block pointer on a lambda expression, we |
| 6724 | // construct a special BlockLiteral instead; this doesn't really make |
| 6725 | // a difference in ARC, but outside of ARC the resulting block literal |
| 6726 | // follows the normal lifetime rules for block literals instead of being |
| 6727 | // autoreleased. |
| 6728 | DiagnosticErrorTrap Trap(Diags); |
Akira Hatanaka | c482acd | 2016-05-04 18:07:20 +0000 | [diff] [blame] | 6729 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6730 | ExprResult Exp = BuildBlockForLambdaConversion(E->getExprLoc(), |
| 6731 | E->getExprLoc(), |
| 6732 | Method, E); |
Akira Hatanaka | c482acd | 2016-05-04 18:07:20 +0000 | [diff] [blame] | 6733 | PopExpressionEvaluationContext(); |
| 6734 | |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6735 | if (Exp.isInvalid()) |
| 6736 | Diag(E->getExprLoc(), diag::note_lambda_to_block_conv); |
| 6737 | return Exp; |
| 6738 | } |
| 6739 | } |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6740 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6741 | ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/nullptr, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6742 | FoundDecl, Method); |
| 6743 | if (Exp.isInvalid()) |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 6744 | return true; |
Eli Friedman | f719553 | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 6745 | |
Aaron Ballman | f4cb2be | 2015-03-24 15:07:53 +0000 | [diff] [blame] | 6746 | MemberExpr *ME = new (Context) MemberExpr( |
| 6747 | Exp.get(), /*IsArrow=*/false, SourceLocation(), Method, SourceLocation(), |
| 6748 | Context.BoundMemberTy, VK_RValue, OK_Ordinary); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6749 | if (HadMultipleCandidates) |
| 6750 | ME->setHadMultipleCandidates(true); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 6751 | MarkMemberReferenced(ME); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6752 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6753 | QualType ResultType = Method->getReturnType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6754 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 6755 | ResultType = ResultType.getNonLValueExprType(Context); |
| 6756 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6757 | CXXMemberCallExpr *CE = |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 6758 | new (Context) CXXMemberCallExpr(Context, ME, None, ResultType, VK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6759 | Exp.get()->getLocEnd()); |
George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6760 | |
| 6761 | if (CheckFunctionCall(Method, CE, |
| 6762 | Method->getType()->castAs<FunctionProtoType>())) |
| 6763 | return ExprError(); |
| 6764 | |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6765 | return CE; |
| 6766 | } |
| 6767 | |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6768 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 6769 | SourceLocation RParen) { |
Aaron Ballman | edc8084 | 2015-04-27 22:31:12 +0000 | [diff] [blame] | 6770 | // If the operand is an unresolved lookup expression, the expression is ill- |
| 6771 | // formed per [over.over]p1, because overloaded function names cannot be used |
| 6772 | // without arguments except in explicit contexts. |
| 6773 | ExprResult R = CheckPlaceholderExpr(Operand); |
| 6774 | if (R.isInvalid()) |
| 6775 | return R; |
| 6776 | |
| 6777 | // The operand may have been modified when checking the placeholder type. |
| 6778 | Operand = R.get(); |
| 6779 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 6780 | if (!inTemplateInstantiation() && Operand->HasSideEffects(Context, false)) { |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 6781 | // The expression operand for noexcept is in an unevaluated expression |
| 6782 | // context, so side effects could result in unintended consequences. |
| 6783 | Diag(Operand->getExprLoc(), diag::warn_side_effects_unevaluated_context); |
| 6784 | } |
| 6785 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 6786 | CanThrowResult CanThrow = canThrow(Operand); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6787 | return new (Context) |
| 6788 | CXXNoexceptExpr(Context.BoolTy, Operand, CanThrow, KeyLoc, RParen); |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 6792 | Expr *Operand, SourceLocation RParen) { |
| 6793 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 22e3a93 | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 6794 | } |
| 6795 | |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6796 | static bool IsSpecialDiscardedValue(Expr *E) { |
| 6797 | // In C++11, discarded-value expressions of a certain form are special, |
| 6798 | // according to [expr]p10: |
| 6799 | // The lvalue-to-rvalue conversion (4.1) is applied only if the |
| 6800 | // expression is an lvalue of volatile-qualified type and it has |
| 6801 | // one of the following forms: |
| 6802 | E = E->IgnoreParens(); |
| 6803 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6804 | // - id-expression (5.1.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6805 | if (isa<DeclRefExpr>(E)) |
| 6806 | return true; |
| 6807 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6808 | // - subscripting (5.2.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6809 | if (isa<ArraySubscriptExpr>(E)) |
| 6810 | return true; |
| 6811 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6812 | // - class member access (5.2.5), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6813 | if (isa<MemberExpr>(E)) |
| 6814 | return true; |
| 6815 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6816 | // - indirection (5.3.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6817 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 6818 | if (UO->getOpcode() == UO_Deref) |
| 6819 | return true; |
| 6820 | |
| 6821 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6822 | // - pointer-to-member operation (5.5), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6823 | if (BO->isPtrMemOp()) |
| 6824 | return true; |
| 6825 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6826 | // - 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] | 6827 | if (BO->getOpcode() == BO_Comma) |
| 6828 | return IsSpecialDiscardedValue(BO->getRHS()); |
| 6829 | } |
| 6830 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6831 | // - conditional expression (5.16) where both the second and the third |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6832 | // operands are one of the above, or |
| 6833 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) |
| 6834 | return IsSpecialDiscardedValue(CO->getTrueExpr()) && |
| 6835 | IsSpecialDiscardedValue(CO->getFalseExpr()); |
| 6836 | // The related edge case of "*x ?: *x". |
| 6837 | if (BinaryConditionalOperator *BCO = |
| 6838 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 6839 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(BCO->getTrueExpr())) |
| 6840 | return IsSpecialDiscardedValue(OVE->getSourceExpr()) && |
| 6841 | IsSpecialDiscardedValue(BCO->getFalseExpr()); |
| 6842 | } |
| 6843 | |
| 6844 | // Objective-C++ extensions to the rule. |
| 6845 | if (isa<PseudoObjectExpr>(E) || isa<ObjCIvarRefExpr>(E)) |
| 6846 | return true; |
| 6847 | |
| 6848 | return false; |
| 6849 | } |
| 6850 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6851 | /// Perform the conversions required for an expression used in a |
| 6852 | /// context that ignores the result. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6853 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6854 | if (E->hasPlaceholderType()) { |
| 6855 | ExprResult result = CheckPlaceholderExpr(E); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6856 | if (result.isInvalid()) return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6857 | E = result.get(); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6858 | } |
| 6859 | |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 6860 | // C99 6.3.2.1: |
| 6861 | // [Except in specific positions,] an lvalue that does not have |
| 6862 | // array type is converted to the value stored in the |
| 6863 | // designated object (and is no longer an lvalue). |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 6864 | if (E->isRValue()) { |
| 6865 | // In C, function designators (i.e. expressions of function type) |
| 6866 | // are r-values, but we still want to do function-to-pointer decay |
| 6867 | // on them. This is both technically correct and convenient for |
| 6868 | // some clients. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6869 | if (!getLangOpts().CPlusPlus && E->getType()->isFunctionType()) |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 6870 | return DefaultFunctionArrayConversion(E); |
| 6871 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6872 | return E; |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 6873 | } |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 6874 | |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6875 | if (getLangOpts().CPlusPlus) { |
| 6876 | // The C++11 standard defines the notion of a discarded-value expression; |
| 6877 | // normally, we don't need to do anything to handle it, but if it is a |
| 6878 | // volatile lvalue with a special form, we perform an lvalue-to-rvalue |
| 6879 | // conversion. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6880 | if (getLangOpts().CPlusPlus11 && E->isGLValue() && |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6881 | E->getType().isVolatileQualified() && |
| 6882 | IsSpecialDiscardedValue(E)) { |
| 6883 | ExprResult Res = DefaultLvalueConversion(E); |
| 6884 | if (Res.isInvalid()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6885 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6886 | E = Res.get(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6887 | } |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 6888 | |
| 6889 | // C++1z: |
| 6890 | // If the expression is a prvalue after this optional conversion, the |
| 6891 | // temporary materialization conversion is applied. |
| 6892 | // |
| 6893 | // We skip this step: IR generation is able to synthesize the storage for |
| 6894 | // itself in the aggregate case, and adding the extra node to the AST is |
| 6895 | // just clutter. |
| 6896 | // FIXME: We don't emit lifetime markers for the temporaries due to this. |
| 6897 | // FIXME: Do any other AST consumers care about this? |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6898 | return E; |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6899 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6900 | |
| 6901 | // GCC seems to also exclude expressions of incomplete enum type. |
| 6902 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
| 6903 | if (!T->getDecl()->isComplete()) { |
| 6904 | // FIXME: stupid workaround for a codegen bug! |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6905 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).get(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6906 | return E; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6907 | } |
| 6908 | } |
| 6909 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6910 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 6911 | if (Res.isInvalid()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6912 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6913 | E = Res.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6914 | |
John McCall | ca61b65 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 6915 | if (!E->getType()->isVoidType()) |
| 6916 | RequireCompleteType(E->getExprLoc(), E->getType(), |
| 6917 | diag::err_incomplete_type); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6918 | return E; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6919 | } |
| 6920 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6921 | // If we can unambiguously determine whether Var can never be used |
| 6922 | // in a constant expression, return true. |
| 6923 | // - if the variable and its initializer are non-dependent, then |
| 6924 | // we can unambiguously check if the variable is a constant expression. |
| 6925 | // - if the initializer is not value dependent - we can determine whether |
| 6926 | // 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] | 6927 | // be used to initialize a constant expression we conclude that Var can |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6928 | // never be a constant expression. |
| 6929 | // - FXIME: if the initializer is dependent, we can still do some analysis and |
| 6930 | // identify certain cases unambiguously as non-const by using a Visitor: |
| 6931 | // - such as those that involve odr-use of a ParmVarDecl, involve a new |
| 6932 | // delete, lambda-expr, dynamic-cast, reinterpret-cast etc... |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6933 | static inline bool VariableCanNeverBeAConstantExpression(VarDecl *Var, |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6934 | ASTContext &Context) { |
| 6935 | if (isa<ParmVarDecl>(Var)) return true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6936 | const VarDecl *DefVD = nullptr; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6937 | |
| 6938 | // If there is no initializer - this can not be a constant expression. |
| 6939 | if (!Var->getAnyInitializer(DefVD)) return true; |
| 6940 | assert(DefVD); |
| 6941 | if (DefVD->isWeak()) return false; |
| 6942 | EvaluatedStmt *Eval = DefVD->ensureEvaluatedStmt(); |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 6943 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6944 | Expr *Init = cast<Expr>(Eval->Value); |
| 6945 | |
| 6946 | if (Var->getType()->isDependentType() || Init->isValueDependent()) { |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 6947 | // FIXME: Teach the constant evaluator to deal with the non-dependent parts |
| 6948 | // of value-dependent expressions, and use it here to determine whether the |
| 6949 | // initializer is a potential constant expression. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6950 | return false; |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 6951 | } |
| 6952 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6953 | return !IsVariableAConstantExpression(Var, Context); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6954 | } |
| 6955 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6956 | /// \brief Check if the current lambda has any potential captures |
| 6957 | /// that must be captured by any of its enclosing lambdas that are ready to |
| 6958 | /// capture. If there is a lambda that can capture a nested |
| 6959 | /// potential-capture, go ahead and do so. Also, check to see if any |
| 6960 | /// 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] | 6961 | /// need to be captured. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6962 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 6963 | static void CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures( |
| 6964 | Expr *const FE, LambdaScopeInfo *const CurrentLSI, Sema &S) { |
| 6965 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6966 | assert(!S.isUnevaluatedContext()); |
| 6967 | assert(S.CurContext->isDependentContext()); |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 6968 | #ifndef NDEBUG |
| 6969 | DeclContext *DC = S.CurContext; |
| 6970 | while (DC && isa<CapturedDecl>(DC)) |
| 6971 | DC = DC->getParent(); |
| 6972 | assert( |
| 6973 | CurrentLSI->CallOperator == DC && |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 6974 | "The current call operator must be synchronized with Sema's CurContext"); |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 6975 | #endif // NDEBUG |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 6976 | |
| 6977 | const bool IsFullExprInstantiationDependent = FE->isInstantiationDependent(); |
| 6978 | |
| 6979 | ArrayRef<const FunctionScopeInfo *> FunctionScopesArrayRef( |
| 6980 | S.FunctionScopes.data(), S.FunctionScopes.size()); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6981 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 6982 | // All the potentially captureable variables in the current nested |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6983 | // lambda (within a generic outer lambda), must be captured by an |
| 6984 | // outer lambda that is enclosed within a non-dependent context. |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 6985 | const unsigned NumPotentialCaptures = |
| 6986 | CurrentLSI->getNumPotentialVariableCaptures(); |
| 6987 | for (unsigned I = 0; I != NumPotentialCaptures; ++I) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6988 | Expr *VarExpr = nullptr; |
| 6989 | VarDecl *Var = nullptr; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 6990 | CurrentLSI->getPotentialVariableCapture(I, Var, VarExpr); |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 6991 | // 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] | 6992 | // expression is not instantiation dependent, only then do we not |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 6993 | // need to check enclosing lambda's for speculative captures. |
| 6994 | // For e.g.: |
| 6995 | // Even though 'x' is not odr-used, it should be captured. |
| 6996 | // int test() { |
| 6997 | // const int x = 10; |
| 6998 | // auto L = [=](auto a) { |
| 6999 | // (void) +x + a; |
| 7000 | // }; |
| 7001 | // } |
| 7002 | if (CurrentLSI->isVariableExprMarkedAsNonODRUsed(VarExpr) && |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7003 | !IsFullExprInstantiationDependent) |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7004 | continue; |
| 7005 | |
| 7006 | // If we have a capture-capable lambda for the variable, go ahead and |
| 7007 | // capture the variable in that lambda (and all its enclosing lambdas). |
| 7008 | if (const Optional<unsigned> Index = |
| 7009 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
| 7010 | FunctionScopesArrayRef, Var, S)) { |
| 7011 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
| 7012 | MarkVarDeclODRUsed(Var, VarExpr->getExprLoc(), S, |
| 7013 | &FunctionScopeIndexOfCapturableLambda); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7014 | } |
| 7015 | const bool IsVarNeverAConstantExpression = |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7016 | VariableCanNeverBeAConstantExpression(Var, S.Context); |
| 7017 | if (!IsFullExprInstantiationDependent || IsVarNeverAConstantExpression) { |
| 7018 | // This full expression is not instantiation dependent or the variable |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7019 | // can not be used in a constant expression - which means |
| 7020 | // this variable must be odr-used here, so diagnose a |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7021 | // capture violation early, if the variable is un-captureable. |
| 7022 | // This is purely for diagnosing errors early. Otherwise, this |
| 7023 | // error would get diagnosed when the lambda becomes capture ready. |
| 7024 | QualType CaptureType, DeclRefType; |
| 7025 | SourceLocation ExprLoc = VarExpr->getExprLoc(); |
| 7026 | if (S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7027 | /*EllipsisLoc*/ SourceLocation(), |
| 7028 | /*BuildAndDiagnose*/false, CaptureType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7029 | DeclRefType, nullptr)) { |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7030 | // We will never be able to capture this variable, and we need |
| 7031 | // to be able to in any and all instantiations, so diagnose it. |
| 7032 | S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7033 | /*EllipsisLoc*/ SourceLocation(), |
| 7034 | /*BuildAndDiagnose*/true, CaptureType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7035 | DeclRefType, nullptr); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7036 | } |
| 7037 | } |
| 7038 | } |
| 7039 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7040 | // Check if 'this' needs to be captured. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7041 | if (CurrentLSI->hasPotentialThisCapture()) { |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7042 | // If we have a capture-capable lambda for 'this', go ahead and capture |
| 7043 | // 'this' in that lambda (and all its enclosing lambdas). |
| 7044 | if (const Optional<unsigned> Index = |
| 7045 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7046 | FunctionScopesArrayRef, /*0 is 'this'*/ nullptr, S)) { |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7047 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
| 7048 | S.CheckCXXThisCapture(CurrentLSI->PotentialThisCaptureLocation, |
| 7049 | /*Explicit*/ false, /*BuildAndDiagnose*/ true, |
| 7050 | &FunctionScopeIndexOfCapturableLambda); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7051 | } |
| 7052 | } |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7053 | |
| 7054 | // Reset all the potential captures at the end of each full-expression. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7055 | CurrentLSI->clearPotentialCaptures(); |
| 7056 | } |
| 7057 | |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7058 | static ExprResult attemptRecovery(Sema &SemaRef, |
| 7059 | const TypoCorrectionConsumer &Consumer, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 7060 | const TypoCorrection &TC) { |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7061 | LookupResult R(SemaRef, Consumer.getLookupResult().getLookupNameInfo(), |
| 7062 | Consumer.getLookupResult().getLookupKind()); |
| 7063 | const CXXScopeSpec *SS = Consumer.getSS(); |
| 7064 | CXXScopeSpec NewSS; |
| 7065 | |
| 7066 | // Use an approprate CXXScopeSpec for building the expr. |
| 7067 | if (auto *NNS = TC.getCorrectionSpecifier()) |
| 7068 | NewSS.MakeTrivial(SemaRef.Context, NNS, TC.getCorrectionRange()); |
| 7069 | else if (SS && !TC.WillReplaceSpecifier()) |
| 7070 | NewSS = *SS; |
| 7071 | |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7072 | if (auto *ND = TC.getFoundDecl()) { |
Nick Lewycky | 01ad4ae | 2014-12-13 02:54:28 +0000 | [diff] [blame] | 7073 | R.setLookupName(ND->getDeclName()); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7074 | R.addDecl(ND); |
| 7075 | if (ND->isCXXClassMember()) { |
Nick Lewycky | 01ad4ae | 2014-12-13 02:54:28 +0000 | [diff] [blame] | 7076 | // Figure out the correct naming class to add to the LookupResult. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7077 | CXXRecordDecl *Record = nullptr; |
| 7078 | if (auto *NNS = TC.getCorrectionSpecifier()) |
| 7079 | Record = NNS->getAsType()->getAsCXXRecordDecl(); |
| 7080 | if (!Record) |
Olivier Goffart | ed13fab | 2015-01-09 09:37:26 +0000 | [diff] [blame] | 7081 | Record = |
| 7082 | dyn_cast<CXXRecordDecl>(ND->getDeclContext()->getRedeclContext()); |
| 7083 | if (Record) |
| 7084 | R.setNamingClass(Record); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7085 | |
| 7086 | // Detect and handle the case where the decl might be an implicit |
| 7087 | // member. |
| 7088 | bool MightBeImplicitMember; |
| 7089 | if (!Consumer.isAddressOfOperand()) |
| 7090 | MightBeImplicitMember = true; |
| 7091 | else if (!NewSS.isEmpty()) |
| 7092 | MightBeImplicitMember = false; |
| 7093 | else if (R.isOverloadedResult()) |
| 7094 | MightBeImplicitMember = false; |
| 7095 | else if (R.isUnresolvableResult()) |
| 7096 | MightBeImplicitMember = true; |
| 7097 | else |
| 7098 | MightBeImplicitMember = isa<FieldDecl>(ND) || |
| 7099 | isa<IndirectFieldDecl>(ND) || |
| 7100 | isa<MSPropertyDecl>(ND); |
| 7101 | |
| 7102 | if (MightBeImplicitMember) |
| 7103 | return SemaRef.BuildPossibleImplicitMemberExpr( |
| 7104 | NewSS, /*TemplateKWLoc*/ SourceLocation(), R, |
Aaron Ballman | 6924dcd | 2015-09-01 14:49:24 +0000 | [diff] [blame] | 7105 | /*TemplateArgs*/ nullptr, /*S*/ nullptr); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7106 | } else if (auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
| 7107 | return SemaRef.LookupInObjCMethod(R, Consumer.getScope(), |
| 7108 | Ivar->getIdentifier()); |
| 7109 | } |
| 7110 | } |
| 7111 | |
Kaelyn Takata | 6f71ce2 | 2014-11-20 22:06:33 +0000 | [diff] [blame] | 7112 | return SemaRef.BuildDeclarationNameExpr(NewSS, R, /*NeedsADL*/ false, |
| 7113 | /*AcceptInvalidDecl*/ true); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7114 | } |
| 7115 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7116 | namespace { |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7117 | class FindTypoExprs : public RecursiveASTVisitor<FindTypoExprs> { |
| 7118 | llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs; |
| 7119 | |
| 7120 | public: |
| 7121 | explicit FindTypoExprs(llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs) |
| 7122 | : TypoExprs(TypoExprs) {} |
| 7123 | bool VisitTypoExpr(TypoExpr *TE) { |
| 7124 | TypoExprs.insert(TE); |
| 7125 | return true; |
| 7126 | } |
| 7127 | }; |
| 7128 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7129 | class TransformTypos : public TreeTransform<TransformTypos> { |
| 7130 | typedef TreeTransform<TransformTypos> BaseTransform; |
| 7131 | |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7132 | VarDecl *InitDecl; // A decl to avoid as a correction because it is in the |
| 7133 | // process of being initialized. |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7134 | llvm::function_ref<ExprResult(Expr *)> ExprFilter; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7135 | llvm::SmallSetVector<TypoExpr *, 2> TypoExprs, AmbiguousTypoExprs; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7136 | llvm::SmallDenseMap<TypoExpr *, ExprResult, 2> TransformCache; |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7137 | llvm::SmallDenseMap<OverloadExpr *, Expr *, 4> OverloadResolution; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7138 | |
| 7139 | /// \brief Emit diagnostics for all of the TypoExprs encountered. |
| 7140 | /// If the TypoExprs were successfully corrected, then the diagnostics should |
| 7141 | /// suggest the corrections. Otherwise the diagnostics will not suggest |
| 7142 | /// anything (having been passed an empty TypoCorrection). |
| 7143 | void EmitAllDiagnostics() { |
| 7144 | for (auto E : TypoExprs) { |
| 7145 | TypoExpr *TE = cast<TypoExpr>(E); |
| 7146 | auto &State = SemaRef.getTypoExprState(TE); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7147 | if (State.DiagHandler) { |
| 7148 | TypoCorrection TC = State.Consumer->getCurrentCorrection(); |
| 7149 | ExprResult Replacement = TransformCache[TE]; |
| 7150 | |
| 7151 | // Extract the NamedDecl from the transformed TypoExpr and add it to the |
| 7152 | // TypoCorrection, replacing the existing decls. This ensures the right |
| 7153 | // NamedDecl is used in diagnostics e.g. in the case where overload |
| 7154 | // resolution was used to select one from several possible decls that |
| 7155 | // had been stored in the TypoCorrection. |
| 7156 | if (auto *ND = getDeclFromExpr( |
| 7157 | Replacement.isInvalid() ? nullptr : Replacement.get())) |
| 7158 | TC.setCorrectionDecl(ND); |
| 7159 | |
| 7160 | State.DiagHandler(TC); |
| 7161 | } |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7162 | SemaRef.clearDelayedTypo(TE); |
| 7163 | } |
| 7164 | } |
| 7165 | |
| 7166 | /// \brief If corrections for the first TypoExpr have been exhausted for a |
| 7167 | /// given combination of the other TypoExprs, retry those corrections against |
| 7168 | /// the next combination of substitutions for the other TypoExprs by advancing |
| 7169 | /// to the next potential correction of the second TypoExpr. For the second |
| 7170 | /// and subsequent TypoExprs, if its stream of corrections has been exhausted, |
| 7171 | /// the stream is reset and the next TypoExpr's stream is advanced by one (a |
| 7172 | /// TypoExpr's correction stream is advanced by removing the TypoExpr from the |
| 7173 | /// TransformCache). Returns true if there is still any untried combinations |
| 7174 | /// of corrections. |
| 7175 | bool CheckAndAdvanceTypoExprCorrectionStreams() { |
| 7176 | for (auto TE : TypoExprs) { |
| 7177 | auto &State = SemaRef.getTypoExprState(TE); |
| 7178 | TransformCache.erase(TE); |
| 7179 | if (!State.Consumer->finished()) |
| 7180 | return true; |
| 7181 | State.Consumer->resetCorrectionStream(); |
| 7182 | } |
| 7183 | return false; |
| 7184 | } |
| 7185 | |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7186 | NamedDecl *getDeclFromExpr(Expr *E) { |
| 7187 | if (auto *OE = dyn_cast_or_null<OverloadExpr>(E)) |
| 7188 | E = OverloadResolution[OE]; |
| 7189 | |
| 7190 | if (!E) |
| 7191 | return nullptr; |
| 7192 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7193 | return DRE->getFoundDecl(); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7194 | if (auto *ME = dyn_cast<MemberExpr>(E)) |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7195 | return ME->getFoundDecl(); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7196 | // FIXME: Add any other expr types that could be be seen by the delayed typo |
| 7197 | // correction TreeTransform for which the corresponding TypoCorrection could |
Nick Lewycky | 39f9dbc | 2014-12-16 21:48:39 +0000 | [diff] [blame] | 7198 | // contain multiple decls. |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7199 | return nullptr; |
| 7200 | } |
| 7201 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7202 | ExprResult TryTransform(Expr *E) { |
| 7203 | Sema::SFINAETrap Trap(SemaRef); |
| 7204 | ExprResult Res = TransformExpr(E); |
| 7205 | if (Trap.hasErrorOccurred() || Res.isInvalid()) |
| 7206 | return ExprError(); |
| 7207 | |
| 7208 | return ExprFilter(Res.get()); |
| 7209 | } |
| 7210 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7211 | public: |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7212 | TransformTypos(Sema &SemaRef, VarDecl *InitDecl, llvm::function_ref<ExprResult(Expr *)> Filter) |
| 7213 | : BaseTransform(SemaRef), InitDecl(InitDecl), ExprFilter(Filter) {} |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7214 | |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7215 | ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc, |
| 7216 | MultiExprArg Args, |
| 7217 | SourceLocation RParenLoc, |
| 7218 | Expr *ExecConfig = nullptr) { |
| 7219 | auto Result = BaseTransform::RebuildCallExpr(Callee, LParenLoc, Args, |
| 7220 | RParenLoc, ExecConfig); |
| 7221 | if (auto *OE = dyn_cast<OverloadExpr>(Callee)) { |
Reid Kleckner | a9e65ba | 2014-12-13 01:11:23 +0000 | [diff] [blame] | 7222 | if (Result.isUsable()) { |
Reid Kleckner | a7fe33e | 2014-12-13 00:53:10 +0000 | [diff] [blame] | 7223 | Expr *ResultCall = Result.get(); |
| 7224 | if (auto *BE = dyn_cast<CXXBindTemporaryExpr>(ResultCall)) |
| 7225 | ResultCall = BE->getSubExpr(); |
| 7226 | if (auto *CE = dyn_cast<CallExpr>(ResultCall)) |
| 7227 | OverloadResolution[OE] = CE->getCallee(); |
| 7228 | } |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7229 | } |
| 7230 | return Result; |
| 7231 | } |
| 7232 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7233 | ExprResult TransformLambdaExpr(LambdaExpr *E) { return Owned(E); } |
| 7234 | |
Saleem Abdulrasool | a174241 | 2015-10-31 00:39:15 +0000 | [diff] [blame] | 7235 | ExprResult TransformBlockExpr(BlockExpr *E) { return Owned(E); } |
| 7236 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7237 | ExprResult Transform(Expr *E) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7238 | ExprResult Res; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7239 | while (true) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7240 | Res = TryTransform(E); |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7241 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7242 | // Exit if either the transform was valid or if there were no TypoExprs |
| 7243 | // to transform that still have any untried correction candidates.. |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7244 | if (!Res.isInvalid() || |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7245 | !CheckAndAdvanceTypoExprCorrectionStreams()) |
| 7246 | break; |
| 7247 | } |
| 7248 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7249 | // Ensure none of the TypoExprs have multiple typo correction candidates |
| 7250 | // with the same edit length that pass all the checks and filters. |
| 7251 | // TODO: Properly handle various permutations of possible corrections when |
| 7252 | // there is more than one potentially ambiguous typo correction. |
Kaelyn Takata | 26ffc5f | 2015-06-25 23:47:39 +0000 | [diff] [blame] | 7253 | // Also, disable typo correction while attempting the transform when |
| 7254 | // handling potentially ambiguous typo corrections as any new TypoExprs will |
| 7255 | // have been introduced by the application of one of the correction |
| 7256 | // candidates and add little to no value if corrected. |
| 7257 | SemaRef.DisableTypoCorrection = true; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7258 | while (!AmbiguousTypoExprs.empty()) { |
| 7259 | auto TE = AmbiguousTypoExprs.back(); |
| 7260 | auto Cached = TransformCache[TE]; |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7261 | auto &State = SemaRef.getTypoExprState(TE); |
| 7262 | State.Consumer->saveCurrentPosition(); |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7263 | TransformCache.erase(TE); |
| 7264 | if (!TryTransform(E).isInvalid()) { |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7265 | State.Consumer->resetCorrectionStream(); |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7266 | TransformCache.erase(TE); |
| 7267 | Res = ExprError(); |
| 7268 | break; |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7269 | } |
| 7270 | AmbiguousTypoExprs.remove(TE); |
| 7271 | State.Consumer->restoreSavedPosition(); |
| 7272 | TransformCache[TE] = Cached; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7273 | } |
Kaelyn Takata | 26ffc5f | 2015-06-25 23:47:39 +0000 | [diff] [blame] | 7274 | SemaRef.DisableTypoCorrection = false; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7275 | |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7276 | // 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] | 7277 | if (!Res.isUsable()) |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7278 | FindTypoExprs(TypoExprs).TraverseStmt(E); |
| 7279 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7280 | EmitAllDiagnostics(); |
| 7281 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7282 | return Res; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7283 | } |
| 7284 | |
| 7285 | ExprResult TransformTypoExpr(TypoExpr *E) { |
| 7286 | // If the TypoExpr hasn't been seen before, record it. Otherwise, return the |
| 7287 | // cached transformation result if there is one and the TypoExpr isn't the |
| 7288 | // first one that was encountered. |
| 7289 | auto &CacheEntry = TransformCache[E]; |
| 7290 | if (!TypoExprs.insert(E) && !CacheEntry.isUnset()) { |
| 7291 | return CacheEntry; |
| 7292 | } |
| 7293 | |
| 7294 | auto &State = SemaRef.getTypoExprState(E); |
| 7295 | assert(State.Consumer && "Cannot transform a cleared TypoExpr"); |
| 7296 | |
| 7297 | // For the first TypoExpr and an uncached TypoExpr, find the next likely |
| 7298 | // typo correction and return it. |
| 7299 | while (TypoCorrection TC = State.Consumer->getNextCorrection()) { |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7300 | if (InitDecl && TC.getFoundDecl() == InitDecl) |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7301 | continue; |
Richard Smith | 1cf4541 | 2017-01-04 23:14:16 +0000 | [diff] [blame] | 7302 | // FIXME: If we would typo-correct to an invalid declaration, it's |
| 7303 | // probably best to just suppress all errors from this typo correction. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7304 | ExprResult NE = State.RecoveryHandler ? |
| 7305 | State.RecoveryHandler(SemaRef, E, TC) : |
| 7306 | attemptRecovery(SemaRef, *State.Consumer, TC); |
| 7307 | if (!NE.isInvalid()) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7308 | // Check whether there may be a second viable correction with the same |
| 7309 | // edit distance; if so, remember this TypoExpr may have an ambiguous |
| 7310 | // correction so it can be more thoroughly vetted later. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7311 | TypoCorrection Next; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7312 | if ((Next = State.Consumer->peekNextCorrection()) && |
| 7313 | Next.getEditDistance(false) == TC.getEditDistance(false)) { |
| 7314 | AmbiguousTypoExprs.insert(E); |
| 7315 | } else { |
| 7316 | AmbiguousTypoExprs.remove(E); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7317 | } |
| 7318 | assert(!NE.isUnset() && |
| 7319 | "Typo was transformed into a valid-but-null ExprResult"); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7320 | return CacheEntry = NE; |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7321 | } |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7322 | } |
| 7323 | return CacheEntry = ExprError(); |
| 7324 | } |
| 7325 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7326 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7327 | |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7328 | ExprResult |
| 7329 | Sema::CorrectDelayedTyposInExpr(Expr *E, VarDecl *InitDecl, |
| 7330 | llvm::function_ref<ExprResult(Expr *)> Filter) { |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7331 | // If the current evaluation context indicates there are uncorrected typos |
| 7332 | // and the current expression isn't guaranteed to not have typos, try to |
| 7333 | // resolve any TypoExpr nodes that might be in the expression. |
Kaelyn Takata | c71dda2 | 2014-12-02 22:05:35 +0000 | [diff] [blame] | 7334 | if (E && !ExprEvalContexts.empty() && ExprEvalContexts.back().NumTypos && |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7335 | (E->isTypeDependent() || E->isValueDependent() || |
| 7336 | E->isInstantiationDependent())) { |
Kaelyn Takata | c49838b | 2015-01-28 21:10:46 +0000 | [diff] [blame] | 7337 | auto TyposInContext = ExprEvalContexts.back().NumTypos; |
| 7338 | assert(TyposInContext < ~0U && "Recursive call of CorrectDelayedTyposInExpr"); |
| 7339 | ExprEvalContexts.back().NumTypos = ~0U; |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7340 | auto TyposResolved = DelayedTypos.size(); |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7341 | auto Result = TransformTypos(*this, InitDecl, Filter).Transform(E); |
Kaelyn Takata | c49838b | 2015-01-28 21:10:46 +0000 | [diff] [blame] | 7342 | ExprEvalContexts.back().NumTypos = TyposInContext; |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7343 | TyposResolved -= DelayedTypos.size(); |
Nick Lewycky | 4d59b77 | 2014-12-16 22:02:06 +0000 | [diff] [blame] | 7344 | if (Result.isInvalid() || Result.get() != E) { |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7345 | ExprEvalContexts.back().NumTypos -= TyposResolved; |
| 7346 | return Result; |
| 7347 | } |
Nick Lewycky | 4d59b77 | 2014-12-16 22:02:06 +0000 | [diff] [blame] | 7348 | assert(TyposResolved == 0 && "Corrected typo but got same Expr back?"); |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7349 | } |
| 7350 | return E; |
| 7351 | } |
| 7352 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7353 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC, |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7354 | bool DiscardedValue, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7355 | bool IsConstexpr, |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7356 | bool IsLambdaInitCaptureInitializer) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7357 | ExprResult FullExpr = FE; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7358 | |
| 7359 | if (!FullExpr.get()) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 7360 | return ExprError(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7361 | |
| 7362 | // If we are an init-expression in a lambdas init-capture, we should not |
| 7363 | // diagnose an unexpanded pack now (will be diagnosed once lambda-expr |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7364 | // containing full-expression is done). |
| 7365 | // template<class ... Ts> void test(Ts ... t) { |
| 7366 | // test([&a(t)]() { <-- (t) is an init-expr that shouldn't be diagnosed now. |
| 7367 | // return a; |
| 7368 | // }() ...); |
| 7369 | // } |
| 7370 | // FIXME: This is a hack. It would be better if we pushed the lambda scope |
| 7371 | // when we parse the lambda introducer, and teach capturing (but not |
| 7372 | // unexpanded pack detection) to walk over LambdaScopeInfos which don't have a |
| 7373 | // corresponding class yet (that is, have LambdaScopeInfo either represent a |
| 7374 | // lambda where we've entered the introducer but not the body, or represent a |
| 7375 | // lambda where we've entered the body, depending on where the |
| 7376 | // parser/instantiation has got to). |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7377 | if (!IsLambdaInitCaptureInitializer && |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7378 | DiagnoseUnexpandedParameterPack(FullExpr.get())) |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 7379 | return ExprError(); |
| 7380 | |
Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7381 | // Top-level expressions default to 'id' when we're in a debugger. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7382 | if (DiscardedValue && getLangOpts().DebuggerCastResultToId && |
Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7383 | FullExpr.get()->getType() == Context.UnknownAnyTy) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7384 | FullExpr = forceUnknownAnyToType(FullExpr.get(), Context.getObjCIdType()); |
Douglas Gregor | 95715f9 | 2011-12-15 00:53:32 +0000 | [diff] [blame] | 7385 | if (FullExpr.isInvalid()) |
| 7386 | return ExprError(); |
| 7387 | } |
Douglas Gregor | 0ec210b | 2011-03-07 02:05:23 +0000 | [diff] [blame] | 7388 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7389 | if (DiscardedValue) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7390 | FullExpr = CheckPlaceholderExpr(FullExpr.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7391 | if (FullExpr.isInvalid()) |
| 7392 | return ExprError(); |
| 7393 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7394 | FullExpr = IgnoredValueConversions(FullExpr.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7395 | if (FullExpr.isInvalid()) |
| 7396 | return ExprError(); |
| 7397 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7398 | |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7399 | FullExpr = CorrectDelayedTyposInExpr(FullExpr.get()); |
| 7400 | if (FullExpr.isInvalid()) |
| 7401 | return ExprError(); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7402 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7403 | CheckCompletedExpr(FullExpr.get(), CC, IsConstexpr); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7404 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7405 | // At the end of this full expression (which could be a deeply nested |
| 7406 | // lambda), if there is a potential capture within the nested lambda, |
Faisal Vali | 218e94b | 2013-11-12 03:56:08 +0000 | [diff] [blame] | 7407 | // have the outer capture-able lambda try and capture it. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7408 | // Consider the following code: |
| 7409 | // void f(int, int); |
| 7410 | // void f(const int&, double); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7411 | // void foo() { |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7412 | // const int x = 10, y = 20; |
| 7413 | // auto L = [=](auto a) { |
| 7414 | // auto M = [=](auto b) { |
| 7415 | // f(x, b); <-- requires x to be captured by L and M |
| 7416 | // f(y, a); <-- requires y to be captured by L, but not all Ms |
| 7417 | // }; |
| 7418 | // }; |
| 7419 | // } |
| 7420 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7421 | // FIXME: Also consider what happens for something like this that involves |
| 7422 | // the gnu-extension statement-expressions or even lambda-init-captures: |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7423 | // void f() { |
| 7424 | // const int n = 0; |
| 7425 | // auto L = [&](auto a) { |
| 7426 | // +n + ({ 0; a; }); |
| 7427 | // }; |
| 7428 | // } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7429 | // |
| 7430 | // Here, we see +n, and then the full-expression 0; ends, so we don't |
| 7431 | // capture n (and instead remove it from our list of potential captures), |
| 7432 | // 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] | 7433 | // for us to see that we need to capture n after all. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7434 | |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7435 | LambdaScopeInfo *const CurrentLSI = |
| 7436 | getCurLambda(/*IgnoreCapturedRegions=*/true); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7437 | // FIXME: PR 17877 showed that getCurLambda() can return a valid pointer |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7438 | // 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] | 7439 | // for an example of the code that might cause this asynchrony. |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7440 | // By ensuring we are in the context of a lambda's call operator |
| 7441 | // 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] | 7442 | // 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] | 7443 | // PR, a proper fix would entail : |
| 7444 | // "Alternative suggestion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7445 | // - Add to Sema an integer holding the smallest (outermost) scope |
| 7446 | // index that we are *lexically* within, and save/restore/set to |
| 7447 | // FunctionScopes.size() in InstantiatingTemplate's |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7448 | // constructor/destructor. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7449 | // - Teach the handful of places that iterate over FunctionScopes to |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7450 | // stop at the outermost enclosing lexical scope." |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7451 | DeclContext *DC = CurContext; |
| 7452 | while (DC && isa<CapturedDecl>(DC)) |
| 7453 | DC = DC->getParent(); |
| 7454 | const bool IsInLambdaDeclContext = isLambdaCallOperator(DC); |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7455 | if (IsInLambdaDeclContext && CurrentLSI && |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7456 | CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid()) |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7457 | CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(FE, CurrentLSI, |
| 7458 | *this); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7459 | return MaybeCreateExprWithCleanups(FullExpr); |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 7460 | } |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 7461 | |
| 7462 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
| 7463 | if (!FullStmt) return StmtError(); |
| 7464 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7465 | return MaybeCreateStmtWithCleanups(FullStmt); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 7466 | } |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7467 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7468 | Sema::IfExistsResult |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7469 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, |
| 7470 | CXXScopeSpec &SS, |
| 7471 | const DeclarationNameInfo &TargetNameInfo) { |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7472 | DeclarationName TargetName = TargetNameInfo.getName(); |
| 7473 | if (!TargetName) |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7474 | return IER_DoesNotExist; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7475 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7476 | // If the name itself is dependent, then the result is dependent. |
| 7477 | if (TargetName.isDependentName()) |
| 7478 | return IER_Dependent; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7479 | |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7480 | // Do the redeclaration lookup in the current scope. |
| 7481 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
| 7482 | Sema::NotForRedeclaration); |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7483 | LookupParsedName(R, S, &SS); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7484 | R.suppressDiagnostics(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7485 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7486 | switch (R.getResultKind()) { |
| 7487 | case LookupResult::Found: |
| 7488 | case LookupResult::FoundOverloaded: |
| 7489 | case LookupResult::FoundUnresolvedValue: |
| 7490 | case LookupResult::Ambiguous: |
| 7491 | return IER_Exists; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7492 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7493 | case LookupResult::NotFound: |
| 7494 | return IER_DoesNotExist; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7495 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7496 | case LookupResult::NotFoundInCurrentInstantiation: |
| 7497 | return IER_Dependent; |
| 7498 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7499 | |
| 7500 | llvm_unreachable("Invalid LookupResult Kind!"); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7501 | } |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7502 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7503 | Sema::IfExistsResult |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 7504 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc, |
| 7505 | bool IsIfExists, CXXScopeSpec &SS, |
| 7506 | UnqualifiedId &Name) { |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7507 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7508 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 7509 | // Check for an unexpanded parameter pack. |
| 7510 | auto UPPC = IsIfExists ? UPPC_IfExists : UPPC_IfNotExists; |
| 7511 | if (DiagnoseUnexpandedParameterPack(SS, UPPC) || |
| 7512 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC)) |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 7513 | return IER_Error; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7514 | |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7515 | return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo); |
| 7516 | } |