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(); |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame^] | 192 | if (Step == 0 && LookupCtx) { |
| 193 | if (RequireCompleteDeclContext(SS, LookupCtx)) |
| 194 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 195 | LookupQualifiedName(Found, LookupCtx); |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame^] | 196 | } else if (Step == 1 && LookInScope && S) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 197 | LookupName(Found, S); |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame^] | 198 | } else { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 199 | continue; |
John McCall | cb73154 | 2017-06-11 20:33:00 +0000 | [diff] [blame^] | 200 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 201 | |
| 202 | // FIXME: Should we be suppressing ambiguities here? |
| 203 | if (Found.isAmbiguous()) |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 204 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 205 | |
| 206 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
| 207 | QualType T = Context.getTypeDeclType(Type); |
Nico Weber | 83a6387 | 2014-11-12 04:33:52 +0000 | [diff] [blame] | 208 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 209 | |
| 210 | if (SearchType.isNull() || SearchType->isDependentType() || |
| 211 | Context.hasSameUnqualifiedType(T, SearchType)) { |
| 212 | // We found our type! |
| 213 | |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 214 | return CreateParsedType(T, |
| 215 | Context.getTrivialTypeSourceInfo(T, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 216 | } |
John Wiegley | b4a9e51 | 2011-03-08 08:13:22 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 218 | if (!SearchType.isNull()) |
| 219 | NonMatchingTypeDecl = Type; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // If the name that we found is a class template name, and it is |
| 223 | // the same name as the template name in the last part of the |
| 224 | // nested-name-specifier (if present) or the object type, then |
| 225 | // this is the destructor for that class. |
| 226 | // FIXME: This is a workaround until we get real drafting for core |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 227 | // issue 399, for which there isn't even an obvious direction. |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 228 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
| 229 | QualType MemberOfType; |
| 230 | if (SS.isSet()) { |
| 231 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
| 232 | // Figure out the type of the context, if it has one. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 233 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) |
| 234 | MemberOfType = Context.getTypeDeclType(Record); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | if (MemberOfType.isNull()) |
| 238 | MemberOfType = SearchType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 239 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 240 | if (MemberOfType.isNull()) |
| 241 | continue; |
| 242 | |
| 243 | // We're referring into a class template specialization. If the |
| 244 | // class template we found is the same as the template being |
| 245 | // specialized, we found what we are looking for. |
| 246 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
| 247 | if (ClassTemplateSpecializationDecl *Spec |
| 248 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
| 249 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
| 250 | Template->getCanonicalDecl()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 251 | return CreateParsedType( |
| 252 | MemberOfType, |
| 253 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | continue; |
| 257 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 258 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 259 | // We're referring to an unresolved class template |
| 260 | // specialization. Determine whether we class template we found |
| 261 | // is the same as the template being specialized or, if we don't |
| 262 | // know which template is being specialized, that it at least |
| 263 | // has the same name. |
| 264 | if (const TemplateSpecializationType *SpecType |
| 265 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
| 266 | TemplateName SpecName = SpecType->getTemplateName(); |
| 267 | |
| 268 | // The class template we found is the same template being |
| 269 | // specialized. |
| 270 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
| 271 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 272 | return CreateParsedType( |
| 273 | MemberOfType, |
| 274 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 275 | |
| 276 | continue; |
| 277 | } |
| 278 | |
| 279 | // The class template we found has the same name as the |
| 280 | // (dependent) template name being specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 281 | if (DependentTemplateName *DepTemplate |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 282 | = SpecName.getAsDependentTemplateName()) { |
| 283 | if (DepTemplate->isIdentifier() && |
| 284 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
Richard Smith | c278c00 | 2014-01-22 00:30:17 +0000 | [diff] [blame] | 285 | return CreateParsedType( |
| 286 | MemberOfType, |
| 287 | Context.getTrivialTypeSourceInfo(MemberOfType, NameLoc)); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 288 | |
| 289 | continue; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (isDependent) { |
| 296 | // We didn't find our type, but that's okay: it's dependent |
| 297 | // anyway. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 298 | |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 299 | // FIXME: What if we have no nested-name-specifier? |
| 300 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), |
| 301 | SS.getWithLocInContext(Context), |
| 302 | II, NameLoc); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 303 | return ParsedType::make(T); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 306 | if (NonMatchingTypeDecl) { |
| 307 | QualType T = Context.getTypeDeclType(NonMatchingTypeDecl); |
| 308 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) |
| 309 | << T << SearchType; |
| 310 | Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here) |
| 311 | << T; |
| 312 | } else if (ObjectTypePtr) |
| 313 | Diag(NameLoc, diag::err_ident_in_dtor_not_a_type) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 314 | << &II; |
David Blaikie | 5e026f5 | 2013-03-20 17:42:13 +0000 | [diff] [blame] | 315 | else { |
| 316 | SemaDiagnosticBuilder DtorDiag = Diag(NameLoc, |
| 317 | diag::err_destructor_class_name); |
| 318 | if (S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 319 | const DeclContext *Ctx = S->getEntity(); |
David Blaikie | 5e026f5 | 2013-03-20 17:42:13 +0000 | [diff] [blame] | 320 | if (const CXXRecordDecl *Class = dyn_cast_or_null<CXXRecordDecl>(Ctx)) |
| 321 | DtorDiag << FixItHint::CreateReplacement(SourceRange(NameLoc), |
| 322 | Class->getNameAsString()); |
| 323 | } |
| 324 | } |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 325 | |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 326 | return nullptr; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 329 | ParsedType Sema::getDestructorTypeForDecltype(const DeclSpec &DS, |
| 330 | ParsedType ObjectType) { |
| 331 | if (DS.getTypeSpecType() == DeclSpec::TST_error) |
| 332 | return nullptr; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 333 | |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 334 | if (DS.getTypeSpecType() == DeclSpec::TST_decltype_auto) { |
| 335 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decltype_auto_invalid); |
| 336 | return nullptr; |
| 337 | } |
| 338 | |
| 339 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype && |
| 340 | "unexpected type in getDestructorType"); |
| 341 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 342 | |
| 343 | // If we know the type of the object, check that the correct destructor |
| 344 | // type was named now; we can give better diagnostics this way. |
| 345 | QualType SearchType = GetTypeFromParser(ObjectType); |
| 346 | if (!SearchType.isNull() && !SearchType->isDependentType() && |
| 347 | !Context.hasSameUnqualifiedType(T, SearchType)) { |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 348 | Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch) |
| 349 | << T << SearchType; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 350 | return nullptr; |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | return ParsedType::make(T); |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 356 | bool Sema::checkLiteralOperatorId(const CXXScopeSpec &SS, |
| 357 | const UnqualifiedId &Name) { |
| 358 | assert(Name.getKind() == UnqualifiedId::IK_LiteralOperatorId); |
| 359 | |
| 360 | if (!SS.isValid()) |
| 361 | return false; |
| 362 | |
| 363 | switch (SS.getScopeRep()->getKind()) { |
| 364 | case NestedNameSpecifier::Identifier: |
| 365 | case NestedNameSpecifier::TypeSpec: |
| 366 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 367 | // Per C++11 [over.literal]p2, literal operators can only be declared at |
| 368 | // namespace scope. Therefore, this unqualified-id cannot name anything. |
| 369 | // Reject it early, because we have no AST representation for this in the |
| 370 | // case where the scope is dependent. |
| 371 | Diag(Name.getLocStart(), diag::err_literal_operator_id_outside_namespace) |
| 372 | << SS.getScopeRep(); |
| 373 | return true; |
| 374 | |
| 375 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 376 | case NestedNameSpecifier::Super: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 377 | case NestedNameSpecifier::Namespace: |
| 378 | case NestedNameSpecifier::NamespaceAlias: |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | llvm_unreachable("unknown nested name specifier kind"); |
| 383 | } |
| 384 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 385 | /// \brief Build a C++ typeid expression with a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 386 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 387 | SourceLocation TypeidLoc, |
| 388 | TypeSourceInfo *Operand, |
| 389 | SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 390 | // C++ [expr.typeid]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 391 | // 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] | 392 | // that is the operand of typeid are always ignored. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 393 | // 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] | 394 | // type, the class shall be completely-defined. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 395 | Qualifiers Quals; |
| 396 | QualType T |
| 397 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
| 398 | Quals); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 399 | if (T->getAs<RecordType>() && |
| 400 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 401 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 402 | |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 403 | if (T->isVariablyModifiedType()) |
| 404 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) << T); |
| 405 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 406 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), Operand, |
| 407 | SourceRange(TypeidLoc, RParenLoc)); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /// \brief Build a C++ typeid expression with an expression operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 411 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 412 | SourceLocation TypeidLoc, |
| 413 | Expr *E, |
| 414 | SourceLocation RParenLoc) { |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 415 | bool WasEvaluated = false; |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 416 | if (E && !E->isTypeDependent()) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 417 | if (E->getType()->isPlaceholderType()) { |
| 418 | ExprResult result = CheckPlaceholderExpr(E); |
| 419 | if (result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 420 | E = result.get(); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 423 | QualType T = E->getType(); |
| 424 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
| 425 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 426 | // C++ [expr.typeid]p3: |
| 427 | // [...] If the type of the expression is a class type, the class |
| 428 | // shall be completely-defined. |
| 429 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 430 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 431 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 432 | // C++ [expr.typeid]p3: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 433 | // 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] | 434 | // polymorphic class type [...] [the] expression is an unevaluated |
| 435 | // operand. [...] |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 436 | if (RecordD->isPolymorphic() && E->isGLValue()) { |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 437 | // The subexpression is potentially evaluated; switch the context |
| 438 | // and recheck the subexpression. |
Benjamin Kramer | d81108f | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 439 | ExprResult Result = TransformToPotentiallyEvaluated(E); |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 440 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 441 | E = Result.get(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 442 | |
| 443 | // We require a vtable to query the type at run time. |
| 444 | MarkVTableUsed(TypeidLoc, RecordD); |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 445 | WasEvaluated = true; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 446 | } |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 447 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 449 | // C++ [expr.typeid]p4: |
| 450 | // [...] 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] | 451 | // cv-qualified type, the result of the typeid expression refers to a |
| 452 | // std::type_info object representing the cv-unqualified referenced |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 453 | // type. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 454 | Qualifiers Quals; |
| 455 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
| 456 | if (!Context.hasSameType(T, UnqualT)) { |
| 457 | T = UnqualT; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 458 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).get(); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 461 | |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 462 | if (E->getType()->isVariablyModifiedType()) |
| 463 | return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid) |
| 464 | << E->getType()); |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 465 | else if (!inTemplateInstantiation() && |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 466 | E->HasSideEffects(Context, WasEvaluated)) { |
| 467 | // The expression operand for typeid is in an unevaluated expression |
| 468 | // context, so side effects could result in unintended consequences. |
| 469 | Diag(E->getExprLoc(), WasEvaluated |
| 470 | ? diag::warn_side_effects_typeid |
| 471 | : diag::warn_side_effects_unevaluated_context); |
| 472 | } |
David Majnemer | 6f3150a | 2014-11-21 21:09:12 +0000 | [diff] [blame] | 473 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 474 | return new (Context) CXXTypeidExpr(TypeInfoType.withConst(), E, |
| 475 | SourceRange(TypeidLoc, RParenLoc)); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 479 | ExprResult |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 480 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 481 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 482 | // Find the std::type_info type. |
Sebastian Redl | 7ac9741 | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 483 | if (!getStdNamespace()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 484 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 485 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 486 | if (!CXXTypeInfoDecl) { |
| 487 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 488 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 489 | LookupQualifiedName(R, getStdNamespace()); |
| 490 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
Nico Weber | 5f96883 | 2012-06-19 23:58:27 +0000 | [diff] [blame] | 491 | // Microsoft's typeinfo doesn't have type_info in std but in the global |
| 492 | // namespace if _HAS_EXCEPTIONS is defined to 0. See PR13153. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 493 | if (!CXXTypeInfoDecl && LangOpts.MSVCCompat) { |
Nico Weber | 5f96883 | 2012-06-19 23:58:27 +0000 | [diff] [blame] | 494 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 495 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 496 | } |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 497 | if (!CXXTypeInfoDecl) |
| 498 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | |
Nico Weber | 1b7f39d | 2012-05-20 01:27:21 +0000 | [diff] [blame] | 501 | if (!getLangOpts().RTTI) { |
| 502 | return ExprError(Diag(OpLoc, diag::err_no_typeid_with_fno_rtti)); |
| 503 | } |
| 504 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 505 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 506 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 507 | if (isType) { |
| 508 | // The operand is a type; handle it as such. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 509 | TypeSourceInfo *TInfo = nullptr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 510 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 511 | &TInfo); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 512 | if (T.isNull()) |
| 513 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 514 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 515 | if (!TInfo) |
| 516 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 517 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 518 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 519 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 521 | // The operand is an expression. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 522 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 523 | } |
| 524 | |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 525 | /// Grabs __declspec(uuid()) off a type, or returns 0 if we cannot resolve to |
| 526 | /// a single GUID. |
| 527 | static void |
| 528 | getUuidAttrOfType(Sema &SemaRef, QualType QT, |
| 529 | llvm::SmallSetVector<const UuidAttr *, 1> &UuidAttrs) { |
| 530 | // Optionally remove one level of pointer, reference or array indirection. |
| 531 | const Type *Ty = QT.getTypePtr(); |
| 532 | if (QT->isPointerType() || QT->isReferenceType()) |
| 533 | Ty = QT->getPointeeType().getTypePtr(); |
| 534 | else if (QT->isArrayType()) |
| 535 | Ty = Ty->getBaseElementTypeUnsafe(); |
| 536 | |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 537 | const auto *TD = Ty->getAsTagDecl(); |
| 538 | if (!TD) |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 539 | return; |
| 540 | |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 541 | if (const auto *Uuid = TD->getMostRecentDecl()->getAttr<UuidAttr>()) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 542 | UuidAttrs.insert(Uuid); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | // __uuidof can grab UUIDs from template arguments. |
Reid Kleckner | e516eab | 2016-12-13 18:58:09 +0000 | [diff] [blame] | 547 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(TD)) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 548 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
| 549 | for (const TemplateArgument &TA : TAL.asArray()) { |
| 550 | const UuidAttr *UuidForTA = nullptr; |
| 551 | if (TA.getKind() == TemplateArgument::Type) |
| 552 | getUuidAttrOfType(SemaRef, TA.getAsType(), UuidAttrs); |
| 553 | else if (TA.getKind() == TemplateArgument::Declaration) |
| 554 | getUuidAttrOfType(SemaRef, TA.getAsDecl()->getType(), UuidAttrs); |
| 555 | |
| 556 | if (UuidForTA) |
| 557 | UuidAttrs.insert(UuidForTA); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 562 | /// \brief Build a Microsoft __uuidof expression with a type operand. |
| 563 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 564 | SourceLocation TypeidLoc, |
| 565 | TypeSourceInfo *Operand, |
| 566 | SourceLocation RParenLoc) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 567 | StringRef UuidStr; |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 568 | if (!Operand->getType()->isDependentType()) { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 569 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
| 570 | getUuidAttrOfType(*this, Operand->getType(), UuidAttrs); |
| 571 | if (UuidAttrs.empty()) |
| 572 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 573 | if (UuidAttrs.size() > 1) |
| 574 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 575 | UuidStr = UuidAttrs.back()->getGuid(); |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 576 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 577 | |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 578 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), Operand, UuidStr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 579 | SourceRange(TypeidLoc, RParenLoc)); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /// \brief Build a Microsoft __uuidof expression with an expression operand. |
| 583 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 584 | SourceLocation TypeidLoc, |
| 585 | Expr *E, |
| 586 | SourceLocation RParenLoc) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 587 | StringRef UuidStr; |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 588 | if (!E->getType()->isDependentType()) { |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 589 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 590 | UuidStr = "00000000-0000-0000-0000-000000000000"; |
| 591 | } else { |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 592 | llvm::SmallSetVector<const UuidAttr *, 1> UuidAttrs; |
| 593 | getUuidAttrOfType(*this, E->getType(), UuidAttrs); |
| 594 | if (UuidAttrs.empty()) |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 595 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
David Majnemer | 1dbc7a7 | 2016-03-27 04:46:07 +0000 | [diff] [blame] | 596 | if (UuidAttrs.size() > 1) |
| 597 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_with_multiple_guids)); |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 598 | UuidStr = UuidAttrs.back()->getGuid(); |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 599 | } |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 600 | } |
David Majnemer | 59c0ec2 | 2013-09-07 06:59:46 +0000 | [diff] [blame] | 601 | |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 602 | return new (Context) CXXUuidofExpr(TypeInfoType.withConst(), E, UuidStr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 603 | SourceRange(TypeidLoc, RParenLoc)); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 607 | ExprResult |
| 608 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 609 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 610 | // If MSVCGuidDecl has not been cached, do the lookup. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 611 | if (!MSVCGuidDecl) { |
| 612 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 613 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 614 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 615 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 616 | if (!MSVCGuidDecl) |
| 617 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 620 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 621 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 622 | if (isType) { |
| 623 | // The operand is a type; handle it as such. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 624 | TypeSourceInfo *TInfo = nullptr; |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 625 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 626 | &TInfo); |
| 627 | if (T.isNull()) |
| 628 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 629 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 630 | if (!TInfo) |
| 631 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 632 | |
| 633 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 634 | } |
| 635 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 636 | // The operand is an expression. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 637 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 638 | } |
| 639 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 640 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 641 | ExprResult |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 642 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 643 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Bill Wendling | bf313b0 | 2007-02-13 20:09:46 +0000 | [diff] [blame] | 644 | "Unknown C++ Boolean value!"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 645 | return new (Context) |
| 646 | CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 647 | } |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 648 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 649 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 650 | ExprResult |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 651 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 652 | return new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 655 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 656 | ExprResult |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 657 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
| 658 | bool IsThrownVarInScope = false; |
| 659 | if (Ex) { |
| 660 | // C++0x [class.copymove]p31: |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 661 | // When certain criteria are met, an implementation is allowed to omit the |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 662 | // copy/move construction of a class object [...] |
| 663 | // |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 664 | // - in a throw-expression, when the operand is the name of a |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 665 | // non-volatile automatic object (other than a function or catch- |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 666 | // clause parameter) whose scope does not extend beyond the end of the |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 667 | // innermost enclosing try-block (if there is one), the copy/move |
| 668 | // operation from the operand to the exception object (15.1) can be |
| 669 | // omitted by constructing the automatic object directly into the |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 670 | // exception object |
| 671 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) |
| 672 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 673 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { |
| 674 | for( ; S; S = S->getParent()) { |
| 675 | if (S->isDeclScope(Var)) { |
| 676 | IsThrownVarInScope = true; |
| 677 | break; |
| 678 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 679 | |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 680 | if (S->getFlags() & |
| 681 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
| 682 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | |
| 683 | Scope::TryScope)) |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 689 | |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 690 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
| 691 | } |
| 692 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 693 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 694 | bool IsThrownVarInScope) { |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 695 | // Don't report an error if 'throw' is used in system headers. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 696 | if (!getLangOpts().CXXExceptions && |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 697 | !getSourceManager().isInSystemHeader(OpLoc)) |
Anders Carlsson | b94ad3e | 2011-02-19 21:53:09 +0000 | [diff] [blame] | 698 | Diag(OpLoc, diag::err_exceptions_disabled) << "throw"; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 699 | |
Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 700 | // Exceptions aren't allowed in CUDA device code. |
| 701 | if (getLangOpts().CUDA) |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 702 | CUDADiagIfDeviceCode(OpLoc, diag::err_cuda_device_exceptions) |
| 703 | << "throw" << CurrentCUDATarget(); |
Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 704 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 705 | if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope()) |
| 706 | Diag(OpLoc, diag::err_omp_simd_region_cannot_use_stmt) << "throw"; |
| 707 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 708 | if (Ex && !Ex->isTypeDependent()) { |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 709 | QualType ExceptionObjectTy = Context.getExceptionObjectType(Ex->getType()); |
| 710 | if (CheckCXXThrowOperand(OpLoc, ExceptionObjectTy, Ex)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 711 | return ExprError(); |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 712 | |
| 713 | // Initialize the exception result. This implicitly weeds out |
| 714 | // abstract types or types with inaccessible copy constructors. |
| 715 | |
| 716 | // C++0x [class.copymove]p31: |
| 717 | // When certain criteria are met, an implementation is allowed to omit the |
| 718 | // copy/move construction of a class object [...] |
| 719 | // |
| 720 | // - in a throw-expression, when the operand is the name of a |
| 721 | // non-volatile automatic object (other than a function or |
| 722 | // catch-clause |
| 723 | // parameter) whose scope does not extend beyond the end of the |
| 724 | // innermost enclosing try-block (if there is one), the copy/move |
| 725 | // operation from the operand to the exception object (15.1) can be |
| 726 | // omitted by constructing the automatic object directly into the |
| 727 | // exception object |
| 728 | const VarDecl *NRVOVariable = nullptr; |
| 729 | if (IsThrownVarInScope) |
| 730 | NRVOVariable = getCopyElisionCandidate(QualType(), Ex, false); |
| 731 | |
| 732 | InitializedEntity Entity = InitializedEntity::InitializeException( |
| 733 | OpLoc, ExceptionObjectTy, |
| 734 | /*NRVO=*/NRVOVariable != nullptr); |
| 735 | ExprResult Res = PerformMoveOrCopyInitialization( |
| 736 | Entity, NRVOVariable, QualType(), Ex, IsThrownVarInScope); |
| 737 | if (Res.isInvalid()) |
| 738 | return ExprError(); |
| 739 | Ex = Res.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 740 | } |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 741 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 742 | return new (Context) |
| 743 | CXXThrowExpr(Ex, Context.VoidTy, OpLoc, IsThrownVarInScope); |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 744 | } |
| 745 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 746 | static void |
| 747 | collectPublicBases(CXXRecordDecl *RD, |
| 748 | llvm::DenseMap<CXXRecordDecl *, unsigned> &SubobjectsSeen, |
| 749 | llvm::SmallPtrSetImpl<CXXRecordDecl *> &VBases, |
| 750 | llvm::SetVector<CXXRecordDecl *> &PublicSubobjectsSeen, |
| 751 | bool ParentIsPublic) { |
| 752 | for (const CXXBaseSpecifier &BS : RD->bases()) { |
| 753 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
| 754 | bool NewSubobject; |
| 755 | // Virtual bases constitute the same subobject. Non-virtual bases are |
| 756 | // always distinct subobjects. |
| 757 | if (BS.isVirtual()) |
| 758 | NewSubobject = VBases.insert(BaseDecl).second; |
| 759 | else |
| 760 | NewSubobject = true; |
| 761 | |
| 762 | if (NewSubobject) |
| 763 | ++SubobjectsSeen[BaseDecl]; |
| 764 | |
| 765 | // Only add subobjects which have public access throughout the entire chain. |
| 766 | bool PublicPath = ParentIsPublic && BS.getAccessSpecifier() == AS_public; |
| 767 | if (PublicPath) |
| 768 | PublicSubobjectsSeen.insert(BaseDecl); |
| 769 | |
| 770 | // Recurse on to each base subobject. |
| 771 | collectPublicBases(BaseDecl, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
| 772 | PublicPath); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | static void getUnambiguousPublicSubobjects( |
| 777 | CXXRecordDecl *RD, llvm::SmallVectorImpl<CXXRecordDecl *> &Objects) { |
| 778 | llvm::DenseMap<CXXRecordDecl *, unsigned> SubobjectsSeen; |
| 779 | llvm::SmallSet<CXXRecordDecl *, 2> VBases; |
| 780 | llvm::SetVector<CXXRecordDecl *> PublicSubobjectsSeen; |
| 781 | SubobjectsSeen[RD] = 1; |
| 782 | PublicSubobjectsSeen.insert(RD); |
| 783 | collectPublicBases(RD, SubobjectsSeen, VBases, PublicSubobjectsSeen, |
| 784 | /*ParentIsPublic=*/true); |
| 785 | |
| 786 | for (CXXRecordDecl *PublicSubobject : PublicSubobjectsSeen) { |
| 787 | // Skip ambiguous objects. |
| 788 | if (SubobjectsSeen[PublicSubobject] > 1) |
| 789 | continue; |
| 790 | |
| 791 | Objects.push_back(PublicSubobject); |
| 792 | } |
| 793 | } |
| 794 | |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 795 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 796 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, |
| 797 | QualType ExceptionObjectTy, Expr *E) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 798 | // If the type of the exception would be an incomplete type or a pointer |
| 799 | // 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] | 800 | QualType Ty = ExceptionObjectTy; |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 801 | bool isPointer = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 802 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 803 | Ty = Ptr->getPointeeType(); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 804 | isPointer = true; |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 805 | } |
| 806 | if (!isPointer || !Ty->isVoidType()) { |
| 807 | if (RequireCompleteType(ThrowLoc, Ty, |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 808 | isPointer ? diag::err_throw_incomplete_ptr |
| 809 | : diag::err_throw_incomplete, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 810 | E->getSourceRange())) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 811 | return true; |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 812 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 813 | if (RequireNonAbstractType(ThrowLoc, ExceptionObjectTy, |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 814 | diag::err_throw_abstract_type, E)) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 815 | return true; |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 818 | // If the exception has class type, we need additional handling. |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 819 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 820 | if (!RD) |
| 821 | return false; |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 822 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 823 | // If we are throwing a polymorphic class type or pointer thereof, |
| 824 | // exception handling will make use of the vtable. |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 825 | MarkVTableUsed(ThrowLoc, RD); |
| 826 | |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 827 | // If a pointer is thrown, the referenced object will not be destroyed. |
| 828 | if (isPointer) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 829 | return false; |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 830 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 831 | // 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] | 832 | if (!RD->hasIrrelevantDestructor()) { |
| 833 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
| 834 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
| 835 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 836 | PDiag(diag::err_access_dtor_exception) << Ty); |
| 837 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 838 | return true; |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 841 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 842 | // The MSVC ABI creates a list of all types which can catch the exception |
| 843 | // object. This list also references the appropriate copy constructor to call |
| 844 | // 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] | 845 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 846 | // We are only interested in the public, unambiguous bases contained within |
| 847 | // the exception object. Bases which are ambiguous or otherwise |
| 848 | // inaccessible are not catchable types. |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 849 | llvm::SmallVector<CXXRecordDecl *, 2> UnambiguousPublicSubobjects; |
| 850 | getUnambiguousPublicSubobjects(RD, UnambiguousPublicSubobjects); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 851 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 852 | for (CXXRecordDecl *Subobject : UnambiguousPublicSubobjects) { |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 853 | // Attempt to lookup the copy constructor. Various pieces of machinery |
| 854 | // will spring into action, like template instantiation, which means this |
| 855 | // cannot be a simple walk of the class's decls. Instead, we must perform |
| 856 | // lookup and overload resolution. |
| 857 | CXXConstructorDecl *CD = LookupCopyingConstructor(Subobject, 0); |
| 858 | if (!CD) |
| 859 | continue; |
| 860 | |
| 861 | // Mark the constructor referenced as it is used by this throw expression. |
| 862 | MarkFunctionReferenced(E->getExprLoc(), CD); |
| 863 | |
| 864 | // Skip this copy constructor if it is trivial, we don't need to record it |
| 865 | // in the catchable type data. |
| 866 | if (CD->isTrivial()) |
| 867 | continue; |
| 868 | |
| 869 | // The copy constructor is non-trivial, create a mapping from this class |
| 870 | // type to this constructor. |
| 871 | // N.B. The selection of copy constructor is not sensitive to this |
| 872 | // particular throw-site. Lookup will be performed at the catch-site to |
| 873 | // ensure that the copy constructor is, in fact, accessible (via |
| 874 | // friendship or any other means). |
| 875 | Context.addCopyConstructorForExceptionObject(Subobject, CD); |
| 876 | |
| 877 | // We don't keep the instantiated default argument expressions around so |
| 878 | // we must rebuild them here. |
| 879 | for (unsigned I = 1, E = CD->getNumParams(); I != E; ++I) { |
Reid Kleckner | c01ee75 | 2016-11-23 16:51:30 +0000 | [diff] [blame] | 880 | if (CheckCXXDefaultArgExpr(ThrowLoc, CD, CD->getParamDecl(I))) |
| 881 | return true; |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | } |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 885 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 886 | return false; |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 887 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 888 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 889 | static QualType adjustCVQualifiersForCXXThisWithinLambda( |
| 890 | ArrayRef<FunctionScopeInfo *> FunctionScopes, QualType ThisTy, |
| 891 | DeclContext *CurSemaContext, ASTContext &ASTCtx) { |
| 892 | |
| 893 | QualType ClassType = ThisTy->getPointeeType(); |
| 894 | LambdaScopeInfo *CurLSI = nullptr; |
| 895 | DeclContext *CurDC = CurSemaContext; |
| 896 | |
| 897 | // Iterate through the stack of lambdas starting from the innermost lambda to |
| 898 | // the outermost lambda, checking if '*this' is ever captured by copy - since |
| 899 | // that could change the cv-qualifiers of the '*this' object. |
| 900 | // The object referred to by '*this' starts out with the cv-qualifiers of its |
| 901 | // member function. We then start with the innermost lambda and iterate |
| 902 | // outward checking to see if any lambda performs a by-copy capture of '*this' |
| 903 | // - and if so, any nested lambda must respect the 'constness' of that |
| 904 | // capturing lamdbda's call operator. |
| 905 | // |
| 906 | |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 907 | // Since the FunctionScopeInfo stack is representative of the lexical |
| 908 | // nesting of the lambda expressions during initial parsing (and is the best |
| 909 | // place for querying information about captures about lambdas that are |
| 910 | // partially processed) and perhaps during instantiation of function templates |
| 911 | // that contain lambda expressions that need to be transformed BUT not |
| 912 | // necessarily during instantiation of a nested generic lambda's function call |
| 913 | // operator (which might even be instantiated at the end of the TU) - at which |
| 914 | // time the DeclContext tree is mature enough to query capture information |
| 915 | // reliably - we use a two pronged approach to walk through all the lexically |
| 916 | // enclosing lambda expressions: |
| 917 | // |
| 918 | // 1) Climb down the FunctionScopeInfo stack as long as each item represents |
| 919 | // a Lambda (i.e. LambdaScopeInfo) AND each LSI's 'closure-type' is lexically |
| 920 | // enclosed by the call-operator of the LSI below it on the stack (while |
| 921 | // tracking the enclosing DC for step 2 if needed). Note the topmost LSI on |
| 922 | // the stack represents the innermost lambda. |
| 923 | // |
| 924 | // 2) If we run out of enclosing LSI's, check if the enclosing DeclContext |
| 925 | // represents a lambda's call operator. If it does, we must be instantiating |
| 926 | // a generic lambda's call operator (represented by the Current LSI, and |
| 927 | // should be the only scenario where an inconsistency between the LSI and the |
| 928 | // DeclContext should occur), so climb out the DeclContexts if they |
| 929 | // represent lambdas, while querying the corresponding closure types |
| 930 | // regarding capture information. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 931 | |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 932 | // 1) Climb down the function scope info stack. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 933 | for (int I = FunctionScopes.size(); |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 934 | I-- && isa<LambdaScopeInfo>(FunctionScopes[I]) && |
| 935 | (!CurLSI || !CurLSI->Lambda || CurLSI->Lambda->getDeclContext() == |
| 936 | cast<LambdaScopeInfo>(FunctionScopes[I])->CallOperator); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 937 | CurDC = getLambdaAwareParentOfDeclContext(CurDC)) { |
| 938 | CurLSI = cast<LambdaScopeInfo>(FunctionScopes[I]); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 939 | |
| 940 | if (!CurLSI->isCXXThisCaptured()) |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 941 | continue; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 942 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 943 | auto C = CurLSI->getCXXThisCapture(); |
| 944 | |
| 945 | if (C.isCopyCapture()) { |
| 946 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 947 | if (CurLSI->CallOperator->isConst()) |
| 948 | ClassType.addConst(); |
| 949 | return ASTCtx.getPointerType(ClassType); |
| 950 | } |
| 951 | } |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 952 | |
| 953 | // 2) We've run out of ScopeInfos but check if CurDC is a lambda (which can |
| 954 | // happen during instantiation of its nested generic lambda call operator) |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 955 | if (isLambdaCallOperator(CurDC)) { |
Faisal Vali | 999f27e | 2017-05-02 20:56:34 +0000 | [diff] [blame] | 956 | assert(CurLSI && "While computing 'this' capture-type for a generic " |
| 957 | "lambda, we must have a corresponding LambdaScopeInfo"); |
| 958 | assert(isGenericLambdaCallOperatorSpecialization(CurLSI->CallOperator) && |
| 959 | "While computing 'this' capture-type for a generic lambda, when we " |
| 960 | "run out of enclosing LSI's, yet the enclosing DC is a " |
| 961 | "lambda-call-operator we must be (i.e. Current LSI) in a generic " |
| 962 | "lambda call oeprator"); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 963 | assert(CurDC == getLambdaAwareParentOfDeclContext(CurLSI->CallOperator)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 964 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 965 | auto IsThisCaptured = |
| 966 | [](CXXRecordDecl *Closure, bool &IsByCopy, bool &IsConst) { |
| 967 | IsConst = false; |
| 968 | IsByCopy = false; |
| 969 | for (auto &&C : Closure->captures()) { |
| 970 | if (C.capturesThis()) { |
| 971 | if (C.getCaptureKind() == LCK_StarThis) |
| 972 | IsByCopy = true; |
| 973 | if (Closure->getLambdaCallOperator()->isConst()) |
| 974 | IsConst = true; |
| 975 | return true; |
| 976 | } |
| 977 | } |
| 978 | return false; |
| 979 | }; |
| 980 | |
| 981 | bool IsByCopyCapture = false; |
| 982 | bool IsConstCapture = false; |
| 983 | CXXRecordDecl *Closure = cast<CXXRecordDecl>(CurDC->getParent()); |
| 984 | while (Closure && |
| 985 | IsThisCaptured(Closure, IsByCopyCapture, IsConstCapture)) { |
| 986 | if (IsByCopyCapture) { |
| 987 | ClassType.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 988 | if (IsConstCapture) |
| 989 | ClassType.addConst(); |
| 990 | return ASTCtx.getPointerType(ClassType); |
| 991 | } |
| 992 | Closure = isLambdaCallOperator(Closure->getParent()) |
| 993 | ? cast<CXXRecordDecl>(Closure->getParent()->getParent()) |
| 994 | : nullptr; |
| 995 | } |
| 996 | } |
| 997 | return ASTCtx.getPointerType(ClassType); |
| 998 | } |
| 999 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1000 | QualType Sema::getCurrentThisType() { |
| 1001 | DeclContext *DC = getFunctionLevelDeclContext(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1002 | QualType ThisTy = CXXThisTypeOverride; |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 1003 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1004 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { |
| 1005 | if (method && method->isInstance()) |
| 1006 | ThisTy = method->getThisType(Context); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1007 | } |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1008 | |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 1009 | if (ThisTy.isNull() && isLambdaCallOperator(CurContext) && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1010 | inTemplateInstantiation()) { |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1011 | |
Erik Pilkington | 3cdc317 | 2016-07-27 18:25:10 +0000 | [diff] [blame] | 1012 | assert(isa<CXXRecordDecl>(DC) && |
| 1013 | "Trying to get 'this' type from static method?"); |
| 1014 | |
| 1015 | // This is a lambda call operator that is being instantiated as a default |
| 1016 | // initializer. DC must point to the enclosing class type, so we can recover |
| 1017 | // the 'this' type from it. |
| 1018 | |
| 1019 | QualType ClassTy = Context.getTypeDeclType(cast<CXXRecordDecl>(DC)); |
| 1020 | // There are no cv-qualifiers for 'this' within default initializers, |
| 1021 | // per [expr.prim.general]p4. |
| 1022 | ThisTy = Context.getPointerType(ClassTy); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1023 | } |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1024 | |
| 1025 | // If we are within a lambda's call operator, the cv-qualifiers of 'this' |
| 1026 | // might need to be adjusted if the lambda or any of its enclosing lambda's |
| 1027 | // captures '*this' by copy. |
| 1028 | if (!ThisTy.isNull() && isLambdaCallOperator(CurContext)) |
| 1029 | return adjustCVQualifiersForCXXThisWithinLambda(FunctionScopes, ThisTy, |
| 1030 | CurContext, Context); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1031 | return ThisTy; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1034 | Sema::CXXThisScopeRAII::CXXThisScopeRAII(Sema &S, |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1035 | Decl *ContextDecl, |
| 1036 | unsigned CXXThisTypeQuals, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1037 | bool Enabled) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1038 | : S(S), OldCXXThisTypeOverride(S.CXXThisTypeOverride), Enabled(false) |
| 1039 | { |
| 1040 | if (!Enabled || !ContextDecl) |
| 1041 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1042 | |
| 1043 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1044 | if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(ContextDecl)) |
| 1045 | Record = Template->getTemplatedDecl(); |
| 1046 | else |
| 1047 | Record = cast<CXXRecordDecl>(ContextDecl); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1048 | |
Andrey Bokhanko | 67a4186 | 2016-05-26 10:06:01 +0000 | [diff] [blame] | 1049 | // We care only for CVR qualifiers here, so cut everything else. |
| 1050 | CXXThisTypeQuals &= Qualifiers::FastMask; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1051 | S.CXXThisTypeOverride |
| 1052 | = S.Context.getPointerType( |
| 1053 | S.Context.getRecordType(Record).withCVRQualifiers(CXXThisTypeQuals)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1054 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1055 | this->Enabled = true; |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | Sema::CXXThisScopeRAII::~CXXThisScopeRAII() { |
| 1060 | if (Enabled) { |
| 1061 | S.CXXThisTypeOverride = OldCXXThisTypeOverride; |
| 1062 | } |
| 1063 | } |
| 1064 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1065 | static Expr *captureThis(Sema &S, ASTContext &Context, RecordDecl *RD, |
| 1066 | QualType ThisTy, SourceLocation Loc, |
| 1067 | const bool ByCopy) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1068 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1069 | QualType AdjustedThisTy = ThisTy; |
| 1070 | // The type of the corresponding data member (not a 'this' pointer if 'by |
| 1071 | // copy'). |
| 1072 | QualType CaptureThisFieldTy = ThisTy; |
| 1073 | if (ByCopy) { |
| 1074 | // If we are capturing the object referred to by '*this' by copy, ignore any |
| 1075 | // cv qualifiers inherited from the type of the member function for the type |
| 1076 | // of the closure-type's corresponding data member and any use of 'this'. |
| 1077 | CaptureThisFieldTy = ThisTy->getPointeeType(); |
| 1078 | CaptureThisFieldTy.removeLocalCVRQualifiers(Qualifiers::CVRMask); |
| 1079 | AdjustedThisTy = Context.getPointerType(CaptureThisFieldTy); |
| 1080 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1081 | |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1082 | FieldDecl *Field = FieldDecl::Create( |
| 1083 | Context, RD, Loc, Loc, nullptr, CaptureThisFieldTy, |
| 1084 | Context.getTrivialTypeSourceInfo(CaptureThisFieldTy, Loc), nullptr, false, |
| 1085 | ICIS_NoInit); |
| 1086 | |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1087 | Field->setImplicit(true); |
| 1088 | Field->setAccess(AS_private); |
| 1089 | RD->addDecl(Field); |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1090 | Expr *This = |
| 1091 | new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit*/ true); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1092 | if (ByCopy) { |
| 1093 | Expr *StarThis = S.CreateBuiltinUnaryOp(Loc, |
| 1094 | UO_Deref, |
| 1095 | This).get(); |
| 1096 | InitializedEntity Entity = InitializedEntity::InitializeLambdaCapture( |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1097 | nullptr, CaptureThisFieldTy, Loc); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1098 | InitializationKind InitKind = InitializationKind::CreateDirect(Loc, Loc, Loc); |
| 1099 | InitializationSequence Init(S, Entity, InitKind, StarThis); |
| 1100 | ExprResult ER = Init.Perform(S, Entity, InitKind, StarThis); |
| 1101 | if (ER.isInvalid()) return nullptr; |
| 1102 | return ER.get(); |
| 1103 | } |
| 1104 | return This; |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1107 | bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit, |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1108 | bool BuildAndDiagnose, const unsigned *const FunctionScopeIndexToStopAt, |
| 1109 | const bool ByCopy) { |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1110 | // We don't need to capture this in an unevaluated context. |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1111 | if (isUnevaluatedContext() && !Explicit) |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1112 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1113 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1114 | assert((!ByCopy || Explicit) && "cannot implicitly capture *this by value"); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1115 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1116 | const unsigned MaxFunctionScopesIndex = FunctionScopeIndexToStopAt ? |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1117 | *FunctionScopeIndexToStopAt : FunctionScopes.size() - 1; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1118 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1119 | // Check that we can capture the *enclosing object* (referred to by '*this') |
| 1120 | // by the capturing-entity/closure (lambda/block/etc) at |
| 1121 | // MaxFunctionScopesIndex-deep on the FunctionScopes stack. |
| 1122 | |
| 1123 | // Note: The *enclosing object* can only be captured by-value by a |
| 1124 | // closure that is a lambda, using the explicit notation: |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1125 | // [*this] { ... }. |
| 1126 | // Every other capture of the *enclosing object* results in its by-reference |
| 1127 | // capture. |
| 1128 | |
| 1129 | // For a closure 'L' (at MaxFunctionScopesIndex in the FunctionScopes |
| 1130 | // stack), we can capture the *enclosing object* only if: |
| 1131 | // - 'L' has an explicit byref or byval capture of the *enclosing object* |
| 1132 | // - or, 'L' has an implicit capture. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1133 | // AND |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1134 | // -- there is no enclosing closure |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1135 | // -- or, there is some enclosing closure 'E' that has already captured the |
| 1136 | // *enclosing object*, and every intervening closure (if any) between 'E' |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1137 | // and 'L' can implicitly capture the *enclosing object*. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1138 | // -- or, every enclosing closure can implicitly capture the |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1139 | // *enclosing object* |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1140 | |
| 1141 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1142 | unsigned NumCapturingClosures = 0; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1143 | for (unsigned idx = MaxFunctionScopesIndex; idx != 0; idx--) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1144 | if (CapturingScopeInfo *CSI = |
| 1145 | dyn_cast<CapturingScopeInfo>(FunctionScopes[idx])) { |
| 1146 | if (CSI->CXXThisCaptureIndex != 0) { |
| 1147 | // 'this' is already being captured; there isn't anything more to do. |
Malcolm Parsons | 87a0362 | 2017-01-13 15:01:06 +0000 | [diff] [blame] | 1148 | CSI->Captures[CSI->CXXThisCaptureIndex - 1].markUsed(BuildAndDiagnose); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1151 | LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI); |
| 1152 | if (LSI && isGenericLambdaCallOperatorSpecialization(LSI->CallOperator)) { |
| 1153 | // This context can't implicitly capture 'this'; fail out. |
| 1154 | if (BuildAndDiagnose) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1155 | Diag(Loc, diag::err_this_capture) |
| 1156 | << (Explicit && idx == MaxFunctionScopesIndex); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1157 | return true; |
| 1158 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1159 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref || |
Douglas Gregor | a1bffa2 | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 1160 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval || |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 1161 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block || |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1162 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_CapturedRegion || |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1163 | (Explicit && idx == MaxFunctionScopesIndex)) { |
| 1164 | // Regarding (Explicit && idx == MaxFunctionScopesIndex): only the first |
| 1165 | // iteration through can be an explicit capture, all enclosing closures, |
| 1166 | // if any, must perform implicit captures. |
| 1167 | |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 1168 | // This closure can capture 'this'; continue looking upwards. |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1169 | NumCapturingClosures++; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1170 | continue; |
| 1171 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1172 | // This context can't implicitly capture 'this'; fail out. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1173 | if (BuildAndDiagnose) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1174 | Diag(Loc, diag::err_this_capture) |
| 1175 | << (Explicit && idx == MaxFunctionScopesIndex); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1176 | return true; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1177 | } |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1178 | break; |
| 1179 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1180 | if (!BuildAndDiagnose) return false; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1181 | |
| 1182 | // If we got here, then the closure at MaxFunctionScopesIndex on the |
| 1183 | // FunctionScopes stack, can capture the *enclosing object*, so capture it |
| 1184 | // (including implicit by-reference captures in any enclosing closures). |
| 1185 | |
| 1186 | // In the loop below, respect the ByCopy flag only for the closure requesting |
| 1187 | // 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] | 1188 | // all enclosing closure's up to NumCapturingClosures (since they must be |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1189 | // implicitly capturing the *enclosing object* by reference (see loop |
| 1190 | // above)). |
| 1191 | assert((!ByCopy || |
| 1192 | dyn_cast<LambdaScopeInfo>(FunctionScopes[MaxFunctionScopesIndex])) && |
| 1193 | "Only a lambda can capture the enclosing object (referred to by " |
| 1194 | "*this) by copy"); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1195 | // FIXME: We need to delay this marking in PotentiallyPotentiallyEvaluated |
| 1196 | // contexts. |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1197 | QualType ThisTy = getCurrentThisType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1198 | for (unsigned idx = MaxFunctionScopesIndex; NumCapturingClosures; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1199 | --idx, --NumCapturingClosures) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 1200 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1201 | Expr *ThisExpr = nullptr; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1202 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1203 | if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI)) { |
| 1204 | // For lambda expressions, build a field and an initializing expression, |
| 1205 | // and capture the *enclosing object* by copy only if this is the first |
| 1206 | // iteration. |
| 1207 | ThisExpr = captureThis(*this, Context, LSI->Lambda, ThisTy, Loc, |
| 1208 | ByCopy && idx == MaxFunctionScopesIndex); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1209 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1210 | } else if (CapturedRegionScopeInfo *RSI |
Ben Langmuir | e7d7c4c | 2013-04-29 13:32:41 +0000 | [diff] [blame] | 1211 | = dyn_cast<CapturedRegionScopeInfo>(FunctionScopes[idx])) |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1212 | ThisExpr = |
| 1213 | captureThis(*this, Context, RSI->TheRecordDecl, ThisTy, Loc, |
| 1214 | false/*ByCopy*/); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1215 | |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 1216 | bool isNested = NumCapturingClosures > 1; |
Faisal Vali | 67b0446 | 2016-06-11 16:41:54 +0000 | [diff] [blame] | 1217 | CSI->addThisCapture(isNested, Loc, ThisExpr, ByCopy); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1218 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 1219 | return false; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1222 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1223 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 1224 | /// is a non-lvalue expression whose value is the address of the object for |
| 1225 | /// which the function is called. |
| 1226 | |
Douglas Gregor | 09deffa | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 1227 | QualType ThisTy = getCurrentThisType(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1228 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1229 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1230 | CheckCXXThisCapture(Loc); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1231 | return new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1232 | } |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1233 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1234 | bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) { |
| 1235 | // If we're outside the body of a member function, then we'll have a specified |
| 1236 | // type for 'this'. |
| 1237 | if (CXXThisTypeOverride.isNull()) |
| 1238 | return false; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1239 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1240 | // Determine whether we're looking into a class that's currently being |
| 1241 | // defined. |
| 1242 | CXXRecordDecl *Class = BaseType->getAsCXXRecordDecl(); |
| 1243 | return Class && Class->isBeingDefined(); |
| 1244 | } |
| 1245 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1246 | ExprResult |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1247 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1248 | SourceLocation LParenLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1249 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1250 | SourceLocation RParenLoc) { |
Douglas Gregor | 7df89f5 | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 1251 | if (!TypeRep) |
| 1252 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1253 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 1254 | TypeSourceInfo *TInfo; |
| 1255 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 1256 | if (!TInfo) |
| 1257 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1258 | |
Richard Smith | b8c414c | 2016-06-30 20:24:30 +0000 | [diff] [blame] | 1259 | auto Result = BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); |
| 1260 | // Avoid creating a non-type-dependent expression that contains typos. |
| 1261 | // Non-type-dependent expressions are liable to be discarded without |
| 1262 | // checking for embedded typos. |
| 1263 | if (!Result.isInvalid() && Result.get()->isInstantiationDependent() && |
| 1264 | !Result.get()->isTypeDependent()) |
| 1265 | Result = CorrectDelayedTyposInExpr(Result.get()); |
| 1266 | return Result; |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 1270 | /// Can be interpreted either as function-style casting ("int(x)") |
| 1271 | /// or class type construction ("ClassType(x,y,z)") |
| 1272 | /// or creation of a value-initialized type ("int()"). |
| 1273 | ExprResult |
| 1274 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
| 1275 | SourceLocation LParenLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1276 | MultiExprArg Exprs, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1277 | SourceLocation RParenLoc) { |
| 1278 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 1279 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1280 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1281 | if (Ty->isDependentType() || CallExpr::hasAnyTypeDependentArguments(Exprs)) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1282 | return CXXUnresolvedConstructExpr::Create(Context, TInfo, LParenLoc, Exprs, |
| 1283 | RParenLoc); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1286 | bool ListInitialization = LParenLoc.isInvalid(); |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1287 | assert((!ListInitialization || |
| 1288 | (Exprs.size() == 1 && isa<InitListExpr>(Exprs[0]))) && |
| 1289 | "List initialization must have initializer list as expression."); |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1290 | SourceRange FullRange = SourceRange(TyBeginLoc, |
| 1291 | ListInitialization ? Exprs[0]->getSourceRange().getEnd() : RParenLoc); |
| 1292 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1293 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 1294 | InitializationKind Kind = |
| 1295 | Exprs.size() |
| 1296 | ? ListInitialization |
| 1297 | ? InitializationKind::CreateDirectList(TyBeginLoc) |
| 1298 | : InitializationKind::CreateDirect(TyBeginLoc, LParenLoc, |
| 1299 | RParenLoc) |
| 1300 | : InitializationKind::CreateValue(TyBeginLoc, LParenLoc, RParenLoc); |
| 1301 | |
| 1302 | // C++1z [expr.type.conv]p1: |
| 1303 | // If the type is a placeholder for a deduced class type, [...perform class |
| 1304 | // template argument deduction...] |
| 1305 | DeducedType *Deduced = Ty->getContainedDeducedType(); |
| 1306 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
| 1307 | Ty = DeduceTemplateSpecializationFromInitializer(TInfo, Entity, |
| 1308 | Kind, Exprs); |
| 1309 | if (Ty.isNull()) |
| 1310 | return ExprError(); |
| 1311 | Entity = InitializedEntity::InitializeTemporary(TInfo, Ty); |
| 1312 | } |
| 1313 | |
Douglas Gregor | dd04d33 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 1314 | // C++ [expr.type.conv]p1: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1315 | // If the expression list is a parenthesized single expression, the type |
| 1316 | // conversion expression is equivalent (in definedness, and if defined in |
| 1317 | // meaning) to the corresponding cast expression. |
| 1318 | if (Exprs.size() == 1 && !ListInitialization && |
| 1319 | !isa<InitListExpr>(Exprs[0])) { |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1320 | Expr *Arg = Exprs[0]; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1321 | return BuildCXXFunctionalCastExpr(TInfo, Ty, LParenLoc, Arg, RParenLoc); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1324 | // 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] | 1325 | QualType ElemTy = Ty; |
| 1326 | if (Ty->isArrayType()) { |
| 1327 | if (!ListInitialization) |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1328 | return ExprError(Diag(TyBeginLoc, diag::err_value_init_for_array_type) |
| 1329 | << FullRange); |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1330 | ElemTy = Context.getBaseElementType(Ty); |
| 1331 | } |
| 1332 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1333 | // There doesn't seem to be an explicit rule against this but sanity demands |
| 1334 | // we only construct objects with object types. |
| 1335 | if (Ty->isFunctionType()) |
| 1336 | return ExprError(Diag(TyBeginLoc, diag::err_init_for_function_type) |
| 1337 | << Ty << FullRange); |
David Majnemer | 7eddcff | 2015-09-14 07:05:00 +0000 | [diff] [blame] | 1338 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1339 | // C++17 [expr.type.conv]p2: |
| 1340 | // If the type is cv void and the initializer is (), the expression is a |
| 1341 | // prvalue of the specified type that performs no initialization. |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1342 | if (!Ty->isVoidType() && |
| 1343 | RequireCompleteType(TyBeginLoc, ElemTy, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1344 | diag::err_invalid_incomplete_type_use, FullRange)) |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 1345 | return ExprError(); |
| 1346 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1347 | // Otherwise, the expression is a prvalue of the specified type whose |
| 1348 | // result object is direct-initialized (11.6) with the initializer. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1349 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs); |
| 1350 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Exprs); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1351 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1352 | if (Result.isInvalid()) |
Richard Smith | 9006190 | 2013-09-23 02:20:00 +0000 | [diff] [blame] | 1353 | return Result; |
| 1354 | |
| 1355 | Expr *Inner = Result.get(); |
| 1356 | if (CXXBindTemporaryExpr *BTE = dyn_cast_or_null<CXXBindTemporaryExpr>(Inner)) |
| 1357 | Inner = BTE->getSubExpr(); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1358 | if (!isa<CXXTemporaryObjectExpr>(Inner) && |
| 1359 | !isa<CXXScalarValueInitExpr>(Inner)) { |
Richard Smith | 1ae689c | 2015-01-28 22:06:01 +0000 | [diff] [blame] | 1360 | // If we created a CXXTemporaryObjectExpr, that node also represents the |
| 1361 | // functional cast. Otherwise, create an explicit cast to represent |
| 1362 | // the syntactic form of a functional-style cast that was used here. |
| 1363 | // |
| 1364 | // FIXME: Creating a CXXFunctionalCastExpr around a CXXConstructExpr |
| 1365 | // would give a more consistent AST representation than using a |
| 1366 | // CXXTemporaryObjectExpr. It's also weird that the functional cast |
| 1367 | // is sometimes handled by initialization and sometimes not. |
Richard Smith | 9006190 | 2013-09-23 02:20:00 +0000 | [diff] [blame] | 1368 | QualType ResultType = Result.get()->getType(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1369 | Result = CXXFunctionalCastExpr::Create( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1370 | Context, ResultType, Expr::getValueKindForType(Ty), TInfo, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1371 | CK_NoOp, Result.get(), /*Path=*/nullptr, LParenLoc, RParenLoc); |
Sebastian Redl | 2b80af4 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1374 | return Result; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1375 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1376 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1377 | /// \brief Determine whether the given function is a non-placement |
| 1378 | /// deallocation function. |
| 1379 | static bool isNonPlacementDeallocationFunction(Sema &S, FunctionDecl *FD) { |
| 1380 | if (FD->isInvalidDecl()) |
| 1381 | return false; |
| 1382 | |
| 1383 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 1384 | return Method->isUsualDeallocationFunction(); |
| 1385 | |
| 1386 | if (FD->getOverloadedOperator() != OO_Delete && |
| 1387 | FD->getOverloadedOperator() != OO_Array_Delete) |
| 1388 | return false; |
| 1389 | |
| 1390 | unsigned UsualParams = 1; |
| 1391 | |
| 1392 | if (S.getLangOpts().SizedDeallocation && UsualParams < FD->getNumParams() && |
| 1393 | S.Context.hasSameUnqualifiedType( |
| 1394 | FD->getParamDecl(UsualParams)->getType(), |
| 1395 | S.Context.getSizeType())) |
| 1396 | ++UsualParams; |
| 1397 | |
| 1398 | if (S.getLangOpts().AlignedAllocation && UsualParams < FD->getNumParams() && |
| 1399 | S.Context.hasSameUnqualifiedType( |
| 1400 | FD->getParamDecl(UsualParams)->getType(), |
| 1401 | S.Context.getTypeDeclType(S.getStdAlignValT()))) |
| 1402 | ++UsualParams; |
| 1403 | |
| 1404 | return UsualParams == FD->getNumParams(); |
| 1405 | } |
| 1406 | |
| 1407 | namespace { |
| 1408 | struct UsualDeallocFnInfo { |
| 1409 | UsualDeallocFnInfo() : Found(), FD(nullptr) {} |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1410 | UsualDeallocFnInfo(Sema &S, DeclAccessPair Found) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1411 | : Found(Found), FD(dyn_cast<FunctionDecl>(Found->getUnderlyingDecl())), |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1412 | HasSizeT(false), HasAlignValT(false), CUDAPref(Sema::CFP_Native) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1413 | // A function template declaration is never a usual deallocation function. |
| 1414 | if (!FD) |
| 1415 | return; |
| 1416 | if (FD->getNumParams() == 3) |
| 1417 | HasAlignValT = HasSizeT = true; |
| 1418 | else if (FD->getNumParams() == 2) { |
| 1419 | HasSizeT = FD->getParamDecl(1)->getType()->isIntegerType(); |
| 1420 | HasAlignValT = !HasSizeT; |
| 1421 | } |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1422 | |
| 1423 | // In CUDA, determine how much we'd like / dislike to call this. |
| 1424 | if (S.getLangOpts().CUDA) |
| 1425 | if (auto *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 1426 | CUDAPref = S.IdentifyCUDAPreference(Caller, FD); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | operator bool() const { return FD; } |
| 1430 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1431 | bool isBetterThan(const UsualDeallocFnInfo &Other, bool WantSize, |
| 1432 | bool WantAlign) const { |
| 1433 | // C++17 [expr.delete]p10: |
| 1434 | // If the type has new-extended alignment, a function with a parameter |
| 1435 | // of type std::align_val_t is preferred; otherwise a function without |
| 1436 | // such a parameter is preferred |
| 1437 | if (HasAlignValT != Other.HasAlignValT) |
| 1438 | return HasAlignValT == WantAlign; |
| 1439 | |
| 1440 | if (HasSizeT != Other.HasSizeT) |
| 1441 | return HasSizeT == WantSize; |
| 1442 | |
| 1443 | // Use CUDA call preference as a tiebreaker. |
| 1444 | return CUDAPref > Other.CUDAPref; |
| 1445 | } |
| 1446 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1447 | DeclAccessPair Found; |
| 1448 | FunctionDecl *FD; |
| 1449 | bool HasSizeT, HasAlignValT; |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1450 | Sema::CUDAFunctionPreference CUDAPref; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1451 | }; |
| 1452 | } |
| 1453 | |
| 1454 | /// Determine whether a type has new-extended alignment. This may be called when |
| 1455 | /// the type is incomplete (for a delete-expression with an incomplete pointee |
| 1456 | /// type), in which case it will conservatively return false if the alignment is |
| 1457 | /// not known. |
| 1458 | static bool hasNewExtendedAlignment(Sema &S, QualType AllocType) { |
| 1459 | return S.getLangOpts().AlignedAllocation && |
| 1460 | S.getASTContext().getTypeAlignIfKnown(AllocType) > |
| 1461 | S.getASTContext().getTargetInfo().getNewAlign(); |
| 1462 | } |
| 1463 | |
| 1464 | /// Select the correct "usual" deallocation function to use from a selection of |
| 1465 | /// deallocation functions (either global or class-scope). |
| 1466 | static UsualDeallocFnInfo resolveDeallocationOverload( |
| 1467 | Sema &S, LookupResult &R, bool WantSize, bool WantAlign, |
| 1468 | llvm::SmallVectorImpl<UsualDeallocFnInfo> *BestFns = nullptr) { |
| 1469 | UsualDeallocFnInfo Best; |
| 1470 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1471 | for (auto I = R.begin(), E = R.end(); I != E; ++I) { |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1472 | UsualDeallocFnInfo Info(S, I.getPair()); |
| 1473 | if (!Info || !isNonPlacementDeallocationFunction(S, Info.FD) || |
| 1474 | Info.CUDAPref == Sema::CFP_Never) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1475 | continue; |
| 1476 | |
| 1477 | if (!Best) { |
| 1478 | Best = Info; |
| 1479 | if (BestFns) |
| 1480 | BestFns->push_back(Info); |
| 1481 | continue; |
| 1482 | } |
| 1483 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1484 | if (Best.isBetterThan(Info, WantSize, WantAlign)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1485 | continue; |
| 1486 | |
| 1487 | // If more than one preferred function is found, all non-preferred |
| 1488 | // functions are eliminated from further consideration. |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 1489 | if (BestFns && Info.isBetterThan(Best, WantSize, WantAlign)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1490 | BestFns->clear(); |
| 1491 | |
| 1492 | Best = Info; |
| 1493 | if (BestFns) |
| 1494 | BestFns->push_back(Info); |
| 1495 | } |
| 1496 | |
| 1497 | return Best; |
| 1498 | } |
| 1499 | |
| 1500 | /// Determine whether a given type is a class for which 'delete[]' would call |
| 1501 | /// a member 'operator delete[]' with a 'size_t' parameter. This implies that |
| 1502 | /// we need to store the array size (even if the type is |
| 1503 | /// trivially-destructible). |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1504 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
| 1505 | QualType allocType) { |
| 1506 | const RecordType *record = |
| 1507 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 1508 | if (!record) return false; |
| 1509 | |
| 1510 | // Try to find an operator delete[] in class scope. |
| 1511 | |
| 1512 | DeclarationName deleteName = |
| 1513 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
| 1514 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
| 1515 | S.LookupQualifiedName(ops, record->getDecl()); |
| 1516 | |
| 1517 | // We're just doing this for information. |
| 1518 | ops.suppressDiagnostics(); |
| 1519 | |
| 1520 | // Very likely: there's no operator delete[]. |
| 1521 | if (ops.empty()) return false; |
| 1522 | |
| 1523 | // If it's ambiguous, it should be illegal to call operator delete[] |
| 1524 | // on this thing, so it doesn't matter if we allocate extra space or not. |
| 1525 | if (ops.isAmbiguous()) return false; |
| 1526 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1527 | // C++17 [expr.delete]p10: |
| 1528 | // If the deallocation functions have class scope, the one without a |
| 1529 | // parameter of type std::size_t is selected. |
| 1530 | auto Best = resolveDeallocationOverload( |
| 1531 | S, ops, /*WantSize*/false, |
| 1532 | /*WantAlign*/hasNewExtendedAlignment(S, allocType)); |
| 1533 | return Best && Best.HasSizeT; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1534 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1535 | |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1536 | /// \brief Parsed a C++ 'new' expression (C++ 5.3.4). |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 1537 | /// |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1538 | /// E.g.: |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1539 | /// @code new (memory) int[size][4] @endcode |
| 1540 | /// or |
| 1541 | /// @code ::new Foo(23, "hello") @endcode |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1542 | /// |
| 1543 | /// \param StartLoc The first location of the expression. |
| 1544 | /// \param UseGlobal True if 'new' was prefixed with '::'. |
| 1545 | /// \param PlacementLParen Opening paren of the placement arguments. |
| 1546 | /// \param PlacementArgs Placement new arguments. |
| 1547 | /// \param PlacementRParen Closing paren of the placement arguments. |
| 1548 | /// \param TypeIdParens If the type is in parens, the source range. |
| 1549 | /// \param D The type to be allocated, as well as array dimensions. |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 1550 | /// \param Initializer The initializing expression or initializer-list, or null |
| 1551 | /// if there is none. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1552 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1553 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1554 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1555 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1556 | Declarator &D, Expr *Initializer) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1557 | Expr *ArraySize = nullptr; |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1558 | // If the specified type is an array, unwrap it and save the expression. |
| 1559 | if (D.getNumTypeObjects() > 0 && |
| 1560 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1561 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
Richard Smith | bfbff07 | 2017-02-10 22:35:37 +0000 | [diff] [blame] | 1562 | if (D.getDeclSpec().hasAutoTypeSpec()) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1563 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
| 1564 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1565 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1566 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 1567 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1568 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1569 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 1570 | << D.getSourceRange()); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1571 | |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1572 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1573 | D.DropFirstTypeObject(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1576 | // Every dimension shall be of constant size. |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1577 | if (ArraySize) { |
| 1578 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1579 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 1580 | break; |
| 1581 | |
| 1582 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 1583 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1584 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent()) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1585 | if (getLangOpts().CPlusPlus14) { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1586 | // C++1y [expr.new]p6: Every constant-expression in a noptr-new-declarator |
| 1587 | // shall be a converted constant expression (5.19) of type std::size_t |
| 1588 | // and shall evaluate to a strictly positive value. |
| 1589 | unsigned IntWidth = Context.getTargetInfo().getIntWidth(); |
| 1590 | assert(IntWidth && "Builtin type of size 0?"); |
| 1591 | llvm::APSInt Value(IntWidth); |
| 1592 | Array.NumElts |
| 1593 | = CheckConvertedConstantExpression(NumElts, Context.getSizeType(), Value, |
| 1594 | CCEK_NewExpr) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1595 | .get(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1596 | } else { |
| 1597 | Array.NumElts |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1598 | = VerifyIntegerConstantExpression(NumElts, nullptr, |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1599 | diag::err_new_array_nonconst) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1600 | .get(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1601 | } |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1602 | if (!Array.NumElts) |
| 1603 | return ExprError(); |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | } |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1608 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1609 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/nullptr); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1610 | QualType AllocType = TInfo->getType(); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1611 | if (D.isInvalidType()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1612 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1613 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1614 | SourceRange DirectInitRange; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 1615 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1616 | DirectInitRange = List->getSourceRange(); |
| 1617 | |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1618 | return BuildCXXNew(SourceRange(StartLoc, D.getLocEnd()), UseGlobal, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1619 | PlacementLParen, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1620 | PlacementArgs, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1621 | PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1622 | TypeIdParens, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1624 | TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1625 | ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1626 | DirectInitRange, |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1627 | Initializer); |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1630 | static bool isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style, |
| 1631 | Expr *Init) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1632 | if (!Init) |
| 1633 | return true; |
Sebastian Redl | eb54f08 | 2012-02-17 08:42:32 +0000 | [diff] [blame] | 1634 | if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) |
| 1635 | return PLE->getNumExprs() == 0; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1636 | if (isa<ImplicitValueInitExpr>(Init)) |
| 1637 | return true; |
| 1638 | else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) |
| 1639 | return !CCE->isListInitialization() && |
| 1640 | CCE->getConstructor()->isDefaultConstructor(); |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1641 | else if (Style == CXXNewExpr::ListInit) { |
| 1642 | assert(isa<InitListExpr>(Init) && |
| 1643 | "Shouldn't create list CXXConstructExprs for arrays."); |
| 1644 | return true; |
| 1645 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1646 | return false; |
| 1647 | } |
| 1648 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1649 | ExprResult |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1650 | Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1651 | SourceLocation PlacementLParen, |
| 1652 | MultiExprArg PlacementArgs, |
| 1653 | SourceLocation PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1654 | SourceRange TypeIdParens, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1655 | QualType AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1656 | TypeSourceInfo *AllocTypeInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1657 | Expr *ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1658 | SourceRange DirectInitRange, |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1659 | Expr *Initializer) { |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1660 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
David Blaikie | 7b97aef | 2012-11-07 00:12:38 +0000 | [diff] [blame] | 1661 | SourceLocation StartLoc = Range.getBegin(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1662 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1663 | CXXNewExpr::InitializationStyle initStyle; |
| 1664 | if (DirectInitRange.isValid()) { |
| 1665 | assert(Initializer && "Have parens but no initializer."); |
| 1666 | initStyle = CXXNewExpr::CallInit; |
| 1667 | } else if (Initializer && isa<InitListExpr>(Initializer)) |
| 1668 | initStyle = CXXNewExpr::ListInit; |
| 1669 | else { |
| 1670 | assert((!Initializer || isa<ImplicitValueInitExpr>(Initializer) || |
| 1671 | isa<CXXConstructExpr>(Initializer)) && |
| 1672 | "Initializer expression that cannot have been implicitly created."); |
| 1673 | initStyle = CXXNewExpr::NoInit; |
| 1674 | } |
| 1675 | |
| 1676 | Expr **Inits = &Initializer; |
| 1677 | unsigned NumInits = Initializer ? 1 : 0; |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 1678 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) { |
| 1679 | assert(initStyle == CXXNewExpr::CallInit && "paren init for non-call init"); |
| 1680 | Inits = List->getExprs(); |
| 1681 | NumInits = List->getNumExprs(); |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1684 | // C++11 [expr.new]p15: |
| 1685 | // A new-expression that creates an object of type T initializes that |
| 1686 | // object as follows: |
| 1687 | InitializationKind Kind |
| 1688 | // - If the new-initializer is omitted, the object is default- |
| 1689 | // initialized (8.5); if no initialization is performed, |
| 1690 | // the object has indeterminate value |
| 1691 | = initStyle == CXXNewExpr::NoInit |
| 1692 | ? InitializationKind::CreateDefault(TypeRange.getBegin()) |
| 1693 | // - Otherwise, the new-initializer is interpreted according to the |
| 1694 | // initialization rules of 8.5 for direct-initialization. |
| 1695 | : initStyle == CXXNewExpr::ListInit |
| 1696 | ? InitializationKind::CreateDirectList(TypeRange.getBegin()) |
| 1697 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
| 1698 | DirectInitRange.getBegin(), |
| 1699 | DirectInitRange.getEnd()); |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1700 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 1701 | // C++11 [dcl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 1702 | auto *Deduced = AllocType->getContainedDeducedType(); |
| 1703 | if (Deduced && isa<DeducedTemplateSpecializationType>(Deduced)) { |
| 1704 | if (ArraySize) |
| 1705 | return ExprError(Diag(ArraySize->getExprLoc(), |
| 1706 | diag::err_deduced_class_template_compound_type) |
| 1707 | << /*array*/ 2 << ArraySize->getSourceRange()); |
| 1708 | |
| 1709 | InitializedEntity Entity |
| 1710 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
| 1711 | AllocType = DeduceTemplateSpecializationFromInitializer( |
| 1712 | AllocTypeInfo, Entity, Kind, MultiExprArg(Inits, NumInits)); |
| 1713 | if (AllocType.isNull()) |
| 1714 | return ExprError(); |
| 1715 | } else if (Deduced) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1716 | if (initStyle == CXXNewExpr::NoInit || NumInits == 0) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1717 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
| 1718 | << AllocType << TypeRange); |
Richard Smith | 66204ec | 2014-03-12 17:42:45 +0000 | [diff] [blame] | 1719 | if (initStyle == CXXNewExpr::ListInit || |
| 1720 | (NumInits == 1 && isa<InitListExpr>(Inits[0]))) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1721 | return ExprError(Diag(Inits[0]->getLocStart(), |
Richard Smith | 66204ec | 2014-03-12 17:42:45 +0000 | [diff] [blame] | 1722 | diag::err_auto_new_list_init) |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1723 | << AllocType << TypeRange); |
| 1724 | if (NumInits > 1) { |
| 1725 | Expr *FirstBad = Inits[1]; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1726 | return ExprError(Diag(FirstBad->getLocStart(), |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1727 | diag::err_auto_new_ctor_multiple_expressions) |
| 1728 | << AllocType << TypeRange); |
| 1729 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1730 | Expr *Deduce = Inits[0]; |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1731 | QualType DeducedType; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 1732 | if (DeduceAutoType(AllocTypeInfo, Deduce, DeducedType) == DAR_Failed) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1733 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1734 | << AllocType << Deduce->getType() |
| 1735 | << TypeRange << Deduce->getSourceRange()); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1736 | if (DeducedType.isNull()) |
Richard Smith | 9647d3c | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 1737 | return ExprError(); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1738 | AllocType = DeducedType; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1739 | } |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1740 | |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1741 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 1742 | // typedef of an array type. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1743 | if (!ArraySize) { |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1744 | if (const ConstantArrayType *Array |
| 1745 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1746 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 1747 | Context.getSizeType(), |
| 1748 | TypeRange.getEnd()); |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1749 | AllocType = Array->getElementType(); |
| 1750 | } |
| 1751 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1752 | |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1753 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 1754 | return ExprError(); |
| 1755 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1756 | if (initStyle == CXXNewExpr::ListInit && |
| 1757 | isStdInitializerList(AllocType, nullptr)) { |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1758 | Diag(AllocTypeInfo->getTypeLoc().getBeginLoc(), |
| 1759 | diag::warn_dangling_std_initializer_list) |
Sebastian Redl | 73cfbeb | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1760 | << /*at end of FE*/0 << Inits[0]->getSourceRange(); |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1763 | // In ARC, infer 'retaining' for the allocated |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1764 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1765 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 1766 | AllocType->isObjCLifetimeType()) { |
| 1767 | AllocType = Context.getLifetimeQualifiedType(AllocType, |
| 1768 | AllocType->getObjCARCImplicitLifetime()); |
| 1769 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1770 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1771 | QualType ResultType = Context.getPointerType(AllocType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1772 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1773 | if (ArraySize && ArraySize->getType()->isNonOverloadPlaceholderType()) { |
| 1774 | ExprResult result = CheckPlaceholderExpr(ArraySize); |
| 1775 | if (result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1776 | ArraySize = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1777 | } |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 1778 | // C++98 5.3.4p6: "The expression in a direct-new-declarator shall have |
| 1779 | // integral or enumeration type with a non-negative value." |
| 1780 | // C++11 [expr.new]p6: The expression [...] shall be of integral or unscoped |
| 1781 | // enumeration type, or a class type for which a single non-explicit |
| 1782 | // conversion function to integral or unscoped enumeration type exists. |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1783 | // C++1y [expr.new]p6: The expression [...] is implicitly converted to |
Larisse Voufo | bf4aa57 | 2013-06-18 03:08:53 +0000 | [diff] [blame] | 1784 | // std::size_t. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1785 | llvm::Optional<uint64_t> KnownArraySize; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1786 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1787 | ExprResult ConvertedSize; |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1788 | if (getLangOpts().CPlusPlus14) { |
Alp Toker | 965f882 | 2013-11-27 05:22:15 +0000 | [diff] [blame] | 1789 | assert(Context.getTargetInfo().getIntWidth() && "Builtin type of size 0?"); |
| 1790 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1791 | ConvertedSize = PerformImplicitConversion(ArraySize, Context.getSizeType(), |
| 1792 | AA_Converting); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1793 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1794 | if (!ConvertedSize.isInvalid() && |
Larisse Voufo | bf4aa57 | 2013-06-18 03:08:53 +0000 | [diff] [blame] | 1795 | ArraySize->getType()->getAs<RecordType>()) |
Larisse Voufo | 9f380c5 | 2013-06-18 01:27:47 +0000 | [diff] [blame] | 1796 | // Diagnose the compatibility of this conversion. |
| 1797 | Diag(StartLoc, diag::warn_cxx98_compat_array_size_conversion) |
| 1798 | << ArraySize->getType() << 0 << "'size_t'"; |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1799 | } else { |
| 1800 | class SizeConvertDiagnoser : public ICEConvertDiagnoser { |
| 1801 | protected: |
| 1802 | Expr *ArraySize; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 1803 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1804 | public: |
| 1805 | SizeConvertDiagnoser(Expr *ArraySize) |
| 1806 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false, false, false), |
| 1807 | ArraySize(ArraySize) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1808 | |
| 1809 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 1810 | QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1811 | return S.Diag(Loc, diag::err_array_size_not_integral) |
| 1812 | << S.getLangOpts().CPlusPlus11 << T; |
| 1813 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1814 | |
| 1815 | SemaDiagnosticBuilder diagnoseIncomplete( |
| 1816 | Sema &S, SourceLocation Loc, QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1817 | return S.Diag(Loc, diag::err_array_size_incomplete_type) |
| 1818 | << T << ArraySize->getSourceRange(); |
| 1819 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1820 | |
| 1821 | SemaDiagnosticBuilder diagnoseExplicitConv( |
| 1822 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1823 | return S.Diag(Loc, diag::err_array_size_explicit_conversion) << T << ConvTy; |
| 1824 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1825 | |
| 1826 | SemaDiagnosticBuilder noteExplicitConv( |
| 1827 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1828 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
| 1829 | << ConvTy->isEnumeralType() << ConvTy; |
| 1830 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1831 | |
| 1832 | SemaDiagnosticBuilder diagnoseAmbiguous( |
| 1833 | Sema &S, SourceLocation Loc, QualType T) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1834 | return S.Diag(Loc, diag::err_array_size_ambiguous_conversion) << T; |
| 1835 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1836 | |
| 1837 | SemaDiagnosticBuilder noteAmbiguous( |
| 1838 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1839 | return S.Diag(Conv->getLocation(), diag::note_array_size_conversion) |
| 1840 | << ConvTy->isEnumeralType() << ConvTy; |
| 1841 | } |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1842 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1843 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
| 1844 | QualType T, |
| 1845 | QualType ConvTy) override { |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1846 | return S.Diag(Loc, |
| 1847 | S.getLangOpts().CPlusPlus11 |
| 1848 | ? diag::warn_cxx98_compat_array_size_conversion |
| 1849 | : diag::ext_array_size_conversion) |
| 1850 | << T << ConvTy->isEnumeralType() << ConvTy; |
| 1851 | } |
| 1852 | } SizeDiagnoser(ArraySize); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 1853 | |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1854 | ConvertedSize = PerformContextualImplicitConversion(StartLoc, ArraySize, |
| 1855 | SizeDiagnoser); |
| 1856 | } |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1857 | if (ConvertedSize.isInvalid()) |
| 1858 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1859 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1860 | ArraySize = ConvertedSize.get(); |
John McCall | 9b80c21 | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 1861 | QualType SizeType = ArraySize->getType(); |
Larisse Voufo | 0f1394c | 2013-06-15 20:17:46 +0000 | [diff] [blame] | 1862 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1863 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1864 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1865 | |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1866 | // C++98 [expr.new]p7: |
| 1867 | // The expression in a direct-new-declarator shall have integral type |
| 1868 | // with a non-negative value. |
| 1869 | // |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1870 | // Let's see if this is a constant < 0. If so, we reject it out of hand, |
| 1871 | // per CWG1464. Otherwise, if it's not a constant, we must have an |
| 1872 | // unparenthesized array type. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1873 | if (!ArraySize->isValueDependent()) { |
| 1874 | llvm::APSInt Value; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1875 | // We've already performed any required implicit conversion to integer or |
| 1876 | // unscoped enumeration type. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1877 | // FIXME: Per CWG1464, we are required to check the value prior to |
| 1878 | // converting to size_t. This will never find a negative array size in |
| 1879 | // C++14 onwards, because Value is always unsigned here! |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1880 | if (ArraySize->isIntegerConstantExpr(Value, Context)) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1881 | if (Value.isSigned() && Value.isNegative()) { |
| 1882 | return ExprError(Diag(ArraySize->getLocStart(), |
| 1883 | diag::err_typecheck_negative_array_size) |
| 1884 | << ArraySize->getSourceRange()); |
| 1885 | } |
| 1886 | |
| 1887 | if (!AllocType->isDependentType()) { |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1888 | unsigned ActiveSizeBits = |
| 1889 | ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1890 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) |
| 1891 | return ExprError(Diag(ArraySize->getLocStart(), |
| 1892 | diag::err_array_too_large) |
| 1893 | << Value.toString(10) |
| 1894 | << ArraySize->getSourceRange()); |
Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1895 | } |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1896 | |
| 1897 | KnownArraySize = Value.getZExtValue(); |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1898 | } else if (TypeIdParens.isValid()) { |
| 1899 | // Can't have dynamic array size when the type-id is in parentheses. |
| 1900 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 1901 | << ArraySize->getSourceRange() |
| 1902 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 1903 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1904 | |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1905 | TypeIdParens = SourceRange(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1906 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1907 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1908 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 1909 | // Note that we do *not* convert the argument in any way. It can |
| 1910 | // be signed, larger than size_t, whatever. |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1911 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1912 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1913 | FunctionDecl *OperatorNew = nullptr; |
| 1914 | FunctionDecl *OperatorDelete = nullptr; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1915 | unsigned Alignment = |
| 1916 | AllocType->isDependentType() ? 0 : Context.getTypeAlign(AllocType); |
| 1917 | unsigned NewAlignment = Context.getTargetInfo().getNewAlign(); |
| 1918 | bool PassAlignment = getLangOpts().AlignedAllocation && |
| 1919 | Alignment > NewAlignment; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1920 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1921 | if (!AllocType->isDependentType() && |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 1922 | !Expr::hasAnyTypeDependentArguments(PlacementArgs) && |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1923 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1924 | SourceRange(PlacementLParen, PlacementRParen), |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1925 | UseGlobal, AllocType, ArraySize, PassAlignment, |
| 1926 | PlacementArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1927 | return ExprError(); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1928 | |
| 1929 | // If this is an array allocation, compute whether the usual array |
| 1930 | // deallocation function for the type has a size_t parameter. |
| 1931 | bool UsualArrayDeleteWantsSize = false; |
| 1932 | if (ArraySize && !AllocType->isDependentType()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1933 | UsualArrayDeleteWantsSize = |
| 1934 | doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1936 | SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 1eab66c | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1937 | if (OperatorNew) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1938 | const FunctionProtoType *Proto = |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 1939 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
| 1940 | VariadicCallType CallType = Proto->isVariadic() ? VariadicFunction |
| 1941 | : VariadicDoesNotApply; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1942 | |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 1943 | // We've already converted the placement args, just fill in any default |
| 1944 | // arguments. Skip the first parameter because we don't have a corresponding |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1945 | // argument. Skip the second parameter too if we're passing in the |
| 1946 | // alignment; we've already filled it in. |
| 1947 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, Proto, |
| 1948 | PassAlignment ? 2 : 1, PlacementArgs, |
| 1949 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1950 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1951 | |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 1952 | if (!AllPlaceArgs.empty()) |
| 1953 | PlacementArgs = AllPlaceArgs; |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 1954 | |
Richard Smith | d6f9e73 | 2014-05-13 19:56:21 +0000 | [diff] [blame] | 1955 | // FIXME: This is wrong: PlacementArgs misses out the first (size) argument. |
Dmitri Gribenko | be02210 | 2013-05-10 13:22:23 +0000 | [diff] [blame] | 1956 | DiagnoseSentinelCalls(OperatorNew, PlacementLParen, PlacementArgs); |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 1957 | |
| 1958 | // FIXME: Missing call to CheckFunctionCall or equivalent |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1959 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1960 | // Warn if the type is over-aligned and is being allocated by (unaligned) |
| 1961 | // global operator new. |
| 1962 | if (PlacementArgs.empty() && !PassAlignment && |
| 1963 | (OperatorNew->isImplicit() || |
| 1964 | (OperatorNew->getLocStart().isValid() && |
| 1965 | getSourceManager().isInSystemHeader(OperatorNew->getLocStart())))) { |
| 1966 | if (Alignment > NewAlignment) |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 1967 | Diag(StartLoc, diag::warn_overaligned_type) |
| 1968 | << AllocType |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1969 | << unsigned(Alignment / Context.getCharWidth()) |
| 1970 | << unsigned(NewAlignment / Context.getCharWidth()); |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 1971 | } |
| 1972 | } |
| 1973 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1974 | // Array 'new' can't have any initializers except empty parentheses. |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1975 | // Initializer lists are also allowed, in C++11. Rely on the parser for the |
| 1976 | // dialect distinction. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1977 | if (ArraySize && !isLegalArrayNewInitializer(initStyle, Initializer)) { |
| 1978 | SourceRange InitRange(Inits[0]->getLocStart(), |
| 1979 | Inits[NumInits - 1]->getLocEnd()); |
| 1980 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 1981 | return ExprError(); |
Anders Carlsson | c6bb0e1 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Richard Smith | dd2ca57 | 2012-11-26 08:32:48 +0000 | [diff] [blame] | 1984 | // If we can perform the initialization, and we've not already done so, |
| 1985 | // do it now. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1986 | if (!AllocType->isDependentType() && |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1987 | !Expr::hasAnyTypeDependentArguments( |
Richard Smith | c6abd96 | 2014-07-25 01:12:44 +0000 | [diff] [blame] | 1988 | llvm::makeArrayRef(Inits, NumInits))) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1989 | // The type we initialize is the complete type, including the array bound. |
| 1990 | QualType InitType; |
| 1991 | if (KnownArraySize) |
| 1992 | InitType = Context.getConstantArrayType( |
| 1993 | AllocType, llvm::APInt(Context.getTypeSize(Context.getSizeType()), |
| 1994 | *KnownArraySize), |
| 1995 | ArrayType::Normal, 0); |
| 1996 | else if (ArraySize) |
| 1997 | InitType = |
| 1998 | Context.getIncompleteArrayType(AllocType, ArrayType::Normal, 0); |
| 1999 | else |
| 2000 | InitType = AllocType; |
| 2001 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2002 | InitializedEntity Entity |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 2003 | = InitializedEntity::InitializeNew(StartLoc, InitType); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2004 | InitializationSequence InitSeq(*this, Entity, Kind, |
| 2005 | MultiExprArg(Inits, NumInits)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2006 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2007 | MultiExprArg(Inits, NumInits)); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2008 | if (FullInit.isInvalid()) |
| 2009 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2010 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2011 | // FullInit is our initializer; strip off CXXBindTemporaryExprs, because |
| 2012 | // we don't want the initialized object to be destructed. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 2013 | // FIXME: We should not create these in the first place. |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2014 | if (CXXBindTemporaryExpr *Binder = |
| 2015 | dyn_cast_or_null<CXXBindTemporaryExpr>(FullInit.get())) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2016 | FullInit = Binder->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2017 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2018 | Initializer = FullInit.get(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2019 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2020 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2021 | // Mark the new and delete operators as referenced. |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2022 | if (OperatorNew) { |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2023 | if (DiagnoseUseOfDecl(OperatorNew, StartLoc)) |
| 2024 | return ExprError(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2025 | MarkFunctionReferenced(StartLoc, OperatorNew); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2026 | } |
| 2027 | if (OperatorDelete) { |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2028 | if (DiagnoseUseOfDecl(OperatorDelete, StartLoc)) |
| 2029 | return ExprError(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2030 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2031 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2032 | |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 2033 | // C++0x [expr.new]p17: |
| 2034 | // If the new expression creates an array of objects of class type, |
| 2035 | // access and ambiguity control are done for the destructor. |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2036 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
| 2037 | if (ArraySize && !BaseAllocType->isDependentType()) { |
| 2038 | if (const RecordType *BaseRecordType = BaseAllocType->getAs<RecordType>()) { |
| 2039 | if (CXXDestructorDecl *dtor = LookupDestructor( |
| 2040 | cast<CXXRecordDecl>(BaseRecordType->getDecl()))) { |
| 2041 | MarkFunctionReferenced(StartLoc, dtor); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2042 | CheckDestructorAccess(StartLoc, dtor, |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2043 | PDiag(diag::err_access_dtor) |
| 2044 | << BaseAllocType); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 2045 | if (DiagnoseUseOfDecl(dtor, StartLoc)) |
| 2046 | return ExprError(); |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 2047 | } |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 2048 | } |
| 2049 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2050 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2051 | return new (Context) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2052 | CXXNewExpr(Context, UseGlobal, OperatorNew, OperatorDelete, PassAlignment, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2053 | UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens, |
| 2054 | ArraySize, initStyle, Initializer, ResultType, AllocTypeInfo, |
| 2055 | Range, DirectInitRange); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2058 | /// \brief Checks that a type is suitable as the allocated type |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2059 | /// in a new-expression. |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2060 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | SourceRange R) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2062 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 2063 | // abstract class type or array thereof. |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2064 | if (AllocType->isFunctionType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2065 | return Diag(Loc, diag::err_bad_new_type) |
| 2066 | << AllocType << 0 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2067 | else if (AllocType->isReferenceType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2068 | return Diag(Loc, diag::err_bad_new_type) |
| 2069 | << AllocType << 1 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2070 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2071 | RequireCompleteType(Loc, AllocType, diag::err_new_incomplete_type,R)) |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2072 | return true; |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 2073 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2074 | diag::err_allocation_of_abstract_type)) |
| 2075 | return true; |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 2076 | else if (AllocType->isVariablyModifiedType()) |
| 2077 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 2078 | << AllocType; |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 2079 | else if (AllocType.getAddressSpace()) |
Douglas Gregor | 39d1a09 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 2080 | return Diag(Loc, diag::err_address_space_qualified_new) |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 2081 | << AllocType.getUnqualifiedType() |
| 2082 | << AllocType.getQualifiers().getAddressSpaceAttributePrintValue(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2083 | else if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2084 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { |
| 2085 | QualType BaseAllocType = Context.getBaseElementType(AT); |
| 2086 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 2087 | BaseAllocType->isObjCLifetimeType()) |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 2088 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2089 | << BaseAllocType; |
| 2090 | } |
| 2091 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2092 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2093 | return false; |
| 2094 | } |
| 2095 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2096 | static bool |
| 2097 | resolveAllocationOverload(Sema &S, LookupResult &R, SourceRange Range, |
| 2098 | SmallVectorImpl<Expr *> &Args, bool &PassAlignment, |
| 2099 | FunctionDecl *&Operator, |
| 2100 | OverloadCandidateSet *AlignedCandidates = nullptr, |
| 2101 | Expr *AlignArg = nullptr) { |
| 2102 | OverloadCandidateSet Candidates(R.getNameLoc(), |
| 2103 | OverloadCandidateSet::CSK_Normal); |
| 2104 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 2105 | Alloc != AllocEnd; ++Alloc) { |
| 2106 | // Even member operator new/delete are implicitly treated as |
| 2107 | // static, so don't use AddMemberCandidate. |
| 2108 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
| 2109 | |
| 2110 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 2111 | S.AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
| 2112 | /*ExplicitTemplateArgs=*/nullptr, Args, |
| 2113 | Candidates, |
| 2114 | /*SuppressUserConversions=*/false); |
| 2115 | continue; |
| 2116 | } |
| 2117 | |
| 2118 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 2119 | S.AddOverloadCandidate(Fn, Alloc.getPair(), Args, Candidates, |
| 2120 | /*SuppressUserConversions=*/false); |
| 2121 | } |
| 2122 | |
| 2123 | // Do the resolution. |
| 2124 | OverloadCandidateSet::iterator Best; |
| 2125 | switch (Candidates.BestViableFunction(S, R.getNameLoc(), Best)) { |
| 2126 | case OR_Success: { |
| 2127 | // Got one! |
| 2128 | FunctionDecl *FnDecl = Best->Function; |
| 2129 | if (S.CheckAllocationAccess(R.getNameLoc(), Range, R.getNamingClass(), |
| 2130 | Best->FoundDecl) == Sema::AR_inaccessible) |
| 2131 | return true; |
| 2132 | |
| 2133 | Operator = FnDecl; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2134 | return false; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2135 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2136 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2137 | case OR_No_Viable_Function: |
| 2138 | // C++17 [expr.new]p13: |
| 2139 | // If no matching function is found and the allocated object type has |
| 2140 | // new-extended alignment, the alignment argument is removed from the |
| 2141 | // argument list, and overload resolution is performed again. |
| 2142 | if (PassAlignment) { |
| 2143 | PassAlignment = false; |
| 2144 | AlignArg = Args[1]; |
| 2145 | Args.erase(Args.begin() + 1); |
| 2146 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
| 2147 | Operator, &Candidates, AlignArg); |
| 2148 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2149 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2150 | // MSVC will fall back on trying to find a matching global operator new |
| 2151 | // if operator new[] cannot be found. Also, MSVC will leak by not |
| 2152 | // generating a call to operator delete or operator delete[], but we |
| 2153 | // will not replicate that bug. |
| 2154 | // FIXME: Find out how this interacts with the std::align_val_t fallback |
| 2155 | // once MSVC implements it. |
| 2156 | if (R.getLookupName().getCXXOverloadedOperator() == OO_Array_New && |
| 2157 | S.Context.getLangOpts().MSVCCompat) { |
| 2158 | R.clear(); |
| 2159 | R.setLookupName(S.Context.DeclarationNames.getCXXOperatorName(OO_New)); |
| 2160 | S.LookupQualifiedName(R, S.Context.getTranslationUnitDecl()); |
| 2161 | // FIXME: This will give bad diagnostics pointing at the wrong functions. |
| 2162 | return resolveAllocationOverload(S, R, Range, Args, PassAlignment, |
| 2163 | Operator, nullptr); |
| 2164 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2165 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2166 | S.Diag(R.getNameLoc(), diag::err_ovl_no_viable_function_in_call) |
| 2167 | << R.getLookupName() << Range; |
| 2168 | |
| 2169 | // If we have aligned candidates, only note the align_val_t candidates |
| 2170 | // from AlignedCandidates and the non-align_val_t candidates from |
| 2171 | // Candidates. |
| 2172 | if (AlignedCandidates) { |
| 2173 | auto IsAligned = [](OverloadCandidate &C) { |
| 2174 | return C.Function->getNumParams() > 1 && |
| 2175 | C.Function->getParamDecl(1)->getType()->isAlignValT(); |
| 2176 | }; |
| 2177 | auto IsUnaligned = [&](OverloadCandidate &C) { return !IsAligned(C); }; |
| 2178 | |
| 2179 | // This was an overaligned allocation, so list the aligned candidates |
| 2180 | // first. |
| 2181 | Args.insert(Args.begin() + 1, AlignArg); |
| 2182 | AlignedCandidates->NoteCandidates(S, OCD_AllCandidates, Args, "", |
| 2183 | R.getNameLoc(), IsAligned); |
| 2184 | Args.erase(Args.begin() + 1); |
| 2185 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args, "", R.getNameLoc(), |
| 2186 | IsUnaligned); |
| 2187 | } else { |
| 2188 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 2189 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2190 | return true; |
| 2191 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2192 | case OR_Ambiguous: |
| 2193 | S.Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) |
| 2194 | << R.getLookupName() << Range; |
| 2195 | Candidates.NoteCandidates(S, OCD_ViableCandidates, Args); |
| 2196 | return true; |
| 2197 | |
| 2198 | case OR_Deleted: { |
| 2199 | S.Diag(R.getNameLoc(), diag::err_ovl_deleted_call) |
| 2200 | << Best->Function->isDeleted() |
| 2201 | << R.getLookupName() |
| 2202 | << S.getDeletedOrUnavailableSuffix(Best->Function) |
| 2203 | << Range; |
| 2204 | Candidates.NoteCandidates(S, OCD_AllCandidates, Args); |
| 2205 | return true; |
| 2206 | } |
| 2207 | } |
| 2208 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2211 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2212 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 2213 | /// that are appropriate for the allocation. |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 2214 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 2215 | bool UseGlobal, QualType AllocType, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2216 | bool IsArray, bool &PassAlignment, |
| 2217 | MultiExprArg PlaceArgs, |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2218 | FunctionDecl *&OperatorNew, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2220 | // --- Choosing an allocation function --- |
| 2221 | // C++ 5.3.4p8 - 14 & 18 |
| 2222 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 2223 | // in the scope of the allocated class. |
| 2224 | // 2) If an array size is given, look for operator new[], else look for |
| 2225 | // operator new. |
| 2226 | // 3) The first argument is always size_t. Append the arguments from the |
| 2227 | // placement form. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2228 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2229 | SmallVector<Expr*, 8> AllocArgs; |
| 2230 | AllocArgs.reserve((PassAlignment ? 2 : 1) + PlaceArgs.size()); |
| 2231 | |
| 2232 | // We don't care about the actual value of these arguments. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2233 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 2234 | // tree? Or should the consumer just recalculate the value? |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2235 | // FIXME: Using a dummy value will interact poorly with attribute enable_if. |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2236 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2237 | Context.getTargetInfo().getPointerWidth(0)), |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2238 | Context.getSizeType(), |
| 2239 | SourceLocation()); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2240 | AllocArgs.push_back(&Size); |
| 2241 | |
| 2242 | QualType AlignValT = Context.VoidTy; |
| 2243 | if (PassAlignment) { |
| 2244 | DeclareGlobalNewDelete(); |
| 2245 | AlignValT = Context.getTypeDeclType(getStdAlignValT()); |
| 2246 | } |
| 2247 | CXXScalarValueInitExpr Align(AlignValT, nullptr, SourceLocation()); |
| 2248 | if (PassAlignment) |
| 2249 | AllocArgs.push_back(&Align); |
| 2250 | |
| 2251 | AllocArgs.insert(AllocArgs.end(), PlaceArgs.begin(), PlaceArgs.end()); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2252 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2253 | // C++ [expr.new]p8: |
| 2254 | // If the allocated type is a non-array type, the allocation |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2255 | // function's name is operator new and the deallocation function's |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2256 | // name is operator delete. If the allocated type is an array |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2257 | // type, the allocation function's name is operator new[] and the |
| 2258 | // deallocation function's name is operator delete[]. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2259 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2260 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2261 | |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2262 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 2263 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2264 | // Find the allocation function. |
| 2265 | { |
| 2266 | LookupResult R(*this, NewName, StartLoc, LookupOrdinaryName); |
| 2267 | |
| 2268 | // C++1z [expr.new]p9: |
| 2269 | // If the new-expression begins with a unary :: operator, the allocation |
| 2270 | // function's name is looked up in the global scope. Otherwise, if the |
| 2271 | // allocated type is a class type T or array thereof, the allocation |
| 2272 | // function's name is looked up in the scope of T. |
| 2273 | if (AllocElemType->isRecordType() && !UseGlobal) |
| 2274 | LookupQualifiedName(R, AllocElemType->getAsCXXRecordDecl()); |
| 2275 | |
| 2276 | // We can see ambiguity here if the allocation function is found in |
| 2277 | // multiple base classes. |
| 2278 | if (R.isAmbiguous()) |
| 2279 | return true; |
| 2280 | |
| 2281 | // If this lookup fails to find the name, or if the allocated type is not |
| 2282 | // a class type, the allocation function's name is looked up in the |
| 2283 | // global scope. |
| 2284 | if (R.empty()) |
| 2285 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 2286 | |
| 2287 | assert(!R.empty() && "implicitly declared allocation functions not found"); |
| 2288 | assert(!R.isAmbiguous() && "global allocation functions are ambiguous"); |
| 2289 | |
| 2290 | // We do our own custom access checks below. |
| 2291 | R.suppressDiagnostics(); |
| 2292 | |
| 2293 | if (resolveAllocationOverload(*this, R, Range, AllocArgs, PassAlignment, |
| 2294 | OperatorNew)) |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2295 | return true; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2296 | } |
Aaron Ballman | 324fbee | 2013-05-30 01:55:39 +0000 | [diff] [blame] | 2297 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2298 | // 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] | 2299 | if (!getLangOpts().Exceptions) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2300 | OperatorDelete = nullptr; |
John McCall | 0f55a03 | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 2301 | return false; |
| 2302 | } |
| 2303 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2304 | // Note, the name of OperatorNew might have been changed from array to |
| 2305 | // non-array by resolveAllocationOverload. |
| 2306 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 2307 | OperatorNew->getDeclName().getCXXOverloadedOperator() == OO_Array_New |
| 2308 | ? OO_Array_Delete |
| 2309 | : OO_Delete); |
| 2310 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2311 | // C++ [expr.new]p19: |
| 2312 | // |
| 2313 | // If the new-expression begins with a unary :: operator, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2314 | // deallocation function's name is looked up in the global |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2315 | // 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] | 2316 | // array thereof, the deallocation function's name is looked up in |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2317 | // the scope of T. If this lookup fails to find the name, or if |
| 2318 | // the allocated type is not a class type or array thereof, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2319 | // deallocation function's name is looked up in the global scope. |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2320 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2321 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2322 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 2323 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2324 | LookupQualifiedName(FoundDelete, RD); |
| 2325 | } |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 2326 | if (FoundDelete.isAmbiguous()) |
| 2327 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2328 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2329 | bool FoundGlobalDelete = FoundDelete.empty(); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2330 | if (FoundDelete.empty()) { |
| 2331 | DeclareGlobalNewDelete(); |
| 2332 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 2333 | } |
| 2334 | |
| 2335 | FoundDelete.suppressDiagnostics(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2336 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2337 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2338 | |
John McCall | d3be2c8 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 2339 | // Whether we're looking for a placement operator delete is dictated |
| 2340 | // by whether we selected a placement operator new, not by whether |
| 2341 | // we had explicit placement arguments. This matters for things like |
| 2342 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 2343 | // A *a = new A() |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2344 | // |
| 2345 | // We don't have any definition for what a "placement allocation function" |
| 2346 | // is, but we assume it's any allocation function whose |
| 2347 | // parameter-declaration-clause is anything other than (size_t). |
| 2348 | // |
| 2349 | // FIXME: Should (size_t, std::align_val_t) also be considered non-placement? |
| 2350 | // This affects whether an exception from the constructor of an overaligned |
| 2351 | // type uses the sized or non-sized form of aligned operator delete. |
| 2352 | bool isPlacementNew = !PlaceArgs.empty() || OperatorNew->param_size() != 1 || |
| 2353 | OperatorNew->isVariadic(); |
John McCall | d3be2c8 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 2354 | |
| 2355 | if (isPlacementNew) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2356 | // C++ [expr.new]p20: |
| 2357 | // A declaration of a placement deallocation function matches the |
| 2358 | // declaration of a placement allocation function if it has the |
| 2359 | // same number of parameters and, after parameter transformations |
| 2360 | // (8.3.5), all parameter types except the first are |
| 2361 | // identical. [...] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2362 | // |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2363 | // To perform this comparison, we compute the function type that |
| 2364 | // the deallocation function should have, and use that type both |
| 2365 | // for template argument deduction and for comparison purposes. |
| 2366 | QualType ExpectedFunctionType; |
| 2367 | { |
| 2368 | const FunctionProtoType *Proto |
| 2369 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2370 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2371 | SmallVector<QualType, 4> ArgTypes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2372 | ArgTypes.push_back(Context.VoidPtrTy); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2373 | for (unsigned I = 1, N = Proto->getNumParams(); I < N; ++I) |
| 2374 | ArgTypes.push_back(Proto->getParamType(I)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2375 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2376 | FunctionProtoType::ExtProtoInfo EPI; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2377 | // FIXME: This is not part of the standard's rule. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2378 | EPI.Variadic = Proto->isVariadic(); |
| 2379 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2380 | ExpectedFunctionType |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2381 | = Context.getFunctionType(Context.VoidTy, ArgTypes, EPI); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2384 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2385 | DEnd = FoundDelete.end(); |
| 2386 | D != DEnd; ++D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2387 | FunctionDecl *Fn = nullptr; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 2388 | if (FunctionTemplateDecl *FnTmpl = |
| 2389 | dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2390 | // Perform template argument deduction to try to match the |
| 2391 | // expected function type. |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 2392 | TemplateDeductionInfo Info(StartLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2393 | if (DeduceTemplateArguments(FnTmpl, nullptr, ExpectedFunctionType, Fn, |
| 2394 | Info)) |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2395 | continue; |
| 2396 | } else |
| 2397 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 2398 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 2399 | if (Context.hasSameType(adjustCCAndNoReturn(Fn->getType(), |
| 2400 | ExpectedFunctionType, |
| 2401 | /*AdjustExcpetionSpec*/true), |
| 2402 | ExpectedFunctionType)) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2403 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2404 | } |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 2405 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2406 | if (getLangOpts().CUDA) |
| 2407 | EraseUnwantedCUDAMatches(dyn_cast<FunctionDecl>(CurContext), Matches); |
| 2408 | } else { |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2409 | // C++1y [expr.new]p22: |
| 2410 | // For a non-placement allocation function, the normal deallocation |
| 2411 | // function lookup is used |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2412 | // |
| 2413 | // Per [expr.delete]p10, this lookup prefers a member operator delete |
| 2414 | // without a size_t argument, but prefers a non-member operator delete |
| 2415 | // with a size_t where possible (which it always is in this case). |
| 2416 | llvm::SmallVector<UsualDeallocFnInfo, 4> BestDeallocFns; |
| 2417 | UsualDeallocFnInfo Selected = resolveDeallocationOverload( |
| 2418 | *this, FoundDelete, /*WantSize*/ FoundGlobalDelete, |
| 2419 | /*WantAlign*/ hasNewExtendedAlignment(*this, AllocElemType), |
| 2420 | &BestDeallocFns); |
| 2421 | if (Selected) |
| 2422 | Matches.push_back(std::make_pair(Selected.Found, Selected.FD)); |
| 2423 | else { |
| 2424 | // If we failed to select an operator, all remaining functions are viable |
| 2425 | // but ambiguous. |
| 2426 | for (auto Fn : BestDeallocFns) |
| 2427 | Matches.push_back(std::make_pair(Fn.Found, Fn.FD)); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2428 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | // C++ [expr.new]p20: |
| 2432 | // [...] If the lookup finds a single matching deallocation |
| 2433 | // function, that function will be called; otherwise, no |
| 2434 | // deallocation function will be called. |
| 2435 | if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2436 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2437 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2438 | // C++1z [expr.new]p23: |
| 2439 | // If the lookup finds a usual deallocation function (3.7.4.2) |
| 2440 | // with a parameter of type std::size_t and that function, considered |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2441 | // as a placement deallocation function, would have been |
| 2442 | // selected as a match for the allocation function, the program |
| 2443 | // is ill-formed. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2444 | if (getLangOpts().CPlusPlus11 && isPlacementNew && |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2445 | isNonPlacementDeallocationFunction(*this, OperatorDelete)) { |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 2446 | UsualDeallocFnInfo Info(*this, |
| 2447 | DeclAccessPair::make(OperatorDelete, AS_public)); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2448 | // Core issue, per mail to core reflector, 2016-10-09: |
| 2449 | // If this is a member operator delete, and there is a corresponding |
| 2450 | // non-sized member operator delete, this isn't /really/ a sized |
| 2451 | // deallocation function, it just happens to have a size_t parameter. |
| 2452 | bool IsSizedDelete = Info.HasSizeT; |
| 2453 | if (IsSizedDelete && !FoundGlobalDelete) { |
| 2454 | auto NonSizedDelete = |
| 2455 | resolveDeallocationOverload(*this, FoundDelete, /*WantSize*/false, |
| 2456 | /*WantAlign*/Info.HasAlignValT); |
| 2457 | if (NonSizedDelete && !NonSizedDelete.HasSizeT && |
| 2458 | NonSizedDelete.HasAlignValT == Info.HasAlignValT) |
| 2459 | IsSizedDelete = false; |
| 2460 | } |
| 2461 | |
| 2462 | if (IsSizedDelete) { |
| 2463 | SourceRange R = PlaceArgs.empty() |
| 2464 | ? SourceRange() |
| 2465 | : SourceRange(PlaceArgs.front()->getLocStart(), |
| 2466 | PlaceArgs.back()->getLocEnd()); |
| 2467 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) << R; |
| 2468 | if (!OperatorDelete->isImplicit()) |
| 2469 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 2470 | << DeleteName; |
| 2471 | } |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2472 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2473 | |
| 2474 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
| 2475 | Matches[0].first); |
| 2476 | } else if (!Matches.empty()) { |
| 2477 | // We found multiple suitable operators. Per [expr.new]p20, that means we |
| 2478 | // call no 'operator delete' function, but we should at least warn the user. |
| 2479 | // FIXME: Suppress this warning if the construction cannot throw. |
| 2480 | Diag(StartLoc, diag::warn_ambiguous_suitable_delete_function_found) |
| 2481 | << DeleteName << AllocElemType; |
| 2482 | |
| 2483 | for (auto &Match : Matches) |
| 2484 | Diag(Match.second->getLocation(), |
| 2485 | diag::note_member_declared_here) << DeleteName; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2488 | return false; |
| 2489 | } |
| 2490 | |
| 2491 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 2492 | /// delete. These are: |
| 2493 | /// @code |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2494 | /// // C++03: |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2495 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 2496 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 2497 | /// void operator delete(void *) throw(); |
| 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; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2511 | /// @endcode |
| 2512 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 2513 | /// declared. Their use requires including \<new\>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2514 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2515 | if (GlobalNewDeleteDeclared) |
| 2516 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2517 | |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2518 | // C++ [basic.std.dynamic]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2519 | // [...] The following allocation and deallocation functions (18.4) are |
| 2520 | // implicitly declared in global scope in each translation unit of a |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2521 | // program |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2522 | // |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2523 | // C++03: |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2524 | // void* operator new(std::size_t) throw(std::bad_alloc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2525 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 2526 | // void operator delete(void*) throw(); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2527 | // void operator delete[](void*) throw(); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2528 | // C++11: |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2529 | // void* operator new(std::size_t); |
| 2530 | // void* operator new[](std::size_t); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2531 | // void operator delete(void*) noexcept; |
| 2532 | // void operator delete[](void*) noexcept; |
| 2533 | // C++1y: |
| 2534 | // void* operator new(std::size_t); |
| 2535 | // void* operator new[](std::size_t); |
| 2536 | // void operator delete(void*) noexcept; |
| 2537 | // void operator delete[](void*) noexcept; |
| 2538 | // void operator delete(void*, std::size_t) noexcept; |
| 2539 | // void operator delete[](void*, std::size_t) noexcept; |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2540 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2541 | // These implicit declarations introduce only the function names operator |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2542 | // new, operator new[], operator delete, operator delete[]. |
| 2543 | // |
| 2544 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 2545 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 2546 | // However, we do not make these implicit declarations visible to name |
| 2547 | // lookup. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2548 | if (!StdBadAlloc && !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2549 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 2550 | // implicitly. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2551 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
| 2552 | getOrCreateStdNamespace(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2553 | SourceLocation(), SourceLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2554 | &PP.getIdentifierTable().get("bad_alloc"), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2555 | nullptr); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 2556 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2557 | } |
Richard Smith | 5913902 | 2016-09-30 22:41:36 +0000 | [diff] [blame] | 2558 | if (!StdAlignValT && getLangOpts().AlignedAllocation) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2559 | // The "std::align_val_t" enum class has not yet been declared, so build it |
| 2560 | // implicitly. |
| 2561 | auto *AlignValT = EnumDecl::Create( |
| 2562 | Context, getOrCreateStdNamespace(), SourceLocation(), SourceLocation(), |
| 2563 | &PP.getIdentifierTable().get("align_val_t"), nullptr, true, true, true); |
| 2564 | AlignValT->setIntegerType(Context.getSizeType()); |
| 2565 | AlignValT->setPromotionType(Context.getSizeType()); |
| 2566 | AlignValT->setImplicit(true); |
| 2567 | StdAlignValT = AlignValT; |
| 2568 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2569 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2570 | GlobalNewDeleteDeclared = true; |
| 2571 | |
| 2572 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 2573 | QualType SizeT = Context.getSizeType(); |
| 2574 | |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2575 | auto DeclareGlobalAllocationFunctions = [&](OverloadedOperatorKind Kind, |
| 2576 | QualType Return, QualType Param) { |
| 2577 | llvm::SmallVector<QualType, 3> Params; |
| 2578 | Params.push_back(Param); |
| 2579 | |
| 2580 | // Create up to four variants of the function (sized/aligned). |
| 2581 | bool HasSizedVariant = getLangOpts().SizedDeallocation && |
| 2582 | (Kind == OO_Delete || Kind == OO_Array_Delete); |
Richard Smith | 5913902 | 2016-09-30 22:41:36 +0000 | [diff] [blame] | 2583 | bool HasAlignedVariant = getLangOpts().AlignedAllocation; |
Simon Pilgrim | d69fc8e | 2016-09-30 14:18:06 +0000 | [diff] [blame] | 2584 | |
| 2585 | int NumSizeVariants = (HasSizedVariant ? 2 : 1); |
| 2586 | int NumAlignVariants = (HasAlignedVariant ? 2 : 1); |
| 2587 | for (int Sized = 0; Sized < NumSizeVariants; ++Sized) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2588 | if (Sized) |
| 2589 | Params.push_back(SizeT); |
| 2590 | |
Simon Pilgrim | d69fc8e | 2016-09-30 14:18:06 +0000 | [diff] [blame] | 2591 | for (int Aligned = 0; Aligned < NumAlignVariants; ++Aligned) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2592 | if (Aligned) |
| 2593 | Params.push_back(Context.getTypeDeclType(getStdAlignValT())); |
| 2594 | |
| 2595 | DeclareGlobalAllocationFunction( |
| 2596 | Context.DeclarationNames.getCXXOperatorName(Kind), Return, Params); |
| 2597 | |
| 2598 | if (Aligned) |
| 2599 | Params.pop_back(); |
| 2600 | } |
| 2601 | } |
| 2602 | }; |
| 2603 | |
| 2604 | DeclareGlobalAllocationFunctions(OO_New, VoidPtr, SizeT); |
| 2605 | DeclareGlobalAllocationFunctions(OO_Array_New, VoidPtr, SizeT); |
| 2606 | DeclareGlobalAllocationFunctions(OO_Delete, Context.VoidTy, VoidPtr); |
| 2607 | DeclareGlobalAllocationFunctions(OO_Array_Delete, Context.VoidTy, VoidPtr); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 2611 | /// allocation function if it doesn't already exist. |
| 2612 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2613 | QualType Return, |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2614 | ArrayRef<QualType> Params) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2615 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 2616 | |
| 2617 | // Check if this function is already declared. |
Serge Pavlov | d548907 | 2013-09-14 12:00:01 +0000 | [diff] [blame] | 2618 | DeclContext::lookup_result R = GlobalCtx->lookup(Name); |
| 2619 | for (DeclContext::lookup_iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 2620 | Alloc != AllocEnd; ++Alloc) { |
| 2621 | // Only look at non-template functions, as it is the predefined, |
| 2622 | // non-templated allocation function we are trying to declare here. |
| 2623 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2624 | if (Func->getNumParams() == Params.size()) { |
| 2625 | llvm::SmallVector<QualType, 3> FuncParams; |
| 2626 | for (auto *P : Func->parameters()) |
| 2627 | FuncParams.push_back( |
| 2628 | Context.getCanonicalType(P->getType().getUnqualifiedType())); |
| 2629 | if (llvm::makeArrayRef(FuncParams) == Params) { |
Serge Pavlov | d548907 | 2013-09-14 12:00:01 +0000 | [diff] [blame] | 2630 | // Make the function visible to name lookup, even if we found it in |
| 2631 | // an unimported module. It either is an implicitly-declared global |
Richard Smith | 42713d7 | 2013-07-14 02:01:48 +0000 | [diff] [blame] | 2632 | // allocation function, or is suppressing that function. |
| 2633 | Func->setHidden(false); |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 2634 | return; |
Douglas Gregor | c1a42fd | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 2635 | } |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 2636 | } |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2637 | } |
| 2638 | } |
Reid Kleckner | 7270ef5 | 2015-03-19 17:03:58 +0000 | [diff] [blame] | 2639 | |
Richard Smith | c015bc2 | 2014-02-07 22:39:53 +0000 | [diff] [blame] | 2640 | FunctionProtoType::ExtProtoInfo EPI; |
| 2641 | |
Richard Smith | f8b417c | 2014-02-08 00:42:45 +0000 | [diff] [blame] | 2642 | QualType BadAllocType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2643 | bool HasBadAllocExceptionSpec |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2644 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 2645 | Name.getCXXOverloadedOperator() == OO_Array_New); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2646 | if (HasBadAllocExceptionSpec) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2647 | if (!getLangOpts().CPlusPlus11) { |
Richard Smith | f8b417c | 2014-02-08 00:42:45 +0000 | [diff] [blame] | 2648 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Richard Smith | c015bc2 | 2014-02-07 22:39:53 +0000 | [diff] [blame] | 2649 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2650 | EPI.ExceptionSpec.Type = EST_Dynamic; |
| 2651 | EPI.ExceptionSpec.Exceptions = llvm::makeArrayRef(BadAllocType); |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2652 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2653 | } else { |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2654 | EPI.ExceptionSpec = |
| 2655 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2656 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2657 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2658 | auto CreateAllocationFunctionDecl = [&](Attr *ExtraAttr) { |
| 2659 | QualType FnType = Context.getFunctionType(Return, Params, EPI); |
| 2660 | FunctionDecl *Alloc = FunctionDecl::Create( |
| 2661 | Context, GlobalCtx, SourceLocation(), SourceLocation(), Name, |
| 2662 | FnType, /*TInfo=*/nullptr, SC_None, false, true); |
| 2663 | Alloc->setImplicit(); |
Vassil Vassilev | 41cafcd | 2017-06-09 21:36:28 +0000 | [diff] [blame] | 2664 | // Global allocation functions should always be visible. |
| 2665 | Alloc->setHidden(false); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 2666 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2667 | // Implicit sized deallocation functions always have default visibility. |
| 2668 | Alloc->addAttr( |
| 2669 | VisibilityAttr::CreateImplicit(Context, VisibilityAttr::Default)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2670 | |
Artem Belevich | 07db5cf | 2016-10-21 20:34:05 +0000 | [diff] [blame] | 2671 | llvm::SmallVector<ParmVarDecl *, 3> ParamDecls; |
| 2672 | for (QualType T : Params) { |
| 2673 | ParamDecls.push_back(ParmVarDecl::Create( |
| 2674 | Context, Alloc, SourceLocation(), SourceLocation(), nullptr, T, |
| 2675 | /*TInfo=*/nullptr, SC_None, nullptr)); |
| 2676 | ParamDecls.back()->setImplicit(); |
| 2677 | } |
| 2678 | Alloc->setParams(ParamDecls); |
| 2679 | if (ExtraAttr) |
| 2680 | Alloc->addAttr(ExtraAttr); |
| 2681 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
| 2682 | IdResolver.tryAddTopLevelDecl(Alloc, Name); |
| 2683 | }; |
| 2684 | |
| 2685 | if (!LangOpts.CUDA) |
| 2686 | CreateAllocationFunctionDecl(nullptr); |
| 2687 | else { |
| 2688 | // Host and device get their own declaration so each can be |
| 2689 | // defined or re-declared independently. |
| 2690 | CreateAllocationFunctionDecl(CUDAHostAttr::CreateImplicit(Context)); |
| 2691 | CreateAllocationFunctionDecl(CUDADeviceAttr::CreateImplicit(Context)); |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 2692 | } |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2695 | FunctionDecl *Sema::FindUsualDeallocationFunction(SourceLocation StartLoc, |
| 2696 | bool CanProvideSize, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2697 | bool Overaligned, |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2698 | DeclarationName Name) { |
| 2699 | DeclareGlobalNewDelete(); |
| 2700 | |
| 2701 | LookupResult FoundDelete(*this, Name, StartLoc, LookupOrdinaryName); |
| 2702 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 2703 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2704 | // FIXME: It's possible for this to result in ambiguity, through a |
| 2705 | // user-declared variadic operator delete or the enable_if attribute. We |
| 2706 | // should probably not consider those cases to be usual deallocation |
| 2707 | // functions. But for now we just make an arbitrary choice in that case. |
| 2708 | auto Result = resolveDeallocationOverload(*this, FoundDelete, CanProvideSize, |
| 2709 | Overaligned); |
| 2710 | assert(Result.FD && "operator delete missing from global scope?"); |
| 2711 | return Result.FD; |
| 2712 | } |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2713 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2714 | FunctionDecl *Sema::FindDeallocationFunctionForDestructor(SourceLocation Loc, |
| 2715 | CXXRecordDecl *RD) { |
| 2716 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2717 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2718 | FunctionDecl *OperatorDelete = nullptr; |
| 2719 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
| 2720 | return nullptr; |
| 2721 | if (OperatorDelete) |
| 2722 | return OperatorDelete; |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 2723 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2724 | // If there's no class-specific operator delete, look up the global |
| 2725 | // non-array delete. |
| 2726 | return FindUsualDeallocationFunction( |
| 2727 | Loc, true, hasNewExtendedAlignment(*this, Context.getRecordType(RD)), |
| 2728 | Name); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2731 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 2732 | DeclarationName Name, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2733 | FunctionDecl *&Operator, bool Diagnose) { |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2734 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2735 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2736 | LookupQualifiedName(Found, RD); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2737 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2738 | if (Found.isAmbiguous()) |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2739 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2740 | |
Chandler Carruth | b6f9917 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 2741 | Found.suppressDiagnostics(); |
| 2742 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2743 | bool Overaligned = hasNewExtendedAlignment(*this, Context.getRecordType(RD)); |
Chandler Carruth | 9b41823 | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 2744 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2745 | // C++17 [expr.delete]p10: |
| 2746 | // If the deallocation functions have class scope, the one without a |
| 2747 | // parameter of type std::size_t is selected. |
| 2748 | llvm::SmallVector<UsualDeallocFnInfo, 4> Matches; |
| 2749 | resolveDeallocationOverload(*this, Found, /*WantSize*/ false, |
| 2750 | /*WantAlign*/ Overaligned, &Matches); |
Chandler Carruth | 9b41823 | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 2751 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2752 | // If we could find an overload, use it. |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2753 | if (Matches.size() == 1) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2754 | Operator = cast<CXXMethodDecl>(Matches[0].FD); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2755 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2756 | // FIXME: DiagnoseUseOfDecl? |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2757 | if (Operator->isDeleted()) { |
| 2758 | if (Diagnose) { |
| 2759 | Diag(StartLoc, diag::err_deleted_function_use); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 2760 | NoteDeletedFunction(Operator); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2761 | } |
| 2762 | return true; |
| 2763 | } |
| 2764 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 2765 | if (CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2766 | Matches[0].Found, Diagnose) == AR_inaccessible) |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 2767 | return true; |
| 2768 | |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2769 | return false; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2770 | } |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2771 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2772 | // We found multiple suitable operators; complain about the ambiguity. |
| 2773 | // FIXME: The standard doesn't say to do this; it appears that the intent |
| 2774 | // is that this should never happen. |
| 2775 | if (!Matches.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2776 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2777 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 2778 | << Name << RD; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2779 | for (auto &Match : Matches) |
| 2780 | Diag(Match.FD->getLocation(), diag::note_member_declared_here) << Name; |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2781 | } |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 2782 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | // We did find operator delete/operator delete[] declarations, but |
| 2786 | // none of them were suitable. |
| 2787 | if (!Found.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 2788 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2789 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 2790 | << Name << RD; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2791 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2792 | for (NamedDecl *D : Found) |
| 2793 | Diag(D->getUnderlyingDecl()->getLocation(), |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 2794 | diag::note_member_declared_here) << Name; |
| 2795 | } |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2796 | return true; |
| 2797 | } |
| 2798 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2799 | Operator = nullptr; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2800 | return false; |
| 2801 | } |
| 2802 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2803 | namespace { |
| 2804 | /// \brief Checks whether delete-expression, and new-expression used for |
| 2805 | /// initializing deletee have the same array form. |
| 2806 | class MismatchingNewDeleteDetector { |
| 2807 | public: |
| 2808 | enum MismatchResult { |
| 2809 | /// Indicates that there is no mismatch or a mismatch cannot be proven. |
| 2810 | NoMismatch, |
| 2811 | /// Indicates that variable is initialized with mismatching form of \a new. |
| 2812 | VarInitMismatches, |
| 2813 | /// Indicates that member is initialized with mismatching form of \a new. |
| 2814 | MemberInitMismatches, |
| 2815 | /// Indicates that 1 or more constructors' definitions could not been |
| 2816 | /// analyzed, and they will be checked again at the end of translation unit. |
| 2817 | AnalyzeLater |
| 2818 | }; |
| 2819 | |
| 2820 | /// \param EndOfTU True, if this is the final analysis at the end of |
| 2821 | /// translation unit. False, if this is the initial analysis at the point |
| 2822 | /// delete-expression was encountered. |
| 2823 | explicit MismatchingNewDeleteDetector(bool EndOfTU) |
Alexander Shaposhnikov | 3087b2c | 2016-09-01 23:18:00 +0000 | [diff] [blame] | 2824 | : Field(nullptr), IsArrayForm(false), EndOfTU(EndOfTU), |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2825 | HasUndefinedConstructors(false) {} |
| 2826 | |
| 2827 | /// \brief Checks whether pointee of a delete-expression is initialized with |
| 2828 | /// matching form of new-expression. |
| 2829 | /// |
| 2830 | /// If return value is \c VarInitMismatches or \c MemberInitMismatches at the |
| 2831 | /// point where delete-expression is encountered, then a warning will be |
| 2832 | /// issued immediately. If return value is \c AnalyzeLater at the point where |
| 2833 | /// delete-expression is seen, then member will be analyzed at the end of |
| 2834 | /// translation unit. \c AnalyzeLater is returned iff at least one constructor |
| 2835 | /// couldn't be analyzed. If at least one constructor initializes the member |
| 2836 | /// with matching type of new, the return value is \c NoMismatch. |
| 2837 | MismatchResult analyzeDeleteExpr(const CXXDeleteExpr *DE); |
| 2838 | /// \brief Analyzes a class member. |
| 2839 | /// \param Field Class member to analyze. |
| 2840 | /// \param DeleteWasArrayForm Array form-ness of the delete-expression used |
| 2841 | /// for deleting the \p Field. |
| 2842 | MismatchResult analyzeField(FieldDecl *Field, bool DeleteWasArrayForm); |
Alexander Shaposhnikov | 3087b2c | 2016-09-01 23:18:00 +0000 | [diff] [blame] | 2843 | FieldDecl *Field; |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2844 | /// List of mismatching new-expressions used for initialization of the pointee |
| 2845 | llvm::SmallVector<const CXXNewExpr *, 4> NewExprs; |
| 2846 | /// Indicates whether delete-expression was in array form. |
| 2847 | bool IsArrayForm; |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2848 | |
| 2849 | private: |
| 2850 | const bool EndOfTU; |
| 2851 | /// \brief Indicates that there is at least one constructor without body. |
| 2852 | bool HasUndefinedConstructors; |
| 2853 | /// \brief Returns \c CXXNewExpr from given initialization expression. |
| 2854 | /// \param E Expression used for initializing pointee in delete-expression. |
NAKAMURA Takumi | 4bd67d8 | 2015-05-19 06:47:23 +0000 | [diff] [blame] | 2855 | /// E can be a single-element \c InitListExpr consisting of new-expression. |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2856 | const CXXNewExpr *getNewExprFromInitListOrExpr(const Expr *E); |
| 2857 | /// \brief Returns whether member is initialized with mismatching form of |
| 2858 | /// \c new either by the member initializer or in-class initialization. |
| 2859 | /// |
| 2860 | /// If bodies of all constructors are not visible at the end of translation |
| 2861 | /// unit or at least one constructor initializes member with the matching |
| 2862 | /// form of \c new, mismatch cannot be proven, and this function will return |
| 2863 | /// \c NoMismatch. |
| 2864 | MismatchResult analyzeMemberExpr(const MemberExpr *ME); |
| 2865 | /// \brief Returns whether variable is initialized with mismatching form of |
| 2866 | /// \c new. |
| 2867 | /// |
| 2868 | /// If variable is initialized with matching form of \c new or variable is not |
| 2869 | /// initialized with a \c new expression, this function will return true. |
| 2870 | /// If variable is initialized with mismatching form of \c new, returns false. |
| 2871 | /// \param D Variable to analyze. |
| 2872 | bool hasMatchingVarInit(const DeclRefExpr *D); |
| 2873 | /// \brief Checks whether the constructor initializes pointee with mismatching |
| 2874 | /// form of \c new. |
| 2875 | /// |
| 2876 | /// Returns true, if member is initialized with matching form of \c new in |
| 2877 | /// member initializer list. Returns false, if member is initialized with the |
| 2878 | /// matching form of \c new in this constructor's initializer or given |
| 2879 | /// constructor isn't defined at the point where delete-expression is seen, or |
| 2880 | /// member isn't initialized by the constructor. |
| 2881 | bool hasMatchingNewInCtor(const CXXConstructorDecl *CD); |
| 2882 | /// \brief Checks whether member is initialized with matching form of |
| 2883 | /// \c new in member initializer list. |
| 2884 | bool hasMatchingNewInCtorInit(const CXXCtorInitializer *CI); |
| 2885 | /// Checks whether member is initialized with mismatching form of \c new by |
| 2886 | /// in-class initializer. |
| 2887 | MismatchResult analyzeInClassInitializer(); |
| 2888 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2889 | } |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2890 | |
| 2891 | MismatchingNewDeleteDetector::MismatchResult |
| 2892 | MismatchingNewDeleteDetector::analyzeDeleteExpr(const CXXDeleteExpr *DE) { |
| 2893 | NewExprs.clear(); |
| 2894 | assert(DE && "Expected delete-expression"); |
| 2895 | IsArrayForm = DE->isArrayForm(); |
| 2896 | const Expr *E = DE->getArgument()->IgnoreParenImpCasts(); |
| 2897 | if (const MemberExpr *ME = dyn_cast<const MemberExpr>(E)) { |
| 2898 | return analyzeMemberExpr(ME); |
| 2899 | } else if (const DeclRefExpr *D = dyn_cast<const DeclRefExpr>(E)) { |
| 2900 | if (!hasMatchingVarInit(D)) |
| 2901 | return VarInitMismatches; |
| 2902 | } |
| 2903 | return NoMismatch; |
| 2904 | } |
| 2905 | |
| 2906 | const CXXNewExpr * |
| 2907 | MismatchingNewDeleteDetector::getNewExprFromInitListOrExpr(const Expr *E) { |
| 2908 | assert(E != nullptr && "Expected a valid initializer expression"); |
| 2909 | E = E->IgnoreParenImpCasts(); |
| 2910 | if (const InitListExpr *ILE = dyn_cast<const InitListExpr>(E)) { |
| 2911 | if (ILE->getNumInits() == 1) |
| 2912 | E = dyn_cast<const CXXNewExpr>(ILE->getInit(0)->IgnoreParenImpCasts()); |
| 2913 | } |
| 2914 | |
| 2915 | return dyn_cast_or_null<const CXXNewExpr>(E); |
| 2916 | } |
| 2917 | |
| 2918 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtorInit( |
| 2919 | const CXXCtorInitializer *CI) { |
| 2920 | const CXXNewExpr *NE = nullptr; |
| 2921 | if (Field == CI->getMember() && |
| 2922 | (NE = getNewExprFromInitListOrExpr(CI->getInit()))) { |
| 2923 | if (NE->isArray() == IsArrayForm) |
| 2924 | return true; |
| 2925 | else |
| 2926 | NewExprs.push_back(NE); |
| 2927 | } |
| 2928 | return false; |
| 2929 | } |
| 2930 | |
| 2931 | bool MismatchingNewDeleteDetector::hasMatchingNewInCtor( |
| 2932 | const CXXConstructorDecl *CD) { |
| 2933 | if (CD->isImplicit()) |
| 2934 | return false; |
| 2935 | const FunctionDecl *Definition = CD; |
| 2936 | if (!CD->isThisDeclarationADefinition() && !CD->isDefined(Definition)) { |
| 2937 | HasUndefinedConstructors = true; |
| 2938 | return EndOfTU; |
| 2939 | } |
| 2940 | for (const auto *CI : cast<const CXXConstructorDecl>(Definition)->inits()) { |
| 2941 | if (hasMatchingNewInCtorInit(CI)) |
| 2942 | return true; |
| 2943 | } |
| 2944 | return false; |
| 2945 | } |
| 2946 | |
| 2947 | MismatchingNewDeleteDetector::MismatchResult |
| 2948 | MismatchingNewDeleteDetector::analyzeInClassInitializer() { |
| 2949 | assert(Field != nullptr && "This should be called only for members"); |
Ismail Pazarbasi | 7ff1836 | 2015-10-26 19:20:24 +0000 | [diff] [blame] | 2950 | const Expr *InitExpr = Field->getInClassInitializer(); |
| 2951 | if (!InitExpr) |
| 2952 | return EndOfTU ? NoMismatch : AnalyzeLater; |
| 2953 | if (const CXXNewExpr *NE = getNewExprFromInitListOrExpr(InitExpr)) { |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 2954 | if (NE->isArray() != IsArrayForm) { |
| 2955 | NewExprs.push_back(NE); |
| 2956 | return MemberInitMismatches; |
| 2957 | } |
| 2958 | } |
| 2959 | return NoMismatch; |
| 2960 | } |
| 2961 | |
| 2962 | MismatchingNewDeleteDetector::MismatchResult |
| 2963 | MismatchingNewDeleteDetector::analyzeField(FieldDecl *Field, |
| 2964 | bool DeleteWasArrayForm) { |
| 2965 | assert(Field != nullptr && "Analysis requires a valid class member."); |
| 2966 | this->Field = Field; |
| 2967 | IsArrayForm = DeleteWasArrayForm; |
| 2968 | const CXXRecordDecl *RD = cast<const CXXRecordDecl>(Field->getParent()); |
| 2969 | for (const auto *CD : RD->ctors()) { |
| 2970 | if (hasMatchingNewInCtor(CD)) |
| 2971 | return NoMismatch; |
| 2972 | } |
| 2973 | if (HasUndefinedConstructors) |
| 2974 | return EndOfTU ? NoMismatch : AnalyzeLater; |
| 2975 | if (!NewExprs.empty()) |
| 2976 | return MemberInitMismatches; |
| 2977 | return Field->hasInClassInitializer() ? analyzeInClassInitializer() |
| 2978 | : NoMismatch; |
| 2979 | } |
| 2980 | |
| 2981 | MismatchingNewDeleteDetector::MismatchResult |
| 2982 | MismatchingNewDeleteDetector::analyzeMemberExpr(const MemberExpr *ME) { |
| 2983 | assert(ME != nullptr && "Expected a member expression"); |
| 2984 | if (FieldDecl *F = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2985 | return analyzeField(F, IsArrayForm); |
| 2986 | return NoMismatch; |
| 2987 | } |
| 2988 | |
| 2989 | bool MismatchingNewDeleteDetector::hasMatchingVarInit(const DeclRefExpr *D) { |
| 2990 | const CXXNewExpr *NE = nullptr; |
| 2991 | if (const VarDecl *VD = dyn_cast<const VarDecl>(D->getDecl())) { |
| 2992 | if (VD->hasInit() && (NE = getNewExprFromInitListOrExpr(VD->getInit())) && |
| 2993 | NE->isArray() != IsArrayForm) { |
| 2994 | NewExprs.push_back(NE); |
| 2995 | } |
| 2996 | } |
| 2997 | return NewExprs.empty(); |
| 2998 | } |
| 2999 | |
| 3000 | static void |
| 3001 | DiagnoseMismatchedNewDelete(Sema &SemaRef, SourceLocation DeleteLoc, |
| 3002 | const MismatchingNewDeleteDetector &Detector) { |
| 3003 | SourceLocation EndOfDelete = SemaRef.getLocForEndOfToken(DeleteLoc); |
| 3004 | FixItHint H; |
| 3005 | if (!Detector.IsArrayForm) |
| 3006 | H = FixItHint::CreateInsertion(EndOfDelete, "[]"); |
| 3007 | else { |
| 3008 | SourceLocation RSquare = Lexer::findLocationAfterToken( |
| 3009 | DeleteLoc, tok::l_square, SemaRef.getSourceManager(), |
| 3010 | SemaRef.getLangOpts(), true); |
| 3011 | if (RSquare.isValid()) |
| 3012 | H = FixItHint::CreateRemoval(SourceRange(EndOfDelete, RSquare)); |
| 3013 | } |
| 3014 | SemaRef.Diag(DeleteLoc, diag::warn_mismatched_delete_new) |
| 3015 | << Detector.IsArrayForm << H; |
| 3016 | |
| 3017 | for (const auto *NE : Detector.NewExprs) |
| 3018 | SemaRef.Diag(NE->getExprLoc(), diag::note_allocated_here) |
| 3019 | << Detector.IsArrayForm; |
| 3020 | } |
| 3021 | |
| 3022 | void Sema::AnalyzeDeleteExprMismatch(const CXXDeleteExpr *DE) { |
| 3023 | if (Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) |
| 3024 | return; |
| 3025 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/false); |
| 3026 | switch (Detector.analyzeDeleteExpr(DE)) { |
| 3027 | case MismatchingNewDeleteDetector::VarInitMismatches: |
| 3028 | case MismatchingNewDeleteDetector::MemberInitMismatches: { |
| 3029 | DiagnoseMismatchedNewDelete(*this, DE->getLocStart(), Detector); |
| 3030 | break; |
| 3031 | } |
| 3032 | case MismatchingNewDeleteDetector::AnalyzeLater: { |
| 3033 | DeleteExprs[Detector.Field].push_back( |
| 3034 | std::make_pair(DE->getLocStart(), DE->isArrayForm())); |
| 3035 | break; |
| 3036 | } |
| 3037 | case MismatchingNewDeleteDetector::NoMismatch: |
| 3038 | break; |
| 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | void Sema::AnalyzeDeleteExprMismatch(FieldDecl *Field, SourceLocation DeleteLoc, |
| 3043 | bool DeleteWasArrayForm) { |
| 3044 | MismatchingNewDeleteDetector Detector(/*EndOfTU=*/true); |
| 3045 | switch (Detector.analyzeField(Field, DeleteWasArrayForm)) { |
| 3046 | case MismatchingNewDeleteDetector::VarInitMismatches: |
| 3047 | llvm_unreachable("This analysis should have been done for class members."); |
| 3048 | case MismatchingNewDeleteDetector::AnalyzeLater: |
| 3049 | llvm_unreachable("Analysis cannot be postponed any point beyond end of " |
| 3050 | "translation unit."); |
| 3051 | case MismatchingNewDeleteDetector::MemberInitMismatches: |
| 3052 | DiagnoseMismatchedNewDelete(*this, DeleteLoc, Detector); |
| 3053 | break; |
| 3054 | case MismatchingNewDeleteDetector::NoMismatch: |
| 3055 | break; |
| 3056 | } |
| 3057 | } |
| 3058 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3059 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 3060 | /// @code ::delete ptr; @endcode |
| 3061 | /// or |
| 3062 | /// @code delete [] ptr; @endcode |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3063 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3064 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3065 | bool ArrayForm, Expr *ExE) { |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3066 | // C++ [expr.delete]p1: |
| 3067 | // 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] | 3068 | // non-explicit conversion function to a pointer type. The result has type |
| 3069 | // void. |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3070 | // |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3071 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 3072 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3073 | ExprResult Ex = ExE; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3074 | FunctionDecl *OperatorDelete = nullptr; |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3075 | bool ArrayFormAsWritten = ArrayForm; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3076 | bool UsualArrayDeleteWantsSize = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3078 | if (!Ex.get()->isTypeDependent()) { |
John McCall | ef42902 | 2012-03-09 04:08:29 +0000 | [diff] [blame] | 3079 | // Perform lvalue-to-rvalue cast, if needed. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3080 | Ex = DefaultLvalueConversion(Ex.get()); |
Eli Friedman | 89a4a2c | 2012-12-13 00:37:17 +0000 | [diff] [blame] | 3081 | if (Ex.isInvalid()) |
| 3082 | return ExprError(); |
John McCall | ef42902 | 2012-03-09 04:08:29 +0000 | [diff] [blame] | 3083 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3084 | QualType Type = Ex.get()->getType(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3085 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3086 | class DeleteConverter : public ContextualImplicitConverter { |
| 3087 | public: |
| 3088 | DeleteConverter() : ContextualImplicitConverter(false, true) {} |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3089 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3090 | bool match(QualType ConvType) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3091 | // FIXME: If we have an operator T* and an operator void*, we must pick |
| 3092 | // the operator T*. |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3093 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3094 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3095 | return true; |
| 3096 | return false; |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 3097 | } |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3098 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3099 | SemaDiagnosticBuilder diagnoseNoMatch(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3100 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3101 | return S.Diag(Loc, diag::err_delete_operand) << T; |
| 3102 | } |
| 3103 | |
| 3104 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3105 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3106 | return S.Diag(Loc, diag::err_delete_incomplete_class_type) << T; |
| 3107 | } |
| 3108 | |
| 3109 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3110 | QualType T, |
| 3111 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3112 | return S.Diag(Loc, diag::err_delete_explicit_conversion) << T << ConvTy; |
| 3113 | } |
| 3114 | |
| 3115 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3116 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3117 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
| 3118 | << ConvTy; |
| 3119 | } |
| 3120 | |
| 3121 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3122 | QualType T) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3123 | return S.Diag(Loc, diag::err_ambiguous_delete_operand) << T; |
| 3124 | } |
| 3125 | |
| 3126 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3127 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3128 | return S.Diag(Conv->getLocation(), diag::note_delete_conversion) |
| 3129 | << ConvTy; |
| 3130 | } |
| 3131 | |
| 3132 | SemaDiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3133 | QualType T, |
| 3134 | QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3135 | llvm_unreachable("conversion functions are permitted"); |
| 3136 | } |
| 3137 | } Converter; |
| 3138 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3139 | Ex = PerformContextualImplicitConversion(StartLoc, Ex.get(), Converter); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 3140 | if (Ex.isInvalid()) |
| 3141 | return ExprError(); |
| 3142 | Type = Ex.get()->getType(); |
| 3143 | if (!Converter.match(Type)) |
| 3144 | // FIXME: PerformContextualImplicitConversion should return ExprError |
| 3145 | // itself in this case. |
| 3146 | return ExprError(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3147 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3148 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3149 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 3150 | |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 3151 | if (Pointee.getAddressSpace()) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3152 | return Diag(Ex.get()->getLocStart(), |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3153 | diag::err_address_space_qualified_delete) |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 3154 | << Pointee.getUnqualifiedType() |
| 3155 | << Pointee.getQualifiers().getAddressSpaceAttributePrintValue(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3156 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3157 | CXXRecordDecl *PointeeRD = nullptr; |
Douglas Gregor | bb3348e | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 3158 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3159 | // 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] | 3160 | // effectively bans deletion of "void*". However, most compilers support |
| 3161 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 3162 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3163 | << Type << Ex.get()->getSourceRange(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3164 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3165 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3166 | << Type << Ex.get()->getSourceRange()); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3167 | } else if (!Pointee->isDependentType()) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3168 | // FIXME: This can result in errors if the definition was imported from a |
| 3169 | // module but is hidden. |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3170 | if (!RequireCompleteType(StartLoc, Pointee, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3171 | diag::warn_delete_incomplete, Ex.get())) { |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3172 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
| 3173 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3174 | } |
| 3175 | } |
| 3176 | |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3177 | if (Pointee->isArrayType() && !ArrayForm) { |
| 3178 | Diag(StartLoc, diag::warn_delete_array_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3179 | << Type << Ex.get()->getSourceRange() |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 3180 | << FixItHint::CreateInsertion(getLocForEndOfToken(StartLoc), "[]"); |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 3181 | ArrayForm = true; |
| 3182 | } |
| 3183 | |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 3184 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 3185 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 3186 | |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3187 | if (PointeeRD) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3188 | if (!UseGlobal && |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3189 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, |
| 3190 | OperatorDelete)) |
Anders Carlsson | 654e5c7 | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 3191 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3192 | |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3193 | // If we're allocating an array of records, check whether the |
| 3194 | // usual operator delete[] has a size_t parameter. |
| 3195 | if (ArrayForm) { |
| 3196 | // If the user specifically asked to use the global allocator, |
| 3197 | // we'll need to do the lookup into the class. |
| 3198 | if (UseGlobal) |
| 3199 | UsualArrayDeleteWantsSize = |
| 3200 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); |
| 3201 | |
| 3202 | // Otherwise, the usual operator delete[] should be the |
| 3203 | // function we just found. |
Richard Smith | f03bd30 | 2013-12-05 08:30:59 +0000 | [diff] [blame] | 3204 | else if (OperatorDelete && isa<CXXMethodDecl>(OperatorDelete)) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3205 | UsualArrayDeleteWantsSize = |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 3206 | UsualDeallocFnInfo(*this, |
| 3207 | DeclAccessPair::make(OperatorDelete, AS_public)) |
| 3208 | .HasSizeT; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3211 | if (!PointeeRD->hasIrrelevantDestructor()) |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3212 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3213 | MarkFunctionReferenced(StartLoc, |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3214 | const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 3215 | if (DiagnoseUseOfDecl(Dtor, StartLoc)) |
| 3216 | return ExprError(); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3217 | } |
Argyrios Kyrtzidis | 8bd4285 | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 3218 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3219 | CheckVirtualDtorCall(PointeeRD->getDestructor(), StartLoc, |
| 3220 | /*IsDelete=*/true, /*CallCanBeVirtual=*/true, |
| 3221 | /*WarnOnNonAbstractTypes=*/!ArrayForm, |
| 3222 | SourceLocation()); |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 3223 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3224 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3225 | if (!OperatorDelete) { |
| 3226 | bool IsComplete = isCompleteType(StartLoc, Pointee); |
| 3227 | bool CanProvideSize = |
| 3228 | IsComplete && (!ArrayForm || UsualArrayDeleteWantsSize || |
| 3229 | Pointee.isDestructedType()); |
| 3230 | bool Overaligned = hasNewExtendedAlignment(*this, Pointee); |
| 3231 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 3232 | // Look for a global declaration. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3233 | OperatorDelete = FindUsualDeallocationFunction(StartLoc, CanProvideSize, |
| 3234 | Overaligned, DeleteName); |
| 3235 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3236 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3237 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3238 | |
Douglas Gregor | fa77813 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 3239 | // Check access and ambiguity of operator delete and destructor. |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 3240 | if (PointeeRD) { |
| 3241 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3242 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
Douglas Gregor | fa77813 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 3243 | PDiag(diag::err_access_dtor) << PointeeElem); |
| 3244 | } |
| 3245 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3248 | CXXDeleteExpr *Result = new (Context) CXXDeleteExpr( |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3249 | Context.VoidTy, UseGlobal, ArrayForm, ArrayFormAsWritten, |
| 3250 | UsualArrayDeleteWantsSize, OperatorDelete, Ex.get(), StartLoc); |
Ismail Pazarbasi | e5768d1 | 2015-05-18 19:59:11 +0000 | [diff] [blame] | 3251 | AnalyzeDeleteExprMismatch(Result); |
| 3252 | return Result; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 3255 | void Sema::CheckVirtualDtorCall(CXXDestructorDecl *dtor, SourceLocation Loc, |
| 3256 | bool IsDelete, bool CallCanBeVirtual, |
| 3257 | bool WarnOnNonAbstractTypes, |
| 3258 | SourceLocation DtorLoc) { |
| 3259 | if (!dtor || dtor->isVirtual() || !CallCanBeVirtual) |
| 3260 | return; |
| 3261 | |
| 3262 | // C++ [expr.delete]p3: |
| 3263 | // In the first alternative (delete object), if the static type of the |
| 3264 | // object to be deleted is different from its dynamic type, the static |
| 3265 | // type shall be a base class of the dynamic type of the object to be |
| 3266 | // deleted and the static type shall have a virtual destructor or the |
| 3267 | // behavior is undefined. |
| 3268 | // |
| 3269 | const CXXRecordDecl *PointeeRD = dtor->getParent(); |
| 3270 | // Note: a final class cannot be derived from, no issue there |
| 3271 | if (!PointeeRD->isPolymorphic() || PointeeRD->hasAttr<FinalAttr>()) |
| 3272 | return; |
| 3273 | |
| 3274 | QualType ClassType = dtor->getThisType(Context)->getPointeeType(); |
| 3275 | if (PointeeRD->isAbstract()) { |
| 3276 | // If the class is abstract, we warn by default, because we're |
| 3277 | // sure the code has undefined behavior. |
| 3278 | Diag(Loc, diag::warn_delete_abstract_non_virtual_dtor) << (IsDelete ? 0 : 1) |
| 3279 | << ClassType; |
| 3280 | } else if (WarnOnNonAbstractTypes) { |
| 3281 | // Otherwise, if this is not an array delete, it's a bit suspect, |
| 3282 | // but not necessarily wrong. |
| 3283 | Diag(Loc, diag::warn_delete_non_virtual_dtor) << (IsDelete ? 0 : 1) |
| 3284 | << ClassType; |
| 3285 | } |
| 3286 | if (!IsDelete) { |
| 3287 | std::string TypeStr; |
| 3288 | ClassType.getAsStringInternal(TypeStr, getPrintingPolicy()); |
| 3289 | Diag(DtorLoc, diag::note_delete_non_virtual) |
| 3290 | << FixItHint::CreateInsertion(DtorLoc, TypeStr + "::"); |
| 3291 | } |
| 3292 | } |
| 3293 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3294 | Sema::ConditionResult Sema::ActOnConditionVariable(Decl *ConditionVar, |
| 3295 | SourceLocation StmtLoc, |
| 3296 | ConditionKind CK) { |
| 3297 | ExprResult E = |
| 3298 | CheckConditionVariable(cast<VarDecl>(ConditionVar), StmtLoc, CK); |
| 3299 | if (E.isInvalid()) |
| 3300 | return ConditionError(); |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3301 | return ConditionResult(*this, ConditionVar, MakeFullExpr(E.get(), StmtLoc), |
| 3302 | CK == ConditionKind::ConstexprIf); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3305 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 3306 | /// while, do-while, or switch statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3307 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3308 | SourceLocation StmtLoc, |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3309 | ConditionKind CK) { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3310 | if (ConditionVar->isInvalidDecl()) |
| 3311 | return ExprError(); |
| 3312 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3313 | QualType T = ConditionVar->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3314 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3315 | // C++ [stmt.select]p2: |
| 3316 | // The declarator shall not specify a function or an array. |
| 3317 | if (T->isFunctionType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3318 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3319 | diag::err_invalid_use_of_function_type) |
| 3320 | << ConditionVar->getSourceRange()); |
| 3321 | else if (T->isArrayType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3322 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3323 | diag::err_invalid_use_of_array_type) |
| 3324 | << ConditionVar->getSourceRange()); |
Douglas Gregor | 0156d1c | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 3325 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3326 | ExprResult Condition = DeclRefExpr::Create( |
| 3327 | Context, NestedNameSpecifierLoc(), SourceLocation(), ConditionVar, |
| 3328 | /*enclosing*/ false, ConditionVar->getLocation(), |
| 3329 | ConditionVar->getType().getNonReferenceType(), VK_LValue); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3330 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3331 | MarkDeclRefReferenced(cast<DeclRefExpr>(Condition.get())); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3332 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3333 | switch (CK) { |
| 3334 | case ConditionKind::Boolean: |
| 3335 | return CheckBooleanCondition(StmtLoc, Condition.get()); |
| 3336 | |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3337 | case ConditionKind::ConstexprIf: |
| 3338 | return CheckBooleanCondition(StmtLoc, Condition.get(), true); |
| 3339 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3340 | case ConditionKind::Switch: |
| 3341 | return CheckSwitchCondition(StmtLoc, Condition.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3342 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3343 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 3344 | llvm_unreachable("unexpected condition kind"); |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3347 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3348 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr, bool IsConstexpr) { |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3349 | // C++ 6.4p4: |
| 3350 | // The value of a condition that is an initialized declaration in a statement |
| 3351 | // other than a switch statement is the value of the declared variable |
| 3352 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 3353 | // program is ill-formed. |
| 3354 | // The value of a condition that is an expression is the value of the |
| 3355 | // expression, implicitly converted to bool. |
| 3356 | // |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3357 | // FIXME: Return this value to the caller so they don't need to recompute it. |
| 3358 | llvm::APSInt Value(/*BitWidth*/1); |
| 3359 | return (IsConstexpr && !CondExpr->isValueDependent()) |
| 3360 | ? CheckConvertedConstantExpression(CondExpr, Context.BoolTy, Value, |
| 3361 | CCEK_ConstexprIf) |
| 3362 | : PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 3363 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3364 | |
| 3365 | /// Helper function to determine whether this is the (deprecated) C++ |
| 3366 | /// conversion from a string literal to a pointer to non-const char or |
| 3367 | /// non-const wchar_t (for narrow and wide string literals, |
| 3368 | /// respectively). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3369 | bool |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3370 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 3371 | // Look inside the implicit cast, if it exists. |
| 3372 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 3373 | From = Cast->getSubExpr(); |
| 3374 | |
| 3375 | // A string literal (2.13.4) that is not a wide string literal can |
| 3376 | // be converted to an rvalue of type "pointer to char"; a wide |
| 3377 | // string literal can be converted to an rvalue of type "pointer |
| 3378 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 689999d | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 3379 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3380 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | if (const BuiltinType *ToPointeeType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3382 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3383 | // This conversion is considered only when there is an |
| 3384 | // explicit appropriate pointer target type (C++ 4.2p2). |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3385 | if (!ToPtrType->getPointeeType().hasQualifiers()) { |
| 3386 | switch (StrLit->getKind()) { |
| 3387 | case StringLiteral::UTF8: |
| 3388 | case StringLiteral::UTF16: |
| 3389 | case StringLiteral::UTF32: |
| 3390 | // We don't allow UTF literals to be implicitly converted |
| 3391 | break; |
| 3392 | case StringLiteral::Ascii: |
| 3393 | return (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 3394 | ToPointeeType->getKind() == BuiltinType::Char_S); |
| 3395 | case StringLiteral::Wide: |
Dmitry Polukhin | 9d64f72 | 2016-04-14 09:52:06 +0000 | [diff] [blame] | 3396 | return Context.typesAreCompatible(Context.getWideCharType(), |
| 3397 | QualType(ToPointeeType, 0)); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 3398 | } |
| 3399 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | return false; |
| 3403 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3404 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3405 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3406 | SourceLocation CastLoc, |
| 3407 | QualType Ty, |
| 3408 | CastKind Kind, |
| 3409 | CXXMethodDecl *Method, |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3410 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3411 | bool HadMultipleCandidates, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3412 | Expr *From) { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3413 | switch (Kind) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3414 | default: llvm_unreachable("Unhandled cast kind!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3415 | case CK_ConstructorConversion: { |
Douglas Gregor | c7a3107 | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 3416 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3417 | SmallVector<Expr*, 8> ConstructorArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3418 | |
Richard Smith | 72d7405 | 2013-07-20 19:41:36 +0000 | [diff] [blame] | 3419 | if (S.RequireNonAbstractType(CastLoc, Ty, |
| 3420 | diag::err_allocation_of_abstract_type)) |
| 3421 | return ExprError(); |
| 3422 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3423 | if (S.CompleteConstructorCall(Constructor, From, CastLoc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3424 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3425 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 3426 | S.CheckConstructorAccess(CastLoc, Constructor, FoundDecl, |
| 3427 | InitializedEntity::InitializeTemporary(Ty)); |
Richard Smith | 7c9442a | 2015-02-24 21:44:43 +0000 | [diff] [blame] | 3428 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3429 | return ExprError(); |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 3430 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3431 | ExprResult Result = S.BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3432 | CastLoc, Ty, FoundDecl, cast<CXXConstructorDecl>(Method), |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3433 | ConstructorArgs, HadMultipleCandidates, |
| 3434 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3435 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3436 | if (Result.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3437 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3438 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3439 | return S.MaybeBindToTemporary(Result.getAs<Expr>()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3440 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3441 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3442 | case CK_UserDefinedConversion: { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3443 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3444 | |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3445 | S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ nullptr, FoundDecl); |
Richard Smith | 7c9442a | 2015-02-24 21:44:43 +0000 | [diff] [blame] | 3446 | if (S.DiagnoseUseOfDecl(Method, CastLoc)) |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3447 | return ExprError(); |
| 3448 | |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3449 | // Create an implicit call expr that calls it. |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 3450 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(Method); |
| 3451 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Conv, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3452 | HadMultipleCandidates); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3453 | if (Result.isInvalid()) |
| 3454 | return ExprError(); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 3455 | // Record usage of conversion in an implicit cast. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3456 | Result = ImplicitCastExpr::Create(S.Context, Result.get()->getType(), |
| 3457 | CK_UserDefinedConversion, Result.get(), |
| 3458 | nullptr, Result.get()->getValueKind()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3459 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3460 | return S.MaybeBindToTemporary(Result.get()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3461 | } |
| 3462 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3463 | } |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3464 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3465 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 3466 | /// expression From to the type ToType using the pre-computed implicit |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3467 | /// conversion sequence ICS. Returns the converted |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 3468 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3469 | /// used in the error message. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3470 | ExprResult |
| 3471 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3472 | const ImplicitConversionSequence &ICS, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3473 | AssignmentAction Action, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3474 | CheckedConversionKind CCK) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3475 | switch (ICS.getKind()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3476 | case ImplicitConversionSequence::StandardConversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3477 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, |
| 3478 | Action, CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3479 | if (Res.isInvalid()) |
| 3480 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3481 | From = Res.get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3482 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3483 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3484 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3485 | case ImplicitConversionSequence::UserDefinedConversion: { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3486 | |
Fariborz Jahanian | 2fee79a | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 3487 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3488 | CastKind CastKind; |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3489 | QualType BeforeToType; |
Richard Smith | d3f2d32 | 2015-02-24 21:16:19 +0000 | [diff] [blame] | 3490 | assert(FD && "no conversion function for user-defined conversion seq"); |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3491 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3492 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3493 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 3494 | // If the user-defined conversion is specified by a conversion function, |
| 3495 | // the initial standard conversion sequence converts the source type to |
| 3496 | // the implicit object parameter of the conversion function. |
| 3497 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
John McCall | a03edda | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 3498 | } else { |
| 3499 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3500 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3501 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 3502 | if (!ICS.UserDefined.EllipsisConversion) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3503 | // If the user-defined conversion is specified by a constructor, the |
Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 3504 | // initial standard conversion sequence converts the source type to |
| 3505 | // the type required by the argument of the constructor |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 3506 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3507 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3508 | } |
Richard Smith | 72d7405 | 2013-07-20 19:41:36 +0000 | [diff] [blame] | 3509 | // Watch out for ellipsis conversion. |
Fariborz Jahanian | eec642f | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 3510 | if (!ICS.UserDefined.EllipsisConversion) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3511 | ExprResult Res = |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3512 | PerformImplicitConversion(From, BeforeToType, |
| 3513 | ICS.UserDefined.Before, AA_Converting, |
| 3514 | CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3515 | if (Res.isInvalid()) |
| 3516 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3517 | From = Res.get(); |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3518 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3519 | |
| 3520 | ExprResult CastArg |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 3521 | = BuildCXXCastArgument(*this, |
| 3522 | From->getLocStart(), |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3523 | ToType.getNonReferenceType(), |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 3524 | CastKind, cast<CXXMethodDecl>(FD), |
| 3525 | ICS.UserDefined.FoundConversionFunction, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3526 | ICS.UserDefined.HadMultipleCandidates, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3527 | From); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3528 | |
| 3529 | if (CastArg.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3530 | return ExprError(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 3531 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3532 | From = CastArg.get(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 3533 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3534 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
| 3535 | AA_Converting, CCK); |
Fariborz Jahanian | da21efb | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 3536 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3537 | |
| 3538 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3539 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3540 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 3541 | << From->getSourceRange()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3542 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3543 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3544 | case ImplicitConversionSequence::EllipsisConversion: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3545 | llvm_unreachable("Cannot perform an ellipsis conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3546 | |
| 3547 | case ImplicitConversionSequence::BadConversion: |
Richard Smith | e15a370 | 2016-10-06 23:12:58 +0000 | [diff] [blame] | 3548 | bool Diagnosed = |
| 3549 | DiagnoseAssignmentResult(Incompatible, From->getExprLoc(), ToType, |
| 3550 | From->getType(), From, Action); |
| 3551 | assert(Diagnosed && "failed to diagnose bad conversion"); (void)Diagnosed; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3552 | return ExprError(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | // Everything went well. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3556 | return From; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3559 | /// PerformImplicitConversion - Perform an implicit conversion of the |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3560 | /// expression From to the type ToType by following the standard |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3561 | /// conversion sequence SCS. Returns the converted |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3562 | /// expression. Flavor is the context in which we're performing this |
| 3563 | /// conversion, for use in error messages. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3564 | ExprResult |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3565 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3566 | const StandardConversionSequence& SCS, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3567 | AssignmentAction Action, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3568 | CheckedConversionKind CCK) { |
| 3569 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3570 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3571 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 3572 | // much extra work. What this means is that we need to keep track of more |
| 3573 | // information that is computed when we try the implicit conversion initially, |
| 3574 | // so that we don't need to recompute anything here. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3575 | QualType FromType = From->getType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3576 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3577 | if (SCS.CopyConstructor) { |
Anders Carlsson | 549c5bd | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 3578 | // FIXME: When can ToType be a reference type? |
| 3579 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3580 | if (SCS.Second == ICK_Derived_To_Base) { |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3581 | SmallVector<Expr*, 8> ConstructorArgs; |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3582 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3583 | From, /*FIXME:ConstructLoc*/SourceLocation(), |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3584 | ConstructorArgs)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3585 | return ExprError(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3586 | return BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3587 | /*FIXME:ConstructLoc*/ SourceLocation(), ToType, |
| 3588 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3589 | ConstructorArgs, /*HadMultipleCandidates*/ false, |
| 3590 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3591 | CXXConstructExpr::CK_Complete, SourceRange()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 3592 | } |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3593 | return BuildCXXConstructExpr( |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3594 | /*FIXME:ConstructLoc*/ SourceLocation(), ToType, |
| 3595 | SCS.FoundCopyConstructor, SCS.CopyConstructor, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3596 | From, /*HadMultipleCandidates*/ false, |
| 3597 | /*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false, |
| 3598 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3601 | // Resolve overloaded function references. |
| 3602 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 3603 | DeclAccessPair Found; |
| 3604 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 3605 | true, Found); |
| 3606 | if (!Fn) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3607 | return ExprError(); |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3608 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3609 | if (DiagnoseUseOfDecl(Fn, From->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3610 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3611 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 3612 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 3613 | FromType = From->getType(); |
| 3614 | } |
| 3615 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3616 | // If we're converting to an atomic type, first convert to the corresponding |
| 3617 | // non-atomic type. |
| 3618 | QualType ToAtomicType; |
| 3619 | if (const AtomicType *ToAtomic = ToType->getAs<AtomicType>()) { |
| 3620 | ToAtomicType = ToType; |
| 3621 | ToType = ToAtomic->getValueType(); |
| 3622 | } |
| 3623 | |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 3624 | QualType InitialFromType = FromType; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3625 | // Perform the first implicit conversion. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3626 | switch (SCS.First) { |
| 3627 | case ICK_Identity: |
David Majnemer | 3087a2b | 2014-12-28 21:47:31 +0000 | [diff] [blame] | 3628 | if (const AtomicType *FromAtomic = FromType->getAs<AtomicType>()) { |
| 3629 | FromType = FromAtomic->getValueType().getUnqualifiedType(); |
| 3630 | From = ImplicitCastExpr::Create(Context, FromType, CK_AtomicToNonAtomic, |
| 3631 | From, /*BasePath=*/nullptr, VK_RValue); |
| 3632 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3633 | break; |
| 3634 | |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3635 | case ICK_Lvalue_To_Rvalue: { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 3636 | assert(From->getObjectKind() != OK_ObjCProperty); |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3637 | ExprResult FromRes = DefaultLvalueConversion(From); |
| 3638 | assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!"); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3639 | From = FromRes.get(); |
David Majnemer | e7029bc | 2014-12-16 06:31:17 +0000 | [diff] [blame] | 3640 | FromType = From->getType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3641 | break; |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 3642 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3643 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3644 | case ICK_Array_To_Pointer: |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3645 | FromType = Context.getArrayDecayedType(FromType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3646 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3647 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3648 | break; |
| 3649 | |
| 3650 | case ICK_Function_To_Pointer: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3651 | FromType = Context.getPointerType(FromType); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3652 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3653 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3654 | break; |
| 3655 | |
| 3656 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3657 | llvm_unreachable("Improper first standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3660 | // Perform the second implicit conversion |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3661 | switch (SCS.Second) { |
| 3662 | case ICK_Identity: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 3663 | // C++ [except.spec]p5: |
| 3664 | // [For] assignment to and initialization of pointers to functions, |
| 3665 | // pointers to member functions, and references to functions: the |
| 3666 | // target entity shall allow at least the exceptions allowed by the |
| 3667 | // source value in the assignment or initialization. |
| 3668 | switch (Action) { |
| 3669 | case AA_Assigning: |
| 3670 | case AA_Initializing: |
| 3671 | // Note, function argument passing and returning are initialization. |
| 3672 | case AA_Passing: |
| 3673 | case AA_Returning: |
| 3674 | case AA_Sending: |
| 3675 | case AA_Passing_CFAudited: |
| 3676 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 3677 | return ExprError(); |
| 3678 | break; |
| 3679 | |
| 3680 | case AA_Casting: |
| 3681 | case AA_Converting: |
| 3682 | // Casts and implicit conversions are not initialization, so are not |
| 3683 | // checked for exception specification mismatches. |
| 3684 | break; |
| 3685 | } |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3686 | // Nothing else to do. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3687 | break; |
| 3688 | |
| 3689 | case ICK_Integral_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3690 | case ICK_Integral_Conversion: |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3691 | if (ToType->isBooleanType()) { |
| 3692 | assert(FromType->castAs<EnumType>()->getDecl()->isFixed() && |
| 3693 | SCS.Second == ICK_Integral_Promotion && |
| 3694 | "only enums with fixed underlying type can promote to bool"); |
| 3695 | From = ImpCastExprToType(From, ToType, CK_IntegralToBoolean, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3696 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3697 | } else { |
| 3698 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3699 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 3700 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | |
| 3703 | case ICK_Floating_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3704 | case ICK_Floating_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3705 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3706 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3707 | break; |
| 3708 | |
| 3709 | case ICK_Complex_Promotion: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3710 | case ICK_Complex_Conversion: { |
| 3711 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
| 3712 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
| 3713 | CastKind CK; |
| 3714 | if (FromEl->isRealFloatingType()) { |
| 3715 | if (ToEl->isRealFloatingType()) |
| 3716 | CK = CK_FloatingComplexCast; |
| 3717 | else |
| 3718 | CK = CK_FloatingComplexToIntegralComplex; |
| 3719 | } else if (ToEl->isRealFloatingType()) { |
| 3720 | CK = CK_IntegralComplexToFloatingComplex; |
| 3721 | } else { |
| 3722 | CK = CK_IntegralComplexCast; |
| 3723 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3724 | From = ImpCastExprToType(From, ToType, CK, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3725 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3726 | break; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3727 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3728 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3729 | case ICK_Floating_Integral: |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 3730 | if (ToType->isRealFloatingType()) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3731 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3732 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3733 | else |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3734 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3735 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3736 | break; |
| 3737 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 3738 | case ICK_Compatible_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3739 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3740 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3741 | break; |
| 3742 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3743 | case ICK_Writeback_Conversion: |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3744 | case ICK_Pointer_Conversion: { |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3745 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3746 | // Diagnose incompatible Objective-C conversions |
Douglas Gregor | 2720dc6 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 3747 | if (Action == AA_Initializing || Action == AA_Assigning) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3748 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 3749 | diag::ext_typecheck_convert_incompatible_pointer) |
| 3750 | << ToType << From->getType() << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 3751 | << From->getSourceRange() << 0; |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 3752 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3753 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 3754 | diag::ext_typecheck_convert_incompatible_pointer) |
| 3755 | << From->getType() << ToType << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 3756 | << From->getSourceRange() << 0; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3757 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3758 | if (From->getType()->isObjCObjectPointerType() && |
| 3759 | ToType->isObjCObjectPointerType()) |
| 3760 | EmitRelatedResultTypeNote(From); |
Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame] | 3761 | } else if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && |
| 3762 | !CheckObjCARCUnavailableWeakConversion(ToType, |
| 3763 | From->getType())) { |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 3764 | if (Action == AA_Initializing) |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3765 | Diag(From->getLocStart(), |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 3766 | diag::err_arc_weak_unavailable_assign); |
| 3767 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3768 | Diag(From->getLocStart(), |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3769 | diag::err_arc_convesion_of_weak_unavailable) |
| 3770 | << (Action == AA_Casting) << From->getType() << ToType |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 3771 | << From->getSourceRange(); |
| 3772 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3773 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3774 | CastKind Kind = CK_Invalid; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3775 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3776 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3777 | return ExprError(); |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3778 | |
| 3779 | // Make sure we extend blocks if necessary. |
| 3780 | // FIXME: doing this here is really ugly. |
| 3781 | if (Kind == CK_BlockPointerToObjCPointerCast) { |
| 3782 | ExprResult E = From; |
| 3783 | (void) PrepareCastToObjCObjectPointer(E); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3784 | From = E.get(); |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3785 | } |
Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame] | 3786 | if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) |
| 3787 | CheckObjCConversion(SourceRange(), ToType, From, CCK); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3788 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3789 | .get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3790 | break; |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3791 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3792 | |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3793 | case ICK_Pointer_Member: { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3794 | CastKind Kind = CK_Invalid; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3795 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3796 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3797 | return ExprError(); |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3798 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3799 | return ExprError(); |
David Majnemer | d96b997 | 2014-08-08 00:10:39 +0000 | [diff] [blame] | 3800 | |
| 3801 | // We may not have been able to figure out what this member pointer resolved |
| 3802 | // 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] | 3803 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3804 | (void)isCompleteType(From->getExprLoc(), From->getType()); |
| 3805 | (void)isCompleteType(From->getExprLoc(), ToType); |
David Majnemer | fc22e47 | 2015-06-12 17:55:44 +0000 | [diff] [blame] | 3806 | } |
David Majnemer | d96b997 | 2014-08-08 00:10:39 +0000 | [diff] [blame] | 3807 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3808 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3809 | .get(); |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 3810 | break; |
| 3811 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3812 | |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 3813 | case ICK_Boolean_Conversion: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3814 | // Perform half-to-boolean conversion via float. |
| 3815 | if (From->getType()->isHalfType()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3816 | From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).get(); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3817 | FromType = Context.FloatTy; |
| 3818 | } |
| 3819 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3820 | From = ImpCastExprToType(From, Context.BoolTy, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3821 | ScalarTypeToBooleanCastKind(FromType), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3822 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3823 | break; |
| 3824 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3825 | case ICK_Derived_To_Base: { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3826 | CXXCastPath BasePath; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3827 | if (CheckDerivedToBaseConversion(From->getType(), |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 3828 | ToType.getNonReferenceType(), |
| 3829 | From->getLocStart(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3830 | From->getSourceRange(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3831 | &BasePath, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3832 | CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3833 | return ExprError(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3834 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3835 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 3836 | CK_DerivedToBase, From->getValueKind(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3837 | &BasePath, CCK).get(); |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 3838 | break; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3841 | case ICK_Vector_Conversion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3842 | From = ImpCastExprToType(From, ToType, CK_BitCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3843 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3844 | break; |
| 3845 | |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3846 | case ICK_Vector_Splat: { |
Fariborz Jahanian | 28d94b1 | 2015-03-05 23:06:09 +0000 | [diff] [blame] | 3847 | // Vector splat from any arithmetic type to a vector. |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3848 | Expr *Elem = prepareVectorSplat(ToType, From).get(); |
| 3849 | From = ImpCastExprToType(Elem, ToType, CK_VectorSplat, VK_RValue, |
| 3850 | /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3851 | break; |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3852 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3853 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3854 | case ICK_Complex_Real: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3855 | // Case 1. x -> _Complex y |
| 3856 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
| 3857 | QualType ElType = ToComplex->getElementType(); |
| 3858 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 3859 | |
| 3860 | // x -> y |
| 3861 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
| 3862 | // do nothing |
| 3863 | } else if (From->getType()->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3864 | From = ImpCastExprToType(From, ElType, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3865 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3866 | } else { |
| 3867 | assert(From->getType()->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3868 | From = ImpCastExprToType(From, ElType, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3869 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3870 | } |
| 3871 | // y -> _Complex y |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3872 | From = ImpCastExprToType(From, ToType, |
| 3873 | isFloatingComplex ? CK_FloatingRealToComplex |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3874 | : CK_IntegralRealToComplex).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3875 | |
| 3876 | // Case 2. _Complex x -> y |
| 3877 | } else { |
| 3878 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
| 3879 | assert(FromComplex); |
| 3880 | |
| 3881 | QualType ElType = FromComplex->getElementType(); |
| 3882 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 3883 | |
| 3884 | // _Complex x -> x |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3885 | From = ImpCastExprToType(From, ElType, |
| 3886 | isFloatingComplex ? CK_FloatingComplexToReal |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3887 | : CK_IntegralComplexToReal, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3888 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3889 | |
| 3890 | // x -> y |
| 3891 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
| 3892 | // do nothing |
| 3893 | } else if (ToType->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3894 | From = ImpCastExprToType(From, ToType, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3895 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3896 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3897 | } else { |
| 3898 | assert(ToType->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3899 | From = ImpCastExprToType(From, ToType, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3900 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3901 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3902 | } |
| 3903 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3904 | break; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3905 | |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 3906 | case ICK_Block_Pointer_Conversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3907 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3908 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3909 | break; |
| 3910 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 3911 | |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 3912 | case ICK_TransparentUnionConversion: { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3913 | ExprResult FromRes = From; |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 3914 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3915 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); |
| 3916 | if (FromRes.isInvalid()) |
| 3917 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3918 | From = FromRes.get(); |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 3919 | assert ((ConvTy == Sema::Compatible) && |
| 3920 | "Improper transparent union conversion"); |
| 3921 | (void)ConvTy; |
| 3922 | break; |
| 3923 | } |
| 3924 | |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 3925 | case ICK_Zero_Event_Conversion: |
| 3926 | From = ImpCastExprToType(From, ToType, |
| 3927 | CK_ZeroToOCLEvent, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3928 | From->getValueKind()).get(); |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 3929 | break; |
| 3930 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3931 | case ICK_Zero_Queue_Conversion: |
| 3932 | From = ImpCastExprToType(From, ToType, |
| 3933 | CK_ZeroToOCLQueue, |
| 3934 | From->getValueKind()).get(); |
| 3935 | break; |
| 3936 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3937 | case ICK_Lvalue_To_Rvalue: |
| 3938 | case ICK_Array_To_Pointer: |
| 3939 | case ICK_Function_To_Pointer: |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 3940 | case ICK_Function_Conversion: |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3941 | case ICK_Qualification: |
| 3942 | case ICK_Num_Conversion_Kinds: |
George Burgess IV | 78ed9b4 | 2015-10-11 20:37:14 +0000 | [diff] [blame] | 3943 | case ICK_C_Only_Conversion: |
George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 3944 | case ICK_Incompatible_Pointer_Conversion: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3945 | llvm_unreachable("Improper second standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | switch (SCS.Third) { |
| 3949 | case ICK_Identity: |
| 3950 | // Nothing to do. |
| 3951 | break; |
| 3952 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 3953 | case ICK_Function_Conversion: |
| 3954 | // If both sides are functions (or pointers/references to them), there could |
| 3955 | // be incompatible exception declarations. |
| 3956 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 3957 | return ExprError(); |
| 3958 | |
| 3959 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 3960 | VK_RValue, /*BasePath=*/nullptr, CCK).get(); |
| 3961 | break; |
| 3962 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3963 | case ICK_Qualification: { |
| 3964 | // The qualification keeps the category of the inner expression, unless the |
| 3965 | // target type isn't a reference. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3966 | ExprValueKind VK = ToType->isReferenceType() ? |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 3967 | From->getValueKind() : VK_RValue; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 3968 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3969 | CK_NoOp, VK, /*BasePath=*/nullptr, CCK).get(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3970 | |
Douglas Gregor | e981bb0 | 2011-03-14 16:13:32 +0000 | [diff] [blame] | 3971 | if (SCS.DeprecatedStringLiteralToCharPtr && |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3972 | !getLangOpts().WritableStrings) { |
| 3973 | Diag(From->getLocStart(), getLangOpts().CPlusPlus11 |
| 3974 | ? diag::ext_deprecated_string_literal_conversion |
| 3975 | : diag::warn_deprecated_string_literal_conversion) |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3976 | << ToType.getNonReferenceType(); |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3977 | } |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3978 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3979 | break; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3980 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3981 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3982 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3983 | llvm_unreachable("Improper third standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Douglas Gregor | 298f43d | 2012-04-12 20:42:30 +0000 | [diff] [blame] | 3986 | // If this conversion sequence involved a scalar -> atomic conversion, perform |
| 3987 | // that conversion now. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3988 | if (!ToAtomicType.isNull()) { |
| 3989 | assert(Context.hasSameType( |
| 3990 | ToAtomicType->castAs<AtomicType>()->getValueType(), From->getType())); |
| 3991 | From = ImpCastExprToType(From, ToAtomicType, CK_NonAtomicToAtomic, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3992 | VK_RValue, nullptr, CCK).get(); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
George Burgess IV | 8d141e0 | 2015-12-14 22:00:49 +0000 | [diff] [blame] | 3995 | // If this conversion sequence succeeded and involved implicitly converting a |
| 3996 | // _Nullable type to a _Nonnull one, complain. |
| 3997 | if (CCK == CCK_ImplicitConversion) |
| 3998 | diagnoseNullableToNonnullConversion(ToType, InitialFromType, |
| 3999 | From->getLocStart()); |
| 4000 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4001 | return From; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4004 | /// \brief Check the completeness of a type in a unary type trait. |
| 4005 | /// |
| 4006 | /// If the particular type trait requires a complete type, tries to complete |
| 4007 | /// it. If completing the type fails, a diagnostic is emitted and false |
| 4008 | /// returned. If completing the type succeeds or no completion was required, |
| 4009 | /// returns true. |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4010 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT, |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4011 | SourceLocation Loc, |
| 4012 | QualType ArgTy) { |
| 4013 | // C++0x [meta.unary.prop]p3: |
| 4014 | // For all of the class templates X declared in this Clause, instantiating |
| 4015 | // that template with a template argument that is a class template |
| 4016 | // specialization may result in the implicit instantiation of the template |
| 4017 | // argument if and only if the semantics of X require that the argument |
| 4018 | // must be a complete type. |
| 4019 | // We apply this rule to all the type trait expressions used to implement |
| 4020 | // these class templates. We also try to follow any GCC documented behavior |
| 4021 | // in these expressions to ensure portability of standard libraries. |
| 4022 | switch (UTT) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4023 | default: llvm_unreachable("not a UTT"); |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4024 | // is_complete_type somewhat obviously cannot require a complete type. |
| 4025 | case UTT_IsCompleteType: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 4026 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4027 | |
| 4028 | // These traits are modeled on the type predicates in C++0x |
| 4029 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as |
| 4030 | // requiring a complete type, as whether or not they return true cannot be |
| 4031 | // impacted by the completeness of the type. |
| 4032 | case UTT_IsVoid: |
| 4033 | case UTT_IsIntegral: |
| 4034 | case UTT_IsFloatingPoint: |
| 4035 | case UTT_IsArray: |
| 4036 | case UTT_IsPointer: |
| 4037 | case UTT_IsLvalueReference: |
| 4038 | case UTT_IsRvalueReference: |
| 4039 | case UTT_IsMemberFunctionPointer: |
| 4040 | case UTT_IsMemberObjectPointer: |
| 4041 | case UTT_IsEnum: |
| 4042 | case UTT_IsUnion: |
| 4043 | case UTT_IsClass: |
| 4044 | case UTT_IsFunction: |
| 4045 | case UTT_IsReference: |
| 4046 | case UTT_IsArithmetic: |
| 4047 | case UTT_IsFundamental: |
| 4048 | case UTT_IsObject: |
| 4049 | case UTT_IsScalar: |
| 4050 | case UTT_IsCompound: |
| 4051 | case UTT_IsMemberPointer: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 4052 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4053 | |
| 4054 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] |
| 4055 | // which requires some of its traits to have the complete type. However, |
| 4056 | // the completeness of the type cannot impact these traits' semantics, and |
| 4057 | // so they don't require it. This matches the comments on these traits in |
| 4058 | // Table 49. |
| 4059 | case UTT_IsConst: |
| 4060 | case UTT_IsVolatile: |
| 4061 | case UTT_IsSigned: |
| 4062 | case UTT_IsUnsigned: |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4063 | |
| 4064 | // This type trait always returns false, checking the type is moot. |
| 4065 | case UTT_IsInterfaceClass: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4066 | return true; |
| 4067 | |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4068 | // C++14 [meta.unary.prop]: |
| 4069 | // If T is a non-union class type, T shall be a complete type. |
| 4070 | case UTT_IsEmpty: |
| 4071 | case UTT_IsPolymorphic: |
| 4072 | case UTT_IsAbstract: |
| 4073 | if (const auto *RD = ArgTy->getAsCXXRecordDecl()) |
| 4074 | if (!RD->isUnion()) |
| 4075 | return !S.RequireCompleteType( |
| 4076 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
| 4077 | return true; |
| 4078 | |
| 4079 | // C++14 [meta.unary.prop]: |
| 4080 | // If T is a class type, T shall be a complete type. |
| 4081 | case UTT_IsFinal: |
| 4082 | case UTT_IsSealed: |
| 4083 | if (ArgTy->getAsCXXRecordDecl()) |
| 4084 | return !S.RequireCompleteType( |
| 4085 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
| 4086 | return true; |
| 4087 | |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4088 | // C++1z [meta.unary.prop]: |
| 4089 | // remove_all_extents_t<T> shall be a complete type or cv void. |
Eric Fiselier | 0736066 | 2017-04-12 22:12:15 +0000 | [diff] [blame] | 4090 | case UTT_IsAggregate: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4091 | case UTT_IsTrivial: |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 4092 | case UTT_IsTriviallyCopyable: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4093 | case UTT_IsStandardLayout: |
| 4094 | case UTT_IsPOD: |
| 4095 | case UTT_IsLiteral: |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4096 | ArgTy = QualType(ArgTy->getBaseElementTypeUnsafe(), 0); |
| 4097 | LLVM_FALLTHROUGH; |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4098 | |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4099 | // C++1z [meta.unary.prop]: |
| 4100 | // T shall be a complete type, cv void, or an array of unknown bound. |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4101 | case UTT_IsDestructible: |
| 4102 | case UTT_IsNothrowDestructible: |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4103 | case UTT_IsTriviallyDestructible: |
| 4104 | // Per the GCC type traits documentation, the same constraints apply to these. |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4105 | case UTT_HasNothrowAssign: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4106 | case UTT_HasNothrowMoveAssign: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4107 | case UTT_HasNothrowConstructor: |
| 4108 | case UTT_HasNothrowCopy: |
| 4109 | case UTT_HasTrivialAssign: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4110 | case UTT_HasTrivialMoveAssign: |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4111 | case UTT_HasTrivialDefaultConstructor: |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4112 | case UTT_HasTrivialMoveConstructor: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4113 | case UTT_HasTrivialCopy: |
| 4114 | case UTT_HasTrivialDestructor: |
| 4115 | case UTT_HasVirtualDestructor: |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4116 | if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType()) |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4117 | return true; |
| 4118 | |
| 4119 | return !S.RequireCompleteType( |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4120 | Loc, ArgTy, diag::err_incomplete_type_used_in_type_trait_expr); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4121 | } |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4124 | static bool HasNoThrowOperator(const RecordType *RT, OverloadedOperatorKind Op, |
| 4125 | Sema &Self, SourceLocation KeyLoc, ASTContext &C, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4126 | bool (CXXRecordDecl::*HasTrivial)() const, |
| 4127 | bool (CXXRecordDecl::*HasNonTrivial)() const, |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4128 | bool (CXXMethodDecl::*IsDesiredOp)() const) |
| 4129 | { |
| 4130 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 4131 | if ((RD->*HasTrivial)() && !(RD->*HasNonTrivial)()) |
| 4132 | return true; |
| 4133 | |
| 4134 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(Op); |
| 4135 | DeclarationNameInfo NameInfo(Name, KeyLoc); |
| 4136 | LookupResult Res(Self, NameInfo, Sema::LookupOrdinaryName); |
| 4137 | if (Self.LookupQualifiedName(Res, RD)) { |
| 4138 | bool FoundOperator = false; |
| 4139 | Res.suppressDiagnostics(); |
| 4140 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 4141 | Op != OpEnd; ++Op) { |
| 4142 | if (isa<FunctionTemplateDecl>(*Op)) |
| 4143 | continue; |
| 4144 | |
| 4145 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 4146 | if((Operator->*IsDesiredOp)()) { |
| 4147 | FoundOperator = true; |
| 4148 | const FunctionProtoType *CPT = |
| 4149 | Operator->getType()->getAs<FunctionProtoType>(); |
| 4150 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4151 | if (!CPT || !CPT->isNothrow(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4152 | return false; |
| 4153 | } |
| 4154 | } |
| 4155 | return FoundOperator; |
| 4156 | } |
| 4157 | return false; |
| 4158 | } |
| 4159 | |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4160 | static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT, |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 4161 | SourceLocation KeyLoc, QualType T) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4162 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4163 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4164 | ASTContext &C = Self.Context; |
| 4165 | switch(UTT) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4166 | default: llvm_unreachable("not a UTT"); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4167 | // Type trait expressions corresponding to the primary type category |
| 4168 | // predicates in C++0x [meta.unary.cat]. |
| 4169 | case UTT_IsVoid: |
| 4170 | return T->isVoidType(); |
| 4171 | case UTT_IsIntegral: |
| 4172 | return T->isIntegralType(C); |
| 4173 | case UTT_IsFloatingPoint: |
| 4174 | return T->isFloatingType(); |
| 4175 | case UTT_IsArray: |
| 4176 | return T->isArrayType(); |
| 4177 | case UTT_IsPointer: |
| 4178 | return T->isPointerType(); |
| 4179 | case UTT_IsLvalueReference: |
| 4180 | return T->isLValueReferenceType(); |
| 4181 | case UTT_IsRvalueReference: |
| 4182 | return T->isRValueReferenceType(); |
| 4183 | case UTT_IsMemberFunctionPointer: |
| 4184 | return T->isMemberFunctionPointerType(); |
| 4185 | case UTT_IsMemberObjectPointer: |
| 4186 | return T->isMemberDataPointerType(); |
| 4187 | case UTT_IsEnum: |
| 4188 | return T->isEnumeralType(); |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4189 | case UTT_IsUnion: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4190 | return T->isUnionType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4191 | case UTT_IsClass: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4192 | return T->isClassType() || T->isStructureType() || T->isInterfaceType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4193 | case UTT_IsFunction: |
| 4194 | return T->isFunctionType(); |
| 4195 | |
| 4196 | // Type trait expressions which correspond to the convenient composition |
| 4197 | // predicates in C++0x [meta.unary.comp]. |
| 4198 | case UTT_IsReference: |
| 4199 | return T->isReferenceType(); |
| 4200 | case UTT_IsArithmetic: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4201 | return T->isArithmeticType() && !T->isEnumeralType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4202 | case UTT_IsFundamental: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4203 | return T->isFundamentalType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4204 | case UTT_IsObject: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4205 | return T->isObjectType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4206 | case UTT_IsScalar: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4207 | // Note: semantic analysis depends on Objective-C lifetime types to be |
| 4208 | // considered scalar types. However, such types do not actually behave |
| 4209 | // like scalar types at run time (since they may require retain/release |
| 4210 | // operations), so we report them as non-scalar. |
| 4211 | if (T->isObjCLifetimeType()) { |
| 4212 | switch (T.getObjCLifetime()) { |
| 4213 | case Qualifiers::OCL_None: |
| 4214 | case Qualifiers::OCL_ExplicitNone: |
| 4215 | return true; |
| 4216 | |
| 4217 | case Qualifiers::OCL_Strong: |
| 4218 | case Qualifiers::OCL_Weak: |
| 4219 | case Qualifiers::OCL_Autoreleasing: |
| 4220 | return false; |
| 4221 | } |
| 4222 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4223 | |
Chandler Carruth | 7ba7bd3 | 2011-05-01 09:29:55 +0000 | [diff] [blame] | 4224 | return T->isScalarType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4225 | case UTT_IsCompound: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 4226 | return T->isCompoundType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4227 | case UTT_IsMemberPointer: |
| 4228 | return T->isMemberPointerType(); |
| 4229 | |
| 4230 | // Type trait expressions which correspond to the type property predicates |
| 4231 | // in C++0x [meta.unary.prop]. |
| 4232 | case UTT_IsConst: |
| 4233 | return T.isConstQualified(); |
| 4234 | case UTT_IsVolatile: |
| 4235 | return T.isVolatileQualified(); |
| 4236 | case UTT_IsTrivial: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4237 | return T.isTrivialType(C); |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 4238 | case UTT_IsTriviallyCopyable: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4239 | return T.isTriviallyCopyableType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4240 | case UTT_IsStandardLayout: |
| 4241 | return T->isStandardLayoutType(); |
| 4242 | case UTT_IsPOD: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4243 | return T.isPODType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4244 | case UTT_IsLiteral: |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4245 | return T->isLiteralType(C); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4246 | case UTT_IsEmpty: |
| 4247 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4248 | return !RD->isUnion() && RD->isEmpty(); |
| 4249 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4250 | case UTT_IsPolymorphic: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4251 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4252 | return !RD->isUnion() && RD->isPolymorphic(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4253 | return false; |
| 4254 | case UTT_IsAbstract: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 4255 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4256 | return !RD->isUnion() && RD->isAbstract(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4257 | return false; |
Eric Fiselier | 0736066 | 2017-04-12 22:12:15 +0000 | [diff] [blame] | 4258 | case UTT_IsAggregate: |
| 4259 | // Report vector extensions and complex types as aggregates because they |
| 4260 | // support aggregate initialization. GCC mirrors this behavior for vectors |
| 4261 | // but not _Complex. |
| 4262 | return T->isAggregateType() || T->isVectorType() || T->isExtVectorType() || |
| 4263 | T->isAnyComplexType(); |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4264 | // __is_interface_class only returns true when CL is invoked in /CLR mode and |
| 4265 | // even then only when it is used with the 'interface struct ...' syntax |
| 4266 | // Clang doesn't support /CLR which makes this type trait moot. |
John McCall | bf4a7d7 | 2012-09-25 07:32:49 +0000 | [diff] [blame] | 4267 | case UTT_IsInterfaceClass: |
John McCall | bf4a7d7 | 2012-09-25 07:32:49 +0000 | [diff] [blame] | 4268 | return false; |
Douglas Gregor | dca70af | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 4269 | case UTT_IsFinal: |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4270 | case UTT_IsSealed: |
| 4271 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
David Majnemer | 213bea3 | 2015-11-16 06:58:51 +0000 | [diff] [blame] | 4272 | return RD->hasAttr<FinalAttr>(); |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4273 | return false; |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4274 | case UTT_IsSigned: |
| 4275 | return T->isSignedIntegerType(); |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4276 | case UTT_IsUnsigned: |
| 4277 | return T->isUnsignedIntegerType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4278 | |
| 4279 | // Type trait expressions which query classes regarding their construction, |
| 4280 | // destruction, and copying. Rather than being based directly on the |
| 4281 | // related type predicates in the standard, they are specified by both |
| 4282 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those |
| 4283 | // specifications. |
| 4284 | // |
| 4285 | // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html |
| 4286 | // 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] | 4287 | // |
| 4288 | // Note that these builtins do not behave as documented in g++: if a class |
| 4289 | // has both a trivial and a non-trivial special member of a particular kind, |
| 4290 | // they return false! For now, we emulate this behavior. |
| 4291 | // FIXME: This appears to be a g++ bug: more complex cases reveal that it |
| 4292 | // does not correctly compute triviality in the presence of multiple special |
| 4293 | // 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] | 4294 | case UTT_HasTrivialDefaultConstructor: |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4295 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4296 | // If __is_pod (type) is true then the trait is true, else if type is |
| 4297 | // a cv class or union type (or array thereof) with a trivial default |
| 4298 | // constructor ([class.ctor]) then the trait is true, else it is false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4299 | if (T.isPODType(C)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4300 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4301 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4302 | return RD->hasTrivialDefaultConstructor() && |
| 4303 | !RD->hasNonTrivialDefaultConstructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4304 | return false; |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4305 | case UTT_HasTrivialMoveConstructor: |
| 4306 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4307 | // standard library headers. Specifically this is used as the logic |
| 4308 | // behind std::is_trivially_move_constructible (20.9.4.3). |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4309 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4310 | return true; |
| 4311 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4312 | return RD->hasTrivialMoveConstructor() && !RD->hasNonTrivialMoveConstructor(); |
| 4313 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4314 | case UTT_HasTrivialCopy: |
| 4315 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4316 | // If __is_pod (type) is true or type is a reference type then |
| 4317 | // the trait is true, else if type is a cv class or union type |
| 4318 | // with a trivial copy constructor ([class.copy]) then the trait |
| 4319 | // is true, else it is false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4320 | if (T.isPODType(C) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4321 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4322 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4323 | return RD->hasTrivialCopyConstructor() && |
| 4324 | !RD->hasNonTrivialCopyConstructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4325 | return false; |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4326 | case UTT_HasTrivialMoveAssign: |
| 4327 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4328 | // standard library headers. Specifically it is used as the logic |
| 4329 | // behind std::is_trivially_move_assignable (20.9.4.3) |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4330 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4331 | return true; |
| 4332 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4333 | return RD->hasTrivialMoveAssignment() && !RD->hasNonTrivialMoveAssignment(); |
| 4334 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4335 | case UTT_HasTrivialAssign: |
| 4336 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4337 | // If type is const qualified or is a reference type then the |
| 4338 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 4339 | // trait is true, else if type is a cv class or union type with |
| 4340 | // a trivial copy assignment ([class.copy]) then the trait is |
| 4341 | // true, else it is false. |
| 4342 | // Note: the const and reference restrictions are interesting, |
| 4343 | // given that const and reference members don't prevent a class |
| 4344 | // from having a trivial copy assignment operator (but do cause |
| 4345 | // errors if the copy assignment operator is actually used, q.v. |
| 4346 | // [class.copy]p12). |
| 4347 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4348 | if (T.isConstQualified()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4349 | return false; |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4350 | if (T.isPODType(C)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4351 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4352 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 4353 | return RD->hasTrivialCopyAssignment() && |
| 4354 | !RD->hasNonTrivialCopyAssignment(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4355 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4356 | case UTT_IsDestructible: |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4357 | case UTT_IsTriviallyDestructible: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4358 | case UTT_IsNothrowDestructible: |
David Majnemer | ac73de9 | 2015-08-11 03:03:28 +0000 | [diff] [blame] | 4359 | // C++14 [meta.unary.prop]: |
| 4360 | // For reference types, is_destructible<T>::value is true. |
| 4361 | if (T->isReferenceType()) |
| 4362 | return true; |
| 4363 | |
| 4364 | // Objective-C++ ARC: autorelease types don't require destruction. |
| 4365 | if (T->isObjCLifetimeType() && |
| 4366 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 4367 | return true; |
| 4368 | |
| 4369 | // C++14 [meta.unary.prop]: |
| 4370 | // For incomplete types and function types, is_destructible<T>::value is |
| 4371 | // false. |
| 4372 | if (T->isIncompleteType() || T->isFunctionType()) |
| 4373 | return false; |
| 4374 | |
Richard Smith | f03e908 | 2017-06-01 00:28:16 +0000 | [diff] [blame] | 4375 | // A type that requires destruction (via a non-trivial destructor or ARC |
| 4376 | // lifetime semantics) is not trivially-destructible. |
| 4377 | if (UTT == UTT_IsTriviallyDestructible && T.isDestructedType()) |
| 4378 | return false; |
| 4379 | |
David Majnemer | ac73de9 | 2015-08-11 03:03:28 +0000 | [diff] [blame] | 4380 | // C++14 [meta.unary.prop]: |
| 4381 | // For object types and given U equal to remove_all_extents_t<T>, if the |
| 4382 | // expression std::declval<U&>().~U() is well-formed when treated as an |
| 4383 | // unevaluated operand (Clause 5), then is_destructible<T>::value is true |
| 4384 | if (auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
| 4385 | CXXDestructorDecl *Destructor = Self.LookupDestructor(RD); |
| 4386 | if (!Destructor) |
| 4387 | return false; |
| 4388 | // C++14 [dcl.fct.def.delete]p2: |
| 4389 | // A program that refers to a deleted function implicitly or |
| 4390 | // explicitly, other than to declare it, is ill-formed. |
| 4391 | if (Destructor->isDeleted()) |
| 4392 | return false; |
| 4393 | if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public) |
| 4394 | return false; |
| 4395 | if (UTT == UTT_IsNothrowDestructible) { |
| 4396 | const FunctionProtoType *CPT = |
| 4397 | Destructor->getType()->getAs<FunctionProtoType>(); |
| 4398 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4399 | if (!CPT || !CPT->isNothrow(C)) |
| 4400 | return false; |
| 4401 | } |
| 4402 | } |
| 4403 | return true; |
| 4404 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4405 | case UTT_HasTrivialDestructor: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4406 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4407 | // If __is_pod (type) is true or type is a reference type |
| 4408 | // then the trait is true, else if type is a cv class or union |
| 4409 | // type (or array thereof) with a trivial destructor |
| 4410 | // ([class.dtor]) then the trait is true, else it is |
| 4411 | // false. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4412 | if (T.isPODType(C) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4413 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4414 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4415 | // Objective-C++ ARC: autorelease types don't require destruction. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4416 | if (T->isObjCLifetimeType() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4417 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 4418 | return true; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4419 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4420 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) |
| 4421 | return RD->hasTrivialDestructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4422 | return false; |
| 4423 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 4424 | case UTT_HasNothrowAssign: |
| 4425 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4426 | // If type is const qualified or is a reference type then the |
| 4427 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 4428 | // is true then the trait is true, else if type is a cv class |
| 4429 | // or union type with copy assignment operators that are known |
| 4430 | // not to throw an exception then the trait is true, else it is |
| 4431 | // false. |
| 4432 | if (C.getBaseElementType(T).isConstQualified()) |
| 4433 | return false; |
| 4434 | if (T->isReferenceType()) |
| 4435 | return false; |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4436 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4437 | return true; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4438 | |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4439 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 4440 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
| 4441 | &CXXRecordDecl::hasTrivialCopyAssignment, |
| 4442 | &CXXRecordDecl::hasNonTrivialCopyAssignment, |
| 4443 | &CXXMethodDecl::isCopyAssignmentOperator); |
| 4444 | return false; |
| 4445 | case UTT_HasNothrowMoveAssign: |
| 4446 | // This trait is implemented by MSVC 2012 and needed to parse the |
| 4447 | // standard library headers. Specifically this is used as the logic |
| 4448 | // behind std::is_nothrow_move_assignable (20.9.4.3). |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4449 | if (T.isPODType(C)) |
Joao Matos | c9523d4 | 2013-03-27 01:34:16 +0000 | [diff] [blame] | 4450 | return true; |
| 4451 | |
| 4452 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) |
| 4453 | return HasNoThrowOperator(RT, OO_Equal, Self, KeyLoc, C, |
| 4454 | &CXXRecordDecl::hasTrivialMoveAssignment, |
| 4455 | &CXXRecordDecl::hasNonTrivialMoveAssignment, |
| 4456 | &CXXMethodDecl::isMoveAssignmentOperator); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4457 | return false; |
| 4458 | case UTT_HasNothrowCopy: |
| 4459 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4460 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 4461 | // if type is a cv class or union type with copy constructors that are |
| 4462 | // known not to throw an exception then the trait is true, else it is |
| 4463 | // false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4464 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4465 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4466 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
| 4467 | if (RD->hasTrivialCopyConstructor() && |
| 4468 | !RD->hasNonTrivialCopyConstructor()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4469 | return true; |
| 4470 | |
| 4471 | bool FoundConstructor = false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4472 | unsigned FoundTQs; |
Aaron Ballman | e4de1a51 | 2015-07-21 22:33:52 +0000 | [diff] [blame] | 4473 | for (const auto *ND : Self.LookupConstructors(RD)) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4474 | // A template constructor is never a copy constructor. |
| 4475 | // FIXME: However, it may actually be selected at the actual overload |
| 4476 | // resolution point. |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4477 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4478 | continue; |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4479 | // UsingDecl itself is not a constructor |
| 4480 | if (isa<UsingDecl>(ND)) |
| 4481 | continue; |
| 4482 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4483 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 4484 | FoundConstructor = true; |
| 4485 | const FunctionProtoType *CPT |
| 4486 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4487 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4488 | if (!CPT) |
| 4489 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4490 | // TODO: check whether evaluating default arguments can throw. |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4491 | // For now, we'll be conservative and assume that they can throw. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4492 | if (!CPT->isNothrow(C) || CPT->getNumParams() > 1) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4493 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4494 | } |
| 4495 | } |
| 4496 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +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_HasNothrowConstructor: |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4501 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4502 | // If __has_trivial_constructor (type) is true then the trait is |
| 4503 | // true, else if type is a cv class or union type (or array |
| 4504 | // thereof) with a default constructor that is known not to |
| 4505 | // throw an exception then the trait is true, else it is false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4506 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4507 | return true; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4508 | if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) { |
| 4509 | if (RD->hasTrivialDefaultConstructor() && |
| 4510 | !RD->hasNonTrivialDefaultConstructor()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4511 | return true; |
| 4512 | |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4513 | bool FoundConstructor = false; |
Aaron Ballman | e4de1a51 | 2015-07-21 22:33:52 +0000 | [diff] [blame] | 4514 | for (const auto *ND : Self.LookupConstructors(RD)) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4515 | // FIXME: In C++0x, a constructor template can be a default constructor. |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4516 | if (isa<FunctionTemplateDecl>(ND->getUnderlyingDecl())) |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 4517 | continue; |
Hal Finkel | fec8345 | 2016-11-27 16:26:14 +0000 | [diff] [blame] | 4518 | // UsingDecl itself is not a constructor |
| 4519 | if (isa<UsingDecl>(ND)) |
| 4520 | continue; |
| 4521 | auto *Constructor = cast<CXXConstructorDecl>(ND->getUnderlyingDecl()); |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4522 | if (Constructor->isDefaultConstructor()) { |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4523 | FoundConstructor = true; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4524 | const FunctionProtoType *CPT |
| 4525 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4526 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 4527 | if (!CPT) |
| 4528 | return false; |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4529 | // FIXME: check whether evaluating default arguments can throw. |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4530 | // For now, we'll be conservative and assume that they can throw. |
Aaron Ballman | d4a392c | 2015-07-21 21:07:11 +0000 | [diff] [blame] | 4531 | if (!CPT->isNothrow(C) || CPT->getNumParams() > 0) |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4532 | return false; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4533 | } |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4534 | } |
Alp Toker | b4bca41 | 2014-01-20 00:23:47 +0000 | [diff] [blame] | 4535 | return FoundConstructor; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4536 | } |
| 4537 | return false; |
| 4538 | case UTT_HasVirtualDestructor: |
| 4539 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 4540 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 4541 | // then the trait is true, else it is false. |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4542 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
Sebastian Redl | 058fc82 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 4543 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4544 | return Destructor->isVirtual(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4545 | return false; |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 4546 | |
| 4547 | // These type trait expressions are modeled on the specifications for the |
| 4548 | // Embarcadero C++0x type trait functions: |
| 4549 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
| 4550 | case UTT_IsCompleteType: |
| 4551 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): |
| 4552 | // Returns True if and only if T is a complete type at the point of the |
| 4553 | // function call. |
| 4554 | return !T->isIncompleteType(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4555 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4556 | } |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 4557 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4558 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
| 4559 | QualType RhsT, SourceLocation KeyLoc); |
| 4560 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4561 | static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc, |
| 4562 | ArrayRef<TypeSourceInfo *> Args, |
| 4563 | SourceLocation RParenLoc) { |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4564 | if (Kind <= UTT_Last) |
| 4565 | return EvaluateUnaryTypeTrait(S, Kind, KWLoc, Args[0]->getType()); |
| 4566 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4567 | if (Kind <= BTT_Last) |
| 4568 | return EvaluateBinaryTypeTrait(S, Kind, Args[0]->getType(), |
| 4569 | Args[1]->getType(), RParenLoc); |
| 4570 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4571 | switch (Kind) { |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4572 | case clang::TT_IsConstructible: |
| 4573 | case clang::TT_IsNothrowConstructible: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4574 | case clang::TT_IsTriviallyConstructible: { |
| 4575 | // C++11 [meta.unary.prop]: |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 4576 | // is_trivially_constructible is defined as: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4577 | // |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 4578 | // is_constructible<T, Args...>::value is true and the variable |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 4579 | // definition for is_constructible, as defined below, is known to call |
| 4580 | // no operation that is not trivial. |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4581 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4582 | // The predicate condition for a template specialization |
| 4583 | // is_constructible<T, Args...> shall be satisfied if and only if the |
| 4584 | // following variable definition would be well-formed for some invented |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4585 | // variable t: |
| 4586 | // |
| 4587 | // T t(create<Args>()...); |
Alp Toker | 40f9b1c | 2013-12-12 21:23:03 +0000 | [diff] [blame] | 4588 | assert(!Args.empty()); |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4589 | |
| 4590 | // Precondition: T and all types in the parameter pack Args shall be |
| 4591 | // complete types, (possibly cv-qualified) void, or arrays of |
| 4592 | // unknown bound. |
Aaron Ballman | 2bf2cad | 2015-07-21 21:18:29 +0000 | [diff] [blame] | 4593 | for (const auto *TSI : Args) { |
| 4594 | QualType ArgTy = TSI->getType(); |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4595 | if (ArgTy->isVoidType() || ArgTy->isIncompleteArrayType()) |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4596 | continue; |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4597 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4598 | if (S.RequireCompleteType(KWLoc, ArgTy, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4599 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4600 | return false; |
| 4601 | } |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4602 | |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4603 | // Make sure the first argument is not incomplete nor a function type. |
| 4604 | QualType T = Args[0]->getType(); |
| 4605 | if (T->isIncompleteType() || T->isFunctionType()) |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4606 | return false; |
Eli Friedman | 9ea1e16 | 2013-09-11 02:53:02 +0000 | [diff] [blame] | 4607 | |
Nikola Smiljanic | 1b4b6ba | 2014-04-15 11:30:15 +0000 | [diff] [blame] | 4608 | // Make sure the first argument is not an abstract type. |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4609 | CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
Nikola Smiljanic | 1b4b6ba | 2014-04-15 11:30:15 +0000 | [diff] [blame] | 4610 | if (RD && RD->isAbstract()) |
| 4611 | return false; |
| 4612 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4613 | SmallVector<OpaqueValueExpr, 2> OpaqueArgExprs; |
| 4614 | SmallVector<Expr *, 2> ArgExprs; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4615 | ArgExprs.reserve(Args.size() - 1); |
| 4616 | for (unsigned I = 1, N = Args.size(); I != N; ++I) { |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4617 | QualType ArgTy = Args[I]->getType(); |
| 4618 | if (ArgTy->isObjectType() || ArgTy->isFunctionType()) |
| 4619 | ArgTy = S.Context.getRValueReferenceType(ArgTy); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4620 | OpaqueArgExprs.push_back( |
David Majnemer | 9658ecc | 2015-11-13 05:32:43 +0000 | [diff] [blame] | 4621 | OpaqueValueExpr(Args[I]->getTypeLoc().getLocStart(), |
| 4622 | ArgTy.getNonLValueExprType(S.Context), |
| 4623 | Expr::getValueKindForType(ArgTy))); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4624 | } |
Richard Smith | a507bfc | 2014-07-23 20:07:08 +0000 | [diff] [blame] | 4625 | for (Expr &E : OpaqueArgExprs) |
| 4626 | ArgExprs.push_back(&E); |
| 4627 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4628 | // Perform the initialization in an unevaluated context within a SFINAE |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4629 | // trap at translation unit scope. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4630 | EnterExpressionEvaluationContext Unevaluated( |
| 4631 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4632 | Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true); |
| 4633 | Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl()); |
| 4634 | InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0])); |
| 4635 | InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc, |
| 4636 | RParenLoc)); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4637 | InitializationSequence Init(S, To, InitKind, ArgExprs); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4638 | if (Init.Failed()) |
| 4639 | return false; |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4640 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4641 | ExprResult Result = Init.Perform(S, To, InitKind, ArgExprs); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4642 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 4643 | return false; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4644 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4645 | if (Kind == clang::TT_IsConstructible) |
| 4646 | return true; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4647 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4648 | if (Kind == clang::TT_IsNothrowConstructible) |
| 4649 | return S.canThrow(Result.get()) == CT_Cannot; |
| 4650 | |
| 4651 | if (Kind == clang::TT_IsTriviallyConstructible) { |
Brian Kelley | 93c640b | 2017-03-29 17:40:35 +0000 | [diff] [blame] | 4652 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
| 4653 | // Objective-C lifetime, this is a non-trivial construction. |
| 4654 | if (T.getNonReferenceType().hasNonTrivialObjCLifetime()) |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4655 | return false; |
| 4656 | |
| 4657 | // The initialization succeeded; now make sure there are no non-trivial |
| 4658 | // calls. |
| 4659 | return !Result.get()->hasNonTrivialCall(S.Context); |
| 4660 | } |
| 4661 | |
| 4662 | llvm_unreachable("unhandled type trait"); |
| 4663 | return false; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4664 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4665 | default: llvm_unreachable("not a TT"); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4666 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4667 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4668 | return false; |
| 4669 | } |
| 4670 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4671 | ExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 4672 | ArrayRef<TypeSourceInfo *> Args, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4673 | SourceLocation RParenLoc) { |
Alp Toker | 5294e6e | 2013-12-25 01:47:02 +0000 | [diff] [blame] | 4674 | QualType ResultType = Context.getLogicalOperationType(); |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4675 | |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 4676 | if (Kind <= UTT_Last && !CheckUnaryTypeTraitTypeCompleteness( |
| 4677 | *this, Kind, KWLoc, Args[0]->getType())) |
| 4678 | return ExprError(); |
| 4679 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4680 | bool Dependent = false; |
| 4681 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 4682 | if (Args[I]->getType()->isDependentType()) { |
| 4683 | Dependent = true; |
| 4684 | break; |
| 4685 | } |
| 4686 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4687 | |
| 4688 | bool Result = false; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4689 | if (!Dependent) |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4690 | Result = evaluateTypeTrait(*this, Kind, KWLoc, Args, RParenLoc); |
| 4691 | |
| 4692 | return TypeTraitExpr::Create(Context, ResultType, KWLoc, Kind, Args, |
| 4693 | RParenLoc, Result); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Alp Toker | 88f64e6 | 2013-12-13 21:19:30 +0000 | [diff] [blame] | 4696 | ExprResult Sema::ActOnTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 4697 | ArrayRef<ParsedType> Args, |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4698 | SourceLocation RParenLoc) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4699 | SmallVector<TypeSourceInfo *, 4> ConvertedArgs; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4700 | ConvertedArgs.reserve(Args.size()); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4701 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4702 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 4703 | TypeSourceInfo *TInfo; |
| 4704 | QualType T = GetTypeFromParser(Args[I], &TInfo); |
| 4705 | if (!TInfo) |
| 4706 | TInfo = Context.getTrivialTypeSourceInfo(T, KWLoc); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4707 | |
| 4708 | ConvertedArgs.push_back(TInfo); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4709 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4710 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4711 | return BuildTypeTrait(Kind, KWLoc, ConvertedArgs, RParenLoc); |
| 4712 | } |
| 4713 | |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4714 | static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, QualType LhsT, |
| 4715 | QualType RhsT, SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4716 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
| 4717 | "Cannot evaluate traits of dependent types"); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4718 | |
| 4719 | switch(BTT) { |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4720 | case BTT_IsBaseOf: { |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4721 | // C++0x [meta.rel]p2 |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4722 | // 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] | 4723 | // Base and Derived are not unions and name the same class type without |
| 4724 | // regard to cv-qualifiers. |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4725 | |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4726 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4727 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
Erik Pilkington | 07f8c43 | 2017-05-10 17:18:56 +0000 | [diff] [blame] | 4728 | if (!rhsRecord || !lhsRecord) { |
| 4729 | const ObjCObjectType *LHSObjTy = LhsT->getAs<ObjCObjectType>(); |
| 4730 | const ObjCObjectType *RHSObjTy = RhsT->getAs<ObjCObjectType>(); |
| 4731 | if (!LHSObjTy || !RHSObjTy) |
| 4732 | return false; |
| 4733 | |
| 4734 | ObjCInterfaceDecl *BaseInterface = LHSObjTy->getInterface(); |
| 4735 | ObjCInterfaceDecl *DerivedInterface = RHSObjTy->getInterface(); |
| 4736 | if (!BaseInterface || !DerivedInterface) |
| 4737 | return false; |
| 4738 | |
| 4739 | if (Self.RequireCompleteType( |
| 4740 | KeyLoc, RhsT, diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4741 | return false; |
| 4742 | |
| 4743 | return BaseInterface->isSuperClassOf(DerivedInterface); |
| 4744 | } |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4745 | |
| 4746 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
| 4747 | == (lhsRecord == rhsRecord)); |
| 4748 | |
| 4749 | if (lhsRecord == rhsRecord) |
| 4750 | return !lhsRecord->getDecl()->isUnion(); |
| 4751 | |
| 4752 | // C++0x [meta.rel]p2: |
| 4753 | // If Base and Derived are class types and are different types |
| 4754 | // (ignoring possible cv-qualifiers) then Derived shall be a |
| 4755 | // complete type. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4756 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 4757 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4758 | return false; |
| 4759 | |
| 4760 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) |
| 4761 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); |
| 4762 | } |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4763 | case BTT_IsSame: |
| 4764 | return Self.Context.hasSameType(LhsT, RhsT); |
Francois Pichet | 34b2113 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 4765 | case BTT_TypeCompatible: |
| 4766 | return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(), |
| 4767 | RhsT.getUnqualifiedType()); |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 4768 | case BTT_IsConvertible: |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4769 | case BTT_IsConvertibleTo: { |
| 4770 | // C++0x [meta.rel]p4: |
| 4771 | // Given the following function prototype: |
| 4772 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4773 | // template <class T> |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4774 | // typename add_rvalue_reference<T>::type create(); |
| 4775 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4776 | // the predicate condition for a template specialization |
| 4777 | // is_convertible<From, To> shall be satisfied if and only if |
| 4778 | // the return expression in the following code would be |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4779 | // well-formed, including any implicit conversions to the return |
| 4780 | // type of the function: |
| 4781 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4782 | // To test() { |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4783 | // return create<From>(); |
| 4784 | // } |
| 4785 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4786 | // Access checking is performed as if in a context unrelated to To and |
| 4787 | // From. Only the validity of the immediate context of the expression |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4788 | // of the return-statement (including conversions to the return type) |
| 4789 | // is considered. |
| 4790 | // |
| 4791 | // We model the initialization as a copy-initialization of a temporary |
| 4792 | // of the appropriate type, which for this expression is identical to the |
| 4793 | // return statement (since NRVO doesn't apply). |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4794 | |
| 4795 | // Functions aren't allowed to return function or array types. |
| 4796 | if (RhsT->isFunctionType() || RhsT->isArrayType()) |
| 4797 | return false; |
| 4798 | |
| 4799 | // A return statement in a void function must have void type. |
| 4800 | if (RhsT->isVoidType()) |
| 4801 | return LhsT->isVoidType(); |
| 4802 | |
| 4803 | // A function definition requires a complete, non-abstract return type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4804 | if (!Self.isCompleteType(KeyLoc, RhsT) || Self.isAbstractType(KeyLoc, RhsT)) |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4805 | return false; |
| 4806 | |
| 4807 | // Compute the result of add_rvalue_reference. |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4808 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 4809 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4810 | |
| 4811 | // Build a fake source and destination for initialization. |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4812 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 4813 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4814 | Expr::getValueKindForType(LhsT)); |
| 4815 | Expr *FromPtr = &From; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4816 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4817 | SourceLocation())); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4818 | |
| 4819 | // Perform the initialization in an unevaluated context within a SFINAE |
Eli Friedman | a59b190 | 2012-01-25 01:05:57 +0000 | [diff] [blame] | 4820 | // trap at translation unit scope. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4821 | EnterExpressionEvaluationContext Unevaluated( |
| 4822 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 4823 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 4824 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4825 | InitializationSequence Init(Self, To, Kind, FromPtr); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 4826 | if (Init.Failed()) |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4827 | return false; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 4828 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4829 | ExprResult Result = Init.Perform(Self, To, Kind, FromPtr); |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 4830 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); |
| 4831 | } |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4832 | |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 4833 | case BTT_IsAssignable: |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4834 | case BTT_IsNothrowAssignable: |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4835 | case BTT_IsTriviallyAssignable: { |
| 4836 | // C++11 [meta.unary.prop]p3: |
| 4837 | // is_trivially_assignable is defined as: |
| 4838 | // is_assignable<T, U>::value is true and the assignment, as defined by |
| 4839 | // is_assignable, is known to call no operation that is not trivial |
| 4840 | // |
| 4841 | // is_assignable is defined as: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4842 | // The expression declval<T>() = declval<U>() is well-formed when |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4843 | // treated as an unevaluated operand (Clause 5). |
| 4844 | // |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4845 | // For both, T and U shall be complete types, (possibly cv-qualified) |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4846 | // void, or arrays of unknown bound. |
| 4847 | if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() && |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4848 | Self.RequireCompleteType(KeyLoc, LhsT, |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4849 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4850 | return false; |
| 4851 | if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() && |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4852 | Self.RequireCompleteType(KeyLoc, RhsT, |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4853 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 4854 | return false; |
| 4855 | |
| 4856 | // cv void is never assignable. |
| 4857 | if (LhsT->isVoidType() || RhsT->isVoidType()) |
| 4858 | return false; |
| 4859 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4860 | // Build expressions that emulate the effect of declval<T>() and |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4861 | // declval<U>(). |
| 4862 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 4863 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
| 4864 | if (RhsT->isObjectType() || RhsT->isFunctionType()) |
| 4865 | RhsT = Self.Context.getRValueReferenceType(RhsT); |
| 4866 | OpaqueValueExpr Lhs(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
| 4867 | Expr::getValueKindForType(LhsT)); |
| 4868 | OpaqueValueExpr Rhs(KeyLoc, RhsT.getNonLValueExprType(Self.Context), |
| 4869 | Expr::getValueKindForType(RhsT)); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 4870 | |
| 4871 | // Attempt the assignment in an unevaluated context within a SFINAE |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4872 | // trap at translation unit scope. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4873 | EnterExpressionEvaluationContext Unevaluated( |
| 4874 | Self, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4875 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 4876 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4877 | ExprResult Result = Self.BuildBinOp(/*S=*/nullptr, KeyLoc, BO_Assign, &Lhs, |
| 4878 | &Rhs); |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4879 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 4880 | return false; |
| 4881 | |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 4882 | if (BTT == BTT_IsAssignable) |
| 4883 | return true; |
| 4884 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4885 | if (BTT == BTT_IsNothrowAssignable) |
| 4886 | return Self.canThrow(Result.get()) == CT_Cannot; |
Douglas Gregor | 6bd56ca | 2012-06-29 00:49:17 +0000 | [diff] [blame] | 4887 | |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4888 | if (BTT == BTT_IsTriviallyAssignable) { |
Brian Kelley | 93c640b | 2017-03-29 17:40:35 +0000 | [diff] [blame] | 4889 | // Under Objective-C ARC and Weak, if the destination has non-trivial |
| 4890 | // Objective-C lifetime, this is a non-trivial assignment. |
| 4891 | if (LhsT.getNonReferenceType().hasNonTrivialObjCLifetime()) |
Alp Toker | 73287bf | 2014-01-20 00:24:09 +0000 | [diff] [blame] | 4892 | return false; |
| 4893 | |
| 4894 | return !Result.get()->hasNonTrivialCall(Self.Context); |
| 4895 | } |
| 4896 | |
| 4897 | llvm_unreachable("unhandled type trait"); |
| 4898 | return false; |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 4899 | } |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 4900 | default: llvm_unreachable("not a BTT"); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4901 | } |
| 4902 | llvm_unreachable("Unknown type trait or not implemented"); |
| 4903 | } |
| 4904 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4905 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
| 4906 | SourceLocation KWLoc, |
| 4907 | ParsedType Ty, |
| 4908 | Expr* DimExpr, |
| 4909 | SourceLocation RParen) { |
| 4910 | TypeSourceInfo *TSInfo; |
| 4911 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
| 4912 | if (!TSInfo) |
| 4913 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
| 4914 | |
| 4915 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
| 4916 | } |
| 4917 | |
| 4918 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
| 4919 | QualType T, Expr *DimExpr, |
| 4920 | SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4921 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4922 | |
| 4923 | switch(ATT) { |
| 4924 | case ATT_ArrayRank: |
| 4925 | if (T->isArrayType()) { |
| 4926 | unsigned Dim = 0; |
| 4927 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 4928 | ++Dim; |
| 4929 | T = AT->getElementType(); |
| 4930 | } |
| 4931 | return Dim; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4932 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4933 | return 0; |
| 4934 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4935 | case ATT_ArrayExtent: { |
| 4936 | llvm::APSInt Value; |
| 4937 | uint64_t Dim; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4938 | if (Self.VerifyIntegerConstantExpression(DimExpr, &Value, |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4939 | diag::err_dimension_expr_not_constant_integer, |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4940 | false).isInvalid()) |
| 4941 | return 0; |
| 4942 | if (Value.isSigned() && Value.isNegative()) { |
Daniel Dunbar | 900cead | 2012-03-09 21:38:22 +0000 | [diff] [blame] | 4943 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) |
| 4944 | << DimExpr->getSourceRange(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4945 | return 0; |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4946 | } |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4947 | Dim = Value.getLimitedValue(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4948 | |
| 4949 | if (T->isArrayType()) { |
| 4950 | unsigned D = 0; |
| 4951 | bool Matched = false; |
| 4952 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 4953 | if (Dim == D) { |
| 4954 | Matched = true; |
| 4955 | break; |
| 4956 | } |
| 4957 | ++D; |
| 4958 | T = AT->getElementType(); |
| 4959 | } |
| 4960 | |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4961 | if (Matched && T->isArrayType()) { |
| 4962 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
| 4963 | return CAT->getSize().getLimitedValue(); |
| 4964 | } |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4965 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 4966 | return 0; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4967 | } |
| 4968 | } |
| 4969 | llvm_unreachable("Unknown type trait or not implemented"); |
| 4970 | } |
| 4971 | |
| 4972 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
| 4973 | SourceLocation KWLoc, |
| 4974 | TypeSourceInfo *TSInfo, |
| 4975 | Expr* DimExpr, |
| 4976 | SourceLocation RParen) { |
| 4977 | QualType T = TSInfo->getType(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4978 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 4979 | // FIXME: This should likely be tracked as an APInt to remove any host |
| 4980 | // assumptions about the width of size_t on the target. |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 4981 | uint64_t Value = 0; |
| 4982 | if (!T->isDependentType()) |
| 4983 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); |
| 4984 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 4985 | // While the specification for these traits from the Embarcadero C++ |
| 4986 | // compiler's documentation says the return type is 'unsigned int', Clang |
| 4987 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero |
| 4988 | // compiler, there is no difference. On several other platforms this is an |
| 4989 | // important distinction. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4990 | return new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, DimExpr, |
| 4991 | RParen, Context.getSizeType()); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4992 | } |
| 4993 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4994 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 4995 | SourceLocation KWLoc, |
| 4996 | Expr *Queried, |
| 4997 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4998 | // If error parsing the expression, ignore. |
| 4999 | if (!Queried) |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5000 | return ExprError(); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5001 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5002 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5003 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5004 | return Result; |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5007 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
| 5008 | switch (ET) { |
| 5009 | case ET_IsLValueExpr: return E->isLValue(); |
| 5010 | case ET_IsRValueExpr: return E->isRValue(); |
| 5011 | } |
| 5012 | llvm_unreachable("Expression trait not covered by switch"); |
| 5013 | } |
| 5014 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5015 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5016 | SourceLocation KWLoc, |
| 5017 | Expr *Queried, |
| 5018 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5019 | if (Queried->isTypeDependent()) { |
| 5020 | // Delay type-checking for type-dependent expressions. |
| 5021 | } else if (Queried->getType()->isPlaceholderType()) { |
| 5022 | ExprResult PE = CheckPlaceholderExpr(Queried); |
| 5023 | if (PE.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5024 | return BuildExpressionTrait(ET, KWLoc, PE.get(), RParen); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 5027 | bool Value = EvaluateExpressionTrait(ET, Queried); |
Chandler Carruth | f57eba3 | 2011-05-01 08:48:19 +0000 | [diff] [blame] | 5028 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5029 | return new (Context) |
| 5030 | ExpressionTraitExpr(KWLoc, ET, Queried, Value, RParen, Context.BoolTy); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5033 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5034 | ExprValueKind &VK, |
| 5035 | SourceLocation Loc, |
| 5036 | bool isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5037 | assert(!LHS.get()->getType()->isPlaceholderType() && |
| 5038 | !RHS.get()->getType()->isPlaceholderType() && |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 5039 | "placeholders should have been weeded out by now"); |
| 5040 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 5041 | // The LHS undergoes lvalue conversions if this is ->*, and undergoes the |
| 5042 | // temporary materialization conversion otherwise. |
| 5043 | if (isIndirect) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5044 | LHS = DefaultLvalueConversion(LHS.get()); |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 5045 | else if (LHS.get()->isRValue()) |
| 5046 | LHS = TemporaryMaterializationConversion(LHS.get()); |
| 5047 | if (LHS.isInvalid()) |
| 5048 | return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 5049 | |
| 5050 | // The RHS always undergoes lvalue conversions. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5051 | RHS = DefaultLvalueConversion(RHS.get()); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5052 | if (RHS.isInvalid()) return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 5053 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5054 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 5055 | // C++ 5.5p2 |
| 5056 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 5057 | // be of type "pointer to member of T" (where T is a completely-defined |
| 5058 | // class type) [...] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5059 | QualType RHSType = RHS.get()->getType(); |
| 5060 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5061 | if (!MemPtr) { |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5062 | Diag(Loc, diag::err_bad_memptr_rhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5063 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5064 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5065 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5066 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5067 | QualType Class(MemPtr->getClass(), 0); |
| 5068 | |
Douglas Gregor | d07ba34 | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 5069 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
| 5070 | // member pointer points must be completely-defined. However, there is no |
| 5071 | // reason for this semantic distinction, and the rule is not enforced by |
| 5072 | // other compilers. Therefore, we do not check this property, as it is |
| 5073 | // likely to be considered a defect. |
Sebastian Redl | c72350e | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 5074 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5075 | // C++ 5.5p2 |
| 5076 | // [...] to its first operand, which shall be of class T or of a class of |
| 5077 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 5078 | // such a class] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5079 | QualType LHSType = LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5080 | if (isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5081 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) |
| 5082 | LHSType = Ptr->getPointeeType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5083 | else { |
| 5084 | Diag(Loc, diag::err_bad_memptr_lhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5085 | << OpSpelling << 1 << LHSType |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5086 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5087 | return QualType(); |
| 5088 | } |
| 5089 | } |
| 5090 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5091 | if (!Context.hasSameUnqualifiedType(Class, LHSType)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 5092 | // If we want to check the hierarchy, we need a complete type. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5093 | if (RequireCompleteType(Loc, LHSType, diag::err_bad_memptr_lhs, |
| 5094 | OpSpelling, (int)isIndirect)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 5095 | return QualType(); |
| 5096 | } |
Richard Smith | db05cd3 | 2013-12-12 03:40:18 +0000 | [diff] [blame] | 5097 | |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5098 | if (!IsDerivedFrom(Loc, LHSType, Class)) { |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5099 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5100 | << (int)isIndirect << LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5101 | return QualType(); |
| 5102 | } |
Richard Smith | db05cd3 | 2013-12-12 03:40:18 +0000 | [diff] [blame] | 5103 | |
| 5104 | CXXCastPath BasePath; |
| 5105 | if (CheckDerivedToBaseConversion(LHSType, Class, Loc, |
| 5106 | SourceRange(LHS.get()->getLocStart(), |
| 5107 | RHS.get()->getLocEnd()), |
| 5108 | &BasePath)) |
| 5109 | return QualType(); |
| 5110 | |
Eli Friedman | 1fcf66b | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 5111 | // Cast LHS to type of use. |
Richard Smith | 01e4a7f2 | 2017-06-09 22:25:28 +0000 | [diff] [blame] | 5112 | QualType UseType = Context.getQualifiedType(Class, LHSType.getQualifiers()); |
| 5113 | if (isIndirect) |
| 5114 | UseType = Context.getPointerType(UseType); |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5115 | ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5116 | LHS = ImpCastExprToType(LHS.get(), UseType, CK_DerivedToBase, VK, |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5117 | &BasePath); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5118 | } |
| 5119 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5120 | if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) { |
Fariborz Jahanian | 1bc0f9a | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 5121 | // Diagnose use of pointer-to-member type which when used as |
| 5122 | // the functional cast in a pointer-to-member expression. |
| 5123 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 5124 | return QualType(); |
| 5125 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5126 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5127 | // C++ 5.5p2 |
| 5128 | // The result is an object or a function of the type specified by the |
| 5129 | // second operand. |
| 5130 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 5131 | // in accordance with 5.5p5 and 5.2.5. |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5132 | QualType Result = MemPtr->getPointeeType(); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5133 | Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5134 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5135 | // C++0x [expr.mptr.oper]p6: |
| 5136 | // In a .* expression whose object expression is an rvalue, the program is |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5137 | // ill-formed if the second operand is a pointer to member function with |
| 5138 | // ref-qualifier &. In a ->* expression or in a .* expression whose object |
| 5139 | // 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] | 5140 | // is a pointer to member function with ref-qualifier &&. |
| 5141 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
| 5142 | switch (Proto->getRefQualifier()) { |
| 5143 | case RQ_None: |
| 5144 | // Do nothing |
| 5145 | break; |
| 5146 | |
| 5147 | case RQ_LValue: |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5148 | if (!isIndirect && !LHS.get()->Classify(Context).isLValue()) |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5149 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5150 | << RHSType << 1 << LHS.get()->getSourceRange(); |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5151 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5152 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5153 | case RQ_RValue: |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5154 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5155 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5156 | << RHSType << 0 << LHS.get()->getSourceRange(); |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 5157 | break; |
| 5158 | } |
| 5159 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5160 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5161 | // C++ [expr.mptr.oper]p6: |
| 5162 | // The result of a .* expression whose second operand is a pointer |
| 5163 | // to a data member is of the same value category as its |
| 5164 | // first operand. The result of a .* expression whose second |
| 5165 | // operand is a pointer to a member function is a prvalue. The |
| 5166 | // result of an ->* expression is an lvalue if its second operand |
| 5167 | // is a pointer to data member and a prvalue otherwise. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5168 | if (Result->isFunctionType()) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5169 | VK = VK_RValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5170 | return Context.BoundMemberTy; |
| 5171 | } else if (isIndirect) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5172 | VK = VK_LValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5173 | } else { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 5174 | VK = LHS.get()->getValueKind(); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5175 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5176 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 5177 | return Result; |
| 5178 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5179 | |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5180 | /// \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] | 5181 | /// |
| 5182 | /// This is part of the parameter validation for the ? operator. If either |
| 5183 | /// value operand is a class type, the two operands are attempted to be |
| 5184 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5185 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 5186 | /// as such. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5187 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 5188 | SourceLocation QuestionLoc, |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5189 | bool &HaveConversion, |
| 5190 | QualType &ToType) { |
| 5191 | HaveConversion = false; |
| 5192 | ToType = To->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5193 | |
| 5194 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5195 | SourceLocation()); |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5196 | // C++11 5.16p3 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5197 | // The process for determining whether an operand expression E1 of type T1 |
| 5198 | // can be converted to match an operand expression E2 of type T2 is defined |
| 5199 | // as follows: |
Richard Smith | 2414bca | 2016-04-25 19:30:37 +0000 | [diff] [blame] | 5200 | // -- If E2 is an lvalue: E1 can be converted to match E2 if E1 can be |
| 5201 | // implicitly converted to type "lvalue reference to T2", subject to the |
| 5202 | // constraint that in the conversion the reference must bind directly to |
| 5203 | // an lvalue. |
| 5204 | // -- If E2 is an xvalue: E1 can be converted to match E2 if E1 can be |
| 5205 | // implicitly conveted to the type "rvalue reference to R2", subject to |
| 5206 | // the constraint that the reference must bind directly. |
| 5207 | if (To->isLValue() || To->isXValue()) { |
| 5208 | QualType T = To->isLValue() ? Self.Context.getLValueReferenceType(ToType) |
| 5209 | : Self.Context.getRValueReferenceType(ToType); |
| 5210 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5211 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5212 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5213 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5214 | if (InitSeq.isDirectReferenceBinding()) { |
| 5215 | ToType = T; |
| 5216 | HaveConversion = true; |
| 5217 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5218 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5219 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5220 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5221 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5222 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5223 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5224 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 5225 | // -- if E1 and E2 have class type, and the underlying class types are |
| 5226 | // the same or one is a base class of the other: |
| 5227 | QualType FTy = From->getType(); |
| 5228 | QualType TTy = To->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5229 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 5230 | const RecordType *TRec = TTy->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5231 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5232 | Self.IsDerivedFrom(QuestionLoc, FTy, TTy); |
| 5233 | if (FRec && TRec && (FRec == TRec || FDerivedFromT || |
| 5234 | Self.IsDerivedFrom(QuestionLoc, TTy, FTy))) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5235 | // E1 can be converted to match E2 if the class of T2 is the |
| 5236 | // same type as, or a base class of, the class of T1, and |
| 5237 | // [cv2 > cv1]. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5238 | if (FRec == TRec || FDerivedFromT) { |
| 5239 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5240 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5241 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5242 | if (InitSeq) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5243 | HaveConversion = true; |
| 5244 | return false; |
| 5245 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5246 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5247 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5248 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5249 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5250 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5251 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5252 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5253 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5254 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5255 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 5256 | // implicitly converted to the type that expression E2 would have |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5257 | // 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] | 5258 | // an rvalue). |
| 5259 | // |
| 5260 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 5261 | // to the array-to-pointer or function-to-pointer conversions. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5262 | TTy = TTy.getNonLValueExprType(Self.Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5263 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5264 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5265 | InitializationSequence InitSeq(Self, Entity, Kind, From); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5266 | HaveConversion = !InitSeq.Failed(); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5267 | ToType = TTy; |
| 5268 | if (InitSeq.isAmbiguous()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5269 | return InitSeq.Diagnose(Self, Entity, Kind, From); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5270 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5271 | return false; |
| 5272 | } |
| 5273 | |
| 5274 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 5275 | /// |
| 5276 | /// This is part of the parameter validation for the ? operator. If either |
| 5277 | /// value operand is a class type, overload resolution is used to find a |
| 5278 | /// conversion to a common type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5279 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5280 | SourceLocation QuestionLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5281 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5282 | OverloadCandidateSet CandidateSet(QuestionLoc, |
| 5283 | OverloadCandidateSet::CSK_Operator); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 5284 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5285 | CandidateSet); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5286 | |
| 5287 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5288 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5289 | case OR_Success: { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5290 | // We found a match. Perform the conversions on the arguments and move on. |
George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 5291 | ExprResult LHSRes = Self.PerformImplicitConversion( |
| 5292 | LHS.get(), Best->BuiltinParamTypes[0], Best->Conversions[0], |
| 5293 | Sema::AA_Converting); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5294 | if (LHSRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5295 | break; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5296 | LHS = LHSRes; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5297 | |
George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 5298 | ExprResult RHSRes = Self.PerformImplicitConversion( |
| 5299 | RHS.get(), Best->BuiltinParamTypes[1], Best->Conversions[1], |
| 5300 | Sema::AA_Converting); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5301 | if (RHSRes.isInvalid()) |
| 5302 | break; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5303 | RHS = RHSRes; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 5304 | if (Best->Function) |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5305 | Self.MarkFunctionReferenced(QuestionLoc, Best->Function); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5306 | return false; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5307 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 5308 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5309 | case OR_No_Viable_Function: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5310 | |
| 5311 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5312 | // constant and the other is a pointer type. In this case, the user most |
| 5313 | // likely forgot to take the address of the other expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5314 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5315 | return true; |
| 5316 | |
| 5317 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5318 | << LHS.get()->getType() << RHS.get()->getType() |
| 5319 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5320 | return true; |
| 5321 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5322 | case OR_Ambiguous: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5323 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5324 | << LHS.get()->getType() << RHS.get()->getType() |
| 5325 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5326 | // FIXME: Print the possible common types by printing the return types of |
| 5327 | // the viable candidates. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5328 | break; |
| 5329 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5330 | case OR_Deleted: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5331 | llvm_unreachable("Conditional operator has only built-in overloads"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5332 | } |
| 5333 | return true; |
| 5334 | } |
| 5335 | |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5336 | /// \brief Perform an "extended" implicit conversion as returned by |
| 5337 | /// TryClassUnification. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5338 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5339 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5340 | InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5341 | SourceLocation()); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5342 | Expr *Arg = E.get(); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5343 | InitializationSequence InitSeq(Self, Entity, Kind, Arg); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5344 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, Arg); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5345 | if (Result.isInvalid()) |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5346 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5347 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5348 | E = Result; |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 5349 | return false; |
| 5350 | } |
| 5351 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5352 | /// \brief Check the operands of ?: under C++ semantics. |
| 5353 | /// |
| 5354 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 5355 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5356 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 5357 | ExprResult &RHS, ExprValueKind &VK, |
| 5358 | ExprObjectKind &OK, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5359 | SourceLocation QuestionLoc) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5360 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 5361 | // interface pointers. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5362 | |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5363 | // C++11 [expr.cond]p1 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5364 | // The first expression is contextually converted to bool. |
Simon Dardis | 7cd5876 | 2017-05-12 19:11:06 +0000 | [diff] [blame] | 5365 | // |
| 5366 | // FIXME; GCC's vector extension permits the use of a?b:c where the type of |
| 5367 | // a is that of a integer vector with the same number of elements and |
| 5368 | // size as the vectors of b and c. If one of either b or c is a scalar |
| 5369 | // it is implicitly converted to match the type of the vector. |
| 5370 | // Otherwise the expression is ill-formed. If both b and c are scalars, |
| 5371 | // then b and c are checked and converted to the type of a if possible. |
| 5372 | // Unlike the OpenCL ?: operator, the expression is evaluated as |
| 5373 | // (a[0] != 0 ? b[0] : c[0], .. , a[n] != 0 ? b[n] : c[n]). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5374 | if (!Cond.get()->isTypeDependent()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5375 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5376 | if (CondRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5377 | return QualType(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5378 | Cond = CondRes; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5381 | // Assume r-value. |
| 5382 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5383 | OK = OK_Ordinary; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5384 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5385 | // Either of the arguments dependent? |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5386 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5387 | return Context.DependentTy; |
| 5388 | |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5389 | // C++11 [expr.cond]p2 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5390 | // If either the second or the third operand has type (cv) void, ... |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5391 | QualType LTy = LHS.get()->getType(); |
| 5392 | QualType RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5393 | bool LVoid = LTy->isVoidType(); |
| 5394 | bool RVoid = RTy->isVoidType(); |
| 5395 | if (LVoid || RVoid) { |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 5396 | // ... one of the following shall hold: |
| 5397 | // -- The second or the third operand (but not both) is a (possibly |
| 5398 | // parenthesized) throw-expression; the result is of the type |
| 5399 | // and value category of the other. |
| 5400 | bool LThrow = isa<CXXThrowExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 5401 | bool RThrow = isa<CXXThrowExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 5402 | if (LThrow != RThrow) { |
| 5403 | Expr *NonThrow = LThrow ? RHS.get() : LHS.get(); |
| 5404 | VK = NonThrow->getValueKind(); |
| 5405 | // DR (no number yet): the result is a bit-field if the |
| 5406 | // non-throw-expression operand is a bit-field. |
| 5407 | OK = NonThrow->getObjectKind(); |
| 5408 | return NonThrow->getType(); |
Richard Smith | 45edb70 | 2012-08-07 22:06:48 +0000 | [diff] [blame] | 5409 | } |
| 5410 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5411 | // -- 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] | 5412 | // type void and is a prvalue. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5413 | if (LVoid && RVoid) |
| 5414 | return Context.VoidTy; |
| 5415 | |
| 5416 | // Neither holds, error. |
| 5417 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 5418 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5419 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5420 | return QualType(); |
| 5421 | } |
| 5422 | |
| 5423 | // Neither is void. |
| 5424 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5425 | // C++11 [expr.cond]p3 |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5426 | // Otherwise, if the second and third operand have different types, and |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5427 | // either has (cv) class type [...] an attempt is made to convert each of |
| 5428 | // those operands to the type of the other. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5429 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5430 | (LTy->isRecordType() || RTy->isRecordType())) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5431 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 5432 | QualType L2RType, R2LType; |
| 5433 | bool HaveL2R, HaveR2L; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5434 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5435 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5436 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5437 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5438 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5439 | // If both can be converted, [...] the program is ill-formed. |
| 5440 | if (HaveL2R && HaveR2L) { |
| 5441 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5442 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5443 | return QualType(); |
| 5444 | } |
| 5445 | |
| 5446 | // If exactly one conversion is possible, that conversion is applied to |
| 5447 | // the chosen operand and the converted operands are used in place of the |
| 5448 | // original operands for the remainder of this section. |
| 5449 | if (HaveL2R) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5450 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5451 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5452 | LTy = LHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5453 | } else if (HaveR2L) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5454 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5455 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5456 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5457 | } |
| 5458 | } |
| 5459 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5460 | // C++11 [expr.cond]p3 |
| 5461 | // if both are glvalues of the same value category and the same type except |
| 5462 | // for cv-qualification, an attempt is made to convert each of those |
| 5463 | // operands to the type of the other. |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5464 | // FIXME: |
| 5465 | // Resolving a defect in P0012R1: we extend this to cover all cases where |
| 5466 | // one of the operands is reference-compatible with the other, in order |
| 5467 | // to support conditionals between functions differing in noexcept. |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5468 | ExprValueKind LVK = LHS.get()->getValueKind(); |
| 5469 | ExprValueKind RVK = RHS.get()->getValueKind(); |
| 5470 | if (!Context.hasSameType(LTy, RTy) && |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5471 | LVK == RVK && LVK != VK_RValue) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5472 | // DerivedToBase was already handled by the class-specific case above. |
| 5473 | // FIXME: Should we allow ObjC conversions here? |
| 5474 | bool DerivedToBase, ObjCConversion, ObjCLifetimeConversion; |
| 5475 | if (CompareReferenceRelationship( |
| 5476 | QuestionLoc, LTy, RTy, DerivedToBase, |
| 5477 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5478 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
| 5479 | // [...] subject to the constraint that the reference must bind |
| 5480 | // directly [...] |
| 5481 | !RHS.get()->refersToBitField() && |
| 5482 | !RHS.get()->refersToVectorElement()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5483 | RHS = ImpCastExprToType(RHS.get(), LTy, CK_NoOp, RVK); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5484 | RTy = RHS.get()->getType(); |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5485 | } else if (CompareReferenceRelationship( |
| 5486 | QuestionLoc, RTy, LTy, DerivedToBase, |
| 5487 | ObjCConversion, ObjCLifetimeConversion) == Ref_Compatible && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5488 | !DerivedToBase && !ObjCConversion && !ObjCLifetimeConversion && |
| 5489 | !LHS.get()->refersToBitField() && |
| 5490 | !LHS.get()->refersToVectorElement()) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 5491 | LHS = ImpCastExprToType(LHS.get(), RTy, CK_NoOp, LVK); |
| 5492 | LTy = LHS.get()->getType(); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5493 | } |
| 5494 | } |
| 5495 | |
| 5496 | // C++11 [expr.cond]p4 |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5497 | // If the second and third operands are glvalues of the same value |
| 5498 | // category and have the same type, the result is of that type and |
| 5499 | // value category and it is a bit-field if the second or the third |
| 5500 | // operand is a bit-field, or if both are bit-fields. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5501 | // We only extend this to bitfields, not to the crazy other kinds of |
| 5502 | // l-values. |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 5503 | bool Same = Context.hasSameType(LTy, RTy); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5504 | if (Same && LVK == RVK && LVK != VK_RValue && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5505 | LHS.get()->isOrdinaryOrBitFieldObject() && |
| 5506 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
| 5507 | VK = LHS.get()->getValueKind(); |
| 5508 | if (LHS.get()->getObjectKind() == OK_BitField || |
| 5509 | RHS.get()->getObjectKind() == OK_BitField) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5510 | OK = OK_BitField; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5511 | |
| 5512 | // If we have function pointer types, unify them anyway to unify their |
| 5513 | // exception specifications, if any. |
| 5514 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
| 5515 | Qualifiers Qs = LTy.getQualifiers(); |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5516 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5517 | /*ConvertArgs*/false); |
| 5518 | LTy = Context.getQualifiedType(LTy, Qs); |
| 5519 | |
| 5520 | assert(!LTy.isNull() && "failed to find composite pointer type for " |
| 5521 | "canonically equivalent function ptr types"); |
| 5522 | assert(Context.hasSameType(LTy, RTy) && "bad composite pointer type"); |
| 5523 | } |
| 5524 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5525 | return LTy; |
Fariborz Jahanian | c60da03 | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 5526 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5527 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5528 | // C++11 [expr.cond]p5 |
| 5529 | // Otherwise, the result is a prvalue. If the second and third operands |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5530 | // do not have the same type, and either has (cv) class type, ... |
| 5531 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 5532 | // ... overload resolution is used to determine the conversions (if any) |
| 5533 | // to be applied to the operands. If the overload resolution fails, the |
| 5534 | // program is ill-formed. |
| 5535 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 5536 | return QualType(); |
| 5537 | } |
| 5538 | |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5539 | // C++11 [expr.cond]p6 |
| 5540 | // Lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5541 | // conversions are performed on the second and third operands. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5542 | LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); |
| 5543 | RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5544 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5545 | return QualType(); |
| 5546 | LTy = LHS.get()->getType(); |
| 5547 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5548 | |
| 5549 | // After those conversions, one of the following shall hold: |
| 5550 | // -- The second and third operands have the same type; the result |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5551 | // is of that type. If the operands have class type, the result |
| 5552 | // is a prvalue temporary of the result type, which is |
| 5553 | // copy-initialized from either the second operand or the third |
| 5554 | // operand depending on the value of the first operand. |
| 5555 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 5556 | if (LTy->isRecordType()) { |
| 5557 | // The operands have class type. Make a temporary copy. |
| 5558 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
David Blaikie | 6154ef9 | 2012-09-10 22:05:41 +0000 | [diff] [blame] | 5559 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5560 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
| 5561 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5562 | LHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5563 | if (LHSCopy.isInvalid()) |
| 5564 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5565 | |
| 5566 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
| 5567 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5568 | RHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5569 | if (RHSCopy.isInvalid()) |
| 5570 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5571 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5572 | LHS = LHSCopy; |
| 5573 | RHS = RHSCopy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5574 | } |
| 5575 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5576 | // If we have function pointer types, unify them anyway to unify their |
| 5577 | // exception specifications, if any. |
| 5578 | if (LTy->isFunctionPointerType() || LTy->isMemberFunctionPointerType()) { |
| 5579 | LTy = FindCompositePointerType(QuestionLoc, LHS, RHS); |
| 5580 | assert(!LTy.isNull() && "failed to find composite pointer type for " |
| 5581 | "canonically equivalent function ptr types"); |
| 5582 | } |
| 5583 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5584 | return LTy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 5585 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5586 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 5587 | // Extension: conditional operator involving vector types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5588 | if (LTy->isVectorType() || RTy->isVectorType()) |
Ulrich Weigand | 3c5038a | 2015-07-30 14:08:36 +0000 | [diff] [blame] | 5589 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false, |
| 5590 | /*AllowBothBool*/true, |
| 5591 | /*AllowBoolConversions*/false); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 5592 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5593 | // -- The second and third operands have arithmetic or enumeration type; |
| 5594 | // the usual arithmetic conversions are performed to bring them to a |
| 5595 | // common type, and the result is of that type. |
| 5596 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 5597 | QualType ResTy = UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5598 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5599 | return QualType(); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5600 | if (ResTy.isNull()) { |
| 5601 | Diag(QuestionLoc, |
| 5602 | diag::err_typecheck_cond_incompatible_operands) << LTy << RTy |
| 5603 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5604 | return QualType(); |
| 5605 | } |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 5606 | |
| 5607 | LHS = ImpCastExprToType(LHS.get(), ResTy, PrepareScalarCast(LHS, ResTy)); |
| 5608 | RHS = ImpCastExprToType(RHS.get(), ResTy, PrepareScalarCast(RHS, ResTy)); |
| 5609 | |
| 5610 | return ResTy; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | // -- The second and third operands have pointer type, or one has pointer |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5614 | // type and the other is a null pointer constant, or both are null |
| 5615 | // pointer constants, at least one of which is non-integral; pointer |
| 5616 | // conversions and qualification conversions are performed to bring them |
| 5617 | // to their composite pointer type. The result is of the composite |
| 5618 | // pointer type. |
Eli Friedman | 81390df | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 5619 | // -- The second and third operands have pointer to member type, or one has |
| 5620 | // pointer to member type and the other is a null pointer constant; |
| 5621 | // pointer to member conversions and qualification conversions are |
| 5622 | // performed to bring them to a common type, whose cv-qualification |
| 5623 | // shall match the cv-qualification of either the second or the third |
| 5624 | // operand. The result is of the common type. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5625 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS); |
| 5626 | if (!Composite.isNull()) |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5627 | return Composite; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5628 | |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 5629 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 798d2bd | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 5630 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 5631 | if (!Composite.isNull()) |
| 5632 | return Composite; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5633 | |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 5634 | // Check if we are using a null with a non-pointer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5635 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 5636 | return QualType(); |
| 5637 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5638 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5639 | << LHS.get()->getType() << RHS.get()->getType() |
| 5640 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5641 | return QualType(); |
| 5642 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5643 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5644 | static FunctionProtoType::ExceptionSpecInfo |
| 5645 | mergeExceptionSpecs(Sema &S, FunctionProtoType::ExceptionSpecInfo ESI1, |
| 5646 | FunctionProtoType::ExceptionSpecInfo ESI2, |
| 5647 | SmallVectorImpl<QualType> &ExceptionTypeStorage) { |
| 5648 | ExceptionSpecificationType EST1 = ESI1.Type; |
| 5649 | ExceptionSpecificationType EST2 = ESI2.Type; |
| 5650 | |
| 5651 | // If either of them can throw anything, that is the result. |
| 5652 | if (EST1 == EST_None) return ESI1; |
| 5653 | if (EST2 == EST_None) return ESI2; |
| 5654 | if (EST1 == EST_MSAny) return ESI1; |
| 5655 | if (EST2 == EST_MSAny) return ESI2; |
| 5656 | |
| 5657 | // If either of them is non-throwing, the result is the other. |
| 5658 | if (EST1 == EST_DynamicNone) return ESI2; |
| 5659 | if (EST2 == EST_DynamicNone) return ESI1; |
| 5660 | if (EST1 == EST_BasicNoexcept) return ESI2; |
| 5661 | if (EST2 == EST_BasicNoexcept) return ESI1; |
| 5662 | |
| 5663 | // If either of them is a non-value-dependent computed noexcept, that |
| 5664 | // determines the result. |
| 5665 | if (EST2 == EST_ComputedNoexcept && ESI2.NoexceptExpr && |
| 5666 | !ESI2.NoexceptExpr->isValueDependent()) |
| 5667 | return !ESI2.NoexceptExpr->EvaluateKnownConstInt(S.Context) ? ESI2 : ESI1; |
| 5668 | if (EST1 == EST_ComputedNoexcept && ESI1.NoexceptExpr && |
| 5669 | !ESI1.NoexceptExpr->isValueDependent()) |
| 5670 | return !ESI1.NoexceptExpr->EvaluateKnownConstInt(S.Context) ? ESI1 : ESI2; |
| 5671 | // If we're left with value-dependent computed noexcept expressions, we're |
| 5672 | // stuck. Before C++17, we can just drop the exception specification entirely, |
| 5673 | // since it's not actually part of the canonical type. And this should never |
| 5674 | // happen in C++17, because it would mean we were computing the composite |
| 5675 | // pointer type of dependent types, which should never happen. |
| 5676 | if (EST1 == EST_ComputedNoexcept || EST2 == EST_ComputedNoexcept) { |
| 5677 | assert(!S.getLangOpts().CPlusPlus1z && |
| 5678 | "computing composite pointer type of dependent types"); |
| 5679 | return FunctionProtoType::ExceptionSpecInfo(); |
| 5680 | } |
| 5681 | |
| 5682 | // Switch over the possibilities so that people adding new values know to |
| 5683 | // update this function. |
| 5684 | switch (EST1) { |
| 5685 | case EST_None: |
| 5686 | case EST_DynamicNone: |
| 5687 | case EST_MSAny: |
| 5688 | case EST_BasicNoexcept: |
| 5689 | case EST_ComputedNoexcept: |
| 5690 | llvm_unreachable("handled above"); |
| 5691 | |
| 5692 | case EST_Dynamic: { |
| 5693 | // This is the fun case: both exception specifications are dynamic. Form |
| 5694 | // the union of the two lists. |
| 5695 | assert(EST2 == EST_Dynamic && "other cases should already be handled"); |
| 5696 | llvm::SmallPtrSet<QualType, 8> Found; |
| 5697 | for (auto &Exceptions : {ESI1.Exceptions, ESI2.Exceptions}) |
| 5698 | for (QualType E : Exceptions) |
| 5699 | if (Found.insert(S.Context.getCanonicalType(E)).second) |
| 5700 | ExceptionTypeStorage.push_back(E); |
| 5701 | |
| 5702 | FunctionProtoType::ExceptionSpecInfo Result(EST_Dynamic); |
| 5703 | Result.Exceptions = ExceptionTypeStorage; |
| 5704 | return Result; |
| 5705 | } |
| 5706 | |
| 5707 | case EST_Unevaluated: |
| 5708 | case EST_Uninstantiated: |
| 5709 | case EST_Unparsed: |
| 5710 | llvm_unreachable("shouldn't see unresolved exception specifications here"); |
| 5711 | } |
| 5712 | |
| 5713 | llvm_unreachable("invalid ExceptionSpecificationType"); |
| 5714 | } |
| 5715 | |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5716 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 5717 | /// |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5718 | /// 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] | 5719 | /// 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] | 5720 | /// type and returns it. |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5721 | /// It does not emit diagnostics. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5722 | /// |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5723 | /// \param Loc The location of the operator requiring these two expressions to |
| 5724 | /// be converted to the composite pointer type. |
| 5725 | /// |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5726 | /// \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] | 5727 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5728 | Expr *&E1, Expr *&E2, |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5729 | bool ConvertArgs) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5730 | assert(getLangOpts().CPlusPlus && "This function assumes C++"); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5731 | |
| 5732 | // C++1z [expr]p14: |
| 5733 | // The composite pointer type of two operands p1 and p2 having types T1 |
| 5734 | // and T2 |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5735 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5736 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5737 | // where at least one is a pointer or pointer to member type or |
| 5738 | // std::nullptr_t is: |
| 5739 | bool T1IsPointerLike = T1->isAnyPointerType() || T1->isMemberPointerType() || |
| 5740 | T1->isNullPtrType(); |
| 5741 | bool T2IsPointerLike = T2->isAnyPointerType() || T2->isMemberPointerType() || |
| 5742 | T2->isNullPtrType(); |
| 5743 | if (!T1IsPointerLike && !T2IsPointerLike) |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5744 | return QualType(); |
Richard Smith | f2b084f | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5745 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5746 | // - if both p1 and p2 are null pointer constants, std::nullptr_t; |
| 5747 | // This can't actually happen, following the standard, but we also use this |
| 5748 | // to implement the end of [expr.conv], which hits this case. |
| 5749 | // |
| 5750 | // - if either p1 or p2 is a null pointer constant, T2 or T1, respectively; |
| 5751 | if (T1IsPointerLike && |
| 5752 | E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5753 | if (ConvertArgs) |
| 5754 | E2 = ImpCastExprToType(E2, T1, T1->isMemberPointerType() |
| 5755 | ? CK_NullToMemberPointer |
| 5756 | : CK_NullToPointer).get(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5757 | return T1; |
| 5758 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5759 | if (T2IsPointerLike && |
| 5760 | E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5761 | if (ConvertArgs) |
| 5762 | E1 = ImpCastExprToType(E1, T2, T2->isMemberPointerType() |
| 5763 | ? CK_NullToMemberPointer |
| 5764 | : CK_NullToPointer).get(); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5765 | return T2; |
| 5766 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5767 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5768 | // Now both have to be pointers or member pointers. |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5769 | if (!T1IsPointerLike || !T2IsPointerLike) |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5770 | return QualType(); |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5771 | assert(!T1->isNullPtrType() && !T2->isNullPtrType() && |
| 5772 | "nullptr_t should be a null pointer constant"); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5773 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5774 | // - if T1 or T2 is "pointer to cv1 void" and the other type is |
| 5775 | // "pointer to cv2 T", "pointer to cv12 void", where cv12 is |
| 5776 | // the union of cv1 and cv2; |
| 5777 | // - if T1 or T2 is "pointer to noexcept function" and the other type is |
| 5778 | // "pointer to function", where the function types are otherwise the same, |
| 5779 | // "pointer to function"; |
| 5780 | // FIXME: This rule is defective: it should also permit removing noexcept |
| 5781 | // from a pointer to member function. As a Clang extension, we also |
| 5782 | // permit removing 'noreturn', so we generalize this rule to; |
| 5783 | // - [Clang] If T1 and T2 are both of type "pointer to function" or |
| 5784 | // "pointer to member function" and the pointee types can be unified |
| 5785 | // by a function pointer conversion, that conversion is applied |
| 5786 | // before checking the following rules. |
| 5787 | // - if T1 is "pointer to cv1 C1" and T2 is "pointer to cv2 C2", where C1 |
| 5788 | // is reference-related to C2 or C2 is reference-related to C1 (8.6.3), |
| 5789 | // the cv-combined type of T1 and T2 or the cv-combined type of T2 and T1, |
| 5790 | // respectively; |
| 5791 | // - if T1 is "pointer to member of C1 of type cv1 U1" and T2 is "pointer |
| 5792 | // to member of C2 of type cv2 U2" where C1 is reference-related to C2 or |
| 5793 | // C2 is reference-related to C1 (8.6.3), the cv-combined type of T2 and |
| 5794 | // T1 or the cv-combined type of T1 and T2, respectively; |
| 5795 | // - if T1 and T2 are similar types (4.5), the cv-combined type of T1 and |
| 5796 | // T2; |
| 5797 | // |
| 5798 | // If looked at in the right way, these bullets all do the same thing. |
| 5799 | // What we do here is, we build the two possible cv-combined types, and try |
| 5800 | // the conversions in both directions. If only one works, or if the two |
| 5801 | // composite types are the same, we have succeeded. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5802 | // FIXME: extended qualifiers? |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5803 | // |
| 5804 | // Note that this will fail to find a composite pointer type for "pointer |
| 5805 | // to void" and "pointer to function". We can't actually perform the final |
| 5806 | // conversion in this case, even though a composite pointer type formally |
| 5807 | // exists. |
| 5808 | SmallVector<unsigned, 4> QualifierUnion; |
| 5809 | SmallVector<std::pair<const Type *, const Type *>, 4> MemberOfClass; |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5810 | QualType Composite1 = T1; |
| 5811 | QualType Composite2 = T2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5812 | unsigned NeedConstBefore = 0; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5813 | while (true) { |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5814 | const PointerType *Ptr1, *Ptr2; |
| 5815 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 5816 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 5817 | Composite1 = Ptr1->getPointeeType(); |
| 5818 | Composite2 = Ptr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5819 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5820 | // 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] | 5821 | // of where we need to fill in additional 'const' qualifiers. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5822 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5823 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5824 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5825 | QualifierUnion.push_back( |
| 5826 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5827 | MemberOfClass.push_back(std::make_pair(nullptr, nullptr)); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5828 | continue; |
| 5829 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5830 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5831 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 5832 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 5833 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 5834 | Composite1 = MemPtr1->getPointeeType(); |
| 5835 | Composite2 = MemPtr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5836 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5837 | // 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] | 5838 | // of where we need to fill in additional 'const' qualifiers. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5839 | if (Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5840 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5841 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5842 | QualifierUnion.push_back( |
| 5843 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 5844 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 5845 | MemPtr2->getClass())); |
| 5846 | continue; |
| 5847 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5848 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5849 | // FIXME: block pointer types? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5850 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5851 | // Cannot unwrap any more types. |
| 5852 | break; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5853 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5854 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5855 | // Apply the function pointer conversion to unify the types. We've already |
| 5856 | // unwrapped down to the function types, and we want to merge rather than |
| 5857 | // just convert, so do this ourselves rather than calling |
| 5858 | // IsFunctionConversion. |
| 5859 | // |
| 5860 | // FIXME: In order to match the standard wording as closely as possible, we |
| 5861 | // currently only do this under a single level of pointers. Ideally, we would |
| 5862 | // allow this in general, and set NeedConstBefore to the relevant depth on |
| 5863 | // the side(s) where we changed anything. |
| 5864 | if (QualifierUnion.size() == 1) { |
| 5865 | if (auto *FPT1 = Composite1->getAs<FunctionProtoType>()) { |
| 5866 | if (auto *FPT2 = Composite2->getAs<FunctionProtoType>()) { |
| 5867 | FunctionProtoType::ExtProtoInfo EPI1 = FPT1->getExtProtoInfo(); |
| 5868 | FunctionProtoType::ExtProtoInfo EPI2 = FPT2->getExtProtoInfo(); |
| 5869 | |
| 5870 | // The result is noreturn if both operands are. |
| 5871 | bool Noreturn = |
| 5872 | EPI1.ExtInfo.getNoReturn() && EPI2.ExtInfo.getNoReturn(); |
| 5873 | EPI1.ExtInfo = EPI1.ExtInfo.withNoReturn(Noreturn); |
| 5874 | EPI2.ExtInfo = EPI2.ExtInfo.withNoReturn(Noreturn); |
| 5875 | |
| 5876 | // The result is nothrow if both operands are. |
| 5877 | SmallVector<QualType, 8> ExceptionTypeStorage; |
| 5878 | EPI1.ExceptionSpec = EPI2.ExceptionSpec = |
| 5879 | mergeExceptionSpecs(*this, EPI1.ExceptionSpec, EPI2.ExceptionSpec, |
| 5880 | ExceptionTypeStorage); |
| 5881 | |
| 5882 | Composite1 = Context.getFunctionType(FPT1->getReturnType(), |
| 5883 | FPT1->getParamTypes(), EPI1); |
| 5884 | Composite2 = Context.getFunctionType(FPT2->getReturnType(), |
| 5885 | FPT2->getParamTypes(), EPI2); |
| 5886 | } |
| 5887 | } |
| 5888 | } |
| 5889 | |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5890 | if (NeedConstBefore) { |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5891 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5892 | // mismatch, so that our (non-standard!) composite type meets the |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5893 | // requirements of C++ [conv.qual]p4 bullet 3. |
Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 5894 | for (unsigned I = 0; I != NeedConstBefore; ++I) |
| 5895 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5896 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5897 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5898 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5899 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5900 | auto MOC = MemberOfClass.rbegin(); |
| 5901 | for (unsigned CVR : llvm::reverse(QualifierUnion)) { |
| 5902 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 5903 | auto Classes = *MOC++; |
| 5904 | if (Classes.first && Classes.second) { |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5905 | // Rebuild member pointer type |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5906 | Composite1 = Context.getMemberPointerType( |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5907 | Context.getQualifiedType(Composite1, Quals), Classes.first); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5908 | Composite2 = Context.getMemberPointerType( |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5909 | Context.getQualifiedType(Composite2, Quals), Classes.second); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5910 | } else { |
| 5911 | // Rebuild pointer type |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5912 | Composite1 = |
| 5913 | Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 5914 | Composite2 = |
| 5915 | Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5916 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5917 | } |
| 5918 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5919 | struct Conversion { |
| 5920 | Sema &S; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5921 | Expr *&E1, *&E2; |
| 5922 | QualType Composite; |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 5923 | InitializedEntity Entity; |
| 5924 | InitializationKind Kind; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5925 | InitializationSequence E1ToC, E2ToC; |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 5926 | bool Viable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5927 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5928 | Conversion(Sema &S, SourceLocation Loc, Expr *&E1, Expr *&E2, |
| 5929 | QualType Composite) |
Richard Smith | e38da03 | 2016-10-20 07:53:17 +0000 | [diff] [blame] | 5930 | : S(S), E1(E1), E2(E2), Composite(Composite), |
| 5931 | Entity(InitializedEntity::InitializeTemporary(Composite)), |
| 5932 | Kind(InitializationKind::CreateCopy(Loc, SourceLocation())), |
| 5933 | E1ToC(S, Entity, Kind, E1), E2ToC(S, Entity, Kind, E2), |
| 5934 | Viable(E1ToC && E2ToC) {} |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5935 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5936 | bool perform() { |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5937 | ExprResult E1Result = E1ToC.Perform(S, Entity, Kind, E1); |
| 5938 | if (E1Result.isInvalid()) |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5939 | return true; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5940 | E1 = E1Result.getAs<Expr>(); |
| 5941 | |
| 5942 | ExprResult E2Result = E2ToC.Perform(S, Entity, Kind, E2); |
| 5943 | if (E2Result.isInvalid()) |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5944 | return true; |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5945 | E2 = E2Result.getAs<Expr>(); |
| 5946 | |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5947 | return false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5948 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5949 | }; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5950 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5951 | // Try to convert to each composite pointer type. |
| 5952 | Conversion C1(*this, Loc, E1, E2, Composite1); |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5953 | if (C1.Viable && Context.hasSameType(Composite1, Composite2)) { |
| 5954 | if (ConvertArgs && C1.perform()) |
| 5955 | return QualType(); |
| 5956 | return C1.Composite; |
| 5957 | } |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5958 | Conversion C2(*this, Loc, E1, E2, Composite2); |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5959 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5960 | if (C1.Viable == C2.Viable) { |
| 5961 | // Either Composite1 and Composite2 are viable and are different, or |
| 5962 | // neither is viable. |
| 5963 | // FIXME: How both be viable and different? |
| 5964 | return QualType(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5965 | } |
| 5966 | |
Richard Smith | 6ffdb1f | 2016-10-20 01:20:00 +0000 | [diff] [blame] | 5967 | // Convert to the chosen type. |
Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 5968 | if (ConvertArgs && (C1.Viable ? C1 : C2).perform()) |
| 5969 | return QualType(); |
| 5970 | |
| 5971 | return C1.Viable ? C1.Composite : C2.Composite; |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 5972 | } |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 5973 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5974 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Douglas Gregor | 298087b | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 5975 | if (!E) |
| 5976 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5977 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5978 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 5979 | |
| 5980 | // If the result is a glvalue, we shouldn't bind it. |
| 5981 | if (!E->isRValue()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5982 | return E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5983 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5984 | // In ARC, calls that return a retainable type can return retained, |
| 5985 | // in which case we have to insert a consuming cast. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5986 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5987 | E->getType()->isObjCRetainableType()) { |
| 5988 | |
| 5989 | bool ReturnsRetained; |
| 5990 | |
| 5991 | // For actual calls, we compute this by examining the type of the |
| 5992 | // called value. |
| 5993 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { |
| 5994 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
| 5995 | QualType T = Callee->getType(); |
| 5996 | |
| 5997 | if (T == Context.BoundMemberTy) { |
| 5998 | // Handle pointer-to-members. |
| 5999 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) |
| 6000 | T = BinOp->getRHS()->getType(); |
| 6001 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) |
| 6002 | T = Mem->getMemberDecl()->getType(); |
| 6003 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6004 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6005 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
| 6006 | T = Ptr->getPointeeType(); |
| 6007 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
| 6008 | T = Ptr->getPointeeType(); |
| 6009 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
| 6010 | T = MemPtr->getPointeeType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6011 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6012 | const FunctionType *FTy = T->getAs<FunctionType>(); |
| 6013 | assert(FTy && "call to value not of function type?"); |
| 6014 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
| 6015 | |
| 6016 | // ActOnStmtExpr arranges things so that StmtExprs of retainable |
| 6017 | // type always produce a +1 object. |
| 6018 | } else if (isa<StmtExpr>(E)) { |
| 6019 | ReturnsRetained = true; |
| 6020 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6021 | // We hit this case with the lambda conversion-to-block optimization; |
| 6022 | // we don't want any extra casts here. |
| 6023 | } else if (isa<CastExpr>(E) && |
| 6024 | isa<BlockExpr>(cast<CastExpr>(E)->getSubExpr())) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6025 | return E; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6026 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6027 | // For message sends and property references, we try to find an |
| 6028 | // actual method. FIXME: we should infer retention by selector in |
| 6029 | // cases where we don't have an actual method. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6030 | } else { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6031 | ObjCMethodDecl *D = nullptr; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6032 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { |
| 6033 | D = Send->getMethodDecl(); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 6034 | } else if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(E)) { |
| 6035 | D = BoxedExpr->getBoxingMethod(); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6036 | } else if (ObjCArrayLiteral *ArrayLit = dyn_cast<ObjCArrayLiteral>(E)) { |
Akira Hatanaka | 4d53a1c | 2017-04-15 06:42:00 +0000 | [diff] [blame] | 6037 | // Don't do reclaims if we're using the zero-element array |
| 6038 | // constant. |
| 6039 | if (ArrayLit->getNumElements() == 0 && |
| 6040 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
| 6041 | return E; |
| 6042 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6043 | D = ArrayLit->getArrayWithObjectsMethod(); |
| 6044 | } else if (ObjCDictionaryLiteral *DictLit |
| 6045 | = dyn_cast<ObjCDictionaryLiteral>(E)) { |
Akira Hatanaka | 4d53a1c | 2017-04-15 06:42:00 +0000 | [diff] [blame] | 6046 | // Don't do reclaims if we're using the zero-element dictionary |
| 6047 | // constant. |
| 6048 | if (DictLit->getNumElements() == 0 && |
| 6049 | Context.getLangOpts().ObjCRuntime.hasEmptyCollections()) |
| 6050 | return E; |
| 6051 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6052 | D = DictLit->getDictWithObjectsMethod(); |
| 6053 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6054 | |
| 6055 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
John McCall | 32a4da0 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 6056 | |
| 6057 | // Don't do reclaims on performSelector calls; despite their |
| 6058 | // return type, the invoked method doesn't necessarily actually |
| 6059 | // return an object. |
| 6060 | if (!ReturnsRetained && |
| 6061 | D && D->getMethodFamily() == OMF_performSelector) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6062 | return E; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6063 | } |
| 6064 | |
John McCall | 16de4d2 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 6065 | // Don't reclaim an object of Class type. |
| 6066 | if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6067 | return E; |
John McCall | 16de4d2 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 6068 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6069 | Cleanup.setExprNeedsCleanups(true); |
John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 6070 | |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6071 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject |
| 6072 | : CK_ARCReclaimReturnedObject); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6073 | return ImplicitCastExpr::Create(Context, E->getType(), ck, E, nullptr, |
| 6074 | VK_RValue); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6075 | } |
| 6076 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6077 | if (!getLangOpts().CPlusPlus) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6078 | return E; |
Douglas Gregor | 363b151 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 6079 | |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6080 | // Search for the base element type (cf. ASTContext::getBaseElementType) with |
| 6081 | // a fast path for the common case that the type is directly a RecordType. |
| 6082 | const Type *T = Context.getCanonicalType(E->getType().getTypePtr()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6083 | const RecordType *RT = nullptr; |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6084 | while (!RT) { |
| 6085 | switch (T->getTypeClass()) { |
| 6086 | case Type::Record: |
| 6087 | RT = cast<RecordType>(T); |
| 6088 | break; |
| 6089 | case Type::ConstantArray: |
| 6090 | case Type::IncompleteArray: |
| 6091 | case Type::VariableArray: |
| 6092 | case Type::DependentSizedArray: |
| 6093 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
| 6094 | break; |
| 6095 | default: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6096 | return E; |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 6097 | } |
| 6098 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6099 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6100 | // That should be enough to guarantee that this type is complete, if we're |
| 6101 | // not processing a decltype expression. |
Jeffrey Yasskin | bbc4eea | 2011-01-27 19:17:54 +0000 | [diff] [blame] | 6102 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6103 | if (RD->isInvalidDecl() || RD->isDependentContext()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6104 | return E; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6105 | |
| 6106 | bool IsDecltype = ExprEvalContexts.back().IsDecltype; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6107 | CXXDestructorDecl *Destructor = IsDecltype ? nullptr : LookupDestructor(RD); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6108 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6109 | if (Destructor) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6110 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
John McCall | 8e36d53 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 6111 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 6112 | PDiag(diag::err_access_dtor_temp) |
| 6113 | << E->getType()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6114 | if (DiagnoseUseOfDecl(Destructor, E->getExprLoc())) |
| 6115 | return ExprError(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6116 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6117 | // If destructor is trivial, we can avoid the extra copy. |
| 6118 | if (Destructor->isTrivial()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6119 | return E; |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6120 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6121 | // 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] | 6122 | Cleanup.setExprNeedsCleanups(true); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6123 | } |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 6124 | |
| 6125 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6126 | CXXBindTemporaryExpr *Bind = CXXBindTemporaryExpr::Create(Context, Temp, E); |
| 6127 | |
| 6128 | if (IsDecltype) |
| 6129 | ExprEvalContexts.back().DelayedDecltypeBinds.push_back(Bind); |
| 6130 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6131 | return Bind; |
Anders Carlsson | 2d4cada | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 6132 | } |
| 6133 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6134 | ExprResult |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6135 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6136 | if (SubExpr.isInvalid()) |
| 6137 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6138 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6139 | return MaybeCreateExprWithCleanups(SubExpr.get()); |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6142 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 6143 | assert(SubExpr && "subexpression can't be null!"); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6144 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 6145 | CleanupVarDeclMarking(); |
| 6146 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6147 | unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects; |
| 6148 | assert(ExprCleanupObjects.size() >= FirstCleanup); |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6149 | assert(Cleanup.exprNeedsCleanups() || |
| 6150 | ExprCleanupObjects.size() == FirstCleanup); |
| 6151 | if (!Cleanup.exprNeedsCleanups()) |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6152 | return SubExpr; |
| 6153 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6154 | auto Cleanups = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup, |
| 6155 | ExprCleanupObjects.size() - FirstCleanup); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6156 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6157 | auto *E = ExprWithCleanups::Create( |
| 6158 | Context, SubExpr, Cleanup.cleanupsHaveSideEffects(), Cleanups); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6159 | DiscardCleanupsInEvaluationContext(); |
| 6160 | |
| 6161 | return E; |
| 6162 | } |
| 6163 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6164 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 6165 | assert(SubStmt && "sub-statement can't be null!"); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6166 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 6167 | CleanupVarDeclMarking(); |
| 6168 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 6169 | if (!Cleanup.exprNeedsCleanups()) |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6170 | return SubStmt; |
| 6171 | |
| 6172 | // FIXME: In order to attach the temporaries, wrap the statement into |
| 6173 | // a StmtExpr; currently this is only used for asm statements. |
| 6174 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
| 6175 | // a new AsmStmtWithTemporaries. |
Nico Weber | a2a0eb9 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 6176 | CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, SubStmt, |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6177 | SourceLocation(), |
| 6178 | SourceLocation()); |
| 6179 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
| 6180 | SourceLocation()); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6181 | return MaybeCreateExprWithCleanups(E); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 6182 | } |
| 6183 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6184 | /// Process the expression contained within a decltype. For such expressions, |
| 6185 | /// certain semantic checks on temporaries are delayed until this point, and |
| 6186 | /// are omitted for the 'topmost' call in the decltype expression. If the |
| 6187 | /// topmost call bound a temporary, strip that temporary off the expression. |
| 6188 | ExprResult Sema::ActOnDecltypeExpression(Expr *E) { |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6189 | assert(ExprEvalContexts.back().IsDecltype && "not in a decltype expression"); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6190 | |
| 6191 | // C++11 [expr.call]p11: |
| 6192 | // If a function call is a prvalue of object type, |
| 6193 | // -- if the function call is either |
| 6194 | // -- the operand of a decltype-specifier, or |
| 6195 | // -- the right operand of a comma operator that is the operand of a |
| 6196 | // decltype-specifier, |
| 6197 | // a temporary object is not introduced for the prvalue. |
| 6198 | |
| 6199 | // Recursively rebuild ParenExprs and comma expressions to strip out the |
| 6200 | // outermost CXXBindTemporaryExpr, if any. |
| 6201 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 6202 | ExprResult SubExpr = ActOnDecltypeExpression(PE->getSubExpr()); |
| 6203 | if (SubExpr.isInvalid()) |
| 6204 | return ExprError(); |
| 6205 | if (SubExpr.get() == PE->getSubExpr()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6206 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6207 | return ActOnParenExpr(PE->getLParen(), PE->getRParen(), SubExpr.get()); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6208 | } |
| 6209 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 6210 | if (BO->getOpcode() == BO_Comma) { |
| 6211 | ExprResult RHS = ActOnDecltypeExpression(BO->getRHS()); |
| 6212 | if (RHS.isInvalid()) |
| 6213 | return ExprError(); |
| 6214 | if (RHS.get() == BO->getRHS()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6215 | return E; |
| 6216 | return new (Context) BinaryOperator( |
| 6217 | BO->getLHS(), RHS.get(), BO_Comma, BO->getType(), BO->getValueKind(), |
Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 6218 | BO->getObjectKind(), BO->getOperatorLoc(), BO->getFPFeatures()); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6219 | } |
| 6220 | } |
| 6221 | |
| 6222 | CXXBindTemporaryExpr *TopBind = dyn_cast<CXXBindTemporaryExpr>(E); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6223 | CallExpr *TopCall = TopBind ? dyn_cast<CallExpr>(TopBind->getSubExpr()) |
| 6224 | : nullptr; |
Richard Smith | 202dc13 | 2014-02-18 03:51:47 +0000 | [diff] [blame] | 6225 | if (TopCall) |
| 6226 | E = TopCall; |
| 6227 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6228 | TopBind = nullptr; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6229 | |
| 6230 | // Disable the special decltype handling now. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6231 | ExprEvalContexts.back().IsDecltype = false; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6232 | |
Richard Smith | f86b0ae | 2012-07-28 19:54:11 +0000 | [diff] [blame] | 6233 | // In MS mode, don't perform any extra checking of call return types within a |
| 6234 | // decltype expression. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 6235 | if (getLangOpts().MSVCCompat) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6236 | return E; |
Richard Smith | f86b0ae | 2012-07-28 19:54:11 +0000 | [diff] [blame] | 6237 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6238 | // Perform the semantic checks we delayed until this point. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6239 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeCalls.size(); |
| 6240 | I != N; ++I) { |
| 6241 | CallExpr *Call = ExprEvalContexts.back().DelayedDecltypeCalls[I]; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6242 | if (Call == TopCall) |
| 6243 | continue; |
| 6244 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6245 | if (CheckCallReturnType(Call->getCallReturnType(Context), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6246 | Call->getLocStart(), |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6247 | Call, Call->getDirectCallee())) |
| 6248 | return ExprError(); |
| 6249 | } |
| 6250 | |
| 6251 | // Now all relevant types are complete, check the destructors are accessible |
| 6252 | // and non-deleted, and annotate them on the temporaries. |
Benjamin Kramer | 671f4c0 | 2012-11-15 15:18:42 +0000 | [diff] [blame] | 6253 | for (unsigned I = 0, N = ExprEvalContexts.back().DelayedDecltypeBinds.size(); |
| 6254 | I != N; ++I) { |
| 6255 | CXXBindTemporaryExpr *Bind = |
| 6256 | ExprEvalContexts.back().DelayedDecltypeBinds[I]; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6257 | if (Bind == TopBind) |
| 6258 | continue; |
| 6259 | |
| 6260 | CXXTemporary *Temp = Bind->getTemporary(); |
| 6261 | |
| 6262 | CXXRecordDecl *RD = |
| 6263 | Bind->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6264 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
| 6265 | Temp->setDestructor(Destructor); |
| 6266 | |
Richard Smith | 7d847b1 | 2012-05-11 22:20:10 +0000 | [diff] [blame] | 6267 | MarkFunctionReferenced(Bind->getExprLoc(), Destructor); |
| 6268 | CheckDestructorAccess(Bind->getExprLoc(), Destructor, |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6269 | PDiag(diag::err_access_dtor_temp) |
Richard Smith | 7d847b1 | 2012-05-11 22:20:10 +0000 | [diff] [blame] | 6270 | << Bind->getType()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6271 | if (DiagnoseUseOfDecl(Destructor, Bind->getExprLoc())) |
| 6272 | return ExprError(); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6273 | |
| 6274 | // 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] | 6275 | Cleanup.setExprNeedsCleanups(true); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6276 | } |
| 6277 | |
| 6278 | // Possibly strip off the top CXXBindTemporaryExpr. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6279 | return E; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 6280 | } |
| 6281 | |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6282 | /// Note a set of 'operator->' functions that were used for a member access. |
| 6283 | static void noteOperatorArrows(Sema &S, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 6284 | ArrayRef<FunctionDecl *> OperatorArrows) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6285 | unsigned SkipStart = OperatorArrows.size(), SkipCount = 0; |
| 6286 | // FIXME: Make this configurable? |
| 6287 | unsigned Limit = 9; |
| 6288 | if (OperatorArrows.size() > Limit) { |
| 6289 | // Produce Limit-1 normal notes and one 'skipping' note. |
| 6290 | SkipStart = (Limit - 1) / 2 + (Limit - 1) % 2; |
| 6291 | SkipCount = OperatorArrows.size() - (Limit - 1); |
| 6292 | } |
| 6293 | |
| 6294 | for (unsigned I = 0; I < OperatorArrows.size(); /**/) { |
| 6295 | if (I == SkipStart) { |
| 6296 | S.Diag(OperatorArrows[I]->getLocation(), |
| 6297 | diag::note_operator_arrows_suppressed) |
| 6298 | << SkipCount; |
| 6299 | I += SkipCount; |
| 6300 | } else { |
| 6301 | S.Diag(OperatorArrows[I]->getLocation(), diag::note_operator_arrow_here) |
| 6302 | << OperatorArrows[I]->getCallResultType(); |
| 6303 | ++I; |
| 6304 | } |
| 6305 | } |
| 6306 | } |
| 6307 | |
Nico Weber | 964d332 | 2015-02-16 22:35:45 +0000 | [diff] [blame] | 6308 | ExprResult Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, |
| 6309 | SourceLocation OpLoc, |
| 6310 | tok::TokenKind OpKind, |
| 6311 | ParsedType &ObjectType, |
| 6312 | bool &MayBePseudoDestructor) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6313 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6314 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6315 | if (Result.isInvalid()) return ExprError(); |
| 6316 | Base = Result.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6317 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6318 | Result = CheckPlaceholderExpr(Base); |
| 6319 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6320 | Base = Result.get(); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6321 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6322 | QualType BaseType = Base->getType(); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6323 | MayBePseudoDestructor = false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6324 | if (BaseType->isDependentType()) { |
Douglas Gregor | 4112718 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 6325 | // If we have a pointer to a dependent type and are using the -> operator, |
| 6326 | // the object type is the type that the pointer points to. We might still |
| 6327 | // have enough information about that type to do something useful. |
| 6328 | if (OpKind == tok::arrow) |
| 6329 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 6330 | BaseType = Ptr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6331 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6332 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6333 | MayBePseudoDestructor = true; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6334 | return Base; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6335 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6336 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6337 | // C++ [over.match.oper]p8: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6338 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6339 | // returned, with the original second operand. |
| 6340 | if (OpKind == tok::arrow) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6341 | QualType StartingType = BaseType; |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6342 | bool NoArrowOperatorFound = false; |
| 6343 | bool FirstIteration = true; |
| 6344 | FunctionDecl *CurFD = dyn_cast<FunctionDecl>(CurContext); |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 6345 | // The set of types we've considered so far. |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 6346 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6347 | SmallVector<FunctionDecl*, 8> OperatorArrows; |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 6348 | CTypes.insert(Context.getCanonicalType(BaseType)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6349 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6350 | while (BaseType->isRecordType()) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6351 | if (OperatorArrows.size() >= getLangOpts().ArrowDepth) { |
| 6352 | Diag(OpLoc, diag::err_operator_arrow_depth_exceeded) |
Richard Smith | 9dbc574 | 2013-11-06 19:43:09 +0000 | [diff] [blame] | 6353 | << StartingType << getLangOpts().ArrowDepth << Base->getSourceRange(); |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6354 | noteOperatorArrows(*this, OperatorArrows); |
| 6355 | Diag(OpLoc, diag::note_operator_arrow_depth) |
| 6356 | << getLangOpts().ArrowDepth; |
| 6357 | return ExprError(); |
| 6358 | } |
| 6359 | |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6360 | Result = BuildOverloadedArrowExpr( |
| 6361 | S, Base, OpLoc, |
| 6362 | // When in a template specialization and on the first loop iteration, |
| 6363 | // potentially give the default diagnostic (with the fixit in a |
| 6364 | // separate note) instead of having the error reported back to here |
| 6365 | // and giving a diagnostic with a fixit attached to the error itself. |
| 6366 | (FirstIteration && CurFD && CurFD->isFunctionTemplateSpecialization()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6367 | ? nullptr |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6368 | : &NoArrowOperatorFound); |
| 6369 | if (Result.isInvalid()) { |
| 6370 | if (NoArrowOperatorFound) { |
| 6371 | if (FirstIteration) { |
| 6372 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Richard Smith | 9dbc574 | 2013-11-06 19:43:09 +0000 | [diff] [blame] | 6373 | << BaseType << 1 << Base->getSourceRange() |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6374 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 6375 | OpKind = tok::period; |
| 6376 | break; |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 6377 | } |
| 6378 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 6379 | << BaseType << Base->getSourceRange(); |
| 6380 | CallExpr *CE = dyn_cast<CallExpr>(Base); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6381 | if (Decl *CD = (CE ? CE->getCalleeDecl() : nullptr)) { |
Kaelyn Uhrain | 957c8b1 | 2013-07-31 20:16:17 +0000 | [diff] [blame] | 6382 | Diag(CD->getLocStart(), |
| 6383 | diag::note_member_reference_arrow_from_operator_arrow); |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6384 | } |
| 6385 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6386 | return ExprError(); |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6387 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6388 | Base = Result.get(); |
| 6389 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6390 | OperatorArrows.push_back(OpCall->getDirectCallee()); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6391 | BaseType = Base->getType(); |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 6392 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6393 | if (!CTypes.insert(CBaseType).second) { |
Richard Smith | 79c927b | 2013-11-06 19:31:51 +0000 | [diff] [blame] | 6394 | Diag(OpLoc, diag::err_operator_arrow_circular) << StartingType; |
| 6395 | noteOperatorArrows(*this, OperatorArrows); |
Fariborz Jahanian | 10ce958 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 6396 | return ExprError(); |
| 6397 | } |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6398 | FirstIteration = false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6399 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6400 | |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 6401 | if (OpKind == tok::arrow && |
| 6402 | (BaseType->isPointerType() || BaseType->isObjCObjectPointerType())) |
Douglas Gregor | e4f764f | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 6403 | BaseType = BaseType->getPointeeType(); |
| 6404 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6405 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6406 | // Objective-C properties allow "." access on Objective-C pointer types, |
| 6407 | // so adjust the base type to the object type itself. |
| 6408 | if (BaseType->isObjCObjectPointerType()) |
| 6409 | BaseType = BaseType->getPointeeType(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6410 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6411 | // C++ [basic.lookup.classref]p2: |
| 6412 | // [...] If the type of the object expression is of pointer to scalar |
| 6413 | // type, the unqualified-id is looked up in the context of the complete |
| 6414 | // postfix-expression. |
| 6415 | // |
| 6416 | // This also indicates that we could be parsing a pseudo-destructor-name. |
| 6417 | // Note that Objective-C class and object types can be pseudo-destructor |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6418 | // expressions or normal member (ivar or property) access expressions, and |
| 6419 | // it's legal for the type to be incomplete if this is a pseudo-destructor |
| 6420 | // call. We'll do more incomplete-type checks later in the lookup process, |
| 6421 | // so just skip this check for ObjC types. |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6422 | if (BaseType->isObjCObjectOrInterfaceType()) { |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6423 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6424 | MayBePseudoDestructor = true; |
John McCall | 9d145df | 2015-12-14 19:12:54 +0000 | [diff] [blame] | 6425 | return Base; |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 6426 | } else if (!BaseType->isRecordType()) { |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 6427 | ObjectType = nullptr; |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6428 | MayBePseudoDestructor = true; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6429 | return Base; |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 6430 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6431 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6432 | // The object type must be complete (or dependent), or |
| 6433 | // C++11 [expr.prim.general]p3: |
| 6434 | // Unlike the object expression in other contexts, *this is not required to |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6435 | // 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] | 6436 | // the member function body. |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 6437 | if (!BaseType->isDependentType() && |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6438 | !isThisOutsideMemberFunctionBody(BaseType) && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 6439 | RequireCompleteType(OpLoc, BaseType, diag::err_incomplete_member_access)) |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 6440 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6441 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 6442 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6443 | // 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] | 6444 | // 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] | 6445 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 6446 | // up in the scope of class C. [...] |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6447 | ObjectType = ParsedType::make(BaseType); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6448 | return Base; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 6449 | } |
| 6450 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6451 | static bool CheckArrow(Sema& S, QualType& ObjectType, Expr *&Base, |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6452 | tok::TokenKind& OpKind, SourceLocation OpLoc) { |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6453 | if (Base->hasPlaceholderType()) { |
| 6454 | ExprResult result = S.CheckPlaceholderExpr(Base); |
| 6455 | if (result.isInvalid()) return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6456 | Base = result.get(); |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6457 | } |
| 6458 | ObjectType = Base->getType(); |
| 6459 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6460 | // C++ [expr.pseudo]p2: |
| 6461 | // The left-hand side of the dot operator shall be of scalar type. The |
| 6462 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 6463 | // This scalar type is the object type. |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 6464 | // Note that this is rather different from the normal handling for the |
| 6465 | // arrow operator. |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6466 | if (OpKind == tok::arrow) { |
| 6467 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 6468 | ObjectType = Ptr->getPointeeType(); |
| 6469 | } else if (!Base->isTypeDependent()) { |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 6470 | // The user wrote "p->" when they probably meant "p."; fix it. |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6471 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 6472 | << ObjectType << true |
| 6473 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 6474 | if (S.isSFINAEContext()) |
| 6475 | return true; |
| 6476 | |
| 6477 | OpKind = tok::period; |
| 6478 | } |
| 6479 | } |
| 6480 | |
| 6481 | return false; |
| 6482 | } |
| 6483 | |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6484 | /// \brief Check if it's ok to try and recover dot pseudo destructor calls on |
| 6485 | /// pointer objects. |
| 6486 | static bool |
| 6487 | canRecoverDotPseudoDestructorCallsOnPointerObjects(Sema &SemaRef, |
| 6488 | QualType DestructedType) { |
| 6489 | // If this is a record type, check if its destructor is callable. |
| 6490 | if (auto *RD = DestructedType->getAsCXXRecordDecl()) { |
| 6491 | if (CXXDestructorDecl *D = SemaRef.LookupDestructor(RD)) |
| 6492 | return SemaRef.CanUseDecl(D, /*TreatUnavailableAsInvalid=*/false); |
| 6493 | return false; |
| 6494 | } |
| 6495 | |
| 6496 | // Otherwise, check if it's a type for which it's valid to use a pseudo-dtor. |
| 6497 | return DestructedType->isDependentType() || DestructedType->isScalarType() || |
| 6498 | DestructedType->isVectorType(); |
| 6499 | } |
| 6500 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6501 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6502 | SourceLocation OpLoc, |
| 6503 | tok::TokenKind OpKind, |
| 6504 | const CXXScopeSpec &SS, |
| 6505 | TypeSourceInfo *ScopeTypeInfo, |
| 6506 | SourceLocation CCLoc, |
| 6507 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6508 | PseudoDestructorTypeStorage Destructed) { |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6509 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6510 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6511 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6512 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6513 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6514 | |
Douglas Gregor | c5c5734 | 2012-09-10 14:57:06 +0000 | [diff] [blame] | 6515 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType() && |
| 6516 | !ObjectType->isVectorType()) { |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 6517 | if (getLangOpts().MSVCCompat && ObjectType->isVoidType()) |
Nico Weber | 4bc6499 | 2012-01-23 06:08:16 +0000 | [diff] [blame] | 6518 | Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange(); |
Reid Kleckner | e502507 | 2014-05-01 16:50:23 +0000 | [diff] [blame] | 6519 | else { |
Nico Weber | 5882927 | 2012-01-23 05:50:57 +0000 | [diff] [blame] | 6520 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 6521 | << ObjectType << Base->getSourceRange(); |
Reid Kleckner | e502507 | 2014-05-01 16:50:23 +0000 | [diff] [blame] | 6522 | return ExprError(); |
| 6523 | } |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6527 | // [...] The cv-unqualified versions of the object type and of the type |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6528 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6529 | if (DestructedTypeInfo) { |
| 6530 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 6531 | SourceLocation DestructedTypeStart |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6532 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6533 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
| 6534 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6535 | // Detect dot pseudo destructor calls on pointer objects, e.g.: |
| 6536 | // Foo *foo; |
| 6537 | // foo.~Foo(); |
| 6538 | if (OpKind == tok::period && ObjectType->isPointerType() && |
| 6539 | Context.hasSameUnqualifiedType(DestructedType, |
| 6540 | ObjectType->getPointeeType())) { |
| 6541 | auto Diagnostic = |
| 6542 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 6543 | << ObjectType << /*IsArrow=*/0 << Base->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6544 | |
Alex Lorenz | 56fb6fe | 2017-01-20 15:38:58 +0000 | [diff] [blame] | 6545 | // Issue a fixit only when the destructor is valid. |
| 6546 | if (canRecoverDotPseudoDestructorCallsOnPointerObjects( |
| 6547 | *this, DestructedType)) |
| 6548 | Diagnostic << FixItHint::CreateReplacement(OpLoc, "->"); |
| 6549 | |
| 6550 | // Recover by setting the object type to the destructed type and the |
| 6551 | // operator to '->'. |
| 6552 | ObjectType = DestructedType; |
| 6553 | OpKind = tok::arrow; |
| 6554 | } else { |
| 6555 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
| 6556 | << ObjectType << DestructedType << Base->getSourceRange() |
| 6557 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 6558 | |
| 6559 | // Recover by setting the destructed type to the object type. |
| 6560 | DestructedType = ObjectType; |
| 6561 | DestructedTypeInfo = |
| 6562 | Context.getTrivialTypeSourceInfo(ObjectType, DestructedTypeStart); |
| 6563 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6564 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6565 | } else if (DestructedType.getObjCLifetime() != |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6566 | ObjectType.getObjCLifetime()) { |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6567 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6568 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
| 6569 | // Okay: just pretend that the user provided the correctly-qualified |
| 6570 | // type. |
| 6571 | } else { |
| 6572 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
| 6573 | << ObjectType << DestructedType << Base->getSourceRange() |
| 6574 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 6575 | } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6576 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6577 | // Recover by setting the destructed type to the object type. |
| 6578 | DestructedType = ObjectType; |
| 6579 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 6580 | DestructedTypeStart); |
| 6581 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6582 | } |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6583 | } |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6584 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6585 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6586 | // C++ [expr.pseudo]p2: |
| 6587 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 6588 | // form |
| 6589 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6590 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6591 | // |
| 6592 | // shall designate the same scalar type. |
| 6593 | if (ScopeTypeInfo) { |
| 6594 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 6595 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | b86a6b8 | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 6596 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6597 | |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6598 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6599 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6600 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6601 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6602 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6603 | ScopeType = QualType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6604 | ScopeTypeInfo = nullptr; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6605 | } |
| 6606 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6607 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6608 | Expr *Result |
| 6609 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 6610 | OpKind == tok::arrow, OpLoc, |
Douglas Gregor | a6ce608 | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 6611 | SS.getWithLocInContext(Context), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6612 | ScopeTypeInfo, |
| 6613 | CCLoc, |
| 6614 | TildeLoc, |
| 6615 | Destructed); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6616 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6617 | return Result; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6618 | } |
| 6619 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6620 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6621 | SourceLocation OpLoc, |
| 6622 | tok::TokenKind OpKind, |
| 6623 | CXXScopeSpec &SS, |
| 6624 | UnqualifiedId &FirstTypeName, |
| 6625 | SourceLocation CCLoc, |
| 6626 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6627 | UnqualifiedId &SecondTypeName) { |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6628 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 6629 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 6630 | "Invalid first type name in pseudo-destructor"); |
| 6631 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 6632 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 6633 | "Invalid second type name in pseudo-destructor"); |
| 6634 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6635 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6636 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6637 | return ExprError(); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6638 | |
| 6639 | // Compute the object type that we should use for name lookup purposes. Only |
| 6640 | // record types and dependent types matter. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6641 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6642 | if (!SS.isSet()) { |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 6643 | if (ObjectType->isRecordType()) |
| 6644 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6645 | else if (ObjectType->isDependentType()) |
| 6646 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6647 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6648 | |
| 6649 | // Convert the name of the type being destructed (following the ~) into a |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6650 | // type (with source-location information). |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6651 | QualType DestructedType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6652 | TypeSourceInfo *DestructedTypeInfo = nullptr; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6653 | PseudoDestructorTypeStorage Destructed; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6654 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6655 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6656 | SecondTypeName.StartLocation, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6657 | S, &SS, true, false, ObjectTypePtrForLookup, |
| 6658 | /*IsCtorOrDtorName*/true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6659 | if (!T && |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6660 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 6661 | (!SS.isSet() && ObjectType->isDependentType()))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6662 | // 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] | 6663 | // couldn't find anything useful in scope. Just store the identifier and |
| 6664 | // it's location, and we'll perform (qualified) name lookup again at |
| 6665 | // template instantiation time. |
| 6666 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 6667 | SecondTypeName.StartLocation); |
| 6668 | } else if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6669 | Diag(SecondTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6670 | diag::err_pseudo_dtor_destructor_non_type) |
| 6671 | << SecondTypeName.Identifier << ObjectType; |
| 6672 | if (isSFINAEContext()) |
| 6673 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6674 | |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6675 | // Recover by assuming we had the right type all along. |
| 6676 | DestructedType = ObjectType; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6677 | } else |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6678 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6679 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6680 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6681 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6682 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6683 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6684 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6685 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6686 | TemplateId->Template, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6687 | TemplateId->Name, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6688 | TemplateId->TemplateNameLoc, |
| 6689 | TemplateId->LAngleLoc, |
| 6690 | TemplateArgsPtr, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6691 | TemplateId->RAngleLoc, |
| 6692 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6693 | if (T.isInvalid() || !T.get()) { |
| 6694 | // Recover by assuming we had the right type all along. |
| 6695 | DestructedType = ObjectType; |
| 6696 | } else |
| 6697 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6698 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6699 | |
| 6700 | // 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] | 6701 | // information. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6702 | if (!DestructedType.isNull()) { |
| 6703 | if (!DestructedTypeInfo) |
| 6704 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6705 | SecondTypeName.StartLocation); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 6706 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 6707 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6708 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6709 | // 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] | 6710 | TypeSourceInfo *ScopeTypeInfo = nullptr; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6711 | QualType ScopeType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6712 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6713 | FirstTypeName.Identifier) { |
| 6714 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6715 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6716 | FirstTypeName.StartLocation, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6717 | S, &SS, true, false, ObjectTypePtrForLookup, |
| 6718 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6719 | if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6720 | Diag(FirstTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6721 | diag::err_pseudo_dtor_destructor_non_type) |
| 6722 | << FirstTypeName.Identifier << ObjectType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6723 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6724 | if (isSFINAEContext()) |
| 6725 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6726 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6727 | // Just drop this type. It's unnecessary anyway. |
| 6728 | ScopeType = QualType(); |
| 6729 | } else |
| 6730 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6731 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6732 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6733 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6734 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6735 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6736 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6737 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6738 | TemplateId->Template, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6739 | TemplateId->Name, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6740 | TemplateId->TemplateNameLoc, |
| 6741 | TemplateId->LAngleLoc, |
| 6742 | TemplateArgsPtr, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 6743 | TemplateId->RAngleLoc, |
| 6744 | /*IsCtorOrDtorName*/true); |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 6745 | if (T.isInvalid() || !T.get()) { |
| 6746 | // Recover by dropping this type. |
| 6747 | ScopeType = QualType(); |
| 6748 | } else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6749 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 6750 | } |
| 6751 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6752 | |
Douglas Gregor | 90ad922 | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 6753 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 6754 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 6755 | FirstTypeName.StartLocation); |
| 6756 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6757 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6758 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | cdbd515 | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 6759 | ScopeTypeInfo, CCLoc, TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6760 | Destructed); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 6761 | } |
| 6762 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6763 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
| 6764 | SourceLocation OpLoc, |
| 6765 | tok::TokenKind OpKind, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6766 | SourceLocation TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6767 | const DeclSpec& DS) { |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 6768 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6769 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 6770 | return ExprError(); |
| 6771 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 6772 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc(), |
| 6773 | false); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6774 | |
| 6775 | TypeLocBuilder TLB; |
| 6776 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
| 6777 | DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 6778 | TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T); |
| 6779 | PseudoDestructorTypeStorage Destructed(DestructedTypeInfo); |
| 6780 | |
| 6781 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, CXXScopeSpec(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6782 | nullptr, SourceLocation(), TildeLoc, |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 6783 | Destructed); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 6784 | } |
| 6785 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6786 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 6787 | CXXConversionDecl *Method, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6788 | bool HadMultipleCandidates) { |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6789 | if (Method->getParent()->isLambda() && |
| 6790 | Method->getConversionType()->isBlockPointerType()) { |
| 6791 | // This is a lambda coversion to block pointer; check if the argument |
| 6792 | // is a LambdaExpr. |
| 6793 | Expr *SubE = E; |
| 6794 | CastExpr *CE = dyn_cast<CastExpr>(SubE); |
| 6795 | if (CE && CE->getCastKind() == CK_NoOp) |
| 6796 | SubE = CE->getSubExpr(); |
| 6797 | SubE = SubE->IgnoreParens(); |
| 6798 | if (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(SubE)) |
| 6799 | SubE = BE->getSubExpr(); |
| 6800 | if (isa<LambdaExpr>(SubE)) { |
| 6801 | // For the conversion to block pointer on a lambda expression, we |
| 6802 | // construct a special BlockLiteral instead; this doesn't really make |
| 6803 | // a difference in ARC, but outside of ARC the resulting block literal |
| 6804 | // follows the normal lifetime rules for block literals instead of being |
| 6805 | // autoreleased. |
| 6806 | DiagnosticErrorTrap Trap(Diags); |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6807 | PushExpressionEvaluationContext( |
| 6808 | ExpressionEvaluationContext::PotentiallyEvaluated); |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6809 | ExprResult Exp = BuildBlockForLambdaConversion(E->getExprLoc(), |
| 6810 | E->getExprLoc(), |
| 6811 | Method, E); |
Akira Hatanaka | c482acd | 2016-05-04 18:07:20 +0000 | [diff] [blame] | 6812 | PopExpressionEvaluationContext(); |
| 6813 | |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6814 | if (Exp.isInvalid()) |
| 6815 | Diag(E->getExprLoc(), diag::note_lambda_to_block_conv); |
| 6816 | return Exp; |
| 6817 | } |
| 6818 | } |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 6819 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6820 | ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/nullptr, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6821 | FoundDecl, Method); |
| 6822 | if (Exp.isInvalid()) |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 6823 | return true; |
Eli Friedman | f719553 | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 6824 | |
Aaron Ballman | f4cb2be | 2015-03-24 15:07:53 +0000 | [diff] [blame] | 6825 | MemberExpr *ME = new (Context) MemberExpr( |
| 6826 | Exp.get(), /*IsArrow=*/false, SourceLocation(), Method, SourceLocation(), |
| 6827 | Context.BoundMemberTy, VK_RValue, OK_Ordinary); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6828 | if (HadMultipleCandidates) |
| 6829 | ME->setHadMultipleCandidates(true); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 6830 | MarkMemberReferenced(ME); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6831 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6832 | QualType ResultType = Method->getReturnType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6833 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 6834 | ResultType = ResultType.getNonLValueExprType(Context); |
| 6835 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6836 | CXXMemberCallExpr *CE = |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 6837 | new (Context) CXXMemberCallExpr(Context, ME, None, ResultType, VK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6838 | Exp.get()->getLocEnd()); |
George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6839 | |
| 6840 | if (CheckFunctionCall(Method, CE, |
| 6841 | Method->getType()->castAs<FunctionProtoType>())) |
| 6842 | return ExprError(); |
| 6843 | |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6844 | return CE; |
| 6845 | } |
| 6846 | |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6847 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 6848 | SourceLocation RParen) { |
Aaron Ballman | edc8084 | 2015-04-27 22:31:12 +0000 | [diff] [blame] | 6849 | // If the operand is an unresolved lookup expression, the expression is ill- |
| 6850 | // formed per [over.over]p1, because overloaded function names cannot be used |
| 6851 | // without arguments except in explicit contexts. |
| 6852 | ExprResult R = CheckPlaceholderExpr(Operand); |
| 6853 | if (R.isInvalid()) |
| 6854 | return R; |
| 6855 | |
| 6856 | // The operand may have been modified when checking the placeholder type. |
| 6857 | Operand = R.get(); |
| 6858 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 6859 | if (!inTemplateInstantiation() && Operand->HasSideEffects(Context, false)) { |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 6860 | // The expression operand for noexcept is in an unevaluated expression |
| 6861 | // context, so side effects could result in unintended consequences. |
| 6862 | Diag(Operand->getExprLoc(), diag::warn_side_effects_unevaluated_context); |
| 6863 | } |
| 6864 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 6865 | CanThrowResult CanThrow = canThrow(Operand); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6866 | return new (Context) |
| 6867 | CXXNoexceptExpr(Context.BoolTy, Operand, CanThrow, KeyLoc, RParen); |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6868 | } |
| 6869 | |
| 6870 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 6871 | Expr *Operand, SourceLocation RParen) { |
| 6872 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 22e3a93 | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 6873 | } |
| 6874 | |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6875 | static bool IsSpecialDiscardedValue(Expr *E) { |
| 6876 | // In C++11, discarded-value expressions of a certain form are special, |
| 6877 | // according to [expr]p10: |
| 6878 | // The lvalue-to-rvalue conversion (4.1) is applied only if the |
| 6879 | // expression is an lvalue of volatile-qualified type and it has |
| 6880 | // one of the following forms: |
| 6881 | E = E->IgnoreParens(); |
| 6882 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6883 | // - id-expression (5.1.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6884 | if (isa<DeclRefExpr>(E)) |
| 6885 | return true; |
| 6886 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6887 | // - subscripting (5.2.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6888 | if (isa<ArraySubscriptExpr>(E)) |
| 6889 | return true; |
| 6890 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6891 | // - class member access (5.2.5), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6892 | if (isa<MemberExpr>(E)) |
| 6893 | return true; |
| 6894 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6895 | // - indirection (5.3.1), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6896 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 6897 | if (UO->getOpcode() == UO_Deref) |
| 6898 | return true; |
| 6899 | |
| 6900 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6901 | // - pointer-to-member operation (5.5), |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6902 | if (BO->isPtrMemOp()) |
| 6903 | return true; |
| 6904 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6905 | // - 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] | 6906 | if (BO->getOpcode() == BO_Comma) |
| 6907 | return IsSpecialDiscardedValue(BO->getRHS()); |
| 6908 | } |
| 6909 | |
Eli Friedman | c49c226 | 2012-05-24 22:36:31 +0000 | [diff] [blame] | 6910 | // - conditional expression (5.16) where both the second and the third |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6911 | // operands are one of the above, or |
| 6912 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) |
| 6913 | return IsSpecialDiscardedValue(CO->getTrueExpr()) && |
| 6914 | IsSpecialDiscardedValue(CO->getFalseExpr()); |
| 6915 | // The related edge case of "*x ?: *x". |
| 6916 | if (BinaryConditionalOperator *BCO = |
| 6917 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 6918 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(BCO->getTrueExpr())) |
| 6919 | return IsSpecialDiscardedValue(OVE->getSourceExpr()) && |
| 6920 | IsSpecialDiscardedValue(BCO->getFalseExpr()); |
| 6921 | } |
| 6922 | |
| 6923 | // Objective-C++ extensions to the rule. |
| 6924 | if (isa<PseudoObjectExpr>(E) || isa<ObjCIvarRefExpr>(E)) |
| 6925 | return true; |
| 6926 | |
| 6927 | return false; |
| 6928 | } |
| 6929 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6930 | /// Perform the conversions required for an expression used in a |
| 6931 | /// context that ignores the result. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6932 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6933 | if (E->hasPlaceholderType()) { |
| 6934 | ExprResult result = CheckPlaceholderExpr(E); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6935 | if (result.isInvalid()) return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6936 | E = result.get(); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6937 | } |
| 6938 | |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 6939 | // C99 6.3.2.1: |
| 6940 | // [Except in specific positions,] an lvalue that does not have |
| 6941 | // array type is converted to the value stored in the |
| 6942 | // designated object (and is no longer an lvalue). |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 6943 | if (E->isRValue()) { |
| 6944 | // In C, function designators (i.e. expressions of function type) |
| 6945 | // are r-values, but we still want to do function-to-pointer decay |
| 6946 | // on them. This is both technically correct and convenient for |
| 6947 | // some clients. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6948 | if (!getLangOpts().CPlusPlus && E->getType()->isFunctionType()) |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 6949 | return DefaultFunctionArrayConversion(E); |
| 6950 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6951 | return E; |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 6952 | } |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 6953 | |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6954 | if (getLangOpts().CPlusPlus) { |
| 6955 | // The C++11 standard defines the notion of a discarded-value expression; |
| 6956 | // normally, we don't need to do anything to handle it, but if it is a |
| 6957 | // volatile lvalue with a special form, we perform an lvalue-to-rvalue |
| 6958 | // conversion. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6959 | if (getLangOpts().CPlusPlus11 && E->isGLValue() && |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6960 | E->getType().isVolatileQualified() && |
| 6961 | IsSpecialDiscardedValue(E)) { |
| 6962 | ExprResult Res = DefaultLvalueConversion(E); |
| 6963 | if (Res.isInvalid()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6964 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6965 | E = Res.get(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 6966 | } |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 6967 | |
| 6968 | // C++1z: |
| 6969 | // If the expression is a prvalue after this optional conversion, the |
| 6970 | // temporary materialization conversion is applied. |
| 6971 | // |
| 6972 | // We skip this step: IR generation is able to synthesize the storage for |
| 6973 | // itself in the aggregate case, and adding the extra node to the AST is |
| 6974 | // just clutter. |
| 6975 | // FIXME: We don't emit lifetime markers for the temporaries due to this. |
| 6976 | // FIXME: Do any other AST consumers care about this? |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6977 | return E; |
Eli Friedman | f798f65 | 2012-05-24 22:04:19 +0000 | [diff] [blame] | 6978 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6979 | |
| 6980 | // GCC seems to also exclude expressions of incomplete enum type. |
| 6981 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
| 6982 | if (!T->getDecl()->isComplete()) { |
| 6983 | // FIXME: stupid workaround for a codegen bug! |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6984 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).get(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6985 | return E; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6986 | } |
| 6987 | } |
| 6988 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6989 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 6990 | if (Res.isInvalid()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6991 | return E; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6992 | E = Res.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6993 | |
John McCall | ca61b65 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 6994 | if (!E->getType()->isVoidType()) |
| 6995 | RequireCompleteType(E->getExprLoc(), E->getType(), |
| 6996 | diag::err_incomplete_type); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6997 | return E; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6998 | } |
| 6999 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7000 | // If we can unambiguously determine whether Var can never be used |
| 7001 | // in a constant expression, return true. |
| 7002 | // - if the variable and its initializer are non-dependent, then |
| 7003 | // we can unambiguously check if the variable is a constant expression. |
| 7004 | // - if the initializer is not value dependent - we can determine whether |
| 7005 | // 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] | 7006 | // be used to initialize a constant expression we conclude that Var can |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7007 | // never be a constant expression. |
| 7008 | // - FXIME: if the initializer is dependent, we can still do some analysis and |
| 7009 | // identify certain cases unambiguously as non-const by using a Visitor: |
| 7010 | // - such as those that involve odr-use of a ParmVarDecl, involve a new |
| 7011 | // delete, lambda-expr, dynamic-cast, reinterpret-cast etc... |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7012 | static inline bool VariableCanNeverBeAConstantExpression(VarDecl *Var, |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7013 | ASTContext &Context) { |
| 7014 | if (isa<ParmVarDecl>(Var)) return true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7015 | const VarDecl *DefVD = nullptr; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7016 | |
| 7017 | // If there is no initializer - this can not be a constant expression. |
| 7018 | if (!Var->getAnyInitializer(DefVD)) return true; |
| 7019 | assert(DefVD); |
| 7020 | if (DefVD->isWeak()) return false; |
| 7021 | EvaluatedStmt *Eval = DefVD->ensureEvaluatedStmt(); |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 7022 | |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7023 | Expr *Init = cast<Expr>(Eval->Value); |
| 7024 | |
| 7025 | if (Var->getType()->isDependentType() || Init->isValueDependent()) { |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 7026 | // FIXME: Teach the constant evaluator to deal with the non-dependent parts |
| 7027 | // of value-dependent expressions, and use it here to determine whether the |
| 7028 | // initializer is a potential constant expression. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7029 | return false; |
Richard Smith | c941ba9 | 2014-02-06 23:35:16 +0000 | [diff] [blame] | 7030 | } |
| 7031 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7032 | return !IsVariableAConstantExpression(Var, Context); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7033 | } |
| 7034 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7035 | /// \brief Check if the current lambda has any potential captures |
| 7036 | /// that must be captured by any of its enclosing lambdas that are ready to |
| 7037 | /// capture. If there is a lambda that can capture a nested |
| 7038 | /// potential-capture, go ahead and do so. Also, check to see if any |
| 7039 | /// 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] | 7040 | /// need to be captured. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7041 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7042 | static void CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures( |
| 7043 | Expr *const FE, LambdaScopeInfo *const CurrentLSI, Sema &S) { |
| 7044 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7045 | assert(!S.isUnevaluatedContext()); |
| 7046 | assert(S.CurContext->isDependentContext()); |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7047 | #ifndef NDEBUG |
| 7048 | DeclContext *DC = S.CurContext; |
| 7049 | while (DC && isa<CapturedDecl>(DC)) |
| 7050 | DC = DC->getParent(); |
| 7051 | assert( |
| 7052 | CurrentLSI->CallOperator == DC && |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7053 | "The current call operator must be synchronized with Sema's CurContext"); |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7054 | #endif // NDEBUG |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7055 | |
| 7056 | const bool IsFullExprInstantiationDependent = FE->isInstantiationDependent(); |
| 7057 | |
| 7058 | ArrayRef<const FunctionScopeInfo *> FunctionScopesArrayRef( |
| 7059 | S.FunctionScopes.data(), S.FunctionScopes.size()); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7060 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7061 | // All the potentially captureable variables in the current nested |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7062 | // lambda (within a generic outer lambda), must be captured by an |
| 7063 | // outer lambda that is enclosed within a non-dependent context. |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7064 | const unsigned NumPotentialCaptures = |
| 7065 | CurrentLSI->getNumPotentialVariableCaptures(); |
| 7066 | for (unsigned I = 0; I != NumPotentialCaptures; ++I) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7067 | Expr *VarExpr = nullptr; |
| 7068 | VarDecl *Var = nullptr; |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7069 | CurrentLSI->getPotentialVariableCapture(I, Var, VarExpr); |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7070 | // 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] | 7071 | // expression is not instantiation dependent, only then do we not |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7072 | // need to check enclosing lambda's for speculative captures. |
| 7073 | // For e.g.: |
| 7074 | // Even though 'x' is not odr-used, it should be captured. |
| 7075 | // int test() { |
| 7076 | // const int x = 10; |
| 7077 | // auto L = [=](auto a) { |
| 7078 | // (void) +x + a; |
| 7079 | // }; |
| 7080 | // } |
| 7081 | if (CurrentLSI->isVariableExprMarkedAsNonODRUsed(VarExpr) && |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7082 | !IsFullExprInstantiationDependent) |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7083 | continue; |
| 7084 | |
| 7085 | // If we have a capture-capable lambda for the variable, go ahead and |
| 7086 | // capture the variable in that lambda (and all its enclosing lambdas). |
| 7087 | if (const Optional<unsigned> Index = |
| 7088 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
| 7089 | FunctionScopesArrayRef, Var, S)) { |
| 7090 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
| 7091 | MarkVarDeclODRUsed(Var, VarExpr->getExprLoc(), S, |
| 7092 | &FunctionScopeIndexOfCapturableLambda); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7093 | } |
| 7094 | const bool IsVarNeverAConstantExpression = |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7095 | VariableCanNeverBeAConstantExpression(Var, S.Context); |
| 7096 | if (!IsFullExprInstantiationDependent || IsVarNeverAConstantExpression) { |
| 7097 | // This full expression is not instantiation dependent or the variable |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7098 | // can not be used in a constant expression - which means |
| 7099 | // this variable must be odr-used here, so diagnose a |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7100 | // capture violation early, if the variable is un-captureable. |
| 7101 | // This is purely for diagnosing errors early. Otherwise, this |
| 7102 | // error would get diagnosed when the lambda becomes capture ready. |
| 7103 | QualType CaptureType, DeclRefType; |
| 7104 | SourceLocation ExprLoc = VarExpr->getExprLoc(); |
| 7105 | if (S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7106 | /*EllipsisLoc*/ SourceLocation(), |
| 7107 | /*BuildAndDiagnose*/false, CaptureType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7108 | DeclRefType, nullptr)) { |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7109 | // We will never be able to capture this variable, and we need |
| 7110 | // to be able to in any and all instantiations, so diagnose it. |
| 7111 | S.tryCaptureVariable(Var, ExprLoc, S.TryCapture_Implicit, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7112 | /*EllipsisLoc*/ SourceLocation(), |
| 7113 | /*BuildAndDiagnose*/true, CaptureType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7114 | DeclRefType, nullptr); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7115 | } |
| 7116 | } |
| 7117 | } |
| 7118 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7119 | // Check if 'this' needs to be captured. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7120 | if (CurrentLSI->hasPotentialThisCapture()) { |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7121 | // If we have a capture-capable lambda for 'this', go ahead and capture |
| 7122 | // 'this' in that lambda (and all its enclosing lambdas). |
| 7123 | if (const Optional<unsigned> Index = |
| 7124 | getStackIndexOfNearestEnclosingCaptureCapableLambda( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7125 | FunctionScopesArrayRef, /*0 is 'this'*/ nullptr, S)) { |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7126 | const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); |
| 7127 | S.CheckCXXThisCapture(CurrentLSI->PotentialThisCaptureLocation, |
| 7128 | /*Explicit*/ false, /*BuildAndDiagnose*/ true, |
| 7129 | &FunctionScopeIndexOfCapturableLambda); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7130 | } |
| 7131 | } |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7132 | |
| 7133 | // Reset all the potential captures at the end of each full-expression. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7134 | CurrentLSI->clearPotentialCaptures(); |
| 7135 | } |
| 7136 | |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7137 | static ExprResult attemptRecovery(Sema &SemaRef, |
| 7138 | const TypoCorrectionConsumer &Consumer, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 7139 | const TypoCorrection &TC) { |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7140 | LookupResult R(SemaRef, Consumer.getLookupResult().getLookupNameInfo(), |
| 7141 | Consumer.getLookupResult().getLookupKind()); |
| 7142 | const CXXScopeSpec *SS = Consumer.getSS(); |
| 7143 | CXXScopeSpec NewSS; |
| 7144 | |
| 7145 | // Use an approprate CXXScopeSpec for building the expr. |
| 7146 | if (auto *NNS = TC.getCorrectionSpecifier()) |
| 7147 | NewSS.MakeTrivial(SemaRef.Context, NNS, TC.getCorrectionRange()); |
| 7148 | else if (SS && !TC.WillReplaceSpecifier()) |
| 7149 | NewSS = *SS; |
| 7150 | |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7151 | if (auto *ND = TC.getFoundDecl()) { |
Nick Lewycky | 01ad4ae | 2014-12-13 02:54:28 +0000 | [diff] [blame] | 7152 | R.setLookupName(ND->getDeclName()); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7153 | R.addDecl(ND); |
| 7154 | if (ND->isCXXClassMember()) { |
Nick Lewycky | 01ad4ae | 2014-12-13 02:54:28 +0000 | [diff] [blame] | 7155 | // Figure out the correct naming class to add to the LookupResult. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7156 | CXXRecordDecl *Record = nullptr; |
| 7157 | if (auto *NNS = TC.getCorrectionSpecifier()) |
| 7158 | Record = NNS->getAsType()->getAsCXXRecordDecl(); |
| 7159 | if (!Record) |
Olivier Goffart | ed13fab | 2015-01-09 09:37:26 +0000 | [diff] [blame] | 7160 | Record = |
| 7161 | dyn_cast<CXXRecordDecl>(ND->getDeclContext()->getRedeclContext()); |
| 7162 | if (Record) |
| 7163 | R.setNamingClass(Record); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7164 | |
| 7165 | // Detect and handle the case where the decl might be an implicit |
| 7166 | // member. |
| 7167 | bool MightBeImplicitMember; |
| 7168 | if (!Consumer.isAddressOfOperand()) |
| 7169 | MightBeImplicitMember = true; |
| 7170 | else if (!NewSS.isEmpty()) |
| 7171 | MightBeImplicitMember = false; |
| 7172 | else if (R.isOverloadedResult()) |
| 7173 | MightBeImplicitMember = false; |
| 7174 | else if (R.isUnresolvableResult()) |
| 7175 | MightBeImplicitMember = true; |
| 7176 | else |
| 7177 | MightBeImplicitMember = isa<FieldDecl>(ND) || |
| 7178 | isa<IndirectFieldDecl>(ND) || |
| 7179 | isa<MSPropertyDecl>(ND); |
| 7180 | |
| 7181 | if (MightBeImplicitMember) |
| 7182 | return SemaRef.BuildPossibleImplicitMemberExpr( |
| 7183 | NewSS, /*TemplateKWLoc*/ SourceLocation(), R, |
Aaron Ballman | 6924dcd | 2015-09-01 14:49:24 +0000 | [diff] [blame] | 7184 | /*TemplateArgs*/ nullptr, /*S*/ nullptr); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7185 | } else if (auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
| 7186 | return SemaRef.LookupInObjCMethod(R, Consumer.getScope(), |
| 7187 | Ivar->getIdentifier()); |
| 7188 | } |
| 7189 | } |
| 7190 | |
Kaelyn Takata | 6f71ce2 | 2014-11-20 22:06:33 +0000 | [diff] [blame] | 7191 | return SemaRef.BuildDeclarationNameExpr(NewSS, R, /*NeedsADL*/ false, |
| 7192 | /*AcceptInvalidDecl*/ true); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7193 | } |
| 7194 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7195 | namespace { |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7196 | class FindTypoExprs : public RecursiveASTVisitor<FindTypoExprs> { |
| 7197 | llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs; |
| 7198 | |
| 7199 | public: |
| 7200 | explicit FindTypoExprs(llvm::SmallSetVector<TypoExpr *, 2> &TypoExprs) |
| 7201 | : TypoExprs(TypoExprs) {} |
| 7202 | bool VisitTypoExpr(TypoExpr *TE) { |
| 7203 | TypoExprs.insert(TE); |
| 7204 | return true; |
| 7205 | } |
| 7206 | }; |
| 7207 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7208 | class TransformTypos : public TreeTransform<TransformTypos> { |
| 7209 | typedef TreeTransform<TransformTypos> BaseTransform; |
| 7210 | |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7211 | VarDecl *InitDecl; // A decl to avoid as a correction because it is in the |
| 7212 | // process of being initialized. |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7213 | llvm::function_ref<ExprResult(Expr *)> ExprFilter; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7214 | llvm::SmallSetVector<TypoExpr *, 2> TypoExprs, AmbiguousTypoExprs; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7215 | llvm::SmallDenseMap<TypoExpr *, ExprResult, 2> TransformCache; |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7216 | llvm::SmallDenseMap<OverloadExpr *, Expr *, 4> OverloadResolution; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7217 | |
| 7218 | /// \brief Emit diagnostics for all of the TypoExprs encountered. |
| 7219 | /// If the TypoExprs were successfully corrected, then the diagnostics should |
| 7220 | /// suggest the corrections. Otherwise the diagnostics will not suggest |
| 7221 | /// anything (having been passed an empty TypoCorrection). |
| 7222 | void EmitAllDiagnostics() { |
| 7223 | for (auto E : TypoExprs) { |
| 7224 | TypoExpr *TE = cast<TypoExpr>(E); |
| 7225 | auto &State = SemaRef.getTypoExprState(TE); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7226 | if (State.DiagHandler) { |
| 7227 | TypoCorrection TC = State.Consumer->getCurrentCorrection(); |
| 7228 | ExprResult Replacement = TransformCache[TE]; |
| 7229 | |
| 7230 | // Extract the NamedDecl from the transformed TypoExpr and add it to the |
| 7231 | // TypoCorrection, replacing the existing decls. This ensures the right |
| 7232 | // NamedDecl is used in diagnostics e.g. in the case where overload |
| 7233 | // resolution was used to select one from several possible decls that |
| 7234 | // had been stored in the TypoCorrection. |
| 7235 | if (auto *ND = getDeclFromExpr( |
| 7236 | Replacement.isInvalid() ? nullptr : Replacement.get())) |
| 7237 | TC.setCorrectionDecl(ND); |
| 7238 | |
| 7239 | State.DiagHandler(TC); |
| 7240 | } |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7241 | SemaRef.clearDelayedTypo(TE); |
| 7242 | } |
| 7243 | } |
| 7244 | |
| 7245 | /// \brief If corrections for the first TypoExpr have been exhausted for a |
| 7246 | /// given combination of the other TypoExprs, retry those corrections against |
| 7247 | /// the next combination of substitutions for the other TypoExprs by advancing |
| 7248 | /// to the next potential correction of the second TypoExpr. For the second |
| 7249 | /// and subsequent TypoExprs, if its stream of corrections has been exhausted, |
| 7250 | /// the stream is reset and the next TypoExpr's stream is advanced by one (a |
| 7251 | /// TypoExpr's correction stream is advanced by removing the TypoExpr from the |
| 7252 | /// TransformCache). Returns true if there is still any untried combinations |
| 7253 | /// of corrections. |
| 7254 | bool CheckAndAdvanceTypoExprCorrectionStreams() { |
| 7255 | for (auto TE : TypoExprs) { |
| 7256 | auto &State = SemaRef.getTypoExprState(TE); |
| 7257 | TransformCache.erase(TE); |
| 7258 | if (!State.Consumer->finished()) |
| 7259 | return true; |
| 7260 | State.Consumer->resetCorrectionStream(); |
| 7261 | } |
| 7262 | return false; |
| 7263 | } |
| 7264 | |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7265 | NamedDecl *getDeclFromExpr(Expr *E) { |
| 7266 | if (auto *OE = dyn_cast_or_null<OverloadExpr>(E)) |
| 7267 | E = OverloadResolution[OE]; |
| 7268 | |
| 7269 | if (!E) |
| 7270 | return nullptr; |
| 7271 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7272 | return DRE->getFoundDecl(); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7273 | if (auto *ME = dyn_cast<MemberExpr>(E)) |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7274 | return ME->getFoundDecl(); |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7275 | // FIXME: Add any other expr types that could be be seen by the delayed typo |
| 7276 | // correction TreeTransform for which the corresponding TypoCorrection could |
Nick Lewycky | 39f9dbc | 2014-12-16 21:48:39 +0000 | [diff] [blame] | 7277 | // contain multiple decls. |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7278 | return nullptr; |
| 7279 | } |
| 7280 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7281 | ExprResult TryTransform(Expr *E) { |
| 7282 | Sema::SFINAETrap Trap(SemaRef); |
| 7283 | ExprResult Res = TransformExpr(E); |
| 7284 | if (Trap.hasErrorOccurred() || Res.isInvalid()) |
| 7285 | return ExprError(); |
| 7286 | |
| 7287 | return ExprFilter(Res.get()); |
| 7288 | } |
| 7289 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7290 | public: |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7291 | TransformTypos(Sema &SemaRef, VarDecl *InitDecl, llvm::function_ref<ExprResult(Expr *)> Filter) |
| 7292 | : BaseTransform(SemaRef), InitDecl(InitDecl), ExprFilter(Filter) {} |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7293 | |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7294 | ExprResult RebuildCallExpr(Expr *Callee, SourceLocation LParenLoc, |
| 7295 | MultiExprArg Args, |
| 7296 | SourceLocation RParenLoc, |
| 7297 | Expr *ExecConfig = nullptr) { |
| 7298 | auto Result = BaseTransform::RebuildCallExpr(Callee, LParenLoc, Args, |
| 7299 | RParenLoc, ExecConfig); |
| 7300 | if (auto *OE = dyn_cast<OverloadExpr>(Callee)) { |
Reid Kleckner | a9e65ba | 2014-12-13 01:11:23 +0000 | [diff] [blame] | 7301 | if (Result.isUsable()) { |
Reid Kleckner | a7fe33e | 2014-12-13 00:53:10 +0000 | [diff] [blame] | 7302 | Expr *ResultCall = Result.get(); |
| 7303 | if (auto *BE = dyn_cast<CXXBindTemporaryExpr>(ResultCall)) |
| 7304 | ResultCall = BE->getSubExpr(); |
| 7305 | if (auto *CE = dyn_cast<CallExpr>(ResultCall)) |
| 7306 | OverloadResolution[OE] = CE->getCallee(); |
| 7307 | } |
Kaelyn Takata | fe408a7 | 2014-10-27 18:07:46 +0000 | [diff] [blame] | 7308 | } |
| 7309 | return Result; |
| 7310 | } |
| 7311 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7312 | ExprResult TransformLambdaExpr(LambdaExpr *E) { return Owned(E); } |
| 7313 | |
Saleem Abdulrasool | a174241 | 2015-10-31 00:39:15 +0000 | [diff] [blame] | 7314 | ExprResult TransformBlockExpr(BlockExpr *E) { return Owned(E); } |
| 7315 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7316 | ExprResult Transform(Expr *E) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7317 | ExprResult Res; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7318 | while (true) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7319 | Res = TryTransform(E); |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7320 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7321 | // Exit if either the transform was valid or if there were no TypoExprs |
| 7322 | // to transform that still have any untried correction candidates.. |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7323 | if (!Res.isInvalid() || |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7324 | !CheckAndAdvanceTypoExprCorrectionStreams()) |
| 7325 | break; |
| 7326 | } |
| 7327 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7328 | // Ensure none of the TypoExprs have multiple typo correction candidates |
| 7329 | // with the same edit length that pass all the checks and filters. |
| 7330 | // TODO: Properly handle various permutations of possible corrections when |
| 7331 | // there is more than one potentially ambiguous typo correction. |
Kaelyn Takata | 26ffc5f | 2015-06-25 23:47:39 +0000 | [diff] [blame] | 7332 | // Also, disable typo correction while attempting the transform when |
| 7333 | // handling potentially ambiguous typo corrections as any new TypoExprs will |
| 7334 | // have been introduced by the application of one of the correction |
| 7335 | // candidates and add little to no value if corrected. |
| 7336 | SemaRef.DisableTypoCorrection = true; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7337 | while (!AmbiguousTypoExprs.empty()) { |
| 7338 | auto TE = AmbiguousTypoExprs.back(); |
| 7339 | auto Cached = TransformCache[TE]; |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7340 | auto &State = SemaRef.getTypoExprState(TE); |
| 7341 | State.Consumer->saveCurrentPosition(); |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7342 | TransformCache.erase(TE); |
| 7343 | if (!TryTransform(E).isInvalid()) { |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7344 | State.Consumer->resetCorrectionStream(); |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7345 | TransformCache.erase(TE); |
| 7346 | Res = ExprError(); |
| 7347 | break; |
Kaelyn Takata | 7a50369 | 2015-01-27 22:01:39 +0000 | [diff] [blame] | 7348 | } |
| 7349 | AmbiguousTypoExprs.remove(TE); |
| 7350 | State.Consumer->restoreSavedPosition(); |
| 7351 | TransformCache[TE] = Cached; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7352 | } |
Kaelyn Takata | 26ffc5f | 2015-06-25 23:47:39 +0000 | [diff] [blame] | 7353 | SemaRef.DisableTypoCorrection = false; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7354 | |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7355 | // 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] | 7356 | if (!Res.isUsable()) |
Kaelyn Takata | 57e07c9 | 2014-11-20 22:06:44 +0000 | [diff] [blame] | 7357 | FindTypoExprs(TypoExprs).TraverseStmt(E); |
| 7358 | |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7359 | EmitAllDiagnostics(); |
| 7360 | |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7361 | return Res; |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7362 | } |
| 7363 | |
| 7364 | ExprResult TransformTypoExpr(TypoExpr *E) { |
| 7365 | // If the TypoExpr hasn't been seen before, record it. Otherwise, return the |
| 7366 | // cached transformation result if there is one and the TypoExpr isn't the |
| 7367 | // first one that was encountered. |
| 7368 | auto &CacheEntry = TransformCache[E]; |
| 7369 | if (!TypoExprs.insert(E) && !CacheEntry.isUnset()) { |
| 7370 | return CacheEntry; |
| 7371 | } |
| 7372 | |
| 7373 | auto &State = SemaRef.getTypoExprState(E); |
| 7374 | assert(State.Consumer && "Cannot transform a cleared TypoExpr"); |
| 7375 | |
| 7376 | // For the first TypoExpr and an uncached TypoExpr, find the next likely |
| 7377 | // typo correction and return it. |
| 7378 | while (TypoCorrection TC = State.Consumer->getNextCorrection()) { |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 7379 | if (InitDecl && TC.getFoundDecl() == InitDecl) |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7380 | continue; |
Richard Smith | 1cf4541 | 2017-01-04 23:14:16 +0000 | [diff] [blame] | 7381 | // FIXME: If we would typo-correct to an invalid declaration, it's |
| 7382 | // probably best to just suppress all errors from this typo correction. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7383 | ExprResult NE = State.RecoveryHandler ? |
| 7384 | State.RecoveryHandler(SemaRef, E, TC) : |
| 7385 | attemptRecovery(SemaRef, *State.Consumer, TC); |
| 7386 | if (!NE.isInvalid()) { |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7387 | // Check whether there may be a second viable correction with the same |
| 7388 | // edit distance; if so, remember this TypoExpr may have an ambiguous |
| 7389 | // correction so it can be more thoroughly vetted later. |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7390 | TypoCorrection Next; |
Kaelyn Takata | 5ca2ecd | 2014-11-21 18:47:58 +0000 | [diff] [blame] | 7391 | if ((Next = State.Consumer->peekNextCorrection()) && |
| 7392 | Next.getEditDistance(false) == TC.getEditDistance(false)) { |
| 7393 | AmbiguousTypoExprs.insert(E); |
| 7394 | } else { |
| 7395 | AmbiguousTypoExprs.remove(E); |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7396 | } |
| 7397 | assert(!NE.isUnset() && |
| 7398 | "Typo was transformed into a valid-but-null ExprResult"); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7399 | return CacheEntry = NE; |
Kaelyn Takata | 3f9794f | 2014-11-20 22:06:30 +0000 | [diff] [blame] | 7400 | } |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7401 | } |
| 7402 | return CacheEntry = ExprError(); |
| 7403 | } |
| 7404 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7405 | } |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7406 | |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7407 | ExprResult |
| 7408 | Sema::CorrectDelayedTyposInExpr(Expr *E, VarDecl *InitDecl, |
| 7409 | llvm::function_ref<ExprResult(Expr *)> Filter) { |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7410 | // If the current evaluation context indicates there are uncorrected typos |
| 7411 | // and the current expression isn't guaranteed to not have typos, try to |
| 7412 | // resolve any TypoExpr nodes that might be in the expression. |
Kaelyn Takata | c71dda2 | 2014-12-02 22:05:35 +0000 | [diff] [blame] | 7413 | if (E && !ExprEvalContexts.empty() && ExprEvalContexts.back().NumTypos && |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7414 | (E->isTypeDependent() || E->isValueDependent() || |
| 7415 | E->isInstantiationDependent())) { |
Kaelyn Takata | c49838b | 2015-01-28 21:10:46 +0000 | [diff] [blame] | 7416 | auto TyposInContext = ExprEvalContexts.back().NumTypos; |
| 7417 | assert(TyposInContext < ~0U && "Recursive call of CorrectDelayedTyposInExpr"); |
| 7418 | ExprEvalContexts.back().NumTypos = ~0U; |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7419 | auto TyposResolved = DelayedTypos.size(); |
Kaelyn Takata | b8499f0 | 2015-05-05 19:17:03 +0000 | [diff] [blame] | 7420 | auto Result = TransformTypos(*this, InitDecl, Filter).Transform(E); |
Kaelyn Takata | c49838b | 2015-01-28 21:10:46 +0000 | [diff] [blame] | 7421 | ExprEvalContexts.back().NumTypos = TyposInContext; |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7422 | TyposResolved -= DelayedTypos.size(); |
Nick Lewycky | 4d59b77 | 2014-12-16 22:02:06 +0000 | [diff] [blame] | 7423 | if (Result.isInvalid() || Result.get() != E) { |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7424 | ExprEvalContexts.back().NumTypos -= TyposResolved; |
| 7425 | return Result; |
| 7426 | } |
Nick Lewycky | 4d59b77 | 2014-12-16 22:02:06 +0000 | [diff] [blame] | 7427 | assert(TyposResolved == 0 && "Corrected typo but got same Expr back?"); |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7428 | } |
| 7429 | return E; |
| 7430 | } |
| 7431 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7432 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC, |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7433 | bool DiscardedValue, |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7434 | bool IsConstexpr, |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7435 | bool IsLambdaInitCaptureInitializer) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7436 | ExprResult FullExpr = FE; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7437 | |
| 7438 | if (!FullExpr.get()) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 7439 | return ExprError(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7440 | |
| 7441 | // If we are an init-expression in a lambdas init-capture, we should not |
| 7442 | // diagnose an unexpanded pack now (will be diagnosed once lambda-expr |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7443 | // containing full-expression is done). |
| 7444 | // template<class ... Ts> void test(Ts ... t) { |
| 7445 | // test([&a(t)]() { <-- (t) is an init-expr that shouldn't be diagnosed now. |
| 7446 | // return a; |
| 7447 | // }() ...); |
| 7448 | // } |
| 7449 | // FIXME: This is a hack. It would be better if we pushed the lambda scope |
| 7450 | // when we parse the lambda introducer, and teach capturing (but not |
| 7451 | // unexpanded pack detection) to walk over LambdaScopeInfos which don't have a |
| 7452 | // corresponding class yet (that is, have LambdaScopeInfo either represent a |
| 7453 | // lambda where we've entered the introducer but not the body, or represent a |
| 7454 | // lambda where we've entered the body, depending on where the |
| 7455 | // parser/instantiation has got to). |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7456 | if (!IsLambdaInitCaptureInitializer && |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 7457 | DiagnoseUnexpandedParameterPack(FullExpr.get())) |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 7458 | return ExprError(); |
| 7459 | |
Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7460 | // Top-level expressions default to 'id' when we're in a debugger. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7461 | if (DiscardedValue && getLangOpts().DebuggerCastResultToId && |
Douglas Gregor | b5af2e9 | 2013-03-07 22:57:58 +0000 | [diff] [blame] | 7462 | FullExpr.get()->getType() == Context.UnknownAnyTy) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7463 | FullExpr = forceUnknownAnyToType(FullExpr.get(), Context.getObjCIdType()); |
Douglas Gregor | 95715f9 | 2011-12-15 00:53:32 +0000 | [diff] [blame] | 7464 | if (FullExpr.isInvalid()) |
| 7465 | return ExprError(); |
| 7466 | } |
Douglas Gregor | 0ec210b | 2011-03-07 02:05:23 +0000 | [diff] [blame] | 7467 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7468 | if (DiscardedValue) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7469 | FullExpr = CheckPlaceholderExpr(FullExpr.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7470 | if (FullExpr.isInvalid()) |
| 7471 | return ExprError(); |
| 7472 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7473 | FullExpr = IgnoredValueConversions(FullExpr.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 7474 | if (FullExpr.isInvalid()) |
| 7475 | return ExprError(); |
| 7476 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7477 | |
Kaelyn Takata | 49d8432 | 2014-11-11 23:26:56 +0000 | [diff] [blame] | 7478 | FullExpr = CorrectDelayedTyposInExpr(FullExpr.get()); |
| 7479 | if (FullExpr.isInvalid()) |
| 7480 | return ExprError(); |
Kaelyn Takata | 6c75951 | 2014-10-27 18:07:37 +0000 | [diff] [blame] | 7481 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7482 | CheckCompletedExpr(FullExpr.get(), CC, IsConstexpr); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7483 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7484 | // At the end of this full expression (which could be a deeply nested |
| 7485 | // lambda), if there is a potential capture within the nested lambda, |
Faisal Vali | 218e94b | 2013-11-12 03:56:08 +0000 | [diff] [blame] | 7486 | // have the outer capture-able lambda try and capture it. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7487 | // Consider the following code: |
| 7488 | // void f(int, int); |
| 7489 | // void f(const int&, double); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7490 | // void foo() { |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7491 | // const int x = 10, y = 20; |
| 7492 | // auto L = [=](auto a) { |
| 7493 | // auto M = [=](auto b) { |
| 7494 | // f(x, b); <-- requires x to be captured by L and M |
| 7495 | // f(y, a); <-- requires y to be captured by L, but not all Ms |
| 7496 | // }; |
| 7497 | // }; |
| 7498 | // } |
| 7499 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7500 | // FIXME: Also consider what happens for something like this that involves |
| 7501 | // the gnu-extension statement-expressions or even lambda-init-captures: |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7502 | // void f() { |
| 7503 | // const int n = 0; |
| 7504 | // auto L = [&](auto a) { |
| 7505 | // +n + ({ 0; a; }); |
| 7506 | // }; |
| 7507 | // } |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7508 | // |
| 7509 | // Here, we see +n, and then the full-expression 0; ends, so we don't |
| 7510 | // capture n (and instead remove it from our list of potential captures), |
| 7511 | // 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] | 7512 | // for us to see that we need to capture n after all. |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 7513 | |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7514 | LambdaScopeInfo *const CurrentLSI = |
| 7515 | getCurLambda(/*IgnoreCapturedRegions=*/true); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7516 | // FIXME: PR 17877 showed that getCurLambda() can return a valid pointer |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7517 | // 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] | 7518 | // for an example of the code that might cause this asynchrony. |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7519 | // By ensuring we are in the context of a lambda's call operator |
| 7520 | // 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] | 7521 | // 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] | 7522 | // PR, a proper fix would entail : |
| 7523 | // "Alternative suggestion: |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7524 | // - Add to Sema an integer holding the smallest (outermost) scope |
| 7525 | // index that we are *lexically* within, and save/restore/set to |
| 7526 | // FunctionScopes.size() in InstantiatingTemplate's |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7527 | // constructor/destructor. |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7528 | // - Teach the handful of places that iterate over FunctionScopes to |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7529 | // stop at the outermost enclosing lexical scope." |
Alexey Bataev | 31939e3 | 2016-11-11 12:36:20 +0000 | [diff] [blame] | 7530 | DeclContext *DC = CurContext; |
| 7531 | while (DC && isa<CapturedDecl>(DC)) |
| 7532 | DC = DC->getParent(); |
| 7533 | const bool IsInLambdaDeclContext = isLambdaCallOperator(DC); |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7534 | if (IsInLambdaDeclContext && CurrentLSI && |
Faisal Vali | 8bc2bc7 | 2013-11-12 03:48:27 +0000 | [diff] [blame] | 7535 | CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid()) |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 7536 | CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(FE, CurrentLSI, |
| 7537 | *this); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7538 | return MaybeCreateExprWithCleanups(FullExpr); |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 7539 | } |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 7540 | |
| 7541 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
| 7542 | if (!FullStmt) return StmtError(); |
| 7543 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7544 | return MaybeCreateStmtWithCleanups(FullStmt); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 7545 | } |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7546 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7547 | Sema::IfExistsResult |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7548 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, |
| 7549 | CXXScopeSpec &SS, |
| 7550 | const DeclarationNameInfo &TargetNameInfo) { |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7551 | DeclarationName TargetName = TargetNameInfo.getName(); |
| 7552 | if (!TargetName) |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7553 | return IER_DoesNotExist; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7554 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7555 | // If the name itself is dependent, then the result is dependent. |
| 7556 | if (TargetName.isDependentName()) |
| 7557 | return IER_Dependent; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7558 | |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7559 | // Do the redeclaration lookup in the current scope. |
| 7560 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
| 7561 | Sema::NotForRedeclaration); |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7562 | LookupParsedName(R, S, &SS); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7563 | R.suppressDiagnostics(); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7564 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7565 | switch (R.getResultKind()) { |
| 7566 | case LookupResult::Found: |
| 7567 | case LookupResult::FoundOverloaded: |
| 7568 | case LookupResult::FoundUnresolvedValue: |
| 7569 | case LookupResult::Ambiguous: |
| 7570 | return IER_Exists; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7571 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7572 | case LookupResult::NotFound: |
| 7573 | return IER_DoesNotExist; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7574 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 7575 | case LookupResult::NotFoundInCurrentInstantiation: |
| 7576 | return IER_Dependent; |
| 7577 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7578 | |
| 7579 | llvm_unreachable("Invalid LookupResult Kind!"); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 7580 | } |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7581 | |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7582 | Sema::IfExistsResult |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 7583 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc, |
| 7584 | bool IsIfExists, CXXScopeSpec &SS, |
| 7585 | UnqualifiedId &Name) { |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7586 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7587 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 7588 | // Check for an unexpanded parameter pack. |
| 7589 | auto UPPC = IsIfExists ? UPPC_IfExists : UPPC_IfNotExists; |
| 7590 | if (DiagnoseUnexpandedParameterPack(SS, UPPC) || |
| 7591 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC)) |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 7592 | return IER_Error; |
Simon Pilgrim | 75c2688 | 2016-09-30 14:25:09 +0000 | [diff] [blame] | 7593 | |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 7594 | return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo); |
| 7595 | } |