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" |
| 19 | #include "clang/Sema/TemplateDeduction.h" |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 21 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Fariborz Jahanian | d426662 | 2010-06-16 18:56:04 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 26 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/STLExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 31 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 33 | ParsedType Sema::getDestructorName(SourceLocation TildeLoc, |
| 34 | IdentifierInfo &II, |
| 35 | SourceLocation NameLoc, |
| 36 | Scope *S, CXXScopeSpec &SS, |
| 37 | ParsedType ObjectTypePtr, |
| 38 | bool EnteringContext) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 39 | // Determine where to perform name lookup. |
| 40 | |
| 41 | // FIXME: This area of the standard is very messy, and the current |
| 42 | // wording is rather unclear about which scopes we search for the |
| 43 | // destructor name; see core issues 399 and 555. Issue 399 in |
| 44 | // particular shows where the current description of destructor name |
| 45 | // lookup is completely out of line with existing practice, e.g., |
| 46 | // this appears to be ill-formed: |
| 47 | // |
| 48 | // namespace N { |
| 49 | // template <typename T> struct S { |
| 50 | // ~S(); |
| 51 | // }; |
| 52 | // } |
| 53 | // |
| 54 | // void f(N::S<int>* s) { |
| 55 | // s->N::S<int>::~S(); |
| 56 | // } |
| 57 | // |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 58 | // See also PR6358 and PR6359. |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 59 | // For this reason, we're currently only doing the C++03 version of this |
| 60 | // 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] | 61 | QualType SearchType; |
| 62 | DeclContext *LookupCtx = 0; |
| 63 | bool isDependent = false; |
| 64 | bool LookInScope = false; |
| 65 | |
| 66 | // If we have an object type, it's because we are in a |
| 67 | // pseudo-destructor-expression or a member access expression, and |
| 68 | // we know what type we're looking for. |
| 69 | if (ObjectTypePtr) |
| 70 | SearchType = GetTypeFromParser(ObjectTypePtr); |
| 71 | |
| 72 | if (SS.isSet()) { |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 73 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 74 | |
| 75 | bool AlreadySearched = false; |
| 76 | bool LookAtPrefix = true; |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 77 | // C++ [basic.lookup.qual]p6: |
| 78 | // If a pseudo-destructor-name (5.2.4) contains a nested-name-specifier, |
| 79 | // the type-names are looked up as types in the scope designated by the |
| 80 | // nested-name-specifier. In a qualified-id of the form: |
| 81 | // |
| 82 | // ::[opt] nested-name-specifier ̃ class-name |
| 83 | // |
| 84 | // where the nested-name-specifier designates a namespace scope, and in |
Chandler Carruth | 5e895a8 | 2010-02-21 10:19:54 +0000 | [diff] [blame] | 85 | // a qualified-id of the form: |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 86 | // |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 87 | // ::opt nested-name-specifier class-name :: ̃ class-name |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 88 | // |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 89 | // the class-names are looked up as types in the scope designated by |
| 90 | // the nested-name-specifier. |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 91 | // |
Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 92 | // Here, we check the first case (completely) and determine whether the |
| 93 | // code below is permitted to look at the prefix of the |
| 94 | // nested-name-specifier. |
| 95 | DeclContext *DC = computeDeclContext(SS, EnteringContext); |
| 96 | if (DC && DC->isFileContext()) { |
| 97 | AlreadySearched = true; |
| 98 | LookupCtx = DC; |
| 99 | isDependent = false; |
| 100 | } else if (DC && isa<CXXRecordDecl>(DC)) |
| 101 | LookAtPrefix = false; |
| 102 | |
| 103 | // The second case from the C++03 rules quoted further above. |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 104 | NestedNameSpecifier *Prefix = 0; |
| 105 | if (AlreadySearched) { |
| 106 | // Nothing left to do. |
| 107 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { |
| 108 | CXXScopeSpec PrefixSS; |
| 109 | PrefixSS.setScopeRep(Prefix); |
| 110 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); |
| 111 | isDependent = isDependentScopeSpecifier(PrefixSS); |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 112 | } else if (ObjectTypePtr) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 113 | LookupCtx = computeDeclContext(SearchType); |
| 114 | isDependent = SearchType->isDependentType(); |
| 115 | } else { |
| 116 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 117 | isDependent = LookupCtx && LookupCtx->isDependentContext(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 118 | } |
Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 120 | LookInScope = false; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 121 | } else if (ObjectTypePtr) { |
| 122 | // C++ [basic.lookup.classref]p3: |
| 123 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 124 | // in the context of the entire postfix-expression. If the type T |
| 125 | // of the object expression is of a class type C, the type-name is |
| 126 | // also looked up in the scope of class C. At least one of the |
| 127 | // lookups shall find a name that refers to (possibly |
| 128 | // cv-qualified) T. |
| 129 | LookupCtx = computeDeclContext(SearchType); |
| 130 | isDependent = SearchType->isDependentType(); |
| 131 | assert((isDependent || !SearchType->isIncompleteType()) && |
| 132 | "Caller should have completed object type"); |
| 133 | |
| 134 | LookInScope = true; |
| 135 | } else { |
| 136 | // Perform lookup into the current scope (only). |
| 137 | LookInScope = true; |
| 138 | } |
| 139 | |
| 140 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); |
| 141 | for (unsigned Step = 0; Step != 2; ++Step) { |
| 142 | // Look for the name first in the computed lookup context (if we |
| 143 | // have one) and, if that fails to find a match, in the sope (if |
| 144 | // we're allowed to look there). |
| 145 | Found.clear(); |
| 146 | if (Step == 0 && LookupCtx) |
| 147 | LookupQualifiedName(Found, LookupCtx); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 148 | else if (Step == 1 && LookInScope && S) |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 149 | LookupName(Found, S); |
| 150 | else |
| 151 | continue; |
| 152 | |
| 153 | // FIXME: Should we be suppressing ambiguities here? |
| 154 | if (Found.isAmbiguous()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 155 | return ParsedType(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 156 | |
| 157 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
| 158 | QualType T = Context.getTypeDeclType(Type); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 159 | |
| 160 | if (SearchType.isNull() || SearchType->isDependentType() || |
| 161 | Context.hasSameUnqualifiedType(T, SearchType)) { |
| 162 | // We found our type! |
| 163 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 164 | return ParsedType::make(T); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | // If the name that we found is a class template name, and it is |
| 169 | // the same name as the template name in the last part of the |
| 170 | // nested-name-specifier (if present) or the object type, then |
| 171 | // this is the destructor for that class. |
| 172 | // FIXME: This is a workaround until we get real drafting for core |
| 173 | // issue 399, for which there isn't even an obvious direction. |
| 174 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
| 175 | QualType MemberOfType; |
| 176 | if (SS.isSet()) { |
| 177 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
| 178 | // Figure out the type of the context, if it has one. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 179 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) |
| 180 | MemberOfType = Context.getTypeDeclType(Record); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | if (MemberOfType.isNull()) |
| 184 | MemberOfType = SearchType; |
| 185 | |
| 186 | if (MemberOfType.isNull()) |
| 187 | continue; |
| 188 | |
| 189 | // We're referring into a class template specialization. If the |
| 190 | // class template we found is the same as the template being |
| 191 | // specialized, we found what we are looking for. |
| 192 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
| 193 | if (ClassTemplateSpecializationDecl *Spec |
| 194 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
| 195 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
| 196 | Template->getCanonicalDecl()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 197 | return ParsedType::make(MemberOfType); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | // We're referring to an unresolved class template |
| 204 | // specialization. Determine whether we class template we found |
| 205 | // is the same as the template being specialized or, if we don't |
| 206 | // know which template is being specialized, that it at least |
| 207 | // has the same name. |
| 208 | if (const TemplateSpecializationType *SpecType |
| 209 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
| 210 | TemplateName SpecName = SpecType->getTemplateName(); |
| 211 | |
| 212 | // The class template we found is the same template being |
| 213 | // specialized. |
| 214 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
| 215 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 216 | return ParsedType::make(MemberOfType); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 217 | |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | // The class template we found has the same name as the |
| 222 | // (dependent) template name being specialized. |
| 223 | if (DependentTemplateName *DepTemplate |
| 224 | = SpecName.getAsDependentTemplateName()) { |
| 225 | if (DepTemplate->isIdentifier() && |
| 226 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 227 | return ParsedType::make(MemberOfType); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 228 | |
| 229 | continue; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (isDependent) { |
| 236 | // We didn't find our type, but that's okay: it's dependent |
| 237 | // anyway. |
| 238 | NestedNameSpecifier *NNS = 0; |
| 239 | SourceRange Range; |
| 240 | if (SS.isSet()) { |
| 241 | NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 242 | Range = SourceRange(SS.getRange().getBegin(), NameLoc); |
| 243 | } else { |
| 244 | NNS = NestedNameSpecifier::Create(Context, &II); |
| 245 | Range = SourceRange(NameLoc); |
| 246 | } |
| 247 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 248 | QualType T = CheckTypenameType(ETK_None, NNS, II, |
| 249 | SourceLocation(), |
| 250 | Range, NameLoc); |
| 251 | return ParsedType::make(T); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (ObjectTypePtr) |
| 255 | Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type) |
| 256 | << &II; |
| 257 | else |
| 258 | Diag(NameLoc, diag::err_destructor_class_name); |
| 259 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 260 | return ParsedType(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 263 | /// \brief Build a C++ typeid expression with a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 264 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 265 | SourceLocation TypeidLoc, |
| 266 | TypeSourceInfo *Operand, |
| 267 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 268 | // C++ [expr.typeid]p4: |
| 269 | // The top-level cv-qualifiers of the lvalue expression or the type-id |
| 270 | // that is the operand of typeid are always ignored. |
| 271 | // If the type of the type-id is a class type or a reference to a class |
| 272 | // type, the class shall be completely-defined. |
Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 273 | Qualifiers Quals; |
| 274 | QualType T |
| 275 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), |
| 276 | Quals); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 277 | if (T->getAs<RecordType>() && |
| 278 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 279 | return ExprError(); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 281 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
| 282 | Operand, |
| 283 | SourceRange(TypeidLoc, RParenLoc))); |
| 284 | } |
| 285 | |
| 286 | /// \brief Build a C++ typeid expression with an expression operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 287 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 288 | SourceLocation TypeidLoc, |
| 289 | Expr *E, |
| 290 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 291 | bool isUnevaluatedOperand = true; |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 292 | if (E && !E->isTypeDependent()) { |
| 293 | QualType T = E->getType(); |
| 294 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
| 295 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 296 | // C++ [expr.typeid]p3: |
| 297 | // [...] If the type of the expression is a class type, the class |
| 298 | // shall be completely-defined. |
| 299 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) |
| 300 | return ExprError(); |
| 301 | |
| 302 | // C++ [expr.typeid]p3: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 303 | // 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] | 304 | // polymorphic class type [...] [the] expression is an unevaluated |
| 305 | // operand. [...] |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 306 | if (RecordD->isPolymorphic() && E->Classify(Context).isGLValue()) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 307 | isUnevaluatedOperand = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 308 | |
| 309 | // We require a vtable to query the type at run time. |
| 310 | MarkVTableUsed(TypeidLoc, RecordD); |
| 311 | } |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // C++ [expr.typeid]p4: |
| 315 | // [...] If the type of the type-id is a reference to a possibly |
| 316 | // cv-qualified type, the result of the typeid expression refers to a |
| 317 | // std::type_info object representing the cv-unqualified referenced |
| 318 | // type. |
Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 319 | Qualifiers Quals; |
| 320 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); |
| 321 | if (!Context.hasSameType(T, UnqualT)) { |
| 322 | T = UnqualT; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 323 | ImpCastExprToType(E, UnqualT, CK_NoOp, CastCategory(E)); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | // If this is an unevaluated operand, clear out the set of |
| 328 | // declaration references we have been computing and eliminate any |
| 329 | // temporaries introduced in its computation. |
| 330 | if (isUnevaluatedOperand) |
| 331 | ExprEvalContexts.back().Context = Unevaluated; |
| 332 | |
| 333 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 334 | E, |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 335 | SourceRange(TypeidLoc, RParenLoc))); |
| 336 | } |
| 337 | |
| 338 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 339 | ExprResult |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 340 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 341 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 342 | // Find the std::type_info type. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 343 | if (!StdNamespace) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 344 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 345 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 346 | if (!CXXTypeInfoDecl) { |
| 347 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 348 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 349 | LookupQualifiedName(R, getStdNamespace()); |
| 350 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 351 | if (!CXXTypeInfoDecl) |
| 352 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 353 | } |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 354 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 355 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 356 | |
| 357 | if (isType) { |
| 358 | // The operand is a type; handle it as such. |
| 359 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 360 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 361 | &TInfo); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 362 | if (T.isNull()) |
| 363 | return ExprError(); |
| 364 | |
| 365 | if (!TInfo) |
| 366 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 368 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 371 | // The operand is an expression. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 372 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 375 | /// \brief Build a Microsoft __uuidof expression with a type operand. |
| 376 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 377 | SourceLocation TypeidLoc, |
| 378 | TypeSourceInfo *Operand, |
| 379 | SourceLocation RParenLoc) { |
| 380 | // FIXME: add __uuidof semantic analysis for type operand. |
| 381 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 382 | Operand, |
| 383 | SourceRange(TypeidLoc, RParenLoc))); |
| 384 | } |
| 385 | |
| 386 | /// \brief Build a Microsoft __uuidof expression with an expression operand. |
| 387 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 388 | SourceLocation TypeidLoc, |
| 389 | Expr *E, |
| 390 | SourceLocation RParenLoc) { |
| 391 | // FIXME: add __uuidof semantic analysis for expr operand. |
| 392 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 393 | E, |
| 394 | SourceRange(TypeidLoc, RParenLoc))); |
| 395 | } |
| 396 | |
| 397 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 398 | ExprResult |
| 399 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 400 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
| 401 | // If MSVCGuidDecl has not been cached, do the lookup. |
| 402 | if (!MSVCGuidDecl) { |
| 403 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 404 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 405 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 406 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 407 | if (!MSVCGuidDecl) |
| 408 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
| 409 | } |
| 410 | |
| 411 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
| 412 | |
| 413 | if (isType) { |
| 414 | // The operand is a type; handle it as such. |
| 415 | TypeSourceInfo *TInfo = 0; |
| 416 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 417 | &TInfo); |
| 418 | if (T.isNull()) |
| 419 | return ExprError(); |
| 420 | |
| 421 | if (!TInfo) |
| 422 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 423 | |
| 424 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 425 | } |
| 426 | |
| 427 | // The operand is an expression. |
| 428 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 429 | } |
| 430 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 431 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 432 | ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 433 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 434 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 435 | "Unknown C++ Boolean value!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 436 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 437 | Context.BoolTy, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 438 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 439 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 440 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 441 | ExprResult |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 442 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
| 443 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 444 | } |
| 445 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 446 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 447 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 448 | Sema::ActOnCXXThrow(SourceLocation OpLoc, Expr *Ex) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 449 | if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) |
| 450 | return ExprError(); |
| 451 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); |
| 452 | } |
| 453 | |
| 454 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
| 455 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { |
| 456 | // C++ [except.throw]p3: |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 457 | // A throw-expression initializes a temporary object, called the exception |
| 458 | // object, the type of which is determined by removing any top-level |
| 459 | // cv-qualifiers from the static type of the operand of throw and adjusting |
| 460 | // the type from "array of T" or "function returning T" to "pointer to T" |
| 461 | // or "pointer to function returning T", [...] |
| 462 | if (E->getType().hasQualifiers()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 463 | ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp, |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 464 | CastCategory(E)); |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 465 | |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 466 | DefaultFunctionArrayConversion(E); |
| 467 | |
| 468 | // If the type of the exception would be an incomplete type or a pointer |
| 469 | // to an incomplete type other than (cv) void the program is ill-formed. |
| 470 | QualType Ty = E->getType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 471 | bool isPointer = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 472 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 473 | Ty = Ptr->getPointeeType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 474 | isPointer = true; |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 475 | } |
| 476 | if (!isPointer || !Ty->isVoidType()) { |
| 477 | if (RequireCompleteType(ThrowLoc, Ty, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 478 | PDiag(isPointer ? diag::err_throw_incomplete_ptr |
| 479 | : diag::err_throw_incomplete) |
| 480 | << E->getSourceRange())) |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 481 | return true; |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | bf422f9 | 2010-04-15 18:05:39 +0000 | [diff] [blame] | 483 | if (RequireNonAbstractType(ThrowLoc, E->getType(), |
| 484 | PDiag(diag::err_throw_abstract_type) |
| 485 | << E->getSourceRange())) |
| 486 | return true; |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 487 | } |
| 488 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 489 | // Initialize the exception result. This implicitly weeds out |
| 490 | // abstract types or types with inaccessible copy constructors. |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 491 | // FIXME: Determine whether we can elide this copy per C++0x [class.copy]p34. |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 492 | InitializedEntity Entity = |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 493 | InitializedEntity::InitializeException(ThrowLoc, E->getType(), |
| 494 | /*NRVO=*/false); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 495 | ExprResult Res = PerformCopyInitialization(Entity, |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 496 | SourceLocation(), |
| 497 | Owned(E)); |
| 498 | if (Res.isInvalid()) |
| 499 | return true; |
| 500 | E = Res.takeAs<Expr>(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 501 | |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 502 | // If the exception has class type, we need additional handling. |
| 503 | const RecordType *RecordTy = Ty->getAs<RecordType>(); |
| 504 | if (!RecordTy) |
| 505 | return false; |
| 506 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 507 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 508 | // If we are throwing a polymorphic class type or pointer thereof, |
| 509 | // exception handling will make use of the vtable. |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 510 | MarkVTableUsed(ThrowLoc, RD); |
| 511 | |
| 512 | // If the class has a non-trivial destructor, we must be able to call it. |
| 513 | if (RD->hasTrivialDestructor()) |
| 514 | return false; |
| 515 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 516 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 517 | = const_cast<CXXDestructorDecl*>(LookupDestructor(RD)); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 518 | if (!Destructor) |
| 519 | return false; |
| 520 | |
| 521 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
| 522 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 523 | PDiag(diag::err_access_dtor_exception) << Ty); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 524 | return false; |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 525 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 526 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 527 | ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 528 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 529 | /// is a non-lvalue expression whose value is the address of the object for |
| 530 | /// which the function is called. |
| 531 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 532 | DeclContext *DC = getFunctionLevelDeclContext(); |
| 533 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 534 | if (MD->isInstance()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 535 | return Owned(new (Context) CXXThisExpr(ThisLoc, |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 536 | MD->getThisType(Context), |
| 537 | /*isImplicit=*/false)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 538 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 539 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 540 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 541 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 542 | ExprResult |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 543 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 544 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 545 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 546 | SourceLocation RParenLoc) { |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 547 | if (!TypeRep) |
| 548 | return ExprError(); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 549 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 550 | TypeSourceInfo *TInfo; |
| 551 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 552 | if (!TInfo) |
| 553 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 554 | |
| 555 | return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); |
| 556 | } |
| 557 | |
| 558 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 559 | /// Can be interpreted either as function-style casting ("int(x)") |
| 560 | /// or class type construction ("ClassType(x,y,z)") |
| 561 | /// or creation of a value-initialized type ("int()"). |
| 562 | ExprResult |
| 563 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
| 564 | SourceLocation LParenLoc, |
| 565 | MultiExprArg exprs, |
| 566 | SourceLocation RParenLoc) { |
| 567 | QualType Ty = TInfo->getType(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 568 | unsigned NumExprs = exprs.size(); |
| 569 | Expr **Exprs = (Expr**)exprs.get(); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 570 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 571 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 572 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 573 | if (Ty->isDependentType() || |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 574 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 575 | exprs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 577 | return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo, |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 578 | LParenLoc, |
| 579 | Exprs, NumExprs, |
| 580 | RParenLoc)); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 583 | if (Ty->isArrayType()) |
| 584 | return ExprError(Diag(TyBeginLoc, |
| 585 | diag::err_value_init_for_array_type) << FullRange); |
| 586 | if (!Ty->isVoidType() && |
| 587 | RequireCompleteType(TyBeginLoc, Ty, |
| 588 | PDiag(diag::err_invalid_incomplete_type_use) |
| 589 | << FullRange)) |
| 590 | return ExprError(); |
Fariborz Jahanian | f071e9b | 2009-10-23 21:01:39 +0000 | [diff] [blame] | 591 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 592 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 593 | diag::err_allocation_of_abstract_type)) |
| 594 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | |
| 596 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 597 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 598 | // If the expression list is a single expression, the type conversion |
| 599 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 600 | // corresponding cast expression. |
| 601 | // |
| 602 | if (NumExprs == 1) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 603 | CastKind Kind = CK_Unknown; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 604 | CXXCastPath BasePath; |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 605 | if (CheckCastTypes(TInfo->getTypeLoc().getSourceRange(), Ty, Exprs[0], |
| 606 | Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 607 | /*FunctionalStyle=*/true)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 608 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 609 | |
| 610 | exprs.release(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 611 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 612 | return Owned(CXXFunctionalCastExpr::Create(Context, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 613 | Ty.getNonLValueExprType(Context), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 614 | TInfo, TyBeginLoc, Kind, |
| 615 | Exprs[0], &BasePath, |
| 616 | RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 619 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 620 | InitializationKind Kind |
| 621 | = NumExprs ? InitializationKind::CreateDirect(TyBeginLoc, |
| 622 | LParenLoc, RParenLoc) |
| 623 | : InitializationKind::CreateValue(TyBeginLoc, |
| 624 | LParenLoc, RParenLoc); |
| 625 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); |
| 626 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(exprs)); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 628 | // FIXME: Improve AST representation? |
| 629 | return move(Result); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 630 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 631 | |
| 632 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 633 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 634 | /// @code new (memory) int[size][4] @endcode |
| 635 | /// or |
| 636 | /// @code ::new Foo(23, "hello") @endcode |
| 637 | /// For the interpretation of this heap of arguments, consult the base version. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 638 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 639 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 640 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 641 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 642 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 643 | MultiExprArg ConstructorArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | SourceLocation ConstructorRParen) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 645 | Expr *ArraySize = 0; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 646 | // If the specified type is an array, unwrap it and save the expression. |
| 647 | if (D.getNumTypeObjects() > 0 && |
| 648 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 649 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
| 650 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 651 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 652 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 653 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 654 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 655 | << D.getSourceRange()); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 656 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 657 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 658 | D.DropFirstTypeObject(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 661 | // Every dimension shall be of constant size. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 662 | if (ArraySize) { |
| 663 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 664 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 665 | break; |
| 666 | |
| 667 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 668 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
| 669 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && |
| 670 | !NumElts->isIntegerConstantExpr(Context)) { |
| 671 | Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) |
| 672 | << NumElts->getSourceRange(); |
| 673 | return ExprError(); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | } |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 678 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 679 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/0); |
| 680 | QualType AllocType = TInfo->getType(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 681 | if (D.isInvalidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 682 | return ExprError(); |
Ted Kremenek | f9d5bac | 2010-06-25 22:48:49 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 684 | if (!TInfo) |
| 685 | TInfo = Context.getTrivialTypeSourceInfo(AllocType); |
| 686 | |
Ted Kremenek | f9d5bac | 2010-06-25 22:48:49 +0000 | [diff] [blame] | 687 | SourceRange R = TInfo->getTypeLoc().getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | return BuildCXXNew(StartLoc, UseGlobal, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 689 | PlacementLParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | move(PlacementArgs), |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 691 | PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 692 | TypeIdParens, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 694 | TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 695 | ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 696 | ConstructorLParen, |
| 697 | move(ConstructorArgs), |
| 698 | ConstructorRParen); |
| 699 | } |
| 700 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 701 | ExprResult |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 702 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 703 | SourceLocation PlacementLParen, |
| 704 | MultiExprArg PlacementArgs, |
| 705 | SourceLocation PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 706 | SourceRange TypeIdParens, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 707 | QualType AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 708 | TypeSourceInfo *AllocTypeInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 709 | Expr *ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 710 | SourceLocation ConstructorLParen, |
| 711 | MultiExprArg ConstructorArgs, |
| 712 | SourceLocation ConstructorRParen) { |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 713 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 715 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 716 | // typedef of an array type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 717 | if (!ArraySize) { |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 718 | if (const ConstantArrayType *Array |
| 719 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 720 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 721 | Context.getSizeType(), |
| 722 | TypeRange.getEnd()); |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 723 | AllocType = Array->getElementType(); |
| 724 | } |
| 725 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 727 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 728 | return ExprError(); |
| 729 | |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 730 | QualType ResultType = Context.getPointerType(AllocType); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 731 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 732 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 733 | // or enumeration type with a non-negative value." |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 734 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 735 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 736 | QualType SizeType = ArraySize->getType(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 737 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 738 | ExprResult ConvertedSize |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 739 | = ConvertToIntegralOrEnumerationType(StartLoc, ArraySize, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 740 | PDiag(diag::err_array_size_not_integral), |
| 741 | PDiag(diag::err_array_size_incomplete_type) |
| 742 | << ArraySize->getSourceRange(), |
| 743 | PDiag(diag::err_array_size_explicit_conversion), |
| 744 | PDiag(diag::note_array_size_conversion), |
| 745 | PDiag(diag::err_array_size_ambiguous_conversion), |
| 746 | PDiag(diag::note_array_size_conversion), |
| 747 | PDiag(getLangOptions().CPlusPlus0x? 0 |
| 748 | : diag::ext_array_size_conversion)); |
| 749 | if (ConvertedSize.isInvalid()) |
| 750 | return ExprError(); |
| 751 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 752 | ArraySize = ConvertedSize.take(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 753 | SizeType = ArraySize->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 754 | if (!SizeType->isIntegralOrEnumerationType()) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 755 | return ExprError(); |
| 756 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 757 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 758 | // We don't care about special rules, so we tell the machinery it's not |
| 759 | // evaluated - it gives us a result in more cases. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 760 | if (!ArraySize->isValueDependent()) { |
| 761 | llvm::APSInt Value; |
| 762 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 763 | if (Value < llvm::APSInt( |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 764 | llvm::APInt::getNullValue(Value.getBitWidth()), |
| 765 | Value.isUnsigned())) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 766 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 767 | diag::err_typecheck_negative_array_size) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 768 | << ArraySize->getSourceRange()); |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 769 | |
| 770 | if (!AllocType->isDependentType()) { |
| 771 | unsigned ActiveSizeBits |
| 772 | = ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
| 773 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
| 774 | Diag(ArraySize->getSourceRange().getBegin(), |
| 775 | diag::err_array_too_large) |
| 776 | << Value.toString(10) |
| 777 | << ArraySize->getSourceRange(); |
| 778 | return ExprError(); |
| 779 | } |
| 780 | } |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 781 | } else if (TypeIdParens.isValid()) { |
| 782 | // Can't have dynamic array size when the type-id is in parentheses. |
| 783 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 784 | << ArraySize->getSourceRange() |
| 785 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 786 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
| 787 | |
| 788 | TypeIdParens = SourceRange(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 789 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 790 | } |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 791 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 792 | ImpCastExprToType(ArraySize, Context.getSizeType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 793 | CK_IntegralCast); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 794 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 795 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 796 | FunctionDecl *OperatorNew = 0; |
| 797 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 798 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 799 | unsigned NumPlaceArgs = PlacementArgs.size(); |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 800 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 801 | if (!AllocType->isDependentType() && |
| 802 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && |
| 803 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 804 | SourceRange(PlacementLParen, PlacementRParen), |
| 805 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 806 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 807 | return ExprError(); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 808 | llvm::SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 809 | if (OperatorNew) { |
| 810 | // Add default arguments, if any. |
| 811 | const FunctionProtoType *Proto = |
| 812 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 813 | VariadicCallType CallType = |
| 814 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 815 | |
| 816 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, |
| 817 | Proto, 1, PlaceArgs, NumPlaceArgs, |
| 818 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 819 | return ExprError(); |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 820 | |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 821 | NumPlaceArgs = AllPlaceArgs.size(); |
| 822 | if (NumPlaceArgs > 0) |
| 823 | PlaceArgs = &AllPlaceArgs[0]; |
| 824 | } |
| 825 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 826 | bool Init = ConstructorLParen.isValid(); |
| 827 | // --- Choosing a constructor --- |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 828 | CXXConstructorDecl *Constructor = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 829 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); |
| 830 | unsigned NumConsArgs = ConstructorArgs.size(); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 831 | ASTOwningVector<Expr*> ConvertedConstructorArgs(*this); |
Eli Friedman | a8ce9ec | 2009-11-08 22:15:39 +0000 | [diff] [blame] | 832 | |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 833 | // Array 'new' can't have any initializers. |
Anders Carlsson | 55cbd6e | 2010-05-16 16:24:20 +0000 | [diff] [blame] | 834 | if (NumConsArgs && (ResultType->isArrayType() || ArraySize)) { |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 835 | SourceRange InitRange(ConsArgs[0]->getLocStart(), |
| 836 | ConsArgs[NumConsArgs - 1]->getLocEnd()); |
| 837 | |
| 838 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 839 | return ExprError(); |
| 840 | } |
| 841 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 842 | if (!AllocType->isDependentType() && |
| 843 | !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { |
| 844 | // C++0x [expr.new]p15: |
| 845 | // A new-expression that creates an object of type T initializes that |
| 846 | // object as follows: |
| 847 | InitializationKind Kind |
| 848 | // - If the new-initializer is omitted, the object is default- |
| 849 | // initialized (8.5); if no initialization is performed, |
| 850 | // the object has indeterminate value |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 851 | = !Init? InitializationKind::CreateDefault(TypeRange.getBegin()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 852 | // - Otherwise, the new-initializer is interpreted according to the |
| 853 | // initialization rules of 8.5 for direct-initialization. |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 854 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 855 | ConstructorLParen, |
| 856 | ConstructorRParen); |
| 857 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 858 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 859 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 860 | InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 861 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 862 | move(ConstructorArgs)); |
| 863 | if (FullInit.isInvalid()) |
| 864 | return ExprError(); |
| 865 | |
| 866 | // FullInit is our initializer; walk through it to determine if it's a |
| 867 | // constructor call, which CXXNewExpr handles directly. |
| 868 | if (Expr *FullInitExpr = (Expr *)FullInit.get()) { |
| 869 | if (CXXBindTemporaryExpr *Binder |
| 870 | = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr)) |
| 871 | FullInitExpr = Binder->getSubExpr(); |
| 872 | if (CXXConstructExpr *Construct |
| 873 | = dyn_cast<CXXConstructExpr>(FullInitExpr)) { |
| 874 | Constructor = Construct->getConstructor(); |
| 875 | for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(), |
| 876 | AEnd = Construct->arg_end(); |
| 877 | A != AEnd; ++A) |
| 878 | ConvertedConstructorArgs.push_back(A->Retain()); |
| 879 | } else { |
| 880 | // Take the converted initializer. |
| 881 | ConvertedConstructorArgs.push_back(FullInit.release()); |
| 882 | } |
| 883 | } else { |
| 884 | // No initialization required. |
| 885 | } |
| 886 | |
| 887 | // Take the converted arguments and use them for the new expression. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 888 | NumConsArgs = ConvertedConstructorArgs.size(); |
| 889 | ConsArgs = (Expr **)ConvertedConstructorArgs.take(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 890 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 892 | // Mark the new and delete operators as referenced. |
| 893 | if (OperatorNew) |
| 894 | MarkDeclarationReferenced(StartLoc, OperatorNew); |
| 895 | if (OperatorDelete) |
| 896 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
| 897 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 898 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) |
Douglas Gregor | 089407b | 2009-10-17 21:40:42 +0000 | [diff] [blame] | 899 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 900 | PlacementArgs.release(); |
| 901 | ConstructorArgs.release(); |
Ted Kremenek | f9d5bac | 2010-06-25 22:48:49 +0000 | [diff] [blame] | 902 | |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 903 | return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 904 | PlaceArgs, NumPlaceArgs, TypeIdParens, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 905 | ArraySize, Constructor, Init, |
| 906 | ConsArgs, NumConsArgs, OperatorDelete, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 907 | ResultType, AllocTypeInfo, |
| 908 | StartLoc, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 909 | Init ? ConstructorRParen : |
Ted Kremenek | f9d5bac | 2010-06-25 22:48:49 +0000 | [diff] [blame] | 910 | TypeRange.getEnd())); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 914 | /// in a new-expression. |
| 915 | /// dimension off and stores the size expression in ArraySize. |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 916 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | SourceRange R) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 918 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 919 | // abstract class type or array thereof. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 920 | if (AllocType->isFunctionType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 921 | return Diag(Loc, diag::err_bad_new_type) |
| 922 | << AllocType << 0 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 923 | else if (AllocType->isReferenceType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 924 | return Diag(Loc, diag::err_bad_new_type) |
| 925 | << AllocType << 1 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 926 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 927 | RequireCompleteType(Loc, AllocType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 928 | PDiag(diag::err_new_incomplete_type) |
| 929 | << R)) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 930 | return true; |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 931 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 932 | diag::err_allocation_of_abstract_type)) |
| 933 | return true; |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 934 | else if (AllocType->isVariablyModifiedType()) |
| 935 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 936 | << AllocType; |
| 937 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 938 | return false; |
| 939 | } |
| 940 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 941 | /// \brief Determine whether the given function is a non-placement |
| 942 | /// deallocation function. |
| 943 | static bool isNonPlacementDeallocationFunction(FunctionDecl *FD) { |
| 944 | if (FD->isInvalidDecl()) |
| 945 | return false; |
| 946 | |
| 947 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 948 | return Method->isUsualDeallocationFunction(); |
| 949 | |
| 950 | return ((FD->getOverloadedOperator() == OO_Delete || |
| 951 | FD->getOverloadedOperator() == OO_Array_Delete) && |
| 952 | FD->getNumParams() == 1); |
| 953 | } |
| 954 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 955 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 956 | /// that are appropriate for the allocation. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 957 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 958 | bool UseGlobal, QualType AllocType, |
| 959 | bool IsArray, Expr **PlaceArgs, |
| 960 | unsigned NumPlaceArgs, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 961 | FunctionDecl *&OperatorNew, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 963 | // --- Choosing an allocation function --- |
| 964 | // C++ 5.3.4p8 - 14 & 18 |
| 965 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 966 | // in the scope of the allocated class. |
| 967 | // 2) If an array size is given, look for operator new[], else look for |
| 968 | // operator new. |
| 969 | // 3) The first argument is always size_t. Append the arguments from the |
| 970 | // placement form. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 971 | |
| 972 | llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
| 973 | // We don't care about the actual value of this argument. |
| 974 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 975 | // tree? Or should the consumer just recalculate the value? |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 976 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 977 | Context.Target.getPointerWidth(0)), |
| 978 | Context.getSizeType(), |
| 979 | SourceLocation()); |
| 980 | AllocArgs[0] = &Size; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 981 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 982 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 983 | // C++ [expr.new]p8: |
| 984 | // If the allocated type is a non-array type, the allocation |
| 985 | // function’s name is operator new and the deallocation function’s |
| 986 | // name is operator delete. If the allocated type is an array |
| 987 | // type, the allocation function’s name is operator new[] and the |
| 988 | // deallocation function’s name is operator delete[]. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 989 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 990 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 991 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 992 | IsArray ? OO_Array_Delete : OO_Delete); |
| 993 | |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 994 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 995 | |
| 996 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | CXXRecordDecl *Record |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 998 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 999 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1000 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 1001 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1002 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1003 | } |
| 1004 | if (!OperatorNew) { |
| 1005 | // Didn't find a member overload. Look for a global one. |
| 1006 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1007 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1008 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1009 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 1010 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1011 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1014 | // We don't need an operator delete if we're running under |
| 1015 | // -fno-exceptions. |
| 1016 | if (!getLangOptions().Exceptions) { |
| 1017 | OperatorDelete = 0; |
| 1018 | return false; |
| 1019 | } |
| 1020 | |
Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 1021 | // FindAllocationOverload can change the passed in arguments, so we need to |
| 1022 | // copy them back. |
| 1023 | if (NumPlaceArgs > 0) |
| 1024 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1026 | // C++ [expr.new]p19: |
| 1027 | // |
| 1028 | // If the new-expression begins with a unary :: operator, the |
| 1029 | // deallocation function’s name is looked up in the global |
| 1030 | // scope. Otherwise, if the allocated type is a class type T or an |
| 1031 | // array thereof, the deallocation function’s name is looked up in |
| 1032 | // the scope of T. If this lookup fails to find the name, or if |
| 1033 | // the allocated type is not a class type or array thereof, the |
| 1034 | // deallocation function’s name is looked up in the global scope. |
| 1035 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1036 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1037 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1038 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1039 | LookupQualifiedName(FoundDelete, RD); |
| 1040 | } |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1041 | if (FoundDelete.isAmbiguous()) |
| 1042 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1043 | |
| 1044 | if (FoundDelete.empty()) { |
| 1045 | DeclareGlobalNewDelete(); |
| 1046 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 1047 | } |
| 1048 | |
| 1049 | FoundDelete.suppressDiagnostics(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1050 | |
| 1051 | llvm::SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
| 1052 | |
John McCall | edeb6c9 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 1053 | // Whether we're looking for a placement operator delete is dictated |
| 1054 | // by whether we selected a placement operator new, not by whether |
| 1055 | // we had explicit placement arguments. This matters for things like |
| 1056 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 1057 | // A *a = new A() |
| 1058 | bool isPlacementNew = (NumPlaceArgs > 0 || OperatorNew->param_size() != 1); |
| 1059 | |
| 1060 | if (isPlacementNew) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1061 | // C++ [expr.new]p20: |
| 1062 | // A declaration of a placement deallocation function matches the |
| 1063 | // declaration of a placement allocation function if it has the |
| 1064 | // same number of parameters and, after parameter transformations |
| 1065 | // (8.3.5), all parameter types except the first are |
| 1066 | // identical. [...] |
| 1067 | // |
| 1068 | // To perform this comparison, we compute the function type that |
| 1069 | // the deallocation function should have, and use that type both |
| 1070 | // for template argument deduction and for comparison purposes. |
| 1071 | QualType ExpectedFunctionType; |
| 1072 | { |
| 1073 | const FunctionProtoType *Proto |
| 1074 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
| 1075 | llvm::SmallVector<QualType, 4> ArgTypes; |
| 1076 | ArgTypes.push_back(Context.VoidPtrTy); |
| 1077 | for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I) |
| 1078 | ArgTypes.push_back(Proto->getArgType(I)); |
| 1079 | |
| 1080 | ExpectedFunctionType |
| 1081 | = Context.getFunctionType(Context.VoidTy, ArgTypes.data(), |
| 1082 | ArgTypes.size(), |
| 1083 | Proto->isVariadic(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1084 | 0, false, false, 0, 0, |
| 1085 | FunctionType::ExtInfo()); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | for (LookupResult::iterator D = FoundDelete.begin(), |
| 1089 | DEnd = FoundDelete.end(); |
| 1090 | D != DEnd; ++D) { |
| 1091 | FunctionDecl *Fn = 0; |
| 1092 | if (FunctionTemplateDecl *FnTmpl |
| 1093 | = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
| 1094 | // Perform template argument deduction to try to match the |
| 1095 | // expected function type. |
| 1096 | TemplateDeductionInfo Info(Context, StartLoc); |
| 1097 | if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info)) |
| 1098 | continue; |
| 1099 | } else |
| 1100 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 1101 | |
| 1102 | if (Context.hasSameType(Fn->getType(), ExpectedFunctionType)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1103 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } else { |
| 1106 | // C++ [expr.new]p20: |
| 1107 | // [...] Any non-placement deallocation function matches a |
| 1108 | // non-placement allocation function. [...] |
| 1109 | for (LookupResult::iterator D = FoundDelete.begin(), |
| 1110 | DEnd = FoundDelete.end(); |
| 1111 | D != DEnd; ++D) { |
| 1112 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl())) |
| 1113 | if (isNonPlacementDeallocationFunction(Fn)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1114 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | // C++ [expr.new]p20: |
| 1119 | // [...] If the lookup finds a single matching deallocation |
| 1120 | // function, that function will be called; otherwise, no |
| 1121 | // deallocation function will be called. |
| 1122 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1123 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1124 | |
| 1125 | // C++0x [expr.new]p20: |
| 1126 | // If the lookup finds the two-parameter form of a usual |
| 1127 | // deallocation function (3.7.4.2) and that function, considered |
| 1128 | // as a placement deallocation function, would have been |
| 1129 | // selected as a match for the allocation function, the program |
| 1130 | // is ill-formed. |
| 1131 | if (NumPlaceArgs && getLangOptions().CPlusPlus0x && |
| 1132 | isNonPlacementDeallocationFunction(OperatorDelete)) { |
| 1133 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) |
| 1134 | << SourceRange(PlaceArgs[0]->getLocStart(), |
| 1135 | PlaceArgs[NumPlaceArgs - 1]->getLocEnd()); |
| 1136 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 1137 | << DeleteName; |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1138 | } else { |
| 1139 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1140 | Matches[0].first); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1144 | return false; |
| 1145 | } |
| 1146 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1147 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 1148 | /// function in the specified scope. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1149 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 1150 | DeclarationName Name, Expr** Args, |
| 1151 | unsigned NumArgs, DeclContext *Ctx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | bool AllowMissing, FunctionDecl *&Operator) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1153 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); |
| 1154 | LookupQualifiedName(R, Ctx); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1155 | if (R.empty()) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1156 | if (AllowMissing) |
| 1157 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1158 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1159 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1162 | if (R.isAmbiguous()) |
| 1163 | return true; |
| 1164 | |
| 1165 | R.suppressDiagnostics(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1166 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1167 | OverloadCandidateSet Candidates(StartLoc); |
Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 1168 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 1169 | Alloc != AllocEnd; ++Alloc) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1170 | // Even member operator new/delete are implicitly treated as |
| 1171 | // static, so don't use AddMemberCandidate. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1172 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1173 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1174 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 1175 | AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1176 | /*ExplicitTemplateArgs=*/0, Args, NumArgs, |
| 1177 | Candidates, |
| 1178 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1179 | continue; |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1182 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 1183 | AddOverloadCandidate(Fn, Alloc.getPair(), Args, NumArgs, Candidates, |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1184 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | // Do the resolution. |
| 1188 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1189 | switch (Candidates.BestViableFunction(*this, StartLoc, Best)) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1190 | case OR_Success: { |
| 1191 | // Got one! |
| 1192 | FunctionDecl *FnDecl = Best->Function; |
| 1193 | // The first argument is size_t, and the first parameter must be size_t, |
| 1194 | // too. This is checked on declaration and can be assumed. (It can't be |
| 1195 | // asserted on, though, since invalid decls are left in there.) |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1196 | // Watch out for variadic allocator function. |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1197 | unsigned NumArgsInFnDecl = FnDecl->getNumParams(); |
| 1198 | for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1199 | ExprResult Result |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1200 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1201 | Context, |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1202 | FnDecl->getParamDecl(i)), |
| 1203 | SourceLocation(), |
| 1204 | Owned(Args[i]->Retain())); |
| 1205 | if (Result.isInvalid()) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1206 | return true; |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1207 | |
| 1208 | Args[i] = Result.takeAs<Expr>(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1209 | } |
| 1210 | Operator = FnDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1211 | CheckAllocationAccess(StartLoc, Range, R.getNamingClass(), Best->FoundDecl); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1212 | return false; |
| 1213 | } |
| 1214 | |
| 1215 | case OR_No_Viable_Function: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1216 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1217 | << Name << Range; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1218 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1219 | return true; |
| 1220 | |
| 1221 | case OR_Ambiguous: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1222 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1223 | << Name << Range; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1224 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1225 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1226 | |
| 1227 | case OR_Deleted: |
| 1228 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 1229 | << Best->Function->isDeleted() |
| 1230 | << Name << Range; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1231 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1232 | return true; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1233 | } |
| 1234 | assert(false && "Unreachable, bad result from BestViableFunction"); |
| 1235 | return true; |
| 1236 | } |
| 1237 | |
| 1238 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1239 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 1240 | /// delete. These are: |
| 1241 | /// @code |
| 1242 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 1243 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1244 | /// void operator delete(void *) throw(); |
| 1245 | /// void operator delete[](void *) throw(); |
| 1246 | /// @endcode |
| 1247 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 1248 | /// declared. Their use requires including \<new\>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1250 | if (GlobalNewDeleteDeclared) |
| 1251 | return; |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1252 | |
| 1253 | // C++ [basic.std.dynamic]p2: |
| 1254 | // [...] The following allocation and deallocation functions (18.4) are |
| 1255 | // implicitly declared in global scope in each translation unit of a |
| 1256 | // program |
| 1257 | // |
| 1258 | // void* operator new(std::size_t) throw(std::bad_alloc); |
| 1259 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1260 | // void operator delete(void*) throw(); |
| 1261 | // void operator delete[](void*) throw(); |
| 1262 | // |
| 1263 | // These implicit declarations introduce only the function names operator |
| 1264 | // new, operator new[], operator delete, operator delete[]. |
| 1265 | // |
| 1266 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 1267 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 1268 | // However, we do not make these implicit declarations visible to name |
| 1269 | // lookup. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1270 | if (!StdBadAlloc) { |
| 1271 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 1272 | // implicitly. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1273 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 1274 | getOrCreateStdNamespace(), |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1275 | SourceLocation(), |
| 1276 | &PP.getIdentifierTable().get("bad_alloc"), |
| 1277 | SourceLocation(), 0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1278 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1281 | GlobalNewDeleteDeclared = true; |
| 1282 | |
| 1283 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 1284 | QualType SizeT = Context.getSizeType(); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1285 | bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1286 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1287 | DeclareGlobalAllocationFunction( |
| 1288 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1289 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1290 | DeclareGlobalAllocationFunction( |
| 1291 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1292 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1293 | DeclareGlobalAllocationFunction( |
| 1294 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 1295 | Context.VoidTy, VoidPtr); |
| 1296 | DeclareGlobalAllocationFunction( |
| 1297 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 1298 | Context.VoidTy, VoidPtr); |
| 1299 | } |
| 1300 | |
| 1301 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 1302 | /// allocation function if it doesn't already exist. |
| 1303 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1304 | QualType Return, QualType Argument, |
| 1305 | bool AddMallocAttr) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1306 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 1307 | |
| 1308 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1309 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 1310 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1311 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1312 | Alloc != AllocEnd; ++Alloc) { |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1313 | // Only look at non-template functions, as it is the predefined, |
| 1314 | // non-templated allocation function we are trying to declare here. |
| 1315 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
| 1316 | QualType InitialParamType = |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 1317 | Context.getCanonicalType( |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1318 | Func->getParamDecl(0)->getType().getUnqualifiedType()); |
| 1319 | // FIXME: Do we need to check for default arguments here? |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1320 | if (Func->getNumParams() == 1 && InitialParamType == Argument) { |
| 1321 | if(AddMallocAttr && !Func->hasAttr<MallocAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1322 | Func->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1323 | return; |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1324 | } |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1325 | } |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1329 | QualType BadAllocType; |
| 1330 | bool HasBadAllocExceptionSpec |
| 1331 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 1332 | Name.getCXXOverloadedOperator() == OO_Array_New); |
| 1333 | if (HasBadAllocExceptionSpec) { |
| 1334 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1335 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0, |
| 1339 | true, false, |
| 1340 | HasBadAllocExceptionSpec? 1 : 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1341 | &BadAllocType, |
| 1342 | FunctionType::ExtInfo()); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1343 | FunctionDecl *Alloc = |
| 1344 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1345 | FnType, /*TInfo=*/0, SC_None, |
| 1346 | SC_None, false, true); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1347 | Alloc->setImplicit(); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1348 | |
| 1349 | if (AddMallocAttr) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1350 | Alloc->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1351 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1352 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1353 | 0, Argument, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1354 | SC_None, |
| 1355 | SC_None, 0); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1356 | Alloc->setParams(&Param, 1); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1357 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1358 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 1359 | // make sure it is at the end of the chain to coincide with the |
| 1360 | // global scope. |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1361 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1364 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 1365 | DeclarationName Name, |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1366 | FunctionDecl* &Operator) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1367 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1368 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1369 | LookupQualifiedName(Found, RD); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1370 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1371 | if (Found.isAmbiguous()) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1372 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1373 | |
Chandler Carruth | 2389324 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 1374 | Found.suppressDiagnostics(); |
| 1375 | |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1376 | llvm::SmallVector<DeclAccessPair,4> Matches; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1377 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1378 | F != FEnd; ++F) { |
Chandler Carruth | 09556fd | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 1379 | NamedDecl *ND = (*F)->getUnderlyingDecl(); |
| 1380 | |
| 1381 | // Ignore template operator delete members from the check for a usual |
| 1382 | // deallocation function. |
| 1383 | if (isa<FunctionTemplateDecl>(ND)) |
| 1384 | continue; |
| 1385 | |
| 1386 | if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction()) |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1387 | Matches.push_back(F.getPair()); |
| 1388 | } |
| 1389 | |
| 1390 | // There's exactly one suitable operator; pick it. |
| 1391 | if (Matches.size() == 1) { |
| 1392 | Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl()); |
| 1393 | CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
| 1394 | Matches[0]); |
| 1395 | return false; |
| 1396 | |
| 1397 | // We found multiple suitable operators; complain about the ambiguity. |
| 1398 | } else if (!Matches.empty()) { |
| 1399 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 1400 | << Name << RD; |
| 1401 | |
| 1402 | for (llvm::SmallVectorImpl<DeclAccessPair>::iterator |
| 1403 | F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F) |
| 1404 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1405 | diag::note_member_declared_here) << Name; |
| 1406 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | // We did find operator delete/operator delete[] declarations, but |
| 1410 | // none of them were suitable. |
| 1411 | if (!Found.empty()) { |
| 1412 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 1413 | << Name << RD; |
| 1414 | |
| 1415 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1416 | F != FEnd; ++F) |
| 1417 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1418 | diag::note_member_declared_here) << Name; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1419 | |
| 1420 | return true; |
| 1421 | } |
| 1422 | |
| 1423 | // Look for a global declaration. |
| 1424 | DeclareGlobalNewDelete(); |
| 1425 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
| 1426 | |
| 1427 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); |
| 1428 | Expr* DeallocArgs[1]; |
| 1429 | DeallocArgs[0] = &Null; |
| 1430 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, |
| 1431 | DeallocArgs, 1, TUDecl, /*AllowMissing=*/false, |
| 1432 | Operator)) |
| 1433 | return true; |
| 1434 | |
| 1435 | assert(Operator && "Did not find a deallocation function!"); |
| 1436 | return false; |
| 1437 | } |
| 1438 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1439 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 1440 | /// @code ::delete ptr; @endcode |
| 1441 | /// or |
| 1442 | /// @code delete [] ptr; @endcode |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1443 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1444 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1445 | bool ArrayForm, Expr *Ex) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1446 | // C++ [expr.delete]p1: |
| 1447 | // The operand shall have a pointer type, or a class type having a single |
| 1448 | // conversion function to a pointer type. The result has type void. |
| 1449 | // |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1450 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 1451 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1452 | FunctionDecl *OperatorDelete = 0; |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1453 | bool ArrayFormAsWritten = ArrayForm; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1455 | if (!Ex->isTypeDependent()) { |
| 1456 | QualType Type = Ex->getType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1457 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1458 | if (const RecordType *Record = Type->getAs<RecordType>()) { |
Douglas Gregor | 254a942 | 2010-07-29 14:44:35 +0000 | [diff] [blame] | 1459 | if (RequireCompleteType(StartLoc, Type, |
| 1460 | PDiag(diag::err_delete_incomplete_class_type))) |
| 1461 | return ExprError(); |
| 1462 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1463 | llvm::SmallVector<CXXConversionDecl*, 4> ObjectPtrConversions; |
| 1464 | |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1465 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1466 | const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1467 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1468 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1469 | NamedDecl *D = I.getDecl(); |
| 1470 | if (isa<UsingShadowDecl>(D)) |
| 1471 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1472 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1473 | // Skip over templated conversion functions; they aren't considered. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1474 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1475 | continue; |
| 1476 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1477 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1478 | |
| 1479 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 1480 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1481 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1482 | ObjectPtrConversions.push_back(Conv); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1483 | } |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1484 | if (ObjectPtrConversions.size() == 1) { |
| 1485 | // We have a single conversion to a pointer-to-object type. Perform |
| 1486 | // that conversion. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1487 | // TODO: don't redo the conversion calculation. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1488 | if (!PerformImplicitConversion(Ex, |
| 1489 | ObjectPtrConversions.front()->getConversionType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1490 | AA_Converting)) { |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1491 | Type = Ex->getType(); |
| 1492 | } |
| 1493 | } |
| 1494 | else if (ObjectPtrConversions.size() > 1) { |
| 1495 | Diag(StartLoc, diag::err_ambiguous_delete_operand) |
| 1496 | << Type << Ex->getSourceRange(); |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1497 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) |
| 1498 | NoteOverloadCandidate(ObjectPtrConversions[i]); |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1499 | return ExprError(); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1500 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1503 | if (!Type->isPointerType()) |
| 1504 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 1505 | << Type << Ex->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1506 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1507 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 94a6157 | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 1508 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
| 1509 | // The C++ standard bans deleting a pointer to a non-object type, which |
| 1510 | // effectively bans deletion of "void*". However, most compilers support |
| 1511 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 1512 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
| 1513 | << Type << Ex->getSourceRange(); |
| 1514 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1515 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 1516 | << Type << Ex->getSourceRange()); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1517 | else if (!Pointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | RequireCompleteType(StartLoc, Pointee, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1519 | PDiag(diag::warn_delete_incomplete) |
| 1520 | << Ex->getSourceRange())) |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1521 | return ExprError(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1522 | |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 1523 | // C++ [expr.delete]p2: |
| 1524 | // [Note: a pointer to a const type can be the operand of a |
| 1525 | // delete-expression; it is not necessary to cast away the constness |
| 1526 | // (5.2.11) of the pointer expression before it is used as the operand |
| 1527 | // of the delete-expression. ] |
| 1528 | ImpCastExprToType(Ex, Context.getPointerType(Context.VoidTy), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1529 | CK_NoOp); |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1530 | |
| 1531 | if (Pointee->isArrayType() && !ArrayForm) { |
| 1532 | Diag(StartLoc, diag::warn_delete_array_type) |
| 1533 | << Type << Ex->getSourceRange() |
| 1534 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]"); |
| 1535 | ArrayForm = true; |
| 1536 | } |
| 1537 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1538 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1539 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 1540 | |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1541 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 1542 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) { |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1543 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1544 | |
| 1545 | if (!UseGlobal && |
| 1546 | FindDeallocationFunction(StartLoc, RD, DeleteName, OperatorDelete)) |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1547 | return ExprError(); |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1548 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1549 | if (!RD->hasTrivialDestructor()) |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 1550 | if (const CXXDestructorDecl *Dtor = LookupDestructor(RD)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | MarkDeclarationReferenced(StartLoc, |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1552 | const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1553 | } |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1554 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1555 | if (!OperatorDelete) { |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1556 | // Look for a global declaration. |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1557 | DeclareGlobalNewDelete(); |
| 1558 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1560 | &Ex, 1, TUDecl, /*AllowMissing=*/false, |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1561 | OperatorDelete)) |
| 1562 | return ExprError(); |
| 1563 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1565 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
| 1566 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1567 | // FIXME: Check access and ambiguity of operator delete and destructor. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1570 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1571 | ArrayFormAsWritten, OperatorDelete, |
| 1572 | Ex, StartLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1575 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 1576 | /// while, do-while, or switch statement. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1577 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1578 | SourceLocation StmtLoc, |
| 1579 | bool ConvertToBoolean) { |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1580 | QualType T = ConditionVar->getType(); |
| 1581 | |
| 1582 | // C++ [stmt.select]p2: |
| 1583 | // The declarator shall not specify a function or an array. |
| 1584 | if (T->isFunctionType()) |
| 1585 | return ExprError(Diag(ConditionVar->getLocation(), |
| 1586 | diag::err_invalid_use_of_function_type) |
| 1587 | << ConditionVar->getSourceRange()); |
| 1588 | else if (T->isArrayType()) |
| 1589 | return ExprError(Diag(ConditionVar->getLocation(), |
| 1590 | diag::err_invalid_use_of_array_type) |
| 1591 | << ConditionVar->getSourceRange()); |
Douglas Gregor | a7605db | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 1592 | |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1593 | Expr *Condition = DeclRefExpr::Create(Context, 0, SourceRange(), ConditionVar, |
| 1594 | ConditionVar->getLocation(), |
| 1595 | ConditionVar->getType().getNonReferenceType()); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 1596 | if (ConvertToBoolean && CheckBooleanCondition(Condition, StmtLoc)) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1597 | return ExprError(); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1598 | |
| 1599 | return Owned(Condition); |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1602 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 1603 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 1604 | // C++ 6.4p4: |
| 1605 | // The value of a condition that is an initialized declaration in a statement |
| 1606 | // other than a switch statement is the value of the declared variable |
| 1607 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 1608 | // program is ill-formed. |
| 1609 | // The value of a condition that is an expression is the value of the |
| 1610 | // expression, implicitly converted to bool. |
| 1611 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1612 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1613 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1614 | |
| 1615 | /// Helper function to determine whether this is the (deprecated) C++ |
| 1616 | /// conversion from a string literal to a pointer to non-const char or |
| 1617 | /// non-const wchar_t (for narrow and wide string literals, |
| 1618 | /// respectively). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | bool |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1620 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 1621 | // Look inside the implicit cast, if it exists. |
| 1622 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 1623 | From = Cast->getSubExpr(); |
| 1624 | |
| 1625 | // A string literal (2.13.4) that is not a wide string literal can |
| 1626 | // be converted to an rvalue of type "pointer to char"; a wide |
| 1627 | // string literal can be converted to an rvalue of type "pointer |
| 1628 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 1984eb9 | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 1629 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1630 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | if (const BuiltinType *ToPointeeType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1632 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1633 | // This conversion is considered only when there is an |
| 1634 | // explicit appropriate pointer target type (C++ 4.2p2). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1635 | if (!ToPtrType->getPointeeType().hasQualifiers() && |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1636 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 1637 | (!StrLit->isWide() && |
| 1638 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 1639 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 1640 | return true; |
| 1641 | } |
| 1642 | |
| 1643 | return false; |
| 1644 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1645 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1646 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1647 | SourceLocation CastLoc, |
| 1648 | QualType Ty, |
| 1649 | CastKind Kind, |
| 1650 | CXXMethodDecl *Method, |
| 1651 | Expr *From) { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1652 | switch (Kind) { |
| 1653 | default: assert(0 && "Unhandled cast kind!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1654 | case CK_ConstructorConversion: { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1655 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1656 | |
| 1657 | if (S.CompleteConstructorCall(cast<CXXConstructorDecl>(Method), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1658 | MultiExprArg(&From, 1), |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1659 | CastLoc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1660 | return ExprError(); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1661 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1662 | ExprResult Result = |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1663 | S.BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 1664 | move_arg(ConstructorArgs), |
| 1665 | /*ZeroInit*/ false, CXXConstructExpr::CK_Complete); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1666 | if (Result.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1667 | return ExprError(); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1668 | |
| 1669 | return S.MaybeBindToTemporary(Result.takeAs<Expr>()); |
| 1670 | } |
| 1671 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1672 | case CK_UserDefinedConversion: { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1673 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
| 1674 | |
| 1675 | // Create an implicit call expr that calls it. |
| 1676 | // FIXME: pass the FoundDecl for the user-defined conversion here |
| 1677 | CXXMemberCallExpr *CE = S.BuildCXXMemberCallExpr(From, Method, Method); |
| 1678 | return S.MaybeBindToTemporary(CE); |
| 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1683 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1684 | /// expression From to the type ToType using the pre-computed implicit |
| 1685 | /// conversion sequence ICS. Returns true if there was an error, false |
| 1686 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1687 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1688 | /// used in the error message. |
| 1689 | bool |
| 1690 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 1691 | const ImplicitConversionSequence &ICS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1692 | AssignmentAction Action, bool IgnoreBaseAccess) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1693 | switch (ICS.getKind()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1694 | case ImplicitConversionSequence::StandardConversion: |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1695 | if (PerformImplicitConversion(From, ToType, ICS.Standard, Action, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1696 | IgnoreBaseAccess)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1697 | return true; |
| 1698 | break; |
| 1699 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1700 | case ImplicitConversionSequence::UserDefinedConversion: { |
| 1701 | |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 1702 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1703 | CastKind CastKind = CK_Unknown; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1704 | QualType BeforeToType; |
| 1705 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1706 | CastKind = CK_UserDefinedConversion; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1707 | |
| 1708 | // If the user-defined conversion is specified by a conversion function, |
| 1709 | // the initial standard conversion sequence converts the source type to |
| 1710 | // the implicit object parameter of the conversion function. |
| 1711 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
| 1712 | } else if (const CXXConstructorDecl *Ctor = |
| 1713 | dyn_cast<CXXConstructorDecl>(FD)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1714 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1715 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 1716 | if (!ICS.UserDefined.EllipsisConversion) { |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1717 | // If the user-defined conversion is specified by a constructor, the |
| 1718 | // initial standard conversion sequence converts the source type to the |
| 1719 | // type required by the argument of the constructor |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 1720 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 1721 | } |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1722 | } |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1723 | else |
| 1724 | assert(0 && "Unknown conversion function kind!"); |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1725 | // Whatch out for elipsis conversion. |
Fariborz Jahanian | 4c0cea2 | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 1726 | if (!ICS.UserDefined.EllipsisConversion) { |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1727 | if (PerformImplicitConversion(From, BeforeToType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1728 | ICS.UserDefined.Before, AA_Converting, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1729 | IgnoreBaseAccess)) |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1730 | return true; |
| 1731 | } |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1732 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1733 | ExprResult CastArg |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1734 | = BuildCXXCastArgument(*this, |
| 1735 | From->getLocStart(), |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1736 | ToType.getNonReferenceType(), |
| 1737 | CastKind, cast<CXXMethodDecl>(FD), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1738 | From); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1739 | |
| 1740 | if (CastArg.isInvalid()) |
| 1741 | return true; |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1742 | |
| 1743 | From = CastArg.takeAs<Expr>(); |
| 1744 | |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1745 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1746 | AA_Converting, IgnoreBaseAccess); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1747 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1748 | |
| 1749 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1750 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1751 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 1752 | << From->getSourceRange()); |
| 1753 | return true; |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1754 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1755 | case ImplicitConversionSequence::EllipsisConversion: |
| 1756 | assert(false && "Cannot perform an ellipsis conversion"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1757 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1758 | |
| 1759 | case ImplicitConversionSequence::BadConversion: |
| 1760 | return true; |
| 1761 | } |
| 1762 | |
| 1763 | // Everything went well. |
| 1764 | return false; |
| 1765 | } |
| 1766 | |
| 1767 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1768 | /// expression From to the type ToType by following the standard |
| 1769 | /// conversion sequence SCS. Returns true if there was an error, false |
| 1770 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1771 | /// expression. Flavor is the context in which we're performing this |
| 1772 | /// conversion, for use in error messages. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | bool |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1774 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1775 | const StandardConversionSequence& SCS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1776 | AssignmentAction Action, bool IgnoreBaseAccess) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1777 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 1778 | // much extra work. What this means is that we need to keep track of more |
| 1779 | // information that is computed when we try the implicit conversion initially, |
| 1780 | // so that we don't need to recompute anything here. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1781 | QualType FromType = From->getType(); |
| 1782 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1783 | if (SCS.CopyConstructor) { |
Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 1784 | // FIXME: When can ToType be a reference type? |
| 1785 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1786 | if (SCS.Second == ICK_Derived_To_Base) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1787 | ASTOwningVector<Expr*> ConstructorArgs(*this); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1788 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1789 | MultiExprArg(*this, &From, 1), |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1790 | /*FIXME:ConstructLoc*/SourceLocation(), |
| 1791 | ConstructorArgs)) |
| 1792 | return true; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1793 | ExprResult FromResult = |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1794 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 1795 | ToType, SCS.CopyConstructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 1796 | move_arg(ConstructorArgs), |
| 1797 | /*ZeroInit*/ false, |
| 1798 | CXXConstructExpr::CK_Complete); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1799 | if (FromResult.isInvalid()) |
| 1800 | return true; |
| 1801 | From = FromResult.takeAs<Expr>(); |
| 1802 | return false; |
| 1803 | } |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1804 | ExprResult FromResult = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 1806 | ToType, SCS.CopyConstructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 1807 | MultiExprArg(*this, &From, 1), |
| 1808 | /*ZeroInit*/ false, |
| 1809 | CXXConstructExpr::CK_Complete); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1810 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1811 | if (FromResult.isInvalid()) |
| 1812 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1814 | From = FromResult.takeAs<Expr>(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1815 | return false; |
| 1816 | } |
| 1817 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1818 | // Resolve overloaded function references. |
| 1819 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 1820 | DeclAccessPair Found; |
| 1821 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 1822 | true, Found); |
| 1823 | if (!Fn) |
| 1824 | return true; |
| 1825 | |
| 1826 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) |
| 1827 | return true; |
| 1828 | |
| 1829 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 1830 | FromType = From->getType(); |
| 1831 | } |
| 1832 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1833 | // Perform the first implicit conversion. |
| 1834 | switch (SCS.First) { |
| 1835 | case ICK_Identity: |
| 1836 | case ICK_Lvalue_To_Rvalue: |
| 1837 | // Nothing to do. |
| 1838 | break; |
| 1839 | |
| 1840 | case ICK_Array_To_Pointer: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1841 | FromType = Context.getArrayDecayedType(FromType); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1842 | ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1843 | break; |
| 1844 | |
| 1845 | case ICK_Function_To_Pointer: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1846 | FromType = Context.getPointerType(FromType); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1847 | ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1848 | break; |
| 1849 | |
| 1850 | default: |
| 1851 | assert(false && "Improper first standard conversion"); |
| 1852 | break; |
| 1853 | } |
| 1854 | |
| 1855 | // Perform the second implicit conversion |
| 1856 | switch (SCS.Second) { |
| 1857 | case ICK_Identity: |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1858 | // If both sides are functions (or pointers/references to them), there could |
| 1859 | // be incompatible exception declarations. |
| 1860 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1861 | return true; |
| 1862 | // Nothing else to do. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1863 | break; |
| 1864 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1865 | case ICK_NoReturn_Adjustment: |
| 1866 | // If both sides are functions (or pointers/references to them), there could |
| 1867 | // be incompatible exception declarations. |
| 1868 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1869 | return true; |
| 1870 | |
| 1871 | ImpCastExprToType(From, Context.getNoReturnType(From->getType(), false), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1872 | CK_NoOp); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1873 | break; |
| 1874 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1875 | case ICK_Integral_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1876 | case ICK_Integral_Conversion: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1877 | ImpCastExprToType(From, ToType, CK_IntegralCast); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1878 | break; |
| 1879 | |
| 1880 | case ICK_Floating_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1881 | case ICK_Floating_Conversion: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1882 | ImpCastExprToType(From, ToType, CK_FloatingCast); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1883 | break; |
| 1884 | |
| 1885 | case ICK_Complex_Promotion: |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1886 | case ICK_Complex_Conversion: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1887 | ImpCastExprToType(From, ToType, CK_Unknown); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1888 | break; |
| 1889 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1890 | case ICK_Floating_Integral: |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1891 | if (ToType->isRealFloatingType()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1892 | ImpCastExprToType(From, ToType, CK_IntegralToFloating); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1893 | else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1894 | ImpCastExprToType(From, ToType, CK_FloatingToIntegral); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1895 | break; |
| 1896 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1897 | case ICK_Compatible_Conversion: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1898 | ImpCastExprToType(From, ToType, CK_NoOp); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1899 | break; |
| 1900 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1901 | case ICK_Pointer_Conversion: { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1902 | if (SCS.IncompatibleObjC) { |
| 1903 | // Diagnose incompatible Objective-C conversions |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | Diag(From->getSourceRange().getBegin(), |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1905 | diag::ext_typecheck_convert_incompatible_pointer) |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1906 | << From->getType() << ToType << Action |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1907 | << From->getSourceRange(); |
| 1908 | } |
| 1909 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1910 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1911 | CastKind Kind = CK_Unknown; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1912 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1913 | if (CheckPointerConversion(From, ToType, Kind, BasePath, IgnoreBaseAccess)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1914 | return true; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1915 | ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1916 | break; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | case ICK_Pointer_Member: { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1920 | CastKind Kind = CK_Unknown; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1921 | CXXCastPath BasePath; |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1922 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, |
| 1923 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1924 | return true; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1925 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1926 | return true; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1927 | ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath); |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1928 | break; |
| 1929 | } |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 1930 | case ICK_Boolean_Conversion: { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1931 | CastKind Kind = CK_Unknown; |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 1932 | if (FromType->isMemberPointerType()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1933 | Kind = CK_MemberPointerToBoolean; |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 1934 | |
| 1935 | ImpCastExprToType(From, Context.BoolTy, Kind); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1936 | break; |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 1937 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1939 | case ICK_Derived_To_Base: { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1940 | CXXCastPath BasePath; |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1941 | if (CheckDerivedToBaseConversion(From->getType(), |
| 1942 | ToType.getNonReferenceType(), |
| 1943 | From->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1944 | From->getSourceRange(), |
| 1945 | &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1946 | IgnoreBaseAccess)) |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1947 | return true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1948 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1949 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1950 | CK_DerivedToBase, CastCategory(From), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1951 | &BasePath); |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1952 | break; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1955 | case ICK_Vector_Conversion: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1956 | ImpCastExprToType(From, ToType, CK_BitCast); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | |
| 1959 | case ICK_Vector_Splat: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1960 | ImpCastExprToType(From, ToType, CK_VectorSplat); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1961 | break; |
| 1962 | |
| 1963 | case ICK_Complex_Real: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1964 | ImpCastExprToType(From, ToType, CK_Unknown); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1965 | break; |
| 1966 | |
| 1967 | case ICK_Lvalue_To_Rvalue: |
| 1968 | case ICK_Array_To_Pointer: |
| 1969 | case ICK_Function_To_Pointer: |
| 1970 | case ICK_Qualification: |
| 1971 | case ICK_Num_Conversion_Kinds: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1972 | assert(false && "Improper second standard conversion"); |
| 1973 | break; |
| 1974 | } |
| 1975 | |
| 1976 | switch (SCS.Third) { |
| 1977 | case ICK_Identity: |
| 1978 | // Nothing to do. |
| 1979 | break; |
| 1980 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1981 | case ICK_Qualification: { |
| 1982 | // The qualification keeps the category of the inner expression, unless the |
| 1983 | // target type isn't a reference. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1984 | ExprValueKind VK = ToType->isReferenceType() ? |
| 1985 | CastCategory(From) : VK_RValue; |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1986 | ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1987 | CK_NoOp, VK); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1988 | |
| 1989 | if (SCS.DeprecatedStringLiteralToCharPtr) |
| 1990 | Diag(From->getLocStart(), diag::warn_deprecated_string_literal_conversion) |
| 1991 | << ToType.getNonReferenceType(); |
| 1992 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1993 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1996 | default: |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1997 | assert(false && "Improper third standard conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1998 | break; |
| 1999 | } |
| 2000 | |
| 2001 | return false; |
| 2002 | } |
| 2003 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2004 | ExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2005 | SourceLocation KWLoc, |
| 2006 | ParsedType Ty, |
| 2007 | SourceLocation RParen) { |
| 2008 | TypeSourceInfo *TSInfo; |
| 2009 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2011 | if (!TSInfo) |
| 2012 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2013 | return BuildUnaryTypeTrait(UTT, KWLoc, TSInfo, RParen); |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2016 | static bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT, QualType T, |
| 2017 | SourceLocation KeyLoc) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2018 | assert(!T->isDependentType() && |
| 2019 | "Cannot evaluate traits for dependent types."); |
| 2020 | ASTContext &C = Self.Context; |
| 2021 | switch(UTT) { |
| 2022 | default: assert(false && "Unknown type trait or not implemented"); |
| 2023 | case UTT_IsPOD: return T->isPODType(); |
| 2024 | case UTT_IsLiteral: return T->isLiteralType(); |
| 2025 | case UTT_IsClass: // Fallthrough |
| 2026 | case UTT_IsUnion: |
| 2027 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2028 | bool Union = Record->getDecl()->isUnion(); |
| 2029 | return UTT == UTT_IsUnion ? Union : !Union; |
| 2030 | } |
| 2031 | return false; |
| 2032 | case UTT_IsEnum: return T->isEnumeralType(); |
| 2033 | case UTT_IsPolymorphic: |
| 2034 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2035 | // Type traits are only parsed in C++, so we've got CXXRecords. |
| 2036 | return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic(); |
| 2037 | } |
| 2038 | return false; |
| 2039 | case UTT_IsAbstract: |
| 2040 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2041 | return cast<CXXRecordDecl>(RT->getDecl())->isAbstract(); |
| 2042 | return false; |
| 2043 | case UTT_IsEmpty: |
| 2044 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2045 | return !Record->getDecl()->isUnion() |
| 2046 | && cast<CXXRecordDecl>(Record->getDecl())->isEmpty(); |
| 2047 | } |
| 2048 | return false; |
| 2049 | case UTT_HasTrivialConstructor: |
| 2050 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2051 | // If __is_pod (type) is true then the trait is true, else if type is |
| 2052 | // a cv class or union type (or array thereof) with a trivial default |
| 2053 | // constructor ([class.ctor]) then the trait is true, else it is false. |
| 2054 | if (T->isPODType()) |
| 2055 | return true; |
| 2056 | if (const RecordType *RT = |
| 2057 | C.getBaseElementType(T)->getAs<RecordType>()) |
| 2058 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor(); |
| 2059 | return false; |
| 2060 | case UTT_HasTrivialCopy: |
| 2061 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2062 | // If __is_pod (type) is true or type is a reference type then |
| 2063 | // the trait is true, else if type is a cv class or union type |
| 2064 | // with a trivial copy constructor ([class.copy]) then the trait |
| 2065 | // is true, else it is false. |
| 2066 | if (T->isPODType() || T->isReferenceType()) |
| 2067 | return true; |
| 2068 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2069 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor(); |
| 2070 | return false; |
| 2071 | case UTT_HasTrivialAssign: |
| 2072 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2073 | // If type is const qualified or is a reference type then the |
| 2074 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 2075 | // trait is true, else if type is a cv class or union type with |
| 2076 | // a trivial copy assignment ([class.copy]) then the trait is |
| 2077 | // true, else it is false. |
| 2078 | // Note: the const and reference restrictions are interesting, |
| 2079 | // given that const and reference members don't prevent a class |
| 2080 | // from having a trivial copy assignment operator (but do cause |
| 2081 | // errors if the copy assignment operator is actually used, q.v. |
| 2082 | // [class.copy]p12). |
| 2083 | |
| 2084 | if (C.getBaseElementType(T).isConstQualified()) |
| 2085 | return false; |
| 2086 | if (T->isPODType()) |
| 2087 | return true; |
| 2088 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2089 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment(); |
| 2090 | return false; |
| 2091 | case UTT_HasTrivialDestructor: |
| 2092 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2093 | // If __is_pod (type) is true or type is a reference type |
| 2094 | // then the trait is true, else if type is a cv class or union |
| 2095 | // type (or array thereof) with a trivial destructor |
| 2096 | // ([class.dtor]) then the trait is true, else it is |
| 2097 | // false. |
| 2098 | if (T->isPODType() || T->isReferenceType()) |
| 2099 | return true; |
| 2100 | if (const RecordType *RT = |
| 2101 | C.getBaseElementType(T)->getAs<RecordType>()) |
| 2102 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor(); |
| 2103 | return false; |
| 2104 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 2105 | case UTT_HasNothrowAssign: |
| 2106 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2107 | // If type is const qualified or is a reference type then the |
| 2108 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 2109 | // is true then the trait is true, else if type is a cv class |
| 2110 | // or union type with copy assignment operators that are known |
| 2111 | // not to throw an exception then the trait is true, else it is |
| 2112 | // false. |
| 2113 | if (C.getBaseElementType(T).isConstQualified()) |
| 2114 | return false; |
| 2115 | if (T->isReferenceType()) |
| 2116 | return false; |
| 2117 | if (T->isPODType()) |
| 2118 | return true; |
| 2119 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2120 | CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2121 | if (RD->hasTrivialCopyAssignment()) |
| 2122 | return true; |
| 2123 | |
| 2124 | bool FoundAssign = false; |
| 2125 | bool AllNoThrow = true; |
| 2126 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2127 | LookupResult Res(Self, DeclarationNameInfo(Name, KeyLoc), |
| 2128 | Sema::LookupOrdinaryName); |
| 2129 | if (Self.LookupQualifiedName(Res, RD)) { |
| 2130 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 2131 | Op != OpEnd; ++Op) { |
| 2132 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 2133 | if (Operator->isCopyAssignmentOperator()) { |
| 2134 | FoundAssign = true; |
| 2135 | const FunctionProtoType *CPT |
| 2136 | = Operator->getType()->getAs<FunctionProtoType>(); |
| 2137 | if (!CPT->hasEmptyExceptionSpec()) { |
| 2138 | AllNoThrow = false; |
| 2139 | break; |
| 2140 | } |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | return FoundAssign && AllNoThrow; |
| 2146 | } |
| 2147 | return false; |
| 2148 | case UTT_HasNothrowCopy: |
| 2149 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2150 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 2151 | // if type is a cv class or union type with copy constructors that are |
| 2152 | // known not to throw an exception then the trait is true, else it is |
| 2153 | // false. |
| 2154 | if (T->isPODType() || T->isReferenceType()) |
| 2155 | return true; |
| 2156 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2157 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2158 | if (RD->hasTrivialCopyConstructor()) |
| 2159 | return true; |
| 2160 | |
| 2161 | bool FoundConstructor = false; |
| 2162 | bool AllNoThrow = true; |
| 2163 | unsigned FoundTQs; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2164 | DeclContext::lookup_const_iterator Con, ConEnd; |
Sebastian Redl | 5f4e899 | 2010-09-13 21:10:20 +0000 | [diff] [blame] | 2165 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2166 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2167 | // A template constructor is never a copy constructor. |
| 2168 | // FIXME: However, it may actually be selected at the actual overload |
| 2169 | // resolution point. |
| 2170 | if (isa<FunctionTemplateDecl>(*Con)) |
| 2171 | continue; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2172 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 2173 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 2174 | FoundConstructor = true; |
| 2175 | const FunctionProtoType *CPT |
| 2176 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2177 | // TODO: check whether evaluating default arguments can throw. |
| 2178 | // For now, we'll be conservative and assume that they can throw. |
| 2179 | if (!CPT->hasEmptyExceptionSpec() || CPT->getNumArgs() > 1) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2180 | AllNoThrow = false; |
| 2181 | break; |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | return FoundConstructor && AllNoThrow; |
| 2187 | } |
| 2188 | return false; |
| 2189 | case UTT_HasNothrowConstructor: |
| 2190 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2191 | // If __has_trivial_constructor (type) is true then the trait is |
| 2192 | // true, else if type is a cv class or union type (or array |
| 2193 | // thereof) with a default constructor that is known not to |
| 2194 | // throw an exception then the trait is true, else it is false. |
| 2195 | if (T->isPODType()) |
| 2196 | return true; |
| 2197 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) { |
| 2198 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2199 | if (RD->hasTrivialConstructor()) |
| 2200 | return true; |
| 2201 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2202 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 2203 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
| 2204 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2205 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 2206 | if (isa<FunctionTemplateDecl>(*Con)) |
| 2207 | continue; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2208 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 2209 | if (Constructor->isDefaultConstructor()) { |
| 2210 | const FunctionProtoType *CPT |
| 2211 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 2212 | // TODO: check whether evaluating default arguments can throw. |
| 2213 | // For now, we'll be conservative and assume that they can throw. |
| 2214 | return CPT->hasEmptyExceptionSpec() && CPT->getNumArgs() == 0; |
| 2215 | } |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2216 | } |
| 2217 | } |
| 2218 | return false; |
| 2219 | case UTT_HasVirtualDestructor: |
| 2220 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2221 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 2222 | // then the trait is true, else it is false. |
| 2223 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2224 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2225 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2226 | return Destructor->isVirtual(); |
| 2227 | } |
| 2228 | return false; |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | ExprResult Sema::BuildUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2233 | SourceLocation KWLoc, |
| 2234 | TypeSourceInfo *TSInfo, |
| 2235 | SourceLocation RParen) { |
| 2236 | QualType T = TSInfo->getType(); |
| 2237 | |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 2238 | // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
| 2239 | // all traits except __is_class, __is_enum and __is_union require a the type |
Sebastian Redl | 607a178 | 2010-09-08 00:48:43 +0000 | [diff] [blame] | 2240 | // to be complete, an array of unknown bound, or void. |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2241 | if (UTT != UTT_IsClass && UTT != UTT_IsEnum && UTT != UTT_IsUnion) { |
Sebastian Redl | 607a178 | 2010-09-08 00:48:43 +0000 | [diff] [blame] | 2242 | QualType E = T; |
| 2243 | if (T->isIncompleteArrayType()) |
| 2244 | E = Context.getAsArrayType(T)->getElementType(); |
| 2245 | if (!T->isVoidType() && |
| 2246 | RequireCompleteType(KWLoc, E, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 2247 | diag::err_incomplete_type_used_in_type_trait_expr)) |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 2248 | return ExprError(); |
| 2249 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2250 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2251 | bool Value = false; |
| 2252 | if (!T->isDependentType()) |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2253 | Value = EvaluateUnaryTypeTrait(*this, UTT, T, KWLoc); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2254 | |
| 2255 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, UTT, TSInfo, Value, |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 2256 | RParen, Context.BoolTy)); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2257 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2258 | |
| 2259 | QualType Sema::CheckPointerToMemberOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2261 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 2262 | // C++ 5.5p2 |
| 2263 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 2264 | // be of type "pointer to member of T" (where T is a completely-defined |
| 2265 | // class type) [...] |
| 2266 | QualType RType = rex->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2267 | const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2268 | if (!MemPtr) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2269 | Diag(Loc, diag::err_bad_memptr_rhs) |
| 2270 | << OpSpelling << RType << rex->getSourceRange(); |
| 2271 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2273 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2274 | QualType Class(MemPtr->getClass(), 0); |
| 2275 | |
Sebastian Redl | 59fc269 | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 2276 | if (RequireCompleteType(Loc, Class, diag::err_memptr_rhs_to_incomplete)) |
| 2277 | return QualType(); |
| 2278 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2279 | // C++ 5.5p2 |
| 2280 | // [...] to its first operand, which shall be of class T or of a class of |
| 2281 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 2282 | // such a class] |
| 2283 | QualType LType = lex->getType(); |
| 2284 | if (isIndirect) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2285 | if (const PointerType *Ptr = LType->getAs<PointerType>()) |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2286 | LType = Ptr->getPointeeType().getNonReferenceType(); |
| 2287 | else { |
| 2288 | Diag(Loc, diag::err_bad_memptr_lhs) |
Fariborz Jahanian | ef78ac6 | 2009-10-26 20:45:27 +0000 | [diff] [blame] | 2289 | << OpSpelling << 1 << LType |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2290 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2291 | return QualType(); |
| 2292 | } |
| 2293 | } |
| 2294 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2295 | if (!Context.hasSameUnqualifiedType(Class, LType)) { |
Sebastian Redl | 17e1d35 | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 2296 | // If we want to check the hierarchy, we need a complete type. |
| 2297 | if (RequireCompleteType(Loc, LType, PDiag(diag::err_bad_memptr_lhs) |
| 2298 | << OpSpelling << (int)isIndirect)) { |
| 2299 | return QualType(); |
| 2300 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2301 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2302 | /*DetectVirtual=*/false); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2303 | // FIXME: Would it be useful to print full ambiguity paths, or is that |
| 2304 | // overkill? |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2305 | if (!IsDerivedFrom(LType, Class, Paths) || |
| 2306 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
| 2307 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 2308 | << (int)isIndirect << lex->getType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2309 | return QualType(); |
| 2310 | } |
Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 2311 | // Cast LHS to type of use. |
| 2312 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2313 | ExprValueKind VK = |
| 2314 | isIndirect ? VK_RValue : CastCategory(lex); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2315 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2316 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2317 | BuildBasePathArray(Paths, BasePath); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2318 | ImpCastExprToType(lex, UseType, CK_DerivedToBase, VK, &BasePath); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 2321 | if (isa<CXXScalarValueInitExpr>(rex->IgnoreParens())) { |
Fariborz Jahanian | 05ebda9 | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 2322 | // Diagnose use of pointer-to-member type which when used as |
| 2323 | // the functional cast in a pointer-to-member expression. |
| 2324 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 2325 | return QualType(); |
| 2326 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2327 | // C++ 5.5p2 |
| 2328 | // The result is an object or a function of the type specified by the |
| 2329 | // second operand. |
| 2330 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 2331 | // in accordance with 5.5p5 and 5.2.5. |
| 2332 | // FIXME: This returns a dereferenced member function pointer as a normal |
| 2333 | // function type. However, the only operation valid on such functions is |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2334 | // calling them. There's also a GCC extension to get a function pointer to the |
| 2335 | // thing, which is another complication, because this type - unlike the type |
| 2336 | // that is the result of this expression - takes the class as the first |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2337 | // argument. |
| 2338 | // We probably need a "MemberFunctionClosureType" or something like that. |
| 2339 | QualType Result = MemPtr->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2340 | Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers()); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2341 | return Result; |
| 2342 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2343 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2344 | /// \brief Try to convert a type to another according to C++0x 5.16p3. |
| 2345 | /// |
| 2346 | /// This is part of the parameter validation for the ? operator. If either |
| 2347 | /// value operand is a class type, the two operands are attempted to be |
| 2348 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2349 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 2350 | /// as such. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2351 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 2352 | SourceLocation QuestionLoc, |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2353 | bool &HaveConversion, |
| 2354 | QualType &ToType) { |
| 2355 | HaveConversion = false; |
| 2356 | ToType = To->getType(); |
| 2357 | |
| 2358 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
| 2359 | SourceLocation()); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2360 | // C++0x 5.16p3 |
| 2361 | // The process for determining whether an operand expression E1 of type T1 |
| 2362 | // can be converted to match an operand expression E2 of type T2 is defined |
| 2363 | // as follows: |
| 2364 | // -- If E2 is an lvalue: |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 2365 | bool ToIsLvalue = (To->isLvalue(Self.Context) == Expr::LV_Valid); |
| 2366 | if (ToIsLvalue) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2367 | // E1 can be converted to match E2 if E1 can be implicitly converted to |
| 2368 | // type "lvalue reference to T2", subject to the constraint that in the |
| 2369 | // conversion the reference must bind directly to E1. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2370 | QualType T = Self.Context.getLValueReferenceType(ToType); |
| 2371 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
| 2372 | |
| 2373 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 2374 | if (InitSeq.isDirectReferenceBinding()) { |
| 2375 | ToType = T; |
| 2376 | HaveConversion = true; |
| 2377 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2378 | } |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2379 | |
| 2380 | if (InitSeq.isAmbiguous()) |
| 2381 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2382 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2383 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2384 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 2385 | // -- if E1 and E2 have class type, and the underlying class types are |
| 2386 | // the same or one is a base class of the other: |
| 2387 | QualType FTy = From->getType(); |
| 2388 | QualType TTy = To->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2389 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 2390 | const RecordType *TRec = TTy->getAs<RecordType>(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2391 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
| 2392 | Self.IsDerivedFrom(FTy, TTy); |
| 2393 | if (FRec && TRec && |
| 2394 | (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2395 | // E1 can be converted to match E2 if the class of T2 is the |
| 2396 | // same type as, or a base class of, the class of T1, and |
| 2397 | // [cv2 > cv1]. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2398 | if (FRec == TRec || FDerivedFromT) { |
| 2399 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2400 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 2401 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 2402 | if (InitSeq.getKind() != InitializationSequence::FailedSequence) { |
| 2403 | HaveConversion = true; |
| 2404 | return false; |
| 2405 | } |
| 2406 | |
| 2407 | if (InitSeq.isAmbiguous()) |
| 2408 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
| 2409 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2410 | } |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2411 | |
| 2412 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2413 | } |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2414 | |
| 2415 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 2416 | // implicitly converted to the type that expression E2 would have |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 2417 | // if E2 were converted to an rvalue (or the type it has, if E2 is |
| 2418 | // an rvalue). |
| 2419 | // |
| 2420 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 2421 | // to the array-to-pointer or function-to-pointer conversions. |
| 2422 | if (!TTy->getAs<TagType>()) |
| 2423 | TTy = TTy.getUnqualifiedType(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2424 | |
| 2425 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 2426 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 2427 | HaveConversion = InitSeq.getKind() != InitializationSequence::FailedSequence; |
| 2428 | ToType = TTy; |
| 2429 | if (InitSeq.isAmbiguous()) |
| 2430 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
| 2431 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2432 | return false; |
| 2433 | } |
| 2434 | |
| 2435 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 2436 | /// |
| 2437 | /// This is part of the parameter validation for the ? operator. If either |
| 2438 | /// value operand is a class type, overload resolution is used to find a |
| 2439 | /// conversion to a common type. |
| 2440 | static bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS, |
| 2441 | SourceLocation Loc) { |
| 2442 | Expr *Args[2] = { LHS, RHS }; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2443 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2444 | Self.AddBuiltinOperatorCandidates(OO_Conditional, Loc, Args, 2, CandidateSet); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2445 | |
| 2446 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2447 | switch (CandidateSet.BestViableFunction(Self, Loc, Best)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2448 | case OR_Success: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2449 | // We found a match. Perform the conversions on the arguments and move on. |
| 2450 | if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2451 | Best->Conversions[0], Sema::AA_Converting) || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2452 | Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2453 | Best->Conversions[1], Sema::AA_Converting)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2454 | break; |
| 2455 | return false; |
| 2456 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2457 | case OR_No_Viable_Function: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2458 | Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 2459 | << LHS->getType() << RHS->getType() |
| 2460 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2461 | return true; |
| 2462 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2463 | case OR_Ambiguous: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2464 | Self.Diag(Loc, diag::err_conditional_ambiguous_ovl) |
| 2465 | << LHS->getType() << RHS->getType() |
| 2466 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2467 | // FIXME: Print the possible common types by printing the return types of |
| 2468 | // the viable candidates. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2469 | break; |
| 2470 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2471 | case OR_Deleted: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2472 | assert(false && "Conditional operator has only built-in overloads"); |
| 2473 | break; |
| 2474 | } |
| 2475 | return true; |
| 2476 | } |
| 2477 | |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2478 | /// \brief Perform an "extended" implicit conversion as returned by |
| 2479 | /// TryClassUnification. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2480 | static bool ConvertForConditional(Sema &Self, Expr *&E, QualType T) { |
| 2481 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
| 2482 | InitializationKind Kind = InitializationKind::CreateCopy(E->getLocStart(), |
| 2483 | SourceLocation()); |
| 2484 | InitializationSequence InitSeq(Self, Entity, Kind, &E, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2485 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, MultiExprArg(&E, 1)); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2486 | if (Result.isInvalid()) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2487 | return true; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2488 | |
| 2489 | E = Result.takeAs<Expr>(); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2490 | return false; |
| 2491 | } |
| 2492 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2493 | /// \brief Check the operands of ?: under C++ semantics. |
| 2494 | /// |
| 2495 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 2496 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
| 2497 | QualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
Fariborz Jahanian | 1fb019b | 2010-09-18 19:38:38 +0000 | [diff] [blame] | 2498 | Expr *&SAVE, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2499 | SourceLocation QuestionLoc) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2500 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 2501 | // interface pointers. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2502 | |
| 2503 | // C++0x 5.16p1 |
| 2504 | // The first expression is contextually converted to bool. |
| 2505 | if (!Cond->isTypeDependent()) { |
Fariborz Jahanian | 1fb019b | 2010-09-18 19:38:38 +0000 | [diff] [blame] | 2506 | if (SAVE && Cond->getType()->isArrayType()) { |
| 2507 | QualType CondTy = Cond->getType(); |
| 2508 | CondTy = Context.getArrayDecayedType(CondTy); |
| 2509 | ImpCastExprToType(Cond, CondTy, CK_ArrayToPointerDecay); |
| 2510 | SAVE = LHS = Cond; |
| 2511 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2512 | if (CheckCXXBooleanCondition(Cond)) |
| 2513 | return QualType(); |
| 2514 | } |
| 2515 | |
| 2516 | // Either of the arguments dependent? |
| 2517 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) |
| 2518 | return Context.DependentTy; |
| 2519 | |
| 2520 | // C++0x 5.16p2 |
| 2521 | // If either the second or the third operand has type (cv) void, ... |
| 2522 | QualType LTy = LHS->getType(); |
| 2523 | QualType RTy = RHS->getType(); |
| 2524 | bool LVoid = LTy->isVoidType(); |
| 2525 | bool RVoid = RTy->isVoidType(); |
| 2526 | if (LVoid || RVoid) { |
| 2527 | // ... then the [l2r] conversions are performed on the second and third |
| 2528 | // operands ... |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2529 | DefaultFunctionArrayLvalueConversion(LHS); |
| 2530 | DefaultFunctionArrayLvalueConversion(RHS); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2531 | LTy = LHS->getType(); |
| 2532 | RTy = RHS->getType(); |
| 2533 | |
| 2534 | // ... and one of the following shall hold: |
| 2535 | // -- The second or the third operand (but not both) is a throw- |
| 2536 | // expression; the result is of the type of the other and is an rvalue. |
| 2537 | bool LThrow = isa<CXXThrowExpr>(LHS); |
| 2538 | bool RThrow = isa<CXXThrowExpr>(RHS); |
| 2539 | if (LThrow && !RThrow) |
| 2540 | return RTy; |
| 2541 | if (RThrow && !LThrow) |
| 2542 | return LTy; |
| 2543 | |
| 2544 | // -- Both the second and third operands have type void; the result is of |
| 2545 | // type void and is an rvalue. |
| 2546 | if (LVoid && RVoid) |
| 2547 | return Context.VoidTy; |
| 2548 | |
| 2549 | // Neither holds, error. |
| 2550 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 2551 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
| 2552 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2553 | return QualType(); |
| 2554 | } |
| 2555 | |
| 2556 | // Neither is void. |
| 2557 | |
| 2558 | // C++0x 5.16p3 |
| 2559 | // Otherwise, if the second and third operand have different types, and |
| 2560 | // either has (cv) class type, and attempt is made to convert each of those |
| 2561 | // operands to the other. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2562 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2563 | (LTy->isRecordType() || RTy->isRecordType())) { |
| 2564 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; |
| 2565 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2566 | QualType L2RType, R2LType; |
| 2567 | bool HaveL2R, HaveR2L; |
| 2568 | if (TryClassUnification(*this, LHS, RHS, QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2569 | return QualType(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2570 | if (TryClassUnification(*this, RHS, LHS, QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2571 | return QualType(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2572 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2573 | // If both can be converted, [...] the program is ill-formed. |
| 2574 | if (HaveL2R && HaveR2L) { |
| 2575 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
| 2576 | << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2577 | return QualType(); |
| 2578 | } |
| 2579 | |
| 2580 | // If exactly one conversion is possible, that conversion is applied to |
| 2581 | // the chosen operand and the converted operands are used in place of the |
| 2582 | // original operands for the remainder of this section. |
| 2583 | if (HaveL2R) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2584 | if (ConvertForConditional(*this, LHS, L2RType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2585 | return QualType(); |
| 2586 | LTy = LHS->getType(); |
| 2587 | } else if (HaveR2L) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2588 | if (ConvertForConditional(*this, RHS, R2LType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2589 | return QualType(); |
| 2590 | RTy = RHS->getType(); |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | // C++0x 5.16p4 |
| 2595 | // If the second and third operands are lvalues and have the same type, |
| 2596 | // the result is of that type [...] |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 2597 | bool Same = Context.hasSameType(LTy, RTy); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2598 | if (Same && LHS->isLvalue(Context) == Expr::LV_Valid && |
Fariborz Jahanian | 3911a1a | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 2599 | RHS->isLvalue(Context) == Expr::LV_Valid) { |
| 2600 | // In this context, property reference is really a message call and |
| 2601 | // is not considered an l-value. |
| 2602 | bool lhsProperty = (isa<ObjCPropertyRefExpr>(LHS) || |
| 2603 | isa<ObjCImplicitSetterGetterRefExpr>(LHS)); |
| 2604 | bool rhsProperty = (isa<ObjCPropertyRefExpr>(RHS) || |
| 2605 | isa<ObjCImplicitSetterGetterRefExpr>(RHS)); |
| 2606 | if (!lhsProperty && !rhsProperty) |
| 2607 | return LTy; |
| 2608 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2609 | |
| 2610 | // C++0x 5.16p5 |
| 2611 | // Otherwise, the result is an rvalue. If the second and third operands |
| 2612 | // do not have the same type, and either has (cv) class type, ... |
| 2613 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 2614 | // ... overload resolution is used to determine the conversions (if any) |
| 2615 | // to be applied to the operands. If the overload resolution fails, the |
| 2616 | // program is ill-formed. |
| 2617 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 2618 | return QualType(); |
| 2619 | } |
| 2620 | |
| 2621 | // C++0x 5.16p6 |
| 2622 | // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 2623 | // conversions are performed on the second and third operands. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2624 | DefaultFunctionArrayLvalueConversion(LHS); |
| 2625 | DefaultFunctionArrayLvalueConversion(RHS); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2626 | LTy = LHS->getType(); |
| 2627 | RTy = RHS->getType(); |
| 2628 | |
| 2629 | // After those conversions, one of the following shall hold: |
| 2630 | // -- The second and third operands have the same type; the result |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 2631 | // is of that type. If the operands have class type, the result |
| 2632 | // is a prvalue temporary of the result type, which is |
| 2633 | // copy-initialized from either the second operand or the third |
| 2634 | // operand depending on the value of the first operand. |
| 2635 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 2636 | if (LTy->isRecordType()) { |
| 2637 | // The operands have class type. Make a temporary copy. |
| 2638 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2639 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 2640 | SourceLocation(), |
| 2641 | Owned(LHS)); |
| 2642 | if (LHSCopy.isInvalid()) |
| 2643 | return QualType(); |
| 2644 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2645 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 2646 | SourceLocation(), |
| 2647 | Owned(RHS)); |
| 2648 | if (RHSCopy.isInvalid()) |
| 2649 | return QualType(); |
| 2650 | |
| 2651 | LHS = LHSCopy.takeAs<Expr>(); |
| 2652 | RHS = RHSCopy.takeAs<Expr>(); |
| 2653 | } |
| 2654 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2655 | return LTy; |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 2656 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2657 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2658 | // Extension: conditional operator involving vector types. |
| 2659 | if (LTy->isVectorType() || RTy->isVectorType()) |
| 2660 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
| 2661 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2662 | // -- The second and third operands have arithmetic or enumeration type; |
| 2663 | // the usual arithmetic conversions are performed to bring them to a |
| 2664 | // common type, and the result is of that type. |
| 2665 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
| 2666 | UsualArithmeticConversions(LHS, RHS); |
| 2667 | return LHS->getType(); |
| 2668 | } |
| 2669 | |
| 2670 | // -- The second and third operands have pointer type, or one has pointer |
| 2671 | // type and the other is a null pointer constant; pointer conversions |
| 2672 | // and qualification conversions are performed to bring them to their |
| 2673 | // composite pointer type. The result is of the composite pointer type. |
Eli Friedman | de8ac49 | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 2674 | // -- The second and third operands have pointer to member type, or one has |
| 2675 | // pointer to member type and the other is a null pointer constant; |
| 2676 | // pointer to member conversions and qualification conversions are |
| 2677 | // performed to bring them to a common type, whose cv-qualification |
| 2678 | // shall match the cv-qualification of either the second or the third |
| 2679 | // operand. The result is of the common type. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2680 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2681 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2682 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
| 2683 | if (!Composite.isNull()) { |
| 2684 | if (NonStandardCompositeType) |
| 2685 | Diag(QuestionLoc, |
| 2686 | diag::ext_typecheck_cond_incompatible_operands_nonstandard) |
| 2687 | << LTy << RTy << Composite |
| 2688 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2689 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2690 | return Composite; |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2691 | } |
Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 2692 | |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 2693 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 2694 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 2695 | if (!Composite.isNull()) |
| 2696 | return Composite; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2697 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2698 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 2699 | << LHS->getType() << RHS->getType() |
| 2700 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2701 | return QualType(); |
| 2702 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2703 | |
| 2704 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 2705 | /// |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2706 | /// This finds the composite pointer type (or member pointer type) for @p E1 |
| 2707 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this |
| 2708 | /// type and returns it. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2709 | /// It does not emit diagnostics. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2710 | /// |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2711 | /// \param Loc The location of the operator requiring these two expressions to |
| 2712 | /// be converted to the composite pointer type. |
| 2713 | /// |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2714 | /// If \p NonStandardCompositeType is non-NULL, then we are permitted to find |
| 2715 | /// a non-standard (but still sane) composite type to which both expressions |
| 2716 | /// can be converted. When such a type is chosen, \c *NonStandardCompositeType |
| 2717 | /// will be set true. |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2718 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
| 2719 | Expr *&E1, Expr *&E2, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2720 | bool *NonStandardCompositeType) { |
| 2721 | if (NonStandardCompositeType) |
| 2722 | *NonStandardCompositeType = false; |
| 2723 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2724 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); |
| 2725 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | |
Fariborz Jahanian | 0cedfbd | 2009-12-08 20:04:24 +0000 | [diff] [blame] | 2727 | if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && |
| 2728 | !T2->isAnyPointerType() && !T2->isMemberPointerType()) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2729 | return QualType(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2730 | |
| 2731 | // C++0x 5.9p2 |
| 2732 | // Pointer conversions and qualification conversions are performed on |
| 2733 | // pointer operands to bring them to their composite pointer type. If |
| 2734 | // one operand is a null pointer constant, the composite pointer type is |
| 2735 | // the type of the other operand. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2736 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2737 | if (T2->isMemberPointerType()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2738 | ImpCastExprToType(E1, T2, CK_NullToMemberPointer); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2739 | else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2740 | ImpCastExprToType(E1, T2, CK_IntegralToPointer); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2741 | return T2; |
| 2742 | } |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2743 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2744 | if (T1->isMemberPointerType()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2745 | ImpCastExprToType(E2, T1, CK_NullToMemberPointer); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2746 | else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2747 | ImpCastExprToType(E2, T1, CK_IntegralToPointer); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2748 | return T1; |
| 2749 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2750 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2751 | // Now both have to be pointers or member pointers. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2752 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || |
| 2753 | (!T2->isPointerType() && !T2->isMemberPointerType())) |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2754 | return QualType(); |
| 2755 | |
| 2756 | // Otherwise, of one of the operands has type "pointer to cv1 void," then |
| 2757 | // the other has type "pointer to cv2 T" and the composite pointer type is |
| 2758 | // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. |
| 2759 | // Otherwise, the composite pointer type is a pointer type similar to the |
| 2760 | // type of one of the operands, with a cv-qualification signature that is |
| 2761 | // the union of the cv-qualification signatures of the operand types. |
| 2762 | // In practice, the first part here is redundant; it's subsumed by the second. |
| 2763 | // What we do here is, we build the two possible composite types, and try the |
| 2764 | // conversions in both directions. If only one works, or if the two composite |
| 2765 | // types are the same, we have succeeded. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2766 | // FIXME: extended qualifiers? |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2767 | typedef llvm::SmallVector<unsigned, 4> QualifierVector; |
| 2768 | QualifierVector QualifierUnion; |
| 2769 | typedef llvm::SmallVector<std::pair<const Type *, const Type *>, 4> |
| 2770 | ContainingClassVector; |
| 2771 | ContainingClassVector MemberOfClass; |
| 2772 | QualType Composite1 = Context.getCanonicalType(T1), |
| 2773 | Composite2 = Context.getCanonicalType(T2); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2774 | unsigned NeedConstBefore = 0; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2775 | do { |
| 2776 | const PointerType *Ptr1, *Ptr2; |
| 2777 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 2778 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 2779 | Composite1 = Ptr1->getPointeeType(); |
| 2780 | Composite2 = Ptr2->getPointeeType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2781 | |
| 2782 | // If we're allowed to create a non-standard composite type, keep track |
| 2783 | // of where we need to fill in additional 'const' qualifiers. |
| 2784 | if (NonStandardCompositeType && |
| 2785 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 2786 | NeedConstBefore = QualifierUnion.size(); |
| 2787 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2788 | QualifierUnion.push_back( |
| 2789 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 2790 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); |
| 2791 | continue; |
| 2792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2793 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2794 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 2795 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 2796 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 2797 | Composite1 = MemPtr1->getPointeeType(); |
| 2798 | Composite2 = MemPtr2->getPointeeType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2799 | |
| 2800 | // If we're allowed to create a non-standard composite type, keep track |
| 2801 | // of where we need to fill in additional 'const' qualifiers. |
| 2802 | if (NonStandardCompositeType && |
| 2803 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 2804 | NeedConstBefore = QualifierUnion.size(); |
| 2805 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2806 | QualifierUnion.push_back( |
| 2807 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 2808 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 2809 | MemPtr2->getClass())); |
| 2810 | continue; |
| 2811 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2813 | // FIXME: block pointer types? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2814 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2815 | // Cannot unwrap any more types. |
| 2816 | break; |
| 2817 | } while (true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2819 | if (NeedConstBefore && NonStandardCompositeType) { |
| 2820 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
| 2821 | // mismatch, so that our (non-standard!) composite type meets the |
| 2822 | // requirements of C++ [conv.qual]p4 bullet 3. |
| 2823 | for (unsigned I = 0; I != NeedConstBefore; ++I) { |
| 2824 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) { |
| 2825 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
| 2826 | *NonStandardCompositeType = true; |
| 2827 | } |
| 2828 | } |
| 2829 | } |
| 2830 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2831 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2832 | ContainingClassVector::reverse_iterator MOC |
| 2833 | = MemberOfClass.rbegin(); |
| 2834 | for (QualifierVector::reverse_iterator |
| 2835 | I = QualifierUnion.rbegin(), |
| 2836 | E = QualifierUnion.rend(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2837 | I != E; (void)++I, ++MOC) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2838 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2839 | if (MOC->first && MOC->second) { |
| 2840 | // Rebuild member pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2841 | Composite1 = Context.getMemberPointerType( |
| 2842 | Context.getQualifiedType(Composite1, Quals), |
| 2843 | MOC->first); |
| 2844 | Composite2 = Context.getMemberPointerType( |
| 2845 | Context.getQualifiedType(Composite2, Quals), |
| 2846 | MOC->second); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2847 | } else { |
| 2848 | // Rebuild pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2849 | Composite1 |
| 2850 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 2851 | Composite2 |
| 2852 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2853 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2856 | // Try to convert to the first composite pointer type. |
| 2857 | InitializedEntity Entity1 |
| 2858 | = InitializedEntity::InitializeTemporary(Composite1); |
| 2859 | InitializationKind Kind |
| 2860 | = InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 2861 | InitializationSequence E1ToC1(*this, Entity1, Kind, &E1, 1); |
| 2862 | InitializationSequence E2ToC1(*this, Entity1, Kind, &E2, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2864 | if (E1ToC1 && E2ToC1) { |
| 2865 | // Conversion to Composite1 is viable. |
| 2866 | if (!Context.hasSameType(Composite1, Composite2)) { |
| 2867 | // Composite2 is a different type from Composite1. Check whether |
| 2868 | // Composite2 is also viable. |
| 2869 | InitializedEntity Entity2 |
| 2870 | = InitializedEntity::InitializeTemporary(Composite2); |
| 2871 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 2872 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 2873 | if (E1ToC2 && E2ToC2) { |
| 2874 | // Both Composite1 and Composite2 are viable and are different; |
| 2875 | // this is an ambiguity. |
| 2876 | return QualType(); |
| 2877 | } |
| 2878 | } |
| 2879 | |
| 2880 | // Convert E1 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2881 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2882 | = E1ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E1,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2883 | if (E1Result.isInvalid()) |
| 2884 | return QualType(); |
| 2885 | E1 = E1Result.takeAs<Expr>(); |
| 2886 | |
| 2887 | // Convert E2 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2888 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2889 | = E2ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E2,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2890 | if (E2Result.isInvalid()) |
| 2891 | return QualType(); |
| 2892 | E2 = E2Result.takeAs<Expr>(); |
| 2893 | |
| 2894 | return Composite1; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2897 | // Check whether Composite2 is viable. |
| 2898 | InitializedEntity Entity2 |
| 2899 | = InitializedEntity::InitializeTemporary(Composite2); |
| 2900 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 2901 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 2902 | if (!E1ToC2 || !E2ToC2) |
| 2903 | return QualType(); |
| 2904 | |
| 2905 | // Convert E1 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2906 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2907 | = E1ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E1, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2908 | if (E1Result.isInvalid()) |
| 2909 | return QualType(); |
| 2910 | E1 = E1Result.takeAs<Expr>(); |
| 2911 | |
| 2912 | // Convert E2 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2913 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2914 | = E2ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E2, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2915 | if (E2Result.isInvalid()) |
| 2916 | return QualType(); |
| 2917 | E2 = E2Result.takeAs<Expr>(); |
| 2918 | |
| 2919 | return Composite2; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2920 | } |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2921 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2922 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Anders Carlsson | 089c260 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 2923 | if (!Context.getLangOptions().CPlusPlus) |
| 2924 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
Douglas Gregor | 5132655 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 2926 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 2927 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2928 | const RecordType *RT = E->getType()->getAs<RecordType>(); |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2929 | if (!RT) |
| 2930 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | |
Anders Carlsson | 0ea4dfd | 2010-07-16 21:18:37 +0000 | [diff] [blame] | 2932 | // If this is the result of a call or an Objective-C message send expression, |
| 2933 | // our source might actually be a reference, in which case we shouldn't bind. |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 2934 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) { |
Anders Carlsson | 0ea4dfd | 2010-07-16 21:18:37 +0000 | [diff] [blame] | 2935 | if (CE->getCallReturnType()->isReferenceType()) |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 2936 | return Owned(E); |
Anders Carlsson | 0ea4dfd | 2010-07-16 21:18:37 +0000 | [diff] [blame] | 2937 | } else if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { |
| 2938 | if (const ObjCMethodDecl *MD = ME->getMethodDecl()) { |
| 2939 | if (MD->getResultType()->isReferenceType()) |
| 2940 | return Owned(E); |
| 2941 | } |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 2942 | } |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2943 | |
| 2944 | // That should be enough to guarantee that this type is complete. |
| 2945 | // If it has a trivial destructor, we can avoid the extra copy. |
| 2946 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 507384f | 2010-08-12 02:40:37 +0000 | [diff] [blame] | 2947 | if (RD->isInvalidDecl() || RD->hasTrivialDestructor()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2948 | return Owned(E); |
| 2949 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2950 | CXXTemporary *Temp = CXXTemporary::Create(Context, LookupDestructor(RD)); |
Anders Carlsson | 860306e | 2009-05-30 21:21:49 +0000 | [diff] [blame] | 2951 | ExprTemporaries.push_back(Temp); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2952 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2953 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 2954 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 2955 | PDiag(diag::err_access_dtor_temp) |
| 2956 | << E->getType()); |
| 2957 | } |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2958 | // FIXME: Add the temporary to the temporaries vector. |
| 2959 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); |
| 2960 | } |
| 2961 | |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 2962 | Expr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr) { |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2963 | assert(SubExpr && "sub expression can't be null!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 2965 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; |
| 2966 | assert(ExprTemporaries.size() >= FirstTemporary); |
| 2967 | if (ExprTemporaries.size() == FirstTemporary) |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2968 | return SubExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2969 | |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2970 | Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr, |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 2971 | &ExprTemporaries[FirstTemporary], |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 2972 | ExprTemporaries.size() - FirstTemporary); |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 2973 | ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, |
| 2974 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2976 | return E; |
| 2977 | } |
| 2978 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2979 | ExprResult |
| 2980 | Sema::MaybeCreateCXXExprWithTemporaries(ExprResult SubExpr) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 2981 | if (SubExpr.isInvalid()) |
| 2982 | return ExprError(); |
| 2983 | |
| 2984 | return Owned(MaybeCreateCXXExprWithTemporaries(SubExpr.takeAs<Expr>())); |
| 2985 | } |
| 2986 | |
Anders Carlsson | 5ee56e9 | 2009-12-16 02:09:40 +0000 | [diff] [blame] | 2987 | FullExpr Sema::CreateFullExpr(Expr *SubExpr) { |
| 2988 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; |
| 2989 | assert(ExprTemporaries.size() >= FirstTemporary); |
| 2990 | |
| 2991 | unsigned NumTemporaries = ExprTemporaries.size() - FirstTemporary; |
| 2992 | CXXTemporary **Temporaries = |
| 2993 | NumTemporaries == 0 ? 0 : &ExprTemporaries[FirstTemporary]; |
| 2994 | |
| 2995 | FullExpr E = FullExpr::Create(Context, SubExpr, Temporaries, NumTemporaries); |
| 2996 | |
| 2997 | ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, |
| 2998 | ExprTemporaries.end()); |
| 2999 | |
| 3000 | return E; |
| 3001 | } |
| 3002 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3003 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3004 | Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3005 | tok::TokenKind OpKind, ParsedType &ObjectType, |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3006 | bool &MayBePseudoDestructor) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3007 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3008 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3009 | if (Result.isInvalid()) return ExprError(); |
| 3010 | Base = Result.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3011 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3012 | QualType BaseType = Base->getType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3013 | MayBePseudoDestructor = false; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3014 | if (BaseType->isDependentType()) { |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 3015 | // If we have a pointer to a dependent type and are using the -> operator, |
| 3016 | // the object type is the type that the pointer points to. We might still |
| 3017 | // have enough information about that type to do something useful. |
| 3018 | if (OpKind == tok::arrow) |
| 3019 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3020 | BaseType = Ptr->getPointeeType(); |
| 3021 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3022 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3023 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3024 | return Owned(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3026 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3027 | // C++ [over.match.oper]p8: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3028 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3029 | // returned, with the original second operand. |
| 3030 | if (OpKind == tok::arrow) { |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 3031 | // The set of types we've considered so far. |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 3032 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 3033 | llvm::SmallVector<SourceLocation, 8> Locations; |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 3034 | CTypes.insert(Context.getCanonicalType(BaseType)); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 3035 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3036 | while (BaseType->isRecordType()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3037 | Result = BuildOverloadedArrowExpr(S, Base, OpLoc); |
| 3038 | if (Result.isInvalid()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3039 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3040 | Base = Result.get(); |
| 3041 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Anders Carlsson | de699e5 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 3042 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3043 | BaseType = Base->getType(); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 3044 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 3045 | if (!CTypes.insert(CBaseType)) { |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 3046 | Diag(OpLoc, diag::err_operator_arrow_circular); |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 3047 | for (unsigned i = 0; i < Locations.size(); i++) |
| 3048 | Diag(Locations[i], diag::note_declared_at); |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 3049 | return ExprError(); |
| 3050 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | |
Douglas Gregor | 31658df | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 3053 | if (BaseType->isPointerType()) |
| 3054 | BaseType = BaseType->getPointeeType(); |
| 3055 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | |
| 3057 | // 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] | 3058 | // vector types or Objective-C interfaces. Just return early and let |
| 3059 | // ActOnMemberReferenceExpr do the work. |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 3060 | if (!BaseType->isRecordType()) { |
| 3061 | // C++ [basic.lookup.classref]p2: |
| 3062 | // [...] If the type of the object expression is of pointer to scalar |
| 3063 | // type, the unqualified-id is looked up in the context of the complete |
| 3064 | // postfix-expression. |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3065 | // |
| 3066 | // This also indicates that we should be parsing a |
| 3067 | // pseudo-destructor-name. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3068 | ObjectType = ParsedType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3069 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3070 | return Owned(Base); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 3071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 3073 | // The object type must be complete (or dependent). |
| 3074 | if (!BaseType->isDependentType() && |
| 3075 | RequireCompleteType(OpLoc, BaseType, |
| 3076 | PDiag(diag::err_incomplete_member_access))) |
| 3077 | return ExprError(); |
| 3078 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 3079 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3080 | // 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] | 3081 | // 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] | 3082 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 3083 | // up in the scope of class C. [...] |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3084 | ObjectType = ParsedType::make(BaseType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | return move(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3088 | ExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3089 | Expr *MemExpr) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3090 | SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3091 | Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call) |
| 3092 | << isa<CXXPseudoDestructorExpr>(MemExpr) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3093 | << FixItHint::CreateInsertion(ExpectedLParenLoc, "()"); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3094 | |
| 3095 | return ActOnCallExpr(/*Scope*/ 0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3096 | MemExpr, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3097 | /*LPLoc*/ ExpectedLParenLoc, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3098 | MultiExprArg(), |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3099 | /*RPLoc*/ ExpectedLParenLoc); |
| 3100 | } |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3101 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3102 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3103 | SourceLocation OpLoc, |
| 3104 | tok::TokenKind OpKind, |
| 3105 | const CXXScopeSpec &SS, |
Douglas Gregor | 26d4ac9 | 2010-02-24 23:40:28 +0000 | [diff] [blame] | 3106 | TypeSourceInfo *ScopeTypeInfo, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3107 | SourceLocation CCLoc, |
Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 3108 | SourceLocation TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3109 | PseudoDestructorTypeStorage Destructed, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3110 | bool HasTrailingLParen) { |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3111 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3112 | |
| 3113 | // C++ [expr.pseudo]p2: |
| 3114 | // The left-hand side of the dot operator shall be of scalar type. The |
| 3115 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 3116 | // This scalar type is the object type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3117 | QualType ObjectType = Base->getType(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3118 | if (OpKind == tok::arrow) { |
| 3119 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 3120 | ObjectType = Ptr->getPointeeType(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3121 | } else if (!Base->isTypeDependent()) { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3122 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 3123 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 3124 | << ObjectType << true |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3125 | << FixItHint::CreateReplacement(OpLoc, "."); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3126 | if (isSFINAEContext()) |
| 3127 | return ExprError(); |
| 3128 | |
| 3129 | OpKind = tok::period; |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType()) { |
| 3134 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3135 | << ObjectType << Base->getSourceRange(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3136 | return ExprError(); |
| 3137 | } |
| 3138 | |
| 3139 | // C++ [expr.pseudo]p2: |
| 3140 | // [...] The cv-unqualified versions of the object type and of the type |
| 3141 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3142 | if (DestructedTypeInfo) { |
| 3143 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 3144 | SourceLocation DestructedTypeStart |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3145 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3146 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType() && |
| 3147 | !Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
| 3148 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3149 | << ObjectType << DestructedType << Base->getSourceRange() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3150 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3151 | |
| 3152 | // Recover by setting the destructed type to the object type. |
| 3153 | DestructedType = ObjectType; |
| 3154 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 3155 | DestructedTypeStart); |
| 3156 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 3157 | } |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3158 | } |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3159 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3160 | // C++ [expr.pseudo]p2: |
| 3161 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 3162 | // form |
| 3163 | // |
| 3164 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
| 3165 | // |
| 3166 | // shall designate the same scalar type. |
| 3167 | if (ScopeTypeInfo) { |
| 3168 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 3169 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | 81e317a | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 3170 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3171 | |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3172 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3173 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3174 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3175 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3176 | |
| 3177 | ScopeType = QualType(); |
| 3178 | ScopeTypeInfo = 0; |
| 3179 | } |
| 3180 | } |
| 3181 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3182 | Expr *Result |
| 3183 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 3184 | OpKind == tok::arrow, OpLoc, |
| 3185 | SS.getScopeRep(), SS.getRange(), |
| 3186 | ScopeTypeInfo, |
| 3187 | CCLoc, |
| 3188 | TildeLoc, |
| 3189 | Destructed); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3190 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3191 | if (HasTrailingLParen) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3192 | return Owned(Result); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3193 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3194 | return DiagnoseDtorReference(Destructed.getLocation(), Result); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3197 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3198 | SourceLocation OpLoc, |
| 3199 | tok::TokenKind OpKind, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3200 | CXXScopeSpec &SS, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3201 | UnqualifiedId &FirstTypeName, |
| 3202 | SourceLocation CCLoc, |
| 3203 | SourceLocation TildeLoc, |
| 3204 | UnqualifiedId &SecondTypeName, |
| 3205 | bool HasTrailingLParen) { |
| 3206 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 3207 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 3208 | "Invalid first type name in pseudo-destructor"); |
| 3209 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 3210 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 3211 | "Invalid second type name in pseudo-destructor"); |
| 3212 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3213 | // C++ [expr.pseudo]p2: |
| 3214 | // The left-hand side of the dot operator shall be of scalar type. The |
| 3215 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 3216 | // This scalar type is the object type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3217 | QualType ObjectType = Base->getType(); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3218 | if (OpKind == tok::arrow) { |
| 3219 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 3220 | ObjectType = Ptr->getPointeeType(); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3221 | } else if (!ObjectType->isDependentType()) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3222 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 3223 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3224 | << ObjectType << true |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3225 | << FixItHint::CreateReplacement(OpLoc, "."); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3226 | if (isSFINAEContext()) |
| 3227 | return ExprError(); |
| 3228 | |
| 3229 | OpKind = tok::period; |
| 3230 | } |
| 3231 | } |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3232 | |
| 3233 | // Compute the object type that we should use for name lookup purposes. Only |
| 3234 | // record types and dependent types matter. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3235 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3236 | if (!SS.isSet()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3237 | if (const Type *T = ObjectType->getAs<RecordType>()) |
| 3238 | ObjectTypePtrForLookup = ParsedType::make(QualType(T, 0)); |
| 3239 | else if (ObjectType->isDependentType()) |
| 3240 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3241 | } |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3242 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3243 | // Convert the name of the type being destructed (following the ~) into a |
| 3244 | // type (with source-location information). |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3245 | QualType DestructedType; |
| 3246 | TypeSourceInfo *DestructedTypeInfo = 0; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3247 | PseudoDestructorTypeStorage Destructed; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3248 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3249 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
| 3250 | SecondTypeName.StartLocation, |
| 3251 | S, &SS, true, ObjectTypePtrForLookup); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3252 | if (!T && |
| 3253 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 3254 | (!SS.isSet() && ObjectType->isDependentType()))) { |
| 3255 | // The name of the type being destroyed is a dependent name, and we |
| 3256 | // couldn't find anything useful in scope. Just store the identifier and |
| 3257 | // it's location, and we'll perform (qualified) name lookup again at |
| 3258 | // template instantiation time. |
| 3259 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 3260 | SecondTypeName.StartLocation); |
| 3261 | } else if (!T) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3262 | Diag(SecondTypeName.StartLocation, |
| 3263 | diag::err_pseudo_dtor_destructor_non_type) |
| 3264 | << SecondTypeName.Identifier << ObjectType; |
| 3265 | if (isSFINAEContext()) |
| 3266 | return ExprError(); |
| 3267 | |
| 3268 | // Recover by assuming we had the right type all along. |
| 3269 | DestructedType = ObjectType; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3270 | } else |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3271 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3272 | } else { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3273 | // Resolve the template-id to a type. |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3274 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3275 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 3276 | TemplateId->getTemplateArgs(), |
| 3277 | TemplateId->NumArgs); |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 3278 | TypeResult T = ActOnTemplateIdType(TemplateId->Template, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3279 | TemplateId->TemplateNameLoc, |
| 3280 | TemplateId->LAngleLoc, |
| 3281 | TemplateArgsPtr, |
| 3282 | TemplateId->RAngleLoc); |
| 3283 | if (T.isInvalid() || !T.get()) { |
| 3284 | // Recover by assuming we had the right type all along. |
| 3285 | DestructedType = ObjectType; |
| 3286 | } else |
| 3287 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3290 | // If we've performed some kind of recovery, (re-)build the type source |
| 3291 | // information. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3292 | if (!DestructedType.isNull()) { |
| 3293 | if (!DestructedTypeInfo) |
| 3294 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3295 | SecondTypeName.StartLocation); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3296 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 3297 | } |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3298 | |
| 3299 | // Convert the name of the scope type (the type prior to '::') into a type. |
| 3300 | TypeSourceInfo *ScopeTypeInfo = 0; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3301 | QualType ScopeType; |
| 3302 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 3303 | FirstTypeName.Identifier) { |
| 3304 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3305 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
| 3306 | FirstTypeName.StartLocation, |
| 3307 | S, &SS, false, ObjectTypePtrForLookup); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3308 | if (!T) { |
| 3309 | Diag(FirstTypeName.StartLocation, |
| 3310 | diag::err_pseudo_dtor_destructor_non_type) |
| 3311 | << FirstTypeName.Identifier << ObjectType; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3312 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3313 | if (isSFINAEContext()) |
| 3314 | return ExprError(); |
| 3315 | |
| 3316 | // Just drop this type. It's unnecessary anyway. |
| 3317 | ScopeType = QualType(); |
| 3318 | } else |
| 3319 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3320 | } else { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3321 | // Resolve the template-id to a type. |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3322 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3323 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 3324 | TemplateId->getTemplateArgs(), |
| 3325 | TemplateId->NumArgs); |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 3326 | TypeResult T = ActOnTemplateIdType(TemplateId->Template, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3327 | TemplateId->TemplateNameLoc, |
| 3328 | TemplateId->LAngleLoc, |
| 3329 | TemplateArgsPtr, |
| 3330 | TemplateId->RAngleLoc); |
| 3331 | if (T.isInvalid() || !T.get()) { |
| 3332 | // Recover by dropping this type. |
| 3333 | ScopeType = QualType(); |
| 3334 | } else |
| 3335 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3336 | } |
| 3337 | } |
Douglas Gregor | b4a418f | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 3338 | |
| 3339 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 3340 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 3341 | FirstTypeName.StartLocation); |
| 3342 | |
| 3343 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3344 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 3345 | ScopeTypeInfo, CCLoc, TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3346 | Destructed, HasTrailingLParen); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 3349 | CXXMemberCallExpr *Sema::BuildCXXMemberCallExpr(Expr *Exp, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3350 | NamedDecl *FoundDecl, |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 3351 | CXXMethodDecl *Method) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3352 | if (PerformObjectArgumentInitialization(Exp, /*Qualifier=*/0, |
| 3353 | FoundDecl, Method)) |
Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 3354 | assert(0 && "Calling BuildCXXMemberCallExpr with invalid call?"); |
| 3355 | |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 3356 | MemberExpr *ME = |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3357 | new (Context) MemberExpr(Exp, /*IsArrow=*/false, Method, |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 3358 | SourceLocation(), Method->getType()); |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3359 | QualType ResultType = Method->getCallResultType(); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3360 | MarkDeclarationReferenced(Exp->getLocStart(), Method); |
| 3361 | CXXMemberCallExpr *CE = |
| 3362 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, |
| 3363 | Exp->getLocEnd()); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 3364 | return CE; |
| 3365 | } |
| 3366 | |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 3367 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 3368 | SourceLocation RParen) { |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 3369 | return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand, |
| 3370 | Operand->CanThrow(Context), |
| 3371 | KeyLoc, RParen)); |
| 3372 | } |
| 3373 | |
| 3374 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 3375 | Expr *Operand, SourceLocation RParen) { |
| 3376 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 3377 | } |
| 3378 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3379 | ExprResult Sema::ActOnFinishFullExpr(Expr *FullExpr) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3380 | if (!FullExpr) return ExprError(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame^] | 3381 | |
| 3382 | CheckImplicitConversions(FullExpr); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3383 | return MaybeCreateCXXExprWithTemporaries(FullExpr); |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 3384 | } |