Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 15 | #include "clang/Sema/DeclSpec.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Initialization.h" |
| 17 | #include "clang/Sema/Lookup.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Scope.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 21 | #include "clang/Sema/TemplateDeduction.h" |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Nick Lewycky | fca84b2 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 24 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprCXX.h" |
Fariborz Jahanian | d426662 | 2010-06-16 18:56:04 +0000 | [diff] [blame] | 27 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 29 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
David Blaikie | 91ec789 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 32 | #include "TypeLocBuilder.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 73e0a91 | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 36 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 38 | ParsedType Sema::getDestructorName(SourceLocation TildeLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 39 | IdentifierInfo &II, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 40 | SourceLocation NameLoc, |
| 41 | Scope *S, CXXScopeSpec &SS, |
| 42 | ParsedType ObjectTypePtr, |
| 43 | bool EnteringContext) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 44 | // Determine where to perform name lookup. |
| 45 | |
| 46 | // FIXME: This area of the standard is very messy, and the current |
| 47 | // wording is rather unclear about which scopes we search for the |
| 48 | // destructor name; see core issues 399 and 555. Issue 399 in |
| 49 | // particular shows where the current description of destructor name |
| 50 | // lookup is completely out of line with existing practice, e.g., |
| 51 | // this appears to be ill-formed: |
| 52 | // |
| 53 | // namespace N { |
| 54 | // template <typename T> struct S { |
| 55 | // ~S(); |
| 56 | // }; |
| 57 | // } |
| 58 | // |
| 59 | // void f(N::S<int>* s) { |
| 60 | // s->N::S<int>::~S(); |
| 61 | // } |
| 62 | // |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 63 | // See also PR6358 and PR6359. |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 64 | // For this reason, we're currently only doing the C++03 version of this |
| 65 | // code; the C++0x version has to wait until we get a proper spec. |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 66 | QualType SearchType; |
| 67 | DeclContext *LookupCtx = 0; |
| 68 | bool isDependent = false; |
| 69 | bool LookInScope = false; |
| 70 | |
| 71 | // If we have an object type, it's because we are in a |
| 72 | // pseudo-destructor-expression or a member access expression, and |
| 73 | // we know what type we're looking for. |
| 74 | if (ObjectTypePtr) |
| 75 | SearchType = GetTypeFromParser(ObjectTypePtr); |
| 76 | |
| 77 | if (SS.isSet()) { |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 78 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 80 | bool AlreadySearched = false; |
| 81 | bool LookAtPrefix = true; |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 82 | // C++ [basic.lookup.qual]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 83 | // If a pseudo-destructor-name (5.2.4) contains a nested-name-specifier, |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 84 | // the type-names are looked up as types in the scope designated by the |
| 85 | // nested-name-specifier. In a qualified-id of the form: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 86 | // |
| 87 | // ::[opt] nested-name-specifier ~ class-name |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 88 | // |
| 89 | // where the nested-name-specifier designates a namespace scope, and in |
Chandler Carruth | 5e895a8 | 2010-02-21 10:19:54 +0000 | [diff] [blame] | 90 | // a qualified-id of the form: |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 91 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 92 | // ::opt nested-name-specifier class-name :: ~ class-name |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 93 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 94 | // the class-names are looked up as types in the scope designated by |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 95 | // the nested-name-specifier. |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 96 | // |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 97 | // Here, we check the first case (completely) and determine whether the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 98 | // code below is permitted to look at the prefix of the |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 99 | // nested-name-specifier. |
| 100 | DeclContext *DC = computeDeclContext(SS, EnteringContext); |
| 101 | if (DC && DC->isFileContext()) { |
| 102 | AlreadySearched = true; |
| 103 | LookupCtx = DC; |
| 104 | isDependent = false; |
| 105 | } else if (DC && isa<CXXRecordDecl>(DC)) |
| 106 | LookAtPrefix = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 107 | |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 108 | // The second case from the C++03 rules quoted further above. |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 109 | NestedNameSpecifier *Prefix = 0; |
| 110 | if (AlreadySearched) { |
| 111 | // Nothing left to do. |
| 112 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { |
| 113 | CXXScopeSpec PrefixSS; |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 114 | PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data())); |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 115 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); |
| 116 | isDependent = isDependentScopeSpecifier(PrefixSS); |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 117 | } else if (ObjectTypePtr) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 118 | LookupCtx = computeDeclContext(SearchType); |
| 119 | isDependent = SearchType->isDependentType(); |
| 120 | } else { |
| 121 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 122 | isDependent = LookupCtx && LookupCtx->isDependentContext(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 123 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 124 | |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 125 | LookInScope = false; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 126 | } else if (ObjectTypePtr) { |
| 127 | // C++ [basic.lookup.classref]p3: |
| 128 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 129 | // in the context of the entire postfix-expression. If the type T |
| 130 | // of the object expression is of a class type C, the type-name is |
| 131 | // also looked up in the scope of class C. At least one of the |
| 132 | // lookups shall find a name that refers to (possibly |
| 133 | // cv-qualified) T. |
| 134 | LookupCtx = computeDeclContext(SearchType); |
| 135 | isDependent = SearchType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 136 | assert((isDependent || !SearchType->isIncompleteType()) && |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 137 | "Caller should have completed object type"); |
| 138 | |
| 139 | LookInScope = true; |
| 140 | } else { |
| 141 | // Perform lookup into the current scope (only). |
| 142 | LookInScope = true; |
| 143 | } |
| 144 | |
Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 145 | TypeDecl *NonMatchingTypeDecl = 0; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 146 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); |
| 147 | for (unsigned Step = 0; Step != 2; ++Step) { |
| 148 | // Look for the name first in the computed lookup context (if we |
Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 149 | // have one) and, if that fails to find a match, in the scope (if |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 150 | // we're allowed to look there). |
| 151 | Found.clear(); |
| 152 | if (Step == 0 && LookupCtx) |
| 153 | LookupQualifiedName(Found, LookupCtx); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 154 | else if (Step == 1 && LookInScope && S) |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 155 | LookupName(Found, S); |
| 156 | else |
| 157 | continue; |
| 158 | |
| 159 | // FIXME: Should we be suppressing ambiguities here? |
| 160 | if (Found.isAmbiguous()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 161 | return ParsedType(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 162 | |
| 163 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
| 164 | QualType T = Context.getTypeDeclType(Type); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 165 | |
| 166 | if (SearchType.isNull() || SearchType->isDependentType() || |
| 167 | Context.hasSameUnqualifiedType(T, SearchType)) { |
| 168 | // We found our type! |
| 169 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 170 | return ParsedType::make(T); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 171 | } |
John Wiegley | 36784e7 | 2011-03-08 08:13:22 +0000 | [diff] [blame] | 172 | |
Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 173 | if (!SearchType.isNull()) |
| 174 | NonMatchingTypeDecl = Type; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // If the name that we found is a class template name, and it is |
| 178 | // the same name as the template name in the last part of the |
| 179 | // nested-name-specifier (if present) or the object type, then |
| 180 | // this is the destructor for that class. |
| 181 | // FIXME: This is a workaround until we get real drafting for core |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 182 | // issue 399, for which there isn't even an obvious direction. |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 183 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
| 184 | QualType MemberOfType; |
| 185 | if (SS.isSet()) { |
| 186 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
| 187 | // Figure out the type of the context, if it has one. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 188 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) |
| 189 | MemberOfType = Context.getTypeDeclType(Record); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | if (MemberOfType.isNull()) |
| 193 | MemberOfType = SearchType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 194 | |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 195 | if (MemberOfType.isNull()) |
| 196 | continue; |
| 197 | |
| 198 | // We're referring into a class template specialization. If the |
| 199 | // class template we found is the same as the template being |
| 200 | // specialized, we found what we are looking for. |
| 201 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
| 202 | if (ClassTemplateSpecializationDecl *Spec |
| 203 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
| 204 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
| 205 | Template->getCanonicalDecl()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 206 | return ParsedType::make(MemberOfType); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | continue; |
| 210 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 211 | |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 212 | // We're referring to an unresolved class template |
| 213 | // specialization. Determine whether we class template we found |
| 214 | // is the same as the template being specialized or, if we don't |
| 215 | // know which template is being specialized, that it at least |
| 216 | // has the same name. |
| 217 | if (const TemplateSpecializationType *SpecType |
| 218 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
| 219 | TemplateName SpecName = SpecType->getTemplateName(); |
| 220 | |
| 221 | // The class template we found is the same template being |
| 222 | // specialized. |
| 223 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
| 224 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 225 | return ParsedType::make(MemberOfType); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 226 | |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | // The class template we found has the same name as the |
| 231 | // (dependent) template name being specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 232 | if (DependentTemplateName *DepTemplate |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 233 | = SpecName.getAsDependentTemplateName()) { |
| 234 | if (DepTemplate->isIdentifier() && |
| 235 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 236 | return ParsedType::make(MemberOfType); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 237 | |
| 238 | continue; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (isDependent) { |
| 245 | // We didn't find our type, but that's okay: it's dependent |
| 246 | // anyway. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 247 | |
| 248 | // FIXME: What if we have no nested-name-specifier? |
| 249 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), |
| 250 | SS.getWithLocInContext(Context), |
| 251 | II, NameLoc); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 252 | return ParsedType::make(T); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 255 | if (NonMatchingTypeDecl) { |
| 256 | QualType T = Context.getTypeDeclType(NonMatchingTypeDecl); |
| 257 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) |
| 258 | << T << SearchType; |
| 259 | Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here) |
| 260 | << T; |
| 261 | } else if (ObjectTypePtr) |
| 262 | Diag(NameLoc, diag::err_ident_in_dtor_not_a_type) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 263 | << &II; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 264 | else |
| 265 | Diag(NameLoc, diag::err_destructor_class_name); |
| 266 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 267 | return ParsedType(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 268 | } |
| 269 | |
David Blaikie | 53a75c0 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 270 | ParsedType Sema::getDestructorType(const DeclSpec& DS, ParsedType ObjectType) { |
David Blaikie | 4db8c44 | 2011-12-12 04:13:55 +0000 | [diff] [blame] | 271 | if (DS.getTypeSpecType() == DeclSpec::TST_error || !ObjectType) |
David Blaikie | 53a75c0 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 272 | return ParsedType(); |
| 273 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype |
| 274 | && "only get destructor types from declspecs"); |
| 275 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 276 | QualType SearchType = GetTypeFromParser(ObjectType); |
| 277 | if (SearchType->isDependentType() || Context.hasSameUnqualifiedType(SearchType, T)) { |
| 278 | return ParsedType::make(T); |
| 279 | } |
| 280 | |
| 281 | Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch) |
| 282 | << T << SearchType; |
| 283 | return ParsedType(); |
| 284 | } |
| 285 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 286 | /// \brief Build a C++ typeid expression with a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 287 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 288 | SourceLocation TypeidLoc, |
| 289 | TypeSourceInfo *Operand, |
| 290 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 291 | // C++ [expr.typeid]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 292 | // The top-level cv-qualifiers of the lvalue expression or the type-id |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 293 | // that is the operand of typeid are always ignored. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 294 | // If the type of the type-id is a class type or a reference to a class |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 295 | // type, the class shall be completely-defined. |
Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 296 | Qualifiers Quals; |
| 297 | QualType T |
| 298 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
| 299 | Quals); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 300 | if (T->getAs<RecordType>() && |
| 301 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 302 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 303 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 304 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
| 305 | Operand, |
| 306 | SourceRange(TypeidLoc, RParenLoc))); |
| 307 | } |
| 308 | |
| 309 | /// \brief Build a C++ typeid expression with an expression operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 310 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 311 | SourceLocation TypeidLoc, |
| 312 | Expr *E, |
| 313 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 314 | if (E && !E->isTypeDependent()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 315 | if (E->getType()->isPlaceholderType()) { |
| 316 | ExprResult result = CheckPlaceholderExpr(E); |
| 317 | if (result.isInvalid()) return ExprError(); |
| 318 | E = result.take(); |
| 319 | } |
| 320 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 321 | QualType T = E->getType(); |
| 322 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
| 323 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 324 | // C++ [expr.typeid]p3: |
| 325 | // [...] If the type of the expression is a class type, the class |
| 326 | // shall be completely-defined. |
| 327 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 328 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 330 | // C++ [expr.typeid]p3: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 331 | // When typeid is applied to an expression other than an glvalue of a |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 332 | // polymorphic class type [...] [the] expression is an unevaluated |
| 333 | // operand. [...] |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 334 | if (RecordD->isPolymorphic() && E->Classify(Context).isGLValue()) { |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 335 | // The subexpression is potentially evaluated; switch the context |
| 336 | // and recheck the subexpression. |
| 337 | ExprResult Result = TranformToPotentiallyEvaluated(E); |
| 338 | if (Result.isInvalid()) return ExprError(); |
| 339 | E = Result.take(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 340 | |
| 341 | // We require a vtable to query the type at run time. |
| 342 | MarkVTableUsed(TypeidLoc, RecordD); |
| 343 | } |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 344 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 345 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 346 | // C++ [expr.typeid]p4: |
| 347 | // [...] If the type of the type-id is a reference to a possibly |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 348 | // cv-qualified type, the result of the typeid expression refers to a |
| 349 | // std::type_info object representing the cv-unqualified referenced |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 350 | // type. |
Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 351 | Qualifiers Quals; |
| 352 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
| 353 | if (!Context.hasSameType(T, UnqualT)) { |
| 354 | T = UnqualT; |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 355 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).take(); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 359 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 360 | E, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 361 | SourceRange(TypeidLoc, RParenLoc))); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 365 | ExprResult |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 366 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 367 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 368 | // Find the std::type_info type. |
Sebastian Redl | ce0682f | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 369 | if (!getStdNamespace()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 370 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 372 | if (!CXXTypeInfoDecl) { |
| 373 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 374 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 375 | LookupQualifiedName(R, getStdNamespace()); |
| 376 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 377 | if (!CXXTypeInfoDecl) |
| 378 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 379 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 380 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 381 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 382 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 383 | if (isType) { |
| 384 | // The operand is a type; handle it as such. |
| 385 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 386 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 387 | &TInfo); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 388 | if (T.isNull()) |
| 389 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 390 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 391 | if (!TInfo) |
| 392 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 393 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 394 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 395 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 397 | // The operand is an expression. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 398 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 401 | /// Retrieve the UuidAttr associated with QT. |
| 402 | static UuidAttr *GetUuidAttrOfType(QualType QT) { |
| 403 | // Optionally remove one level of pointer, reference or array indirection. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 404 | const Type *Ty = QT.getTypePtr();; |
Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 405 | if (QT->isPointerType() || QT->isReferenceType()) |
| 406 | Ty = QT->getPointeeType().getTypePtr(); |
| 407 | else if (QT->isArrayType()) |
| 408 | Ty = cast<ArrayType>(QT)->getElementType().getTypePtr(); |
| 409 | |
Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 410 | // Loop all record redeclaration looking for an uuid attribute. |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 411 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 412 | for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(), |
| 413 | E = RD->redecls_end(); I != E; ++I) { |
| 414 | if (UuidAttr *Uuid = I->getAttr<UuidAttr>()) |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 415 | return Uuid; |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 416 | } |
Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 417 | |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 418 | return 0; |
Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 421 | /// \brief Build a Microsoft __uuidof expression with a type operand. |
| 422 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 423 | SourceLocation TypeidLoc, |
| 424 | TypeSourceInfo *Operand, |
| 425 | SourceLocation RParenLoc) { |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 426 | if (!Operand->getType()->isDependentType()) { |
| 427 | if (!GetUuidAttrOfType(Operand->getType())) |
| 428 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 429 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 430 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 431 | // FIXME: add __uuidof semantic analysis for type operand. |
| 432 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 433 | Operand, |
| 434 | SourceRange(TypeidLoc, RParenLoc))); |
| 435 | } |
| 436 | |
| 437 | /// \brief Build a Microsoft __uuidof expression with an expression operand. |
| 438 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 439 | SourceLocation TypeidLoc, |
| 440 | Expr *E, |
| 441 | SourceLocation RParenLoc) { |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 442 | if (!E->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 443 | if (!GetUuidAttrOfType(E->getType()) && |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 444 | !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 445 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 446 | } |
| 447 | // FIXME: add __uuidof semantic analysis for type operand. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 448 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 449 | E, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 450 | SourceRange(TypeidLoc, RParenLoc))); |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 454 | ExprResult |
| 455 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 456 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 457 | // If MSVCGuidDecl has not been cached, do the lookup. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 458 | if (!MSVCGuidDecl) { |
| 459 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 460 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 461 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 462 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 463 | if (!MSVCGuidDecl) |
| 464 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 467 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 468 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 469 | if (isType) { |
| 470 | // The operand is a type; handle it as such. |
| 471 | TypeSourceInfo *TInfo = 0; |
| 472 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 473 | &TInfo); |
| 474 | if (T.isNull()) |
| 475 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 476 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 477 | if (!TInfo) |
| 478 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 479 | |
| 480 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 481 | } |
| 482 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 483 | // The operand is an expression. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 484 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 485 | } |
| 486 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 487 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 488 | ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 489 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 490 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 491 | "Unknown C++ Boolean value!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 492 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 493 | Context.BoolTy, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 494 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 495 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 496 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 497 | ExprResult |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 498 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
| 499 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 500 | } |
| 501 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 502 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 503 | ExprResult |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 504 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
| 505 | bool IsThrownVarInScope = false; |
| 506 | if (Ex) { |
| 507 | // C++0x [class.copymove]p31: |
| 508 | // When certain criteria are met, an implementation is allowed to omit the |
| 509 | // copy/move construction of a class object [...] |
| 510 | // |
| 511 | // - in a throw-expression, when the operand is the name of a |
| 512 | // non-volatile automatic object (other than a function or catch- |
| 513 | // clause parameter) whose scope does not extend beyond the end of the |
| 514 | // innermost enclosing try-block (if there is one), the copy/move |
| 515 | // operation from the operand to the exception object (15.1) can be |
| 516 | // omitted by constructing the automatic object directly into the |
| 517 | // exception object |
| 518 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) |
| 519 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 520 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { |
| 521 | for( ; S; S = S->getParent()) { |
| 522 | if (S->isDeclScope(Var)) { |
| 523 | IsThrownVarInScope = true; |
| 524 | break; |
| 525 | } |
| 526 | |
| 527 | if (S->getFlags() & |
| 528 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
| 529 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | |
| 530 | Scope::TryScope)) |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
| 538 | } |
| 539 | |
| 540 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
| 541 | bool IsThrownVarInScope) { |
Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 542 | // Don't report an error if 'throw' is used in system headers. |
Anders Carlsson | 15348ae | 2011-02-28 02:27:16 +0000 | [diff] [blame] | 543 | if (!getLangOptions().CXXExceptions && |
Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 544 | !getSourceManager().isInSystemHeader(OpLoc)) |
Anders Carlsson | b1fba31 | 2011-02-19 21:53:09 +0000 | [diff] [blame] | 545 | Diag(OpLoc, diag::err_exceptions_disabled) << "throw"; |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 546 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 547 | if (Ex && !Ex->isTypeDependent()) { |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 548 | ExprResult ExRes = CheckCXXThrowOperand(OpLoc, Ex, IsThrownVarInScope); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 549 | if (ExRes.isInvalid()) |
| 550 | return ExprError(); |
| 551 | Ex = ExRes.take(); |
| 552 | } |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 553 | |
| 554 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc, |
| 555 | IsThrownVarInScope)); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 559 | ExprResult Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *E, |
| 560 | bool IsThrownVarInScope) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 561 | // C++ [except.throw]p3: |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 562 | // A throw-expression initializes a temporary object, called the exception |
| 563 | // object, the type of which is determined by removing any top-level |
| 564 | // cv-qualifiers from the static type of the operand of throw and adjusting |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 565 | // the type from "array of T" or "function returning T" to "pointer to T" |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 566 | // or "pointer to function returning T", [...] |
| 567 | if (E->getType().hasQualifiers()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 568 | E = ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 569 | E->getValueKind()).take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 570 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 571 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 572 | if (Res.isInvalid()) |
| 573 | return ExprError(); |
| 574 | E = Res.take(); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 575 | |
| 576 | // If the type of the exception would be an incomplete type or a pointer |
| 577 | // to an incomplete type other than (cv) void the program is ill-formed. |
| 578 | QualType Ty = E->getType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 579 | bool isPointer = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 580 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 581 | Ty = Ptr->getPointeeType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 582 | isPointer = true; |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 583 | } |
| 584 | if (!isPointer || !Ty->isVoidType()) { |
| 585 | if (RequireCompleteType(ThrowLoc, Ty, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 586 | PDiag(isPointer ? diag::err_throw_incomplete_ptr |
| 587 | : diag::err_throw_incomplete) |
| 588 | << E->getSourceRange())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 589 | return ExprError(); |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | bf422f9 | 2010-04-15 18:05:39 +0000 | [diff] [blame] | 591 | if (RequireNonAbstractType(ThrowLoc, E->getType(), |
| 592 | PDiag(diag::err_throw_abstract_type) |
| 593 | << E->getSourceRange())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 594 | return ExprError(); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 595 | } |
| 596 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 597 | // Initialize the exception result. This implicitly weeds out |
| 598 | // abstract types or types with inaccessible copy constructors. |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 599 | |
| 600 | // C++0x [class.copymove]p31: |
| 601 | // When certain criteria are met, an implementation is allowed to omit the |
| 602 | // copy/move construction of a class object [...] |
| 603 | // |
| 604 | // - in a throw-expression, when the operand is the name of a |
| 605 | // non-volatile automatic object (other than a function or catch-clause |
| 606 | // parameter) whose scope does not extend beyond the end of the |
| 607 | // innermost enclosing try-block (if there is one), the copy/move |
| 608 | // operation from the operand to the exception object (15.1) can be |
| 609 | // omitted by constructing the automatic object directly into the |
| 610 | // exception object |
| 611 | const VarDecl *NRVOVariable = 0; |
| 612 | if (IsThrownVarInScope) |
| 613 | NRVOVariable = getCopyElisionCandidate(QualType(), E, false); |
| 614 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 615 | InitializedEntity Entity = |
Douglas Gregor | 72dfa27 | 2011-01-21 22:46:35 +0000 | [diff] [blame] | 616 | InitializedEntity::InitializeException(ThrowLoc, E->getType(), |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 617 | /*NRVO=*/NRVOVariable != 0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 618 | Res = PerformMoveOrCopyInitialization(Entity, NRVOVariable, |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 619 | QualType(), E, |
| 620 | IsThrownVarInScope); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 621 | if (Res.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 622 | return ExprError(); |
| 623 | E = Res.take(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 624 | |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 625 | // If the exception has class type, we need additional handling. |
| 626 | const RecordType *RecordTy = Ty->getAs<RecordType>(); |
| 627 | if (!RecordTy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 628 | return Owned(E); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 629 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 630 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 631 | // If we are throwing a polymorphic class type or pointer thereof, |
| 632 | // exception handling will make use of the vtable. |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 633 | MarkVTableUsed(ThrowLoc, RD); |
| 634 | |
Eli Friedman | 98efb9f | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 635 | // If a pointer is thrown, the referenced object will not be destroyed. |
| 636 | if (isPointer) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 637 | return Owned(E); |
Eli Friedman | 98efb9f | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 638 | |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 639 | // If the class has a non-trivial destructor, we must be able to call it. |
| 640 | if (RD->hasTrivialDestructor()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 641 | return Owned(E); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 642 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 643 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 644 | = const_cast<CXXDestructorDecl*>(LookupDestructor(RD)); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 645 | if (!Destructor) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 646 | return Owned(E); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 647 | |
| 648 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
| 649 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 650 | PDiag(diag::err_access_dtor_exception) << Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 651 | return Owned(E); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 652 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 653 | |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 654 | QualType Sema::getCurrentThisType() { |
| 655 | DeclContext *DC = getFunctionLevelDeclContext(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 656 | QualType ThisTy; |
| 657 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { |
| 658 | if (method && method->isInstance()) |
| 659 | ThisTy = method->getThisType(Context); |
| 660 | } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { |
| 661 | // C++0x [expr.prim]p4: |
| 662 | // Otherwise, if a member-declarator declares a non-static data member |
| 663 | // of a class X, the expression this is a prvalue of type "pointer to X" |
| 664 | // within the optional brace-or-equal-initializer. |
| 665 | Scope *S = getScopeForContext(DC); |
| 666 | if (!S || S->getFlags() & Scope::ThisScope) |
| 667 | ThisTy = Context.getPointerType(Context.getRecordType(RD)); |
| 668 | } |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 669 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 670 | return ThisTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 673 | void Sema::CheckCXXThisCapture(SourceLocation Loc) { |
| 674 | // We don't need to capture this in an unevaluated context. |
| 675 | if (ExprEvalContexts.back().Context == Unevaluated) |
| 676 | return; |
| 677 | |
| 678 | // Otherwise, check that we can capture 'this'. |
| 679 | unsigned NumClosures = 0; |
| 680 | for (unsigned idx = FunctionScopes.size() - 1; idx != 0; idx--) { |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 681 | if (CapturingScopeInfo *CSI = |
| 682 | dyn_cast<CapturingScopeInfo>(FunctionScopes[idx])) { |
| 683 | if (CSI->CXXThisCaptureIndex != 0) { |
| 684 | // 'this' is already being captured; there isn't anything more to do. |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 685 | break; |
| 686 | } |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 687 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_LambdaByref || |
| 688 | CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_Block) { |
| 689 | // This closure can implicitly capture 'this'; continue looking upwards. |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 690 | // FIXME: Is this check correct? The rules in the standard are a bit |
| 691 | // unclear. |
| 692 | NumClosures++; |
| 693 | continue; |
| 694 | } |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 695 | // This context can't implicitly capture 'this'; fail out. |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 696 | Diag(Loc, diag::err_implicit_this_capture); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 697 | return; |
| 698 | } |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 699 | break; |
| 700 | } |
| 701 | |
| 702 | // Mark that we're implicitly capturing 'this' in all the scopes we skipped. |
| 703 | // FIXME: We need to delay this marking in PotentiallyPotentiallyEvaluated |
| 704 | // contexts. |
| 705 | for (unsigned idx = FunctionScopes.size() - 1; |
| 706 | NumClosures; --idx, --NumClosures) { |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 707 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]); |
| 708 | bool isNested = NumClosures > 1; |
| 709 | CSI->AddThisCapture(isNested); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 713 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 714 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 715 | /// is a non-lvalue expression whose value is the address of the object for |
| 716 | /// which the function is called. |
| 717 | |
Douglas Gregor | 341350e | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 718 | QualType ThisTy = getCurrentThisType(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 719 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 720 | |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 721 | CheckCXXThisCapture(Loc); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 722 | return Owned(new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 723 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 724 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 725 | ExprResult |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 726 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 727 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 728 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 729 | SourceLocation RParenLoc) { |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 730 | if (!TypeRep) |
| 731 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 732 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 733 | TypeSourceInfo *TInfo; |
| 734 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 735 | if (!TInfo) |
| 736 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 737 | |
| 738 | return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); |
| 739 | } |
| 740 | |
| 741 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 742 | /// Can be interpreted either as function-style casting ("int(x)") |
| 743 | /// or class type construction ("ClassType(x,y,z)") |
| 744 | /// or creation of a value-initialized type ("int()"). |
| 745 | ExprResult |
| 746 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
| 747 | SourceLocation LParenLoc, |
| 748 | MultiExprArg exprs, |
| 749 | SourceLocation RParenLoc) { |
| 750 | QualType Ty = TInfo->getType(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 751 | unsigned NumExprs = exprs.size(); |
| 752 | Expr **Exprs = (Expr**)exprs.get(); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 753 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 754 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 755 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 756 | if (Ty->isDependentType() || |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 757 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 758 | exprs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 760 | return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo, |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 761 | LParenLoc, |
| 762 | Exprs, NumExprs, |
| 763 | RParenLoc)); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 766 | if (Ty->isArrayType()) |
| 767 | return ExprError(Diag(TyBeginLoc, |
| 768 | diag::err_value_init_for_array_type) << FullRange); |
| 769 | if (!Ty->isVoidType() && |
| 770 | RequireCompleteType(TyBeginLoc, Ty, |
| 771 | PDiag(diag::err_invalid_incomplete_type_use) |
| 772 | << FullRange)) |
| 773 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 774 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 775 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 776 | diag::err_allocation_of_abstract_type)) |
| 777 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | |
| 779 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 780 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 781 | // If the expression list is a single expression, the type conversion |
| 782 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 783 | // corresponding cast expression. |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 784 | if (NumExprs == 1) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 785 | Expr *Arg = Exprs[0]; |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 786 | exprs.release(); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 787 | return BuildCXXFunctionalCastExpr(TInfo, LParenLoc, Arg, RParenLoc); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 790 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 791 | InitializationKind Kind |
| 792 | = NumExprs ? InitializationKind::CreateDirect(TyBeginLoc, |
| 793 | LParenLoc, RParenLoc) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 794 | : InitializationKind::CreateValue(TyBeginLoc, |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 795 | LParenLoc, RParenLoc); |
| 796 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); |
| 797 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(exprs)); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 798 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 799 | // FIXME: Improve AST representation? |
| 800 | return move(Result); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 801 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 802 | |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 803 | /// doesUsualArrayDeleteWantSize - Answers whether the usual |
| 804 | /// operator delete[] for the given type has a size_t parameter. |
| 805 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
| 806 | QualType allocType) { |
| 807 | const RecordType *record = |
| 808 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 809 | if (!record) return false; |
| 810 | |
| 811 | // Try to find an operator delete[] in class scope. |
| 812 | |
| 813 | DeclarationName deleteName = |
| 814 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
| 815 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
| 816 | S.LookupQualifiedName(ops, record->getDecl()); |
| 817 | |
| 818 | // We're just doing this for information. |
| 819 | ops.suppressDiagnostics(); |
| 820 | |
| 821 | // Very likely: there's no operator delete[]. |
| 822 | if (ops.empty()) return false; |
| 823 | |
| 824 | // If it's ambiguous, it should be illegal to call operator delete[] |
| 825 | // on this thing, so it doesn't matter if we allocate extra space or not. |
| 826 | if (ops.isAmbiguous()) return false; |
| 827 | |
| 828 | LookupResult::Filter filter = ops.makeFilter(); |
| 829 | while (filter.hasNext()) { |
| 830 | NamedDecl *del = filter.next()->getUnderlyingDecl(); |
| 831 | |
| 832 | // C++0x [basic.stc.dynamic.deallocation]p2: |
| 833 | // A template instance is never a usual deallocation function, |
| 834 | // regardless of its signature. |
| 835 | if (isa<FunctionTemplateDecl>(del)) { |
| 836 | filter.erase(); |
| 837 | continue; |
| 838 | } |
| 839 | |
| 840 | // C++0x [basic.stc.dynamic.deallocation]p2: |
| 841 | // If class T does not declare [an operator delete[] with one |
| 842 | // parameter] but does declare a member deallocation function |
| 843 | // named operator delete[] with exactly two parameters, the |
| 844 | // second of which has type std::size_t, then this function |
| 845 | // is a usual deallocation function. |
| 846 | if (!cast<CXXMethodDecl>(del)->isUsualDeallocationFunction()) { |
| 847 | filter.erase(); |
| 848 | continue; |
| 849 | } |
| 850 | } |
| 851 | filter.done(); |
| 852 | |
| 853 | if (!ops.isSingleResult()) return false; |
| 854 | |
| 855 | const FunctionDecl *del = cast<FunctionDecl>(ops.getFoundDecl()); |
| 856 | return (del->getNumParams() == 2); |
| 857 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 858 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 859 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 860 | /// @code new (memory) int[size][4] @endcode |
| 861 | /// or |
| 862 | /// @code ::new Foo(23, "hello") @endcode |
| 863 | /// For the interpretation of this heap of arguments, consult the base version. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 864 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 865 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 866 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 867 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 868 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 869 | MultiExprArg ConstructorArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | SourceLocation ConstructorRParen) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 871 | bool TypeContainsAuto = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 872 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 873 | Expr *ArraySize = 0; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 874 | // If the specified type is an array, unwrap it and save the expression. |
| 875 | if (D.getNumTypeObjects() > 0 && |
| 876 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 877 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 878 | if (TypeContainsAuto) |
| 879 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
| 880 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 881 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 882 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 883 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 884 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 885 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 886 | << D.getSourceRange()); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 887 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 888 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 889 | D.DropFirstTypeObject(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 892 | // Every dimension shall be of constant size. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 893 | if (ArraySize) { |
| 894 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 895 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 896 | break; |
| 897 | |
| 898 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 899 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
| 900 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && |
| 901 | !NumElts->isIntegerConstantExpr(Context)) { |
| 902 | Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) |
| 903 | << NumElts->getSourceRange(); |
| 904 | return ExprError(); |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | } |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 909 | |
Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 910 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/0); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 911 | QualType AllocType = TInfo->getType(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 912 | if (D.isInvalidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 913 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 914 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | return BuildCXXNew(StartLoc, UseGlobal, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 916 | PlacementLParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | move(PlacementArgs), |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 918 | PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 919 | TypeIdParens, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 921 | TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 922 | ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 923 | ConstructorLParen, |
| 924 | move(ConstructorArgs), |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 925 | ConstructorRParen, |
| 926 | TypeContainsAuto); |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 927 | } |
| 928 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 929 | ExprResult |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 930 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 931 | SourceLocation PlacementLParen, |
| 932 | MultiExprArg PlacementArgs, |
| 933 | SourceLocation PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 934 | SourceRange TypeIdParens, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 935 | QualType AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 936 | TypeSourceInfo *AllocTypeInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 937 | Expr *ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 938 | SourceLocation ConstructorLParen, |
| 939 | MultiExprArg ConstructorArgs, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 940 | SourceLocation ConstructorRParen, |
| 941 | bool TypeMayContainAuto) { |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 942 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 943 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 944 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 945 | if (TypeMayContainAuto && AllocType->getContainedAutoType()) { |
| 946 | if (ConstructorArgs.size() == 0) |
| 947 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
| 948 | << AllocType << TypeRange); |
| 949 | if (ConstructorArgs.size() != 1) { |
| 950 | Expr *FirstBad = ConstructorArgs.get()[1]; |
| 951 | return ExprError(Diag(FirstBad->getSourceRange().getBegin(), |
| 952 | diag::err_auto_new_ctor_multiple_expressions) |
| 953 | << AllocType << TypeRange); |
| 954 | } |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 955 | TypeSourceInfo *DeducedType = 0; |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 956 | if (DeduceAutoType(AllocTypeInfo, ConstructorArgs.get()[0], DeducedType) == |
| 957 | DAR_Failed) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 958 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
| 959 | << AllocType |
| 960 | << ConstructorArgs.get()[0]->getType() |
| 961 | << TypeRange |
| 962 | << ConstructorArgs.get()[0]->getSourceRange()); |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 963 | if (!DeducedType) |
| 964 | return ExprError(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 965 | |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 966 | AllocTypeInfo = DeducedType; |
| 967 | AllocType = AllocTypeInfo->getType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 970 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 971 | // typedef of an array type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 972 | if (!ArraySize) { |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 973 | if (const ConstantArrayType *Array |
| 974 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 975 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 976 | Context.getSizeType(), |
| 977 | TypeRange.getEnd()); |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 978 | AllocType = Array->getElementType(); |
| 979 | } |
| 980 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 981 | |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 982 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 983 | return ExprError(); |
| 984 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 985 | // In ARC, infer 'retaining' for the allocated |
| 986 | if (getLangOptions().ObjCAutoRefCount && |
| 987 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 988 | AllocType->isObjCLifetimeType()) { |
| 989 | AllocType = Context.getLifetimeQualifiedType(AllocType, |
| 990 | AllocType->getObjCARCImplicitLifetime()); |
| 991 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 992 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 993 | QualType ResultType = Context.getPointerType(AllocType); |
| 994 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 995 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 996 | // or enumeration type with a non-negative value." |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 997 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 998 | ExprResult ConvertedSize = DefaultFunctionArrayLvalueConversion(ArraySize); |
John McCall | 806054d | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 999 | if (ConvertedSize.isInvalid()) |
| 1000 | return ExprError(); |
| 1001 | ArraySize = ConvertedSize.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1002 | |
John McCall | 806054d | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 1003 | ConvertedSize = ConvertToIntegralOrEnumerationType( |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1004 | StartLoc, ArraySize, |
| 1005 | PDiag(diag::err_array_size_not_integral), |
| 1006 | PDiag(diag::err_array_size_incomplete_type) |
| 1007 | << ArraySize->getSourceRange(), |
| 1008 | PDiag(diag::err_array_size_explicit_conversion), |
| 1009 | PDiag(diag::note_array_size_conversion), |
| 1010 | PDiag(diag::err_array_size_ambiguous_conversion), |
| 1011 | PDiag(diag::note_array_size_conversion), |
| 1012 | PDiag(getLangOptions().CPlusPlus0x ? |
| 1013 | diag::warn_cxx98_compat_array_size_conversion : |
| 1014 | diag::ext_array_size_conversion)); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1015 | if (ConvertedSize.isInvalid()) |
| 1016 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1017 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1018 | ArraySize = ConvertedSize.take(); |
John McCall | 806054d | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 1019 | QualType SizeType = ArraySize->getType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1020 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1021 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1022 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1023 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 1024 | // We don't care about special rules, so we tell the machinery it's not |
| 1025 | // evaluated - it gives us a result in more cases. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1026 | if (!ArraySize->isValueDependent()) { |
| 1027 | llvm::APSInt Value; |
| 1028 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 1029 | if (Value < llvm::APSInt( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1030 | llvm::APInt::getNullValue(Value.getBitWidth()), |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 1031 | Value.isUnsigned())) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1032 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1033 | diag::err_typecheck_negative_array_size) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1034 | << ArraySize->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1035 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1036 | if (!AllocType->isDependentType()) { |
| 1037 | unsigned ActiveSizeBits |
| 1038 | = ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
| 1039 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1040 | Diag(ArraySize->getSourceRange().getBegin(), |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1041 | diag::err_array_too_large) |
| 1042 | << Value.toString(10) |
| 1043 | << ArraySize->getSourceRange(); |
| 1044 | return ExprError(); |
| 1045 | } |
| 1046 | } |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1047 | } else if (TypeIdParens.isValid()) { |
| 1048 | // Can't have dynamic array size when the type-id is in parentheses. |
| 1049 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 1050 | << ArraySize->getSourceRange() |
| 1051 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 1052 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1053 | |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1054 | TypeIdParens = SourceRange(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1055 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1056 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1057 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1058 | // ARC: warn about ABI issues. |
| 1059 | if (getLangOptions().ObjCAutoRefCount) { |
| 1060 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
| 1061 | if (BaseAllocType.hasStrongOrWeakObjCLifetime()) |
| 1062 | Diag(StartLoc, diag::warn_err_new_delete_object_array) |
| 1063 | << 0 << BaseAllocType; |
| 1064 | } |
| 1065 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 1066 | // Note that we do *not* convert the argument in any way. It can |
| 1067 | // be signed, larger than size_t, whatever. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1068 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1069 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1070 | FunctionDecl *OperatorNew = 0; |
| 1071 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1072 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 1073 | unsigned NumPlaceArgs = PlacementArgs.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1074 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1075 | if (!AllocType->isDependentType() && |
| 1076 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && |
| 1077 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1078 | SourceRange(PlacementLParen, PlacementRParen), |
| 1079 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 1080 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1081 | return ExprError(); |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1082 | |
| 1083 | // If this is an array allocation, compute whether the usual array |
| 1084 | // deallocation function for the type has a size_t parameter. |
| 1085 | bool UsualArrayDeleteWantsSize = false; |
| 1086 | if (ArraySize && !AllocType->isDependentType()) |
| 1087 | UsualArrayDeleteWantsSize |
| 1088 | = doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); |
| 1089 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1090 | SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1091 | if (OperatorNew) { |
| 1092 | // Add default arguments, if any. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1093 | const FunctionProtoType *Proto = |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1094 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1095 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 1096 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1097 | |
Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1098 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1099 | Proto, 1, PlaceArgs, NumPlaceArgs, |
Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1100 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1101 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1102 | |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1103 | NumPlaceArgs = AllPlaceArgs.size(); |
| 1104 | if (NumPlaceArgs > 0) |
| 1105 | PlaceArgs = &AllPlaceArgs[0]; |
| 1106 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1107 | |
Nick Lewycky | fca84b2 | 2012-01-24 21:15:41 +0000 | [diff] [blame] | 1108 | // Warn if the type is over-aligned and is being allocated by global operator |
| 1109 | // new. |
| 1110 | if (OperatorNew && |
| 1111 | (OperatorNew->isImplicit() || |
| 1112 | getSourceManager().isInSystemHeader(OperatorNew->getLocStart()))) { |
| 1113 | if (unsigned Align = Context.getPreferredTypeAlign(AllocType.getTypePtr())){ |
| 1114 | unsigned SuitableAlign = Context.getTargetInfo().getSuitableAlign(); |
| 1115 | if (Align > SuitableAlign) |
| 1116 | Diag(StartLoc, diag::warn_overaligned_type) |
| 1117 | << AllocType |
| 1118 | << unsigned(Align / Context.getCharWidth()) |
| 1119 | << unsigned(SuitableAlign / Context.getCharWidth()); |
| 1120 | } |
| 1121 | } |
| 1122 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1123 | bool Init = ConstructorLParen.isValid(); |
| 1124 | // --- Choosing a constructor --- |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1125 | CXXConstructorDecl *Constructor = 0; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1126 | bool HadMultipleCandidates = false; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1127 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); |
| 1128 | unsigned NumConsArgs = ConstructorArgs.size(); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1129 | ASTOwningVector<Expr*> ConvertedConstructorArgs(*this); |
Eli Friedman | a8ce9ec | 2009-11-08 22:15:39 +0000 | [diff] [blame] | 1130 | |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1131 | // Array 'new' can't have any initializers. |
Anders Carlsson | 55cbd6e | 2010-05-16 16:24:20 +0000 | [diff] [blame] | 1132 | if (NumConsArgs && (ResultType->isArrayType() || ArraySize)) { |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1133 | SourceRange InitRange(ConsArgs[0]->getLocStart(), |
| 1134 | ConsArgs[NumConsArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1135 | |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1136 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 1137 | return ExprError(); |
| 1138 | } |
| 1139 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1140 | if (!AllocType->isDependentType() && |
| 1141 | !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { |
| 1142 | // C++0x [expr.new]p15: |
| 1143 | // A new-expression that creates an object of type T initializes that |
| 1144 | // object as follows: |
| 1145 | InitializationKind Kind |
| 1146 | // - If the new-initializer is omitted, the object is default- |
| 1147 | // initialized (8.5); if no initialization is performed, |
| 1148 | // the object has indeterminate value |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1149 | = !Init? InitializationKind::CreateDefault(TypeRange.getBegin()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1150 | // - Otherwise, the new-initializer is interpreted according to the |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1151 | // initialization rules of 8.5 for direct-initialization. |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1152 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1153 | ConstructorLParen, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1154 | ConstructorRParen); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1155 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1156 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1157 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1158 | InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1159 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1160 | move(ConstructorArgs)); |
| 1161 | if (FullInit.isInvalid()) |
| 1162 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1163 | |
| 1164 | // FullInit is our initializer; walk through it to determine if it's a |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1165 | // constructor call, which CXXNewExpr handles directly. |
| 1166 | if (Expr *FullInitExpr = (Expr *)FullInit.get()) { |
| 1167 | if (CXXBindTemporaryExpr *Binder |
| 1168 | = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr)) |
| 1169 | FullInitExpr = Binder->getSubExpr(); |
| 1170 | if (CXXConstructExpr *Construct |
| 1171 | = dyn_cast<CXXConstructExpr>(FullInitExpr)) { |
| 1172 | Constructor = Construct->getConstructor(); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1173 | HadMultipleCandidates = Construct->hadMultipleCandidates(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1174 | for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(), |
| 1175 | AEnd = Construct->arg_end(); |
| 1176 | A != AEnd; ++A) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 1177 | ConvertedConstructorArgs.push_back(*A); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1178 | } else { |
| 1179 | // Take the converted initializer. |
| 1180 | ConvertedConstructorArgs.push_back(FullInit.release()); |
| 1181 | } |
| 1182 | } else { |
| 1183 | // No initialization required. |
| 1184 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1185 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1186 | // Take the converted arguments and use them for the new expression. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1187 | NumConsArgs = ConvertedConstructorArgs.size(); |
| 1188 | ConsArgs = (Expr **)ConvertedConstructorArgs.take(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1189 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1191 | // Mark the new and delete operators as referenced. |
| 1192 | if (OperatorNew) |
| 1193 | MarkDeclarationReferenced(StartLoc, OperatorNew); |
| 1194 | if (OperatorDelete) |
| 1195 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
| 1196 | |
John McCall | 84ff0fc | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 1197 | // C++0x [expr.new]p17: |
| 1198 | // If the new expression creates an array of objects of class type, |
| 1199 | // access and ambiguity control are done for the destructor. |
| 1200 | if (ArraySize && Constructor) { |
| 1201 | if (CXXDestructorDecl *dtor = LookupDestructor(Constructor->getParent())) { |
| 1202 | MarkDeclarationReferenced(StartLoc, dtor); |
| 1203 | CheckDestructorAccess(StartLoc, dtor, |
| 1204 | PDiag(diag::err_access_dtor) |
| 1205 | << Context.getBaseElementType(AllocType)); |
| 1206 | } |
| 1207 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1208 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1209 | PlacementArgs.release(); |
| 1210 | ConstructorArgs.release(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1211 | |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1212 | return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1213 | PlaceArgs, NumPlaceArgs, TypeIdParens, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1214 | ArraySize, Constructor, Init, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1215 | ConsArgs, NumConsArgs, |
| 1216 | HadMultipleCandidates, |
| 1217 | OperatorDelete, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1218 | UsualArrayDeleteWantsSize, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1219 | ResultType, AllocTypeInfo, |
| 1220 | StartLoc, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1221 | Init ? ConstructorRParen : |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 1222 | TypeRange.getEnd(), |
| 1223 | ConstructorLParen, ConstructorRParen)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 1227 | /// in a new-expression. |
| 1228 | /// dimension off and stores the size expression in ArraySize. |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1229 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | SourceRange R) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1231 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 1232 | // abstract class type or array thereof. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1233 | if (AllocType->isFunctionType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1234 | return Diag(Loc, diag::err_bad_new_type) |
| 1235 | << AllocType << 0 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1236 | else if (AllocType->isReferenceType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1237 | return Diag(Loc, diag::err_bad_new_type) |
| 1238 | << AllocType << 1 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1239 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1240 | RequireCompleteType(Loc, AllocType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1241 | PDiag(diag::err_new_incomplete_type) |
| 1242 | << R)) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1243 | return true; |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1244 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1245 | diag::err_allocation_of_abstract_type)) |
| 1246 | return true; |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1247 | else if (AllocType->isVariablyModifiedType()) |
| 1248 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 1249 | << AllocType; |
Douglas Gregor | 5666d36 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1250 | else if (unsigned AddressSpace = AllocType.getAddressSpace()) |
| 1251 | return Diag(Loc, diag::err_address_space_qualified_new) |
| 1252 | << AllocType.getUnqualifiedType() << AddressSpace; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1253 | else if (getLangOptions().ObjCAutoRefCount) { |
| 1254 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { |
| 1255 | QualType BaseAllocType = Context.getBaseElementType(AT); |
| 1256 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 1257 | BaseAllocType->isObjCLifetimeType()) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 1258 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1259 | << BaseAllocType; |
| 1260 | } |
| 1261 | } |
Douglas Gregor | 5666d36 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1262 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1263 | return false; |
| 1264 | } |
| 1265 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1266 | /// \brief Determine whether the given function is a non-placement |
| 1267 | /// deallocation function. |
| 1268 | static bool isNonPlacementDeallocationFunction(FunctionDecl *FD) { |
| 1269 | if (FD->isInvalidDecl()) |
| 1270 | return false; |
| 1271 | |
| 1272 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 1273 | return Method->isUsualDeallocationFunction(); |
| 1274 | |
| 1275 | return ((FD->getOverloadedOperator() == OO_Delete || |
| 1276 | FD->getOverloadedOperator() == OO_Array_Delete) && |
| 1277 | FD->getNumParams() == 1); |
| 1278 | } |
| 1279 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1280 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 1281 | /// that are appropriate for the allocation. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1282 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 1283 | bool UseGlobal, QualType AllocType, |
| 1284 | bool IsArray, Expr **PlaceArgs, |
| 1285 | unsigned NumPlaceArgs, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1286 | FunctionDecl *&OperatorNew, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1288 | // --- Choosing an allocation function --- |
| 1289 | // C++ 5.3.4p8 - 14 & 18 |
| 1290 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 1291 | // in the scope of the allocated class. |
| 1292 | // 2) If an array size is given, look for operator new[], else look for |
| 1293 | // operator new. |
| 1294 | // 3) The first argument is always size_t. Append the arguments from the |
| 1295 | // placement form. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1296 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1297 | SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1298 | // We don't care about the actual value of this argument. |
| 1299 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 1300 | // tree? Or should the consumer just recalculate the value? |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1301 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1302 | Context.getTargetInfo().getPointerWidth(0)), |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1303 | Context.getSizeType(), |
| 1304 | SourceLocation()); |
| 1305 | AllocArgs[0] = &Size; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1306 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 1307 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1308 | // C++ [expr.new]p8: |
| 1309 | // If the allocated type is a non-array type, the allocation |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1310 | // function's name is operator new and the deallocation function's |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1311 | // name is operator delete. If the allocated type is an array |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1312 | // type, the allocation function's name is operator new[] and the |
| 1313 | // deallocation function's name is operator delete[]. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1314 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 1315 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1316 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1317 | IsArray ? OO_Array_Delete : OO_Delete); |
| 1318 | |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1319 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 1320 | |
| 1321 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | CXXRecordDecl *Record |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1323 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1324 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1325 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 1326 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1327 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1328 | } |
| 1329 | if (!OperatorNew) { |
| 1330 | // Didn't find a member overload. Look for a global one. |
| 1331 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1332 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1333 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1334 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 1335 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1336 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1339 | // We don't need an operator delete if we're running under |
| 1340 | // -fno-exceptions. |
| 1341 | if (!getLangOptions().Exceptions) { |
| 1342 | OperatorDelete = 0; |
| 1343 | return false; |
| 1344 | } |
| 1345 | |
Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 1346 | // FindAllocationOverload can change the passed in arguments, so we need to |
| 1347 | // copy them back. |
| 1348 | if (NumPlaceArgs > 0) |
| 1349 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1351 | // C++ [expr.new]p19: |
| 1352 | // |
| 1353 | // If the new-expression begins with a unary :: operator, the |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1354 | // deallocation function's name is looked up in the global |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1355 | // scope. Otherwise, if the allocated type is a class type T or an |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1356 | // array thereof, the deallocation function's name is looked up in |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1357 | // the scope of T. If this lookup fails to find the name, or if |
| 1358 | // the allocated type is not a class type or array thereof, the |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1359 | // deallocation function's name is looked up in the global scope. |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1360 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1361 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1362 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1363 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1364 | LookupQualifiedName(FoundDelete, RD); |
| 1365 | } |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1366 | if (FoundDelete.isAmbiguous()) |
| 1367 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1368 | |
| 1369 | if (FoundDelete.empty()) { |
| 1370 | DeclareGlobalNewDelete(); |
| 1371 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 1372 | } |
| 1373 | |
| 1374 | FoundDelete.suppressDiagnostics(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1376 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1377 | |
John McCall | edeb6c9 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 1378 | // Whether we're looking for a placement operator delete is dictated |
| 1379 | // by whether we selected a placement operator new, not by whether |
| 1380 | // we had explicit placement arguments. This matters for things like |
| 1381 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 1382 | // A *a = new A() |
| 1383 | bool isPlacementNew = (NumPlaceArgs > 0 || OperatorNew->param_size() != 1); |
| 1384 | |
| 1385 | if (isPlacementNew) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1386 | // C++ [expr.new]p20: |
| 1387 | // A declaration of a placement deallocation function matches the |
| 1388 | // declaration of a placement allocation function if it has the |
| 1389 | // same number of parameters and, after parameter transformations |
| 1390 | // (8.3.5), all parameter types except the first are |
| 1391 | // identical. [...] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1392 | // |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1393 | // To perform this comparison, we compute the function type that |
| 1394 | // the deallocation function should have, and use that type both |
| 1395 | // for template argument deduction and for comparison purposes. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1396 | // |
| 1397 | // FIXME: this comparison should ignore CC and the like. |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1398 | QualType ExpectedFunctionType; |
| 1399 | { |
| 1400 | const FunctionProtoType *Proto |
| 1401 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1402 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1403 | SmallVector<QualType, 4> ArgTypes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1404 | ArgTypes.push_back(Context.VoidPtrTy); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1405 | for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I) |
| 1406 | ArgTypes.push_back(Proto->getArgType(I)); |
| 1407 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1408 | FunctionProtoType::ExtProtoInfo EPI; |
| 1409 | EPI.Variadic = Proto->isVariadic(); |
| 1410 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1411 | ExpectedFunctionType |
| 1412 | = Context.getFunctionType(Context.VoidTy, ArgTypes.data(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1413 | ArgTypes.size(), EPI); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1416 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1417 | DEnd = FoundDelete.end(); |
| 1418 | D != DEnd; ++D) { |
| 1419 | FunctionDecl *Fn = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1420 | if (FunctionTemplateDecl *FnTmpl |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1421 | = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
| 1422 | // Perform template argument deduction to try to match the |
| 1423 | // expected function type. |
| 1424 | TemplateDeductionInfo Info(Context, StartLoc); |
| 1425 | if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info)) |
| 1426 | continue; |
| 1427 | } else |
| 1428 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 1429 | |
| 1430 | if (Context.hasSameType(Fn->getType(), ExpectedFunctionType)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1431 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1432 | } |
| 1433 | } else { |
| 1434 | // C++ [expr.new]p20: |
| 1435 | // [...] Any non-placement deallocation function matches a |
| 1436 | // non-placement allocation function. [...] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1437 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1438 | DEnd = FoundDelete.end(); |
| 1439 | D != DEnd; ++D) { |
| 1440 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl())) |
| 1441 | if (isNonPlacementDeallocationFunction(Fn)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1442 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | // C++ [expr.new]p20: |
| 1447 | // [...] If the lookup finds a single matching deallocation |
| 1448 | // function, that function will be called; otherwise, no |
| 1449 | // deallocation function will be called. |
| 1450 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1451 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1452 | |
| 1453 | // C++0x [expr.new]p20: |
| 1454 | // If the lookup finds the two-parameter form of a usual |
| 1455 | // deallocation function (3.7.4.2) and that function, considered |
| 1456 | // as a placement deallocation function, would have been |
| 1457 | // selected as a match for the allocation function, the program |
| 1458 | // is ill-formed. |
| 1459 | if (NumPlaceArgs && getLangOptions().CPlusPlus0x && |
| 1460 | isNonPlacementDeallocationFunction(OperatorDelete)) { |
| 1461 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1462 | << SourceRange(PlaceArgs[0]->getLocStart(), |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1463 | PlaceArgs[NumPlaceArgs - 1]->getLocEnd()); |
| 1464 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 1465 | << DeleteName; |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1466 | } else { |
| 1467 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1468 | Matches[0].first); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1469 | } |
| 1470 | } |
| 1471 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1472 | return false; |
| 1473 | } |
| 1474 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1475 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 1476 | /// function in the specified scope. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1477 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 1478 | DeclarationName Name, Expr** Args, |
| 1479 | unsigned NumArgs, DeclContext *Ctx, |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1480 | bool AllowMissing, FunctionDecl *&Operator, |
| 1481 | bool Diagnose) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1482 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); |
| 1483 | LookupQualifiedName(R, Ctx); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1484 | if (R.empty()) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1485 | if (AllowMissing || !Diagnose) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1486 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1487 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1488 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1491 | if (R.isAmbiguous()) |
| 1492 | return true; |
| 1493 | |
| 1494 | R.suppressDiagnostics(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1495 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1496 | OverloadCandidateSet Candidates(StartLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1497 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 1498 | Alloc != AllocEnd; ++Alloc) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1499 | // Even member operator new/delete are implicitly treated as |
| 1500 | // static, so don't use AddMemberCandidate. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1501 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1502 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1503 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 1504 | AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1505 | /*ExplicitTemplateArgs=*/0, Args, NumArgs, |
| 1506 | Candidates, |
| 1507 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1508 | continue; |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1511 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 1512 | AddOverloadCandidate(Fn, Alloc.getPair(), Args, NumArgs, Candidates, |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1513 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | // Do the resolution. |
| 1517 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1518 | switch (Candidates.BestViableFunction(*this, StartLoc, Best)) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1519 | case OR_Success: { |
| 1520 | // Got one! |
| 1521 | FunctionDecl *FnDecl = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 1522 | MarkDeclarationReferenced(StartLoc, FnDecl); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1523 | // The first argument is size_t, and the first parameter must be size_t, |
| 1524 | // too. This is checked on declaration and can be assumed. (It can't be |
| 1525 | // asserted on, though, since invalid decls are left in there.) |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1526 | // Watch out for variadic allocator function. |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1527 | unsigned NumArgsInFnDecl = FnDecl->getNumParams(); |
| 1528 | for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1529 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 1530 | FnDecl->getParamDecl(i)); |
| 1531 | |
| 1532 | if (!Diagnose && !CanPerformCopyInitialization(Entity, Owned(Args[i]))) |
| 1533 | return true; |
| 1534 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1535 | ExprResult Result |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1536 | = PerformCopyInitialization(Entity, SourceLocation(), Owned(Args[i])); |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1537 | if (Result.isInvalid()) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1538 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1539 | |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1540 | Args[i] = Result.takeAs<Expr>(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1541 | } |
| 1542 | Operator = FnDecl; |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1543 | CheckAllocationAccess(StartLoc, Range, R.getNamingClass(), Best->FoundDecl, |
| 1544 | Diagnose); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1545 | return false; |
| 1546 | } |
| 1547 | |
| 1548 | case OR_No_Viable_Function: |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1549 | if (Diagnose) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1550 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
| 1551 | << Name << Range; |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1552 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 1553 | } |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1554 | return true; |
| 1555 | |
| 1556 | case OR_Ambiguous: |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1557 | if (Diagnose) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1558 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
| 1559 | << Name << Range; |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1560 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
| 1561 | } |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1562 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1563 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1564 | case OR_Deleted: { |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1565 | if (Diagnose) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1566 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 1567 | << Best->Function->isDeleted() |
| 1568 | << Name |
| 1569 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 1570 | << Range; |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1571 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 1572 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1573 | return true; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1574 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1575 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1576 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1580 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 1581 | /// delete. These are: |
| 1582 | /// @code |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1583 | /// // C++03: |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1584 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 1585 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1586 | /// void operator delete(void *) throw(); |
| 1587 | /// void operator delete[](void *) throw(); |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1588 | /// // C++0x: |
| 1589 | /// void* operator new(std::size_t); |
| 1590 | /// void* operator new[](std::size_t); |
| 1591 | /// void operator delete(void *); |
| 1592 | /// void operator delete[](void *); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1593 | /// @endcode |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1594 | /// C++0x operator delete is implicitly noexcept. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1595 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 1596 | /// declared. Their use requires including \<new\>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1598 | if (GlobalNewDeleteDeclared) |
| 1599 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1600 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1601 | // C++ [basic.std.dynamic]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1602 | // [...] The following allocation and deallocation functions (18.4) are |
| 1603 | // implicitly declared in global scope in each translation unit of a |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1604 | // program |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1605 | // |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1606 | // C++03: |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1607 | // void* operator new(std::size_t) throw(std::bad_alloc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1608 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1609 | // void operator delete(void*) throw(); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1610 | // void operator delete[](void*) throw(); |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1611 | // C++0x: |
| 1612 | // void* operator new(std::size_t); |
| 1613 | // void* operator new[](std::size_t); |
| 1614 | // void operator delete(void*); |
| 1615 | // void operator delete[](void*); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1616 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1617 | // These implicit declarations introduce only the function names operator |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1618 | // new, operator new[], operator delete, operator delete[]. |
| 1619 | // |
| 1620 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 1621 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 1622 | // However, we do not make these implicit declarations visible to name |
| 1623 | // lookup. |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1624 | // Note that the C++0x versions of operator delete are deallocation functions, |
| 1625 | // and thus are implicitly noexcept. |
| 1626 | if (!StdBadAlloc && !getLangOptions().CPlusPlus0x) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1627 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 1628 | // implicitly. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1629 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
| 1630 | getOrCreateStdNamespace(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1631 | SourceLocation(), SourceLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1632 | &PP.getIdentifierTable().get("bad_alloc"), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1633 | 0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1634 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1635 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1636 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1637 | GlobalNewDeleteDeclared = true; |
| 1638 | |
| 1639 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 1640 | QualType SizeT = Context.getSizeType(); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1641 | bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1642 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1643 | DeclareGlobalAllocationFunction( |
| 1644 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1645 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1646 | DeclareGlobalAllocationFunction( |
| 1647 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1648 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1649 | DeclareGlobalAllocationFunction( |
| 1650 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 1651 | Context.VoidTy, VoidPtr); |
| 1652 | DeclareGlobalAllocationFunction( |
| 1653 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 1654 | Context.VoidTy, VoidPtr); |
| 1655 | } |
| 1656 | |
| 1657 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 1658 | /// allocation function if it doesn't already exist. |
| 1659 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1660 | QualType Return, QualType Argument, |
| 1661 | bool AddMallocAttr) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1662 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 1663 | |
| 1664 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1665 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 1666 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1667 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1668 | Alloc != AllocEnd; ++Alloc) { |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1669 | // Only look at non-template functions, as it is the predefined, |
| 1670 | // non-templated allocation function we are trying to declare here. |
| 1671 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
| 1672 | QualType InitialParamType = |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 1673 | Context.getCanonicalType( |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1674 | Func->getParamDecl(0)->getType().getUnqualifiedType()); |
| 1675 | // FIXME: Do we need to check for default arguments here? |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1676 | if (Func->getNumParams() == 1 && InitialParamType == Argument) { |
| 1677 | if(AddMallocAttr && !Func->hasAttr<MallocAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1678 | Func->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1679 | return; |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1680 | } |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1681 | } |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1685 | QualType BadAllocType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1686 | bool HasBadAllocExceptionSpec |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1687 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 1688 | Name.getCXXOverloadedOperator() == OO_Array_New); |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1689 | if (HasBadAllocExceptionSpec && !getLangOptions().CPlusPlus0x) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1690 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1691 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1692 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1693 | |
| 1694 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1695 | if (HasBadAllocExceptionSpec) { |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1696 | if (!getLangOptions().CPlusPlus0x) { |
| 1697 | EPI.ExceptionSpecType = EST_Dynamic; |
| 1698 | EPI.NumExceptions = 1; |
| 1699 | EPI.Exceptions = &BadAllocType; |
| 1700 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1701 | } else { |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1702 | EPI.ExceptionSpecType = getLangOptions().CPlusPlus0x ? |
| 1703 | EST_BasicNoexcept : EST_DynamicNone; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1704 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1705 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1706 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, EPI); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1707 | FunctionDecl *Alloc = |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1708 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), |
| 1709 | SourceLocation(), Name, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1710 | FnType, /*TInfo=*/0, SC_None, |
| 1711 | SC_None, false, true); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1712 | Alloc->setImplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1713 | |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1714 | if (AddMallocAttr) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1715 | Alloc->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1716 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1717 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1718 | SourceLocation(), 0, |
| 1719 | Argument, /*TInfo=*/0, |
| 1720 | SC_None, SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1721 | Alloc->setParams(Param); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1722 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1723 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 1724 | // make sure it is at the end of the chain to coincide with the |
| 1725 | // global scope. |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1726 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1729 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 1730 | DeclarationName Name, |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1731 | FunctionDecl* &Operator, bool Diagnose) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1732 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1733 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1734 | LookupQualifiedName(Found, RD); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1735 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1736 | if (Found.isAmbiguous()) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1737 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1738 | |
Chandler Carruth | 2389324 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 1739 | Found.suppressDiagnostics(); |
| 1740 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1741 | SmallVector<DeclAccessPair,4> Matches; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1742 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1743 | F != FEnd; ++F) { |
Chandler Carruth | 09556fd | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 1744 | NamedDecl *ND = (*F)->getUnderlyingDecl(); |
| 1745 | |
| 1746 | // Ignore template operator delete members from the check for a usual |
| 1747 | // deallocation function. |
| 1748 | if (isa<FunctionTemplateDecl>(ND)) |
| 1749 | continue; |
| 1750 | |
| 1751 | if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction()) |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1752 | Matches.push_back(F.getPair()); |
| 1753 | } |
| 1754 | |
| 1755 | // There's exactly one suitable operator; pick it. |
| 1756 | if (Matches.size() == 1) { |
| 1757 | Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl()); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1758 | |
| 1759 | if (Operator->isDeleted()) { |
| 1760 | if (Diagnose) { |
| 1761 | Diag(StartLoc, diag::err_deleted_function_use); |
| 1762 | Diag(Operator->getLocation(), diag::note_unavailable_here) << true; |
| 1763 | } |
| 1764 | return true; |
| 1765 | } |
| 1766 | |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1767 | CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1768 | Matches[0], Diagnose); |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1769 | return false; |
| 1770 | |
| 1771 | // We found multiple suitable operators; complain about the ambiguity. |
| 1772 | } else if (!Matches.empty()) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1773 | if (Diagnose) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1774 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 1775 | << Name << RD; |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1777 | for (SmallVectorImpl<DeclAccessPair>::iterator |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1778 | F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F) |
| 1779 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1780 | diag::note_member_declared_here) << Name; |
| 1781 | } |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1782 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | // We did find operator delete/operator delete[] declarations, but |
| 1786 | // none of them were suitable. |
| 1787 | if (!Found.empty()) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1788 | if (Diagnose) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1789 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 1790 | << Name << RD; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1791 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1792 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1793 | F != FEnd; ++F) |
| 1794 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1795 | diag::note_member_declared_here) << Name; |
| 1796 | } |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1797 | return true; |
| 1798 | } |
| 1799 | |
| 1800 | // Look for a global declaration. |
| 1801 | DeclareGlobalNewDelete(); |
| 1802 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1803 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1804 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); |
| 1805 | Expr* DeallocArgs[1]; |
| 1806 | DeallocArgs[0] = &Null; |
| 1807 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1808 | DeallocArgs, 1, TUDecl, !Diagnose, |
| 1809 | Operator, Diagnose)) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1810 | return true; |
| 1811 | |
| 1812 | assert(Operator && "Did not find a deallocation function!"); |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1816 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 1817 | /// @code ::delete ptr; @endcode |
| 1818 | /// or |
| 1819 | /// @code delete [] ptr; @endcode |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1820 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1821 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1822 | bool ArrayForm, Expr *ExE) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1823 | // C++ [expr.delete]p1: |
| 1824 | // The operand shall have a pointer type, or a class type having a single |
| 1825 | // conversion function to a pointer type. The result has type void. |
| 1826 | // |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1827 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 1828 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1829 | ExprResult Ex = Owned(ExE); |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1830 | FunctionDecl *OperatorDelete = 0; |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1831 | bool ArrayFormAsWritten = ArrayForm; |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1832 | bool UsualArrayDeleteWantsSize = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1834 | if (!Ex.get()->isTypeDependent()) { |
| 1835 | QualType Type = Ex.get()->getType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1837 | if (const RecordType *Record = Type->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1838 | if (RequireCompleteType(StartLoc, Type, |
Douglas Gregor | 254a942 | 2010-07-29 14:44:35 +0000 | [diff] [blame] | 1839 | PDiag(diag::err_delete_incomplete_class_type))) |
| 1840 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1841 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1842 | SmallVector<CXXConversionDecl*, 4> ObjectPtrConversions; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1843 | |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1844 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1845 | const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1846 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1847 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1848 | NamedDecl *D = I.getDecl(); |
| 1849 | if (isa<UsingShadowDecl>(D)) |
| 1850 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1851 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1852 | // Skip over templated conversion functions; they aren't considered. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1853 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1854 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1855 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1856 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1857 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1858 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 1859 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1860 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1861 | ObjectPtrConversions.push_back(Conv); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1862 | } |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1863 | if (ObjectPtrConversions.size() == 1) { |
| 1864 | // We have a single conversion to a pointer-to-object type. Perform |
| 1865 | // that conversion. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1866 | // TODO: don't redo the conversion calculation. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1867 | ExprResult Res = |
| 1868 | PerformImplicitConversion(Ex.get(), |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1869 | ObjectPtrConversions.front()->getConversionType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1870 | AA_Converting); |
| 1871 | if (Res.isUsable()) { |
| 1872 | Ex = move(Res); |
| 1873 | Type = Ex.get()->getType(); |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | else if (ObjectPtrConversions.size() > 1) { |
| 1877 | Diag(StartLoc, diag::err_ambiguous_delete_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1878 | << Type << Ex.get()->getSourceRange(); |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1879 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) |
| 1880 | NoteOverloadCandidate(ObjectPtrConversions[i]); |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1881 | return ExprError(); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1882 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1885 | if (!Type->isPointerType()) |
| 1886 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1887 | << Type << Ex.get()->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1888 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1889 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1890 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 1891 | |
| 1892 | if (unsigned AddressSpace = Pointee.getAddressSpace()) |
| 1893 | return Diag(Ex.get()->getLocStart(), |
| 1894 | diag::err_address_space_qualified_delete) |
| 1895 | << Pointee.getUnqualifiedType() << AddressSpace; |
| 1896 | |
| 1897 | CXXRecordDecl *PointeeRD = 0; |
Douglas Gregor | 94a6157 | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 1898 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1899 | // The C++ standard bans deleting a pointer to a non-object type, which |
Douglas Gregor | 94a6157 | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 1900 | // effectively bans deletion of "void*". However, most compilers support |
| 1901 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 1902 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1903 | << Type << Ex.get()->getSourceRange(); |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1904 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1905 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1906 | << Type << Ex.get()->getSourceRange()); |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1907 | } else if (!Pointee->isDependentType()) { |
| 1908 | if (!RequireCompleteType(StartLoc, Pointee, |
| 1909 | PDiag(diag::warn_delete_incomplete) |
| 1910 | << Ex.get()->getSourceRange())) { |
| 1911 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
| 1912 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1913 | } |
| 1914 | } |
| 1915 | |
Abramo Bagnara | 30bb420 | 2011-11-16 15:42:13 +0000 | [diff] [blame] | 1916 | // Perform lvalue-to-rvalue cast, if needed. |
| 1917 | Ex = DefaultLvalueConversion(Ex.take()); |
| 1918 | |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 1919 | // C++ [expr.delete]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1920 | // [Note: a pointer to a const type can be the operand of a |
| 1921 | // delete-expression; it is not necessary to cast away the constness |
| 1922 | // (5.2.11) of the pointer expression before it is used as the operand |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 1923 | // of the delete-expression. ] |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1924 | if (!Context.hasSameType(Ex.get()->getType(), Context.VoidPtrTy)) |
Abramo Bagnara | 30bb420 | 2011-11-16 15:42:13 +0000 | [diff] [blame] | 1925 | Ex = Owned(ImplicitCastExpr::Create(Context, Context.VoidPtrTy, |
| 1926 | CK_BitCast, Ex.take(), 0, VK_RValue)); |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1927 | |
| 1928 | if (Pointee->isArrayType() && !ArrayForm) { |
| 1929 | Diag(StartLoc, diag::warn_delete_array_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1930 | << Type << Ex.get()->getSourceRange() |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1931 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]"); |
| 1932 | ArrayForm = true; |
| 1933 | } |
| 1934 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1935 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1936 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 1937 | |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1938 | if (PointeeRD) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1939 | if (!UseGlobal && |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1940 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, |
| 1941 | OperatorDelete)) |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1942 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1943 | |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1944 | // If we're allocating an array of records, check whether the |
| 1945 | // usual operator delete[] has a size_t parameter. |
| 1946 | if (ArrayForm) { |
| 1947 | // If the user specifically asked to use the global allocator, |
| 1948 | // we'll need to do the lookup into the class. |
| 1949 | if (UseGlobal) |
| 1950 | UsualArrayDeleteWantsSize = |
| 1951 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); |
| 1952 | |
| 1953 | // Otherwise, the usual operator delete[] should be the |
| 1954 | // function we just found. |
| 1955 | else if (isa<CXXMethodDecl>(OperatorDelete)) |
| 1956 | UsualArrayDeleteWantsSize = (OperatorDelete->getNumParams() == 2); |
| 1957 | } |
| 1958 | |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1959 | if (!PointeeRD->hasTrivialDestructor()) |
| 1960 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | MarkDeclarationReferenced(StartLoc, |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1962 | const_cast<CXXDestructorDecl*>(Dtor)); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1963 | DiagnoseUseOfDecl(Dtor, StartLoc); |
| 1964 | } |
Argyrios Kyrtzidis | 6f0074a | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 1965 | |
| 1966 | // C++ [expr.delete]p3: |
| 1967 | // In the first alternative (delete object), if the static type of the |
| 1968 | // object to be deleted is different from its dynamic type, the static |
| 1969 | // type shall be a base class of the dynamic type of the object to be |
| 1970 | // deleted and the static type shall have a virtual destructor or the |
| 1971 | // behavior is undefined. |
| 1972 | // |
| 1973 | // Note: a final class cannot be derived from, no issue there |
Eli Friedman | ef8c79c | 2011-07-26 23:27:24 +0000 | [diff] [blame] | 1974 | if (PointeeRD->isPolymorphic() && !PointeeRD->hasAttr<FinalAttr>()) { |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1975 | CXXDestructorDecl *dtor = PointeeRD->getDestructor(); |
Eli Friedman | ef8c79c | 2011-07-26 23:27:24 +0000 | [diff] [blame] | 1976 | if (dtor && !dtor->isVirtual()) { |
| 1977 | if (PointeeRD->isAbstract()) { |
| 1978 | // If the class is abstract, we warn by default, because we're |
| 1979 | // sure the code has undefined behavior. |
| 1980 | Diag(StartLoc, diag::warn_delete_abstract_non_virtual_dtor) |
| 1981 | << PointeeElem; |
| 1982 | } else if (!ArrayForm) { |
| 1983 | // Otherwise, if this is not an array delete, it's a bit suspect, |
| 1984 | // but not necessarily wrong. |
| 1985 | Diag(StartLoc, diag::warn_delete_non_virtual_dtor) << PointeeElem; |
| 1986 | } |
| 1987 | } |
Argyrios Kyrtzidis | 6f0074a | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 1988 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1989 | |
| 1990 | } else if (getLangOptions().ObjCAutoRefCount && |
| 1991 | PointeeElem->isObjCLifetimeType() && |
| 1992 | (PointeeElem.getObjCLifetime() == Qualifiers::OCL_Strong || |
| 1993 | PointeeElem.getObjCLifetime() == Qualifiers::OCL_Weak) && |
| 1994 | ArrayForm) { |
| 1995 | Diag(StartLoc, diag::warn_err_new_delete_object_array) |
| 1996 | << 1 << PointeeElem; |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1997 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1998 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1999 | if (!OperatorDelete) { |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 2000 | // Look for a global declaration. |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2001 | DeclareGlobalNewDelete(); |
| 2002 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2003 | Expr *Arg = Ex.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2005 | &Arg, 1, TUDecl, /*AllowMissing=*/false, |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 2006 | OperatorDelete)) |
| 2007 | return ExprError(); |
| 2008 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 2010 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 2011 | |
Douglas Gregor | d880f52 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 2012 | // Check access and ambiguity of operator delete and destructor. |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 2013 | if (PointeeRD) { |
| 2014 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2015 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
Douglas Gregor | d880f52 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 2016 | PDiag(diag::err_access_dtor) << PointeeElem); |
| 2017 | } |
| 2018 | } |
| 2019 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2022 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 2023 | ArrayFormAsWritten, |
| 2024 | UsualArrayDeleteWantsSize, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2025 | OperatorDelete, Ex.take(), StartLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2028 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 2029 | /// while, do-while, or switch statement. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2030 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2031 | SourceLocation StmtLoc, |
| 2032 | bool ConvertToBoolean) { |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2033 | QualType T = ConditionVar->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2034 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2035 | // C++ [stmt.select]p2: |
| 2036 | // The declarator shall not specify a function or an array. |
| 2037 | if (T->isFunctionType()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2038 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2039 | diag::err_invalid_use_of_function_type) |
| 2040 | << ConditionVar->getSourceRange()); |
| 2041 | else if (T->isArrayType()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2042 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2043 | diag::err_invalid_use_of_array_type) |
| 2044 | << ConditionVar->getSourceRange()); |
Douglas Gregor | a7605db | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 2045 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2046 | ExprResult Condition = |
| 2047 | Owned(DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2048 | ConditionVar, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2049 | ConditionVar->getLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2050 | ConditionVar->getType().getNonReferenceType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2051 | VK_LValue)); |
Eli Friedman | cf7c14c | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 2052 | |
| 2053 | MarkDeclarationReferenced(ConditionVar->getLocation(), ConditionVar); |
| 2054 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2055 | if (ConvertToBoolean) { |
| 2056 | Condition = CheckBooleanCondition(Condition.take(), StmtLoc); |
| 2057 | if (Condition.isInvalid()) |
| 2058 | return ExprError(); |
| 2059 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2060 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2061 | return move(Condition); |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2064 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2065 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr) { |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2066 | // C++ 6.4p4: |
| 2067 | // The value of a condition that is an initialized declaration in a statement |
| 2068 | // other than a switch statement is the value of the declared variable |
| 2069 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 2070 | // program is ill-formed. |
| 2071 | // The value of a condition that is an expression is the value of the |
| 2072 | // expression, implicitly converted to bool. |
| 2073 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2074 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2075 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2076 | |
| 2077 | /// Helper function to determine whether this is the (deprecated) C++ |
| 2078 | /// conversion from a string literal to a pointer to non-const char or |
| 2079 | /// non-const wchar_t (for narrow and wide string literals, |
| 2080 | /// respectively). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | bool |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2082 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 2083 | // Look inside the implicit cast, if it exists. |
| 2084 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 2085 | From = Cast->getSubExpr(); |
| 2086 | |
| 2087 | // A string literal (2.13.4) that is not a wide string literal can |
| 2088 | // be converted to an rvalue of type "pointer to char"; a wide |
| 2089 | // string literal can be converted to an rvalue of type "pointer |
| 2090 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 1984eb9 | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 2091 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2092 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | if (const BuiltinType *ToPointeeType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2094 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2095 | // This conversion is considered only when there is an |
| 2096 | // explicit appropriate pointer target type (C++ 4.2p2). |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2097 | if (!ToPtrType->getPointeeType().hasQualifiers()) { |
| 2098 | switch (StrLit->getKind()) { |
| 2099 | case StringLiteral::UTF8: |
| 2100 | case StringLiteral::UTF16: |
| 2101 | case StringLiteral::UTF32: |
| 2102 | // We don't allow UTF literals to be implicitly converted |
| 2103 | break; |
| 2104 | case StringLiteral::Ascii: |
| 2105 | return (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 2106 | ToPointeeType->getKind() == BuiltinType::Char_S); |
| 2107 | case StringLiteral::Wide: |
| 2108 | return ToPointeeType->isWideCharType(); |
| 2109 | } |
| 2110 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | return false; |
| 2114 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2115 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2116 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2117 | SourceLocation CastLoc, |
| 2118 | QualType Ty, |
| 2119 | CastKind Kind, |
| 2120 | CXXMethodDecl *Method, |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2121 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2122 | bool HadMultipleCandidates, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2123 | Expr *From) { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2124 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2125 | default: llvm_unreachable("Unhandled cast kind!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2126 | case CK_ConstructorConversion: { |
Douglas Gregor | 13e1bca | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2127 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2128 | ASTOwningVector<Expr*> ConstructorArgs(S); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2129 | |
Douglas Gregor | 13e1bca | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2130 | if (S.CompleteConstructorCall(Constructor, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2131 | MultiExprArg(&From, 1), |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2132 | CastLoc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2133 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2134 | |
Douglas Gregor | 13e1bca | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2135 | S.CheckConstructorAccess(CastLoc, Constructor, Constructor->getAccess(), |
| 2136 | S.PDiag(diag::err_access_ctor)); |
| 2137 | |
| 2138 | ExprResult Result |
| 2139 | = S.BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), |
| 2140 | move_arg(ConstructorArgs), |
| 2141 | HadMultipleCandidates, /*ZeroInit*/ false, |
| 2142 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2143 | if (Result.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2144 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2146 | return S.MaybeBindToTemporary(Result.takeAs<Expr>()); |
| 2147 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2148 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2149 | case CK_UserDefinedConversion: { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2150 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2151 | |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2152 | // Create an implicit call expr that calls it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2153 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Method, |
| 2154 | HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2155 | if (Result.isInvalid()) |
| 2156 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 2157 | // Record usage of conversion in an implicit cast. |
| 2158 | Result = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 2159 | Result.get()->getType(), |
| 2160 | CK_UserDefinedConversion, |
| 2161 | Result.get(), 0, |
| 2162 | Result.get()->getValueKind())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2163 | |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2164 | S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ 0, FoundDecl); |
| 2165 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2166 | return S.MaybeBindToTemporary(Result.get()); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2167 | } |
| 2168 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2169 | } |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2171 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 2172 | /// expression From to the type ToType using the pre-computed implicit |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2173 | /// conversion sequence ICS. Returns the converted |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2174 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2175 | /// used in the error message. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2176 | ExprResult |
| 2177 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2178 | const ImplicitConversionSequence &ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2179 | AssignmentAction Action, |
| 2180 | CheckedConversionKind CCK) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2181 | switch (ICS.getKind()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2182 | case ImplicitConversionSequence::StandardConversion: { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2183 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, |
| 2184 | Action, CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2185 | if (Res.isInvalid()) |
| 2186 | return ExprError(); |
| 2187 | From = Res.take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2188 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2189 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2190 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2191 | case ImplicitConversionSequence::UserDefinedConversion: { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2192 | |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 2193 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2194 | CastKind CastKind; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2195 | QualType BeforeToType; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 2196 | assert(FD && "FIXME: aggregate initialization from init list"); |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2197 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2198 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2199 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2200 | // If the user-defined conversion is specified by a conversion function, |
| 2201 | // the initial standard conversion sequence converts the source type to |
| 2202 | // the implicit object parameter of the conversion function. |
| 2203 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
John McCall | 9ec9445 | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 2204 | } else { |
| 2205 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2206 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2207 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2208 | if (!ICS.UserDefined.EllipsisConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2209 | // If the user-defined conversion is specified by a constructor, the |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2210 | // initial standard conversion sequence converts the source type to the |
| 2211 | // type required by the argument of the constructor |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2212 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 2213 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2214 | } |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2215 | // Watch out for elipsis conversion. |
Fariborz Jahanian | 4c0cea2 | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 2216 | if (!ICS.UserDefined.EllipsisConversion) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2217 | ExprResult Res = |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2218 | PerformImplicitConversion(From, BeforeToType, |
| 2219 | ICS.UserDefined.Before, AA_Converting, |
| 2220 | CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2221 | if (Res.isInvalid()) |
| 2222 | return ExprError(); |
| 2223 | From = Res.take(); |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2224 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2225 | |
| 2226 | ExprResult CastArg |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2227 | = BuildCXXCastArgument(*this, |
| 2228 | From->getLocStart(), |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2229 | ToType.getNonReferenceType(), |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2230 | CastKind, cast<CXXMethodDecl>(FD), |
| 2231 | ICS.UserDefined.FoundConversionFunction, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2232 | ICS.UserDefined.HadMultipleCandidates, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2233 | From); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2234 | |
| 2235 | if (CastArg.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2236 | return ExprError(); |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2237 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2238 | From = CastArg.take(); |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2239 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2240 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
| 2241 | AA_Converting, CCK); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 2242 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2243 | |
| 2244 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2245 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2246 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 2247 | << From->getSourceRange()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2248 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2249 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2250 | case ImplicitConversionSequence::EllipsisConversion: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2251 | llvm_unreachable("Cannot perform an ellipsis conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2252 | |
| 2253 | case ImplicitConversionSequence::BadConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2254 | return ExprError(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | // Everything went well. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2258 | return Owned(From); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2261 | /// PerformImplicitConversion - Perform an implicit conversion of the |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2262 | /// expression From to the type ToType by following the standard |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2263 | /// conversion sequence SCS. Returns the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2264 | /// expression. Flavor is the context in which we're performing this |
| 2265 | /// conversion, for use in error messages. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2266 | ExprResult |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2267 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2268 | const StandardConversionSequence& SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2269 | AssignmentAction Action, |
| 2270 | CheckedConversionKind CCK) { |
| 2271 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
| 2272 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2273 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 2274 | // much extra work. What this means is that we need to keep track of more |
| 2275 | // information that is computed when we try the implicit conversion initially, |
| 2276 | // so that we don't need to recompute anything here. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2277 | QualType FromType = From->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2278 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2279 | if (SCS.CopyConstructor) { |
Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 2280 | // FIXME: When can ToType be a reference type? |
| 2281 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2282 | if (SCS.Second == ICK_Derived_To_Base) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2283 | ASTOwningVector<Expr*> ConstructorArgs(*this); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2284 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2285 | MultiExprArg(*this, &From, 1), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2286 | /*FIXME:ConstructLoc*/SourceLocation(), |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2287 | ConstructorArgs)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2288 | return ExprError(); |
| 2289 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 2290 | ToType, SCS.CopyConstructor, |
| 2291 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2292 | /*HadMultipleCandidates*/ false, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2293 | /*ZeroInit*/ false, |
| 2294 | CXXConstructExpr::CK_Complete, |
| 2295 | SourceRange()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2296 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2297 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 2298 | ToType, SCS.CopyConstructor, |
| 2299 | MultiExprArg(*this, &From, 1), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2300 | /*HadMultipleCandidates*/ false, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2301 | /*ZeroInit*/ false, |
| 2302 | CXXConstructExpr::CK_Complete, |
| 2303 | SourceRange()); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2306 | // Resolve overloaded function references. |
| 2307 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 2308 | DeclAccessPair Found; |
| 2309 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 2310 | true, Found); |
| 2311 | if (!Fn) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2312 | return ExprError(); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2313 | |
| 2314 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2315 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2316 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2317 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 2318 | FromType = From->getType(); |
| 2319 | } |
| 2320 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2321 | // Perform the first implicit conversion. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2322 | switch (SCS.First) { |
| 2323 | case ICK_Identity: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2324 | // Nothing to do. |
| 2325 | break; |
| 2326 | |
Eli Friedman | d814eaf | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 2327 | case ICK_Lvalue_To_Rvalue: { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 2328 | assert(From->getObjectKind() != OK_ObjCProperty); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2329 | FromType = FromType.getUnqualifiedType(); |
Eli Friedman | d814eaf | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 2330 | ExprResult FromRes = DefaultLvalueConversion(From); |
| 2331 | assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!"); |
| 2332 | From = FromRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2333 | break; |
Eli Friedman | d814eaf | 2012-01-24 22:51:26 +0000 | [diff] [blame] | 2334 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2335 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2336 | case ICK_Array_To_Pointer: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2337 | FromType = Context.getArrayDecayedType(FromType); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2338 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, |
| 2339 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2340 | break; |
| 2341 | |
| 2342 | case ICK_Function_To_Pointer: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2343 | FromType = Context.getPointerType(FromType); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2344 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, |
| 2345 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2346 | break; |
| 2347 | |
| 2348 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2349 | llvm_unreachable("Improper first standard conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2352 | // Perform the second implicit conversion |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2353 | switch (SCS.Second) { |
| 2354 | case ICK_Identity: |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2355 | // If both sides are functions (or pointers/references to them), there could |
| 2356 | // be incompatible exception declarations. |
| 2357 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2358 | return ExprError(); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2359 | // Nothing else to do. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2360 | break; |
| 2361 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2362 | case ICK_NoReturn_Adjustment: |
| 2363 | // If both sides are functions (or pointers/references to them), there could |
| 2364 | // be incompatible exception declarations. |
| 2365 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2366 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2367 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2368 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 2369 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2370 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2371 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2372 | case ICK_Integral_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2373 | case ICK_Integral_Conversion: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2374 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, |
| 2375 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2376 | break; |
| 2377 | |
| 2378 | case ICK_Floating_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2379 | case ICK_Floating_Conversion: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2380 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, |
| 2381 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2382 | break; |
| 2383 | |
| 2384 | case ICK_Complex_Promotion: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2385 | case ICK_Complex_Conversion: { |
| 2386 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
| 2387 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
| 2388 | CastKind CK; |
| 2389 | if (FromEl->isRealFloatingType()) { |
| 2390 | if (ToEl->isRealFloatingType()) |
| 2391 | CK = CK_FloatingComplexCast; |
| 2392 | else |
| 2393 | CK = CK_FloatingComplexToIntegralComplex; |
| 2394 | } else if (ToEl->isRealFloatingType()) { |
| 2395 | CK = CK_IntegralComplexToFloatingComplex; |
| 2396 | } else { |
| 2397 | CK = CK_IntegralComplexCast; |
| 2398 | } |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2399 | From = ImpCastExprToType(From, ToType, CK, |
| 2400 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2401 | break; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2402 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2403 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2404 | case ICK_Floating_Integral: |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 2405 | if (ToType->isRealFloatingType()) |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2406 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, |
| 2407 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2408 | else |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2409 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, |
| 2410 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2413 | case ICK_Compatible_Conversion: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2414 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 2415 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2416 | break; |
| 2417 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2418 | case ICK_Writeback_Conversion: |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2419 | case ICK_Pointer_Conversion: { |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2420 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2421 | // Diagnose incompatible Objective-C conversions |
Douglas Gregor | 8cf0d22 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 2422 | if (Action == AA_Initializing || Action == AA_Assigning) |
Fariborz Jahanian | 84950c7 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2423 | Diag(From->getSourceRange().getBegin(), |
| 2424 | diag::ext_typecheck_convert_incompatible_pointer) |
| 2425 | << ToType << From->getType() << Action |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 2426 | << From->getSourceRange() << 0; |
Fariborz Jahanian | 84950c7 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2427 | else |
| 2428 | Diag(From->getSourceRange().getBegin(), |
| 2429 | diag::ext_typecheck_convert_incompatible_pointer) |
| 2430 | << From->getType() << ToType << Action |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 2431 | << From->getSourceRange() << 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2432 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2433 | if (From->getType()->isObjCObjectPointerType() && |
| 2434 | ToType->isObjCObjectPointerType()) |
| 2435 | EmitRelatedResultTypeNote(From); |
Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2436 | } |
| 2437 | else if (getLangOptions().ObjCAutoRefCount && |
| 2438 | !CheckObjCARCUnavailableWeakConversion(ToType, |
| 2439 | From->getType())) { |
John McCall | 7f3a6d3 | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 2440 | if (Action == AA_Initializing) |
| 2441 | Diag(From->getSourceRange().getBegin(), |
| 2442 | diag::err_arc_weak_unavailable_assign); |
| 2443 | else |
| 2444 | Diag(From->getSourceRange().getBegin(), |
| 2445 | diag::err_arc_convesion_of_weak_unavailable) |
| 2446 | << (Action == AA_Casting) << From->getType() << ToType |
| 2447 | << From->getSourceRange(); |
| 2448 | } |
Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2449 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2450 | CastKind Kind = CK_Invalid; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2451 | CXXCastPath BasePath; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2452 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2453 | return ExprError(); |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2454 | |
| 2455 | // Make sure we extend blocks if necessary. |
| 2456 | // FIXME: doing this here is really ugly. |
| 2457 | if (Kind == CK_BlockPointerToObjCPointerCast) { |
| 2458 | ExprResult E = From; |
| 2459 | (void) PrepareCastToObjCObjectPointer(E); |
| 2460 | From = E.take(); |
| 2461 | } |
| 2462 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2463 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
| 2464 | .take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2465 | break; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2466 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2467 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2468 | case ICK_Pointer_Member: { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2469 | CastKind Kind = CK_Invalid; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2470 | CXXCastPath BasePath; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2471 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2472 | return ExprError(); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2473 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2474 | return ExprError(); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2475 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
| 2476 | .take(); |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2477 | break; |
| 2478 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2479 | |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 2480 | case ICK_Boolean_Conversion: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2481 | // Perform half-to-boolean conversion via float. |
| 2482 | if (From->getType()->isHalfType()) { |
| 2483 | From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).take(); |
| 2484 | FromType = Context.FloatTy; |
| 2485 | } |
| 2486 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2487 | From = ImpCastExprToType(From, Context.BoolTy, |
| 2488 | ScalarTypeToBooleanCastKind(FromType), |
| 2489 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2490 | break; |
| 2491 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2492 | case ICK_Derived_To_Base: { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2493 | CXXCastPath BasePath; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2494 | if (CheckDerivedToBaseConversion(From->getType(), |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2495 | ToType.getNonReferenceType(), |
| 2496 | From->getLocStart(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2497 | From->getSourceRange(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2498 | &BasePath, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2499 | CStyle)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2500 | return ExprError(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2501 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2502 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 2503 | CK_DerivedToBase, From->getValueKind(), |
| 2504 | &BasePath, CCK).take(); |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2505 | break; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2508 | case ICK_Vector_Conversion: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2509 | From = ImpCastExprToType(From, ToType, CK_BitCast, |
| 2510 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2511 | break; |
| 2512 | |
| 2513 | case ICK_Vector_Splat: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2514 | From = ImpCastExprToType(From, ToType, CK_VectorSplat, |
| 2515 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2516 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2517 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2518 | case ICK_Complex_Real: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2519 | // Case 1. x -> _Complex y |
| 2520 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
| 2521 | QualType ElType = ToComplex->getElementType(); |
| 2522 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2523 | |
| 2524 | // x -> y |
| 2525 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
| 2526 | // do nothing |
| 2527 | } else if (From->getType()->isRealFloatingType()) { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2528 | From = ImpCastExprToType(From, ElType, |
| 2529 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2530 | } else { |
| 2531 | assert(From->getType()->isIntegerType()); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2532 | From = ImpCastExprToType(From, ElType, |
| 2533 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2534 | } |
| 2535 | // y -> _Complex y |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2536 | From = ImpCastExprToType(From, ToType, |
| 2537 | isFloatingComplex ? CK_FloatingRealToComplex |
| 2538 | : CK_IntegralRealToComplex).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2539 | |
| 2540 | // Case 2. _Complex x -> y |
| 2541 | } else { |
| 2542 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
| 2543 | assert(FromComplex); |
| 2544 | |
| 2545 | QualType ElType = FromComplex->getElementType(); |
| 2546 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2547 | |
| 2548 | // _Complex x -> x |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2549 | From = ImpCastExprToType(From, ElType, |
| 2550 | isFloatingComplex ? CK_FloatingComplexToReal |
| 2551 | : CK_IntegralComplexToReal, |
| 2552 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2553 | |
| 2554 | // x -> y |
| 2555 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
| 2556 | // do nothing |
| 2557 | } else if (ToType->isRealFloatingType()) { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2558 | From = ImpCastExprToType(From, ToType, |
| 2559 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, |
| 2560 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2561 | } else { |
| 2562 | assert(ToType->isIntegerType()); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2563 | From = ImpCastExprToType(From, ToType, |
| 2564 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, |
| 2565 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2566 | } |
| 2567 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2568 | break; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2569 | |
| 2570 | case ICK_Block_Pointer_Conversion: { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2571 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, |
| 2572 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2573 | break; |
| 2574 | } |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2575 | |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2576 | case ICK_TransparentUnionConversion: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2577 | ExprResult FromRes = Owned(From); |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2578 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2579 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); |
| 2580 | if (FromRes.isInvalid()) |
| 2581 | return ExprError(); |
| 2582 | From = FromRes.take(); |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2583 | assert ((ConvTy == Sema::Compatible) && |
| 2584 | "Improper transparent union conversion"); |
| 2585 | (void)ConvTy; |
| 2586 | break; |
| 2587 | } |
| 2588 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2589 | case ICK_Lvalue_To_Rvalue: |
| 2590 | case ICK_Array_To_Pointer: |
| 2591 | case ICK_Function_To_Pointer: |
| 2592 | case ICK_Qualification: |
| 2593 | case ICK_Num_Conversion_Kinds: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2594 | llvm_unreachable("Improper second standard conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | switch (SCS.Third) { |
| 2598 | case ICK_Identity: |
| 2599 | // Nothing to do. |
| 2600 | break; |
| 2601 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2602 | case ICK_Qualification: { |
| 2603 | // The qualification keeps the category of the inner expression, unless the |
| 2604 | // target type isn't a reference. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2605 | ExprValueKind VK = ToType->isReferenceType() ? |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 2606 | From->getValueKind() : VK_RValue; |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2607 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
| 2608 | CK_NoOp, VK, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2609 | |
Douglas Gregor | 069a6da | 2011-03-14 16:13:32 +0000 | [diff] [blame] | 2610 | if (SCS.DeprecatedStringLiteralToCharPtr && |
| 2611 | !getLangOptions().WritableStrings) |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2612 | Diag(From->getLocStart(), diag::warn_deprecated_string_literal_conversion) |
| 2613 | << ToType.getNonReferenceType(); |
| 2614 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2615 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2618 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2619 | llvm_unreachable("Improper third standard conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2622 | return Owned(From); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2625 | ExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2626 | SourceLocation KWLoc, |
| 2627 | ParsedType Ty, |
| 2628 | SourceLocation RParen) { |
| 2629 | TypeSourceInfo *TSInfo; |
| 2630 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2631 | |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2632 | if (!TSInfo) |
| 2633 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2634 | return BuildUnaryTypeTrait(UTT, KWLoc, TSInfo, RParen); |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2637 | /// \brief Check the completeness of a type in a unary type trait. |
| 2638 | /// |
| 2639 | /// If the particular type trait requires a complete type, tries to complete |
| 2640 | /// it. If completing the type fails, a diagnostic is emitted and false |
| 2641 | /// returned. If completing the type succeeds or no completion was required, |
| 2642 | /// returns true. |
| 2643 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, |
| 2644 | UnaryTypeTrait UTT, |
| 2645 | SourceLocation Loc, |
| 2646 | QualType ArgTy) { |
| 2647 | // C++0x [meta.unary.prop]p3: |
| 2648 | // For all of the class templates X declared in this Clause, instantiating |
| 2649 | // that template with a template argument that is a class template |
| 2650 | // specialization may result in the implicit instantiation of the template |
| 2651 | // argument if and only if the semantics of X require that the argument |
| 2652 | // must be a complete type. |
| 2653 | // We apply this rule to all the type trait expressions used to implement |
| 2654 | // these class templates. We also try to follow any GCC documented behavior |
| 2655 | // in these expressions to ensure portability of standard libraries. |
| 2656 | switch (UTT) { |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2657 | // is_complete_type somewhat obviously cannot require a complete type. |
| 2658 | case UTT_IsCompleteType: |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2659 | // Fall-through |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2660 | |
| 2661 | // These traits are modeled on the type predicates in C++0x |
| 2662 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as |
| 2663 | // requiring a complete type, as whether or not they return true cannot be |
| 2664 | // impacted by the completeness of the type. |
| 2665 | case UTT_IsVoid: |
| 2666 | case UTT_IsIntegral: |
| 2667 | case UTT_IsFloatingPoint: |
| 2668 | case UTT_IsArray: |
| 2669 | case UTT_IsPointer: |
| 2670 | case UTT_IsLvalueReference: |
| 2671 | case UTT_IsRvalueReference: |
| 2672 | case UTT_IsMemberFunctionPointer: |
| 2673 | case UTT_IsMemberObjectPointer: |
| 2674 | case UTT_IsEnum: |
| 2675 | case UTT_IsUnion: |
| 2676 | case UTT_IsClass: |
| 2677 | case UTT_IsFunction: |
| 2678 | case UTT_IsReference: |
| 2679 | case UTT_IsArithmetic: |
| 2680 | case UTT_IsFundamental: |
| 2681 | case UTT_IsObject: |
| 2682 | case UTT_IsScalar: |
| 2683 | case UTT_IsCompound: |
| 2684 | case UTT_IsMemberPointer: |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2685 | // Fall-through |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2686 | |
| 2687 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] |
| 2688 | // which requires some of its traits to have the complete type. However, |
| 2689 | // the completeness of the type cannot impact these traits' semantics, and |
| 2690 | // so they don't require it. This matches the comments on these traits in |
| 2691 | // Table 49. |
| 2692 | case UTT_IsConst: |
| 2693 | case UTT_IsVolatile: |
| 2694 | case UTT_IsSigned: |
| 2695 | case UTT_IsUnsigned: |
| 2696 | return true; |
| 2697 | |
| 2698 | // C++0x [meta.unary.prop] Table 49 requires the following traits to be |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2699 | // applied to a complete type. |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2700 | case UTT_IsTrivial: |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2701 | case UTT_IsTriviallyCopyable: |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2702 | case UTT_IsStandardLayout: |
| 2703 | case UTT_IsPOD: |
| 2704 | case UTT_IsLiteral: |
| 2705 | case UTT_IsEmpty: |
| 2706 | case UTT_IsPolymorphic: |
| 2707 | case UTT_IsAbstract: |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2708 | // Fall-through |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2709 | |
Douglas Gregor | 5e9392b | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 2710 | // These traits require a complete type. |
| 2711 | case UTT_IsFinal: |
| 2712 | |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2713 | // These trait expressions are designed to help implement predicates in |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2714 | // [meta.unary.prop] despite not being named the same. They are specified |
| 2715 | // by both GCC and the Embarcadero C++ compiler, and require the complete |
| 2716 | // type due to the overarching C++0x type predicates being implemented |
| 2717 | // requiring the complete type. |
| 2718 | case UTT_HasNothrowAssign: |
| 2719 | case UTT_HasNothrowConstructor: |
| 2720 | case UTT_HasNothrowCopy: |
| 2721 | case UTT_HasTrivialAssign: |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2722 | case UTT_HasTrivialDefaultConstructor: |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2723 | case UTT_HasTrivialCopy: |
| 2724 | case UTT_HasTrivialDestructor: |
| 2725 | case UTT_HasVirtualDestructor: |
| 2726 | // Arrays of unknown bound are expressly allowed. |
| 2727 | QualType ElTy = ArgTy; |
| 2728 | if (ArgTy->isIncompleteArrayType()) |
| 2729 | ElTy = S.Context.getAsArrayType(ArgTy)->getElementType(); |
| 2730 | |
| 2731 | // The void type is expressly allowed. |
| 2732 | if (ElTy->isVoidType()) |
| 2733 | return true; |
| 2734 | |
| 2735 | return !S.RequireCompleteType( |
| 2736 | Loc, ElTy, diag::err_incomplete_type_used_in_type_trait_expr); |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2737 | } |
Chandler Carruth | 73e0a91 | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 2738 | llvm_unreachable("Type trait not handled by switch"); |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | static bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT, |
| 2742 | SourceLocation KeyLoc, QualType T) { |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 2743 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2744 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2745 | ASTContext &C = Self.Context; |
| 2746 | switch(UTT) { |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2747 | // Type trait expressions corresponding to the primary type category |
| 2748 | // predicates in C++0x [meta.unary.cat]. |
| 2749 | case UTT_IsVoid: |
| 2750 | return T->isVoidType(); |
| 2751 | case UTT_IsIntegral: |
| 2752 | return T->isIntegralType(C); |
| 2753 | case UTT_IsFloatingPoint: |
| 2754 | return T->isFloatingType(); |
| 2755 | case UTT_IsArray: |
| 2756 | return T->isArrayType(); |
| 2757 | case UTT_IsPointer: |
| 2758 | return T->isPointerType(); |
| 2759 | case UTT_IsLvalueReference: |
| 2760 | return T->isLValueReferenceType(); |
| 2761 | case UTT_IsRvalueReference: |
| 2762 | return T->isRValueReferenceType(); |
| 2763 | case UTT_IsMemberFunctionPointer: |
| 2764 | return T->isMemberFunctionPointerType(); |
| 2765 | case UTT_IsMemberObjectPointer: |
| 2766 | return T->isMemberDataPointerType(); |
| 2767 | case UTT_IsEnum: |
| 2768 | return T->isEnumeralType(); |
Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2769 | case UTT_IsUnion: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2770 | return T->isUnionType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2771 | case UTT_IsClass: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2772 | return T->isClassType() || T->isStructureType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2773 | case UTT_IsFunction: |
| 2774 | return T->isFunctionType(); |
| 2775 | |
| 2776 | // Type trait expressions which correspond to the convenient composition |
| 2777 | // predicates in C++0x [meta.unary.comp]. |
| 2778 | case UTT_IsReference: |
| 2779 | return T->isReferenceType(); |
| 2780 | case UTT_IsArithmetic: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2781 | return T->isArithmeticType() && !T->isEnumeralType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2782 | case UTT_IsFundamental: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2783 | return T->isFundamentalType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2784 | case UTT_IsObject: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2785 | return T->isObjectType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2786 | case UTT_IsScalar: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2787 | // Note: semantic analysis depends on Objective-C lifetime types to be |
| 2788 | // considered scalar types. However, such types do not actually behave |
| 2789 | // like scalar types at run time (since they may require retain/release |
| 2790 | // operations), so we report them as non-scalar. |
| 2791 | if (T->isObjCLifetimeType()) { |
| 2792 | switch (T.getObjCLifetime()) { |
| 2793 | case Qualifiers::OCL_None: |
| 2794 | case Qualifiers::OCL_ExplicitNone: |
| 2795 | return true; |
| 2796 | |
| 2797 | case Qualifiers::OCL_Strong: |
| 2798 | case Qualifiers::OCL_Weak: |
| 2799 | case Qualifiers::OCL_Autoreleasing: |
| 2800 | return false; |
| 2801 | } |
| 2802 | } |
| 2803 | |
Chandler Carruth | cec0ced | 2011-05-01 09:29:55 +0000 | [diff] [blame] | 2804 | return T->isScalarType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2805 | case UTT_IsCompound: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2806 | return T->isCompoundType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2807 | case UTT_IsMemberPointer: |
| 2808 | return T->isMemberPointerType(); |
| 2809 | |
| 2810 | // Type trait expressions which correspond to the type property predicates |
| 2811 | // in C++0x [meta.unary.prop]. |
| 2812 | case UTT_IsConst: |
| 2813 | return T.isConstQualified(); |
| 2814 | case UTT_IsVolatile: |
| 2815 | return T.isVolatileQualified(); |
| 2816 | case UTT_IsTrivial: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2817 | return T.isTrivialType(Self.Context); |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2818 | case UTT_IsTriviallyCopyable: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2819 | return T.isTriviallyCopyableType(Self.Context); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2820 | case UTT_IsStandardLayout: |
| 2821 | return T->isStandardLayoutType(); |
| 2822 | case UTT_IsPOD: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2823 | return T.isPODType(Self.Context); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2824 | case UTT_IsLiteral: |
| 2825 | return T->isLiteralType(); |
| 2826 | case UTT_IsEmpty: |
| 2827 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2828 | return !RD->isUnion() && RD->isEmpty(); |
| 2829 | return false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2830 | case UTT_IsPolymorphic: |
Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2831 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2832 | return RD->isPolymorphic(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2833 | return false; |
| 2834 | case UTT_IsAbstract: |
Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2835 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2836 | return RD->isAbstract(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2837 | return false; |
Douglas Gregor | 5e9392b | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 2838 | case UTT_IsFinal: |
| 2839 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2840 | return RD->hasAttr<FinalAttr>(); |
| 2841 | return false; |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2842 | case UTT_IsSigned: |
| 2843 | return T->isSignedIntegerType(); |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2844 | case UTT_IsUnsigned: |
| 2845 | return T->isUnsignedIntegerType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2846 | |
| 2847 | // Type trait expressions which query classes regarding their construction, |
| 2848 | // destruction, and copying. Rather than being based directly on the |
| 2849 | // related type predicates in the standard, they are specified by both |
| 2850 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those |
| 2851 | // specifications. |
| 2852 | // |
| 2853 | // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html |
| 2854 | // 2: http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2855 | case UTT_HasTrivialDefaultConstructor: |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2856 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2857 | // If __is_pod (type) is true then the trait is true, else if type is |
| 2858 | // a cv class or union type (or array thereof) with a trivial default |
| 2859 | // constructor ([class.ctor]) then the trait is true, else it is false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2860 | if (T.isPODType(Self.Context)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2861 | return true; |
| 2862 | if (const RecordType *RT = |
| 2863 | C.getBaseElementType(T)->getAs<RecordType>()) |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2864 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDefaultConstructor(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2865 | return false; |
| 2866 | case UTT_HasTrivialCopy: |
| 2867 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2868 | // If __is_pod (type) is true or type is a reference type then |
| 2869 | // the trait is true, else if type is a cv class or union type |
| 2870 | // with a trivial copy constructor ([class.copy]) then the trait |
| 2871 | // is true, else it is false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2872 | if (T.isPODType(Self.Context) || T->isReferenceType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2873 | return true; |
| 2874 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2875 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor(); |
| 2876 | return false; |
| 2877 | case UTT_HasTrivialAssign: |
| 2878 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2879 | // If type is const qualified or is a reference type then the |
| 2880 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 2881 | // trait is true, else if type is a cv class or union type with |
| 2882 | // a trivial copy assignment ([class.copy]) then the trait is |
| 2883 | // true, else it is false. |
| 2884 | // Note: the const and reference restrictions are interesting, |
| 2885 | // given that const and reference members don't prevent a class |
| 2886 | // from having a trivial copy assignment operator (but do cause |
| 2887 | // errors if the copy assignment operator is actually used, q.v. |
| 2888 | // [class.copy]p12). |
| 2889 | |
| 2890 | if (C.getBaseElementType(T).isConstQualified()) |
| 2891 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2892 | if (T.isPODType(Self.Context)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2893 | return true; |
| 2894 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2895 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment(); |
| 2896 | return false; |
| 2897 | case UTT_HasTrivialDestructor: |
| 2898 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2899 | // If __is_pod (type) is true or type is a reference type |
| 2900 | // then the trait is true, else if type is a cv class or union |
| 2901 | // type (or array thereof) with a trivial destructor |
| 2902 | // ([class.dtor]) then the trait is true, else it is |
| 2903 | // false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2904 | if (T.isPODType(Self.Context) || T->isReferenceType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2905 | return true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2906 | |
| 2907 | // Objective-C++ ARC: autorelease types don't require destruction. |
| 2908 | if (T->isObjCLifetimeType() && |
| 2909 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 2910 | return true; |
| 2911 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2912 | if (const RecordType *RT = |
| 2913 | C.getBaseElementType(T)->getAs<RecordType>()) |
| 2914 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor(); |
| 2915 | return false; |
| 2916 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 2917 | case UTT_HasNothrowAssign: |
| 2918 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2919 | // If type is const qualified or is a reference type then the |
| 2920 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 2921 | // is true then the trait is true, else if type is a cv class |
| 2922 | // or union type with copy assignment operators that are known |
| 2923 | // not to throw an exception then the trait is true, else it is |
| 2924 | // false. |
| 2925 | if (C.getBaseElementType(T).isConstQualified()) |
| 2926 | return false; |
| 2927 | if (T->isReferenceType()) |
| 2928 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2929 | if (T.isPODType(Self.Context) || T->isObjCLifetimeType()) |
| 2930 | return true; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2931 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2932 | CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2933 | if (RD->hasTrivialCopyAssignment()) |
| 2934 | return true; |
| 2935 | |
| 2936 | bool FoundAssign = false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2937 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2938 | LookupResult Res(Self, DeclarationNameInfo(Name, KeyLoc), |
| 2939 | Sema::LookupOrdinaryName); |
| 2940 | if (Self.LookupQualifiedName(Res, RD)) { |
Douglas Gregor | d41679d | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 2941 | Res.suppressDiagnostics(); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2942 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 2943 | Op != OpEnd; ++Op) { |
Douglas Gregor | d41679d | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 2944 | if (isa<FunctionTemplateDecl>(*Op)) |
| 2945 | continue; |
| 2946 | |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2947 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 2948 | if (Operator->isCopyAssignmentOperator()) { |
| 2949 | FoundAssign = true; |
| 2950 | const FunctionProtoType *CPT |
| 2951 | = Operator->getType()->getAs<FunctionProtoType>(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2952 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 2953 | return false; |
| 2954 | if (!CPT->isNothrow(Self.Context)) |
| 2955 | return false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | } |
Douglas Gregor | d41679d | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 2959 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2960 | return FoundAssign; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2961 | } |
| 2962 | return false; |
| 2963 | case UTT_HasNothrowCopy: |
| 2964 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2965 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 2966 | // if type is a cv class or union type with copy constructors that are |
| 2967 | // known not to throw an exception then the trait is true, else it is |
| 2968 | // false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2969 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2970 | return true; |
| 2971 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2972 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2973 | if (RD->hasTrivialCopyConstructor()) |
| 2974 | return true; |
| 2975 | |
| 2976 | bool FoundConstructor = false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2977 | unsigned FoundTQs; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2978 | DeclContext::lookup_const_iterator Con, ConEnd; |
Sebastian Redl | 5f4e899 | 2010-09-13 21:10:20 +0000 | [diff] [blame] | 2979 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2980 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2981 | // A template constructor is never a copy constructor. |
| 2982 | // FIXME: However, it may actually be selected at the actual overload |
| 2983 | // resolution point. |
| 2984 | if (isa<FunctionTemplateDecl>(*Con)) |
| 2985 | continue; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2986 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 2987 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 2988 | FoundConstructor = true; |
| 2989 | const FunctionProtoType *CPT |
| 2990 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2991 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 2992 | return false; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2993 | // FIXME: check whether evaluating default arguments can throw. |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2994 | // For now, we'll be conservative and assume that they can throw. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2995 | if (!CPT->isNothrow(Self.Context) || CPT->getNumArgs() > 1) |
| 2996 | return false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2997 | } |
| 2998 | } |
| 2999 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3000 | return FoundConstructor; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3001 | } |
| 3002 | return false; |
| 3003 | case UTT_HasNothrowConstructor: |
| 3004 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3005 | // If __has_trivial_constructor (type) is true then the trait is |
| 3006 | // true, else if type is a cv class or union type (or array |
| 3007 | // thereof) with a default constructor that is known not to |
| 3008 | // throw an exception then the trait is true, else it is false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3009 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3010 | return true; |
| 3011 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) { |
| 3012 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 3013 | if (RD->hasTrivialDefaultConstructor()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3014 | return true; |
| 3015 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 3016 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 3017 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
| 3018 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 3019 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 3020 | if (isa<FunctionTemplateDecl>(*Con)) |
| 3021 | continue; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 3022 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 3023 | if (Constructor->isDefaultConstructor()) { |
| 3024 | const FunctionProtoType *CPT |
| 3025 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3026 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 3027 | return false; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 3028 | // TODO: check whether evaluating default arguments can throw. |
| 3029 | // For now, we'll be conservative and assume that they can throw. |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 3030 | return CPT->isNothrow(Self.Context) && CPT->getNumArgs() == 0; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 3031 | } |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3032 | } |
| 3033 | } |
| 3034 | return false; |
| 3035 | case UTT_HasVirtualDestructor: |
| 3036 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 3037 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 3038 | // then the trait is true, else it is false. |
| 3039 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 3040 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 3041 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3042 | return Destructor->isVirtual(); |
| 3043 | } |
| 3044 | return false; |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 3045 | |
| 3046 | // These type trait expressions are modeled on the specifications for the |
| 3047 | // Embarcadero C++0x type trait functions: |
| 3048 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
| 3049 | case UTT_IsCompleteType: |
| 3050 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): |
| 3051 | // Returns True if and only if T is a complete type at the point of the |
| 3052 | // function call. |
| 3053 | return !T->isIncompleteType(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3054 | } |
Chandler Carruth | 83f563c | 2011-05-01 07:44:17 +0000 | [diff] [blame] | 3055 | llvm_unreachable("Type trait not covered by switch"); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | ExprResult Sema::BuildUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 3059 | SourceLocation KWLoc, |
| 3060 | TypeSourceInfo *TSInfo, |
| 3061 | SourceLocation RParen) { |
| 3062 | QualType T = TSInfo->getType(); |
Chandler Carruth | eb65a10 | 2011-04-30 10:07:32 +0000 | [diff] [blame] | 3063 | if (!CheckUnaryTypeTraitTypeCompleteness(*this, UTT, KWLoc, T)) |
| 3064 | return ExprError(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3065 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3066 | bool Value = false; |
| 3067 | if (!T->isDependentType()) |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 3068 | Value = EvaluateUnaryTypeTrait(*this, UTT, KWLoc, T); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3069 | |
| 3070 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, UTT, TSInfo, Value, |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 3071 | RParen, Context.BoolTy)); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3072 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3073 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3074 | ExprResult Sema::ActOnBinaryTypeTrait(BinaryTypeTrait BTT, |
| 3075 | SourceLocation KWLoc, |
| 3076 | ParsedType LhsTy, |
| 3077 | ParsedType RhsTy, |
| 3078 | SourceLocation RParen) { |
| 3079 | TypeSourceInfo *LhsTSInfo; |
| 3080 | QualType LhsT = GetTypeFromParser(LhsTy, &LhsTSInfo); |
| 3081 | if (!LhsTSInfo) |
| 3082 | LhsTSInfo = Context.getTrivialTypeSourceInfo(LhsT); |
| 3083 | |
| 3084 | TypeSourceInfo *RhsTSInfo; |
| 3085 | QualType RhsT = GetTypeFromParser(RhsTy, &RhsTSInfo); |
| 3086 | if (!RhsTSInfo) |
| 3087 | RhsTSInfo = Context.getTrivialTypeSourceInfo(RhsT); |
| 3088 | |
| 3089 | return BuildBinaryTypeTrait(BTT, KWLoc, LhsTSInfo, RhsTSInfo, RParen); |
| 3090 | } |
| 3091 | |
| 3092 | static bool EvaluateBinaryTypeTrait(Sema &Self, BinaryTypeTrait BTT, |
| 3093 | QualType LhsT, QualType RhsT, |
| 3094 | SourceLocation KeyLoc) { |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3095 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
| 3096 | "Cannot evaluate traits of dependent types"); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3097 | |
| 3098 | switch(BTT) { |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3099 | case BTT_IsBaseOf: { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3100 | // C++0x [meta.rel]p2 |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3101 | // Base is a base class of Derived without regard to cv-qualifiers or |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3102 | // Base and Derived are not unions and name the same class type without |
| 3103 | // regard to cv-qualifiers. |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3104 | |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3105 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
| 3106 | if (!lhsRecord) return false; |
| 3107 | |
| 3108 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
| 3109 | if (!rhsRecord) return false; |
| 3110 | |
| 3111 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
| 3112 | == (lhsRecord == rhsRecord)); |
| 3113 | |
| 3114 | if (lhsRecord == rhsRecord) |
| 3115 | return !lhsRecord->getDecl()->isUnion(); |
| 3116 | |
| 3117 | // C++0x [meta.rel]p2: |
| 3118 | // If Base and Derived are class types and are different types |
| 3119 | // (ignoring possible cv-qualifiers) then Derived shall be a |
| 3120 | // complete type. |
| 3121 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
| 3122 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 3123 | return false; |
| 3124 | |
| 3125 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) |
| 3126 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); |
| 3127 | } |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3128 | case BTT_IsSame: |
| 3129 | return Self.Context.hasSameType(LhsT, RhsT); |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3130 | case BTT_TypeCompatible: |
| 3131 | return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(), |
| 3132 | RhsT.getUnqualifiedType()); |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3133 | case BTT_IsConvertible: |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3134 | case BTT_IsConvertibleTo: { |
| 3135 | // C++0x [meta.rel]p4: |
| 3136 | // Given the following function prototype: |
| 3137 | // |
| 3138 | // template <class T> |
| 3139 | // typename add_rvalue_reference<T>::type create(); |
| 3140 | // |
| 3141 | // the predicate condition for a template specialization |
| 3142 | // is_convertible<From, To> shall be satisfied if and only if |
| 3143 | // the return expression in the following code would be |
| 3144 | // well-formed, including any implicit conversions to the return |
| 3145 | // type of the function: |
| 3146 | // |
| 3147 | // To test() { |
| 3148 | // return create<From>(); |
| 3149 | // } |
| 3150 | // |
| 3151 | // Access checking is performed as if in a context unrelated to To and |
| 3152 | // From. Only the validity of the immediate context of the expression |
| 3153 | // of the return-statement (including conversions to the return type) |
| 3154 | // is considered. |
| 3155 | // |
| 3156 | // We model the initialization as a copy-initialization of a temporary |
| 3157 | // of the appropriate type, which for this expression is identical to the |
| 3158 | // return statement (since NRVO doesn't apply). |
| 3159 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 3160 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
| 3161 | |
| 3162 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3163 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3164 | Expr::getValueKindForType(LhsT)); |
| 3165 | Expr *FromPtr = &From; |
| 3166 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, |
| 3167 | SourceLocation())); |
| 3168 | |
Eli Friedman | 3add9f0 | 2012-01-25 01:05:57 +0000 | [diff] [blame] | 3169 | // Perform the initialization in an unevaluated context within a SFINAE |
| 3170 | // trap at translation unit scope. |
| 3171 | EnterExpressionEvaluationContext Unevaluated(Self, Sema::Unevaluated); |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3172 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 3173 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3174 | InitializationSequence Init(Self, To, Kind, &FromPtr, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3175 | if (Init.Failed()) |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3176 | return false; |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3177 | |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3178 | ExprResult Result = Init.Perform(Self, To, Kind, MultiExprArg(&FromPtr, 1)); |
| 3179 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); |
| 3180 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3181 | } |
| 3182 | llvm_unreachable("Unknown type trait or not implemented"); |
| 3183 | } |
| 3184 | |
| 3185 | ExprResult Sema::BuildBinaryTypeTrait(BinaryTypeTrait BTT, |
| 3186 | SourceLocation KWLoc, |
| 3187 | TypeSourceInfo *LhsTSInfo, |
| 3188 | TypeSourceInfo *RhsTSInfo, |
| 3189 | SourceLocation RParen) { |
| 3190 | QualType LhsT = LhsTSInfo->getType(); |
| 3191 | QualType RhsT = RhsTSInfo->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3192 | |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3193 | if (BTT == BTT_TypeCompatible) { |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3194 | if (getLangOptions().CPlusPlus) { |
| 3195 | Diag(KWLoc, diag::err_types_compatible_p_in_cplusplus) |
| 3196 | << SourceRange(KWLoc, RParen); |
| 3197 | return ExprError(); |
| 3198 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | bool Value = false; |
| 3202 | if (!LhsT->isDependentType() && !RhsT->isDependentType()) |
| 3203 | Value = EvaluateBinaryTypeTrait(*this, BTT, LhsT, RhsT, KWLoc); |
| 3204 | |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3205 | // Select trait result type. |
| 3206 | QualType ResultType; |
| 3207 | switch (BTT) { |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3208 | case BTT_IsBaseOf: ResultType = Context.BoolTy; break; |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3209 | case BTT_IsConvertible: ResultType = Context.BoolTy; break; |
| 3210 | case BTT_IsSame: ResultType = Context.BoolTy; break; |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3211 | case BTT_TypeCompatible: ResultType = Context.IntTy; break; |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3212 | case BTT_IsConvertibleTo: ResultType = Context.BoolTy; break; |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3215 | return Owned(new (Context) BinaryTypeTraitExpr(KWLoc, BTT, LhsTSInfo, |
| 3216 | RhsTSInfo, Value, RParen, |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3217 | ResultType)); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3220 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
| 3221 | SourceLocation KWLoc, |
| 3222 | ParsedType Ty, |
| 3223 | Expr* DimExpr, |
| 3224 | SourceLocation RParen) { |
| 3225 | TypeSourceInfo *TSInfo; |
| 3226 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
| 3227 | if (!TSInfo) |
| 3228 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
| 3229 | |
| 3230 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
| 3231 | } |
| 3232 | |
| 3233 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
| 3234 | QualType T, Expr *DimExpr, |
| 3235 | SourceLocation KeyLoc) { |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3236 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3237 | |
| 3238 | switch(ATT) { |
| 3239 | case ATT_ArrayRank: |
| 3240 | if (T->isArrayType()) { |
| 3241 | unsigned Dim = 0; |
| 3242 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 3243 | ++Dim; |
| 3244 | T = AT->getElementType(); |
| 3245 | } |
| 3246 | return Dim; |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3247 | } |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3248 | return 0; |
| 3249 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3250 | case ATT_ArrayExtent: { |
| 3251 | llvm::APSInt Value; |
| 3252 | uint64_t Dim; |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3253 | if (DimExpr->isIntegerConstantExpr(Value, Self.Context, 0, false)) { |
| 3254 | if (Value < llvm::APSInt(Value.getBitWidth(), Value.isUnsigned())) { |
| 3255 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) << |
| 3256 | DimExpr->getSourceRange(); |
| 3257 | return false; |
| 3258 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3259 | Dim = Value.getLimitedValue(); |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3260 | } else { |
| 3261 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) << |
| 3262 | DimExpr->getSourceRange(); |
| 3263 | return false; |
| 3264 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3265 | |
| 3266 | if (T->isArrayType()) { |
| 3267 | unsigned D = 0; |
| 3268 | bool Matched = false; |
| 3269 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 3270 | if (Dim == D) { |
| 3271 | Matched = true; |
| 3272 | break; |
| 3273 | } |
| 3274 | ++D; |
| 3275 | T = AT->getElementType(); |
| 3276 | } |
| 3277 | |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3278 | if (Matched && T->isArrayType()) { |
| 3279 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
| 3280 | return CAT->getSize().getLimitedValue(); |
| 3281 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3282 | } |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3283 | return 0; |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3284 | } |
| 3285 | } |
| 3286 | llvm_unreachable("Unknown type trait or not implemented"); |
| 3287 | } |
| 3288 | |
| 3289 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
| 3290 | SourceLocation KWLoc, |
| 3291 | TypeSourceInfo *TSInfo, |
| 3292 | Expr* DimExpr, |
| 3293 | SourceLocation RParen) { |
| 3294 | QualType T = TSInfo->getType(); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3295 | |
Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3296 | // FIXME: This should likely be tracked as an APInt to remove any host |
| 3297 | // assumptions about the width of size_t on the target. |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3298 | uint64_t Value = 0; |
| 3299 | if (!T->isDependentType()) |
| 3300 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); |
| 3301 | |
Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3302 | // While the specification for these traits from the Embarcadero C++ |
| 3303 | // compiler's documentation says the return type is 'unsigned int', Clang |
| 3304 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero |
| 3305 | // compiler, there is no difference. On several other platforms this is an |
| 3306 | // important distinction. |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3307 | return Owned(new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, |
Chandler Carruth | 06207f6 | 2011-05-01 07:49:26 +0000 | [diff] [blame] | 3308 | DimExpr, RParen, |
Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3309 | Context.getSizeType())); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3312 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3313 | SourceLocation KWLoc, |
| 3314 | Expr *Queried, |
| 3315 | SourceLocation RParen) { |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3316 | // If error parsing the expression, ignore. |
| 3317 | if (!Queried) |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3318 | return ExprError(); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3319 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3320 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3321 | |
| 3322 | return move(Result); |
| 3323 | } |
| 3324 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3325 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
| 3326 | switch (ET) { |
| 3327 | case ET_IsLValueExpr: return E->isLValue(); |
| 3328 | case ET_IsRValueExpr: return E->isRValue(); |
| 3329 | } |
| 3330 | llvm_unreachable("Expression trait not covered by switch"); |
| 3331 | } |
| 3332 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3333 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3334 | SourceLocation KWLoc, |
| 3335 | Expr *Queried, |
| 3336 | SourceLocation RParen) { |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3337 | if (Queried->isTypeDependent()) { |
| 3338 | // Delay type-checking for type-dependent expressions. |
| 3339 | } else if (Queried->getType()->isPlaceholderType()) { |
| 3340 | ExprResult PE = CheckPlaceholderExpr(Queried); |
| 3341 | if (PE.isInvalid()) return ExprError(); |
| 3342 | return BuildExpressionTrait(ET, KWLoc, PE.take(), RParen); |
| 3343 | } |
| 3344 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3345 | bool Value = EvaluateExpressionTrait(ET, Queried); |
Chandler Carruth | f7ef000 | 2011-05-01 08:48:19 +0000 | [diff] [blame] | 3346 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3347 | return Owned(new (Context) ExpressionTraitExpr(KWLoc, ET, Queried, Value, |
| 3348 | RParen, Context.BoolTy)); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3351 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3352 | ExprValueKind &VK, |
| 3353 | SourceLocation Loc, |
| 3354 | bool isIndirect) { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3355 | assert(!LHS.get()->getType()->isPlaceholderType() && |
| 3356 | !RHS.get()->getType()->isPlaceholderType() && |
John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3357 | "placeholders should have been weeded out by now"); |
| 3358 | |
| 3359 | // The LHS undergoes lvalue conversions if this is ->*. |
| 3360 | if (isIndirect) { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3361 | LHS = DefaultLvalueConversion(LHS.take()); |
| 3362 | if (LHS.isInvalid()) return QualType(); |
John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | // The RHS always undergoes lvalue conversions. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3366 | RHS = DefaultLvalueConversion(RHS.take()); |
| 3367 | if (RHS.isInvalid()) return QualType(); |
John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3368 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3369 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 3370 | // C++ 5.5p2 |
| 3371 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 3372 | // be of type "pointer to member of T" (where T is a completely-defined |
| 3373 | // class type) [...] |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3374 | QualType RHSType = RHS.get()->getType(); |
| 3375 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3376 | if (!MemPtr) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3377 | Diag(Loc, diag::err_bad_memptr_rhs) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3378 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3379 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3380 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3381 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3382 | QualType Class(MemPtr->getClass(), 0); |
| 3383 | |
Douglas Gregor | 7d520ba | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 3384 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
| 3385 | // member pointer points must be completely-defined. However, there is no |
| 3386 | // reason for this semantic distinction, and the rule is not enforced by |
| 3387 | // other compilers. Therefore, we do not check this property, as it is |
| 3388 | // likely to be considered a defect. |
Sebastian Redl | 59fc269 | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 3389 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3390 | // C++ 5.5p2 |
| 3391 | // [...] to its first operand, which shall be of class T or of a class of |
| 3392 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 3393 | // such a class] |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3394 | QualType LHSType = LHS.get()->getType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3395 | if (isIndirect) { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3396 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) |
| 3397 | LHSType = Ptr->getPointeeType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3398 | else { |
| 3399 | Diag(Loc, diag::err_bad_memptr_lhs) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3400 | << OpSpelling << 1 << LHSType |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3401 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3402 | return QualType(); |
| 3403 | } |
| 3404 | } |
| 3405 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3406 | if (!Context.hasSameUnqualifiedType(Class, LHSType)) { |
Sebastian Redl | 17e1d35 | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 3407 | // If we want to check the hierarchy, we need a complete type. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3408 | if (RequireCompleteType(Loc, LHSType, PDiag(diag::err_bad_memptr_lhs) |
Sebastian Redl | 17e1d35 | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 3409 | << OpSpelling << (int)isIndirect)) { |
| 3410 | return QualType(); |
| 3411 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3412 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 3413 | /*DetectVirtual=*/false); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3414 | // FIXME: Would it be useful to print full ambiguity paths, or is that |
| 3415 | // overkill? |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3416 | if (!IsDerivedFrom(LHSType, Class, Paths) || |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3417 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
| 3418 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3419 | << (int)isIndirect << LHS.get()->getType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3420 | return QualType(); |
| 3421 | } |
Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 3422 | // Cast LHS to type of use. |
| 3423 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 3424 | ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind(); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3425 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3426 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3427 | BuildBasePathArray(Paths, BasePath); |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3428 | LHS = ImpCastExprToType(LHS.take(), UseType, CK_DerivedToBase, VK, |
| 3429 | &BasePath); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3432 | if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) { |
Fariborz Jahanian | 05ebda9 | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 3433 | // Diagnose use of pointer-to-member type which when used as |
| 3434 | // the functional cast in a pointer-to-member expression. |
| 3435 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 3436 | return QualType(); |
| 3437 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3438 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3439 | // C++ 5.5p2 |
| 3440 | // The result is an object or a function of the type specified by the |
| 3441 | // second operand. |
| 3442 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 3443 | // in accordance with 5.5p5 and 5.2.5. |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3444 | QualType Result = MemPtr->getPointeeType(); |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3445 | Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3446 | |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3447 | // C++0x [expr.mptr.oper]p6: |
| 3448 | // In a .* expression whose object expression is an rvalue, the program is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3449 | // ill-formed if the second operand is a pointer to member function with |
| 3450 | // ref-qualifier &. In a ->* expression or in a .* expression whose object |
| 3451 | // expression is an lvalue, the program is ill-formed if the second operand |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3452 | // is a pointer to member function with ref-qualifier &&. |
| 3453 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
| 3454 | switch (Proto->getRefQualifier()) { |
| 3455 | case RQ_None: |
| 3456 | // Do nothing |
| 3457 | break; |
| 3458 | |
| 3459 | case RQ_LValue: |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3460 | if (!isIndirect && !LHS.get()->Classify(Context).isLValue()) |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3461 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3462 | << RHSType << 1 << LHS.get()->getSourceRange(); |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3463 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3464 | |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3465 | case RQ_RValue: |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3466 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3467 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3468 | << RHSType << 0 << LHS.get()->getSourceRange(); |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3469 | break; |
| 3470 | } |
| 3471 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3472 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3473 | // C++ [expr.mptr.oper]p6: |
| 3474 | // The result of a .* expression whose second operand is a pointer |
| 3475 | // to a data member is of the same value category as its |
| 3476 | // first operand. The result of a .* expression whose second |
| 3477 | // operand is a pointer to a member function is a prvalue. The |
| 3478 | // result of an ->* expression is an lvalue if its second operand |
| 3479 | // is a pointer to data member and a prvalue otherwise. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3480 | if (Result->isFunctionType()) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3481 | VK = VK_RValue; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3482 | return Context.BoundMemberTy; |
| 3483 | } else if (isIndirect) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3484 | VK = VK_LValue; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3485 | } else { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3486 | VK = LHS.get()->getValueKind(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3487 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3488 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3489 | return Result; |
| 3490 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3491 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3492 | /// \brief Try to convert a type to another according to C++0x 5.16p3. |
| 3493 | /// |
| 3494 | /// This is part of the parameter validation for the ? operator. If either |
| 3495 | /// value operand is a class type, the two operands are attempted to be |
| 3496 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3497 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 3498 | /// as such. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3499 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 3500 | SourceLocation QuestionLoc, |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3501 | bool &HaveConversion, |
| 3502 | QualType &ToType) { |
| 3503 | HaveConversion = false; |
| 3504 | ToType = To->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3505 | |
| 3506 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3507 | SourceLocation()); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3508 | // C++0x 5.16p3 |
| 3509 | // The process for determining whether an operand expression E1 of type T1 |
| 3510 | // can be converted to match an operand expression E2 of type T2 is defined |
| 3511 | // as follows: |
| 3512 | // -- If E2 is an lvalue: |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 3513 | bool ToIsLvalue = To->isLValue(); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 3514 | if (ToIsLvalue) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3515 | // E1 can be converted to match E2 if E1 can be implicitly converted to |
| 3516 | // type "lvalue reference to T2", subject to the constraint that in the |
| 3517 | // conversion the reference must bind directly to E1. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3518 | QualType T = Self.Context.getLValueReferenceType(ToType); |
| 3519 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3520 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3521 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 3522 | if (InitSeq.isDirectReferenceBinding()) { |
| 3523 | ToType = T; |
| 3524 | HaveConversion = true; |
| 3525 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3526 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3527 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3528 | if (InitSeq.isAmbiguous()) |
| 3529 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3530 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3531 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3532 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 3533 | // -- if E1 and E2 have class type, and the underlying class types are |
| 3534 | // the same or one is a base class of the other: |
| 3535 | QualType FTy = From->getType(); |
| 3536 | QualType TTy = To->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3537 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 3538 | const RecordType *TRec = TTy->getAs<RecordType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3539 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3540 | Self.IsDerivedFrom(FTy, TTy); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3541 | if (FRec && TRec && |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3542 | (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3543 | // E1 can be converted to match E2 if the class of T2 is the |
| 3544 | // same type as, or a base class of, the class of T1, and |
| 3545 | // [cv2 > cv1]. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3546 | if (FRec == TRec || FDerivedFromT) { |
| 3547 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3548 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 3549 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3550 | if (InitSeq) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3551 | HaveConversion = true; |
| 3552 | return false; |
| 3553 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3554 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3555 | if (InitSeq.isAmbiguous()) |
| 3556 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3557 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3558 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3560 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3561 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3562 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3563 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 3564 | // implicitly converted to the type that expression E2 would have |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3565 | // if E2 were converted to an rvalue (or the type it has, if E2 is |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 3566 | // an rvalue). |
| 3567 | // |
| 3568 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 3569 | // to the array-to-pointer or function-to-pointer conversions. |
| 3570 | if (!TTy->getAs<TagType>()) |
| 3571 | TTy = TTy.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3572 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3573 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 3574 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3575 | HaveConversion = !InitSeq.Failed(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3576 | ToType = TTy; |
| 3577 | if (InitSeq.isAmbiguous()) |
| 3578 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
| 3579 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3580 | return false; |
| 3581 | } |
| 3582 | |
| 3583 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 3584 | /// |
| 3585 | /// This is part of the parameter validation for the ? operator. If either |
| 3586 | /// value operand is a class type, overload resolution is used to find a |
| 3587 | /// conversion to a common type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3588 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3589 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3590 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3591 | OverloadCandidateSet CandidateSet(QuestionLoc); |
| 3592 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, 2, |
| 3593 | CandidateSet); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3594 | |
| 3595 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3596 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3597 | case OR_Success: { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3598 | // We found a match. Perform the conversions on the arguments and move on. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3599 | ExprResult LHSRes = |
| 3600 | Self.PerformImplicitConversion(LHS.get(), Best->BuiltinTypes.ParamTypes[0], |
| 3601 | Best->Conversions[0], Sema::AA_Converting); |
| 3602 | if (LHSRes.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3603 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3604 | LHS = move(LHSRes); |
| 3605 | |
| 3606 | ExprResult RHSRes = |
| 3607 | Self.PerformImplicitConversion(RHS.get(), Best->BuiltinTypes.ParamTypes[1], |
| 3608 | Best->Conversions[1], Sema::AA_Converting); |
| 3609 | if (RHSRes.isInvalid()) |
| 3610 | break; |
| 3611 | RHS = move(RHSRes); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3612 | if (Best->Function) |
| 3613 | Self.MarkDeclarationReferenced(QuestionLoc, Best->Function); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3614 | return false; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3617 | case OR_No_Viable_Function: |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3618 | |
| 3619 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 3620 | // constant and the other is a pointer type. In this case, the user most |
| 3621 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3622 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3623 | return true; |
| 3624 | |
| 3625 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3626 | << LHS.get()->getType() << RHS.get()->getType() |
| 3627 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3628 | return true; |
| 3629 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3630 | case OR_Ambiguous: |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3631 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3632 | << LHS.get()->getType() << RHS.get()->getType() |
| 3633 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3634 | // FIXME: Print the possible common types by printing the return types of |
| 3635 | // the viable candidates. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3636 | break; |
| 3637 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3638 | case OR_Deleted: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3639 | llvm_unreachable("Conditional operator has only built-in overloads"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3640 | } |
| 3641 | return true; |
| 3642 | } |
| 3643 | |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3644 | /// \brief Perform an "extended" implicit conversion as returned by |
| 3645 | /// TryClassUnification. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3646 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3647 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3648 | InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(), |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3649 | SourceLocation()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3650 | Expr *Arg = E.take(); |
| 3651 | InitializationSequence InitSeq(Self, Entity, Kind, &Arg, 1); |
| 3652 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, MultiExprArg(&Arg, 1)); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3653 | if (Result.isInvalid()) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3654 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3655 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3656 | E = Result; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3657 | return false; |
| 3658 | } |
| 3659 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3660 | /// \brief Check the operands of ?: under C++ semantics. |
| 3661 | /// |
| 3662 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 3663 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3664 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3665 | ExprValueKind &VK, ExprObjectKind &OK, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3666 | SourceLocation QuestionLoc) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3667 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 3668 | // interface pointers. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3669 | |
| 3670 | // C++0x 5.16p1 |
| 3671 | // The first expression is contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3672 | if (!Cond.get()->isTypeDependent()) { |
| 3673 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.take()); |
| 3674 | if (CondRes.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3675 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3676 | Cond = move(CondRes); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3679 | // Assume r-value. |
| 3680 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3681 | OK = OK_Ordinary; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3682 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3683 | // Either of the arguments dependent? |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3684 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3685 | return Context.DependentTy; |
| 3686 | |
| 3687 | // C++0x 5.16p2 |
| 3688 | // If either the second or the third operand has type (cv) void, ... |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3689 | QualType LTy = LHS.get()->getType(); |
| 3690 | QualType RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3691 | bool LVoid = LTy->isVoidType(); |
| 3692 | bool RVoid = RTy->isVoidType(); |
| 3693 | if (LVoid || RVoid) { |
| 3694 | // ... then the [l2r] conversions are performed on the second and third |
| 3695 | // operands ... |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3696 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 3697 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 3698 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 3699 | return QualType(); |
| 3700 | LTy = LHS.get()->getType(); |
| 3701 | RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3702 | |
| 3703 | // ... and one of the following shall hold: |
| 3704 | // -- The second or the third operand (but not both) is a throw- |
| 3705 | // expression; the result is of the type of the other and is an rvalue. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3706 | bool LThrow = isa<CXXThrowExpr>(LHS.get()); |
| 3707 | bool RThrow = isa<CXXThrowExpr>(RHS.get()); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3708 | if (LThrow && !RThrow) |
| 3709 | return RTy; |
| 3710 | if (RThrow && !LThrow) |
| 3711 | return LTy; |
| 3712 | |
| 3713 | // -- Both the second and third operands have type void; the result is of |
| 3714 | // type void and is an rvalue. |
| 3715 | if (LVoid && RVoid) |
| 3716 | return Context.VoidTy; |
| 3717 | |
| 3718 | // Neither holds, error. |
| 3719 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 3720 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3721 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3722 | return QualType(); |
| 3723 | } |
| 3724 | |
| 3725 | // Neither is void. |
| 3726 | |
| 3727 | // C++0x 5.16p3 |
| 3728 | // Otherwise, if the second and third operand have different types, and |
| 3729 | // either has (cv) class type, and attempt is made to convert each of those |
| 3730 | // operands to the other. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3731 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3732 | (LTy->isRecordType() || RTy->isRecordType())) { |
| 3733 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; |
| 3734 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3735 | QualType L2RType, R2LType; |
| 3736 | bool HaveL2R, HaveR2L; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3737 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3738 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3739 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3740 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3741 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3742 | // If both can be converted, [...] the program is ill-formed. |
| 3743 | if (HaveL2R && HaveR2L) { |
| 3744 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3745 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3746 | return QualType(); |
| 3747 | } |
| 3748 | |
| 3749 | // If exactly one conversion is possible, that conversion is applied to |
| 3750 | // the chosen operand and the converted operands are used in place of the |
| 3751 | // original operands for the remainder of this section. |
| 3752 | if (HaveL2R) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3753 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3754 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3755 | LTy = LHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3756 | } else if (HaveR2L) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3757 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3758 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3759 | RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3760 | } |
| 3761 | } |
| 3762 | |
| 3763 | // C++0x 5.16p4 |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3764 | // If the second and third operands are glvalues of the same value |
| 3765 | // category and have the same type, the result is of that type and |
| 3766 | // value category and it is a bit-field if the second or the third |
| 3767 | // operand is a bit-field, or if both are bit-fields. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3768 | // We only extend this to bitfields, not to the crazy other kinds of |
| 3769 | // l-values. |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 3770 | bool Same = Context.hasSameType(LTy, RTy); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3771 | if (Same && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3772 | LHS.get()->isGLValue() && |
| 3773 | LHS.get()->getValueKind() == RHS.get()->getValueKind() && |
| 3774 | LHS.get()->isOrdinaryOrBitFieldObject() && |
| 3775 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
| 3776 | VK = LHS.get()->getValueKind(); |
| 3777 | if (LHS.get()->getObjectKind() == OK_BitField || |
| 3778 | RHS.get()->getObjectKind() == OK_BitField) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3779 | OK = OK_BitField; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3780 | return LTy; |
Fariborz Jahanian | 3911a1a | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 3781 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3782 | |
| 3783 | // C++0x 5.16p5 |
| 3784 | // Otherwise, the result is an rvalue. If the second and third operands |
| 3785 | // do not have the same type, and either has (cv) class type, ... |
| 3786 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 3787 | // ... overload resolution is used to determine the conversions (if any) |
| 3788 | // to be applied to the operands. If the overload resolution fails, the |
| 3789 | // program is ill-formed. |
| 3790 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 3791 | return QualType(); |
| 3792 | } |
| 3793 | |
| 3794 | // C++0x 5.16p6 |
| 3795 | // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 3796 | // conversions are performed on the second and third operands. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3797 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 3798 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 3799 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 3800 | return QualType(); |
| 3801 | LTy = LHS.get()->getType(); |
| 3802 | RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3803 | |
| 3804 | // After those conversions, one of the following shall hold: |
| 3805 | // -- The second and third operands have the same type; the result |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3806 | // is of that type. If the operands have class type, the result |
| 3807 | // is a prvalue temporary of the result type, which is |
| 3808 | // copy-initialized from either the second operand or the third |
| 3809 | // operand depending on the value of the first operand. |
| 3810 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 3811 | if (LTy->isRecordType()) { |
| 3812 | // The operands have class type. Make a temporary copy. |
| 3813 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3814 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
| 3815 | SourceLocation(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3816 | LHS); |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3817 | if (LHSCopy.isInvalid()) |
| 3818 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3819 | |
| 3820 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
| 3821 | SourceLocation(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3822 | RHS); |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3823 | if (RHSCopy.isInvalid()) |
| 3824 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3825 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3826 | LHS = LHSCopy; |
| 3827 | RHS = RHSCopy; |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3830 | return LTy; |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3831 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3832 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3833 | // Extension: conditional operator involving vector types. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3834 | if (LTy->isVectorType() || RTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 3835 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3836 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3837 | // -- The second and third operands have arithmetic or enumeration type; |
| 3838 | // the usual arithmetic conversions are performed to bring them to a |
| 3839 | // common type, and the result is of that type. |
| 3840 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
| 3841 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3842 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 3843 | return QualType(); |
| 3844 | return LHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | // -- The second and third operands have pointer type, or one has pointer |
| 3848 | // type and the other is a null pointer constant; pointer conversions |
| 3849 | // and qualification conversions are performed to bring them to their |
| 3850 | // composite pointer type. The result is of the composite pointer type. |
Eli Friedman | de8ac49 | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 3851 | // -- The second and third operands have pointer to member type, or one has |
| 3852 | // pointer to member type and the other is a null pointer constant; |
| 3853 | // pointer to member conversions and qualification conversions are |
| 3854 | // performed to bring them to a common type, whose cv-qualification |
| 3855 | // shall match the cv-qualification of either the second or the third |
| 3856 | // operand. The result is of the common type. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3857 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3858 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3859 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
| 3860 | if (!Composite.isNull()) { |
| 3861 | if (NonStandardCompositeType) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3862 | Diag(QuestionLoc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3863 | diag::ext_typecheck_cond_incompatible_operands_nonstandard) |
| 3864 | << LTy << RTy << Composite |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3865 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3866 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3867 | return Composite; |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3868 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 3870 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 3871 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 3872 | if (!Composite.isNull()) |
| 3873 | return Composite; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3874 | |
Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 3875 | // Check if we are using a null with a non-pointer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3876 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 3877 | return QualType(); |
| 3878 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3879 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3880 | << LHS.get()->getType() << RHS.get()->getType() |
| 3881 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3882 | return QualType(); |
| 3883 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3884 | |
| 3885 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 3886 | /// |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3887 | /// This finds the composite pointer type (or member pointer type) for @p E1 |
| 3888 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this |
| 3889 | /// type and returns it. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3890 | /// It does not emit diagnostics. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3891 | /// |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3892 | /// \param Loc The location of the operator requiring these two expressions to |
| 3893 | /// be converted to the composite pointer type. |
| 3894 | /// |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3895 | /// If \p NonStandardCompositeType is non-NULL, then we are permitted to find |
| 3896 | /// a non-standard (but still sane) composite type to which both expressions |
| 3897 | /// can be converted. When such a type is chosen, \c *NonStandardCompositeType |
| 3898 | /// will be set true. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3899 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3900 | Expr *&E1, Expr *&E2, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3901 | bool *NonStandardCompositeType) { |
| 3902 | if (NonStandardCompositeType) |
| 3903 | *NonStandardCompositeType = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3904 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3905 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); |
| 3906 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3907 | |
Fariborz Jahanian | 0cedfbd | 2009-12-08 20:04:24 +0000 | [diff] [blame] | 3908 | if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && |
| 3909 | !T2->isAnyPointerType() && !T2->isMemberPointerType()) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3910 | return QualType(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3911 | |
| 3912 | // C++0x 5.9p2 |
| 3913 | // Pointer conversions and qualification conversions are performed on |
| 3914 | // pointer operands to bring them to their composite pointer type. If |
| 3915 | // one operand is a null pointer constant, the composite pointer type is |
| 3916 | // the type of the other operand. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3917 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3918 | if (T2->isMemberPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3919 | E1 = ImpCastExprToType(E1, T2, CK_NullToMemberPointer).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3920 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3921 | E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).take(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3922 | return T2; |
| 3923 | } |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3924 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3925 | if (T1->isMemberPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3926 | E2 = ImpCastExprToType(E2, T1, CK_NullToMemberPointer).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3927 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3928 | E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).take(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3929 | return T1; |
| 3930 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3931 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3932 | // Now both have to be pointers or member pointers. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3933 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || |
| 3934 | (!T2->isPointerType() && !T2->isMemberPointerType())) |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3935 | return QualType(); |
| 3936 | |
| 3937 | // Otherwise, of one of the operands has type "pointer to cv1 void," then |
| 3938 | // the other has type "pointer to cv2 T" and the composite pointer type is |
| 3939 | // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. |
| 3940 | // Otherwise, the composite pointer type is a pointer type similar to the |
| 3941 | // type of one of the operands, with a cv-qualification signature that is |
| 3942 | // the union of the cv-qualification signatures of the operand types. |
| 3943 | // In practice, the first part here is redundant; it's subsumed by the second. |
| 3944 | // What we do here is, we build the two possible composite types, and try the |
| 3945 | // conversions in both directions. If only one works, or if the two composite |
| 3946 | // types are the same, we have succeeded. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3947 | // FIXME: extended qualifiers? |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3948 | typedef SmallVector<unsigned, 4> QualifierVector; |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3949 | QualifierVector QualifierUnion; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3950 | typedef SmallVector<std::pair<const Type *, const Type *>, 4> |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3951 | ContainingClassVector; |
| 3952 | ContainingClassVector MemberOfClass; |
| 3953 | QualType Composite1 = Context.getCanonicalType(T1), |
| 3954 | Composite2 = Context.getCanonicalType(T2); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3955 | unsigned NeedConstBefore = 0; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3956 | do { |
| 3957 | const PointerType *Ptr1, *Ptr2; |
| 3958 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 3959 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 3960 | Composite1 = Ptr1->getPointeeType(); |
| 3961 | Composite2 = Ptr2->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3963 | // If we're allowed to create a non-standard composite type, keep track |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3964 | // of where we need to fill in additional 'const' qualifiers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3965 | if (NonStandardCompositeType && |
| 3966 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 3967 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3968 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3969 | QualifierUnion.push_back( |
| 3970 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 3971 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); |
| 3972 | continue; |
| 3973 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3974 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3975 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 3976 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 3977 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 3978 | Composite1 = MemPtr1->getPointeeType(); |
| 3979 | Composite2 = MemPtr2->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3980 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3981 | // If we're allowed to create a non-standard composite type, keep track |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3982 | // of where we need to fill in additional 'const' qualifiers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3983 | if (NonStandardCompositeType && |
| 3984 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 3985 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3986 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3987 | QualifierUnion.push_back( |
| 3988 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 3989 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 3990 | MemPtr2->getClass())); |
| 3991 | continue; |
| 3992 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3994 | // FIXME: block pointer types? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3995 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3996 | // Cannot unwrap any more types. |
| 3997 | break; |
| 3998 | } while (true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4000 | if (NeedConstBefore && NonStandardCompositeType) { |
| 4001 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4002 | // mismatch, so that our (non-standard!) composite type meets the |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 4003 | // requirements of C++ [conv.qual]p4 bullet 3. |
| 4004 | for (unsigned I = 0; I != NeedConstBefore; ++I) { |
| 4005 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) { |
| 4006 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
| 4007 | *NonStandardCompositeType = true; |
| 4008 | } |
| 4009 | } |
| 4010 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4011 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4012 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 4013 | ContainingClassVector::reverse_iterator MOC |
| 4014 | = MemberOfClass.rbegin(); |
| 4015 | for (QualifierVector::reverse_iterator |
| 4016 | I = QualifierUnion.rbegin(), |
| 4017 | E = QualifierUnion.rend(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4018 | I != E; (void)++I, ++MOC) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4019 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4020 | if (MOC->first && MOC->second) { |
| 4021 | // Rebuild member pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4022 | Composite1 = Context.getMemberPointerType( |
| 4023 | Context.getQualifiedType(Composite1, Quals), |
| 4024 | MOC->first); |
| 4025 | Composite2 = Context.getMemberPointerType( |
| 4026 | Context.getQualifiedType(Composite2, Quals), |
| 4027 | MOC->second); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4028 | } else { |
| 4029 | // Rebuild pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4030 | Composite1 |
| 4031 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 4032 | Composite2 |
| 4033 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4034 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4037 | // Try to convert to the first composite pointer type. |
| 4038 | InitializedEntity Entity1 |
| 4039 | = InitializedEntity::InitializeTemporary(Composite1); |
| 4040 | InitializationKind Kind |
| 4041 | = InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 4042 | InitializationSequence E1ToC1(*this, Entity1, Kind, &E1, 1); |
| 4043 | InitializationSequence E2ToC1(*this, Entity1, Kind, &E2, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4045 | if (E1ToC1 && E2ToC1) { |
| 4046 | // Conversion to Composite1 is viable. |
| 4047 | if (!Context.hasSameType(Composite1, Composite2)) { |
| 4048 | // Composite2 is a different type from Composite1. Check whether |
| 4049 | // Composite2 is also viable. |
| 4050 | InitializedEntity Entity2 |
| 4051 | = InitializedEntity::InitializeTemporary(Composite2); |
| 4052 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 4053 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 4054 | if (E1ToC2 && E2ToC2) { |
| 4055 | // Both Composite1 and Composite2 are viable and are different; |
| 4056 | // this is an ambiguity. |
| 4057 | return QualType(); |
| 4058 | } |
| 4059 | } |
| 4060 | |
| 4061 | // Convert E1 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4062 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4063 | = E1ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E1,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4064 | if (E1Result.isInvalid()) |
| 4065 | return QualType(); |
| 4066 | E1 = E1Result.takeAs<Expr>(); |
| 4067 | |
| 4068 | // Convert E2 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4069 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4070 | = E2ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E2,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4071 | if (E2Result.isInvalid()) |
| 4072 | return QualType(); |
| 4073 | E2 = E2Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4074 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4075 | return Composite1; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4078 | // Check whether Composite2 is viable. |
| 4079 | InitializedEntity Entity2 |
| 4080 | = InitializedEntity::InitializeTemporary(Composite2); |
| 4081 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 4082 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 4083 | if (!E1ToC2 || !E2ToC2) |
| 4084 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4085 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4086 | // Convert E1 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4087 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4088 | = E1ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E1, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4089 | if (E1Result.isInvalid()) |
| 4090 | return QualType(); |
| 4091 | E1 = E1Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4092 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4093 | // Convert E2 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4094 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4095 | = E2ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E2, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4096 | if (E2Result.isInvalid()) |
| 4097 | return QualType(); |
| 4098 | E2 = E2Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4099 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4100 | return Composite2; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4101 | } |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 4102 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4103 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Douglas Gregor | 19cc1c7 | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 4104 | if (!E) |
| 4105 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4106 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4107 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 4108 | |
| 4109 | // If the result is a glvalue, we shouldn't bind it. |
| 4110 | if (!E->isRValue()) |
Anders Carlsson | 089c260 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 4111 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4112 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4113 | // In ARC, calls that return a retainable type can return retained, |
| 4114 | // in which case we have to insert a consuming cast. |
| 4115 | if (getLangOptions().ObjCAutoRefCount && |
| 4116 | E->getType()->isObjCRetainableType()) { |
| 4117 | |
| 4118 | bool ReturnsRetained; |
| 4119 | |
| 4120 | // For actual calls, we compute this by examining the type of the |
| 4121 | // called value. |
| 4122 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { |
| 4123 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
| 4124 | QualType T = Callee->getType(); |
| 4125 | |
| 4126 | if (T == Context.BoundMemberTy) { |
| 4127 | // Handle pointer-to-members. |
| 4128 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) |
| 4129 | T = BinOp->getRHS()->getType(); |
| 4130 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) |
| 4131 | T = Mem->getMemberDecl()->getType(); |
| 4132 | } |
| 4133 | |
| 4134 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
| 4135 | T = Ptr->getPointeeType(); |
| 4136 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
| 4137 | T = Ptr->getPointeeType(); |
| 4138 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
| 4139 | T = MemPtr->getPointeeType(); |
| 4140 | |
| 4141 | const FunctionType *FTy = T->getAs<FunctionType>(); |
| 4142 | assert(FTy && "call to value not of function type?"); |
| 4143 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
| 4144 | |
| 4145 | // ActOnStmtExpr arranges things so that StmtExprs of retainable |
| 4146 | // type always produce a +1 object. |
| 4147 | } else if (isa<StmtExpr>(E)) { |
| 4148 | ReturnsRetained = true; |
| 4149 | |
| 4150 | // For message sends and property references, we try to find an |
| 4151 | // actual method. FIXME: we should infer retention by selector in |
| 4152 | // cases where we don't have an actual method. |
| 4153 | } else { |
John McCall | fc4b191 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 4154 | ObjCMethodDecl *D = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4155 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { |
| 4156 | D = Send->getMethodDecl(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
John McCall | fc4b191 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 4160 | |
| 4161 | // Don't do reclaims on performSelector calls; despite their |
| 4162 | // return type, the invoked method doesn't necessarily actually |
| 4163 | // return an object. |
| 4164 | if (!ReturnsRetained && |
| 4165 | D && D->getMethodFamily() == OMF_performSelector) |
| 4166 | return Owned(E); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
John McCall | 567c586 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 4169 | // Don't reclaim an object of Class type. |
| 4170 | if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType()) |
| 4171 | return Owned(E); |
| 4172 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4173 | ExprNeedsCleanups = true; |
| 4174 | |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4175 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject |
| 4176 | : CK_ARCReclaimReturnedObject); |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4177 | return Owned(ImplicitCastExpr::Create(Context, E->getType(), ck, E, 0, |
| 4178 | VK_RValue)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | if (!getLangOptions().CPlusPlus) |
| 4182 | return Owned(E); |
Douglas Gregor | 5132655 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 4183 | |
Peter Collingbourne | bceb755 | 2011-11-27 22:09:28 +0000 | [diff] [blame] | 4184 | QualType ET = Context.getBaseElementType(E->getType()); |
| 4185 | const RecordType *RT = ET->getAs<RecordType>(); |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 4186 | if (!RT) |
| 4187 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4189 | // That should be enough to guarantee that this type is complete. |
| 4190 | // If it has a trivial destructor, we can avoid the extra copy. |
Jeffrey Yasskin | b7ee2e5 | 2011-01-27 19:17:54 +0000 | [diff] [blame] | 4191 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 507384f | 2010-08-12 02:40:37 +0000 | [diff] [blame] | 4192 | if (RD->isInvalidDecl() || RD->hasTrivialDestructor()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4193 | return Owned(E); |
| 4194 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4195 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
| 4196 | |
| 4197 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); |
| 4198 | if (Destructor) { |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 4199 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 4200 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 4201 | PDiag(diag::err_access_dtor_temp) |
| 4202 | << E->getType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4203 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4204 | // We need a cleanup, but we don't need to remember the temporary. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4205 | ExprNeedsCleanups = true; |
John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 4206 | } |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 4207 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); |
| 4208 | } |
| 4209 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4210 | ExprResult |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4211 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4212 | if (SubExpr.isInvalid()) |
| 4213 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4214 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4215 | return Owned(MaybeCreateExprWithCleanups(SubExpr.take())); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4218 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
| 4219 | assert(SubExpr && "sub expression can't be null!"); |
| 4220 | |
| 4221 | unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects; |
| 4222 | assert(ExprCleanupObjects.size() >= FirstCleanup); |
| 4223 | assert(ExprNeedsCleanups || ExprCleanupObjects.size() == FirstCleanup); |
| 4224 | if (!ExprNeedsCleanups) |
| 4225 | return SubExpr; |
| 4226 | |
| 4227 | ArrayRef<ExprWithCleanups::CleanupObject> Cleanups |
| 4228 | = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup, |
| 4229 | ExprCleanupObjects.size() - FirstCleanup); |
| 4230 | |
| 4231 | Expr *E = ExprWithCleanups::Create(Context, SubExpr, Cleanups); |
| 4232 | DiscardCleanupsInEvaluationContext(); |
| 4233 | |
| 4234 | return E; |
| 4235 | } |
| 4236 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4237 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4238 | assert(SubStmt && "sub statement can't be null!"); |
| 4239 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4240 | if (!ExprNeedsCleanups) |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4241 | return SubStmt; |
| 4242 | |
| 4243 | // FIXME: In order to attach the temporaries, wrap the statement into |
| 4244 | // a StmtExpr; currently this is only used for asm statements. |
| 4245 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
| 4246 | // a new AsmStmtWithTemporaries. |
| 4247 | CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, &SubStmt, 1, |
| 4248 | SourceLocation(), |
| 4249 | SourceLocation()); |
| 4250 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
| 4251 | SourceLocation()); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4252 | return MaybeCreateExprWithCleanups(E); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4255 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4256 | Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4257 | tok::TokenKind OpKind, ParsedType &ObjectType, |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4258 | bool &MayBePseudoDestructor) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4259 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4260 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4261 | if (Result.isInvalid()) return ExprError(); |
| 4262 | Base = Result.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4263 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4264 | Result = CheckPlaceholderExpr(Base); |
| 4265 | if (Result.isInvalid()) return ExprError(); |
| 4266 | Base = Result.take(); |
| 4267 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4268 | QualType BaseType = Base->getType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4269 | MayBePseudoDestructor = false; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4270 | if (BaseType->isDependentType()) { |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 4271 | // If we have a pointer to a dependent type and are using the -> operator, |
| 4272 | // the object type is the type that the pointer points to. We might still |
| 4273 | // have enough information about that type to do something useful. |
| 4274 | if (OpKind == tok::arrow) |
| 4275 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4276 | BaseType = Ptr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4277 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4278 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4279 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4280 | return Owned(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4283 | // C++ [over.match.oper]p8: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4284 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4285 | // returned, with the original second operand. |
| 4286 | if (OpKind == tok::arrow) { |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4287 | // The set of types we've considered so far. |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4288 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4289 | SmallVector<SourceLocation, 8> Locations; |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4290 | CTypes.insert(Context.getCanonicalType(BaseType)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4291 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4292 | while (BaseType->isRecordType()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4293 | Result = BuildOverloadedArrowExpr(S, Base, OpLoc); |
| 4294 | if (Result.isInvalid()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4295 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4296 | Base = Result.get(); |
| 4297 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Anders Carlsson | de699e5 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 4298 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4299 | BaseType = Base->getType(); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4300 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4301 | if (!CTypes.insert(CBaseType)) { |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4302 | Diag(OpLoc, diag::err_operator_arrow_circular); |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 4303 | for (unsigned i = 0; i < Locations.size(); i++) |
| 4304 | Diag(Locations[i], diag::note_declared_at); |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4305 | return ExprError(); |
| 4306 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4307 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4308 | |
Douglas Gregor | 1d7049a | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 4309 | if (BaseType->isPointerType() || BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 31658df | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 4310 | BaseType = BaseType->getPointeeType(); |
| 4311 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4312 | |
Douglas Gregor | 1d7049a | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 4313 | // Objective-C properties allow "." access on Objective-C pointer types, |
| 4314 | // so adjust the base type to the object type itself. |
| 4315 | if (BaseType->isObjCObjectPointerType()) |
| 4316 | BaseType = BaseType->getPointeeType(); |
| 4317 | |
| 4318 | // C++ [basic.lookup.classref]p2: |
| 4319 | // [...] If the type of the object expression is of pointer to scalar |
| 4320 | // type, the unqualified-id is looked up in the context of the complete |
| 4321 | // postfix-expression. |
| 4322 | // |
| 4323 | // This also indicates that we could be parsing a pseudo-destructor-name. |
| 4324 | // Note that Objective-C class and object types can be pseudo-destructor |
| 4325 | // expressions or normal member (ivar or property) access expressions. |
| 4326 | if (BaseType->isObjCObjectOrInterfaceType()) { |
| 4327 | MayBePseudoDestructor = true; |
| 4328 | } else if (!BaseType->isRecordType()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4329 | ObjectType = ParsedType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4330 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4331 | return Owned(Base); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4332 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4333 | |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4334 | // The object type must be complete (or dependent). |
| 4335 | if (!BaseType->isDependentType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4336 | RequireCompleteType(OpLoc, BaseType, |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4337 | PDiag(diag::err_incomplete_member_access))) |
| 4338 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4339 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4340 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4341 | // If the id-expression in a class member access (5.2.5) is an |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4342 | // unqualified-id, and the type of the object expression is of a class |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4343 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 4344 | // up in the scope of class C. [...] |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4345 | ObjectType = ParsedType::make(BaseType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4346 | return move(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4349 | ExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4350 | Expr *MemExpr) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4351 | SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4352 | Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call) |
| 4353 | << isa<CXXPseudoDestructorExpr>(MemExpr) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4354 | << FixItHint::CreateInsertion(ExpectedLParenLoc, "()"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4355 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4356 | return ActOnCallExpr(/*Scope*/ 0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4357 | MemExpr, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4358 | /*LPLoc*/ ExpectedLParenLoc, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4359 | MultiExprArg(), |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4360 | /*RPLoc*/ ExpectedLParenLoc); |
| 4361 | } |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4362 | |
Eli Friedman | e0dbedf | 2012-01-25 04:29:24 +0000 | [diff] [blame^] | 4363 | static bool CheckArrow(Sema& S, QualType& ObjectType, Expr *&Base, |
David Blaikie | 91ec789 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4364 | tok::TokenKind& OpKind, SourceLocation OpLoc) { |
Eli Friedman | e0dbedf | 2012-01-25 04:29:24 +0000 | [diff] [blame^] | 4365 | if (Base->hasPlaceholderType()) { |
| 4366 | ExprResult result = S.CheckPlaceholderExpr(Base); |
| 4367 | if (result.isInvalid()) return true; |
| 4368 | Base = result.take(); |
| 4369 | } |
| 4370 | ObjectType = Base->getType(); |
| 4371 | |
David Blaikie | 91ec789 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4372 | // C++ [expr.pseudo]p2: |
| 4373 | // The left-hand side of the dot operator shall be of scalar type. The |
| 4374 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 4375 | // This scalar type is the object type. |
Eli Friedman | e0dbedf | 2012-01-25 04:29:24 +0000 | [diff] [blame^] | 4376 | // Note that this is rather different from the normal handling for the |
| 4377 | // arrow operator. |
David Blaikie | 91ec789 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4378 | if (OpKind == tok::arrow) { |
| 4379 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 4380 | ObjectType = Ptr->getPointeeType(); |
| 4381 | } else if (!Base->isTypeDependent()) { |
| 4382 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 4383 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 4384 | << ObjectType << true |
| 4385 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 4386 | if (S.isSFINAEContext()) |
| 4387 | return true; |
| 4388 | |
| 4389 | OpKind = tok::period; |
| 4390 | } |
| 4391 | } |
| 4392 | |
| 4393 | return false; |
| 4394 | } |
| 4395 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4396 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4397 | SourceLocation OpLoc, |
| 4398 | tok::TokenKind OpKind, |
| 4399 | const CXXScopeSpec &SS, |
| 4400 | TypeSourceInfo *ScopeTypeInfo, |
| 4401 | SourceLocation CCLoc, |
| 4402 | SourceLocation TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4403 | PseudoDestructorTypeStorage Destructed, |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4404 | bool HasTrailingLParen) { |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4405 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4406 | |
David Blaikie | 91ec789 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4407 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 4408 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4409 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4410 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType()) { |
Nico Weber | df1be86 | 2012-01-23 05:50:57 +0000 | [diff] [blame] | 4411 | if (getLangOptions().MicrosoftMode && ObjectType->isVoidType()) |
Nico Weber | 2d757ec | 2012-01-23 06:08:16 +0000 | [diff] [blame] | 4412 | Diag(OpLoc, diag::ext_pseudo_dtor_on_void) << Base->getSourceRange(); |
Nico Weber | df1be86 | 2012-01-23 05:50:57 +0000 | [diff] [blame] | 4413 | else |
| 4414 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 4415 | << ObjectType << Base->getSourceRange(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4416 | return ExprError(); |
| 4417 | } |
| 4418 | |
| 4419 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | // [...] The cv-unqualified versions of the object type and of the type |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4421 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4422 | if (DestructedTypeInfo) { |
| 4423 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 4424 | SourceLocation DestructedTypeStart |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4425 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4426 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
| 4427 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
| 4428 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
| 4429 | << ObjectType << DestructedType << Base->getSourceRange() |
| 4430 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4431 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4432 | // Recover by setting the destructed type to the object type. |
| 4433 | DestructedType = ObjectType; |
| 4434 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4435 | DestructedTypeStart); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4436 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4437 | } else if (DestructedType.getObjCLifetime() != |
| 4438 | ObjectType.getObjCLifetime()) { |
| 4439 | |
| 4440 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
| 4441 | // Okay: just pretend that the user provided the correctly-qualified |
| 4442 | // type. |
| 4443 | } else { |
| 4444 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
| 4445 | << ObjectType << DestructedType << Base->getSourceRange() |
| 4446 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 4447 | } |
| 4448 | |
| 4449 | // Recover by setting the destructed type to the object type. |
| 4450 | DestructedType = ObjectType; |
| 4451 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 4452 | DestructedTypeStart); |
| 4453 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4454 | } |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4455 | } |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4456 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4457 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4458 | // C++ [expr.pseudo]p2: |
| 4459 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 4460 | // form |
| 4461 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4462 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4463 | // |
| 4464 | // shall designate the same scalar type. |
| 4465 | if (ScopeTypeInfo) { |
| 4466 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 4467 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | 81e317a | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 4468 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4469 | |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4470 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4471 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4472 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4473 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4475 | ScopeType = QualType(); |
| 4476 | ScopeTypeInfo = 0; |
| 4477 | } |
| 4478 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4479 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4480 | Expr *Result |
| 4481 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 4482 | OpKind == tok::arrow, OpLoc, |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 4483 | SS.getWithLocInContext(Context), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4484 | ScopeTypeInfo, |
| 4485 | CCLoc, |
| 4486 | TildeLoc, |
| 4487 | Destructed); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4488 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4489 | if (HasTrailingLParen) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4490 | return Owned(Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4491 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4492 | return DiagnoseDtorReference(Destructed.getLocation(), Result); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4495 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4496 | SourceLocation OpLoc, |
| 4497 | tok::TokenKind OpKind, |
| 4498 | CXXScopeSpec &SS, |
| 4499 | UnqualifiedId &FirstTypeName, |
| 4500 | SourceLocation CCLoc, |
| 4501 | SourceLocation TildeLoc, |
| 4502 | UnqualifiedId &SecondTypeName, |
| 4503 | bool HasTrailingLParen) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4504 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 4505 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 4506 | "Invalid first type name in pseudo-destructor"); |
| 4507 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 4508 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 4509 | "Invalid second type name in pseudo-destructor"); |
| 4510 | |
David Blaikie | 91ec789 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4511 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 4512 | return ExprError(); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4513 | |
| 4514 | // Compute the object type that we should use for name lookup purposes. Only |
| 4515 | // record types and dependent types matter. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4516 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4517 | if (!SS.isSet()) { |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4518 | if (ObjectType->isRecordType()) |
| 4519 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4520 | else if (ObjectType->isDependentType()) |
| 4521 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4522 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4523 | |
| 4524 | // Convert the name of the type being destructed (following the ~) into a |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4525 | // type (with source-location information). |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4526 | QualType DestructedType; |
| 4527 | TypeSourceInfo *DestructedTypeInfo = 0; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4528 | PseudoDestructorTypeStorage Destructed; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4529 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4530 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4531 | SecondTypeName.StartLocation, |
Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 4532 | S, &SS, true, false, ObjectTypePtrForLookup); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4533 | if (!T && |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4534 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 4535 | (!SS.isSet() && ObjectType->isDependentType()))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4536 | // The name of the type being destroyed is a dependent name, and we |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4537 | // couldn't find anything useful in scope. Just store the identifier and |
| 4538 | // it's location, and we'll perform (qualified) name lookup again at |
| 4539 | // template instantiation time. |
| 4540 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 4541 | SecondTypeName.StartLocation); |
| 4542 | } else if (!T) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | Diag(SecondTypeName.StartLocation, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4544 | diag::err_pseudo_dtor_destructor_non_type) |
| 4545 | << SecondTypeName.Identifier << ObjectType; |
| 4546 | if (isSFINAEContext()) |
| 4547 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4548 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4549 | // Recover by assuming we had the right type all along. |
| 4550 | DestructedType = ObjectType; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4551 | } else |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4552 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4553 | } else { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4554 | // Resolve the template-id to a type. |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4555 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4556 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4557 | TemplateId->getTemplateArgs(), |
| 4558 | TemplateId->NumArgs); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 4559 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
| 4560 | TemplateId->Template, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4561 | TemplateId->TemplateNameLoc, |
| 4562 | TemplateId->LAngleLoc, |
| 4563 | TemplateArgsPtr, |
| 4564 | TemplateId->RAngleLoc); |
| 4565 | if (T.isInvalid() || !T.get()) { |
| 4566 | // Recover by assuming we had the right type all along. |
| 4567 | DestructedType = ObjectType; |
| 4568 | } else |
| 4569 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4570 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4571 | |
| 4572 | // If we've performed some kind of recovery, (re-)build the type source |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4573 | // information. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4574 | if (!DestructedType.isNull()) { |
| 4575 | if (!DestructedTypeInfo) |
| 4576 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4577 | SecondTypeName.StartLocation); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4578 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4579 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4580 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4581 | // Convert the name of the scope type (the type prior to '::') into a type. |
| 4582 | TypeSourceInfo *ScopeTypeInfo = 0; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4583 | QualType ScopeType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4584 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4585 | FirstTypeName.Identifier) { |
| 4586 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4587 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4588 | FirstTypeName.StartLocation, |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 4589 | S, &SS, true, false, ObjectTypePtrForLookup); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4590 | if (!T) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4591 | Diag(FirstTypeName.StartLocation, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4592 | diag::err_pseudo_dtor_destructor_non_type) |
| 4593 | << FirstTypeName.Identifier << ObjectType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4594 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4595 | if (isSFINAEContext()) |
| 4596 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4597 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4598 | // Just drop this type. It's unnecessary anyway. |
| 4599 | ScopeType = QualType(); |
| 4600 | } else |
| 4601 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4602 | } else { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4603 | // Resolve the template-id to a type. |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4604 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4605 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4606 | TemplateId->getTemplateArgs(), |
| 4607 | TemplateId->NumArgs); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 4608 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
| 4609 | TemplateId->Template, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4610 | TemplateId->TemplateNameLoc, |
| 4611 | TemplateId->LAngleLoc, |
| 4612 | TemplateArgsPtr, |
| 4613 | TemplateId->RAngleLoc); |
| 4614 | if (T.isInvalid() || !T.get()) { |
| 4615 | // Recover by dropping this type. |
| 4616 | ScopeType = QualType(); |
| 4617 | } else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4618 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4619 | } |
| 4620 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4621 | |
Douglas Gregor | b4a418f | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 4622 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 4623 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 4624 | FirstTypeName.StartLocation); |
| 4625 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4626 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4627 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 4628 | ScopeTypeInfo, CCLoc, TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4629 | Destructed, HasTrailingLParen); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
David Blaikie | 91ec789 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4632 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
| 4633 | SourceLocation OpLoc, |
| 4634 | tok::TokenKind OpKind, |
| 4635 | SourceLocation TildeLoc, |
| 4636 | const DeclSpec& DS, |
| 4637 | bool HasTrailingLParen) { |
| 4638 | |
David Blaikie | 91ec789 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 4639 | if (CheckArrow(*this, ObjectType, Base, OpKind, OpLoc)) |
| 4640 | return ExprError(); |
| 4641 | |
| 4642 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 4643 | |
| 4644 | TypeLocBuilder TLB; |
| 4645 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
| 4646 | DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 4647 | TypeSourceInfo *DestructedTypeInfo = TLB.getTypeSourceInfo(Context, T); |
| 4648 | PseudoDestructorTypeStorage Destructed(DestructedTypeInfo); |
| 4649 | |
| 4650 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, CXXScopeSpec(), |
| 4651 | 0, SourceLocation(), TildeLoc, |
| 4652 | Destructed, HasTrailingLParen); |
| 4653 | } |
| 4654 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4655 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4656 | CXXMethodDecl *Method, |
| 4657 | bool HadMultipleCandidates) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4658 | ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/0, |
| 4659 | FoundDecl, Method); |
| 4660 | if (Exp.isInvalid()) |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4661 | return true; |
Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 4662 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | MemberExpr *ME = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4664 | new (Context) MemberExpr(Exp.take(), /*IsArrow=*/false, Method, |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 4665 | SourceLocation(), Context.BoundMemberTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4666 | VK_RValue, OK_Ordinary); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4667 | if (HadMultipleCandidates) |
| 4668 | ME->setHadMultipleCandidates(true); |
| 4669 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4670 | QualType ResultType = Method->getResultType(); |
| 4671 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 4672 | ResultType = ResultType.getNonLValueExprType(Context); |
| 4673 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4674 | MarkDeclarationReferenced(Exp.get()->getLocStart(), Method); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4675 | CXXMemberCallExpr *CE = |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4676 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, VK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4677 | Exp.get()->getLocEnd()); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 4678 | return CE; |
| 4679 | } |
| 4680 | |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4681 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 4682 | SourceLocation RParen) { |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4683 | return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand, |
| 4684 | Operand->CanThrow(Context), |
| 4685 | KeyLoc, RParen)); |
| 4686 | } |
| 4687 | |
| 4688 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 4689 | Expr *Operand, SourceLocation RParen) { |
| 4690 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4693 | /// Perform the conversions required for an expression used in a |
| 4694 | /// context that ignores the result. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4695 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4696 | if (E->hasPlaceholderType()) { |
| 4697 | ExprResult result = CheckPlaceholderExpr(E); |
| 4698 | if (result.isInvalid()) return Owned(E); |
| 4699 | E = result.take(); |
| 4700 | } |
| 4701 | |
John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 4702 | // C99 6.3.2.1: |
| 4703 | // [Except in specific positions,] an lvalue that does not have |
| 4704 | // array type is converted to the value stored in the |
| 4705 | // designated object (and is no longer an lvalue). |
John McCall | e6d134b | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 4706 | if (E->isRValue()) { |
| 4707 | // In C, function designators (i.e. expressions of function type) |
| 4708 | // are r-values, but we still want to do function-to-pointer decay |
| 4709 | // on them. This is both technically correct and convenient for |
| 4710 | // some clients. |
| 4711 | if (!getLangOptions().CPlusPlus && E->getType()->isFunctionType()) |
| 4712 | return DefaultFunctionArrayConversion(E); |
| 4713 | |
| 4714 | return Owned(E); |
| 4715 | } |
John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 4716 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4717 | // Otherwise, this rule does not apply in C++, at least not for the moment. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4718 | if (getLangOptions().CPlusPlus) return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4719 | |
| 4720 | // GCC seems to also exclude expressions of incomplete enum type. |
| 4721 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
| 4722 | if (!T->getDecl()->isComplete()) { |
| 4723 | // FIXME: stupid workaround for a codegen bug! |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4724 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).take(); |
| 4725 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4726 | } |
| 4727 | } |
| 4728 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4729 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 4730 | if (Res.isInvalid()) |
| 4731 | return Owned(E); |
| 4732 | E = Res.take(); |
| 4733 | |
John McCall | 85515d6 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 4734 | if (!E->getType()->isVoidType()) |
| 4735 | RequireCompleteType(E->getExprLoc(), E->getType(), |
| 4736 | diag::err_incomplete_type); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4737 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4740 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE) { |
| 4741 | ExprResult FullExpr = Owned(FE); |
| 4742 | |
| 4743 | if (!FullExpr.get()) |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4744 | return ExprError(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4745 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4746 | if (DiagnoseUnexpandedParameterPack(FullExpr.get())) |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 4747 | return ExprError(); |
| 4748 | |
Douglas Gregor | 5e3a8be | 2011-12-15 00:53:32 +0000 | [diff] [blame] | 4749 | // Top-level message sends default to 'id' when we're in a debugger. |
| 4750 | if (getLangOptions().DebuggerSupport && |
| 4751 | FullExpr.get()->getType() == Context.UnknownAnyTy && |
| 4752 | isa<ObjCMessageExpr>(FullExpr.get())) { |
| 4753 | FullExpr = forceUnknownAnyToType(FullExpr.take(), Context.getObjCIdType()); |
| 4754 | if (FullExpr.isInvalid()) |
| 4755 | return ExprError(); |
| 4756 | } |
| 4757 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4758 | FullExpr = CheckPlaceholderExpr(FullExpr.take()); |
| 4759 | if (FullExpr.isInvalid()) |
| 4760 | return ExprError(); |
Douglas Gregor | 353ee24 | 2011-03-07 02:05:23 +0000 | [diff] [blame] | 4761 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4762 | FullExpr = IgnoredValueConversions(FullExpr.take()); |
| 4763 | if (FullExpr.isInvalid()) |
| 4764 | return ExprError(); |
| 4765 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4766 | CheckImplicitConversions(FullExpr.get(), FullExpr.get()->getExprLoc()); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4767 | return MaybeCreateExprWithCleanups(FullExpr); |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 4768 | } |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4769 | |
| 4770 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
| 4771 | if (!FullStmt) return StmtError(); |
| 4772 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4773 | return MaybeCreateStmtWithCleanups(FullStmt); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4774 | } |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4775 | |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4776 | Sema::IfExistsResult |
| 4777 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, |
| 4778 | CXXScopeSpec &SS, |
| 4779 | const DeclarationNameInfo &TargetNameInfo) { |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4780 | DeclarationName TargetName = TargetNameInfo.getName(); |
| 4781 | if (!TargetName) |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4782 | return IER_DoesNotExist; |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4784 | // If the name itself is dependent, then the result is dependent. |
| 4785 | if (TargetName.isDependentName()) |
| 4786 | return IER_Dependent; |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4787 | |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4788 | // Do the redeclaration lookup in the current scope. |
| 4789 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
| 4790 | Sema::NotForRedeclaration); |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4791 | LookupParsedName(R, S, &SS); |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4792 | R.suppressDiagnostics(); |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4793 | |
| 4794 | switch (R.getResultKind()) { |
| 4795 | case LookupResult::Found: |
| 4796 | case LookupResult::FoundOverloaded: |
| 4797 | case LookupResult::FoundUnresolvedValue: |
| 4798 | case LookupResult::Ambiguous: |
| 4799 | return IER_Exists; |
| 4800 | |
| 4801 | case LookupResult::NotFound: |
| 4802 | return IER_DoesNotExist; |
| 4803 | |
| 4804 | case LookupResult::NotFoundInCurrentInstantiation: |
| 4805 | return IER_Dependent; |
| 4806 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4807 | |
| 4808 | llvm_unreachable("Invalid LookupResult Kind!"); |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4809 | } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4810 | |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 4811 | Sema::IfExistsResult |
| 4812 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc, |
| 4813 | bool IsIfExists, CXXScopeSpec &SS, |
| 4814 | UnqualifiedId &Name) { |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4815 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 4816 | |
| 4817 | // Check for unexpanded parameter packs. |
| 4818 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
| 4819 | collectUnexpandedParameterPacks(SS, Unexpanded); |
| 4820 | collectUnexpandedParameterPacks(TargetNameInfo, Unexpanded); |
| 4821 | if (!Unexpanded.empty()) { |
| 4822 | DiagnoseUnexpandedParameterPacks(KeywordLoc, |
| 4823 | IsIfExists? UPPC_IfExists |
| 4824 | : UPPC_IfNotExists, |
| 4825 | Unexpanded); |
| 4826 | return IER_Error; |
| 4827 | } |
| 4828 | |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4829 | return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo); |
| 4830 | } |
| 4831 | |
Eli Friedman | dc3b723 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 4832 | //===----------------------------------------------------------------------===// |
| 4833 | // Lambdas. |
| 4834 | //===----------------------------------------------------------------------===// |
| 4835 | |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 4836 | void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, |
| 4837 | Declarator &ParamInfo, |
| 4838 | Scope *CurScope) { |
| 4839 | DeclContext *DC = CurContext; |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 4840 | while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext())) |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 4841 | DC = DC->getParent(); |
Eli Friedman | dc3b723 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 4842 | |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 4843 | // Start constructing the lambda class. |
| 4844 | CXXRecordDecl *Class = CXXRecordDecl::Create(Context, TTK_Class, DC, |
| 4845 | Intro.Range.getBegin(), |
| 4846 | /*IdLoc=*/SourceLocation(), |
| 4847 | /*Id=*/0); |
| 4848 | Class->startDefinition(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 4849 | Class->setLambda(true); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 4850 | CurContext->addDecl(Class); |
Eli Friedman | dc3b723 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 4851 | |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 4852 | QualType ThisCaptureType; |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 4853 | llvm::DenseMap<VarDecl*, unsigned> CaptureMap; |
| 4854 | unsigned CXXThisCaptureIndex = 0; |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 4855 | llvm::SmallVector<LambdaScopeInfo::Capture, 4> Captures; |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 4856 | for (llvm::SmallVector<LambdaCapture, 4>::const_iterator |
| 4857 | C = Intro.Captures.begin(), E = Intro.Captures.end(); C != E; ++C) { |
| 4858 | if (C->Kind == LCK_This) { |
| 4859 | if (!ThisCaptureType.isNull()) { |
| 4860 | Diag(C->Loc, diag::err_capture_more_than_once) << "'this'"; |
| 4861 | continue; |
| 4862 | } |
| 4863 | |
| 4864 | if (Intro.Default == LCD_ByCopy) { |
| 4865 | Diag(C->Loc, diag::err_this_capture_with_copy_default); |
| 4866 | continue; |
| 4867 | } |
| 4868 | |
| 4869 | ThisCaptureType = getCurrentThisType(); |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 4870 | if (ThisCaptureType.isNull()) { |
| 4871 | Diag(C->Loc, diag::err_invalid_this_use); |
| 4872 | continue; |
| 4873 | } |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 4874 | CheckCXXThisCapture(C->Loc); |
| 4875 | |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 4876 | // FIXME: Need getCurCapture(). |
| 4877 | bool isNested = getCurBlock() || getCurLambda(); |
| 4878 | CapturingScopeInfo::Capture Cap(CapturingScopeInfo::Capture::ThisCapture, |
| 4879 | isNested); |
| 4880 | Captures.push_back(Cap); |
| 4881 | CXXThisCaptureIndex = Captures.size(); |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 4882 | continue; |
| 4883 | } |
| 4884 | |
| 4885 | assert(C->Id && "missing identifier for capture"); |
| 4886 | |
| 4887 | if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) { |
| 4888 | Diag(C->Loc, diag::err_reference_capture_with_reference_default); |
| 4889 | continue; |
| 4890 | } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) { |
| 4891 | Diag(C->Loc, diag::err_copy_capture_with_copy_default); |
| 4892 | continue; |
| 4893 | } |
| 4894 | |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 4895 | DeclarationNameInfo Name(C->Id, C->Loc); |
| 4896 | LookupResult R(*this, Name, LookupOrdinaryName); |
| 4897 | CXXScopeSpec ScopeSpec; |
| 4898 | LookupParsedName(R, CurScope, &ScopeSpec); |
| 4899 | if (R.isAmbiguous()) |
| 4900 | continue; |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 4901 | if (R.empty()) { |
| 4902 | DeclFilterCCC<VarDecl> Validator; |
| 4903 | if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator)) |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 4904 | continue; |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 4905 | } |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 4906 | |
| 4907 | VarDecl *Var = R.getAsSingle<VarDecl>(); |
| 4908 | if (!Var) { |
| 4909 | Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id; |
| 4910 | continue; |
| 4911 | } |
| 4912 | |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 4913 | if (CaptureMap.count(Var)) { |
| 4914 | Diag(C->Loc, diag::err_capture_more_than_once) << C->Id; |
| 4915 | continue; |
| 4916 | } |
| 4917 | |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 4918 | if (!Var->hasLocalStorage()) { |
| 4919 | Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id; |
| 4920 | continue; |
| 4921 | } |
| 4922 | |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 4923 | // FIXME: This is completely wrong for nested captures and variables |
| 4924 | // with a non-trivial constructor. |
| 4925 | // FIXME: We should refuse to capture __block variables. |
| 4926 | Captures.push_back(LambdaScopeInfo::Capture(Var, C->Kind == LCK_ByRef, |
| 4927 | /*isNested*/false, 0)); |
| 4928 | CaptureMap[Var] = Captures.size(); |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 4931 | // Build the call operator; we don't really have all the relevant information |
| 4932 | // at this point, but we need something to attach child declarations to. |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 4933 | QualType MethodTy; |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 4934 | TypeSourceInfo *MethodTyInfo; |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 4935 | if (ParamInfo.getNumTypeObjects() == 0) { |
| 4936 | FunctionProtoType::ExtProtoInfo EPI; |
| 4937 | EPI.TypeQuals |= DeclSpec::TQ_const; |
| 4938 | MethodTy = Context.getFunctionType(Context.DependentTy, |
| 4939 | /*Args=*/0, /*NumArgs=*/0, EPI); |
| 4940 | MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy); |
| 4941 | } else { |
| 4942 | assert(ParamInfo.isFunctionDeclarator() && |
| 4943 | "lambda-declarator is a function"); |
| 4944 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo(); |
| 4945 | if (!FTI.hasMutableQualifier()) |
| 4946 | FTI.TypeQuals |= DeclSpec::TQ_const; |
| 4947 | MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope); |
| 4948 | // FIXME: Can these asserts actually fail? |
| 4949 | assert(MethodTyInfo && "no type from lambda-declarator"); |
| 4950 | MethodTy = MethodTyInfo->getType(); |
| 4951 | assert(!MethodTy.isNull() && "no type from lambda declarator"); |
| 4952 | } |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 4953 | |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 4954 | DeclarationName MethodName |
| 4955 | = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
| 4956 | CXXMethodDecl *Method |
| 4957 | = CXXMethodDecl::Create(Context, |
| 4958 | Class, |
| 4959 | ParamInfo.getSourceRange().getEnd(), |
| 4960 | DeclarationNameInfo(MethodName, |
| 4961 | /*NameLoc=*/SourceLocation()), |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 4962 | MethodTy, |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 4963 | MethodTyInfo, |
| 4964 | /*isStatic=*/false, |
| 4965 | SC_None, |
| 4966 | /*isInline=*/true, |
| 4967 | /*isConstExpr=*/false, |
| 4968 | ParamInfo.getSourceRange().getEnd()); |
| 4969 | Method->setAccess(AS_public); |
| 4970 | Class->addDecl(Method); |
| 4971 | Method->setLexicalDeclContext(DC); // FIXME: Is this really correct? |
| 4972 | |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 4973 | ProcessDeclAttributes(CurScope, Method, ParamInfo); |
| 4974 | |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 4975 | // Enter a new evaluation context to insulate the block from any |
| 4976 | // cleanups from the enclosing full-expression. |
| 4977 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
| 4978 | |
| 4979 | PushDeclContext(CurScope, Method); |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 4980 | |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 4981 | // Set the parameters on the decl, if specified. |
| 4982 | if (isa<FunctionProtoTypeLoc>(MethodTyInfo->getTypeLoc())) { |
| 4983 | FunctionProtoTypeLoc Proto = |
| 4984 | cast<FunctionProtoTypeLoc>(MethodTyInfo->getTypeLoc()); |
| 4985 | Method->setParams(Proto.getParams()); |
| 4986 | CheckParmsForFunctionDef(Method->param_begin(), |
| 4987 | Method->param_end(), |
| 4988 | /*CheckParameterNames=*/false); |
| 4989 | |
| 4990 | // Introduce our parameters into the function scope |
| 4991 | for (unsigned p = 0, NumParams = Method->getNumParams(); p < NumParams; ++p) { |
| 4992 | ParmVarDecl *Param = Method->getParamDecl(p); |
| 4993 | Param->setOwningFunction(Method); |
| 4994 | |
| 4995 | // If this has an identifier, add it to the scope stack. |
| 4996 | if (Param->getIdentifier()) { |
| 4997 | CheckShadow(CurScope, Param); |
| 4998 | |
| 4999 | PushOnScopeChains(Param, CurScope); |
| 5000 | } |
| 5001 | } |
| 5002 | } |
| 5003 | |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 5004 | // Introduce the lambda scope. |
| 5005 | PushLambdaScope(Class); |
| 5006 | |
| 5007 | LambdaScopeInfo *LSI = getCurLambda(); |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 5008 | LSI->CXXThisCaptureIndex = CXXThisCaptureIndex; |
| 5009 | std::swap(LSI->CaptureMap, CaptureMap); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 5010 | std::swap(LSI->Captures, Captures); |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 5011 | LSI->NumExplicitCaptures = Captures.size(); |
| 5012 | if (Intro.Default == LCD_ByCopy) |
| 5013 | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval; |
| 5014 | else if (Intro.Default == LCD_ByRef) |
| 5015 | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref; |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 5016 | |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 5017 | const FunctionType *Fn = MethodTy->getAs<FunctionType>(); |
| 5018 | QualType RetTy = Fn->getResultType(); |
| 5019 | if (RetTy != Context.DependentTy) { |
| 5020 | LSI->ReturnType = RetTy; |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 5021 | } else { |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 5022 | LSI->HasImplicitReturnType = true; |
| 5023 | } |
| 5024 | |
| 5025 | // FIXME: Check return type is complete, !isObjCObjectType |
| 5026 | |
Eli Friedman | dc3b723 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope) { |
| 5030 | // Leave the expression-evaluation context. |
| 5031 | DiscardCleanupsInEvaluationContext(); |
| 5032 | PopExpressionEvaluationContext(); |
| 5033 | |
| 5034 | // Leave the context of the lambda. |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 5035 | PopDeclContext(); |
| 5036 | PopFunctionScopeInfo(); |
Eli Friedman | dc3b723 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 5037 | } |
| 5038 | |
| 5039 | ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, |
| 5040 | Stmt *Body, Scope *CurScope) { |
| 5041 | // FIXME: Implement |
| 5042 | Diag(StartLoc, diag::err_lambda_unsupported); |
| 5043 | ActOnLambdaError(StartLoc, CurScope); |
| 5044 | return ExprError(); |
| 5045 | } |