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