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 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 15 | #include "SemaInit.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 16 | #include "Lookup.h" |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
| 20 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
| 23 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 27 | Action::TypeTy *Sema::getDestructorName(SourceLocation TildeLoc, |
| 28 | IdentifierInfo &II, |
| 29 | SourceLocation NameLoc, |
| 30 | Scope *S, const CXXScopeSpec &SS, |
| 31 | TypeTy *ObjectTypePtr, |
| 32 | bool EnteringContext) { |
| 33 | // Determine where to perform name lookup. |
| 34 | |
| 35 | // FIXME: This area of the standard is very messy, and the current |
| 36 | // wording is rather unclear about which scopes we search for the |
| 37 | // destructor name; see core issues 399 and 555. Issue 399 in |
| 38 | // particular shows where the current description of destructor name |
| 39 | // lookup is completely out of line with existing practice, e.g., |
| 40 | // this appears to be ill-formed: |
| 41 | // |
| 42 | // namespace N { |
| 43 | // template <typename T> struct S { |
| 44 | // ~S(); |
| 45 | // }; |
| 46 | // } |
| 47 | // |
| 48 | // void f(N::S<int>* s) { |
| 49 | // s->N::S<int>::~S(); |
| 50 | // } |
| 51 | // |
| 52 | // |
| 53 | QualType SearchType; |
| 54 | DeclContext *LookupCtx = 0; |
| 55 | bool isDependent = false; |
| 56 | bool LookInScope = false; |
| 57 | |
| 58 | // If we have an object type, it's because we are in a |
| 59 | // pseudo-destructor-expression or a member access expression, and |
| 60 | // we know what type we're looking for. |
| 61 | if (ObjectTypePtr) |
| 62 | SearchType = GetTypeFromParser(ObjectTypePtr); |
| 63 | |
| 64 | if (SS.isSet()) { |
| 65 | // C++ [basic.lookup.qual]p6: |
| 66 | // If a pseudo-destructor-name (5.2.4) contains a |
| 67 | // nested-name-specifier, the type-names are looked up as types |
| 68 | // in the scope designated by the nested-name-specifier. Similarly, in |
Chandler Carruth | 5e895a8 | 2010-02-21 10:19:54 +0000 | [diff] [blame] | 69 | // a qualified-id of the form: |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 70 | // |
| 71 | // :: [opt] nested-name-specifier[opt] class-name :: ~class-name |
| 72 | // |
| 73 | // the second class-name is looked up in the same scope as the first. |
| 74 | // |
Chandler Carruth | 5e895a8 | 2010-02-21 10:19:54 +0000 | [diff] [blame] | 75 | // FIXME: We don't implement this, because it breaks lots of |
| 76 | // perfectly reasonable code that no other compilers diagnose. The |
| 77 | // issue is that the first class-name is looked up as a |
| 78 | // nested-name-specifier, so we ignore value declarations, but the |
| 79 | // second lookup is presumably an ordinary name lookup. Hence, we |
| 80 | // end up finding values (say, a function) and complain. See PRs |
| 81 | // 6358 and 6359 for examples of such code. DPG to investigate |
| 82 | // further. |
| 83 | if (ObjectTypePtr) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 84 | LookupCtx = computeDeclContext(SearchType); |
| 85 | isDependent = SearchType->isDependentType(); |
| 86 | } else { |
| 87 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Douglas Gregor | b10cd04 | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 88 | if (LookupCtx) |
| 89 | isDependent = LookupCtx->isDependentContext(); |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Douglas Gregor | b10cd04 | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 92 | LookInScope = (LookupCtx == 0) && !isDependent; |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 93 | } else if (ObjectTypePtr) { |
| 94 | // C++ [basic.lookup.classref]p3: |
| 95 | // If the unqualified-id is ~type-name, the type-name is looked up |
| 96 | // in the context of the entire postfix-expression. If the type T |
| 97 | // of the object expression is of a class type C, the type-name is |
| 98 | // also looked up in the scope of class C. At least one of the |
| 99 | // lookups shall find a name that refers to (possibly |
| 100 | // cv-qualified) T. |
| 101 | LookupCtx = computeDeclContext(SearchType); |
| 102 | isDependent = SearchType->isDependentType(); |
| 103 | assert((isDependent || !SearchType->isIncompleteType()) && |
| 104 | "Caller should have completed object type"); |
| 105 | |
| 106 | LookInScope = true; |
| 107 | } else { |
| 108 | // Perform lookup into the current scope (only). |
| 109 | LookInScope = true; |
| 110 | } |
| 111 | |
| 112 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); |
| 113 | for (unsigned Step = 0; Step != 2; ++Step) { |
| 114 | // Look for the name first in the computed lookup context (if we |
| 115 | // have one) and, if that fails to find a match, in the sope (if |
| 116 | // we're allowed to look there). |
| 117 | Found.clear(); |
| 118 | if (Step == 0 && LookupCtx) |
| 119 | LookupQualifiedName(Found, LookupCtx); |
| 120 | else if (Step == 1 && LookInScope) |
| 121 | LookupName(Found, S); |
| 122 | else |
| 123 | continue; |
| 124 | |
| 125 | // FIXME: Should we be suppressing ambiguities here? |
| 126 | if (Found.isAmbiguous()) |
| 127 | return 0; |
| 128 | |
| 129 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { |
| 130 | QualType T = Context.getTypeDeclType(Type); |
| 131 | // If we found the injected-class-name of a class template, retrieve the |
| 132 | // type of that template. |
| 133 | // FIXME: We really shouldn't need to do this. |
| 134 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Type)) |
| 135 | if (Record->isInjectedClassName()) |
| 136 | if (Record->getDescribedClassTemplate()) |
| 137 | T = Record->getDescribedClassTemplate() |
| 138 | ->getInjectedClassNameType(Context); |
| 139 | |
| 140 | if (SearchType.isNull() || SearchType->isDependentType() || |
| 141 | Context.hasSameUnqualifiedType(T, SearchType)) { |
| 142 | // We found our type! |
| 143 | |
| 144 | return T.getAsOpaquePtr(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // If the name that we found is a class template name, and it is |
| 149 | // the same name as the template name in the last part of the |
| 150 | // nested-name-specifier (if present) or the object type, then |
| 151 | // this is the destructor for that class. |
| 152 | // FIXME: This is a workaround until we get real drafting for core |
| 153 | // issue 399, for which there isn't even an obvious direction. |
| 154 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { |
| 155 | QualType MemberOfType; |
| 156 | if (SS.isSet()) { |
| 157 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { |
| 158 | // Figure out the type of the context, if it has one. |
| 159 | if (ClassTemplateSpecializationDecl *Spec |
| 160 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) |
| 161 | MemberOfType = Context.getTypeDeclType(Spec); |
| 162 | else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 163 | if (Record->getDescribedClassTemplate()) |
| 164 | MemberOfType = Record->getDescribedClassTemplate() |
| 165 | ->getInjectedClassNameType(Context); |
| 166 | else |
| 167 | MemberOfType = Context.getTypeDeclType(Record); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | if (MemberOfType.isNull()) |
| 172 | MemberOfType = SearchType; |
| 173 | |
| 174 | if (MemberOfType.isNull()) |
| 175 | continue; |
| 176 | |
| 177 | // We're referring into a class template specialization. If the |
| 178 | // class template we found is the same as the template being |
| 179 | // specialized, we found what we are looking for. |
| 180 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { |
| 181 | if (ClassTemplateSpecializationDecl *Spec |
| 182 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
| 183 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == |
| 184 | Template->getCanonicalDecl()) |
| 185 | return MemberOfType.getAsOpaquePtr(); |
| 186 | } |
| 187 | |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | // We're referring to an unresolved class template |
| 192 | // specialization. Determine whether we class template we found |
| 193 | // is the same as the template being specialized or, if we don't |
| 194 | // know which template is being specialized, that it at least |
| 195 | // has the same name. |
| 196 | if (const TemplateSpecializationType *SpecType |
| 197 | = MemberOfType->getAs<TemplateSpecializationType>()) { |
| 198 | TemplateName SpecName = SpecType->getTemplateName(); |
| 199 | |
| 200 | // The class template we found is the same template being |
| 201 | // specialized. |
| 202 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { |
| 203 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) |
| 204 | return MemberOfType.getAsOpaquePtr(); |
| 205 | |
| 206 | continue; |
| 207 | } |
| 208 | |
| 209 | // The class template we found has the same name as the |
| 210 | // (dependent) template name being specialized. |
| 211 | if (DependentTemplateName *DepTemplate |
| 212 | = SpecName.getAsDependentTemplateName()) { |
| 213 | if (DepTemplate->isIdentifier() && |
| 214 | DepTemplate->getIdentifier() == Template->getIdentifier()) |
| 215 | return MemberOfType.getAsOpaquePtr(); |
| 216 | |
| 217 | continue; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (isDependent) { |
| 224 | // We didn't find our type, but that's okay: it's dependent |
| 225 | // anyway. |
| 226 | NestedNameSpecifier *NNS = 0; |
| 227 | SourceRange Range; |
| 228 | if (SS.isSet()) { |
| 229 | NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 230 | Range = SourceRange(SS.getRange().getBegin(), NameLoc); |
| 231 | } else { |
| 232 | NNS = NestedNameSpecifier::Create(Context, &II); |
| 233 | Range = SourceRange(NameLoc); |
| 234 | } |
| 235 | |
| 236 | return CheckTypenameType(NNS, II, Range).getAsOpaquePtr(); |
| 237 | } |
| 238 | |
| 239 | if (ObjectTypePtr) |
| 240 | Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type) |
| 241 | << &II; |
| 242 | else |
| 243 | Diag(NameLoc, diag::err_destructor_class_name); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 248 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ). |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 249 | Action::OwningExprResult |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 250 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 251 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 252 | if (!StdNamespace) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 253 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 254 | |
Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 255 | if (isType) { |
| 256 | // C++ [expr.typeid]p4: |
| 257 | // The top-level cv-qualifiers of the lvalue expression or the type-id |
| 258 | // that is the operand of typeid are always ignored. |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 259 | // FIXME: Preserve type source info. |
Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 260 | // FIXME: Preserve the type before we stripped the cv-qualifiers? |
Douglas Gregor | 765ccba | 2009-12-23 21:06:06 +0000 | [diff] [blame] | 261 | QualType T = GetTypeFromParser(TyOrExpr); |
| 262 | if (T.isNull()) |
| 263 | return ExprError(); |
| 264 | |
| 265 | // C++ [expr.typeid]p4: |
| 266 | // If the type of the type-id is a class type or a reference to a class |
| 267 | // type, the class shall be completely-defined. |
| 268 | QualType CheckT = T; |
| 269 | if (const ReferenceType *RefType = CheckT->getAs<ReferenceType>()) |
| 270 | CheckT = RefType->getPointeeType(); |
| 271 | |
| 272 | if (CheckT->getAs<RecordType>() && |
| 273 | RequireCompleteType(OpLoc, CheckT, diag::err_incomplete_typeid)) |
| 274 | return ExprError(); |
| 275 | |
| 276 | TyOrExpr = T.getUnqualifiedType().getAsOpaquePtr(); |
Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 277 | } |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 279 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 280 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 281 | LookupQualifiedName(R, StdNamespace); |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 282 | RecordDecl *TypeInfoRecordDecl = R.getAsSingle<RecordDecl>(); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 283 | if (!TypeInfoRecordDecl) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 284 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 285 | |
| 286 | QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); |
| 287 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 288 | if (!isType) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 289 | bool isUnevaluatedOperand = true; |
| 290 | Expr *E = static_cast<Expr *>(TyOrExpr); |
Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 291 | if (E && !E->isTypeDependent()) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 292 | QualType T = E->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 293 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 294 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 295 | // C++ [expr.typeid]p3: |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 296 | // [...] If the type of the expression is a class type, the class |
| 297 | // shall be completely-defined. |
| 298 | if (RequireCompleteType(OpLoc, T, diag::err_incomplete_typeid)) |
| 299 | return ExprError(); |
| 300 | |
| 301 | // C++ [expr.typeid]p3: |
Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 302 | // When typeid is applied to an expression other than an lvalue of a |
| 303 | // polymorphic class type [...] [the] expression is an unevaluated |
| 304 | // operand. [...] |
| 305 | if (RecordD->isPolymorphic() && E->isLvalue(Context) == Expr::LV_Valid) |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 306 | isUnevaluatedOperand = false; |
Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // C++ [expr.typeid]p4: |
| 310 | // [...] If the type of the type-id is a reference to a possibly |
| 311 | // cv-qualified type, the result of the typeid expression refers to a |
| 312 | // std::type_info object representing the cv-unqualified referenced |
| 313 | // type. |
| 314 | if (T.hasQualifiers()) { |
| 315 | ImpCastExprToType(E, T.getUnqualifiedType(), CastExpr::CK_NoOp, |
| 316 | E->isLvalue(Context)); |
| 317 | TyOrExpr = E; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 321 | // If this is an unevaluated operand, clear out the set of |
| 322 | // declaration references we have been computing and eliminate any |
| 323 | // temporaries introduced in its computation. |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 324 | if (isUnevaluatedOperand) |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 325 | ExprEvalContexts.back().Context = Unevaluated; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 326 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 328 | return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr, |
| 329 | TypeInfoType.withConst(), |
| 330 | SourceRange(OpLoc, RParenLoc))); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 333 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 334 | Action::OwningExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 335 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 336 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | "Unknown C++ Boolean value!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 338 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 339 | Context.BoolTy, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 341 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 342 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
| 343 | Action::OwningExprResult |
| 344 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
| 345 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 346 | } |
| 347 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 348 | /// ActOnCXXThrow - Parse throw expressions. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 349 | Action::OwningExprResult |
| 350 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 351 | Expr *Ex = E.takeAs<Expr>(); |
| 352 | if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) |
| 353 | return ExprError(); |
| 354 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); |
| 355 | } |
| 356 | |
| 357 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
| 358 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { |
| 359 | // C++ [except.throw]p3: |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 360 | // A throw-expression initializes a temporary object, called the exception |
| 361 | // object, the type of which is determined by removing any top-level |
| 362 | // cv-qualifiers from the static type of the operand of throw and adjusting |
| 363 | // the type from "array of T" or "function returning T" to "pointer to T" |
| 364 | // or "pointer to function returning T", [...] |
| 365 | if (E->getType().hasQualifiers()) |
| 366 | ImpCastExprToType(E, E->getType().getUnqualifiedType(), CastExpr::CK_NoOp, |
| 367 | E->isLvalue(Context) == Expr::LV_Valid); |
| 368 | |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 369 | DefaultFunctionArrayConversion(E); |
| 370 | |
| 371 | // If the type of the exception would be an incomplete type or a pointer |
| 372 | // to an incomplete type other than (cv) void the program is ill-formed. |
| 373 | QualType Ty = E->getType(); |
| 374 | int isPointer = 0; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 375 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 376 | Ty = Ptr->getPointeeType(); |
| 377 | isPointer = 1; |
| 378 | } |
| 379 | if (!isPointer || !Ty->isVoidType()) { |
| 380 | if (RequireCompleteType(ThrowLoc, Ty, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 381 | PDiag(isPointer ? diag::err_throw_incomplete_ptr |
| 382 | : diag::err_throw_incomplete) |
| 383 | << E->getSourceRange())) |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 384 | return true; |
| 385 | } |
| 386 | |
| 387 | // FIXME: Construct a temporary here. |
| 388 | return false; |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 389 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 390 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 391 | Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 392 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 393 | /// is a non-lvalue expression whose value is the address of the object for |
| 394 | /// which the function is called. |
| 395 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 396 | if (!isa<FunctionDecl>(CurContext)) |
| 397 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 398 | |
| 399 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 400 | if (MD->isInstance()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 401 | return Owned(new (Context) CXXThisExpr(ThisLoc, |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 402 | MD->getThisType(Context), |
| 403 | /*isImplicit=*/false)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 404 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 405 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 406 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 407 | |
| 408 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 409 | /// Can be interpreted either as function-style casting ("int(x)") |
| 410 | /// or class type construction ("ClassType(x,y,z)") |
| 411 | /// or creation of a value-initialized type ("int()"). |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 412 | Action::OwningExprResult |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 413 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 414 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 415 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 416 | SourceLocation *CommaLocs, |
| 417 | SourceLocation RParenLoc) { |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 418 | if (!TypeRep) |
| 419 | return ExprError(); |
| 420 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 421 | TypeSourceInfo *TInfo; |
| 422 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 423 | if (!TInfo) |
| 424 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 425 | unsigned NumExprs = exprs.size(); |
| 426 | Expr **Exprs = (Expr**)exprs.get(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 427 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 428 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 429 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 430 | if (Ty->isDependentType() || |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 431 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 432 | exprs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
| 434 | return Owned(CXXUnresolvedConstructExpr::Create(Context, |
| 435 | TypeRange.getBegin(), Ty, |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 436 | LParenLoc, |
| 437 | Exprs, NumExprs, |
| 438 | RParenLoc)); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 441 | if (Ty->isArrayType()) |
| 442 | return ExprError(Diag(TyBeginLoc, |
| 443 | diag::err_value_init_for_array_type) << FullRange); |
| 444 | if (!Ty->isVoidType() && |
| 445 | RequireCompleteType(TyBeginLoc, Ty, |
| 446 | PDiag(diag::err_invalid_incomplete_type_use) |
| 447 | << FullRange)) |
| 448 | return ExprError(); |
Fariborz Jahanian | f071e9b | 2009-10-23 21:01:39 +0000 | [diff] [blame] | 449 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 450 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 451 | diag::err_allocation_of_abstract_type)) |
| 452 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
| 454 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 455 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 456 | // If the expression list is a single expression, the type conversion |
| 457 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 458 | // corresponding cast expression. |
| 459 | // |
| 460 | if (NumExprs == 1) { |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 461 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 462 | CXXMethodDecl *Method = 0; |
| 463 | if (CheckCastTypes(TypeRange, Ty, Exprs[0], Kind, Method, |
| 464 | /*FunctionalStyle=*/true)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 465 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 466 | |
| 467 | exprs.release(); |
| 468 | if (Method) { |
| 469 | OwningExprResult CastArg |
| 470 | = BuildCXXCastArgument(TypeRange.getBegin(), Ty.getNonReferenceType(), |
| 471 | Kind, Method, Owned(Exprs[0])); |
| 472 | if (CastArg.isInvalid()) |
| 473 | return ExprError(); |
| 474 | |
| 475 | Exprs[0] = CastArg.takeAs<Expr>(); |
Fariborz Jahanian | 4fc7ab3 | 2009-08-28 15:11:24 +0000 | [diff] [blame] | 476 | } |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 477 | |
| 478 | return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 479 | TInfo, TyBeginLoc, Kind, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 480 | Exprs[0], RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 483 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 484 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 485 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | if (NumExprs > 1 || !Record->hasTrivialConstructor() || |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 487 | !Record->hasTrivialDestructor()) { |
Eli Friedman | 6997aae | 2010-01-31 20:58:15 +0000 | [diff] [blame] | 488 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(Ty); |
| 489 | InitializationKind Kind |
| 490 | = NumExprs ? InitializationKind::CreateDirect(TypeRange.getBegin(), |
| 491 | LParenLoc, RParenLoc) |
| 492 | : InitializationKind::CreateValue(TypeRange.getBegin(), |
| 493 | LParenLoc, RParenLoc); |
| 494 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); |
| 495 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 496 | move(exprs)); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 497 | |
Eli Friedman | 6997aae | 2010-01-31 20:58:15 +0000 | [diff] [blame] | 498 | // FIXME: Improve AST representation? |
| 499 | return move(Result); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // Fall through to value-initialize an object of class type that |
| 503 | // doesn't have a user-declared default constructor. |
| 504 | } |
| 505 | |
| 506 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 507 | // If the expression list specifies more than a single value, the type shall |
| 508 | // be a class with a suitably declared constructor. |
| 509 | // |
| 510 | if (NumExprs > 1) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 511 | return ExprError(Diag(CommaLocs[0], |
| 512 | diag::err_builtin_func_cast_more_than_one_arg) |
| 513 | << FullRange); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 514 | |
| 515 | assert(NumExprs == 0 && "Expected 0 expressions"); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 516 | // C++ [expr.type.conv]p2: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 517 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 518 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 519 | // rvalue of the specified type, which is value-initialized. |
| 520 | // |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 521 | exprs.release(); |
| 522 | return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 523 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 524 | |
| 525 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 526 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 527 | /// @code new (memory) int[size][4] @endcode |
| 528 | /// or |
| 529 | /// @code ::new Foo(23, "hello") @endcode |
| 530 | /// For the interpretation of this heap of arguments, consult the base version. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 531 | Action::OwningExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 532 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 533 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 534 | SourceLocation PlacementRParen, bool ParenTypeId, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 535 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 536 | MultiExprArg ConstructorArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | SourceLocation ConstructorRParen) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 538 | Expr *ArraySize = 0; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 539 | // If the specified type is an array, unwrap it and save the expression. |
| 540 | if (D.getNumTypeObjects() > 0 && |
| 541 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 542 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
| 543 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 544 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 545 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 546 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 547 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 548 | << D.getSourceRange()); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 549 | |
| 550 | if (ParenTypeId) { |
| 551 | // Can't have dynamic array size when the type-id is in parentheses. |
| 552 | Expr *NumElts = (Expr *)Chunk.Arr.NumElts; |
| 553 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && |
| 554 | !NumElts->isIntegerConstantExpr(Context)) { |
| 555 | Diag(D.getTypeObject(0).Loc, diag::err_new_paren_array_nonconst) |
| 556 | << NumElts->getSourceRange(); |
| 557 | return ExprError(); |
| 558 | } |
| 559 | } |
| 560 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 561 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 562 | D.DropFirstTypeObject(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 565 | // Every dimension shall be of constant size. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 566 | if (ArraySize) { |
| 567 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 568 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 569 | break; |
| 570 | |
| 571 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 572 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
| 573 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && |
| 574 | !NumElts->isIntegerConstantExpr(Context)) { |
| 575 | Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) |
| 576 | << NumElts->getSourceRange(); |
| 577 | return ExprError(); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 582 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 583 | //FIXME: Store TypeSourceInfo in CXXNew expression. |
| 584 | TypeSourceInfo *TInfo = 0; |
| 585 | QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, &TInfo); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 586 | if (D.isInvalidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 587 | return ExprError(); |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 588 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | return BuildCXXNew(StartLoc, UseGlobal, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 590 | PlacementLParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | move(PlacementArgs), |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 592 | PlacementRParen, |
| 593 | ParenTypeId, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | AllocType, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 595 | D.getSourceRange().getBegin(), |
| 596 | D.getSourceRange(), |
| 597 | Owned(ArraySize), |
| 598 | ConstructorLParen, |
| 599 | move(ConstructorArgs), |
| 600 | ConstructorRParen); |
| 601 | } |
| 602 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | Sema::OwningExprResult |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 604 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 605 | SourceLocation PlacementLParen, |
| 606 | MultiExprArg PlacementArgs, |
| 607 | SourceLocation PlacementRParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | bool ParenTypeId, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 609 | QualType AllocType, |
| 610 | SourceLocation TypeLoc, |
| 611 | SourceRange TypeRange, |
| 612 | ExprArg ArraySizeE, |
| 613 | SourceLocation ConstructorLParen, |
| 614 | MultiExprArg ConstructorArgs, |
| 615 | SourceLocation ConstructorRParen) { |
| 616 | if (CheckAllocatedType(AllocType, TypeLoc, TypeRange)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 617 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 619 | QualType ResultType = Context.getPointerType(AllocType); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 620 | |
| 621 | // That every array dimension except the first is constant was already |
| 622 | // checked by the type check above. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 623 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 624 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 625 | // or enumeration type with a non-negative value." |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 626 | Expr *ArraySize = (Expr *)ArraySizeE.get(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 627 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 628 | QualType SizeType = ArraySize->getType(); |
| 629 | if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 630 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
| 631 | diag::err_array_size_not_integral) |
| 632 | << SizeType << ArraySize->getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 633 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 634 | // We don't care about special rules, so we tell the machinery it's not |
| 635 | // evaluated - it gives us a result in more cases. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 636 | if (!ArraySize->isValueDependent()) { |
| 637 | llvm::APSInt Value; |
| 638 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 639 | if (Value < llvm::APSInt( |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 640 | llvm::APInt::getNullValue(Value.getBitWidth()), |
| 641 | Value.isUnsigned())) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 642 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
| 643 | diag::err_typecheck_negative_array_size) |
| 644 | << ArraySize->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 645 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 646 | } |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 647 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 648 | ImpCastExprToType(ArraySize, Context.getSizeType(), |
| 649 | CastExpr::CK_IntegralCast); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 650 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 651 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 652 | FunctionDecl *OperatorNew = 0; |
| 653 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 654 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 655 | unsigned NumPlaceArgs = PlacementArgs.size(); |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 656 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 657 | if (!AllocType->isDependentType() && |
| 658 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && |
| 659 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 660 | SourceRange(PlacementLParen, PlacementRParen), |
| 661 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 662 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 663 | return ExprError(); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 664 | llvm::SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 665 | if (OperatorNew) { |
| 666 | // Add default arguments, if any. |
| 667 | const FunctionProtoType *Proto = |
| 668 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 669 | VariadicCallType CallType = |
| 670 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 671 | bool Invalid = GatherArgumentsForCall(PlacementLParen, OperatorNew, |
| 672 | Proto, 1, PlaceArgs, NumPlaceArgs, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 673 | AllPlaceArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 674 | if (Invalid) |
| 675 | return ExprError(); |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 676 | |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 677 | NumPlaceArgs = AllPlaceArgs.size(); |
| 678 | if (NumPlaceArgs > 0) |
| 679 | PlaceArgs = &AllPlaceArgs[0]; |
| 680 | } |
| 681 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 682 | bool Init = ConstructorLParen.isValid(); |
| 683 | // --- Choosing a constructor --- |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 684 | CXXConstructorDecl *Constructor = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 685 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); |
| 686 | unsigned NumConsArgs = ConstructorArgs.size(); |
Eli Friedman | a8ce9ec | 2009-11-08 22:15:39 +0000 | [diff] [blame] | 687 | ASTOwningVector<&ActionBase::DeleteExpr> ConvertedConstructorArgs(*this); |
| 688 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 689 | if (!AllocType->isDependentType() && |
| 690 | !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { |
| 691 | // C++0x [expr.new]p15: |
| 692 | // A new-expression that creates an object of type T initializes that |
| 693 | // object as follows: |
| 694 | InitializationKind Kind |
| 695 | // - If the new-initializer is omitted, the object is default- |
| 696 | // initialized (8.5); if no initialization is performed, |
| 697 | // the object has indeterminate value |
| 698 | = !Init? InitializationKind::CreateDefault(TypeLoc) |
| 699 | // - Otherwise, the new-initializer is interpreted according to the |
| 700 | // initialization rules of 8.5 for direct-initialization. |
| 701 | : InitializationKind::CreateDirect(TypeLoc, |
| 702 | ConstructorLParen, |
| 703 | ConstructorRParen); |
| 704 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 705 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 706 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 707 | InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 708 | OwningExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
| 709 | move(ConstructorArgs)); |
| 710 | if (FullInit.isInvalid()) |
| 711 | return ExprError(); |
| 712 | |
| 713 | // FullInit is our initializer; walk through it to determine if it's a |
| 714 | // constructor call, which CXXNewExpr handles directly. |
| 715 | if (Expr *FullInitExpr = (Expr *)FullInit.get()) { |
| 716 | if (CXXBindTemporaryExpr *Binder |
| 717 | = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr)) |
| 718 | FullInitExpr = Binder->getSubExpr(); |
| 719 | if (CXXConstructExpr *Construct |
| 720 | = dyn_cast<CXXConstructExpr>(FullInitExpr)) { |
| 721 | Constructor = Construct->getConstructor(); |
| 722 | for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(), |
| 723 | AEnd = Construct->arg_end(); |
| 724 | A != AEnd; ++A) |
| 725 | ConvertedConstructorArgs.push_back(A->Retain()); |
| 726 | } else { |
| 727 | // Take the converted initializer. |
| 728 | ConvertedConstructorArgs.push_back(FullInit.release()); |
| 729 | } |
| 730 | } else { |
| 731 | // No initialization required. |
| 732 | } |
| 733 | |
| 734 | // Take the converted arguments and use them for the new expression. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 735 | NumConsArgs = ConvertedConstructorArgs.size(); |
| 736 | ConsArgs = (Expr **)ConvertedConstructorArgs.take(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 737 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 738 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 739 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) |
Douglas Gregor | 089407b | 2009-10-17 21:40:42 +0000 | [diff] [blame] | 740 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 741 | PlacementArgs.release(); |
| 742 | ConstructorArgs.release(); |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 743 | ArraySizeE.release(); |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 744 | return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, |
| 745 | PlaceArgs, NumPlaceArgs, ParenTypeId, |
| 746 | ArraySize, Constructor, Init, |
| 747 | ConsArgs, NumConsArgs, OperatorDelete, |
| 748 | ResultType, StartLoc, |
| 749 | Init ? ConstructorRParen : |
| 750 | SourceLocation())); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 754 | /// in a new-expression. |
| 755 | /// dimension off and stores the size expression in ArraySize. |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 756 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | SourceRange R) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 758 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 759 | // abstract class type or array thereof. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 760 | if (AllocType->isFunctionType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 761 | return Diag(Loc, diag::err_bad_new_type) |
| 762 | << AllocType << 0 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 763 | else if (AllocType->isReferenceType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 764 | return Diag(Loc, diag::err_bad_new_type) |
| 765 | << AllocType << 1 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 766 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 767 | RequireCompleteType(Loc, AllocType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 768 | PDiag(diag::err_new_incomplete_type) |
| 769 | << R)) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 770 | return true; |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 771 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 772 | diag::err_allocation_of_abstract_type)) |
| 773 | return true; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 774 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 775 | return false; |
| 776 | } |
| 777 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 778 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 779 | /// that are appropriate for the allocation. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 780 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 781 | bool UseGlobal, QualType AllocType, |
| 782 | bool IsArray, Expr **PlaceArgs, |
| 783 | unsigned NumPlaceArgs, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 784 | FunctionDecl *&OperatorNew, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 786 | // --- Choosing an allocation function --- |
| 787 | // C++ 5.3.4p8 - 14 & 18 |
| 788 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 789 | // in the scope of the allocated class. |
| 790 | // 2) If an array size is given, look for operator new[], else look for |
| 791 | // operator new. |
| 792 | // 3) The first argument is always size_t. Append the arguments from the |
| 793 | // placement form. |
| 794 | // FIXME: Also find the appropriate delete operator. |
| 795 | |
| 796 | llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
| 797 | // We don't care about the actual value of this argument. |
| 798 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 799 | // tree? Or should the consumer just recalculate the value? |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 800 | IntegerLiteral Size(llvm::APInt::getNullValue( |
| 801 | Context.Target.getPointerWidth(0)), |
| 802 | Context.getSizeType(), |
| 803 | SourceLocation()); |
| 804 | AllocArgs[0] = &Size; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 805 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 806 | |
| 807 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 808 | IsArray ? OO_Array_New : OO_New); |
| 809 | if (AllocType->isRecordType() && !UseGlobal) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | CXXRecordDecl *Record |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 811 | = cast<CXXRecordDecl>(AllocType->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 812 | // FIXME: We fail to find inherited overloads. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 813 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 814 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 815 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 816 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 817 | } |
| 818 | if (!OperatorNew) { |
| 819 | // Didn't find a member overload. Look for a global one. |
| 820 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 821 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 822 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 823 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 824 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 825 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 828 | // FindAllocationOverload can change the passed in arguments, so we need to |
| 829 | // copy them back. |
| 830 | if (NumPlaceArgs > 0) |
| 831 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 833 | return false; |
| 834 | } |
| 835 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 836 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 837 | /// function in the specified scope. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 838 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 839 | DeclarationName Name, Expr** Args, |
| 840 | unsigned NumArgs, DeclContext *Ctx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | bool AllowMissing, FunctionDecl *&Operator) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 842 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); |
| 843 | LookupQualifiedName(R, Ctx); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 844 | if (R.empty()) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 845 | if (AllowMissing) |
| 846 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 847 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 848 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 849 | } |
| 850 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 851 | // FIXME: handle ambiguity |
| 852 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 853 | OverloadCandidateSet Candidates(StartLoc); |
Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 854 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 855 | Alloc != AllocEnd; ++Alloc) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 856 | // Even member operator new/delete are implicitly treated as |
| 857 | // static, so don't use AddMemberCandidate. |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 858 | |
| 859 | if (FunctionTemplateDecl *FnTemplate = |
| 860 | dyn_cast<FunctionTemplateDecl>((*Alloc)->getUnderlyingDecl())) { |
| 861 | AddTemplateOverloadCandidate(FnTemplate, Alloc.getAccess(), |
| 862 | /*ExplicitTemplateArgs=*/0, Args, NumArgs, |
| 863 | Candidates, |
| 864 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 865 | continue; |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | FunctionDecl *Fn = cast<FunctionDecl>((*Alloc)->getUnderlyingDecl()); |
| 869 | AddOverloadCandidate(Fn, Alloc.getAccess(), Args, NumArgs, Candidates, |
| 870 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | // Do the resolution. |
| 874 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 875 | switch(BestViableFunction(Candidates, StartLoc, Best)) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 876 | case OR_Success: { |
| 877 | // Got one! |
| 878 | FunctionDecl *FnDecl = Best->Function; |
| 879 | // The first argument is size_t, and the first parameter must be size_t, |
| 880 | // too. This is checked on declaration and can be assumed. (It can't be |
| 881 | // asserted on, though, since invalid decls are left in there.) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 882 | // Whatch out for variadic allocator function. |
| 883 | unsigned NumArgsInFnDecl = FnDecl->getNumParams(); |
| 884 | for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { |
Anders Carlsson | fc27d26 | 2009-05-31 19:49:47 +0000 | [diff] [blame] | 885 | if (PerformCopyInitialization(Args[i], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 886 | FnDecl->getParamDecl(i)->getType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 887 | AA_Passing)) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 888 | return true; |
| 889 | } |
| 890 | Operator = FnDecl; |
| 891 | return false; |
| 892 | } |
| 893 | |
| 894 | case OR_No_Viable_Function: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 895 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 896 | << Name << Range; |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 897 | PrintOverloadCandidates(Candidates, OCD_AllCandidates, Args, NumArgs); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 898 | return true; |
| 899 | |
| 900 | case OR_Ambiguous: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 901 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 902 | << Name << Range; |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 903 | PrintOverloadCandidates(Candidates, OCD_ViableCandidates, Args, NumArgs); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 904 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 905 | |
| 906 | case OR_Deleted: |
| 907 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 908 | << Best->Function->isDeleted() |
| 909 | << Name << Range; |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 910 | PrintOverloadCandidates(Candidates, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 911 | return true; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 912 | } |
| 913 | assert(false && "Unreachable, bad result from BestViableFunction"); |
| 914 | return true; |
| 915 | } |
| 916 | |
| 917 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 918 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 919 | /// delete. These are: |
| 920 | /// @code |
| 921 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 922 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 923 | /// void operator delete(void *) throw(); |
| 924 | /// void operator delete[](void *) throw(); |
| 925 | /// @endcode |
| 926 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 927 | /// declared. Their use requires including \<new\>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 929 | if (GlobalNewDeleteDeclared) |
| 930 | return; |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 931 | |
| 932 | // C++ [basic.std.dynamic]p2: |
| 933 | // [...] The following allocation and deallocation functions (18.4) are |
| 934 | // implicitly declared in global scope in each translation unit of a |
| 935 | // program |
| 936 | // |
| 937 | // void* operator new(std::size_t) throw(std::bad_alloc); |
| 938 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 939 | // void operator delete(void*) throw(); |
| 940 | // void operator delete[](void*) throw(); |
| 941 | // |
| 942 | // These implicit declarations introduce only the function names operator |
| 943 | // new, operator new[], operator delete, operator delete[]. |
| 944 | // |
| 945 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 946 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 947 | // However, we do not make these implicit declarations visible to name |
| 948 | // lookup. |
| 949 | if (!StdNamespace) { |
| 950 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 951 | StdNamespace = NamespaceDecl::Create(Context, |
| 952 | Context.getTranslationUnitDecl(), |
| 953 | SourceLocation(), |
| 954 | &PP.getIdentifierTable().get("std")); |
| 955 | StdNamespace->setImplicit(true); |
| 956 | } |
| 957 | |
| 958 | if (!StdBadAlloc) { |
| 959 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 960 | // implicitly. |
| 961 | StdBadAlloc = CXXRecordDecl::Create(Context, TagDecl::TK_class, |
| 962 | StdNamespace, |
| 963 | SourceLocation(), |
| 964 | &PP.getIdentifierTable().get("bad_alloc"), |
| 965 | SourceLocation(), 0); |
| 966 | StdBadAlloc->setImplicit(true); |
| 967 | } |
| 968 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 969 | GlobalNewDeleteDeclared = true; |
| 970 | |
| 971 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 972 | QualType SizeT = Context.getSizeType(); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 973 | bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 974 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 975 | DeclareGlobalAllocationFunction( |
| 976 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 977 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 978 | DeclareGlobalAllocationFunction( |
| 979 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 980 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 981 | DeclareGlobalAllocationFunction( |
| 982 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 983 | Context.VoidTy, VoidPtr); |
| 984 | DeclareGlobalAllocationFunction( |
| 985 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 986 | Context.VoidTy, VoidPtr); |
| 987 | } |
| 988 | |
| 989 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 990 | /// allocation function if it doesn't already exist. |
| 991 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 992 | QualType Return, QualType Argument, |
| 993 | bool AddMallocAttr) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 994 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 995 | |
| 996 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 997 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 998 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 999 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1000 | Alloc != AllocEnd; ++Alloc) { |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1001 | // Only look at non-template functions, as it is the predefined, |
| 1002 | // non-templated allocation function we are trying to declare here. |
| 1003 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
| 1004 | QualType InitialParamType = |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 1005 | Context.getCanonicalType( |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1006 | Func->getParamDecl(0)->getType().getUnqualifiedType()); |
| 1007 | // FIXME: Do we need to check for default arguments here? |
| 1008 | if (Func->getNumParams() == 1 && InitialParamType == Argument) |
| 1009 | return; |
| 1010 | } |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1014 | QualType BadAllocType; |
| 1015 | bool HasBadAllocExceptionSpec |
| 1016 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 1017 | Name.getCXXOverloadedOperator() == OO_Array_New); |
| 1018 | if (HasBadAllocExceptionSpec) { |
| 1019 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
| 1020 | BadAllocType = Context.getTypeDeclType(StdBadAlloc); |
| 1021 | } |
| 1022 | |
| 1023 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0, |
| 1024 | true, false, |
| 1025 | HasBadAllocExceptionSpec? 1 : 0, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame^] | 1026 | &BadAllocType, false, CC_Default); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1027 | FunctionDecl *Alloc = |
| 1028 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1029 | FnType, /*TInfo=*/0, FunctionDecl::None, false, true); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1030 | Alloc->setImplicit(); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1031 | |
| 1032 | if (AddMallocAttr) |
| 1033 | Alloc->addAttr(::new (Context) MallocAttr()); |
| 1034 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1035 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1036 | 0, Argument, /*TInfo=*/0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1037 | VarDecl::None, 0); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1038 | Alloc->setParams(&Param, 1); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1039 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1040 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 1041 | // make sure it is at the end of the chain to coincide with the |
| 1042 | // global scope. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1043 | ((DeclContext *)TUScope->getEntity())->addDecl(Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1046 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 1047 | DeclarationName Name, |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1048 | FunctionDecl* &Operator) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1049 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1050 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1051 | LookupQualifiedName(Found, RD); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1052 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1053 | if (Found.isAmbiguous()) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1054 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1055 | |
| 1056 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1057 | F != FEnd; ++F) { |
| 1058 | if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F)) |
| 1059 | if (Delete->isUsualDeallocationFunction()) { |
| 1060 | Operator = Delete; |
| 1061 | return false; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | // We did find operator delete/operator delete[] declarations, but |
| 1066 | // none of them were suitable. |
| 1067 | if (!Found.empty()) { |
| 1068 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 1069 | << Name << RD; |
| 1070 | |
| 1071 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1072 | F != FEnd; ++F) { |
| 1073 | Diag((*F)->getLocation(), |
| 1074 | diag::note_delete_member_function_declared_here) |
| 1075 | << Name; |
| 1076 | } |
| 1077 | |
| 1078 | return true; |
| 1079 | } |
| 1080 | |
| 1081 | // Look for a global declaration. |
| 1082 | DeclareGlobalNewDelete(); |
| 1083 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
| 1084 | |
| 1085 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); |
| 1086 | Expr* DeallocArgs[1]; |
| 1087 | DeallocArgs[0] = &Null; |
| 1088 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, |
| 1089 | DeallocArgs, 1, TUDecl, /*AllowMissing=*/false, |
| 1090 | Operator)) |
| 1091 | return true; |
| 1092 | |
| 1093 | assert(Operator && "Did not find a deallocation function!"); |
| 1094 | return false; |
| 1095 | } |
| 1096 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1097 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 1098 | /// @code ::delete ptr; @endcode |
| 1099 | /// or |
| 1100 | /// @code delete [] ptr; @endcode |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1101 | Action::OwningExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1102 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | bool ArrayForm, ExprArg Operand) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1104 | // C++ [expr.delete]p1: |
| 1105 | // The operand shall have a pointer type, or a class type having a single |
| 1106 | // conversion function to a pointer type. The result has type void. |
| 1107 | // |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1108 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 1109 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1110 | FunctionDecl *OperatorDelete = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1112 | Expr *Ex = (Expr *)Operand.get(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1113 | if (!Ex->isTypeDependent()) { |
| 1114 | QualType Type = Ex->getType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1115 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1116 | if (const RecordType *Record = Type->getAs<RecordType>()) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1117 | llvm::SmallVector<CXXConversionDecl *, 4> ObjectPtrConversions; |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1118 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1119 | const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1120 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1121 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1122 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1123 | // Skip over templated conversion functions; they aren't considered. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1124 | if (isa<FunctionTemplateDecl>(*I)) |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1125 | continue; |
| 1126 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1127 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1128 | |
| 1129 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 1130 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 1131 | if (ConvPtrType->getPointeeType()->isObjectType()) |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1132 | ObjectPtrConversions.push_back(Conv); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1133 | } |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1134 | if (ObjectPtrConversions.size() == 1) { |
| 1135 | // We have a single conversion to a pointer-to-object type. Perform |
| 1136 | // that conversion. |
| 1137 | Operand.release(); |
| 1138 | if (!PerformImplicitConversion(Ex, |
| 1139 | ObjectPtrConversions.front()->getConversionType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1140 | AA_Converting)) { |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1141 | Operand = Owned(Ex); |
| 1142 | Type = Ex->getType(); |
| 1143 | } |
| 1144 | } |
| 1145 | else if (ObjectPtrConversions.size() > 1) { |
| 1146 | Diag(StartLoc, diag::err_ambiguous_delete_operand) |
| 1147 | << Type << Ex->getSourceRange(); |
| 1148 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) { |
| 1149 | CXXConversionDecl *Conv = ObjectPtrConversions[i]; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 1150 | NoteOverloadCandidate(Conv); |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1151 | } |
| 1152 | return ExprError(); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1153 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1156 | if (!Type->isPointerType()) |
| 1157 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 1158 | << Type << Ex->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1159 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1160 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1161 | if (Pointee->isFunctionType() || Pointee->isVoidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1162 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 1163 | << Type << Ex->getSourceRange()); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1164 | else if (!Pointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | RequireCompleteType(StartLoc, Pointee, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1166 | PDiag(diag::warn_delete_incomplete) |
| 1167 | << Ex->getSourceRange())) |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1168 | return ExprError(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1169 | |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 1170 | // C++ [expr.delete]p2: |
| 1171 | // [Note: a pointer to a const type can be the operand of a |
| 1172 | // delete-expression; it is not necessary to cast away the constness |
| 1173 | // (5.2.11) of the pointer expression before it is used as the operand |
| 1174 | // of the delete-expression. ] |
| 1175 | ImpCastExprToType(Ex, Context.getPointerType(Context.VoidTy), |
| 1176 | CastExpr::CK_NoOp); |
| 1177 | |
| 1178 | // Update the operand. |
| 1179 | Operand.take(); |
| 1180 | Operand = ExprArg(*this, Ex); |
| 1181 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1182 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1183 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 1184 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1185 | if (const RecordType *RT = Pointee->getAs<RecordType>()) { |
| 1186 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1187 | |
| 1188 | if (!UseGlobal && |
| 1189 | FindDeallocationFunction(StartLoc, RD, DeleteName, OperatorDelete)) |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1190 | return ExprError(); |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1191 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1192 | if (!RD->hasTrivialDestructor()) |
| 1193 | if (const CXXDestructorDecl *Dtor = RD->getDestructor(Context)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | MarkDeclarationReferenced(StartLoc, |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1195 | const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1196 | } |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1197 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1198 | if (!OperatorDelete) { |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1199 | // Look for a global declaration. |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1200 | DeclareGlobalNewDelete(); |
| 1201 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1203 | &Ex, 1, TUDecl, /*AllowMissing=*/false, |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1204 | OperatorDelete)) |
| 1205 | return ExprError(); |
| 1206 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1208 | // FIXME: Check access and ambiguity of operator delete and destructor. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1211 | Operand.release(); |
| 1212 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1213 | OperatorDelete, Ex, StartLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1216 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 1217 | /// while, do-while, or switch statement. |
| 1218 | Action::OwningExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar) { |
| 1219 | QualType T = ConditionVar->getType(); |
| 1220 | |
| 1221 | // C++ [stmt.select]p2: |
| 1222 | // The declarator shall not specify a function or an array. |
| 1223 | if (T->isFunctionType()) |
| 1224 | return ExprError(Diag(ConditionVar->getLocation(), |
| 1225 | diag::err_invalid_use_of_function_type) |
| 1226 | << ConditionVar->getSourceRange()); |
| 1227 | else if (T->isArrayType()) |
| 1228 | return ExprError(Diag(ConditionVar->getLocation(), |
| 1229 | diag::err_invalid_use_of_array_type) |
| 1230 | << ConditionVar->getSourceRange()); |
Douglas Gregor | a7605db | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 1231 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1232 | return Owned(DeclRefExpr::Create(Context, 0, SourceRange(), ConditionVar, |
| 1233 | ConditionVar->getLocation(), |
| 1234 | ConditionVar->getType().getNonReferenceType())); |
| 1235 | } |
| 1236 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1237 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 1238 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 1239 | // C++ 6.4p4: |
| 1240 | // The value of a condition that is an initialized declaration in a statement |
| 1241 | // other than a switch statement is the value of the declared variable |
| 1242 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 1243 | // program is ill-formed. |
| 1244 | // The value of a condition that is an expression is the value of the |
| 1245 | // expression, implicitly converted to bool. |
| 1246 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1247 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1248 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1249 | |
| 1250 | /// Helper function to determine whether this is the (deprecated) C++ |
| 1251 | /// conversion from a string literal to a pointer to non-const char or |
| 1252 | /// non-const wchar_t (for narrow and wide string literals, |
| 1253 | /// respectively). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | bool |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1255 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 1256 | // Look inside the implicit cast, if it exists. |
| 1257 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 1258 | From = Cast->getSubExpr(); |
| 1259 | |
| 1260 | // A string literal (2.13.4) that is not a wide string literal can |
| 1261 | // be converted to an rvalue of type "pointer to char"; a wide |
| 1262 | // string literal can be converted to an rvalue of type "pointer |
| 1263 | // to wchar_t" (C++ 4.2p2). |
| 1264 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1265 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | if (const BuiltinType *ToPointeeType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1267 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1268 | // This conversion is considered only when there is an |
| 1269 | // explicit appropriate pointer target type (C++ 4.2p2). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1270 | if (!ToPtrType->getPointeeType().hasQualifiers() && |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1271 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 1272 | (!StrLit->isWide() && |
| 1273 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 1274 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 1275 | return true; |
| 1276 | } |
| 1277 | |
| 1278 | return false; |
| 1279 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1280 | |
| 1281 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1282 | /// expression From to the type ToType. Returns true if there was an |
| 1283 | /// error, false otherwise. The expression From is replaced with the |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1284 | /// converted expression. Flavor is the kind of conversion we're |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1285 | /// performing, used in the error message. If @p AllowExplicit, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1286 | /// explicit user-defined conversions are permitted. @p Elidable should be true |
| 1287 | /// when called for copies which may be elided (C++ 12.8p15). C++0x overload |
| 1288 | /// resolution works differently in that case. |
| 1289 | bool |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1290 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1291 | AssignmentAction Action, bool AllowExplicit, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1292 | bool Elidable) { |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1293 | ImplicitConversionSequence ICS; |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1294 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 1295 | Elidable, ICS); |
| 1296 | } |
| 1297 | |
| 1298 | bool |
| 1299 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1300 | AssignmentAction Action, bool AllowExplicit, |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 1301 | bool Elidable, |
| 1302 | ImplicitConversionSequence& ICS) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1303 | ICS.setBad(); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 1304 | ICS.Bad.init(BadConversionSequence::no_conversion, From, ToType); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1305 | if (Elidable && getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | ICS = TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1307 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | AllowExplicit, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1309 | /*ForceRValue=*/true, |
| 1310 | /*InOverloadResolution=*/false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1311 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1312 | if (ICS.isBad()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | ICS = TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1314 | /*SuppressUserConversions=*/false, |
| 1315 | AllowExplicit, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1316 | /*ForceRValue=*/false, |
| 1317 | /*InOverloadResolution=*/false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1318 | } |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1319 | return PerformImplicitConversion(From, ToType, ICS, Action); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1323 | /// expression From to the type ToType using the pre-computed implicit |
| 1324 | /// conversion sequence ICS. Returns true if there was an error, false |
| 1325 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1326 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1327 | /// used in the error message. |
| 1328 | bool |
| 1329 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 1330 | const ImplicitConversionSequence &ICS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1331 | AssignmentAction Action, bool IgnoreBaseAccess) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1332 | switch (ICS.getKind()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1333 | case ImplicitConversionSequence::StandardConversion: |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1334 | if (PerformImplicitConversion(From, ToType, ICS.Standard, Action, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1335 | IgnoreBaseAccess)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1336 | return true; |
| 1337 | break; |
| 1338 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1339 | case ImplicitConversionSequence::UserDefinedConversion: { |
| 1340 | |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 1341 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
| 1342 | CastExpr::CastKind CastKind = CastExpr::CK_Unknown; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1343 | QualType BeforeToType; |
| 1344 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 1345 | CastKind = CastExpr::CK_UserDefinedConversion; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1346 | |
| 1347 | // If the user-defined conversion is specified by a conversion function, |
| 1348 | // the initial standard conversion sequence converts the source type to |
| 1349 | // the implicit object parameter of the conversion function. |
| 1350 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
| 1351 | } else if (const CXXConstructorDecl *Ctor = |
| 1352 | dyn_cast<CXXConstructorDecl>(FD)) { |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1353 | CastKind = CastExpr::CK_ConstructorConversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1354 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 1355 | if (!ICS.UserDefined.EllipsisConversion) { |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1356 | // If the user-defined conversion is specified by a constructor, the |
| 1357 | // initial standard conversion sequence converts the source type to the |
| 1358 | // type required by the argument of the constructor |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 1359 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 1360 | } |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1361 | } |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1362 | else |
| 1363 | assert(0 && "Unknown conversion function kind!"); |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1364 | // Whatch out for elipsis conversion. |
Fariborz Jahanian | 4c0cea2 | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 1365 | if (!ICS.UserDefined.EllipsisConversion) { |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1366 | if (PerformImplicitConversion(From, BeforeToType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1367 | ICS.UserDefined.Before, AA_Converting, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1368 | IgnoreBaseAccess)) |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1369 | return true; |
| 1370 | } |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1371 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1372 | OwningExprResult CastArg |
| 1373 | = BuildCXXCastArgument(From->getLocStart(), |
| 1374 | ToType.getNonReferenceType(), |
| 1375 | CastKind, cast<CXXMethodDecl>(FD), |
| 1376 | Owned(From)); |
| 1377 | |
| 1378 | if (CastArg.isInvalid()) |
| 1379 | return true; |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1380 | |
| 1381 | From = CastArg.takeAs<Expr>(); |
| 1382 | |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1383 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1384 | AA_Converting, IgnoreBaseAccess); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1385 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1386 | |
| 1387 | case ImplicitConversionSequence::AmbiguousConversion: |
| 1388 | DiagnoseAmbiguousConversion(ICS, From->getExprLoc(), |
| 1389 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 1390 | << From->getSourceRange()); |
| 1391 | return true; |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1392 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1393 | case ImplicitConversionSequence::EllipsisConversion: |
| 1394 | assert(false && "Cannot perform an ellipsis conversion"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1395 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1396 | |
| 1397 | case ImplicitConversionSequence::BadConversion: |
| 1398 | return true; |
| 1399 | } |
| 1400 | |
| 1401 | // Everything went well. |
| 1402 | return false; |
| 1403 | } |
| 1404 | |
| 1405 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1406 | /// expression From to the type ToType by following the standard |
| 1407 | /// conversion sequence SCS. Returns true if there was an error, false |
| 1408 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1409 | /// expression. Flavor is the context in which we're performing this |
| 1410 | /// conversion, for use in error messages. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | bool |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1412 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1413 | const StandardConversionSequence& SCS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1414 | AssignmentAction Action, bool IgnoreBaseAccess) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1415 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 1416 | // much extra work. What this means is that we need to keep track of more |
| 1417 | // information that is computed when we try the implicit conversion initially, |
| 1418 | // so that we don't need to recompute anything here. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1419 | QualType FromType = From->getType(); |
| 1420 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1421 | if (SCS.CopyConstructor) { |
Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 1422 | // FIXME: When can ToType be a reference type? |
| 1423 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1424 | if (SCS.Second == ICK_Derived_To_Base) { |
| 1425 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1426 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
| 1427 | MultiExprArg(*this, (void **)&From, 1), |
| 1428 | /*FIXME:ConstructLoc*/SourceLocation(), |
| 1429 | ConstructorArgs)) |
| 1430 | return true; |
| 1431 | OwningExprResult FromResult = |
| 1432 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 1433 | ToType, SCS.CopyConstructor, |
| 1434 | move_arg(ConstructorArgs)); |
| 1435 | if (FromResult.isInvalid()) |
| 1436 | return true; |
| 1437 | From = FromResult.takeAs<Expr>(); |
| 1438 | return false; |
| 1439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1440 | OwningExprResult FromResult = |
| 1441 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 1442 | ToType, SCS.CopyConstructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 1443 | MultiExprArg(*this, (void**)&From, 1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1445 | if (FromResult.isInvalid()) |
| 1446 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1448 | From = FromResult.takeAs<Expr>(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1452 | // Perform the first implicit conversion. |
| 1453 | switch (SCS.First) { |
| 1454 | case ICK_Identity: |
| 1455 | case ICK_Lvalue_To_Rvalue: |
| 1456 | // Nothing to do. |
| 1457 | break; |
| 1458 | |
| 1459 | case ICK_Array_To_Pointer: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1460 | FromType = Context.getArrayDecayedType(FromType); |
Anders Carlsson | 8249576 | 2009-08-08 21:04:35 +0000 | [diff] [blame] | 1461 | ImpCastExprToType(From, FromType, CastExpr::CK_ArrayToPointerDecay); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1462 | break; |
| 1463 | |
| 1464 | case ICK_Function_To_Pointer: |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1465 | if (Context.getCanonicalType(FromType) == Context.OverloadTy) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1466 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); |
| 1467 | if (!Fn) |
| 1468 | return true; |
| 1469 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1470 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) |
| 1471 | return true; |
| 1472 | |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 1473 | From = FixOverloadedFunctionReference(From, Fn); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1474 | FromType = From->getType(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 1475 | |
Sebastian Redl | 759986e | 2009-10-17 20:50:27 +0000 | [diff] [blame] | 1476 | // If there's already an address-of operator in the expression, we have |
| 1477 | // the right type already, and the code below would just introduce an |
| 1478 | // invalid additional pointer level. |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 1479 | if (FromType->isPointerType() || FromType->isMemberFunctionPointerType()) |
Sebastian Redl | 759986e | 2009-10-17 20:50:27 +0000 | [diff] [blame] | 1480 | break; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1481 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1482 | FromType = Context.getPointerType(FromType); |
Anders Carlsson | b633c4e | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 1483 | ImpCastExprToType(From, FromType, CastExpr::CK_FunctionToPointerDecay); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1484 | break; |
| 1485 | |
| 1486 | default: |
| 1487 | assert(false && "Improper first standard conversion"); |
| 1488 | break; |
| 1489 | } |
| 1490 | |
| 1491 | // Perform the second implicit conversion |
| 1492 | switch (SCS.Second) { |
| 1493 | case ICK_Identity: |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1494 | // If both sides are functions (or pointers/references to them), there could |
| 1495 | // be incompatible exception declarations. |
| 1496 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1497 | return true; |
| 1498 | // Nothing else to do. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1499 | break; |
| 1500 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1501 | case ICK_NoReturn_Adjustment: |
| 1502 | // If both sides are functions (or pointers/references to them), there could |
| 1503 | // be incompatible exception declarations. |
| 1504 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1505 | return true; |
| 1506 | |
| 1507 | ImpCastExprToType(From, Context.getNoReturnType(From->getType(), false), |
| 1508 | CastExpr::CK_NoOp); |
| 1509 | break; |
| 1510 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1511 | case ICK_Integral_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1512 | case ICK_Integral_Conversion: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1513 | ImpCastExprToType(From, ToType, CastExpr::CK_IntegralCast); |
| 1514 | break; |
| 1515 | |
| 1516 | case ICK_Floating_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1517 | case ICK_Floating_Conversion: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1518 | ImpCastExprToType(From, ToType, CastExpr::CK_FloatingCast); |
| 1519 | break; |
| 1520 | |
| 1521 | case ICK_Complex_Promotion: |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1522 | case ICK_Complex_Conversion: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1523 | ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); |
| 1524 | break; |
| 1525 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1526 | case ICK_Floating_Integral: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1527 | if (ToType->isFloatingType()) |
| 1528 | ImpCastExprToType(From, ToType, CastExpr::CK_IntegralToFloating); |
| 1529 | else |
| 1530 | ImpCastExprToType(From, ToType, CastExpr::CK_FloatingToIntegral); |
| 1531 | break; |
| 1532 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1533 | case ICK_Complex_Real: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1534 | ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); |
| 1535 | break; |
| 1536 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1537 | case ICK_Compatible_Conversion: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1538 | ImpCastExprToType(From, ToType, CastExpr::CK_NoOp); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1539 | break; |
| 1540 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1541 | case ICK_Pointer_Conversion: { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1542 | if (SCS.IncompatibleObjC) { |
| 1543 | // Diagnose incompatible Objective-C conversions |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | Diag(From->getSourceRange().getBegin(), |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1545 | diag::ext_typecheck_convert_incompatible_pointer) |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1546 | << From->getType() << ToType << Action |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1547 | << From->getSourceRange(); |
| 1548 | } |
| 1549 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1550 | |
| 1551 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1552 | if (CheckPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1553 | return true; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1554 | ImpCastExprToType(From, ToType, Kind); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1555 | break; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | case ICK_Pointer_Member: { |
| 1559 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1560 | if (CheckMemberPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1561 | return true; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1562 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1563 | return true; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1564 | ImpCastExprToType(From, ToType, Kind); |
| 1565 | break; |
| 1566 | } |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 1567 | case ICK_Boolean_Conversion: { |
| 1568 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
| 1569 | if (FromType->isMemberPointerType()) |
| 1570 | Kind = CastExpr::CK_MemberPointerToBoolean; |
| 1571 | |
| 1572 | ImpCastExprToType(From, Context.BoolTy, Kind); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1573 | break; |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 1574 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1575 | |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1576 | case ICK_Derived_To_Base: |
| 1577 | if (CheckDerivedToBaseConversion(From->getType(), |
| 1578 | ToType.getNonReferenceType(), |
| 1579 | From->getLocStart(), |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1580 | From->getSourceRange(), |
| 1581 | IgnoreBaseAccess)) |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1582 | return true; |
| 1583 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 1584 | CastExpr::CK_DerivedToBase); |
| 1585 | break; |
| 1586 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1587 | default: |
| 1588 | assert(false && "Improper second standard conversion"); |
| 1589 | break; |
| 1590 | } |
| 1591 | |
| 1592 | switch (SCS.Third) { |
| 1593 | case ICK_Identity: |
| 1594 | // Nothing to do. |
| 1595 | break; |
| 1596 | |
| 1597 | case ICK_Qualification: |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1598 | // FIXME: Not sure about lvalue vs rvalue here in the presence of rvalue |
| 1599 | // references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1601 | CastExpr::CK_NoOp, |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1602 | ToType->isLValueReferenceType()); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1603 | break; |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1604 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1605 | default: |
| 1606 | assert(false && "Improper second standard conversion"); |
| 1607 | break; |
| 1608 | } |
| 1609 | |
| 1610 | return false; |
| 1611 | } |
| 1612 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1613 | Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, |
| 1614 | SourceLocation KWLoc, |
| 1615 | SourceLocation LParen, |
| 1616 | TypeTy *Ty, |
| 1617 | SourceLocation RParen) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1618 | QualType T = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1620 | // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
| 1621 | // all traits except __is_class, __is_enum and __is_union require a the type |
| 1622 | // to be complete. |
| 1623 | if (OTT != UTT_IsClass && OTT != UTT_IsEnum && OTT != UTT_IsUnion) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | if (RequireCompleteType(KWLoc, T, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 1625 | diag::err_incomplete_type_used_in_type_trait_expr)) |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1626 | return ExprError(); |
| 1627 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1628 | |
| 1629 | // There is no point in eagerly computing the value. The traits are designed |
| 1630 | // to be used from type trait templates, so Ty will be a template parameter |
| 1631 | // 99% of the time. |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1632 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, T, |
| 1633 | RParen, Context.BoolTy)); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1634 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1635 | |
| 1636 | QualType Sema::CheckPointerToMemberOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1638 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 1639 | // C++ 5.5p2 |
| 1640 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 1641 | // be of type "pointer to member of T" (where T is a completely-defined |
| 1642 | // class type) [...] |
| 1643 | QualType RType = rex->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1644 | const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1645 | if (!MemPtr) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1646 | Diag(Loc, diag::err_bad_memptr_rhs) |
| 1647 | << OpSpelling << RType << rex->getSourceRange(); |
| 1648 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1650 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1651 | QualType Class(MemPtr->getClass(), 0); |
| 1652 | |
| 1653 | // C++ 5.5p2 |
| 1654 | // [...] to its first operand, which shall be of class T or of a class of |
| 1655 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 1656 | // such a class] |
| 1657 | QualType LType = lex->getType(); |
| 1658 | if (isIndirect) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1659 | if (const PointerType *Ptr = LType->getAs<PointerType>()) |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1660 | LType = Ptr->getPointeeType().getNonReferenceType(); |
| 1661 | else { |
| 1662 | Diag(Loc, diag::err_bad_memptr_lhs) |
Fariborz Jahanian | ef78ac6 | 2009-10-26 20:45:27 +0000 | [diff] [blame] | 1663 | << OpSpelling << 1 << LType |
| 1664 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1665 | return QualType(); |
| 1666 | } |
| 1667 | } |
| 1668 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1669 | if (!Context.hasSameUnqualifiedType(Class, LType)) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1670 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1671 | /*DetectVirtual=*/false); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1672 | // FIXME: Would it be useful to print full ambiguity paths, or is that |
| 1673 | // overkill? |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1674 | if (!IsDerivedFrom(LType, Class, Paths) || |
| 1675 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
| 1676 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 1677 | << (int)isIndirect << lex->getType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1678 | return QualType(); |
| 1679 | } |
Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 1680 | // Cast LHS to type of use. |
| 1681 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; |
| 1682 | bool isLValue = !isIndirect && lex->isLvalue(Context) == Expr::LV_Valid; |
| 1683 | ImpCastExprToType(lex, UseType, CastExpr::CK_DerivedToBase, isLValue); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Fariborz Jahanian | 19d7073 | 2009-11-18 22:16:17 +0000 | [diff] [blame] | 1686 | if (isa<CXXZeroInitValueExpr>(rex->IgnoreParens())) { |
Fariborz Jahanian | 05ebda9 | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 1687 | // Diagnose use of pointer-to-member type which when used as |
| 1688 | // the functional cast in a pointer-to-member expression. |
| 1689 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 1690 | return QualType(); |
| 1691 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1692 | // C++ 5.5p2 |
| 1693 | // The result is an object or a function of the type specified by the |
| 1694 | // second operand. |
| 1695 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 1696 | // in accordance with 5.5p5 and 5.2.5. |
| 1697 | // FIXME: This returns a dereferenced member function pointer as a normal |
| 1698 | // function type. However, the only operation valid on such functions is |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1699 | // calling them. There's also a GCC extension to get a function pointer to the |
| 1700 | // thing, which is another complication, because this type - unlike the type |
| 1701 | // 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] | 1702 | // argument. |
| 1703 | // We probably need a "MemberFunctionClosureType" or something like that. |
| 1704 | QualType Result = MemPtr->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1705 | Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers()); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1706 | return Result; |
| 1707 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1708 | |
| 1709 | /// \brief Get the target type of a standard or user-defined conversion. |
| 1710 | static QualType TargetType(const ImplicitConversionSequence &ICS) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1711 | switch (ICS.getKind()) { |
| 1712 | case ImplicitConversionSequence::StandardConversion: |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1713 | return ICS.Standard.getToType(2); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1714 | case ImplicitConversionSequence::UserDefinedConversion: |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1715 | return ICS.UserDefined.After.getToType(2); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1716 | case ImplicitConversionSequence::AmbiguousConversion: |
| 1717 | return ICS.Ambiguous.getToType(); |
| 1718 | case ImplicitConversionSequence::EllipsisConversion: |
| 1719 | case ImplicitConversionSequence::BadConversion: |
| 1720 | llvm_unreachable("function not valid for ellipsis or bad conversions"); |
| 1721 | } |
| 1722 | return QualType(); // silence warnings |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /// \brief Try to convert a type to another according to C++0x 5.16p3. |
| 1726 | /// |
| 1727 | /// This is part of the parameter validation for the ? operator. If either |
| 1728 | /// value operand is a class type, the two operands are attempted to be |
| 1729 | /// converted to each other. This function does the conversion in one direction. |
| 1730 | /// It emits a diagnostic and returns true only if it finds an ambiguous |
| 1731 | /// conversion. |
| 1732 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 1733 | SourceLocation QuestionLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | ImplicitConversionSequence &ICS) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1735 | // C++0x 5.16p3 |
| 1736 | // The process for determining whether an operand expression E1 of type T1 |
| 1737 | // can be converted to match an operand expression E2 of type T2 is defined |
| 1738 | // as follows: |
| 1739 | // -- If E2 is an lvalue: |
| 1740 | if (To->isLvalue(Self.Context) == Expr::LV_Valid) { |
| 1741 | // E1 can be converted to match E2 if E1 can be implicitly converted to |
| 1742 | // type "lvalue reference to T2", subject to the constraint that in the |
| 1743 | // conversion the reference must bind directly to E1. |
| 1744 | if (!Self.CheckReferenceInit(From, |
| 1745 | Self.Context.getLValueReferenceType(To->getType()), |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1746 | To->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1747 | /*SuppressUserConversions=*/false, |
| 1748 | /*AllowExplicit=*/false, |
| 1749 | /*ForceRValue=*/false, |
| 1750 | &ICS)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1751 | { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1752 | assert((ICS.isStandard() || ICS.isUserDefined()) && |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1753 | "expected a definite conversion"); |
| 1754 | bool DirectBinding = |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1755 | ICS.isStandard() ? ICS.Standard.DirectBinding |
| 1756 | : ICS.UserDefined.After.DirectBinding; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1757 | if (DirectBinding) |
| 1758 | return false; |
| 1759 | } |
| 1760 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1761 | ICS.setBad(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1762 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 1763 | // -- if E1 and E2 have class type, and the underlying class types are |
| 1764 | // the same or one is a base class of the other: |
| 1765 | QualType FTy = From->getType(); |
| 1766 | QualType TTy = To->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1767 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 1768 | const RecordType *TRec = TTy->getAs<RecordType>(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1769 | bool FDerivedFromT = FRec && TRec && Self.IsDerivedFrom(FTy, TTy); |
| 1770 | if (FRec && TRec && (FRec == TRec || |
| 1771 | FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { |
| 1772 | // E1 can be converted to match E2 if the class of T2 is the |
| 1773 | // same type as, or a base class of, the class of T1, and |
| 1774 | // [cv2 > cv1]. |
| 1775 | if ((FRec == TRec || FDerivedFromT) && TTy.isAtLeastAsQualifiedAs(FTy)) { |
| 1776 | // Could still fail if there's no copy constructor. |
| 1777 | // FIXME: Is this a hard error then, or just a conversion failure? The |
| 1778 | // standard doesn't say. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | ICS = Self.TryCopyInitialization(From, TTy, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 1780 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 1781 | /*ForceRValue=*/false, |
| 1782 | /*InOverloadResolution=*/false); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } else { |
| 1785 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 1786 | // implicitly converted to the type that expression E2 would have |
| 1787 | // if E2 were converted to an rvalue. |
| 1788 | // First find the decayed type. |
| 1789 | if (TTy->isFunctionType()) |
| 1790 | TTy = Self.Context.getPointerType(TTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | else if (TTy->isArrayType()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1792 | TTy = Self.Context.getArrayDecayedType(TTy); |
| 1793 | |
| 1794 | // Now try the implicit conversion. |
| 1795 | // FIXME: This doesn't detect ambiguities. |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1796 | ICS = Self.TryImplicitConversion(From, TTy, |
| 1797 | /*SuppressUserConversions=*/false, |
| 1798 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1799 | /*ForceRValue=*/false, |
| 1800 | /*InOverloadResolution=*/false); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1801 | } |
| 1802 | return false; |
| 1803 | } |
| 1804 | |
| 1805 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 1806 | /// |
| 1807 | /// This is part of the parameter validation for the ? operator. If either |
| 1808 | /// value operand is a class type, overload resolution is used to find a |
| 1809 | /// conversion to a common type. |
| 1810 | static bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS, |
| 1811 | SourceLocation Loc) { |
| 1812 | Expr *Args[2] = { LHS, RHS }; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1813 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 1814 | Self.AddBuiltinOperatorCandidates(OO_Conditional, Loc, Args, 2, CandidateSet); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1815 | |
| 1816 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1817 | switch (Self.BestViableFunction(CandidateSet, Loc, Best)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1818 | case OR_Success: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1819 | // We found a match. Perform the conversions on the arguments and move on. |
| 1820 | if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1821 | Best->Conversions[0], Sema::AA_Converting) || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1822 | Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1823 | Best->Conversions[1], Sema::AA_Converting)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1824 | break; |
| 1825 | return false; |
| 1826 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1827 | case OR_No_Viable_Function: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1828 | Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 1829 | << LHS->getType() << RHS->getType() |
| 1830 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1831 | return true; |
| 1832 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1833 | case OR_Ambiguous: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1834 | Self.Diag(Loc, diag::err_conditional_ambiguous_ovl) |
| 1835 | << LHS->getType() << RHS->getType() |
| 1836 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1837 | // FIXME: Print the possible common types by printing the return types of |
| 1838 | // the viable candidates. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1839 | break; |
| 1840 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1841 | case OR_Deleted: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1842 | assert(false && "Conditional operator has only built-in overloads"); |
| 1843 | break; |
| 1844 | } |
| 1845 | return true; |
| 1846 | } |
| 1847 | |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1848 | /// \brief Perform an "extended" implicit conversion as returned by |
| 1849 | /// TryClassUnification. |
| 1850 | /// |
| 1851 | /// TryClassUnification generates ICSs that include reference bindings. |
| 1852 | /// PerformImplicitConversion is not suitable for this; it chokes if the |
| 1853 | /// second part of a standard conversion is ICK_DerivedToBase. This function |
| 1854 | /// handles the reference binding specially. |
| 1855 | static bool ConvertForConditional(Sema &Self, Expr *&E, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | const ImplicitConversionSequence &ICS) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1857 | if (ICS.isStandard() && ICS.Standard.ReferenceBinding) { |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1858 | assert(ICS.Standard.DirectBinding && |
| 1859 | "TryClassUnification should never generate indirect ref bindings"); |
Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 1860 | // FIXME: CheckReferenceInit should be able to reuse the ICS instead of |
| 1861 | // redoing all the work. |
| 1862 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1863 | TargetType(ICS)), |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1864 | /*FIXME:*/E->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1865 | /*SuppressUserConversions=*/false, |
| 1866 | /*AllowExplicit=*/false, |
| 1867 | /*ForceRValue=*/false); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1868 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1869 | if (ICS.isUserDefined() && ICS.UserDefined.After.ReferenceBinding) { |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1870 | assert(ICS.UserDefined.After.DirectBinding && |
| 1871 | "TryClassUnification should never generate indirect ref bindings"); |
Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 1872 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1873 | TargetType(ICS)), |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1874 | /*FIXME:*/E->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1875 | /*SuppressUserConversions=*/false, |
| 1876 | /*AllowExplicit=*/false, |
| 1877 | /*ForceRValue=*/false); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1878 | } |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1879 | if (Self.PerformImplicitConversion(E, TargetType(ICS), ICS, Sema::AA_Converting)) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1880 | return true; |
| 1881 | return false; |
| 1882 | } |
| 1883 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1884 | /// \brief Check the operands of ?: under C++ semantics. |
| 1885 | /// |
| 1886 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 1887 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
| 1888 | QualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 1889 | SourceLocation QuestionLoc) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1890 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 1891 | // interface pointers. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1892 | |
| 1893 | // C++0x 5.16p1 |
| 1894 | // The first expression is contextually converted to bool. |
| 1895 | if (!Cond->isTypeDependent()) { |
| 1896 | if (CheckCXXBooleanCondition(Cond)) |
| 1897 | return QualType(); |
| 1898 | } |
| 1899 | |
| 1900 | // Either of the arguments dependent? |
| 1901 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) |
| 1902 | return Context.DependentTy; |
| 1903 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 1904 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); |
| 1905 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1906 | // C++0x 5.16p2 |
| 1907 | // If either the second or the third operand has type (cv) void, ... |
| 1908 | QualType LTy = LHS->getType(); |
| 1909 | QualType RTy = RHS->getType(); |
| 1910 | bool LVoid = LTy->isVoidType(); |
| 1911 | bool RVoid = RTy->isVoidType(); |
| 1912 | if (LVoid || RVoid) { |
| 1913 | // ... then the [l2r] conversions are performed on the second and third |
| 1914 | // operands ... |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 1915 | DefaultFunctionArrayLvalueConversion(LHS); |
| 1916 | DefaultFunctionArrayLvalueConversion(RHS); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1917 | LTy = LHS->getType(); |
| 1918 | RTy = RHS->getType(); |
| 1919 | |
| 1920 | // ... and one of the following shall hold: |
| 1921 | // -- The second or the third operand (but not both) is a throw- |
| 1922 | // expression; the result is of the type of the other and is an rvalue. |
| 1923 | bool LThrow = isa<CXXThrowExpr>(LHS); |
| 1924 | bool RThrow = isa<CXXThrowExpr>(RHS); |
| 1925 | if (LThrow && !RThrow) |
| 1926 | return RTy; |
| 1927 | if (RThrow && !LThrow) |
| 1928 | return LTy; |
| 1929 | |
| 1930 | // -- Both the second and third operands have type void; the result is of |
| 1931 | // type void and is an rvalue. |
| 1932 | if (LVoid && RVoid) |
| 1933 | return Context.VoidTy; |
| 1934 | |
| 1935 | // Neither holds, error. |
| 1936 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 1937 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
| 1938 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1939 | return QualType(); |
| 1940 | } |
| 1941 | |
| 1942 | // Neither is void. |
| 1943 | |
| 1944 | // C++0x 5.16p3 |
| 1945 | // Otherwise, if the second and third operand have different types, and |
| 1946 | // either has (cv) class type, and attempt is made to convert each of those |
| 1947 | // operands to the other. |
| 1948 | if (Context.getCanonicalType(LTy) != Context.getCanonicalType(RTy) && |
| 1949 | (LTy->isRecordType() || RTy->isRecordType())) { |
| 1950 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; |
| 1951 | // These return true if a single direction is already ambiguous. |
| 1952 | if (TryClassUnification(*this, LHS, RHS, QuestionLoc, ICSLeftToRight)) |
| 1953 | return QualType(); |
| 1954 | if (TryClassUnification(*this, RHS, LHS, QuestionLoc, ICSRightToLeft)) |
| 1955 | return QualType(); |
| 1956 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1957 | bool HaveL2R = !ICSLeftToRight.isBad(); |
| 1958 | bool HaveR2L = !ICSRightToLeft.isBad(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1959 | // If both can be converted, [...] the program is ill-formed. |
| 1960 | if (HaveL2R && HaveR2L) { |
| 1961 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
| 1962 | << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1963 | return QualType(); |
| 1964 | } |
| 1965 | |
| 1966 | // If exactly one conversion is possible, that conversion is applied to |
| 1967 | // the chosen operand and the converted operands are used in place of the |
| 1968 | // original operands for the remainder of this section. |
| 1969 | if (HaveL2R) { |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1970 | if (ConvertForConditional(*this, LHS, ICSLeftToRight)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1971 | return QualType(); |
| 1972 | LTy = LHS->getType(); |
| 1973 | } else if (HaveR2L) { |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1974 | if (ConvertForConditional(*this, RHS, ICSRightToLeft)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1975 | return QualType(); |
| 1976 | RTy = RHS->getType(); |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | // C++0x 5.16p4 |
| 1981 | // If the second and third operands are lvalues and have the same type, |
| 1982 | // the result is of that type [...] |
| 1983 | bool Same = Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy); |
| 1984 | if (Same && LHS->isLvalue(Context) == Expr::LV_Valid && |
| 1985 | RHS->isLvalue(Context) == Expr::LV_Valid) |
| 1986 | return LTy; |
| 1987 | |
| 1988 | // C++0x 5.16p5 |
| 1989 | // Otherwise, the result is an rvalue. If the second and third operands |
| 1990 | // do not have the same type, and either has (cv) class type, ... |
| 1991 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 1992 | // ... overload resolution is used to determine the conversions (if any) |
| 1993 | // to be applied to the operands. If the overload resolution fails, the |
| 1994 | // program is ill-formed. |
| 1995 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 1996 | return QualType(); |
| 1997 | } |
| 1998 | |
| 1999 | // C++0x 5.16p6 |
| 2000 | // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 2001 | // conversions are performed on the second and third operands. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2002 | DefaultFunctionArrayLvalueConversion(LHS); |
| 2003 | DefaultFunctionArrayLvalueConversion(RHS); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2004 | LTy = LHS->getType(); |
| 2005 | RTy = RHS->getType(); |
| 2006 | |
| 2007 | // After those conversions, one of the following shall hold: |
| 2008 | // -- The second and third operands have the same type; the result |
| 2009 | // is of that type. |
| 2010 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) |
| 2011 | return LTy; |
| 2012 | |
| 2013 | // -- The second and third operands have arithmetic or enumeration type; |
| 2014 | // the usual arithmetic conversions are performed to bring them to a |
| 2015 | // common type, and the result is of that type. |
| 2016 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
| 2017 | UsualArithmeticConversions(LHS, RHS); |
| 2018 | return LHS->getType(); |
| 2019 | } |
| 2020 | |
| 2021 | // -- The second and third operands have pointer type, or one has pointer |
| 2022 | // type and the other is a null pointer constant; pointer conversions |
| 2023 | // and qualification conversions are performed to bring them to their |
| 2024 | // composite pointer type. The result is of the composite pointer type. |
Eli Friedman | de8ac49 | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 2025 | // -- The second and third operands have pointer to member type, or one has |
| 2026 | // pointer to member type and the other is a null pointer constant; |
| 2027 | // pointer to member conversions and qualification conversions are |
| 2028 | // performed to bring them to a common type, whose cv-qualification |
| 2029 | // shall match the cv-qualification of either the second or the third |
| 2030 | // operand. The result is of the common type. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2031 | QualType Composite = FindCompositePointerType(LHS, RHS); |
| 2032 | if (!Composite.isNull()) |
| 2033 | return Composite; |
Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 2034 | |
| 2035 | // Similarly, attempt to find composite type of twp objective-c pointers. |
| 2036 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 2037 | if (!Composite.isNull()) |
| 2038 | return Composite; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2039 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2040 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 2041 | << LHS->getType() << RHS->getType() |
| 2042 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2043 | return QualType(); |
| 2044 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2045 | |
| 2046 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 2047 | /// |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2048 | /// This finds the composite pointer type (or member pointer type) for @p E1 |
| 2049 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this |
| 2050 | /// type and returns it. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2051 | /// It does not emit diagnostics. |
| 2052 | QualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2) { |
| 2053 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); |
| 2054 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | |
Fariborz Jahanian | 0cedfbd | 2009-12-08 20:04:24 +0000 | [diff] [blame] | 2056 | if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && |
| 2057 | !T2->isAnyPointerType() && !T2->isMemberPointerType()) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2058 | return QualType(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2059 | |
| 2060 | // C++0x 5.9p2 |
| 2061 | // Pointer conversions and qualification conversions are performed on |
| 2062 | // pointer operands to bring them to their composite pointer type. If |
| 2063 | // one operand is a null pointer constant, the composite pointer type is |
| 2064 | // the type of the other operand. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2065 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2066 | if (T2->isMemberPointerType()) |
| 2067 | ImpCastExprToType(E1, T2, CastExpr::CK_NullToMemberPointer); |
| 2068 | else |
| 2069 | ImpCastExprToType(E1, T2, CastExpr::CK_IntegralToPointer); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2070 | return T2; |
| 2071 | } |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2072 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2073 | if (T1->isMemberPointerType()) |
| 2074 | ImpCastExprToType(E2, T1, CastExpr::CK_NullToMemberPointer); |
| 2075 | else |
| 2076 | ImpCastExprToType(E2, T1, CastExpr::CK_IntegralToPointer); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2077 | return T1; |
| 2078 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2080 | // Now both have to be pointers or member pointers. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2081 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || |
| 2082 | (!T2->isPointerType() && !T2->isMemberPointerType())) |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2083 | return QualType(); |
| 2084 | |
| 2085 | // Otherwise, of one of the operands has type "pointer to cv1 void," then |
| 2086 | // the other has type "pointer to cv2 T" and the composite pointer type is |
| 2087 | // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. |
| 2088 | // Otherwise, the composite pointer type is a pointer type similar to the |
| 2089 | // type of one of the operands, with a cv-qualification signature that is |
| 2090 | // the union of the cv-qualification signatures of the operand types. |
| 2091 | // In practice, the first part here is redundant; it's subsumed by the second. |
| 2092 | // What we do here is, we build the two possible composite types, and try the |
| 2093 | // conversions in both directions. If only one works, or if the two composite |
| 2094 | // types are the same, we have succeeded. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2095 | // FIXME: extended qualifiers? |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2096 | typedef llvm::SmallVector<unsigned, 4> QualifierVector; |
| 2097 | QualifierVector QualifierUnion; |
| 2098 | typedef llvm::SmallVector<std::pair<const Type *, const Type *>, 4> |
| 2099 | ContainingClassVector; |
| 2100 | ContainingClassVector MemberOfClass; |
| 2101 | QualType Composite1 = Context.getCanonicalType(T1), |
| 2102 | Composite2 = Context.getCanonicalType(T2); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2103 | do { |
| 2104 | const PointerType *Ptr1, *Ptr2; |
| 2105 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 2106 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 2107 | Composite1 = Ptr1->getPointeeType(); |
| 2108 | Composite2 = Ptr2->getPointeeType(); |
| 2109 | QualifierUnion.push_back( |
| 2110 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 2111 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); |
| 2112 | continue; |
| 2113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2115 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 2116 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 2117 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 2118 | Composite1 = MemPtr1->getPointeeType(); |
| 2119 | Composite2 = MemPtr2->getPointeeType(); |
| 2120 | QualifierUnion.push_back( |
| 2121 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 2122 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 2123 | MemPtr2->getClass())); |
| 2124 | continue; |
| 2125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2127 | // FIXME: block pointer types? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2129 | // Cannot unwrap any more types. |
| 2130 | break; |
| 2131 | } while (true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2133 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2134 | ContainingClassVector::reverse_iterator MOC |
| 2135 | = MemberOfClass.rbegin(); |
| 2136 | for (QualifierVector::reverse_iterator |
| 2137 | I = QualifierUnion.rbegin(), |
| 2138 | E = QualifierUnion.rend(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2139 | I != E; (void)++I, ++MOC) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2140 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2141 | if (MOC->first && MOC->second) { |
| 2142 | // Rebuild member pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2143 | Composite1 = Context.getMemberPointerType( |
| 2144 | Context.getQualifiedType(Composite1, Quals), |
| 2145 | MOC->first); |
| 2146 | Composite2 = Context.getMemberPointerType( |
| 2147 | Context.getQualifiedType(Composite2, Quals), |
| 2148 | MOC->second); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2149 | } else { |
| 2150 | // Rebuild pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2151 | Composite1 |
| 2152 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 2153 | Composite2 |
| 2154 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2155 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | ImplicitConversionSequence E1ToC1 = |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2159 | TryImplicitConversion(E1, Composite1, |
| 2160 | /*SuppressUserConversions=*/false, |
| 2161 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2162 | /*ForceRValue=*/false, |
| 2163 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | ImplicitConversionSequence E2ToC1 = |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2165 | TryImplicitConversion(E2, Composite1, |
| 2166 | /*SuppressUserConversions=*/false, |
| 2167 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2168 | /*ForceRValue=*/false, |
| 2169 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2171 | ImplicitConversionSequence E1ToC2, E2ToC2; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2172 | E1ToC2.setBad(); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2173 | E2ToC2.setBad(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2174 | if (Context.getCanonicalType(Composite1) != |
| 2175 | Context.getCanonicalType(Composite2)) { |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2176 | E1ToC2 = TryImplicitConversion(E1, Composite2, |
| 2177 | /*SuppressUserConversions=*/false, |
| 2178 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2179 | /*ForceRValue=*/false, |
| 2180 | /*InOverloadResolution=*/false); |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2181 | E2ToC2 = TryImplicitConversion(E2, Composite2, |
| 2182 | /*SuppressUserConversions=*/false, |
| 2183 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2184 | /*ForceRValue=*/false, |
| 2185 | /*InOverloadResolution=*/false); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2188 | bool ToC1Viable = !E1ToC1.isBad() && !E2ToC1.isBad(); |
| 2189 | bool ToC2Viable = !E1ToC2.isBad() && !E2ToC2.isBad(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2190 | if (ToC1Viable && !ToC2Viable) { |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2191 | if (!PerformImplicitConversion(E1, Composite1, E1ToC1, Sema::AA_Converting) && |
| 2192 | !PerformImplicitConversion(E2, Composite1, E2ToC1, Sema::AA_Converting)) |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2193 | return Composite1; |
| 2194 | } |
| 2195 | if (ToC2Viable && !ToC1Viable) { |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2196 | if (!PerformImplicitConversion(E1, Composite2, E1ToC2, Sema::AA_Converting) && |
| 2197 | !PerformImplicitConversion(E2, Composite2, E2ToC2, Sema::AA_Converting)) |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2198 | return Composite2; |
| 2199 | } |
| 2200 | return QualType(); |
| 2201 | } |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2202 | |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2203 | Sema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Anders Carlsson | 089c260 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 2204 | if (!Context.getLangOptions().CPlusPlus) |
| 2205 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | |
Douglas Gregor | 5132655 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 2207 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 2208 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2209 | const RecordType *RT = E->getType()->getAs<RecordType>(); |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2210 | if (!RT) |
| 2211 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2213 | // If this is the result of a call expression, our source might |
| 2214 | // actually be a reference, in which case we shouldn't bind. |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 2215 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| 2216 | QualType Ty = CE->getCallee()->getType(); |
| 2217 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
| 2218 | Ty = PT->getPointeeType(); |
Fariborz Jahanian | b372b0f | 2010-02-18 20:31:02 +0000 | [diff] [blame] | 2219 | else if (const BlockPointerType *BPT = Ty->getAs<BlockPointerType>()) |
| 2220 | Ty = BPT->getPointeeType(); |
| 2221 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2222 | const FunctionType *FTy = Ty->getAs<FunctionType>(); |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 2223 | if (FTy->getResultType()->isReferenceType()) |
| 2224 | return Owned(E); |
| 2225 | } |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2226 | |
| 2227 | // That should be enough to guarantee that this type is complete. |
| 2228 | // If it has a trivial destructor, we can avoid the extra copy. |
| 2229 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2230 | if (RD->hasTrivialDestructor()) |
| 2231 | return Owned(E); |
| 2232 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | CXXTemporary *Temp = CXXTemporary::Create(Context, |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2234 | RD->getDestructor(Context)); |
Anders Carlsson | 860306e | 2009-05-30 21:21:49 +0000 | [diff] [blame] | 2235 | ExprTemporaries.push_back(Temp); |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2236 | if (CXXDestructorDecl *Destructor = |
| 2237 | const_cast<CXXDestructorDecl*>(RD->getDestructor(Context))) |
| 2238 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2239 | // FIXME: Add the temporary to the temporaries vector. |
| 2240 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); |
| 2241 | } |
| 2242 | |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 2243 | Expr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr) { |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2244 | assert(SubExpr && "sub expression can't be null!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2245 | |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 2246 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; |
| 2247 | assert(ExprTemporaries.size() >= FirstTemporary); |
| 2248 | if (ExprTemporaries.size() == FirstTemporary) |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2249 | return SubExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2250 | |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2251 | Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr, |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 2252 | &ExprTemporaries[FirstTemporary], |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 2253 | ExprTemporaries.size() - FirstTemporary); |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 2254 | ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, |
| 2255 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2257 | return E; |
| 2258 | } |
| 2259 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 2260 | Sema::OwningExprResult |
| 2261 | Sema::MaybeCreateCXXExprWithTemporaries(OwningExprResult SubExpr) { |
| 2262 | if (SubExpr.isInvalid()) |
| 2263 | return ExprError(); |
| 2264 | |
| 2265 | return Owned(MaybeCreateCXXExprWithTemporaries(SubExpr.takeAs<Expr>())); |
| 2266 | } |
| 2267 | |
Anders Carlsson | 5ee56e9 | 2009-12-16 02:09:40 +0000 | [diff] [blame] | 2268 | FullExpr Sema::CreateFullExpr(Expr *SubExpr) { |
| 2269 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; |
| 2270 | assert(ExprTemporaries.size() >= FirstTemporary); |
| 2271 | |
| 2272 | unsigned NumTemporaries = ExprTemporaries.size() - FirstTemporary; |
| 2273 | CXXTemporary **Temporaries = |
| 2274 | NumTemporaries == 0 ? 0 : &ExprTemporaries[FirstTemporary]; |
| 2275 | |
| 2276 | FullExpr E = FullExpr::Create(Context, SubExpr, Temporaries, NumTemporaries); |
| 2277 | |
| 2278 | ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, |
| 2279 | ExprTemporaries.end()); |
| 2280 | |
| 2281 | return E; |
| 2282 | } |
| 2283 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | Sema::OwningExprResult |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2285 | Sema::ActOnStartCXXMemberReference(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 2286 | tok::TokenKind OpKind, TypeTy *&ObjectType) { |
| 2287 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 2288 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2290 | Expr *BaseExpr = (Expr*)Base.get(); |
| 2291 | assert(BaseExpr && "no record expansion"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2293 | QualType BaseType = BaseExpr->getType(); |
| 2294 | if (BaseType->isDependentType()) { |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 2295 | // If we have a pointer to a dependent type and are using the -> operator, |
| 2296 | // the object type is the type that the pointer points to. We might still |
| 2297 | // have enough information about that type to do something useful. |
| 2298 | if (OpKind == tok::arrow) |
| 2299 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 2300 | BaseType = Ptr->getPointeeType(); |
| 2301 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2302 | ObjectType = BaseType.getAsOpaquePtr(); |
| 2303 | return move(Base); |
| 2304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2305 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2306 | // C++ [over.match.oper]p8: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2308 | // returned, with the original second operand. |
| 2309 | if (OpKind == tok::arrow) { |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 2310 | // The set of types we've considered so far. |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 2311 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 2312 | llvm::SmallVector<SourceLocation, 8> Locations; |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 2313 | CTypes.insert(Context.getCanonicalType(BaseType)); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 2314 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2315 | while (BaseType->isRecordType()) { |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 2316 | Base = BuildOverloadedArrowExpr(S, move(Base), OpLoc); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2317 | BaseExpr = (Expr*)Base.get(); |
| 2318 | if (BaseExpr == NULL) |
| 2319 | return ExprError(); |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 2320 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(BaseExpr)) |
Anders Carlsson | de699e5 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 2321 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 2322 | BaseType = BaseExpr->getType(); |
| 2323 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 2324 | if (!CTypes.insert(CBaseType)) { |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 2325 | Diag(OpLoc, diag::err_operator_arrow_circular); |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 2326 | for (unsigned i = 0; i < Locations.size(); i++) |
| 2327 | Diag(Locations[i], diag::note_declared_at); |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 2328 | return ExprError(); |
| 2329 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2330 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2331 | |
Douglas Gregor | 31658df | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 2332 | if (BaseType->isPointerType()) |
| 2333 | BaseType = BaseType->getPointeeType(); |
| 2334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | |
| 2336 | // 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] | 2337 | // vector types or Objective-C interfaces. Just return early and let |
| 2338 | // ActOnMemberReferenceExpr do the work. |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2339 | if (!BaseType->isRecordType()) { |
| 2340 | // C++ [basic.lookup.classref]p2: |
| 2341 | // [...] If the type of the object expression is of pointer to scalar |
| 2342 | // type, the unqualified-id is looked up in the context of the complete |
| 2343 | // postfix-expression. |
| 2344 | ObjectType = 0; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2345 | return move(Base); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2346 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2347 | |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 2348 | // The object type must be complete (or dependent). |
| 2349 | if (!BaseType->isDependentType() && |
| 2350 | RequireCompleteType(OpLoc, BaseType, |
| 2351 | PDiag(diag::err_incomplete_member_access))) |
| 2352 | return ExprError(); |
| 2353 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2354 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | // 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] | 2356 | // 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] | 2357 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 2358 | // up in the scope of class C. [...] |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2359 | ObjectType = BaseType.getAsOpaquePtr(); |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 2360 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2361 | return move(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2364 | CXXMemberCallExpr *Sema::BuildCXXMemberCallExpr(Expr *Exp, |
| 2365 | CXXMethodDecl *Method) { |
Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 2366 | if (PerformObjectArgumentInitialization(Exp, Method)) |
| 2367 | assert(0 && "Calling BuildCXXMemberCallExpr with invalid call?"); |
| 2368 | |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2369 | MemberExpr *ME = |
| 2370 | new (Context) MemberExpr(Exp, /*IsArrow=*/false, Method, |
| 2371 | SourceLocation(), Method->getType()); |
Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 2372 | QualType ResultType = Method->getResultType().getNonReferenceType(); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2373 | MarkDeclarationReferenced(Exp->getLocStart(), Method); |
| 2374 | CXXMemberCallExpr *CE = |
| 2375 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, |
| 2376 | Exp->getLocEnd()); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2377 | return CE; |
| 2378 | } |
| 2379 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2380 | Sema::OwningExprResult Sema::BuildCXXCastArgument(SourceLocation CastLoc, |
| 2381 | QualType Ty, |
| 2382 | CastExpr::CastKind Kind, |
| 2383 | CXXMethodDecl *Method, |
| 2384 | ExprArg Arg) { |
| 2385 | Expr *From = Arg.takeAs<Expr>(); |
| 2386 | |
| 2387 | switch (Kind) { |
| 2388 | default: assert(0 && "Unhandled cast kind!"); |
| 2389 | case CastExpr::CK_ConstructorConversion: { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 2390 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 2391 | |
| 2392 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(Method), |
| 2393 | MultiExprArg(*this, (void **)&From, 1), |
| 2394 | CastLoc, ConstructorArgs)) |
| 2395 | return ExprError(); |
Anders Carlsson | 4fa2684 | 2009-10-18 21:20:14 +0000 | [diff] [blame] | 2396 | |
| 2397 | OwningExprResult Result = |
| 2398 | BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), |
| 2399 | move_arg(ConstructorArgs)); |
| 2400 | if (Result.isInvalid()) |
| 2401 | return ExprError(); |
| 2402 | |
| 2403 | return MaybeBindToTemporary(Result.takeAs<Expr>()); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | case CastExpr::CK_UserDefinedConversion: { |
Anders Carlsson | aac6e3a | 2009-09-15 07:42:44 +0000 | [diff] [blame] | 2407 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 2408 | |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2409 | // Create an implicit call expr that calls it. |
| 2410 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(From, Method); |
Anders Carlsson | 4fa2684 | 2009-10-18 21:20:14 +0000 | [diff] [blame] | 2411 | return MaybeBindToTemporary(CE); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2412 | } |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2416 | Sema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) { |
| 2417 | Expr *FullExpr = Arg.takeAs<Expr>(); |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2418 | if (FullExpr) |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 2419 | FullExpr = MaybeCreateCXXExprWithTemporaries(FullExpr); |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 2420 | |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2421 | return Owned(FullExpr); |
| 2422 | } |