blob: 55317db5024b5576a2c68da8d962ce628b0eba72 [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"
15#include "clang/AST/ExprCXX.h"
Steve Naroff210679c2007-08-25 14:02:58 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +000017#include "clang/Parse/DeclSpec.h"
Argyrios Kyrtzidis4021a842008-10-06 23:16:35 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattner20c6b3b2009-01-27 18:30:58 +000019#include "clang/Basic/DiagnosticSema.h"
Sebastian Redlb5a57a62008-12-03 20:26:15 +000020#include "clang/Basic/TargetInfo.h"
Douglas Gregor3fc749d2008-12-23 00:26:44 +000021#include "llvm/ADT/STLExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Douglas Gregor487a75a2008-11-19 19:09:45 +000024/// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function
Douglas Gregor2def4832008-11-17 20:34:05 +000025/// name (e.g., operator void const *) as an expression. This is
26/// very similar to ActOnIdentifierExpr, except that instead of
27/// providing an identifier the parser provides the type of the
28/// conversion function.
Sebastian Redlcd965b92009-01-18 18:53:16 +000029Sema::OwningExprResult
Douglas Gregor487a75a2008-11-19 19:09:45 +000030Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc,
31 TypeTy *Ty, bool HasTrailingLParen,
32 const CXXScopeSpec &SS) {
Douglas Gregor2def4832008-11-17 20:34:05 +000033 QualType ConvType = QualType::getFromOpaquePtr(Ty);
34 QualType ConvTypeCanon = Context.getCanonicalType(ConvType);
35 DeclarationName ConvName
36 = Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon);
Sebastian Redlcd965b92009-01-18 18:53:16 +000037 return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen,
Douglas Gregor487a75a2008-11-19 19:09:45 +000038 &SS);
Douglas Gregor2def4832008-11-17 20:34:05 +000039}
Sebastian Redlc42e1182008-11-11 11:37:55 +000040
Douglas Gregor487a75a2008-11-19 19:09:45 +000041/// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator
Douglas Gregore94ca9e42008-11-18 14:39:36 +000042/// name (e.g., @c operator+ ) as an expression. This is very
43/// similar to ActOnIdentifierExpr, except that instead of providing
44/// an identifier the parser provides the kind of overloaded
45/// operator that was parsed.
Sebastian Redlcd965b92009-01-18 18:53:16 +000046Sema::OwningExprResult
Douglas Gregor487a75a2008-11-19 19:09:45 +000047Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc,
48 OverloadedOperatorKind Op,
49 bool HasTrailingLParen,
50 const CXXScopeSpec &SS) {
Douglas Gregore94ca9e42008-11-18 14:39:36 +000051 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op);
Douglas Gregor487a75a2008-11-19 19:09:45 +000052 return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS);
Douglas Gregore94ca9e42008-11-18 14:39:36 +000053}
54
Sebastian Redlc42e1182008-11-11 11:37:55 +000055/// ActOnCXXTypeidOfType - Parse typeid( type-id ).
56Action::ExprResult
57Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc,
58 bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
59 const NamespaceDecl *StdNs = GetStdNamespace();
Chris Lattner572af492008-11-20 05:51:55 +000060 if (!StdNs)
61 return Diag(OpLoc, diag::err_need_header_before_typeid);
62
63 IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info");
Steve Naroff133147d2009-01-28 16:09:22 +000064 Decl *TypeInfoDecl = LookupDecl(TypeInfoII, Decl::IDNS_Tag, 0, StdNs);
Sebastian Redlc42e1182008-11-11 11:37:55 +000065 RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl);
Chris Lattner572af492008-11-20 05:51:55 +000066 if (!TypeInfoRecordDecl)
67 return Diag(OpLoc, diag::err_need_header_before_typeid);
Sebastian Redlc42e1182008-11-11 11:37:55 +000068
69 QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl);
70
71 return new CXXTypeidExpr(isType, TyOrExpr, TypeInfoType.withConst(),
72 SourceRange(OpLoc, RParenLoc));
73}
74
Steve Naroff1b273c42007-09-16 14:56:35 +000075/// ActOnCXXBoolLiteral - Parse {true,false} literals.
Reid Spencer5f016e22007-07-11 17:01:13 +000076Action::ExprResult
Steve Naroff1b273c42007-09-16 14:56:35 +000077Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
Douglas Gregor2f639b92008-10-24 15:36:09 +000078 assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
Reid Spencer5f016e22007-07-11 17:01:13 +000079 "Unknown C++ Boolean value!");
Steve Naroff210679c2007-08-25 14:02:58 +000080 return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
Chris Lattner50dd2892008-02-26 00:51:44 +000082
83/// ActOnCXXThrow - Parse throw expressions.
84Action::ExprResult
85Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) {
86 return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc);
87}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000088
89Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) {
90 /// C++ 9.3.2: In the body of a non-static member function, the keyword this
91 /// is a non-lvalue expression whose value is the address of the object for
92 /// which the function is called.
93
94 if (!isa<FunctionDecl>(CurContext)) {
95 Diag(ThisLoc, diag::err_invalid_this_use);
96 return ExprResult(true);
97 }
98
99 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
100 if (MD->isInstance())
Douglas Gregor796da182008-11-04 14:32:21 +0000101 return new CXXThisExpr(ThisLoc, MD->getThisType(Context));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000102
103 return Diag(ThisLoc, diag::err_invalid_this_use);
104}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000105
106/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
107/// Can be interpreted either as function-style casting ("int(x)")
108/// or class type construction ("ClassType(x,y,z)")
109/// or creation of a value-initialized type ("int()").
110Action::ExprResult
111Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
112 SourceLocation LParenLoc,
113 ExprTy **ExprTys, unsigned NumExprs,
114 SourceLocation *CommaLocs,
115 SourceLocation RParenLoc) {
116 assert(TypeRep && "Missing type!");
117 QualType Ty = QualType::getFromOpaquePtr(TypeRep);
118 Expr **Exprs = (Expr**)ExprTys;
119 SourceLocation TyBeginLoc = TypeRange.getBegin();
120 SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
121
Douglas Gregor506ae412009-01-16 18:33:17 +0000122 // C++ [expr.type.conv]p1:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000123 // If the expression list is a single expression, the type conversion
124 // expression is equivalent (in definedness, and if defined in meaning) to the
125 // corresponding cast expression.
126 //
127 if (NumExprs == 1) {
128 if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
129 return true;
Douglas Gregor49badde2008-10-27 19:41:14 +0000130 return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc,
131 Exprs[0], RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000132 }
133
Douglas Gregor506ae412009-01-16 18:33:17 +0000134 if (const RecordType *RT = Ty->getAsRecordType()) {
135 CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
136
137 if (NumExprs > 1 || Record->hasUserDeclaredConstructor()) {
138 CXXConstructorDecl *Constructor
139 = PerformInitializationByConstructor(Ty, Exprs, NumExprs,
140 TypeRange.getBegin(),
141 SourceRange(TypeRange.getBegin(),
142 RParenLoc),
143 DeclarationName(),
144 IK_Direct);
145
146 if (!Constructor)
147 return true;
148
149 return new CXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc,
150 Exprs, NumExprs, RParenLoc);
151 }
152
153 // Fall through to value-initialize an object of class type that
154 // doesn't have a user-declared default constructor.
155 }
156
157 // C++ [expr.type.conv]p1:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000158 // If the expression list specifies more than a single value, the type shall
159 // be a class with a suitably declared constructor.
160 //
161 if (NumExprs > 1)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000162 return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg)
163 << FullRange;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000164
165 assert(NumExprs == 0 && "Expected 0 expressions");
166
Douglas Gregor506ae412009-01-16 18:33:17 +0000167 // C++ [expr.type.conv]p2:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000168 // The expression T(), where T is a simple-type-specifier for a non-array
169 // complete object type or the (possibly cv-qualified) void type, creates an
170 // rvalue of the specified type, which is value-initialized.
171 //
172 if (Ty->isArrayType())
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000173 return Diag(TyBeginLoc, diag::err_value_init_for_array_type) << FullRange;
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000174 if (!Ty->isDependentType() && !Ty->isVoidType() &&
175 DiagnoseIncompleteType(TyBeginLoc, Ty,
176 diag::err_invalid_incomplete_type_use, FullRange))
177 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000178
179 return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc);
180}
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000181
182
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000183/// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.:
184/// @code new (memory) int[size][4] @endcode
185/// or
186/// @code ::new Foo(23, "hello") @endcode
187/// For the interpretation of this heap of arguments, consult the base version.
188Action::ExprResult
189Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
190 SourceLocation PlacementLParen,
191 ExprTy **PlacementArgs, unsigned NumPlaceArgs,
192 SourceLocation PlacementRParen, bool ParenTypeId,
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000193 Declarator &D, SourceLocation ConstructorLParen,
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000194 ExprTy **ConstructorArgs, unsigned NumConsArgs,
195 SourceLocation ConstructorRParen)
196{
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000197 // FIXME: Throughout this function, we have rather bad location information.
198 // Implementing Declarator::getSourceRange() would go a long way toward
199 // fixing that.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000200
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000201 Expr *ArraySize = 0;
202 unsigned Skip = 0;
203 // If the specified type is an array, unwrap it and save the expression.
204 if (D.getNumTypeObjects() > 0 &&
205 D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
206 DeclaratorChunk &Chunk = D.getTypeObject(0);
207 if (Chunk.Arr.hasStatic)
208 return Diag(Chunk.Loc, diag::err_static_illegal_in_new);
209 if (!Chunk.Arr.NumElts)
210 return Diag(Chunk.Loc, diag::err_array_new_needs_size);
211 ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts);
212 Skip = 1;
213 }
214
215 QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, Skip);
216 if (D.getInvalidType())
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000217 return true;
218
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000219 if (CheckAllocatedType(AllocType, D))
220 return true;
221
222 QualType ResultType = Context.getPointerType(AllocType);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000223
224 // That every array dimension except the first is constant was already
225 // checked by the type check above.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000226
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000227 // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral
228 // or enumeration type with a non-negative value."
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000229 if (ArraySize) {
230 QualType SizeType = ArraySize->getType();
231 if (!SizeType->isIntegralType() && !SizeType->isEnumeralType())
232 return Diag(ArraySize->getSourceRange().getBegin(),
233 diag::err_array_size_not_integral)
234 << SizeType << ArraySize->getSourceRange();
235 // Let's see if this is a constant < 0. If so, we reject it out of hand.
236 // We don't care about special rules, so we tell the machinery it's not
237 // evaluated - it gives us a result in more cases.
238 llvm::APSInt Value;
239 if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) {
240 if (Value < llvm::APSInt(
241 llvm::APInt::getNullValue(Value.getBitWidth()), false))
242 return Diag(ArraySize->getSourceRange().getBegin(),
243 diag::err_typecheck_negative_array_size)
244 << ArraySize->getSourceRange();
245 }
246 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000247
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000248 FunctionDecl *OperatorNew = 0;
249 FunctionDecl *OperatorDelete = 0;
250 Expr **PlaceArgs = (Expr**)PlacementArgs;
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000251 if (FindAllocationFunctions(StartLoc, UseGlobal, AllocType, ArraySize,
252 PlaceArgs, NumPlaceArgs, OperatorNew,
253 OperatorDelete))
254 return true;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000255
256 bool Init = ConstructorLParen.isValid();
257 // --- Choosing a constructor ---
258 // C++ 5.3.4p15
259 // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid)
260 // the object is not initialized. If the object, or any part of it, is
261 // const-qualified, it's an error.
262 // 2) If T is a POD and there's an empty initializer, the object is value-
263 // initialized.
264 // 3) If T is a POD and there's one initializer argument, the object is copy-
265 // constructed.
266 // 4) If T is a POD and there's more initializer arguments, it's an error.
267 // 5) If T is not a POD, the initializer arguments are used as constructor
268 // arguments.
269 //
270 // Or by the C++0x formulation:
271 // 1) If there's no initializer, the object is default-initialized according
272 // to C++0x rules.
273 // 2) Otherwise, the object is direct-initialized.
274 CXXConstructorDecl *Constructor = 0;
275 Expr **ConsArgs = (Expr**)ConstructorArgs;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000276 if (const RecordType *RT = AllocType->getAsRecordType()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000277 // FIXME: This is incorrect for when there is an empty initializer and
278 // no user-defined constructor. Must zero-initialize, not default-construct.
279 Constructor = PerformInitializationByConstructor(
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000280 AllocType, ConsArgs, NumConsArgs,
281 D.getDeclSpec().getSourceRange().getBegin(),
282 SourceRange(D.getDeclSpec().getSourceRange().getBegin(),
283 ConstructorRParen),
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000284 RT->getDecl()->getDeclName(),
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000285 NumConsArgs != 0 ? IK_Direct : IK_Default);
286 if (!Constructor)
287 return true;
288 } else {
289 if (!Init) {
290 // FIXME: Check that no subpart is const.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000291 if (AllocType.isConstQualified()) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000292 Diag(StartLoc, diag::err_new_uninitialized_const)
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000293 << D.getSourceRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000294 return true;
295 }
296 } else if (NumConsArgs == 0) {
297 // Object is value-initialized. Do nothing.
298 } else if (NumConsArgs == 1) {
299 // Object is direct-initialized.
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000300 // FIXME: WHAT DeclarationName do we pass in here?
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000301 if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc,
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000302 DeclarationName() /*AllocType.getAsString()*/,
303 /*DirectInit=*/true))
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000304 return true;
305 } else {
306 Diag(StartLoc, diag::err_builtin_direct_init_more_than_one_arg)
307 << SourceRange(ConstructorLParen, ConstructorRParen);
308 }
309 }
310
311 // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16)
312
313 return new CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, NumPlaceArgs,
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000314 ParenTypeId, ArraySize, Constructor, Init,
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000315 ConsArgs, NumConsArgs, OperatorDelete, ResultType,
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000316 StartLoc, Init ? ConstructorRParen : SourceLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000317}
318
319/// CheckAllocatedType - Checks that a type is suitable as the allocated type
320/// in a new-expression.
321/// dimension off and stores the size expression in ArraySize.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000322bool Sema::CheckAllocatedType(QualType AllocType, const Declarator &D)
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000323{
324 // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
325 // abstract class type or array thereof.
326 // FIXME: We don't have abstract types yet.
327 // FIXME: Under C++ semantics, an incomplete object type is still an object
328 // type. This code assumes the C semantics, where it's not.
329 if (!AllocType->isObjectType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000330 unsigned type; // For the select in the message.
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000331 if (AllocType->isFunctionType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000332 type = 0;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000333 } else if(AllocType->isIncompleteType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000334 type = 1;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000335 } else {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000336 assert(AllocType->isReferenceType() && "What else could it be?");
337 type = 2;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000338 }
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000339 SourceRange TyR = D.getDeclSpec().getSourceRange();
340 // FIXME: This is very much a guess and won't work for, e.g., pointers.
341 if (D.getNumTypeObjects() > 0)
342 TyR.setEnd(D.getTypeObject(0).Loc);
343 Diag(TyR.getBegin(), diag::err_bad_new_type)
344 << AllocType.getAsString() << type << TyR;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000345 return true;
346 }
347
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000348 // Every dimension shall be of constant size.
349 unsigned i = 1;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000350 while (const ArrayType *Array = Context.getAsArrayType(AllocType)) {
351 if (!Array->isConstantArrayType()) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000352 Diag(D.getTypeObject(i).Loc, diag::err_new_array_nonconst)
353 << static_cast<Expr*>(D.getTypeObject(i).Arr.NumElts)->getSourceRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000354 return true;
355 }
356 AllocType = Array->getElementType();
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000357 ++i;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000358 }
359
360 return false;
361}
362
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000363/// FindAllocationFunctions - Finds the overloads of operator new and delete
364/// that are appropriate for the allocation.
365bool Sema::FindAllocationFunctions(SourceLocation StartLoc, bool UseGlobal,
366 QualType AllocType, bool IsArray,
367 Expr **PlaceArgs, unsigned NumPlaceArgs,
368 FunctionDecl *&OperatorNew,
369 FunctionDecl *&OperatorDelete)
370{
371 // --- Choosing an allocation function ---
372 // C++ 5.3.4p8 - 14 & 18
373 // 1) If UseGlobal is true, only look in the global scope. Else, also look
374 // in the scope of the allocated class.
375 // 2) If an array size is given, look for operator new[], else look for
376 // operator new.
377 // 3) The first argument is always size_t. Append the arguments from the
378 // placement form.
379 // FIXME: Also find the appropriate delete operator.
380
381 llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs);
382 // We don't care about the actual value of this argument.
383 // FIXME: Should the Sema create the expression and embed it in the syntax
384 // tree? Or should the consumer just recalculate the value?
385 AllocArgs[0] = new IntegerLiteral(llvm::APInt::getNullValue(
386 Context.Target.getPointerWidth(0)),
387 Context.getSizeType(),
388 SourceLocation());
389 std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1);
390
391 DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName(
392 IsArray ? OO_Array_New : OO_New);
393 if (AllocType->isRecordType() && !UseGlobal) {
Sebastian Redl7f662392008-12-04 22:20:51 +0000394 CXXRecordDecl *Record = cast<CXXRecordType>(AllocType->getAsRecordType())
395 ->getDecl();
396 // FIXME: We fail to find inherited overloads.
397 if (FindAllocationOverload(StartLoc, NewName, &AllocArgs[0],
398 AllocArgs.size(), Record, /*AllowMissing=*/true,
399 OperatorNew))
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000400 return true;
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000401 }
402 if (!OperatorNew) {
403 // Didn't find a member overload. Look for a global one.
404 DeclareGlobalNewDelete();
Sebastian Redl7f662392008-12-04 22:20:51 +0000405 DeclContext *TUDecl = Context.getTranslationUnitDecl();
406 if (FindAllocationOverload(StartLoc, NewName, &AllocArgs[0],
407 AllocArgs.size(), TUDecl, /*AllowMissing=*/false,
408 OperatorNew))
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000409 return true;
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000410 }
411
Sebastian Redl7f662392008-12-04 22:20:51 +0000412 // FIXME: This is leaked on error. But so much is currently in Sema that it's
413 // easier to clean it in one go.
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000414 AllocArgs[0]->Destroy(Context);
415 return false;
416}
417
Sebastian Redl7f662392008-12-04 22:20:51 +0000418/// FindAllocationOverload - Find an fitting overload for the allocation
419/// function in the specified scope.
420bool Sema::FindAllocationOverload(SourceLocation StartLoc, DeclarationName Name,
421 Expr** Args, unsigned NumArgs,
422 DeclContext *Ctx, bool AllowMissing,
423 FunctionDecl *&Operator)
424{
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000425 DeclContext::lookup_iterator Alloc, AllocEnd;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000426 llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Name);
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000427 if (Alloc == AllocEnd) {
Sebastian Redl7f662392008-12-04 22:20:51 +0000428 if (AllowMissing)
429 return false;
430 // FIXME: Bad location information.
431 return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
432 << Name << 0;
433 }
434
435 OverloadCandidateSet Candidates;
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000436 for (; Alloc != AllocEnd; ++Alloc) {
437 // Even member operator new/delete are implicitly treated as
438 // static, so don't use AddMemberCandidate.
439 if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc))
440 AddOverloadCandidate(Fn, Args, NumArgs, Candidates,
441 /*SuppressUserConversions=*/false);
Sebastian Redl7f662392008-12-04 22:20:51 +0000442 }
443
444 // Do the resolution.
445 OverloadCandidateSet::iterator Best;
446 switch(BestViableFunction(Candidates, Best)) {
447 case OR_Success: {
448 // Got one!
449 FunctionDecl *FnDecl = Best->Function;
450 // The first argument is size_t, and the first parameter must be size_t,
451 // too. This is checked on declaration and can be assumed. (It can't be
452 // asserted on, though, since invalid decls are left in there.)
453 for (unsigned i = 1; i < NumArgs; ++i) {
454 // FIXME: Passing word to diagnostic.
455 if (PerformCopyInitialization(Args[i-1],
456 FnDecl->getParamDecl(i)->getType(),
457 "passing"))
458 return true;
459 }
460 Operator = FnDecl;
461 return false;
462 }
463
464 case OR_No_Viable_Function:
465 if (AllowMissing)
466 return false;
467 // FIXME: Bad location information.
468 Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
469 << Name << (unsigned)Candidates.size();
470 PrintOverloadCandidates(Candidates, /*OnlyViable=*/false);
471 return true;
472
473 case OR_Ambiguous:
474 // FIXME: Bad location information.
475 Diag(StartLoc, diag::err_ovl_ambiguous_call)
476 << Name;
477 PrintOverloadCandidates(Candidates, /*OnlyViable=*/true);
478 return true;
479 }
480 assert(false && "Unreachable, bad result from BestViableFunction");
481 return true;
482}
483
484
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000485/// DeclareGlobalNewDelete - Declare the global forms of operator new and
486/// delete. These are:
487/// @code
488/// void* operator new(std::size_t) throw(std::bad_alloc);
489/// void* operator new[](std::size_t) throw(std::bad_alloc);
490/// void operator delete(void *) throw();
491/// void operator delete[](void *) throw();
492/// @endcode
493/// Note that the placement and nothrow forms of new are *not* implicitly
494/// declared. Their use requires including \<new\>.
495void Sema::DeclareGlobalNewDelete()
496{
497 if (GlobalNewDeleteDeclared)
498 return;
499 GlobalNewDeleteDeclared = true;
500
501 QualType VoidPtr = Context.getPointerType(Context.VoidTy);
502 QualType SizeT = Context.getSizeType();
503
504 // FIXME: Exception specifications are not added.
505 DeclareGlobalAllocationFunction(
506 Context.DeclarationNames.getCXXOperatorName(OO_New),
507 VoidPtr, SizeT);
508 DeclareGlobalAllocationFunction(
509 Context.DeclarationNames.getCXXOperatorName(OO_Array_New),
510 VoidPtr, SizeT);
511 DeclareGlobalAllocationFunction(
512 Context.DeclarationNames.getCXXOperatorName(OO_Delete),
513 Context.VoidTy, VoidPtr);
514 DeclareGlobalAllocationFunction(
515 Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
516 Context.VoidTy, VoidPtr);
517}
518
519/// DeclareGlobalAllocationFunction - Declares a single implicit global
520/// allocation function if it doesn't already exist.
521void Sema::DeclareGlobalAllocationFunction(DeclarationName Name,
522 QualType Return, QualType Argument)
523{
524 DeclContext *GlobalCtx = Context.getTranslationUnitDecl();
525
526 // Check if this function is already declared.
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000527 {
Douglas Gregor5cc37092008-12-23 22:05:29 +0000528 DeclContext::lookup_iterator Alloc, AllocEnd;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000529 for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name);
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000530 Alloc != AllocEnd; ++Alloc) {
531 // FIXME: Do we need to check for default arguments here?
532 FunctionDecl *Func = cast<FunctionDecl>(*Alloc);
533 if (Func->getNumParams() == 1 &&
534 Context.getCanonicalType(Func->getParamDecl(0)->getType()) == Argument)
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000535 return;
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000536 }
537 }
538
539 QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0);
540 FunctionDecl *Alloc =
541 FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000542 FnType, FunctionDecl::None, false,
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000543 SourceLocation());
544 Alloc->setImplicit();
545 ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000546 0, Argument, VarDecl::None, 0);
Ted Kremenekfc767612009-01-14 00:42:25 +0000547 Alloc->setParams(Context, &Param, 1);
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000548
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000549 // FIXME: Also add this declaration to the IdentifierResolver, but
550 // make sure it is at the end of the chain to coincide with the
551 // global scope.
Douglas Gregor482b77d2009-01-12 23:27:07 +0000552 ((DeclContext *)TUScope->getEntity())->addDecl(Alloc);
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000553}
554
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000555/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
556/// @code ::delete ptr; @endcode
557/// or
558/// @code delete [] ptr; @endcode
559Action::ExprResult
560Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
561 bool ArrayForm, ExprTy *Operand)
562{
563 // C++ 5.3.5p1: "The operand shall have a pointer type, or a class type
564 // having a single conversion function to a pointer type. The result has
565 // type void."
566 // DR599 amends "pointer type" to "pointer to object type" in both cases.
567
568 Expr *Ex = (Expr *)Operand;
569 QualType Type = Ex->getType();
570
571 if (Type->isRecordType()) {
572 // FIXME: Find that one conversion function and amend the type.
573 }
574
575 if (!Type->isPointerType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000576 Diag(StartLoc, diag::err_delete_operand) << Type << Ex->getSourceRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000577 return true;
578 }
579
580 QualType Pointee = Type->getAsPointerType()->getPointeeType();
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000581 if (!Pointee->isVoidType() &&
582 DiagnoseIncompleteType(StartLoc, Pointee, diag::warn_delete_incomplete,
583 Ex->getSourceRange()))
584 return true;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000585 else if (!Pointee->isObjectType()) {
586 Diag(StartLoc, diag::err_delete_operand)
Chris Lattnerd1625842008-11-24 06:25:27 +0000587 << Type << Ex->getSourceRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000588 return true;
589 }
590
591 // FIXME: Look up the correct operator delete overload and pass a pointer
592 // along.
593 // FIXME: Check access and ambiguity of operator delete and destructor.
594
595 return new CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 0, Ex,
596 StartLoc);
597}
598
599
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000600/// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a
601/// C++ if/switch/while/for statement.
602/// e.g: "if (int x = f()) {...}"
603Action::ExprResult
604Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
605 Declarator &D,
606 SourceLocation EqualLoc,
607 ExprTy *AssignExprVal) {
608 assert(AssignExprVal && "Null assignment expression");
609
610 // C++ 6.4p2:
611 // The declarator shall not specify a function or an array.
612 // The type-specifier-seq shall not contain typedef and shall not declare a
613 // new class or enumeration.
614
615 assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
616 "Parser allowed 'typedef' as storage class of condition decl.");
617
618 QualType Ty = GetTypeForDeclarator(D, S);
619
620 if (Ty->isFunctionType()) { // The declarator shall not specify a function...
621 // We exit without creating a CXXConditionDeclExpr because a FunctionDecl
622 // would be created and CXXConditionDeclExpr wants a VarDecl.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000623 return Diag(StartLoc, diag::err_invalid_use_of_function_type)
624 << SourceRange(StartLoc, EqualLoc);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000625 } else if (Ty->isArrayType()) { // ...or an array.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000626 Diag(StartLoc, diag::err_invalid_use_of_array_type)
627 << SourceRange(StartLoc, EqualLoc);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000628 } else if (const RecordType *RT = Ty->getAsRecordType()) {
629 RecordDecl *RD = RT->getDecl();
630 // The type-specifier-seq shall not declare a new class...
631 if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD)))
632 Diag(RD->getLocation(), diag::err_type_defined_in_condition);
633 } else if (const EnumType *ET = Ty->getAsEnumType()) {
634 EnumDecl *ED = ET->getDecl();
635 // ...or enumeration.
636 if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED)))
637 Diag(ED->getLocation(), diag::err_type_defined_in_condition);
638 }
639
640 DeclTy *Dcl = ActOnDeclarator(S, D, 0);
641 if (!Dcl)
642 return true;
Sebastian Redl798d1192008-12-13 16:23:55 +0000643 AddInitializerToDecl(Dcl, ExprArg(*this, AssignExprVal));
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000644
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000645 // Mark this variable as one that is declared within a conditional.
646 if (VarDecl *VD = dyn_cast<VarDecl>((Decl *)Dcl))
647 VD->setDeclaredInCondition(true);
648
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000649 return new CXXConditionDeclExpr(StartLoc, EqualLoc,
650 cast<VarDecl>(static_cast<Decl *>(Dcl)));
651}
652
653/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
654bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) {
655 // C++ 6.4p4:
656 // The value of a condition that is an initialized declaration in a statement
657 // other than a switch statement is the value of the declared variable
658 // implicitly converted to type bool. If that conversion is ill-formed, the
659 // program is ill-formed.
660 // The value of a condition that is an expression is the value of the
661 // expression, implicitly converted to bool.
662 //
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000663 return PerformContextuallyConvertToBool(CondExpr);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000664}
Douglas Gregor77a52232008-09-12 00:47:35 +0000665
666/// Helper function to determine whether this is the (deprecated) C++
667/// conversion from a string literal to a pointer to non-const char or
668/// non-const wchar_t (for narrow and wide string literals,
669/// respectively).
670bool
671Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
672 // Look inside the implicit cast, if it exists.
673 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
674 From = Cast->getSubExpr();
675
676 // A string literal (2.13.4) that is not a wide string literal can
677 // be converted to an rvalue of type "pointer to char"; a wide
678 // string literal can be converted to an rvalue of type "pointer
679 // to wchar_t" (C++ 4.2p2).
680 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From))
681 if (const PointerType *ToPtrType = ToType->getAsPointerType())
682 if (const BuiltinType *ToPointeeType
683 = ToPtrType->getPointeeType()->getAsBuiltinType()) {
684 // This conversion is considered only when there is an
685 // explicit appropriate pointer target type (C++ 4.2p2).
686 if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 &&
687 ((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
688 (!StrLit->isWide() &&
689 (ToPointeeType->getKind() == BuiltinType::Char_U ||
690 ToPointeeType->getKind() == BuiltinType::Char_S))))
691 return true;
692 }
693
694 return false;
695}
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000696
697/// PerformImplicitConversion - Perform an implicit conversion of the
698/// expression From to the type ToType. Returns true if there was an
699/// error, false otherwise. The expression From is replaced with the
Douglas Gregor45920e82008-12-19 17:40:08 +0000700/// converted expression. Flavor is the kind of conversion we're
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000701/// performing, used in the error message. If @p AllowExplicit,
702/// explicit user-defined conversions are permitted.
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000703bool
Douglas Gregor45920e82008-12-19 17:40:08 +0000704Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000705 const char *Flavor, bool AllowExplicit)
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000706{
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000707 ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType, false,
708 AllowExplicit);
709 return PerformImplicitConversion(From, ToType, ICS, Flavor);
710}
711
712/// PerformImplicitConversion - Perform an implicit conversion of the
713/// expression From to the type ToType using the pre-computed implicit
714/// conversion sequence ICS. Returns true if there was an error, false
715/// otherwise. The expression From is replaced with the converted
716/// expression. Flavor is the kind of conversion we're performing,
717/// used in the error message.
718bool
719Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
720 const ImplicitConversionSequence &ICS,
721 const char* Flavor) {
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000722 switch (ICS.ConversionKind) {
723 case ImplicitConversionSequence::StandardConversion:
Douglas Gregor45920e82008-12-19 17:40:08 +0000724 if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor))
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000725 return true;
726 break;
727
728 case ImplicitConversionSequence::UserDefinedConversion:
729 // FIXME: This is, of course, wrong. We'll need to actually call
730 // the constructor or conversion operator, and then cope with the
731 // standard conversions.
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000732 ImpCastExprToType(From, ToType.getNonReferenceType(),
733 ToType->isReferenceType());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000734 return false;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000735
736 case ImplicitConversionSequence::EllipsisConversion:
737 assert(false && "Cannot perform an ellipsis conversion");
Douglas Gregor60d62c22008-10-31 16:23:19 +0000738 return false;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000739
740 case ImplicitConversionSequence::BadConversion:
741 return true;
742 }
743
744 // Everything went well.
745 return false;
746}
747
748/// PerformImplicitConversion - Perform an implicit conversion of the
749/// expression From to the type ToType by following the standard
750/// conversion sequence SCS. Returns true if there was an error, false
751/// otherwise. The expression From is replaced with the converted
Douglas Gregor45920e82008-12-19 17:40:08 +0000752/// expression. Flavor is the context in which we're performing this
753/// conversion, for use in error messages.
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000754bool
755Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Douglas Gregor45920e82008-12-19 17:40:08 +0000756 const StandardConversionSequence& SCS,
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000757 const char *Flavor) {
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000758 // Overall FIXME: we are recomputing too many types here and doing
759 // far too much extra work. What this means is that we need to keep
760 // track of more information that is computed when we try the
761 // implicit conversion initially, so that we don't need to recompute
762 // anything here.
763 QualType FromType = From->getType();
764
Douglas Gregor225c41e2008-11-03 19:09:14 +0000765 if (SCS.CopyConstructor) {
766 // FIXME: Create a temporary object by calling the copy
767 // constructor.
Douglas Gregor66b947f2009-01-16 19:38:23 +0000768 ImpCastExprToType(From, ToType.getNonReferenceType(),
769 ToType->isReferenceType());
Douglas Gregor225c41e2008-11-03 19:09:14 +0000770 return false;
771 }
772
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000773 // Perform the first implicit conversion.
774 switch (SCS.First) {
775 case ICK_Identity:
776 case ICK_Lvalue_To_Rvalue:
777 // Nothing to do.
778 break;
779
780 case ICK_Array_To_Pointer:
Douglas Gregor904eed32008-11-10 20:40:00 +0000781 if (FromType->isOverloadType()) {
782 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true);
783 if (!Fn)
784 return true;
785
786 FixOverloadedFunctionReference(From, Fn);
787 FromType = From->getType();
788 } else {
789 FromType = Context.getArrayDecayedType(FromType);
790 }
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000791 ImpCastExprToType(From, FromType);
792 break;
793
794 case ICK_Function_To_Pointer:
795 FromType = Context.getPointerType(FromType);
796 ImpCastExprToType(From, FromType);
797 break;
798
799 default:
800 assert(false && "Improper first standard conversion");
801 break;
802 }
803
804 // Perform the second implicit conversion
805 switch (SCS.Second) {
806 case ICK_Identity:
807 // Nothing to do.
808 break;
809
810 case ICK_Integral_Promotion:
811 case ICK_Floating_Promotion:
812 case ICK_Integral_Conversion:
813 case ICK_Floating_Conversion:
814 case ICK_Floating_Integral:
815 FromType = ToType.getUnqualifiedType();
816 ImpCastExprToType(From, FromType);
817 break;
818
819 case ICK_Pointer_Conversion:
Douglas Gregor45920e82008-12-19 17:40:08 +0000820 if (SCS.IncompatibleObjC) {
821 // Diagnose incompatible Objective-C conversions
822 Diag(From->getSourceRange().getBegin(),
823 diag::ext_typecheck_convert_incompatible_pointer)
824 << From->getType() << ToType << Flavor
825 << From->getSourceRange();
826 }
827
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000828 if (CheckPointerConversion(From, ToType))
829 return true;
830 ImpCastExprToType(From, ToType);
831 break;
832
833 case ICK_Pointer_Member:
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000834 if (CheckMemberPointerConversion(From, ToType))
835 return true;
836 ImpCastExprToType(From, ToType);
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000837 break;
838
839 case ICK_Boolean_Conversion:
840 FromType = Context.BoolTy;
841 ImpCastExprToType(From, FromType);
842 break;
843
844 default:
845 assert(false && "Improper second standard conversion");
846 break;
847 }
848
849 switch (SCS.Third) {
850 case ICK_Identity:
851 // Nothing to do.
852 break;
853
854 case ICK_Qualification:
Douglas Gregor66b947f2009-01-16 19:38:23 +0000855 ImpCastExprToType(From, ToType.getNonReferenceType(),
856 ToType->isReferenceType());
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000857 break;
858
859 default:
860 assert(false && "Improper second standard conversion");
861 break;
862 }
863
864 return false;
865}
866
Sebastian Redl64b45f72009-01-05 20:52:13 +0000867Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT,
868 SourceLocation KWLoc,
869 SourceLocation LParen,
870 TypeTy *Ty,
871 SourceLocation RParen) {
872 // FIXME: Some of the type traits have requirements. Interestingly, only the
873 // __is_base_of requirement is explicitly stated to be diagnosed. Indeed,
874 // G++ accepts __is_pod(Incomplete) without complaints, and claims that the
875 // type is indeed a POD.
876
877 // There is no point in eagerly computing the value. The traits are designed
878 // to be used from type trait templates, so Ty will be a template parameter
879 // 99% of the time.
880 return Owned(new UnaryTypeTraitExpr(KWLoc, OTT,
881 QualType::getFromOpaquePtr(Ty),
882 RParen, Context.BoolTy));
883}