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