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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 15 | #include "clang/Sema/DeclSpec.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Initialization.h" |
| 17 | #include "clang/Sema/Lookup.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Scope.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 21 | #include "clang/Sema/TemplateDeduction.h" |
Steve Naroff | aac9415 | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 24 | #include "clang/AST/CXXInheritance.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprCXX.h" |
Fariborz Jahanian | 1d44608 | 2010-06-16 18:56:04 +0000 | [diff] [blame] | 27 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 29 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 32 | #include "TypeLocBuilder.h" |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/APInt.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8b0cf1d | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 36 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 37 | using namespace sema; |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 38 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 39 | ParsedType Sema::getDestructorName(SourceLocation TildeLoc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 40 | IdentifierInfo &II, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 41 | SourceLocation NameLoc, |
| 42 | Scope *S, CXXScopeSpec &SS, |
| 43 | ParsedType ObjectTypePtr, |
| 44 | bool EnteringContext) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 45 | // Determine where to perform name lookup. |
| 46 | |
| 47 | // FIXME: This area of the standard is very messy, and the current |
| 48 | // wording is rather unclear about which scopes we search for the |
| 49 | // destructor name; see core issues 399 and 555. Issue 399 in |
| 50 | // particular shows where the current description of destructor name |
| 51 | // lookup is completely out of line with existing practice, e.g., |
| 52 | // this appears to be ill-formed: |
| 53 | // |
| 54 | // namespace N { |
| 55 | // template <typename T> struct S { |
| 56 | // ~S(); |
| 57 | // }; |
| 58 | // } |
| 59 | // |
| 60 | // void f(N::S<int>* s) { |
| 61 | // s->N::S<int>::~S(); |
| 62 | // } |
| 63 | // |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 64 | // See also PR6358 and PR6359. |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 65 | // For this reason, we're currently only doing the C++03 version of this |
| 66 | // 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] | 67 | QualType SearchType; |
| 68 | DeclContext *LookupCtx = 0; |
| 69 | bool isDependent = false; |
| 70 | bool LookInScope = false; |
| 71 | |
| 72 | // If we have an object type, it's because we are in a |
| 73 | // pseudo-destructor-expression or a member access expression, and |
| 74 | // we know what type we're looking for. |
| 75 | if (ObjectTypePtr) |
| 76 | SearchType = GetTypeFromParser(ObjectTypePtr); |
| 77 | |
| 78 | if (SS.isSet()) { |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 79 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 80 | |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 81 | bool AlreadySearched = false; |
| 82 | bool LookAtPrefix = true; |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 83 | // C++ [basic.lookup.qual]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 84 | // 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] | 85 | // the type-names are looked up as types in the scope designated by the |
| 86 | // nested-name-specifier. In a qualified-id of the form: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 87 | // |
| 88 | // ::[opt] nested-name-specifier ~ class-name |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 89 | // |
| 90 | // where the nested-name-specifier designates a namespace scope, and in |
Chandler Carruth | 8f25481 | 2010-02-21 10:19:54 +0000 | [diff] [blame] | 91 | // a qualified-id of the form: |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 92 | // |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 93 | // ::opt nested-name-specifier class-name :: ~ class-name |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 94 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 95 | // the class-names are looked up as types in the scope designated by |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 96 | // the nested-name-specifier. |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 97 | // |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 98 | // Here, we check the first case (completely) and determine whether the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 99 | // code below is permitted to look at the prefix of the |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 100 | // nested-name-specifier. |
| 101 | DeclContext *DC = computeDeclContext(SS, EnteringContext); |
| 102 | if (DC && DC->isFileContext()) { |
| 103 | AlreadySearched = true; |
| 104 | LookupCtx = DC; |
| 105 | isDependent = false; |
| 106 | } else if (DC && isa<CXXRecordDecl>(DC)) |
| 107 | LookAtPrefix = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 108 | |
Sebastian Redl | a771d22 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 109 | // The second case from the C++03 rules quoted further above. |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 110 | NestedNameSpecifier *Prefix = 0; |
| 111 | if (AlreadySearched) { |
| 112 | // Nothing left to do. |
| 113 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { |
| 114 | CXXScopeSpec PrefixSS; |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 115 | PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data())); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 116 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); |
| 117 | isDependent = isDependentScopeSpecifier(PrefixSS); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 118 | } else if (ObjectTypePtr) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 119 | LookupCtx = computeDeclContext(SearchType); |
| 120 | isDependent = SearchType->isDependentType(); |
| 121 | } else { |
| 122 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Douglas Gregor | 46841e1 | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 123 | isDependent = LookupCtx && LookupCtx->isDependentContext(); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 124 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 125 | |
Douglas Gregor | cd3f49f | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 126 | LookInScope = false; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 127 | } else if (ObjectTypePtr) { |
| 128 | // C++ [basic.lookup.classref]p3: |
| 129 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 130 | // in the context of the entire postfix-expression. If the type T |
| 131 | // of the object expression is of a class type C, the type-name is |
| 132 | // also looked up in the scope of class C. At least one of the |
| 133 | // lookups shall find a name that refers to (possibly |
| 134 | // cv-qualified) T. |
| 135 | LookupCtx = computeDeclContext(SearchType); |
| 136 | isDependent = SearchType->isDependentType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 137 | assert((isDependent || !SearchType->isIncompleteType()) && |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 138 | "Caller should have completed object type"); |
| 139 | |
| 140 | LookInScope = true; |
| 141 | } else { |
| 142 | // Perform lookup into the current scope (only). |
| 143 | LookInScope = true; |
| 144 | } |
| 145 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 146 | TypeDecl *NonMatchingTypeDecl = 0; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 147 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); |
| 148 | for (unsigned Step = 0; Step != 2; ++Step) { |
| 149 | // Look for the name first in the computed lookup context (if we |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 150 | // 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] | 151 | // we're allowed to look there). |
| 152 | Found.clear(); |
| 153 | if (Step == 0 && LookupCtx) |
| 154 | LookupQualifiedName(Found, LookupCtx); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 155 | else if (Step == 1 && LookInScope && S) |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 156 | LookupName(Found, S); |
| 157 | else |
| 158 | continue; |
| 159 | |
| 160 | // FIXME: Should we be suppressing ambiguities here? |
| 161 | if (Found.isAmbiguous()) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 162 | return ParsedType(); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 163 | |
| 164 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
| 165 | QualType T = Context.getTypeDeclType(Type); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 166 | |
| 167 | if (SearchType.isNull() || SearchType->isDependentType() || |
| 168 | Context.hasSameUnqualifiedType(T, SearchType)) { |
| 169 | // We found our type! |
| 170 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 171 | return ParsedType::make(T); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 172 | } |
John Wiegley | b4a9e51 | 2011-03-08 08:13:22 +0000 | [diff] [blame] | 173 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 174 | if (!SearchType.isNull()) |
| 175 | NonMatchingTypeDecl = Type; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // If the name that we found is a class template name, and it is |
| 179 | // the same name as the template name in the last part of the |
| 180 | // nested-name-specifier (if present) or the object type, then |
| 181 | // this is the destructor for that class. |
| 182 | // FIXME: This is a workaround until we get real drafting for core |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 183 | // issue 399, for which there isn't even an obvious direction. |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 184 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
| 185 | QualType MemberOfType; |
| 186 | if (SS.isSet()) { |
| 187 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
| 188 | // Figure out the type of the context, if it has one. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 189 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) |
| 190 | MemberOfType = Context.getTypeDeclType(Record); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | if (MemberOfType.isNull()) |
| 194 | MemberOfType = SearchType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 196 | if (MemberOfType.isNull()) |
| 197 | continue; |
| 198 | |
| 199 | // We're referring into a class template specialization. If the |
| 200 | // class template we found is the same as the template being |
| 201 | // specialized, we found what we are looking for. |
| 202 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
| 203 | if (ClassTemplateSpecializationDecl *Spec |
| 204 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
| 205 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
| 206 | Template->getCanonicalDecl()) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 207 | return ParsedType::make(MemberOfType); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | continue; |
| 211 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 213 | // We're referring to an unresolved class template |
| 214 | // specialization. Determine whether we class template we found |
| 215 | // is the same as the template being specialized or, if we don't |
| 216 | // know which template is being specialized, that it at least |
| 217 | // has the same name. |
| 218 | if (const TemplateSpecializationType *SpecType |
| 219 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
| 220 | TemplateName SpecName = SpecType->getTemplateName(); |
| 221 | |
| 222 | // The class template we found is the same template being |
| 223 | // specialized. |
| 224 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
| 225 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 226 | return ParsedType::make(MemberOfType); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 227 | |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | // The class template we found has the same name as the |
| 232 | // (dependent) template name being specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 233 | if (DependentTemplateName *DepTemplate |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 234 | = SpecName.getAsDependentTemplateName()) { |
| 235 | if (DepTemplate->isIdentifier() && |
| 236 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 237 | return ParsedType::make(MemberOfType); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 238 | |
| 239 | continue; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if (isDependent) { |
| 246 | // We didn't find our type, but that's okay: it's dependent |
| 247 | // anyway. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 248 | |
| 249 | // FIXME: What if we have no nested-name-specifier? |
| 250 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), |
| 251 | SS.getWithLocInContext(Context), |
| 252 | II, NameLoc); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 253 | return ParsedType::make(T); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Douglas Gregor | 4cf85a7 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 256 | if (NonMatchingTypeDecl) { |
| 257 | QualType T = Context.getTypeDeclType(NonMatchingTypeDecl); |
| 258 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) |
| 259 | << T << SearchType; |
| 260 | Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here) |
| 261 | << T; |
| 262 | } else if (ObjectTypePtr) |
| 263 | Diag(NameLoc, diag::err_ident_in_dtor_not_a_type) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 264 | << &II; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 265 | else |
| 266 | Diag(NameLoc, diag::err_destructor_class_name); |
| 267 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 268 | return ParsedType(); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 269 | } |
| 270 | |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 271 | ParsedType Sema::getDestructorType(const DeclSpec& DS, ParsedType ObjectType) { |
David Blaikie | 08608f6 | 2011-12-12 04:13:55 +0000 | [diff] [blame] | 272 | if (DS.getTypeSpecType() == DeclSpec::TST_error || !ObjectType) |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 273 | return ParsedType(); |
| 274 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype |
| 275 | && "only get destructor types from declspecs"); |
| 276 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 277 | QualType SearchType = GetTypeFromParser(ObjectType); |
| 278 | if (SearchType->isDependentType() || Context.hasSameUnqualifiedType(SearchType, T)) { |
| 279 | return ParsedType::make(T); |
| 280 | } |
| 281 | |
| 282 | Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch) |
| 283 | << T << SearchType; |
| 284 | return ParsedType(); |
| 285 | } |
| 286 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 287 | /// \brief Build a C++ typeid expression with a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 288 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 289 | SourceLocation TypeidLoc, |
| 290 | TypeSourceInfo *Operand, |
| 291 | SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 292 | // C++ [expr.typeid]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 293 | // 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] | 294 | // that is the operand of typeid are always ignored. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 295 | // 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] | 296 | // type, the class shall be completely-defined. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 297 | Qualifiers Quals; |
| 298 | QualType T |
| 299 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
| 300 | Quals); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 301 | if (T->getAs<RecordType>() && |
| 302 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 303 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 304 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 305 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
| 306 | Operand, |
| 307 | SourceRange(TypeidLoc, RParenLoc))); |
| 308 | } |
| 309 | |
| 310 | /// \brief Build a C++ typeid expression with an expression operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 311 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 312 | SourceLocation TypeidLoc, |
| 313 | Expr *E, |
| 314 | SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 315 | if (E && !E->isTypeDependent()) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 316 | if (E->getType()->isPlaceholderType()) { |
| 317 | ExprResult result = CheckPlaceholderExpr(E); |
| 318 | if (result.isInvalid()) return ExprError(); |
| 319 | E = result.take(); |
| 320 | } |
| 321 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 322 | QualType T = E->getType(); |
| 323 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
| 324 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 325 | // C++ [expr.typeid]p3: |
| 326 | // [...] If the type of the expression is a class type, the class |
| 327 | // shall be completely-defined. |
| 328 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 329 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 330 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 331 | // C++ [expr.typeid]p3: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 332 | // 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] | 333 | // polymorphic class type [...] [the] expression is an unevaluated |
| 334 | // operand. [...] |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 335 | if (RecordD->isPolymorphic() && E->Classify(Context).isGLValue()) { |
Eli Friedman | 456f018 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 336 | // The subexpression is potentially evaluated; switch the context |
| 337 | // and recheck the subexpression. |
| 338 | ExprResult Result = TranformToPotentiallyEvaluated(E); |
| 339 | if (Result.isInvalid()) return ExprError(); |
| 340 | E = Result.take(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 341 | |
| 342 | // We require a vtable to query the type at run time. |
| 343 | MarkVTableUsed(TypeidLoc, RecordD); |
| 344 | } |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 345 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 346 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 347 | // C++ [expr.typeid]p4: |
| 348 | // [...] 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] | 349 | // cv-qualified type, the result of the typeid expression refers to a |
| 350 | // std::type_info object representing the cv-unqualified referenced |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 351 | // type. |
Douglas Gregor | 876cec2 | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 352 | Qualifiers Quals; |
| 353 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
| 354 | if (!Context.hasSameType(T, UnqualT)) { |
| 355 | T = UnqualT; |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 356 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).take(); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 359 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 360 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 361 | E, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 362 | SourceRange(TypeidLoc, RParenLoc))); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 366 | ExprResult |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 367 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 368 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 369 | // Find the std::type_info type. |
Sebastian Redl | 7ac9741 | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 370 | if (!getStdNamespace()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 371 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 372 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 373 | if (!CXXTypeInfoDecl) { |
| 374 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 375 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 376 | LookupQualifiedName(R, getStdNamespace()); |
| 377 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 378 | if (!CXXTypeInfoDecl) |
| 379 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 380 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 381 | |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 382 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 384 | if (isType) { |
| 385 | // The operand is a type; handle it as such. |
| 386 | TypeSourceInfo *TInfo = 0; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 387 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 388 | &TInfo); |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 389 | if (T.isNull()) |
| 390 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 392 | if (!TInfo) |
| 393 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | 9da6419 | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 395 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 396 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 398 | // The operand is an expression. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 399 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 402 | /// Retrieve the UuidAttr associated with QT. |
| 403 | static UuidAttr *GetUuidAttrOfType(QualType QT) { |
| 404 | // Optionally remove one level of pointer, reference or array indirection. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 405 | const Type *Ty = QT.getTypePtr();; |
Francois Pichet | 9dddd40 | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 406 | if (QT->isPointerType() || QT->isReferenceType()) |
| 407 | Ty = QT->getPointeeType().getTypePtr(); |
| 408 | else if (QT->isArrayType()) |
| 409 | Ty = cast<ArrayType>(QT)->getElementType().getTypePtr(); |
| 410 | |
Francois Pichet | 59d2b01 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 411 | // Loop all record redeclaration looking for an uuid attribute. |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 412 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
Francois Pichet | 59d2b01 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 413 | for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(), |
| 414 | E = RD->redecls_end(); I != E; ++I) { |
| 415 | if (UuidAttr *Uuid = I->getAttr<UuidAttr>()) |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 416 | return Uuid; |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 417 | } |
Francois Pichet | 59d2b01 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 418 | |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 419 | return 0; |
Francois Pichet | 9dddd40 | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 422 | /// \brief Build a Microsoft __uuidof expression with a type operand. |
| 423 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 424 | SourceLocation TypeidLoc, |
| 425 | TypeSourceInfo *Operand, |
| 426 | SourceLocation RParenLoc) { |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 427 | if (!Operand->getType()->isDependentType()) { |
| 428 | if (!GetUuidAttrOfType(Operand->getType())) |
| 429 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 430 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 431 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 432 | // FIXME: add __uuidof semantic analysis for type operand. |
| 433 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 434 | Operand, |
| 435 | SourceRange(TypeidLoc, RParenLoc))); |
| 436 | } |
| 437 | |
| 438 | /// \brief Build a Microsoft __uuidof expression with an expression operand. |
| 439 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 440 | SourceLocation TypeidLoc, |
| 441 | Expr *E, |
| 442 | SourceLocation RParenLoc) { |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 443 | if (!E->getType()->isDependentType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 444 | if (!GetUuidAttrOfType(E->getType()) && |
Francois Pichet | b757765 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 445 | !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 446 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 447 | } |
| 448 | // FIXME: add __uuidof semantic analysis for type operand. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 449 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 450 | E, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 451 | SourceRange(TypeidLoc, RParenLoc))); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 455 | ExprResult |
| 456 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 457 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 458 | // If MSVCGuidDecl has not been cached, do the lookup. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 459 | if (!MSVCGuidDecl) { |
| 460 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 461 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 462 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 463 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 464 | if (!MSVCGuidDecl) |
| 465 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 468 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 469 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 470 | if (isType) { |
| 471 | // The operand is a type; handle it as such. |
| 472 | TypeSourceInfo *TInfo = 0; |
| 473 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 474 | &TInfo); |
| 475 | if (T.isNull()) |
| 476 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 477 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 478 | if (!TInfo) |
| 479 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 480 | |
| 481 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 482 | } |
| 483 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 484 | // The operand is an expression. |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 485 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 486 | } |
| 487 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 488 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 489 | ExprResult |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 490 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 08d918a | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 491 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Bill Wendling | bf313b0 | 2007-02-13 20:09:46 +0000 | [diff] [blame] | 492 | "Unknown C++ Boolean value!"); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 493 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 494 | Context.BoolTy, OpLoc)); |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 496 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 497 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 498 | ExprResult |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 499 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
| 500 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 501 | } |
| 502 | |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 503 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 504 | ExprResult |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 505 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
| 506 | bool IsThrownVarInScope = false; |
| 507 | if (Ex) { |
| 508 | // C++0x [class.copymove]p31: |
| 509 | // When certain criteria are met, an implementation is allowed to omit the |
| 510 | // copy/move construction of a class object [...] |
| 511 | // |
| 512 | // - in a throw-expression, when the operand is the name of a |
| 513 | // non-volatile automatic object (other than a function or catch- |
| 514 | // clause parameter) whose scope does not extend beyond the end of the |
| 515 | // innermost enclosing try-block (if there is one), the copy/move |
| 516 | // operation from the operand to the exception object (15.1) can be |
| 517 | // omitted by constructing the automatic object directly into the |
| 518 | // exception object |
| 519 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) |
| 520 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 521 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { |
| 522 | for( ; S; S = S->getParent()) { |
| 523 | if (S->isDeclScope(Var)) { |
| 524 | IsThrownVarInScope = true; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | if (S->getFlags() & |
| 529 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
| 530 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | |
| 531 | Scope::TryScope)) |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
| 539 | } |
| 540 | |
| 541 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
| 542 | bool IsThrownVarInScope) { |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 543 | // Don't report an error if 'throw' is used in system headers. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 544 | if (!getLangOpts().CXXExceptions && |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 545 | !getSourceManager().isInSystemHeader(OpLoc)) |
Anders Carlsson | b94ad3e | 2011-02-19 21:53:09 +0000 | [diff] [blame] | 546 | Diag(OpLoc, diag::err_exceptions_disabled) << "throw"; |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 547 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 548 | if (Ex && !Ex->isTypeDependent()) { |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 549 | ExprResult ExRes = CheckCXXThrowOperand(OpLoc, Ex, IsThrownVarInScope); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 550 | if (ExRes.isInvalid()) |
| 551 | return ExprError(); |
| 552 | Ex = ExRes.take(); |
| 553 | } |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 554 | |
| 555 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc, |
| 556 | IsThrownVarInScope)); |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 560 | ExprResult Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *E, |
| 561 | bool IsThrownVarInScope) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 562 | // C++ [except.throw]p3: |
Douglas Gregor | 247894b | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 563 | // A throw-expression initializes a temporary object, called the exception |
| 564 | // object, the type of which is determined by removing any top-level |
| 565 | // cv-qualifiers from the static type of the operand of throw and adjusting |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | // the type from "array of T" or "function returning T" to "pointer to T" |
Douglas Gregor | 247894b | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 567 | // or "pointer to function returning T", [...] |
| 568 | if (E->getType().hasQualifiers()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 569 | E = ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 570 | E->getValueKind()).take(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 571 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 572 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 573 | if (Res.isInvalid()) |
| 574 | return ExprError(); |
| 575 | E = Res.take(); |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 576 | |
| 577 | // If the type of the exception would be an incomplete type or a pointer |
| 578 | // to an incomplete type other than (cv) void the program is ill-formed. |
| 579 | QualType Ty = E->getType(); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 580 | bool isPointer = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 581 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 582 | Ty = Ptr->getPointeeType(); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 583 | isPointer = true; |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 584 | } |
| 585 | if (!isPointer || !Ty->isVoidType()) { |
| 586 | if (RequireCompleteType(ThrowLoc, Ty, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 587 | isPointer? diag::err_throw_incomplete_ptr |
| 588 | : diag::err_throw_incomplete, |
| 589 | E->getSourceRange())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 590 | return ExprError(); |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 591 | |
Douglas Gregor | e815433 | 2010-04-15 18:05:39 +0000 | [diff] [blame] | 592 | if (RequireNonAbstractType(ThrowLoc, E->getType(), |
| 593 | PDiag(diag::err_throw_abstract_type) |
| 594 | << E->getSourceRange())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 595 | return ExprError(); |
Sebastian Redl | 4de47b4 | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 596 | } |
| 597 | |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 598 | // Initialize the exception result. This implicitly weeds out |
| 599 | // abstract types or types with inaccessible copy constructors. |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 600 | |
| 601 | // C++0x [class.copymove]p31: |
| 602 | // When certain criteria are met, an implementation is allowed to omit the |
| 603 | // copy/move construction of a class object [...] |
| 604 | // |
| 605 | // - in a throw-expression, when the operand is the name of a |
| 606 | // non-volatile automatic object (other than a function or catch-clause |
| 607 | // parameter) whose scope does not extend beyond the end of the |
| 608 | // innermost enclosing try-block (if there is one), the copy/move |
| 609 | // operation from the operand to the exception object (15.1) can be |
| 610 | // omitted by constructing the automatic object directly into the |
| 611 | // exception object |
| 612 | const VarDecl *NRVOVariable = 0; |
| 613 | if (IsThrownVarInScope) |
| 614 | NRVOVariable = getCopyElisionCandidate(QualType(), E, false); |
| 615 | |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 616 | InitializedEntity Entity = |
Douglas Gregor | c74edc2 | 2011-01-21 22:46:35 +0000 | [diff] [blame] | 617 | InitializedEntity::InitializeException(ThrowLoc, E->getType(), |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 618 | /*NRVO=*/NRVOVariable != 0); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 619 | Res = PerformMoveOrCopyInitialization(Entity, NRVOVariable, |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 620 | QualType(), E, |
| 621 | IsThrownVarInScope); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 622 | if (Res.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 623 | return ExprError(); |
| 624 | E = Res.take(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 625 | |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 626 | // If the exception has class type, we need additional handling. |
| 627 | const RecordType *RecordTy = Ty->getAs<RecordType>(); |
| 628 | if (!RecordTy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 629 | return Owned(E); |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 630 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 631 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 632 | // If we are throwing a polymorphic class type or pointer thereof, |
| 633 | // exception handling will make use of the vtable. |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 634 | MarkVTableUsed(ThrowLoc, RD); |
| 635 | |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 636 | // If a pointer is thrown, the referenced object will not be destroyed. |
| 637 | if (isPointer) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 638 | return Owned(E); |
Eli Friedman | 36ebbec | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 639 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 640 | // If the class has a destructor, we must be able to call it. |
| 641 | if (RD->hasIrrelevantDestructor()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 642 | return Owned(E); |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 643 | |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 644 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 645 | if (!Destructor) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 646 | return Owned(E); |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 647 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 648 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
Eli Friedman | 91a3d27 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 649 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 650 | PDiag(diag::err_access_dtor_exception) << Ty); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 651 | DiagnoseUseOfDecl(Destructor, E->getExprLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 652 | return Owned(E); |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 653 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 654 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 655 | QualType Sema::getCurrentThisType() { |
| 656 | DeclContext *DC = getFunctionLevelDeclContext(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 657 | QualType ThisTy = CXXThisTypeOverride; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 658 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { |
| 659 | if (method && method->isInstance()) |
| 660 | ThisTy = method->getThisType(Context); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 661 | } |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 662 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 663 | return ThisTy; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 666 | Sema::CXXThisScopeRAII::CXXThisScopeRAII(Sema &S, |
| 667 | Decl *ContextDecl, |
| 668 | unsigned CXXThisTypeQuals, |
| 669 | bool Enabled) |
| 670 | : S(S), OldCXXThisTypeOverride(S.CXXThisTypeOverride), Enabled(false) |
| 671 | { |
| 672 | if (!Enabled || !ContextDecl) |
| 673 | return; |
| 674 | |
| 675 | CXXRecordDecl *Record = 0; |
| 676 | if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(ContextDecl)) |
| 677 | Record = Template->getTemplatedDecl(); |
| 678 | else |
| 679 | Record = cast<CXXRecordDecl>(ContextDecl); |
| 680 | |
| 681 | S.CXXThisTypeOverride |
| 682 | = S.Context.getPointerType( |
| 683 | S.Context.getRecordType(Record).withCVRQualifiers(CXXThisTypeQuals)); |
| 684 | |
| 685 | this->Enabled = true; |
| 686 | } |
| 687 | |
| 688 | |
| 689 | Sema::CXXThisScopeRAII::~CXXThisScopeRAII() { |
| 690 | if (Enabled) { |
| 691 | S.CXXThisTypeOverride = OldCXXThisTypeOverride; |
| 692 | } |
| 693 | } |
| 694 | |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 695 | void Sema::CheckCXXThisCapture(SourceLocation Loc, bool Explicit) { |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 696 | // We don't need to capture this in an unevaluated context. |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 697 | if (ExprEvalContexts.back().Context == Unevaluated && !Explicit) |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 698 | return; |
| 699 | |
| 700 | // Otherwise, check that we can capture 'this'. |
| 701 | unsigned NumClosures = 0; |
| 702 | for (unsigned idx = FunctionScopes.size() - 1; idx != 0; idx--) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 703 | if (CapturingScopeInfo *CSI = |
| 704 | dyn_cast<CapturingScopeInfo>(FunctionScopes[idx])) { |
| 705 | if (CSI->CXXThisCaptureIndex != 0) { |
| 706 | // 'this' is already being captured; there isn't anything more to do. |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 707 | break; |
| 708 | } |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 709 | |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 710 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref || |
Douglas Gregor | a1bffa2 | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 711 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByval || |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 712 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block || |
| 713 | Explicit) { |
| 714 | // This closure can capture 'this'; continue looking upwards. |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 715 | NumClosures++; |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 716 | Explicit = false; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 717 | continue; |
| 718 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 719 | // This context can't implicitly capture 'this'; fail out. |
Douglas Gregor | cdd11d4 | 2012-02-01 17:04:21 +0000 | [diff] [blame] | 720 | Diag(Loc, diag::err_this_capture) << Explicit; |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 721 | return; |
| 722 | } |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | |
| 726 | // Mark that we're implicitly capturing 'this' in all the scopes we skipped. |
| 727 | // FIXME: We need to delay this marking in PotentiallyPotentiallyEvaluated |
| 728 | // contexts. |
| 729 | for (unsigned idx = FunctionScopes.size() - 1; |
| 730 | NumClosures; --idx, --NumClosures) { |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 731 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]); |
Eli Friedman | c975106 | 2012-02-11 02:51:16 +0000 | [diff] [blame] | 732 | Expr *ThisExpr = 0; |
Douglas Gregor | fdf598e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 733 | QualType ThisTy = getCurrentThisType(); |
Eli Friedman | c975106 | 2012-02-11 02:51:16 +0000 | [diff] [blame] | 734 | if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI)) { |
| 735 | // For lambda expressions, build a field and an initializing expression. |
Eli Friedman | c975106 | 2012-02-11 02:51:16 +0000 | [diff] [blame] | 736 | CXXRecordDecl *Lambda = LSI->Lambda; |
| 737 | FieldDecl *Field |
| 738 | = FieldDecl::Create(Context, Lambda, Loc, Loc, 0, ThisTy, |
| 739 | Context.getTrivialTypeSourceInfo(ThisTy, Loc), |
| 740 | 0, false, false); |
| 741 | Field->setImplicit(true); |
| 742 | Field->setAccess(AS_private); |
| 743 | Lambda->addDecl(Field); |
| 744 | ThisExpr = new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/true); |
| 745 | } |
Eli Friedman | 20139d3 | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 746 | bool isNested = NumClosures > 1; |
Douglas Gregor | fdf598e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 747 | CSI->addThisCapture(isNested, Loc, ThisTy, ThisExpr); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 751 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 752 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 753 | /// is a non-lvalue expression whose value is the address of the object for |
| 754 | /// which the function is called. |
| 755 | |
Douglas Gregor | 09deffa | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 756 | QualType ThisTy = getCurrentThisType(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 757 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 758 | |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 759 | CheckCXXThisCapture(Loc); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 760 | return Owned(new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false)); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 761 | } |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 762 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 763 | bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) { |
| 764 | // If we're outside the body of a member function, then we'll have a specified |
| 765 | // type for 'this'. |
| 766 | if (CXXThisTypeOverride.isNull()) |
| 767 | return false; |
| 768 | |
| 769 | // Determine whether we're looking into a class that's currently being |
| 770 | // defined. |
| 771 | CXXRecordDecl *Class = BaseType->getAsCXXRecordDecl(); |
| 772 | return Class && Class->isBeingDefined(); |
| 773 | } |
| 774 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 775 | ExprResult |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 776 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 777 | SourceLocation LParenLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 778 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 779 | SourceLocation RParenLoc) { |
Douglas Gregor | 7df89f5 | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 780 | if (!TypeRep) |
| 781 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 782 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 783 | TypeSourceInfo *TInfo; |
| 784 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 785 | if (!TInfo) |
| 786 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 787 | |
| 788 | return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); |
| 789 | } |
| 790 | |
| 791 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 792 | /// Can be interpreted either as function-style casting ("int(x)") |
| 793 | /// or class type construction ("ClassType(x,y,z)") |
| 794 | /// or creation of a value-initialized type ("int()"). |
| 795 | ExprResult |
| 796 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
| 797 | SourceLocation LParenLoc, |
| 798 | MultiExprArg exprs, |
| 799 | SourceLocation RParenLoc) { |
| 800 | QualType Ty = TInfo->getType(); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 801 | unsigned NumExprs = exprs.size(); |
| 802 | Expr **Exprs = (Expr**)exprs.get(); |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 803 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 804 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 805 | if (Ty->isDependentType() || |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 806 | CallExpr::hasAnyTypeDependentArguments( |
| 807 | llvm::makeArrayRef(Exprs, NumExprs))) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 808 | exprs.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 810 | return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo, |
Douglas Gregor | ce93414 | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 811 | LParenLoc, |
| 812 | Exprs, NumExprs, |
| 813 | RParenLoc)); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 816 | bool ListInitialization = LParenLoc.isInvalid(); |
| 817 | assert((!ListInitialization || (NumExprs == 1 && isa<InitListExpr>(Exprs[0]))) |
| 818 | && "List initialization must have initializer list as expression."); |
| 819 | SourceRange FullRange = SourceRange(TyBeginLoc, |
| 820 | ListInitialization ? Exprs[0]->getSourceRange().getEnd() : RParenLoc); |
| 821 | |
Douglas Gregor | dd04d33 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 822 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 823 | // If the expression list is a single expression, the type conversion |
| 824 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 825 | // corresponding cast expression. |
Sebastian Redl | 2b80af4 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 826 | if (NumExprs == 1 && !ListInitialization) { |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 827 | Expr *Arg = Exprs[0]; |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 828 | exprs.release(); |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 829 | return BuildCXXFunctionalCastExpr(TInfo, LParenLoc, Arg, RParenLoc); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 832 | QualType ElemTy = Ty; |
| 833 | if (Ty->isArrayType()) { |
| 834 | if (!ListInitialization) |
| 835 | return ExprError(Diag(TyBeginLoc, |
| 836 | diag::err_value_init_for_array_type) << FullRange); |
| 837 | ElemTy = Context.getBaseElementType(Ty); |
| 838 | } |
| 839 | |
| 840 | if (!Ty->isVoidType() && |
| 841 | RequireCompleteType(TyBeginLoc, ElemTy, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 842 | diag::err_invalid_incomplete_type_use, FullRange)) |
Eli Friedman | 576cbd0 | 2012-02-29 00:00:28 +0000 | [diff] [blame] | 843 | return ExprError(); |
| 844 | |
| 845 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 846 | diag::err_allocation_of_abstract_type)) |
| 847 | return ExprError(); |
| 848 | |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 849 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 850 | InitializationKind Kind |
Sebastian Redl | 2b80af4 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 851 | = NumExprs ? ListInitialization |
| 852 | ? InitializationKind::CreateDirectList(TyBeginLoc) |
| 853 | : InitializationKind::CreateDirect(TyBeginLoc, |
| 854 | LParenLoc, RParenLoc) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 855 | : InitializationKind::CreateValue(TyBeginLoc, |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 856 | LParenLoc, RParenLoc); |
| 857 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); |
| 858 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(exprs)); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 859 | |
Sebastian Redl | 2b80af4 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 860 | if (!Result.isInvalid() && ListInitialization && |
| 861 | isa<InitListExpr>(Result.get())) { |
| 862 | // If the list-initialization doesn't involve a constructor call, we'll get |
| 863 | // the initializer-list (with corrected type) back, but that's not what we |
| 864 | // want, since it will be treated as an initializer list in further |
| 865 | // processing. Explicitly insert a cast here. |
| 866 | InitListExpr *List = cast<InitListExpr>(Result.take()); |
| 867 | Result = Owned(CXXFunctionalCastExpr::Create(Context, List->getType(), |
| 868 | Expr::getValueKindForType(TInfo->getType()), |
| 869 | TInfo, TyBeginLoc, CK_NoOp, |
| 870 | List, /*Path=*/0, RParenLoc)); |
| 871 | } |
| 872 | |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 873 | // FIXME: Improve AST representation? |
| 874 | return move(Result); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 875 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 876 | |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 877 | /// doesUsualArrayDeleteWantSize - Answers whether the usual |
| 878 | /// operator delete[] for the given type has a size_t parameter. |
| 879 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
| 880 | QualType allocType) { |
| 881 | const RecordType *record = |
| 882 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 883 | if (!record) return false; |
| 884 | |
| 885 | // Try to find an operator delete[] in class scope. |
| 886 | |
| 887 | DeclarationName deleteName = |
| 888 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
| 889 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
| 890 | S.LookupQualifiedName(ops, record->getDecl()); |
| 891 | |
| 892 | // We're just doing this for information. |
| 893 | ops.suppressDiagnostics(); |
| 894 | |
| 895 | // Very likely: there's no operator delete[]. |
| 896 | if (ops.empty()) return false; |
| 897 | |
| 898 | // If it's ambiguous, it should be illegal to call operator delete[] |
| 899 | // on this thing, so it doesn't matter if we allocate extra space or not. |
| 900 | if (ops.isAmbiguous()) return false; |
| 901 | |
| 902 | LookupResult::Filter filter = ops.makeFilter(); |
| 903 | while (filter.hasNext()) { |
| 904 | NamedDecl *del = filter.next()->getUnderlyingDecl(); |
| 905 | |
| 906 | // C++0x [basic.stc.dynamic.deallocation]p2: |
| 907 | // A template instance is never a usual deallocation function, |
| 908 | // regardless of its signature. |
| 909 | if (isa<FunctionTemplateDecl>(del)) { |
| 910 | filter.erase(); |
| 911 | continue; |
| 912 | } |
| 913 | |
| 914 | // C++0x [basic.stc.dynamic.deallocation]p2: |
| 915 | // If class T does not declare [an operator delete[] with one |
| 916 | // parameter] but does declare a member deallocation function |
| 917 | // named operator delete[] with exactly two parameters, the |
| 918 | // second of which has type std::size_t, then this function |
| 919 | // is a usual deallocation function. |
| 920 | if (!cast<CXXMethodDecl>(del)->isUsualDeallocationFunction()) { |
| 921 | filter.erase(); |
| 922 | continue; |
| 923 | } |
| 924 | } |
| 925 | filter.done(); |
| 926 | |
| 927 | if (!ops.isSingleResult()) return false; |
| 928 | |
| 929 | const FunctionDecl *del = cast<FunctionDecl>(ops.getFoundDecl()); |
| 930 | return (del->getNumParams() == 2); |
| 931 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 932 | |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 933 | /// \brief Parsed a C++ 'new' expression (C++ 5.3.4). |
| 934 | |
| 935 | /// E.g.: |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 936 | /// @code new (memory) int[size][4] @endcode |
| 937 | /// or |
| 938 | /// @code ::new Foo(23, "hello") @endcode |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 939 | /// |
| 940 | /// \param StartLoc The first location of the expression. |
| 941 | /// \param UseGlobal True if 'new' was prefixed with '::'. |
| 942 | /// \param PlacementLParen Opening paren of the placement arguments. |
| 943 | /// \param PlacementArgs Placement new arguments. |
| 944 | /// \param PlacementRParen Closing paren of the placement arguments. |
| 945 | /// \param TypeIdParens If the type is in parens, the source range. |
| 946 | /// \param D The type to be allocated, as well as array dimensions. |
| 947 | /// \param ConstructorLParen Opening paren of the constructor args, empty if |
| 948 | /// initializer-list syntax is used. |
| 949 | /// \param ConstructorArgs Constructor/initialization arguments. |
| 950 | /// \param ConstructorRParen Closing paren of the constructor args. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 951 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 952 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 953 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 955 | Declarator &D, Expr *Initializer) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 956 | bool TypeContainsAuto = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 957 | |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 958 | Expr *ArraySize = 0; |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 959 | // If the specified type is an array, unwrap it and save the expression. |
| 960 | if (D.getNumTypeObjects() > 0 && |
| 961 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 962 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 963 | if (TypeContainsAuto) |
| 964 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
| 965 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 966 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 967 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 968 | << D.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 969 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 970 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 971 | << D.getSourceRange()); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 972 | |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 973 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 974 | D.DropFirstTypeObject(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 977 | // Every dimension shall be of constant size. |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 978 | if (ArraySize) { |
| 979 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 980 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 981 | break; |
| 982 | |
| 983 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 984 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 985 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent()) { |
| 986 | Array.NumElts = VerifyIntegerConstantExpression(NumElts, 0, |
| 987 | PDiag(diag::err_new_array_nonconst)).take(); |
| 988 | if (!Array.NumElts) |
| 989 | return ExprError(); |
Douglas Gregor | 73341c4 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | } |
| 993 | } |
Sebastian Redl | d7b3d7d | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 994 | |
Argyrios Kyrtzidis | 3ff1357 | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 995 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/0); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 996 | QualType AllocType = TInfo->getType(); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 997 | if (D.isInvalidType()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 998 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 999 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1000 | SourceRange DirectInitRange; |
| 1001 | if (ParenListExpr *List = dyn_cast_or_null<ParenListExpr>(Initializer)) |
| 1002 | DirectInitRange = List->getSourceRange(); |
| 1003 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | return BuildCXXNew(StartLoc, UseGlobal, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1005 | PlacementLParen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | move(PlacementArgs), |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1007 | PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1008 | TypeIdParens, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1010 | TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1011 | ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1012 | DirectInitRange, |
| 1013 | Initializer, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1014 | TypeContainsAuto); |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1017 | static bool isLegalArrayNewInitializer(CXXNewExpr::InitializationStyle Style, |
| 1018 | Expr *Init) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1019 | if (!Init) |
| 1020 | return true; |
Sebastian Redl | eb54f08 | 2012-02-17 08:42:32 +0000 | [diff] [blame] | 1021 | if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) |
| 1022 | return PLE->getNumExprs() == 0; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1023 | if (isa<ImplicitValueInitExpr>(Init)) |
| 1024 | return true; |
| 1025 | else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) |
| 1026 | return !CCE->isListInitialization() && |
| 1027 | CCE->getConstructor()->isDefaultConstructor(); |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1028 | else if (Style == CXXNewExpr::ListInit) { |
| 1029 | assert(isa<InitListExpr>(Init) && |
| 1030 | "Shouldn't create list CXXConstructExprs for arrays."); |
| 1031 | return true; |
| 1032 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1033 | return false; |
| 1034 | } |
| 1035 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1036 | ExprResult |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1037 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 1038 | SourceLocation PlacementLParen, |
| 1039 | MultiExprArg PlacementArgs, |
| 1040 | SourceLocation PlacementRParen, |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1041 | SourceRange TypeIdParens, |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1042 | QualType AllocType, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1043 | TypeSourceInfo *AllocTypeInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1044 | Expr *ArraySize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1045 | SourceRange DirectInitRange, |
| 1046 | Expr *Initializer, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1047 | bool TypeMayContainAuto) { |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1048 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1049 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1050 | CXXNewExpr::InitializationStyle initStyle; |
| 1051 | if (DirectInitRange.isValid()) { |
| 1052 | assert(Initializer && "Have parens but no initializer."); |
| 1053 | initStyle = CXXNewExpr::CallInit; |
| 1054 | } else if (Initializer && isa<InitListExpr>(Initializer)) |
| 1055 | initStyle = CXXNewExpr::ListInit; |
| 1056 | else { |
Sebastian Redl | e7c31a9 | 2012-02-22 09:07:21 +0000 | [diff] [blame] | 1057 | // In template instantiation, the initializer could be a CXXDefaultArgExpr |
| 1058 | // unwrapped from a CXXConstructExpr that was implicitly built. There is no |
| 1059 | // particularly sane way we can handle this (especially since it can even |
| 1060 | // occur for array new), so we throw the initializer away and have it be |
| 1061 | // rebuilt. |
| 1062 | if (Initializer && isa<CXXDefaultArgExpr>(Initializer)) |
| 1063 | Initializer = 0; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1064 | assert((!Initializer || isa<ImplicitValueInitExpr>(Initializer) || |
| 1065 | isa<CXXConstructExpr>(Initializer)) && |
| 1066 | "Initializer expression that cannot have been implicitly created."); |
| 1067 | initStyle = CXXNewExpr::NoInit; |
| 1068 | } |
| 1069 | |
| 1070 | Expr **Inits = &Initializer; |
| 1071 | unsigned NumInits = Initializer ? 1 : 0; |
| 1072 | if (initStyle == CXXNewExpr::CallInit) { |
| 1073 | if (ParenListExpr *List = dyn_cast<ParenListExpr>(Initializer)) { |
| 1074 | Inits = List->getExprs(); |
| 1075 | NumInits = List->getNumExprs(); |
| 1076 | } else if (CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Initializer)){ |
| 1077 | if (!isa<CXXTemporaryObjectExpr>(CCE)) { |
| 1078 | // Can happen in template instantiation. Since this is just an implicit |
| 1079 | // construction, we just take it apart and rebuild it. |
| 1080 | Inits = CCE->getArgs(); |
| 1081 | NumInits = CCE->getNumArgs(); |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1086 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 1087 | if (TypeMayContainAuto && AllocType->getContainedAutoType()) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1088 | if (initStyle == CXXNewExpr::NoInit || NumInits == 0) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1089 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
| 1090 | << AllocType << TypeRange); |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1091 | if (initStyle == CXXNewExpr::ListInit) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1092 | return ExprError(Diag(Inits[0]->getLocStart(), |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1093 | diag::err_auto_new_requires_parens) |
| 1094 | << AllocType << TypeRange); |
| 1095 | if (NumInits > 1) { |
| 1096 | Expr *FirstBad = Inits[1]; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1097 | return ExprError(Diag(FirstBad->getLocStart(), |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1098 | diag::err_auto_new_ctor_multiple_expressions) |
| 1099 | << AllocType << TypeRange); |
| 1100 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1101 | Expr *Deduce = Inits[0]; |
Richard Smith | 9647d3c | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 1102 | TypeSourceInfo *DeducedType = 0; |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1103 | if (DeduceAutoType(AllocTypeInfo, Deduce, DeducedType) == |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 1104 | DAR_Failed) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1105 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1106 | << AllocType << Deduce->getType() |
| 1107 | << TypeRange << Deduce->getSourceRange()); |
Richard Smith | 9647d3c | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 1108 | if (!DeducedType) |
| 1109 | return ExprError(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1110 | |
Richard Smith | 9647d3c | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 1111 | AllocTypeInfo = DeducedType; |
| 1112 | AllocType = AllocTypeInfo->getType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1113 | } |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1114 | |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1115 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 1116 | // typedef of an array type. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1117 | if (!ArraySize) { |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1118 | if (const ConstantArrayType *Array |
| 1119 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1120 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 1121 | Context.getSizeType(), |
| 1122 | TypeRange.getEnd()); |
Douglas Gregor | cda95f4 | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 1123 | AllocType = Array->getElementType(); |
| 1124 | } |
| 1125 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1126 | |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1127 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 1128 | return ExprError(); |
| 1129 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1130 | if (initStyle == CXXNewExpr::ListInit && isStdInitializerList(AllocType, 0)) { |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1131 | Diag(AllocTypeInfo->getTypeLoc().getBeginLoc(), |
| 1132 | diag::warn_dangling_std_initializer_list) |
Sebastian Redl | 73cfbeb | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1133 | << /*at end of FE*/0 << Inits[0]->getSourceRange(); |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1136 | // In ARC, infer 'retaining' for the allocated |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1137 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1138 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 1139 | AllocType->isObjCLifetimeType()) { |
| 1140 | AllocType = Context.getLifetimeQualifiedType(AllocType, |
| 1141 | AllocType->getObjCARCImplicitLifetime()); |
| 1142 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1143 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1144 | QualType ResultType = Context.getPointerType(AllocType); |
| 1145 | |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 1146 | // C++98 5.3.4p6: "The expression in a direct-new-declarator shall have |
| 1147 | // integral or enumeration type with a non-negative value." |
| 1148 | // C++11 [expr.new]p6: The expression [...] shall be of integral or unscoped |
| 1149 | // enumeration type, or a class type for which a single non-explicit |
| 1150 | // conversion function to integral or unscoped enumeration type exists. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1151 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 1152 | ExprResult ConvertedSize = ConvertToIntegralOrEnumerationType( |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1153 | StartLoc, ArraySize, |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1154 | PDiag(diag::err_array_size_not_integral) << getLangOpts().CPlusPlus0x, |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1155 | PDiag(diag::err_array_size_incomplete_type) |
| 1156 | << ArraySize->getSourceRange(), |
| 1157 | PDiag(diag::err_array_size_explicit_conversion), |
| 1158 | PDiag(diag::note_array_size_conversion), |
| 1159 | PDiag(diag::err_array_size_ambiguous_conversion), |
| 1160 | PDiag(diag::note_array_size_conversion), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1161 | PDiag(getLangOpts().CPlusPlus0x ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1162 | diag::warn_cxx98_compat_array_size_conversion : |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 1163 | diag::ext_array_size_conversion), |
| 1164 | /*AllowScopedEnumerations*/ false); |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1165 | if (ConvertedSize.isInvalid()) |
| 1166 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1167 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1168 | ArraySize = ConvertedSize.take(); |
John McCall | 9b80c21 | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 1169 | QualType SizeType = ArraySize->getType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1170 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1171 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1172 | |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1173 | // C++98 [expr.new]p7: |
| 1174 | // The expression in a direct-new-declarator shall have integral type |
| 1175 | // with a non-negative value. |
| 1176 | // |
| 1177 | // Let's see if this is a constant < 0. If so, we reject it out of |
| 1178 | // hand. Otherwise, if it's not a constant, we must have an unparenthesized |
| 1179 | // array type. |
| 1180 | // |
| 1181 | // Note: such a construct has well-defined semantics in C++11: it throws |
| 1182 | // std::bad_array_new_length. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1183 | if (!ArraySize->isValueDependent()) { |
| 1184 | llvm::APSInt Value; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1185 | // We've already performed any required implicit conversion to integer or |
| 1186 | // unscoped enumeration type. |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1187 | if (ArraySize->isIntegerConstantExpr(Value, Context)) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1188 | if (Value < llvm::APSInt( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1189 | llvm::APInt::getNullValue(Value.getBitWidth()), |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1190 | Value.isUnsigned())) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1191 | if (getLangOpts().CPlusPlus0x) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1192 | Diag(ArraySize->getLocStart(), |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1193 | diag::warn_typecheck_negative_array_new_size) |
Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1194 | << ArraySize->getSourceRange(); |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1195 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1196 | return ExprError(Diag(ArraySize->getLocStart(), |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1197 | diag::err_typecheck_negative_array_size) |
| 1198 | << ArraySize->getSourceRange()); |
| 1199 | } else if (!AllocType->isDependentType()) { |
| 1200 | unsigned ActiveSizeBits = |
| 1201 | ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
| 1202 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1203 | if (getLangOpts().CPlusPlus0x) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1204 | Diag(ArraySize->getLocStart(), |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1205 | diag::warn_array_new_too_large) |
| 1206 | << Value.toString(10) |
| 1207 | << ArraySize->getSourceRange(); |
| 1208 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1209 | return ExprError(Diag(ArraySize->getLocStart(), |
Richard Smith | bcc9bcb | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1210 | diag::err_array_too_large) |
| 1211 | << Value.toString(10) |
| 1212 | << ArraySize->getSourceRange()); |
Douglas Gregor | caa1bf4 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1213 | } |
| 1214 | } |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1215 | } else if (TypeIdParens.isValid()) { |
| 1216 | // Can't have dynamic array size when the type-id is in parentheses. |
| 1217 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 1218 | << ArraySize->getSourceRange() |
| 1219 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 1220 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1221 | |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1222 | TypeIdParens = SourceRange(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1223 | } |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1224 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1225 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1226 | // ARC: warn about ABI issues. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1227 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1228 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
| 1229 | if (BaseAllocType.hasStrongOrWeakObjCLifetime()) |
| 1230 | Diag(StartLoc, diag::warn_err_new_delete_object_array) |
| 1231 | << 0 << BaseAllocType; |
| 1232 | } |
| 1233 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 1234 | // Note that we do *not* convert the argument in any way. It can |
| 1235 | // be signed, larger than size_t, whatever. |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1236 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1237 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1238 | FunctionDecl *OperatorNew = 0; |
| 1239 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1240 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 1241 | unsigned NumPlaceArgs = PlacementArgs.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1242 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1243 | if (!AllocType->isDependentType() && |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1244 | !Expr::hasAnyTypeDependentArguments( |
| 1245 | llvm::makeArrayRef(PlaceArgs, NumPlaceArgs)) && |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1246 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1247 | SourceRange(PlacementLParen, PlacementRParen), |
| 1248 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 1249 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1250 | return ExprError(); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1251 | |
| 1252 | // If this is an array allocation, compute whether the usual array |
| 1253 | // deallocation function for the type has a size_t parameter. |
| 1254 | bool UsualArrayDeleteWantsSize = false; |
| 1255 | if (ArraySize && !AllocType->isDependentType()) |
| 1256 | UsualArrayDeleteWantsSize |
| 1257 | = doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); |
| 1258 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1259 | SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 1eab66c | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1260 | if (OperatorNew) { |
| 1261 | // Add default arguments, if any. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1262 | const FunctionProtoType *Proto = |
Fariborz Jahanian | 1eab66c | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1263 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1264 | VariadicCallType CallType = |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 1265 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1266 | |
Anders Carlsson | c144bc2 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1267 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1268 | Proto, 1, PlaceArgs, NumPlaceArgs, |
Anders Carlsson | c144bc2 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1269 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1270 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1271 | |
Fariborz Jahanian | 1eab66c | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1272 | NumPlaceArgs = AllPlaceArgs.size(); |
| 1273 | if (NumPlaceArgs > 0) |
| 1274 | PlaceArgs = &AllPlaceArgs[0]; |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 1275 | |
| 1276 | DiagnoseSentinelCalls(OperatorNew, PlacementLParen, |
| 1277 | PlaceArgs, NumPlaceArgs); |
| 1278 | |
| 1279 | // FIXME: Missing call to CheckFunctionCall or equivalent |
Fariborz Jahanian | 1eab66c | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1280 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1281 | |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 1282 | // Warn if the type is over-aligned and is being allocated by global operator |
| 1283 | // new. |
Nick Lewycky | 96ed7b6 | 2012-02-04 03:30:14 +0000 | [diff] [blame] | 1284 | if (NumPlaceArgs == 0 && OperatorNew && |
Nick Lewycky | 411fc65 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 1285 | (OperatorNew->isImplicit() || |
| 1286 | getSourceManager().isInSystemHeader(OperatorNew->getLocStart()))) { |
| 1287 | if (unsigned Align = Context.getPreferredTypeAlign(AllocType.getTypePtr())){ |
| 1288 | unsigned SuitableAlign = Context.getTargetInfo().getSuitableAlign(); |
| 1289 | if (Align > SuitableAlign) |
| 1290 | Diag(StartLoc, diag::warn_overaligned_type) |
| 1291 | << AllocType |
| 1292 | << unsigned(Align / Context.getCharWidth()) |
| 1293 | << unsigned(SuitableAlign / Context.getCharWidth()); |
| 1294 | } |
| 1295 | } |
| 1296 | |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1297 | QualType InitType = AllocType; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1298 | // Array 'new' can't have any initializers except empty parentheses. |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1299 | // Initializer lists are also allowed, in C++11. Rely on the parser for the |
| 1300 | // dialect distinction. |
| 1301 | if (ResultType->isArrayType() || ArraySize) { |
| 1302 | if (!isLegalArrayNewInitializer(initStyle, Initializer)) { |
| 1303 | SourceRange InitRange(Inits[0]->getLocStart(), |
| 1304 | Inits[NumInits - 1]->getLocEnd()); |
| 1305 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 1306 | return ExprError(); |
| 1307 | } |
| 1308 | if (InitListExpr *ILE = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 1309 | // We do the initialization typechecking against the array type |
| 1310 | // corresponding to the number of initializers + 1 (to also check |
| 1311 | // default-initialization). |
| 1312 | unsigned NumElements = ILE->getNumInits() + 1; |
| 1313 | InitType = Context.getConstantArrayType(AllocType, |
| 1314 | llvm::APInt(Context.getTypeSize(Context.getSizeType()), NumElements), |
| 1315 | ArrayType::Normal, 0); |
| 1316 | } |
Anders Carlsson | c6bb0e1 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1319 | if (!AllocType->isDependentType() && |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1320 | !Expr::hasAnyTypeDependentArguments( |
| 1321 | llvm::makeArrayRef(Inits, NumInits))) { |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1322 | // C++11 [expr.new]p15: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1323 | // A new-expression that creates an object of type T initializes that |
| 1324 | // object as follows: |
| 1325 | InitializationKind Kind |
| 1326 | // - If the new-initializer is omitted, the object is default- |
| 1327 | // initialized (8.5); if no initialization is performed, |
| 1328 | // the object has indeterminate value |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1329 | = initStyle == CXXNewExpr::NoInit |
| 1330 | ? InitializationKind::CreateDefault(TypeRange.getBegin()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1331 | // - Otherwise, the new-initializer is interpreted according to the |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1332 | // initialization rules of 8.5 for direct-initialization. |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1333 | : initStyle == CXXNewExpr::ListInit |
| 1334 | ? InitializationKind::CreateDirectList(TypeRange.getBegin()) |
| 1335 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
| 1336 | DirectInitRange.getBegin(), |
| 1337 | DirectInitRange.getEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1338 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1339 | InitializedEntity Entity |
Sebastian Redl | b8fc477 | 2012-02-16 12:59:47 +0000 | [diff] [blame] | 1340 | = InitializedEntity::InitializeNew(StartLoc, InitType); |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1341 | InitializationSequence InitSeq(*this, Entity, Kind, Inits, NumInits); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1342 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1343 | MultiExprArg(Inits, NumInits)); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1344 | if (FullInit.isInvalid()) |
| 1345 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1346 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1347 | // FullInit is our initializer; strip off CXXBindTemporaryExprs, because |
| 1348 | // we don't want the initialized object to be destructed. |
| 1349 | if (CXXBindTemporaryExpr *Binder = |
| 1350 | dyn_cast_or_null<CXXBindTemporaryExpr>(FullInit.get())) |
| 1351 | FullInit = Owned(Binder->getSubExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1352 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1353 | Initializer = FullInit.take(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1354 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1355 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1356 | // Mark the new and delete operators as referenced. |
| 1357 | if (OperatorNew) |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1358 | MarkFunctionReferenced(StartLoc, OperatorNew); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1359 | if (OperatorDelete) |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1360 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1361 | |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 1362 | // C++0x [expr.new]p17: |
| 1363 | // If the new expression creates an array of objects of class type, |
| 1364 | // access and ambiguity control are done for the destructor. |
David Blaikie | 631a486 | 2012-03-10 23:40:02 +0000 | [diff] [blame] | 1365 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
| 1366 | if (ArraySize && !BaseAllocType->isDependentType()) { |
| 1367 | if (const RecordType *BaseRecordType = BaseAllocType->getAs<RecordType>()) { |
| 1368 | if (CXXDestructorDecl *dtor = LookupDestructor( |
| 1369 | cast<CXXRecordDecl>(BaseRecordType->getDecl()))) { |
| 1370 | MarkFunctionReferenced(StartLoc, dtor); |
| 1371 | CheckDestructorAccess(StartLoc, dtor, |
| 1372 | PDiag(diag::err_access_dtor) |
| 1373 | << BaseAllocType); |
| 1374 | DiagnoseUseOfDecl(dtor, StartLoc); |
| 1375 | } |
John McCall | 928a257 | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1378 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1379 | PlacementArgs.release(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1380 | |
Ted Kremenek | 9d6eb40 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1381 | return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1382 | OperatorDelete, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1383 | UsualArrayDeleteWantsSize, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1384 | PlaceArgs, NumPlaceArgs, TypeIdParens, |
| 1385 | ArraySize, initStyle, Initializer, |
Douglas Gregor | 0744ef6 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1386 | ResultType, AllocTypeInfo, |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1387 | StartLoc, DirectInitRange)); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1390 | /// \brief Checks that a type is suitable as the allocated type |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1391 | /// in a new-expression. |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1392 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | SourceRange R) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1394 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 1395 | // abstract class type or array thereof. |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1396 | if (AllocType->isFunctionType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1397 | return Diag(Loc, diag::err_bad_new_type) |
| 1398 | << AllocType << 0 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1399 | else if (AllocType->isReferenceType()) |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1400 | return Diag(Loc, diag::err_bad_new_type) |
| 1401 | << AllocType << 1 << R; |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1402 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 1403 | RequireCompleteType(Loc, AllocType, diag::err_new_incomplete_type,R)) |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1404 | return true; |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1405 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1406 | diag::err_allocation_of_abstract_type)) |
| 1407 | return true; |
Douglas Gregor | 3999e15 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1408 | else if (AllocType->isVariablyModifiedType()) |
| 1409 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 1410 | << AllocType; |
Douglas Gregor | 39d1a09 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1411 | else if (unsigned AddressSpace = AllocType.getAddressSpace()) |
| 1412 | return Diag(Loc, diag::err_address_space_qualified_new) |
| 1413 | << AllocType.getUnqualifiedType() << AddressSpace; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1414 | else if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1415 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { |
| 1416 | QualType BaseAllocType = Context.getBaseElementType(AT); |
| 1417 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 1418 | BaseAllocType->isObjCLifetimeType()) |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 1419 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1420 | << BaseAllocType; |
| 1421 | } |
| 1422 | } |
Douglas Gregor | 39d1a09 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1423 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1424 | return false; |
| 1425 | } |
| 1426 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1427 | /// \brief Determine whether the given function is a non-placement |
| 1428 | /// deallocation function. |
| 1429 | static bool isNonPlacementDeallocationFunction(FunctionDecl *FD) { |
| 1430 | if (FD->isInvalidDecl()) |
| 1431 | return false; |
| 1432 | |
| 1433 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 1434 | return Method->isUsualDeallocationFunction(); |
| 1435 | |
| 1436 | return ((FD->getOverloadedOperator() == OO_Delete || |
| 1437 | FD->getOverloadedOperator() == OO_Array_Delete) && |
| 1438 | FD->getNumParams() == 1); |
| 1439 | } |
| 1440 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1441 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 1442 | /// that are appropriate for the allocation. |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1443 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 1444 | bool UseGlobal, QualType AllocType, |
| 1445 | bool IsArray, Expr **PlaceArgs, |
| 1446 | unsigned NumPlaceArgs, |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1447 | FunctionDecl *&OperatorNew, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1449 | // --- Choosing an allocation function --- |
| 1450 | // C++ 5.3.4p8 - 14 & 18 |
| 1451 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 1452 | // in the scope of the allocated class. |
| 1453 | // 2) If an array size is given, look for operator new[], else look for |
| 1454 | // operator new. |
| 1455 | // 3) The first argument is always size_t. Append the arguments from the |
| 1456 | // placement form. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1457 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1458 | SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1459 | // We don't care about the actual value of this argument. |
| 1460 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 1461 | // tree? Or should the consumer just recalculate the value? |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1462 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1463 | Context.getTargetInfo().getPointerWidth(0)), |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1464 | Context.getSizeType(), |
| 1465 | SourceLocation()); |
| 1466 | AllocArgs[0] = &Size; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1467 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 1468 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1469 | // C++ [expr.new]p8: |
| 1470 | // If the allocated type is a non-array type, the allocation |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1471 | // function's name is operator new and the deallocation function's |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1472 | // name is operator delete. If the allocated type is an array |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1473 | // type, the allocation function's name is operator new[] and the |
| 1474 | // deallocation function's name is operator delete[]. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1475 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 1476 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1477 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1478 | IsArray ? OO_Array_Delete : OO_Delete); |
| 1479 | |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1480 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 1481 | |
| 1482 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | CXXRecordDecl *Record |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1484 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1485 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1486 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 1487 | OperatorNew)) |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1488 | return true; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1489 | } |
| 1490 | if (!OperatorNew) { |
| 1491 | // Didn't find a member overload. Look for a global one. |
| 1492 | DeclareGlobalNewDelete(); |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1493 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1494 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1495 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 1496 | OperatorNew)) |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1497 | return true; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
John McCall | 0f55a03 | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1500 | // We don't need an operator delete if we're running under |
| 1501 | // -fno-exceptions. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1502 | if (!getLangOpts().Exceptions) { |
John McCall | 0f55a03 | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1503 | OperatorDelete = 0; |
| 1504 | return false; |
| 1505 | } |
| 1506 | |
Anders Carlsson | 6f9dabf | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 1507 | // FindAllocationOverload can change the passed in arguments, so we need to |
| 1508 | // copy them back. |
| 1509 | if (NumPlaceArgs > 0) |
| 1510 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1511 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1512 | // C++ [expr.new]p19: |
| 1513 | // |
| 1514 | // If the new-expression begins with a unary :: operator, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1515 | // deallocation function's name is looked up in the global |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1516 | // 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] | 1517 | // array thereof, the deallocation function's name is looked up in |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1518 | // the scope of T. If this lookup fails to find the name, or if |
| 1519 | // the allocated type is not a class type or array thereof, the |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1520 | // deallocation function's name is looked up in the global scope. |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1521 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1522 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1523 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | 1194d5e | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1524 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1525 | LookupQualifiedName(FoundDelete, RD); |
| 1526 | } |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1527 | if (FoundDelete.isAmbiguous()) |
| 1528 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1529 | |
| 1530 | if (FoundDelete.empty()) { |
| 1531 | DeclareGlobalNewDelete(); |
| 1532 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 1533 | } |
| 1534 | |
| 1535 | FoundDelete.suppressDiagnostics(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1536 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1537 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1538 | |
John McCall | d3be2c8 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 1539 | // Whether we're looking for a placement operator delete is dictated |
| 1540 | // by whether we selected a placement operator new, not by whether |
| 1541 | // we had explicit placement arguments. This matters for things like |
| 1542 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 1543 | // A *a = new A() |
| 1544 | bool isPlacementNew = (NumPlaceArgs > 0 || OperatorNew->param_size() != 1); |
| 1545 | |
| 1546 | if (isPlacementNew) { |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1547 | // C++ [expr.new]p20: |
| 1548 | // A declaration of a placement deallocation function matches the |
| 1549 | // declaration of a placement allocation function if it has the |
| 1550 | // same number of parameters and, after parameter transformations |
| 1551 | // (8.3.5), all parameter types except the first are |
| 1552 | // identical. [...] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1553 | // |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1554 | // To perform this comparison, we compute the function type that |
| 1555 | // the deallocation function should have, and use that type both |
| 1556 | // for template argument deduction and for comparison purposes. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1557 | // |
| 1558 | // FIXME: this comparison should ignore CC and the like. |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1559 | QualType ExpectedFunctionType; |
| 1560 | { |
| 1561 | const FunctionProtoType *Proto |
| 1562 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1564 | SmallVector<QualType, 4> ArgTypes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1565 | ArgTypes.push_back(Context.VoidPtrTy); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1566 | for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I) |
| 1567 | ArgTypes.push_back(Proto->getArgType(I)); |
| 1568 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1569 | FunctionProtoType::ExtProtoInfo EPI; |
| 1570 | EPI.Variadic = Proto->isVariadic(); |
| 1571 | |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1572 | ExpectedFunctionType |
| 1573 | = Context.getFunctionType(Context.VoidTy, ArgTypes.data(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1574 | ArgTypes.size(), EPI); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1577 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1578 | DEnd = FoundDelete.end(); |
| 1579 | D != DEnd; ++D) { |
| 1580 | FunctionDecl *Fn = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1581 | if (FunctionTemplateDecl *FnTmpl |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1582 | = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
| 1583 | // Perform template argument deduction to try to match the |
| 1584 | // expected function type. |
| 1585 | TemplateDeductionInfo Info(Context, StartLoc); |
| 1586 | if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info)) |
| 1587 | continue; |
| 1588 | } else |
| 1589 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 1590 | |
| 1591 | if (Context.hasSameType(Fn->getType(), ExpectedFunctionType)) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1592 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1593 | } |
| 1594 | } else { |
| 1595 | // C++ [expr.new]p20: |
| 1596 | // [...] Any non-placement deallocation function matches a |
| 1597 | // non-placement allocation function. [...] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1598 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1599 | DEnd = FoundDelete.end(); |
| 1600 | D != DEnd; ++D) { |
| 1601 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl())) |
| 1602 | if (isNonPlacementDeallocationFunction(Fn)) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1603 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | // C++ [expr.new]p20: |
| 1608 | // [...] If the lookup finds a single matching deallocation |
| 1609 | // function, that function will be called; otherwise, no |
| 1610 | // deallocation function will be called. |
| 1611 | if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1612 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1613 | |
| 1614 | // C++0x [expr.new]p20: |
| 1615 | // If the lookup finds the two-parameter form of a usual |
| 1616 | // deallocation function (3.7.4.2) and that function, considered |
| 1617 | // as a placement deallocation function, would have been |
| 1618 | // selected as a match for the allocation function, the program |
| 1619 | // is ill-formed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1620 | if (NumPlaceArgs && getLangOpts().CPlusPlus0x && |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1621 | isNonPlacementDeallocationFunction(OperatorDelete)) { |
| 1622 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1623 | << SourceRange(PlaceArgs[0]->getLocStart(), |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1624 | PlaceArgs[NumPlaceArgs - 1]->getLocEnd()); |
| 1625 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 1626 | << DeleteName; |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1627 | } else { |
| 1628 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1629 | Matches[0].first); |
Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1633 | return false; |
| 1634 | } |
| 1635 | |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1636 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 1637 | /// function in the specified scope. |
Sebastian Redl | 1df2bbe | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1638 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 1639 | DeclarationName Name, Expr** Args, |
| 1640 | unsigned NumArgs, DeclContext *Ctx, |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1641 | bool AllowMissing, FunctionDecl *&Operator, |
| 1642 | bool Diagnose) { |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1643 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); |
| 1644 | LookupQualifiedName(R, Ctx); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1645 | if (R.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1646 | if (AllowMissing || !Diagnose) |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1647 | return false; |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1648 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1649 | << Name << Range; |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1652 | if (R.isAmbiguous()) |
| 1653 | return true; |
| 1654 | |
| 1655 | R.suppressDiagnostics(); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1656 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1657 | OverloadCandidateSet Candidates(StartLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1658 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
Douglas Gregor | 80a6cc5 | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 1659 | Alloc != AllocEnd; ++Alloc) { |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1660 | // Even member operator new/delete are implicitly treated as |
| 1661 | // static, so don't use AddMemberCandidate. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1662 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1663 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1664 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 1665 | AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1666 | /*ExplicitTemplateArgs=*/0, |
| 1667 | llvm::makeArrayRef(Args, NumArgs), |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1668 | Candidates, |
| 1669 | /*SuppressUserConversions=*/false); |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1670 | continue; |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1673 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1674 | AddOverloadCandidate(Fn, Alloc.getPair(), |
| 1675 | llvm::makeArrayRef(Args, NumArgs), Candidates, |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1676 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | // Do the resolution. |
| 1680 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1681 | switch (Candidates.BestViableFunction(*this, StartLoc, Best)) { |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1682 | case OR_Success: { |
| 1683 | // Got one! |
| 1684 | FunctionDecl *FnDecl = Best->Function; |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1685 | MarkFunctionReferenced(StartLoc, FnDecl); |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1686 | // The first argument is size_t, and the first parameter must be size_t, |
| 1687 | // too. This is checked on declaration and can be assumed. (It can't be |
| 1688 | // asserted on, though, since invalid decls are left in there.) |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1689 | // Watch out for variadic allocator function. |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1690 | unsigned NumArgsInFnDecl = FnDecl->getNumParams(); |
| 1691 | for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1692 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 1693 | FnDecl->getParamDecl(i)); |
| 1694 | |
| 1695 | if (!Diagnose && !CanPerformCopyInitialization(Entity, Owned(Args[i]))) |
| 1696 | return true; |
| 1697 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1698 | ExprResult Result |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1699 | = PerformCopyInitialization(Entity, SourceLocation(), Owned(Args[i])); |
Douglas Gregor | 3414727 | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1700 | if (Result.isInvalid()) |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1701 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1702 | |
Douglas Gregor | 3414727 | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1703 | Args[i] = Result.takeAs<Expr>(); |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1704 | } |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 1705 | |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1706 | Operator = FnDecl; |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 1707 | |
| 1708 | if (CheckAllocationAccess(StartLoc, Range, R.getNamingClass(), |
| 1709 | Best->FoundDecl, Diagnose) == AR_inaccessible) |
| 1710 | return true; |
| 1711 | |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1712 | return false; |
| 1713 | } |
| 1714 | |
| 1715 | case OR_No_Viable_Function: |
Chandler Carruth | e6c8818 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1716 | if (Diagnose) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1717 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
| 1718 | << Name << Range; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1719 | Candidates.NoteCandidates(*this, OCD_AllCandidates, |
| 1720 | llvm::makeArrayRef(Args, NumArgs)); |
Chandler Carruth | e6c8818 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1721 | } |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1722 | return true; |
| 1723 | |
| 1724 | case OR_Ambiguous: |
Chandler Carruth | e6c8818 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1725 | if (Diagnose) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1726 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
| 1727 | << Name << Range; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1728 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, |
| 1729 | llvm::makeArrayRef(Args, NumArgs)); |
Chandler Carruth | e6c8818 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1730 | } |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1731 | return true; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1732 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1733 | case OR_Deleted: { |
Chandler Carruth | e6c8818 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1734 | if (Diagnose) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1735 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 1736 | << Best->Function->isDeleted() |
| 1737 | << Name |
| 1738 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 1739 | << Range; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1740 | Candidates.NoteCandidates(*this, OCD_AllCandidates, |
| 1741 | llvm::makeArrayRef(Args, NumArgs)); |
Chandler Carruth | e6c8818 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1742 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1743 | return true; |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1744 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1745 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1746 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
Sebastian Redl | 33a3101 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1750 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 1751 | /// delete. These are: |
| 1752 | /// @code |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1753 | /// // C++03: |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1754 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 1755 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1756 | /// void operator delete(void *) throw(); |
| 1757 | /// void operator delete[](void *) throw(); |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1758 | /// // C++0x: |
| 1759 | /// void* operator new(std::size_t); |
| 1760 | /// void* operator new[](std::size_t); |
| 1761 | /// void operator delete(void *); |
| 1762 | /// void operator delete[](void *); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1763 | /// @endcode |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1764 | /// C++0x operator delete is implicitly noexcept. |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1765 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 1766 | /// declared. Their use requires including \<new\>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1768 | if (GlobalNewDeleteDeclared) |
| 1769 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1770 | |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1771 | // C++ [basic.std.dynamic]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1772 | // [...] The following allocation and deallocation functions (18.4) are |
| 1773 | // implicitly declared in global scope in each translation unit of a |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1774 | // program |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1775 | // |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1776 | // C++03: |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1777 | // void* operator new(std::size_t) throw(std::bad_alloc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1778 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1779 | // void operator delete(void*) throw(); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1780 | // void operator delete[](void*) throw(); |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1781 | // C++0x: |
| 1782 | // void* operator new(std::size_t); |
| 1783 | // void* operator new[](std::size_t); |
| 1784 | // void operator delete(void*); |
| 1785 | // void operator delete[](void*); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1786 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1787 | // These implicit declarations introduce only the function names operator |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1788 | // new, operator new[], operator delete, operator delete[]. |
| 1789 | // |
| 1790 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 1791 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 1792 | // However, we do not make these implicit declarations visible to name |
| 1793 | // lookup. |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1794 | // Note that the C++0x versions of operator delete are deallocation functions, |
| 1795 | // and thus are implicitly noexcept. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1796 | if (!StdBadAlloc && !getLangOpts().CPlusPlus0x) { |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1797 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 1798 | // implicitly. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1799 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
| 1800 | getOrCreateStdNamespace(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1801 | SourceLocation(), SourceLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1802 | &PP.getIdentifierTable().get("bad_alloc"), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1803 | 0); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1804 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1805 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1806 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1807 | GlobalNewDeleteDeclared = true; |
| 1808 | |
| 1809 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 1810 | QualType SizeT = Context.getSizeType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1811 | bool AssumeSaneOperatorNew = getLangOpts().AssumeSaneOperatorNew; |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1812 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1813 | DeclareGlobalAllocationFunction( |
| 1814 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
Nuno Lopes | 13c88c7 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1815 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1816 | DeclareGlobalAllocationFunction( |
| 1817 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
Nuno Lopes | 13c88c7 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1818 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1819 | DeclareGlobalAllocationFunction( |
| 1820 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 1821 | Context.VoidTy, VoidPtr); |
| 1822 | DeclareGlobalAllocationFunction( |
| 1823 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 1824 | Context.VoidTy, VoidPtr); |
| 1825 | } |
| 1826 | |
| 1827 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 1828 | /// allocation function if it doesn't already exist. |
| 1829 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Nuno Lopes | 13c88c7 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1830 | QualType Return, QualType Argument, |
| 1831 | bool AddMallocAttr) { |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1832 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 1833 | |
| 1834 | // Check if this function is already declared. |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1835 | { |
Douglas Gregor | 17eb26b | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 1836 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1837 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1838 | Alloc != AllocEnd; ++Alloc) { |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1839 | // Only look at non-template functions, as it is the predefined, |
| 1840 | // non-templated allocation function we are trying to declare here. |
| 1841 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
| 1842 | QualType InitialParamType = |
Douglas Gregor | 684d7bd | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 1843 | Context.getCanonicalType( |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1844 | Func->getParamDecl(0)->getType().getUnqualifiedType()); |
| 1845 | // FIXME: Do we need to check for default arguments here? |
Douglas Gregor | c1a42fd | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1846 | if (Func->getNumParams() == 1 && InitialParamType == Argument) { |
| 1847 | if(AddMallocAttr && !Func->hasAttr<MallocAttr>()) |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1848 | Func->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1849 | return; |
Douglas Gregor | c1a42fd | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1850 | } |
Chandler Carruth | 9353842 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1851 | } |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1852 | } |
| 1853 | } |
| 1854 | |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1855 | QualType BadAllocType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1856 | bool HasBadAllocExceptionSpec |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1857 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 1858 | Name.getCXXOverloadedOperator() == OO_Array_New); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1859 | if (HasBadAllocExceptionSpec && !getLangOpts().CPlusPlus0x) { |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1860 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1861 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1862 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1863 | |
| 1864 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1865 | if (HasBadAllocExceptionSpec) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1866 | if (!getLangOpts().CPlusPlus0x) { |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1867 | EPI.ExceptionSpecType = EST_Dynamic; |
| 1868 | EPI.NumExceptions = 1; |
| 1869 | EPI.Exceptions = &BadAllocType; |
| 1870 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1871 | } else { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1872 | EPI.ExceptionSpecType = getLangOpts().CPlusPlus0x ? |
Sebastian Redl | 3758809 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1873 | EST_BasicNoexcept : EST_DynamicNone; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1874 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1875 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1876 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, EPI); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1877 | FunctionDecl *Alloc = |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1878 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), |
| 1879 | SourceLocation(), Name, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1880 | FnType, /*TInfo=*/0, SC_None, |
| 1881 | SC_None, false, true); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1882 | Alloc->setImplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1883 | |
Nuno Lopes | 13c88c7 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1884 | if (AddMallocAttr) |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1885 | Alloc->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1886 | |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1887 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1888 | SourceLocation(), 0, |
| 1889 | Argument, /*TInfo=*/0, |
| 1890 | SC_None, SC_None, 0); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1891 | Alloc->setParams(Param); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1892 | |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1893 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 1894 | // make sure it is at the end of the chain to coincide with the |
| 1895 | // global scope. |
John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1896 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
Sebastian Redl | faf6808 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1899 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 1900 | DeclarationName Name, |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1901 | FunctionDecl* &Operator, bool Diagnose) { |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1902 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1903 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1904 | LookupQualifiedName(Found, RD); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1905 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1906 | if (Found.isAmbiguous()) |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1907 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1908 | |
Chandler Carruth | b6f9917 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 1909 | Found.suppressDiagnostics(); |
| 1910 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1911 | SmallVector<DeclAccessPair,4> Matches; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1912 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1913 | F != FEnd; ++F) { |
Chandler Carruth | 9b41823 | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 1914 | NamedDecl *ND = (*F)->getUnderlyingDecl(); |
| 1915 | |
| 1916 | // Ignore template operator delete members from the check for a usual |
| 1917 | // deallocation function. |
| 1918 | if (isa<FunctionTemplateDecl>(ND)) |
| 1919 | continue; |
| 1920 | |
| 1921 | if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction()) |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1922 | Matches.push_back(F.getPair()); |
| 1923 | } |
| 1924 | |
| 1925 | // There's exactly one suitable operator; pick it. |
| 1926 | if (Matches.size() == 1) { |
| 1927 | Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl()); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1928 | |
| 1929 | if (Operator->isDeleted()) { |
| 1930 | if (Diagnose) { |
| 1931 | Diag(StartLoc, diag::err_deleted_function_use); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 1932 | NoteDeletedFunction(Operator); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1933 | } |
| 1934 | return true; |
| 1935 | } |
| 1936 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 1937 | if (CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
| 1938 | Matches[0], Diagnose) == AR_inaccessible) |
| 1939 | return true; |
| 1940 | |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1941 | return false; |
| 1942 | |
| 1943 | // We found multiple suitable operators; complain about the ambiguity. |
| 1944 | } else if (!Matches.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1945 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1946 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 1947 | << Name << RD; |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1948 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1949 | for (SmallVectorImpl<DeclAccessPair>::iterator |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1950 | F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F) |
| 1951 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1952 | diag::note_member_declared_here) << Name; |
| 1953 | } |
John McCall | 66a8759 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1954 | return true; |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | // We did find operator delete/operator delete[] declarations, but |
| 1958 | // none of them were suitable. |
| 1959 | if (!Found.empty()) { |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1960 | if (Diagnose) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1961 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 1962 | << Name << RD; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1963 | |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1964 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1965 | F != FEnd; ++F) |
| 1966 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1967 | diag::note_member_declared_here) << Name; |
| 1968 | } |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1969 | return true; |
| 1970 | } |
| 1971 | |
| 1972 | // Look for a global declaration. |
| 1973 | DeclareGlobalNewDelete(); |
| 1974 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1975 | |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1976 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); |
| 1977 | Expr* DeallocArgs[1]; |
| 1978 | DeallocArgs[0] = &Null; |
| 1979 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1980 | DeallocArgs, 1, TUDecl, !Diagnose, |
| 1981 | Operator, Diagnose)) |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1982 | return true; |
| 1983 | |
| 1984 | assert(Operator && "Did not find a deallocation function!"); |
| 1985 | return false; |
| 1986 | } |
| 1987 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1988 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 1989 | /// @code ::delete ptr; @endcode |
| 1990 | /// or |
| 1991 | /// @code delete [] ptr; @endcode |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1992 | ExprResult |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1993 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1994 | bool ArrayForm, Expr *ExE) { |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1995 | // C++ [expr.delete]p1: |
| 1996 | // The operand shall have a pointer type, or a class type having a single |
| 1997 | // conversion function to a pointer type. The result has type void. |
| 1998 | // |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1999 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 2000 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2001 | ExprResult Ex = Owned(ExE); |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2002 | FunctionDecl *OperatorDelete = 0; |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 2003 | bool ArrayFormAsWritten = ArrayForm; |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 2004 | bool UsualArrayDeleteWantsSize = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2006 | if (!Ex.get()->isTypeDependent()) { |
John McCall | ef42902 | 2012-03-09 04:08:29 +0000 | [diff] [blame] | 2007 | // Perform lvalue-to-rvalue cast, if needed. |
| 2008 | Ex = DefaultLvalueConversion(Ex.take()); |
| 2009 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2010 | QualType Type = Ex.get()->getType(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2011 | |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 2012 | if (const RecordType *Record = Type->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2013 | if (RequireCompleteType(StartLoc, Type, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 2014 | diag::err_delete_incomplete_class_type)) |
Douglas Gregor | f65f490 | 2010-07-29 14:44:35 +0000 | [diff] [blame] | 2015 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2016 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2017 | SmallVector<CXXConversionDecl*, 4> ObjectPtrConversions; |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2018 | |
Fariborz Jahanian | b54ccb2 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 2019 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2020 | const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2021 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2022 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2023 | NamedDecl *D = I.getDecl(); |
| 2024 | if (isa<UsingShadowDecl>(D)) |
| 2025 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2026 | |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 2027 | // Skip over templated conversion functions; they aren't considered. |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2028 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 2029 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2030 | |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2031 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2032 | |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 2033 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 2034 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2035 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Fariborz Jahanian | adcea10 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 2036 | ObjectPtrConversions.push_back(Conv); |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 2037 | } |
Fariborz Jahanian | adcea10 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 2038 | if (ObjectPtrConversions.size() == 1) { |
| 2039 | // We have a single conversion to a pointer-to-object type. Perform |
| 2040 | // that conversion. |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2041 | // TODO: don't redo the conversion calculation. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2042 | ExprResult Res = |
| 2043 | PerformImplicitConversion(Ex.get(), |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2044 | ObjectPtrConversions.front()->getConversionType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2045 | AA_Converting); |
| 2046 | if (Res.isUsable()) { |
| 2047 | Ex = move(Res); |
| 2048 | Type = Ex.get()->getType(); |
Fariborz Jahanian | adcea10 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 2049 | } |
| 2050 | } |
| 2051 | else if (ObjectPtrConversions.size() > 1) { |
| 2052 | Diag(StartLoc, diag::err_ambiguous_delete_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2053 | << Type << Ex.get()->getSourceRange(); |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2054 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) |
| 2055 | NoteOverloadCandidate(ObjectPtrConversions[i]); |
Fariborz Jahanian | adcea10 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 2056 | return ExprError(); |
Douglas Gregor | 0fea62d | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 2057 | } |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2060 | if (!Type->isPointerType()) |
| 2061 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2062 | << Type << Ex.get()->getSourceRange()); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2063 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2064 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2065 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 2066 | |
| 2067 | if (unsigned AddressSpace = Pointee.getAddressSpace()) |
| 2068 | return Diag(Ex.get()->getLocStart(), |
| 2069 | diag::err_address_space_qualified_delete) |
| 2070 | << Pointee.getUnqualifiedType() << AddressSpace; |
| 2071 | |
| 2072 | CXXRecordDecl *PointeeRD = 0; |
Douglas Gregor | bb3348e | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 2073 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2074 | // 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] | 2075 | // effectively bans deletion of "void*". However, most compilers support |
| 2076 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 2077 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2078 | << Type << Ex.get()->getSourceRange(); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2079 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2080 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2081 | << Type << Ex.get()->getSourceRange()); |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2082 | } else if (!Pointee->isDependentType()) { |
| 2083 | if (!RequireCompleteType(StartLoc, Pointee, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 2084 | diag::warn_delete_incomplete, Ex.get())) { |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2085 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
| 2086 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2087 | } |
| 2088 | } |
| 2089 | |
Douglas Gregor | 98496dc | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 2090 | // C++ [expr.delete]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2091 | // [Note: a pointer to a const type can be the operand of a |
| 2092 | // delete-expression; it is not necessary to cast away the constness |
| 2093 | // (5.2.11) of the pointer expression before it is used as the operand |
Douglas Gregor | 98496dc | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 2094 | // of the delete-expression. ] |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2095 | if (!Context.hasSameType(Ex.get()->getType(), Context.VoidPtrTy)) |
Abramo Bagnara | d4756b9 | 2011-11-16 15:42:13 +0000 | [diff] [blame] | 2096 | Ex = Owned(ImplicitCastExpr::Create(Context, Context.VoidPtrTy, |
| 2097 | CK_BitCast, Ex.take(), 0, VK_RValue)); |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 2098 | |
| 2099 | if (Pointee->isArrayType() && !ArrayForm) { |
| 2100 | Diag(StartLoc, diag::warn_delete_array_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2101 | << Type << Ex.get()->getSourceRange() |
Argyrios Kyrtzidis | 14ec9f6 | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 2102 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]"); |
| 2103 | ArrayForm = true; |
| 2104 | } |
| 2105 | |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2106 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 2107 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 2108 | |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2109 | if (PointeeRD) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2110 | if (!UseGlobal && |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2111 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, |
| 2112 | OperatorDelete)) |
Anders Carlsson | 654e5c7 | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 2113 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2114 | |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 2115 | // If we're allocating an array of records, check whether the |
| 2116 | // usual operator delete[] has a size_t parameter. |
| 2117 | if (ArrayForm) { |
| 2118 | // If the user specifically asked to use the global allocator, |
| 2119 | // we'll need to do the lookup into the class. |
| 2120 | if (UseGlobal) |
| 2121 | UsualArrayDeleteWantsSize = |
| 2122 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); |
| 2123 | |
| 2124 | // Otherwise, the usual operator delete[] should be the |
| 2125 | // function we just found. |
| 2126 | else if (isa<CXXMethodDecl>(OperatorDelete)) |
| 2127 | UsualArrayDeleteWantsSize = (OperatorDelete->getNumParams() == 2); |
| 2128 | } |
| 2129 | |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 2130 | if (!PointeeRD->hasIrrelevantDestructor()) |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2131 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2132 | MarkFunctionReferenced(StartLoc, |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2133 | const_cast<CXXDestructorDecl*>(Dtor)); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2134 | DiagnoseUseOfDecl(Dtor, StartLoc); |
| 2135 | } |
Argyrios Kyrtzidis | 8bd4285 | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 2136 | |
| 2137 | // C++ [expr.delete]p3: |
| 2138 | // In the first alternative (delete object), if the static type of the |
| 2139 | // object to be deleted is different from its dynamic type, the static |
| 2140 | // type shall be a base class of the dynamic type of the object to be |
| 2141 | // deleted and the static type shall have a virtual destructor or the |
| 2142 | // behavior is undefined. |
| 2143 | // |
| 2144 | // Note: a final class cannot be derived from, no issue there |
Eli Friedman | 1b71a22 | 2011-07-26 23:27:24 +0000 | [diff] [blame] | 2145 | if (PointeeRD->isPolymorphic() && !PointeeRD->hasAttr<FinalAttr>()) { |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2146 | CXXDestructorDecl *dtor = PointeeRD->getDestructor(); |
Eli Friedman | 1b71a22 | 2011-07-26 23:27:24 +0000 | [diff] [blame] | 2147 | if (dtor && !dtor->isVirtual()) { |
| 2148 | if (PointeeRD->isAbstract()) { |
| 2149 | // If the class is abstract, we warn by default, because we're |
| 2150 | // sure the code has undefined behavior. |
| 2151 | Diag(StartLoc, diag::warn_delete_abstract_non_virtual_dtor) |
| 2152 | << PointeeElem; |
| 2153 | } else if (!ArrayForm) { |
| 2154 | // Otherwise, if this is not an array delete, it's a bit suspect, |
| 2155 | // but not necessarily wrong. |
| 2156 | Diag(StartLoc, diag::warn_delete_non_virtual_dtor) << PointeeElem; |
| 2157 | } |
| 2158 | } |
Argyrios Kyrtzidis | 8bd4285 | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 2159 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2160 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2161 | } else if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2162 | PointeeElem->isObjCLifetimeType() && |
| 2163 | (PointeeElem.getObjCLifetime() == Qualifiers::OCL_Strong || |
| 2164 | PointeeElem.getObjCLifetime() == Qualifiers::OCL_Weak) && |
| 2165 | ArrayForm) { |
| 2166 | Diag(StartLoc, diag::warn_err_new_delete_object_array) |
| 2167 | << 1 << PointeeElem; |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2168 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2169 | |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2170 | if (!OperatorDelete) { |
Anders Carlsson | e1d34ba0 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2171 | // Look for a global declaration. |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2172 | DeclareGlobalNewDelete(); |
| 2173 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2174 | Expr *Arg = Ex.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2176 | &Arg, 1, TUDecl, /*AllowMissing=*/false, |
Anders Carlsson | a471db0 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2177 | OperatorDelete)) |
| 2178 | return ExprError(); |
| 2179 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2180 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2181 | MarkFunctionReferenced(StartLoc, OperatorDelete); |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 2182 | |
Douglas Gregor | fa77813 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 2183 | // Check access and ambiguity of operator delete and destructor. |
Eli Friedman | ae4280f | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2184 | if (PointeeRD) { |
| 2185 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2186 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
Douglas Gregor | fa77813 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 2187 | PDiag(diag::err_access_dtor) << PointeeElem); |
| 2188 | } |
| 2189 | } |
| 2190 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2193 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 2194 | ArrayFormAsWritten, |
| 2195 | UsualArrayDeleteWantsSize, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2196 | OperatorDelete, Ex.take(), StartLoc)); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2199 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 2200 | /// while, do-while, or switch statement. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2201 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2202 | SourceLocation StmtLoc, |
| 2203 | bool ConvertToBoolean) { |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2204 | QualType T = ConditionVar->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2205 | |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2206 | // C++ [stmt.select]p2: |
| 2207 | // The declarator shall not specify a function or an array. |
| 2208 | if (T->isFunctionType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2209 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2210 | diag::err_invalid_use_of_function_type) |
| 2211 | << ConditionVar->getSourceRange()); |
| 2212 | else if (T->isArrayType()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2213 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2214 | diag::err_invalid_use_of_array_type) |
| 2215 | << ConditionVar->getSourceRange()); |
Douglas Gregor | 0156d1c | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 2216 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2217 | ExprResult Condition = |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2218 | Owned(DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), |
| 2219 | SourceLocation(), |
| 2220 | ConditionVar, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2221 | /*enclosing*/ false, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2222 | ConditionVar->getLocation(), |
| 2223 | ConditionVar->getType().getNonReferenceType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2224 | VK_LValue)); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 2225 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2226 | MarkDeclRefReferenced(cast<DeclRefExpr>(Condition.get())); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 2227 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2228 | if (ConvertToBoolean) { |
| 2229 | Condition = CheckBooleanCondition(Condition.take(), StmtLoc); |
| 2230 | if (Condition.isInvalid()) |
| 2231 | return ExprError(); |
| 2232 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2233 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2234 | return move(Condition); |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2237 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2238 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr) { |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2239 | // C++ 6.4p4: |
| 2240 | // The value of a condition that is an initialized declaration in a statement |
| 2241 | // other than a switch statement is the value of the declared variable |
| 2242 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 2243 | // program is ill-formed. |
| 2244 | // The value of a condition that is an expression is the value of the |
| 2245 | // expression, implicitly converted to bool. |
| 2246 | // |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2247 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2248 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2249 | |
| 2250 | /// Helper function to determine whether this is the (deprecated) C++ |
| 2251 | /// conversion from a string literal to a pointer to non-const char or |
| 2252 | /// non-const wchar_t (for narrow and wide string literals, |
| 2253 | /// respectively). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | bool |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2255 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 2256 | // Look inside the implicit cast, if it exists. |
| 2257 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 2258 | From = Cast->getSubExpr(); |
| 2259 | |
| 2260 | // A string literal (2.13.4) that is not a wide string literal can |
| 2261 | // be converted to an rvalue of type "pointer to char"; a wide |
| 2262 | // string literal can be converted to an rvalue of type "pointer |
| 2263 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 689999d | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 2264 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2265 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2266 | if (const BuiltinType *ToPointeeType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2267 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2268 | // This conversion is considered only when there is an |
| 2269 | // explicit appropriate pointer target type (C++ 4.2p2). |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2270 | if (!ToPtrType->getPointeeType().hasQualifiers()) { |
| 2271 | switch (StrLit->getKind()) { |
| 2272 | case StringLiteral::UTF8: |
| 2273 | case StringLiteral::UTF16: |
| 2274 | case StringLiteral::UTF32: |
| 2275 | // We don't allow UTF literals to be implicitly converted |
| 2276 | break; |
| 2277 | case StringLiteral::Ascii: |
| 2278 | return (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 2279 | ToPointeeType->getKind() == BuiltinType::Char_S); |
| 2280 | case StringLiteral::Wide: |
| 2281 | return ToPointeeType->isWideCharType(); |
| 2282 | } |
| 2283 | } |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | return false; |
| 2287 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2288 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2289 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2290 | SourceLocation CastLoc, |
| 2291 | QualType Ty, |
| 2292 | CastKind Kind, |
| 2293 | CXXMethodDecl *Method, |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2294 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2295 | bool HadMultipleCandidates, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2296 | Expr *From) { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2297 | switch (Kind) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2298 | default: llvm_unreachable("Unhandled cast kind!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2299 | case CK_ConstructorConversion: { |
Douglas Gregor | c7a3107 | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2300 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method); |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2301 | ASTOwningVector<Expr*> ConstructorArgs(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2302 | |
Douglas Gregor | c7a3107 | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2303 | if (S.CompleteConstructorCall(Constructor, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2304 | MultiExprArg(&From, 1), |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2305 | CastLoc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2306 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2307 | |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 2308 | S.CheckConstructorAccess(CastLoc, Constructor, |
| 2309 | InitializedEntity::InitializeTemporary(Ty), |
| 2310 | Constructor->getAccess()); |
Douglas Gregor | c7a3107 | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2311 | |
| 2312 | ExprResult Result |
| 2313 | = S.BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), |
| 2314 | move_arg(ConstructorArgs), |
| 2315 | HadMultipleCandidates, /*ZeroInit*/ false, |
| 2316 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2317 | if (Result.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2318 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2319 | |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2320 | return S.MaybeBindToTemporary(Result.takeAs<Expr>()); |
| 2321 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2322 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2323 | case CK_UserDefinedConversion: { |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2324 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2325 | |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2326 | // Create an implicit call expr that calls it. |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 2327 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(Method); |
| 2328 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Conv, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2329 | HadMultipleCandidates); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2330 | if (Result.isInvalid()) |
| 2331 | return ExprError(); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 2332 | // Record usage of conversion in an implicit cast. |
| 2333 | Result = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 2334 | Result.get()->getType(), |
| 2335 | CK_UserDefinedConversion, |
| 2336 | Result.get(), 0, |
| 2337 | Result.get()->getValueKind())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2338 | |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2339 | S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ 0, FoundDecl); |
| 2340 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2341 | return S.MaybeBindToTemporary(Result.get()); |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2342 | } |
| 2343 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2344 | } |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2345 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2346 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 2347 | /// expression From to the type ToType using the pre-computed implicit |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2348 | /// conversion sequence ICS. Returns the converted |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2349 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2350 | /// used in the error message. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2351 | ExprResult |
| 2352 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2353 | const ImplicitConversionSequence &ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2354 | AssignmentAction Action, |
| 2355 | CheckedConversionKind CCK) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2356 | switch (ICS.getKind()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2357 | case ImplicitConversionSequence::StandardConversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2358 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, |
| 2359 | Action, CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2360 | if (Res.isInvalid()) |
| 2361 | return ExprError(); |
| 2362 | From = Res.take(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2363 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2364 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2365 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2366 | case ImplicitConversionSequence::UserDefinedConversion: { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2367 | |
Fariborz Jahanian | 2fee79a | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 2368 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2369 | CastKind CastKind; |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2370 | QualType BeforeToType; |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 2371 | assert(FD && "FIXME: aggregate initialization from init list"); |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2372 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2373 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2374 | |
Anders Carlsson | 110b07b | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2375 | // If the user-defined conversion is specified by a conversion function, |
| 2376 | // the initial standard conversion sequence converts the source type to |
| 2377 | // the implicit object parameter of the conversion function. |
| 2378 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
John McCall | a03edda | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 2379 | } else { |
| 2380 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2381 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2382 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2383 | if (!ICS.UserDefined.EllipsisConversion) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2384 | // If the user-defined conversion is specified by a constructor, the |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2385 | // initial standard conversion sequence converts the source type to the |
| 2386 | // type required by the argument of the constructor |
Douglas Gregor | 3153da7 | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2387 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 2388 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2389 | } |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2390 | // Watch out for elipsis conversion. |
Fariborz Jahanian | eec642f | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 2391 | if (!ICS.UserDefined.EllipsisConversion) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2392 | ExprResult Res = |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2393 | PerformImplicitConversion(From, BeforeToType, |
| 2394 | ICS.UserDefined.Before, AA_Converting, |
| 2395 | CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2396 | if (Res.isInvalid()) |
| 2397 | return ExprError(); |
| 2398 | From = Res.take(); |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2399 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2400 | |
| 2401 | ExprResult CastArg |
Douglas Gregor | a425392 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2402 | = BuildCXXCastArgument(*this, |
| 2403 | From->getLocStart(), |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2404 | ToType.getNonReferenceType(), |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2405 | CastKind, cast<CXXMethodDecl>(FD), |
| 2406 | ICS.UserDefined.FoundConversionFunction, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2407 | ICS.UserDefined.HadMultipleCandidates, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2408 | From); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2409 | |
| 2410 | if (CastArg.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2411 | return ExprError(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2412 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2413 | From = CastArg.take(); |
Eli Friedman | e96f1d3 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2414 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2415 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
| 2416 | AA_Converting, CCK); |
Fariborz Jahanian | da21efb | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 2417 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2418 | |
| 2419 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2420 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2421 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 2422 | << From->getSourceRange()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2423 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2424 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2425 | case ImplicitConversionSequence::EllipsisConversion: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2426 | llvm_unreachable("Cannot perform an ellipsis conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2427 | |
| 2428 | case ImplicitConversionSequence::BadConversion: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2429 | return ExprError(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | // Everything went well. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2433 | return Owned(From); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2436 | /// PerformImplicitConversion - Perform an implicit conversion of the |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2437 | /// expression From to the type ToType by following the standard |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2438 | /// conversion sequence SCS. Returns the converted |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2439 | /// expression. Flavor is the context in which we're performing this |
| 2440 | /// conversion, for use in error messages. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2441 | ExprResult |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2442 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2443 | const StandardConversionSequence& SCS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2444 | AssignmentAction Action, |
| 2445 | CheckedConversionKind CCK) { |
| 2446 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
| 2447 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2448 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 2449 | // much extra work. What this means is that we need to keep track of more |
| 2450 | // information that is computed when we try the implicit conversion initially, |
| 2451 | // so that we don't need to recompute anything here. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2452 | QualType FromType = From->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2453 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2454 | if (SCS.CopyConstructor) { |
Anders Carlsson | 549c5bd | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 2455 | // FIXME: When can ToType be a reference type? |
| 2456 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2457 | if (SCS.Second == ICK_Derived_To_Base) { |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2458 | ASTOwningVector<Expr*> ConstructorArgs(*this); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2459 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2460 | MultiExprArg(*this, &From, 1), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2461 | /*FIXME:ConstructLoc*/SourceLocation(), |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2462 | ConstructorArgs)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2463 | return ExprError(); |
| 2464 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 2465 | ToType, SCS.CopyConstructor, |
| 2466 | move_arg(ConstructorArgs), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2467 | /*HadMultipleCandidates*/ false, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2468 | /*ZeroInit*/ false, |
| 2469 | CXXConstructExpr::CK_Complete, |
| 2470 | SourceRange()); |
Fariborz Jahanian | 49850df | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2471 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2472 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 2473 | ToType, SCS.CopyConstructor, |
| 2474 | MultiExprArg(*this, &From, 1), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2475 | /*HadMultipleCandidates*/ false, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2476 | /*ZeroInit*/ false, |
| 2477 | CXXConstructExpr::CK_Complete, |
| 2478 | SourceRange()); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2481 | // Resolve overloaded function references. |
| 2482 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 2483 | DeclAccessPair Found; |
| 2484 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 2485 | true, Found); |
| 2486 | if (!Fn) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2487 | return ExprError(); |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2488 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2489 | if (DiagnoseUseOfDecl(Fn, From->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2490 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2491 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2492 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 2493 | FromType = From->getType(); |
| 2494 | } |
| 2495 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2496 | // Perform the first implicit conversion. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2497 | switch (SCS.First) { |
| 2498 | case ICK_Identity: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2499 | // Nothing to do. |
| 2500 | break; |
| 2501 | |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 2502 | case ICK_Lvalue_To_Rvalue: { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 2503 | assert(From->getObjectKind() != OK_ObjCProperty); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2504 | FromType = FromType.getUnqualifiedType(); |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 2505 | ExprResult FromRes = DefaultLvalueConversion(From); |
| 2506 | assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!"); |
| 2507 | From = FromRes.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2508 | break; |
Eli Friedman | 946b7b5 | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 2509 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2511 | case ICK_Array_To_Pointer: |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2512 | FromType = Context.getArrayDecayedType(FromType); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2513 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, |
| 2514 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2515 | break; |
| 2516 | |
| 2517 | case ICK_Function_To_Pointer: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2518 | FromType = Context.getPointerType(FromType); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2519 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, |
| 2520 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2521 | break; |
| 2522 | |
| 2523 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2524 | llvm_unreachable("Improper first standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2527 | // Perform the second implicit conversion |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2528 | switch (SCS.Second) { |
| 2529 | case ICK_Identity: |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2530 | // If both sides are functions (or pointers/references to them), there could |
| 2531 | // be incompatible exception declarations. |
| 2532 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2533 | return ExprError(); |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2534 | // Nothing else to do. |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2535 | break; |
| 2536 | |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2537 | case ICK_NoReturn_Adjustment: |
| 2538 | // If both sides are functions (or pointers/references to them), there could |
| 2539 | // be incompatible exception declarations. |
| 2540 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2541 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2542 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2543 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 2544 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2545 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2546 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2547 | case ICK_Integral_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2548 | case ICK_Integral_Conversion: |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2549 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, |
| 2550 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2551 | break; |
| 2552 | |
| 2553 | case ICK_Floating_Promotion: |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2554 | case ICK_Floating_Conversion: |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2555 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, |
| 2556 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2557 | break; |
| 2558 | |
| 2559 | case ICK_Complex_Promotion: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2560 | case ICK_Complex_Conversion: { |
| 2561 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
| 2562 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
| 2563 | CastKind CK; |
| 2564 | if (FromEl->isRealFloatingType()) { |
| 2565 | if (ToEl->isRealFloatingType()) |
| 2566 | CK = CK_FloatingComplexCast; |
| 2567 | else |
| 2568 | CK = CK_FloatingComplexToIntegralComplex; |
| 2569 | } else if (ToEl->isRealFloatingType()) { |
| 2570 | CK = CK_IntegralComplexToFloatingComplex; |
| 2571 | } else { |
| 2572 | CK = CK_IntegralComplexCast; |
| 2573 | } |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2574 | From = ImpCastExprToType(From, ToType, CK, |
| 2575 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2576 | break; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2577 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2578 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2579 | case ICK_Floating_Integral: |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 2580 | if (ToType->isRealFloatingType()) |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2581 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, |
| 2582 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2583 | else |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2584 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, |
| 2585 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2586 | break; |
| 2587 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2588 | case ICK_Compatible_Conversion: |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2589 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 2590 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2593 | case ICK_Writeback_Conversion: |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2594 | case ICK_Pointer_Conversion: { |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2595 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2596 | // Diagnose incompatible Objective-C conversions |
Douglas Gregor | 2720dc6 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 2597 | if (Action == AA_Initializing || Action == AA_Assigning) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2598 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2599 | diag::ext_typecheck_convert_incompatible_pointer) |
| 2600 | << ToType << From->getType() << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 2601 | << From->getSourceRange() << 0; |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2602 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2603 | Diag(From->getLocStart(), |
Fariborz Jahanian | 413e064 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2604 | diag::ext_typecheck_convert_incompatible_pointer) |
| 2605 | << From->getType() << ToType << Action |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 2606 | << From->getSourceRange() << 0; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2607 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2608 | if (From->getType()->isObjCObjectPointerType() && |
| 2609 | ToType->isObjCObjectPointerType()) |
| 2610 | EmitRelatedResultTypeNote(From); |
Fariborz Jahanian | f291340 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2611 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2612 | else if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | f291340 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2613 | !CheckObjCARCUnavailableWeakConversion(ToType, |
| 2614 | From->getType())) { |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 2615 | if (Action == AA_Initializing) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2616 | Diag(From->getLocStart(), |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 2617 | diag::err_arc_weak_unavailable_assign); |
| 2618 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2619 | Diag(From->getLocStart(), |
John McCall | 9c3467e | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 2620 | diag::err_arc_convesion_of_weak_unavailable) |
| 2621 | << (Action == AA_Casting) << From->getType() << ToType |
| 2622 | << From->getSourceRange(); |
| 2623 | } |
Fariborz Jahanian | f291340 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2624 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2625 | CastKind Kind = CK_Invalid; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2626 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2627 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2628 | return ExprError(); |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2629 | |
| 2630 | // Make sure we extend blocks if necessary. |
| 2631 | // FIXME: doing this here is really ugly. |
| 2632 | if (Kind == CK_BlockPointerToObjCPointerCast) { |
| 2633 | ExprResult E = From; |
| 2634 | (void) PrepareCastToObjCObjectPointer(E); |
| 2635 | From = E.take(); |
| 2636 | } |
| 2637 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2638 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
| 2639 | .take(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2640 | break; |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2641 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2642 | |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2643 | case ICK_Pointer_Member: { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2644 | CastKind Kind = CK_Invalid; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2645 | CXXCastPath BasePath; |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2646 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2647 | return ExprError(); |
Sebastian Redl | 5d43164 | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2648 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2649 | return ExprError(); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2650 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
| 2651 | .take(); |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2652 | break; |
| 2653 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2654 | |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 2655 | case ICK_Boolean_Conversion: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2656 | // Perform half-to-boolean conversion via float. |
| 2657 | if (From->getType()->isHalfType()) { |
| 2658 | From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).take(); |
| 2659 | FromType = Context.FloatTy; |
| 2660 | } |
| 2661 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2662 | From = ImpCastExprToType(From, Context.BoolTy, |
| 2663 | ScalarTypeToBooleanCastKind(FromType), |
| 2664 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2665 | break; |
| 2666 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2667 | case ICK_Derived_To_Base: { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2668 | CXXCastPath BasePath; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2669 | if (CheckDerivedToBaseConversion(From->getType(), |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2670 | ToType.getNonReferenceType(), |
| 2671 | From->getLocStart(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2672 | From->getSourceRange(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2673 | &BasePath, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2674 | CStyle)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2675 | return ExprError(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2676 | |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2677 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 2678 | CK_DerivedToBase, From->getValueKind(), |
| 2679 | &BasePath, CCK).take(); |
Douglas Gregor | 02ba0ea | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2680 | break; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2683 | case ICK_Vector_Conversion: |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2684 | From = ImpCastExprToType(From, ToType, CK_BitCast, |
| 2685 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2686 | break; |
| 2687 | |
| 2688 | case ICK_Vector_Splat: |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2689 | From = ImpCastExprToType(From, ToType, CK_VectorSplat, |
| 2690 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2691 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2692 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2693 | case ICK_Complex_Real: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2694 | // Case 1. x -> _Complex y |
| 2695 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
| 2696 | QualType ElType = ToComplex->getElementType(); |
| 2697 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2698 | |
| 2699 | // x -> y |
| 2700 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
| 2701 | // do nothing |
| 2702 | } else if (From->getType()->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2703 | From = ImpCastExprToType(From, ElType, |
| 2704 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).take(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2705 | } else { |
| 2706 | assert(From->getType()->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2707 | From = ImpCastExprToType(From, ElType, |
| 2708 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).take(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2709 | } |
| 2710 | // y -> _Complex y |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2711 | From = ImpCastExprToType(From, ToType, |
| 2712 | isFloatingComplex ? CK_FloatingRealToComplex |
| 2713 | : CK_IntegralRealToComplex).take(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2714 | |
| 2715 | // Case 2. _Complex x -> y |
| 2716 | } else { |
| 2717 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
| 2718 | assert(FromComplex); |
| 2719 | |
| 2720 | QualType ElType = FromComplex->getElementType(); |
| 2721 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2722 | |
| 2723 | // _Complex x -> x |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2724 | From = ImpCastExprToType(From, ElType, |
| 2725 | isFloatingComplex ? CK_FloatingComplexToReal |
| 2726 | : CK_IntegralComplexToReal, |
| 2727 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2728 | |
| 2729 | // x -> y |
| 2730 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
| 2731 | // do nothing |
| 2732 | } else if (ToType->isRealFloatingType()) { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2733 | From = ImpCastExprToType(From, ToType, |
| 2734 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, |
| 2735 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2736 | } else { |
| 2737 | assert(ToType->isIntegerType()); |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2738 | From = ImpCastExprToType(From, ToType, |
| 2739 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, |
| 2740 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2741 | } |
| 2742 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2743 | break; |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2744 | |
| 2745 | case ICK_Block_Pointer_Conversion: { |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2746 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, |
| 2747 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2748 | break; |
| 2749 | } |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2750 | |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2751 | case ICK_TransparentUnionConversion: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2752 | ExprResult FromRes = Owned(From); |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2753 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2754 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); |
| 2755 | if (FromRes.isInvalid()) |
| 2756 | return ExprError(); |
| 2757 | From = FromRes.take(); |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2758 | assert ((ConvTy == Sema::Compatible) && |
| 2759 | "Improper transparent union conversion"); |
| 2760 | (void)ConvTy; |
| 2761 | break; |
| 2762 | } |
| 2763 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2764 | case ICK_Lvalue_To_Rvalue: |
| 2765 | case ICK_Array_To_Pointer: |
| 2766 | case ICK_Function_To_Pointer: |
| 2767 | case ICK_Qualification: |
| 2768 | case ICK_Num_Conversion_Kinds: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2769 | llvm_unreachable("Improper second standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | switch (SCS.Third) { |
| 2773 | case ICK_Identity: |
| 2774 | // Nothing to do. |
| 2775 | break; |
| 2776 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2777 | case ICK_Qualification: { |
| 2778 | // The qualification keeps the category of the inner expression, unless the |
| 2779 | // target type isn't a reference. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2780 | ExprValueKind VK = ToType->isReferenceType() ? |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 2781 | From->getValueKind() : VK_RValue; |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2782 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
| 2783 | CK_NoOp, VK, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2784 | |
Douglas Gregor | e981bb0 | 2011-03-14 16:13:32 +0000 | [diff] [blame] | 2785 | if (SCS.DeprecatedStringLiteralToCharPtr && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2786 | !getLangOpts().WritableStrings) |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2787 | Diag(From->getLocStart(), diag::warn_deprecated_string_literal_conversion) |
| 2788 | << ToType.getNonReferenceType(); |
| 2789 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2790 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2793 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2794 | llvm_unreachable("Improper third standard conversion"); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
Douglas Gregor | 298f43d | 2012-04-12 20:42:30 +0000 | [diff] [blame] | 2797 | // If this conversion sequence involved a scalar -> atomic conversion, perform |
| 2798 | // that conversion now. |
| 2799 | if (const AtomicType *ToAtomic = ToType->getAs<AtomicType>()) |
| 2800 | if (Context.hasSameType(ToAtomic->getValueType(), From->getType())) |
| 2801 | From = ImpCastExprToType(From, ToType, CK_NonAtomicToAtomic, VK_RValue, 0, |
| 2802 | CCK).take(); |
| 2803 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2804 | return Owned(From); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2807 | ExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 54e5b13 | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2808 | SourceLocation KWLoc, |
| 2809 | ParsedType Ty, |
| 2810 | SourceLocation RParen) { |
| 2811 | TypeSourceInfo *TSInfo; |
| 2812 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | 54e5b13 | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2814 | if (!TSInfo) |
| 2815 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2816 | return BuildUnaryTypeTrait(UTT, KWLoc, TSInfo, RParen); |
Douglas Gregor | 54e5b13 | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2819 | /// \brief Check the completeness of a type in a unary type trait. |
| 2820 | /// |
| 2821 | /// If the particular type trait requires a complete type, tries to complete |
| 2822 | /// it. If completing the type fails, a diagnostic is emitted and false |
| 2823 | /// returned. If completing the type succeeds or no completion was required, |
| 2824 | /// returns true. |
| 2825 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, |
| 2826 | UnaryTypeTrait UTT, |
| 2827 | SourceLocation Loc, |
| 2828 | QualType ArgTy) { |
| 2829 | // C++0x [meta.unary.prop]p3: |
| 2830 | // For all of the class templates X declared in this Clause, instantiating |
| 2831 | // that template with a template argument that is a class template |
| 2832 | // specialization may result in the implicit instantiation of the template |
| 2833 | // argument if and only if the semantics of X require that the argument |
| 2834 | // must be a complete type. |
| 2835 | // We apply this rule to all the type trait expressions used to implement |
| 2836 | // these class templates. We also try to follow any GCC documented behavior |
| 2837 | // in these expressions to ensure portability of standard libraries. |
| 2838 | switch (UTT) { |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2839 | // is_complete_type somewhat obviously cannot require a complete type. |
| 2840 | case UTT_IsCompleteType: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2841 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2842 | |
| 2843 | // These traits are modeled on the type predicates in C++0x |
| 2844 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as |
| 2845 | // requiring a complete type, as whether or not they return true cannot be |
| 2846 | // impacted by the completeness of the type. |
| 2847 | case UTT_IsVoid: |
| 2848 | case UTT_IsIntegral: |
| 2849 | case UTT_IsFloatingPoint: |
| 2850 | case UTT_IsArray: |
| 2851 | case UTT_IsPointer: |
| 2852 | case UTT_IsLvalueReference: |
| 2853 | case UTT_IsRvalueReference: |
| 2854 | case UTT_IsMemberFunctionPointer: |
| 2855 | case UTT_IsMemberObjectPointer: |
| 2856 | case UTT_IsEnum: |
| 2857 | case UTT_IsUnion: |
| 2858 | case UTT_IsClass: |
| 2859 | case UTT_IsFunction: |
| 2860 | case UTT_IsReference: |
| 2861 | case UTT_IsArithmetic: |
| 2862 | case UTT_IsFundamental: |
| 2863 | case UTT_IsObject: |
| 2864 | case UTT_IsScalar: |
| 2865 | case UTT_IsCompound: |
| 2866 | case UTT_IsMemberPointer: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2867 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2868 | |
| 2869 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] |
| 2870 | // which requires some of its traits to have the complete type. However, |
| 2871 | // the completeness of the type cannot impact these traits' semantics, and |
| 2872 | // so they don't require it. This matches the comments on these traits in |
| 2873 | // Table 49. |
| 2874 | case UTT_IsConst: |
| 2875 | case UTT_IsVolatile: |
| 2876 | case UTT_IsSigned: |
| 2877 | case UTT_IsUnsigned: |
| 2878 | return true; |
| 2879 | |
| 2880 | // C++0x [meta.unary.prop] Table 49 requires the following traits to be |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2881 | // applied to a complete type. |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2882 | case UTT_IsTrivial: |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2883 | case UTT_IsTriviallyCopyable: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2884 | case UTT_IsStandardLayout: |
| 2885 | case UTT_IsPOD: |
| 2886 | case UTT_IsLiteral: |
| 2887 | case UTT_IsEmpty: |
| 2888 | case UTT_IsPolymorphic: |
| 2889 | case UTT_IsAbstract: |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2890 | // Fall-through |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2891 | |
Douglas Gregor | dca70af | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 2892 | // These traits require a complete type. |
| 2893 | case UTT_IsFinal: |
| 2894 | |
Chandler Carruth | a62d8a5 | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2895 | // These trait expressions are designed to help implement predicates in |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2896 | // [meta.unary.prop] despite not being named the same. They are specified |
| 2897 | // by both GCC and the Embarcadero C++ compiler, and require the complete |
| 2898 | // type due to the overarching C++0x type predicates being implemented |
| 2899 | // requiring the complete type. |
| 2900 | case UTT_HasNothrowAssign: |
| 2901 | case UTT_HasNothrowConstructor: |
| 2902 | case UTT_HasNothrowCopy: |
| 2903 | case UTT_HasTrivialAssign: |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2904 | case UTT_HasTrivialDefaultConstructor: |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2905 | case UTT_HasTrivialCopy: |
| 2906 | case UTT_HasTrivialDestructor: |
| 2907 | case UTT_HasVirtualDestructor: |
| 2908 | // Arrays of unknown bound are expressly allowed. |
| 2909 | QualType ElTy = ArgTy; |
| 2910 | if (ArgTy->isIncompleteArrayType()) |
| 2911 | ElTy = S.Context.getAsArrayType(ArgTy)->getElementType(); |
| 2912 | |
| 2913 | // The void type is expressly allowed. |
| 2914 | if (ElTy->isVoidType()) |
| 2915 | return true; |
| 2916 | |
| 2917 | return !S.RequireCompleteType( |
| 2918 | Loc, ElTy, diag::err_incomplete_type_used_in_type_trait_expr); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2919 | } |
Chandler Carruth | 8b0cf1d | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 2920 | llvm_unreachable("Type trait not handled by switch"); |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | static bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT, |
| 2924 | SourceLocation KeyLoc, QualType T) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 2925 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2926 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2927 | ASTContext &C = Self.Context; |
| 2928 | switch(UTT) { |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2929 | // Type trait expressions corresponding to the primary type category |
| 2930 | // predicates in C++0x [meta.unary.cat]. |
| 2931 | case UTT_IsVoid: |
| 2932 | return T->isVoidType(); |
| 2933 | case UTT_IsIntegral: |
| 2934 | return T->isIntegralType(C); |
| 2935 | case UTT_IsFloatingPoint: |
| 2936 | return T->isFloatingType(); |
| 2937 | case UTT_IsArray: |
| 2938 | return T->isArrayType(); |
| 2939 | case UTT_IsPointer: |
| 2940 | return T->isPointerType(); |
| 2941 | case UTT_IsLvalueReference: |
| 2942 | return T->isLValueReferenceType(); |
| 2943 | case UTT_IsRvalueReference: |
| 2944 | return T->isRValueReferenceType(); |
| 2945 | case UTT_IsMemberFunctionPointer: |
| 2946 | return T->isMemberFunctionPointerType(); |
| 2947 | case UTT_IsMemberObjectPointer: |
| 2948 | return T->isMemberDataPointerType(); |
| 2949 | case UTT_IsEnum: |
| 2950 | return T->isEnumeralType(); |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2951 | case UTT_IsUnion: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2952 | return T->isUnionType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2953 | case UTT_IsClass: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2954 | return T->isClassType() || T->isStructureType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2955 | case UTT_IsFunction: |
| 2956 | return T->isFunctionType(); |
| 2957 | |
| 2958 | // Type trait expressions which correspond to the convenient composition |
| 2959 | // predicates in C++0x [meta.unary.comp]. |
| 2960 | case UTT_IsReference: |
| 2961 | return T->isReferenceType(); |
| 2962 | case UTT_IsArithmetic: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2963 | return T->isArithmeticType() && !T->isEnumeralType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2964 | case UTT_IsFundamental: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2965 | return T->isFundamentalType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2966 | case UTT_IsObject: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2967 | return T->isObjectType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2968 | case UTT_IsScalar: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2969 | // Note: semantic analysis depends on Objective-C lifetime types to be |
| 2970 | // considered scalar types. However, such types do not actually behave |
| 2971 | // like scalar types at run time (since they may require retain/release |
| 2972 | // operations), so we report them as non-scalar. |
| 2973 | if (T->isObjCLifetimeType()) { |
| 2974 | switch (T.getObjCLifetime()) { |
| 2975 | case Qualifiers::OCL_None: |
| 2976 | case Qualifiers::OCL_ExplicitNone: |
| 2977 | return true; |
| 2978 | |
| 2979 | case Qualifiers::OCL_Strong: |
| 2980 | case Qualifiers::OCL_Weak: |
| 2981 | case Qualifiers::OCL_Autoreleasing: |
| 2982 | return false; |
| 2983 | } |
| 2984 | } |
| 2985 | |
Chandler Carruth | 7ba7bd3 | 2011-05-01 09:29:55 +0000 | [diff] [blame] | 2986 | return T->isScalarType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2987 | case UTT_IsCompound: |
Chandler Carruth | af85886 | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2988 | return T->isCompoundType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2989 | case UTT_IsMemberPointer: |
| 2990 | return T->isMemberPointerType(); |
| 2991 | |
| 2992 | // Type trait expressions which correspond to the type property predicates |
| 2993 | // in C++0x [meta.unary.prop]. |
| 2994 | case UTT_IsConst: |
| 2995 | return T.isConstQualified(); |
| 2996 | case UTT_IsVolatile: |
| 2997 | return T.isVolatileQualified(); |
| 2998 | case UTT_IsTrivial: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2999 | return T.isTrivialType(Self.Context); |
Alexis Hunt | d9a5cc1 | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 3000 | case UTT_IsTriviallyCopyable: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3001 | return T.isTriviallyCopyableType(Self.Context); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 3002 | case UTT_IsStandardLayout: |
| 3003 | return T->isStandardLayoutType(); |
| 3004 | case UTT_IsPOD: |
Benjamin Kramer | a3c0dad | 2012-04-28 10:00:33 +0000 | [diff] [blame] | 3005 | return T.isPODType(Self.Context); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 3006 | case UTT_IsLiteral: |
| 3007 | return T->isLiteralType(); |
| 3008 | case UTT_IsEmpty: |
| 3009 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 3010 | return !RD->isUnion() && RD->isEmpty(); |
| 3011 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3012 | case UTT_IsPolymorphic: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 3013 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 3014 | return RD->isPolymorphic(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3015 | return false; |
| 3016 | case UTT_IsAbstract: |
Chandler Carruth | 100f3a9 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 3017 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 3018 | return RD->isAbstract(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3019 | return false; |
Douglas Gregor | dca70af | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 3020 | case UTT_IsFinal: |
| 3021 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 3022 | return RD->hasAttr<FinalAttr>(); |
| 3023 | return false; |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3024 | case UTT_IsSigned: |
| 3025 | return T->isSignedIntegerType(); |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3026 | case UTT_IsUnsigned: |
| 3027 | return T->isUnsignedIntegerType(); |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 3028 | |
| 3029 | // Type trait expressions which query classes regarding their construction, |
| 3030 | // destruction, and copying. Rather than being based directly on the |
| 3031 | // related type predicates in the standard, they are specified by both |
| 3032 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those |
| 3033 | // specifications. |
| 3034 | // |
| 3035 | // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html |
| 3036 | // 2: http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 3037 | case UTT_HasTrivialDefaultConstructor: |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3038 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3039 | // If __is_pod (type) is true then the trait is true, else if type is |
| 3040 | // a cv class or union type (or array thereof) with a trivial default |
| 3041 | // constructor ([class.ctor]) then the trait is true, else it is false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3042 | if (T.isPODType(Self.Context)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3043 | return true; |
| 3044 | if (const RecordType *RT = |
| 3045 | C.getBaseElementType(T)->getAs<RecordType>()) |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 3046 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDefaultConstructor(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3047 | return false; |
| 3048 | case UTT_HasTrivialCopy: |
| 3049 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3050 | // If __is_pod (type) is true or type is a reference type then |
| 3051 | // the trait is true, else if type is a cv class or union type |
| 3052 | // with a trivial copy constructor ([class.copy]) then the trait |
| 3053 | // is true, else it is false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3054 | if (T.isPODType(Self.Context) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3055 | return true; |
| 3056 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 3057 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor(); |
| 3058 | return false; |
| 3059 | case UTT_HasTrivialAssign: |
| 3060 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3061 | // If type is const qualified or is a reference type then the |
| 3062 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 3063 | // trait is true, else if type is a cv class or union type with |
| 3064 | // a trivial copy assignment ([class.copy]) then the trait is |
| 3065 | // true, else it is false. |
| 3066 | // Note: the const and reference restrictions are interesting, |
| 3067 | // given that const and reference members don't prevent a class |
| 3068 | // from having a trivial copy assignment operator (but do cause |
| 3069 | // errors if the copy assignment operator is actually used, q.v. |
| 3070 | // [class.copy]p12). |
| 3071 | |
| 3072 | if (C.getBaseElementType(T).isConstQualified()) |
| 3073 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3074 | if (T.isPODType(Self.Context)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3075 | return true; |
| 3076 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 3077 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment(); |
| 3078 | return false; |
| 3079 | case UTT_HasTrivialDestructor: |
| 3080 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3081 | // If __is_pod (type) is true or type is a reference type |
| 3082 | // then the trait is true, else if type is a cv class or union |
| 3083 | // type (or array thereof) with a trivial destructor |
| 3084 | // ([class.dtor]) then the trait is true, else it is |
| 3085 | // false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3086 | if (T.isPODType(Self.Context) || T->isReferenceType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3087 | return true; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3088 | |
| 3089 | // Objective-C++ ARC: autorelease types don't require destruction. |
| 3090 | if (T->isObjCLifetimeType() && |
| 3091 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 3092 | return true; |
| 3093 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3094 | if (const RecordType *RT = |
| 3095 | C.getBaseElementType(T)->getAs<RecordType>()) |
| 3096 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor(); |
| 3097 | return false; |
| 3098 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 3099 | case UTT_HasNothrowAssign: |
| 3100 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3101 | // If type is const qualified or is a reference type then the |
| 3102 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 3103 | // is true then the trait is true, else if type is a cv class |
| 3104 | // or union type with copy assignment operators that are known |
| 3105 | // not to throw an exception then the trait is true, else it is |
| 3106 | // false. |
| 3107 | if (C.getBaseElementType(T).isConstQualified()) |
| 3108 | return false; |
| 3109 | if (T->isReferenceType()) |
| 3110 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3111 | if (T.isPODType(Self.Context) || T->isObjCLifetimeType()) |
| 3112 | return true; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3113 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3114 | CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3115 | if (RD->hasTrivialCopyAssignment()) |
| 3116 | return true; |
| 3117 | |
| 3118 | bool FoundAssign = false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3119 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal); |
Sebastian Redl | 058fc82 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 3120 | LookupResult Res(Self, DeclarationNameInfo(Name, KeyLoc), |
| 3121 | Sema::LookupOrdinaryName); |
| 3122 | if (Self.LookupQualifiedName(Res, RD)) { |
Douglas Gregor | 6a0e23f | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 3123 | Res.suppressDiagnostics(); |
Sebastian Redl | 058fc82 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 3124 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 3125 | Op != OpEnd; ++Op) { |
Douglas Gregor | 6a0e23f | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 3126 | if (isa<FunctionTemplateDecl>(*Op)) |
| 3127 | continue; |
| 3128 | |
Sebastian Redl | 058fc82 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 3129 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 3130 | if (Operator->isCopyAssignmentOperator()) { |
| 3131 | FoundAssign = true; |
| 3132 | const FunctionProtoType *CPT |
| 3133 | = Operator->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3134 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 3135 | if (!CPT) |
| 3136 | return false; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3137 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 3138 | return false; |
| 3139 | if (!CPT->isNothrow(Self.Context)) |
| 3140 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | } |
Douglas Gregor | 6a0e23f | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 3144 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3145 | return FoundAssign; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3146 | } |
| 3147 | return false; |
| 3148 | case UTT_HasNothrowCopy: |
| 3149 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3150 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 3151 | // if type is a cv class or union type with copy constructors that are |
| 3152 | // known not to throw an exception then the trait is true, else it is |
| 3153 | // false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3154 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3155 | return true; |
| 3156 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3157 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 3158 | if (RD->hasTrivialCopyConstructor()) |
| 3159 | return true; |
| 3160 | |
| 3161 | bool FoundConstructor = false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3162 | unsigned FoundTQs; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3163 | DeclContext::lookup_const_iterator Con, ConEnd; |
Sebastian Redl | 951006f | 2010-09-13 21:10:20 +0000 | [diff] [blame] | 3164 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3165 | Con != ConEnd; ++Con) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 3166 | // A template constructor is never a copy constructor. |
| 3167 | // FIXME: However, it may actually be selected at the actual overload |
| 3168 | // resolution point. |
| 3169 | if (isa<FunctionTemplateDecl>(*Con)) |
| 3170 | continue; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3171 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 3172 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 3173 | FoundConstructor = true; |
| 3174 | const FunctionProtoType *CPT |
| 3175 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3176 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 3177 | if (!CPT) |
| 3178 | return false; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3179 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 3180 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3181 | // FIXME: check whether evaluating default arguments can throw. |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 3182 | // For now, we'll be conservative and assume that they can throw. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3183 | if (!CPT->isNothrow(Self.Context) || CPT->getNumArgs() > 1) |
| 3184 | return false; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3188 | return FoundConstructor; |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3189 | } |
| 3190 | return false; |
| 3191 | case UTT_HasNothrowConstructor: |
| 3192 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3193 | // If __has_trivial_constructor (type) is true then the trait is |
| 3194 | // true, else if type is a cv class or union type (or array |
| 3195 | // thereof) with a default constructor that is known not to |
| 3196 | // throw an exception then the trait is true, else it is false. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3197 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3198 | return true; |
| 3199 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) { |
| 3200 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 3201 | if (RD->hasTrivialDefaultConstructor()) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3202 | return true; |
| 3203 | |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 3204 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 3205 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
| 3206 | Con != ConEnd; ++Con) { |
Sebastian Redl | 5c649bc | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 3207 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 3208 | if (isa<FunctionTemplateDecl>(*Con)) |
| 3209 | continue; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 3210 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 3211 | if (Constructor->isDefaultConstructor()) { |
| 3212 | const FunctionProtoType *CPT |
| 3213 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3214 | CPT = Self.ResolveExceptionSpec(KeyLoc, CPT); |
| 3215 | if (!CPT) |
| 3216 | return false; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3217 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 3218 | return false; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 3219 | // TODO: check whether evaluating default arguments can throw. |
| 3220 | // For now, we'll be conservative and assume that they can throw. |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 3221 | return CPT->isNothrow(Self.Context) && CPT->getNumArgs() == 0; |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 3222 | } |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3223 | } |
| 3224 | } |
| 3225 | return false; |
| 3226 | case UTT_HasVirtualDestructor: |
| 3227 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3228 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 3229 | // then the trait is true, else it is false. |
| 3230 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 3231 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
Sebastian Redl | 058fc82 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 3232 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3233 | return Destructor->isVirtual(); |
| 3234 | } |
| 3235 | return false; |
Chandler Carruth | d2479ea | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 3236 | |
| 3237 | // These type trait expressions are modeled on the specifications for the |
| 3238 | // Embarcadero C++0x type trait functions: |
| 3239 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
| 3240 | case UTT_IsCompleteType: |
| 3241 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): |
| 3242 | // Returns True if and only if T is a complete type at the point of the |
| 3243 | // function call. |
| 3244 | return !T->isIncompleteType(); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3245 | } |
Chandler Carruth | b42fb19 | 2011-05-01 07:44:17 +0000 | [diff] [blame] | 3246 | llvm_unreachable("Type trait not covered by switch"); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | ExprResult Sema::BuildUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 54e5b13 | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 3250 | SourceLocation KWLoc, |
| 3251 | TypeSourceInfo *TSInfo, |
| 3252 | SourceLocation RParen) { |
| 3253 | QualType T = TSInfo->getType(); |
Chandler Carruth | b077620 | 2011-04-30 10:07:32 +0000 | [diff] [blame] | 3254 | if (!CheckUnaryTypeTraitTypeCompleteness(*this, UTT, KWLoc, T)) |
| 3255 | return ExprError(); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3256 | |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3257 | bool Value = false; |
| 3258 | if (!T->isDependentType()) |
Chandler Carruth | 8e172c6 | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 3259 | Value = EvaluateUnaryTypeTrait(*this, UTT, KWLoc, T); |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3260 | |
| 3261 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, UTT, TSInfo, Value, |
Anders Carlsson | 1f9648d | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 3262 | RParen, Context.BoolTy)); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3263 | } |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3264 | |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3265 | ExprResult Sema::ActOnBinaryTypeTrait(BinaryTypeTrait BTT, |
| 3266 | SourceLocation KWLoc, |
| 3267 | ParsedType LhsTy, |
| 3268 | ParsedType RhsTy, |
| 3269 | SourceLocation RParen) { |
| 3270 | TypeSourceInfo *LhsTSInfo; |
| 3271 | QualType LhsT = GetTypeFromParser(LhsTy, &LhsTSInfo); |
| 3272 | if (!LhsTSInfo) |
| 3273 | LhsTSInfo = Context.getTrivialTypeSourceInfo(LhsT); |
| 3274 | |
| 3275 | TypeSourceInfo *RhsTSInfo; |
| 3276 | QualType RhsT = GetTypeFromParser(RhsTy, &RhsTSInfo); |
| 3277 | if (!RhsTSInfo) |
| 3278 | RhsTSInfo = Context.getTrivialTypeSourceInfo(RhsT); |
| 3279 | |
| 3280 | return BuildBinaryTypeTrait(BTT, KWLoc, LhsTSInfo, RhsTSInfo, RParen); |
| 3281 | } |
| 3282 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3283 | static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc, |
| 3284 | ArrayRef<TypeSourceInfo *> Args, |
| 3285 | SourceLocation RParenLoc) { |
| 3286 | switch (Kind) { |
| 3287 | case clang::TT_IsTriviallyConstructible: { |
| 3288 | // C++11 [meta.unary.prop]: |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 3289 | // is_trivially_constructible is defined as: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3290 | // |
Dmitri Gribenko | 85e8764 | 2012-02-24 20:03:35 +0000 | [diff] [blame] | 3291 | // is_constructible<T, Args...>::value is true and the variable |
| 3292 | // definition for is_constructible, as defined below, is known to call no |
| 3293 | // operation that is not trivial. |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3294 | // |
| 3295 | // The predicate condition for a template specialization |
| 3296 | // is_constructible<T, Args...> shall be satisfied if and only if the |
| 3297 | // following variable definition would be well-formed for some invented |
| 3298 | // variable t: |
| 3299 | // |
| 3300 | // T t(create<Args>()...); |
| 3301 | if (Args.empty()) { |
| 3302 | S.Diag(KWLoc, diag::err_type_trait_arity) |
| 3303 | << 1 << 1 << 1 << (int)Args.size(); |
| 3304 | return false; |
| 3305 | } |
| 3306 | |
| 3307 | bool SawVoid = false; |
| 3308 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3309 | if (Args[I]->getType()->isVoidType()) { |
| 3310 | SawVoid = true; |
| 3311 | continue; |
| 3312 | } |
| 3313 | |
| 3314 | if (!Args[I]->getType()->isIncompleteType() && |
| 3315 | S.RequireCompleteType(KWLoc, Args[I]->getType(), |
| 3316 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 3317 | return false; |
| 3318 | } |
| 3319 | |
| 3320 | // If any argument was 'void', of course it won't type-check. |
| 3321 | if (SawVoid) |
| 3322 | return false; |
| 3323 | |
| 3324 | llvm::SmallVector<OpaqueValueExpr, 2> OpaqueArgExprs; |
| 3325 | llvm::SmallVector<Expr *, 2> ArgExprs; |
| 3326 | ArgExprs.reserve(Args.size() - 1); |
| 3327 | for (unsigned I = 1, N = Args.size(); I != N; ++I) { |
| 3328 | QualType T = Args[I]->getType(); |
| 3329 | if (T->isObjectType() || T->isFunctionType()) |
| 3330 | T = S.Context.getRValueReferenceType(T); |
| 3331 | OpaqueArgExprs.push_back( |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3332 | OpaqueValueExpr(Args[I]->getTypeLoc().getLocStart(), |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3333 | T.getNonLValueExprType(S.Context), |
| 3334 | Expr::getValueKindForType(T))); |
| 3335 | ArgExprs.push_back(&OpaqueArgExprs.back()); |
| 3336 | } |
| 3337 | |
| 3338 | // Perform the initialization in an unevaluated context within a SFINAE |
| 3339 | // trap at translation unit scope. |
| 3340 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 3341 | Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true); |
| 3342 | Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl()); |
| 3343 | InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0])); |
| 3344 | InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc, |
| 3345 | RParenLoc)); |
| 3346 | InitializationSequence Init(S, To, InitKind, |
| 3347 | ArgExprs.begin(), ArgExprs.size()); |
| 3348 | if (Init.Failed()) |
| 3349 | return false; |
| 3350 | |
| 3351 | ExprResult Result = Init.Perform(S, To, InitKind, |
| 3352 | MultiExprArg(ArgExprs.data(), |
| 3353 | ArgExprs.size())); |
| 3354 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 3355 | return false; |
| 3356 | |
| 3357 | // The initialization succeeded; not make sure there are no non-trivial |
| 3358 | // calls. |
| 3359 | return !Result.get()->hasNonTrivialCall(S.Context); |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | return false; |
| 3364 | } |
| 3365 | |
| 3366 | ExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 3367 | ArrayRef<TypeSourceInfo *> Args, |
| 3368 | SourceLocation RParenLoc) { |
| 3369 | bool Dependent = false; |
| 3370 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3371 | if (Args[I]->getType()->isDependentType()) { |
| 3372 | Dependent = true; |
| 3373 | break; |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | bool Value = false; |
| 3378 | if (!Dependent) |
| 3379 | Value = evaluateTypeTrait(*this, Kind, KWLoc, Args, RParenLoc); |
| 3380 | |
| 3381 | return TypeTraitExpr::Create(Context, Context.BoolTy, KWLoc, Kind, |
| 3382 | Args, RParenLoc, Value); |
| 3383 | } |
| 3384 | |
| 3385 | ExprResult Sema::ActOnTypeTrait(TypeTrait Kind, SourceLocation KWLoc, |
| 3386 | ArrayRef<ParsedType> Args, |
| 3387 | SourceLocation RParenLoc) { |
| 3388 | llvm::SmallVector<TypeSourceInfo *, 4> ConvertedArgs; |
| 3389 | ConvertedArgs.reserve(Args.size()); |
| 3390 | |
| 3391 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3392 | TypeSourceInfo *TInfo; |
| 3393 | QualType T = GetTypeFromParser(Args[I], &TInfo); |
| 3394 | if (!TInfo) |
| 3395 | TInfo = Context.getTrivialTypeSourceInfo(T, KWLoc); |
| 3396 | |
| 3397 | ConvertedArgs.push_back(TInfo); |
| 3398 | } |
| 3399 | |
| 3400 | return BuildTypeTrait(Kind, KWLoc, ConvertedArgs, RParenLoc); |
| 3401 | } |
| 3402 | |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3403 | static bool EvaluateBinaryTypeTrait(Sema &Self, BinaryTypeTrait BTT, |
| 3404 | QualType LhsT, QualType RhsT, |
| 3405 | SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3406 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
| 3407 | "Cannot evaluate traits of dependent types"); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3408 | |
| 3409 | switch(BTT) { |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3410 | case BTT_IsBaseOf: { |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3411 | // C++0x [meta.rel]p2 |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3412 | // 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] | 3413 | // Base and Derived are not unions and name the same class type without |
| 3414 | // regard to cv-qualifiers. |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3415 | |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3416 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
| 3417 | if (!lhsRecord) return false; |
| 3418 | |
| 3419 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
| 3420 | if (!rhsRecord) return false; |
| 3421 | |
| 3422 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
| 3423 | == (lhsRecord == rhsRecord)); |
| 3424 | |
| 3425 | if (lhsRecord == rhsRecord) |
| 3426 | return !lhsRecord->getDecl()->isUnion(); |
| 3427 | |
| 3428 | // C++0x [meta.rel]p2: |
| 3429 | // If Base and Derived are class types and are different types |
| 3430 | // (ignoring possible cv-qualifiers) then Derived shall be a |
| 3431 | // complete type. |
| 3432 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
| 3433 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 3434 | return false; |
| 3435 | |
| 3436 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) |
| 3437 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); |
| 3438 | } |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3439 | case BTT_IsSame: |
| 3440 | return Self.Context.hasSameType(LhsT, RhsT); |
Francois Pichet | 34b2113 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3441 | case BTT_TypeCompatible: |
| 3442 | return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(), |
| 3443 | RhsT.getUnqualifiedType()); |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3444 | case BTT_IsConvertible: |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3445 | case BTT_IsConvertibleTo: { |
| 3446 | // C++0x [meta.rel]p4: |
| 3447 | // Given the following function prototype: |
| 3448 | // |
| 3449 | // template <class T> |
| 3450 | // typename add_rvalue_reference<T>::type create(); |
| 3451 | // |
| 3452 | // the predicate condition for a template specialization |
| 3453 | // is_convertible<From, To> shall be satisfied if and only if |
| 3454 | // the return expression in the following code would be |
| 3455 | // well-formed, including any implicit conversions to the return |
| 3456 | // type of the function: |
| 3457 | // |
| 3458 | // To test() { |
| 3459 | // return create<From>(); |
| 3460 | // } |
| 3461 | // |
| 3462 | // Access checking is performed as if in a context unrelated to To and |
| 3463 | // From. Only the validity of the immediate context of the expression |
| 3464 | // of the return-statement (including conversions to the return type) |
| 3465 | // is considered. |
| 3466 | // |
| 3467 | // We model the initialization as a copy-initialization of a temporary |
| 3468 | // of the appropriate type, which for this expression is identical to the |
| 3469 | // return statement (since NRVO doesn't apply). |
| 3470 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 3471 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
| 3472 | |
| 3473 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3474 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3475 | Expr::getValueKindForType(LhsT)); |
| 3476 | Expr *FromPtr = &From; |
| 3477 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, |
| 3478 | SourceLocation())); |
| 3479 | |
Eli Friedman | a59b190 | 2012-01-25 01:05:57 +0000 | [diff] [blame] | 3480 | // Perform the initialization in an unevaluated context within a SFINAE |
| 3481 | // trap at translation unit scope. |
| 3482 | EnterExpressionEvaluationContext Unevaluated(Self, Sema::Unevaluated); |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3483 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 3484 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3485 | InitializationSequence Init(Self, To, Kind, &FromPtr, 1); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3486 | if (Init.Failed()) |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3487 | return false; |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3488 | |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3489 | ExprResult Result = Init.Perform(Self, To, Kind, MultiExprArg(&FromPtr, 1)); |
| 3490 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); |
| 3491 | } |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3492 | |
| 3493 | case BTT_IsTriviallyAssignable: { |
| 3494 | // C++11 [meta.unary.prop]p3: |
| 3495 | // is_trivially_assignable is defined as: |
| 3496 | // is_assignable<T, U>::value is true and the assignment, as defined by |
| 3497 | // is_assignable, is known to call no operation that is not trivial |
| 3498 | // |
| 3499 | // is_assignable is defined as: |
| 3500 | // The expression declval<T>() = declval<U>() is well-formed when |
| 3501 | // treated as an unevaluated operand (Clause 5). |
| 3502 | // |
| 3503 | // For both, T and U shall be complete types, (possibly cv-qualified) |
| 3504 | // void, or arrays of unknown bound. |
| 3505 | if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() && |
| 3506 | Self.RequireCompleteType(KeyLoc, LhsT, |
| 3507 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 3508 | return false; |
| 3509 | if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() && |
| 3510 | Self.RequireCompleteType(KeyLoc, RhsT, |
| 3511 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 3512 | return false; |
| 3513 | |
| 3514 | // cv void is never assignable. |
| 3515 | if (LhsT->isVoidType() || RhsT->isVoidType()) |
| 3516 | return false; |
| 3517 | |
| 3518 | // Build expressions that emulate the effect of declval<T>() and |
| 3519 | // declval<U>(). |
| 3520 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 3521 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
| 3522 | if (RhsT->isObjectType() || RhsT->isFunctionType()) |
| 3523 | RhsT = Self.Context.getRValueReferenceType(RhsT); |
| 3524 | OpaqueValueExpr Lhs(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
| 3525 | Expr::getValueKindForType(LhsT)); |
| 3526 | OpaqueValueExpr Rhs(KeyLoc, RhsT.getNonLValueExprType(Self.Context), |
| 3527 | Expr::getValueKindForType(RhsT)); |
| 3528 | |
| 3529 | // Attempt the assignment in an unevaluated context within a SFINAE |
| 3530 | // trap at translation unit scope. |
| 3531 | EnterExpressionEvaluationContext Unevaluated(Self, Sema::Unevaluated); |
| 3532 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 3533 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
| 3534 | ExprResult Result = Self.BuildBinOp(/*S=*/0, KeyLoc, BO_Assign, &Lhs, &Rhs); |
| 3535 | if (Result.isInvalid() || SFINAE.hasErrorOccurred()) |
| 3536 | return false; |
| 3537 | |
| 3538 | return !Result.get()->hasNonTrivialCall(Self.Context); |
| 3539 | } |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3540 | } |
| 3541 | llvm_unreachable("Unknown type trait or not implemented"); |
| 3542 | } |
| 3543 | |
| 3544 | ExprResult Sema::BuildBinaryTypeTrait(BinaryTypeTrait BTT, |
| 3545 | SourceLocation KWLoc, |
| 3546 | TypeSourceInfo *LhsTSInfo, |
| 3547 | TypeSourceInfo *RhsTSInfo, |
| 3548 | SourceLocation RParen) { |
| 3549 | QualType LhsT = LhsTSInfo->getType(); |
| 3550 | QualType RhsT = RhsTSInfo->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3551 | |
John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3552 | if (BTT == BTT_TypeCompatible) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3553 | if (getLangOpts().CPlusPlus) { |
Francois Pichet | 34b2113 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3554 | Diag(KWLoc, diag::err_types_compatible_p_in_cplusplus) |
| 3555 | << SourceRange(KWLoc, RParen); |
| 3556 | return ExprError(); |
| 3557 | } |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | bool Value = false; |
| 3561 | if (!LhsT->isDependentType() && !RhsT->isDependentType()) |
| 3562 | Value = EvaluateBinaryTypeTrait(*this, BTT, LhsT, RhsT, KWLoc); |
| 3563 | |
Francois Pichet | 34b2113 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3564 | // Select trait result type. |
| 3565 | QualType ResultType; |
| 3566 | switch (BTT) { |
Francois Pichet | 34b2113 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3567 | case BTT_IsBaseOf: ResultType = Context.BoolTy; break; |
John Wiegley | 65497cc | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3568 | case BTT_IsConvertible: ResultType = Context.BoolTy; break; |
| 3569 | case BTT_IsSame: ResultType = Context.BoolTy; break; |
Francois Pichet | 34b2113 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3570 | case BTT_TypeCompatible: ResultType = Context.IntTy; break; |
Douglas Gregor | 8006e76 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3571 | case BTT_IsConvertibleTo: ResultType = Context.BoolTy; break; |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3572 | case BTT_IsTriviallyAssignable: ResultType = Context.BoolTy; |
Francois Pichet | 34b2113 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3575 | return Owned(new (Context) BinaryTypeTraitExpr(KWLoc, BTT, LhsTSInfo, |
| 3576 | RhsTSInfo, Value, RParen, |
Francois Pichet | 34b2113 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3577 | ResultType)); |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3580 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
| 3581 | SourceLocation KWLoc, |
| 3582 | ParsedType Ty, |
| 3583 | Expr* DimExpr, |
| 3584 | SourceLocation RParen) { |
| 3585 | TypeSourceInfo *TSInfo; |
| 3586 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
| 3587 | if (!TSInfo) |
| 3588 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
| 3589 | |
| 3590 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
| 3591 | } |
| 3592 | |
| 3593 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
| 3594 | QualType T, Expr *DimExpr, |
| 3595 | SourceLocation KeyLoc) { |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3596 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3597 | |
| 3598 | switch(ATT) { |
| 3599 | case ATT_ArrayRank: |
| 3600 | if (T->isArrayType()) { |
| 3601 | unsigned Dim = 0; |
| 3602 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 3603 | ++Dim; |
| 3604 | T = AT->getElementType(); |
| 3605 | } |
| 3606 | return Dim; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3607 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3608 | return 0; |
| 3609 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3610 | case ATT_ArrayExtent: { |
| 3611 | llvm::APSInt Value; |
| 3612 | uint64_t Dim; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3613 | if (Self.VerifyIntegerConstantExpression(DimExpr, &Value, |
| 3614 | Self.PDiag(diag::err_dimension_expr_not_constant_integer), |
| 3615 | false).isInvalid()) |
| 3616 | return 0; |
| 3617 | if (Value.isSigned() && Value.isNegative()) { |
Daniel Dunbar | 900cead | 2012-03-09 21:38:22 +0000 | [diff] [blame] | 3618 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) |
| 3619 | << DimExpr->getSourceRange(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3620 | return 0; |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3621 | } |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3622 | Dim = Value.getLimitedValue(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3623 | |
| 3624 | if (T->isArrayType()) { |
| 3625 | unsigned D = 0; |
| 3626 | bool Matched = false; |
| 3627 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 3628 | if (Dim == D) { |
| 3629 | Matched = true; |
| 3630 | break; |
| 3631 | } |
| 3632 | ++D; |
| 3633 | T = AT->getElementType(); |
| 3634 | } |
| 3635 | |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3636 | if (Matched && T->isArrayType()) { |
| 3637 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
| 3638 | return CAT->getSize().getLimitedValue(); |
| 3639 | } |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3640 | } |
John Wiegley | d352222 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3641 | return 0; |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3642 | } |
| 3643 | } |
| 3644 | llvm_unreachable("Unknown type trait or not implemented"); |
| 3645 | } |
| 3646 | |
| 3647 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
| 3648 | SourceLocation KWLoc, |
| 3649 | TypeSourceInfo *TSInfo, |
| 3650 | Expr* DimExpr, |
| 3651 | SourceLocation RParen) { |
| 3652 | QualType T = TSInfo->getType(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3653 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3654 | // FIXME: This should likely be tracked as an APInt to remove any host |
| 3655 | // assumptions about the width of size_t on the target. |
Chandler Carruth | 0d1a54f | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3656 | uint64_t Value = 0; |
| 3657 | if (!T->isDependentType()) |
| 3658 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); |
| 3659 | |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3660 | // While the specification for these traits from the Embarcadero C++ |
| 3661 | // compiler's documentation says the return type is 'unsigned int', Clang |
| 3662 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero |
| 3663 | // compiler, there is no difference. On several other platforms this is an |
| 3664 | // important distinction. |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3665 | return Owned(new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, |
Chandler Carruth | 9cf632c | 2011-05-01 07:49:26 +0000 | [diff] [blame] | 3666 | DimExpr, RParen, |
Chandler Carruth | c5276e5 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3667 | Context.getSizeType())); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3670 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3671 | SourceLocation KWLoc, |
| 3672 | Expr *Queried, |
| 3673 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3674 | // If error parsing the expression, ignore. |
| 3675 | if (!Queried) |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3676 | return ExprError(); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3677 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3678 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3679 | |
| 3680 | return move(Result); |
| 3681 | } |
| 3682 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3683 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
| 3684 | switch (ET) { |
| 3685 | case ET_IsLValueExpr: return E->isLValue(); |
| 3686 | case ET_IsRValueExpr: return E->isRValue(); |
| 3687 | } |
| 3688 | llvm_unreachable("Expression trait not covered by switch"); |
| 3689 | } |
| 3690 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3691 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3692 | SourceLocation KWLoc, |
| 3693 | Expr *Queried, |
| 3694 | SourceLocation RParen) { |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3695 | if (Queried->isTypeDependent()) { |
| 3696 | // Delay type-checking for type-dependent expressions. |
| 3697 | } else if (Queried->getType()->isPlaceholderType()) { |
| 3698 | ExprResult PE = CheckPlaceholderExpr(Queried); |
| 3699 | if (PE.isInvalid()) return ExprError(); |
| 3700 | return BuildExpressionTrait(ET, KWLoc, PE.take(), RParen); |
| 3701 | } |
| 3702 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3703 | bool Value = EvaluateExpressionTrait(ET, Queried); |
Chandler Carruth | f57eba3 | 2011-05-01 08:48:19 +0000 | [diff] [blame] | 3704 | |
Chandler Carruth | 20b9bc8 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3705 | return Owned(new (Context) ExpressionTraitExpr(KWLoc, ET, Queried, Value, |
| 3706 | RParen, Context.BoolTy)); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3709 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3710 | ExprValueKind &VK, |
| 3711 | SourceLocation Loc, |
| 3712 | bool isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3713 | assert(!LHS.get()->getType()->isPlaceholderType() && |
| 3714 | !RHS.get()->getType()->isPlaceholderType() && |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3715 | "placeholders should have been weeded out by now"); |
| 3716 | |
| 3717 | // The LHS undergoes lvalue conversions if this is ->*. |
| 3718 | if (isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3719 | LHS = DefaultLvalueConversion(LHS.take()); |
| 3720 | if (LHS.isInvalid()) return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | // The RHS always undergoes lvalue conversions. |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3724 | RHS = DefaultLvalueConversion(RHS.take()); |
| 3725 | if (RHS.isInvalid()) return QualType(); |
John McCall | 0b645e9 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3726 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3727 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 3728 | // C++ 5.5p2 |
| 3729 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 3730 | // be of type "pointer to member of T" (where T is a completely-defined |
| 3731 | // class type) [...] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3732 | QualType RHSType = RHS.get()->getType(); |
| 3733 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3734 | if (!MemPtr) { |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3735 | Diag(Loc, diag::err_bad_memptr_rhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3736 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3737 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3739 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3740 | QualType Class(MemPtr->getClass(), 0); |
| 3741 | |
Douglas Gregor | d07ba34 | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 3742 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
| 3743 | // member pointer points must be completely-defined. However, there is no |
| 3744 | // reason for this semantic distinction, and the rule is not enforced by |
| 3745 | // other compilers. Therefore, we do not check this property, as it is |
| 3746 | // likely to be considered a defect. |
Sebastian Redl | c72350e | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 3747 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3748 | // C++ 5.5p2 |
| 3749 | // [...] to its first operand, which shall be of class T or of a class of |
| 3750 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 3751 | // such a class] |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3752 | QualType LHSType = LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3753 | if (isIndirect) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3754 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) |
| 3755 | LHSType = Ptr->getPointeeType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3756 | else { |
| 3757 | Diag(Loc, diag::err_bad_memptr_lhs) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3758 | << OpSpelling << 1 << LHSType |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3759 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3760 | return QualType(); |
| 3761 | } |
| 3762 | } |
| 3763 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3764 | if (!Context.hasSameUnqualifiedType(Class, LHSType)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 3765 | // If we want to check the hierarchy, we need a complete type. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 3766 | if (RequireCompleteType(Loc, LHSType, diag::err_bad_memptr_lhs, |
| 3767 | OpSpelling, (int)isIndirect)) { |
Sebastian Redl | 26a0f1c | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 3768 | return QualType(); |
| 3769 | } |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3770 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 3771 | /*DetectVirtual=*/false); |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3772 | // FIXME: Would it be useful to print full ambiguity paths, or is that |
| 3773 | // overkill? |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3774 | if (!IsDerivedFrom(LHSType, Class, Paths) || |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3775 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
| 3776 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3777 | << (int)isIndirect << LHS.get()->getType(); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3778 | return QualType(); |
| 3779 | } |
Eli Friedman | 1fcf66b | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 3780 | // Cast LHS to type of use. |
| 3781 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 3782 | ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind(); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3783 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3784 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3785 | BuildBasePathArray(Paths, BasePath); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3786 | LHS = ImpCastExprToType(LHS.take(), UseType, CK_DerivedToBase, VK, |
| 3787 | &BasePath); |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3790 | if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) { |
Fariborz Jahanian | 1bc0f9a | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 3791 | // Diagnose use of pointer-to-member type which when used as |
| 3792 | // the functional cast in a pointer-to-member expression. |
| 3793 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 3794 | return QualType(); |
| 3795 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3796 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3797 | // C++ 5.5p2 |
| 3798 | // The result is an object or a function of the type specified by the |
| 3799 | // second operand. |
| 3800 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 3801 | // in accordance with 5.5p5 and 5.2.5. |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3802 | QualType Result = MemPtr->getPointeeType(); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3803 | Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3804 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3805 | // C++0x [expr.mptr.oper]p6: |
| 3806 | // In a .* expression whose object expression is an rvalue, the program is |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3807 | // ill-formed if the second operand is a pointer to member function with |
| 3808 | // ref-qualifier &. In a ->* expression or in a .* expression whose object |
| 3809 | // 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] | 3810 | // is a pointer to member function with ref-qualifier &&. |
| 3811 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
| 3812 | switch (Proto->getRefQualifier()) { |
| 3813 | case RQ_None: |
| 3814 | // Do nothing |
| 3815 | break; |
| 3816 | |
| 3817 | case RQ_LValue: |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3818 | if (!isIndirect && !LHS.get()->Classify(Context).isLValue()) |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3819 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3820 | << RHSType << 1 << LHS.get()->getSourceRange(); |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3821 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3822 | |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3823 | case RQ_RValue: |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3824 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3825 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3826 | << RHSType << 0 << LHS.get()->getSourceRange(); |
Douglas Gregor | 1d04209 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3827 | break; |
| 3828 | } |
| 3829 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3830 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3831 | // C++ [expr.mptr.oper]p6: |
| 3832 | // The result of a .* expression whose second operand is a pointer |
| 3833 | // to a data member is of the same value category as its |
| 3834 | // first operand. The result of a .* expression whose second |
| 3835 | // operand is a pointer to a member function is a prvalue. The |
| 3836 | // result of an ->* expression is an lvalue if its second operand |
| 3837 | // is a pointer to data member and a prvalue otherwise. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3838 | if (Result->isFunctionType()) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3839 | VK = VK_RValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3840 | return Context.BoundMemberTy; |
| 3841 | } else if (isIndirect) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3842 | VK = VK_LValue; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3843 | } else { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3844 | VK = LHS.get()->getValueKind(); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3845 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3846 | |
Sebastian Redl | 5822f08 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3847 | return Result; |
| 3848 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3849 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3850 | /// \brief Try to convert a type to another according to C++0x 5.16p3. |
| 3851 | /// |
| 3852 | /// This is part of the parameter validation for the ? operator. If either |
| 3853 | /// value operand is a class type, the two operands are attempted to be |
| 3854 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3855 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 3856 | /// as such. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3857 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 3858 | SourceLocation QuestionLoc, |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3859 | bool &HaveConversion, |
| 3860 | QualType &ToType) { |
| 3861 | HaveConversion = false; |
| 3862 | ToType = To->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3863 | |
| 3864 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3865 | SourceLocation()); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3866 | // C++0x 5.16p3 |
| 3867 | // The process for determining whether an operand expression E1 of type T1 |
| 3868 | // can be converted to match an operand expression E2 of type T2 is defined |
| 3869 | // as follows: |
| 3870 | // -- If E2 is an lvalue: |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 3871 | bool ToIsLvalue = To->isLValue(); |
Douglas Gregor | f9edf80 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 3872 | if (ToIsLvalue) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3873 | // E1 can be converted to match E2 if E1 can be implicitly converted to |
| 3874 | // type "lvalue reference to T2", subject to the constraint that in the |
| 3875 | // conversion the reference must bind directly to E1. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3876 | QualType T = Self.Context.getLValueReferenceType(ToType); |
| 3877 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3878 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3879 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 3880 | if (InitSeq.isDirectReferenceBinding()) { |
| 3881 | ToType = T; |
| 3882 | HaveConversion = true; |
| 3883 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3884 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3885 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3886 | if (InitSeq.isAmbiguous()) |
| 3887 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3888 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3889 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3890 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 3891 | // -- if E1 and E2 have class type, and the underlying class types are |
| 3892 | // the same or one is a base class of the other: |
| 3893 | QualType FTy = From->getType(); |
| 3894 | QualType TTy = To->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3895 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 3896 | const RecordType *TRec = TTy->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3897 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3898 | Self.IsDerivedFrom(FTy, TTy); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3899 | if (FRec && TRec && |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3900 | (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3901 | // E1 can be converted to match E2 if the class of T2 is the |
| 3902 | // same type as, or a base class of, the class of T1, and |
| 3903 | // [cv2 > cv1]. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3904 | if (FRec == TRec || FDerivedFromT) { |
| 3905 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3906 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 3907 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3908 | if (InitSeq) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3909 | HaveConversion = true; |
| 3910 | return false; |
| 3911 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3912 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3913 | if (InitSeq.isAmbiguous()) |
| 3914 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3915 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3916 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3917 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3918 | return false; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3919 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3920 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3921 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 3922 | // implicitly converted to the type that expression E2 would have |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3923 | // 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] | 3924 | // an rvalue). |
| 3925 | // |
| 3926 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 3927 | // to the array-to-pointer or function-to-pointer conversions. |
| 3928 | if (!TTy->getAs<TagType>()) |
| 3929 | TTy = TTy.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3931 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 3932 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3933 | HaveConversion = !InitSeq.Failed(); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3934 | ToType = TTy; |
| 3935 | if (InitSeq.isAmbiguous()) |
| 3936 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
| 3937 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3938 | return false; |
| 3939 | } |
| 3940 | |
| 3941 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 3942 | /// |
| 3943 | /// This is part of the parameter validation for the ? operator. If either |
| 3944 | /// value operand is a class type, overload resolution is used to find a |
| 3945 | /// conversion to a common type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3946 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3947 | SourceLocation QuestionLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3948 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3949 | OverloadCandidateSet CandidateSet(QuestionLoc); |
| 3950 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, 2, |
| 3951 | CandidateSet); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3952 | |
| 3953 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3954 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3955 | case OR_Success: { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3956 | // We found a match. Perform the conversions on the arguments and move on. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3957 | ExprResult LHSRes = |
| 3958 | Self.PerformImplicitConversion(LHS.get(), Best->BuiltinTypes.ParamTypes[0], |
| 3959 | Best->Conversions[0], Sema::AA_Converting); |
| 3960 | if (LHSRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3961 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3962 | LHS = move(LHSRes); |
| 3963 | |
| 3964 | ExprResult RHSRes = |
| 3965 | Self.PerformImplicitConversion(RHS.get(), Best->BuiltinTypes.ParamTypes[1], |
| 3966 | Best->Conversions[1], Sema::AA_Converting); |
| 3967 | if (RHSRes.isInvalid()) |
| 3968 | break; |
| 3969 | RHS = move(RHSRes); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3970 | if (Best->Function) |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3971 | Self.MarkFunctionReferenced(QuestionLoc, Best->Function); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3972 | return false; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3975 | case OR_No_Viable_Function: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3976 | |
| 3977 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 3978 | // constant and the other is a pointer type. In this case, the user most |
| 3979 | // likely forgot to take the address of the other expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3980 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3981 | return true; |
| 3982 | |
| 3983 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3984 | << LHS.get()->getType() << RHS.get()->getType() |
| 3985 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3986 | return true; |
| 3987 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3988 | case OR_Ambiguous: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3989 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3990 | << LHS.get()->getType() << RHS.get()->getType() |
| 3991 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3992 | // FIXME: Print the possible common types by printing the return types of |
| 3993 | // the viable candidates. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3994 | break; |
| 3995 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3996 | case OR_Deleted: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3997 | llvm_unreachable("Conditional operator has only built-in overloads"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3998 | } |
| 3999 | return true; |
| 4000 | } |
| 4001 | |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 4002 | /// \brief Perform an "extended" implicit conversion as returned by |
| 4003 | /// TryClassUnification. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4004 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 4005 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4006 | InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(), |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 4007 | SourceLocation()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4008 | Expr *Arg = E.take(); |
| 4009 | InitializationSequence InitSeq(Self, Entity, Kind, &Arg, 1); |
| 4010 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, MultiExprArg(&Arg, 1)); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 4011 | if (Result.isInvalid()) |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 4012 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4013 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4014 | E = Result; |
Sebastian Redl | 5775af1a | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 4015 | return false; |
| 4016 | } |
| 4017 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4018 | /// \brief Check the operands of ?: under C++ semantics. |
| 4019 | /// |
| 4020 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 4021 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4022 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4023 | ExprValueKind &VK, ExprObjectKind &OK, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4024 | SourceLocation QuestionLoc) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4025 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 4026 | // interface pointers. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4027 | |
| 4028 | // C++0x 5.16p1 |
| 4029 | // The first expression is contextually converted to bool. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4030 | if (!Cond.get()->isTypeDependent()) { |
| 4031 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.take()); |
| 4032 | if (CondRes.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4033 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4034 | Cond = move(CondRes); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4037 | // Assume r-value. |
| 4038 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4039 | OK = OK_Ordinary; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4040 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4041 | // Either of the arguments dependent? |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4042 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4043 | return Context.DependentTy; |
| 4044 | |
| 4045 | // C++0x 5.16p2 |
| 4046 | // If either the second or the third operand has type (cv) void, ... |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4047 | QualType LTy = LHS.get()->getType(); |
| 4048 | QualType RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4049 | bool LVoid = LTy->isVoidType(); |
| 4050 | bool RVoid = RTy->isVoidType(); |
| 4051 | if (LVoid || RVoid) { |
| 4052 | // ... then the [l2r] conversions are performed on the second and third |
| 4053 | // operands ... |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4054 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 4055 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 4056 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4057 | return QualType(); |
| 4058 | LTy = LHS.get()->getType(); |
| 4059 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4060 | |
| 4061 | // ... and one of the following shall hold: |
| 4062 | // -- The second or the third operand (but not both) is a throw- |
| 4063 | // expression; the result is of the type of the other and is an rvalue. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4064 | bool LThrow = isa<CXXThrowExpr>(LHS.get()); |
| 4065 | bool RThrow = isa<CXXThrowExpr>(RHS.get()); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4066 | if (LThrow && !RThrow) |
| 4067 | return RTy; |
| 4068 | if (RThrow && !LThrow) |
| 4069 | return LTy; |
| 4070 | |
| 4071 | // -- Both the second and third operands have type void; the result is of |
| 4072 | // type void and is an rvalue. |
| 4073 | if (LVoid && RVoid) |
| 4074 | return Context.VoidTy; |
| 4075 | |
| 4076 | // Neither holds, error. |
| 4077 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 4078 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4079 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4080 | return QualType(); |
| 4081 | } |
| 4082 | |
| 4083 | // Neither is void. |
| 4084 | |
| 4085 | // C++0x 5.16p3 |
| 4086 | // Otherwise, if the second and third operand have different types, and |
| 4087 | // either has (cv) class type, and attempt is made to convert each of those |
| 4088 | // operands to the other. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4089 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4090 | (LTy->isRecordType() || RTy->isRecordType())) { |
| 4091 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; |
| 4092 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 4093 | QualType L2RType, R2LType; |
| 4094 | bool HaveL2R, HaveR2L; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4095 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4096 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4097 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4098 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4099 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4100 | // If both can be converted, [...] the program is ill-formed. |
| 4101 | if (HaveL2R && HaveR2L) { |
| 4102 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4103 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4104 | return QualType(); |
| 4105 | } |
| 4106 | |
| 4107 | // If exactly one conversion is possible, that conversion is applied to |
| 4108 | // the chosen operand and the converted operands are used in place of the |
| 4109 | // original operands for the remainder of this section. |
| 4110 | if (HaveL2R) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4111 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4112 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4113 | LTy = LHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4114 | } else if (HaveR2L) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4115 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4116 | return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4117 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4118 | } |
| 4119 | } |
| 4120 | |
| 4121 | // C++0x 5.16p4 |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4122 | // If the second and third operands are glvalues of the same value |
| 4123 | // category and have the same type, the result is of that type and |
| 4124 | // value category and it is a bit-field if the second or the third |
| 4125 | // operand is a bit-field, or if both are bit-fields. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4126 | // We only extend this to bitfields, not to the crazy other kinds of |
| 4127 | // l-values. |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 4128 | bool Same = Context.hasSameType(LTy, RTy); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4129 | if (Same && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4130 | LHS.get()->isGLValue() && |
| 4131 | LHS.get()->getValueKind() == RHS.get()->getValueKind() && |
| 4132 | LHS.get()->isOrdinaryOrBitFieldObject() && |
| 4133 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
| 4134 | VK = LHS.get()->getValueKind(); |
| 4135 | if (LHS.get()->getObjectKind() == OK_BitField || |
| 4136 | RHS.get()->getObjectKind() == OK_BitField) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4137 | OK = OK_BitField; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4138 | return LTy; |
Fariborz Jahanian | c60da03 | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 4139 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4140 | |
| 4141 | // C++0x 5.16p5 |
| 4142 | // Otherwise, the result is an rvalue. If the second and third operands |
| 4143 | // do not have the same type, and either has (cv) class type, ... |
| 4144 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 4145 | // ... overload resolution is used to determine the conversions (if any) |
| 4146 | // to be applied to the operands. If the overload resolution fails, the |
| 4147 | // program is ill-formed. |
| 4148 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 4149 | return QualType(); |
| 4150 | } |
| 4151 | |
| 4152 | // C++0x 5.16p6 |
| 4153 | // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 4154 | // conversions are performed on the second and third operands. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4155 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 4156 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 4157 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4158 | return QualType(); |
| 4159 | LTy = LHS.get()->getType(); |
| 4160 | RTy = RHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4161 | |
| 4162 | // After those conversions, one of the following shall hold: |
| 4163 | // -- The second and third operands have the same type; the result |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 4164 | // is of that type. If the operands have class type, the result |
| 4165 | // is a prvalue temporary of the result type, which is |
| 4166 | // copy-initialized from either the second operand or the third |
| 4167 | // operand depending on the value of the first operand. |
| 4168 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 4169 | if (LTy->isRecordType()) { |
| 4170 | // The operands have class type. Make a temporary copy. |
| 4171 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4172 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
| 4173 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4174 | LHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 4175 | if (LHSCopy.isInvalid()) |
| 4176 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4177 | |
| 4178 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
| 4179 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4180 | RHS); |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 4181 | if (RHSCopy.isInvalid()) |
| 4182 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4183 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4184 | LHS = LHSCopy; |
| 4185 | RHS = RHSCopy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4188 | return LTy; |
Douglas Gregor | fa6010b | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 4189 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4190 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4191 | // Extension: conditional operator involving vector types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4192 | if (LTy->isVectorType() || RTy->isVectorType()) |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4193 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 4194 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4195 | // -- The second and third operands have arithmetic or enumeration type; |
| 4196 | // the usual arithmetic conversions are performed to bring them to a |
| 4197 | // common type, and the result is of that type. |
| 4198 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
| 4199 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4200 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4201 | return QualType(); |
| 4202 | return LHS.get()->getType(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | // -- The second and third operands have pointer type, or one has pointer |
| 4206 | // type and the other is a null pointer constant; pointer conversions |
| 4207 | // and qualification conversions are performed to bring them to their |
| 4208 | // composite pointer type. The result is of the composite pointer type. |
Eli Friedman | 81390df | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 4209 | // -- The second and third operands have pointer to member type, or one has |
| 4210 | // pointer to member type and the other is a null pointer constant; |
| 4211 | // pointer to member conversions and qualification conversions are |
| 4212 | // performed to bring them to a common type, whose cv-qualification |
| 4213 | // shall match the cv-qualification of either the second or the third |
| 4214 | // operand. The result is of the common type. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4215 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4216 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4217 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
| 4218 | if (!Composite.isNull()) { |
| 4219 | if (NonStandardCompositeType) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4220 | Diag(QuestionLoc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4221 | diag::ext_typecheck_cond_incompatible_operands_nonstandard) |
| 4222 | << LTy << RTy << Composite |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4223 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4224 | |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4225 | return Composite; |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4226 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4227 | |
Douglas Gregor | 697a391 | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 4228 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 798d2bd | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 4229 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 4230 | if (!Composite.isNull()) |
| 4231 | return Composite; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4232 | |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 4233 | // Check if we are using a null with a non-pointer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4234 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 9c9127e | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 4235 | return QualType(); |
| 4236 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4237 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4238 | << LHS.get()->getType() << RHS.get()->getType() |
| 4239 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4240 | return QualType(); |
| 4241 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4242 | |
| 4243 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 4244 | /// |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4245 | /// This finds the composite pointer type (or member pointer type) for @p E1 |
| 4246 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this |
| 4247 | /// type and returns it. |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4248 | /// It does not emit diagnostics. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4249 | /// |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4250 | /// \param Loc The location of the operator requiring these two expressions to |
| 4251 | /// be converted to the composite pointer type. |
| 4252 | /// |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4253 | /// If \p NonStandardCompositeType is non-NULL, then we are permitted to find |
| 4254 | /// a non-standard (but still sane) composite type to which both expressions |
| 4255 | /// can be converted. When such a type is chosen, \c *NonStandardCompositeType |
| 4256 | /// will be set true. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4257 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4258 | Expr *&E1, Expr *&E2, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4259 | bool *NonStandardCompositeType) { |
| 4260 | if (NonStandardCompositeType) |
| 4261 | *NonStandardCompositeType = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4263 | assert(getLangOpts().CPlusPlus && "This function assumes C++"); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4264 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | |
Fariborz Jahanian | 33e148f | 2009-12-08 20:04:24 +0000 | [diff] [blame] | 4266 | if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && |
| 4267 | !T2->isAnyPointerType() && !T2->isMemberPointerType()) |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4268 | return QualType(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4269 | |
| 4270 | // C++0x 5.9p2 |
| 4271 | // Pointer conversions and qualification conversions are performed on |
| 4272 | // pointer operands to bring them to their composite pointer type. If |
| 4273 | // one operand is a null pointer constant, the composite pointer type is |
| 4274 | // the type of the other operand. |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4275 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4276 | if (T2->isMemberPointerType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4277 | E1 = ImpCastExprToType(E1, T2, CK_NullToMemberPointer).take(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4278 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4279 | E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).take(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4280 | return T2; |
| 4281 | } |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4282 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4283 | if (T1->isMemberPointerType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4284 | E2 = ImpCastExprToType(E2, T1, CK_NullToMemberPointer).take(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4285 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4286 | E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).take(); |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4287 | return T1; |
| 4288 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4289 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4290 | // Now both have to be pointers or member pointers. |
Sebastian Redl | 658262f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 4291 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || |
| 4292 | (!T2->isPointerType() && !T2->isMemberPointerType())) |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4293 | return QualType(); |
| 4294 | |
| 4295 | // Otherwise, of one of the operands has type "pointer to cv1 void," then |
| 4296 | // the other has type "pointer to cv2 T" and the composite pointer type is |
| 4297 | // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. |
| 4298 | // Otherwise, the composite pointer type is a pointer type similar to the |
| 4299 | // type of one of the operands, with a cv-qualification signature that is |
| 4300 | // the union of the cv-qualification signatures of the operand types. |
| 4301 | // In practice, the first part here is redundant; it's subsumed by the second. |
| 4302 | // What we do here is, we build the two possible composite types, and try the |
| 4303 | // conversions in both directions. If only one works, or if the two composite |
| 4304 | // types are the same, we have succeeded. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4305 | // FIXME: extended qualifiers? |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4306 | typedef SmallVector<unsigned, 4> QualifierVector; |
Sebastian Redl | 658262f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 4307 | QualifierVector QualifierUnion; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4308 | typedef SmallVector<std::pair<const Type *, const Type *>, 4> |
Sebastian Redl | 658262f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 4309 | ContainingClassVector; |
| 4310 | ContainingClassVector MemberOfClass; |
| 4311 | QualType Composite1 = Context.getCanonicalType(T1), |
| 4312 | Composite2 = Context.getCanonicalType(T2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4313 | unsigned NeedConstBefore = 0; |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4314 | do { |
| 4315 | const PointerType *Ptr1, *Ptr2; |
| 4316 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 4317 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 4318 | Composite1 = Ptr1->getPointeeType(); |
| 4319 | Composite2 = Ptr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4320 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4321 | // 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] | 4322 | // of where we need to fill in additional 'const' qualifiers. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4323 | if (NonStandardCompositeType && |
| 4324 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 4325 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4326 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4327 | QualifierUnion.push_back( |
| 4328 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 4329 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); |
| 4330 | continue; |
| 4331 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4332 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4333 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 4334 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 4335 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 4336 | Composite1 = MemPtr1->getPointeeType(); |
| 4337 | Composite2 = MemPtr2->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4338 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4339 | // 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] | 4340 | // of where we need to fill in additional 'const' qualifiers. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4341 | if (NonStandardCompositeType && |
| 4342 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 4343 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4345 | QualifierUnion.push_back( |
| 4346 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 4347 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 4348 | MemPtr2->getClass())); |
| 4349 | continue; |
| 4350 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4351 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4352 | // FIXME: block pointer types? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4353 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4354 | // Cannot unwrap any more types. |
| 4355 | break; |
| 4356 | } while (true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4357 | |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4358 | if (NeedConstBefore && NonStandardCompositeType) { |
| 4359 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4360 | // mismatch, so that our (non-standard!) composite type meets the |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4361 | // requirements of C++ [conv.qual]p4 bullet 3. |
| 4362 | for (unsigned I = 0; I != NeedConstBefore; ++I) { |
| 4363 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) { |
| 4364 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
| 4365 | *NonStandardCompositeType = true; |
| 4366 | } |
| 4367 | } |
| 4368 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4369 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4370 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Sebastian Redl | 658262f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 4371 | ContainingClassVector::reverse_iterator MOC |
| 4372 | = MemberOfClass.rbegin(); |
| 4373 | for (QualifierVector::reverse_iterator |
| 4374 | I = QualifierUnion.rbegin(), |
| 4375 | E = QualifierUnion.rend(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4376 | I != E; (void)++I, ++MOC) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4377 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4378 | if (MOC->first && MOC->second) { |
| 4379 | // Rebuild member pointer type |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4380 | Composite1 = Context.getMemberPointerType( |
| 4381 | Context.getQualifiedType(Composite1, Quals), |
| 4382 | MOC->first); |
| 4383 | Composite2 = Context.getMemberPointerType( |
| 4384 | Context.getQualifiedType(Composite2, Quals), |
| 4385 | MOC->second); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4386 | } else { |
| 4387 | // Rebuild pointer type |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4388 | Composite1 |
| 4389 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 4390 | Composite2 |
| 4391 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4392 | } |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4395 | // Try to convert to the first composite pointer type. |
| 4396 | InitializedEntity Entity1 |
| 4397 | = InitializedEntity::InitializeTemporary(Composite1); |
| 4398 | InitializationKind Kind |
| 4399 | = InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 4400 | InitializationSequence E1ToC1(*this, Entity1, Kind, &E1, 1); |
| 4401 | InitializationSequence E2ToC1(*this, Entity1, Kind, &E2, 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4402 | |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4403 | if (E1ToC1 && E2ToC1) { |
| 4404 | // Conversion to Composite1 is viable. |
| 4405 | if (!Context.hasSameType(Composite1, Composite2)) { |
| 4406 | // Composite2 is a different type from Composite1. Check whether |
| 4407 | // Composite2 is also viable. |
| 4408 | InitializedEntity Entity2 |
| 4409 | = InitializedEntity::InitializeTemporary(Composite2); |
| 4410 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 4411 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 4412 | if (E1ToC2 && E2ToC2) { |
| 4413 | // Both Composite1 and Composite2 are viable and are different; |
| 4414 | // this is an ambiguity. |
| 4415 | return QualType(); |
| 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | // Convert E1 to Composite1 |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4420 | ExprResult E1Result |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4421 | = E1ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E1,1)); |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4422 | if (E1Result.isInvalid()) |
| 4423 | return QualType(); |
| 4424 | E1 = E1Result.takeAs<Expr>(); |
| 4425 | |
| 4426 | // Convert E2 to Composite1 |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4427 | ExprResult E2Result |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4428 | = E2ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E2,1)); |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4429 | if (E2Result.isInvalid()) |
| 4430 | return QualType(); |
| 4431 | E2 = E2Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4432 | |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4433 | return Composite1; |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4436 | // Check whether Composite2 is viable. |
| 4437 | InitializedEntity Entity2 |
| 4438 | = InitializedEntity::InitializeTemporary(Composite2); |
| 4439 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 4440 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 4441 | if (!E1ToC2 || !E2ToC2) |
| 4442 | return QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4444 | // Convert E1 to Composite2 |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4445 | ExprResult E1Result |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4446 | = E1ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E1, 1)); |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4447 | if (E1Result.isInvalid()) |
| 4448 | return QualType(); |
| 4449 | E1 = E1Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4450 | |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4451 | // Convert E2 to Composite2 |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4452 | ExprResult E2Result |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4453 | = E2ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E2, 1)); |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4454 | if (E2Result.isInvalid()) |
| 4455 | return QualType(); |
| 4456 | E2 = E2Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4457 | |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4458 | return Composite2; |
Sebastian Redl | 3b7ef5e | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4459 | } |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 4460 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4461 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Douglas Gregor | 298087b | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 4462 | if (!E) |
| 4463 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4464 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4465 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 4466 | |
| 4467 | // If the result is a glvalue, we shouldn't bind it. |
| 4468 | if (!E->isRValue()) |
Anders Carlsson | f86a8d1 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 4469 | return Owned(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4470 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4471 | // In ARC, calls that return a retainable type can return retained, |
| 4472 | // in which case we have to insert a consuming cast. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4473 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4474 | E->getType()->isObjCRetainableType()) { |
| 4475 | |
| 4476 | bool ReturnsRetained; |
| 4477 | |
| 4478 | // For actual calls, we compute this by examining the type of the |
| 4479 | // called value. |
| 4480 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { |
| 4481 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
| 4482 | QualType T = Callee->getType(); |
| 4483 | |
| 4484 | if (T == Context.BoundMemberTy) { |
| 4485 | // Handle pointer-to-members. |
| 4486 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) |
| 4487 | T = BinOp->getRHS()->getType(); |
| 4488 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) |
| 4489 | T = Mem->getMemberDecl()->getType(); |
| 4490 | } |
| 4491 | |
| 4492 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
| 4493 | T = Ptr->getPointeeType(); |
| 4494 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
| 4495 | T = Ptr->getPointeeType(); |
| 4496 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
| 4497 | T = MemPtr->getPointeeType(); |
| 4498 | |
| 4499 | const FunctionType *FTy = T->getAs<FunctionType>(); |
| 4500 | assert(FTy && "call to value not of function type?"); |
| 4501 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
| 4502 | |
| 4503 | // ActOnStmtExpr arranges things so that StmtExprs of retainable |
| 4504 | // type always produce a +1 object. |
| 4505 | } else if (isa<StmtExpr>(E)) { |
| 4506 | ReturnsRetained = true; |
| 4507 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4508 | // We hit this case with the lambda conversion-to-block optimization; |
| 4509 | // we don't want any extra casts here. |
| 4510 | } else if (isa<CastExpr>(E) && |
| 4511 | isa<BlockExpr>(cast<CastExpr>(E)->getSubExpr())) { |
| 4512 | return Owned(E); |
| 4513 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4514 | // For message sends and property references, we try to find an |
| 4515 | // actual method. FIXME: we should infer retention by selector in |
| 4516 | // cases where we don't have an actual method. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4517 | } else { |
| 4518 | ObjCMethodDecl *D = 0; |
| 4519 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { |
| 4520 | D = Send->getMethodDecl(); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 4521 | } else if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(E)) { |
| 4522 | D = BoxedExpr->getBoxingMethod(); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4523 | } else if (ObjCArrayLiteral *ArrayLit = dyn_cast<ObjCArrayLiteral>(E)) { |
| 4524 | D = ArrayLit->getArrayWithObjectsMethod(); |
| 4525 | } else if (ObjCDictionaryLiteral *DictLit |
| 4526 | = dyn_cast<ObjCDictionaryLiteral>(E)) { |
| 4527 | D = DictLit->getDictWithObjectsMethod(); |
| 4528 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4529 | |
| 4530 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
John McCall | 32a4da0 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 4531 | |
| 4532 | // Don't do reclaims on performSelector calls; despite their |
| 4533 | // return type, the invoked method doesn't necessarily actually |
| 4534 | // return an object. |
| 4535 | if (!ReturnsRetained && |
| 4536 | D && D->getMethodFamily() == OMF_performSelector) |
| 4537 | return Owned(E); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
John McCall | 16de4d2 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 4540 | // Don't reclaim an object of Class type. |
| 4541 | if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType()) |
| 4542 | return Owned(E); |
| 4543 | |
John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4544 | ExprNeedsCleanups = true; |
| 4545 | |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4546 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject |
| 4547 | : CK_ARCReclaimReturnedObject); |
John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4548 | return Owned(ImplicitCastExpr::Create(Context, E->getType(), ck, E, 0, |
| 4549 | VK_RValue)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4552 | if (!getLangOpts().CPlusPlus) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4553 | return Owned(E); |
Douglas Gregor | 363b151 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 4554 | |
Peter Collingbourne | c331a1e | 2012-01-26 03:33:51 +0000 | [diff] [blame] | 4555 | // Search for the base element type (cf. ASTContext::getBaseElementType) with |
| 4556 | // a fast path for the common case that the type is directly a RecordType. |
| 4557 | const Type *T = Context.getCanonicalType(E->getType().getTypePtr()); |
| 4558 | const RecordType *RT = 0; |
| 4559 | while (!RT) { |
| 4560 | switch (T->getTypeClass()) { |
| 4561 | case Type::Record: |
| 4562 | RT = cast<RecordType>(T); |
| 4563 | break; |
| 4564 | case Type::ConstantArray: |
| 4565 | case Type::IncompleteArray: |
| 4566 | case Type::VariableArray: |
| 4567 | case Type::DependentSizedArray: |
| 4568 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); |
| 4569 | break; |
| 4570 | default: |
| 4571 | return Owned(E); |
| 4572 | } |
| 4573 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4574 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 4575 | // That should be enough to guarantee that this type is complete, if we're |
| 4576 | // not processing a decltype expression. |
Jeffrey Yasskin | bbc4eea | 2011-01-27 19:17:54 +0000 | [diff] [blame] | 4577 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4578 | if (RD->isInvalidDecl() || RD->isDependentContext()) |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4579 | return Owned(E); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 4580 | |
| 4581 | bool IsDecltype = ExprEvalContexts.back().IsDecltype; |
| 4582 | CXXDestructorDecl *Destructor = IsDecltype ? 0 : LookupDestructor(RD); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4583 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4584 | if (Destructor) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4585 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
John McCall | 8e36d53 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 4586 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 4587 | PDiag(diag::err_access_dtor_temp) |
| 4588 | << E->getType()); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4589 | DiagnoseUseOfDecl(Destructor, E->getExprLoc()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4590 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 4591 | // If destructor is trivial, we can avoid the extra copy. |
| 4592 | if (Destructor->isTrivial()) |
| 4593 | return Owned(E); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4594 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4595 | // We need a cleanup, but we don't need to remember the temporary. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4596 | ExprNeedsCleanups = true; |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 4597 | } |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4598 | |
| 4599 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 4600 | CXXBindTemporaryExpr *Bind = CXXBindTemporaryExpr::Create(Context, Temp, E); |
| 4601 | |
| 4602 | if (IsDecltype) |
| 4603 | ExprEvalContexts.back().DelayedDecltypeBinds.push_back(Bind); |
| 4604 | |
| 4605 | return Owned(Bind); |
Anders Carlsson | 2d4cada | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4608 | ExprResult |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4609 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4610 | if (SubExpr.isInvalid()) |
| 4611 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4612 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4613 | return Owned(MaybeCreateExprWithCleanups(SubExpr.take())); |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4616 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
| 4617 | assert(SubExpr && "sub expression can't be null!"); |
| 4618 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 4619 | CleanupVarDeclMarking(); |
| 4620 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4621 | unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects; |
| 4622 | assert(ExprCleanupObjects.size() >= FirstCleanup); |
| 4623 | assert(ExprNeedsCleanups || ExprCleanupObjects.size() == FirstCleanup); |
| 4624 | if (!ExprNeedsCleanups) |
| 4625 | return SubExpr; |
| 4626 | |
| 4627 | ArrayRef<ExprWithCleanups::CleanupObject> Cleanups |
| 4628 | = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup, |
| 4629 | ExprCleanupObjects.size() - FirstCleanup); |
| 4630 | |
| 4631 | Expr *E = ExprWithCleanups::Create(Context, SubExpr, Cleanups); |
| 4632 | DiscardCleanupsInEvaluationContext(); |
| 4633 | |
| 4634 | return E; |
| 4635 | } |
| 4636 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4637 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4638 | assert(SubStmt && "sub statement can't be null!"); |
| 4639 | |
Eli Friedman | 3bda6b1 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 4640 | CleanupVarDeclMarking(); |
| 4641 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4642 | if (!ExprNeedsCleanups) |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4643 | return SubStmt; |
| 4644 | |
| 4645 | // FIXME: In order to attach the temporaries, wrap the statement into |
| 4646 | // a StmtExpr; currently this is only used for asm statements. |
| 4647 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
| 4648 | // a new AsmStmtWithTemporaries. |
| 4649 | CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, &SubStmt, 1, |
| 4650 | SourceLocation(), |
| 4651 | SourceLocation()); |
| 4652 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
| 4653 | SourceLocation()); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4654 | return MaybeCreateExprWithCleanups(E); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 4657 | /// Process the expression contained within a decltype. For such expressions, |
| 4658 | /// certain semantic checks on temporaries are delayed until this point, and |
| 4659 | /// are omitted for the 'topmost' call in the decltype expression. If the |
| 4660 | /// topmost call bound a temporary, strip that temporary off the expression. |
| 4661 | ExprResult Sema::ActOnDecltypeExpression(Expr *E) { |
| 4662 | ExpressionEvaluationContextRecord &Rec = ExprEvalContexts.back(); |
| 4663 | assert(Rec.IsDecltype && "not in a decltype expression"); |
| 4664 | |
| 4665 | // C++11 [expr.call]p11: |
| 4666 | // If a function call is a prvalue of object type, |
| 4667 | // -- if the function call is either |
| 4668 | // -- the operand of a decltype-specifier, or |
| 4669 | // -- the right operand of a comma operator that is the operand of a |
| 4670 | // decltype-specifier, |
| 4671 | // a temporary object is not introduced for the prvalue. |
| 4672 | |
| 4673 | // Recursively rebuild ParenExprs and comma expressions to strip out the |
| 4674 | // outermost CXXBindTemporaryExpr, if any. |
| 4675 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 4676 | ExprResult SubExpr = ActOnDecltypeExpression(PE->getSubExpr()); |
| 4677 | if (SubExpr.isInvalid()) |
| 4678 | return ExprError(); |
| 4679 | if (SubExpr.get() == PE->getSubExpr()) |
| 4680 | return Owned(E); |
| 4681 | return ActOnParenExpr(PE->getLParen(), PE->getRParen(), SubExpr.take()); |
| 4682 | } |
| 4683 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 4684 | if (BO->getOpcode() == BO_Comma) { |
| 4685 | ExprResult RHS = ActOnDecltypeExpression(BO->getRHS()); |
| 4686 | if (RHS.isInvalid()) |
| 4687 | return ExprError(); |
| 4688 | if (RHS.get() == BO->getRHS()) |
| 4689 | return Owned(E); |
| 4690 | return Owned(new (Context) BinaryOperator(BO->getLHS(), RHS.take(), |
| 4691 | BO_Comma, BO->getType(), |
| 4692 | BO->getValueKind(), |
| 4693 | BO->getObjectKind(), |
| 4694 | BO->getOperatorLoc())); |
| 4695 | } |
| 4696 | } |
| 4697 | |
| 4698 | CXXBindTemporaryExpr *TopBind = dyn_cast<CXXBindTemporaryExpr>(E); |
| 4699 | if (TopBind) |
| 4700 | E = TopBind->getSubExpr(); |
| 4701 | |
| 4702 | // Disable the special decltype handling now. |
| 4703 | Rec.IsDecltype = false; |
| 4704 | |
| 4705 | // Perform the semantic checks we delayed until this point. |
| 4706 | CallExpr *TopCall = dyn_cast<CallExpr>(E); |
| 4707 | for (unsigned I = 0, N = Rec.DelayedDecltypeCalls.size(); I != N; ++I) { |
| 4708 | CallExpr *Call = Rec.DelayedDecltypeCalls[I]; |
| 4709 | if (Call == TopCall) |
| 4710 | continue; |
| 4711 | |
| 4712 | if (CheckCallReturnType(Call->getCallReturnType(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4713 | Call->getLocStart(), |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 4714 | Call, Call->getDirectCallee())) |
| 4715 | return ExprError(); |
| 4716 | } |
| 4717 | |
| 4718 | // Now all relevant types are complete, check the destructors are accessible |
| 4719 | // and non-deleted, and annotate them on the temporaries. |
| 4720 | for (unsigned I = 0, N = Rec.DelayedDecltypeBinds.size(); I != N; ++I) { |
| 4721 | CXXBindTemporaryExpr *Bind = Rec.DelayedDecltypeBinds[I]; |
| 4722 | if (Bind == TopBind) |
| 4723 | continue; |
| 4724 | |
| 4725 | CXXTemporary *Temp = Bind->getTemporary(); |
| 4726 | |
| 4727 | CXXRecordDecl *RD = |
| 4728 | Bind->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 4729 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
| 4730 | Temp->setDestructor(Destructor); |
| 4731 | |
| 4732 | MarkFunctionReferenced(E->getExprLoc(), Destructor); |
| 4733 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 4734 | PDiag(diag::err_access_dtor_temp) |
| 4735 | << E->getType()); |
| 4736 | DiagnoseUseOfDecl(Destructor, E->getExprLoc()); |
| 4737 | |
| 4738 | // We need a cleanup, but we don't need to remember the temporary. |
| 4739 | ExprNeedsCleanups = true; |
| 4740 | } |
| 4741 | |
| 4742 | // Possibly strip off the top CXXBindTemporaryExpr. |
| 4743 | return Owned(E); |
| 4744 | } |
| 4745 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4746 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4747 | Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4748 | tok::TokenKind OpKind, ParsedType &ObjectType, |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4749 | bool &MayBePseudoDestructor) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4750 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4751 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4752 | if (Result.isInvalid()) return ExprError(); |
| 4753 | Base = Result.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4755 | Result = CheckPlaceholderExpr(Base); |
| 4756 | if (Result.isInvalid()) return ExprError(); |
| 4757 | Base = Result.take(); |
| 4758 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4759 | QualType BaseType = Base->getType(); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4760 | MayBePseudoDestructor = false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4761 | if (BaseType->isDependentType()) { |
Douglas Gregor | 4112718 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 4762 | // If we have a pointer to a dependent type and are using the -> operator, |
| 4763 | // the object type is the type that the pointer points to. We might still |
| 4764 | // have enough information about that type to do something useful. |
| 4765 | if (OpKind == tok::arrow) |
| 4766 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4767 | BaseType = Ptr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4769 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4770 | MayBePseudoDestructor = true; |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4771 | return Owned(Base); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4772 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4773 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4774 | // C++ [over.match.oper]p8: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4775 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4776 | // returned, with the original second operand. |
| 4777 | if (OpKind == tok::arrow) { |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4778 | // The set of types we've considered so far. |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4779 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4780 | SmallVector<SourceLocation, 8> Locations; |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4781 | CTypes.insert(Context.getCanonicalType(BaseType)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4782 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4783 | while (BaseType->isRecordType()) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4784 | Result = BuildOverloadedArrowExpr(S, Base, OpLoc); |
| 4785 | if (Result.isInvalid()) |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4786 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4787 | Base = Result.get(); |
| 4788 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Anders Carlsson | fbd2d49 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 4789 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4790 | BaseType = Base->getType(); |
John McCall | c1538c0 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4791 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
John McCall | bd0465b | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4792 | if (!CTypes.insert(CBaseType)) { |
Fariborz Jahanian | 10ce958 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4793 | Diag(OpLoc, diag::err_operator_arrow_circular); |
Fariborz Jahanian | ac3005c | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 4794 | for (unsigned i = 0; i < Locations.size(); i++) |
| 4795 | Diag(Locations[i], diag::note_declared_at); |
Fariborz Jahanian | 10ce958 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4796 | return ExprError(); |
| 4797 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4798 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4799 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 4800 | if (BaseType->isPointerType() || BaseType->isObjCObjectPointerType()) |
Douglas Gregor | e4f764f | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 4801 | BaseType = BaseType->getPointeeType(); |
| 4802 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 4804 | // Objective-C properties allow "." access on Objective-C pointer types, |
| 4805 | // so adjust the base type to the object type itself. |
| 4806 | if (BaseType->isObjCObjectPointerType()) |
| 4807 | BaseType = BaseType->getPointeeType(); |
| 4808 | |
| 4809 | // C++ [basic.lookup.classref]p2: |
| 4810 | // [...] If the type of the object expression is of pointer to scalar |
| 4811 | // type, the unqualified-id is looked up in the context of the complete |
| 4812 | // postfix-expression. |
| 4813 | // |
| 4814 | // This also indicates that we could be parsing a pseudo-destructor-name. |
| 4815 | // Note that Objective-C class and object types can be pseudo-destructor |
| 4816 | // expressions or normal member (ivar or property) access expressions. |
| 4817 | if (BaseType->isObjCObjectOrInterfaceType()) { |
| 4818 | MayBePseudoDestructor = true; |
| 4819 | } else if (!BaseType->isRecordType()) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4820 | ObjectType = ParsedType(); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4821 | MayBePseudoDestructor = true; |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4822 | return Owned(Base); |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4823 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4824 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4825 | // The object type must be complete (or dependent), or |
| 4826 | // C++11 [expr.prim.general]p3: |
| 4827 | // Unlike the object expression in other contexts, *this is not required to |
| 4828 | // be of complete type for purposes of class member access (5.2.5) outside |
| 4829 | // the member function body. |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4830 | if (!BaseType->isDependentType() && |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4831 | !isThisOutsideMemberFunctionBody(BaseType) && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 4832 | RequireCompleteType(OpLoc, BaseType, diag::err_incomplete_member_access)) |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4833 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4834 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4835 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4836 | // 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] | 4837 | // 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] | 4838 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 4839 | // up in the scope of class C. [...] |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4840 | ObjectType = ParsedType::make(BaseType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4841 | return move(Base); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4844 | ExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4845 | Expr *MemExpr) { |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4846 | SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4847 | Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call) |
| 4848 | << isa<CXXPseudoDestructorExpr>(MemExpr) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4849 | << FixItHint::CreateInsertion(ExpectedLParenLoc, "()"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4850 | |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4851 | return ActOnCallExpr(/*Scope*/ 0, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4852 | MemExpr, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4853 | /*LPLoc*/ ExpectedLParenLoc, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4854 | MultiExprArg(), |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4855 | /*RPLoc*/ ExpectedLParenLoc); |
| 4856 | } |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4857 | |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 4858 | static bool CheckArrow(Sema& S, QualType& ObjectType, Expr *&Base, |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4859 | tok::TokenKind& OpKind, SourceLocation OpLoc) { |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 4860 | if (Base->hasPlaceholderType()) { |
| 4861 | ExprResult result = S.CheckPlaceholderExpr(Base); |
| 4862 | if (result.isInvalid()) return true; |
| 4863 | Base = result.take(); |
| 4864 | } |
| 4865 | ObjectType = Base->getType(); |
| 4866 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4867 | // C++ [expr.pseudo]p2: |
| 4868 | // The left-hand side of the dot operator shall be of scalar type. The |
| 4869 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 4870 | // This scalar type is the object type. |
Eli Friedman | 6601b55 | 2012-01-25 04:29:24 +0000 | [diff] [blame] | 4871 | // Note that this is rather different from the normal handling for the |
| 4872 | // arrow operator. |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4873 | if (OpKind == tok::arrow) { |
| 4874 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 4875 | ObjectType = Ptr->getPointeeType(); |
| 4876 | } else if (!Base->isTypeDependent()) { |
| 4877 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 4878 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 4879 | << ObjectType << true |
| 4880 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 4881 | if (S.isSFINAEContext()) |
| 4882 | return true; |
| 4883 | |
| 4884 | OpKind = tok::period; |
| 4885 | } |
| 4886 | } |
| 4887 | |
| 4888 | return false; |
| 4889 | } |
| 4890 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4891 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4892 | SourceLocation OpLoc, |
| 4893 | tok::TokenKind OpKind, |
| 4894 | const CXXScopeSpec &SS, |
| 4895 | TypeSourceInfo *ScopeTypeInfo, |
| 4896 | SourceLocation CCLoc, |
| 4897 | SourceLocation TildeLoc, |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4898 | PseudoDestructorTypeStorage Destructed, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4899 | bool HasTrailingLParen) { |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4900 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4901 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 4902 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4903 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 4904 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4905 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4906 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4907 | if (getLangOpts().MicrosoftMode && ObjectType->isVoidType()) |
Nico Weber | 4bc6499 | 2012-01-23 06:08:16 +0000 | [diff] [blame] | 4908 | Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange(); |
Nico Weber | 5882927 | 2012-01-23 05:50:57 +0000 | [diff] [blame] | 4909 | else |
| 4910 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 4911 | << ObjectType << Base->getSourceRange(); |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4912 | return ExprError(); |
| 4913 | } |
| 4914 | |
| 4915 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4916 | // [...] The cv-unqualified versions of the object type and of the type |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4917 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4918 | if (DestructedTypeInfo) { |
| 4919 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 4920 | SourceLocation DestructedTypeStart |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4921 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4922 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
| 4923 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
| 4924 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
| 4925 | << ObjectType << DestructedType << Base->getSourceRange() |
| 4926 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4927 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4928 | // Recover by setting the destructed type to the object type. |
| 4929 | DestructedType = ObjectType; |
| 4930 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4931 | DestructedTypeStart); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4932 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4933 | } else if (DestructedType.getObjCLifetime() != |
| 4934 | ObjectType.getObjCLifetime()) { |
| 4935 | |
| 4936 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
| 4937 | // Okay: just pretend that the user provided the correctly-qualified |
| 4938 | // type. |
| 4939 | } else { |
| 4940 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
| 4941 | << ObjectType << DestructedType << Base->getSourceRange() |
| 4942 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 4943 | } |
| 4944 | |
| 4945 | // Recover by setting the destructed type to the object type. |
| 4946 | DestructedType = ObjectType; |
| 4947 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 4948 | DestructedTypeStart); |
| 4949 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4950 | } |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4951 | } |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4952 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4953 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4954 | // C++ [expr.pseudo]p2: |
| 4955 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 4956 | // form |
| 4957 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4958 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4959 | // |
| 4960 | // shall designate the same scalar type. |
| 4961 | if (ScopeTypeInfo) { |
| 4962 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 4963 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | b86a6b8 | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 4964 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4966 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4967 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4968 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4969 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4970 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4971 | ScopeType = QualType(); |
| 4972 | ScopeTypeInfo = 0; |
| 4973 | } |
| 4974 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4975 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4976 | Expr *Result |
| 4977 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 4978 | OpKind == tok::arrow, OpLoc, |
Douglas Gregor | a6ce608 | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 4979 | SS.getWithLocInContext(Context), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4980 | ScopeTypeInfo, |
| 4981 | CCLoc, |
| 4982 | TildeLoc, |
| 4983 | Destructed); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4984 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4985 | if (HasTrailingLParen) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4986 | return Owned(Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4987 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4988 | return DiagnoseDtorReference(Destructed.getLocation(), Result); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4991 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4992 | SourceLocation OpLoc, |
| 4993 | tok::TokenKind OpKind, |
| 4994 | CXXScopeSpec &SS, |
| 4995 | UnqualifiedId &FirstTypeName, |
| 4996 | SourceLocation CCLoc, |
| 4997 | SourceLocation TildeLoc, |
| 4998 | UnqualifiedId &SecondTypeName, |
| 4999 | bool HasTrailingLParen) { |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5000 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 5001 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 5002 | "Invalid first type name in pseudo-destructor"); |
| 5003 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 5004 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 5005 | "Invalid second type name in pseudo-destructor"); |
| 5006 | |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 5007 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 5008 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 5009 | return ExprError(); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 5010 | |
| 5011 | // Compute the object type that we should use for name lookup purposes. Only |
| 5012 | // record types and dependent types matter. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5013 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 5014 | if (!SS.isSet()) { |
John McCall | a2c4e72 | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 5015 | if (ObjectType->isRecordType()) |
| 5016 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5017 | else if (ObjectType->isDependentType()) |
| 5018 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 5019 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5020 | |
| 5021 | // Convert the name of the type being destructed (following the ~) into a |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5022 | // type (with source-location information). |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5023 | QualType DestructedType; |
| 5024 | TypeSourceInfo *DestructedTypeInfo = 0; |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 5025 | PseudoDestructorTypeStorage Destructed; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5026 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5027 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5028 | SecondTypeName.StartLocation, |
Fariborz Jahanian | 8796742 | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 5029 | S, &SS, true, false, ObjectTypePtrForLookup); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5030 | if (!T && |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 5031 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 5032 | (!SS.isSet() && ObjectType->isDependentType()))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5033 | // 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] | 5034 | // couldn't find anything useful in scope. Just store the identifier and |
| 5035 | // it's location, and we'll perform (qualified) name lookup again at |
| 5036 | // template instantiation time. |
| 5037 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 5038 | SecondTypeName.StartLocation); |
| 5039 | } else if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5040 | Diag(SecondTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5041 | diag::err_pseudo_dtor_destructor_non_type) |
| 5042 | << SecondTypeName.Identifier << ObjectType; |
| 5043 | if (isSFINAEContext()) |
| 5044 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5045 | |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5046 | // Recover by assuming we had the right type all along. |
| 5047 | DestructedType = ObjectType; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5048 | } else |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5049 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5050 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5051 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5052 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5053 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5054 | TemplateId->getTemplateArgs(), |
| 5055 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 5056 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 5057 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 5058 | TemplateId->Template, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5059 | TemplateId->TemplateNameLoc, |
| 5060 | TemplateId->LAngleLoc, |
| 5061 | TemplateArgsPtr, |
| 5062 | TemplateId->RAngleLoc); |
| 5063 | if (T.isInvalid() || !T.get()) { |
| 5064 | // Recover by assuming we had the right type all along. |
| 5065 | DestructedType = ObjectType; |
| 5066 | } else |
| 5067 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5068 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5069 | |
| 5070 | // 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] | 5071 | // information. |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 5072 | if (!DestructedType.isNull()) { |
| 5073 | if (!DestructedTypeInfo) |
| 5074 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5075 | SecondTypeName.StartLocation); |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 5076 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 5077 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5078 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5079 | // Convert the name of the scope type (the type prior to '::') into a type. |
| 5080 | TypeSourceInfo *ScopeTypeInfo = 0; |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5081 | QualType ScopeType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5083 | FirstTypeName.Identifier) { |
| 5084 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5085 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5086 | FirstTypeName.StartLocation, |
Douglas Gregor | a6ce608 | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 5087 | S, &SS, true, false, ObjectTypePtrForLookup); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5088 | if (!T) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5089 | Diag(FirstTypeName.StartLocation, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5090 | diag::err_pseudo_dtor_destructor_non_type) |
| 5091 | << FirstTypeName.Identifier << ObjectType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5092 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5093 | if (isSFINAEContext()) |
| 5094 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5095 | |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5096 | // Just drop this type. It's unnecessary anyway. |
| 5097 | ScopeType = QualType(); |
| 5098 | } else |
| 5099 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5100 | } else { |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5101 | // Resolve the template-id to a type. |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5102 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5103 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5104 | TemplateId->getTemplateArgs(), |
| 5105 | TemplateId->NumArgs); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 5106 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 5107 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 5108 | TemplateId->Template, |
Douglas Gregor | b1dd23f | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 5109 | TemplateId->TemplateNameLoc, |
| 5110 | TemplateId->LAngleLoc, |
| 5111 | TemplateArgsPtr, |
| 5112 | TemplateId->RAngleLoc); |
| 5113 | if (T.isInvalid() || !T.get()) { |
| 5114 | // Recover by dropping this type. |
| 5115 | ScopeType = QualType(); |
| 5116 | } else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5117 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 5118 | } |
| 5119 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5120 | |
Douglas Gregor | 90ad922 | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 5121 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 5122 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 5123 | FirstTypeName.StartLocation); |
| 5124 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5125 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5126 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | cdbd515 | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 5127 | ScopeTypeInfo, CCLoc, TildeLoc, |
Douglas Gregor | 678f90d | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 5128 | Destructed, HasTrailingLParen); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 5131 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
| 5132 | SourceLocation OpLoc, |
| 5133 | tok::TokenKind OpKind, |
| 5134 | SourceLocation TildeLoc, |
| 5135 | const DeclSpec& DS, |
| 5136 | bool HasTrailingLParen) { |
Eli Friedman | 0ce4de4 | 2012-01-25 04:35:06 +0000 | [diff] [blame] | 5137 | QualType ObjectType; |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 5138 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 5139 | return ExprError(); |
| 5140 | |
| 5141 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 5142 | |
| 5143 | TypeLocBuilder TLB; |
| 5144 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
| 5145 | DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 5146 | TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T); |
| 5147 | PseudoDestructorTypeStorage Destructed(DestructedTypeInfo); |
| 5148 | |
| 5149 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, CXXScopeSpec(), |
| 5150 | 0, SourceLocation(), TildeLoc, |
| 5151 | Destructed, HasTrailingLParen); |
| 5152 | } |
| 5153 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5154 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, |
Eli Friedman | 2fb8512 | 2012-03-01 01:30:04 +0000 | [diff] [blame] | 5155 | CXXConversionDecl *Method, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5156 | bool HadMultipleCandidates) { |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 5157 | if (Method->getParent()->isLambda() && |
| 5158 | Method->getConversionType()->isBlockPointerType()) { |
| 5159 | // This is a lambda coversion to block pointer; check if the argument |
| 5160 | // is a LambdaExpr. |
| 5161 | Expr *SubE = E; |
| 5162 | CastExpr *CE = dyn_cast<CastExpr>(SubE); |
| 5163 | if (CE && CE->getCastKind() == CK_NoOp) |
| 5164 | SubE = CE->getSubExpr(); |
| 5165 | SubE = SubE->IgnoreParens(); |
| 5166 | if (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(SubE)) |
| 5167 | SubE = BE->getSubExpr(); |
| 5168 | if (isa<LambdaExpr>(SubE)) { |
| 5169 | // For the conversion to block pointer on a lambda expression, we |
| 5170 | // construct a special BlockLiteral instead; this doesn't really make |
| 5171 | // a difference in ARC, but outside of ARC the resulting block literal |
| 5172 | // follows the normal lifetime rules for block literals instead of being |
| 5173 | // autoreleased. |
| 5174 | DiagnosticErrorTrap Trap(Diags); |
| 5175 | ExprResult Exp = BuildBlockForLambdaConversion(E->getExprLoc(), |
| 5176 | E->getExprLoc(), |
| 5177 | Method, E); |
| 5178 | if (Exp.isInvalid()) |
| 5179 | Diag(E->getExprLoc(), diag::note_lambda_to_block_conv); |
| 5180 | return Exp; |
| 5181 | } |
| 5182 | } |
| 5183 | |
| 5184 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5185 | ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/0, |
| 5186 | FoundDecl, Method); |
| 5187 | if (Exp.isInvalid()) |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 5188 | return true; |
Eli Friedman | f719553 | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 5189 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5190 | MemberExpr *ME = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5191 | new (Context) MemberExpr(Exp.take(), /*IsArrow=*/false, Method, |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5192 | SourceLocation(), Context.BoundMemberTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5193 | VK_RValue, OK_Ordinary); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5194 | if (HadMultipleCandidates) |
| 5195 | ME->setHadMultipleCandidates(true); |
| 5196 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5197 | QualType ResultType = Method->getResultType(); |
| 5198 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 5199 | ResultType = ResultType.getNonLValueExprType(Context); |
| 5200 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5201 | MarkFunctionReferenced(Exp.get()->getLocStart(), Method); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5202 | CXXMemberCallExpr *CE = |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5203 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, VK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5204 | Exp.get()->getLocEnd()); |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 5205 | return CE; |
| 5206 | } |
| 5207 | |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 5208 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 5209 | SourceLocation RParen) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 5210 | CanThrowResult CanThrow = canThrow(Operand); |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 5211 | return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand, |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 5212 | CanThrow, KeyLoc, RParen)); |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
| 5215 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 5216 | Expr *Operand, SourceLocation RParen) { |
| 5217 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 22e3a93 | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 5218 | } |
| 5219 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5220 | /// Perform the conversions required for an expression used in a |
| 5221 | /// context that ignores the result. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5222 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5223 | if (E->hasPlaceholderType()) { |
| 5224 | ExprResult result = CheckPlaceholderExpr(E); |
| 5225 | if (result.isInvalid()) return Owned(E); |
| 5226 | E = result.take(); |
| 5227 | } |
| 5228 | |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 5229 | // C99 6.3.2.1: |
| 5230 | // [Except in specific positions,] an lvalue that does not have |
| 5231 | // array type is converted to the value stored in the |
| 5232 | // designated object (and is no longer an lvalue). |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 5233 | if (E->isRValue()) { |
| 5234 | // In C, function designators (i.e. expressions of function type) |
| 5235 | // are r-values, but we still want to do function-to-pointer decay |
| 5236 | // on them. This is both technically correct and convenient for |
| 5237 | // some clients. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5238 | if (!getLangOpts().CPlusPlus && E->getType()->isFunctionType()) |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 5239 | return DefaultFunctionArrayConversion(E); |
| 5240 | |
| 5241 | return Owned(E); |
| 5242 | } |
John McCall | fee942d | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 5243 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5244 | // Otherwise, this rule does not apply in C++, at least not for the moment. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5245 | if (getLangOpts().CPlusPlus) return Owned(E); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5246 | |
| 5247 | // GCC seems to also exclude expressions of incomplete enum type. |
| 5248 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
| 5249 | if (!T->getDecl()->isComplete()) { |
| 5250 | // FIXME: stupid workaround for a codegen bug! |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5251 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).take(); |
| 5252 | return Owned(E); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5253 | } |
| 5254 | } |
| 5255 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5256 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 5257 | if (Res.isInvalid()) |
| 5258 | return Owned(E); |
| 5259 | E = Res.take(); |
| 5260 | |
John McCall | ca61b65 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 5261 | if (!E->getType()->isVoidType()) |
| 5262 | RequireCompleteType(E->getExprLoc(), E->getType(), |
| 5263 | diag::err_incomplete_type); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5264 | return Owned(E); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5265 | } |
| 5266 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5267 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE) { |
| 5268 | ExprResult FullExpr = Owned(FE); |
| 5269 | |
| 5270 | if (!FullExpr.get()) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 5271 | return ExprError(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5272 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5273 | if (DiagnoseUnexpandedParameterPack(FullExpr.get())) |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 5274 | return ExprError(); |
| 5275 | |
Douglas Gregor | 95715f9 | 2011-12-15 00:53:32 +0000 | [diff] [blame] | 5276 | // Top-level message sends default to 'id' when we're in a debugger. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5277 | if (getLangOpts().DebuggerCastResultToId && |
Douglas Gregor | 95715f9 | 2011-12-15 00:53:32 +0000 | [diff] [blame] | 5278 | FullExpr.get()->getType() == Context.UnknownAnyTy && |
| 5279 | isa<ObjCMessageExpr>(FullExpr.get())) { |
| 5280 | FullExpr = forceUnknownAnyToType(FullExpr.take(), Context.getObjCIdType()); |
| 5281 | if (FullExpr.isInvalid()) |
| 5282 | return ExprError(); |
| 5283 | } |
| 5284 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5285 | FullExpr = CheckPlaceholderExpr(FullExpr.take()); |
| 5286 | if (FullExpr.isInvalid()) |
| 5287 | return ExprError(); |
Douglas Gregor | 0ec210b | 2011-03-07 02:05:23 +0000 | [diff] [blame] | 5288 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5289 | FullExpr = IgnoredValueConversions(FullExpr.take()); |
| 5290 | if (FullExpr.isInvalid()) |
| 5291 | return ExprError(); |
| 5292 | |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 5293 | CheckImplicitConversions(FullExpr.get(), FullExpr.get()->getExprLoc()); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 5294 | return MaybeCreateExprWithCleanups(FullExpr); |
Anders Carlsson | 85a307d | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 5295 | } |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 5296 | |
| 5297 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
| 5298 | if (!FullStmt) return StmtError(); |
| 5299 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 5300 | return MaybeCreateStmtWithCleanups(FullStmt); |
Argyrios Kyrtzidis | 3050d9b | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 5301 | } |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 5302 | |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 5303 | Sema::IfExistsResult |
| 5304 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, |
| 5305 | CXXScopeSpec &SS, |
| 5306 | const DeclarationNameInfo &TargetNameInfo) { |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 5307 | DeclarationName TargetName = TargetNameInfo.getName(); |
| 5308 | if (!TargetName) |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 5309 | return IER_DoesNotExist; |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 5310 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 5311 | // If the name itself is dependent, then the result is dependent. |
| 5312 | if (TargetName.isDependentName()) |
| 5313 | return IER_Dependent; |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 5314 | |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 5315 | // Do the redeclaration lookup in the current scope. |
| 5316 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
| 5317 | Sema::NotForRedeclaration); |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 5318 | LookupParsedName(R, S, &SS); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 5319 | R.suppressDiagnostics(); |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 5320 | |
| 5321 | switch (R.getResultKind()) { |
| 5322 | case LookupResult::Found: |
| 5323 | case LookupResult::FoundOverloaded: |
| 5324 | case LookupResult::FoundUnresolvedValue: |
| 5325 | case LookupResult::Ambiguous: |
| 5326 | return IER_Exists; |
| 5327 | |
| 5328 | case LookupResult::NotFound: |
| 5329 | return IER_DoesNotExist; |
| 5330 | |
| 5331 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5332 | return IER_Dependent; |
| 5333 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5334 | |
| 5335 | llvm_unreachable("Invalid LookupResult Kind!"); |
Francois Pichet | 4a7de3e | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 5336 | } |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 5337 | |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 5338 | Sema::IfExistsResult |
| 5339 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc, |
| 5340 | bool IsIfExists, CXXScopeSpec &SS, |
| 5341 | UnqualifiedId &Name) { |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 5342 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 5343 | |
| 5344 | // Check for unexpanded parameter packs. |
| 5345 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
| 5346 | collectUnexpandedParameterPacks(SS, Unexpanded); |
| 5347 | collectUnexpandedParameterPacks(TargetNameInfo, Unexpanded); |
| 5348 | if (!Unexpanded.empty()) { |
| 5349 | DiagnoseUnexpandedParameterPacks(KeywordLoc, |
| 5350 | IsIfExists? UPPC_IfExists |
| 5351 | : UPPC_IfNotExists, |
| 5352 | Unexpanded); |
| 5353 | return IER_Error; |
| 5354 | } |
| 5355 | |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 5356 | return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo); |
| 5357 | } |