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