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" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 23 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Fariborz Jahanian | d426662 | 2010-06-16 18:56:04 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 30 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 73e0a91 | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 34 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 36 | ParsedType Sema::getDestructorName(SourceLocation TildeLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 37 | IdentifierInfo &II, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 38 | SourceLocation NameLoc, |
| 39 | Scope *S, CXXScopeSpec &SS, |
| 40 | ParsedType ObjectTypePtr, |
| 41 | bool EnteringContext) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 42 | // Determine where to perform name lookup. |
| 43 | |
| 44 | // FIXME: This area of the standard is very messy, and the current |
| 45 | // wording is rather unclear about which scopes we search for the |
| 46 | // destructor name; see core issues 399 and 555. Issue 399 in |
| 47 | // particular shows where the current description of destructor name |
| 48 | // lookup is completely out of line with existing practice, e.g., |
| 49 | // this appears to be ill-formed: |
| 50 | // |
| 51 | // namespace N { |
| 52 | // template <typename T> struct S { |
| 53 | // ~S(); |
| 54 | // }; |
| 55 | // } |
| 56 | // |
| 57 | // void f(N::S<int>* s) { |
| 58 | // s->N::S<int>::~S(); |
| 59 | // } |
| 60 | // |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 61 | // See also PR6358 and PR6359. |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 62 | // For this reason, we're currently only doing the C++03 version of this |
| 63 | // 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] | 64 | QualType SearchType; |
| 65 | DeclContext *LookupCtx = 0; |
| 66 | bool isDependent = false; |
| 67 | bool LookInScope = false; |
| 68 | |
| 69 | // If we have an object type, it's because we are in a |
| 70 | // pseudo-destructor-expression or a member access expression, and |
| 71 | // we know what type we're looking for. |
| 72 | if (ObjectTypePtr) |
| 73 | SearchType = GetTypeFromParser(ObjectTypePtr); |
| 74 | |
| 75 | if (SS.isSet()) { |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 76 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 78 | bool AlreadySearched = false; |
| 79 | bool LookAtPrefix = true; |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 80 | // C++ [basic.lookup.qual]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 81 | // 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] | 82 | // the type-names are looked up as types in the scope designated by the |
| 83 | // nested-name-specifier. In a qualified-id of the form: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 84 | // |
| 85 | // ::[opt] nested-name-specifier ~ class-name |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 86 | // |
| 87 | // where the nested-name-specifier designates a namespace scope, and in |
Chandler Carruth | 5e895a8 | 2010-02-21 10:19:54 +0000 | [diff] [blame] | 88 | // a qualified-id of the form: |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 89 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 90 | // ::opt nested-name-specifier class-name :: ~ class-name |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 91 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 92 | // 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] | 93 | // the nested-name-specifier. |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 94 | // |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 95 | // Here, we check the first case (completely) and determine whether the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 96 | // code below is permitted to look at the prefix of the |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 97 | // nested-name-specifier. |
| 98 | DeclContext *DC = computeDeclContext(SS, EnteringContext); |
| 99 | if (DC && DC->isFileContext()) { |
| 100 | AlreadySearched = true; |
| 101 | LookupCtx = DC; |
| 102 | isDependent = false; |
| 103 | } else if (DC && isa<CXXRecordDecl>(DC)) |
| 104 | LookAtPrefix = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 105 | |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 106 | // The second case from the C++03 rules quoted further above. |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 107 | NestedNameSpecifier *Prefix = 0; |
| 108 | if (AlreadySearched) { |
| 109 | // Nothing left to do. |
| 110 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { |
| 111 | CXXScopeSpec PrefixSS; |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 112 | PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data())); |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 113 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); |
| 114 | isDependent = isDependentScopeSpecifier(PrefixSS); |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 115 | } else if (ObjectTypePtr) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 116 | LookupCtx = computeDeclContext(SearchType); |
| 117 | isDependent = SearchType->isDependentType(); |
| 118 | } else { |
| 119 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 120 | isDependent = LookupCtx && LookupCtx->isDependentContext(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 121 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 122 | |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 123 | LookInScope = false; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 124 | } else if (ObjectTypePtr) { |
| 125 | // C++ [basic.lookup.classref]p3: |
| 126 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 127 | // in the context of the entire postfix-expression. If the type T |
| 128 | // of the object expression is of a class type C, the type-name is |
| 129 | // also looked up in the scope of class C. At least one of the |
| 130 | // lookups shall find a name that refers to (possibly |
| 131 | // cv-qualified) T. |
| 132 | LookupCtx = computeDeclContext(SearchType); |
| 133 | isDependent = SearchType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 134 | assert((isDependent || !SearchType->isIncompleteType()) && |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 135 | "Caller should have completed object type"); |
| 136 | |
| 137 | LookInScope = true; |
| 138 | } else { |
| 139 | // Perform lookup into the current scope (only). |
| 140 | LookInScope = true; |
| 141 | } |
| 142 | |
Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 143 | TypeDecl *NonMatchingTypeDecl = 0; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 144 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); |
| 145 | for (unsigned Step = 0; Step != 2; ++Step) { |
| 146 | // Look for the name first in the computed lookup context (if we |
Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 147 | // 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] | 148 | // we're allowed to look there). |
| 149 | Found.clear(); |
| 150 | if (Step == 0 && LookupCtx) |
| 151 | LookupQualifiedName(Found, LookupCtx); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 152 | else if (Step == 1 && LookInScope && S) |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 153 | LookupName(Found, S); |
| 154 | else |
| 155 | continue; |
| 156 | |
| 157 | // FIXME: Should we be suppressing ambiguities here? |
| 158 | if (Found.isAmbiguous()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 159 | return ParsedType(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 160 | |
| 161 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
| 162 | QualType T = Context.getTypeDeclType(Type); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 163 | |
| 164 | if (SearchType.isNull() || SearchType->isDependentType() || |
| 165 | Context.hasSameUnqualifiedType(T, SearchType)) { |
| 166 | // We found our type! |
| 167 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 168 | return ParsedType::make(T); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 169 | } |
John Wiegley | 36784e7 | 2011-03-08 08:13:22 +0000 | [diff] [blame] | 170 | |
Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 171 | if (!SearchType.isNull()) |
| 172 | NonMatchingTypeDecl = Type; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // If the name that we found is a class template name, and it is |
| 176 | // the same name as the template name in the last part of the |
| 177 | // nested-name-specifier (if present) or the object type, then |
| 178 | // this is the destructor for that class. |
| 179 | // FIXME: This is a workaround until we get real drafting for core |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 180 | // issue 399, for which there isn't even an obvious direction. |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 181 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
| 182 | QualType MemberOfType; |
| 183 | if (SS.isSet()) { |
| 184 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
| 185 | // Figure out the type of the context, if it has one. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 186 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) |
| 187 | MemberOfType = Context.getTypeDeclType(Record); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | if (MemberOfType.isNull()) |
| 191 | MemberOfType = SearchType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 193 | if (MemberOfType.isNull()) |
| 194 | continue; |
| 195 | |
| 196 | // We're referring into a class template specialization. If the |
| 197 | // class template we found is the same as the template being |
| 198 | // specialized, we found what we are looking for. |
| 199 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
| 200 | if (ClassTemplateSpecializationDecl *Spec |
| 201 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
| 202 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
| 203 | Template->getCanonicalDecl()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 204 | return ParsedType::make(MemberOfType); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | continue; |
| 208 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 209 | |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 210 | // We're referring to an unresolved class template |
| 211 | // specialization. Determine whether we class template we found |
| 212 | // is the same as the template being specialized or, if we don't |
| 213 | // know which template is being specialized, that it at least |
| 214 | // has the same name. |
| 215 | if (const TemplateSpecializationType *SpecType |
| 216 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
| 217 | TemplateName SpecName = SpecType->getTemplateName(); |
| 218 | |
| 219 | // The class template we found is the same template being |
| 220 | // specialized. |
| 221 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
| 222 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 223 | return ParsedType::make(MemberOfType); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 224 | |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | // The class template we found has the same name as the |
| 229 | // (dependent) template name being specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 230 | if (DependentTemplateName *DepTemplate |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 231 | = SpecName.getAsDependentTemplateName()) { |
| 232 | if (DepTemplate->isIdentifier() && |
| 233 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 234 | return ParsedType::make(MemberOfType); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 235 | |
| 236 | continue; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (isDependent) { |
| 243 | // We didn't find our type, but that's okay: it's dependent |
| 244 | // anyway. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 245 | |
| 246 | // FIXME: What if we have no nested-name-specifier? |
| 247 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), |
| 248 | SS.getWithLocInContext(Context), |
| 249 | II, NameLoc); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 250 | return ParsedType::make(T); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 253 | if (NonMatchingTypeDecl) { |
| 254 | QualType T = Context.getTypeDeclType(NonMatchingTypeDecl); |
| 255 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) |
| 256 | << T << SearchType; |
| 257 | Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here) |
| 258 | << T; |
| 259 | } else if (ObjectTypePtr) |
| 260 | Diag(NameLoc, diag::err_ident_in_dtor_not_a_type) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 261 | << &II; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 262 | else |
| 263 | Diag(NameLoc, diag::err_destructor_class_name); |
| 264 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 265 | return ParsedType(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 266 | } |
| 267 | |
David Blaikie | 53a75c0 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 268 | ParsedType Sema::getDestructorType(const DeclSpec& DS, ParsedType ObjectType) { |
David Blaikie | 4db8c44 | 2011-12-12 04:13:55 +0000 | [diff] [blame^] | 269 | if (DS.getTypeSpecType() == DeclSpec::TST_error || !ObjectType) |
David Blaikie | 53a75c0 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 270 | return ParsedType(); |
| 271 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype |
| 272 | && "only get destructor types from declspecs"); |
| 273 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 274 | QualType SearchType = GetTypeFromParser(ObjectType); |
| 275 | if (SearchType->isDependentType() || Context.hasSameUnqualifiedType(SearchType, T)) { |
| 276 | return ParsedType::make(T); |
| 277 | } |
| 278 | |
| 279 | Diag(DS.getTypeSpecTypeLoc(), diag::err_destructor_expr_type_mismatch) |
| 280 | << T << SearchType; |
| 281 | return ParsedType(); |
| 282 | } |
| 283 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 284 | /// \brief Build a C++ typeid expression with a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 285 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 286 | SourceLocation TypeidLoc, |
| 287 | TypeSourceInfo *Operand, |
| 288 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 289 | // C++ [expr.typeid]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 290 | // 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] | 291 | // that is the operand of typeid are always ignored. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 292 | // 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] | 293 | // type, the class shall be completely-defined. |
Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 294 | Qualifiers Quals; |
| 295 | QualType T |
| 296 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
| 297 | Quals); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 298 | if (T->getAs<RecordType>() && |
| 299 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 300 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 301 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 302 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
| 303 | Operand, |
| 304 | SourceRange(TypeidLoc, RParenLoc))); |
| 305 | } |
| 306 | |
| 307 | /// \brief Build a C++ typeid expression with an expression operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 308 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 309 | SourceLocation TypeidLoc, |
| 310 | Expr *E, |
| 311 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 312 | bool isUnevaluatedOperand = true; |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 313 | if (E && !E->isTypeDependent()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 314 | if (E->getType()->isPlaceholderType()) { |
| 315 | ExprResult result = CheckPlaceholderExpr(E); |
| 316 | if (result.isInvalid()) return ExprError(); |
| 317 | E = result.take(); |
| 318 | } |
| 319 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 320 | QualType T = E->getType(); |
| 321 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
| 322 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 323 | // C++ [expr.typeid]p3: |
| 324 | // [...] If the type of the expression is a class type, the class |
| 325 | // shall be completely-defined. |
| 326 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 327 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 328 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 329 | // C++ [expr.typeid]p3: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 330 | // 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] | 331 | // polymorphic class type [...] [the] expression is an unevaluated |
| 332 | // operand. [...] |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 333 | if (RecordD->isPolymorphic() && E->Classify(Context).isGLValue()) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 334 | isUnevaluatedOperand = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 335 | |
| 336 | // We require a vtable to query the type at run time. |
| 337 | MarkVTableUsed(TypeidLoc, RecordD); |
| 338 | } |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 339 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 340 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 341 | // C++ [expr.typeid]p4: |
| 342 | // [...] 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] | 343 | // cv-qualified type, the result of the typeid expression refers to a |
| 344 | // std::type_info object representing the cv-unqualified referenced |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 345 | // type. |
Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 346 | Qualifiers Quals; |
| 347 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
| 348 | if (!Context.hasSameType(T, UnqualT)) { |
| 349 | T = UnqualT; |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 350 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).take(); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 353 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 354 | // If this is an unevaluated operand, clear out the set of |
| 355 | // declaration references we have been computing and eliminate any |
| 356 | // temporaries introduced in its computation. |
| 357 | if (isUnevaluatedOperand) |
| 358 | ExprEvalContexts.back().Context = Unevaluated; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 359 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 360 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 361 | E, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 362 | SourceRange(TypeidLoc, RParenLoc))); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 366 | ExprResult |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 367 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 368 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 369 | // Find the std::type_info type. |
Sebastian Redl | ce0682f | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 370 | if (!getStdNamespace()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 371 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 372 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 373 | if (!CXXTypeInfoDecl) { |
| 374 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 375 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 376 | LookupQualifiedName(R, getStdNamespace()); |
| 377 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 378 | if (!CXXTypeInfoDecl) |
| 379 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 380 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 381 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 382 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 384 | if (isType) { |
| 385 | // The operand is a type; handle it as such. |
| 386 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 387 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 388 | &TInfo); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 389 | if (T.isNull()) |
| 390 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 392 | if (!TInfo) |
| 393 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 395 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 396 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 398 | // The operand is an expression. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 399 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 402 | /// Retrieve the UuidAttr associated with QT. |
| 403 | static UuidAttr *GetUuidAttrOfType(QualType QT) { |
| 404 | // Optionally remove one level of pointer, reference or array indirection. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 405 | const Type *Ty = QT.getTypePtr();; |
Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 406 | if (QT->isPointerType() || QT->isReferenceType()) |
| 407 | Ty = QT->getPointeeType().getTypePtr(); |
| 408 | else if (QT->isArrayType()) |
| 409 | Ty = cast<ArrayType>(QT)->getElementType().getTypePtr(); |
| 410 | |
Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 411 | // Loop all record redeclaration looking for an uuid attribute. |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 412 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 413 | for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(), |
| 414 | E = RD->redecls_end(); I != E; ++I) { |
| 415 | if (UuidAttr *Uuid = I->getAttr<UuidAttr>()) |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 416 | return Uuid; |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 417 | } |
Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 418 | |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 419 | return 0; |
Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 422 | /// \brief Build a Microsoft __uuidof expression with a type operand. |
| 423 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 424 | SourceLocation TypeidLoc, |
| 425 | TypeSourceInfo *Operand, |
| 426 | SourceLocation RParenLoc) { |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 427 | if (!Operand->getType()->isDependentType()) { |
| 428 | if (!GetUuidAttrOfType(Operand->getType())) |
| 429 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 430 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 431 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 432 | // FIXME: add __uuidof semantic analysis for type operand. |
| 433 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 434 | Operand, |
| 435 | SourceRange(TypeidLoc, RParenLoc))); |
| 436 | } |
| 437 | |
| 438 | /// \brief Build a Microsoft __uuidof expression with an expression operand. |
| 439 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 440 | SourceLocation TypeidLoc, |
| 441 | Expr *E, |
| 442 | SourceLocation RParenLoc) { |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 443 | if (!E->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 444 | if (!GetUuidAttrOfType(E->getType()) && |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 445 | !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 446 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 447 | } |
| 448 | // FIXME: add __uuidof semantic analysis for type operand. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 449 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 450 | E, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 451 | SourceRange(TypeidLoc, RParenLoc))); |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 455 | ExprResult |
| 456 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 457 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 458 | // If MSVCGuidDecl has not been cached, do the lookup. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 459 | if (!MSVCGuidDecl) { |
| 460 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 461 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 462 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 463 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 464 | if (!MSVCGuidDecl) |
| 465 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 468 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 469 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 470 | if (isType) { |
| 471 | // The operand is a type; handle it as such. |
| 472 | TypeSourceInfo *TInfo = 0; |
| 473 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 474 | &TInfo); |
| 475 | if (T.isNull()) |
| 476 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 477 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 478 | if (!TInfo) |
| 479 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 480 | |
| 481 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 482 | } |
| 483 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 484 | // The operand is an expression. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 485 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 486 | } |
| 487 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 488 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 489 | ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 490 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 491 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 492 | "Unknown C++ Boolean value!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 493 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 494 | Context.BoolTy, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 496 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 497 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 498 | ExprResult |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 499 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
| 500 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 501 | } |
| 502 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 503 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 504 | ExprResult |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 505 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
| 506 | bool IsThrownVarInScope = false; |
| 507 | if (Ex) { |
| 508 | // C++0x [class.copymove]p31: |
| 509 | // When certain criteria are met, an implementation is allowed to omit the |
| 510 | // copy/move construction of a class object [...] |
| 511 | // |
| 512 | // - in a throw-expression, when the operand is the name of a |
| 513 | // non-volatile automatic object (other than a function or catch- |
| 514 | // clause parameter) whose scope does not extend beyond the end of the |
| 515 | // innermost enclosing try-block (if there is one), the copy/move |
| 516 | // operation from the operand to the exception object (15.1) can be |
| 517 | // omitted by constructing the automatic object directly into the |
| 518 | // exception object |
| 519 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) |
| 520 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 521 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { |
| 522 | for( ; S; S = S->getParent()) { |
| 523 | if (S->isDeclScope(Var)) { |
| 524 | IsThrownVarInScope = true; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | if (S->getFlags() & |
| 529 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
| 530 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | |
| 531 | Scope::TryScope)) |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
| 539 | } |
| 540 | |
| 541 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
| 542 | bool IsThrownVarInScope) { |
Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 543 | // Don't report an error if 'throw' is used in system headers. |
Anders Carlsson | 15348ae | 2011-02-28 02:27:16 +0000 | [diff] [blame] | 544 | if (!getLangOptions().CXXExceptions && |
Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 545 | !getSourceManager().isInSystemHeader(OpLoc)) |
Anders Carlsson | b1fba31 | 2011-02-19 21:53:09 +0000 | [diff] [blame] | 546 | Diag(OpLoc, diag::err_exceptions_disabled) << "throw"; |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 547 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 548 | if (Ex && !Ex->isTypeDependent()) { |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 549 | ExprResult ExRes = CheckCXXThrowOperand(OpLoc, Ex, IsThrownVarInScope); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 550 | if (ExRes.isInvalid()) |
| 551 | return ExprError(); |
| 552 | Ex = ExRes.take(); |
| 553 | } |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 554 | |
| 555 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc, |
| 556 | IsThrownVarInScope)); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 560 | ExprResult Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *E, |
| 561 | bool IsThrownVarInScope) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 562 | // C++ [except.throw]p3: |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 563 | // A throw-expression initializes a temporary object, called the exception |
| 564 | // object, the type of which is determined by removing any top-level |
| 565 | // cv-qualifiers from the static type of the operand of throw and adjusting |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | // the type from "array of T" or "function returning T" to "pointer to T" |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 567 | // or "pointer to function returning T", [...] |
| 568 | if (E->getType().hasQualifiers()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 569 | E = ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 570 | E->getValueKind()).take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 571 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 572 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 573 | if (Res.isInvalid()) |
| 574 | return ExprError(); |
| 575 | E = Res.take(); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 576 | |
| 577 | // If the type of the exception would be an incomplete type or a pointer |
| 578 | // to an incomplete type other than (cv) void the program is ill-formed. |
| 579 | QualType Ty = E->getType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 580 | bool isPointer = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 581 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 582 | Ty = Ptr->getPointeeType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 583 | isPointer = true; |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 584 | } |
| 585 | if (!isPointer || !Ty->isVoidType()) { |
| 586 | if (RequireCompleteType(ThrowLoc, Ty, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 587 | PDiag(isPointer ? diag::err_throw_incomplete_ptr |
| 588 | : diag::err_throw_incomplete) |
| 589 | << E->getSourceRange())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 590 | return ExprError(); |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 591 | |
Douglas Gregor | bf422f9 | 2010-04-15 18:05:39 +0000 | [diff] [blame] | 592 | if (RequireNonAbstractType(ThrowLoc, E->getType(), |
| 593 | PDiag(diag::err_throw_abstract_type) |
| 594 | << E->getSourceRange())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 595 | return ExprError(); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 596 | } |
| 597 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 598 | // Initialize the exception result. This implicitly weeds out |
| 599 | // abstract types or types with inaccessible copy constructors. |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 600 | |
| 601 | // C++0x [class.copymove]p31: |
| 602 | // When certain criteria are met, an implementation is allowed to omit the |
| 603 | // copy/move construction of a class object [...] |
| 604 | // |
| 605 | // - in a throw-expression, when the operand is the name of a |
| 606 | // non-volatile automatic object (other than a function or catch-clause |
| 607 | // parameter) whose scope does not extend beyond the end of the |
| 608 | // innermost enclosing try-block (if there is one), the copy/move |
| 609 | // operation from the operand to the exception object (15.1) can be |
| 610 | // omitted by constructing the automatic object directly into the |
| 611 | // exception object |
| 612 | const VarDecl *NRVOVariable = 0; |
| 613 | if (IsThrownVarInScope) |
| 614 | NRVOVariable = getCopyElisionCandidate(QualType(), E, false); |
| 615 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 616 | InitializedEntity Entity = |
Douglas Gregor | 72dfa27 | 2011-01-21 22:46:35 +0000 | [diff] [blame] | 617 | InitializedEntity::InitializeException(ThrowLoc, E->getType(), |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 618 | /*NRVO=*/NRVOVariable != 0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 619 | Res = PerformMoveOrCopyInitialization(Entity, NRVOVariable, |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 620 | QualType(), E, |
| 621 | IsThrownVarInScope); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 622 | if (Res.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 623 | return ExprError(); |
| 624 | E = Res.take(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 625 | |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 626 | // If the exception has class type, we need additional handling. |
| 627 | const RecordType *RecordTy = Ty->getAs<RecordType>(); |
| 628 | if (!RecordTy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 629 | return Owned(E); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 630 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 631 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 632 | // If we are throwing a polymorphic class type or pointer thereof, |
| 633 | // exception handling will make use of the vtable. |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 634 | MarkVTableUsed(ThrowLoc, RD); |
| 635 | |
Eli Friedman | 98efb9f | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 636 | // If a pointer is thrown, the referenced object will not be destroyed. |
| 637 | if (isPointer) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 638 | return Owned(E); |
Eli Friedman | 98efb9f | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 639 | |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 640 | // If the class has a non-trivial destructor, we must be able to call it. |
| 641 | if (RD->hasTrivialDestructor()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 642 | return Owned(E); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 643 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 644 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 645 | = const_cast<CXXDestructorDecl*>(LookupDestructor(RD)); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 646 | if (!Destructor) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 647 | return Owned(E); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 648 | |
| 649 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
| 650 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 651 | PDiag(diag::err_access_dtor_exception) << Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 652 | return Owned(E); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 653 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 654 | |
Douglas Gregor | 341350e | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 655 | QualType Sema::getCurrentThisType(bool Capture) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 656 | // Ignore block scopes: we can capture through them. |
| 657 | // Ignore nested enum scopes: we'll diagnose non-constant expressions |
| 658 | // where they're invalid, and other uses are legitimate. |
| 659 | // Don't ignore nested class scopes: you can't use 'this' in a local class. |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 660 | DeclContext *DC = CurContext; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 661 | unsigned NumBlocks = 0; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 662 | while (true) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 663 | if (isa<BlockDecl>(DC)) { |
| 664 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 665 | ++NumBlocks; |
| 666 | } else if (isa<EnumDecl>(DC)) |
| 667 | DC = cast<EnumDecl>(DC)->getDeclContext(); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 668 | else break; |
| 669 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 670 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 671 | QualType ThisTy; |
| 672 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { |
| 673 | if (method && method->isInstance()) |
| 674 | ThisTy = method->getThisType(Context); |
| 675 | } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { |
| 676 | // C++0x [expr.prim]p4: |
| 677 | // Otherwise, if a member-declarator declares a non-static data member |
| 678 | // of a class X, the expression this is a prvalue of type "pointer to X" |
| 679 | // within the optional brace-or-equal-initializer. |
| 680 | Scope *S = getScopeForContext(DC); |
| 681 | if (!S || S->getFlags() & Scope::ThisScope) |
| 682 | ThisTy = Context.getPointerType(Context.getRecordType(RD)); |
| 683 | } |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 684 | |
Douglas Gregor | 341350e | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 685 | if (!Capture || ThisTy.isNull()) |
| 686 | return ThisTy; |
| 687 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 688 | // Mark that we're closing on 'this' in all the block scopes we ignored. |
Douglas Gregor | 341350e | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 689 | for (unsigned idx = FunctionScopes.size() - 1; |
| 690 | NumBlocks; --idx, --NumBlocks) |
| 691 | cast<BlockScopeInfo>(FunctionScopes[idx])->CapturesCXXThis = true; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 692 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 693 | return ThisTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 696 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 697 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 698 | /// is a non-lvalue expression whose value is the address of the object for |
| 699 | /// which the function is called. |
| 700 | |
Douglas Gregor | 341350e | 2011-10-18 16:47:30 +0000 | [diff] [blame] | 701 | QualType ThisTy = getCurrentThisType(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 702 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 703 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 704 | return Owned(new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 705 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 706 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 707 | ExprResult |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 708 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 709 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 710 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 711 | SourceLocation RParenLoc) { |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 712 | if (!TypeRep) |
| 713 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 714 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 715 | TypeSourceInfo *TInfo; |
| 716 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 717 | if (!TInfo) |
| 718 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 719 | |
| 720 | return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); |
| 721 | } |
| 722 | |
| 723 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 724 | /// Can be interpreted either as function-style casting ("int(x)") |
| 725 | /// or class type construction ("ClassType(x,y,z)") |
| 726 | /// or creation of a value-initialized type ("int()"). |
| 727 | ExprResult |
| 728 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
| 729 | SourceLocation LParenLoc, |
| 730 | MultiExprArg exprs, |
| 731 | SourceLocation RParenLoc) { |
| 732 | QualType Ty = TInfo->getType(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 733 | unsigned NumExprs = exprs.size(); |
| 734 | Expr **Exprs = (Expr**)exprs.get(); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 735 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 736 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 737 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 738 | if (Ty->isDependentType() || |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 739 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 740 | exprs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 742 | return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo, |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 743 | LParenLoc, |
| 744 | Exprs, NumExprs, |
| 745 | RParenLoc)); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 748 | if (Ty->isArrayType()) |
| 749 | return ExprError(Diag(TyBeginLoc, |
| 750 | diag::err_value_init_for_array_type) << FullRange); |
| 751 | if (!Ty->isVoidType() && |
| 752 | RequireCompleteType(TyBeginLoc, Ty, |
| 753 | PDiag(diag::err_invalid_incomplete_type_use) |
| 754 | << FullRange)) |
| 755 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 756 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 757 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 758 | diag::err_allocation_of_abstract_type)) |
| 759 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | |
| 761 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 762 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 763 | // If the expression list is a single expression, the type conversion |
| 764 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 765 | // corresponding cast expression. |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 766 | if (NumExprs == 1) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 767 | Expr *Arg = Exprs[0]; |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 768 | exprs.release(); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 769 | return BuildCXXFunctionalCastExpr(TInfo, LParenLoc, Arg, RParenLoc); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 772 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 773 | InitializationKind Kind |
| 774 | = NumExprs ? InitializationKind::CreateDirect(TyBeginLoc, |
| 775 | LParenLoc, RParenLoc) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 776 | : InitializationKind::CreateValue(TyBeginLoc, |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 777 | LParenLoc, RParenLoc); |
| 778 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); |
| 779 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(exprs)); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 781 | // FIXME: Improve AST representation? |
| 782 | return move(Result); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 783 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 784 | |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 785 | /// doesUsualArrayDeleteWantSize - Answers whether the usual |
| 786 | /// operator delete[] for the given type has a size_t parameter. |
| 787 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
| 788 | QualType allocType) { |
| 789 | const RecordType *record = |
| 790 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 791 | if (!record) return false; |
| 792 | |
| 793 | // Try to find an operator delete[] in class scope. |
| 794 | |
| 795 | DeclarationName deleteName = |
| 796 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
| 797 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
| 798 | S.LookupQualifiedName(ops, record->getDecl()); |
| 799 | |
| 800 | // We're just doing this for information. |
| 801 | ops.suppressDiagnostics(); |
| 802 | |
| 803 | // Very likely: there's no operator delete[]. |
| 804 | if (ops.empty()) return false; |
| 805 | |
| 806 | // If it's ambiguous, it should be illegal to call operator delete[] |
| 807 | // on this thing, so it doesn't matter if we allocate extra space or not. |
| 808 | if (ops.isAmbiguous()) return false; |
| 809 | |
| 810 | LookupResult::Filter filter = ops.makeFilter(); |
| 811 | while (filter.hasNext()) { |
| 812 | NamedDecl *del = filter.next()->getUnderlyingDecl(); |
| 813 | |
| 814 | // C++0x [basic.stc.dynamic.deallocation]p2: |
| 815 | // A template instance is never a usual deallocation function, |
| 816 | // regardless of its signature. |
| 817 | if (isa<FunctionTemplateDecl>(del)) { |
| 818 | filter.erase(); |
| 819 | continue; |
| 820 | } |
| 821 | |
| 822 | // C++0x [basic.stc.dynamic.deallocation]p2: |
| 823 | // If class T does not declare [an operator delete[] with one |
| 824 | // parameter] but does declare a member deallocation function |
| 825 | // named operator delete[] with exactly two parameters, the |
| 826 | // second of which has type std::size_t, then this function |
| 827 | // is a usual deallocation function. |
| 828 | if (!cast<CXXMethodDecl>(del)->isUsualDeallocationFunction()) { |
| 829 | filter.erase(); |
| 830 | continue; |
| 831 | } |
| 832 | } |
| 833 | filter.done(); |
| 834 | |
| 835 | if (!ops.isSingleResult()) return false; |
| 836 | |
| 837 | const FunctionDecl *del = cast<FunctionDecl>(ops.getFoundDecl()); |
| 838 | return (del->getNumParams() == 2); |
| 839 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 840 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 841 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 842 | /// @code new (memory) int[size][4] @endcode |
| 843 | /// or |
| 844 | /// @code ::new Foo(23, "hello") @endcode |
| 845 | /// For the interpretation of this heap of arguments, consult the base version. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 846 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 847 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 848 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 849 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 850 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 851 | MultiExprArg ConstructorArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | SourceLocation ConstructorRParen) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 853 | bool TypeContainsAuto = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 854 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 855 | Expr *ArraySize = 0; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 856 | // If the specified type is an array, unwrap it and save the expression. |
| 857 | if (D.getNumTypeObjects() > 0 && |
| 858 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 859 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 860 | if (TypeContainsAuto) |
| 861 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
| 862 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 863 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 864 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 865 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 866 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 867 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 868 | << D.getSourceRange()); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 869 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 870 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 871 | D.DropFirstTypeObject(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 874 | // Every dimension shall be of constant size. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 875 | if (ArraySize) { |
| 876 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 877 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 878 | break; |
| 879 | |
| 880 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 881 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
| 882 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && |
| 883 | !NumElts->isIntegerConstantExpr(Context)) { |
| 884 | Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) |
| 885 | << NumElts->getSourceRange(); |
| 886 | return ExprError(); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | } |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 891 | |
Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 892 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/0); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 893 | QualType AllocType = TInfo->getType(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 894 | if (D.isInvalidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 895 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 896 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | return BuildCXXNew(StartLoc, UseGlobal, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 898 | PlacementLParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | move(PlacementArgs), |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 900 | PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 901 | TypeIdParens, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 903 | TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 904 | ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 905 | ConstructorLParen, |
| 906 | move(ConstructorArgs), |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 907 | ConstructorRParen, |
| 908 | TypeContainsAuto); |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 909 | } |
| 910 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 911 | ExprResult |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 912 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 913 | SourceLocation PlacementLParen, |
| 914 | MultiExprArg PlacementArgs, |
| 915 | SourceLocation PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 916 | SourceRange TypeIdParens, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 917 | QualType AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 918 | TypeSourceInfo *AllocTypeInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 919 | Expr *ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 920 | SourceLocation ConstructorLParen, |
| 921 | MultiExprArg ConstructorArgs, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 922 | SourceLocation ConstructorRParen, |
| 923 | bool TypeMayContainAuto) { |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 924 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 925 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 926 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 927 | if (TypeMayContainAuto && AllocType->getContainedAutoType()) { |
| 928 | if (ConstructorArgs.size() == 0) |
| 929 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
| 930 | << AllocType << TypeRange); |
| 931 | if (ConstructorArgs.size() != 1) { |
| 932 | Expr *FirstBad = ConstructorArgs.get()[1]; |
| 933 | return ExprError(Diag(FirstBad->getSourceRange().getBegin(), |
| 934 | diag::err_auto_new_ctor_multiple_expressions) |
| 935 | << AllocType << TypeRange); |
| 936 | } |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 937 | TypeSourceInfo *DeducedType = 0; |
| 938 | if (!DeduceAutoType(AllocTypeInfo, ConstructorArgs.get()[0], DeducedType)) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 939 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
| 940 | << AllocType |
| 941 | << ConstructorArgs.get()[0]->getType() |
| 942 | << TypeRange |
| 943 | << ConstructorArgs.get()[0]->getSourceRange()); |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 944 | if (!DeducedType) |
| 945 | return ExprError(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 946 | |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 947 | AllocTypeInfo = DeducedType; |
| 948 | AllocType = AllocTypeInfo->getType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 951 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 952 | // typedef of an array type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 953 | if (!ArraySize) { |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 954 | if (const ConstantArrayType *Array |
| 955 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 956 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 957 | Context.getSizeType(), |
| 958 | TypeRange.getEnd()); |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 959 | AllocType = Array->getElementType(); |
| 960 | } |
| 961 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 962 | |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 963 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 964 | return ExprError(); |
| 965 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 966 | // In ARC, infer 'retaining' for the allocated |
| 967 | if (getLangOptions().ObjCAutoRefCount && |
| 968 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 969 | AllocType->isObjCLifetimeType()) { |
| 970 | AllocType = Context.getLifetimeQualifiedType(AllocType, |
| 971 | AllocType->getObjCARCImplicitLifetime()); |
| 972 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 973 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 974 | QualType ResultType = Context.getPointerType(AllocType); |
| 975 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 976 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 977 | // or enumeration type with a non-negative value." |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 978 | if (ArraySize && !ArraySize->isTypeDependent()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 979 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 980 | QualType SizeType = ArraySize->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 981 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 982 | ExprResult ConvertedSize = ConvertToIntegralOrEnumerationType( |
| 983 | StartLoc, ArraySize, |
| 984 | PDiag(diag::err_array_size_not_integral), |
| 985 | PDiag(diag::err_array_size_incomplete_type) |
| 986 | << ArraySize->getSourceRange(), |
| 987 | PDiag(diag::err_array_size_explicit_conversion), |
| 988 | PDiag(diag::note_array_size_conversion), |
| 989 | PDiag(diag::err_array_size_ambiguous_conversion), |
| 990 | PDiag(diag::note_array_size_conversion), |
| 991 | PDiag(getLangOptions().CPlusPlus0x ? |
| 992 | diag::warn_cxx98_compat_array_size_conversion : |
| 993 | diag::ext_array_size_conversion)); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 994 | if (ConvertedSize.isInvalid()) |
| 995 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 996 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 997 | ArraySize = ConvertedSize.take(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 998 | SizeType = ArraySize->getType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 999 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 1000 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1001 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1002 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 1003 | // We don't care about special rules, so we tell the machinery it's not |
| 1004 | // evaluated - it gives us a result in more cases. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1005 | if (!ArraySize->isValueDependent()) { |
| 1006 | llvm::APSInt Value; |
| 1007 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 1008 | if (Value < llvm::APSInt( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1009 | llvm::APInt::getNullValue(Value.getBitWidth()), |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 1010 | Value.isUnsigned())) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1011 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1012 | diag::err_typecheck_negative_array_size) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1013 | << ArraySize->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1014 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1015 | if (!AllocType->isDependentType()) { |
| 1016 | unsigned ActiveSizeBits |
| 1017 | = ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
| 1018 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1019 | Diag(ArraySize->getSourceRange().getBegin(), |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1020 | diag::err_array_too_large) |
| 1021 | << Value.toString(10) |
| 1022 | << ArraySize->getSourceRange(); |
| 1023 | return ExprError(); |
| 1024 | } |
| 1025 | } |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1026 | } else if (TypeIdParens.isValid()) { |
| 1027 | // Can't have dynamic array size when the type-id is in parentheses. |
| 1028 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 1029 | << ArraySize->getSourceRange() |
| 1030 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 1031 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1033 | TypeIdParens = SourceRange(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1034 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1035 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1037 | // ARC: warn about ABI issues. |
| 1038 | if (getLangOptions().ObjCAutoRefCount) { |
| 1039 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
| 1040 | if (BaseAllocType.hasStrongOrWeakObjCLifetime()) |
| 1041 | Diag(StartLoc, diag::warn_err_new_delete_object_array) |
| 1042 | << 0 << BaseAllocType; |
| 1043 | } |
| 1044 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 1045 | // Note that we do *not* convert the argument in any way. It can |
| 1046 | // be signed, larger than size_t, whatever. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1047 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1048 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1049 | FunctionDecl *OperatorNew = 0; |
| 1050 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1051 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 1052 | unsigned NumPlaceArgs = PlacementArgs.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1053 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1054 | if (!AllocType->isDependentType() && |
| 1055 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && |
| 1056 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1057 | SourceRange(PlacementLParen, PlacementRParen), |
| 1058 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 1059 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1060 | return ExprError(); |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1061 | |
| 1062 | // If this is an array allocation, compute whether the usual array |
| 1063 | // deallocation function for the type has a size_t parameter. |
| 1064 | bool UsualArrayDeleteWantsSize = false; |
| 1065 | if (ArraySize && !AllocType->isDependentType()) |
| 1066 | UsualArrayDeleteWantsSize |
| 1067 | = doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); |
| 1068 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1069 | SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1070 | if (OperatorNew) { |
| 1071 | // Add default arguments, if any. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1072 | const FunctionProtoType *Proto = |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1073 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1074 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 1075 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1076 | |
Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1077 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1078 | Proto, 1, PlaceArgs, NumPlaceArgs, |
Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1079 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1080 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1081 | |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1082 | NumPlaceArgs = AllPlaceArgs.size(); |
| 1083 | if (NumPlaceArgs > 0) |
| 1084 | PlaceArgs = &AllPlaceArgs[0]; |
| 1085 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1086 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1087 | bool Init = ConstructorLParen.isValid(); |
| 1088 | // --- Choosing a constructor --- |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1089 | CXXConstructorDecl *Constructor = 0; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1090 | bool HadMultipleCandidates = false; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1091 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); |
| 1092 | unsigned NumConsArgs = ConstructorArgs.size(); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1093 | ASTOwningVector<Expr*> ConvertedConstructorArgs(*this); |
Eli Friedman | a8ce9ec | 2009-11-08 22:15:39 +0000 | [diff] [blame] | 1094 | |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1095 | // Array 'new' can't have any initializers. |
Anders Carlsson | 55cbd6e | 2010-05-16 16:24:20 +0000 | [diff] [blame] | 1096 | if (NumConsArgs && (ResultType->isArrayType() || ArraySize)) { |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1097 | SourceRange InitRange(ConsArgs[0]->getLocStart(), |
| 1098 | ConsArgs[NumConsArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1099 | |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1100 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 1101 | return ExprError(); |
| 1102 | } |
| 1103 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1104 | if (!AllocType->isDependentType() && |
| 1105 | !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { |
| 1106 | // C++0x [expr.new]p15: |
| 1107 | // A new-expression that creates an object of type T initializes that |
| 1108 | // object as follows: |
| 1109 | InitializationKind Kind |
| 1110 | // - If the new-initializer is omitted, the object is default- |
| 1111 | // initialized (8.5); if no initialization is performed, |
| 1112 | // the object has indeterminate value |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1113 | = !Init? InitializationKind::CreateDefault(TypeRange.getBegin()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1114 | // - Otherwise, the new-initializer is interpreted according to the |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1115 | // initialization rules of 8.5 for direct-initialization. |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1116 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1117 | ConstructorLParen, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1118 | ConstructorRParen); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1119 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1120 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1121 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1122 | InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1123 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1124 | move(ConstructorArgs)); |
| 1125 | if (FullInit.isInvalid()) |
| 1126 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1127 | |
| 1128 | // 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] | 1129 | // constructor call, which CXXNewExpr handles directly. |
| 1130 | if (Expr *FullInitExpr = (Expr *)FullInit.get()) { |
| 1131 | if (CXXBindTemporaryExpr *Binder |
| 1132 | = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr)) |
| 1133 | FullInitExpr = Binder->getSubExpr(); |
| 1134 | if (CXXConstructExpr *Construct |
| 1135 | = dyn_cast<CXXConstructExpr>(FullInitExpr)) { |
| 1136 | Constructor = Construct->getConstructor(); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1137 | HadMultipleCandidates = Construct->hadMultipleCandidates(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1138 | for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(), |
| 1139 | AEnd = Construct->arg_end(); |
| 1140 | A != AEnd; ++A) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 1141 | ConvertedConstructorArgs.push_back(*A); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1142 | } else { |
| 1143 | // Take the converted initializer. |
| 1144 | ConvertedConstructorArgs.push_back(FullInit.release()); |
| 1145 | } |
| 1146 | } else { |
| 1147 | // No initialization required. |
| 1148 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1149 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1150 | // Take the converted arguments and use them for the new expression. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1151 | NumConsArgs = ConvertedConstructorArgs.size(); |
| 1152 | ConsArgs = (Expr **)ConvertedConstructorArgs.take(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1153 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1154 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1155 | // Mark the new and delete operators as referenced. |
| 1156 | if (OperatorNew) |
| 1157 | MarkDeclarationReferenced(StartLoc, OperatorNew); |
| 1158 | if (OperatorDelete) |
| 1159 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
| 1160 | |
John McCall | 84ff0fc | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 1161 | // C++0x [expr.new]p17: |
| 1162 | // If the new expression creates an array of objects of class type, |
| 1163 | // access and ambiguity control are done for the destructor. |
| 1164 | if (ArraySize && Constructor) { |
| 1165 | if (CXXDestructorDecl *dtor = LookupDestructor(Constructor->getParent())) { |
| 1166 | MarkDeclarationReferenced(StartLoc, dtor); |
| 1167 | CheckDestructorAccess(StartLoc, dtor, |
| 1168 | PDiag(diag::err_access_dtor) |
| 1169 | << Context.getBaseElementType(AllocType)); |
| 1170 | } |
| 1171 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1172 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1173 | PlacementArgs.release(); |
| 1174 | ConstructorArgs.release(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1175 | |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1176 | return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1177 | PlaceArgs, NumPlaceArgs, TypeIdParens, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1178 | ArraySize, Constructor, Init, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1179 | ConsArgs, NumConsArgs, |
| 1180 | HadMultipleCandidates, |
| 1181 | OperatorDelete, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1182 | UsualArrayDeleteWantsSize, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1183 | ResultType, AllocTypeInfo, |
| 1184 | StartLoc, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1185 | Init ? ConstructorRParen : |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 1186 | TypeRange.getEnd(), |
| 1187 | ConstructorLParen, ConstructorRParen)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 1191 | /// in a new-expression. |
| 1192 | /// dimension off and stores the size expression in ArraySize. |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1193 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | SourceRange R) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1195 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 1196 | // abstract class type or array thereof. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1197 | if (AllocType->isFunctionType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1198 | return Diag(Loc, diag::err_bad_new_type) |
| 1199 | << AllocType << 0 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1200 | else if (AllocType->isReferenceType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1201 | return Diag(Loc, diag::err_bad_new_type) |
| 1202 | << AllocType << 1 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1203 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1204 | RequireCompleteType(Loc, AllocType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1205 | PDiag(diag::err_new_incomplete_type) |
| 1206 | << R)) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1207 | return true; |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1208 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1209 | diag::err_allocation_of_abstract_type)) |
| 1210 | return true; |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1211 | else if (AllocType->isVariablyModifiedType()) |
| 1212 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 1213 | << AllocType; |
Douglas Gregor | 5666d36 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1214 | else if (unsigned AddressSpace = AllocType.getAddressSpace()) |
| 1215 | return Diag(Loc, diag::err_address_space_qualified_new) |
| 1216 | << AllocType.getUnqualifiedType() << AddressSpace; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1217 | else if (getLangOptions().ObjCAutoRefCount) { |
| 1218 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { |
| 1219 | QualType BaseAllocType = Context.getBaseElementType(AT); |
| 1220 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 1221 | BaseAllocType->isObjCLifetimeType()) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 1222 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1223 | << BaseAllocType; |
| 1224 | } |
| 1225 | } |
Douglas Gregor | 5666d36 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1226 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1227 | return false; |
| 1228 | } |
| 1229 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1230 | /// \brief Determine whether the given function is a non-placement |
| 1231 | /// deallocation function. |
| 1232 | static bool isNonPlacementDeallocationFunction(FunctionDecl *FD) { |
| 1233 | if (FD->isInvalidDecl()) |
| 1234 | return false; |
| 1235 | |
| 1236 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 1237 | return Method->isUsualDeallocationFunction(); |
| 1238 | |
| 1239 | return ((FD->getOverloadedOperator() == OO_Delete || |
| 1240 | FD->getOverloadedOperator() == OO_Array_Delete) && |
| 1241 | FD->getNumParams() == 1); |
| 1242 | } |
| 1243 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1244 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 1245 | /// that are appropriate for the allocation. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1246 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 1247 | bool UseGlobal, QualType AllocType, |
| 1248 | bool IsArray, Expr **PlaceArgs, |
| 1249 | unsigned NumPlaceArgs, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1250 | FunctionDecl *&OperatorNew, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1252 | // --- Choosing an allocation function --- |
| 1253 | // C++ 5.3.4p8 - 14 & 18 |
| 1254 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 1255 | // in the scope of the allocated class. |
| 1256 | // 2) If an array size is given, look for operator new[], else look for |
| 1257 | // operator new. |
| 1258 | // 3) The first argument is always size_t. Append the arguments from the |
| 1259 | // placement form. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1260 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1261 | SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1262 | // We don't care about the actual value of this argument. |
| 1263 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 1264 | // tree? Or should the consumer just recalculate the value? |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1265 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1266 | Context.getTargetInfo().getPointerWidth(0)), |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1267 | Context.getSizeType(), |
| 1268 | SourceLocation()); |
| 1269 | AllocArgs[0] = &Size; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1270 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 1271 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1272 | // C++ [expr.new]p8: |
| 1273 | // If the allocated type is a non-array type, the allocation |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1274 | // function's name is operator new and the deallocation function's |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1275 | // name is operator delete. If the allocated type is an array |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1276 | // type, the allocation function's name is operator new[] and the |
| 1277 | // deallocation function's name is operator delete[]. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1278 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 1279 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1280 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1281 | IsArray ? OO_Array_Delete : OO_Delete); |
| 1282 | |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1283 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 1284 | |
| 1285 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | CXXRecordDecl *Record |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1287 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1288 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1289 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 1290 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1291 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1292 | } |
| 1293 | if (!OperatorNew) { |
| 1294 | // Didn't find a member overload. Look for a global one. |
| 1295 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1296 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1297 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1298 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 1299 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1300 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1303 | // We don't need an operator delete if we're running under |
| 1304 | // -fno-exceptions. |
| 1305 | if (!getLangOptions().Exceptions) { |
| 1306 | OperatorDelete = 0; |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 1310 | // FindAllocationOverload can change the passed in arguments, so we need to |
| 1311 | // copy them back. |
| 1312 | if (NumPlaceArgs > 0) |
| 1313 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1315 | // C++ [expr.new]p19: |
| 1316 | // |
| 1317 | // If the new-expression begins with a unary :: operator, the |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1318 | // deallocation function's name is looked up in the global |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1319 | // 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] | 1320 | // array thereof, the deallocation function's name is looked up in |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1321 | // the scope of T. If this lookup fails to find the name, or if |
| 1322 | // the allocated type is not a class type or array thereof, the |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1323 | // deallocation function's name is looked up in the global scope. |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1324 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1325 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1326 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1327 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1328 | LookupQualifiedName(FoundDelete, RD); |
| 1329 | } |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1330 | if (FoundDelete.isAmbiguous()) |
| 1331 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1332 | |
| 1333 | if (FoundDelete.empty()) { |
| 1334 | DeclareGlobalNewDelete(); |
| 1335 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 1336 | } |
| 1337 | |
| 1338 | FoundDelete.suppressDiagnostics(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1339 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1340 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1341 | |
John McCall | edeb6c9 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 1342 | // Whether we're looking for a placement operator delete is dictated |
| 1343 | // by whether we selected a placement operator new, not by whether |
| 1344 | // we had explicit placement arguments. This matters for things like |
| 1345 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 1346 | // A *a = new A() |
| 1347 | bool isPlacementNew = (NumPlaceArgs > 0 || OperatorNew->param_size() != 1); |
| 1348 | |
| 1349 | if (isPlacementNew) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1350 | // C++ [expr.new]p20: |
| 1351 | // A declaration of a placement deallocation function matches the |
| 1352 | // declaration of a placement allocation function if it has the |
| 1353 | // same number of parameters and, after parameter transformations |
| 1354 | // (8.3.5), all parameter types except the first are |
| 1355 | // identical. [...] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1356 | // |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1357 | // To perform this comparison, we compute the function type that |
| 1358 | // the deallocation function should have, and use that type both |
| 1359 | // for template argument deduction and for comparison purposes. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1360 | // |
| 1361 | // FIXME: this comparison should ignore CC and the like. |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1362 | QualType ExpectedFunctionType; |
| 1363 | { |
| 1364 | const FunctionProtoType *Proto |
| 1365 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1366 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1367 | SmallVector<QualType, 4> ArgTypes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1368 | ArgTypes.push_back(Context.VoidPtrTy); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1369 | for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I) |
| 1370 | ArgTypes.push_back(Proto->getArgType(I)); |
| 1371 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1372 | FunctionProtoType::ExtProtoInfo EPI; |
| 1373 | EPI.Variadic = Proto->isVariadic(); |
| 1374 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1375 | ExpectedFunctionType |
| 1376 | = Context.getFunctionType(Context.VoidTy, ArgTypes.data(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1377 | ArgTypes.size(), EPI); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1380 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1381 | DEnd = FoundDelete.end(); |
| 1382 | D != DEnd; ++D) { |
| 1383 | FunctionDecl *Fn = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1384 | if (FunctionTemplateDecl *FnTmpl |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1385 | = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
| 1386 | // Perform template argument deduction to try to match the |
| 1387 | // expected function type. |
| 1388 | TemplateDeductionInfo Info(Context, StartLoc); |
| 1389 | if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info)) |
| 1390 | continue; |
| 1391 | } else |
| 1392 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 1393 | |
| 1394 | if (Context.hasSameType(Fn->getType(), ExpectedFunctionType)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1395 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } else { |
| 1398 | // C++ [expr.new]p20: |
| 1399 | // [...] Any non-placement deallocation function matches a |
| 1400 | // non-placement allocation function. [...] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1401 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1402 | DEnd = FoundDelete.end(); |
| 1403 | D != DEnd; ++D) { |
| 1404 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl())) |
| 1405 | if (isNonPlacementDeallocationFunction(Fn)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1406 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | // C++ [expr.new]p20: |
| 1411 | // [...] If the lookup finds a single matching deallocation |
| 1412 | // function, that function will be called; otherwise, no |
| 1413 | // deallocation function will be called. |
| 1414 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1415 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1416 | |
| 1417 | // C++0x [expr.new]p20: |
| 1418 | // If the lookup finds the two-parameter form of a usual |
| 1419 | // deallocation function (3.7.4.2) and that function, considered |
| 1420 | // as a placement deallocation function, would have been |
| 1421 | // selected as a match for the allocation function, the program |
| 1422 | // is ill-formed. |
| 1423 | if (NumPlaceArgs && getLangOptions().CPlusPlus0x && |
| 1424 | isNonPlacementDeallocationFunction(OperatorDelete)) { |
| 1425 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1426 | << SourceRange(PlaceArgs[0]->getLocStart(), |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1427 | PlaceArgs[NumPlaceArgs - 1]->getLocEnd()); |
| 1428 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 1429 | << DeleteName; |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1430 | } else { |
| 1431 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1432 | Matches[0].first); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1436 | return false; |
| 1437 | } |
| 1438 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1439 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 1440 | /// function in the specified scope. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1441 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 1442 | DeclarationName Name, Expr** Args, |
| 1443 | unsigned NumArgs, DeclContext *Ctx, |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1444 | bool AllowMissing, FunctionDecl *&Operator, |
| 1445 | bool Diagnose) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1446 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); |
| 1447 | LookupQualifiedName(R, Ctx); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1448 | if (R.empty()) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1449 | if (AllowMissing || !Diagnose) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1450 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1451 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1452 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1455 | if (R.isAmbiguous()) |
| 1456 | return true; |
| 1457 | |
| 1458 | R.suppressDiagnostics(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1459 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1460 | OverloadCandidateSet Candidates(StartLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1461 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 1462 | Alloc != AllocEnd; ++Alloc) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1463 | // Even member operator new/delete are implicitly treated as |
| 1464 | // static, so don't use AddMemberCandidate. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1465 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1466 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1467 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 1468 | AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1469 | /*ExplicitTemplateArgs=*/0, Args, NumArgs, |
| 1470 | Candidates, |
| 1471 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1472 | continue; |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1475 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 1476 | AddOverloadCandidate(Fn, Alloc.getPair(), Args, NumArgs, Candidates, |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1477 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | // Do the resolution. |
| 1481 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1482 | switch (Candidates.BestViableFunction(*this, StartLoc, Best)) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1483 | case OR_Success: { |
| 1484 | // Got one! |
| 1485 | FunctionDecl *FnDecl = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 1486 | MarkDeclarationReferenced(StartLoc, FnDecl); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1487 | // The first argument is size_t, and the first parameter must be size_t, |
| 1488 | // too. This is checked on declaration and can be assumed. (It can't be |
| 1489 | // asserted on, though, since invalid decls are left in there.) |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1490 | // Watch out for variadic allocator function. |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1491 | unsigned NumArgsInFnDecl = FnDecl->getNumParams(); |
| 1492 | for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1493 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 1494 | FnDecl->getParamDecl(i)); |
| 1495 | |
| 1496 | if (!Diagnose && !CanPerformCopyInitialization(Entity, Owned(Args[i]))) |
| 1497 | return true; |
| 1498 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1499 | ExprResult Result |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1500 | = PerformCopyInitialization(Entity, SourceLocation(), Owned(Args[i])); |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1501 | if (Result.isInvalid()) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1502 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1503 | |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1504 | Args[i] = Result.takeAs<Expr>(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1505 | } |
| 1506 | Operator = FnDecl; |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1507 | CheckAllocationAccess(StartLoc, Range, R.getNamingClass(), Best->FoundDecl, |
| 1508 | Diagnose); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1509 | return false; |
| 1510 | } |
| 1511 | |
| 1512 | case OR_No_Viable_Function: |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1513 | if (Diagnose) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1514 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
| 1515 | << Name << Range; |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1516 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 1517 | } |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1518 | return true; |
| 1519 | |
| 1520 | case OR_Ambiguous: |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1521 | if (Diagnose) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1522 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
| 1523 | << Name << Range; |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1524 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
| 1525 | } |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1526 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1527 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1528 | case OR_Deleted: { |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1529 | if (Diagnose) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1530 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 1531 | << Best->Function->isDeleted() |
| 1532 | << Name |
| 1533 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 1534 | << Range; |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1535 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 1536 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1537 | return true; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1538 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1539 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1540 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1544 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 1545 | /// delete. These are: |
| 1546 | /// @code |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1547 | /// // C++03: |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1548 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 1549 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1550 | /// void operator delete(void *) throw(); |
| 1551 | /// void operator delete[](void *) throw(); |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1552 | /// // C++0x: |
| 1553 | /// void* operator new(std::size_t); |
| 1554 | /// void* operator new[](std::size_t); |
| 1555 | /// void operator delete(void *); |
| 1556 | /// void operator delete[](void *); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1557 | /// @endcode |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1558 | /// C++0x operator delete is implicitly noexcept. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1559 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 1560 | /// declared. Their use requires including \<new\>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1562 | if (GlobalNewDeleteDeclared) |
| 1563 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1564 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1565 | // C++ [basic.std.dynamic]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1566 | // [...] The following allocation and deallocation functions (18.4) are |
| 1567 | // implicitly declared in global scope in each translation unit of a |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1568 | // program |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1569 | // |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1570 | // C++03: |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1571 | // void* operator new(std::size_t) throw(std::bad_alloc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1572 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1573 | // void operator delete(void*) throw(); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1574 | // void operator delete[](void*) throw(); |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1575 | // C++0x: |
| 1576 | // void* operator new(std::size_t); |
| 1577 | // void* operator new[](std::size_t); |
| 1578 | // void operator delete(void*); |
| 1579 | // void operator delete[](void*); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1580 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1581 | // These implicit declarations introduce only the function names operator |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1582 | // new, operator new[], operator delete, operator delete[]. |
| 1583 | // |
| 1584 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 1585 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 1586 | // However, we do not make these implicit declarations visible to name |
| 1587 | // lookup. |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1588 | // Note that the C++0x versions of operator delete are deallocation functions, |
| 1589 | // and thus are implicitly noexcept. |
| 1590 | if (!StdBadAlloc && !getLangOptions().CPlusPlus0x) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1591 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 1592 | // implicitly. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1593 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
| 1594 | getOrCreateStdNamespace(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1595 | SourceLocation(), SourceLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1596 | &PP.getIdentifierTable().get("bad_alloc"), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1597 | 0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1598 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1599 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1600 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1601 | GlobalNewDeleteDeclared = true; |
| 1602 | |
| 1603 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 1604 | QualType SizeT = Context.getSizeType(); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1605 | bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1606 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1607 | DeclareGlobalAllocationFunction( |
| 1608 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1609 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1610 | DeclareGlobalAllocationFunction( |
| 1611 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1612 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1613 | DeclareGlobalAllocationFunction( |
| 1614 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 1615 | Context.VoidTy, VoidPtr); |
| 1616 | DeclareGlobalAllocationFunction( |
| 1617 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 1618 | Context.VoidTy, VoidPtr); |
| 1619 | } |
| 1620 | |
| 1621 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 1622 | /// allocation function if it doesn't already exist. |
| 1623 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1624 | QualType Return, QualType Argument, |
| 1625 | bool AddMallocAttr) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1626 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 1627 | |
| 1628 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1629 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 1630 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1631 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1632 | Alloc != AllocEnd; ++Alloc) { |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1633 | // Only look at non-template functions, as it is the predefined, |
| 1634 | // non-templated allocation function we are trying to declare here. |
| 1635 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
| 1636 | QualType InitialParamType = |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 1637 | Context.getCanonicalType( |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1638 | Func->getParamDecl(0)->getType().getUnqualifiedType()); |
| 1639 | // FIXME: Do we need to check for default arguments here? |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1640 | if (Func->getNumParams() == 1 && InitialParamType == Argument) { |
| 1641 | if(AddMallocAttr && !Func->hasAttr<MallocAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1642 | Func->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1643 | return; |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1644 | } |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1645 | } |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1649 | QualType BadAllocType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1650 | bool HasBadAllocExceptionSpec |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1651 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 1652 | Name.getCXXOverloadedOperator() == OO_Array_New); |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1653 | if (HasBadAllocExceptionSpec && !getLangOptions().CPlusPlus0x) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1654 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1655 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1656 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1657 | |
| 1658 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1659 | if (HasBadAllocExceptionSpec) { |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1660 | if (!getLangOptions().CPlusPlus0x) { |
| 1661 | EPI.ExceptionSpecType = EST_Dynamic; |
| 1662 | EPI.NumExceptions = 1; |
| 1663 | EPI.Exceptions = &BadAllocType; |
| 1664 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1665 | } else { |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1666 | EPI.ExceptionSpecType = getLangOptions().CPlusPlus0x ? |
| 1667 | EST_BasicNoexcept : EST_DynamicNone; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1668 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1669 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1670 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, EPI); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1671 | FunctionDecl *Alloc = |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1672 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), |
| 1673 | SourceLocation(), Name, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1674 | FnType, /*TInfo=*/0, SC_None, |
| 1675 | SC_None, false, true); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1676 | Alloc->setImplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1677 | |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1678 | if (AddMallocAttr) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1679 | Alloc->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1680 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1681 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1682 | SourceLocation(), 0, |
| 1683 | Argument, /*TInfo=*/0, |
| 1684 | SC_None, SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1685 | Alloc->setParams(Param); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1686 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1687 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 1688 | // make sure it is at the end of the chain to coincide with the |
| 1689 | // global scope. |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1690 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1693 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 1694 | DeclarationName Name, |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1695 | FunctionDecl* &Operator, bool Diagnose) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1696 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1697 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1698 | LookupQualifiedName(Found, RD); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1699 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1700 | if (Found.isAmbiguous()) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1701 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1702 | |
Chandler Carruth | 2389324 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 1703 | Found.suppressDiagnostics(); |
| 1704 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1705 | SmallVector<DeclAccessPair,4> Matches; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1706 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1707 | F != FEnd; ++F) { |
Chandler Carruth | 09556fd | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 1708 | NamedDecl *ND = (*F)->getUnderlyingDecl(); |
| 1709 | |
| 1710 | // Ignore template operator delete members from the check for a usual |
| 1711 | // deallocation function. |
| 1712 | if (isa<FunctionTemplateDecl>(ND)) |
| 1713 | continue; |
| 1714 | |
| 1715 | if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction()) |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1716 | Matches.push_back(F.getPair()); |
| 1717 | } |
| 1718 | |
| 1719 | // There's exactly one suitable operator; pick it. |
| 1720 | if (Matches.size() == 1) { |
| 1721 | Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl()); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1722 | |
| 1723 | if (Operator->isDeleted()) { |
| 1724 | if (Diagnose) { |
| 1725 | Diag(StartLoc, diag::err_deleted_function_use); |
| 1726 | Diag(Operator->getLocation(), diag::note_unavailable_here) << true; |
| 1727 | } |
| 1728 | return true; |
| 1729 | } |
| 1730 | |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1731 | CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1732 | Matches[0], Diagnose); |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1733 | return false; |
| 1734 | |
| 1735 | // We found multiple suitable operators; complain about the ambiguity. |
| 1736 | } else if (!Matches.empty()) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1737 | if (Diagnose) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1738 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 1739 | << Name << RD; |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1740 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1741 | for (SmallVectorImpl<DeclAccessPair>::iterator |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1742 | F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F) |
| 1743 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1744 | diag::note_member_declared_here) << Name; |
| 1745 | } |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1746 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | // We did find operator delete/operator delete[] declarations, but |
| 1750 | // none of them were suitable. |
| 1751 | if (!Found.empty()) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1752 | if (Diagnose) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1753 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 1754 | << Name << RD; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1755 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1756 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1757 | F != FEnd; ++F) |
| 1758 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1759 | diag::note_member_declared_here) << Name; |
| 1760 | } |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1761 | return true; |
| 1762 | } |
| 1763 | |
| 1764 | // Look for a global declaration. |
| 1765 | DeclareGlobalNewDelete(); |
| 1766 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1767 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1768 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); |
| 1769 | Expr* DeallocArgs[1]; |
| 1770 | DeallocArgs[0] = &Null; |
| 1771 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1772 | DeallocArgs, 1, TUDecl, !Diagnose, |
| 1773 | Operator, Diagnose)) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1774 | return true; |
| 1775 | |
| 1776 | assert(Operator && "Did not find a deallocation function!"); |
| 1777 | return false; |
| 1778 | } |
| 1779 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1780 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 1781 | /// @code ::delete ptr; @endcode |
| 1782 | /// or |
| 1783 | /// @code delete [] ptr; @endcode |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1784 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1785 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1786 | bool ArrayForm, Expr *ExE) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1787 | // C++ [expr.delete]p1: |
| 1788 | // The operand shall have a pointer type, or a class type having a single |
| 1789 | // conversion function to a pointer type. The result has type void. |
| 1790 | // |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1791 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 1792 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1793 | ExprResult Ex = Owned(ExE); |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1794 | FunctionDecl *OperatorDelete = 0; |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1795 | bool ArrayFormAsWritten = ArrayForm; |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1796 | bool UsualArrayDeleteWantsSize = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1798 | if (!Ex.get()->isTypeDependent()) { |
| 1799 | QualType Type = Ex.get()->getType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1801 | if (const RecordType *Record = Type->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1802 | if (RequireCompleteType(StartLoc, Type, |
Douglas Gregor | 254a942 | 2010-07-29 14:44:35 +0000 | [diff] [blame] | 1803 | PDiag(diag::err_delete_incomplete_class_type))) |
| 1804 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1805 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1806 | SmallVector<CXXConversionDecl*, 4> ObjectPtrConversions; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1807 | |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1808 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1809 | const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1810 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1811 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1812 | NamedDecl *D = I.getDecl(); |
| 1813 | if (isa<UsingShadowDecl>(D)) |
| 1814 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1815 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1816 | // Skip over templated conversion functions; they aren't considered. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1817 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1818 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1819 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1820 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1821 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1822 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 1823 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1824 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1825 | ObjectPtrConversions.push_back(Conv); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1826 | } |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1827 | if (ObjectPtrConversions.size() == 1) { |
| 1828 | // We have a single conversion to a pointer-to-object type. Perform |
| 1829 | // that conversion. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1830 | // TODO: don't redo the conversion calculation. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1831 | ExprResult Res = |
| 1832 | PerformImplicitConversion(Ex.get(), |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1833 | ObjectPtrConversions.front()->getConversionType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1834 | AA_Converting); |
| 1835 | if (Res.isUsable()) { |
| 1836 | Ex = move(Res); |
| 1837 | Type = Ex.get()->getType(); |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | else if (ObjectPtrConversions.size() > 1) { |
| 1841 | Diag(StartLoc, diag::err_ambiguous_delete_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1842 | << Type << Ex.get()->getSourceRange(); |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1843 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) |
| 1844 | NoteOverloadCandidate(ObjectPtrConversions[i]); |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1845 | return ExprError(); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1846 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1849 | if (!Type->isPointerType()) |
| 1850 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1851 | << Type << Ex.get()->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1852 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1853 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1854 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 1855 | |
| 1856 | if (unsigned AddressSpace = Pointee.getAddressSpace()) |
| 1857 | return Diag(Ex.get()->getLocStart(), |
| 1858 | diag::err_address_space_qualified_delete) |
| 1859 | << Pointee.getUnqualifiedType() << AddressSpace; |
| 1860 | |
| 1861 | CXXRecordDecl *PointeeRD = 0; |
Douglas Gregor | 94a6157 | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 1862 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1863 | // 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] | 1864 | // effectively bans deletion of "void*". However, most compilers support |
| 1865 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 1866 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1867 | << Type << Ex.get()->getSourceRange(); |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1868 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1869 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1870 | << Type << Ex.get()->getSourceRange()); |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1871 | } else if (!Pointee->isDependentType()) { |
| 1872 | if (!RequireCompleteType(StartLoc, Pointee, |
| 1873 | PDiag(diag::warn_delete_incomplete) |
| 1874 | << Ex.get()->getSourceRange())) { |
| 1875 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
| 1876 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1877 | } |
| 1878 | } |
| 1879 | |
Abramo Bagnara | 30bb420 | 2011-11-16 15:42:13 +0000 | [diff] [blame] | 1880 | // Perform lvalue-to-rvalue cast, if needed. |
| 1881 | Ex = DefaultLvalueConversion(Ex.take()); |
| 1882 | |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 1883 | // C++ [expr.delete]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1884 | // [Note: a pointer to a const type can be the operand of a |
| 1885 | // delete-expression; it is not necessary to cast away the constness |
| 1886 | // (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] | 1887 | // of the delete-expression. ] |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1888 | if (!Context.hasSameType(Ex.get()->getType(), Context.VoidPtrTy)) |
Abramo Bagnara | 30bb420 | 2011-11-16 15:42:13 +0000 | [diff] [blame] | 1889 | Ex = Owned(ImplicitCastExpr::Create(Context, Context.VoidPtrTy, |
| 1890 | CK_BitCast, Ex.take(), 0, VK_RValue)); |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1891 | |
| 1892 | if (Pointee->isArrayType() && !ArrayForm) { |
| 1893 | Diag(StartLoc, diag::warn_delete_array_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1894 | << Type << Ex.get()->getSourceRange() |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1895 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]"); |
| 1896 | ArrayForm = true; |
| 1897 | } |
| 1898 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1899 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1900 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 1901 | |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1902 | if (PointeeRD) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1903 | if (!UseGlobal && |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1904 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, |
| 1905 | OperatorDelete)) |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1906 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1907 | |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1908 | // If we're allocating an array of records, check whether the |
| 1909 | // usual operator delete[] has a size_t parameter. |
| 1910 | if (ArrayForm) { |
| 1911 | // If the user specifically asked to use the global allocator, |
| 1912 | // we'll need to do the lookup into the class. |
| 1913 | if (UseGlobal) |
| 1914 | UsualArrayDeleteWantsSize = |
| 1915 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); |
| 1916 | |
| 1917 | // Otherwise, the usual operator delete[] should be the |
| 1918 | // function we just found. |
| 1919 | else if (isa<CXXMethodDecl>(OperatorDelete)) |
| 1920 | UsualArrayDeleteWantsSize = (OperatorDelete->getNumParams() == 2); |
| 1921 | } |
| 1922 | |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1923 | if (!PointeeRD->hasTrivialDestructor()) |
| 1924 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1925 | MarkDeclarationReferenced(StartLoc, |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1926 | const_cast<CXXDestructorDecl*>(Dtor)); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1927 | DiagnoseUseOfDecl(Dtor, StartLoc); |
| 1928 | } |
Argyrios Kyrtzidis | 6f0074a | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 1929 | |
| 1930 | // C++ [expr.delete]p3: |
| 1931 | // In the first alternative (delete object), if the static type of the |
| 1932 | // object to be deleted is different from its dynamic type, the static |
| 1933 | // type shall be a base class of the dynamic type of the object to be |
| 1934 | // deleted and the static type shall have a virtual destructor or the |
| 1935 | // behavior is undefined. |
| 1936 | // |
| 1937 | // Note: a final class cannot be derived from, no issue there |
Eli Friedman | ef8c79c | 2011-07-26 23:27:24 +0000 | [diff] [blame] | 1938 | if (PointeeRD->isPolymorphic() && !PointeeRD->hasAttr<FinalAttr>()) { |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1939 | CXXDestructorDecl *dtor = PointeeRD->getDestructor(); |
Eli Friedman | ef8c79c | 2011-07-26 23:27:24 +0000 | [diff] [blame] | 1940 | if (dtor && !dtor->isVirtual()) { |
| 1941 | if (PointeeRD->isAbstract()) { |
| 1942 | // If the class is abstract, we warn by default, because we're |
| 1943 | // sure the code has undefined behavior. |
| 1944 | Diag(StartLoc, diag::warn_delete_abstract_non_virtual_dtor) |
| 1945 | << PointeeElem; |
| 1946 | } else if (!ArrayForm) { |
| 1947 | // Otherwise, if this is not an array delete, it's a bit suspect, |
| 1948 | // but not necessarily wrong. |
| 1949 | Diag(StartLoc, diag::warn_delete_non_virtual_dtor) << PointeeElem; |
| 1950 | } |
| 1951 | } |
Argyrios Kyrtzidis | 6f0074a | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 1952 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1953 | |
| 1954 | } else if (getLangOptions().ObjCAutoRefCount && |
| 1955 | PointeeElem->isObjCLifetimeType() && |
| 1956 | (PointeeElem.getObjCLifetime() == Qualifiers::OCL_Strong || |
| 1957 | PointeeElem.getObjCLifetime() == Qualifiers::OCL_Weak) && |
| 1958 | ArrayForm) { |
| 1959 | Diag(StartLoc, diag::warn_err_new_delete_object_array) |
| 1960 | << 1 << PointeeElem; |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1961 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1962 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1963 | if (!OperatorDelete) { |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1964 | // Look for a global declaration. |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1965 | DeclareGlobalNewDelete(); |
| 1966 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1967 | Expr *Arg = Ex.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1969 | &Arg, 1, TUDecl, /*AllowMissing=*/false, |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1970 | OperatorDelete)) |
| 1971 | return ExprError(); |
| 1972 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1974 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | d880f52 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 1976 | // Check access and ambiguity of operator delete and destructor. |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1977 | if (PointeeRD) { |
| 1978 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1979 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
Douglas Gregor | d880f52 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 1980 | PDiag(diag::err_access_dtor) << PointeeElem); |
| 1981 | } |
| 1982 | } |
| 1983 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1986 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1987 | ArrayFormAsWritten, |
| 1988 | UsualArrayDeleteWantsSize, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1989 | OperatorDelete, Ex.take(), StartLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1992 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 1993 | /// while, do-while, or switch statement. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1994 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1995 | SourceLocation StmtLoc, |
| 1996 | bool ConvertToBoolean) { |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1997 | QualType T = ConditionVar->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1999 | // C++ [stmt.select]p2: |
| 2000 | // The declarator shall not specify a function or an array. |
| 2001 | if (T->isFunctionType()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2002 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2003 | diag::err_invalid_use_of_function_type) |
| 2004 | << ConditionVar->getSourceRange()); |
| 2005 | else if (T->isArrayType()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2006 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2007 | diag::err_invalid_use_of_array_type) |
| 2008 | << ConditionVar->getSourceRange()); |
Douglas Gregor | a7605db | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 2009 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2010 | ExprResult Condition = |
| 2011 | Owned(DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2012 | ConditionVar, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2013 | ConditionVar->getLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2014 | ConditionVar->getType().getNonReferenceType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2015 | VK_LValue)); |
| 2016 | if (ConvertToBoolean) { |
| 2017 | Condition = CheckBooleanCondition(Condition.take(), StmtLoc); |
| 2018 | if (Condition.isInvalid()) |
| 2019 | return ExprError(); |
| 2020 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2021 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2022 | return move(Condition); |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2025 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2026 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr) { |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2027 | // C++ 6.4p4: |
| 2028 | // The value of a condition that is an initialized declaration in a statement |
| 2029 | // other than a switch statement is the value of the declared variable |
| 2030 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 2031 | // program is ill-formed. |
| 2032 | // The value of a condition that is an expression is the value of the |
| 2033 | // expression, implicitly converted to bool. |
| 2034 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2035 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2036 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2037 | |
| 2038 | /// Helper function to determine whether this is the (deprecated) C++ |
| 2039 | /// conversion from a string literal to a pointer to non-const char or |
| 2040 | /// non-const wchar_t (for narrow and wide string literals, |
| 2041 | /// respectively). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | bool |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2043 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 2044 | // Look inside the implicit cast, if it exists. |
| 2045 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 2046 | From = Cast->getSubExpr(); |
| 2047 | |
| 2048 | // A string literal (2.13.4) that is not a wide string literal can |
| 2049 | // be converted to an rvalue of type "pointer to char"; a wide |
| 2050 | // string literal can be converted to an rvalue of type "pointer |
| 2051 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 1984eb9 | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 2052 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2053 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | if (const BuiltinType *ToPointeeType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2055 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2056 | // This conversion is considered only when there is an |
| 2057 | // explicit appropriate pointer target type (C++ 4.2p2). |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2058 | if (!ToPtrType->getPointeeType().hasQualifiers()) { |
| 2059 | switch (StrLit->getKind()) { |
| 2060 | case StringLiteral::UTF8: |
| 2061 | case StringLiteral::UTF16: |
| 2062 | case StringLiteral::UTF32: |
| 2063 | // We don't allow UTF literals to be implicitly converted |
| 2064 | break; |
| 2065 | case StringLiteral::Ascii: |
| 2066 | return (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 2067 | ToPointeeType->getKind() == BuiltinType::Char_S); |
| 2068 | case StringLiteral::Wide: |
| 2069 | return ToPointeeType->isWideCharType(); |
| 2070 | } |
| 2071 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | return false; |
| 2075 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2076 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2077 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2078 | SourceLocation CastLoc, |
| 2079 | QualType Ty, |
| 2080 | CastKind Kind, |
| 2081 | CXXMethodDecl *Method, |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2082 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2083 | bool HadMultipleCandidates, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2084 | Expr *From) { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2085 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2086 | default: llvm_unreachable("Unhandled cast kind!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2087 | case CK_ConstructorConversion: { |
Douglas Gregor | 13e1bca | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2088 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2089 | ASTOwningVector<Expr*> ConstructorArgs(S); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | 13e1bca | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2091 | if (S.CompleteConstructorCall(Constructor, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2092 | MultiExprArg(&From, 1), |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2093 | CastLoc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2094 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2095 | |
Douglas Gregor | 13e1bca | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2096 | S.CheckConstructorAccess(CastLoc, Constructor, Constructor->getAccess(), |
| 2097 | S.PDiag(diag::err_access_ctor)); |
| 2098 | |
| 2099 | ExprResult Result |
| 2100 | = S.BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), |
| 2101 | move_arg(ConstructorArgs), |
| 2102 | HadMultipleCandidates, /*ZeroInit*/ false, |
| 2103 | CXXConstructExpr::CK_Complete, SourceRange()); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2104 | if (Result.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2105 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2106 | |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2107 | return S.MaybeBindToTemporary(Result.takeAs<Expr>()); |
| 2108 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2109 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2110 | case CK_UserDefinedConversion: { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2111 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2113 | // Create an implicit call expr that calls it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2114 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Method, |
| 2115 | HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2116 | if (Result.isInvalid()) |
| 2117 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 2118 | // Record usage of conversion in an implicit cast. |
| 2119 | Result = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 2120 | Result.get()->getType(), |
| 2121 | CK_UserDefinedConversion, |
| 2122 | Result.get(), 0, |
| 2123 | Result.get()->getValueKind())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2124 | |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2125 | S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ 0, FoundDecl); |
| 2126 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2127 | return S.MaybeBindToTemporary(Result.get()); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2128 | } |
| 2129 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2130 | } |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2131 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2132 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 2133 | /// expression From to the type ToType using the pre-computed implicit |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2134 | /// conversion sequence ICS. Returns the converted |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2135 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2136 | /// used in the error message. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2137 | ExprResult |
| 2138 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2139 | const ImplicitConversionSequence &ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2140 | AssignmentAction Action, |
| 2141 | CheckedConversionKind CCK) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2142 | switch (ICS.getKind()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2143 | case ImplicitConversionSequence::StandardConversion: { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2144 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, |
| 2145 | Action, CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2146 | if (Res.isInvalid()) |
| 2147 | return ExprError(); |
| 2148 | From = Res.take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2149 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2150 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2151 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2152 | case ImplicitConversionSequence::UserDefinedConversion: { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2153 | |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 2154 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2155 | CastKind CastKind; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2156 | QualType BeforeToType; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 2157 | assert(FD && "FIXME: aggregate initialization from init list"); |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2158 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2159 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2160 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2161 | // If the user-defined conversion is specified by a conversion function, |
| 2162 | // the initial standard conversion sequence converts the source type to |
| 2163 | // the implicit object parameter of the conversion function. |
| 2164 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
John McCall | 9ec9445 | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 2165 | } else { |
| 2166 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2167 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2168 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2169 | if (!ICS.UserDefined.EllipsisConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2170 | // If the user-defined conversion is specified by a constructor, the |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2171 | // initial standard conversion sequence converts the source type to the |
| 2172 | // type required by the argument of the constructor |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2173 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 2174 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2175 | } |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2176 | // Watch out for elipsis conversion. |
Fariborz Jahanian | 4c0cea2 | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 2177 | if (!ICS.UserDefined.EllipsisConversion) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2178 | ExprResult Res = |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2179 | PerformImplicitConversion(From, BeforeToType, |
| 2180 | ICS.UserDefined.Before, AA_Converting, |
| 2181 | CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2182 | if (Res.isInvalid()) |
| 2183 | return ExprError(); |
| 2184 | From = Res.take(); |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2185 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2186 | |
| 2187 | ExprResult CastArg |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2188 | = BuildCXXCastArgument(*this, |
| 2189 | From->getLocStart(), |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2190 | ToType.getNonReferenceType(), |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2191 | CastKind, cast<CXXMethodDecl>(FD), |
| 2192 | ICS.UserDefined.FoundConversionFunction, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2193 | ICS.UserDefined.HadMultipleCandidates, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2194 | From); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2195 | |
| 2196 | if (CastArg.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2197 | return ExprError(); |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2198 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2199 | From = CastArg.take(); |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2200 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2201 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
| 2202 | AA_Converting, CCK); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 2203 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2204 | |
| 2205 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2206 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2207 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 2208 | << From->getSourceRange()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2209 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2211 | case ImplicitConversionSequence::EllipsisConversion: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2212 | llvm_unreachable("Cannot perform an ellipsis conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2213 | |
| 2214 | case ImplicitConversionSequence::BadConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2215 | return ExprError(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | // Everything went well. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2219 | return Owned(From); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2222 | /// PerformImplicitConversion - Perform an implicit conversion of the |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2223 | /// expression From to the type ToType by following the standard |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2224 | /// conversion sequence SCS. Returns the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2225 | /// expression. Flavor is the context in which we're performing this |
| 2226 | /// conversion, for use in error messages. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2227 | ExprResult |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2228 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2229 | const StandardConversionSequence& SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2230 | AssignmentAction Action, |
| 2231 | CheckedConversionKind CCK) { |
| 2232 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
| 2233 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2234 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 2235 | // much extra work. What this means is that we need to keep track of more |
| 2236 | // information that is computed when we try the implicit conversion initially, |
| 2237 | // so that we don't need to recompute anything here. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2238 | QualType FromType = From->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2239 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2240 | if (SCS.CopyConstructor) { |
Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 2241 | // FIXME: When can ToType be a reference type? |
| 2242 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2243 | if (SCS.Second == ICK_Derived_To_Base) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2244 | ASTOwningVector<Expr*> ConstructorArgs(*this); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2245 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2246 | MultiExprArg(*this, &From, 1), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2247 | /*FIXME:ConstructLoc*/SourceLocation(), |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2248 | ConstructorArgs)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2249 | return ExprError(); |
| 2250 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 2251 | ToType, SCS.CopyConstructor, |
| 2252 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2253 | /*HadMultipleCandidates*/ false, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2254 | /*ZeroInit*/ false, |
| 2255 | CXXConstructExpr::CK_Complete, |
| 2256 | SourceRange()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2257 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2258 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 2259 | ToType, SCS.CopyConstructor, |
| 2260 | MultiExprArg(*this, &From, 1), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2261 | /*HadMultipleCandidates*/ false, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2262 | /*ZeroInit*/ false, |
| 2263 | CXXConstructExpr::CK_Complete, |
| 2264 | SourceRange()); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2267 | // Resolve overloaded function references. |
| 2268 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 2269 | DeclAccessPair Found; |
| 2270 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 2271 | true, Found); |
| 2272 | if (!Fn) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2273 | return ExprError(); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2274 | |
| 2275 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2276 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2277 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2278 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 2279 | FromType = From->getType(); |
| 2280 | } |
| 2281 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2282 | // Perform the first implicit conversion. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2283 | switch (SCS.First) { |
| 2284 | case ICK_Identity: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2285 | // Nothing to do. |
| 2286 | break; |
| 2287 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2288 | case ICK_Lvalue_To_Rvalue: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 2289 | assert(From->getObjectKind() != OK_ObjCProperty); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2290 | FromType = FromType.getUnqualifiedType(); |
| 2291 | From = ImplicitCastExpr::Create(Context, FromType, CK_LValueToRValue, |
| 2292 | From, 0, VK_RValue); |
| 2293 | break; |
| 2294 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2295 | case ICK_Array_To_Pointer: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2296 | FromType = Context.getArrayDecayedType(FromType); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2297 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, |
| 2298 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2299 | break; |
| 2300 | |
| 2301 | case ICK_Function_To_Pointer: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2302 | FromType = Context.getPointerType(FromType); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2303 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, |
| 2304 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2305 | break; |
| 2306 | |
| 2307 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2308 | llvm_unreachable("Improper first standard conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2311 | // Perform the second implicit conversion |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2312 | switch (SCS.Second) { |
| 2313 | case ICK_Identity: |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2314 | // If both sides are functions (or pointers/references to them), there could |
| 2315 | // be incompatible exception declarations. |
| 2316 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2317 | return ExprError(); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2318 | // Nothing else to do. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2319 | break; |
| 2320 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2321 | case ICK_NoReturn_Adjustment: |
| 2322 | // If both sides are functions (or pointers/references to them), there could |
| 2323 | // be incompatible exception declarations. |
| 2324 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2325 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2326 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2327 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 2328 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2329 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2330 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2331 | case ICK_Integral_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2332 | case ICK_Integral_Conversion: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2333 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, |
| 2334 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2335 | break; |
| 2336 | |
| 2337 | case ICK_Floating_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2338 | case ICK_Floating_Conversion: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2339 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, |
| 2340 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | |
| 2343 | case ICK_Complex_Promotion: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2344 | case ICK_Complex_Conversion: { |
| 2345 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
| 2346 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
| 2347 | CastKind CK; |
| 2348 | if (FromEl->isRealFloatingType()) { |
| 2349 | if (ToEl->isRealFloatingType()) |
| 2350 | CK = CK_FloatingComplexCast; |
| 2351 | else |
| 2352 | CK = CK_FloatingComplexToIntegralComplex; |
| 2353 | } else if (ToEl->isRealFloatingType()) { |
| 2354 | CK = CK_IntegralComplexToFloatingComplex; |
| 2355 | } else { |
| 2356 | CK = CK_IntegralComplexCast; |
| 2357 | } |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2358 | From = ImpCastExprToType(From, ToType, CK, |
| 2359 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2360 | break; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2361 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2362 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2363 | case ICK_Floating_Integral: |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 2364 | if (ToType->isRealFloatingType()) |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2365 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, |
| 2366 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2367 | else |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2368 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, |
| 2369 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2370 | break; |
| 2371 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2372 | case ICK_Compatible_Conversion: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2373 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 2374 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2375 | break; |
| 2376 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2377 | case ICK_Writeback_Conversion: |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2378 | case ICK_Pointer_Conversion: { |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2379 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2380 | // Diagnose incompatible Objective-C conversions |
Douglas Gregor | 8cf0d22 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 2381 | if (Action == AA_Initializing || Action == AA_Assigning) |
Fariborz Jahanian | 84950c7 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2382 | Diag(From->getSourceRange().getBegin(), |
| 2383 | diag::ext_typecheck_convert_incompatible_pointer) |
| 2384 | << ToType << From->getType() << Action |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 2385 | << From->getSourceRange() << 0; |
Fariborz Jahanian | 84950c7 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2386 | else |
| 2387 | Diag(From->getSourceRange().getBegin(), |
| 2388 | diag::ext_typecheck_convert_incompatible_pointer) |
| 2389 | << From->getType() << ToType << Action |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 2390 | << From->getSourceRange() << 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2391 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2392 | if (From->getType()->isObjCObjectPointerType() && |
| 2393 | ToType->isObjCObjectPointerType()) |
| 2394 | EmitRelatedResultTypeNote(From); |
Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2395 | } |
| 2396 | else if (getLangOptions().ObjCAutoRefCount && |
| 2397 | !CheckObjCARCUnavailableWeakConversion(ToType, |
| 2398 | From->getType())) { |
John McCall | 7f3a6d3 | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 2399 | if (Action == AA_Initializing) |
| 2400 | Diag(From->getSourceRange().getBegin(), |
| 2401 | diag::err_arc_weak_unavailable_assign); |
| 2402 | else |
| 2403 | Diag(From->getSourceRange().getBegin(), |
| 2404 | diag::err_arc_convesion_of_weak_unavailable) |
| 2405 | << (Action == AA_Casting) << From->getType() << ToType |
| 2406 | << From->getSourceRange(); |
| 2407 | } |
Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2408 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2409 | CastKind Kind = CK_Invalid; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2410 | CXXCastPath BasePath; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2411 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2412 | return ExprError(); |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Make sure we extend blocks if necessary. |
| 2415 | // FIXME: doing this here is really ugly. |
| 2416 | if (Kind == CK_BlockPointerToObjCPointerCast) { |
| 2417 | ExprResult E = From; |
| 2418 | (void) PrepareCastToObjCObjectPointer(E); |
| 2419 | From = E.take(); |
| 2420 | } |
| 2421 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2422 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
| 2423 | .take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2424 | break; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2425 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2426 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2427 | case ICK_Pointer_Member: { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2428 | CastKind Kind = CK_Invalid; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2429 | CXXCastPath BasePath; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2430 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2431 | return ExprError(); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2432 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2433 | return ExprError(); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2434 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
| 2435 | .take(); |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2436 | break; |
| 2437 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2438 | |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 2439 | case ICK_Boolean_Conversion: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2440 | // Perform half-to-boolean conversion via float. |
| 2441 | if (From->getType()->isHalfType()) { |
| 2442 | From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).take(); |
| 2443 | FromType = Context.FloatTy; |
| 2444 | } |
| 2445 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2446 | From = ImpCastExprToType(From, Context.BoolTy, |
| 2447 | ScalarTypeToBooleanCastKind(FromType), |
| 2448 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2449 | break; |
| 2450 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2451 | case ICK_Derived_To_Base: { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2452 | CXXCastPath BasePath; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2453 | if (CheckDerivedToBaseConversion(From->getType(), |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2454 | ToType.getNonReferenceType(), |
| 2455 | From->getLocStart(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2456 | From->getSourceRange(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2457 | &BasePath, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2458 | CStyle)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2459 | return ExprError(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2460 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2461 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 2462 | CK_DerivedToBase, From->getValueKind(), |
| 2463 | &BasePath, CCK).take(); |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2464 | break; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2467 | case ICK_Vector_Conversion: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2468 | From = ImpCastExprToType(From, ToType, CK_BitCast, |
| 2469 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2470 | break; |
| 2471 | |
| 2472 | case ICK_Vector_Splat: |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2473 | From = ImpCastExprToType(From, ToType, CK_VectorSplat, |
| 2474 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2475 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2476 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2477 | case ICK_Complex_Real: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2478 | // Case 1. x -> _Complex y |
| 2479 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
| 2480 | QualType ElType = ToComplex->getElementType(); |
| 2481 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2482 | |
| 2483 | // x -> y |
| 2484 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
| 2485 | // do nothing |
| 2486 | } else if (From->getType()->isRealFloatingType()) { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2487 | From = ImpCastExprToType(From, ElType, |
| 2488 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2489 | } else { |
| 2490 | assert(From->getType()->isIntegerType()); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2491 | From = ImpCastExprToType(From, ElType, |
| 2492 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2493 | } |
| 2494 | // y -> _Complex y |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2495 | From = ImpCastExprToType(From, ToType, |
| 2496 | isFloatingComplex ? CK_FloatingRealToComplex |
| 2497 | : CK_IntegralRealToComplex).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2498 | |
| 2499 | // Case 2. _Complex x -> y |
| 2500 | } else { |
| 2501 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
| 2502 | assert(FromComplex); |
| 2503 | |
| 2504 | QualType ElType = FromComplex->getElementType(); |
| 2505 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2506 | |
| 2507 | // _Complex x -> x |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2508 | From = ImpCastExprToType(From, ElType, |
| 2509 | isFloatingComplex ? CK_FloatingComplexToReal |
| 2510 | : CK_IntegralComplexToReal, |
| 2511 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2512 | |
| 2513 | // x -> y |
| 2514 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
| 2515 | // do nothing |
| 2516 | } else if (ToType->isRealFloatingType()) { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2517 | From = ImpCastExprToType(From, ToType, |
| 2518 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, |
| 2519 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2520 | } else { |
| 2521 | assert(ToType->isIntegerType()); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2522 | From = ImpCastExprToType(From, ToType, |
| 2523 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, |
| 2524 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2525 | } |
| 2526 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2527 | break; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2528 | |
| 2529 | case ICK_Block_Pointer_Conversion: { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2530 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, |
| 2531 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2532 | break; |
| 2533 | } |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2534 | |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2535 | case ICK_TransparentUnionConversion: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2536 | ExprResult FromRes = Owned(From); |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2537 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2538 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); |
| 2539 | if (FromRes.isInvalid()) |
| 2540 | return ExprError(); |
| 2541 | From = FromRes.take(); |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2542 | assert ((ConvTy == Sema::Compatible) && |
| 2543 | "Improper transparent union conversion"); |
| 2544 | (void)ConvTy; |
| 2545 | break; |
| 2546 | } |
| 2547 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2548 | case ICK_Lvalue_To_Rvalue: |
| 2549 | case ICK_Array_To_Pointer: |
| 2550 | case ICK_Function_To_Pointer: |
| 2551 | case ICK_Qualification: |
| 2552 | case ICK_Num_Conversion_Kinds: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2553 | llvm_unreachable("Improper second standard conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | switch (SCS.Third) { |
| 2557 | case ICK_Identity: |
| 2558 | // Nothing to do. |
| 2559 | break; |
| 2560 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2561 | case ICK_Qualification: { |
| 2562 | // The qualification keeps the category of the inner expression, unless the |
| 2563 | // target type isn't a reference. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2564 | ExprValueKind VK = ToType->isReferenceType() ? |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 2565 | From->getValueKind() : VK_RValue; |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2566 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
| 2567 | CK_NoOp, VK, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2568 | |
Douglas Gregor | 069a6da | 2011-03-14 16:13:32 +0000 | [diff] [blame] | 2569 | if (SCS.DeprecatedStringLiteralToCharPtr && |
| 2570 | !getLangOptions().WritableStrings) |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2571 | Diag(From->getLocStart(), diag::warn_deprecated_string_literal_conversion) |
| 2572 | << ToType.getNonReferenceType(); |
| 2573 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2574 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2577 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2578 | llvm_unreachable("Improper third standard conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2581 | return Owned(From); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2584 | ExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2585 | SourceLocation KWLoc, |
| 2586 | ParsedType Ty, |
| 2587 | SourceLocation RParen) { |
| 2588 | TypeSourceInfo *TSInfo; |
| 2589 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2591 | if (!TSInfo) |
| 2592 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2593 | return BuildUnaryTypeTrait(UTT, KWLoc, TSInfo, RParen); |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2596 | /// \brief Check the completeness of a type in a unary type trait. |
| 2597 | /// |
| 2598 | /// If the particular type trait requires a complete type, tries to complete |
| 2599 | /// it. If completing the type fails, a diagnostic is emitted and false |
| 2600 | /// returned. If completing the type succeeds or no completion was required, |
| 2601 | /// returns true. |
| 2602 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, |
| 2603 | UnaryTypeTrait UTT, |
| 2604 | SourceLocation Loc, |
| 2605 | QualType ArgTy) { |
| 2606 | // C++0x [meta.unary.prop]p3: |
| 2607 | // For all of the class templates X declared in this Clause, instantiating |
| 2608 | // that template with a template argument that is a class template |
| 2609 | // specialization may result in the implicit instantiation of the template |
| 2610 | // argument if and only if the semantics of X require that the argument |
| 2611 | // must be a complete type. |
| 2612 | // We apply this rule to all the type trait expressions used to implement |
| 2613 | // these class templates. We also try to follow any GCC documented behavior |
| 2614 | // in these expressions to ensure portability of standard libraries. |
| 2615 | switch (UTT) { |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2616 | // is_complete_type somewhat obviously cannot require a complete type. |
| 2617 | case UTT_IsCompleteType: |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2618 | // Fall-through |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2619 | |
| 2620 | // These traits are modeled on the type predicates in C++0x |
| 2621 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as |
| 2622 | // requiring a complete type, as whether or not they return true cannot be |
| 2623 | // impacted by the completeness of the type. |
| 2624 | case UTT_IsVoid: |
| 2625 | case UTT_IsIntegral: |
| 2626 | case UTT_IsFloatingPoint: |
| 2627 | case UTT_IsArray: |
| 2628 | case UTT_IsPointer: |
| 2629 | case UTT_IsLvalueReference: |
| 2630 | case UTT_IsRvalueReference: |
| 2631 | case UTT_IsMemberFunctionPointer: |
| 2632 | case UTT_IsMemberObjectPointer: |
| 2633 | case UTT_IsEnum: |
| 2634 | case UTT_IsUnion: |
| 2635 | case UTT_IsClass: |
| 2636 | case UTT_IsFunction: |
| 2637 | case UTT_IsReference: |
| 2638 | case UTT_IsArithmetic: |
| 2639 | case UTT_IsFundamental: |
| 2640 | case UTT_IsObject: |
| 2641 | case UTT_IsScalar: |
| 2642 | case UTT_IsCompound: |
| 2643 | case UTT_IsMemberPointer: |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2644 | // Fall-through |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2645 | |
| 2646 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] |
| 2647 | // which requires some of its traits to have the complete type. However, |
| 2648 | // the completeness of the type cannot impact these traits' semantics, and |
| 2649 | // so they don't require it. This matches the comments on these traits in |
| 2650 | // Table 49. |
| 2651 | case UTT_IsConst: |
| 2652 | case UTT_IsVolatile: |
| 2653 | case UTT_IsSigned: |
| 2654 | case UTT_IsUnsigned: |
| 2655 | return true; |
| 2656 | |
| 2657 | // 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] | 2658 | // applied to a complete type. |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2659 | case UTT_IsTrivial: |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2660 | case UTT_IsTriviallyCopyable: |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2661 | case UTT_IsStandardLayout: |
| 2662 | case UTT_IsPOD: |
| 2663 | case UTT_IsLiteral: |
| 2664 | case UTT_IsEmpty: |
| 2665 | case UTT_IsPolymorphic: |
| 2666 | case UTT_IsAbstract: |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2667 | // Fall-through |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2668 | |
Douglas Gregor | 5e9392b | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 2669 | // These traits require a complete type. |
| 2670 | case UTT_IsFinal: |
| 2671 | |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2672 | // These trait expressions are designed to help implement predicates in |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2673 | // [meta.unary.prop] despite not being named the same. They are specified |
| 2674 | // by both GCC and the Embarcadero C++ compiler, and require the complete |
| 2675 | // type due to the overarching C++0x type predicates being implemented |
| 2676 | // requiring the complete type. |
| 2677 | case UTT_HasNothrowAssign: |
| 2678 | case UTT_HasNothrowConstructor: |
| 2679 | case UTT_HasNothrowCopy: |
| 2680 | case UTT_HasTrivialAssign: |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2681 | case UTT_HasTrivialDefaultConstructor: |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2682 | case UTT_HasTrivialCopy: |
| 2683 | case UTT_HasTrivialDestructor: |
| 2684 | case UTT_HasVirtualDestructor: |
| 2685 | // Arrays of unknown bound are expressly allowed. |
| 2686 | QualType ElTy = ArgTy; |
| 2687 | if (ArgTy->isIncompleteArrayType()) |
| 2688 | ElTy = S.Context.getAsArrayType(ArgTy)->getElementType(); |
| 2689 | |
| 2690 | // The void type is expressly allowed. |
| 2691 | if (ElTy->isVoidType()) |
| 2692 | return true; |
| 2693 | |
| 2694 | return !S.RequireCompleteType( |
| 2695 | Loc, ElTy, diag::err_incomplete_type_used_in_type_trait_expr); |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2696 | } |
Chandler Carruth | 73e0a91 | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 2697 | llvm_unreachable("Type trait not handled by switch"); |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | static bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT, |
| 2701 | SourceLocation KeyLoc, QualType T) { |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 2702 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2703 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2704 | ASTContext &C = Self.Context; |
| 2705 | switch(UTT) { |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2706 | // Type trait expressions corresponding to the primary type category |
| 2707 | // predicates in C++0x [meta.unary.cat]. |
| 2708 | case UTT_IsVoid: |
| 2709 | return T->isVoidType(); |
| 2710 | case UTT_IsIntegral: |
| 2711 | return T->isIntegralType(C); |
| 2712 | case UTT_IsFloatingPoint: |
| 2713 | return T->isFloatingType(); |
| 2714 | case UTT_IsArray: |
| 2715 | return T->isArrayType(); |
| 2716 | case UTT_IsPointer: |
| 2717 | return T->isPointerType(); |
| 2718 | case UTT_IsLvalueReference: |
| 2719 | return T->isLValueReferenceType(); |
| 2720 | case UTT_IsRvalueReference: |
| 2721 | return T->isRValueReferenceType(); |
| 2722 | case UTT_IsMemberFunctionPointer: |
| 2723 | return T->isMemberFunctionPointerType(); |
| 2724 | case UTT_IsMemberObjectPointer: |
| 2725 | return T->isMemberDataPointerType(); |
| 2726 | case UTT_IsEnum: |
| 2727 | return T->isEnumeralType(); |
Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2728 | case UTT_IsUnion: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2729 | return T->isUnionType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2730 | case UTT_IsClass: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2731 | return T->isClassType() || T->isStructureType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2732 | case UTT_IsFunction: |
| 2733 | return T->isFunctionType(); |
| 2734 | |
| 2735 | // Type trait expressions which correspond to the convenient composition |
| 2736 | // predicates in C++0x [meta.unary.comp]. |
| 2737 | case UTT_IsReference: |
| 2738 | return T->isReferenceType(); |
| 2739 | case UTT_IsArithmetic: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2740 | return T->isArithmeticType() && !T->isEnumeralType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2741 | case UTT_IsFundamental: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2742 | return T->isFundamentalType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2743 | case UTT_IsObject: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2744 | return T->isObjectType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2745 | case UTT_IsScalar: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2746 | // Note: semantic analysis depends on Objective-C lifetime types to be |
| 2747 | // considered scalar types. However, such types do not actually behave |
| 2748 | // like scalar types at run time (since they may require retain/release |
| 2749 | // operations), so we report them as non-scalar. |
| 2750 | if (T->isObjCLifetimeType()) { |
| 2751 | switch (T.getObjCLifetime()) { |
| 2752 | case Qualifiers::OCL_None: |
| 2753 | case Qualifiers::OCL_ExplicitNone: |
| 2754 | return true; |
| 2755 | |
| 2756 | case Qualifiers::OCL_Strong: |
| 2757 | case Qualifiers::OCL_Weak: |
| 2758 | case Qualifiers::OCL_Autoreleasing: |
| 2759 | return false; |
| 2760 | } |
| 2761 | } |
| 2762 | |
Chandler Carruth | cec0ced | 2011-05-01 09:29:55 +0000 | [diff] [blame] | 2763 | return T->isScalarType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2764 | case UTT_IsCompound: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2765 | return T->isCompoundType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2766 | case UTT_IsMemberPointer: |
| 2767 | return T->isMemberPointerType(); |
| 2768 | |
| 2769 | // Type trait expressions which correspond to the type property predicates |
| 2770 | // in C++0x [meta.unary.prop]. |
| 2771 | case UTT_IsConst: |
| 2772 | return T.isConstQualified(); |
| 2773 | case UTT_IsVolatile: |
| 2774 | return T.isVolatileQualified(); |
| 2775 | case UTT_IsTrivial: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2776 | return T.isTrivialType(Self.Context); |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2777 | case UTT_IsTriviallyCopyable: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2778 | return T.isTriviallyCopyableType(Self.Context); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2779 | case UTT_IsStandardLayout: |
| 2780 | return T->isStandardLayoutType(); |
| 2781 | case UTT_IsPOD: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2782 | return T.isPODType(Self.Context); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2783 | case UTT_IsLiteral: |
| 2784 | return T->isLiteralType(); |
| 2785 | case UTT_IsEmpty: |
| 2786 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2787 | return !RD->isUnion() && RD->isEmpty(); |
| 2788 | return false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2789 | case UTT_IsPolymorphic: |
Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2790 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2791 | return RD->isPolymorphic(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2792 | return false; |
| 2793 | case UTT_IsAbstract: |
Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2794 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2795 | return RD->isAbstract(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2796 | return false; |
Douglas Gregor | 5e9392b | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 2797 | case UTT_IsFinal: |
| 2798 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2799 | return RD->hasAttr<FinalAttr>(); |
| 2800 | return false; |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2801 | case UTT_IsSigned: |
| 2802 | return T->isSignedIntegerType(); |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2803 | case UTT_IsUnsigned: |
| 2804 | return T->isUnsignedIntegerType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2805 | |
| 2806 | // Type trait expressions which query classes regarding their construction, |
| 2807 | // destruction, and copying. Rather than being based directly on the |
| 2808 | // related type predicates in the standard, they are specified by both |
| 2809 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those |
| 2810 | // specifications. |
| 2811 | // |
| 2812 | // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html |
| 2813 | // 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] | 2814 | case UTT_HasTrivialDefaultConstructor: |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2815 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2816 | // If __is_pod (type) is true then the trait is true, else if type is |
| 2817 | // a cv class or union type (or array thereof) with a trivial default |
| 2818 | // constructor ([class.ctor]) then the trait is true, else it is false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2819 | if (T.isPODType(Self.Context)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2820 | return true; |
| 2821 | if (const RecordType *RT = |
| 2822 | C.getBaseElementType(T)->getAs<RecordType>()) |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2823 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDefaultConstructor(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2824 | return false; |
| 2825 | case UTT_HasTrivialCopy: |
| 2826 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2827 | // If __is_pod (type) is true or type is a reference type then |
| 2828 | // the trait is true, else if type is a cv class or union type |
| 2829 | // with a trivial copy constructor ([class.copy]) then the trait |
| 2830 | // is true, else it is false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2831 | if (T.isPODType(Self.Context) || T->isReferenceType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2832 | return true; |
| 2833 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2834 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor(); |
| 2835 | return false; |
| 2836 | case UTT_HasTrivialAssign: |
| 2837 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2838 | // If type is const qualified or is a reference type then the |
| 2839 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 2840 | // trait is true, else if type is a cv class or union type with |
| 2841 | // a trivial copy assignment ([class.copy]) then the trait is |
| 2842 | // true, else it is false. |
| 2843 | // Note: the const and reference restrictions are interesting, |
| 2844 | // given that const and reference members don't prevent a class |
| 2845 | // from having a trivial copy assignment operator (but do cause |
| 2846 | // errors if the copy assignment operator is actually used, q.v. |
| 2847 | // [class.copy]p12). |
| 2848 | |
| 2849 | if (C.getBaseElementType(T).isConstQualified()) |
| 2850 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2851 | if (T.isPODType(Self.Context)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2852 | return true; |
| 2853 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2854 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment(); |
| 2855 | return false; |
| 2856 | case UTT_HasTrivialDestructor: |
| 2857 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2858 | // If __is_pod (type) is true or type is a reference type |
| 2859 | // then the trait is true, else if type is a cv class or union |
| 2860 | // type (or array thereof) with a trivial destructor |
| 2861 | // ([class.dtor]) then the trait is true, else it is |
| 2862 | // false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2863 | if (T.isPODType(Self.Context) || T->isReferenceType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2864 | return true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2865 | |
| 2866 | // Objective-C++ ARC: autorelease types don't require destruction. |
| 2867 | if (T->isObjCLifetimeType() && |
| 2868 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 2869 | return true; |
| 2870 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2871 | if (const RecordType *RT = |
| 2872 | C.getBaseElementType(T)->getAs<RecordType>()) |
| 2873 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor(); |
| 2874 | return false; |
| 2875 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 2876 | case UTT_HasNothrowAssign: |
| 2877 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2878 | // If type is const qualified or is a reference type then the |
| 2879 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 2880 | // is true then the trait is true, else if type is a cv class |
| 2881 | // or union type with copy assignment operators that are known |
| 2882 | // not to throw an exception then the trait is true, else it is |
| 2883 | // false. |
| 2884 | if (C.getBaseElementType(T).isConstQualified()) |
| 2885 | return false; |
| 2886 | if (T->isReferenceType()) |
| 2887 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2888 | if (T.isPODType(Self.Context) || T->isObjCLifetimeType()) |
| 2889 | return true; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2890 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2891 | CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2892 | if (RD->hasTrivialCopyAssignment()) |
| 2893 | return true; |
| 2894 | |
| 2895 | bool FoundAssign = false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2896 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2897 | LookupResult Res(Self, DeclarationNameInfo(Name, KeyLoc), |
| 2898 | Sema::LookupOrdinaryName); |
| 2899 | if (Self.LookupQualifiedName(Res, RD)) { |
Douglas Gregor | d41679d | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 2900 | Res.suppressDiagnostics(); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2901 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 2902 | Op != OpEnd; ++Op) { |
Douglas Gregor | d41679d | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 2903 | if (isa<FunctionTemplateDecl>(*Op)) |
| 2904 | continue; |
| 2905 | |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2906 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 2907 | if (Operator->isCopyAssignmentOperator()) { |
| 2908 | FoundAssign = true; |
| 2909 | const FunctionProtoType *CPT |
| 2910 | = Operator->getType()->getAs<FunctionProtoType>(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2911 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 2912 | return false; |
| 2913 | if (!CPT->isNothrow(Self.Context)) |
| 2914 | return false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2915 | } |
| 2916 | } |
| 2917 | } |
Douglas Gregor | d41679d | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 2918 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2919 | return FoundAssign; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2920 | } |
| 2921 | return false; |
| 2922 | case UTT_HasNothrowCopy: |
| 2923 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2924 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 2925 | // if type is a cv class or union type with copy constructors that are |
| 2926 | // known not to throw an exception then the trait is true, else it is |
| 2927 | // false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2928 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2929 | return true; |
| 2930 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2931 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2932 | if (RD->hasTrivialCopyConstructor()) |
| 2933 | return true; |
| 2934 | |
| 2935 | bool FoundConstructor = false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2936 | unsigned FoundTQs; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2937 | DeclContext::lookup_const_iterator Con, ConEnd; |
Sebastian Redl | 5f4e899 | 2010-09-13 21:10:20 +0000 | [diff] [blame] | 2938 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2939 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2940 | // A template constructor is never a copy constructor. |
| 2941 | // FIXME: However, it may actually be selected at the actual overload |
| 2942 | // resolution point. |
| 2943 | if (isa<FunctionTemplateDecl>(*Con)) |
| 2944 | continue; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2945 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 2946 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 2947 | FoundConstructor = true; |
| 2948 | const FunctionProtoType *CPT |
| 2949 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2950 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 2951 | return false; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2952 | // FIXME: check whether evaluating default arguments can throw. |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2953 | // For now, we'll be conservative and assume that they can throw. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2954 | if (!CPT->isNothrow(Self.Context) || CPT->getNumArgs() > 1) |
| 2955 | return false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2959 | return FoundConstructor; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2960 | } |
| 2961 | return false; |
| 2962 | case UTT_HasNothrowConstructor: |
| 2963 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2964 | // If __has_trivial_constructor (type) is true then the trait is |
| 2965 | // true, else if type is a cv class or union type (or array |
| 2966 | // thereof) with a default constructor that is known not to |
| 2967 | // throw an exception then the trait is true, else it is false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2968 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2969 | return true; |
| 2970 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) { |
| 2971 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2972 | if (RD->hasTrivialDefaultConstructor()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2973 | return true; |
| 2974 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2975 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 2976 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
| 2977 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2978 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 2979 | if (isa<FunctionTemplateDecl>(*Con)) |
| 2980 | continue; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2981 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 2982 | if (Constructor->isDefaultConstructor()) { |
| 2983 | const FunctionProtoType *CPT |
| 2984 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2985 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 2986 | return false; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2987 | // TODO: check whether evaluating default arguments can throw. |
| 2988 | // For now, we'll be conservative and assume that they can throw. |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2989 | return CPT->isNothrow(Self.Context) && CPT->getNumArgs() == 0; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2990 | } |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2991 | } |
| 2992 | } |
| 2993 | return false; |
| 2994 | case UTT_HasVirtualDestructor: |
| 2995 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2996 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 2997 | // then the trait is true, else it is false. |
| 2998 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2999 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 3000 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3001 | return Destructor->isVirtual(); |
| 3002 | } |
| 3003 | return false; |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 3004 | |
| 3005 | // These type trait expressions are modeled on the specifications for the |
| 3006 | // Embarcadero C++0x type trait functions: |
| 3007 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
| 3008 | case UTT_IsCompleteType: |
| 3009 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): |
| 3010 | // Returns True if and only if T is a complete type at the point of the |
| 3011 | // function call. |
| 3012 | return !T->isIncompleteType(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3013 | } |
Chandler Carruth | 83f563c | 2011-05-01 07:44:17 +0000 | [diff] [blame] | 3014 | llvm_unreachable("Type trait not covered by switch"); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | ExprResult Sema::BuildUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 3018 | SourceLocation KWLoc, |
| 3019 | TypeSourceInfo *TSInfo, |
| 3020 | SourceLocation RParen) { |
| 3021 | QualType T = TSInfo->getType(); |
Chandler Carruth | eb65a10 | 2011-04-30 10:07:32 +0000 | [diff] [blame] | 3022 | if (!CheckUnaryTypeTraitTypeCompleteness(*this, UTT, KWLoc, T)) |
| 3023 | return ExprError(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3024 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3025 | bool Value = false; |
| 3026 | if (!T->isDependentType()) |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 3027 | Value = EvaluateUnaryTypeTrait(*this, UTT, KWLoc, T); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3028 | |
| 3029 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, UTT, TSInfo, Value, |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 3030 | RParen, Context.BoolTy)); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3031 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3032 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3033 | ExprResult Sema::ActOnBinaryTypeTrait(BinaryTypeTrait BTT, |
| 3034 | SourceLocation KWLoc, |
| 3035 | ParsedType LhsTy, |
| 3036 | ParsedType RhsTy, |
| 3037 | SourceLocation RParen) { |
| 3038 | TypeSourceInfo *LhsTSInfo; |
| 3039 | QualType LhsT = GetTypeFromParser(LhsTy, &LhsTSInfo); |
| 3040 | if (!LhsTSInfo) |
| 3041 | LhsTSInfo = Context.getTrivialTypeSourceInfo(LhsT); |
| 3042 | |
| 3043 | TypeSourceInfo *RhsTSInfo; |
| 3044 | QualType RhsT = GetTypeFromParser(RhsTy, &RhsTSInfo); |
| 3045 | if (!RhsTSInfo) |
| 3046 | RhsTSInfo = Context.getTrivialTypeSourceInfo(RhsT); |
| 3047 | |
| 3048 | return BuildBinaryTypeTrait(BTT, KWLoc, LhsTSInfo, RhsTSInfo, RParen); |
| 3049 | } |
| 3050 | |
| 3051 | static bool EvaluateBinaryTypeTrait(Sema &Self, BinaryTypeTrait BTT, |
| 3052 | QualType LhsT, QualType RhsT, |
| 3053 | SourceLocation KeyLoc) { |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3054 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
| 3055 | "Cannot evaluate traits of dependent types"); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3056 | |
| 3057 | switch(BTT) { |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3058 | case BTT_IsBaseOf: { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3059 | // C++0x [meta.rel]p2 |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3060 | // 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] | 3061 | // Base and Derived are not unions and name the same class type without |
| 3062 | // regard to cv-qualifiers. |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3063 | |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3064 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
| 3065 | if (!lhsRecord) return false; |
| 3066 | |
| 3067 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
| 3068 | if (!rhsRecord) return false; |
| 3069 | |
| 3070 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
| 3071 | == (lhsRecord == rhsRecord)); |
| 3072 | |
| 3073 | if (lhsRecord == rhsRecord) |
| 3074 | return !lhsRecord->getDecl()->isUnion(); |
| 3075 | |
| 3076 | // C++0x [meta.rel]p2: |
| 3077 | // If Base and Derived are class types and are different types |
| 3078 | // (ignoring possible cv-qualifiers) then Derived shall be a |
| 3079 | // complete type. |
| 3080 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
| 3081 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 3082 | return false; |
| 3083 | |
| 3084 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) |
| 3085 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); |
| 3086 | } |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3087 | case BTT_IsSame: |
| 3088 | return Self.Context.hasSameType(LhsT, RhsT); |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3089 | case BTT_TypeCompatible: |
| 3090 | return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(), |
| 3091 | RhsT.getUnqualifiedType()); |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3092 | case BTT_IsConvertible: |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3093 | case BTT_IsConvertibleTo: { |
| 3094 | // C++0x [meta.rel]p4: |
| 3095 | // Given the following function prototype: |
| 3096 | // |
| 3097 | // template <class T> |
| 3098 | // typename add_rvalue_reference<T>::type create(); |
| 3099 | // |
| 3100 | // the predicate condition for a template specialization |
| 3101 | // is_convertible<From, To> shall be satisfied if and only if |
| 3102 | // the return expression in the following code would be |
| 3103 | // well-formed, including any implicit conversions to the return |
| 3104 | // type of the function: |
| 3105 | // |
| 3106 | // To test() { |
| 3107 | // return create<From>(); |
| 3108 | // } |
| 3109 | // |
| 3110 | // Access checking is performed as if in a context unrelated to To and |
| 3111 | // From. Only the validity of the immediate context of the expression |
| 3112 | // of the return-statement (including conversions to the return type) |
| 3113 | // is considered. |
| 3114 | // |
| 3115 | // We model the initialization as a copy-initialization of a temporary |
| 3116 | // of the appropriate type, which for this expression is identical to the |
| 3117 | // return statement (since NRVO doesn't apply). |
| 3118 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 3119 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
| 3120 | |
| 3121 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3122 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3123 | Expr::getValueKindForType(LhsT)); |
| 3124 | Expr *FromPtr = &From; |
| 3125 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, |
| 3126 | SourceLocation())); |
| 3127 | |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3128 | // Perform the initialization within a SFINAE trap at translation unit |
| 3129 | // scope. |
| 3130 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 3131 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3132 | InitializationSequence Init(Self, To, Kind, &FromPtr, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3133 | if (Init.Failed()) |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3134 | return false; |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3135 | |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3136 | ExprResult Result = Init.Perform(Self, To, Kind, MultiExprArg(&FromPtr, 1)); |
| 3137 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); |
| 3138 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3139 | } |
| 3140 | llvm_unreachable("Unknown type trait or not implemented"); |
| 3141 | } |
| 3142 | |
| 3143 | ExprResult Sema::BuildBinaryTypeTrait(BinaryTypeTrait BTT, |
| 3144 | SourceLocation KWLoc, |
| 3145 | TypeSourceInfo *LhsTSInfo, |
| 3146 | TypeSourceInfo *RhsTSInfo, |
| 3147 | SourceLocation RParen) { |
| 3148 | QualType LhsT = LhsTSInfo->getType(); |
| 3149 | QualType RhsT = RhsTSInfo->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3150 | |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3151 | if (BTT == BTT_TypeCompatible) { |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3152 | if (getLangOptions().CPlusPlus) { |
| 3153 | Diag(KWLoc, diag::err_types_compatible_p_in_cplusplus) |
| 3154 | << SourceRange(KWLoc, RParen); |
| 3155 | return ExprError(); |
| 3156 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | bool Value = false; |
| 3160 | if (!LhsT->isDependentType() && !RhsT->isDependentType()) |
| 3161 | Value = EvaluateBinaryTypeTrait(*this, BTT, LhsT, RhsT, KWLoc); |
| 3162 | |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3163 | // Select trait result type. |
| 3164 | QualType ResultType; |
| 3165 | switch (BTT) { |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3166 | case BTT_IsBaseOf: ResultType = Context.BoolTy; break; |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3167 | case BTT_IsConvertible: ResultType = Context.BoolTy; break; |
| 3168 | case BTT_IsSame: ResultType = Context.BoolTy; break; |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3169 | case BTT_TypeCompatible: ResultType = Context.IntTy; break; |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3170 | case BTT_IsConvertibleTo: ResultType = Context.BoolTy; break; |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3173 | return Owned(new (Context) BinaryTypeTraitExpr(KWLoc, BTT, LhsTSInfo, |
| 3174 | RhsTSInfo, Value, RParen, |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3175 | ResultType)); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3178 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
| 3179 | SourceLocation KWLoc, |
| 3180 | ParsedType Ty, |
| 3181 | Expr* DimExpr, |
| 3182 | SourceLocation RParen) { |
| 3183 | TypeSourceInfo *TSInfo; |
| 3184 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
| 3185 | if (!TSInfo) |
| 3186 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
| 3187 | |
| 3188 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
| 3189 | } |
| 3190 | |
| 3191 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
| 3192 | QualType T, Expr *DimExpr, |
| 3193 | SourceLocation KeyLoc) { |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3194 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3195 | |
| 3196 | switch(ATT) { |
| 3197 | case ATT_ArrayRank: |
| 3198 | if (T->isArrayType()) { |
| 3199 | unsigned Dim = 0; |
| 3200 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 3201 | ++Dim; |
| 3202 | T = AT->getElementType(); |
| 3203 | } |
| 3204 | return Dim; |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3205 | } |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3206 | return 0; |
| 3207 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3208 | case ATT_ArrayExtent: { |
| 3209 | llvm::APSInt Value; |
| 3210 | uint64_t Dim; |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3211 | if (DimExpr->isIntegerConstantExpr(Value, Self.Context, 0, false)) { |
| 3212 | if (Value < llvm::APSInt(Value.getBitWidth(), Value.isUnsigned())) { |
| 3213 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) << |
| 3214 | DimExpr->getSourceRange(); |
| 3215 | return false; |
| 3216 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3217 | Dim = Value.getLimitedValue(); |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3218 | } else { |
| 3219 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) << |
| 3220 | DimExpr->getSourceRange(); |
| 3221 | return false; |
| 3222 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3223 | |
| 3224 | if (T->isArrayType()) { |
| 3225 | unsigned D = 0; |
| 3226 | bool Matched = false; |
| 3227 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 3228 | if (Dim == D) { |
| 3229 | Matched = true; |
| 3230 | break; |
| 3231 | } |
| 3232 | ++D; |
| 3233 | T = AT->getElementType(); |
| 3234 | } |
| 3235 | |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3236 | if (Matched && T->isArrayType()) { |
| 3237 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
| 3238 | return CAT->getSize().getLimitedValue(); |
| 3239 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3240 | } |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3241 | return 0; |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3242 | } |
| 3243 | } |
| 3244 | llvm_unreachable("Unknown type trait or not implemented"); |
| 3245 | } |
| 3246 | |
| 3247 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
| 3248 | SourceLocation KWLoc, |
| 3249 | TypeSourceInfo *TSInfo, |
| 3250 | Expr* DimExpr, |
| 3251 | SourceLocation RParen) { |
| 3252 | QualType T = TSInfo->getType(); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3253 | |
Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3254 | // FIXME: This should likely be tracked as an APInt to remove any host |
| 3255 | // assumptions about the width of size_t on the target. |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3256 | uint64_t Value = 0; |
| 3257 | if (!T->isDependentType()) |
| 3258 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); |
| 3259 | |
Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3260 | // While the specification for these traits from the Embarcadero C++ |
| 3261 | // compiler's documentation says the return type is 'unsigned int', Clang |
| 3262 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero |
| 3263 | // compiler, there is no difference. On several other platforms this is an |
| 3264 | // important distinction. |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3265 | return Owned(new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, |
Chandler Carruth | 06207f6 | 2011-05-01 07:49:26 +0000 | [diff] [blame] | 3266 | DimExpr, RParen, |
Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3267 | Context.getSizeType())); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3270 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3271 | SourceLocation KWLoc, |
| 3272 | Expr *Queried, |
| 3273 | SourceLocation RParen) { |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3274 | // If error parsing the expression, ignore. |
| 3275 | if (!Queried) |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3276 | return ExprError(); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3277 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3278 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3279 | |
| 3280 | return move(Result); |
| 3281 | } |
| 3282 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3283 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
| 3284 | switch (ET) { |
| 3285 | case ET_IsLValueExpr: return E->isLValue(); |
| 3286 | case ET_IsRValueExpr: return E->isRValue(); |
| 3287 | } |
| 3288 | llvm_unreachable("Expression trait not covered by switch"); |
| 3289 | } |
| 3290 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3291 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3292 | SourceLocation KWLoc, |
| 3293 | Expr *Queried, |
| 3294 | SourceLocation RParen) { |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3295 | if (Queried->isTypeDependent()) { |
| 3296 | // Delay type-checking for type-dependent expressions. |
| 3297 | } else if (Queried->getType()->isPlaceholderType()) { |
| 3298 | ExprResult PE = CheckPlaceholderExpr(Queried); |
| 3299 | if (PE.isInvalid()) return ExprError(); |
| 3300 | return BuildExpressionTrait(ET, KWLoc, PE.take(), RParen); |
| 3301 | } |
| 3302 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3303 | bool Value = EvaluateExpressionTrait(ET, Queried); |
Chandler Carruth | f7ef000 | 2011-05-01 08:48:19 +0000 | [diff] [blame] | 3304 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3305 | return Owned(new (Context) ExpressionTraitExpr(KWLoc, ET, Queried, Value, |
| 3306 | RParen, Context.BoolTy)); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3309 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3310 | ExprValueKind &VK, |
| 3311 | SourceLocation Loc, |
| 3312 | bool isIndirect) { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3313 | assert(!LHS.get()->getType()->isPlaceholderType() && |
| 3314 | !RHS.get()->getType()->isPlaceholderType() && |
John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3315 | "placeholders should have been weeded out by now"); |
| 3316 | |
| 3317 | // The LHS undergoes lvalue conversions if this is ->*. |
| 3318 | if (isIndirect) { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3319 | LHS = DefaultLvalueConversion(LHS.take()); |
| 3320 | if (LHS.isInvalid()) return QualType(); |
John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | // The RHS always undergoes lvalue conversions. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3324 | RHS = DefaultLvalueConversion(RHS.take()); |
| 3325 | if (RHS.isInvalid()) return QualType(); |
John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3326 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3327 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 3328 | // C++ 5.5p2 |
| 3329 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 3330 | // be of type "pointer to member of T" (where T is a completely-defined |
| 3331 | // class type) [...] |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3332 | QualType RHSType = RHS.get()->getType(); |
| 3333 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3334 | if (!MemPtr) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3335 | Diag(Loc, diag::err_bad_memptr_rhs) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3336 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3337 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3338 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3339 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3340 | QualType Class(MemPtr->getClass(), 0); |
| 3341 | |
Douglas Gregor | 7d520ba | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 3342 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
| 3343 | // member pointer points must be completely-defined. However, there is no |
| 3344 | // reason for this semantic distinction, and the rule is not enforced by |
| 3345 | // other compilers. Therefore, we do not check this property, as it is |
| 3346 | // likely to be considered a defect. |
Sebastian Redl | 59fc269 | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 3347 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3348 | // C++ 5.5p2 |
| 3349 | // [...] to its first operand, which shall be of class T or of a class of |
| 3350 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 3351 | // such a class] |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3352 | QualType LHSType = LHS.get()->getType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3353 | if (isIndirect) { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3354 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) |
| 3355 | LHSType = Ptr->getPointeeType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3356 | else { |
| 3357 | Diag(Loc, diag::err_bad_memptr_lhs) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3358 | << OpSpelling << 1 << LHSType |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3359 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3360 | return QualType(); |
| 3361 | } |
| 3362 | } |
| 3363 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3364 | if (!Context.hasSameUnqualifiedType(Class, LHSType)) { |
Sebastian Redl | 17e1d35 | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 3365 | // If we want to check the hierarchy, we need a complete type. |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3366 | if (RequireCompleteType(Loc, LHSType, PDiag(diag::err_bad_memptr_lhs) |
Sebastian Redl | 17e1d35 | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 3367 | << OpSpelling << (int)isIndirect)) { |
| 3368 | return QualType(); |
| 3369 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3370 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 3371 | /*DetectVirtual=*/false); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3372 | // FIXME: Would it be useful to print full ambiguity paths, or is that |
| 3373 | // overkill? |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3374 | if (!IsDerivedFrom(LHSType, Class, Paths) || |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3375 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
| 3376 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3377 | << (int)isIndirect << LHS.get()->getType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3378 | return QualType(); |
| 3379 | } |
Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 3380 | // Cast LHS to type of use. |
| 3381 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 3382 | ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind(); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3383 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3384 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3385 | BuildBasePathArray(Paths, BasePath); |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3386 | LHS = ImpCastExprToType(LHS.take(), UseType, CK_DerivedToBase, VK, |
| 3387 | &BasePath); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3390 | if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) { |
Fariborz Jahanian | 05ebda9 | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 3391 | // Diagnose use of pointer-to-member type which when used as |
| 3392 | // the functional cast in a pointer-to-member expression. |
| 3393 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 3394 | return QualType(); |
| 3395 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3396 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3397 | // C++ 5.5p2 |
| 3398 | // The result is an object or a function of the type specified by the |
| 3399 | // second operand. |
| 3400 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 3401 | // in accordance with 5.5p5 and 5.2.5. |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3402 | QualType Result = MemPtr->getPointeeType(); |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3403 | Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3404 | |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3405 | // C++0x [expr.mptr.oper]p6: |
| 3406 | // In a .* expression whose object expression is an rvalue, the program is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3407 | // ill-formed if the second operand is a pointer to member function with |
| 3408 | // ref-qualifier &. In a ->* expression or in a .* expression whose object |
| 3409 | // 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] | 3410 | // is a pointer to member function with ref-qualifier &&. |
| 3411 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
| 3412 | switch (Proto->getRefQualifier()) { |
| 3413 | case RQ_None: |
| 3414 | // Do nothing |
| 3415 | break; |
| 3416 | |
| 3417 | case RQ_LValue: |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3418 | if (!isIndirect && !LHS.get()->Classify(Context).isLValue()) |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3419 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3420 | << RHSType << 1 << LHS.get()->getSourceRange(); |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3421 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3422 | |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3423 | case RQ_RValue: |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3424 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3425 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3426 | << RHSType << 0 << LHS.get()->getSourceRange(); |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3427 | break; |
| 3428 | } |
| 3429 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3430 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3431 | // C++ [expr.mptr.oper]p6: |
| 3432 | // The result of a .* expression whose second operand is a pointer |
| 3433 | // to a data member is of the same value category as its |
| 3434 | // first operand. The result of a .* expression whose second |
| 3435 | // operand is a pointer to a member function is a prvalue. The |
| 3436 | // result of an ->* expression is an lvalue if its second operand |
| 3437 | // is a pointer to data member and a prvalue otherwise. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3438 | if (Result->isFunctionType()) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3439 | VK = VK_RValue; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3440 | return Context.BoundMemberTy; |
| 3441 | } else if (isIndirect) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3442 | VK = VK_LValue; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3443 | } else { |
Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3444 | VK = LHS.get()->getValueKind(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3445 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3446 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3447 | return Result; |
| 3448 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3449 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3450 | /// \brief Try to convert a type to another according to C++0x 5.16p3. |
| 3451 | /// |
| 3452 | /// This is part of the parameter validation for the ? operator. If either |
| 3453 | /// value operand is a class type, the two operands are attempted to be |
| 3454 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3455 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 3456 | /// as such. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3457 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 3458 | SourceLocation QuestionLoc, |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3459 | bool &HaveConversion, |
| 3460 | QualType &ToType) { |
| 3461 | HaveConversion = false; |
| 3462 | ToType = To->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3463 | |
| 3464 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3465 | SourceLocation()); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3466 | // C++0x 5.16p3 |
| 3467 | // The process for determining whether an operand expression E1 of type T1 |
| 3468 | // can be converted to match an operand expression E2 of type T2 is defined |
| 3469 | // as follows: |
| 3470 | // -- If E2 is an lvalue: |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 3471 | bool ToIsLvalue = To->isLValue(); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 3472 | if (ToIsLvalue) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3473 | // E1 can be converted to match E2 if E1 can be implicitly converted to |
| 3474 | // type "lvalue reference to T2", subject to the constraint that in the |
| 3475 | // conversion the reference must bind directly to E1. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3476 | QualType T = Self.Context.getLValueReferenceType(ToType); |
| 3477 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3478 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3479 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 3480 | if (InitSeq.isDirectReferenceBinding()) { |
| 3481 | ToType = T; |
| 3482 | HaveConversion = true; |
| 3483 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3484 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3485 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3486 | if (InitSeq.isAmbiguous()) |
| 3487 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3488 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3489 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3490 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 3491 | // -- if E1 and E2 have class type, and the underlying class types are |
| 3492 | // the same or one is a base class of the other: |
| 3493 | QualType FTy = From->getType(); |
| 3494 | QualType TTy = To->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3495 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 3496 | const RecordType *TRec = TTy->getAs<RecordType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3497 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3498 | Self.IsDerivedFrom(FTy, TTy); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3499 | if (FRec && TRec && |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3500 | (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3501 | // E1 can be converted to match E2 if the class of T2 is the |
| 3502 | // same type as, or a base class of, the class of T1, and |
| 3503 | // [cv2 > cv1]. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3504 | if (FRec == TRec || FDerivedFromT) { |
| 3505 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3506 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 3507 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3508 | if (InitSeq) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3509 | HaveConversion = true; |
| 3510 | return false; |
| 3511 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3512 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3513 | if (InitSeq.isAmbiguous()) |
| 3514 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3515 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3516 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3517 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3518 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3519 | } |
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 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 3522 | // implicitly converted to the type that expression E2 would have |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3523 | // 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] | 3524 | // an rvalue). |
| 3525 | // |
| 3526 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 3527 | // to the array-to-pointer or function-to-pointer conversions. |
| 3528 | if (!TTy->getAs<TagType>()) |
| 3529 | TTy = TTy.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3530 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3531 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 3532 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3533 | HaveConversion = !InitSeq.Failed(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3534 | ToType = TTy; |
| 3535 | if (InitSeq.isAmbiguous()) |
| 3536 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
| 3537 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3538 | return false; |
| 3539 | } |
| 3540 | |
| 3541 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 3542 | /// |
| 3543 | /// This is part of the parameter validation for the ? operator. If either |
| 3544 | /// value operand is a class type, overload resolution is used to find a |
| 3545 | /// conversion to a common type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3546 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3547 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3548 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3549 | OverloadCandidateSet CandidateSet(QuestionLoc); |
| 3550 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, 2, |
| 3551 | CandidateSet); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3552 | |
| 3553 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3554 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3555 | case OR_Success: { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3556 | // 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] | 3557 | ExprResult LHSRes = |
| 3558 | Self.PerformImplicitConversion(LHS.get(), Best->BuiltinTypes.ParamTypes[0], |
| 3559 | Best->Conversions[0], Sema::AA_Converting); |
| 3560 | if (LHSRes.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3561 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3562 | LHS = move(LHSRes); |
| 3563 | |
| 3564 | ExprResult RHSRes = |
| 3565 | Self.PerformImplicitConversion(RHS.get(), Best->BuiltinTypes.ParamTypes[1], |
| 3566 | Best->Conversions[1], Sema::AA_Converting); |
| 3567 | if (RHSRes.isInvalid()) |
| 3568 | break; |
| 3569 | RHS = move(RHSRes); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3570 | if (Best->Function) |
| 3571 | Self.MarkDeclarationReferenced(QuestionLoc, Best->Function); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3572 | return false; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3575 | case OR_No_Viable_Function: |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3576 | |
| 3577 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 3578 | // constant and the other is a pointer type. In this case, the user most |
| 3579 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3580 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3581 | return true; |
| 3582 | |
| 3583 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3584 | << LHS.get()->getType() << RHS.get()->getType() |
| 3585 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3586 | return true; |
| 3587 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3588 | case OR_Ambiguous: |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3589 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3590 | << LHS.get()->getType() << RHS.get()->getType() |
| 3591 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3592 | // FIXME: Print the possible common types by printing the return types of |
| 3593 | // the viable candidates. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3594 | break; |
| 3595 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3596 | case OR_Deleted: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3597 | llvm_unreachable("Conditional operator has only built-in overloads"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3598 | } |
| 3599 | return true; |
| 3600 | } |
| 3601 | |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3602 | /// \brief Perform an "extended" implicit conversion as returned by |
| 3603 | /// TryClassUnification. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3604 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3605 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3606 | InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(), |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3607 | SourceLocation()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3608 | Expr *Arg = E.take(); |
| 3609 | InitializationSequence InitSeq(Self, Entity, Kind, &Arg, 1); |
| 3610 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, MultiExprArg(&Arg, 1)); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3611 | if (Result.isInvalid()) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3612 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3613 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3614 | E = Result; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3615 | return false; |
| 3616 | } |
| 3617 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3618 | /// \brief Check the operands of ?: under C++ semantics. |
| 3619 | /// |
| 3620 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 3621 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3622 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3623 | ExprValueKind &VK, ExprObjectKind &OK, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3624 | SourceLocation QuestionLoc) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3625 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 3626 | // interface pointers. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3627 | |
| 3628 | // C++0x 5.16p1 |
| 3629 | // The first expression is contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3630 | if (!Cond.get()->isTypeDependent()) { |
| 3631 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.take()); |
| 3632 | if (CondRes.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3633 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3634 | Cond = move(CondRes); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3637 | // Assume r-value. |
| 3638 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3639 | OK = OK_Ordinary; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3640 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3641 | // Either of the arguments dependent? |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3642 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3643 | return Context.DependentTy; |
| 3644 | |
| 3645 | // C++0x 5.16p2 |
| 3646 | // If either the second or the third operand has type (cv) void, ... |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3647 | QualType LTy = LHS.get()->getType(); |
| 3648 | QualType RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3649 | bool LVoid = LTy->isVoidType(); |
| 3650 | bool RVoid = RTy->isVoidType(); |
| 3651 | if (LVoid || RVoid) { |
| 3652 | // ... then the [l2r] conversions are performed on the second and third |
| 3653 | // operands ... |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3654 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 3655 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 3656 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 3657 | return QualType(); |
| 3658 | LTy = LHS.get()->getType(); |
| 3659 | RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3660 | |
| 3661 | // ... and one of the following shall hold: |
| 3662 | // -- The second or the third operand (but not both) is a throw- |
| 3663 | // 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] | 3664 | bool LThrow = isa<CXXThrowExpr>(LHS.get()); |
| 3665 | bool RThrow = isa<CXXThrowExpr>(RHS.get()); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3666 | if (LThrow && !RThrow) |
| 3667 | return RTy; |
| 3668 | if (RThrow && !LThrow) |
| 3669 | return LTy; |
| 3670 | |
| 3671 | // -- Both the second and third operands have type void; the result is of |
| 3672 | // type void and is an rvalue. |
| 3673 | if (LVoid && RVoid) |
| 3674 | return Context.VoidTy; |
| 3675 | |
| 3676 | // Neither holds, error. |
| 3677 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 3678 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3679 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3680 | return QualType(); |
| 3681 | } |
| 3682 | |
| 3683 | // Neither is void. |
| 3684 | |
| 3685 | // C++0x 5.16p3 |
| 3686 | // Otherwise, if the second and third operand have different types, and |
| 3687 | // either has (cv) class type, and attempt is made to convert each of those |
| 3688 | // operands to the other. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3689 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3690 | (LTy->isRecordType() || RTy->isRecordType())) { |
| 3691 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; |
| 3692 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3693 | QualType L2RType, R2LType; |
| 3694 | bool HaveL2R, HaveR2L; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3695 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3696 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3697 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3698 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3699 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3700 | // If both can be converted, [...] the program is ill-formed. |
| 3701 | if (HaveL2R && HaveR2L) { |
| 3702 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3703 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3704 | return QualType(); |
| 3705 | } |
| 3706 | |
| 3707 | // If exactly one conversion is possible, that conversion is applied to |
| 3708 | // the chosen operand and the converted operands are used in place of the |
| 3709 | // original operands for the remainder of this section. |
| 3710 | if (HaveL2R) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3711 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3712 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3713 | LTy = LHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3714 | } else if (HaveR2L) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3715 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3716 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3717 | RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3718 | } |
| 3719 | } |
| 3720 | |
| 3721 | // C++0x 5.16p4 |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3722 | // If the second and third operands are glvalues of the same value |
| 3723 | // category and have the same type, the result is of that type and |
| 3724 | // value category and it is a bit-field if the second or the third |
| 3725 | // operand is a bit-field, or if both are bit-fields. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3726 | // We only extend this to bitfields, not to the crazy other kinds of |
| 3727 | // l-values. |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 3728 | bool Same = Context.hasSameType(LTy, RTy); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3729 | if (Same && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3730 | LHS.get()->isGLValue() && |
| 3731 | LHS.get()->getValueKind() == RHS.get()->getValueKind() && |
| 3732 | LHS.get()->isOrdinaryOrBitFieldObject() && |
| 3733 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
| 3734 | VK = LHS.get()->getValueKind(); |
| 3735 | if (LHS.get()->getObjectKind() == OK_BitField || |
| 3736 | RHS.get()->getObjectKind() == OK_BitField) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3737 | OK = OK_BitField; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3738 | return LTy; |
Fariborz Jahanian | 3911a1a | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 3739 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3740 | |
| 3741 | // C++0x 5.16p5 |
| 3742 | // Otherwise, the result is an rvalue. If the second and third operands |
| 3743 | // do not have the same type, and either has (cv) class type, ... |
| 3744 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 3745 | // ... overload resolution is used to determine the conversions (if any) |
| 3746 | // to be applied to the operands. If the overload resolution fails, the |
| 3747 | // program is ill-formed. |
| 3748 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 3749 | return QualType(); |
| 3750 | } |
| 3751 | |
| 3752 | // C++0x 5.16p6 |
| 3753 | // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 3754 | // conversions are performed on the second and third operands. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3755 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 3756 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 3757 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 3758 | return QualType(); |
| 3759 | LTy = LHS.get()->getType(); |
| 3760 | RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3761 | |
| 3762 | // After those conversions, one of the following shall hold: |
| 3763 | // -- The second and third operands have the same type; the result |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3764 | // is of that type. If the operands have class type, the result |
| 3765 | // is a prvalue temporary of the result type, which is |
| 3766 | // copy-initialized from either the second operand or the third |
| 3767 | // operand depending on the value of the first operand. |
| 3768 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 3769 | if (LTy->isRecordType()) { |
| 3770 | // The operands have class type. Make a temporary copy. |
| 3771 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3772 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
| 3773 | SourceLocation(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3774 | LHS); |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3775 | if (LHSCopy.isInvalid()) |
| 3776 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3777 | |
| 3778 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
| 3779 | SourceLocation(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3780 | RHS); |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3781 | if (RHSCopy.isInvalid()) |
| 3782 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3783 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3784 | LHS = LHSCopy; |
| 3785 | RHS = RHSCopy; |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3788 | return LTy; |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3789 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3790 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3791 | // Extension: conditional operator involving vector types. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3792 | if (LTy->isVectorType() || RTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 3793 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3794 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3795 | // -- The second and third operands have arithmetic or enumeration type; |
| 3796 | // the usual arithmetic conversions are performed to bring them to a |
| 3797 | // common type, and the result is of that type. |
| 3798 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
| 3799 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3800 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 3801 | return QualType(); |
| 3802 | return LHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | // -- The second and third operands have pointer type, or one has pointer |
| 3806 | // type and the other is a null pointer constant; pointer conversions |
| 3807 | // and qualification conversions are performed to bring them to their |
| 3808 | // composite pointer type. The result is of the composite pointer type. |
Eli Friedman | de8ac49 | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 3809 | // -- The second and third operands have pointer to member type, or one has |
| 3810 | // pointer to member type and the other is a null pointer constant; |
| 3811 | // pointer to member conversions and qualification conversions are |
| 3812 | // performed to bring them to a common type, whose cv-qualification |
| 3813 | // shall match the cv-qualification of either the second or the third |
| 3814 | // operand. The result is of the common type. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3815 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3816 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3817 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
| 3818 | if (!Composite.isNull()) { |
| 3819 | if (NonStandardCompositeType) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3820 | Diag(QuestionLoc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3821 | diag::ext_typecheck_cond_incompatible_operands_nonstandard) |
| 3822 | << LTy << RTy << Composite |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3823 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3824 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3825 | return Composite; |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3826 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3827 | |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 3828 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 3829 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 3830 | if (!Composite.isNull()) |
| 3831 | return Composite; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3832 | |
Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 3833 | // Check if we are using a null with a non-pointer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3834 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 3835 | return QualType(); |
| 3836 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3837 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3838 | << LHS.get()->getType() << RHS.get()->getType() |
| 3839 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3840 | return QualType(); |
| 3841 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3842 | |
| 3843 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 3844 | /// |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3845 | /// This finds the composite pointer type (or member pointer type) for @p E1 |
| 3846 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this |
| 3847 | /// type and returns it. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3848 | /// It does not emit diagnostics. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3849 | /// |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3850 | /// \param Loc The location of the operator requiring these two expressions to |
| 3851 | /// be converted to the composite pointer type. |
| 3852 | /// |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3853 | /// If \p NonStandardCompositeType is non-NULL, then we are permitted to find |
| 3854 | /// a non-standard (but still sane) composite type to which both expressions |
| 3855 | /// can be converted. When such a type is chosen, \c *NonStandardCompositeType |
| 3856 | /// will be set true. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3857 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3858 | Expr *&E1, Expr *&E2, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3859 | bool *NonStandardCompositeType) { |
| 3860 | if (NonStandardCompositeType) |
| 3861 | *NonStandardCompositeType = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3862 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3863 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); |
| 3864 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3865 | |
Fariborz Jahanian | 0cedfbd | 2009-12-08 20:04:24 +0000 | [diff] [blame] | 3866 | if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && |
| 3867 | !T2->isAnyPointerType() && !T2->isMemberPointerType()) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3868 | return QualType(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3869 | |
| 3870 | // C++0x 5.9p2 |
| 3871 | // Pointer conversions and qualification conversions are performed on |
| 3872 | // pointer operands to bring them to their composite pointer type. If |
| 3873 | // one operand is a null pointer constant, the composite pointer type is |
| 3874 | // the type of the other operand. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3875 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3876 | if (T2->isMemberPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3877 | E1 = ImpCastExprToType(E1, T2, CK_NullToMemberPointer).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3878 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3879 | E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).take(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3880 | return T2; |
| 3881 | } |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3882 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3883 | if (T1->isMemberPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3884 | E2 = ImpCastExprToType(E2, T1, CK_NullToMemberPointer).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3885 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3886 | E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).take(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3887 | return T1; |
| 3888 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3889 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3890 | // Now both have to be pointers or member pointers. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3891 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || |
| 3892 | (!T2->isPointerType() && !T2->isMemberPointerType())) |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3893 | return QualType(); |
| 3894 | |
| 3895 | // Otherwise, of one of the operands has type "pointer to cv1 void," then |
| 3896 | // the other has type "pointer to cv2 T" and the composite pointer type is |
| 3897 | // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. |
| 3898 | // Otherwise, the composite pointer type is a pointer type similar to the |
| 3899 | // type of one of the operands, with a cv-qualification signature that is |
| 3900 | // the union of the cv-qualification signatures of the operand types. |
| 3901 | // In practice, the first part here is redundant; it's subsumed by the second. |
| 3902 | // What we do here is, we build the two possible composite types, and try the |
| 3903 | // conversions in both directions. If only one works, or if the two composite |
| 3904 | // types are the same, we have succeeded. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3905 | // FIXME: extended qualifiers? |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3906 | typedef SmallVector<unsigned, 4> QualifierVector; |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3907 | QualifierVector QualifierUnion; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3908 | typedef SmallVector<std::pair<const Type *, const Type *>, 4> |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3909 | ContainingClassVector; |
| 3910 | ContainingClassVector MemberOfClass; |
| 3911 | QualType Composite1 = Context.getCanonicalType(T1), |
| 3912 | Composite2 = Context.getCanonicalType(T2); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3913 | unsigned NeedConstBefore = 0; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3914 | do { |
| 3915 | const PointerType *Ptr1, *Ptr2; |
| 3916 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 3917 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 3918 | Composite1 = Ptr1->getPointeeType(); |
| 3919 | Composite2 = Ptr2->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3920 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3921 | // 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] | 3922 | // of where we need to fill in additional 'const' qualifiers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3923 | if (NonStandardCompositeType && |
| 3924 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 3925 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3926 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3927 | QualifierUnion.push_back( |
| 3928 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 3929 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); |
| 3930 | continue; |
| 3931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3933 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 3934 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 3935 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 3936 | Composite1 = MemPtr1->getPointeeType(); |
| 3937 | Composite2 = MemPtr2->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3938 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3939 | // 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] | 3940 | // of where we need to fill in additional 'const' qualifiers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3941 | if (NonStandardCompositeType && |
| 3942 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 3943 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3945 | QualifierUnion.push_back( |
| 3946 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 3947 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 3948 | MemPtr2->getClass())); |
| 3949 | continue; |
| 3950 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3951 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3952 | // FIXME: block pointer types? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3954 | // Cannot unwrap any more types. |
| 3955 | break; |
| 3956 | } while (true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3958 | if (NeedConstBefore && NonStandardCompositeType) { |
| 3959 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3960 | // mismatch, so that our (non-standard!) composite type meets the |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3961 | // requirements of C++ [conv.qual]p4 bullet 3. |
| 3962 | for (unsigned I = 0; I != NeedConstBefore; ++I) { |
| 3963 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) { |
| 3964 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
| 3965 | *NonStandardCompositeType = true; |
| 3966 | } |
| 3967 | } |
| 3968 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3969 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3970 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3971 | ContainingClassVector::reverse_iterator MOC |
| 3972 | = MemberOfClass.rbegin(); |
| 3973 | for (QualifierVector::reverse_iterator |
| 3974 | I = QualifierUnion.rbegin(), |
| 3975 | E = QualifierUnion.rend(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3976 | I != E; (void)++I, ++MOC) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3977 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3978 | if (MOC->first && MOC->second) { |
| 3979 | // Rebuild member pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3980 | Composite1 = Context.getMemberPointerType( |
| 3981 | Context.getQualifiedType(Composite1, Quals), |
| 3982 | MOC->first); |
| 3983 | Composite2 = Context.getMemberPointerType( |
| 3984 | Context.getQualifiedType(Composite2, Quals), |
| 3985 | MOC->second); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3986 | } else { |
| 3987 | // Rebuild pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3988 | Composite1 |
| 3989 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 3990 | Composite2 |
| 3991 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3992 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3995 | // Try to convert to the first composite pointer type. |
| 3996 | InitializedEntity Entity1 |
| 3997 | = InitializedEntity::InitializeTemporary(Composite1); |
| 3998 | InitializationKind Kind |
| 3999 | = InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 4000 | InitializationSequence E1ToC1(*this, Entity1, Kind, &E1, 1); |
| 4001 | InitializationSequence E2ToC1(*this, Entity1, Kind, &E2, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4002 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4003 | if (E1ToC1 && E2ToC1) { |
| 4004 | // Conversion to Composite1 is viable. |
| 4005 | if (!Context.hasSameType(Composite1, Composite2)) { |
| 4006 | // Composite2 is a different type from Composite1. Check whether |
| 4007 | // Composite2 is also viable. |
| 4008 | InitializedEntity Entity2 |
| 4009 | = InitializedEntity::InitializeTemporary(Composite2); |
| 4010 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 4011 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 4012 | if (E1ToC2 && E2ToC2) { |
| 4013 | // Both Composite1 and Composite2 are viable and are different; |
| 4014 | // this is an ambiguity. |
| 4015 | return QualType(); |
| 4016 | } |
| 4017 | } |
| 4018 | |
| 4019 | // Convert E1 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4020 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4021 | = E1ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E1,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4022 | if (E1Result.isInvalid()) |
| 4023 | return QualType(); |
| 4024 | E1 = E1Result.takeAs<Expr>(); |
| 4025 | |
| 4026 | // Convert E2 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4027 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4028 | = E2ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E2,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4029 | if (E2Result.isInvalid()) |
| 4030 | return QualType(); |
| 4031 | E2 = E2Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4032 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4033 | return Composite1; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4036 | // Check whether Composite2 is viable. |
| 4037 | InitializedEntity Entity2 |
| 4038 | = InitializedEntity::InitializeTemporary(Composite2); |
| 4039 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 4040 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 4041 | if (!E1ToC2 || !E2ToC2) |
| 4042 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4043 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4044 | // Convert E1 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4045 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4046 | = E1ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E1, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4047 | if (E1Result.isInvalid()) |
| 4048 | return QualType(); |
| 4049 | E1 = E1Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4050 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4051 | // Convert E2 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4052 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4053 | = E2ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E2, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4054 | if (E2Result.isInvalid()) |
| 4055 | return QualType(); |
| 4056 | E2 = E2Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4057 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4058 | return Composite2; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4059 | } |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 4060 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4061 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Douglas Gregor | 19cc1c7 | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 4062 | if (!E) |
| 4063 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4065 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 4066 | |
| 4067 | // If the result is a glvalue, we shouldn't bind it. |
| 4068 | if (!E->isRValue()) |
Anders Carlsson | 089c260 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 4069 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4071 | // In ARC, calls that return a retainable type can return retained, |
| 4072 | // in which case we have to insert a consuming cast. |
| 4073 | if (getLangOptions().ObjCAutoRefCount && |
| 4074 | E->getType()->isObjCRetainableType()) { |
| 4075 | |
| 4076 | bool ReturnsRetained; |
| 4077 | |
| 4078 | // For actual calls, we compute this by examining the type of the |
| 4079 | // called value. |
| 4080 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { |
| 4081 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
| 4082 | QualType T = Callee->getType(); |
| 4083 | |
| 4084 | if (T == Context.BoundMemberTy) { |
| 4085 | // Handle pointer-to-members. |
| 4086 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) |
| 4087 | T = BinOp->getRHS()->getType(); |
| 4088 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) |
| 4089 | T = Mem->getMemberDecl()->getType(); |
| 4090 | } |
| 4091 | |
| 4092 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
| 4093 | T = Ptr->getPointeeType(); |
| 4094 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
| 4095 | T = Ptr->getPointeeType(); |
| 4096 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
| 4097 | T = MemPtr->getPointeeType(); |
| 4098 | |
| 4099 | const FunctionType *FTy = T->getAs<FunctionType>(); |
| 4100 | assert(FTy && "call to value not of function type?"); |
| 4101 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
| 4102 | |
| 4103 | // ActOnStmtExpr arranges things so that StmtExprs of retainable |
| 4104 | // type always produce a +1 object. |
| 4105 | } else if (isa<StmtExpr>(E)) { |
| 4106 | ReturnsRetained = true; |
| 4107 | |
| 4108 | // For message sends and property references, we try to find an |
| 4109 | // actual method. FIXME: we should infer retention by selector in |
| 4110 | // cases where we don't have an actual method. |
| 4111 | } else { |
John McCall | fc4b191 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 4112 | ObjCMethodDecl *D = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4113 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { |
| 4114 | D = Send->getMethodDecl(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
John McCall | fc4b191 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 4118 | |
| 4119 | // Don't do reclaims on performSelector calls; despite their |
| 4120 | // return type, the invoked method doesn't necessarily actually |
| 4121 | // return an object. |
| 4122 | if (!ReturnsRetained && |
| 4123 | D && D->getMethodFamily() == OMF_performSelector) |
| 4124 | return Owned(E); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
John McCall | 567c586 | 2011-11-14 19:53:16 +0000 | [diff] [blame] | 4127 | // Don't reclaim an object of Class type. |
| 4128 | if (!ReturnsRetained && E->getType()->isObjCARCImplicitlyUnretainedType()) |
| 4129 | return Owned(E); |
| 4130 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4131 | ExprNeedsCleanups = true; |
| 4132 | |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4133 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject |
| 4134 | : CK_ARCReclaimReturnedObject); |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4135 | return Owned(ImplicitCastExpr::Create(Context, E->getType(), ck, E, 0, |
| 4136 | VK_RValue)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | if (!getLangOptions().CPlusPlus) |
| 4140 | return Owned(E); |
Douglas Gregor | 5132655 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 4141 | |
Peter Collingbourne | bceb755 | 2011-11-27 22:09:28 +0000 | [diff] [blame] | 4142 | QualType ET = Context.getBaseElementType(E->getType()); |
| 4143 | const RecordType *RT = ET->getAs<RecordType>(); |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 4144 | if (!RT) |
| 4145 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4146 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4147 | // That should be enough to guarantee that this type is complete. |
| 4148 | // If it has a trivial destructor, we can avoid the extra copy. |
Jeffrey Yasskin | b7ee2e5 | 2011-01-27 19:17:54 +0000 | [diff] [blame] | 4149 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 507384f | 2010-08-12 02:40:37 +0000 | [diff] [blame] | 4150 | if (RD->isInvalidDecl() || RD->hasTrivialDestructor()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4151 | return Owned(E); |
| 4152 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4153 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
| 4154 | |
| 4155 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); |
| 4156 | if (Destructor) { |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 4157 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 4158 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 4159 | PDiag(diag::err_access_dtor_temp) |
| 4160 | << E->getType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4161 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4162 | // 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] | 4163 | ExprNeedsCleanups = true; |
John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 4164 | } |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 4165 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); |
| 4166 | } |
| 4167 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4168 | ExprResult |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4169 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4170 | if (SubExpr.isInvalid()) |
| 4171 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4172 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4173 | return Owned(MaybeCreateExprWithCleanups(SubExpr.take())); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4174 | } |
| 4175 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 4176 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
| 4177 | assert(SubExpr && "sub expression can't be null!"); |
| 4178 | |
| 4179 | unsigned FirstCleanup = ExprEvalContexts.back().NumCleanupObjects; |
| 4180 | assert(ExprCleanupObjects.size() >= FirstCleanup); |
| 4181 | assert(ExprNeedsCleanups || ExprCleanupObjects.size() == FirstCleanup); |
| 4182 | if (!ExprNeedsCleanups) |
| 4183 | return SubExpr; |
| 4184 | |
| 4185 | ArrayRef<ExprWithCleanups::CleanupObject> Cleanups |
| 4186 | = llvm::makeArrayRef(ExprCleanupObjects.begin() + FirstCleanup, |
| 4187 | ExprCleanupObjects.size() - FirstCleanup); |
| 4188 | |
| 4189 | Expr *E = ExprWithCleanups::Create(Context, SubExpr, Cleanups); |
| 4190 | DiscardCleanupsInEvaluationContext(); |
| 4191 | |
| 4192 | return E; |
| 4193 | } |
| 4194 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4195 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4196 | assert(SubStmt && "sub statement can't be null!"); |
| 4197 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4198 | if (!ExprNeedsCleanups) |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4199 | return SubStmt; |
| 4200 | |
| 4201 | // FIXME: In order to attach the temporaries, wrap the statement into |
| 4202 | // a StmtExpr; currently this is only used for asm statements. |
| 4203 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
| 4204 | // a new AsmStmtWithTemporaries. |
| 4205 | CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, &SubStmt, 1, |
| 4206 | SourceLocation(), |
| 4207 | SourceLocation()); |
| 4208 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
| 4209 | SourceLocation()); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4210 | return MaybeCreateExprWithCleanups(E); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4213 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4214 | Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4215 | tok::TokenKind OpKind, ParsedType &ObjectType, |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4216 | bool &MayBePseudoDestructor) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4217 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4218 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4219 | if (Result.isInvalid()) return ExprError(); |
| 4220 | Base = Result.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4221 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4222 | Result = CheckPlaceholderExpr(Base); |
| 4223 | if (Result.isInvalid()) return ExprError(); |
| 4224 | Base = Result.take(); |
| 4225 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4226 | QualType BaseType = Base->getType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4227 | MayBePseudoDestructor = false; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4228 | if (BaseType->isDependentType()) { |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 4229 | // If we have a pointer to a dependent type and are using the -> operator, |
| 4230 | // the object type is the type that the pointer points to. We might still |
| 4231 | // have enough information about that type to do something useful. |
| 4232 | if (OpKind == tok::arrow) |
| 4233 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4234 | BaseType = Ptr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4235 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4236 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4237 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4238 | return Owned(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4239 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4240 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4241 | // C++ [over.match.oper]p8: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4243 | // returned, with the original second operand. |
| 4244 | if (OpKind == tok::arrow) { |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4245 | // The set of types we've considered so far. |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4246 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4247 | SmallVector<SourceLocation, 8> Locations; |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4248 | CTypes.insert(Context.getCanonicalType(BaseType)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4250 | while (BaseType->isRecordType()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4251 | Result = BuildOverloadedArrowExpr(S, Base, OpLoc); |
| 4252 | if (Result.isInvalid()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4253 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4254 | Base = Result.get(); |
| 4255 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Anders Carlsson | de699e5 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 4256 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4257 | BaseType = Base->getType(); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4258 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4259 | if (!CTypes.insert(CBaseType)) { |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4260 | Diag(OpLoc, diag::err_operator_arrow_circular); |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 4261 | for (unsigned i = 0; i < Locations.size(); i++) |
| 4262 | Diag(Locations[i], diag::note_declared_at); |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4263 | return ExprError(); |
| 4264 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4265 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | 31658df | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 4267 | if (BaseType->isPointerType()) |
| 4268 | BaseType = BaseType->getPointeeType(); |
| 4269 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4270 | |
| 4271 | // We could end up with various non-record types here, such as extended |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4272 | // vector types or Objective-C interfaces. Just return early and let |
| 4273 | // ActOnMemberReferenceExpr do the work. |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4274 | if (!BaseType->isRecordType()) { |
| 4275 | // C++ [basic.lookup.classref]p2: |
| 4276 | // [...] If the type of the object expression is of pointer to scalar |
| 4277 | // type, the unqualified-id is looked up in the context of the complete |
| 4278 | // postfix-expression. |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4279 | // |
| 4280 | // This also indicates that we should be parsing a |
| 4281 | // pseudo-destructor-name. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4282 | ObjectType = ParsedType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4283 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4284 | return Owned(Base); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4285 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4286 | |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4287 | // The object type must be complete (or dependent). |
| 4288 | if (!BaseType->isDependentType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4289 | RequireCompleteType(OpLoc, BaseType, |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4290 | PDiag(diag::err_incomplete_member_access))) |
| 4291 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4292 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4293 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | // 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] | 4295 | // 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] | 4296 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 4297 | // up in the scope of class C. [...] |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4298 | ObjectType = ParsedType::make(BaseType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4299 | return move(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4302 | ExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4303 | Expr *MemExpr) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4304 | SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4305 | Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call) |
| 4306 | << isa<CXXPseudoDestructorExpr>(MemExpr) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4307 | << FixItHint::CreateInsertion(ExpectedLParenLoc, "()"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4309 | return ActOnCallExpr(/*Scope*/ 0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4310 | MemExpr, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4311 | /*LPLoc*/ ExpectedLParenLoc, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4312 | MultiExprArg(), |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4313 | /*RPLoc*/ ExpectedLParenLoc); |
| 4314 | } |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4315 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4316 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4317 | SourceLocation OpLoc, |
| 4318 | tok::TokenKind OpKind, |
| 4319 | const CXXScopeSpec &SS, |
| 4320 | TypeSourceInfo *ScopeTypeInfo, |
| 4321 | SourceLocation CCLoc, |
| 4322 | SourceLocation TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4323 | PseudoDestructorTypeStorage Destructed, |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4324 | bool HasTrailingLParen) { |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4325 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4326 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4327 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | // The left-hand side of the dot operator shall be of scalar type. The |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4329 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4330 | // This scalar type is the object type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4331 | QualType ObjectType = Base->getType(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4332 | if (OpKind == tok::arrow) { |
| 4333 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 4334 | ObjectType = Ptr->getPointeeType(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4335 | } else if (!Base->isTypeDependent()) { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4336 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 4337 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 4338 | << ObjectType << true |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4339 | << FixItHint::CreateReplacement(OpLoc, "."); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4340 | if (isSFINAEContext()) |
| 4341 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4342 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4343 | OpKind = tok::period; |
| 4344 | } |
| 4345 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4347 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType()) { |
| 4348 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4349 | << ObjectType << Base->getSourceRange(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4350 | return ExprError(); |
| 4351 | } |
| 4352 | |
| 4353 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4354 | // [...] The cv-unqualified versions of the object type and of the type |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4355 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4356 | if (DestructedTypeInfo) { |
| 4357 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 4358 | SourceLocation DestructedTypeStart |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4359 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4360 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
| 4361 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
| 4362 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
| 4363 | << ObjectType << DestructedType << Base->getSourceRange() |
| 4364 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4365 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4366 | // Recover by setting the destructed type to the object type. |
| 4367 | DestructedType = ObjectType; |
| 4368 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4369 | DestructedTypeStart); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4370 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4371 | } else if (DestructedType.getObjCLifetime() != |
| 4372 | ObjectType.getObjCLifetime()) { |
| 4373 | |
| 4374 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
| 4375 | // Okay: just pretend that the user provided the correctly-qualified |
| 4376 | // type. |
| 4377 | } else { |
| 4378 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
| 4379 | << ObjectType << DestructedType << Base->getSourceRange() |
| 4380 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 4381 | } |
| 4382 | |
| 4383 | // Recover by setting the destructed type to the object type. |
| 4384 | DestructedType = ObjectType; |
| 4385 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 4386 | DestructedTypeStart); |
| 4387 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4388 | } |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4389 | } |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4390 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4391 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4392 | // C++ [expr.pseudo]p2: |
| 4393 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 4394 | // form |
| 4395 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4396 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4397 | // |
| 4398 | // shall designate the same scalar type. |
| 4399 | if (ScopeTypeInfo) { |
| 4400 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 4401 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | 81e317a | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 4402 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4403 | |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4404 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4405 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4406 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4407 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4408 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4409 | ScopeType = QualType(); |
| 4410 | ScopeTypeInfo = 0; |
| 4411 | } |
| 4412 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4413 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4414 | Expr *Result |
| 4415 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 4416 | OpKind == tok::arrow, OpLoc, |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 4417 | SS.getWithLocInContext(Context), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4418 | ScopeTypeInfo, |
| 4419 | CCLoc, |
| 4420 | TildeLoc, |
| 4421 | Destructed); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4422 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4423 | if (HasTrailingLParen) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4424 | return Owned(Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4425 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4426 | return DiagnoseDtorReference(Destructed.getLocation(), Result); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4429 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4430 | SourceLocation OpLoc, |
| 4431 | tok::TokenKind OpKind, |
| 4432 | CXXScopeSpec &SS, |
| 4433 | UnqualifiedId &FirstTypeName, |
| 4434 | SourceLocation CCLoc, |
| 4435 | SourceLocation TildeLoc, |
| 4436 | UnqualifiedId &SecondTypeName, |
| 4437 | bool HasTrailingLParen) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4438 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 4439 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 4440 | "Invalid first type name in pseudo-destructor"); |
| 4441 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 4442 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 4443 | "Invalid second type name in pseudo-destructor"); |
| 4444 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4445 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4446 | // The left-hand side of the dot operator shall be of scalar type. The |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4447 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4448 | // This scalar type is the object type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4449 | QualType ObjectType = Base->getType(); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4450 | if (OpKind == tok::arrow) { |
| 4451 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 4452 | ObjectType = Ptr->getPointeeType(); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4453 | } else if (!ObjectType->isDependentType()) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4454 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 4455 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4456 | << ObjectType << true |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4457 | << FixItHint::CreateReplacement(OpLoc, "."); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4458 | if (isSFINAEContext()) |
| 4459 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4460 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4461 | OpKind = tok::period; |
| 4462 | } |
| 4463 | } |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4464 | |
| 4465 | // Compute the object type that we should use for name lookup purposes. Only |
| 4466 | // record types and dependent types matter. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4467 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4468 | if (!SS.isSet()) { |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4469 | if (ObjectType->isRecordType()) |
| 4470 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4471 | else if (ObjectType->isDependentType()) |
| 4472 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4473 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | |
| 4475 | // Convert the name of the type being destructed (following the ~) into a |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4476 | // type (with source-location information). |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4477 | QualType DestructedType; |
| 4478 | TypeSourceInfo *DestructedTypeInfo = 0; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4479 | PseudoDestructorTypeStorage Destructed; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4480 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4481 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4482 | SecondTypeName.StartLocation, |
Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 4483 | S, &SS, true, false, ObjectTypePtrForLookup); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4484 | if (!T && |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4485 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 4486 | (!SS.isSet() && ObjectType->isDependentType()))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4487 | // 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] | 4488 | // couldn't find anything useful in scope. Just store the identifier and |
| 4489 | // it's location, and we'll perform (qualified) name lookup again at |
| 4490 | // template instantiation time. |
| 4491 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 4492 | SecondTypeName.StartLocation); |
| 4493 | } else if (!T) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4494 | Diag(SecondTypeName.StartLocation, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4495 | diag::err_pseudo_dtor_destructor_non_type) |
| 4496 | << SecondTypeName.Identifier << ObjectType; |
| 4497 | if (isSFINAEContext()) |
| 4498 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4499 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4500 | // Recover by assuming we had the right type all along. |
| 4501 | DestructedType = ObjectType; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4502 | } else |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4503 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4504 | } else { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4505 | // Resolve the template-id to a type. |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4506 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4507 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4508 | TemplateId->getTemplateArgs(), |
| 4509 | TemplateId->NumArgs); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 4510 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
| 4511 | TemplateId->Template, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4512 | TemplateId->TemplateNameLoc, |
| 4513 | TemplateId->LAngleLoc, |
| 4514 | TemplateArgsPtr, |
| 4515 | TemplateId->RAngleLoc); |
| 4516 | if (T.isInvalid() || !T.get()) { |
| 4517 | // Recover by assuming we had the right type all along. |
| 4518 | DestructedType = ObjectType; |
| 4519 | } else |
| 4520 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4521 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4522 | |
| 4523 | // 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] | 4524 | // information. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4525 | if (!DestructedType.isNull()) { |
| 4526 | if (!DestructedTypeInfo) |
| 4527 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4528 | SecondTypeName.StartLocation); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4529 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4530 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4531 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4532 | // Convert the name of the scope type (the type prior to '::') into a type. |
| 4533 | TypeSourceInfo *ScopeTypeInfo = 0; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4534 | QualType ScopeType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4535 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4536 | FirstTypeName.Identifier) { |
| 4537 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4538 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4539 | FirstTypeName.StartLocation, |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 4540 | S, &SS, true, false, ObjectTypePtrForLookup); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4541 | if (!T) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4542 | Diag(FirstTypeName.StartLocation, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4543 | diag::err_pseudo_dtor_destructor_non_type) |
| 4544 | << FirstTypeName.Identifier << ObjectType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4546 | if (isSFINAEContext()) |
| 4547 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4548 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4549 | // Just drop this type. It's unnecessary anyway. |
| 4550 | ScopeType = QualType(); |
| 4551 | } else |
| 4552 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
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 = FirstTypeName.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 dropping this type. |
| 4567 | ScopeType = QualType(); |
| 4568 | } else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4570 | } |
| 4571 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4572 | |
Douglas Gregor | b4a418f | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 4573 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 4574 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 4575 | FirstTypeName.StartLocation); |
| 4576 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4577 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4578 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 4579 | ScopeTypeInfo, CCLoc, TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4580 | Destructed, HasTrailingLParen); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4583 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4584 | CXXMethodDecl *Method, |
| 4585 | bool HadMultipleCandidates) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4586 | ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/0, |
| 4587 | FoundDecl, Method); |
| 4588 | if (Exp.isInvalid()) |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4589 | return true; |
Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 4590 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4591 | MemberExpr *ME = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4592 | new (Context) MemberExpr(Exp.take(), /*IsArrow=*/false, Method, |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 4593 | SourceLocation(), Context.BoundMemberTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4594 | VK_RValue, OK_Ordinary); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4595 | if (HadMultipleCandidates) |
| 4596 | ME->setHadMultipleCandidates(true); |
| 4597 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4598 | QualType ResultType = Method->getResultType(); |
| 4599 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 4600 | ResultType = ResultType.getNonLValueExprType(Context); |
| 4601 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4602 | MarkDeclarationReferenced(Exp.get()->getLocStart(), Method); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4603 | CXXMemberCallExpr *CE = |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4604 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, VK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4605 | Exp.get()->getLocEnd()); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 4606 | return CE; |
| 4607 | } |
| 4608 | |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4609 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 4610 | SourceLocation RParen) { |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4611 | return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand, |
| 4612 | Operand->CanThrow(Context), |
| 4613 | KeyLoc, RParen)); |
| 4614 | } |
| 4615 | |
| 4616 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 4617 | Expr *Operand, SourceLocation RParen) { |
| 4618 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4621 | /// Perform the conversions required for an expression used in a |
| 4622 | /// context that ignores the result. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4623 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4624 | if (E->hasPlaceholderType()) { |
| 4625 | ExprResult result = CheckPlaceholderExpr(E); |
| 4626 | if (result.isInvalid()) return Owned(E); |
| 4627 | E = result.take(); |
| 4628 | } |
| 4629 | |
John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 4630 | // C99 6.3.2.1: |
| 4631 | // [Except in specific positions,] an lvalue that does not have |
| 4632 | // array type is converted to the value stored in the |
| 4633 | // designated object (and is no longer an lvalue). |
John McCall | e6d134b | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 4634 | if (E->isRValue()) { |
| 4635 | // In C, function designators (i.e. expressions of function type) |
| 4636 | // are r-values, but we still want to do function-to-pointer decay |
| 4637 | // on them. This is both technically correct and convenient for |
| 4638 | // some clients. |
| 4639 | if (!getLangOptions().CPlusPlus && E->getType()->isFunctionType()) |
| 4640 | return DefaultFunctionArrayConversion(E); |
| 4641 | |
| 4642 | return Owned(E); |
| 4643 | } |
John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 4644 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4645 | // 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] | 4646 | if (getLangOptions().CPlusPlus) return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4647 | |
| 4648 | // GCC seems to also exclude expressions of incomplete enum type. |
| 4649 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
| 4650 | if (!T->getDecl()->isComplete()) { |
| 4651 | // FIXME: stupid workaround for a codegen bug! |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4652 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).take(); |
| 4653 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4654 | } |
| 4655 | } |
| 4656 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4657 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 4658 | if (Res.isInvalid()) |
| 4659 | return Owned(E); |
| 4660 | E = Res.take(); |
| 4661 | |
John McCall | 85515d6 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 4662 | if (!E->getType()->isVoidType()) |
| 4663 | RequireCompleteType(E->getExprLoc(), E->getType(), |
| 4664 | diag::err_incomplete_type); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4665 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4668 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE) { |
| 4669 | ExprResult FullExpr = Owned(FE); |
| 4670 | |
| 4671 | if (!FullExpr.get()) |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4672 | return ExprError(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4673 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4674 | if (DiagnoseUnexpandedParameterPack(FullExpr.get())) |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 4675 | return ExprError(); |
| 4676 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4677 | FullExpr = CheckPlaceholderExpr(FullExpr.take()); |
| 4678 | if (FullExpr.isInvalid()) |
| 4679 | return ExprError(); |
Douglas Gregor | 353ee24 | 2011-03-07 02:05:23 +0000 | [diff] [blame] | 4680 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4681 | FullExpr = IgnoredValueConversions(FullExpr.take()); |
| 4682 | if (FullExpr.isInvalid()) |
| 4683 | return ExprError(); |
| 4684 | |
Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4685 | CheckImplicitConversions(FullExpr.get(), FullExpr.get()->getExprLoc()); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4686 | return MaybeCreateExprWithCleanups(FullExpr); |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 4687 | } |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4688 | |
| 4689 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
| 4690 | if (!FullStmt) return StmtError(); |
| 4691 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4692 | return MaybeCreateStmtWithCleanups(FullStmt); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4693 | } |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4694 | |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4695 | Sema::IfExistsResult |
| 4696 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, |
| 4697 | CXXScopeSpec &SS, |
| 4698 | const DeclarationNameInfo &TargetNameInfo) { |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4699 | DeclarationName TargetName = TargetNameInfo.getName(); |
| 4700 | if (!TargetName) |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4701 | return IER_DoesNotExist; |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4702 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4703 | // If the name itself is dependent, then the result is dependent. |
| 4704 | if (TargetName.isDependentName()) |
| 4705 | return IER_Dependent; |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4706 | |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4707 | // Do the redeclaration lookup in the current scope. |
| 4708 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
| 4709 | Sema::NotForRedeclaration); |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4710 | LookupParsedName(R, S, &SS); |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4711 | R.suppressDiagnostics(); |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4712 | |
| 4713 | switch (R.getResultKind()) { |
| 4714 | case LookupResult::Found: |
| 4715 | case LookupResult::FoundOverloaded: |
| 4716 | case LookupResult::FoundUnresolvedValue: |
| 4717 | case LookupResult::Ambiguous: |
| 4718 | return IER_Exists; |
| 4719 | |
| 4720 | case LookupResult::NotFound: |
| 4721 | return IER_DoesNotExist; |
| 4722 | |
| 4723 | case LookupResult::NotFoundInCurrentInstantiation: |
| 4724 | return IER_Dependent; |
| 4725 | } |
| 4726 | |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4727 | return IER_DoesNotExist; |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4728 | } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4729 | |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 4730 | Sema::IfExistsResult |
| 4731 | Sema::CheckMicrosoftIfExistsSymbol(Scope *S, SourceLocation KeywordLoc, |
| 4732 | bool IsIfExists, CXXScopeSpec &SS, |
| 4733 | UnqualifiedId &Name) { |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4734 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 4735 | |
| 4736 | // Check for unexpanded parameter packs. |
| 4737 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
| 4738 | collectUnexpandedParameterPacks(SS, Unexpanded); |
| 4739 | collectUnexpandedParameterPacks(TargetNameInfo, Unexpanded); |
| 4740 | if (!Unexpanded.empty()) { |
| 4741 | DiagnoseUnexpandedParameterPacks(KeywordLoc, |
| 4742 | IsIfExists? UPPC_IfExists |
| 4743 | : UPPC_IfNotExists, |
| 4744 | Unexpanded); |
| 4745 | return IER_Error; |
| 4746 | } |
| 4747 | |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 4748 | return CheckMicrosoftIfExistsSymbol(S, SS, TargetNameInfo); |
| 4749 | } |
| 4750 | |