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