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" |
| 16 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" |
Argyrios Kyrtzidis | 4021a84 | 2008-10-06 23:16:35 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 24 | /// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 25 | /// name (e.g., operator void const *) as an expression. This is |
| 26 | /// very similar to ActOnIdentifierExpr, except that instead of |
| 27 | /// providing an identifier the parser provides the type of the |
| 28 | /// conversion function. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 29 | Sema::OwningExprResult |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 30 | Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc, |
| 31 | TypeTy *Ty, bool HasTrailingLParen, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 32 | const CXXScopeSpec &SS, |
| 33 | bool isAddressOfOperand) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 34 | QualType ConvType = QualType::getFromOpaquePtr(Ty); |
| 35 | QualType ConvTypeCanon = Context.getCanonicalType(ConvType); |
| 36 | DeclarationName ConvName |
| 37 | = Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 38 | return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 39 | &SS, isAddressOfOperand); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 40 | } |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 42 | /// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 43 | /// name (e.g., @c operator+ ) as an expression. This is very |
| 44 | /// similar to ActOnIdentifierExpr, except that instead of providing |
| 45 | /// an identifier the parser provides the kind of overloaded |
| 46 | /// operator that was parsed. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 47 | Sema::OwningExprResult |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 48 | Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc, |
| 49 | OverloadedOperatorKind Op, |
| 50 | bool HasTrailingLParen, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 51 | const CXXScopeSpec &SS, |
| 52 | bool isAddressOfOperand) { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 53 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 54 | return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 55 | isAddressOfOperand); |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 58 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ). |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 59 | Action::OwningExprResult |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 60 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 61 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 62 | NamespaceDecl *StdNs = GetStdNamespace(); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 63 | if (!StdNs) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 64 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 65 | |
| 66 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 67 | Decl *TypeInfoDecl = LookupQualifiedName(StdNs, TypeInfoII, LookupTagName); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 68 | RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 69 | if (!TypeInfoRecordDecl) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 70 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 71 | |
| 72 | QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); |
| 73 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 74 | return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr, |
| 75 | TypeInfoType.withConst(), |
| 76 | SourceRange(OpLoc, RParenLoc))); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 79 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 80 | Action::OwningExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 81 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 82 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | "Unknown C++ Boolean value!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 84 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 85 | Context.BoolTy, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 87 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame^] | 88 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
| 89 | Action::OwningExprResult |
| 90 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
| 91 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 92 | } |
| 93 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 94 | /// ActOnCXXThrow - Parse throw expressions. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 95 | Action::OwningExprResult |
| 96 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 97 | Expr *Ex = E.takeAs<Expr>(); |
| 98 | if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) |
| 99 | return ExprError(); |
| 100 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); |
| 101 | } |
| 102 | |
| 103 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
| 104 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { |
| 105 | // C++ [except.throw]p3: |
| 106 | // [...] adjusting the type from "array of T" or "function returning T" |
| 107 | // to "pointer to T" or "pointer to function returning T", [...] |
| 108 | DefaultFunctionArrayConversion(E); |
| 109 | |
| 110 | // If the type of the exception would be an incomplete type or a pointer |
| 111 | // to an incomplete type other than (cv) void the program is ill-formed. |
| 112 | QualType Ty = E->getType(); |
| 113 | int isPointer = 0; |
| 114 | if (const PointerType* Ptr = Ty->getAsPointerType()) { |
| 115 | Ty = Ptr->getPointeeType(); |
| 116 | isPointer = 1; |
| 117 | } |
| 118 | if (!isPointer || !Ty->isVoidType()) { |
| 119 | if (RequireCompleteType(ThrowLoc, Ty, |
| 120 | isPointer ? diag::err_throw_incomplete_ptr |
| 121 | : diag::err_throw_incomplete, |
| 122 | E->getSourceRange(), SourceRange(), QualType())) |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | // FIXME: Construct a temporary here. |
| 127 | return false; |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 128 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 129 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 130 | Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 131 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 132 | /// is a non-lvalue expression whose value is the address of the object for |
| 133 | /// which the function is called. |
| 134 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 135 | if (!isa<FunctionDecl>(CurContext)) |
| 136 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 137 | |
| 138 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 139 | if (MD->isInstance()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 140 | return Owned(new (Context) CXXThisExpr(ThisLoc, |
| 141 | MD->getThisType(Context))); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 142 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 143 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 144 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 145 | |
| 146 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 147 | /// Can be interpreted either as function-style casting ("int(x)") |
| 148 | /// or class type construction ("ClassType(x,y,z)") |
| 149 | /// or creation of a value-initialized type ("int()"). |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 150 | Action::OwningExprResult |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 151 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 152 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 153 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 154 | SourceLocation *CommaLocs, |
| 155 | SourceLocation RParenLoc) { |
| 156 | assert(TypeRep && "Missing type!"); |
| 157 | QualType Ty = QualType::getFromOpaquePtr(TypeRep); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 158 | unsigned NumExprs = exprs.size(); |
| 159 | Expr **Exprs = (Expr**)exprs.get(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 160 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 161 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 162 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 163 | if (Ty->isDependentType() || |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 164 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 165 | exprs.release(); |
Anders Carlsson | 26de549 | 2009-04-24 05:23:13 +0000 | [diff] [blame] | 166 | |
| 167 | // FIXME: Is this correct? |
| 168 | CXXTempVarDecl *Temp = CXXTempVarDecl::Create(Context, CurContext, Ty); |
Anders Carlsson | 09c4abb | 2009-04-24 05:44:25 +0000 | [diff] [blame] | 169 | return Owned(new (Context) CXXTemporaryObjectExpr(Context, Temp, 0, Ty, |
| 170 | TyBeginLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 171 | Exprs, NumExprs, |
| 172 | RParenLoc)); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 176 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 177 | // If the expression list is a single expression, the type conversion |
| 178 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 179 | // corresponding cast expression. |
| 180 | // |
| 181 | if (NumExprs == 1) { |
| 182 | if (CheckCastTypes(TypeRange, Ty, Exprs[0])) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 183 | return ExprError(); |
| 184 | exprs.release(); |
| 185 | return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), |
| 186 | Ty, TyBeginLoc, Exprs[0], |
| 187 | RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 190 | if (const RecordType *RT = Ty->getAsRecordType()) { |
| 191 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 193 | if (NumExprs > 1 || Record->hasUserDeclaredConstructor()) { |
| 194 | CXXConstructorDecl *Constructor |
| 195 | = PerformInitializationByConstructor(Ty, Exprs, NumExprs, |
| 196 | TypeRange.getBegin(), |
| 197 | SourceRange(TypeRange.getBegin(), |
| 198 | RParenLoc), |
| 199 | DeclarationName(), |
| 200 | IK_Direct); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 201 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 202 | if (!Constructor) |
| 203 | return ExprError(); |
| 204 | |
Anders Carlsson | 26de549 | 2009-04-24 05:23:13 +0000 | [diff] [blame] | 205 | CXXTempVarDecl *Temp = CXXTempVarDecl::Create(Context, CurContext, Ty); |
| 206 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 207 | exprs.release(); |
Anders Carlsson | 09c4abb | 2009-04-24 05:44:25 +0000 | [diff] [blame] | 208 | return Owned(new (Context) CXXTemporaryObjectExpr(Context, Temp, |
| 209 | Constructor, Ty, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 210 | TyBeginLoc, Exprs, |
| 211 | NumExprs, RParenLoc)); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // Fall through to value-initialize an object of class type that |
| 215 | // doesn't have a user-declared default constructor. |
| 216 | } |
| 217 | |
| 218 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 219 | // If the expression list specifies more than a single value, the type shall |
| 220 | // be a class with a suitably declared constructor. |
| 221 | // |
| 222 | if (NumExprs > 1) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 223 | return ExprError(Diag(CommaLocs[0], |
| 224 | diag::err_builtin_func_cast_more_than_one_arg) |
| 225 | << FullRange); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 226 | |
| 227 | assert(NumExprs == 0 && "Expected 0 expressions"); |
| 228 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 229 | // C++ [expr.type.conv]p2: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 230 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 231 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 232 | // rvalue of the specified type, which is value-initialized. |
| 233 | // |
| 234 | if (Ty->isArrayType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 235 | return ExprError(Diag(TyBeginLoc, |
| 236 | diag::err_value_init_for_array_type) << FullRange); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 237 | if (!Ty->isDependentType() && !Ty->isVoidType() && |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 238 | RequireCompleteType(TyBeginLoc, Ty, |
| 239 | diag::err_invalid_incomplete_type_use, FullRange)) |
| 240 | return ExprError(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 241 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 242 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 243 | diag::err_allocation_of_abstract_type)) |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 244 | return ExprError(); |
| 245 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 246 | exprs.release(); |
| 247 | return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 248 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 249 | |
| 250 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 251 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 252 | /// @code new (memory) int[size][4] @endcode |
| 253 | /// or |
| 254 | /// @code ::new Foo(23, "hello") @endcode |
| 255 | /// For the interpretation of this heap of arguments, consult the base version. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 256 | Action::OwningExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 257 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 258 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 259 | SourceLocation PlacementRParen, bool ParenTypeId, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 260 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 261 | MultiExprArg ConstructorArgs, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 262 | SourceLocation ConstructorRParen) |
| 263 | { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 264 | Expr *ArraySize = 0; |
| 265 | unsigned Skip = 0; |
| 266 | // If the specified type is an array, unwrap it and save the expression. |
| 267 | if (D.getNumTypeObjects() > 0 && |
| 268 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 269 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
| 270 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 271 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 272 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 273 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 274 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 275 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 276 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
| 277 | Skip = 1; |
| 278 | } |
| 279 | |
| 280 | QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, Skip); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 281 | if (D.isInvalidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 282 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 283 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 284 | if (CheckAllocatedType(AllocType, D)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 285 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 286 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 287 | QualType ResultType = AllocType->isDependentType() |
| 288 | ? Context.DependentTy |
| 289 | : Context.getPointerType(AllocType); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 290 | |
| 291 | // That every array dimension except the first is constant was already |
| 292 | // checked by the type check above. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 293 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 294 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 295 | // or enumeration type with a non-negative value." |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 296 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 297 | QualType SizeType = ArraySize->getType(); |
| 298 | if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 299 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
| 300 | diag::err_array_size_not_integral) |
| 301 | << SizeType << ArraySize->getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 302 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 303 | // We don't care about special rules, so we tell the machinery it's not |
| 304 | // evaluated - it gives us a result in more cases. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 305 | if (!ArraySize->isValueDependent()) { |
| 306 | llvm::APSInt Value; |
| 307 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 308 | if (Value < llvm::APSInt( |
| 309 | llvm::APInt::getNullValue(Value.getBitWidth()), false)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 310 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
| 311 | diag::err_typecheck_negative_array_size) |
| 312 | << ArraySize->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 313 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 316 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 317 | FunctionDecl *OperatorNew = 0; |
| 318 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 319 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 320 | unsigned NumPlaceArgs = PlacementArgs.size(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 321 | if (!AllocType->isDependentType() && |
| 322 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && |
| 323 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 324 | SourceRange(PlacementLParen, PlacementRParen), |
| 325 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 326 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 327 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 328 | |
| 329 | bool Init = ConstructorLParen.isValid(); |
| 330 | // --- Choosing a constructor --- |
| 331 | // C++ 5.3.4p15 |
| 332 | // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid) |
| 333 | // the object is not initialized. If the object, or any part of it, is |
| 334 | // const-qualified, it's an error. |
| 335 | // 2) If T is a POD and there's an empty initializer, the object is value- |
| 336 | // initialized. |
| 337 | // 3) If T is a POD and there's one initializer argument, the object is copy- |
| 338 | // constructed. |
| 339 | // 4) If T is a POD and there's more initializer arguments, it's an error. |
| 340 | // 5) If T is not a POD, the initializer arguments are used as constructor |
| 341 | // arguments. |
| 342 | // |
| 343 | // Or by the C++0x formulation: |
| 344 | // 1) If there's no initializer, the object is default-initialized according |
| 345 | // to C++0x rules. |
| 346 | // 2) Otherwise, the object is direct-initialized. |
| 347 | CXXConstructorDecl *Constructor = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 348 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); |
Sebastian Redl | 4f14963 | 2009-05-07 16:14:23 +0000 | [diff] [blame] | 349 | const RecordType *RT; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 350 | unsigned NumConsArgs = ConstructorArgs.size(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 351 | if (AllocType->isDependentType()) { |
| 352 | // Skip all the checks. |
| 353 | } |
Sebastian Redl | 4f14963 | 2009-05-07 16:14:23 +0000 | [diff] [blame] | 354 | else if ((RT = AllocType->getAsRecordType()) && |
| 355 | !AllocType->isAggregateType()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 356 | Constructor = PerformInitializationByConstructor( |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 357 | AllocType, ConsArgs, NumConsArgs, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 358 | D.getSourceRange().getBegin(), |
| 359 | SourceRange(D.getSourceRange().getBegin(), |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 360 | ConstructorRParen), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 361 | RT->getDecl()->getDeclName(), |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 362 | NumConsArgs != 0 ? IK_Direct : IK_Default); |
| 363 | if (!Constructor) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 364 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 365 | } else { |
| 366 | if (!Init) { |
| 367 | // FIXME: Check that no subpart is const. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 368 | if (AllocType.isConstQualified()) |
| 369 | return ExprError(Diag(StartLoc, diag::err_new_uninitialized_const) |
| 370 | << D.getSourceRange()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 371 | } else if (NumConsArgs == 0) { |
| 372 | // Object is value-initialized. Do nothing. |
| 373 | } else if (NumConsArgs == 1) { |
| 374 | // Object is direct-initialized. |
Sebastian Redl | 4f14963 | 2009-05-07 16:14:23 +0000 | [diff] [blame] | 375 | // FIXME: What DeclarationName do we pass in here? |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 376 | if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 377 | DeclarationName() /*AllocType.getAsString()*/, |
| 378 | /*DirectInit=*/true)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 379 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 380 | } else { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 381 | return ExprError(Diag(StartLoc, |
| 382 | diag::err_builtin_direct_init_more_than_one_arg) |
| 383 | << SourceRange(ConstructorLParen, ConstructorRParen)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) |
| 388 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 389 | PlacementArgs.release(); |
| 390 | ConstructorArgs.release(); |
| 391 | return Owned(new (Context) CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 392 | NumPlaceArgs, ParenTypeId, ArraySize, Constructor, Init, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 393 | ConsArgs, NumConsArgs, OperatorDelete, ResultType, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 394 | StartLoc, Init ? ConstructorRParen : SourceLocation())); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 398 | /// in a new-expression. |
| 399 | /// dimension off and stores the size expression in ArraySize. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 400 | bool Sema::CheckAllocatedType(QualType AllocType, const Declarator &D) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 401 | { |
| 402 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 403 | // abstract class type or array thereof. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 404 | if (AllocType->isFunctionType()) |
| 405 | return Diag(D.getSourceRange().getBegin(), diag::err_bad_new_type) |
| 406 | << AllocType << 0 << D.getSourceRange(); |
| 407 | else if (AllocType->isReferenceType()) |
| 408 | return Diag(D.getSourceRange().getBegin(), diag::err_bad_new_type) |
| 409 | << AllocType << 1 << D.getSourceRange(); |
| 410 | else if (!AllocType->isDependentType() && |
| 411 | RequireCompleteType(D.getSourceRange().getBegin(), AllocType, |
| 412 | diag::err_new_incomplete_type, |
| 413 | D.getSourceRange())) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 414 | return true; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 415 | else if (RequireNonAbstractType(D.getSourceRange().getBegin(), AllocType, |
| 416 | diag::err_allocation_of_abstract_type)) |
| 417 | return true; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 418 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 419 | // Every dimension shall be of constant size. |
| 420 | unsigned i = 1; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 421 | while (const ArrayType *Array = Context.getAsArrayType(AllocType)) { |
| 422 | if (!Array->isConstantArrayType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 423 | Diag(D.getTypeObject(i).Loc, diag::err_new_array_nonconst) |
| 424 | << static_cast<Expr*>(D.getTypeObject(i).Arr.NumElts)->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 425 | return true; |
| 426 | } |
| 427 | AllocType = Array->getElementType(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 428 | ++i; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | return false; |
| 432 | } |
| 433 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 434 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 435 | /// that are appropriate for the allocation. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 436 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 437 | bool UseGlobal, QualType AllocType, |
| 438 | bool IsArray, Expr **PlaceArgs, |
| 439 | unsigned NumPlaceArgs, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 440 | FunctionDecl *&OperatorNew, |
| 441 | FunctionDecl *&OperatorDelete) |
| 442 | { |
| 443 | // --- Choosing an allocation function --- |
| 444 | // C++ 5.3.4p8 - 14 & 18 |
| 445 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 446 | // in the scope of the allocated class. |
| 447 | // 2) If an array size is given, look for operator new[], else look for |
| 448 | // operator new. |
| 449 | // 3) The first argument is always size_t. Append the arguments from the |
| 450 | // placement form. |
| 451 | // FIXME: Also find the appropriate delete operator. |
| 452 | |
| 453 | llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
| 454 | // We don't care about the actual value of this argument. |
| 455 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 456 | // tree? Or should the consumer just recalculate the value? |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 457 | AllocArgs[0] = new (Context) IntegerLiteral(llvm::APInt::getNullValue( |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 458 | Context.Target.getPointerWidth(0)), |
| 459 | Context.getSizeType(), |
| 460 | SourceLocation()); |
| 461 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 462 | |
| 463 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 464 | IsArray ? OO_Array_New : OO_New); |
| 465 | if (AllocType->isRecordType() && !UseGlobal) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 466 | CXXRecordDecl *Record |
| 467 | = cast<CXXRecordDecl>(AllocType->getAsRecordType()->getDecl()); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 468 | // FIXME: We fail to find inherited overloads. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 469 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 470 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 471 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 472 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 473 | } |
| 474 | if (!OperatorNew) { |
| 475 | // Didn't find a member overload. Look for a global one. |
| 476 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 477 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 478 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 479 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 480 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 481 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 484 | // FIXME: This is leaked on error. But so much is currently in Sema that it's |
| 485 | // easier to clean it in one go. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 486 | AllocArgs[0]->Destroy(Context); |
| 487 | return false; |
| 488 | } |
| 489 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 490 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 491 | /// function in the specified scope. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 492 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 493 | DeclarationName Name, Expr** Args, |
| 494 | unsigned NumArgs, DeclContext *Ctx, |
| 495 | bool AllowMissing, FunctionDecl *&Operator) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 496 | { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 497 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 498 | llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Context, Name); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 499 | if (Alloc == AllocEnd) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 500 | if (AllowMissing) |
| 501 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 502 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 503 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | OverloadCandidateSet Candidates; |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 507 | for (; Alloc != AllocEnd; ++Alloc) { |
| 508 | // Even member operator new/delete are implicitly treated as |
| 509 | // static, so don't use AddMemberCandidate. |
| 510 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc)) |
| 511 | AddOverloadCandidate(Fn, Args, NumArgs, Candidates, |
| 512 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | // Do the resolution. |
| 516 | OverloadCandidateSet::iterator Best; |
| 517 | switch(BestViableFunction(Candidates, Best)) { |
| 518 | case OR_Success: { |
| 519 | // Got one! |
| 520 | FunctionDecl *FnDecl = Best->Function; |
| 521 | // The first argument is size_t, and the first parameter must be size_t, |
| 522 | // too. This is checked on declaration and can be assumed. (It can't be |
| 523 | // asserted on, though, since invalid decls are left in there.) |
| 524 | for (unsigned i = 1; i < NumArgs; ++i) { |
| 525 | // FIXME: Passing word to diagnostic. |
| 526 | if (PerformCopyInitialization(Args[i-1], |
| 527 | FnDecl->getParamDecl(i)->getType(), |
| 528 | "passing")) |
| 529 | return true; |
| 530 | } |
| 531 | Operator = FnDecl; |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | case OR_No_Viable_Function: |
| 536 | if (AllowMissing) |
| 537 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 538 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 539 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 540 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/false); |
| 541 | return true; |
| 542 | |
| 543 | case OR_Ambiguous: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 544 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 545 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 546 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); |
| 547 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 548 | |
| 549 | case OR_Deleted: |
| 550 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 551 | << Best->Function->isDeleted() |
| 552 | << Name << Range; |
| 553 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); |
| 554 | return true; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 555 | } |
| 556 | assert(false && "Unreachable, bad result from BestViableFunction"); |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 561 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 562 | /// delete. These are: |
| 563 | /// @code |
| 564 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 565 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 566 | /// void operator delete(void *) throw(); |
| 567 | /// void operator delete[](void *) throw(); |
| 568 | /// @endcode |
| 569 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 570 | /// declared. Their use requires including \<new\>. |
| 571 | void Sema::DeclareGlobalNewDelete() |
| 572 | { |
| 573 | if (GlobalNewDeleteDeclared) |
| 574 | return; |
| 575 | GlobalNewDeleteDeclared = true; |
| 576 | |
| 577 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 578 | QualType SizeT = Context.getSizeType(); |
| 579 | |
| 580 | // FIXME: Exception specifications are not added. |
| 581 | DeclareGlobalAllocationFunction( |
| 582 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
| 583 | VoidPtr, SizeT); |
| 584 | DeclareGlobalAllocationFunction( |
| 585 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
| 586 | VoidPtr, SizeT); |
| 587 | DeclareGlobalAllocationFunction( |
| 588 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 589 | Context.VoidTy, VoidPtr); |
| 590 | DeclareGlobalAllocationFunction( |
| 591 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 592 | Context.VoidTy, VoidPtr); |
| 593 | } |
| 594 | |
| 595 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 596 | /// allocation function if it doesn't already exist. |
| 597 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
| 598 | QualType Return, QualType Argument) |
| 599 | { |
| 600 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 601 | |
| 602 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 603 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 604 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 605 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Context, Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 606 | Alloc != AllocEnd; ++Alloc) { |
| 607 | // FIXME: Do we need to check for default arguments here? |
| 608 | FunctionDecl *Func = cast<FunctionDecl>(*Alloc); |
| 609 | if (Func->getNumParams() == 1 && |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 610 | Context.getCanonicalType(Func->getParamDecl(0)->getType())==Argument) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 611 | return; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0); |
| 616 | FunctionDecl *Alloc = |
| 617 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 618 | FnType, FunctionDecl::None, false, true, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 619 | SourceLocation()); |
| 620 | Alloc->setImplicit(); |
| 621 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 622 | 0, Argument, VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 623 | Alloc->setParams(Context, &Param, 1); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 625 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 626 | // make sure it is at the end of the chain to coincide with the |
| 627 | // global scope. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 628 | ((DeclContext *)TUScope->getEntity())->addDecl(Context, Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 631 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 632 | /// @code ::delete ptr; @endcode |
| 633 | /// or |
| 634 | /// @code delete [] ptr; @endcode |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 635 | Action::OwningExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 636 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 637 | bool ArrayForm, ExprArg Operand) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 638 | { |
| 639 | // C++ 5.3.5p1: "The operand shall have a pointer type, or a class type |
| 640 | // having a single conversion function to a pointer type. The result has |
| 641 | // type void." |
| 642 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 643 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 644 | Expr *Ex = (Expr *)Operand.get(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 645 | if (!Ex->isTypeDependent()) { |
| 646 | QualType Type = Ex->getType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 647 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 648 | if (Type->isRecordType()) { |
| 649 | // FIXME: Find that one conversion function and amend the type. |
| 650 | } |
| 651 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 652 | if (!Type->isPointerType()) |
| 653 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 654 | << Type << Ex->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 655 | |
| 656 | QualType Pointee = Type->getAsPointerType()->getPointeeType(); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 657 | if (Pointee->isFunctionType() || Pointee->isVoidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 658 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 659 | << Type << Ex->getSourceRange()); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 660 | else if (!Pointee->isDependentType() && |
| 661 | RequireCompleteType(StartLoc, Pointee, |
| 662 | diag::warn_delete_incomplete, |
| 663 | Ex->getSourceRange())) |
| 664 | return ExprError(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 665 | |
| 666 | // FIXME: Look up the correct operator delete overload and pass a pointer |
| 667 | // along. |
| 668 | // FIXME: Check access and ambiguity of operator delete and destructor. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 671 | Operand.release(); |
| 672 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
| 673 | 0, Ex, StartLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 677 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 678 | /// C++ if/switch/while/for statement. |
| 679 | /// e.g: "if (int x = f()) {...}" |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 680 | Action::OwningExprResult |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 681 | Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, |
| 682 | Declarator &D, |
| 683 | SourceLocation EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 684 | ExprArg AssignExprVal) { |
| 685 | assert(AssignExprVal.get() && "Null assignment expression"); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 686 | |
| 687 | // C++ 6.4p2: |
| 688 | // The declarator shall not specify a function or an array. |
| 689 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 690 | // new class or enumeration. |
| 691 | |
| 692 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 693 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 694 | |
| 695 | QualType Ty = GetTypeForDeclarator(D, S); |
| 696 | |
| 697 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 698 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 699 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 700 | return ExprError(Diag(StartLoc, diag::err_invalid_use_of_function_type) |
| 701 | << SourceRange(StartLoc, EqualLoc)); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 702 | } else if (Ty->isArrayType()) { // ...or an array. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 703 | Diag(StartLoc, diag::err_invalid_use_of_array_type) |
| 704 | << SourceRange(StartLoc, EqualLoc); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 705 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
| 706 | RecordDecl *RD = RT->getDecl(); |
| 707 | // The type-specifier-seq shall not declare a new class... |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 708 | if (RD->isDefinition() && |
| 709 | (RD->getIdentifier() == 0 || S->isDeclScope(DeclPtrTy::make(RD)))) |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 710 | Diag(RD->getLocation(), diag::err_type_defined_in_condition); |
| 711 | } else if (const EnumType *ET = Ty->getAsEnumType()) { |
| 712 | EnumDecl *ED = ET->getDecl(); |
| 713 | // ...or enumeration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 714 | if (ED->isDefinition() && |
| 715 | (ED->getIdentifier() == 0 || S->isDeclScope(DeclPtrTy::make(ED)))) |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 716 | Diag(ED->getLocation(), diag::err_type_defined_in_condition); |
| 717 | } |
| 718 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 719 | DeclPtrTy Dcl = ActOnDeclarator(S, D, DeclPtrTy()); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 720 | if (!Dcl) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 721 | return ExprError(); |
| 722 | AddInitializerToDecl(Dcl, move(AssignExprVal)); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 723 | |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 724 | // Mark this variable as one that is declared within a conditional. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 725 | // We know that the decl had to be a VarDecl because that is the only type of |
| 726 | // decl that can be assigned and the grammar requires an '='. |
| 727 | VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>()); |
| 728 | VD->setDeclaredInCondition(true); |
| 729 | return Owned(new (Context) CXXConditionDeclExpr(StartLoc, EqualLoc, VD)); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 733 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 734 | // C++ 6.4p4: |
| 735 | // The value of a condition that is an initialized declaration in a statement |
| 736 | // other than a switch statement is the value of the declared variable |
| 737 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 738 | // program is ill-formed. |
| 739 | // The value of a condition that is an expression is the value of the |
| 740 | // expression, implicitly converted to bool. |
| 741 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 742 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 743 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 744 | |
| 745 | /// Helper function to determine whether this is the (deprecated) C++ |
| 746 | /// conversion from a string literal to a pointer to non-const char or |
| 747 | /// non-const wchar_t (for narrow and wide string literals, |
| 748 | /// respectively). |
| 749 | bool |
| 750 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 751 | // Look inside the implicit cast, if it exists. |
| 752 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 753 | From = Cast->getSubExpr(); |
| 754 | |
| 755 | // A string literal (2.13.4) that is not a wide string literal can |
| 756 | // be converted to an rvalue of type "pointer to char"; a wide |
| 757 | // string literal can be converted to an rvalue of type "pointer |
| 758 | // to wchar_t" (C++ 4.2p2). |
| 759 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) |
| 760 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) |
| 761 | if (const BuiltinType *ToPointeeType |
| 762 | = ToPtrType->getPointeeType()->getAsBuiltinType()) { |
| 763 | // This conversion is considered only when there is an |
| 764 | // explicit appropriate pointer target type (C++ 4.2p2). |
| 765 | if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 && |
| 766 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 767 | (!StrLit->isWide() && |
| 768 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 769 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | return false; |
| 774 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 775 | |
| 776 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 777 | /// expression From to the type ToType. Returns true if there was an |
| 778 | /// error, false otherwise. The expression From is replaced with the |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 779 | /// converted expression. Flavor is the kind of conversion we're |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 780 | /// performing, used in the error message. If @p AllowExplicit, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 781 | /// explicit user-defined conversions are permitted. @p Elidable should be true |
| 782 | /// when called for copies which may be elided (C++ 12.8p15). C++0x overload |
| 783 | /// resolution works differently in that case. |
| 784 | bool |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 785 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 786 | const char *Flavor, bool AllowExplicit, |
| 787 | bool Elidable) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 788 | { |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 789 | ImplicitConversionSequence ICS; |
| 790 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 791 | if (Elidable && getLangOptions().CPlusPlus0x) { |
| 792 | ICS = TryImplicitConversion(From, ToType, /*SuppressUserConversions*/false, |
| 793 | AllowExplicit, /*ForceRValue*/true); |
| 794 | } |
| 795 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 796 | ICS = TryImplicitConversion(From, ToType, false, AllowExplicit); |
| 797 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 798 | return PerformImplicitConversion(From, ToType, ICS, Flavor); |
| 799 | } |
| 800 | |
| 801 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 802 | /// expression From to the type ToType using the pre-computed implicit |
| 803 | /// conversion sequence ICS. Returns true if there was an error, false |
| 804 | /// otherwise. The expression From is replaced with the converted |
| 805 | /// expression. Flavor is the kind of conversion we're performing, |
| 806 | /// used in the error message. |
| 807 | bool |
| 808 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 809 | const ImplicitConversionSequence &ICS, |
| 810 | const char* Flavor) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 811 | switch (ICS.ConversionKind) { |
| 812 | case ImplicitConversionSequence::StandardConversion: |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 813 | if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 814 | return true; |
| 815 | break; |
| 816 | |
| 817 | case ImplicitConversionSequence::UserDefinedConversion: |
| 818 | // FIXME: This is, of course, wrong. We'll need to actually call |
| 819 | // the constructor or conversion operator, and then cope with the |
| 820 | // standard conversions. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 821 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 822 | ToType->isLValueReferenceType()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 823 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 824 | |
| 825 | case ImplicitConversionSequence::EllipsisConversion: |
| 826 | assert(false && "Cannot perform an ellipsis conversion"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 827 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 828 | |
| 829 | case ImplicitConversionSequence::BadConversion: |
| 830 | return true; |
| 831 | } |
| 832 | |
| 833 | // Everything went well. |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 838 | /// expression From to the type ToType by following the standard |
| 839 | /// conversion sequence SCS. Returns true if there was an error, false |
| 840 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 841 | /// expression. Flavor is the context in which we're performing this |
| 842 | /// conversion, for use in error messages. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 843 | bool |
| 844 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 845 | const StandardConversionSequence& SCS, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 846 | const char *Flavor) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 847 | // Overall FIXME: we are recomputing too many types here and doing |
| 848 | // far too much extra work. What this means is that we need to keep |
| 849 | // track of more information that is computed when we try the |
| 850 | // implicit conversion initially, so that we don't need to recompute |
| 851 | // anything here. |
| 852 | QualType FromType = From->getType(); |
| 853 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 854 | if (SCS.CopyConstructor) { |
| 855 | // FIXME: Create a temporary object by calling the copy |
| 856 | // constructor. |
Douglas Gregor | 66b947f | 2009-01-16 19:38:23 +0000 | [diff] [blame] | 857 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 858 | ToType->isLValueReferenceType()); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 859 | return false; |
| 860 | } |
| 861 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 862 | // Perform the first implicit conversion. |
| 863 | switch (SCS.First) { |
| 864 | case ICK_Identity: |
| 865 | case ICK_Lvalue_To_Rvalue: |
| 866 | // Nothing to do. |
| 867 | break; |
| 868 | |
| 869 | case ICK_Array_To_Pointer: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 870 | FromType = Context.getArrayDecayedType(FromType); |
| 871 | ImpCastExprToType(From, FromType); |
| 872 | break; |
| 873 | |
| 874 | case ICK_Function_To_Pointer: |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 875 | if (Context.getCanonicalType(FromType) == Context.OverloadTy) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 876 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); |
| 877 | if (!Fn) |
| 878 | return true; |
| 879 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 880 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) |
| 881 | return true; |
| 882 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 883 | FixOverloadedFunctionReference(From, Fn); |
| 884 | FromType = From->getType(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 885 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 886 | FromType = Context.getPointerType(FromType); |
| 887 | ImpCastExprToType(From, FromType); |
| 888 | break; |
| 889 | |
| 890 | default: |
| 891 | assert(false && "Improper first standard conversion"); |
| 892 | break; |
| 893 | } |
| 894 | |
| 895 | // Perform the second implicit conversion |
| 896 | switch (SCS.Second) { |
| 897 | case ICK_Identity: |
| 898 | // Nothing to do. |
| 899 | break; |
| 900 | |
| 901 | case ICK_Integral_Promotion: |
| 902 | case ICK_Floating_Promotion: |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 903 | case ICK_Complex_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 904 | case ICK_Integral_Conversion: |
| 905 | case ICK_Floating_Conversion: |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 906 | case ICK_Complex_Conversion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 907 | case ICK_Floating_Integral: |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 908 | case ICK_Complex_Real: |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 909 | case ICK_Compatible_Conversion: |
| 910 | // FIXME: Go deeper to get the unqualified type! |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 911 | FromType = ToType.getUnqualifiedType(); |
| 912 | ImpCastExprToType(From, FromType); |
| 913 | break; |
| 914 | |
| 915 | case ICK_Pointer_Conversion: |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 916 | if (SCS.IncompatibleObjC) { |
| 917 | // Diagnose incompatible Objective-C conversions |
| 918 | Diag(From->getSourceRange().getBegin(), |
| 919 | diag::ext_typecheck_convert_incompatible_pointer) |
| 920 | << From->getType() << ToType << Flavor |
| 921 | << From->getSourceRange(); |
| 922 | } |
| 923 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 924 | if (CheckPointerConversion(From, ToType)) |
| 925 | return true; |
| 926 | ImpCastExprToType(From, ToType); |
| 927 | break; |
| 928 | |
| 929 | case ICK_Pointer_Member: |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 930 | if (CheckMemberPointerConversion(From, ToType)) |
| 931 | return true; |
| 932 | ImpCastExprToType(From, ToType); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 933 | break; |
| 934 | |
| 935 | case ICK_Boolean_Conversion: |
| 936 | FromType = Context.BoolTy; |
| 937 | ImpCastExprToType(From, FromType); |
| 938 | break; |
| 939 | |
| 940 | default: |
| 941 | assert(false && "Improper second standard conversion"); |
| 942 | break; |
| 943 | } |
| 944 | |
| 945 | switch (SCS.Third) { |
| 946 | case ICK_Identity: |
| 947 | // Nothing to do. |
| 948 | break; |
| 949 | |
| 950 | case ICK_Qualification: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 951 | // FIXME: Not sure about lvalue vs rvalue here in the presence of |
| 952 | // rvalue references. |
Douglas Gregor | 66b947f | 2009-01-16 19:38:23 +0000 | [diff] [blame] | 953 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 954 | ToType->isLValueReferenceType()); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 955 | break; |
| 956 | |
| 957 | default: |
| 958 | assert(false && "Improper second standard conversion"); |
| 959 | break; |
| 960 | } |
| 961 | |
| 962 | return false; |
| 963 | } |
| 964 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 965 | Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, |
| 966 | SourceLocation KWLoc, |
| 967 | SourceLocation LParen, |
| 968 | TypeTy *Ty, |
| 969 | SourceLocation RParen) { |
| 970 | // FIXME: Some of the type traits have requirements. Interestingly, only the |
| 971 | // __is_base_of requirement is explicitly stated to be diagnosed. Indeed, |
| 972 | // G++ accepts __is_pod(Incomplete) without complaints, and claims that the |
| 973 | // type is indeed a POD. |
| 974 | |
| 975 | // There is no point in eagerly computing the value. The traits are designed |
| 976 | // to be used from type trait templates, so Ty will be a template parameter |
| 977 | // 99% of the time. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 978 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 979 | QualType::getFromOpaquePtr(Ty), |
| 980 | RParen, Context.BoolTy)); |
| 981 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 982 | |
| 983 | QualType Sema::CheckPointerToMemberOperands( |
| 984 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) |
| 985 | { |
| 986 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 987 | // C++ 5.5p2 |
| 988 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 989 | // be of type "pointer to member of T" (where T is a completely-defined |
| 990 | // class type) [...] |
| 991 | QualType RType = rex->getType(); |
| 992 | const MemberPointerType *MemPtr = RType->getAsMemberPointerType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 993 | if (!MemPtr) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 994 | Diag(Loc, diag::err_bad_memptr_rhs) |
| 995 | << OpSpelling << RType << rex->getSourceRange(); |
| 996 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 997 | } else if (RequireCompleteType(Loc, QualType(MemPtr->getClass(), 0), |
| 998 | diag::err_memptr_rhs_incomplete, |
| 999 | rex->getSourceRange())) |
| 1000 | return QualType(); |
| 1001 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1002 | QualType Class(MemPtr->getClass(), 0); |
| 1003 | |
| 1004 | // C++ 5.5p2 |
| 1005 | // [...] to its first operand, which shall be of class T or of a class of |
| 1006 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 1007 | // such a class] |
| 1008 | QualType LType = lex->getType(); |
| 1009 | if (isIndirect) { |
| 1010 | if (const PointerType *Ptr = LType->getAsPointerType()) |
| 1011 | LType = Ptr->getPointeeType().getNonReferenceType(); |
| 1012 | else { |
| 1013 | Diag(Loc, diag::err_bad_memptr_lhs) |
| 1014 | << OpSpelling << 1 << LType << lex->getSourceRange(); |
| 1015 | return QualType(); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | if (Context.getCanonicalType(Class).getUnqualifiedType() != |
| 1020 | Context.getCanonicalType(LType).getUnqualifiedType()) { |
| 1021 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1022 | /*DetectVirtual=*/false); |
| 1023 | // FIXME: Would it be useful to print full ambiguity paths, |
| 1024 | // or is that overkill? |
| 1025 | if (!IsDerivedFrom(LType, Class, Paths) || |
| 1026 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
| 1027 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
| 1028 | << (int)isIndirect << lex->getType() << lex->getSourceRange(); |
| 1029 | return QualType(); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | // C++ 5.5p2 |
| 1034 | // The result is an object or a function of the type specified by the |
| 1035 | // second operand. |
| 1036 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 1037 | // in accordance with 5.5p5 and 5.2.5. |
| 1038 | // FIXME: This returns a dereferenced member function pointer as a normal |
| 1039 | // function type. However, the only operation valid on such functions is |
| 1040 | // calling them. There's also a GCC extension to get a function pointer to |
| 1041 | // the thing, which is another complication, because this type - unlike the |
| 1042 | // type that is the result of this expression - takes the class as the first |
| 1043 | // argument. |
| 1044 | // We probably need a "MemberFunctionClosureType" or something like that. |
| 1045 | QualType Result = MemPtr->getPointeeType(); |
| 1046 | if (LType.isConstQualified()) |
| 1047 | Result.addConst(); |
| 1048 | if (LType.isVolatileQualified()) |
| 1049 | Result.addVolatile(); |
| 1050 | return Result; |
| 1051 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1052 | |
| 1053 | /// \brief Get the target type of a standard or user-defined conversion. |
| 1054 | static QualType TargetType(const ImplicitConversionSequence &ICS) { |
| 1055 | assert((ICS.ConversionKind == |
| 1056 | ImplicitConversionSequence::StandardConversion || |
| 1057 | ICS.ConversionKind == |
| 1058 | ImplicitConversionSequence::UserDefinedConversion) && |
| 1059 | "function only valid for standard or user-defined conversions"); |
| 1060 | if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1061 | return QualType::getFromOpaquePtr(ICS.Standard.ToTypePtr); |
| 1062 | return QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr); |
| 1063 | } |
| 1064 | |
| 1065 | /// \brief Try to convert a type to another according to C++0x 5.16p3. |
| 1066 | /// |
| 1067 | /// This is part of the parameter validation for the ? operator. If either |
| 1068 | /// value operand is a class type, the two operands are attempted to be |
| 1069 | /// converted to each other. This function does the conversion in one direction. |
| 1070 | /// It emits a diagnostic and returns true only if it finds an ambiguous |
| 1071 | /// conversion. |
| 1072 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 1073 | SourceLocation QuestionLoc, |
| 1074 | ImplicitConversionSequence &ICS) |
| 1075 | { |
| 1076 | // C++0x 5.16p3 |
| 1077 | // The process for determining whether an operand expression E1 of type T1 |
| 1078 | // can be converted to match an operand expression E2 of type T2 is defined |
| 1079 | // as follows: |
| 1080 | // -- If E2 is an lvalue: |
| 1081 | if (To->isLvalue(Self.Context) == Expr::LV_Valid) { |
| 1082 | // E1 can be converted to match E2 if E1 can be implicitly converted to |
| 1083 | // type "lvalue reference to T2", subject to the constraint that in the |
| 1084 | // conversion the reference must bind directly to E1. |
| 1085 | if (!Self.CheckReferenceInit(From, |
| 1086 | Self.Context.getLValueReferenceType(To->getType()), |
| 1087 | &ICS)) |
| 1088 | { |
| 1089 | assert((ICS.ConversionKind == |
| 1090 | ImplicitConversionSequence::StandardConversion || |
| 1091 | ICS.ConversionKind == |
| 1092 | ImplicitConversionSequence::UserDefinedConversion) && |
| 1093 | "expected a definite conversion"); |
| 1094 | bool DirectBinding = |
| 1095 | ICS.ConversionKind == ImplicitConversionSequence::StandardConversion ? |
| 1096 | ICS.Standard.DirectBinding : ICS.UserDefined.After.DirectBinding; |
| 1097 | if (DirectBinding) |
| 1098 | return false; |
| 1099 | } |
| 1100 | } |
| 1101 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1102 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 1103 | // -- if E1 and E2 have class type, and the underlying class types are |
| 1104 | // the same or one is a base class of the other: |
| 1105 | QualType FTy = From->getType(); |
| 1106 | QualType TTy = To->getType(); |
| 1107 | const RecordType *FRec = FTy->getAsRecordType(); |
| 1108 | const RecordType *TRec = TTy->getAsRecordType(); |
| 1109 | bool FDerivedFromT = FRec && TRec && Self.IsDerivedFrom(FTy, TTy); |
| 1110 | if (FRec && TRec && (FRec == TRec || |
| 1111 | FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { |
| 1112 | // E1 can be converted to match E2 if the class of T2 is the |
| 1113 | // same type as, or a base class of, the class of T1, and |
| 1114 | // [cv2 > cv1]. |
| 1115 | if ((FRec == TRec || FDerivedFromT) && TTy.isAtLeastAsQualifiedAs(FTy)) { |
| 1116 | // Could still fail if there's no copy constructor. |
| 1117 | // FIXME: Is this a hard error then, or just a conversion failure? The |
| 1118 | // standard doesn't say. |
| 1119 | ICS = Self.TryCopyInitialization(From, TTy); |
| 1120 | } |
| 1121 | } else { |
| 1122 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 1123 | // implicitly converted to the type that expression E2 would have |
| 1124 | // if E2 were converted to an rvalue. |
| 1125 | // First find the decayed type. |
| 1126 | if (TTy->isFunctionType()) |
| 1127 | TTy = Self.Context.getPointerType(TTy); |
| 1128 | else if(TTy->isArrayType()) |
| 1129 | TTy = Self.Context.getArrayDecayedType(TTy); |
| 1130 | |
| 1131 | // Now try the implicit conversion. |
| 1132 | // FIXME: This doesn't detect ambiguities. |
| 1133 | ICS = Self.TryImplicitConversion(From, TTy); |
| 1134 | } |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 1139 | /// |
| 1140 | /// This is part of the parameter validation for the ? operator. If either |
| 1141 | /// value operand is a class type, overload resolution is used to find a |
| 1142 | /// conversion to a common type. |
| 1143 | static bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS, |
| 1144 | SourceLocation Loc) { |
| 1145 | Expr *Args[2] = { LHS, RHS }; |
| 1146 | OverloadCandidateSet CandidateSet; |
| 1147 | Self.AddBuiltinOperatorCandidates(OO_Conditional, Args, 2, CandidateSet); |
| 1148 | |
| 1149 | OverloadCandidateSet::iterator Best; |
| 1150 | switch (Self.BestViableFunction(CandidateSet, Best)) { |
| 1151 | case Sema::OR_Success: |
| 1152 | // We found a match. Perform the conversions on the arguments and move on. |
| 1153 | if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], |
| 1154 | Best->Conversions[0], "converting") || |
| 1155 | Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], |
| 1156 | Best->Conversions[1], "converting")) |
| 1157 | break; |
| 1158 | return false; |
| 1159 | |
| 1160 | case Sema::OR_No_Viable_Function: |
| 1161 | Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 1162 | << LHS->getType() << RHS->getType() |
| 1163 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1164 | return true; |
| 1165 | |
| 1166 | case Sema::OR_Ambiguous: |
| 1167 | Self.Diag(Loc, diag::err_conditional_ambiguous_ovl) |
| 1168 | << LHS->getType() << RHS->getType() |
| 1169 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1170 | // FIXME: Print the possible common types by printing the return types |
| 1171 | // of the viable candidates. |
| 1172 | break; |
| 1173 | |
| 1174 | case Sema::OR_Deleted: |
| 1175 | assert(false && "Conditional operator has only built-in overloads"); |
| 1176 | break; |
| 1177 | } |
| 1178 | return true; |
| 1179 | } |
| 1180 | |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1181 | /// \brief Perform an "extended" implicit conversion as returned by |
| 1182 | /// TryClassUnification. |
| 1183 | /// |
| 1184 | /// TryClassUnification generates ICSs that include reference bindings. |
| 1185 | /// PerformImplicitConversion is not suitable for this; it chokes if the |
| 1186 | /// second part of a standard conversion is ICK_DerivedToBase. This function |
| 1187 | /// handles the reference binding specially. |
| 1188 | static bool ConvertForConditional(Sema &Self, Expr *&E, |
| 1189 | const ImplicitConversionSequence &ICS) |
| 1190 | { |
| 1191 | if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion && |
| 1192 | ICS.Standard.ReferenceBinding) { |
| 1193 | assert(ICS.Standard.DirectBinding && |
| 1194 | "TryClassUnification should never generate indirect ref bindings"); |
Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 1195 | // FIXME: CheckReferenceInit should be able to reuse the ICS instead of |
| 1196 | // redoing all the work. |
| 1197 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( |
| 1198 | TargetType(ICS))); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1199 | } |
| 1200 | if (ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion && |
| 1201 | ICS.UserDefined.After.ReferenceBinding) { |
| 1202 | assert(ICS.UserDefined.After.DirectBinding && |
| 1203 | "TryClassUnification should never generate indirect ref bindings"); |
Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 1204 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( |
| 1205 | TargetType(ICS))); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1206 | } |
| 1207 | if (Self.PerformImplicitConversion(E, TargetType(ICS), ICS, "converting")) |
| 1208 | return true; |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1212 | /// \brief Check the operands of ?: under C++ semantics. |
| 1213 | /// |
| 1214 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 1215 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
| 1216 | QualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 1217 | SourceLocation QuestionLoc) { |
| 1218 | // FIXME: Handle C99's complex types, vector types, block pointers and |
| 1219 | // Obj-C++ interface pointers. |
| 1220 | |
| 1221 | // C++0x 5.16p1 |
| 1222 | // The first expression is contextually converted to bool. |
| 1223 | if (!Cond->isTypeDependent()) { |
| 1224 | if (CheckCXXBooleanCondition(Cond)) |
| 1225 | return QualType(); |
| 1226 | } |
| 1227 | |
| 1228 | // Either of the arguments dependent? |
| 1229 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) |
| 1230 | return Context.DependentTy; |
| 1231 | |
| 1232 | // C++0x 5.16p2 |
| 1233 | // If either the second or the third operand has type (cv) void, ... |
| 1234 | QualType LTy = LHS->getType(); |
| 1235 | QualType RTy = RHS->getType(); |
| 1236 | bool LVoid = LTy->isVoidType(); |
| 1237 | bool RVoid = RTy->isVoidType(); |
| 1238 | if (LVoid || RVoid) { |
| 1239 | // ... then the [l2r] conversions are performed on the second and third |
| 1240 | // operands ... |
| 1241 | DefaultFunctionArrayConversion(LHS); |
| 1242 | DefaultFunctionArrayConversion(RHS); |
| 1243 | LTy = LHS->getType(); |
| 1244 | RTy = RHS->getType(); |
| 1245 | |
| 1246 | // ... and one of the following shall hold: |
| 1247 | // -- The second or the third operand (but not both) is a throw- |
| 1248 | // expression; the result is of the type of the other and is an rvalue. |
| 1249 | bool LThrow = isa<CXXThrowExpr>(LHS); |
| 1250 | bool RThrow = isa<CXXThrowExpr>(RHS); |
| 1251 | if (LThrow && !RThrow) |
| 1252 | return RTy; |
| 1253 | if (RThrow && !LThrow) |
| 1254 | return LTy; |
| 1255 | |
| 1256 | // -- Both the second and third operands have type void; the result is of |
| 1257 | // type void and is an rvalue. |
| 1258 | if (LVoid && RVoid) |
| 1259 | return Context.VoidTy; |
| 1260 | |
| 1261 | // Neither holds, error. |
| 1262 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 1263 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
| 1264 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1265 | return QualType(); |
| 1266 | } |
| 1267 | |
| 1268 | // Neither is void. |
| 1269 | |
| 1270 | // C++0x 5.16p3 |
| 1271 | // Otherwise, if the second and third operand have different types, and |
| 1272 | // either has (cv) class type, and attempt is made to convert each of those |
| 1273 | // operands to the other. |
| 1274 | if (Context.getCanonicalType(LTy) != Context.getCanonicalType(RTy) && |
| 1275 | (LTy->isRecordType() || RTy->isRecordType())) { |
| 1276 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; |
| 1277 | // These return true if a single direction is already ambiguous. |
| 1278 | if (TryClassUnification(*this, LHS, RHS, QuestionLoc, ICSLeftToRight)) |
| 1279 | return QualType(); |
| 1280 | if (TryClassUnification(*this, RHS, LHS, QuestionLoc, ICSRightToLeft)) |
| 1281 | return QualType(); |
| 1282 | |
| 1283 | bool HaveL2R = ICSLeftToRight.ConversionKind != |
| 1284 | ImplicitConversionSequence::BadConversion; |
| 1285 | bool HaveR2L = ICSRightToLeft.ConversionKind != |
| 1286 | ImplicitConversionSequence::BadConversion; |
| 1287 | // If both can be converted, [...] the program is ill-formed. |
| 1288 | if (HaveL2R && HaveR2L) { |
| 1289 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
| 1290 | << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1291 | return QualType(); |
| 1292 | } |
| 1293 | |
| 1294 | // If exactly one conversion is possible, that conversion is applied to |
| 1295 | // the chosen operand and the converted operands are used in place of the |
| 1296 | // original operands for the remainder of this section. |
| 1297 | if (HaveL2R) { |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1298 | if (ConvertForConditional(*this, LHS, ICSLeftToRight)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1299 | return QualType(); |
| 1300 | LTy = LHS->getType(); |
| 1301 | } else if (HaveR2L) { |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1302 | if (ConvertForConditional(*this, RHS, ICSRightToLeft)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1303 | return QualType(); |
| 1304 | RTy = RHS->getType(); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | // C++0x 5.16p4 |
| 1309 | // If the second and third operands are lvalues and have the same type, |
| 1310 | // the result is of that type [...] |
| 1311 | bool Same = Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy); |
| 1312 | if (Same && LHS->isLvalue(Context) == Expr::LV_Valid && |
| 1313 | RHS->isLvalue(Context) == Expr::LV_Valid) |
| 1314 | return LTy; |
| 1315 | |
| 1316 | // C++0x 5.16p5 |
| 1317 | // Otherwise, the result is an rvalue. If the second and third operands |
| 1318 | // do not have the same type, and either has (cv) class type, ... |
| 1319 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 1320 | // ... overload resolution is used to determine the conversions (if any) |
| 1321 | // to be applied to the operands. If the overload resolution fails, the |
| 1322 | // program is ill-formed. |
| 1323 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 1324 | return QualType(); |
| 1325 | } |
| 1326 | |
| 1327 | // C++0x 5.16p6 |
| 1328 | // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 1329 | // conversions are performed on the second and third operands. |
| 1330 | DefaultFunctionArrayConversion(LHS); |
| 1331 | DefaultFunctionArrayConversion(RHS); |
| 1332 | LTy = LHS->getType(); |
| 1333 | RTy = RHS->getType(); |
| 1334 | |
| 1335 | // After those conversions, one of the following shall hold: |
| 1336 | // -- The second and third operands have the same type; the result |
| 1337 | // is of that type. |
| 1338 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) |
| 1339 | return LTy; |
| 1340 | |
| 1341 | // -- The second and third operands have arithmetic or enumeration type; |
| 1342 | // the usual arithmetic conversions are performed to bring them to a |
| 1343 | // common type, and the result is of that type. |
| 1344 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
| 1345 | UsualArithmeticConversions(LHS, RHS); |
| 1346 | return LHS->getType(); |
| 1347 | } |
| 1348 | |
| 1349 | // -- The second and third operands have pointer type, or one has pointer |
| 1350 | // type and the other is a null pointer constant; pointer conversions |
| 1351 | // and qualification conversions are performed to bring them to their |
| 1352 | // composite pointer type. The result is of the composite pointer type. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1353 | QualType Composite = FindCompositePointerType(LHS, RHS); |
| 1354 | if (!Composite.isNull()) |
| 1355 | return Composite; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1356 | |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1357 | // Fourth bullet is same for pointers-to-member. However, the possible |
| 1358 | // conversions are far more limited: we have null-to-pointer, upcast of |
| 1359 | // containing class, and second-level cv-ness. |
| 1360 | // cv-ness is not a union, but must match one of the two operands. (Which, |
| 1361 | // frankly, is stupid.) |
| 1362 | const MemberPointerType *LMemPtr = LTy->getAsMemberPointerType(); |
| 1363 | const MemberPointerType *RMemPtr = RTy->getAsMemberPointerType(); |
| 1364 | if (LMemPtr && RHS->isNullPointerConstant(Context)) { |
| 1365 | ImpCastExprToType(RHS, LTy); |
| 1366 | return LTy; |
| 1367 | } |
| 1368 | if (RMemPtr && LHS->isNullPointerConstant(Context)) { |
| 1369 | ImpCastExprToType(LHS, RTy); |
| 1370 | return RTy; |
| 1371 | } |
| 1372 | if (LMemPtr && RMemPtr) { |
| 1373 | QualType LPointee = LMemPtr->getPointeeType(); |
| 1374 | QualType RPointee = RMemPtr->getPointeeType(); |
| 1375 | // First, we check that the unqualified pointee type is the same. If it's |
| 1376 | // not, there's no conversion that will unify the two pointers. |
| 1377 | if (Context.getCanonicalType(LPointee).getUnqualifiedType() == |
| 1378 | Context.getCanonicalType(RPointee).getUnqualifiedType()) { |
| 1379 | // Second, we take the greater of the two cv qualifications. If neither |
| 1380 | // is greater than the other, the conversion is not possible. |
| 1381 | unsigned Q = LPointee.getCVRQualifiers() | RPointee.getCVRQualifiers(); |
| 1382 | if (Q == LPointee.getCVRQualifiers() || Q == RPointee.getCVRQualifiers()){ |
| 1383 | // Third, we check if either of the container classes is derived from |
| 1384 | // the other. |
| 1385 | QualType LContainer(LMemPtr->getClass(), 0); |
| 1386 | QualType RContainer(RMemPtr->getClass(), 0); |
| 1387 | QualType MoreDerived; |
| 1388 | if (Context.getCanonicalType(LContainer) == |
| 1389 | Context.getCanonicalType(RContainer)) |
| 1390 | MoreDerived = LContainer; |
| 1391 | else if (IsDerivedFrom(LContainer, RContainer)) |
| 1392 | MoreDerived = LContainer; |
| 1393 | else if (IsDerivedFrom(RContainer, LContainer)) |
| 1394 | MoreDerived = RContainer; |
| 1395 | |
| 1396 | if (!MoreDerived.isNull()) { |
| 1397 | // The type 'Q Pointee (MoreDerived::*)' is the common type. |
| 1398 | // We don't use ImpCastExprToType here because this could still fail |
| 1399 | // for ambiguous or inaccessible conversions. |
| 1400 | QualType Common = Context.getMemberPointerType( |
| 1401 | LPointee.getQualifiedType(Q), MoreDerived.getTypePtr()); |
| 1402 | if (PerformImplicitConversion(LHS, Common, "converting")) |
| 1403 | return QualType(); |
| 1404 | if (PerformImplicitConversion(RHS, Common, "converting")) |
| 1405 | return QualType(); |
| 1406 | return Common; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1412 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 1413 | << LHS->getType() << RHS->getType() |
| 1414 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1415 | return QualType(); |
| 1416 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1417 | |
| 1418 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 1419 | /// |
| 1420 | /// This finds the composite pointer type for @p E1 and @p E2 according to |
| 1421 | /// C++0x 5.9p2. It converts both expressions to this type and returns it. |
| 1422 | /// It does not emit diagnostics. |
| 1423 | QualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2) { |
| 1424 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); |
| 1425 | QualType T1 = E1->getType(), T2 = E2->getType(); |
| 1426 | if(!T1->isPointerType() && !T2->isPointerType()) |
| 1427 | return QualType(); |
| 1428 | |
| 1429 | // C++0x 5.9p2 |
| 1430 | // Pointer conversions and qualification conversions are performed on |
| 1431 | // pointer operands to bring them to their composite pointer type. If |
| 1432 | // one operand is a null pointer constant, the composite pointer type is |
| 1433 | // the type of the other operand. |
| 1434 | if (E1->isNullPointerConstant(Context)) { |
| 1435 | ImpCastExprToType(E1, T2); |
| 1436 | return T2; |
| 1437 | } |
| 1438 | if (E2->isNullPointerConstant(Context)) { |
| 1439 | ImpCastExprToType(E2, T1); |
| 1440 | return T1; |
| 1441 | } |
| 1442 | // Now both have to be pointers. |
| 1443 | if(!T1->isPointerType() || !T2->isPointerType()) |
| 1444 | return QualType(); |
| 1445 | |
| 1446 | // Otherwise, of one of the operands has type "pointer to cv1 void," then |
| 1447 | // the other has type "pointer to cv2 T" and the composite pointer type is |
| 1448 | // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. |
| 1449 | // Otherwise, the composite pointer type is a pointer type similar to the |
| 1450 | // type of one of the operands, with a cv-qualification signature that is |
| 1451 | // the union of the cv-qualification signatures of the operand types. |
| 1452 | // In practice, the first part here is redundant; it's subsumed by the second. |
| 1453 | // What we do here is, we build the two possible composite types, and try the |
| 1454 | // conversions in both directions. If only one works, or if the two composite |
| 1455 | // types are the same, we have succeeded. |
| 1456 | llvm::SmallVector<unsigned, 4> QualifierUnion; |
| 1457 | QualType Composite1 = T1, Composite2 = T2; |
| 1458 | const PointerType *Ptr1, *Ptr2; |
| 1459 | while ((Ptr1 = Composite1->getAsPointerType()) && |
| 1460 | (Ptr2 = Composite2->getAsPointerType())) { |
| 1461 | Composite1 = Ptr1->getPointeeType(); |
| 1462 | Composite2 = Ptr2->getPointeeType(); |
| 1463 | QualifierUnion.push_back( |
| 1464 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 1465 | } |
| 1466 | // Rewrap the composites as pointers with the union CVRs. |
| 1467 | for (llvm::SmallVector<unsigned, 4>::iterator I = QualifierUnion.begin(), |
| 1468 | E = QualifierUnion.end(); I != E; ++I) { |
| 1469 | Composite1 = Context.getPointerType(Composite1.getQualifiedType(*I)); |
| 1470 | Composite2 = Context.getPointerType(Composite2.getQualifiedType(*I)); |
| 1471 | } |
| 1472 | |
| 1473 | ImplicitConversionSequence E1ToC1 = TryImplicitConversion(E1, Composite1); |
| 1474 | ImplicitConversionSequence E2ToC1 = TryImplicitConversion(E2, Composite1); |
| 1475 | ImplicitConversionSequence E1ToC2, E2ToC2; |
| 1476 | E1ToC2.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1477 | E2ToC2.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1478 | if (Context.getCanonicalType(Composite1) != |
| 1479 | Context.getCanonicalType(Composite2)) { |
| 1480 | E1ToC2 = TryImplicitConversion(E1, Composite2); |
| 1481 | E2ToC2 = TryImplicitConversion(E2, Composite2); |
| 1482 | } |
| 1483 | |
| 1484 | bool ToC1Viable = E1ToC1.ConversionKind != |
| 1485 | ImplicitConversionSequence::BadConversion |
| 1486 | && E2ToC1.ConversionKind != |
| 1487 | ImplicitConversionSequence::BadConversion; |
| 1488 | bool ToC2Viable = E1ToC2.ConversionKind != |
| 1489 | ImplicitConversionSequence::BadConversion |
| 1490 | && E2ToC2.ConversionKind != |
| 1491 | ImplicitConversionSequence::BadConversion; |
| 1492 | if (ToC1Viable && !ToC2Viable) { |
| 1493 | if (!PerformImplicitConversion(E1, Composite1, E1ToC1, "converting") && |
| 1494 | !PerformImplicitConversion(E2, Composite1, E2ToC1, "converting")) |
| 1495 | return Composite1; |
| 1496 | } |
| 1497 | if (ToC2Viable && !ToC1Viable) { |
| 1498 | if (!PerformImplicitConversion(E1, Composite2, E1ToC2, "converting") && |
| 1499 | !PerformImplicitConversion(E2, Composite2, E2ToC2, "converting")) |
| 1500 | return Composite2; |
| 1501 | } |
| 1502 | return QualType(); |
| 1503 | } |