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