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 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 268 | /// \brief Build a C++ typeid expression with a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 269 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 270 | SourceLocation TypeidLoc, |
| 271 | TypeSourceInfo *Operand, |
| 272 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 273 | // C++ [expr.typeid]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 274 | // 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] | 275 | // that is the operand of typeid are always ignored. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 276 | // 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] | 277 | // type, the class shall be completely-defined. |
Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 278 | Qualifiers Quals; |
| 279 | QualType T |
| 280 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
| 281 | Quals); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 282 | if (T->getAs<RecordType>() && |
| 283 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 284 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 285 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 286 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
| 287 | Operand, |
| 288 | SourceRange(TypeidLoc, RParenLoc))); |
| 289 | } |
| 290 | |
| 291 | /// \brief Build a C++ typeid expression with an expression operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 292 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 293 | SourceLocation TypeidLoc, |
| 294 | Expr *E, |
| 295 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 296 | bool isUnevaluatedOperand = true; |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 297 | if (E && !E->isTypeDependent()) { |
| 298 | QualType T = E->getType(); |
| 299 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
| 300 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 301 | // C++ [expr.typeid]p3: |
| 302 | // [...] If the type of the expression is a class type, the class |
| 303 | // shall be completely-defined. |
| 304 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 305 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 307 | // C++ [expr.typeid]p3: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 308 | // 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] | 309 | // polymorphic class type [...] [the] expression is an unevaluated |
| 310 | // operand. [...] |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 311 | if (RecordD->isPolymorphic() && E->Classify(Context).isGLValue()) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 312 | isUnevaluatedOperand = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 313 | |
| 314 | // We require a vtable to query the type at run time. |
| 315 | MarkVTableUsed(TypeidLoc, RecordD); |
| 316 | } |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 317 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 319 | // C++ [expr.typeid]p4: |
| 320 | // [...] 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] | 321 | // cv-qualified type, the result of the typeid expression refers to a |
| 322 | // std::type_info object representing the cv-unqualified referenced |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 323 | // type. |
Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 324 | Qualifiers Quals; |
| 325 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
| 326 | if (!Context.hasSameType(T, UnqualT)) { |
| 327 | T = UnqualT; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 328 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, CastCategory(E)).take(); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 331 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 332 | // If this is an unevaluated operand, clear out the set of |
| 333 | // declaration references we have been computing and eliminate any |
| 334 | // temporaries introduced in its computation. |
| 335 | if (isUnevaluatedOperand) |
| 336 | ExprEvalContexts.back().Context = Unevaluated; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 337 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 338 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 339 | E, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 340 | SourceRange(TypeidLoc, RParenLoc))); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 344 | ExprResult |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 345 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 346 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 347 | // Find the std::type_info type. |
Sebastian Redl | ce0682f | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 348 | if (!getStdNamespace()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 349 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 350 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 351 | if (!CXXTypeInfoDecl) { |
| 352 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 353 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 354 | LookupQualifiedName(R, getStdNamespace()); |
| 355 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 356 | if (!CXXTypeInfoDecl) |
| 357 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 358 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 359 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 360 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 361 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 362 | if (isType) { |
| 363 | // The operand is a type; handle it as such. |
| 364 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 365 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 366 | &TInfo); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 367 | if (T.isNull()) |
| 368 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 370 | if (!TInfo) |
| 371 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 372 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 373 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 374 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 376 | // The operand is an expression. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 377 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 380 | /// Retrieve the UuidAttr associated with QT. |
| 381 | static UuidAttr *GetUuidAttrOfType(QualType QT) { |
| 382 | // Optionally remove one level of pointer, reference or array indirection. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 383 | const Type *Ty = QT.getTypePtr();; |
Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 384 | if (QT->isPointerType() || QT->isReferenceType()) |
| 385 | Ty = QT->getPointeeType().getTypePtr(); |
| 386 | else if (QT->isArrayType()) |
| 387 | Ty = cast<ArrayType>(QT)->getElementType().getTypePtr(); |
| 388 | |
Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 389 | // Loop all record redeclaration looking for an uuid attribute. |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 390 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 391 | for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(), |
| 392 | E = RD->redecls_end(); I != E; ++I) { |
| 393 | if (UuidAttr *Uuid = I->getAttr<UuidAttr>()) |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 394 | return Uuid; |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 395 | } |
Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 396 | |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 397 | return 0; |
Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 400 | /// \brief Build a Microsoft __uuidof expression with a type operand. |
| 401 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 402 | SourceLocation TypeidLoc, |
| 403 | TypeSourceInfo *Operand, |
| 404 | SourceLocation RParenLoc) { |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 405 | if (!Operand->getType()->isDependentType()) { |
| 406 | if (!GetUuidAttrOfType(Operand->getType())) |
| 407 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 408 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 409 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 410 | // FIXME: add __uuidof semantic analysis for type operand. |
| 411 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 412 | Operand, |
| 413 | SourceRange(TypeidLoc, RParenLoc))); |
| 414 | } |
| 415 | |
| 416 | /// \brief Build a Microsoft __uuidof expression with an expression operand. |
| 417 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 418 | SourceLocation TypeidLoc, |
| 419 | Expr *E, |
| 420 | SourceLocation RParenLoc) { |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 421 | if (!E->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 422 | if (!GetUuidAttrOfType(E->getType()) && |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 423 | !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 424 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 425 | } |
| 426 | // FIXME: add __uuidof semantic analysis for type operand. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 427 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 428 | E, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 429 | SourceRange(TypeidLoc, RParenLoc))); |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 433 | ExprResult |
| 434 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 435 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 436 | // If MSVCGuidDecl has not been cached, do the lookup. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 437 | if (!MSVCGuidDecl) { |
| 438 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 439 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 440 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 441 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 442 | if (!MSVCGuidDecl) |
| 443 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 446 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 447 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 448 | if (isType) { |
| 449 | // The operand is a type; handle it as such. |
| 450 | TypeSourceInfo *TInfo = 0; |
| 451 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 452 | &TInfo); |
| 453 | if (T.isNull()) |
| 454 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 455 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 456 | if (!TInfo) |
| 457 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 458 | |
| 459 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 460 | } |
| 461 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 462 | // The operand is an expression. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 463 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 464 | } |
| 465 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 466 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 467 | ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 468 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 469 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 470 | "Unknown C++ Boolean value!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 471 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 472 | Context.BoolTy, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 474 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 475 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 476 | ExprResult |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 477 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
| 478 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 479 | } |
| 480 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 481 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 482 | ExprResult |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 483 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { |
| 484 | bool IsThrownVarInScope = false; |
| 485 | if (Ex) { |
| 486 | // C++0x [class.copymove]p31: |
| 487 | // When certain criteria are met, an implementation is allowed to omit the |
| 488 | // copy/move construction of a class object [...] |
| 489 | // |
| 490 | // - in a throw-expression, when the operand is the name of a |
| 491 | // non-volatile automatic object (other than a function or catch- |
| 492 | // clause parameter) whose scope does not extend beyond the end of the |
| 493 | // innermost enclosing try-block (if there is one), the copy/move |
| 494 | // operation from the operand to the exception object (15.1) can be |
| 495 | // omitted by constructing the automatic object directly into the |
| 496 | // exception object |
| 497 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) |
| 498 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 499 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { |
| 500 | for( ; S; S = S->getParent()) { |
| 501 | if (S->isDeclScope(Var)) { |
| 502 | IsThrownVarInScope = true; |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | if (S->getFlags() & |
| 507 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | |
| 508 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | |
| 509 | Scope::TryScope)) |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); |
| 517 | } |
| 518 | |
| 519 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, |
| 520 | bool IsThrownVarInScope) { |
Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 521 | // Don't report an error if 'throw' is used in system headers. |
Anders Carlsson | 15348ae | 2011-02-28 02:27:16 +0000 | [diff] [blame] | 522 | if (!getLangOptions().CXXExceptions && |
Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 523 | !getSourceManager().isInSystemHeader(OpLoc)) |
Anders Carlsson | b1fba31 | 2011-02-19 21:53:09 +0000 | [diff] [blame] | 524 | Diag(OpLoc, diag::err_exceptions_disabled) << "throw"; |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 525 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 526 | if (Ex && !Ex->isTypeDependent()) { |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 527 | ExprResult ExRes = CheckCXXThrowOperand(OpLoc, Ex, IsThrownVarInScope); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 528 | if (ExRes.isInvalid()) |
| 529 | return ExprError(); |
| 530 | Ex = ExRes.take(); |
| 531 | } |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 532 | |
| 533 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc, |
| 534 | IsThrownVarInScope)); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 538 | ExprResult Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *E, |
| 539 | bool IsThrownVarInScope) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 540 | // C++ [except.throw]p3: |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 541 | // A throw-expression initializes a temporary object, called the exception |
| 542 | // object, the type of which is determined by removing any top-level |
| 543 | // 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] | 544 | // 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] | 545 | // or "pointer to function returning T", [...] |
| 546 | if (E->getType().hasQualifiers()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 547 | E = ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp, |
| 548 | CastCategory(E)).take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 549 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 550 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 551 | if (Res.isInvalid()) |
| 552 | return ExprError(); |
| 553 | E = Res.take(); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 554 | |
| 555 | // If the type of the exception would be an incomplete type or a pointer |
| 556 | // to an incomplete type other than (cv) void the program is ill-formed. |
| 557 | QualType Ty = E->getType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 558 | bool isPointer = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 559 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 560 | Ty = Ptr->getPointeeType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 561 | isPointer = true; |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 562 | } |
| 563 | if (!isPointer || !Ty->isVoidType()) { |
| 564 | if (RequireCompleteType(ThrowLoc, Ty, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 565 | PDiag(isPointer ? diag::err_throw_incomplete_ptr |
| 566 | : diag::err_throw_incomplete) |
| 567 | << E->getSourceRange())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 568 | return ExprError(); |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 569 | |
Douglas Gregor | bf422f9 | 2010-04-15 18:05:39 +0000 | [diff] [blame] | 570 | if (RequireNonAbstractType(ThrowLoc, E->getType(), |
| 571 | PDiag(diag::err_throw_abstract_type) |
| 572 | << E->getSourceRange())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 573 | return ExprError(); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 574 | } |
| 575 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 576 | // Initialize the exception result. This implicitly weeds out |
| 577 | // abstract types or types with inaccessible copy constructors. |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 578 | |
| 579 | // C++0x [class.copymove]p31: |
| 580 | // When certain criteria are met, an implementation is allowed to omit the |
| 581 | // copy/move construction of a class object [...] |
| 582 | // |
| 583 | // - in a throw-expression, when the operand is the name of a |
| 584 | // non-volatile automatic object (other than a function or catch-clause |
| 585 | // parameter) whose scope does not extend beyond the end of the |
| 586 | // innermost enclosing try-block (if there is one), the copy/move |
| 587 | // operation from the operand to the exception object (15.1) can be |
| 588 | // omitted by constructing the automatic object directly into the |
| 589 | // exception object |
| 590 | const VarDecl *NRVOVariable = 0; |
| 591 | if (IsThrownVarInScope) |
| 592 | NRVOVariable = getCopyElisionCandidate(QualType(), E, false); |
| 593 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 594 | InitializedEntity Entity = |
Douglas Gregor | 72dfa27 | 2011-01-21 22:46:35 +0000 | [diff] [blame] | 595 | InitializedEntity::InitializeException(ThrowLoc, E->getType(), |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 596 | /*NRVO=*/NRVOVariable != 0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 597 | Res = PerformMoveOrCopyInitialization(Entity, NRVOVariable, |
Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 598 | QualType(), E, |
| 599 | IsThrownVarInScope); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 600 | if (Res.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 601 | return ExprError(); |
| 602 | E = Res.take(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 603 | |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 604 | // If the exception has class type, we need additional handling. |
| 605 | const RecordType *RecordTy = Ty->getAs<RecordType>(); |
| 606 | if (!RecordTy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 607 | return Owned(E); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 608 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 609 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 610 | // If we are throwing a polymorphic class type or pointer thereof, |
| 611 | // exception handling will make use of the vtable. |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 612 | MarkVTableUsed(ThrowLoc, RD); |
| 613 | |
Eli Friedman | 98efb9f | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 614 | // If a pointer is thrown, the referenced object will not be destroyed. |
| 615 | if (isPointer) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 616 | return Owned(E); |
Eli Friedman | 98efb9f | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 617 | |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 618 | // If the class has a non-trivial destructor, we must be able to call it. |
| 619 | if (RD->hasTrivialDestructor()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 620 | return Owned(E); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 621 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 622 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 623 | = const_cast<CXXDestructorDecl*>(LookupDestructor(RD)); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 624 | if (!Destructor) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 625 | return Owned(E); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 626 | |
| 627 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
| 628 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 629 | PDiag(diag::err_access_dtor_exception) << Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 630 | return Owned(E); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 631 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 632 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 633 | QualType Sema::getAndCaptureCurrentThisType() { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 634 | // Ignore block scopes: we can capture through them. |
| 635 | // Ignore nested enum scopes: we'll diagnose non-constant expressions |
| 636 | // where they're invalid, and other uses are legitimate. |
| 637 | // 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] | 638 | DeclContext *DC = CurContext; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 639 | unsigned NumBlocks = 0; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 640 | while (true) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 641 | if (isa<BlockDecl>(DC)) { |
| 642 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 643 | ++NumBlocks; |
| 644 | } else if (isa<EnumDecl>(DC)) |
| 645 | DC = cast<EnumDecl>(DC)->getDeclContext(); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 646 | else break; |
| 647 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 648 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 649 | QualType ThisTy; |
| 650 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { |
| 651 | if (method && method->isInstance()) |
| 652 | ThisTy = method->getThisType(Context); |
| 653 | } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { |
| 654 | // C++0x [expr.prim]p4: |
| 655 | // Otherwise, if a member-declarator declares a non-static data member |
| 656 | // of a class X, the expression this is a prvalue of type "pointer to X" |
| 657 | // within the optional brace-or-equal-initializer. |
| 658 | Scope *S = getScopeForContext(DC); |
| 659 | if (!S || S->getFlags() & Scope::ThisScope) |
| 660 | ThisTy = Context.getPointerType(Context.getRecordType(RD)); |
| 661 | } |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 662 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 663 | // Mark that we're closing on 'this' in all the block scopes we ignored. |
| 664 | if (!ThisTy.isNull()) |
| 665 | for (unsigned idx = FunctionScopes.size() - 1; |
| 666 | NumBlocks; --idx, --NumBlocks) |
| 667 | cast<BlockScopeInfo>(FunctionScopes[idx])->CapturesCXXThis = true; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 668 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 669 | return ThisTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 672 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 673 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 674 | /// is a non-lvalue expression whose value is the address of the object for |
| 675 | /// which the function is called. |
| 676 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 677 | QualType ThisTy = getAndCaptureCurrentThisType(); |
| 678 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 679 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 680 | return Owned(new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 681 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 682 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 683 | ExprResult |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 684 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 685 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 686 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 687 | SourceLocation RParenLoc) { |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 688 | if (!TypeRep) |
| 689 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 690 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 691 | TypeSourceInfo *TInfo; |
| 692 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 693 | if (!TInfo) |
| 694 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 695 | |
| 696 | return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); |
| 697 | } |
| 698 | |
| 699 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 700 | /// Can be interpreted either as function-style casting ("int(x)") |
| 701 | /// or class type construction ("ClassType(x,y,z)") |
| 702 | /// or creation of a value-initialized type ("int()"). |
| 703 | ExprResult |
| 704 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
| 705 | SourceLocation LParenLoc, |
| 706 | MultiExprArg exprs, |
| 707 | SourceLocation RParenLoc) { |
| 708 | QualType Ty = TInfo->getType(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 709 | unsigned NumExprs = exprs.size(); |
| 710 | Expr **Exprs = (Expr**)exprs.get(); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 711 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 712 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 713 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 714 | if (Ty->isDependentType() || |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 715 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 716 | exprs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 718 | return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo, |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 719 | LParenLoc, |
| 720 | Exprs, NumExprs, |
| 721 | RParenLoc)); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 724 | if (Ty->isArrayType()) |
| 725 | return ExprError(Diag(TyBeginLoc, |
| 726 | diag::err_value_init_for_array_type) << FullRange); |
| 727 | if (!Ty->isVoidType() && |
| 728 | RequireCompleteType(TyBeginLoc, Ty, |
| 729 | PDiag(diag::err_invalid_incomplete_type_use) |
| 730 | << FullRange)) |
| 731 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 732 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 733 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 734 | diag::err_allocation_of_abstract_type)) |
| 735 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | |
| 737 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 738 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 739 | // If the expression list is a single expression, the type conversion |
| 740 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 741 | // corresponding cast expression. |
| 742 | // |
| 743 | if (NumExprs == 1) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 744 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 745 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 746 | CXXCastPath BasePath; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 747 | ExprResult CastExpr = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 748 | CheckCastTypes(TInfo->getTypeLoc().getBeginLoc(), |
| 749 | TInfo->getTypeLoc().getSourceRange(), Ty, Exprs[0], |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 750 | Kind, VK, BasePath, |
| 751 | /*FunctionalStyle=*/true); |
| 752 | if (CastExpr.isInvalid()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 753 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 754 | Exprs[0] = CastExpr.take(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 755 | |
| 756 | exprs.release(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 757 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 758 | return Owned(CXXFunctionalCastExpr::Create(Context, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 759 | Ty.getNonLValueExprType(Context), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 760 | VK, TInfo, TyBeginLoc, Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 761 | Exprs[0], &BasePath, |
| 762 | RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 765 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 766 | InitializationKind Kind |
| 767 | = NumExprs ? InitializationKind::CreateDirect(TyBeginLoc, |
| 768 | LParenLoc, RParenLoc) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 769 | : InitializationKind::CreateValue(TyBeginLoc, |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 770 | LParenLoc, RParenLoc); |
| 771 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); |
| 772 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(exprs)); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 774 | // FIXME: Improve AST representation? |
| 775 | return move(Result); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 776 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 777 | |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 778 | /// doesUsualArrayDeleteWantSize - Answers whether the usual |
| 779 | /// operator delete[] for the given type has a size_t parameter. |
| 780 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, |
| 781 | QualType allocType) { |
| 782 | const RecordType *record = |
| 783 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 784 | if (!record) return false; |
| 785 | |
| 786 | // Try to find an operator delete[] in class scope. |
| 787 | |
| 788 | DeclarationName deleteName = |
| 789 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
| 790 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); |
| 791 | S.LookupQualifiedName(ops, record->getDecl()); |
| 792 | |
| 793 | // We're just doing this for information. |
| 794 | ops.suppressDiagnostics(); |
| 795 | |
| 796 | // Very likely: there's no operator delete[]. |
| 797 | if (ops.empty()) return false; |
| 798 | |
| 799 | // If it's ambiguous, it should be illegal to call operator delete[] |
| 800 | // on this thing, so it doesn't matter if we allocate extra space or not. |
| 801 | if (ops.isAmbiguous()) return false; |
| 802 | |
| 803 | LookupResult::Filter filter = ops.makeFilter(); |
| 804 | while (filter.hasNext()) { |
| 805 | NamedDecl *del = filter.next()->getUnderlyingDecl(); |
| 806 | |
| 807 | // C++0x [basic.stc.dynamic.deallocation]p2: |
| 808 | // A template instance is never a usual deallocation function, |
| 809 | // regardless of its signature. |
| 810 | if (isa<FunctionTemplateDecl>(del)) { |
| 811 | filter.erase(); |
| 812 | continue; |
| 813 | } |
| 814 | |
| 815 | // C++0x [basic.stc.dynamic.deallocation]p2: |
| 816 | // If class T does not declare [an operator delete[] with one |
| 817 | // parameter] but does declare a member deallocation function |
| 818 | // named operator delete[] with exactly two parameters, the |
| 819 | // second of which has type std::size_t, then this function |
| 820 | // is a usual deallocation function. |
| 821 | if (!cast<CXXMethodDecl>(del)->isUsualDeallocationFunction()) { |
| 822 | filter.erase(); |
| 823 | continue; |
| 824 | } |
| 825 | } |
| 826 | filter.done(); |
| 827 | |
| 828 | if (!ops.isSingleResult()) return false; |
| 829 | |
| 830 | const FunctionDecl *del = cast<FunctionDecl>(ops.getFoundDecl()); |
| 831 | return (del->getNumParams() == 2); |
| 832 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 833 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 834 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 835 | /// @code new (memory) int[size][4] @endcode |
| 836 | /// or |
| 837 | /// @code ::new Foo(23, "hello") @endcode |
| 838 | /// For the interpretation of this heap of arguments, consult the base version. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 839 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 840 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 841 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 842 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 843 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 844 | MultiExprArg ConstructorArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | SourceLocation ConstructorRParen) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 846 | bool TypeContainsAuto = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 847 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 848 | Expr *ArraySize = 0; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 849 | // If the specified type is an array, unwrap it and save the expression. |
| 850 | if (D.getNumTypeObjects() > 0 && |
| 851 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 852 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 853 | if (TypeContainsAuto) |
| 854 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) |
| 855 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 856 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 857 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 858 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 859 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 860 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 861 | << D.getSourceRange()); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 862 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 863 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 864 | D.DropFirstTypeObject(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 867 | // Every dimension shall be of constant size. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 868 | if (ArraySize) { |
| 869 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 870 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 871 | break; |
| 872 | |
| 873 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 874 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
| 875 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && |
| 876 | !NumElts->isIntegerConstantExpr(Context)) { |
| 877 | Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) |
| 878 | << NumElts->getSourceRange(); |
| 879 | return ExprError(); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | } |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 884 | |
Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 885 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/0); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 886 | QualType AllocType = TInfo->getType(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 887 | if (D.isInvalidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 888 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 889 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | return BuildCXXNew(StartLoc, UseGlobal, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 891 | PlacementLParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | move(PlacementArgs), |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 893 | PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 894 | TypeIdParens, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 896 | TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 897 | ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 898 | ConstructorLParen, |
| 899 | move(ConstructorArgs), |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 900 | ConstructorRParen, |
| 901 | TypeContainsAuto); |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 902 | } |
| 903 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 904 | ExprResult |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 905 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 906 | SourceLocation PlacementLParen, |
| 907 | MultiExprArg PlacementArgs, |
| 908 | SourceLocation PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 909 | SourceRange TypeIdParens, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 910 | QualType AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 911 | TypeSourceInfo *AllocTypeInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 912 | Expr *ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 913 | SourceLocation ConstructorLParen, |
| 914 | MultiExprArg ConstructorArgs, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 915 | SourceLocation ConstructorRParen, |
| 916 | bool TypeMayContainAuto) { |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 917 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 918 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 919 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 920 | if (TypeMayContainAuto && AllocType->getContainedAutoType()) { |
| 921 | if (ConstructorArgs.size() == 0) |
| 922 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) |
| 923 | << AllocType << TypeRange); |
| 924 | if (ConstructorArgs.size() != 1) { |
| 925 | Expr *FirstBad = ConstructorArgs.get()[1]; |
| 926 | return ExprError(Diag(FirstBad->getSourceRange().getBegin(), |
| 927 | diag::err_auto_new_ctor_multiple_expressions) |
| 928 | << AllocType << TypeRange); |
| 929 | } |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 930 | TypeSourceInfo *DeducedType = 0; |
| 931 | if (!DeduceAutoType(AllocTypeInfo, ConstructorArgs.get()[0], DeducedType)) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 932 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) |
| 933 | << AllocType |
| 934 | << ConstructorArgs.get()[0]->getType() |
| 935 | << TypeRange |
| 936 | << ConstructorArgs.get()[0]->getSourceRange()); |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 937 | if (!DeducedType) |
| 938 | return ExprError(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 939 | |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 940 | AllocTypeInfo = DeducedType; |
| 941 | AllocType = AllocTypeInfo->getType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 944 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 945 | // typedef of an array type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 946 | if (!ArraySize) { |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 947 | if (const ConstantArrayType *Array |
| 948 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 949 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 950 | Context.getSizeType(), |
| 951 | TypeRange.getEnd()); |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 952 | AllocType = Array->getElementType(); |
| 953 | } |
| 954 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 955 | |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 956 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 957 | return ExprError(); |
| 958 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 959 | // In ARC, infer 'retaining' for the allocated |
| 960 | if (getLangOptions().ObjCAutoRefCount && |
| 961 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 962 | AllocType->isObjCLifetimeType()) { |
| 963 | AllocType = Context.getLifetimeQualifiedType(AllocType, |
| 964 | AllocType->getObjCARCImplicitLifetime()); |
| 965 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 966 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 967 | QualType ResultType = Context.getPointerType(AllocType); |
| 968 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 969 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 970 | // or enumeration type with a non-negative value." |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 971 | if (ArraySize && !ArraySize->isTypeDependent()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 972 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 973 | QualType SizeType = ArraySize->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 974 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 975 | ExprResult ConvertedSize |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 976 | = ConvertToIntegralOrEnumerationType(StartLoc, ArraySize, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 977 | PDiag(diag::err_array_size_not_integral), |
| 978 | PDiag(diag::err_array_size_incomplete_type) |
| 979 | << ArraySize->getSourceRange(), |
| 980 | PDiag(diag::err_array_size_explicit_conversion), |
| 981 | PDiag(diag::note_array_size_conversion), |
| 982 | PDiag(diag::err_array_size_ambiguous_conversion), |
| 983 | PDiag(diag::note_array_size_conversion), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 984 | PDiag(getLangOptions().CPlusPlus0x? 0 |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 985 | : diag::ext_array_size_conversion)); |
| 986 | if (ConvertedSize.isInvalid()) |
| 987 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 988 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 989 | ArraySize = ConvertedSize.take(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 990 | SizeType = ArraySize->getType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 991 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 992 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 993 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 994 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 995 | // We don't care about special rules, so we tell the machinery it's not |
| 996 | // evaluated - it gives us a result in more cases. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 997 | if (!ArraySize->isValueDependent()) { |
| 998 | llvm::APSInt Value; |
| 999 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 1000 | if (Value < llvm::APSInt( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1001 | llvm::APInt::getNullValue(Value.getBitWidth()), |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 1002 | Value.isUnsigned())) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1003 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1004 | diag::err_typecheck_negative_array_size) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1005 | << ArraySize->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1007 | if (!AllocType->isDependentType()) { |
| 1008 | unsigned ActiveSizeBits |
| 1009 | = ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
| 1010 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1011 | Diag(ArraySize->getSourceRange().getBegin(), |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1012 | diag::err_array_too_large) |
| 1013 | << Value.toString(10) |
| 1014 | << ArraySize->getSourceRange(); |
| 1015 | return ExprError(); |
| 1016 | } |
| 1017 | } |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1018 | } else if (TypeIdParens.isValid()) { |
| 1019 | // Can't have dynamic array size when the type-id is in parentheses. |
| 1020 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 1021 | << ArraySize->getSourceRange() |
| 1022 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 1023 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1024 | |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1025 | TypeIdParens = SourceRange(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1026 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1027 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1028 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1029 | // ARC: warn about ABI issues. |
| 1030 | if (getLangOptions().ObjCAutoRefCount) { |
| 1031 | QualType BaseAllocType = Context.getBaseElementType(AllocType); |
| 1032 | if (BaseAllocType.hasStrongOrWeakObjCLifetime()) |
| 1033 | Diag(StartLoc, diag::warn_err_new_delete_object_array) |
| 1034 | << 0 << BaseAllocType; |
| 1035 | } |
| 1036 | |
John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 1037 | // Note that we do *not* convert the argument in any way. It can |
| 1038 | // be signed, larger than size_t, whatever. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1039 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1040 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1041 | FunctionDecl *OperatorNew = 0; |
| 1042 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1043 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 1044 | unsigned NumPlaceArgs = PlacementArgs.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1045 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1046 | if (!AllocType->isDependentType() && |
| 1047 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && |
| 1048 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1049 | SourceRange(PlacementLParen, PlacementRParen), |
| 1050 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 1051 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1052 | return ExprError(); |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1053 | |
| 1054 | // If this is an array allocation, compute whether the usual array |
| 1055 | // deallocation function for the type has a size_t parameter. |
| 1056 | bool UsualArrayDeleteWantsSize = false; |
| 1057 | if (ArraySize && !AllocType->isDependentType()) |
| 1058 | UsualArrayDeleteWantsSize |
| 1059 | = doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); |
| 1060 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1061 | SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1062 | if (OperatorNew) { |
| 1063 | // Add default arguments, if any. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1064 | const FunctionProtoType *Proto = |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1065 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1066 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 1067 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1068 | |
Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1069 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1070 | Proto, 1, PlaceArgs, NumPlaceArgs, |
Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1071 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1072 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1073 | |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1074 | NumPlaceArgs = AllPlaceArgs.size(); |
| 1075 | if (NumPlaceArgs > 0) |
| 1076 | PlaceArgs = &AllPlaceArgs[0]; |
| 1077 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1078 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1079 | bool Init = ConstructorLParen.isValid(); |
| 1080 | // --- Choosing a constructor --- |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1081 | CXXConstructorDecl *Constructor = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1082 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); |
| 1083 | unsigned NumConsArgs = ConstructorArgs.size(); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1084 | ASTOwningVector<Expr*> ConvertedConstructorArgs(*this); |
Eli Friedman | a8ce9ec | 2009-11-08 22:15:39 +0000 | [diff] [blame] | 1085 | |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1086 | // Array 'new' can't have any initializers. |
Anders Carlsson | 55cbd6e | 2010-05-16 16:24:20 +0000 | [diff] [blame] | 1087 | if (NumConsArgs && (ResultType->isArrayType() || ArraySize)) { |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1088 | SourceRange InitRange(ConsArgs[0]->getLocStart(), |
| 1089 | ConsArgs[NumConsArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1090 | |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1091 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 1092 | return ExprError(); |
| 1093 | } |
| 1094 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1095 | if (!AllocType->isDependentType() && |
| 1096 | !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { |
| 1097 | // C++0x [expr.new]p15: |
| 1098 | // A new-expression that creates an object of type T initializes that |
| 1099 | // object as follows: |
| 1100 | InitializationKind Kind |
| 1101 | // - If the new-initializer is omitted, the object is default- |
| 1102 | // initialized (8.5); if no initialization is performed, |
| 1103 | // the object has indeterminate value |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1104 | = !Init? InitializationKind::CreateDefault(TypeRange.getBegin()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1105 | // - Otherwise, the new-initializer is interpreted according to the |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1106 | // initialization rules of 8.5 for direct-initialization. |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1107 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1108 | ConstructorLParen, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1109 | ConstructorRParen); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1110 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1111 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1112 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1113 | InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1114 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1115 | move(ConstructorArgs)); |
| 1116 | if (FullInit.isInvalid()) |
| 1117 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1118 | |
| 1119 | // 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] | 1120 | // constructor call, which CXXNewExpr handles directly. |
| 1121 | if (Expr *FullInitExpr = (Expr *)FullInit.get()) { |
| 1122 | if (CXXBindTemporaryExpr *Binder |
| 1123 | = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr)) |
| 1124 | FullInitExpr = Binder->getSubExpr(); |
| 1125 | if (CXXConstructExpr *Construct |
| 1126 | = dyn_cast<CXXConstructExpr>(FullInitExpr)) { |
| 1127 | Constructor = Construct->getConstructor(); |
| 1128 | for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(), |
| 1129 | AEnd = Construct->arg_end(); |
| 1130 | A != AEnd; ++A) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 1131 | ConvertedConstructorArgs.push_back(*A); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1132 | } else { |
| 1133 | // Take the converted initializer. |
| 1134 | ConvertedConstructorArgs.push_back(FullInit.release()); |
| 1135 | } |
| 1136 | } else { |
| 1137 | // No initialization required. |
| 1138 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1140 | // Take the converted arguments and use them for the new expression. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1141 | NumConsArgs = ConvertedConstructorArgs.size(); |
| 1142 | ConsArgs = (Expr **)ConvertedConstructorArgs.take(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1143 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1144 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1145 | // Mark the new and delete operators as referenced. |
| 1146 | if (OperatorNew) |
| 1147 | MarkDeclarationReferenced(StartLoc, OperatorNew); |
| 1148 | if (OperatorDelete) |
| 1149 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
| 1150 | |
John McCall | 84ff0fc | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 1151 | // C++0x [expr.new]p17: |
| 1152 | // If the new expression creates an array of objects of class type, |
| 1153 | // access and ambiguity control are done for the destructor. |
| 1154 | if (ArraySize && Constructor) { |
| 1155 | if (CXXDestructorDecl *dtor = LookupDestructor(Constructor->getParent())) { |
| 1156 | MarkDeclarationReferenced(StartLoc, dtor); |
| 1157 | CheckDestructorAccess(StartLoc, dtor, |
| 1158 | PDiag(diag::err_access_dtor) |
| 1159 | << Context.getBaseElementType(AllocType)); |
| 1160 | } |
| 1161 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1162 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1163 | PlacementArgs.release(); |
| 1164 | ConstructorArgs.release(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1165 | |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1166 | return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1167 | PlaceArgs, NumPlaceArgs, TypeIdParens, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1168 | ArraySize, Constructor, Init, |
| 1169 | ConsArgs, NumConsArgs, OperatorDelete, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1170 | UsualArrayDeleteWantsSize, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1171 | ResultType, AllocTypeInfo, |
| 1172 | StartLoc, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1173 | Init ? ConstructorRParen : |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 1174 | TypeRange.getEnd(), |
| 1175 | ConstructorLParen, ConstructorRParen)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 1179 | /// in a new-expression. |
| 1180 | /// dimension off and stores the size expression in ArraySize. |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1181 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | SourceRange R) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1183 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 1184 | // abstract class type or array thereof. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1185 | if (AllocType->isFunctionType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1186 | return Diag(Loc, diag::err_bad_new_type) |
| 1187 | << AllocType << 0 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1188 | else if (AllocType->isReferenceType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1189 | return Diag(Loc, diag::err_bad_new_type) |
| 1190 | << AllocType << 1 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1191 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1192 | RequireCompleteType(Loc, AllocType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1193 | PDiag(diag::err_new_incomplete_type) |
| 1194 | << R)) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1195 | return true; |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1196 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1197 | diag::err_allocation_of_abstract_type)) |
| 1198 | return true; |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1199 | else if (AllocType->isVariablyModifiedType()) |
| 1200 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 1201 | << AllocType; |
Douglas Gregor | 5666d36 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1202 | else if (unsigned AddressSpace = AllocType.getAddressSpace()) |
| 1203 | return Diag(Loc, diag::err_address_space_qualified_new) |
| 1204 | << AllocType.getUnqualifiedType() << AddressSpace; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1205 | else if (getLangOptions().ObjCAutoRefCount) { |
| 1206 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { |
| 1207 | QualType BaseAllocType = Context.getBaseElementType(AT); |
| 1208 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && |
| 1209 | BaseAllocType->isObjCLifetimeType()) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 1210 | return Diag(Loc, diag::err_arc_new_array_without_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1211 | << BaseAllocType; |
| 1212 | } |
| 1213 | } |
Douglas Gregor | 5666d36 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1214 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1215 | return false; |
| 1216 | } |
| 1217 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1218 | /// \brief Determine whether the given function is a non-placement |
| 1219 | /// deallocation function. |
| 1220 | static bool isNonPlacementDeallocationFunction(FunctionDecl *FD) { |
| 1221 | if (FD->isInvalidDecl()) |
| 1222 | return false; |
| 1223 | |
| 1224 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 1225 | return Method->isUsualDeallocationFunction(); |
| 1226 | |
| 1227 | return ((FD->getOverloadedOperator() == OO_Delete || |
| 1228 | FD->getOverloadedOperator() == OO_Array_Delete) && |
| 1229 | FD->getNumParams() == 1); |
| 1230 | } |
| 1231 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1232 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 1233 | /// that are appropriate for the allocation. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1234 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 1235 | bool UseGlobal, QualType AllocType, |
| 1236 | bool IsArray, Expr **PlaceArgs, |
| 1237 | unsigned NumPlaceArgs, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1238 | FunctionDecl *&OperatorNew, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1240 | // --- Choosing an allocation function --- |
| 1241 | // C++ 5.3.4p8 - 14 & 18 |
| 1242 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 1243 | // in the scope of the allocated class. |
| 1244 | // 2) If an array size is given, look for operator new[], else look for |
| 1245 | // operator new. |
| 1246 | // 3) The first argument is always size_t. Append the arguments from the |
| 1247 | // placement form. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1248 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1249 | SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1250 | // We don't care about the actual value of this argument. |
| 1251 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 1252 | // tree? Or should the consumer just recalculate the value? |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1253 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1254 | Context.Target.getPointerWidth(0)), |
| 1255 | Context.getSizeType(), |
| 1256 | SourceLocation()); |
| 1257 | AllocArgs[0] = &Size; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1258 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 1259 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1260 | // C++ [expr.new]p8: |
| 1261 | // If the allocated type is a non-array type, the allocation |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1262 | // function's name is operator new and the deallocation function's |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1263 | // name is operator delete. If the allocated type is an array |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1264 | // type, the allocation function's name is operator new[] and the |
| 1265 | // deallocation function's name is operator delete[]. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1266 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 1267 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1268 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1269 | IsArray ? OO_Array_Delete : OO_Delete); |
| 1270 | |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1271 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 1272 | |
| 1273 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | CXXRecordDecl *Record |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1275 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1276 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1277 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 1278 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1279 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1280 | } |
| 1281 | if (!OperatorNew) { |
| 1282 | // Didn't find a member overload. Look for a global one. |
| 1283 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1284 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1285 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1286 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 1287 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1288 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1291 | // We don't need an operator delete if we're running under |
| 1292 | // -fno-exceptions. |
| 1293 | if (!getLangOptions().Exceptions) { |
| 1294 | OperatorDelete = 0; |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 1298 | // FindAllocationOverload can change the passed in arguments, so we need to |
| 1299 | // copy them back. |
| 1300 | if (NumPlaceArgs > 0) |
| 1301 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1303 | // C++ [expr.new]p19: |
| 1304 | // |
| 1305 | // If the new-expression begins with a unary :: operator, the |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1306 | // deallocation function's name is looked up in the global |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1307 | // 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] | 1308 | // array thereof, the deallocation function's name is looked up in |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1309 | // the scope of T. If this lookup fails to find the name, or if |
| 1310 | // the allocated type is not a class type or array thereof, the |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1311 | // deallocation function's name is looked up in the global scope. |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1312 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1313 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1314 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1315 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1316 | LookupQualifiedName(FoundDelete, RD); |
| 1317 | } |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1318 | if (FoundDelete.isAmbiguous()) |
| 1319 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1320 | |
| 1321 | if (FoundDelete.empty()) { |
| 1322 | DeclareGlobalNewDelete(); |
| 1323 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 1324 | } |
| 1325 | |
| 1326 | FoundDelete.suppressDiagnostics(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1327 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1328 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1329 | |
John McCall | edeb6c9 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 1330 | // Whether we're looking for a placement operator delete is dictated |
| 1331 | // by whether we selected a placement operator new, not by whether |
| 1332 | // we had explicit placement arguments. This matters for things like |
| 1333 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 1334 | // A *a = new A() |
| 1335 | bool isPlacementNew = (NumPlaceArgs > 0 || OperatorNew->param_size() != 1); |
| 1336 | |
| 1337 | if (isPlacementNew) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1338 | // C++ [expr.new]p20: |
| 1339 | // A declaration of a placement deallocation function matches the |
| 1340 | // declaration of a placement allocation function if it has the |
| 1341 | // same number of parameters and, after parameter transformations |
| 1342 | // (8.3.5), all parameter types except the first are |
| 1343 | // identical. [...] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1344 | // |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1345 | // To perform this comparison, we compute the function type that |
| 1346 | // the deallocation function should have, and use that type both |
| 1347 | // for template argument deduction and for comparison purposes. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1348 | // |
| 1349 | // FIXME: this comparison should ignore CC and the like. |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1350 | QualType ExpectedFunctionType; |
| 1351 | { |
| 1352 | const FunctionProtoType *Proto |
| 1353 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1355 | SmallVector<QualType, 4> ArgTypes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1356 | ArgTypes.push_back(Context.VoidPtrTy); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1357 | for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I) |
| 1358 | ArgTypes.push_back(Proto->getArgType(I)); |
| 1359 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1360 | FunctionProtoType::ExtProtoInfo EPI; |
| 1361 | EPI.Variadic = Proto->isVariadic(); |
| 1362 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1363 | ExpectedFunctionType |
| 1364 | = Context.getFunctionType(Context.VoidTy, ArgTypes.data(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1365 | ArgTypes.size(), EPI); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1368 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1369 | DEnd = FoundDelete.end(); |
| 1370 | D != DEnd; ++D) { |
| 1371 | FunctionDecl *Fn = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1372 | if (FunctionTemplateDecl *FnTmpl |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1373 | = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
| 1374 | // Perform template argument deduction to try to match the |
| 1375 | // expected function type. |
| 1376 | TemplateDeductionInfo Info(Context, StartLoc); |
| 1377 | if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info)) |
| 1378 | continue; |
| 1379 | } else |
| 1380 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 1381 | |
| 1382 | if (Context.hasSameType(Fn->getType(), ExpectedFunctionType)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1383 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1384 | } |
| 1385 | } else { |
| 1386 | // C++ [expr.new]p20: |
| 1387 | // [...] Any non-placement deallocation function matches a |
| 1388 | // non-placement allocation function. [...] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1389 | for (LookupResult::iterator D = FoundDelete.begin(), |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1390 | DEnd = FoundDelete.end(); |
| 1391 | D != DEnd; ++D) { |
| 1392 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl())) |
| 1393 | if (isNonPlacementDeallocationFunction(Fn)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1394 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | // C++ [expr.new]p20: |
| 1399 | // [...] If the lookup finds a single matching deallocation |
| 1400 | // function, that function will be called; otherwise, no |
| 1401 | // deallocation function will be called. |
| 1402 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1403 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1404 | |
| 1405 | // C++0x [expr.new]p20: |
| 1406 | // If the lookup finds the two-parameter form of a usual |
| 1407 | // deallocation function (3.7.4.2) and that function, considered |
| 1408 | // as a placement deallocation function, would have been |
| 1409 | // selected as a match for the allocation function, the program |
| 1410 | // is ill-formed. |
| 1411 | if (NumPlaceArgs && getLangOptions().CPlusPlus0x && |
| 1412 | isNonPlacementDeallocationFunction(OperatorDelete)) { |
| 1413 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1414 | << SourceRange(PlaceArgs[0]->getLocStart(), |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1415 | PlaceArgs[NumPlaceArgs - 1]->getLocEnd()); |
| 1416 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 1417 | << DeleteName; |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1418 | } else { |
| 1419 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1420 | Matches[0].first); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1424 | return false; |
| 1425 | } |
| 1426 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1427 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 1428 | /// function in the specified scope. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1429 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 1430 | DeclarationName Name, Expr** Args, |
| 1431 | unsigned NumArgs, DeclContext *Ctx, |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1432 | bool AllowMissing, FunctionDecl *&Operator, |
| 1433 | bool Diagnose) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1434 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); |
| 1435 | LookupQualifiedName(R, Ctx); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1436 | if (R.empty()) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1437 | if (AllowMissing || !Diagnose) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1438 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1439 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1440 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1443 | if (R.isAmbiguous()) |
| 1444 | return true; |
| 1445 | |
| 1446 | R.suppressDiagnostics(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1447 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1448 | OverloadCandidateSet Candidates(StartLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1449 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 1450 | Alloc != AllocEnd; ++Alloc) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1451 | // Even member operator new/delete are implicitly treated as |
| 1452 | // static, so don't use AddMemberCandidate. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1453 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1454 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1455 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 1456 | AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1457 | /*ExplicitTemplateArgs=*/0, Args, NumArgs, |
| 1458 | Candidates, |
| 1459 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1460 | continue; |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1463 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 1464 | AddOverloadCandidate(Fn, Alloc.getPair(), Args, NumArgs, Candidates, |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1465 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | // Do the resolution. |
| 1469 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1470 | switch (Candidates.BestViableFunction(*this, StartLoc, Best)) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1471 | case OR_Success: { |
| 1472 | // Got one! |
| 1473 | FunctionDecl *FnDecl = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 1474 | MarkDeclarationReferenced(StartLoc, FnDecl); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1475 | // The first argument is size_t, and the first parameter must be size_t, |
| 1476 | // too. This is checked on declaration and can be assumed. (It can't be |
| 1477 | // asserted on, though, since invalid decls are left in there.) |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1478 | // Watch out for variadic allocator function. |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1479 | unsigned NumArgsInFnDecl = FnDecl->getNumParams(); |
| 1480 | for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1481 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 1482 | FnDecl->getParamDecl(i)); |
| 1483 | |
| 1484 | if (!Diagnose && !CanPerformCopyInitialization(Entity, Owned(Args[i]))) |
| 1485 | return true; |
| 1486 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1487 | ExprResult Result |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1488 | = PerformCopyInitialization(Entity, SourceLocation(), Owned(Args[i])); |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1489 | if (Result.isInvalid()) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1490 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1491 | |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1492 | Args[i] = Result.takeAs<Expr>(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1493 | } |
| 1494 | Operator = FnDecl; |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1495 | CheckAllocationAccess(StartLoc, Range, R.getNamingClass(), Best->FoundDecl, |
| 1496 | Diagnose); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1497 | return false; |
| 1498 | } |
| 1499 | |
| 1500 | case OR_No_Viable_Function: |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1501 | if (Diagnose) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1502 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
| 1503 | << Name << Range; |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1504 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 1505 | } |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1506 | return true; |
| 1507 | |
| 1508 | case OR_Ambiguous: |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1509 | if (Diagnose) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1510 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
| 1511 | << Name << Range; |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1512 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
| 1513 | } |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1514 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1516 | case OR_Deleted: { |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1517 | if (Diagnose) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1518 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 1519 | << Best->Function->isDeleted() |
| 1520 | << Name |
| 1521 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 1522 | << Range; |
Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1523 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 1524 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1525 | return true; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1526 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1527 | } |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1528 | assert(false && "Unreachable, bad result from BestViableFunction"); |
| 1529 | return true; |
| 1530 | } |
| 1531 | |
| 1532 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1533 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 1534 | /// delete. These are: |
| 1535 | /// @code |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1536 | /// // C++03: |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1537 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 1538 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1539 | /// void operator delete(void *) throw(); |
| 1540 | /// void operator delete[](void *) throw(); |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1541 | /// // C++0x: |
| 1542 | /// void* operator new(std::size_t); |
| 1543 | /// void* operator new[](std::size_t); |
| 1544 | /// void operator delete(void *); |
| 1545 | /// void operator delete[](void *); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1546 | /// @endcode |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1547 | /// C++0x operator delete is implicitly noexcept. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1548 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 1549 | /// declared. Their use requires including \<new\>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1551 | if (GlobalNewDeleteDeclared) |
| 1552 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1553 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1554 | // C++ [basic.std.dynamic]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1555 | // [...] The following allocation and deallocation functions (18.4) are |
| 1556 | // implicitly declared in global scope in each translation unit of a |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1557 | // program |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1558 | // |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1559 | // C++03: |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1560 | // void* operator new(std::size_t) throw(std::bad_alloc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1561 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1562 | // void operator delete(void*) throw(); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1563 | // void operator delete[](void*) throw(); |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1564 | // C++0x: |
| 1565 | // void* operator new(std::size_t); |
| 1566 | // void* operator new[](std::size_t); |
| 1567 | // void operator delete(void*); |
| 1568 | // void operator delete[](void*); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1569 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1570 | // These implicit declarations introduce only the function names operator |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1571 | // new, operator new[], operator delete, operator delete[]. |
| 1572 | // |
| 1573 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 1574 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 1575 | // However, we do not make these implicit declarations visible to name |
| 1576 | // lookup. |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1577 | // Note that the C++0x versions of operator delete are deallocation functions, |
| 1578 | // and thus are implicitly noexcept. |
| 1579 | if (!StdBadAlloc && !getLangOptions().CPlusPlus0x) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1580 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 1581 | // implicitly. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1582 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
| 1583 | getOrCreateStdNamespace(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1584 | SourceLocation(), SourceLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1585 | &PP.getIdentifierTable().get("bad_alloc"), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1586 | 0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1587 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1588 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1589 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1590 | GlobalNewDeleteDeclared = true; |
| 1591 | |
| 1592 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 1593 | QualType SizeT = Context.getSizeType(); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1594 | bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1595 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1596 | DeclareGlobalAllocationFunction( |
| 1597 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1598 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1599 | DeclareGlobalAllocationFunction( |
| 1600 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1601 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1602 | DeclareGlobalAllocationFunction( |
| 1603 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 1604 | Context.VoidTy, VoidPtr); |
| 1605 | DeclareGlobalAllocationFunction( |
| 1606 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 1607 | Context.VoidTy, VoidPtr); |
| 1608 | } |
| 1609 | |
| 1610 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 1611 | /// allocation function if it doesn't already exist. |
| 1612 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1613 | QualType Return, QualType Argument, |
| 1614 | bool AddMallocAttr) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1615 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 1616 | |
| 1617 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1618 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 1619 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1620 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1621 | Alloc != AllocEnd; ++Alloc) { |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1622 | // Only look at non-template functions, as it is the predefined, |
| 1623 | // non-templated allocation function we are trying to declare here. |
| 1624 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
| 1625 | QualType InitialParamType = |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 1626 | Context.getCanonicalType( |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1627 | Func->getParamDecl(0)->getType().getUnqualifiedType()); |
| 1628 | // FIXME: Do we need to check for default arguments here? |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1629 | if (Func->getNumParams() == 1 && InitialParamType == Argument) { |
| 1630 | if(AddMallocAttr && !Func->hasAttr<MallocAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1631 | Func->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1632 | return; |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1633 | } |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1634 | } |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1638 | QualType BadAllocType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1639 | bool HasBadAllocExceptionSpec |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1640 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 1641 | Name.getCXXOverloadedOperator() == OO_Array_New); |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1642 | if (HasBadAllocExceptionSpec && !getLangOptions().CPlusPlus0x) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1643 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1644 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1645 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1646 | |
| 1647 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1648 | if (HasBadAllocExceptionSpec) { |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1649 | if (!getLangOptions().CPlusPlus0x) { |
| 1650 | EPI.ExceptionSpecType = EST_Dynamic; |
| 1651 | EPI.NumExceptions = 1; |
| 1652 | EPI.Exceptions = &BadAllocType; |
| 1653 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1654 | } else { |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1655 | EPI.ExceptionSpecType = getLangOptions().CPlusPlus0x ? |
| 1656 | EST_BasicNoexcept : EST_DynamicNone; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1657 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1658 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1659 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, EPI); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1660 | FunctionDecl *Alloc = |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1661 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), |
| 1662 | SourceLocation(), Name, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1663 | FnType, /*TInfo=*/0, SC_None, |
| 1664 | SC_None, false, true); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1665 | Alloc->setImplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1666 | |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1667 | if (AddMallocAttr) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1668 | Alloc->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1669 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1670 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1671 | SourceLocation(), 0, |
| 1672 | Argument, /*TInfo=*/0, |
| 1673 | SC_None, SC_None, 0); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1674 | Alloc->setParams(&Param, 1); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1675 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1676 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 1677 | // make sure it is at the end of the chain to coincide with the |
| 1678 | // global scope. |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1679 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1682 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 1683 | DeclarationName Name, |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1684 | FunctionDecl* &Operator, bool Diagnose) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1685 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1686 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1687 | LookupQualifiedName(Found, RD); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1688 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1689 | if (Found.isAmbiguous()) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1690 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1691 | |
Chandler Carruth | 2389324 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 1692 | Found.suppressDiagnostics(); |
| 1693 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1694 | SmallVector<DeclAccessPair,4> Matches; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1695 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1696 | F != FEnd; ++F) { |
Chandler Carruth | 09556fd | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 1697 | NamedDecl *ND = (*F)->getUnderlyingDecl(); |
| 1698 | |
| 1699 | // Ignore template operator delete members from the check for a usual |
| 1700 | // deallocation function. |
| 1701 | if (isa<FunctionTemplateDecl>(ND)) |
| 1702 | continue; |
| 1703 | |
| 1704 | if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction()) |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1705 | Matches.push_back(F.getPair()); |
| 1706 | } |
| 1707 | |
| 1708 | // There's exactly one suitable operator; pick it. |
| 1709 | if (Matches.size() == 1) { |
| 1710 | Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl()); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1711 | |
| 1712 | if (Operator->isDeleted()) { |
| 1713 | if (Diagnose) { |
| 1714 | Diag(StartLoc, diag::err_deleted_function_use); |
| 1715 | Diag(Operator->getLocation(), diag::note_unavailable_here) << true; |
| 1716 | } |
| 1717 | return true; |
| 1718 | } |
| 1719 | |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1720 | CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1721 | Matches[0], Diagnose); |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1722 | return false; |
| 1723 | |
| 1724 | // We found multiple suitable operators; complain about the ambiguity. |
| 1725 | } else if (!Matches.empty()) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1726 | if (Diagnose) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1727 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 1728 | << Name << RD; |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1729 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1730 | for (SmallVectorImpl<DeclAccessPair>::iterator |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1731 | F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F) |
| 1732 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1733 | diag::note_member_declared_here) << Name; |
| 1734 | } |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1735 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | // We did find operator delete/operator delete[] declarations, but |
| 1739 | // none of them were suitable. |
| 1740 | if (!Found.empty()) { |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1741 | if (Diagnose) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1742 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 1743 | << Name << RD; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1744 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1745 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1746 | F != FEnd; ++F) |
| 1747 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1748 | diag::note_member_declared_here) << Name; |
| 1749 | } |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1750 | return true; |
| 1751 | } |
| 1752 | |
| 1753 | // Look for a global declaration. |
| 1754 | DeclareGlobalNewDelete(); |
| 1755 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1756 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1757 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); |
| 1758 | Expr* DeallocArgs[1]; |
| 1759 | DeallocArgs[0] = &Null; |
| 1760 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1761 | DeallocArgs, 1, TUDecl, !Diagnose, |
| 1762 | Operator, Diagnose)) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1763 | return true; |
| 1764 | |
| 1765 | assert(Operator && "Did not find a deallocation function!"); |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1769 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 1770 | /// @code ::delete ptr; @endcode |
| 1771 | /// or |
| 1772 | /// @code delete [] ptr; @endcode |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1773 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1774 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1775 | bool ArrayForm, Expr *ExE) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1776 | // C++ [expr.delete]p1: |
| 1777 | // The operand shall have a pointer type, or a class type having a single |
| 1778 | // conversion function to a pointer type. The result has type void. |
| 1779 | // |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1780 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 1781 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1782 | ExprResult Ex = Owned(ExE); |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1783 | FunctionDecl *OperatorDelete = 0; |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1784 | bool ArrayFormAsWritten = ArrayForm; |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1785 | bool UsualArrayDeleteWantsSize = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1787 | if (!Ex.get()->isTypeDependent()) { |
| 1788 | QualType Type = Ex.get()->getType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1790 | if (const RecordType *Record = Type->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1791 | if (RequireCompleteType(StartLoc, Type, |
Douglas Gregor | 254a942 | 2010-07-29 14:44:35 +0000 | [diff] [blame] | 1792 | PDiag(diag::err_delete_incomplete_class_type))) |
| 1793 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1794 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1795 | SmallVector<CXXConversionDecl*, 4> ObjectPtrConversions; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1796 | |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1797 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1798 | const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1799 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1800 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1801 | NamedDecl *D = I.getDecl(); |
| 1802 | if (isa<UsingShadowDecl>(D)) |
| 1803 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1804 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1805 | // Skip over templated conversion functions; they aren't considered. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1806 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1807 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1808 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1809 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1810 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1811 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 1812 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1813 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1814 | ObjectPtrConversions.push_back(Conv); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1815 | } |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1816 | if (ObjectPtrConversions.size() == 1) { |
| 1817 | // We have a single conversion to a pointer-to-object type. Perform |
| 1818 | // that conversion. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1819 | // TODO: don't redo the conversion calculation. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1820 | ExprResult Res = |
| 1821 | PerformImplicitConversion(Ex.get(), |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1822 | ObjectPtrConversions.front()->getConversionType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1823 | AA_Converting); |
| 1824 | if (Res.isUsable()) { |
| 1825 | Ex = move(Res); |
| 1826 | Type = Ex.get()->getType(); |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | else if (ObjectPtrConversions.size() > 1) { |
| 1830 | Diag(StartLoc, diag::err_ambiguous_delete_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1831 | << Type << Ex.get()->getSourceRange(); |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1832 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) |
| 1833 | NoteOverloadCandidate(ObjectPtrConversions[i]); |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1834 | return ExprError(); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1835 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1838 | if (!Type->isPointerType()) |
| 1839 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1840 | << Type << Ex.get()->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1841 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1842 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1843 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 1844 | |
| 1845 | if (unsigned AddressSpace = Pointee.getAddressSpace()) |
| 1846 | return Diag(Ex.get()->getLocStart(), |
| 1847 | diag::err_address_space_qualified_delete) |
| 1848 | << Pointee.getUnqualifiedType() << AddressSpace; |
| 1849 | |
| 1850 | CXXRecordDecl *PointeeRD = 0; |
Douglas Gregor | 94a6157 | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 1851 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1852 | // 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] | 1853 | // effectively bans deletion of "void*". However, most compilers support |
| 1854 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 1855 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1856 | << Type << Ex.get()->getSourceRange(); |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1857 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1858 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1859 | << Type << Ex.get()->getSourceRange()); |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1860 | } else if (!Pointee->isDependentType()) { |
| 1861 | if (!RequireCompleteType(StartLoc, Pointee, |
| 1862 | PDiag(diag::warn_delete_incomplete) |
| 1863 | << Ex.get()->getSourceRange())) { |
| 1864 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) |
| 1865 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1866 | } |
| 1867 | } |
| 1868 | |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 1869 | // C++ [expr.delete]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1870 | // [Note: a pointer to a const type can be the operand of a |
| 1871 | // delete-expression; it is not necessary to cast away the constness |
| 1872 | // (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] | 1873 | // of the delete-expression. ] |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1874 | if (!Context.hasSameType(Ex.get()->getType(), Context.VoidPtrTy)) |
| 1875 | Ex = Owned(ImplicitCastExpr::Create(Context, Context.VoidPtrTy, CK_NoOp, |
| 1876 | Ex.take(), 0, VK_RValue)); |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1877 | |
| 1878 | if (Pointee->isArrayType() && !ArrayForm) { |
| 1879 | Diag(StartLoc, diag::warn_delete_array_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1880 | << Type << Ex.get()->getSourceRange() |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1881 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]"); |
| 1882 | ArrayForm = true; |
| 1883 | } |
| 1884 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1885 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1886 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 1887 | |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1888 | if (PointeeRD) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1889 | if (!UseGlobal && |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1890 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, |
| 1891 | OperatorDelete)) |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1892 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1893 | |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1894 | // If we're allocating an array of records, check whether the |
| 1895 | // usual operator delete[] has a size_t parameter. |
| 1896 | if (ArrayForm) { |
| 1897 | // If the user specifically asked to use the global allocator, |
| 1898 | // we'll need to do the lookup into the class. |
| 1899 | if (UseGlobal) |
| 1900 | UsualArrayDeleteWantsSize = |
| 1901 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); |
| 1902 | |
| 1903 | // Otherwise, the usual operator delete[] should be the |
| 1904 | // function we just found. |
| 1905 | else if (isa<CXXMethodDecl>(OperatorDelete)) |
| 1906 | UsualArrayDeleteWantsSize = (OperatorDelete->getNumParams() == 2); |
| 1907 | } |
| 1908 | |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1909 | if (!PointeeRD->hasTrivialDestructor()) |
| 1910 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | MarkDeclarationReferenced(StartLoc, |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1912 | const_cast<CXXDestructorDecl*>(Dtor)); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1913 | DiagnoseUseOfDecl(Dtor, StartLoc); |
| 1914 | } |
Argyrios Kyrtzidis | 6f0074a | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 1915 | |
| 1916 | // C++ [expr.delete]p3: |
| 1917 | // In the first alternative (delete object), if the static type of the |
| 1918 | // object to be deleted is different from its dynamic type, the static |
| 1919 | // type shall be a base class of the dynamic type of the object to be |
| 1920 | // deleted and the static type shall have a virtual destructor or the |
| 1921 | // behavior is undefined. |
| 1922 | // |
| 1923 | // Note: a final class cannot be derived from, no issue there |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1924 | if (!ArrayForm && PointeeRD->isPolymorphic() && |
| 1925 | !PointeeRD->hasAttr<FinalAttr>()) { |
| 1926 | CXXDestructorDecl *dtor = PointeeRD->getDestructor(); |
Argyrios Kyrtzidis | 6f0074a | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 1927 | if (!dtor || !dtor->isVirtual()) |
| 1928 | Diag(StartLoc, diag::warn_delete_non_virtual_dtor) << PointeeElem; |
| 1929 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1930 | |
| 1931 | } else if (getLangOptions().ObjCAutoRefCount && |
| 1932 | PointeeElem->isObjCLifetimeType() && |
| 1933 | (PointeeElem.getObjCLifetime() == Qualifiers::OCL_Strong || |
| 1934 | PointeeElem.getObjCLifetime() == Qualifiers::OCL_Weak) && |
| 1935 | ArrayForm) { |
| 1936 | Diag(StartLoc, diag::warn_err_new_delete_object_array) |
| 1937 | << 1 << PointeeElem; |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1938 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1939 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1940 | if (!OperatorDelete) { |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1941 | // Look for a global declaration. |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1942 | DeclareGlobalNewDelete(); |
| 1943 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1944 | Expr *Arg = Ex.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1946 | &Arg, 1, TUDecl, /*AllowMissing=*/false, |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1947 | OperatorDelete)) |
| 1948 | return ExprError(); |
| 1949 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1951 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1952 | |
Douglas Gregor | d880f52 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 1953 | // Check access and ambiguity of operator delete and destructor. |
Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1954 | if (PointeeRD) { |
| 1955 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1956 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, |
Douglas Gregor | d880f52 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 1957 | PDiag(diag::err_access_dtor) << PointeeElem); |
| 1958 | } |
| 1959 | } |
| 1960 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1963 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1964 | ArrayFormAsWritten, |
| 1965 | UsualArrayDeleteWantsSize, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1966 | OperatorDelete, Ex.take(), StartLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1969 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 1970 | /// while, do-while, or switch statement. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1971 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1972 | SourceLocation StmtLoc, |
| 1973 | bool ConvertToBoolean) { |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1974 | QualType T = ConditionVar->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1976 | // C++ [stmt.select]p2: |
| 1977 | // The declarator shall not specify a function or an array. |
| 1978 | if (T->isFunctionType()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1979 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1980 | diag::err_invalid_use_of_function_type) |
| 1981 | << ConditionVar->getSourceRange()); |
| 1982 | else if (T->isArrayType()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1983 | return ExprError(Diag(ConditionVar->getLocation(), |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1984 | diag::err_invalid_use_of_array_type) |
| 1985 | << ConditionVar->getSourceRange()); |
Douglas Gregor | a7605db | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 1986 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1987 | ExprResult Condition = |
| 1988 | Owned(DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1989 | ConditionVar, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1990 | ConditionVar->getLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1991 | ConditionVar->getType().getNonReferenceType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1992 | VK_LValue)); |
| 1993 | if (ConvertToBoolean) { |
| 1994 | Condition = CheckBooleanCondition(Condition.take(), StmtLoc); |
| 1995 | if (Condition.isInvalid()) |
| 1996 | return ExprError(); |
| 1997 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1998 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1999 | return move(Condition); |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2002 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2003 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr) { |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2004 | // C++ 6.4p4: |
| 2005 | // The value of a condition that is an initialized declaration in a statement |
| 2006 | // other than a switch statement is the value of the declared variable |
| 2007 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 2008 | // program is ill-formed. |
| 2009 | // The value of a condition that is an expression is the value of the |
| 2010 | // expression, implicitly converted to bool. |
| 2011 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2012 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2013 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2014 | |
| 2015 | /// Helper function to determine whether this is the (deprecated) C++ |
| 2016 | /// conversion from a string literal to a pointer to non-const char or |
| 2017 | /// non-const wchar_t (for narrow and wide string literals, |
| 2018 | /// respectively). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | bool |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2020 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 2021 | // Look inside the implicit cast, if it exists. |
| 2022 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 2023 | From = Cast->getSubExpr(); |
| 2024 | |
| 2025 | // A string literal (2.13.4) that is not a wide string literal can |
| 2026 | // be converted to an rvalue of type "pointer to char"; a wide |
| 2027 | // string literal can be converted to an rvalue of type "pointer |
| 2028 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 1984eb9 | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 2029 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2030 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | if (const BuiltinType *ToPointeeType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2032 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2033 | // This conversion is considered only when there is an |
| 2034 | // explicit appropriate pointer target type (C++ 4.2p2). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2035 | if (!ToPtrType->getPointeeType().hasQualifiers() && |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2036 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 2037 | (!StrLit->isWide() && |
| 2038 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 2039 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 2040 | return true; |
| 2041 | } |
| 2042 | |
| 2043 | return false; |
| 2044 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2045 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2046 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2047 | SourceLocation CastLoc, |
| 2048 | QualType Ty, |
| 2049 | CastKind Kind, |
| 2050 | CXXMethodDecl *Method, |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2051 | NamedDecl *FoundDecl, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2052 | Expr *From) { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2053 | switch (Kind) { |
| 2054 | default: assert(0 && "Unhandled cast kind!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2055 | case CK_ConstructorConversion: { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2056 | ASTOwningVector<Expr*> ConstructorArgs(S); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2057 | |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2058 | if (S.CompleteConstructorCall(cast<CXXConstructorDecl>(Method), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2059 | MultiExprArg(&From, 1), |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2060 | CastLoc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2061 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2062 | |
| 2063 | ExprResult Result = |
| 2064 | S.BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 2065 | move_arg(ConstructorArgs), |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 2066 | /*ZeroInit*/ false, CXXConstructExpr::CK_Complete, |
| 2067 | SourceRange()); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2068 | if (Result.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2069 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2070 | |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2071 | return S.MaybeBindToTemporary(Result.takeAs<Expr>()); |
| 2072 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2073 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2074 | case CK_UserDefinedConversion: { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2075 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2076 | |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2077 | // Create an implicit call expr that calls it. |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2078 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Method); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2079 | if (Result.isInvalid()) |
| 2080 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2082 | return S.MaybeBindToTemporary(Result.get()); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2083 | } |
| 2084 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2085 | } |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2086 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2087 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 2088 | /// expression From to the type ToType using the pre-computed implicit |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2089 | /// conversion sequence ICS. Returns the converted |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2090 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2091 | /// used in the error message. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2092 | ExprResult |
| 2093 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2094 | const ImplicitConversionSequence &ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2095 | AssignmentAction Action, |
| 2096 | CheckedConversionKind CCK) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2097 | switch (ICS.getKind()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2098 | case ImplicitConversionSequence::StandardConversion: { |
| 2099 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2100 | Action, CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2101 | if (Res.isInvalid()) |
| 2102 | return ExprError(); |
| 2103 | From = Res.take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2104 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2105 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2106 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2107 | case ImplicitConversionSequence::UserDefinedConversion: { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2108 | |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 2109 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2110 | CastKind CastKind; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2111 | QualType BeforeToType; |
| 2112 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2113 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2114 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2115 | // If the user-defined conversion is specified by a conversion function, |
| 2116 | // the initial standard conversion sequence converts the source type to |
| 2117 | // the implicit object parameter of the conversion function. |
| 2118 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
John McCall | 9ec9445 | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 2119 | } else { |
| 2120 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2121 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2122 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2123 | if (!ICS.UserDefined.EllipsisConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2124 | // If the user-defined conversion is specified by a constructor, the |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2125 | // initial standard conversion sequence converts the source type to the |
| 2126 | // type required by the argument of the constructor |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2127 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 2128 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2129 | } |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2130 | // Watch out for elipsis conversion. |
Fariborz Jahanian | 4c0cea2 | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 2131 | if (!ICS.UserDefined.EllipsisConversion) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2132 | ExprResult Res = |
| 2133 | PerformImplicitConversion(From, BeforeToType, |
| 2134 | ICS.UserDefined.Before, AA_Converting, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2135 | CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2136 | if (Res.isInvalid()) |
| 2137 | return ExprError(); |
| 2138 | From = Res.take(); |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2139 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2140 | |
| 2141 | ExprResult CastArg |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2142 | = BuildCXXCastArgument(*this, |
| 2143 | From->getLocStart(), |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2144 | ToType.getNonReferenceType(), |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2145 | CastKind, cast<CXXMethodDecl>(FD), |
| 2146 | ICS.UserDefined.FoundConversionFunction, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2147 | From); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2148 | |
| 2149 | if (CastArg.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2150 | return ExprError(); |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2151 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2152 | From = CastArg.take(); |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2153 | |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2154 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2155 | AA_Converting, CCK); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 2156 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2157 | |
| 2158 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2159 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2160 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 2161 | << From->getSourceRange()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2162 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2163 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2164 | case ImplicitConversionSequence::EllipsisConversion: |
| 2165 | assert(false && "Cannot perform an ellipsis conversion"); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2166 | return Owned(From); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2167 | |
| 2168 | case ImplicitConversionSequence::BadConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2169 | return ExprError(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | // Everything went well. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2173 | return Owned(From); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 2177 | /// expression From to the type ToType by following the standard |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2178 | /// conversion sequence SCS. Returns the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2179 | /// expression. Flavor is the context in which we're performing this |
| 2180 | /// conversion, for use in error messages. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2181 | ExprResult |
| 2182 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2183 | const StandardConversionSequence& SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2184 | AssignmentAction Action, |
| 2185 | CheckedConversionKind CCK) { |
| 2186 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); |
| 2187 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2188 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 2189 | // much extra work. What this means is that we need to keep track of more |
| 2190 | // information that is computed when we try the implicit conversion initially, |
| 2191 | // so that we don't need to recompute anything here. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2192 | QualType FromType = From->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2193 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2194 | if (SCS.CopyConstructor) { |
Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 2195 | // FIXME: When can ToType be a reference type? |
| 2196 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2197 | if (SCS.Second == ICK_Derived_To_Base) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2198 | ASTOwningVector<Expr*> ConstructorArgs(*this); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2199 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2200 | MultiExprArg(*this, &From, 1), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2201 | /*FIXME:ConstructLoc*/SourceLocation(), |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2202 | ConstructorArgs)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2203 | return ExprError(); |
| 2204 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 2205 | ToType, SCS.CopyConstructor, |
| 2206 | move_arg(ConstructorArgs), |
| 2207 | /*ZeroInit*/ false, |
| 2208 | CXXConstructExpr::CK_Complete, |
| 2209 | SourceRange()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2210 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2211 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 2212 | ToType, SCS.CopyConstructor, |
| 2213 | MultiExprArg(*this, &From, 1), |
| 2214 | /*ZeroInit*/ false, |
| 2215 | CXXConstructExpr::CK_Complete, |
| 2216 | SourceRange()); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2219 | // Resolve overloaded function references. |
| 2220 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 2221 | DeclAccessPair Found; |
| 2222 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 2223 | true, Found); |
| 2224 | if (!Fn) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2225 | return ExprError(); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2226 | |
| 2227 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2228 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2229 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2230 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 2231 | FromType = From->getType(); |
| 2232 | } |
| 2233 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2234 | // Perform the first implicit conversion. |
| 2235 | switch (SCS.First) { |
| 2236 | case ICK_Identity: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2237 | // Nothing to do. |
| 2238 | break; |
| 2239 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2240 | case ICK_Lvalue_To_Rvalue: |
| 2241 | // Should this get its own ICK? |
| 2242 | if (From->getObjectKind() == OK_ObjCProperty) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2243 | ExprResult FromRes = ConvertPropertyForRValue(From); |
| 2244 | if (FromRes.isInvalid()) |
| 2245 | return ExprError(); |
| 2246 | From = FromRes.take(); |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 2247 | if (!From->isGLValue()) break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 2250 | // Check for trivial buffer overflows. |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 2251 | CheckArrayAccess(From); |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 2252 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2253 | FromType = FromType.getUnqualifiedType(); |
| 2254 | From = ImplicitCastExpr::Create(Context, FromType, CK_LValueToRValue, |
| 2255 | From, 0, VK_RValue); |
| 2256 | break; |
| 2257 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2258 | case ICK_Array_To_Pointer: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2259 | FromType = Context.getArrayDecayedType(FromType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2260 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, |
| 2261 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2262 | break; |
| 2263 | |
| 2264 | case ICK_Function_To_Pointer: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2265 | FromType = Context.getPointerType(FromType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2266 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, |
| 2267 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2268 | break; |
| 2269 | |
| 2270 | default: |
| 2271 | assert(false && "Improper first standard conversion"); |
| 2272 | break; |
| 2273 | } |
| 2274 | |
| 2275 | // Perform the second implicit conversion |
| 2276 | switch (SCS.Second) { |
| 2277 | case ICK_Identity: |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2278 | // If both sides are functions (or pointers/references to them), there could |
| 2279 | // be incompatible exception declarations. |
| 2280 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2281 | return ExprError(); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2282 | // Nothing else to do. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2283 | break; |
| 2284 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2285 | case ICK_NoReturn_Adjustment: |
| 2286 | // If both sides are functions (or pointers/references to them), there could |
| 2287 | // be incompatible exception declarations. |
| 2288 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2289 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2290 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2291 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 2292 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2293 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2294 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2295 | case ICK_Integral_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2296 | case ICK_Integral_Conversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2297 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, |
| 2298 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2299 | break; |
| 2300 | |
| 2301 | case ICK_Floating_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2302 | case ICK_Floating_Conversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2303 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, |
| 2304 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2305 | break; |
| 2306 | |
| 2307 | case ICK_Complex_Promotion: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2308 | case ICK_Complex_Conversion: { |
| 2309 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
| 2310 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
| 2311 | CastKind CK; |
| 2312 | if (FromEl->isRealFloatingType()) { |
| 2313 | if (ToEl->isRealFloatingType()) |
| 2314 | CK = CK_FloatingComplexCast; |
| 2315 | else |
| 2316 | CK = CK_FloatingComplexToIntegralComplex; |
| 2317 | } else if (ToEl->isRealFloatingType()) { |
| 2318 | CK = CK_IntegralComplexToFloatingComplex; |
| 2319 | } else { |
| 2320 | CK = CK_IntegralComplexCast; |
| 2321 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2322 | From = ImpCastExprToType(From, ToType, CK, |
| 2323 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2324 | break; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2325 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2326 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2327 | case ICK_Floating_Integral: |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 2328 | if (ToType->isRealFloatingType()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2329 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, |
| 2330 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2331 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2332 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, |
| 2333 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2336 | case ICK_Compatible_Conversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2337 | From = ImpCastExprToType(From, ToType, CK_NoOp, |
| 2338 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2339 | break; |
| 2340 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2341 | case ICK_Writeback_Conversion: |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2342 | case ICK_Pointer_Conversion: { |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2343 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2344 | // Diagnose incompatible Objective-C conversions |
Douglas Gregor | 8cf0d22 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 2345 | if (Action == AA_Initializing || Action == AA_Assigning) |
Fariborz Jahanian | 84950c7 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2346 | Diag(From->getSourceRange().getBegin(), |
| 2347 | diag::ext_typecheck_convert_incompatible_pointer) |
| 2348 | << ToType << From->getType() << Action |
| 2349 | << From->getSourceRange(); |
| 2350 | else |
| 2351 | Diag(From->getSourceRange().getBegin(), |
| 2352 | diag::ext_typecheck_convert_incompatible_pointer) |
| 2353 | << From->getType() << ToType << Action |
| 2354 | << From->getSourceRange(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2356 | if (From->getType()->isObjCObjectPointerType() && |
| 2357 | ToType->isObjCObjectPointerType()) |
| 2358 | EmitRelatedResultTypeNote(From); |
Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2359 | } |
| 2360 | else if (getLangOptions().ObjCAutoRefCount && |
| 2361 | !CheckObjCARCUnavailableWeakConversion(ToType, |
| 2362 | From->getType())) { |
| 2363 | if (Action == AA_Initializing) |
| 2364 | Diag(From->getSourceRange().getBegin(), |
| 2365 | diag::err_arc_weak_unavailable_assign); |
| 2366 | else |
| 2367 | Diag(From->getSourceRange().getBegin(), |
| 2368 | diag::err_arc_convesion_of_weak_unavailable) |
| 2369 | << (Action == AA_Casting) << From->getType() << ToType |
| 2370 | << From->getSourceRange(); |
| 2371 | } |
| 2372 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2373 | CastKind Kind = CK_Invalid; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2374 | CXXCastPath BasePath; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2375 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2376 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2377 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
| 2378 | .take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2379 | break; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2380 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2381 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2382 | case ICK_Pointer_Member: { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2383 | CastKind Kind = CK_Invalid; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2384 | CXXCastPath BasePath; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2385 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2386 | return ExprError(); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2387 | if (CheckExceptionSpecCompatibility(From, ToType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2388 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2389 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) |
| 2390 | .take(); |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2391 | break; |
| 2392 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2393 | |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 2394 | case ICK_Boolean_Conversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2395 | From = ImpCastExprToType(From, Context.BoolTy, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2396 | ScalarTypeToBooleanCastKind(FromType), |
| 2397 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2398 | break; |
| 2399 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2400 | case ICK_Derived_To_Base: { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2401 | CXXCastPath BasePath; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2402 | if (CheckDerivedToBaseConversion(From->getType(), |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2403 | ToType.getNonReferenceType(), |
| 2404 | From->getLocStart(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2405 | From->getSourceRange(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2406 | &BasePath, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2407 | CStyle)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2408 | return ExprError(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2409 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2410 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2411 | CK_DerivedToBase, CastCategory(From), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2412 | &BasePath, CCK).take(); |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2413 | break; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2416 | case ICK_Vector_Conversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2417 | From = ImpCastExprToType(From, ToType, CK_BitCast, |
| 2418 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2419 | break; |
| 2420 | |
| 2421 | case ICK_Vector_Splat: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2422 | From = ImpCastExprToType(From, ToType, CK_VectorSplat, |
| 2423 | VK_RValue, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2424 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2425 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2426 | case ICK_Complex_Real: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2427 | // Case 1. x -> _Complex y |
| 2428 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
| 2429 | QualType ElType = ToComplex->getElementType(); |
| 2430 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2431 | |
| 2432 | // x -> y |
| 2433 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
| 2434 | // do nothing |
| 2435 | } else if (From->getType()->isRealFloatingType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2436 | From = ImpCastExprToType(From, ElType, |
| 2437 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2438 | } else { |
| 2439 | assert(From->getType()->isIntegerType()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2440 | From = ImpCastExprToType(From, ElType, |
| 2441 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2442 | } |
| 2443 | // y -> _Complex y |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2444 | From = ImpCastExprToType(From, ToType, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2445 | isFloatingComplex ? CK_FloatingRealToComplex |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2446 | : CK_IntegralRealToComplex).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2447 | |
| 2448 | // Case 2. _Complex x -> y |
| 2449 | } else { |
| 2450 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
| 2451 | assert(FromComplex); |
| 2452 | |
| 2453 | QualType ElType = FromComplex->getElementType(); |
| 2454 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2455 | |
| 2456 | // _Complex x -> x |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2457 | From = ImpCastExprToType(From, ElType, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2458 | isFloatingComplex ? CK_FloatingComplexToReal |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2459 | : CK_IntegralComplexToReal, |
| 2460 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2461 | |
| 2462 | // x -> y |
| 2463 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
| 2464 | // do nothing |
| 2465 | } else if (ToType->isRealFloatingType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2466 | From = ImpCastExprToType(From, ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2467 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, |
| 2468 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2469 | } else { |
| 2470 | assert(ToType->isIntegerType()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2471 | From = ImpCastExprToType(From, ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2472 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, |
| 2473 | VK_RValue, /*BasePath=*/0, CCK).take(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2474 | } |
| 2475 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2476 | break; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2477 | |
| 2478 | case ICK_Block_Pointer_Conversion: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2479 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, |
| 2480 | VK_RValue, /*BasePath=*/0, CCK).take(); |
| 2481 | break; |
| 2482 | } |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2483 | |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2484 | case ICK_TransparentUnionConversion: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2485 | ExprResult FromRes = Owned(From); |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2486 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2487 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); |
| 2488 | if (FromRes.isInvalid()) |
| 2489 | return ExprError(); |
| 2490 | From = FromRes.take(); |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2491 | assert ((ConvTy == Sema::Compatible) && |
| 2492 | "Improper transparent union conversion"); |
| 2493 | (void)ConvTy; |
| 2494 | break; |
| 2495 | } |
| 2496 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2497 | case ICK_Lvalue_To_Rvalue: |
| 2498 | case ICK_Array_To_Pointer: |
| 2499 | case ICK_Function_To_Pointer: |
| 2500 | case ICK_Qualification: |
| 2501 | case ICK_Num_Conversion_Kinds: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2502 | assert(false && "Improper second standard conversion"); |
| 2503 | break; |
| 2504 | } |
| 2505 | |
| 2506 | switch (SCS.Third) { |
| 2507 | case ICK_Identity: |
| 2508 | // Nothing to do. |
| 2509 | break; |
| 2510 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2511 | case ICK_Qualification: { |
| 2512 | // The qualification keeps the category of the inner expression, unless the |
| 2513 | // target type isn't a reference. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2514 | ExprValueKind VK = ToType->isReferenceType() ? |
| 2515 | CastCategory(From) : VK_RValue; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2516 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2517 | CK_NoOp, VK, /*BasePath=*/0, CCK).take(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2518 | |
Douglas Gregor | 069a6da | 2011-03-14 16:13:32 +0000 | [diff] [blame] | 2519 | if (SCS.DeprecatedStringLiteralToCharPtr && |
| 2520 | !getLangOptions().WritableStrings) |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2521 | Diag(From->getLocStart(), diag::warn_deprecated_string_literal_conversion) |
| 2522 | << ToType.getNonReferenceType(); |
| 2523 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2524 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2527 | default: |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2528 | assert(false && "Improper third standard conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2529 | break; |
| 2530 | } |
| 2531 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2532 | return Owned(From); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2535 | ExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2536 | SourceLocation KWLoc, |
| 2537 | ParsedType Ty, |
| 2538 | SourceLocation RParen) { |
| 2539 | TypeSourceInfo *TSInfo; |
| 2540 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2542 | if (!TSInfo) |
| 2543 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2544 | return BuildUnaryTypeTrait(UTT, KWLoc, TSInfo, RParen); |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2547 | /// \brief Check the completeness of a type in a unary type trait. |
| 2548 | /// |
| 2549 | /// If the particular type trait requires a complete type, tries to complete |
| 2550 | /// it. If completing the type fails, a diagnostic is emitted and false |
| 2551 | /// returned. If completing the type succeeds or no completion was required, |
| 2552 | /// returns true. |
| 2553 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, |
| 2554 | UnaryTypeTrait UTT, |
| 2555 | SourceLocation Loc, |
| 2556 | QualType ArgTy) { |
| 2557 | // C++0x [meta.unary.prop]p3: |
| 2558 | // For all of the class templates X declared in this Clause, instantiating |
| 2559 | // that template with a template argument that is a class template |
| 2560 | // specialization may result in the implicit instantiation of the template |
| 2561 | // argument if and only if the semantics of X require that the argument |
| 2562 | // must be a complete type. |
| 2563 | // We apply this rule to all the type trait expressions used to implement |
| 2564 | // these class templates. We also try to follow any GCC documented behavior |
| 2565 | // in these expressions to ensure portability of standard libraries. |
| 2566 | switch (UTT) { |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2567 | // is_complete_type somewhat obviously cannot require a complete type. |
| 2568 | case UTT_IsCompleteType: |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2569 | // Fall-through |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2570 | |
| 2571 | // These traits are modeled on the type predicates in C++0x |
| 2572 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as |
| 2573 | // requiring a complete type, as whether or not they return true cannot be |
| 2574 | // impacted by the completeness of the type. |
| 2575 | case UTT_IsVoid: |
| 2576 | case UTT_IsIntegral: |
| 2577 | case UTT_IsFloatingPoint: |
| 2578 | case UTT_IsArray: |
| 2579 | case UTT_IsPointer: |
| 2580 | case UTT_IsLvalueReference: |
| 2581 | case UTT_IsRvalueReference: |
| 2582 | case UTT_IsMemberFunctionPointer: |
| 2583 | case UTT_IsMemberObjectPointer: |
| 2584 | case UTT_IsEnum: |
| 2585 | case UTT_IsUnion: |
| 2586 | case UTT_IsClass: |
| 2587 | case UTT_IsFunction: |
| 2588 | case UTT_IsReference: |
| 2589 | case UTT_IsArithmetic: |
| 2590 | case UTT_IsFundamental: |
| 2591 | case UTT_IsObject: |
| 2592 | case UTT_IsScalar: |
| 2593 | case UTT_IsCompound: |
| 2594 | case UTT_IsMemberPointer: |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2595 | // Fall-through |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2596 | |
| 2597 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] |
| 2598 | // which requires some of its traits to have the complete type. However, |
| 2599 | // the completeness of the type cannot impact these traits' semantics, and |
| 2600 | // so they don't require it. This matches the comments on these traits in |
| 2601 | // Table 49. |
| 2602 | case UTT_IsConst: |
| 2603 | case UTT_IsVolatile: |
| 2604 | case UTT_IsSigned: |
| 2605 | case UTT_IsUnsigned: |
| 2606 | return true; |
| 2607 | |
| 2608 | // 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] | 2609 | // applied to a complete type. |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2610 | case UTT_IsTrivial: |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2611 | case UTT_IsTriviallyCopyable: |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2612 | case UTT_IsStandardLayout: |
| 2613 | case UTT_IsPOD: |
| 2614 | case UTT_IsLiteral: |
| 2615 | case UTT_IsEmpty: |
| 2616 | case UTT_IsPolymorphic: |
| 2617 | case UTT_IsAbstract: |
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 | |
Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2620 | // These trait expressions are designed to help implement predicates in |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2621 | // [meta.unary.prop] despite not being named the same. They are specified |
| 2622 | // by both GCC and the Embarcadero C++ compiler, and require the complete |
| 2623 | // type due to the overarching C++0x type predicates being implemented |
| 2624 | // requiring the complete type. |
| 2625 | case UTT_HasNothrowAssign: |
| 2626 | case UTT_HasNothrowConstructor: |
| 2627 | case UTT_HasNothrowCopy: |
| 2628 | case UTT_HasTrivialAssign: |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2629 | case UTT_HasTrivialDefaultConstructor: |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2630 | case UTT_HasTrivialCopy: |
| 2631 | case UTT_HasTrivialDestructor: |
| 2632 | case UTT_HasVirtualDestructor: |
| 2633 | // Arrays of unknown bound are expressly allowed. |
| 2634 | QualType ElTy = ArgTy; |
| 2635 | if (ArgTy->isIncompleteArrayType()) |
| 2636 | ElTy = S.Context.getAsArrayType(ArgTy)->getElementType(); |
| 2637 | |
| 2638 | // The void type is expressly allowed. |
| 2639 | if (ElTy->isVoidType()) |
| 2640 | return true; |
| 2641 | |
| 2642 | return !S.RequireCompleteType( |
| 2643 | Loc, ElTy, diag::err_incomplete_type_used_in_type_trait_expr); |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2644 | } |
Chandler Carruth | 73e0a91 | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 2645 | llvm_unreachable("Type trait not handled by switch"); |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | static bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT, |
| 2649 | SourceLocation KeyLoc, QualType T) { |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 2650 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2651 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2652 | ASTContext &C = Self.Context; |
| 2653 | switch(UTT) { |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2654 | // Type trait expressions corresponding to the primary type category |
| 2655 | // predicates in C++0x [meta.unary.cat]. |
| 2656 | case UTT_IsVoid: |
| 2657 | return T->isVoidType(); |
| 2658 | case UTT_IsIntegral: |
| 2659 | return T->isIntegralType(C); |
| 2660 | case UTT_IsFloatingPoint: |
| 2661 | return T->isFloatingType(); |
| 2662 | case UTT_IsArray: |
| 2663 | return T->isArrayType(); |
| 2664 | case UTT_IsPointer: |
| 2665 | return T->isPointerType(); |
| 2666 | case UTT_IsLvalueReference: |
| 2667 | return T->isLValueReferenceType(); |
| 2668 | case UTT_IsRvalueReference: |
| 2669 | return T->isRValueReferenceType(); |
| 2670 | case UTT_IsMemberFunctionPointer: |
| 2671 | return T->isMemberFunctionPointerType(); |
| 2672 | case UTT_IsMemberObjectPointer: |
| 2673 | return T->isMemberDataPointerType(); |
| 2674 | case UTT_IsEnum: |
| 2675 | return T->isEnumeralType(); |
Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2676 | case UTT_IsUnion: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2677 | return T->isUnionType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2678 | case UTT_IsClass: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2679 | return T->isClassType() || T->isStructureType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2680 | case UTT_IsFunction: |
| 2681 | return T->isFunctionType(); |
| 2682 | |
| 2683 | // Type trait expressions which correspond to the convenient composition |
| 2684 | // predicates in C++0x [meta.unary.comp]. |
| 2685 | case UTT_IsReference: |
| 2686 | return T->isReferenceType(); |
| 2687 | case UTT_IsArithmetic: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2688 | return T->isArithmeticType() && !T->isEnumeralType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2689 | case UTT_IsFundamental: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2690 | return T->isFundamentalType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2691 | case UTT_IsObject: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2692 | return T->isObjectType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2693 | case UTT_IsScalar: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2694 | // Note: semantic analysis depends on Objective-C lifetime types to be |
| 2695 | // considered scalar types. However, such types do not actually behave |
| 2696 | // like scalar types at run time (since they may require retain/release |
| 2697 | // operations), so we report them as non-scalar. |
| 2698 | if (T->isObjCLifetimeType()) { |
| 2699 | switch (T.getObjCLifetime()) { |
| 2700 | case Qualifiers::OCL_None: |
| 2701 | case Qualifiers::OCL_ExplicitNone: |
| 2702 | return true; |
| 2703 | |
| 2704 | case Qualifiers::OCL_Strong: |
| 2705 | case Qualifiers::OCL_Weak: |
| 2706 | case Qualifiers::OCL_Autoreleasing: |
| 2707 | return false; |
| 2708 | } |
| 2709 | } |
| 2710 | |
Chandler Carruth | cec0ced | 2011-05-01 09:29:55 +0000 | [diff] [blame] | 2711 | return T->isScalarType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2712 | case UTT_IsCompound: |
Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2713 | return T->isCompoundType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2714 | case UTT_IsMemberPointer: |
| 2715 | return T->isMemberPointerType(); |
| 2716 | |
| 2717 | // Type trait expressions which correspond to the type property predicates |
| 2718 | // in C++0x [meta.unary.prop]. |
| 2719 | case UTT_IsConst: |
| 2720 | return T.isConstQualified(); |
| 2721 | case UTT_IsVolatile: |
| 2722 | return T.isVolatileQualified(); |
| 2723 | case UTT_IsTrivial: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2724 | return T.isTrivialType(Self.Context); |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2725 | case UTT_IsTriviallyCopyable: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2726 | return T.isTriviallyCopyableType(Self.Context); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2727 | case UTT_IsStandardLayout: |
| 2728 | return T->isStandardLayoutType(); |
| 2729 | case UTT_IsPOD: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2730 | return T.isPODType(Self.Context); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2731 | case UTT_IsLiteral: |
| 2732 | return T->isLiteralType(); |
| 2733 | case UTT_IsEmpty: |
| 2734 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2735 | return !RD->isUnion() && RD->isEmpty(); |
| 2736 | return false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2737 | case UTT_IsPolymorphic: |
Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2738 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2739 | return RD->isPolymorphic(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2740 | return false; |
| 2741 | case UTT_IsAbstract: |
Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2742 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2743 | return RD->isAbstract(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2744 | return false; |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2745 | case UTT_IsSigned: |
| 2746 | return T->isSignedIntegerType(); |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2747 | case UTT_IsUnsigned: |
| 2748 | return T->isUnsignedIntegerType(); |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2749 | |
| 2750 | // Type trait expressions which query classes regarding their construction, |
| 2751 | // destruction, and copying. Rather than being based directly on the |
| 2752 | // related type predicates in the standard, they are specified by both |
| 2753 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those |
| 2754 | // specifications. |
| 2755 | // |
| 2756 | // 1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html |
| 2757 | // 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] | 2758 | case UTT_HasTrivialDefaultConstructor: |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2759 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2760 | // If __is_pod (type) is true then the trait is true, else if type is |
| 2761 | // a cv class or union type (or array thereof) with a trivial default |
| 2762 | // constructor ([class.ctor]) then the trait is true, else it is false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2763 | if (T.isPODType(Self.Context)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2764 | return true; |
| 2765 | if (const RecordType *RT = |
| 2766 | C.getBaseElementType(T)->getAs<RecordType>()) |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2767 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDefaultConstructor(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2768 | return false; |
| 2769 | case UTT_HasTrivialCopy: |
| 2770 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2771 | // If __is_pod (type) is true or type is a reference type then |
| 2772 | // the trait is true, else if type is a cv class or union type |
| 2773 | // with a trivial copy constructor ([class.copy]) then the trait |
| 2774 | // is true, else it is false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2775 | if (T.isPODType(Self.Context) || T->isReferenceType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2776 | return true; |
| 2777 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2778 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor(); |
| 2779 | return false; |
| 2780 | case UTT_HasTrivialAssign: |
| 2781 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2782 | // If type is const qualified or is a reference type then the |
| 2783 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 2784 | // trait is true, else if type is a cv class or union type with |
| 2785 | // a trivial copy assignment ([class.copy]) then the trait is |
| 2786 | // true, else it is false. |
| 2787 | // Note: the const and reference restrictions are interesting, |
| 2788 | // given that const and reference members don't prevent a class |
| 2789 | // from having a trivial copy assignment operator (but do cause |
| 2790 | // errors if the copy assignment operator is actually used, q.v. |
| 2791 | // [class.copy]p12). |
| 2792 | |
| 2793 | if (C.getBaseElementType(T).isConstQualified()) |
| 2794 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2795 | if (T.isPODType(Self.Context)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2796 | return true; |
| 2797 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2798 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment(); |
| 2799 | return false; |
| 2800 | case UTT_HasTrivialDestructor: |
| 2801 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2802 | // If __is_pod (type) is true or type is a reference type |
| 2803 | // then the trait is true, else if type is a cv class or union |
| 2804 | // type (or array thereof) with a trivial destructor |
| 2805 | // ([class.dtor]) then the trait is true, else it is |
| 2806 | // false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2807 | if (T.isPODType(Self.Context) || T->isReferenceType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2808 | return true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2809 | |
| 2810 | // Objective-C++ ARC: autorelease types don't require destruction. |
| 2811 | if (T->isObjCLifetimeType() && |
| 2812 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) |
| 2813 | return true; |
| 2814 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2815 | if (const RecordType *RT = |
| 2816 | C.getBaseElementType(T)->getAs<RecordType>()) |
| 2817 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor(); |
| 2818 | return false; |
| 2819 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 2820 | case UTT_HasNothrowAssign: |
| 2821 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2822 | // If type is const qualified or is a reference type then the |
| 2823 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 2824 | // is true then the trait is true, else if type is a cv class |
| 2825 | // or union type with copy assignment operators that are known |
| 2826 | // not to throw an exception then the trait is true, else it is |
| 2827 | // false. |
| 2828 | if (C.getBaseElementType(T).isConstQualified()) |
| 2829 | return false; |
| 2830 | if (T->isReferenceType()) |
| 2831 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2832 | if (T.isPODType(Self.Context) || T->isObjCLifetimeType()) |
| 2833 | return true; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2834 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2835 | CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2836 | if (RD->hasTrivialCopyAssignment()) |
| 2837 | return true; |
| 2838 | |
| 2839 | bool FoundAssign = false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2840 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2841 | LookupResult Res(Self, DeclarationNameInfo(Name, KeyLoc), |
| 2842 | Sema::LookupOrdinaryName); |
| 2843 | if (Self.LookupQualifiedName(Res, RD)) { |
| 2844 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 2845 | Op != OpEnd; ++Op) { |
| 2846 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 2847 | if (Operator->isCopyAssignmentOperator()) { |
| 2848 | FoundAssign = true; |
| 2849 | const FunctionProtoType *CPT |
| 2850 | = Operator->getType()->getAs<FunctionProtoType>(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2851 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 2852 | return false; |
| 2853 | if (!CPT->isNothrow(Self.Context)) |
| 2854 | return false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | } |
| 2858 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2859 | return FoundAssign; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2860 | } |
| 2861 | return false; |
| 2862 | case UTT_HasNothrowCopy: |
| 2863 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2864 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 2865 | // if type is a cv class or union type with copy constructors that are |
| 2866 | // known not to throw an exception then the trait is true, else it is |
| 2867 | // false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2868 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2869 | return true; |
| 2870 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2871 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2872 | if (RD->hasTrivialCopyConstructor()) |
| 2873 | return true; |
| 2874 | |
| 2875 | bool FoundConstructor = false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2876 | unsigned FoundTQs; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2877 | DeclContext::lookup_const_iterator Con, ConEnd; |
Sebastian Redl | 5f4e899 | 2010-09-13 21:10:20 +0000 | [diff] [blame] | 2878 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2879 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2880 | // A template constructor is never a copy constructor. |
| 2881 | // FIXME: However, it may actually be selected at the actual overload |
| 2882 | // resolution point. |
| 2883 | if (isa<FunctionTemplateDecl>(*Con)) |
| 2884 | continue; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2885 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 2886 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 2887 | FoundConstructor = true; |
| 2888 | const FunctionProtoType *CPT |
| 2889 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2890 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 2891 | return false; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2892 | // FIXME: check whether evaluating default arguments can throw. |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2893 | // For now, we'll be conservative and assume that they can throw. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2894 | if (!CPT->isNothrow(Self.Context) || CPT->getNumArgs() > 1) |
| 2895 | return false; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2896 | } |
| 2897 | } |
| 2898 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2899 | return FoundConstructor; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2900 | } |
| 2901 | return false; |
| 2902 | case UTT_HasNothrowConstructor: |
| 2903 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2904 | // If __has_trivial_constructor (type) is true then the trait is |
| 2905 | // true, else if type is a cv class or union type (or array |
| 2906 | // thereof) with a default constructor that is known not to |
| 2907 | // throw an exception then the trait is true, else it is false. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2908 | if (T.isPODType(C) || T->isObjCLifetimeType()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2909 | return true; |
| 2910 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) { |
| 2911 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2912 | if (RD->hasTrivialDefaultConstructor()) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2913 | return true; |
| 2914 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2915 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 2916 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
| 2917 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2918 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 2919 | if (isa<FunctionTemplateDecl>(*Con)) |
| 2920 | continue; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2921 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 2922 | if (Constructor->isDefaultConstructor()) { |
| 2923 | const FunctionProtoType *CPT |
| 2924 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2925 | if (CPT->getExceptionSpecType() == EST_Delayed) |
| 2926 | return false; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2927 | // TODO: check whether evaluating default arguments can throw. |
| 2928 | // For now, we'll be conservative and assume that they can throw. |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2929 | return CPT->isNothrow(Self.Context) && CPT->getNumArgs() == 0; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2930 | } |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2931 | } |
| 2932 | } |
| 2933 | return false; |
| 2934 | case UTT_HasVirtualDestructor: |
| 2935 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2936 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 2937 | // then the trait is true, else it is false. |
| 2938 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2939 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2940 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2941 | return Destructor->isVirtual(); |
| 2942 | } |
| 2943 | return false; |
Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2944 | |
| 2945 | // These type trait expressions are modeled on the specifications for the |
| 2946 | // Embarcadero C++0x type trait functions: |
| 2947 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index |
| 2948 | case UTT_IsCompleteType: |
| 2949 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): |
| 2950 | // Returns True if and only if T is a complete type at the point of the |
| 2951 | // function call. |
| 2952 | return !T->isIncompleteType(); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2953 | } |
Chandler Carruth | 83f563c | 2011-05-01 07:44:17 +0000 | [diff] [blame] | 2954 | llvm_unreachable("Type trait not covered by switch"); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | ExprResult Sema::BuildUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2958 | SourceLocation KWLoc, |
| 2959 | TypeSourceInfo *TSInfo, |
| 2960 | SourceLocation RParen) { |
| 2961 | QualType T = TSInfo->getType(); |
Chandler Carruth | eb65a10 | 2011-04-30 10:07:32 +0000 | [diff] [blame] | 2962 | if (!CheckUnaryTypeTraitTypeCompleteness(*this, UTT, KWLoc, T)) |
| 2963 | return ExprError(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2964 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2965 | bool Value = false; |
| 2966 | if (!T->isDependentType()) |
Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2967 | Value = EvaluateUnaryTypeTrait(*this, UTT, KWLoc, T); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2968 | |
| 2969 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, UTT, TSInfo, Value, |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 2970 | RParen, Context.BoolTy)); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2971 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2972 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2973 | ExprResult Sema::ActOnBinaryTypeTrait(BinaryTypeTrait BTT, |
| 2974 | SourceLocation KWLoc, |
| 2975 | ParsedType LhsTy, |
| 2976 | ParsedType RhsTy, |
| 2977 | SourceLocation RParen) { |
| 2978 | TypeSourceInfo *LhsTSInfo; |
| 2979 | QualType LhsT = GetTypeFromParser(LhsTy, &LhsTSInfo); |
| 2980 | if (!LhsTSInfo) |
| 2981 | LhsTSInfo = Context.getTrivialTypeSourceInfo(LhsT); |
| 2982 | |
| 2983 | TypeSourceInfo *RhsTSInfo; |
| 2984 | QualType RhsT = GetTypeFromParser(RhsTy, &RhsTSInfo); |
| 2985 | if (!RhsTSInfo) |
| 2986 | RhsTSInfo = Context.getTrivialTypeSourceInfo(RhsT); |
| 2987 | |
| 2988 | return BuildBinaryTypeTrait(BTT, KWLoc, LhsTSInfo, RhsTSInfo, RParen); |
| 2989 | } |
| 2990 | |
| 2991 | static bool EvaluateBinaryTypeTrait(Sema &Self, BinaryTypeTrait BTT, |
| 2992 | QualType LhsT, QualType RhsT, |
| 2993 | SourceLocation KeyLoc) { |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 2994 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && |
| 2995 | "Cannot evaluate traits of dependent types"); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2996 | |
| 2997 | switch(BTT) { |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 2998 | case BTT_IsBaseOf: { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2999 | // C++0x [meta.rel]p2 |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3000 | // 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] | 3001 | // Base and Derived are not unions and name the same class type without |
| 3002 | // regard to cv-qualifiers. |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3003 | |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3004 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); |
| 3005 | if (!lhsRecord) return false; |
| 3006 | |
| 3007 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); |
| 3008 | if (!rhsRecord) return false; |
| 3009 | |
| 3010 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) |
| 3011 | == (lhsRecord == rhsRecord)); |
| 3012 | |
| 3013 | if (lhsRecord == rhsRecord) |
| 3014 | return !lhsRecord->getDecl()->isUnion(); |
| 3015 | |
| 3016 | // C++0x [meta.rel]p2: |
| 3017 | // If Base and Derived are class types and are different types |
| 3018 | // (ignoring possible cv-qualifiers) then Derived shall be a |
| 3019 | // complete type. |
| 3020 | if (Self.RequireCompleteType(KeyLoc, RhsT, |
| 3021 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 3022 | return false; |
| 3023 | |
| 3024 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) |
| 3025 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); |
| 3026 | } |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3027 | case BTT_IsSame: |
| 3028 | return Self.Context.hasSameType(LhsT, RhsT); |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3029 | case BTT_TypeCompatible: |
| 3030 | return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(), |
| 3031 | RhsT.getUnqualifiedType()); |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3032 | case BTT_IsConvertible: |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3033 | case BTT_IsConvertibleTo: { |
| 3034 | // C++0x [meta.rel]p4: |
| 3035 | // Given the following function prototype: |
| 3036 | // |
| 3037 | // template <class T> |
| 3038 | // typename add_rvalue_reference<T>::type create(); |
| 3039 | // |
| 3040 | // the predicate condition for a template specialization |
| 3041 | // is_convertible<From, To> shall be satisfied if and only if |
| 3042 | // the return expression in the following code would be |
| 3043 | // well-formed, including any implicit conversions to the return |
| 3044 | // type of the function: |
| 3045 | // |
| 3046 | // To test() { |
| 3047 | // return create<From>(); |
| 3048 | // } |
| 3049 | // |
| 3050 | // Access checking is performed as if in a context unrelated to To and |
| 3051 | // From. Only the validity of the immediate context of the expression |
| 3052 | // of the return-statement (including conversions to the return type) |
| 3053 | // is considered. |
| 3054 | // |
| 3055 | // We model the initialization as a copy-initialization of a temporary |
| 3056 | // of the appropriate type, which for this expression is identical to the |
| 3057 | // return statement (since NRVO doesn't apply). |
| 3058 | if (LhsT->isObjectType() || LhsT->isFunctionType()) |
| 3059 | LhsT = Self.Context.getRValueReferenceType(LhsT); |
| 3060 | |
| 3061 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3062 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3063 | Expr::getValueKindForType(LhsT)); |
| 3064 | Expr *FromPtr = &From; |
| 3065 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, |
| 3066 | SourceLocation())); |
| 3067 | |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3068 | // Perform the initialization within a SFINAE trap at translation unit |
| 3069 | // scope. |
| 3070 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); |
| 3071 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3072 | InitializationSequence Init(Self, To, Kind, &FromPtr, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3073 | if (Init.Failed()) |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3074 | return false; |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3075 | |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3076 | ExprResult Result = Init.Perform(Self, To, Kind, MultiExprArg(&FromPtr, 1)); |
| 3077 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); |
| 3078 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3079 | } |
| 3080 | llvm_unreachable("Unknown type trait or not implemented"); |
| 3081 | } |
| 3082 | |
| 3083 | ExprResult Sema::BuildBinaryTypeTrait(BinaryTypeTrait BTT, |
| 3084 | SourceLocation KWLoc, |
| 3085 | TypeSourceInfo *LhsTSInfo, |
| 3086 | TypeSourceInfo *RhsTSInfo, |
| 3087 | SourceLocation RParen) { |
| 3088 | QualType LhsT = LhsTSInfo->getType(); |
| 3089 | QualType RhsT = RhsTSInfo->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3090 | |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3091 | if (BTT == BTT_TypeCompatible) { |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3092 | if (getLangOptions().CPlusPlus) { |
| 3093 | Diag(KWLoc, diag::err_types_compatible_p_in_cplusplus) |
| 3094 | << SourceRange(KWLoc, RParen); |
| 3095 | return ExprError(); |
| 3096 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | bool Value = false; |
| 3100 | if (!LhsT->isDependentType() && !RhsT->isDependentType()) |
| 3101 | Value = EvaluateBinaryTypeTrait(*this, BTT, LhsT, RhsT, KWLoc); |
| 3102 | |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3103 | // Select trait result type. |
| 3104 | QualType ResultType; |
| 3105 | switch (BTT) { |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3106 | case BTT_IsBaseOf: ResultType = Context.BoolTy; break; |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3107 | case BTT_IsConvertible: ResultType = Context.BoolTy; break; |
| 3108 | case BTT_IsSame: ResultType = Context.BoolTy; break; |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3109 | case BTT_TypeCompatible: ResultType = Context.IntTy; break; |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3110 | case BTT_IsConvertibleTo: ResultType = Context.BoolTy; break; |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3113 | return Owned(new (Context) BinaryTypeTraitExpr(KWLoc, BTT, LhsTSInfo, |
| 3114 | RhsTSInfo, Value, RParen, |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3115 | ResultType)); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3118 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, |
| 3119 | SourceLocation KWLoc, |
| 3120 | ParsedType Ty, |
| 3121 | Expr* DimExpr, |
| 3122 | SourceLocation RParen) { |
| 3123 | TypeSourceInfo *TSInfo; |
| 3124 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
| 3125 | if (!TSInfo) |
| 3126 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
| 3127 | |
| 3128 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); |
| 3129 | } |
| 3130 | |
| 3131 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, |
| 3132 | QualType T, Expr *DimExpr, |
| 3133 | SourceLocation KeyLoc) { |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3134 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3135 | |
| 3136 | switch(ATT) { |
| 3137 | case ATT_ArrayRank: |
| 3138 | if (T->isArrayType()) { |
| 3139 | unsigned Dim = 0; |
| 3140 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 3141 | ++Dim; |
| 3142 | T = AT->getElementType(); |
| 3143 | } |
| 3144 | return Dim; |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3145 | } |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3146 | return 0; |
| 3147 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3148 | case ATT_ArrayExtent: { |
| 3149 | llvm::APSInt Value; |
| 3150 | uint64_t Dim; |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3151 | if (DimExpr->isIntegerConstantExpr(Value, Self.Context, 0, false)) { |
| 3152 | if (Value < llvm::APSInt(Value.getBitWidth(), Value.isUnsigned())) { |
| 3153 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) << |
| 3154 | DimExpr->getSourceRange(); |
| 3155 | return false; |
| 3156 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3157 | Dim = Value.getLimitedValue(); |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3158 | } else { |
| 3159 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) << |
| 3160 | DimExpr->getSourceRange(); |
| 3161 | return false; |
| 3162 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3163 | |
| 3164 | if (T->isArrayType()) { |
| 3165 | unsigned D = 0; |
| 3166 | bool Matched = false; |
| 3167 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { |
| 3168 | if (Dim == D) { |
| 3169 | Matched = true; |
| 3170 | break; |
| 3171 | } |
| 3172 | ++D; |
| 3173 | T = AT->getElementType(); |
| 3174 | } |
| 3175 | |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3176 | if (Matched && T->isArrayType()) { |
| 3177 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) |
| 3178 | return CAT->getSize().getLimitedValue(); |
| 3179 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3180 | } |
John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3181 | return 0; |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | llvm_unreachable("Unknown type trait or not implemented"); |
| 3185 | } |
| 3186 | |
| 3187 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, |
| 3188 | SourceLocation KWLoc, |
| 3189 | TypeSourceInfo *TSInfo, |
| 3190 | Expr* DimExpr, |
| 3191 | SourceLocation RParen) { |
| 3192 | QualType T = TSInfo->getType(); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3193 | |
Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3194 | // FIXME: This should likely be tracked as an APInt to remove any host |
| 3195 | // assumptions about the width of size_t on the target. |
Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3196 | uint64_t Value = 0; |
| 3197 | if (!T->isDependentType()) |
| 3198 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); |
| 3199 | |
Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3200 | // While the specification for these traits from the Embarcadero C++ |
| 3201 | // compiler's documentation says the return type is 'unsigned int', Clang |
| 3202 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero |
| 3203 | // compiler, there is no difference. On several other platforms this is an |
| 3204 | // important distinction. |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3205 | return Owned(new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, |
Chandler Carruth | 06207f6 | 2011-05-01 07:49:26 +0000 | [diff] [blame] | 3206 | DimExpr, RParen, |
Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3207 | Context.getSizeType())); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3210 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3211 | SourceLocation KWLoc, |
| 3212 | Expr *Queried, |
| 3213 | SourceLocation RParen) { |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3214 | // If error parsing the expression, ignore. |
| 3215 | if (!Queried) |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3216 | return ExprError(); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3217 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3218 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3219 | |
| 3220 | return move(Result); |
| 3221 | } |
| 3222 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3223 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { |
| 3224 | switch (ET) { |
| 3225 | case ET_IsLValueExpr: return E->isLValue(); |
| 3226 | case ET_IsRValueExpr: return E->isRValue(); |
| 3227 | } |
| 3228 | llvm_unreachable("Expression trait not covered by switch"); |
| 3229 | } |
| 3230 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3231 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3232 | SourceLocation KWLoc, |
| 3233 | Expr *Queried, |
| 3234 | SourceLocation RParen) { |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3235 | if (Queried->isTypeDependent()) { |
| 3236 | // Delay type-checking for type-dependent expressions. |
| 3237 | } else if (Queried->getType()->isPlaceholderType()) { |
| 3238 | ExprResult PE = CheckPlaceholderExpr(Queried); |
| 3239 | if (PE.isInvalid()) return ExprError(); |
| 3240 | return BuildExpressionTrait(ET, KWLoc, PE.take(), RParen); |
| 3241 | } |
| 3242 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3243 | bool Value = EvaluateExpressionTrait(ET, Queried); |
Chandler Carruth | f7ef000 | 2011-05-01 08:48:19 +0000 | [diff] [blame] | 3244 | |
Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3245 | return Owned(new (Context) ExpressionTraitExpr(KWLoc, ET, Queried, Value, |
| 3246 | RParen, Context.BoolTy)); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3249 | QualType Sema::CheckPointerToMemberOperands(ExprResult &lex, ExprResult &rex, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3250 | ExprValueKind &VK, |
| 3251 | SourceLocation Loc, |
| 3252 | bool isIndirect) { |
John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3253 | assert(!lex.get()->getType()->isPlaceholderType() && |
| 3254 | !rex.get()->getType()->isPlaceholderType() && |
| 3255 | "placeholders should have been weeded out by now"); |
| 3256 | |
| 3257 | // The LHS undergoes lvalue conversions if this is ->*. |
| 3258 | if (isIndirect) { |
| 3259 | lex = DefaultLvalueConversion(lex.take()); |
| 3260 | if (lex.isInvalid()) return QualType(); |
| 3261 | } |
| 3262 | |
| 3263 | // The RHS always undergoes lvalue conversions. |
| 3264 | rex = DefaultLvalueConversion(rex.take()); |
| 3265 | if (rex.isInvalid()) return QualType(); |
| 3266 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3267 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 3268 | // C++ 5.5p2 |
| 3269 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 3270 | // be of type "pointer to member of T" (where T is a completely-defined |
| 3271 | // class type) [...] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3272 | QualType RType = rex.get()->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3273 | const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3274 | if (!MemPtr) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3275 | Diag(Loc, diag::err_bad_memptr_rhs) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3276 | << OpSpelling << RType << rex.get()->getSourceRange(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3277 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3278 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3279 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3280 | QualType Class(MemPtr->getClass(), 0); |
| 3281 | |
Douglas Gregor | 7d520ba | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 3282 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
| 3283 | // member pointer points must be completely-defined. However, there is no |
| 3284 | // reason for this semantic distinction, and the rule is not enforced by |
| 3285 | // other compilers. Therefore, we do not check this property, as it is |
| 3286 | // likely to be considered a defect. |
Sebastian Redl | 59fc269 | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 3287 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3288 | // C++ 5.5p2 |
| 3289 | // [...] to its first operand, which shall be of class T or of a class of |
| 3290 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 3291 | // such a class] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3292 | QualType LType = lex.get()->getType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3293 | if (isIndirect) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3294 | if (const PointerType *Ptr = LType->getAs<PointerType>()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3295 | LType = Ptr->getPointeeType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3296 | else { |
| 3297 | Diag(Loc, diag::err_bad_memptr_lhs) |
Fariborz Jahanian | ef78ac6 | 2009-10-26 20:45:27 +0000 | [diff] [blame] | 3298 | << OpSpelling << 1 << LType |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3299 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3300 | return QualType(); |
| 3301 | } |
| 3302 | } |
| 3303 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3304 | if (!Context.hasSameUnqualifiedType(Class, LType)) { |
Sebastian Redl | 17e1d35 | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 3305 | // If we want to check the hierarchy, we need a complete type. |
| 3306 | if (RequireCompleteType(Loc, LType, PDiag(diag::err_bad_memptr_lhs) |
| 3307 | << OpSpelling << (int)isIndirect)) { |
| 3308 | return QualType(); |
| 3309 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3310 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 3311 | /*DetectVirtual=*/false); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3312 | // FIXME: Would it be useful to print full ambiguity paths, or is that |
| 3313 | // overkill? |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3314 | if (!IsDerivedFrom(LType, Class, Paths) || |
| 3315 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
| 3316 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3317 | << (int)isIndirect << lex.get()->getType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3318 | return QualType(); |
| 3319 | } |
Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 3320 | // Cast LHS to type of use. |
| 3321 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3322 | ExprValueKind VK = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3323 | isIndirect ? VK_RValue : CastCategory(lex.get()); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3324 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3325 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3326 | BuildBasePathArray(Paths, BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3327 | lex = ImpCastExprToType(lex.take(), UseType, CK_DerivedToBase, VK, &BasePath); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3330 | if (isa<CXXScalarValueInitExpr>(rex.get()->IgnoreParens())) { |
Fariborz Jahanian | 05ebda9 | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 3331 | // Diagnose use of pointer-to-member type which when used as |
| 3332 | // the functional cast in a pointer-to-member expression. |
| 3333 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 3334 | return QualType(); |
| 3335 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3336 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3337 | // C++ 5.5p2 |
| 3338 | // The result is an object or a function of the type specified by the |
| 3339 | // second operand. |
| 3340 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 3341 | // in accordance with 5.5p5 and 5.2.5. |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3342 | QualType Result = MemPtr->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3343 | Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3344 | |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3345 | // C++0x [expr.mptr.oper]p6: |
| 3346 | // In a .* expression whose object expression is an rvalue, the program is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3347 | // ill-formed if the second operand is a pointer to member function with |
| 3348 | // ref-qualifier &. In a ->* expression or in a .* expression whose object |
| 3349 | // 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] | 3350 | // is a pointer to member function with ref-qualifier &&. |
| 3351 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { |
| 3352 | switch (Proto->getRefQualifier()) { |
| 3353 | case RQ_None: |
| 3354 | // Do nothing |
| 3355 | break; |
| 3356 | |
| 3357 | case RQ_LValue: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3358 | if (!isIndirect && !lex.get()->Classify(Context).isLValue()) |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3359 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3360 | << RType << 1 << lex.get()->getSourceRange(); |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3361 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3362 | |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3363 | case RQ_RValue: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3364 | if (isIndirect || !lex.get()->Classify(Context).isRValue()) |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3365 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3366 | << RType << 0 << lex.get()->getSourceRange(); |
Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3367 | break; |
| 3368 | } |
| 3369 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3370 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3371 | // C++ [expr.mptr.oper]p6: |
| 3372 | // The result of a .* expression whose second operand is a pointer |
| 3373 | // to a data member is of the same value category as its |
| 3374 | // first operand. The result of a .* expression whose second |
| 3375 | // operand is a pointer to a member function is a prvalue. The |
| 3376 | // result of an ->* expression is an lvalue if its second operand |
| 3377 | // is a pointer to data member and a prvalue otherwise. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3378 | if (Result->isFunctionType()) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3379 | VK = VK_RValue; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3380 | return Context.BoundMemberTy; |
| 3381 | } else if (isIndirect) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3382 | VK = VK_LValue; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3383 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3384 | VK = lex.get()->getValueKind(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3385 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3386 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3387 | return Result; |
| 3388 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3389 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3390 | /// \brief Try to convert a type to another according to C++0x 5.16p3. |
| 3391 | /// |
| 3392 | /// This is part of the parameter validation for the ? operator. If either |
| 3393 | /// value operand is a class type, the two operands are attempted to be |
| 3394 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3395 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 3396 | /// as such. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3397 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 3398 | SourceLocation QuestionLoc, |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3399 | bool &HaveConversion, |
| 3400 | QualType &ToType) { |
| 3401 | HaveConversion = false; |
| 3402 | ToType = To->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3403 | |
| 3404 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3405 | SourceLocation()); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3406 | // C++0x 5.16p3 |
| 3407 | // The process for determining whether an operand expression E1 of type T1 |
| 3408 | // can be converted to match an operand expression E2 of type T2 is defined |
| 3409 | // as follows: |
| 3410 | // -- If E2 is an lvalue: |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 3411 | bool ToIsLvalue = To->isLValue(); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 3412 | if (ToIsLvalue) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3413 | // E1 can be converted to match E2 if E1 can be implicitly converted to |
| 3414 | // type "lvalue reference to T2", subject to the constraint that in the |
| 3415 | // conversion the reference must bind directly to E1. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3416 | QualType T = Self.Context.getLValueReferenceType(ToType); |
| 3417 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3418 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3419 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 3420 | if (InitSeq.isDirectReferenceBinding()) { |
| 3421 | ToType = T; |
| 3422 | HaveConversion = true; |
| 3423 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3424 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3426 | if (InitSeq.isAmbiguous()) |
| 3427 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3428 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3429 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3430 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 3431 | // -- if E1 and E2 have class type, and the underlying class types are |
| 3432 | // the same or one is a base class of the other: |
| 3433 | QualType FTy = From->getType(); |
| 3434 | QualType TTy = To->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3435 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 3436 | const RecordType *TRec = TTy->getAs<RecordType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3437 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3438 | Self.IsDerivedFrom(FTy, TTy); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3439 | if (FRec && TRec && |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3440 | (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3441 | // E1 can be converted to match E2 if the class of T2 is the |
| 3442 | // same type as, or a base class of, the class of T1, and |
| 3443 | // [cv2 > cv1]. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3444 | if (FRec == TRec || FDerivedFromT) { |
| 3445 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3446 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 3447 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3448 | if (InitSeq) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3449 | HaveConversion = true; |
| 3450 | return false; |
| 3451 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3452 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3453 | if (InitSeq.isAmbiguous()) |
| 3454 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3455 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3456 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3458 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3459 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3460 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3461 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 3462 | // implicitly converted to the type that expression E2 would have |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3463 | // 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] | 3464 | // an rvalue). |
| 3465 | // |
| 3466 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 3467 | // to the array-to-pointer or function-to-pointer conversions. |
| 3468 | if (!TTy->getAs<TagType>()) |
| 3469 | TTy = TTy.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3470 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3471 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 3472 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3473 | HaveConversion = !InitSeq.Failed(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3474 | ToType = TTy; |
| 3475 | if (InitSeq.isAmbiguous()) |
| 3476 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
| 3477 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3478 | return false; |
| 3479 | } |
| 3480 | |
| 3481 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 3482 | /// |
| 3483 | /// This is part of the parameter validation for the ? operator. If either |
| 3484 | /// value operand is a class type, overload resolution is used to find a |
| 3485 | /// conversion to a common type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3486 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3487 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3488 | Expr *Args[2] = { LHS.get(), RHS.get() }; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3489 | OverloadCandidateSet CandidateSet(QuestionLoc); |
| 3490 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, 2, |
| 3491 | CandidateSet); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3492 | |
| 3493 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3494 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3495 | case OR_Success: { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3496 | // 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] | 3497 | ExprResult LHSRes = |
| 3498 | Self.PerformImplicitConversion(LHS.get(), Best->BuiltinTypes.ParamTypes[0], |
| 3499 | Best->Conversions[0], Sema::AA_Converting); |
| 3500 | if (LHSRes.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3501 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3502 | LHS = move(LHSRes); |
| 3503 | |
| 3504 | ExprResult RHSRes = |
| 3505 | Self.PerformImplicitConversion(RHS.get(), Best->BuiltinTypes.ParamTypes[1], |
| 3506 | Best->Conversions[1], Sema::AA_Converting); |
| 3507 | if (RHSRes.isInvalid()) |
| 3508 | break; |
| 3509 | RHS = move(RHSRes); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3510 | if (Best->Function) |
| 3511 | Self.MarkDeclarationReferenced(QuestionLoc, Best->Function); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3512 | return false; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3515 | case OR_No_Viable_Function: |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3516 | |
| 3517 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 3518 | // constant and the other is a pointer type. In this case, the user most |
| 3519 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3520 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3521 | return true; |
| 3522 | |
| 3523 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3524 | << LHS.get()->getType() << RHS.get()->getType() |
| 3525 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3526 | return true; |
| 3527 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3528 | case OR_Ambiguous: |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3529 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3530 | << LHS.get()->getType() << RHS.get()->getType() |
| 3531 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3532 | // FIXME: Print the possible common types by printing the return types of |
| 3533 | // the viable candidates. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3534 | break; |
| 3535 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3536 | case OR_Deleted: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3537 | assert(false && "Conditional operator has only built-in overloads"); |
| 3538 | break; |
| 3539 | } |
| 3540 | return true; |
| 3541 | } |
| 3542 | |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3543 | /// \brief Perform an "extended" implicit conversion as returned by |
| 3544 | /// TryClassUnification. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3545 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3546 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3547 | InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(), |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3548 | SourceLocation()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3549 | Expr *Arg = E.take(); |
| 3550 | InitializationSequence InitSeq(Self, Entity, Kind, &Arg, 1); |
| 3551 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, MultiExprArg(&Arg, 1)); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3552 | if (Result.isInvalid()) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3553 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3554 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3555 | E = Result; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3556 | return false; |
| 3557 | } |
| 3558 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3559 | /// \brief Check the operands of ?: under C++ semantics. |
| 3560 | /// |
| 3561 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 3562 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3563 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3564 | ExprValueKind &VK, ExprObjectKind &OK, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3565 | SourceLocation QuestionLoc) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3566 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 3567 | // interface pointers. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3568 | |
| 3569 | // C++0x 5.16p1 |
| 3570 | // The first expression is contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3571 | if (!Cond.get()->isTypeDependent()) { |
| 3572 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.take()); |
| 3573 | if (CondRes.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3574 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3575 | Cond = move(CondRes); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3578 | // Assume r-value. |
| 3579 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3580 | OK = OK_Ordinary; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3581 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3582 | // Either of the arguments dependent? |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3583 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3584 | return Context.DependentTy; |
| 3585 | |
| 3586 | // C++0x 5.16p2 |
| 3587 | // If either the second or the third operand has type (cv) void, ... |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3588 | QualType LTy = LHS.get()->getType(); |
| 3589 | QualType RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3590 | bool LVoid = LTy->isVoidType(); |
| 3591 | bool RVoid = RTy->isVoidType(); |
| 3592 | if (LVoid || RVoid) { |
| 3593 | // ... then the [l2r] conversions are performed on the second and third |
| 3594 | // operands ... |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3595 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 3596 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 3597 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 3598 | return QualType(); |
| 3599 | LTy = LHS.get()->getType(); |
| 3600 | RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3601 | |
| 3602 | // ... and one of the following shall hold: |
| 3603 | // -- The second or the third operand (but not both) is a throw- |
| 3604 | // 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] | 3605 | bool LThrow = isa<CXXThrowExpr>(LHS.get()); |
| 3606 | bool RThrow = isa<CXXThrowExpr>(RHS.get()); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3607 | if (LThrow && !RThrow) |
| 3608 | return RTy; |
| 3609 | if (RThrow && !LThrow) |
| 3610 | return LTy; |
| 3611 | |
| 3612 | // -- Both the second and third operands have type void; the result is of |
| 3613 | // type void and is an rvalue. |
| 3614 | if (LVoid && RVoid) |
| 3615 | return Context.VoidTy; |
| 3616 | |
| 3617 | // Neither holds, error. |
| 3618 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 3619 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3620 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3621 | return QualType(); |
| 3622 | } |
| 3623 | |
| 3624 | // Neither is void. |
| 3625 | |
| 3626 | // C++0x 5.16p3 |
| 3627 | // Otherwise, if the second and third operand have different types, and |
| 3628 | // either has (cv) class type, and attempt is made to convert each of those |
| 3629 | // operands to the other. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3630 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3631 | (LTy->isRecordType() || RTy->isRecordType())) { |
| 3632 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; |
| 3633 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3634 | QualType L2RType, R2LType; |
| 3635 | bool HaveL2R, HaveR2L; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3636 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3637 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3638 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3639 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3640 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3641 | // If both can be converted, [...] the program is ill-formed. |
| 3642 | if (HaveL2R && HaveR2L) { |
| 3643 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3644 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3645 | return QualType(); |
| 3646 | } |
| 3647 | |
| 3648 | // If exactly one conversion is possible, that conversion is applied to |
| 3649 | // the chosen operand and the converted operands are used in place of the |
| 3650 | // original operands for the remainder of this section. |
| 3651 | if (HaveL2R) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3652 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3653 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3654 | LTy = LHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3655 | } else if (HaveR2L) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3656 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3657 | return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3658 | RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | // C++0x 5.16p4 |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3663 | // If the second and third operands are glvalues of the same value |
| 3664 | // category and have the same type, the result is of that type and |
| 3665 | // value category and it is a bit-field if the second or the third |
| 3666 | // operand is a bit-field, or if both are bit-fields. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3667 | // We only extend this to bitfields, not to the crazy other kinds of |
| 3668 | // l-values. |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 3669 | bool Same = Context.hasSameType(LTy, RTy); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3670 | if (Same && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3671 | LHS.get()->isGLValue() && |
| 3672 | LHS.get()->getValueKind() == RHS.get()->getValueKind() && |
| 3673 | LHS.get()->isOrdinaryOrBitFieldObject() && |
| 3674 | RHS.get()->isOrdinaryOrBitFieldObject()) { |
| 3675 | VK = LHS.get()->getValueKind(); |
| 3676 | if (LHS.get()->getObjectKind() == OK_BitField || |
| 3677 | RHS.get()->getObjectKind() == OK_BitField) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3678 | OK = OK_BitField; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3679 | return LTy; |
Fariborz Jahanian | 3911a1a | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 3680 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3681 | |
| 3682 | // C++0x 5.16p5 |
| 3683 | // Otherwise, the result is an rvalue. If the second and third operands |
| 3684 | // do not have the same type, and either has (cv) class type, ... |
| 3685 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 3686 | // ... overload resolution is used to determine the conversions (if any) |
| 3687 | // to be applied to the operands. If the overload resolution fails, the |
| 3688 | // program is ill-formed. |
| 3689 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 3690 | return QualType(); |
| 3691 | } |
| 3692 | |
| 3693 | // C++0x 5.16p6 |
| 3694 | // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 3695 | // conversions are performed on the second and third operands. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3696 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 3697 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 3698 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 3699 | return QualType(); |
| 3700 | LTy = LHS.get()->getType(); |
| 3701 | RTy = RHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3702 | |
| 3703 | // After those conversions, one of the following shall hold: |
| 3704 | // -- The second and third operands have the same type; the result |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3705 | // is of that type. If the operands have class type, the result |
| 3706 | // is a prvalue temporary of the result type, which is |
| 3707 | // copy-initialized from either the second operand or the third |
| 3708 | // operand depending on the value of the first operand. |
| 3709 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 3710 | if (LTy->isRecordType()) { |
| 3711 | // The operands have class type. Make a temporary copy. |
| 3712 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3713 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
| 3714 | SourceLocation(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3715 | LHS); |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3716 | if (LHSCopy.isInvalid()) |
| 3717 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3718 | |
| 3719 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
| 3720 | SourceLocation(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3721 | RHS); |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3722 | if (RHSCopy.isInvalid()) |
| 3723 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3724 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3725 | LHS = LHSCopy; |
| 3726 | RHS = RHSCopy; |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3729 | return LTy; |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3730 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3731 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3732 | // Extension: conditional operator involving vector types. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3733 | if (LTy->isVectorType() || RTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 3734 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3735 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3736 | // -- The second and third operands have arithmetic or enumeration type; |
| 3737 | // the usual arithmetic conversions are performed to bring them to a |
| 3738 | // common type, and the result is of that type. |
| 3739 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
| 3740 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3741 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 3742 | return QualType(); |
| 3743 | return LHS.get()->getType(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | // -- The second and third operands have pointer type, or one has pointer |
| 3747 | // type and the other is a null pointer constant; pointer conversions |
| 3748 | // and qualification conversions are performed to bring them to their |
| 3749 | // composite pointer type. The result is of the composite pointer type. |
Eli Friedman | de8ac49 | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 3750 | // -- The second and third operands have pointer to member type, or one has |
| 3751 | // pointer to member type and the other is a null pointer constant; |
| 3752 | // pointer to member conversions and qualification conversions are |
| 3753 | // performed to bring them to a common type, whose cv-qualification |
| 3754 | // shall match the cv-qualification of either the second or the third |
| 3755 | // operand. The result is of the common type. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3756 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3757 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3758 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
| 3759 | if (!Composite.isNull()) { |
| 3760 | if (NonStandardCompositeType) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3761 | Diag(QuestionLoc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3762 | diag::ext_typecheck_cond_incompatible_operands_nonstandard) |
| 3763 | << LTy << RTy << Composite |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3764 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3765 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3766 | return Composite; |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3767 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3768 | |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 3769 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 3770 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 3771 | if (!Composite.isNull()) |
| 3772 | return Composite; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3773 | |
Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 3774 | // Check if we are using a null with a non-pointer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3775 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 3776 | return QualType(); |
| 3777 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3778 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3779 | << LHS.get()->getType() << RHS.get()->getType() |
| 3780 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3781 | return QualType(); |
| 3782 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3783 | |
| 3784 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 3785 | /// |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3786 | /// This finds the composite pointer type (or member pointer type) for @p E1 |
| 3787 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this |
| 3788 | /// type and returns it. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3789 | /// It does not emit diagnostics. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3790 | /// |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3791 | /// \param Loc The location of the operator requiring these two expressions to |
| 3792 | /// be converted to the composite pointer type. |
| 3793 | /// |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3794 | /// If \p NonStandardCompositeType is non-NULL, then we are permitted to find |
| 3795 | /// a non-standard (but still sane) composite type to which both expressions |
| 3796 | /// can be converted. When such a type is chosen, \c *NonStandardCompositeType |
| 3797 | /// will be set true. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3798 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3799 | Expr *&E1, Expr *&E2, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3800 | bool *NonStandardCompositeType) { |
| 3801 | if (NonStandardCompositeType) |
| 3802 | *NonStandardCompositeType = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3803 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3804 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); |
| 3805 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3806 | |
Fariborz Jahanian | 0cedfbd | 2009-12-08 20:04:24 +0000 | [diff] [blame] | 3807 | if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && |
| 3808 | !T2->isAnyPointerType() && !T2->isMemberPointerType()) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3809 | return QualType(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3810 | |
| 3811 | // C++0x 5.9p2 |
| 3812 | // Pointer conversions and qualification conversions are performed on |
| 3813 | // pointer operands to bring them to their composite pointer type. If |
| 3814 | // one operand is a null pointer constant, the composite pointer type is |
| 3815 | // the type of the other operand. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3816 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3817 | if (T2->isMemberPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3818 | E1 = ImpCastExprToType(E1, T2, CK_NullToMemberPointer).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3819 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3820 | E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).take(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3821 | return T2; |
| 3822 | } |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3823 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3824 | if (T1->isMemberPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3825 | E2 = ImpCastExprToType(E2, T1, CK_NullToMemberPointer).take(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3826 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3827 | E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).take(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3828 | return T1; |
| 3829 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3830 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3831 | // Now both have to be pointers or member pointers. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3832 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || |
| 3833 | (!T2->isPointerType() && !T2->isMemberPointerType())) |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3834 | return QualType(); |
| 3835 | |
| 3836 | // Otherwise, of one of the operands has type "pointer to cv1 void," then |
| 3837 | // the other has type "pointer to cv2 T" and the composite pointer type is |
| 3838 | // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. |
| 3839 | // Otherwise, the composite pointer type is a pointer type similar to the |
| 3840 | // type of one of the operands, with a cv-qualification signature that is |
| 3841 | // the union of the cv-qualification signatures of the operand types. |
| 3842 | // In practice, the first part here is redundant; it's subsumed by the second. |
| 3843 | // What we do here is, we build the two possible composite types, and try the |
| 3844 | // conversions in both directions. If only one works, or if the two composite |
| 3845 | // types are the same, we have succeeded. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3846 | // FIXME: extended qualifiers? |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3847 | typedef SmallVector<unsigned, 4> QualifierVector; |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3848 | QualifierVector QualifierUnion; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3849 | typedef SmallVector<std::pair<const Type *, const Type *>, 4> |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3850 | ContainingClassVector; |
| 3851 | ContainingClassVector MemberOfClass; |
| 3852 | QualType Composite1 = Context.getCanonicalType(T1), |
| 3853 | Composite2 = Context.getCanonicalType(T2); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3854 | unsigned NeedConstBefore = 0; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3855 | do { |
| 3856 | const PointerType *Ptr1, *Ptr2; |
| 3857 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 3858 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 3859 | Composite1 = Ptr1->getPointeeType(); |
| 3860 | Composite2 = Ptr2->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3861 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3862 | // 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] | 3863 | // of where we need to fill in additional 'const' qualifiers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3864 | if (NonStandardCompositeType && |
| 3865 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 3866 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3867 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3868 | QualifierUnion.push_back( |
| 3869 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 3870 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); |
| 3871 | continue; |
| 3872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3873 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3874 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 3875 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 3876 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 3877 | Composite1 = MemPtr1->getPointeeType(); |
| 3878 | Composite2 = MemPtr2->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3879 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3880 | // 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] | 3881 | // of where we need to fill in additional 'const' qualifiers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3882 | if (NonStandardCompositeType && |
| 3883 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 3884 | NeedConstBefore = QualifierUnion.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3885 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3886 | QualifierUnion.push_back( |
| 3887 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 3888 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 3889 | MemPtr2->getClass())); |
| 3890 | continue; |
| 3891 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3893 | // FIXME: block pointer types? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3894 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3895 | // Cannot unwrap any more types. |
| 3896 | break; |
| 3897 | } while (true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3898 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3899 | if (NeedConstBefore && NonStandardCompositeType) { |
| 3900 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3901 | // mismatch, so that our (non-standard!) composite type meets the |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3902 | // requirements of C++ [conv.qual]p4 bullet 3. |
| 3903 | for (unsigned I = 0; I != NeedConstBefore; ++I) { |
| 3904 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) { |
| 3905 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
| 3906 | *NonStandardCompositeType = true; |
| 3907 | } |
| 3908 | } |
| 3909 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3910 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3911 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3912 | ContainingClassVector::reverse_iterator MOC |
| 3913 | = MemberOfClass.rbegin(); |
| 3914 | for (QualifierVector::reverse_iterator |
| 3915 | I = QualifierUnion.rbegin(), |
| 3916 | E = QualifierUnion.rend(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3917 | I != E; (void)++I, ++MOC) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3918 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3919 | if (MOC->first && MOC->second) { |
| 3920 | // Rebuild member pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3921 | Composite1 = Context.getMemberPointerType( |
| 3922 | Context.getQualifiedType(Composite1, Quals), |
| 3923 | MOC->first); |
| 3924 | Composite2 = Context.getMemberPointerType( |
| 3925 | Context.getQualifiedType(Composite2, Quals), |
| 3926 | MOC->second); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3927 | } else { |
| 3928 | // Rebuild pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3929 | Composite1 |
| 3930 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 3931 | Composite2 |
| 3932 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3933 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3936 | // Try to convert to the first composite pointer type. |
| 3937 | InitializedEntity Entity1 |
| 3938 | = InitializedEntity::InitializeTemporary(Composite1); |
| 3939 | InitializationKind Kind |
| 3940 | = InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 3941 | InitializationSequence E1ToC1(*this, Entity1, Kind, &E1, 1); |
| 3942 | InitializationSequence E2ToC1(*this, Entity1, Kind, &E2, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3943 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3944 | if (E1ToC1 && E2ToC1) { |
| 3945 | // Conversion to Composite1 is viable. |
| 3946 | if (!Context.hasSameType(Composite1, Composite2)) { |
| 3947 | // Composite2 is a different type from Composite1. Check whether |
| 3948 | // Composite2 is also viable. |
| 3949 | InitializedEntity Entity2 |
| 3950 | = InitializedEntity::InitializeTemporary(Composite2); |
| 3951 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 3952 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 3953 | if (E1ToC2 && E2ToC2) { |
| 3954 | // Both Composite1 and Composite2 are viable and are different; |
| 3955 | // this is an ambiguity. |
| 3956 | return QualType(); |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | // Convert E1 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3961 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3962 | = E1ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E1,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3963 | if (E1Result.isInvalid()) |
| 3964 | return QualType(); |
| 3965 | E1 = E1Result.takeAs<Expr>(); |
| 3966 | |
| 3967 | // Convert E2 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3968 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3969 | = E2ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E2,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3970 | if (E2Result.isInvalid()) |
| 3971 | return QualType(); |
| 3972 | E2 = E2Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3973 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3974 | return Composite1; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3977 | // Check whether Composite2 is viable. |
| 3978 | InitializedEntity Entity2 |
| 3979 | = InitializedEntity::InitializeTemporary(Composite2); |
| 3980 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 3981 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 3982 | if (!E1ToC2 || !E2ToC2) |
| 3983 | return QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3984 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3985 | // Convert E1 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3986 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3987 | = E1ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E1, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3988 | if (E1Result.isInvalid()) |
| 3989 | return QualType(); |
| 3990 | E1 = E1Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3991 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3992 | // Convert E2 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3993 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3994 | = E2ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E2, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3995 | if (E2Result.isInvalid()) |
| 3996 | return QualType(); |
| 3997 | E2 = E2Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3998 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3999 | return Composite2; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4000 | } |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 4001 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4002 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Douglas Gregor | 19cc1c7 | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 4003 | if (!E) |
| 4004 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4005 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4006 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 4007 | |
| 4008 | // If the result is a glvalue, we shouldn't bind it. |
| 4009 | if (!E->isRValue()) |
Anders Carlsson | 089c260 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 4010 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4012 | // In ARC, calls that return a retainable type can return retained, |
| 4013 | // in which case we have to insert a consuming cast. |
| 4014 | if (getLangOptions().ObjCAutoRefCount && |
| 4015 | E->getType()->isObjCRetainableType()) { |
| 4016 | |
| 4017 | bool ReturnsRetained; |
| 4018 | |
| 4019 | // For actual calls, we compute this by examining the type of the |
| 4020 | // called value. |
| 4021 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { |
| 4022 | Expr *Callee = Call->getCallee()->IgnoreParens(); |
| 4023 | QualType T = Callee->getType(); |
| 4024 | |
| 4025 | if (T == Context.BoundMemberTy) { |
| 4026 | // Handle pointer-to-members. |
| 4027 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) |
| 4028 | T = BinOp->getRHS()->getType(); |
| 4029 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) |
| 4030 | T = Mem->getMemberDecl()->getType(); |
| 4031 | } |
| 4032 | |
| 4033 | if (const PointerType *Ptr = T->getAs<PointerType>()) |
| 4034 | T = Ptr->getPointeeType(); |
| 4035 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) |
| 4036 | T = Ptr->getPointeeType(); |
| 4037 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) |
| 4038 | T = MemPtr->getPointeeType(); |
| 4039 | |
| 4040 | const FunctionType *FTy = T->getAs<FunctionType>(); |
| 4041 | assert(FTy && "call to value not of function type?"); |
| 4042 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); |
| 4043 | |
| 4044 | // ActOnStmtExpr arranges things so that StmtExprs of retainable |
| 4045 | // type always produce a +1 object. |
| 4046 | } else if (isa<StmtExpr>(E)) { |
| 4047 | ReturnsRetained = true; |
| 4048 | |
| 4049 | // For message sends and property references, we try to find an |
| 4050 | // actual method. FIXME: we should infer retention by selector in |
| 4051 | // cases where we don't have an actual method. |
| 4052 | } else { |
| 4053 | Decl *D = 0; |
| 4054 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { |
| 4055 | D = Send->getMethodDecl(); |
| 4056 | } else { |
| 4057 | CastExpr *CE = cast<CastExpr>(E); |
| 4058 | // FIXME. What other cast kinds to check for? |
| 4059 | if (CE->getCastKind() == CK_ObjCProduceObject || |
| 4060 | CE->getCastKind() == CK_LValueToRValue) |
| 4061 | return MaybeBindToTemporary(CE->getSubExpr()); |
| 4062 | assert(CE->getCastKind() == CK_GetObjCProperty); |
| 4063 | const ObjCPropertyRefExpr *PRE = CE->getSubExpr()->getObjCProperty(); |
| 4064 | D = (PRE->isImplicitProperty() ? PRE->getImplicitPropertyGetter() : 0); |
| 4065 | } |
| 4066 | |
| 4067 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); |
| 4068 | } |
| 4069 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4070 | ExprNeedsCleanups = true; |
| 4071 | |
| 4072 | CastKind ck = (ReturnsRetained ? CK_ObjCConsumeObject |
| 4073 | : CK_ObjCReclaimReturnedObject); |
| 4074 | return Owned(ImplicitCastExpr::Create(Context, E->getType(), ck, E, 0, |
| 4075 | VK_RValue)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | if (!getLangOptions().CPlusPlus) |
| 4079 | return Owned(E); |
Douglas Gregor | 5132655 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 4080 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4081 | const RecordType *RT = E->getType()->getAs<RecordType>(); |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 4082 | if (!RT) |
| 4083 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4085 | // That should be enough to guarantee that this type is complete. |
| 4086 | // If it has a trivial destructor, we can avoid the extra copy. |
Jeffrey Yasskin | b7ee2e5 | 2011-01-27 19:17:54 +0000 | [diff] [blame] | 4087 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 507384f | 2010-08-12 02:40:37 +0000 | [diff] [blame] | 4088 | if (RD->isInvalidDecl() || RD->hasTrivialDestructor()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4089 | return Owned(E); |
| 4090 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4091 | CXXDestructorDecl *Destructor = LookupDestructor(RD); |
| 4092 | |
| 4093 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); |
| 4094 | if (Destructor) { |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 4095 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 4096 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 4097 | PDiag(diag::err_access_dtor_temp) |
| 4098 | << E->getType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4099 | |
| 4100 | ExprTemporaries.push_back(Temp); |
| 4101 | ExprNeedsCleanups = true; |
John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 4102 | } |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 4103 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); |
| 4104 | } |
| 4105 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4106 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 4107 | assert(SubExpr && "sub expression can't be null!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4108 | |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 4109 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; |
| 4110 | assert(ExprTemporaries.size() >= FirstTemporary); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4111 | assert(ExprNeedsCleanups || ExprTemporaries.size() == FirstTemporary); |
| 4112 | if (!ExprNeedsCleanups) |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 4113 | return SubExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4114 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4115 | Expr *E = ExprWithCleanups::Create(Context, SubExpr, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4116 | ExprTemporaries.begin() + FirstTemporary, |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4117 | ExprTemporaries.size() - FirstTemporary); |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 4118 | ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, |
| 4119 | ExprTemporaries.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4120 | ExprNeedsCleanups = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 4122 | return E; |
| 4123 | } |
| 4124 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4125 | ExprResult |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4126 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4127 | if (SubExpr.isInvalid()) |
| 4128 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4129 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4130 | return Owned(MaybeCreateExprWithCleanups(SubExpr.take())); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4133 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4134 | assert(SubStmt && "sub statement can't be null!"); |
| 4135 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4136 | if (!ExprNeedsCleanups) |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4137 | return SubStmt; |
| 4138 | |
| 4139 | // FIXME: In order to attach the temporaries, wrap the statement into |
| 4140 | // a StmtExpr; currently this is only used for asm statements. |
| 4141 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
| 4142 | // a new AsmStmtWithTemporaries. |
| 4143 | CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, &SubStmt, 1, |
| 4144 | SourceLocation(), |
| 4145 | SourceLocation()); |
| 4146 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
| 4147 | SourceLocation()); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4148 | return MaybeCreateExprWithCleanups(E); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4151 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4152 | Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4153 | tok::TokenKind OpKind, ParsedType &ObjectType, |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4154 | bool &MayBePseudoDestructor) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4155 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4156 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4157 | if (Result.isInvalid()) return ExprError(); |
| 4158 | Base = Result.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4159 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4160 | QualType BaseType = Base->getType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4161 | MayBePseudoDestructor = false; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4162 | if (BaseType->isDependentType()) { |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 4163 | // If we have a pointer to a dependent type and are using the -> operator, |
| 4164 | // the object type is the type that the pointer points to. We might still |
| 4165 | // have enough information about that type to do something useful. |
| 4166 | if (OpKind == tok::arrow) |
| 4167 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4168 | BaseType = Ptr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4169 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4170 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4171 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4172 | return Owned(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4173 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4174 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4175 | // C++ [over.match.oper]p8: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4176 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4177 | // returned, with the original second operand. |
| 4178 | if (OpKind == tok::arrow) { |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4179 | // The set of types we've considered so far. |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4180 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4181 | SmallVector<SourceLocation, 8> Locations; |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4182 | CTypes.insert(Context.getCanonicalType(BaseType)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4183 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4184 | while (BaseType->isRecordType()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4185 | Result = BuildOverloadedArrowExpr(S, Base, OpLoc); |
| 4186 | if (Result.isInvalid()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4187 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4188 | Base = Result.get(); |
| 4189 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Anders Carlsson | de699e5 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 4190 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4191 | BaseType = Base->getType(); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4192 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4193 | if (!CTypes.insert(CBaseType)) { |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4194 | Diag(OpLoc, diag::err_operator_arrow_circular); |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 4195 | for (unsigned i = 0; i < Locations.size(); i++) |
| 4196 | Diag(Locations[i], diag::note_declared_at); |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4197 | return ExprError(); |
| 4198 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4199 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4200 | |
Douglas Gregor | 31658df | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 4201 | if (BaseType->isPointerType()) |
| 4202 | BaseType = BaseType->getPointeeType(); |
| 4203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4204 | |
| 4205 | // 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] | 4206 | // vector types or Objective-C interfaces. Just return early and let |
| 4207 | // ActOnMemberReferenceExpr do the work. |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4208 | if (!BaseType->isRecordType()) { |
| 4209 | // C++ [basic.lookup.classref]p2: |
| 4210 | // [...] If the type of the object expression is of pointer to scalar |
| 4211 | // type, the unqualified-id is looked up in the context of the complete |
| 4212 | // postfix-expression. |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4213 | // |
| 4214 | // This also indicates that we should be parsing a |
| 4215 | // pseudo-destructor-name. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4216 | ObjectType = ParsedType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4217 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4218 | return Owned(Base); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4219 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4220 | |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4221 | // The object type must be complete (or dependent). |
| 4222 | if (!BaseType->isDependentType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4223 | RequireCompleteType(OpLoc, BaseType, |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4224 | PDiag(diag::err_incomplete_member_access))) |
| 4225 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4226 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4227 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4228 | // 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] | 4229 | // 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] | 4230 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 4231 | // up in the scope of class C. [...] |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4232 | ObjectType = ParsedType::make(BaseType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4233 | return move(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4236 | ExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4237 | Expr *MemExpr) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4238 | SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4239 | Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call) |
| 4240 | << isa<CXXPseudoDestructorExpr>(MemExpr) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4241 | << FixItHint::CreateInsertion(ExpectedLParenLoc, "()"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4242 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4243 | return ActOnCallExpr(/*Scope*/ 0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4244 | MemExpr, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4245 | /*LPLoc*/ ExpectedLParenLoc, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4246 | MultiExprArg(), |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4247 | /*RPLoc*/ ExpectedLParenLoc); |
| 4248 | } |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4249 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4250 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4251 | SourceLocation OpLoc, |
| 4252 | tok::TokenKind OpKind, |
| 4253 | const CXXScopeSpec &SS, |
| 4254 | TypeSourceInfo *ScopeTypeInfo, |
| 4255 | SourceLocation CCLoc, |
| 4256 | SourceLocation TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4257 | PseudoDestructorTypeStorage Destructed, |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4258 | bool HasTrailingLParen) { |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4259 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4260 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4261 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 | // 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] | 4263 | // 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] | 4264 | // This scalar type is the object type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4265 | QualType ObjectType = Base->getType(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4266 | if (OpKind == tok::arrow) { |
| 4267 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 4268 | ObjectType = Ptr->getPointeeType(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4269 | } else if (!Base->isTypeDependent()) { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4270 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 4271 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 4272 | << ObjectType << true |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4273 | << FixItHint::CreateReplacement(OpLoc, "."); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4274 | if (isSFINAEContext()) |
| 4275 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4277 | OpKind = tok::period; |
| 4278 | } |
| 4279 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4280 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4281 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType()) { |
| 4282 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4283 | << ObjectType << Base->getSourceRange(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4284 | return ExprError(); |
| 4285 | } |
| 4286 | |
| 4287 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4288 | // [...] The cv-unqualified versions of the object type and of the type |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4289 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4290 | if (DestructedTypeInfo) { |
| 4291 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 4292 | SourceLocation DestructedTypeStart |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4293 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4294 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { |
| 4295 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
| 4296 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
| 4297 | << ObjectType << DestructedType << Base->getSourceRange() |
| 4298 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4299 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4300 | // Recover by setting the destructed type to the object type. |
| 4301 | DestructedType = ObjectType; |
| 4302 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4303 | DestructedTypeStart); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4304 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4305 | } else if (DestructedType.getObjCLifetime() != |
| 4306 | ObjectType.getObjCLifetime()) { |
| 4307 | |
| 4308 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { |
| 4309 | // Okay: just pretend that the user provided the correctly-qualified |
| 4310 | // type. |
| 4311 | } else { |
| 4312 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) |
| 4313 | << ObjectType << DestructedType << Base->getSourceRange() |
| 4314 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
| 4315 | } |
| 4316 | |
| 4317 | // Recover by setting the destructed type to the object type. |
| 4318 | DestructedType = ObjectType; |
| 4319 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 4320 | DestructedTypeStart); |
| 4321 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4322 | } |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4323 | } |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4324 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4325 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4326 | // C++ [expr.pseudo]p2: |
| 4327 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 4328 | // form |
| 4329 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4330 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4331 | // |
| 4332 | // shall designate the same scalar type. |
| 4333 | if (ScopeTypeInfo) { |
| 4334 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 4335 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | 81e317a | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 4336 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4337 | |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4338 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4339 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4340 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4341 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
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 | ScopeType = QualType(); |
| 4344 | ScopeTypeInfo = 0; |
| 4345 | } |
| 4346 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4347 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4348 | Expr *Result |
| 4349 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 4350 | OpKind == tok::arrow, OpLoc, |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 4351 | SS.getWithLocInContext(Context), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4352 | ScopeTypeInfo, |
| 4353 | CCLoc, |
| 4354 | TildeLoc, |
| 4355 | Destructed); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4356 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4357 | if (HasTrailingLParen) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4358 | return Owned(Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4359 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4360 | return DiagnoseDtorReference(Destructed.getLocation(), Result); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4363 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4364 | SourceLocation OpLoc, |
| 4365 | tok::TokenKind OpKind, |
| 4366 | CXXScopeSpec &SS, |
| 4367 | UnqualifiedId &FirstTypeName, |
| 4368 | SourceLocation CCLoc, |
| 4369 | SourceLocation TildeLoc, |
| 4370 | UnqualifiedId &SecondTypeName, |
| 4371 | bool HasTrailingLParen) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4372 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 4373 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 4374 | "Invalid first type name in pseudo-destructor"); |
| 4375 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 4376 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 4377 | "Invalid second type name in pseudo-destructor"); |
| 4378 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4379 | // C++ [expr.pseudo]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | // 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] | 4381 | // 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] | 4382 | // This scalar type is the object type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4383 | QualType ObjectType = Base->getType(); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4384 | if (OpKind == tok::arrow) { |
| 4385 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 4386 | ObjectType = Ptr->getPointeeType(); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4387 | } else if (!ObjectType->isDependentType()) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4388 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 4389 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4390 | << ObjectType << true |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4391 | << FixItHint::CreateReplacement(OpLoc, "."); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4392 | if (isSFINAEContext()) |
| 4393 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4394 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4395 | OpKind = tok::period; |
| 4396 | } |
| 4397 | } |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4398 | |
| 4399 | // Compute the object type that we should use for name lookup purposes. Only |
| 4400 | // record types and dependent types matter. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4401 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4402 | if (!SS.isSet()) { |
John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4403 | if (ObjectType->isRecordType()) |
| 4404 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4405 | else if (ObjectType->isDependentType()) |
| 4406 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4407 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4408 | |
| 4409 | // Convert the name of the type being destructed (following the ~) into a |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4410 | // type (with source-location information). |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4411 | QualType DestructedType; |
| 4412 | TypeSourceInfo *DestructedTypeInfo = 0; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4413 | PseudoDestructorTypeStorage Destructed; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4414 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4415 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4416 | SecondTypeName.StartLocation, |
Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 4417 | S, &SS, true, false, ObjectTypePtrForLookup); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4418 | if (!T && |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4419 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 4420 | (!SS.isSet() && ObjectType->isDependentType()))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4421 | // 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] | 4422 | // couldn't find anything useful in scope. Just store the identifier and |
| 4423 | // it's location, and we'll perform (qualified) name lookup again at |
| 4424 | // template instantiation time. |
| 4425 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 4426 | SecondTypeName.StartLocation); |
| 4427 | } else if (!T) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4428 | Diag(SecondTypeName.StartLocation, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4429 | diag::err_pseudo_dtor_destructor_non_type) |
| 4430 | << SecondTypeName.Identifier << ObjectType; |
| 4431 | if (isSFINAEContext()) |
| 4432 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4433 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4434 | // Recover by assuming we had the right type all along. |
| 4435 | DestructedType = ObjectType; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4436 | } else |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4437 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4438 | } else { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4439 | // Resolve the template-id to a type. |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4440 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4441 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4442 | TemplateId->getTemplateArgs(), |
| 4443 | TemplateId->NumArgs); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 4444 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
| 4445 | TemplateId->Template, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4446 | TemplateId->TemplateNameLoc, |
| 4447 | TemplateId->LAngleLoc, |
| 4448 | TemplateArgsPtr, |
| 4449 | TemplateId->RAngleLoc); |
| 4450 | if (T.isInvalid() || !T.get()) { |
| 4451 | // Recover by assuming we had the right type all along. |
| 4452 | DestructedType = ObjectType; |
| 4453 | } else |
| 4454 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4455 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4456 | |
| 4457 | // 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] | 4458 | // information. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4459 | if (!DestructedType.isNull()) { |
| 4460 | if (!DestructedTypeInfo) |
| 4461 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4462 | SecondTypeName.StartLocation); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4463 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 4464 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4465 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4466 | // Convert the name of the scope type (the type prior to '::') into a type. |
| 4467 | TypeSourceInfo *ScopeTypeInfo = 0; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4468 | QualType ScopeType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4469 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4470 | FirstTypeName.Identifier) { |
| 4471 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4473 | FirstTypeName.StartLocation, |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 4474 | S, &SS, true, false, ObjectTypePtrForLookup); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4475 | if (!T) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4476 | Diag(FirstTypeName.StartLocation, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4477 | diag::err_pseudo_dtor_destructor_non_type) |
| 4478 | << FirstTypeName.Identifier << ObjectType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4479 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4480 | if (isSFINAEContext()) |
| 4481 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4482 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4483 | // Just drop this type. It's unnecessary anyway. |
| 4484 | ScopeType = QualType(); |
| 4485 | } else |
| 4486 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4487 | } else { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4488 | // Resolve the template-id to a type. |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4489 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4490 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4491 | TemplateId->getTemplateArgs(), |
| 4492 | TemplateId->NumArgs); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 4493 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, |
| 4494 | TemplateId->Template, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4495 | TemplateId->TemplateNameLoc, |
| 4496 | TemplateId->LAngleLoc, |
| 4497 | TemplateArgsPtr, |
| 4498 | TemplateId->RAngleLoc); |
| 4499 | if (T.isInvalid() || !T.get()) { |
| 4500 | // Recover by dropping this type. |
| 4501 | ScopeType = QualType(); |
| 4502 | } else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4503 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4504 | } |
| 4505 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4506 | |
Douglas Gregor | b4a418f | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 4507 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 4508 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 4509 | FirstTypeName.StartLocation); |
| 4510 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4511 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4512 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 4513 | ScopeTypeInfo, CCLoc, TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4514 | Destructed, HasTrailingLParen); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4517 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4518 | CXXMethodDecl *Method) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4519 | ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/0, |
| 4520 | FoundDecl, Method); |
| 4521 | if (Exp.isInvalid()) |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4522 | return true; |
Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 4523 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4524 | MemberExpr *ME = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4525 | new (Context) MemberExpr(Exp.take(), /*IsArrow=*/false, Method, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4526 | SourceLocation(), Method->getType(), |
| 4527 | VK_RValue, OK_Ordinary); |
| 4528 | QualType ResultType = Method->getResultType(); |
| 4529 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 4530 | ResultType = ResultType.getNonLValueExprType(Context); |
| 4531 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4532 | MarkDeclarationReferenced(Exp.get()->getLocStart(), Method); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4533 | CXXMemberCallExpr *CE = |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4534 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, VK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4535 | Exp.get()->getLocEnd()); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 4536 | return CE; |
| 4537 | } |
| 4538 | |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4539 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 4540 | SourceLocation RParen) { |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4541 | return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand, |
| 4542 | Operand->CanThrow(Context), |
| 4543 | KeyLoc, RParen)); |
| 4544 | } |
| 4545 | |
| 4546 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 4547 | Expr *Operand, SourceLocation RParen) { |
| 4548 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4551 | /// Perform the conversions required for an expression used in a |
| 4552 | /// context that ignores the result. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4553 | ExprResult Sema::IgnoredValueConversions(Expr *E) { |
John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 4554 | // C99 6.3.2.1: |
| 4555 | // [Except in specific positions,] an lvalue that does not have |
| 4556 | // array type is converted to the value stored in the |
| 4557 | // designated object (and is no longer an lvalue). |
John McCall | e6d134b | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 4558 | if (E->isRValue()) { |
| 4559 | // In C, function designators (i.e. expressions of function type) |
| 4560 | // are r-values, but we still want to do function-to-pointer decay |
| 4561 | // on them. This is both technically correct and convenient for |
| 4562 | // some clients. |
| 4563 | if (!getLangOptions().CPlusPlus && E->getType()->isFunctionType()) |
| 4564 | return DefaultFunctionArrayConversion(E); |
| 4565 | |
| 4566 | return Owned(E); |
| 4567 | } |
John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 4568 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4569 | // We always want to do this on ObjC property references. |
| 4570 | if (E->getObjectKind() == OK_ObjCProperty) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4571 | ExprResult Res = ConvertPropertyForRValue(E); |
| 4572 | if (Res.isInvalid()) return Owned(E); |
| 4573 | E = Res.take(); |
| 4574 | if (E->isRValue()) return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | // 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] | 4578 | if (getLangOptions().CPlusPlus) return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4579 | |
| 4580 | // GCC seems to also exclude expressions of incomplete enum type. |
| 4581 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
| 4582 | if (!T->getDecl()->isComplete()) { |
| 4583 | // FIXME: stupid workaround for a codegen bug! |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4584 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).take(); |
| 4585 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4586 | } |
| 4587 | } |
| 4588 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4589 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 4590 | if (Res.isInvalid()) |
| 4591 | return Owned(E); |
| 4592 | E = Res.take(); |
| 4593 | |
John McCall | 85515d6 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 4594 | if (!E->getType()->isVoidType()) |
| 4595 | RequireCompleteType(E->getExprLoc(), E->getType(), |
| 4596 | diag::err_incomplete_type); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4597 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4600 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE) { |
| 4601 | ExprResult FullExpr = Owned(FE); |
| 4602 | |
| 4603 | if (!FullExpr.get()) |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4604 | return ExprError(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4605 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4606 | if (DiagnoseUnexpandedParameterPack(FullExpr.get())) |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 4607 | return ExprError(); |
| 4608 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4609 | FullExpr = CheckPlaceholderExpr(FullExpr.take()); |
| 4610 | if (FullExpr.isInvalid()) |
| 4611 | return ExprError(); |
Douglas Gregor | 353ee24 | 2011-03-07 02:05:23 +0000 | [diff] [blame] | 4612 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4613 | FullExpr = IgnoredValueConversions(FullExpr.take()); |
| 4614 | if (FullExpr.isInvalid()) |
| 4615 | return ExprError(); |
| 4616 | |
| 4617 | CheckImplicitConversions(FullExpr.get()); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4618 | return MaybeCreateExprWithCleanups(FullExpr); |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 4619 | } |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4620 | |
| 4621 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
| 4622 | if (!FullStmt) return StmtError(); |
| 4623 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4624 | return MaybeCreateStmtWithCleanups(FullStmt); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4625 | } |
Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4626 | |
| 4627 | bool Sema::CheckMicrosoftIfExistsSymbol(CXXScopeSpec &SS, |
| 4628 | UnqualifiedId &Name) { |
| 4629 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 4630 | DeclarationName TargetName = TargetNameInfo.getName(); |
| 4631 | if (!TargetName) |
| 4632 | return false; |
| 4633 | |
| 4634 | // Do the redeclaration lookup in the current scope. |
| 4635 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, |
| 4636 | Sema::NotForRedeclaration); |
| 4637 | R.suppressDiagnostics(); |
| 4638 | LookupParsedName(R, getCurScope(), &SS); |
| 4639 | return !R.empty(); |
| 4640 | } |