blob: 7fc27a44f7b69fb3417228178f5f239787a648ce [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for C++ expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Steve Naroff210679c2007-08-25 14:02:58 +000015#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000016#include "clang/AST/CXXInheritance.h"
Anders Carlssond497ba72009-08-26 22:59:12 +000017#include "clang/AST/ExprCXX.h"
18#include "clang/Basic/PartialDiagnostic.h"
Sebastian Redlb5a57a62008-12-03 20:26:15 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlssond497ba72009-08-26 22:59:12 +000020#include "clang/Lex/Preprocessor.h"
21#include "clang/Parse/DeclSpec.h"
Douglas Gregor3fc749d2008-12-23 00:26:44 +000022#include "llvm/ADT/STLExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Douglas Gregor487a75a2008-11-19 19:09:45 +000025/// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function
Douglas Gregor2def4832008-11-17 20:34:05 +000026/// name (e.g., operator void const *) as an expression. This is
27/// very similar to ActOnIdentifierExpr, except that instead of
28/// providing an identifier the parser provides the type of the
29/// conversion function.
Sebastian Redlcd965b92009-01-18 18:53:16 +000030Sema::OwningExprResult
Douglas Gregor487a75a2008-11-19 19:09:45 +000031Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc,
32 TypeTy *Ty, bool HasTrailingLParen,
Sebastian Redlebc07d52009-02-03 20:19:35 +000033 const CXXScopeSpec &SS,
34 bool isAddressOfOperand) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +000035 //FIXME: Preserve type source info.
36 QualType ConvType = GetTypeFromParser(Ty);
Douglas Gregor50d62d12009-08-05 05:36:45 +000037 CanQualType ConvTypeCanon = Context.getCanonicalType(ConvType);
Mike Stump1eb44332009-09-09 15:08:12 +000038 DeclarationName ConvName
Douglas Gregor2def4832008-11-17 20:34:05 +000039 = Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon);
Sebastian Redlcd965b92009-01-18 18:53:16 +000040 return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen,
Douglas Gregor17330012009-02-04 15:01:18 +000041 &SS, isAddressOfOperand);
Douglas Gregor2def4832008-11-17 20:34:05 +000042}
Sebastian Redlc42e1182008-11-11 11:37:55 +000043
Douglas Gregor487a75a2008-11-19 19:09:45 +000044/// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator
Douglas Gregore94ca9e42008-11-18 14:39:36 +000045/// name (e.g., @c operator+ ) as an expression. This is very
46/// similar to ActOnIdentifierExpr, except that instead of providing
47/// an identifier the parser provides the kind of overloaded
48/// operator that was parsed.
Sebastian Redlcd965b92009-01-18 18:53:16 +000049Sema::OwningExprResult
Douglas Gregor487a75a2008-11-19 19:09:45 +000050Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc,
51 OverloadedOperatorKind Op,
52 bool HasTrailingLParen,
Sebastian Redlebc07d52009-02-03 20:19:35 +000053 const CXXScopeSpec &SS,
54 bool isAddressOfOperand) {
Douglas Gregore94ca9e42008-11-18 14:39:36 +000055 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op);
Sebastian Redlebc07d52009-02-03 20:19:35 +000056 return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS,
Douglas Gregor17330012009-02-04 15:01:18 +000057 isAddressOfOperand);
Douglas Gregore94ca9e42008-11-18 14:39:36 +000058}
59
Sebastian Redlc42e1182008-11-11 11:37:55 +000060/// ActOnCXXTypeidOfType - Parse typeid( type-id ).
Sebastian Redlf53597f2009-03-15 17:47:39 +000061Action::OwningExprResult
Sebastian Redlc42e1182008-11-11 11:37:55 +000062Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc,
63 bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
Douglas Gregor7adb10f2009-09-15 22:30:29 +000064 if (!StdNamespace)
Sebastian Redlf53597f2009-03-15 17:47:39 +000065 return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +000066
67 if (isType)
68 // FIXME: Preserve type source info.
69 TyOrExpr = GetTypeFromParser(TyOrExpr).getAsOpaquePtr();
70
Chris Lattner572af492008-11-20 05:51:55 +000071 IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info");
John McCallf36e02d2009-10-09 21:13:30 +000072 LookupResult R;
73 LookupQualifiedName(R, StdNamespace, TypeInfoII, LookupTagName);
74 Decl *TypeInfoDecl = R.getAsSingleDecl(Context);
Sebastian Redlc42e1182008-11-11 11:37:55 +000075 RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl);
Chris Lattner572af492008-11-20 05:51:55 +000076 if (!TypeInfoRecordDecl)
Sebastian Redlf53597f2009-03-15 17:47:39 +000077 return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
Sebastian Redlc42e1182008-11-11 11:37:55 +000078
79 QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl);
80
Douglas Gregorac7610d2009-06-22 20:57:11 +000081 if (!isType) {
82 // C++0x [expr.typeid]p3:
Mike Stump1eb44332009-09-09 15:08:12 +000083 // When typeid is applied to an expression other than an lvalue of a
84 // polymorphic class type [...] [the] expression is an unevaluated
Douglas Gregorac7610d2009-06-22 20:57:11 +000085 // operand.
Mike Stump1eb44332009-09-09 15:08:12 +000086
Douglas Gregorac7610d2009-06-22 20:57:11 +000087 // FIXME: if the type of the expression is a class type, the class
88 // shall be completely defined.
89 bool isUnevaluatedOperand = true;
90 Expr *E = static_cast<Expr *>(TyOrExpr);
91 if (E && !E->isTypeDependent() && E->isLvalue(Context) == Expr::LV_Valid) {
92 QualType T = E->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +000093 if (const RecordType *RecordT = T->getAs<RecordType>()) {
Douglas Gregorac7610d2009-06-22 20:57:11 +000094 CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl());
95 if (RecordD->isPolymorphic())
96 isUnevaluatedOperand = false;
97 }
98 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Douglas Gregorac7610d2009-06-22 20:57:11 +0000100 // If this is an unevaluated operand, clear out the set of declaration
101 // references we have been computing.
102 if (isUnevaluatedOperand)
103 PotentiallyReferencedDeclStack.back().clear();
104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Sebastian Redlf53597f2009-03-15 17:47:39 +0000106 return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr,
107 TypeInfoType.withConst(),
108 SourceRange(OpLoc, RParenLoc)));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000109}
110
Steve Naroff1b273c42007-09-16 14:56:35 +0000111/// ActOnCXXBoolLiteral - Parse {true,false} literals.
Sebastian Redlf53597f2009-03-15 17:47:39 +0000112Action::OwningExprResult
Steve Naroff1b273c42007-09-16 14:56:35 +0000113Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
Douglas Gregor2f639b92008-10-24 15:36:09 +0000114 assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 "Unknown C++ Boolean value!");
Sebastian Redlf53597f2009-03-15 17:47:39 +0000116 return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true,
117 Context.BoolTy, OpLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000118}
Chris Lattner50dd2892008-02-26 00:51:44 +0000119
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000120/// ActOnCXXNullPtrLiteral - Parse 'nullptr'.
121Action::OwningExprResult
122Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) {
123 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
124}
125
Chris Lattner50dd2892008-02-26 00:51:44 +0000126/// ActOnCXXThrow - Parse throw expressions.
Sebastian Redlf53597f2009-03-15 17:47:39 +0000127Action::OwningExprResult
128Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) {
Sebastian Redl972041f2009-04-27 20:27:31 +0000129 Expr *Ex = E.takeAs<Expr>();
130 if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex))
131 return ExprError();
132 return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc));
133}
134
135/// CheckCXXThrowOperand - Validate the operand of a throw.
136bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) {
137 // C++ [except.throw]p3:
138 // [...] adjusting the type from "array of T" or "function returning T"
139 // to "pointer to T" or "pointer to function returning T", [...]
140 DefaultFunctionArrayConversion(E);
141
142 // If the type of the exception would be an incomplete type or a pointer
143 // to an incomplete type other than (cv) void the program is ill-formed.
144 QualType Ty = E->getType();
145 int isPointer = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +0000146 if (const PointerType* Ptr = Ty->getAs<PointerType>()) {
Sebastian Redl972041f2009-04-27 20:27:31 +0000147 Ty = Ptr->getPointeeType();
148 isPointer = 1;
149 }
150 if (!isPointer || !Ty->isVoidType()) {
151 if (RequireCompleteType(ThrowLoc, Ty,
Anders Carlssond497ba72009-08-26 22:59:12 +0000152 PDiag(isPointer ? diag::err_throw_incomplete_ptr
153 : diag::err_throw_incomplete)
154 << E->getSourceRange()))
Sebastian Redl972041f2009-04-27 20:27:31 +0000155 return true;
156 }
157
158 // FIXME: Construct a temporary here.
159 return false;
Chris Lattner50dd2892008-02-26 00:51:44 +0000160}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000161
Sebastian Redlf53597f2009-03-15 17:47:39 +0000162Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) {
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000163 /// C++ 9.3.2: In the body of a non-static member function, the keyword this
164 /// is a non-lvalue expression whose value is the address of the object for
165 /// which the function is called.
166
Sebastian Redlf53597f2009-03-15 17:47:39 +0000167 if (!isa<FunctionDecl>(CurContext))
168 return ExprError(Diag(ThisLoc, diag::err_invalid_this_use));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000169
170 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
171 if (MD->isInstance())
Sebastian Redlf53597f2009-03-15 17:47:39 +0000172 return Owned(new (Context) CXXThisExpr(ThisLoc,
173 MD->getThisType(Context)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000174
Sebastian Redlf53597f2009-03-15 17:47:39 +0000175 return ExprError(Diag(ThisLoc, diag::err_invalid_this_use));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000176}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000177
178/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
179/// Can be interpreted either as function-style casting ("int(x)")
180/// or class type construction ("ClassType(x,y,z)")
181/// or creation of a value-initialized type ("int()").
Sebastian Redlf53597f2009-03-15 17:47:39 +0000182Action::OwningExprResult
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000183Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
184 SourceLocation LParenLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000185 MultiExprArg exprs,
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000186 SourceLocation *CommaLocs,
187 SourceLocation RParenLoc) {
188 assert(TypeRep && "Missing type!");
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000189 // FIXME: Preserve type source info.
190 QualType Ty = GetTypeFromParser(TypeRep);
Sebastian Redlf53597f2009-03-15 17:47:39 +0000191 unsigned NumExprs = exprs.size();
192 Expr **Exprs = (Expr**)exprs.get();
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000193 SourceLocation TyBeginLoc = TypeRange.getBegin();
194 SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
195
Sebastian Redlf53597f2009-03-15 17:47:39 +0000196 if (Ty->isDependentType() ||
Douglas Gregorba498172009-03-13 21:01:28 +0000197 CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) {
Sebastian Redlf53597f2009-03-15 17:47:39 +0000198 exprs.release();
Mike Stump1eb44332009-09-09 15:08:12 +0000199
200 return Owned(CXXUnresolvedConstructExpr::Create(Context,
201 TypeRange.getBegin(), Ty,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000202 LParenLoc,
203 Exprs, NumExprs,
204 RParenLoc));
Douglas Gregorba498172009-03-13 21:01:28 +0000205 }
206
Anders Carlssonbb60a502009-08-27 03:53:50 +0000207 if (Ty->isArrayType())
208 return ExprError(Diag(TyBeginLoc,
209 diag::err_value_init_for_array_type) << FullRange);
210 if (!Ty->isVoidType() &&
211 RequireCompleteType(TyBeginLoc, Ty,
212 PDiag(diag::err_invalid_incomplete_type_use)
213 << FullRange))
214 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Anders Carlssonbb60a502009-08-27 03:53:50 +0000216 if (RequireNonAbstractType(TyBeginLoc, Ty,
217 diag::err_allocation_of_abstract_type))
218 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000219
220
Douglas Gregor506ae412009-01-16 18:33:17 +0000221 // C++ [expr.type.conv]p1:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000222 // If the expression list is a single expression, the type conversion
223 // expression is equivalent (in definedness, and if defined in meaning) to the
224 // corresponding cast expression.
225 //
226 if (NumExprs == 1) {
Anders Carlssoncdb61972009-08-07 22:21:05 +0000227 CastExpr::CastKind Kind = CastExpr::CK_Unknown;
Anders Carlsson0aebc812009-09-09 21:33:21 +0000228 CXXMethodDecl *Method = 0;
229 if (CheckCastTypes(TypeRange, Ty, Exprs[0], Kind, Method,
230 /*FunctionalStyle=*/true))
Sebastian Redlf53597f2009-03-15 17:47:39 +0000231 return ExprError();
Anders Carlsson0aebc812009-09-09 21:33:21 +0000232
233 exprs.release();
234 if (Method) {
235 OwningExprResult CastArg
236 = BuildCXXCastArgument(TypeRange.getBegin(), Ty.getNonReferenceType(),
237 Kind, Method, Owned(Exprs[0]));
238 if (CastArg.isInvalid())
239 return ExprError();
240
241 Exprs[0] = CastArg.takeAs<Expr>();
Fariborz Jahanian4fc7ab32009-08-28 15:11:24 +0000242 }
Anders Carlsson0aebc812009-09-09 21:33:21 +0000243
244 return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(),
245 Ty, TyBeginLoc, Kind,
246 Exprs[0], RParenLoc));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000247 }
248
Ted Kremenek6217b802009-07-29 21:53:49 +0000249 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Douglas Gregor506ae412009-01-16 18:33:17 +0000250 CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
Sebastian Redlf53597f2009-03-15 17:47:39 +0000251
Mike Stump1eb44332009-09-09 15:08:12 +0000252 if (NumExprs > 1 || !Record->hasTrivialConstructor() ||
Anders Carlssone7624a72009-08-27 05:08:22 +0000253 !Record->hasTrivialDestructor()) {
Douglas Gregor39da0b82009-09-09 23:08:42 +0000254 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
255
Douglas Gregor506ae412009-01-16 18:33:17 +0000256 CXXConstructorDecl *Constructor
Douglas Gregor39da0b82009-09-09 23:08:42 +0000257 = PerformInitializationByConstructor(Ty, move(exprs),
Douglas Gregor506ae412009-01-16 18:33:17 +0000258 TypeRange.getBegin(),
259 SourceRange(TypeRange.getBegin(),
260 RParenLoc),
261 DeclarationName(),
Douglas Gregor39da0b82009-09-09 23:08:42 +0000262 IK_Direct,
263 ConstructorArgs);
Douglas Gregor506ae412009-01-16 18:33:17 +0000264
Sebastian Redlf53597f2009-03-15 17:47:39 +0000265 if (!Constructor)
266 return ExprError();
267
Mike Stump1eb44332009-09-09 15:08:12 +0000268 OwningExprResult Result =
269 BuildCXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc,
Douglas Gregor39da0b82009-09-09 23:08:42 +0000270 move_arg(ConstructorArgs), RParenLoc);
Anders Carlssone7624a72009-08-27 05:08:22 +0000271 if (Result.isInvalid())
272 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Anders Carlssone7624a72009-08-27 05:08:22 +0000274 return MaybeBindToTemporary(Result.takeAs<Expr>());
Douglas Gregor506ae412009-01-16 18:33:17 +0000275 }
276
277 // Fall through to value-initialize an object of class type that
278 // doesn't have a user-declared default constructor.
279 }
280
281 // C++ [expr.type.conv]p1:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000282 // If the expression list specifies more than a single value, the type shall
283 // be a class with a suitably declared constructor.
284 //
285 if (NumExprs > 1)
Sebastian Redlf53597f2009-03-15 17:47:39 +0000286 return ExprError(Diag(CommaLocs[0],
287 diag::err_builtin_func_cast_more_than_one_arg)
288 << FullRange);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000289
290 assert(NumExprs == 0 && "Expected 0 expressions");
291
Douglas Gregor506ae412009-01-16 18:33:17 +0000292 // C++ [expr.type.conv]p2:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000293 // The expression T(), where T is a simple-type-specifier for a non-array
294 // complete object type or the (possibly cv-qualified) void type, creates an
295 // rvalue of the specified type, which is value-initialized.
296 //
Sebastian Redlf53597f2009-03-15 17:47:39 +0000297 exprs.release();
298 return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000299}
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000300
301
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000302/// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.:
303/// @code new (memory) int[size][4] @endcode
304/// or
305/// @code ::new Foo(23, "hello") @endcode
306/// For the interpretation of this heap of arguments, consult the base version.
Sebastian Redlf53597f2009-03-15 17:47:39 +0000307Action::OwningExprResult
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000308Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000309 SourceLocation PlacementLParen, MultiExprArg PlacementArgs,
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000310 SourceLocation PlacementRParen, bool ParenTypeId,
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000311 Declarator &D, SourceLocation ConstructorLParen,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000312 MultiExprArg ConstructorArgs,
Mike Stump1eb44332009-09-09 15:08:12 +0000313 SourceLocation ConstructorRParen) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000314 Expr *ArraySize = 0;
315 unsigned Skip = 0;
316 // If the specified type is an array, unwrap it and save the expression.
317 if (D.getNumTypeObjects() > 0 &&
318 D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
319 DeclaratorChunk &Chunk = D.getTypeObject(0);
320 if (Chunk.Arr.hasStatic)
Sebastian Redlf53597f2009-03-15 17:47:39 +0000321 return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new)
322 << D.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000323 if (!Chunk.Arr.NumElts)
Sebastian Redlf53597f2009-03-15 17:47:39 +0000324 return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size)
325 << D.getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000326 ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts);
327 Skip = 1;
328 }
329
Douglas Gregor043cad22009-09-11 00:18:58 +0000330 // Every dimension shall be of constant size.
331 if (D.getNumTypeObjects() > 0 &&
332 D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
333 for (unsigned I = 1, N = D.getNumTypeObjects(); I < N; ++I) {
334 if (D.getTypeObject(I).Kind != DeclaratorChunk::Array)
335 break;
336
337 DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr;
338 if (Expr *NumElts = (Expr *)Array.NumElts) {
339 if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() &&
340 !NumElts->isIntegerConstantExpr(Context)) {
341 Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst)
342 << NumElts->getSourceRange();
343 return ExprError();
344 }
345 }
346 }
347 }
348
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000349 //FIXME: Store DeclaratorInfo in CXXNew expression.
350 DeclaratorInfo *DInfo = 0;
351 QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, &DInfo, Skip);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000352 if (D.isInvalidType())
Sebastian Redlf53597f2009-03-15 17:47:39 +0000353 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000354
Mike Stump1eb44332009-09-09 15:08:12 +0000355 return BuildCXXNew(StartLoc, UseGlobal,
Douglas Gregor3433cf72009-05-21 00:00:09 +0000356 PlacementLParen,
Mike Stump1eb44332009-09-09 15:08:12 +0000357 move(PlacementArgs),
Douglas Gregor3433cf72009-05-21 00:00:09 +0000358 PlacementRParen,
359 ParenTypeId,
Mike Stump1eb44332009-09-09 15:08:12 +0000360 AllocType,
Douglas Gregor3433cf72009-05-21 00:00:09 +0000361 D.getSourceRange().getBegin(),
362 D.getSourceRange(),
363 Owned(ArraySize),
364 ConstructorLParen,
365 move(ConstructorArgs),
366 ConstructorRParen);
367}
368
Mike Stump1eb44332009-09-09 15:08:12 +0000369Sema::OwningExprResult
Douglas Gregor3433cf72009-05-21 00:00:09 +0000370Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal,
371 SourceLocation PlacementLParen,
372 MultiExprArg PlacementArgs,
373 SourceLocation PlacementRParen,
Mike Stump1eb44332009-09-09 15:08:12 +0000374 bool ParenTypeId,
Douglas Gregor3433cf72009-05-21 00:00:09 +0000375 QualType AllocType,
376 SourceLocation TypeLoc,
377 SourceRange TypeRange,
378 ExprArg ArraySizeE,
379 SourceLocation ConstructorLParen,
380 MultiExprArg ConstructorArgs,
381 SourceLocation ConstructorRParen) {
382 if (CheckAllocatedType(AllocType, TypeLoc, TypeRange))
Sebastian Redlf53597f2009-03-15 17:47:39 +0000383 return ExprError();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000384
Douglas Gregor3433cf72009-05-21 00:00:09 +0000385 QualType ResultType = Context.getPointerType(AllocType);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000386
387 // That every array dimension except the first is constant was already
388 // checked by the type check above.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000389
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000390 // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral
391 // or enumeration type with a non-negative value."
Douglas Gregor3433cf72009-05-21 00:00:09 +0000392 Expr *ArraySize = (Expr *)ArraySizeE.get();
Sebastian Redl28507842009-02-26 14:39:58 +0000393 if (ArraySize && !ArraySize->isTypeDependent()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000394 QualType SizeType = ArraySize->getType();
395 if (!SizeType->isIntegralType() && !SizeType->isEnumeralType())
Sebastian Redlf53597f2009-03-15 17:47:39 +0000396 return ExprError(Diag(ArraySize->getSourceRange().getBegin(),
397 diag::err_array_size_not_integral)
398 << SizeType << ArraySize->getSourceRange());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000399 // Let's see if this is a constant < 0. If so, we reject it out of hand.
400 // We don't care about special rules, so we tell the machinery it's not
401 // evaluated - it gives us a result in more cases.
Sebastian Redl28507842009-02-26 14:39:58 +0000402 if (!ArraySize->isValueDependent()) {
403 llvm::APSInt Value;
404 if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) {
405 if (Value < llvm::APSInt(
Anders Carlssonac18b2e2009-09-23 00:37:25 +0000406 llvm::APInt::getNullValue(Value.getBitWidth()),
407 Value.isUnsigned()))
Sebastian Redlf53597f2009-03-15 17:47:39 +0000408 return ExprError(Diag(ArraySize->getSourceRange().getBegin(),
409 diag::err_typecheck_negative_array_size)
410 << ArraySize->getSourceRange());
Sebastian Redl28507842009-02-26 14:39:58 +0000411 }
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000412 }
Anders Carlssonac18b2e2009-09-23 00:37:25 +0000413
414 ImpCastExprToType(ArraySize, Context.getSizeType());
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000415 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000416
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000417 FunctionDecl *OperatorNew = 0;
418 FunctionDecl *OperatorDelete = 0;
Sebastian Redlf53597f2009-03-15 17:47:39 +0000419 Expr **PlaceArgs = (Expr**)PlacementArgs.get();
420 unsigned NumPlaceArgs = PlacementArgs.size();
Sebastian Redl28507842009-02-26 14:39:58 +0000421 if (!AllocType->isDependentType() &&
422 !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) &&
423 FindAllocationFunctions(StartLoc,
Sebastian Redl00e68e22009-02-09 18:24:27 +0000424 SourceRange(PlacementLParen, PlacementRParen),
425 UseGlobal, AllocType, ArraySize, PlaceArgs,
426 NumPlaceArgs, OperatorNew, OperatorDelete))
Sebastian Redlf53597f2009-03-15 17:47:39 +0000427 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000428
429 bool Init = ConstructorLParen.isValid();
430 // --- Choosing a constructor ---
431 // C++ 5.3.4p15
432 // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid)
433 // the object is not initialized. If the object, or any part of it, is
434 // const-qualified, it's an error.
435 // 2) If T is a POD and there's an empty initializer, the object is value-
436 // initialized.
437 // 3) If T is a POD and there's one initializer argument, the object is copy-
438 // constructed.
439 // 4) If T is a POD and there's more initializer arguments, it's an error.
440 // 5) If T is not a POD, the initializer arguments are used as constructor
441 // arguments.
442 //
443 // Or by the C++0x formulation:
444 // 1) If there's no initializer, the object is default-initialized according
445 // to C++0x rules.
446 // 2) Otherwise, the object is direct-initialized.
447 CXXConstructorDecl *Constructor = 0;
Sebastian Redlf53597f2009-03-15 17:47:39 +0000448 Expr **ConsArgs = (Expr**)ConstructorArgs.get();
Sebastian Redl4f149632009-05-07 16:14:23 +0000449 const RecordType *RT;
Sebastian Redlf53597f2009-03-15 17:47:39 +0000450 unsigned NumConsArgs = ConstructorArgs.size();
Sebastian Redl28507842009-02-26 14:39:58 +0000451 if (AllocType->isDependentType()) {
452 // Skip all the checks.
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000453 } else if ((RT = AllocType->getAs<RecordType>()) &&
454 !AllocType->isAggregateType()) {
Douglas Gregor39da0b82009-09-09 23:08:42 +0000455 ASTOwningVector<&ActionBase::DeleteExpr> ConvertedConstructorArgs(*this);
456
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000457 Constructor = PerformInitializationByConstructor(
Douglas Gregor39da0b82009-09-09 23:08:42 +0000458 AllocType, move(ConstructorArgs),
Douglas Gregor3433cf72009-05-21 00:00:09 +0000459 TypeLoc,
460 SourceRange(TypeLoc, ConstructorRParen),
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000461 RT->getDecl()->getDeclName(),
Douglas Gregor39da0b82009-09-09 23:08:42 +0000462 NumConsArgs != 0 ? IK_Direct : IK_Default,
463 ConvertedConstructorArgs);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000464 if (!Constructor)
Sebastian Redlf53597f2009-03-15 17:47:39 +0000465 return ExprError();
Douglas Gregor39da0b82009-09-09 23:08:42 +0000466
467 // Take the converted constructor arguments and use them for the new
468 // expression.
469 NumConsArgs = ConvertedConstructorArgs.size();
470 ConsArgs = (Expr **)ConvertedConstructorArgs.take();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000471 } else {
472 if (!Init) {
473 // FIXME: Check that no subpart is const.
Sebastian Redlf53597f2009-03-15 17:47:39 +0000474 if (AllocType.isConstQualified())
475 return ExprError(Diag(StartLoc, diag::err_new_uninitialized_const)
Douglas Gregor3433cf72009-05-21 00:00:09 +0000476 << TypeRange);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000477 } else if (NumConsArgs == 0) {
478 // Object is value-initialized. Do nothing.
479 } else if (NumConsArgs == 1) {
480 // Object is direct-initialized.
Sebastian Redl4f149632009-05-07 16:14:23 +0000481 // FIXME: What DeclarationName do we pass in here?
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000482 if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc,
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000483 DeclarationName() /*AllocType.getAsString()*/,
484 /*DirectInit=*/true))
Sebastian Redlf53597f2009-03-15 17:47:39 +0000485 return ExprError();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000486 } else {
Sebastian Redlf53597f2009-03-15 17:47:39 +0000487 return ExprError(Diag(StartLoc,
488 diag::err_builtin_direct_init_more_than_one_arg)
489 << SourceRange(ConstructorLParen, ConstructorRParen));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000490 }
491 }
492
493 // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16)
494
Sebastian Redlf53597f2009-03-15 17:47:39 +0000495 PlacementArgs.release();
496 ConstructorArgs.release();
Douglas Gregor3433cf72009-05-21 00:00:09 +0000497 ArraySizeE.release();
Sebastian Redlf53597f2009-03-15 17:47:39 +0000498 return Owned(new (Context) CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000499 NumPlaceArgs, ParenTypeId, ArraySize, Constructor, Init,
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000500 ConsArgs, NumConsArgs, OperatorDelete, ResultType,
Mike Stump1eb44332009-09-09 15:08:12 +0000501 StartLoc, Init ? ConstructorRParen : SourceLocation()));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000502}
503
504/// CheckAllocatedType - Checks that a type is suitable as the allocated type
505/// in a new-expression.
506/// dimension off and stores the size expression in ArraySize.
Douglas Gregor3433cf72009-05-21 00:00:09 +0000507bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000508 SourceRange R) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000509 // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
510 // abstract class type or array thereof.
Douglas Gregore7450f52009-03-24 19:52:54 +0000511 if (AllocType->isFunctionType())
Douglas Gregor3433cf72009-05-21 00:00:09 +0000512 return Diag(Loc, diag::err_bad_new_type)
513 << AllocType << 0 << R;
Douglas Gregore7450f52009-03-24 19:52:54 +0000514 else if (AllocType->isReferenceType())
Douglas Gregor3433cf72009-05-21 00:00:09 +0000515 return Diag(Loc, diag::err_bad_new_type)
516 << AllocType << 1 << R;
Douglas Gregore7450f52009-03-24 19:52:54 +0000517 else if (!AllocType->isDependentType() &&
Douglas Gregor3433cf72009-05-21 00:00:09 +0000518 RequireCompleteType(Loc, AllocType,
Anders Carlssonb7906612009-08-26 23:45:07 +0000519 PDiag(diag::err_new_incomplete_type)
520 << R))
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000521 return true;
Douglas Gregor3433cf72009-05-21 00:00:09 +0000522 else if (RequireNonAbstractType(Loc, AllocType,
Douglas Gregore7450f52009-03-24 19:52:54 +0000523 diag::err_allocation_of_abstract_type))
524 return true;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000525
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000526 return false;
527}
528
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000529/// FindAllocationFunctions - Finds the overloads of operator new and delete
530/// that are appropriate for the allocation.
Sebastian Redl00e68e22009-02-09 18:24:27 +0000531bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
532 bool UseGlobal, QualType AllocType,
533 bool IsArray, Expr **PlaceArgs,
534 unsigned NumPlaceArgs,
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000535 FunctionDecl *&OperatorNew,
Mike Stump1eb44332009-09-09 15:08:12 +0000536 FunctionDecl *&OperatorDelete) {
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000537 // --- Choosing an allocation function ---
538 // C++ 5.3.4p8 - 14 & 18
539 // 1) If UseGlobal is true, only look in the global scope. Else, also look
540 // in the scope of the allocated class.
541 // 2) If an array size is given, look for operator new[], else look for
542 // operator new.
543 // 3) The first argument is always size_t. Append the arguments from the
544 // placement form.
545 // FIXME: Also find the appropriate delete operator.
546
547 llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs);
548 // We don't care about the actual value of this argument.
549 // FIXME: Should the Sema create the expression and embed it in the syntax
550 // tree? Or should the consumer just recalculate the value?
Anders Carlssond67c4c32009-08-16 20:29:29 +0000551 IntegerLiteral Size(llvm::APInt::getNullValue(
552 Context.Target.getPointerWidth(0)),
553 Context.getSizeType(),
554 SourceLocation());
555 AllocArgs[0] = &Size;
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000556 std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1);
557
558 DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName(
559 IsArray ? OO_Array_New : OO_New);
560 if (AllocType->isRecordType() && !UseGlobal) {
Mike Stump1eb44332009-09-09 15:08:12 +0000561 CXXRecordDecl *Record
Ted Kremenek6217b802009-07-29 21:53:49 +0000562 = cast<CXXRecordDecl>(AllocType->getAs<RecordType>()->getDecl());
Sebastian Redl7f662392008-12-04 22:20:51 +0000563 // FIXME: We fail to find inherited overloads.
Sebastian Redl00e68e22009-02-09 18:24:27 +0000564 if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
Sebastian Redl7f662392008-12-04 22:20:51 +0000565 AllocArgs.size(), Record, /*AllowMissing=*/true,
566 OperatorNew))
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000567 return true;
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000568 }
569 if (!OperatorNew) {
570 // Didn't find a member overload. Look for a global one.
571 DeclareGlobalNewDelete();
Sebastian Redl7f662392008-12-04 22:20:51 +0000572 DeclContext *TUDecl = Context.getTranslationUnitDecl();
Sebastian Redl00e68e22009-02-09 18:24:27 +0000573 if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
Sebastian Redl7f662392008-12-04 22:20:51 +0000574 AllocArgs.size(), TUDecl, /*AllowMissing=*/false,
575 OperatorNew))
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000576 return true;
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000577 }
578
Anders Carlssond9583892009-05-31 20:26:12 +0000579 // FindAllocationOverload can change the passed in arguments, so we need to
580 // copy them back.
581 if (NumPlaceArgs > 0)
582 std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs);
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000584 return false;
585}
586
Sebastian Redl7f662392008-12-04 22:20:51 +0000587/// FindAllocationOverload - Find an fitting overload for the allocation
588/// function in the specified scope.
Sebastian Redl00e68e22009-02-09 18:24:27 +0000589bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range,
590 DeclarationName Name, Expr** Args,
591 unsigned NumArgs, DeclContext *Ctx,
Mike Stump1eb44332009-09-09 15:08:12 +0000592 bool AllowMissing, FunctionDecl *&Operator) {
John McCallf36e02d2009-10-09 21:13:30 +0000593 LookupResult R;
594 LookupQualifiedName(R, Ctx, Name, LookupOrdinaryName);
595 if (R.empty()) {
Sebastian Redl7f662392008-12-04 22:20:51 +0000596 if (AllowMissing)
597 return false;
Sebastian Redl7f662392008-12-04 22:20:51 +0000598 return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
Chris Lattner4330d652009-02-17 07:29:20 +0000599 << Name << Range;
Sebastian Redl7f662392008-12-04 22:20:51 +0000600 }
601
John McCallf36e02d2009-10-09 21:13:30 +0000602 // FIXME: handle ambiguity
603
Sebastian Redl7f662392008-12-04 22:20:51 +0000604 OverloadCandidateSet Candidates;
Douglas Gregor5d64e5b2009-09-30 00:03:47 +0000605 for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end();
606 Alloc != AllocEnd; ++Alloc) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000607 // Even member operator new/delete are implicitly treated as
608 // static, so don't use AddMemberCandidate.
Douglas Gregor90916562009-09-29 18:16:17 +0000609 if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc)) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000610 AddOverloadCandidate(Fn, Args, NumArgs, Candidates,
611 /*SuppressUserConversions=*/false);
Douglas Gregor90916562009-09-29 18:16:17 +0000612 continue;
613 }
614
615 // FIXME: Handle function templates
Sebastian Redl7f662392008-12-04 22:20:51 +0000616 }
617
618 // Do the resolution.
619 OverloadCandidateSet::iterator Best;
Douglas Gregore0762c92009-06-19 23:52:42 +0000620 switch(BestViableFunction(Candidates, StartLoc, Best)) {
Sebastian Redl7f662392008-12-04 22:20:51 +0000621 case OR_Success: {
622 // Got one!
623 FunctionDecl *FnDecl = Best->Function;
624 // The first argument is size_t, and the first parameter must be size_t,
625 // too. This is checked on declaration and can be assumed. (It can't be
626 // asserted on, though, since invalid decls are left in there.)
Douglas Gregor90916562009-09-29 18:16:17 +0000627 for (unsigned i = 0; i < NumArgs; ++i) {
Sebastian Redl7f662392008-12-04 22:20:51 +0000628 // FIXME: Passing word to diagnostic.
Anders Carlssonfc27d262009-05-31 19:49:47 +0000629 if (PerformCopyInitialization(Args[i],
Sebastian Redl7f662392008-12-04 22:20:51 +0000630 FnDecl->getParamDecl(i)->getType(),
631 "passing"))
632 return true;
633 }
634 Operator = FnDecl;
635 return false;
636 }
637
638 case OR_No_Viable_Function:
Sebastian Redl7f662392008-12-04 22:20:51 +0000639 Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
Chris Lattner4330d652009-02-17 07:29:20 +0000640 << Name << Range;
Sebastian Redl7f662392008-12-04 22:20:51 +0000641 PrintOverloadCandidates(Candidates, /*OnlyViable=*/false);
642 return true;
643
644 case OR_Ambiguous:
Sebastian Redl7f662392008-12-04 22:20:51 +0000645 Diag(StartLoc, diag::err_ovl_ambiguous_call)
Sebastian Redl00e68e22009-02-09 18:24:27 +0000646 << Name << Range;
Sebastian Redl7f662392008-12-04 22:20:51 +0000647 PrintOverloadCandidates(Candidates, /*OnlyViable=*/true);
648 return true;
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000649
650 case OR_Deleted:
651 Diag(StartLoc, diag::err_ovl_deleted_call)
652 << Best->Function->isDeleted()
653 << Name << Range;
654 PrintOverloadCandidates(Candidates, /*OnlyViable=*/true);
655 return true;
Sebastian Redl7f662392008-12-04 22:20:51 +0000656 }
657 assert(false && "Unreachable, bad result from BestViableFunction");
658 return true;
659}
660
661
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000662/// DeclareGlobalNewDelete - Declare the global forms of operator new and
663/// delete. These are:
664/// @code
665/// void* operator new(std::size_t) throw(std::bad_alloc);
666/// void* operator new[](std::size_t) throw(std::bad_alloc);
667/// void operator delete(void *) throw();
668/// void operator delete[](void *) throw();
669/// @endcode
670/// Note that the placement and nothrow forms of new are *not* implicitly
671/// declared. Their use requires including \<new\>.
Mike Stump1eb44332009-09-09 15:08:12 +0000672void Sema::DeclareGlobalNewDelete() {
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000673 if (GlobalNewDeleteDeclared)
674 return;
Douglas Gregor7adb10f2009-09-15 22:30:29 +0000675
676 // C++ [basic.std.dynamic]p2:
677 // [...] The following allocation and deallocation functions (18.4) are
678 // implicitly declared in global scope in each translation unit of a
679 // program
680 //
681 // void* operator new(std::size_t) throw(std::bad_alloc);
682 // void* operator new[](std::size_t) throw(std::bad_alloc);
683 // void operator delete(void*) throw();
684 // void operator delete[](void*) throw();
685 //
686 // These implicit declarations introduce only the function names operator
687 // new, operator new[], operator delete, operator delete[].
688 //
689 // Here, we need to refer to std::bad_alloc, so we will implicitly declare
690 // "std" or "bad_alloc" as necessary to form the exception specification.
691 // However, we do not make these implicit declarations visible to name
692 // lookup.
693 if (!StdNamespace) {
694 // The "std" namespace has not yet been defined, so build one implicitly.
695 StdNamespace = NamespaceDecl::Create(Context,
696 Context.getTranslationUnitDecl(),
697 SourceLocation(),
698 &PP.getIdentifierTable().get("std"));
699 StdNamespace->setImplicit(true);
700 }
701
702 if (!StdBadAlloc) {
703 // The "std::bad_alloc" class has not yet been declared, so build it
704 // implicitly.
705 StdBadAlloc = CXXRecordDecl::Create(Context, TagDecl::TK_class,
706 StdNamespace,
707 SourceLocation(),
708 &PP.getIdentifierTable().get("bad_alloc"),
709 SourceLocation(), 0);
710 StdBadAlloc->setImplicit(true);
711 }
712
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000713 GlobalNewDeleteDeclared = true;
714
715 QualType VoidPtr = Context.getPointerType(Context.VoidTy);
716 QualType SizeT = Context.getSizeType();
717
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000718 DeclareGlobalAllocationFunction(
719 Context.DeclarationNames.getCXXOperatorName(OO_New),
720 VoidPtr, SizeT);
721 DeclareGlobalAllocationFunction(
722 Context.DeclarationNames.getCXXOperatorName(OO_Array_New),
723 VoidPtr, SizeT);
724 DeclareGlobalAllocationFunction(
725 Context.DeclarationNames.getCXXOperatorName(OO_Delete),
726 Context.VoidTy, VoidPtr);
727 DeclareGlobalAllocationFunction(
728 Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
729 Context.VoidTy, VoidPtr);
730}
731
732/// DeclareGlobalAllocationFunction - Declares a single implicit global
733/// allocation function if it doesn't already exist.
734void Sema::DeclareGlobalAllocationFunction(DeclarationName Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000735 QualType Return, QualType Argument) {
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000736 DeclContext *GlobalCtx = Context.getTranslationUnitDecl();
737
738 // Check if this function is already declared.
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000739 {
Douglas Gregor5cc37092008-12-23 22:05:29 +0000740 DeclContext::lookup_iterator Alloc, AllocEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000741 for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name);
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000742 Alloc != AllocEnd; ++Alloc) {
743 // FIXME: Do we need to check for default arguments here?
744 FunctionDecl *Func = cast<FunctionDecl>(*Alloc);
745 if (Func->getNumParams() == 1 &&
Ted Kremenek8189cde2009-02-07 01:47:29 +0000746 Context.getCanonicalType(Func->getParamDecl(0)->getType())==Argument)
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000747 return;
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000748 }
749 }
750
Douglas Gregor7adb10f2009-09-15 22:30:29 +0000751 QualType BadAllocType;
752 bool HasBadAllocExceptionSpec
753 = (Name.getCXXOverloadedOperator() == OO_New ||
754 Name.getCXXOverloadedOperator() == OO_Array_New);
755 if (HasBadAllocExceptionSpec) {
756 assert(StdBadAlloc && "Must have std::bad_alloc declared");
757 BadAllocType = Context.getTypeDeclType(StdBadAlloc);
758 }
759
760 QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0,
761 true, false,
762 HasBadAllocExceptionSpec? 1 : 0,
763 &BadAllocType);
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000764 FunctionDecl *Alloc =
765 FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000766 FnType, /*DInfo=*/0, FunctionDecl::None, false, true);
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000767 Alloc->setImplicit();
768 ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000769 0, Argument, /*DInfo=*/0,
770 VarDecl::None, 0);
Ted Kremenekfc767612009-01-14 00:42:25 +0000771 Alloc->setParams(Context, &Param, 1);
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000772
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000773 // FIXME: Also add this declaration to the IdentifierResolver, but
774 // make sure it is at the end of the chain to coincide with the
775 // global scope.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000776 ((DeclContext *)TUScope->getEntity())->addDecl(Alloc);
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000777}
778
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000779/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
780/// @code ::delete ptr; @endcode
781/// or
782/// @code delete [] ptr; @endcode
Sebastian Redlf53597f2009-03-15 17:47:39 +0000783Action::OwningExprResult
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000784Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
Mike Stump1eb44332009-09-09 15:08:12 +0000785 bool ArrayForm, ExprArg Operand) {
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000786 // C++ [expr.delete]p1:
787 // The operand shall have a pointer type, or a class type having a single
788 // conversion function to a pointer type. The result has type void.
789 //
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000790 // DR599 amends "pointer type" to "pointer to object type" in both cases.
791
Anders Carlssond67c4c32009-08-16 20:29:29 +0000792 FunctionDecl *OperatorDelete = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Sebastian Redlf53597f2009-03-15 17:47:39 +0000794 Expr *Ex = (Expr *)Operand.get();
Sebastian Redl28507842009-02-26 14:39:58 +0000795 if (!Ex->isTypeDependent()) {
796 QualType Type = Ex->getType();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000797
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000798 if (const RecordType *Record = Type->getAs<RecordType>()) {
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000799 llvm::SmallVector<CXXConversionDecl *, 4> ObjectPtrConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000800 CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
801 OverloadedFunctionDecl *Conversions =
Fariborz Jahanian62509212009-09-12 18:26:03 +0000802 RD->getVisibleConversionFunctions();
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000803
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000804 for (OverloadedFunctionDecl::function_iterator
805 Func = Conversions->function_begin(),
806 FuncEnd = Conversions->function_end();
807 Func != FuncEnd; ++Func) {
808 // Skip over templated conversion functions; they aren't considered.
809 if (isa<FunctionTemplateDecl>(*Func))
810 continue;
811
812 CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func);
813
814 QualType ConvType = Conv->getConversionType().getNonReferenceType();
815 if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>())
816 if (ConvPtrType->getPointeeType()->isObjectType())
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000817 ObjectPtrConversions.push_back(Conv);
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000818 }
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000819 if (ObjectPtrConversions.size() == 1) {
820 // We have a single conversion to a pointer-to-object type. Perform
821 // that conversion.
822 Operand.release();
823 if (!PerformImplicitConversion(Ex,
824 ObjectPtrConversions.front()->getConversionType(),
825 "converting")) {
826 Operand = Owned(Ex);
827 Type = Ex->getType();
828 }
829 }
830 else if (ObjectPtrConversions.size() > 1) {
831 Diag(StartLoc, diag::err_ambiguous_delete_operand)
832 << Type << Ex->getSourceRange();
833 for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) {
834 CXXConversionDecl *Conv = ObjectPtrConversions[i];
835 Diag(Conv->getLocation(), diag::err_ovl_candidate);
836 }
837 return ExprError();
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000838 }
Sebastian Redl28507842009-02-26 14:39:58 +0000839 }
840
Sebastian Redlf53597f2009-03-15 17:47:39 +0000841 if (!Type->isPointerType())
842 return ExprError(Diag(StartLoc, diag::err_delete_operand)
843 << Type << Ex->getSourceRange());
Sebastian Redl28507842009-02-26 14:39:58 +0000844
Ted Kremenek6217b802009-07-29 21:53:49 +0000845 QualType Pointee = Type->getAs<PointerType>()->getPointeeType();
Douglas Gregor8dcb29d2009-03-24 20:13:58 +0000846 if (Pointee->isFunctionType() || Pointee->isVoidType())
Sebastian Redlf53597f2009-03-15 17:47:39 +0000847 return ExprError(Diag(StartLoc, diag::err_delete_operand)
848 << Type << Ex->getSourceRange());
Douglas Gregor8dcb29d2009-03-24 20:13:58 +0000849 else if (!Pointee->isDependentType() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000850 RequireCompleteType(StartLoc, Pointee,
Anders Carlssonb7906612009-08-26 23:45:07 +0000851 PDiag(diag::warn_delete_incomplete)
852 << Ex->getSourceRange()))
Douglas Gregor8dcb29d2009-03-24 20:13:58 +0000853 return ExprError();
Sebastian Redl28507842009-02-26 14:39:58 +0000854
Douglas Gregor1070c9f2009-09-29 21:38:53 +0000855 // C++ [expr.delete]p2:
856 // [Note: a pointer to a const type can be the operand of a
857 // delete-expression; it is not necessary to cast away the constness
858 // (5.2.11) of the pointer expression before it is used as the operand
859 // of the delete-expression. ]
860 ImpCastExprToType(Ex, Context.getPointerType(Context.VoidTy),
861 CastExpr::CK_NoOp);
862
863 // Update the operand.
864 Operand.take();
865 Operand = ExprArg(*this, Ex);
866
Anders Carlssond67c4c32009-08-16 20:29:29 +0000867 DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName(
868 ArrayForm ? OO_Array_Delete : OO_Delete);
869
870 if (Pointee->isRecordType() && !UseGlobal) {
Mike Stump1eb44332009-09-09 15:08:12 +0000871 CXXRecordDecl *Record
Anders Carlssond67c4c32009-08-16 20:29:29 +0000872 = cast<CXXRecordDecl>(Pointee->getAs<RecordType>()->getDecl());
Douglas Gregor90916562009-09-29 18:16:17 +0000873
874 // Try to find operator delete/operator delete[] in class scope.
John McCallf36e02d2009-10-09 21:13:30 +0000875 LookupResult Found;
876 LookupQualifiedName(Found, Record, DeleteName, LookupOrdinaryName);
Douglas Gregor90916562009-09-29 18:16:17 +0000877 // FIXME: Diagnose ambiguity properly
878 assert(!Found.isAmbiguous() && "Ambiguous delete/delete[] not handled");
879 for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
880 F != FEnd; ++F) {
881 if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F))
882 if (Delete->isUsualDeallocationFunction()) {
883 OperatorDelete = Delete;
884 break;
885 }
886 }
887
Fariborz Jahanian34374e62009-09-03 23:18:17 +0000888 if (!Record->hasTrivialDestructor())
889 if (const CXXDestructorDecl *Dtor = Record->getDestructor(Context))
Mike Stump1eb44332009-09-09 15:08:12 +0000890 MarkDeclarationReferenced(StartLoc,
Fariborz Jahanian34374e62009-09-03 23:18:17 +0000891 const_cast<CXXDestructorDecl*>(Dtor));
Anders Carlssond67c4c32009-08-16 20:29:29 +0000892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Anders Carlssond67c4c32009-08-16 20:29:29 +0000894 if (!OperatorDelete) {
895 // Didn't find a member overload. Look for a global one.
896 DeclareGlobalNewDelete();
897 DeclContext *TUDecl = Context.getTranslationUnitDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000898 if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName,
Douglas Gregor90916562009-09-29 18:16:17 +0000899 &Ex, 1, TUDecl, /*AllowMissing=*/false,
Anders Carlssond67c4c32009-08-16 20:29:29 +0000900 OperatorDelete))
901 return ExprError();
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Sebastian Redl28507842009-02-26 14:39:58 +0000904 // FIXME: Check access and ambiguity of operator delete and destructor.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000905 }
906
Sebastian Redlf53597f2009-03-15 17:47:39 +0000907 Operand.release();
908 return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm,
Anders Carlssond67c4c32009-08-16 20:29:29 +0000909 OperatorDelete, Ex, StartLoc));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000910}
911
912
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000913/// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a
914/// C++ if/switch/while/for statement.
915/// e.g: "if (int x = f()) {...}"
Sebastian Redlf53597f2009-03-15 17:47:39 +0000916Action::OwningExprResult
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000917Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
918 Declarator &D,
919 SourceLocation EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000920 ExprArg AssignExprVal) {
921 assert(AssignExprVal.get() && "Null assignment expression");
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000922
923 // C++ 6.4p2:
924 // The declarator shall not specify a function or an array.
925 // The type-specifier-seq shall not contain typedef and shall not declare a
926 // new class or enumeration.
927
928 assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
929 "Parser allowed 'typedef' as storage class of condition decl.");
930
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000931 // FIXME: Store DeclaratorInfo in the expression.
932 DeclaratorInfo *DInfo = 0;
Argyrios Kyrtzidise955e722009-08-11 05:20:41 +0000933 TagDecl *OwnedTag = 0;
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000934 QualType Ty = GetTypeForDeclarator(D, S, &DInfo, /*Skip=*/0, &OwnedTag);
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000936 if (Ty->isFunctionType()) { // The declarator shall not specify a function...
937 // We exit without creating a CXXConditionDeclExpr because a FunctionDecl
938 // would be created and CXXConditionDeclExpr wants a VarDecl.
Sebastian Redlf53597f2009-03-15 17:47:39 +0000939 return ExprError(Diag(StartLoc, diag::err_invalid_use_of_function_type)
940 << SourceRange(StartLoc, EqualLoc));
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000941 } else if (Ty->isArrayType()) { // ...or an array.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000942 Diag(StartLoc, diag::err_invalid_use_of_array_type)
943 << SourceRange(StartLoc, EqualLoc);
Argyrios Kyrtzidise955e722009-08-11 05:20:41 +0000944 } else if (OwnedTag && OwnedTag->isDefinition()) {
945 // The type-specifier-seq shall not declare a new class or enumeration.
946 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000947 }
948
Douglas Gregor2e01cda2009-06-23 21:43:56 +0000949 DeclPtrTy Dcl = ActOnDeclarator(S, D);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000950 if (!Dcl)
Sebastian Redlf53597f2009-03-15 17:47:39 +0000951 return ExprError();
Anders Carlssonf5dcd382009-05-30 21:37:25 +0000952 AddInitializerToDecl(Dcl, move(AssignExprVal), /*DirectInit=*/false);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000953
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000954 // Mark this variable as one that is declared within a conditional.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000955 // We know that the decl had to be a VarDecl because that is the only type of
956 // decl that can be assigned and the grammar requires an '='.
957 VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>());
958 VD->setDeclaredInCondition(true);
959 return Owned(new (Context) CXXConditionDeclExpr(StartLoc, EqualLoc, VD));
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000960}
961
962/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
963bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) {
964 // C++ 6.4p4:
965 // The value of a condition that is an initialized declaration in a statement
966 // other than a switch statement is the value of the declared variable
967 // implicitly converted to type bool. If that conversion is ill-formed, the
968 // program is ill-formed.
969 // The value of a condition that is an expression is the value of the
970 // expression, implicitly converted to bool.
971 //
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000972 return PerformContextuallyConvertToBool(CondExpr);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000973}
Douglas Gregor77a52232008-09-12 00:47:35 +0000974
975/// Helper function to determine whether this is the (deprecated) C++
976/// conversion from a string literal to a pointer to non-const char or
977/// non-const wchar_t (for narrow and wide string literals,
978/// respectively).
Mike Stump1eb44332009-09-09 15:08:12 +0000979bool
Douglas Gregor77a52232008-09-12 00:47:35 +0000980Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
981 // Look inside the implicit cast, if it exists.
982 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
983 From = Cast->getSubExpr();
984
985 // A string literal (2.13.4) that is not a wide string literal can
986 // be converted to an rvalue of type "pointer to char"; a wide
987 // string literal can be converted to an rvalue of type "pointer
988 // to wchar_t" (C++ 4.2p2).
989 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From))
Ted Kremenek6217b802009-07-29 21:53:49 +0000990 if (const PointerType *ToPtrType = ToType->getAs<PointerType>())
Mike Stump1eb44332009-09-09 15:08:12 +0000991 if (const BuiltinType *ToPointeeType
John McCall183700f2009-09-21 23:43:11 +0000992 = ToPtrType->getPointeeType()->getAs<BuiltinType>()) {
Douglas Gregor77a52232008-09-12 00:47:35 +0000993 // This conversion is considered only when there is an
994 // explicit appropriate pointer target type (C++ 4.2p2).
John McCall0953e762009-09-24 19:53:00 +0000995 if (!ToPtrType->getPointeeType().hasQualifiers() &&
Douglas Gregor77a52232008-09-12 00:47:35 +0000996 ((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
997 (!StrLit->isWide() &&
998 (ToPointeeType->getKind() == BuiltinType::Char_U ||
999 ToPointeeType->getKind() == BuiltinType::Char_S))))
1000 return true;
1001 }
1002
1003 return false;
1004}
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001005
1006/// PerformImplicitConversion - Perform an implicit conversion of the
1007/// expression From to the type ToType. Returns true if there was an
1008/// error, false otherwise. The expression From is replaced with the
Douglas Gregor45920e82008-12-19 17:40:08 +00001009/// converted expression. Flavor is the kind of conversion we're
Douglas Gregor09f41cf2009-01-14 15:45:31 +00001010/// performing, used in the error message. If @p AllowExplicit,
Sebastian Redle2b68332009-04-12 17:16:29 +00001011/// explicit user-defined conversions are permitted. @p Elidable should be true
1012/// when called for copies which may be elided (C++ 12.8p15). C++0x overload
1013/// resolution works differently in that case.
1014bool
Douglas Gregor45920e82008-12-19 17:40:08 +00001015Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Sebastian Redle2b68332009-04-12 17:16:29 +00001016 const char *Flavor, bool AllowExplicit,
Mike Stump1eb44332009-09-09 15:08:12 +00001017 bool Elidable) {
Sebastian Redle2b68332009-04-12 17:16:29 +00001018 ImplicitConversionSequence ICS;
Fariborz Jahanian51bebc82009-09-23 20:55:32 +00001019 return PerformImplicitConversion(From, ToType, Flavor, AllowExplicit,
1020 Elidable, ICS);
1021}
1022
1023bool
1024Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
1025 const char *Flavor, bool AllowExplicit,
1026 bool Elidable,
1027 ImplicitConversionSequence& ICS) {
Sebastian Redle2b68332009-04-12 17:16:29 +00001028 ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
1029 if (Elidable && getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00001030 ICS = TryImplicitConversion(From, ToType,
Anders Carlssonda7a18b2009-08-27 17:24:15 +00001031 /*SuppressUserConversions=*/false,
Mike Stump1eb44332009-09-09 15:08:12 +00001032 AllowExplicit,
Anders Carlsson08972922009-08-28 15:33:32 +00001033 /*ForceRValue=*/true,
1034 /*InOverloadResolution=*/false);
Sebastian Redle2b68332009-04-12 17:16:29 +00001035 }
1036 if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) {
Mike Stump1eb44332009-09-09 15:08:12 +00001037 ICS = TryImplicitConversion(From, ToType,
Anders Carlssonda7a18b2009-08-27 17:24:15 +00001038 /*SuppressUserConversions=*/false,
1039 AllowExplicit,
Anders Carlsson08972922009-08-28 15:33:32 +00001040 /*ForceRValue=*/false,
1041 /*InOverloadResolution=*/false);
Sebastian Redle2b68332009-04-12 17:16:29 +00001042 }
Douglas Gregor09f41cf2009-01-14 15:45:31 +00001043 return PerformImplicitConversion(From, ToType, ICS, Flavor);
1044}
1045
Fariborz Jahanian93034ca2009-10-16 19:20:59 +00001046/// BuildCXXDerivedToBaseExpr - This routine generates the suitable AST
1047/// for the derived to base conversion of the expression 'From'. All
1048/// necessary information is passed in ICS.
1049bool
1050Sema::BuildCXXDerivedToBaseExpr(Expr *&From, CastExpr::CastKind CastKind,
1051 const ImplicitConversionSequence& ICS,
1052 const char *Flavor) {
1053 QualType BaseType =
1054 QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr);
1055 // Must do additional defined to base conversion.
1056 QualType DerivedType =
1057 QualType::getFromOpaquePtr(ICS.UserDefined.After.FromTypePtr);
1058
1059 From = new (Context) ImplicitCastExpr(
1060 DerivedType.getNonReferenceType(),
1061 CastKind,
1062 From,
1063 DerivedType->isLValueReferenceType());
1064 From = new (Context) ImplicitCastExpr(BaseType.getNonReferenceType(),
1065 CastExpr::CK_DerivedToBase, From,
1066 BaseType->isLValueReferenceType());
1067 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
1068 OwningExprResult FromResult =
1069 BuildCXXConstructExpr(
1070 ICS.UserDefined.After.CopyConstructor->getLocation(),
1071 BaseType,
1072 ICS.UserDefined.After.CopyConstructor,
1073 MultiExprArg(*this, (void **)&From, 1));
1074 if (FromResult.isInvalid())
1075 return true;
1076 From = FromResult.takeAs<Expr>();
1077 return false;
1078}
1079
Douglas Gregor09f41cf2009-01-14 15:45:31 +00001080/// PerformImplicitConversion - Perform an implicit conversion of the
1081/// expression From to the type ToType using the pre-computed implicit
1082/// conversion sequence ICS. Returns true if there was an error, false
1083/// otherwise. The expression From is replaced with the converted
1084/// expression. Flavor is the kind of conversion we're performing,
1085/// used in the error message.
1086bool
1087Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
1088 const ImplicitConversionSequence &ICS,
1089 const char* Flavor) {
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001090 switch (ICS.ConversionKind) {
1091 case ImplicitConversionSequence::StandardConversion:
Douglas Gregor45920e82008-12-19 17:40:08 +00001092 if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor))
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001093 return true;
1094 break;
1095
Anders Carlssonf6c213a2009-09-15 06:28:28 +00001096 case ImplicitConversionSequence::UserDefinedConversion: {
1097
Fariborz Jahanian7fe5d722009-08-28 22:04:50 +00001098 FunctionDecl *FD = ICS.UserDefined.ConversionFunction;
1099 CastExpr::CastKind CastKind = CastExpr::CK_Unknown;
Anders Carlssonf6c213a2009-09-15 06:28:28 +00001100 QualType BeforeToType;
1101 if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) {
Fariborz Jahanian7fe5d722009-08-28 22:04:50 +00001102 CastKind = CastExpr::CK_UserDefinedConversion;
Anders Carlssonf6c213a2009-09-15 06:28:28 +00001103
1104 // If the user-defined conversion is specified by a conversion function,
1105 // the initial standard conversion sequence converts the source type to
1106 // the implicit object parameter of the conversion function.
1107 BeforeToType = Context.getTagDeclType(Conv->getParent());
1108 } else if (const CXXConstructorDecl *Ctor =
1109 dyn_cast<CXXConstructorDecl>(FD)) {
Anders Carlsson0aebc812009-09-09 21:33:21 +00001110 CastKind = CastExpr::CK_ConstructorConversion;
Anders Carlssonf6c213a2009-09-15 06:28:28 +00001111
1112 // If the user-defined conversion is specified by a constructor, the
1113 // initial standard conversion sequence converts the source type to the
1114 // type required by the argument of the constructor
1115 BeforeToType = Ctor->getParamDecl(0)->getType();
1116 }
Anders Carlsson0aebc812009-09-09 21:33:21 +00001117 else
1118 assert(0 && "Unknown conversion function kind!");
1119
Anders Carlssonf6c213a2009-09-15 06:28:28 +00001120 if (PerformImplicitConversion(From, BeforeToType,
1121 ICS.UserDefined.Before, "converting"))
1122 return true;
1123
Anders Carlsson0aebc812009-09-09 21:33:21 +00001124 OwningExprResult CastArg
1125 = BuildCXXCastArgument(From->getLocStart(),
1126 ToType.getNonReferenceType(),
1127 CastKind, cast<CXXMethodDecl>(FD),
1128 Owned(From));
1129
1130 if (CastArg.isInvalid())
1131 return true;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +00001132
1133 if (ICS.UserDefined.After.Second == ICK_Derived_To_Base &&
1134 ICS.UserDefined.After.CopyConstructor) {
1135 From = CastArg.takeAs<Expr>();
1136 return BuildCXXDerivedToBaseExpr(From, CastKind, ICS, Flavor);
1137 }
Anders Carlsson0aebc812009-09-09 21:33:21 +00001138
Anders Carlsson626c2d62009-09-15 05:49:31 +00001139 From = new (Context) ImplicitCastExpr(ToType.getNonReferenceType(),
Fariborz Jahanian93034ca2009-10-16 19:20:59 +00001140 CastKind, CastArg.takeAs<Expr>(),
Anders Carlsson626c2d62009-09-15 05:49:31 +00001141 ToType->isLValueReferenceType());
Fariborz Jahanian7fe5d722009-08-28 22:04:50 +00001142 return false;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +00001143 }
1144
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001145 case ImplicitConversionSequence::EllipsisConversion:
1146 assert(false && "Cannot perform an ellipsis conversion");
Douglas Gregor60d62c22008-10-31 16:23:19 +00001147 return false;
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001148
1149 case ImplicitConversionSequence::BadConversion:
1150 return true;
1151 }
1152
1153 // Everything went well.
1154 return false;
1155}
1156
1157/// PerformImplicitConversion - Perform an implicit conversion of the
1158/// expression From to the type ToType by following the standard
1159/// conversion sequence SCS. Returns true if there was an error, false
1160/// otherwise. The expression From is replaced with the converted
Douglas Gregor45920e82008-12-19 17:40:08 +00001161/// expression. Flavor is the context in which we're performing this
1162/// conversion, for use in error messages.
Mike Stump1eb44332009-09-09 15:08:12 +00001163bool
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001164Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Douglas Gregor45920e82008-12-19 17:40:08 +00001165 const StandardConversionSequence& SCS,
Douglas Gregor09f41cf2009-01-14 15:45:31 +00001166 const char *Flavor) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001167 // Overall FIXME: we are recomputing too many types here and doing far too
1168 // much extra work. What this means is that we need to keep track of more
1169 // information that is computed when we try the implicit conversion initially,
1170 // so that we don't need to recompute anything here.
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001171 QualType FromType = From->getType();
1172
Douglas Gregor225c41e2008-11-03 19:09:14 +00001173 if (SCS.CopyConstructor) {
Anders Carlsson7c3e8a12009-05-19 04:45:15 +00001174 // FIXME: When can ToType be a reference type?
1175 assert(!ToType->isReferenceType());
Fariborz Jahanianb3c47742009-09-25 18:59:21 +00001176 if (SCS.Second == ICK_Derived_To_Base) {
1177 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
1178 if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor),
1179 MultiExprArg(*this, (void **)&From, 1),
1180 /*FIXME:ConstructLoc*/SourceLocation(),
1181 ConstructorArgs))
1182 return true;
1183 OwningExprResult FromResult =
1184 BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
1185 ToType, SCS.CopyConstructor,
1186 move_arg(ConstructorArgs));
1187 if (FromResult.isInvalid())
1188 return true;
1189 From = FromResult.takeAs<Expr>();
1190 return false;
1191 }
Mike Stump1eb44332009-09-09 15:08:12 +00001192 OwningExprResult FromResult =
1193 BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
1194 ToType, SCS.CopyConstructor,
Anders Carlssonf47511a2009-09-07 22:23:31 +00001195 MultiExprArg(*this, (void**)&From, 1));
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001197 if (FromResult.isInvalid())
1198 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001200 From = FromResult.takeAs<Expr>();
Douglas Gregor225c41e2008-11-03 19:09:14 +00001201 return false;
1202 }
1203
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001204 // Perform the first implicit conversion.
1205 switch (SCS.First) {
1206 case ICK_Identity:
1207 case ICK_Lvalue_To_Rvalue:
1208 // Nothing to do.
1209 break;
1210
1211 case ICK_Array_To_Pointer:
Douglas Gregor48f3bb92009-02-18 21:56:37 +00001212 FromType = Context.getArrayDecayedType(FromType);
Anders Carlsson82495762009-08-08 21:04:35 +00001213 ImpCastExprToType(From, FromType, CastExpr::CK_ArrayToPointerDecay);
Douglas Gregor48f3bb92009-02-18 21:56:37 +00001214 break;
1215
1216 case ICK_Function_To_Pointer:
Douglas Gregor063daf62009-03-13 18:40:31 +00001217 if (Context.getCanonicalType(FromType) == Context.OverloadTy) {
Douglas Gregor904eed32008-11-10 20:40:00 +00001218 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true);
1219 if (!Fn)
1220 return true;
1221
Douglas Gregor48f3bb92009-02-18 21:56:37 +00001222 if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin()))
1223 return true;
1224
Douglas Gregor904eed32008-11-10 20:40:00 +00001225 FixOverloadedFunctionReference(From, Fn);
1226 FromType = From->getType();
Douglas Gregor904eed32008-11-10 20:40:00 +00001227 }
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001228 FromType = Context.getPointerType(FromType);
Anders Carlssonb633c4e2009-09-01 20:37:18 +00001229 ImpCastExprToType(From, FromType, CastExpr::CK_FunctionToPointerDecay);
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001230 break;
1231
1232 default:
1233 assert(false && "Improper first standard conversion");
1234 break;
1235 }
1236
1237 // Perform the second implicit conversion
1238 switch (SCS.Second) {
1239 case ICK_Identity:
Sebastian Redl2c7588f2009-10-10 12:04:10 +00001240 // If both sides are functions (or pointers/references to them), there could
1241 // be incompatible exception declarations.
1242 if (CheckExceptionSpecCompatibility(From, ToType))
1243 return true;
1244 // Nothing else to do.
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001245 break;
1246
1247 case ICK_Integral_Promotion:
1248 case ICK_Floating_Promotion:
Douglas Gregor5cdf8212009-02-12 00:15:05 +00001249 case ICK_Complex_Promotion:
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001250 case ICK_Integral_Conversion:
1251 case ICK_Floating_Conversion:
Douglas Gregor5cdf8212009-02-12 00:15:05 +00001252 case ICK_Complex_Conversion:
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001253 case ICK_Floating_Integral:
Douglas Gregor5cdf8212009-02-12 00:15:05 +00001254 case ICK_Complex_Real:
Douglas Gregorf9201e02009-02-11 23:02:49 +00001255 case ICK_Compatible_Conversion:
1256 // FIXME: Go deeper to get the unqualified type!
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001257 FromType = ToType.getUnqualifiedType();
1258 ImpCastExprToType(From, FromType);
1259 break;
1260
Anders Carlsson61faec12009-09-12 04:46:44 +00001261 case ICK_Pointer_Conversion: {
Douglas Gregor45920e82008-12-19 17:40:08 +00001262 if (SCS.IncompatibleObjC) {
1263 // Diagnose incompatible Objective-C conversions
Mike Stump1eb44332009-09-09 15:08:12 +00001264 Diag(From->getSourceRange().getBegin(),
Douglas Gregor45920e82008-12-19 17:40:08 +00001265 diag::ext_typecheck_convert_incompatible_pointer)
1266 << From->getType() << ToType << Flavor
1267 << From->getSourceRange();
1268 }
1269
Anders Carlsson61faec12009-09-12 04:46:44 +00001270
1271 CastExpr::CastKind Kind = CastExpr::CK_Unknown;
1272 if (CheckPointerConversion(From, ToType, Kind))
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001273 return true;
Anders Carlsson61faec12009-09-12 04:46:44 +00001274 ImpCastExprToType(From, ToType, Kind);
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001275 break;
Anders Carlsson61faec12009-09-12 04:46:44 +00001276 }
1277
1278 case ICK_Pointer_Member: {
1279 CastExpr::CastKind Kind = CastExpr::CK_Unknown;
1280 if (CheckMemberPointerConversion(From, ToType, Kind))
1281 return true;
Sebastian Redl2c7588f2009-10-10 12:04:10 +00001282 if (CheckExceptionSpecCompatibility(From, ToType))
1283 return true;
Anders Carlsson61faec12009-09-12 04:46:44 +00001284 ImpCastExprToType(From, ToType, Kind);
1285 break;
1286 }
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001287 case ICK_Boolean_Conversion:
1288 FromType = Context.BoolTy;
1289 ImpCastExprToType(From, FromType);
1290 break;
1291
1292 default:
1293 assert(false && "Improper second standard conversion");
1294 break;
1295 }
1296
1297 switch (SCS.Third) {
1298 case ICK_Identity:
1299 // Nothing to do.
1300 break;
1301
1302 case ICK_Qualification:
Mike Stump390b4cc2009-05-16 07:39:55 +00001303 // FIXME: Not sure about lvalue vs rvalue here in the presence of rvalue
1304 // references.
Mike Stump1eb44332009-09-09 15:08:12 +00001305 ImpCastExprToType(From, ToType.getNonReferenceType(),
Anders Carlsson3503d042009-07-31 01:23:52 +00001306 CastExpr::CK_Unknown,
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001307 ToType->isLValueReferenceType());
Douglas Gregor94b1dd22008-10-24 04:54:22 +00001308 break;
1309
1310 default:
1311 assert(false && "Improper second standard conversion");
1312 break;
1313 }
1314
1315 return false;
1316}
1317
Sebastian Redl64b45f72009-01-05 20:52:13 +00001318Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT,
1319 SourceLocation KWLoc,
1320 SourceLocation LParen,
1321 TypeTy *Ty,
1322 SourceLocation RParen) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001323 QualType T = GetTypeFromParser(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Anders Carlsson3292d5c2009-07-07 19:06:02 +00001325 // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html
1326 // all traits except __is_class, __is_enum and __is_union require a the type
1327 // to be complete.
1328 if (OTT != UTT_IsClass && OTT != UTT_IsEnum && OTT != UTT_IsUnion) {
Mike Stump1eb44332009-09-09 15:08:12 +00001329 if (RequireCompleteType(KWLoc, T,
Anders Carlssond497ba72009-08-26 22:59:12 +00001330 diag::err_incomplete_type_used_in_type_trait_expr))
Anders Carlsson3292d5c2009-07-07 19:06:02 +00001331 return ExprError();
1332 }
Sebastian Redl64b45f72009-01-05 20:52:13 +00001333
1334 // There is no point in eagerly computing the value. The traits are designed
1335 // to be used from type trait templates, so Ty will be a template parameter
1336 // 99% of the time.
Anders Carlsson3292d5c2009-07-07 19:06:02 +00001337 return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, T,
1338 RParen, Context.BoolTy));
Sebastian Redl64b45f72009-01-05 20:52:13 +00001339}
Sebastian Redl7c8bd602009-02-07 20:10:22 +00001340
1341QualType Sema::CheckPointerToMemberOperands(
Mike Stump1eb44332009-09-09 15:08:12 +00001342 Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) {
Sebastian Redl7c8bd602009-02-07 20:10:22 +00001343 const char *OpSpelling = isIndirect ? "->*" : ".*";
1344 // C++ 5.5p2
1345 // The binary operator .* [p3: ->*] binds its second operand, which shall
1346 // be of type "pointer to member of T" (where T is a completely-defined
1347 // class type) [...]
1348 QualType RType = rex->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001349 const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>();
Douglas Gregore7450f52009-03-24 19:52:54 +00001350 if (!MemPtr) {
Sebastian Redl7c8bd602009-02-07 20:10:22 +00001351 Diag(Loc, diag::err_bad_memptr_rhs)
1352 << OpSpelling << RType << rex->getSourceRange();
1353 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00001354 }
Douglas Gregore7450f52009-03-24 19:52:54 +00001355
Sebastian Redl7c8bd602009-02-07 20:10:22 +00001356 QualType Class(MemPtr->getClass(), 0);
1357
1358 // C++ 5.5p2
1359 // [...] to its first operand, which shall be of class T or of a class of
1360 // which T is an unambiguous and accessible base class. [p3: a pointer to
1361 // such a class]
1362 QualType LType = lex->getType();
1363 if (isIndirect) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001364 if (const PointerType *Ptr = LType->getAs<PointerType>())
Sebastian Redl7c8bd602009-02-07 20:10:22 +00001365 LType = Ptr->getPointeeType().getNonReferenceType();
1366 else {
1367 Diag(Loc, diag::err_bad_memptr_lhs)
1368 << OpSpelling << 1 << LType << lex->getSourceRange();
1369 return QualType();
1370 }
1371 }
1372
1373 if (Context.getCanonicalType(Class).getUnqualifiedType() !=
1374 Context.getCanonicalType(LType).getUnqualifiedType()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +00001375 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
1376 /*DetectVirtual=*/false);
Mike Stump390b4cc2009-05-16 07:39:55 +00001377 // FIXME: Would it be useful to print full ambiguity paths, or is that
1378 // overkill?
Sebastian Redl7c8bd602009-02-07 20:10:22 +00001379 if (!IsDerivedFrom(LType, Class, Paths) ||
1380 Paths.isAmbiguous(Context.getCanonicalType(Class))) {
1381 Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling
1382 << (int)isIndirect << lex->getType() << lex->getSourceRange();
1383 return QualType();
1384 }
1385 }
1386
1387 // C++ 5.5p2
1388 // The result is an object or a function of the type specified by the
1389 // second operand.
1390 // The cv qualifiers are the union of those in the pointer and the left side,
1391 // in accordance with 5.5p5 and 5.2.5.
1392 // FIXME: This returns a dereferenced member function pointer as a normal
1393 // function type. However, the only operation valid on such functions is
Mike Stump390b4cc2009-05-16 07:39:55 +00001394 // calling them. There's also a GCC extension to get a function pointer to the
1395 // thing, which is another complication, because this type - unlike the type
1396 // that is the result of this expression - takes the class as the first
Sebastian Redl7c8bd602009-02-07 20:10:22 +00001397 // argument.
1398 // We probably need a "MemberFunctionClosureType" or something like that.
1399 QualType Result = MemPtr->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001400 Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers());
Sebastian Redl7c8bd602009-02-07 20:10:22 +00001401 return Result;
1402}
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001403
1404/// \brief Get the target type of a standard or user-defined conversion.
1405static QualType TargetType(const ImplicitConversionSequence &ICS) {
1406 assert((ICS.ConversionKind ==
1407 ImplicitConversionSequence::StandardConversion ||
1408 ICS.ConversionKind ==
1409 ImplicitConversionSequence::UserDefinedConversion) &&
1410 "function only valid for standard or user-defined conversions");
1411 if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion)
1412 return QualType::getFromOpaquePtr(ICS.Standard.ToTypePtr);
1413 return QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr);
1414}
1415
1416/// \brief Try to convert a type to another according to C++0x 5.16p3.
1417///
1418/// This is part of the parameter validation for the ? operator. If either
1419/// value operand is a class type, the two operands are attempted to be
1420/// converted to each other. This function does the conversion in one direction.
1421/// It emits a diagnostic and returns true only if it finds an ambiguous
1422/// conversion.
1423static bool TryClassUnification(Sema &Self, Expr *From, Expr *To,
1424 SourceLocation QuestionLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001425 ImplicitConversionSequence &ICS) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001426 // C++0x 5.16p3
1427 // The process for determining whether an operand expression E1 of type T1
1428 // can be converted to match an operand expression E2 of type T2 is defined
1429 // as follows:
1430 // -- If E2 is an lvalue:
1431 if (To->isLvalue(Self.Context) == Expr::LV_Valid) {
1432 // E1 can be converted to match E2 if E1 can be implicitly converted to
1433 // type "lvalue reference to T2", subject to the constraint that in the
1434 // conversion the reference must bind directly to E1.
1435 if (!Self.CheckReferenceInit(From,
1436 Self.Context.getLValueReferenceType(To->getType()),
Douglas Gregor739d8282009-09-23 23:04:10 +00001437 To->getLocStart(),
Anders Carlsson2de3ace2009-08-27 17:30:43 +00001438 /*SuppressUserConversions=*/false,
1439 /*AllowExplicit=*/false,
1440 /*ForceRValue=*/false,
1441 &ICS))
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001442 {
1443 assert((ICS.ConversionKind ==
1444 ImplicitConversionSequence::StandardConversion ||
1445 ICS.ConversionKind ==
1446 ImplicitConversionSequence::UserDefinedConversion) &&
1447 "expected a definite conversion");
1448 bool DirectBinding =
1449 ICS.ConversionKind == ImplicitConversionSequence::StandardConversion ?
1450 ICS.Standard.DirectBinding : ICS.UserDefined.After.DirectBinding;
1451 if (DirectBinding)
1452 return false;
1453 }
1454 }
1455 ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
1456 // -- If E2 is an rvalue, or if the conversion above cannot be done:
1457 // -- if E1 and E2 have class type, and the underlying class types are
1458 // the same or one is a base class of the other:
1459 QualType FTy = From->getType();
1460 QualType TTy = To->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001461 const RecordType *FRec = FTy->getAs<RecordType>();
1462 const RecordType *TRec = TTy->getAs<RecordType>();
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001463 bool FDerivedFromT = FRec && TRec && Self.IsDerivedFrom(FTy, TTy);
1464 if (FRec && TRec && (FRec == TRec ||
1465 FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) {
1466 // E1 can be converted to match E2 if the class of T2 is the
1467 // same type as, or a base class of, the class of T1, and
1468 // [cv2 > cv1].
1469 if ((FRec == TRec || FDerivedFromT) && TTy.isAtLeastAsQualifiedAs(FTy)) {
1470 // Could still fail if there's no copy constructor.
1471 // FIXME: Is this a hard error then, or just a conversion failure? The
1472 // standard doesn't say.
Mike Stump1eb44332009-09-09 15:08:12 +00001473 ICS = Self.TryCopyInitialization(From, TTy,
Anders Carlssond28b4282009-08-27 17:18:13 +00001474 /*SuppressUserConversions=*/false,
Anders Carlsson7b361b52009-08-27 17:37:39 +00001475 /*ForceRValue=*/false,
1476 /*InOverloadResolution=*/false);
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001477 }
1478 } else {
1479 // -- Otherwise: E1 can be converted to match E2 if E1 can be
1480 // implicitly converted to the type that expression E2 would have
1481 // if E2 were converted to an rvalue.
1482 // First find the decayed type.
1483 if (TTy->isFunctionType())
1484 TTy = Self.Context.getPointerType(TTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001485 else if (TTy->isArrayType())
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001486 TTy = Self.Context.getArrayDecayedType(TTy);
1487
1488 // Now try the implicit conversion.
1489 // FIXME: This doesn't detect ambiguities.
Anders Carlssonda7a18b2009-08-27 17:24:15 +00001490 ICS = Self.TryImplicitConversion(From, TTy,
1491 /*SuppressUserConversions=*/false,
1492 /*AllowExplicit=*/false,
Anders Carlsson08972922009-08-28 15:33:32 +00001493 /*ForceRValue=*/false,
1494 /*InOverloadResolution=*/false);
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001495 }
1496 return false;
1497}
1498
1499/// \brief Try to find a common type for two according to C++0x 5.16p5.
1500///
1501/// This is part of the parameter validation for the ? operator. If either
1502/// value operand is a class type, overload resolution is used to find a
1503/// conversion to a common type.
1504static bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS,
1505 SourceLocation Loc) {
1506 Expr *Args[2] = { LHS, RHS };
1507 OverloadCandidateSet CandidateSet;
1508 Self.AddBuiltinOperatorCandidates(OO_Conditional, Args, 2, CandidateSet);
1509
1510 OverloadCandidateSet::iterator Best;
Douglas Gregore0762c92009-06-19 23:52:42 +00001511 switch (Self.BestViableFunction(CandidateSet, Loc, Best)) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001512 case Sema::OR_Success:
1513 // We found a match. Perform the conversions on the arguments and move on.
1514 if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0],
1515 Best->Conversions[0], "converting") ||
1516 Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1],
1517 Best->Conversions[1], "converting"))
1518 break;
1519 return false;
1520
1521 case Sema::OR_No_Viable_Function:
1522 Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands)
1523 << LHS->getType() << RHS->getType()
1524 << LHS->getSourceRange() << RHS->getSourceRange();
1525 return true;
1526
1527 case Sema::OR_Ambiguous:
1528 Self.Diag(Loc, diag::err_conditional_ambiguous_ovl)
1529 << LHS->getType() << RHS->getType()
1530 << LHS->getSourceRange() << RHS->getSourceRange();
Mike Stump390b4cc2009-05-16 07:39:55 +00001531 // FIXME: Print the possible common types by printing the return types of
1532 // the viable candidates.
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001533 break;
1534
1535 case Sema::OR_Deleted:
1536 assert(false && "Conditional operator has only built-in overloads");
1537 break;
1538 }
1539 return true;
1540}
1541
Sebastian Redl76458502009-04-17 16:30:52 +00001542/// \brief Perform an "extended" implicit conversion as returned by
1543/// TryClassUnification.
1544///
1545/// TryClassUnification generates ICSs that include reference bindings.
1546/// PerformImplicitConversion is not suitable for this; it chokes if the
1547/// second part of a standard conversion is ICK_DerivedToBase. This function
1548/// handles the reference binding specially.
1549static bool ConvertForConditional(Sema &Self, Expr *&E,
Mike Stump1eb44332009-09-09 15:08:12 +00001550 const ImplicitConversionSequence &ICS) {
Sebastian Redl76458502009-04-17 16:30:52 +00001551 if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion &&
1552 ICS.Standard.ReferenceBinding) {
1553 assert(ICS.Standard.DirectBinding &&
1554 "TryClassUnification should never generate indirect ref bindings");
Sebastian Redla5cd2cd2009-04-26 11:21:02 +00001555 // FIXME: CheckReferenceInit should be able to reuse the ICS instead of
1556 // redoing all the work.
1557 return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType(
Anders Carlsson2de3ace2009-08-27 17:30:43 +00001558 TargetType(ICS)),
Douglas Gregor739d8282009-09-23 23:04:10 +00001559 /*FIXME:*/E->getLocStart(),
Anders Carlsson2de3ace2009-08-27 17:30:43 +00001560 /*SuppressUserConversions=*/false,
1561 /*AllowExplicit=*/false,
1562 /*ForceRValue=*/false);
Sebastian Redl76458502009-04-17 16:30:52 +00001563 }
1564 if (ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion &&
1565 ICS.UserDefined.After.ReferenceBinding) {
1566 assert(ICS.UserDefined.After.DirectBinding &&
1567 "TryClassUnification should never generate indirect ref bindings");
Sebastian Redla5cd2cd2009-04-26 11:21:02 +00001568 return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType(
Anders Carlsson2de3ace2009-08-27 17:30:43 +00001569 TargetType(ICS)),
Douglas Gregor739d8282009-09-23 23:04:10 +00001570 /*FIXME:*/E->getLocStart(),
Anders Carlsson2de3ace2009-08-27 17:30:43 +00001571 /*SuppressUserConversions=*/false,
1572 /*AllowExplicit=*/false,
1573 /*ForceRValue=*/false);
Sebastian Redl76458502009-04-17 16:30:52 +00001574 }
1575 if (Self.PerformImplicitConversion(E, TargetType(ICS), ICS, "converting"))
1576 return true;
1577 return false;
1578}
1579
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001580/// \brief Check the operands of ?: under C++ semantics.
1581///
1582/// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y
1583/// extension. In this case, LHS == Cond. (But they're not aliases.)
1584QualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS,
1585 SourceLocation QuestionLoc) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001586 // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++
1587 // interface pointers.
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001588
1589 // C++0x 5.16p1
1590 // The first expression is contextually converted to bool.
1591 if (!Cond->isTypeDependent()) {
1592 if (CheckCXXBooleanCondition(Cond))
1593 return QualType();
1594 }
1595
1596 // Either of the arguments dependent?
1597 if (LHS->isTypeDependent() || RHS->isTypeDependent())
1598 return Context.DependentTy;
1599
1600 // C++0x 5.16p2
1601 // If either the second or the third operand has type (cv) void, ...
1602 QualType LTy = LHS->getType();
1603 QualType RTy = RHS->getType();
1604 bool LVoid = LTy->isVoidType();
1605 bool RVoid = RTy->isVoidType();
1606 if (LVoid || RVoid) {
1607 // ... then the [l2r] conversions are performed on the second and third
1608 // operands ...
1609 DefaultFunctionArrayConversion(LHS);
1610 DefaultFunctionArrayConversion(RHS);
1611 LTy = LHS->getType();
1612 RTy = RHS->getType();
1613
1614 // ... and one of the following shall hold:
1615 // -- The second or the third operand (but not both) is a throw-
1616 // expression; the result is of the type of the other and is an rvalue.
1617 bool LThrow = isa<CXXThrowExpr>(LHS);
1618 bool RThrow = isa<CXXThrowExpr>(RHS);
1619 if (LThrow && !RThrow)
1620 return RTy;
1621 if (RThrow && !LThrow)
1622 return LTy;
1623
1624 // -- Both the second and third operands have type void; the result is of
1625 // type void and is an rvalue.
1626 if (LVoid && RVoid)
1627 return Context.VoidTy;
1628
1629 // Neither holds, error.
1630 Diag(QuestionLoc, diag::err_conditional_void_nonvoid)
1631 << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1)
1632 << LHS->getSourceRange() << RHS->getSourceRange();
1633 return QualType();
1634 }
1635
1636 // Neither is void.
1637
1638 // C++0x 5.16p3
1639 // Otherwise, if the second and third operand have different types, and
1640 // either has (cv) class type, and attempt is made to convert each of those
1641 // operands to the other.
1642 if (Context.getCanonicalType(LTy) != Context.getCanonicalType(RTy) &&
1643 (LTy->isRecordType() || RTy->isRecordType())) {
1644 ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft;
1645 // These return true if a single direction is already ambiguous.
1646 if (TryClassUnification(*this, LHS, RHS, QuestionLoc, ICSLeftToRight))
1647 return QualType();
1648 if (TryClassUnification(*this, RHS, LHS, QuestionLoc, ICSRightToLeft))
1649 return QualType();
1650
1651 bool HaveL2R = ICSLeftToRight.ConversionKind !=
1652 ImplicitConversionSequence::BadConversion;
1653 bool HaveR2L = ICSRightToLeft.ConversionKind !=
1654 ImplicitConversionSequence::BadConversion;
1655 // If both can be converted, [...] the program is ill-formed.
1656 if (HaveL2R && HaveR2L) {
1657 Diag(QuestionLoc, diag::err_conditional_ambiguous)
1658 << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange();
1659 return QualType();
1660 }
1661
1662 // If exactly one conversion is possible, that conversion is applied to
1663 // the chosen operand and the converted operands are used in place of the
1664 // original operands for the remainder of this section.
1665 if (HaveL2R) {
Sebastian Redl76458502009-04-17 16:30:52 +00001666 if (ConvertForConditional(*this, LHS, ICSLeftToRight))
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001667 return QualType();
1668 LTy = LHS->getType();
1669 } else if (HaveR2L) {
Sebastian Redl76458502009-04-17 16:30:52 +00001670 if (ConvertForConditional(*this, RHS, ICSRightToLeft))
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001671 return QualType();
1672 RTy = RHS->getType();
1673 }
1674 }
1675
1676 // C++0x 5.16p4
1677 // If the second and third operands are lvalues and have the same type,
1678 // the result is of that type [...]
1679 bool Same = Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy);
1680 if (Same && LHS->isLvalue(Context) == Expr::LV_Valid &&
1681 RHS->isLvalue(Context) == Expr::LV_Valid)
1682 return LTy;
1683
1684 // C++0x 5.16p5
1685 // Otherwise, the result is an rvalue. If the second and third operands
1686 // do not have the same type, and either has (cv) class type, ...
1687 if (!Same && (LTy->isRecordType() || RTy->isRecordType())) {
1688 // ... overload resolution is used to determine the conversions (if any)
1689 // to be applied to the operands. If the overload resolution fails, the
1690 // program is ill-formed.
1691 if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc))
1692 return QualType();
1693 }
1694
1695 // C++0x 5.16p6
1696 // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard
1697 // conversions are performed on the second and third operands.
1698 DefaultFunctionArrayConversion(LHS);
1699 DefaultFunctionArrayConversion(RHS);
1700 LTy = LHS->getType();
1701 RTy = RHS->getType();
1702
1703 // After those conversions, one of the following shall hold:
1704 // -- The second and third operands have the same type; the result
1705 // is of that type.
1706 if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy))
1707 return LTy;
1708
1709 // -- The second and third operands have arithmetic or enumeration type;
1710 // the usual arithmetic conversions are performed to bring them to a
1711 // common type, and the result is of that type.
1712 if (LTy->isArithmeticType() && RTy->isArithmeticType()) {
1713 UsualArithmeticConversions(LHS, RHS);
1714 return LHS->getType();
1715 }
1716
1717 // -- The second and third operands have pointer type, or one has pointer
1718 // type and the other is a null pointer constant; pointer conversions
1719 // and qualification conversions are performed to bring them to their
1720 // composite pointer type. The result is of the composite pointer type.
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001721 QualType Composite = FindCompositePointerType(LHS, RHS);
1722 if (!Composite.isNull())
1723 return Composite;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001724
Sebastian Redl9bebfad2009-04-19 21:15:26 +00001725 // Fourth bullet is same for pointers-to-member. However, the possible
1726 // conversions are far more limited: we have null-to-pointer, upcast of
1727 // containing class, and second-level cv-ness.
1728 // cv-ness is not a union, but must match one of the two operands. (Which,
1729 // frankly, is stupid.)
Ted Kremenek6217b802009-07-29 21:53:49 +00001730 const MemberPointerType *LMemPtr = LTy->getAs<MemberPointerType>();
1731 const MemberPointerType *RMemPtr = RTy->getAs<MemberPointerType>();
Douglas Gregorce940492009-09-25 04:25:58 +00001732 if (LMemPtr &&
1733 RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
Sebastian Redl9bebfad2009-04-19 21:15:26 +00001734 ImpCastExprToType(RHS, LTy);
1735 return LTy;
1736 }
Douglas Gregorce940492009-09-25 04:25:58 +00001737 if (RMemPtr &&
1738 LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
Sebastian Redl9bebfad2009-04-19 21:15:26 +00001739 ImpCastExprToType(LHS, RTy);
1740 return RTy;
1741 }
1742 if (LMemPtr && RMemPtr) {
1743 QualType LPointee = LMemPtr->getPointeeType();
1744 QualType RPointee = RMemPtr->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001745
1746 QualifierCollector LPQuals, RPQuals;
1747 const Type *LPCan = LPQuals.strip(Context.getCanonicalType(LPointee));
1748 const Type *RPCan = RPQuals.strip(Context.getCanonicalType(RPointee));
1749
Sebastian Redl9bebfad2009-04-19 21:15:26 +00001750 // First, we check that the unqualified pointee type is the same. If it's
1751 // not, there's no conversion that will unify the two pointers.
John McCall0953e762009-09-24 19:53:00 +00001752 if (LPCan == RPCan) {
1753
1754 // Second, we take the greater of the two qualifications. If neither
Sebastian Redl9bebfad2009-04-19 21:15:26 +00001755 // is greater than the other, the conversion is not possible.
John McCall0953e762009-09-24 19:53:00 +00001756
1757 Qualifiers MergedQuals = LPQuals + RPQuals;
1758
1759 bool CompatibleQuals = true;
1760 if (MergedQuals.getCVRQualifiers() != LPQuals.getCVRQualifiers() &&
1761 MergedQuals.getCVRQualifiers() != RPQuals.getCVRQualifiers())
1762 CompatibleQuals = false;
1763 else if (LPQuals.getAddressSpace() != RPQuals.getAddressSpace())
1764 // FIXME:
1765 // C99 6.5.15 as modified by TR 18037:
1766 // If the second and third operands are pointers into different
1767 // address spaces, the address spaces must overlap.
1768 CompatibleQuals = false;
1769 // FIXME: GC qualifiers?
1770
1771 if (CompatibleQuals) {
Sebastian Redl9bebfad2009-04-19 21:15:26 +00001772 // Third, we check if either of the container classes is derived from
1773 // the other.
1774 QualType LContainer(LMemPtr->getClass(), 0);
1775 QualType RContainer(RMemPtr->getClass(), 0);
1776 QualType MoreDerived;
1777 if (Context.getCanonicalType(LContainer) ==
1778 Context.getCanonicalType(RContainer))
1779 MoreDerived = LContainer;
1780 else if (IsDerivedFrom(LContainer, RContainer))
1781 MoreDerived = LContainer;
1782 else if (IsDerivedFrom(RContainer, LContainer))
1783 MoreDerived = RContainer;
1784
1785 if (!MoreDerived.isNull()) {
1786 // The type 'Q Pointee (MoreDerived::*)' is the common type.
1787 // We don't use ImpCastExprToType here because this could still fail
1788 // for ambiguous or inaccessible conversions.
John McCall0953e762009-09-24 19:53:00 +00001789 LPointee = Context.getQualifiedType(LPointee, MergedQuals);
1790 QualType Common
1791 = Context.getMemberPointerType(LPointee, MoreDerived.getTypePtr());
Sebastian Redl9bebfad2009-04-19 21:15:26 +00001792 if (PerformImplicitConversion(LHS, Common, "converting"))
1793 return QualType();
1794 if (PerformImplicitConversion(RHS, Common, "converting"))
1795 return QualType();
1796 return Common;
1797 }
1798 }
1799 }
1800 }
1801
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001802 Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands)
1803 << LHS->getType() << RHS->getType()
1804 << LHS->getSourceRange() << RHS->getSourceRange();
1805 return QualType();
1806}
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001807
1808/// \brief Find a merged pointer type and convert the two expressions to it.
1809///
Douglas Gregor20b3e992009-08-24 17:42:35 +00001810/// This finds the composite pointer type (or member pointer type) for @p E1
1811/// and @p E2 according to C++0x 5.9p2. It converts both expressions to this
1812/// type and returns it.
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001813/// It does not emit diagnostics.
1814QualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2) {
1815 assert(getLangOptions().CPlusPlus && "This function assumes C++");
1816 QualType T1 = E1->getType(), T2 = E2->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Douglas Gregor20b3e992009-08-24 17:42:35 +00001818 if (!T1->isPointerType() && !T1->isMemberPointerType() &&
1819 !T2->isPointerType() && !T2->isMemberPointerType())
1820 return QualType();
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001821
Douglas Gregor20b3e992009-08-24 17:42:35 +00001822 // FIXME: Do we need to work on the canonical types?
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001824 // C++0x 5.9p2
1825 // Pointer conversions and qualification conversions are performed on
1826 // pointer operands to bring them to their composite pointer type. If
1827 // one operand is a null pointer constant, the composite pointer type is
1828 // the type of the other operand.
Douglas Gregorce940492009-09-25 04:25:58 +00001829 if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001830 ImpCastExprToType(E1, T2);
1831 return T2;
1832 }
Douglas Gregorce940492009-09-25 04:25:58 +00001833 if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) {
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001834 ImpCastExprToType(E2, T1);
1835 return T1;
1836 }
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Douglas Gregor20b3e992009-08-24 17:42:35 +00001838 // Now both have to be pointers or member pointers.
1839 if (!T1->isPointerType() && !T1->isMemberPointerType() &&
1840 !T2->isPointerType() && !T2->isMemberPointerType())
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001841 return QualType();
1842
1843 // Otherwise, of one of the operands has type "pointer to cv1 void," then
1844 // the other has type "pointer to cv2 T" and the composite pointer type is
1845 // "pointer to cv12 void," where cv12 is the union of cv1 and cv2.
1846 // Otherwise, the composite pointer type is a pointer type similar to the
1847 // type of one of the operands, with a cv-qualification signature that is
1848 // the union of the cv-qualification signatures of the operand types.
1849 // In practice, the first part here is redundant; it's subsumed by the second.
1850 // What we do here is, we build the two possible composite types, and try the
1851 // conversions in both directions. If only one works, or if the two composite
1852 // types are the same, we have succeeded.
John McCall0953e762009-09-24 19:53:00 +00001853 // FIXME: extended qualifiers?
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001854 llvm::SmallVector<unsigned, 4> QualifierUnion;
Douglas Gregor20b3e992009-08-24 17:42:35 +00001855 llvm::SmallVector<std::pair<const Type *, const Type *>, 4> MemberOfClass;
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001856 QualType Composite1 = T1, Composite2 = T2;
Douglas Gregor20b3e992009-08-24 17:42:35 +00001857 do {
1858 const PointerType *Ptr1, *Ptr2;
1859 if ((Ptr1 = Composite1->getAs<PointerType>()) &&
1860 (Ptr2 = Composite2->getAs<PointerType>())) {
1861 Composite1 = Ptr1->getPointeeType();
1862 Composite2 = Ptr2->getPointeeType();
1863 QualifierUnion.push_back(
1864 Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
1865 MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0));
1866 continue;
1867 }
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Douglas Gregor20b3e992009-08-24 17:42:35 +00001869 const MemberPointerType *MemPtr1, *MemPtr2;
1870 if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) &&
1871 (MemPtr2 = Composite2->getAs<MemberPointerType>())) {
1872 Composite1 = MemPtr1->getPointeeType();
1873 Composite2 = MemPtr2->getPointeeType();
1874 QualifierUnion.push_back(
1875 Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers());
1876 MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(),
1877 MemPtr2->getClass()));
1878 continue;
1879 }
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Douglas Gregor20b3e992009-08-24 17:42:35 +00001881 // FIXME: block pointer types?
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Douglas Gregor20b3e992009-08-24 17:42:35 +00001883 // Cannot unwrap any more types.
1884 break;
1885 } while (true);
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Douglas Gregor20b3e992009-08-24 17:42:35 +00001887 // Rewrap the composites as pointers or member pointers with the union CVRs.
1888 llvm::SmallVector<std::pair<const Type *, const Type *>, 4>::iterator MOC
1889 = MemberOfClass.begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001890 for (llvm::SmallVector<unsigned, 4>::iterator
Douglas Gregor20b3e992009-08-24 17:42:35 +00001891 I = QualifierUnion.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001892 E = QualifierUnion.end();
Douglas Gregor20b3e992009-08-24 17:42:35 +00001893 I != E; (void)++I, ++MOC) {
John McCall0953e762009-09-24 19:53:00 +00001894 Qualifiers Quals = Qualifiers::fromCVRMask(*I);
Douglas Gregor20b3e992009-08-24 17:42:35 +00001895 if (MOC->first && MOC->second) {
1896 // Rebuild member pointer type
John McCall0953e762009-09-24 19:53:00 +00001897 Composite1 = Context.getMemberPointerType(
1898 Context.getQualifiedType(Composite1, Quals),
1899 MOC->first);
1900 Composite2 = Context.getMemberPointerType(
1901 Context.getQualifiedType(Composite2, Quals),
1902 MOC->second);
Douglas Gregor20b3e992009-08-24 17:42:35 +00001903 } else {
1904 // Rebuild pointer type
John McCall0953e762009-09-24 19:53:00 +00001905 Composite1
1906 = Context.getPointerType(Context.getQualifiedType(Composite1, Quals));
1907 Composite2
1908 = Context.getPointerType(Context.getQualifiedType(Composite2, Quals));
Douglas Gregor20b3e992009-08-24 17:42:35 +00001909 }
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001910 }
1911
Mike Stump1eb44332009-09-09 15:08:12 +00001912 ImplicitConversionSequence E1ToC1 =
Anders Carlssonda7a18b2009-08-27 17:24:15 +00001913 TryImplicitConversion(E1, Composite1,
1914 /*SuppressUserConversions=*/false,
1915 /*AllowExplicit=*/false,
Anders Carlsson08972922009-08-28 15:33:32 +00001916 /*ForceRValue=*/false,
1917 /*InOverloadResolution=*/false);
Mike Stump1eb44332009-09-09 15:08:12 +00001918 ImplicitConversionSequence E2ToC1 =
Anders Carlssonda7a18b2009-08-27 17:24:15 +00001919 TryImplicitConversion(E2, Composite1,
1920 /*SuppressUserConversions=*/false,
1921 /*AllowExplicit=*/false,
Anders Carlsson08972922009-08-28 15:33:32 +00001922 /*ForceRValue=*/false,
1923 /*InOverloadResolution=*/false);
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001925 ImplicitConversionSequence E1ToC2, E2ToC2;
1926 E1ToC2.ConversionKind = ImplicitConversionSequence::BadConversion;
1927 E2ToC2.ConversionKind = ImplicitConversionSequence::BadConversion;
1928 if (Context.getCanonicalType(Composite1) !=
1929 Context.getCanonicalType(Composite2)) {
Anders Carlssonda7a18b2009-08-27 17:24:15 +00001930 E1ToC2 = TryImplicitConversion(E1, Composite2,
1931 /*SuppressUserConversions=*/false,
1932 /*AllowExplicit=*/false,
Anders Carlsson08972922009-08-28 15:33:32 +00001933 /*ForceRValue=*/false,
1934 /*InOverloadResolution=*/false);
Anders Carlssonda7a18b2009-08-27 17:24:15 +00001935 E2ToC2 = TryImplicitConversion(E2, Composite2,
1936 /*SuppressUserConversions=*/false,
1937 /*AllowExplicit=*/false,
Anders Carlsson08972922009-08-28 15:33:32 +00001938 /*ForceRValue=*/false,
1939 /*InOverloadResolution=*/false);
Sebastian Redld1bd7fc2009-04-19 19:26:31 +00001940 }
1941
1942 bool ToC1Viable = E1ToC1.ConversionKind !=
1943 ImplicitConversionSequence::BadConversion
1944 && E2ToC1.ConversionKind !=
1945 ImplicitConversionSequence::BadConversion;
1946 bool ToC2Viable = E1ToC2.ConversionKind !=
1947 ImplicitConversionSequence::BadConversion
1948 && E2ToC2.ConversionKind !=
1949 ImplicitConversionSequence::BadConversion;
1950 if (ToC1Viable && !ToC2Viable) {
1951 if (!PerformImplicitConversion(E1, Composite1, E1ToC1, "converting") &&
1952 !PerformImplicitConversion(E2, Composite1, E2ToC1, "converting"))
1953 return Composite1;
1954 }
1955 if (ToC2Viable && !ToC1Viable) {
1956 if (!PerformImplicitConversion(E1, Composite2, E1ToC2, "converting") &&
1957 !PerformImplicitConversion(E2, Composite2, E2ToC2, "converting"))
1958 return Composite2;
1959 }
1960 return QualType();
1961}
Anders Carlsson165a0a02009-05-17 18:41:29 +00001962
Anders Carlssondef11992009-05-30 20:36:53 +00001963Sema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) {
Anders Carlsson089c2602009-08-15 23:41:35 +00001964 if (!Context.getLangOptions().CPlusPlus)
1965 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Ted Kremenek6217b802009-07-29 21:53:49 +00001967 const RecordType *RT = E->getType()->getAs<RecordType>();
Anders Carlssondef11992009-05-30 20:36:53 +00001968 if (!RT)
1969 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Anders Carlssondef11992009-05-30 20:36:53 +00001971 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1972 if (RD->hasTrivialDestructor())
1973 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Anders Carlsson283e4d52009-09-14 01:30:44 +00001975 if (CallExpr *CE = dyn_cast<CallExpr>(E)) {
1976 QualType Ty = CE->getCallee()->getType();
1977 if (const PointerType *PT = Ty->getAs<PointerType>())
1978 Ty = PT->getPointeeType();
1979
John McCall183700f2009-09-21 23:43:11 +00001980 const FunctionType *FTy = Ty->getAs<FunctionType>();
Anders Carlsson283e4d52009-09-14 01:30:44 +00001981 if (FTy->getResultType()->isReferenceType())
1982 return Owned(E);
1983 }
Mike Stump1eb44332009-09-09 15:08:12 +00001984 CXXTemporary *Temp = CXXTemporary::Create(Context,
Anders Carlssondef11992009-05-30 20:36:53 +00001985 RD->getDestructor(Context));
Anders Carlsson860306e2009-05-30 21:21:49 +00001986 ExprTemporaries.push_back(Temp);
Fariborz Jahaniana83f7ed2009-08-03 19:13:25 +00001987 if (CXXDestructorDecl *Destructor =
1988 const_cast<CXXDestructorDecl*>(RD->getDestructor(Context)))
1989 MarkDeclarationReferenced(E->getExprLoc(), Destructor);
Anders Carlssondef11992009-05-30 20:36:53 +00001990 // FIXME: Add the temporary to the temporaries vector.
1991 return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E));
1992}
1993
Mike Stump1eb44332009-09-09 15:08:12 +00001994Expr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr,
Anders Carlssonf54741e2009-06-16 03:37:31 +00001995 bool ShouldDestroyTemps) {
Anders Carlsson99ba36d2009-06-05 15:38:08 +00001996 assert(SubExpr && "sub expression can't be null!");
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Anders Carlsson99ba36d2009-06-05 15:38:08 +00001998 if (ExprTemporaries.empty())
1999 return SubExpr;
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Anders Carlsson99ba36d2009-06-05 15:38:08 +00002001 Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr,
Mike Stump1eb44332009-09-09 15:08:12 +00002002 &ExprTemporaries[0],
Anders Carlsson99ba36d2009-06-05 15:38:08 +00002003 ExprTemporaries.size(),
Anders Carlssonf54741e2009-06-16 03:37:31 +00002004 ShouldDestroyTemps);
Anders Carlsson99ba36d2009-06-05 15:38:08 +00002005 ExprTemporaries.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Anders Carlsson99ba36d2009-06-05 15:38:08 +00002007 return E;
2008}
2009
Mike Stump1eb44332009-09-09 15:08:12 +00002010Sema::OwningExprResult
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002011Sema::ActOnStartCXXMemberReference(Scope *S, ExprArg Base, SourceLocation OpLoc,
2012 tok::TokenKind OpKind, TypeTy *&ObjectType) {
2013 // Since this might be a postfix expression, get rid of ParenListExprs.
2014 Base = MaybeConvertParenListExprToParenExpr(S, move(Base));
Mike Stump1eb44332009-09-09 15:08:12 +00002015
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002016 Expr *BaseExpr = (Expr*)Base.get();
2017 assert(BaseExpr && "no record expansion");
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002019 QualType BaseType = BaseExpr->getType();
2020 if (BaseType->isDependentType()) {
2021 // FIXME: member of the current instantiation
2022 ObjectType = BaseType.getAsOpaquePtr();
2023 return move(Base);
2024 }
Mike Stump1eb44332009-09-09 15:08:12 +00002025
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002026 // C++ [over.match.oper]p8:
Mike Stump1eb44332009-09-09 15:08:12 +00002027 // [...] When operator->returns, the operator-> is applied to the value
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002028 // returned, with the original second operand.
2029 if (OpKind == tok::arrow) {
John McCallc4e83212009-09-30 01:01:30 +00002030 // The set of types we've considered so far.
John McCall432887f2009-09-30 01:30:54 +00002031 llvm::SmallPtrSet<CanQualType,8> CTypes;
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +00002032 llvm::SmallVector<SourceLocation, 8> Locations;
John McCall432887f2009-09-30 01:30:54 +00002033 CTypes.insert(Context.getCanonicalType(BaseType));
John McCallc4e83212009-09-30 01:01:30 +00002034
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002035 while (BaseType->isRecordType()) {
Anders Carlsson15ea3782009-10-13 22:43:21 +00002036 Base = BuildOverloadedArrowExpr(S, move(Base), OpLoc);
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002037 BaseExpr = (Expr*)Base.get();
2038 if (BaseExpr == NULL)
2039 return ExprError();
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +00002040 if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(BaseExpr))
Anders Carlssonde699e52009-10-13 22:55:59 +00002041 Locations.push_back(OpCall->getDirectCallee()->getLocation());
John McCallc4e83212009-09-30 01:01:30 +00002042 BaseType = BaseExpr->getType();
2043 CanQualType CBaseType = Context.getCanonicalType(BaseType);
John McCall432887f2009-09-30 01:30:54 +00002044 if (!CTypes.insert(CBaseType)) {
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +00002045 Diag(OpLoc, diag::err_operator_arrow_circular);
Fariborz Jahanian7a8233a2009-09-30 17:46:20 +00002046 for (unsigned i = 0; i < Locations.size(); i++)
2047 Diag(Locations[i], diag::note_declared_at);
Fariborz Jahanian4a4e3452009-09-30 00:19:41 +00002048 return ExprError();
2049 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002050 }
2051 }
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002053 if (BaseType->isPointerType())
2054 BaseType = BaseType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00002055
2056 // We could end up with various non-record types here, such as extended
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002057 // vector types or Objective-C interfaces. Just return early and let
2058 // ActOnMemberReferenceExpr do the work.
Douglas Gregorc68afe22009-09-03 21:38:09 +00002059 if (!BaseType->isRecordType()) {
2060 // C++ [basic.lookup.classref]p2:
2061 // [...] If the type of the object expression is of pointer to scalar
2062 // type, the unqualified-id is looked up in the context of the complete
2063 // postfix-expression.
2064 ObjectType = 0;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002065 return move(Base);
Douglas Gregorc68afe22009-09-03 21:38:09 +00002066 }
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Douglas Gregorc68afe22009-09-03 21:38:09 +00002068 // C++ [basic.lookup.classref]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00002069 // If the id-expression in a class member access (5.2.5) is an
Douglas Gregorc68afe22009-09-03 21:38:09 +00002070 // unqualified-id, and the type of the object expres- sion is of a class
2071 // type C (or of pointer to a class type C), the unqualified-id is looked
2072 // up in the scope of class C. [...]
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002073 ObjectType = BaseType.getAsOpaquePtr();
Mike Stump1eb44332009-09-09 15:08:12 +00002074 return move(Base);
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002075}
2076
Anders Carlssonec773872009-08-25 23:46:41 +00002077Sema::OwningExprResult
Anders Carlsson3aa4ca42009-08-26 17:36:19 +00002078Sema::ActOnDestructorReferenceExpr(Scope *S, ExprArg Base,
Anders Carlssonec773872009-08-25 23:46:41 +00002079 SourceLocation OpLoc,
2080 tok::TokenKind OpKind,
2081 SourceLocation ClassNameLoc,
2082 IdentifierInfo *ClassName,
Douglas Gregora78c5c32009-09-04 18:29:40 +00002083 const CXXScopeSpec &SS,
2084 bool HasTrailingLParen) {
2085 if (SS.isInvalid())
Anders Carlssonec773872009-08-25 23:46:41 +00002086 return ExprError();
Anders Carlsson2cf738f2009-08-26 19:22:42 +00002087
Douglas Gregora71d8192009-09-04 17:36:40 +00002088 QualType BaseType;
Douglas Gregora78c5c32009-09-04 18:29:40 +00002089 if (isUnknownSpecialization(SS))
2090 BaseType = Context.getTypenameType((NestedNameSpecifier *)SS.getScopeRep(),
Douglas Gregora71d8192009-09-04 17:36:40 +00002091 ClassName);
2092 else {
Douglas Gregora78c5c32009-09-04 18:29:40 +00002093 TypeTy *BaseTy = getTypeName(*ClassName, ClassNameLoc, S, &SS);
Douglas Gregora71d8192009-09-04 17:36:40 +00002094 if (!BaseTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00002095 Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type)
Douglas Gregora71d8192009-09-04 17:36:40 +00002096 << ClassName;
2097 return ExprError();
2098 }
Mike Stump1eb44332009-09-09 15:08:12 +00002099
Douglas Gregora71d8192009-09-04 17:36:40 +00002100 BaseType = GetTypeFromParser(BaseTy);
Anders Carlsson2cf738f2009-08-26 19:22:42 +00002101 }
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Anders Carlsson2cf738f2009-08-26 19:22:42 +00002103 CanQualType CanBaseType = Context.getCanonicalType(BaseType);
Mike Stump1eb44332009-09-09 15:08:12 +00002104 DeclarationName DtorName =
Anders Carlsson2cf738f2009-08-26 19:22:42 +00002105 Context.DeclarationNames.getCXXDestructorName(CanBaseType);
2106
Douglas Gregora78c5c32009-09-04 18:29:40 +00002107 OwningExprResult Result
2108 = BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, ClassNameLoc,
2109 DtorName, DeclPtrTy(), &SS);
2110 if (Result.isInvalid() || HasTrailingLParen)
2111 return move(Result);
Mike Stump1eb44332009-09-09 15:08:12 +00002112
2113 // The only way a reference to a destructor can be used is to
Douglas Gregora78c5c32009-09-04 18:29:40 +00002114 // immediately call them. Since the next token is not a '(', produce a
2115 // diagnostic and build the call now.
2116 Expr *E = (Expr *)Result.get();
2117 SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(E->getLocEnd());
2118 Diag(E->getLocStart(), diag::err_dtor_expr_without_call)
2119 << isa<CXXPseudoDestructorExpr>(E)
2120 << CodeModificationHint::CreateInsertion(ExpectedLParenLoc, "()");
Mike Stump1eb44332009-09-09 15:08:12 +00002121
2122 return ActOnCallExpr(0, move(Result), ExpectedLParenLoc,
Douglas Gregora78c5c32009-09-04 18:29:40 +00002123 MultiExprArg(*this, 0, 0), 0, ExpectedLParenLoc);
Anders Carlssonec773872009-08-25 23:46:41 +00002124}
2125
Douglas Gregora6f0f9d2009-08-31 19:52:13 +00002126Sema::OwningExprResult
2127Sema::ActOnOverloadedOperatorReferenceExpr(Scope *S, ExprArg Base,
2128 SourceLocation OpLoc,
2129 tok::TokenKind OpKind,
2130 SourceLocation ClassNameLoc,
2131 OverloadedOperatorKind OverOpKind,
2132 const CXXScopeSpec *SS) {
2133 if (SS && SS->isInvalid())
2134 return ExprError();
2135
2136 DeclarationName Name =
2137 Context.DeclarationNames.getCXXOperatorName(OverOpKind);
2138
2139 return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, ClassNameLoc,
2140 Name, DeclPtrTy(), SS);
2141}
2142
2143Sema::OwningExprResult
2144Sema::ActOnConversionOperatorReferenceExpr(Scope *S, ExprArg Base,
2145 SourceLocation OpLoc,
2146 tok::TokenKind OpKind,
2147 SourceLocation ClassNameLoc,
2148 TypeTy *Ty,
2149 const CXXScopeSpec *SS) {
2150 if (SS && SS->isInvalid())
2151 return ExprError();
2152
2153 //FIXME: Preserve type source info.
2154 QualType ConvType = GetTypeFromParser(Ty);
2155 CanQualType ConvTypeCanon = Context.getCanonicalType(ConvType);
2156 DeclarationName ConvName =
2157 Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon);
2158
2159 return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, ClassNameLoc,
2160 ConvName, DeclPtrTy(), SS);
2161}
2162
Fariborz Jahanianb7400232009-09-28 23:23:40 +00002163CXXMemberCallExpr *Sema::BuildCXXMemberCallExpr(Expr *Exp,
2164 CXXMethodDecl *Method) {
2165 MemberExpr *ME =
2166 new (Context) MemberExpr(Exp, /*IsArrow=*/false, Method,
2167 SourceLocation(), Method->getType());
2168 QualType ResultType;
2169 if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(Method))
2170 ResultType = Conv->getConversionType().getNonReferenceType();
2171 else
2172 ResultType = Method->getResultType().getNonReferenceType();
2173
2174 CXXMemberCallExpr *CE =
2175 new (Context) CXXMemberCallExpr(Context, ME, 0, 0,
2176 ResultType,
2177 SourceLocation());
2178 return CE;
2179}
2180
Anders Carlsson0aebc812009-09-09 21:33:21 +00002181Sema::OwningExprResult Sema::BuildCXXCastArgument(SourceLocation CastLoc,
2182 QualType Ty,
2183 CastExpr::CastKind Kind,
2184 CXXMethodDecl *Method,
2185 ExprArg Arg) {
2186 Expr *From = Arg.takeAs<Expr>();
2187
2188 switch (Kind) {
2189 default: assert(0 && "Unhandled cast kind!");
2190 case CastExpr::CK_ConstructorConversion: {
Douglas Gregor39da0b82009-09-09 23:08:42 +00002191 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
2192
2193 if (CompleteConstructorCall(cast<CXXConstructorDecl>(Method),
2194 MultiExprArg(*this, (void **)&From, 1),
2195 CastLoc, ConstructorArgs))
2196 return ExprError();
2197
Anders Carlsson0aebc812009-09-09 21:33:21 +00002198 return BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method),
Douglas Gregor39da0b82009-09-09 23:08:42 +00002199 move_arg(ConstructorArgs));
Anders Carlsson0aebc812009-09-09 21:33:21 +00002200 }
2201
2202 case CastExpr::CK_UserDefinedConversion: {
Anders Carlssonaac6e3a2009-09-15 07:42:44 +00002203 assert(!From->getType()->isPointerType() && "Arg can't have pointer type!");
2204
2205 // Cast to base if needed.
2206 if (PerformObjectArgumentInitialization(From, Method))
2207 return ExprError();
2208
Fariborz Jahanianb7400232009-09-28 23:23:40 +00002209 // Create an implicit call expr that calls it.
2210 CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(From, Method);
Anders Carlsson0aebc812009-09-09 21:33:21 +00002211 return Owned(CE);
2212 }
Anders Carlsson0aebc812009-09-09 21:33:21 +00002213 }
2214}
2215
Anders Carlsson165a0a02009-05-17 18:41:29 +00002216Sema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) {
2217 Expr *FullExpr = Arg.takeAs<Expr>();
Anders Carlsson99ba36d2009-06-05 15:38:08 +00002218 if (FullExpr)
Mike Stump1eb44332009-09-09 15:08:12 +00002219 FullExpr = MaybeCreateCXXExprWithTemporaries(FullExpr,
Anders Carlssonf54741e2009-06-16 03:37:31 +00002220 /*ShouldDestroyTemps=*/true);
Anders Carlsson165a0a02009-05-17 18:41:29 +00002221
Anders Carlssonec773872009-08-25 23:46:41 +00002222
Anders Carlsson165a0a02009-05-17 18:41:29 +00002223 return Owned(FullExpr);
2224}