blob: d0ceb97b3ae9a8a48f5caf104ec2b2e11e9da5cf [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroffac5d4f12007-08-25 14:02:58 +000016#include "clang/AST/ASTContext.h"
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +000017#include "clang/Parse/DeclSpec.h"
Argiris Kirtzidiscf4e8f82008-10-06 23:16:35 +000018#include "clang/Lex/Preprocessor.h"
Sebastian Redlb5ee8742008-12-03 20:26:15 +000019#include "clang/Basic/TargetInfo.h"
Douglas Gregorddfd9d52008-12-23 00:26:44 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Douglas Gregor24094772008-11-19 19:09:45 +000023/// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function
Douglas Gregorb0212bd2008-11-17 20:34:05 +000024/// name (e.g., operator void const *) as an expression. This is
25/// very similar to ActOnIdentifierExpr, except that instead of
26/// providing an identifier the parser provides the type of the
27/// conversion function.
Sebastian Redlcd883f72009-01-18 18:53:16 +000028Sema::OwningExprResult
Douglas Gregor24094772008-11-19 19:09:45 +000029Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc,
30 TypeTy *Ty, bool HasTrailingLParen,
31 const CXXScopeSpec &SS) {
Douglas Gregorb0212bd2008-11-17 20:34:05 +000032 QualType ConvType = QualType::getFromOpaquePtr(Ty);
33 QualType ConvTypeCanon = Context.getCanonicalType(ConvType);
34 DeclarationName ConvName
35 = Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon);
Sebastian Redlcd883f72009-01-18 18:53:16 +000036 return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen,
Douglas Gregor24094772008-11-19 19:09:45 +000037 &SS);
Douglas Gregorb0212bd2008-11-17 20:34:05 +000038}
Sebastian Redlb93b49c2008-11-11 11:37:55 +000039
Douglas Gregor24094772008-11-19 19:09:45 +000040/// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator
Douglas Gregor96a32dd2008-11-18 14:39:36 +000041/// name (e.g., @c operator+ ) as an expression. This is very
42/// similar to ActOnIdentifierExpr, except that instead of providing
43/// an identifier the parser provides the kind of overloaded
44/// operator that was parsed.
Sebastian Redlcd883f72009-01-18 18:53:16 +000045Sema::OwningExprResult
Douglas Gregor24094772008-11-19 19:09:45 +000046Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc,
47 OverloadedOperatorKind Op,
48 bool HasTrailingLParen,
49 const CXXScopeSpec &SS) {
Douglas Gregor96a32dd2008-11-18 14:39:36 +000050 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op);
Douglas Gregor24094772008-11-19 19:09:45 +000051 return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS);
Douglas Gregor96a32dd2008-11-18 14:39:36 +000052}
53
Sebastian Redlb93b49c2008-11-11 11:37:55 +000054/// ActOnCXXTypeidOfType - Parse typeid( type-id ).
55Action::ExprResult
56Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc,
57 bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
58 const NamespaceDecl *StdNs = GetStdNamespace();
Chris Lattnerc2a5f512008-11-20 05:51:55 +000059 if (!StdNs)
60 return Diag(OpLoc, diag::err_need_header_before_typeid);
61
62 IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info");
Steve Naroffc349ee22009-01-29 00:07:50 +000063 Decl *TypeInfoDecl = LookupDeclInContext(TypeInfoII, Decl::IDNS_Tag, StdNs);
Sebastian Redlb93b49c2008-11-11 11:37:55 +000064 RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl);
Chris Lattnerc2a5f512008-11-20 05:51:55 +000065 if (!TypeInfoRecordDecl)
66 return Diag(OpLoc, diag::err_need_header_before_typeid);
Sebastian Redlb93b49c2008-11-11 11:37:55 +000067
68 QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl);
69
70 return new CXXTypeidExpr(isType, TyOrExpr, TypeInfoType.withConst(),
71 SourceRange(OpLoc, RParenLoc));
72}
73
Steve Naroff5cbb02f2007-09-16 14:56:35 +000074/// ActOnCXXBoolLiteral - Parse {true,false} literals.
Chris Lattner4b009652007-07-25 00:24:17 +000075Action::ExprResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000076Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
Douglas Gregorf8e92702008-10-24 15:36:09 +000077 assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
Chris Lattner4b009652007-07-25 00:24:17 +000078 "Unknown C++ Boolean value!");
Steve Naroffac5d4f12007-08-25 14:02:58 +000079 return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000080}
Chris Lattnera7447ba2008-02-26 00:51:44 +000081
82/// ActOnCXXThrow - Parse throw expressions.
83Action::ExprResult
84Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) {
85 return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc);
86}
Argiris Kirtzidis38f16712008-07-01 10:37:29 +000087
88Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) {
89 /// C++ 9.3.2: In the body of a non-static member function, the keyword this
90 /// is a non-lvalue expression whose value is the address of the object for
91 /// which the function is called.
92
93 if (!isa<FunctionDecl>(CurContext)) {
94 Diag(ThisLoc, diag::err_invalid_this_use);
95 return ExprResult(true);
96 }
97
98 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
99 if (MD->isInstance())
Douglas Gregora5b022a2008-11-04 14:32:21 +0000100 return new CXXThisExpr(ThisLoc, MD->getThisType(Context));
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000101
102 return Diag(ThisLoc, diag::err_invalid_this_use);
103}
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000104
105/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
106/// Can be interpreted either as function-style casting ("int(x)")
107/// or class type construction ("ClassType(x,y,z)")
108/// or creation of a value-initialized type ("int()").
109Action::ExprResult
110Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
111 SourceLocation LParenLoc,
112 ExprTy **ExprTys, unsigned NumExprs,
113 SourceLocation *CommaLocs,
114 SourceLocation RParenLoc) {
115 assert(TypeRep && "Missing type!");
116 QualType Ty = QualType::getFromOpaquePtr(TypeRep);
117 Expr **Exprs = (Expr**)ExprTys;
118 SourceLocation TyBeginLoc = TypeRange.getBegin();
119 SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
120
Douglas Gregor861e7902009-01-16 18:33:17 +0000121 // C++ [expr.type.conv]p1:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000122 // If the expression list is a single expression, the type conversion
123 // expression is equivalent (in definedness, and if defined in meaning) to the
124 // corresponding cast expression.
125 //
126 if (NumExprs == 1) {
127 if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
128 return true;
Douglas Gregor21a04f32008-10-27 19:41:14 +0000129 return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc,
130 Exprs[0], RParenLoc);
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000131 }
132
Douglas Gregor861e7902009-01-16 18:33:17 +0000133 if (const RecordType *RT = Ty->getAsRecordType()) {
134 CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
135
136 if (NumExprs > 1 || Record->hasUserDeclaredConstructor()) {
137 CXXConstructorDecl *Constructor
138 = PerformInitializationByConstructor(Ty, Exprs, NumExprs,
139 TypeRange.getBegin(),
140 SourceRange(TypeRange.getBegin(),
141 RParenLoc),
142 DeclarationName(),
143 IK_Direct);
144
145 if (!Constructor)
146 return true;
147
148 return new CXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc,
149 Exprs, NumExprs, RParenLoc);
150 }
151
152 // Fall through to value-initialize an object of class type that
153 // doesn't have a user-declared default constructor.
154 }
155
156 // C++ [expr.type.conv]p1:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000157 // If the expression list specifies more than a single value, the type shall
158 // be a class with a suitably declared constructor.
159 //
160 if (NumExprs > 1)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000161 return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg)
162 << FullRange;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000163
164 assert(NumExprs == 0 && "Expected 0 expressions");
165
Douglas Gregor861e7902009-01-16 18:33:17 +0000166 // C++ [expr.type.conv]p2:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000167 // The expression T(), where T is a simple-type-specifier for a non-array
168 // complete object type or the (possibly cv-qualified) void type, creates an
169 // rvalue of the specified type, which is value-initialized.
170 //
171 if (Ty->isArrayType())
Chris Lattner9d2cf082008-11-19 05:27:50 +0000172 return Diag(TyBeginLoc, diag::err_value_init_for_array_type) << FullRange;
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000173 if (!Ty->isDependentType() && !Ty->isVoidType() &&
174 DiagnoseIncompleteType(TyBeginLoc, Ty,
175 diag::err_invalid_incomplete_type_use, FullRange))
176 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000177
178 return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc);
179}
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000180
181
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000182/// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.:
183/// @code new (memory) int[size][4] @endcode
184/// or
185/// @code ::new Foo(23, "hello") @endcode
186/// For the interpretation of this heap of arguments, consult the base version.
187Action::ExprResult
188Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
189 SourceLocation PlacementLParen,
190 ExprTy **PlacementArgs, unsigned NumPlaceArgs,
191 SourceLocation PlacementRParen, bool ParenTypeId,
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000192 Declarator &D, SourceLocation ConstructorLParen,
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000193 ExprTy **ConstructorArgs, unsigned NumConsArgs,
194 SourceLocation ConstructorRParen)
195{
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000196 // FIXME: Throughout this function, we have rather bad location information.
197 // Implementing Declarator::getSourceRange() would go a long way toward
198 // fixing that.
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000199
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000200 Expr *ArraySize = 0;
201 unsigned Skip = 0;
202 // If the specified type is an array, unwrap it and save the expression.
203 if (D.getNumTypeObjects() > 0 &&
204 D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
205 DeclaratorChunk &Chunk = D.getTypeObject(0);
206 if (Chunk.Arr.hasStatic)
207 return Diag(Chunk.Loc, diag::err_static_illegal_in_new);
208 if (!Chunk.Arr.NumElts)
209 return Diag(Chunk.Loc, diag::err_array_new_needs_size);
210 ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts);
211 Skip = 1;
212 }
213
214 QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, Skip);
215 if (D.getInvalidType())
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000216 return true;
217
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000218 if (CheckAllocatedType(AllocType, D))
219 return true;
220
221 QualType ResultType = Context.getPointerType(AllocType);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000222
223 // That every array dimension except the first is constant was already
224 // checked by the type check above.
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000225
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000226 // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral
227 // or enumeration type with a non-negative value."
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000228 if (ArraySize) {
229 QualType SizeType = ArraySize->getType();
230 if (!SizeType->isIntegralType() && !SizeType->isEnumeralType())
231 return Diag(ArraySize->getSourceRange().getBegin(),
232 diag::err_array_size_not_integral)
233 << SizeType << ArraySize->getSourceRange();
234 // Let's see if this is a constant < 0. If so, we reject it out of hand.
235 // We don't care about special rules, so we tell the machinery it's not
236 // evaluated - it gives us a result in more cases.
237 llvm::APSInt Value;
238 if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) {
239 if (Value < llvm::APSInt(
240 llvm::APInt::getNullValue(Value.getBitWidth()), false))
241 return Diag(ArraySize->getSourceRange().getBegin(),
242 diag::err_typecheck_negative_array_size)
243 << ArraySize->getSourceRange();
244 }
245 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000246
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000247 FunctionDecl *OperatorNew = 0;
248 FunctionDecl *OperatorDelete = 0;
249 Expr **PlaceArgs = (Expr**)PlacementArgs;
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000250 if (FindAllocationFunctions(StartLoc, UseGlobal, AllocType, ArraySize,
251 PlaceArgs, NumPlaceArgs, OperatorNew,
252 OperatorDelete))
253 return true;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000254
255 bool Init = ConstructorLParen.isValid();
256 // --- Choosing a constructor ---
257 // C++ 5.3.4p15
258 // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid)
259 // the object is not initialized. If the object, or any part of it, is
260 // const-qualified, it's an error.
261 // 2) If T is a POD and there's an empty initializer, the object is value-
262 // initialized.
263 // 3) If T is a POD and there's one initializer argument, the object is copy-
264 // constructed.
265 // 4) If T is a POD and there's more initializer arguments, it's an error.
266 // 5) If T is not a POD, the initializer arguments are used as constructor
267 // arguments.
268 //
269 // Or by the C++0x formulation:
270 // 1) If there's no initializer, the object is default-initialized according
271 // to C++0x rules.
272 // 2) Otherwise, the object is direct-initialized.
273 CXXConstructorDecl *Constructor = 0;
274 Expr **ConsArgs = (Expr**)ConstructorArgs;
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000275 if (const RecordType *RT = AllocType->getAsRecordType()) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000276 // FIXME: This is incorrect for when there is an empty initializer and
277 // no user-defined constructor. Must zero-initialize, not default-construct.
278 Constructor = PerformInitializationByConstructor(
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000279 AllocType, ConsArgs, NumConsArgs,
280 D.getDeclSpec().getSourceRange().getBegin(),
281 SourceRange(D.getDeclSpec().getSourceRange().getBegin(),
282 ConstructorRParen),
Chris Lattner271d4c22008-11-24 05:29:24 +0000283 RT->getDecl()->getDeclName(),
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000284 NumConsArgs != 0 ? IK_Direct : IK_Default);
285 if (!Constructor)
286 return true;
287 } else {
288 if (!Init) {
289 // FIXME: Check that no subpart is const.
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000290 if (AllocType.isConstQualified()) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000291 Diag(StartLoc, diag::err_new_uninitialized_const)
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000292 << D.getSourceRange();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000293 return true;
294 }
295 } else if (NumConsArgs == 0) {
296 // Object is value-initialized. Do nothing.
297 } else if (NumConsArgs == 1) {
298 // Object is direct-initialized.
Chris Lattner271d4c22008-11-24 05:29:24 +0000299 // FIXME: WHAT DeclarationName do we pass in here?
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000300 if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc,
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000301 DeclarationName() /*AllocType.getAsString()*/,
302 /*DirectInit=*/true))
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000303 return true;
304 } else {
305 Diag(StartLoc, diag::err_builtin_direct_init_more_than_one_arg)
306 << SourceRange(ConstructorLParen, ConstructorRParen);
307 }
308 }
309
310 // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16)
311
312 return new CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, NumPlaceArgs,
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000313 ParenTypeId, ArraySize, Constructor, Init,
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000314 ConsArgs, NumConsArgs, OperatorDelete, ResultType,
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000315 StartLoc, Init ? ConstructorRParen : SourceLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000316}
317
318/// CheckAllocatedType - Checks that a type is suitable as the allocated type
319/// in a new-expression.
320/// dimension off and stores the size expression in ArraySize.
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000321bool Sema::CheckAllocatedType(QualType AllocType, const Declarator &D)
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000322{
323 // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
324 // abstract class type or array thereof.
325 // FIXME: We don't have abstract types yet.
326 // FIXME: Under C++ semantics, an incomplete object type is still an object
327 // type. This code assumes the C semantics, where it's not.
328 if (!AllocType->isObjectType()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000329 unsigned type; // For the select in the message.
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000330 if (AllocType->isFunctionType()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000331 type = 0;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000332 } else if(AllocType->isIncompleteType()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000333 type = 1;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000334 } else {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000335 assert(AllocType->isReferenceType() && "What else could it be?");
336 type = 2;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000337 }
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000338 SourceRange TyR = D.getDeclSpec().getSourceRange();
339 // FIXME: This is very much a guess and won't work for, e.g., pointers.
340 if (D.getNumTypeObjects() > 0)
341 TyR.setEnd(D.getTypeObject(0).Loc);
342 Diag(TyR.getBegin(), diag::err_bad_new_type)
343 << AllocType.getAsString() << type << TyR;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000344 return true;
345 }
346
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000347 // Every dimension shall be of constant size.
348 unsigned i = 1;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000349 while (const ArrayType *Array = Context.getAsArrayType(AllocType)) {
350 if (!Array->isConstantArrayType()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000351 Diag(D.getTypeObject(i).Loc, diag::err_new_array_nonconst)
352 << static_cast<Expr*>(D.getTypeObject(i).Arr.NumElts)->getSourceRange();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000353 return true;
354 }
355 AllocType = Array->getElementType();
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000356 ++i;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000357 }
358
359 return false;
360}
361
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000362/// FindAllocationFunctions - Finds the overloads of operator new and delete
363/// that are appropriate for the allocation.
364bool Sema::FindAllocationFunctions(SourceLocation StartLoc, bool UseGlobal,
365 QualType AllocType, bool IsArray,
366 Expr **PlaceArgs, unsigned NumPlaceArgs,
367 FunctionDecl *&OperatorNew,
368 FunctionDecl *&OperatorDelete)
369{
370 // --- Choosing an allocation function ---
371 // C++ 5.3.4p8 - 14 & 18
372 // 1) If UseGlobal is true, only look in the global scope. Else, also look
373 // in the scope of the allocated class.
374 // 2) If an array size is given, look for operator new[], else look for
375 // operator new.
376 // 3) The first argument is always size_t. Append the arguments from the
377 // placement form.
378 // FIXME: Also find the appropriate delete operator.
379
380 llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs);
381 // We don't care about the actual value of this argument.
382 // FIXME: Should the Sema create the expression and embed it in the syntax
383 // tree? Or should the consumer just recalculate the value?
384 AllocArgs[0] = new IntegerLiteral(llvm::APInt::getNullValue(
385 Context.Target.getPointerWidth(0)),
386 Context.getSizeType(),
387 SourceLocation());
388 std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1);
389
390 DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName(
391 IsArray ? OO_Array_New : OO_New);
392 if (AllocType->isRecordType() && !UseGlobal) {
Sebastian Redlec5f3262008-12-04 22:20:51 +0000393 CXXRecordDecl *Record = cast<CXXRecordType>(AllocType->getAsRecordType())
394 ->getDecl();
395 // FIXME: We fail to find inherited overloads.
396 if (FindAllocationOverload(StartLoc, NewName, &AllocArgs[0],
397 AllocArgs.size(), Record, /*AllowMissing=*/true,
398 OperatorNew))
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000399 return true;
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000400 }
401 if (!OperatorNew) {
402 // Didn't find a member overload. Look for a global one.
403 DeclareGlobalNewDelete();
Sebastian Redlec5f3262008-12-04 22:20:51 +0000404 DeclContext *TUDecl = Context.getTranslationUnitDecl();
405 if (FindAllocationOverload(StartLoc, NewName, &AllocArgs[0],
406 AllocArgs.size(), TUDecl, /*AllowMissing=*/false,
407 OperatorNew))
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000408 return true;
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000409 }
410
Sebastian Redlec5f3262008-12-04 22:20:51 +0000411 // FIXME: This is leaked on error. But so much is currently in Sema that it's
412 // easier to clean it in one go.
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000413 AllocArgs[0]->Destroy(Context);
414 return false;
415}
416
Sebastian Redlec5f3262008-12-04 22:20:51 +0000417/// FindAllocationOverload - Find an fitting overload for the allocation
418/// function in the specified scope.
419bool Sema::FindAllocationOverload(SourceLocation StartLoc, DeclarationName Name,
420 Expr** Args, unsigned NumArgs,
421 DeclContext *Ctx, bool AllowMissing,
422 FunctionDecl *&Operator)
423{
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000424 DeclContext::lookup_iterator Alloc, AllocEnd;
Steve Naroffab63fd62009-01-08 17:28:14 +0000425 llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Name);
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000426 if (Alloc == AllocEnd) {
Sebastian Redlec5f3262008-12-04 22:20:51 +0000427 if (AllowMissing)
428 return false;
429 // FIXME: Bad location information.
430 return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
431 << Name << 0;
432 }
433
434 OverloadCandidateSet Candidates;
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000435 for (; Alloc != AllocEnd; ++Alloc) {
436 // Even member operator new/delete are implicitly treated as
437 // static, so don't use AddMemberCandidate.
438 if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc))
439 AddOverloadCandidate(Fn, Args, NumArgs, Candidates,
440 /*SuppressUserConversions=*/false);
Sebastian Redlec5f3262008-12-04 22:20:51 +0000441 }
442
443 // Do the resolution.
444 OverloadCandidateSet::iterator Best;
445 switch(BestViableFunction(Candidates, Best)) {
446 case OR_Success: {
447 // Got one!
448 FunctionDecl *FnDecl = Best->Function;
449 // The first argument is size_t, and the first parameter must be size_t,
450 // too. This is checked on declaration and can be assumed. (It can't be
451 // asserted on, though, since invalid decls are left in there.)
452 for (unsigned i = 1; i < NumArgs; ++i) {
453 // FIXME: Passing word to diagnostic.
454 if (PerformCopyInitialization(Args[i-1],
455 FnDecl->getParamDecl(i)->getType(),
456 "passing"))
457 return true;
458 }
459 Operator = FnDecl;
460 return false;
461 }
462
463 case OR_No_Viable_Function:
464 if (AllowMissing)
465 return false;
466 // FIXME: Bad location information.
467 Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
468 << Name << (unsigned)Candidates.size();
469 PrintOverloadCandidates(Candidates, /*OnlyViable=*/false);
470 return true;
471
472 case OR_Ambiguous:
473 // FIXME: Bad location information.
474 Diag(StartLoc, diag::err_ovl_ambiguous_call)
475 << Name;
476 PrintOverloadCandidates(Candidates, /*OnlyViable=*/true);
477 return true;
478 }
479 assert(false && "Unreachable, bad result from BestViableFunction");
480 return true;
481}
482
483
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000484/// DeclareGlobalNewDelete - Declare the global forms of operator new and
485/// delete. These are:
486/// @code
487/// void* operator new(std::size_t) throw(std::bad_alloc);
488/// void* operator new[](std::size_t) throw(std::bad_alloc);
489/// void operator delete(void *) throw();
490/// void operator delete[](void *) throw();
491/// @endcode
492/// Note that the placement and nothrow forms of new are *not* implicitly
493/// declared. Their use requires including \<new\>.
494void Sema::DeclareGlobalNewDelete()
495{
496 if (GlobalNewDeleteDeclared)
497 return;
498 GlobalNewDeleteDeclared = true;
499
500 QualType VoidPtr = Context.getPointerType(Context.VoidTy);
501 QualType SizeT = Context.getSizeType();
502
503 // FIXME: Exception specifications are not added.
504 DeclareGlobalAllocationFunction(
505 Context.DeclarationNames.getCXXOperatorName(OO_New),
506 VoidPtr, SizeT);
507 DeclareGlobalAllocationFunction(
508 Context.DeclarationNames.getCXXOperatorName(OO_Array_New),
509 VoidPtr, SizeT);
510 DeclareGlobalAllocationFunction(
511 Context.DeclarationNames.getCXXOperatorName(OO_Delete),
512 Context.VoidTy, VoidPtr);
513 DeclareGlobalAllocationFunction(
514 Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
515 Context.VoidTy, VoidPtr);
516}
517
518/// DeclareGlobalAllocationFunction - Declares a single implicit global
519/// allocation function if it doesn't already exist.
520void Sema::DeclareGlobalAllocationFunction(DeclarationName Name,
521 QualType Return, QualType Argument)
522{
523 DeclContext *GlobalCtx = Context.getTranslationUnitDecl();
524
525 // Check if this function is already declared.
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000526 {
Douglas Gregor7c865852008-12-23 22:05:29 +0000527 DeclContext::lookup_iterator Alloc, AllocEnd;
Steve Naroffab63fd62009-01-08 17:28:14 +0000528 for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name);
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000529 Alloc != AllocEnd; ++Alloc) {
530 // FIXME: Do we need to check for default arguments here?
531 FunctionDecl *Func = cast<FunctionDecl>(*Alloc);
532 if (Func->getNumParams() == 1 &&
533 Context.getCanonicalType(Func->getParamDecl(0)->getType()) == Argument)
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000534 return;
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000535 }
536 }
537
538 QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0);
539 FunctionDecl *Alloc =
540 FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000541 FnType, FunctionDecl::None, false,
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000542 SourceLocation());
543 Alloc->setImplicit();
544 ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000545 0, Argument, VarDecl::None, 0);
Ted Kremenek8494c962009-01-14 00:42:25 +0000546 Alloc->setParams(Context, &Param, 1);
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000547
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000548 // FIXME: Also add this declaration to the IdentifierResolver, but
549 // make sure it is at the end of the chain to coincide with the
550 // global scope.
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000551 ((DeclContext *)TUScope->getEntity())->addDecl(Alloc);
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000552}
553
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000554/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
555/// @code ::delete ptr; @endcode
556/// or
557/// @code delete [] ptr; @endcode
558Action::ExprResult
559Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
560 bool ArrayForm, ExprTy *Operand)
561{
562 // C++ 5.3.5p1: "The operand shall have a pointer type, or a class type
563 // having a single conversion function to a pointer type. The result has
564 // type void."
565 // DR599 amends "pointer type" to "pointer to object type" in both cases.
566
567 Expr *Ex = (Expr *)Operand;
568 QualType Type = Ex->getType();
569
570 if (Type->isRecordType()) {
571 // FIXME: Find that one conversion function and amend the type.
572 }
573
574 if (!Type->isPointerType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000575 Diag(StartLoc, diag::err_delete_operand) << Type << Ex->getSourceRange();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000576 return true;
577 }
578
579 QualType Pointee = Type->getAsPointerType()->getPointeeType();
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000580 if (!Pointee->isVoidType() &&
581 DiagnoseIncompleteType(StartLoc, Pointee, diag::warn_delete_incomplete,
582 Ex->getSourceRange()))
583 return true;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000584 else if (!Pointee->isObjectType()) {
585 Diag(StartLoc, diag::err_delete_operand)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000586 << Type << Ex->getSourceRange();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000587 return true;
588 }
589
590 // FIXME: Look up the correct operator delete overload and pass a pointer
591 // along.
592 // FIXME: Check access and ambiguity of operator delete and destructor.
593
594 return new CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 0, Ex,
595 StartLoc);
596}
597
598
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000599/// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a
600/// C++ if/switch/while/for statement.
601/// e.g: "if (int x = f()) {...}"
602Action::ExprResult
603Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
604 Declarator &D,
605 SourceLocation EqualLoc,
606 ExprTy *AssignExprVal) {
607 assert(AssignExprVal && "Null assignment expression");
608
609 // C++ 6.4p2:
610 // The declarator shall not specify a function or an array.
611 // The type-specifier-seq shall not contain typedef and shall not declare a
612 // new class or enumeration.
613
614 assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
615 "Parser allowed 'typedef' as storage class of condition decl.");
616
617 QualType Ty = GetTypeForDeclarator(D, S);
618
619 if (Ty->isFunctionType()) { // The declarator shall not specify a function...
620 // We exit without creating a CXXConditionDeclExpr because a FunctionDecl
621 // would be created and CXXConditionDeclExpr wants a VarDecl.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000622 return Diag(StartLoc, diag::err_invalid_use_of_function_type)
623 << SourceRange(StartLoc, EqualLoc);
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000624 } else if (Ty->isArrayType()) { // ...or an array.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000625 Diag(StartLoc, diag::err_invalid_use_of_array_type)
626 << SourceRange(StartLoc, EqualLoc);
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000627 } else if (const RecordType *RT = Ty->getAsRecordType()) {
628 RecordDecl *RD = RT->getDecl();
629 // The type-specifier-seq shall not declare a new class...
630 if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD)))
631 Diag(RD->getLocation(), diag::err_type_defined_in_condition);
632 } else if (const EnumType *ET = Ty->getAsEnumType()) {
633 EnumDecl *ED = ET->getDecl();
634 // ...or enumeration.
635 if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED)))
636 Diag(ED->getLocation(), diag::err_type_defined_in_condition);
637 }
638
639 DeclTy *Dcl = ActOnDeclarator(S, D, 0);
640 if (!Dcl)
641 return true;
Sebastian Redl91f9b0a2008-12-13 16:23:55 +0000642 AddInitializerToDecl(Dcl, ExprArg(*this, AssignExprVal));
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000643
Douglas Gregor48840c72008-12-10 23:01:14 +0000644 // Mark this variable as one that is declared within a conditional.
645 if (VarDecl *VD = dyn_cast<VarDecl>((Decl *)Dcl))
646 VD->setDeclaredInCondition(true);
647
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000648 return new CXXConditionDeclExpr(StartLoc, EqualLoc,
649 cast<VarDecl>(static_cast<Decl *>(Dcl)));
650}
651
652/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
653bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) {
654 // C++ 6.4p4:
655 // The value of a condition that is an initialized declaration in a statement
656 // other than a switch statement is the value of the declared variable
657 // implicitly converted to type bool. If that conversion is ill-formed, the
658 // program is ill-formed.
659 // The value of a condition that is an expression is the value of the
660 // expression, implicitly converted to bool.
661 //
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000662 return PerformContextuallyConvertToBool(CondExpr);
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000663}
Douglas Gregor1815b3b2008-09-12 00:47:35 +0000664
665/// Helper function to determine whether this is the (deprecated) C++
666/// conversion from a string literal to a pointer to non-const char or
667/// non-const wchar_t (for narrow and wide string literals,
668/// respectively).
669bool
670Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
671 // Look inside the implicit cast, if it exists.
672 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
673 From = Cast->getSubExpr();
674
675 // A string literal (2.13.4) that is not a wide string literal can
676 // be converted to an rvalue of type "pointer to char"; a wide
677 // string literal can be converted to an rvalue of type "pointer
678 // to wchar_t" (C++ 4.2p2).
679 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From))
680 if (const PointerType *ToPtrType = ToType->getAsPointerType())
681 if (const BuiltinType *ToPointeeType
682 = ToPtrType->getPointeeType()->getAsBuiltinType()) {
683 // This conversion is considered only when there is an
684 // explicit appropriate pointer target type (C++ 4.2p2).
685 if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 &&
686 ((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
687 (!StrLit->isWide() &&
688 (ToPointeeType->getKind() == BuiltinType::Char_U ||
689 ToPointeeType->getKind() == BuiltinType::Char_S))))
690 return true;
691 }
692
693 return false;
694}
Douglas Gregorbb461502008-10-24 04:54:22 +0000695
696/// PerformImplicitConversion - Perform an implicit conversion of the
697/// expression From to the type ToType. Returns true if there was an
698/// error, false otherwise. The expression From is replaced with the
Douglas Gregor6fd35572008-12-19 17:40:08 +0000699/// converted expression. Flavor is the kind of conversion we're
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000700/// performing, used in the error message. If @p AllowExplicit,
701/// explicit user-defined conversions are permitted.
Douglas Gregorbb461502008-10-24 04:54:22 +0000702bool
Douglas Gregor6fd35572008-12-19 17:40:08 +0000703Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000704 const char *Flavor, bool AllowExplicit)
Douglas Gregorbb461502008-10-24 04:54:22 +0000705{
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000706 ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType, false,
707 AllowExplicit);
708 return PerformImplicitConversion(From, ToType, ICS, Flavor);
709}
710
711/// PerformImplicitConversion - Perform an implicit conversion of the
712/// expression From to the type ToType using the pre-computed implicit
713/// conversion sequence ICS. Returns true if there was an error, false
714/// otherwise. The expression From is replaced with the converted
715/// expression. Flavor is the kind of conversion we're performing,
716/// used in the error message.
717bool
718Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
719 const ImplicitConversionSequence &ICS,
720 const char* Flavor) {
Douglas Gregorbb461502008-10-24 04:54:22 +0000721 switch (ICS.ConversionKind) {
722 case ImplicitConversionSequence::StandardConversion:
Douglas Gregor6fd35572008-12-19 17:40:08 +0000723 if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor))
Douglas Gregorbb461502008-10-24 04:54:22 +0000724 return true;
725 break;
726
727 case ImplicitConversionSequence::UserDefinedConversion:
728 // FIXME: This is, of course, wrong. We'll need to actually call
729 // the constructor or conversion operator, and then cope with the
730 // standard conversions.
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000731 ImpCastExprToType(From, ToType.getNonReferenceType(),
732 ToType->isReferenceType());
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000733 return false;
Douglas Gregorbb461502008-10-24 04:54:22 +0000734
735 case ImplicitConversionSequence::EllipsisConversion:
736 assert(false && "Cannot perform an ellipsis conversion");
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000737 return false;
Douglas Gregorbb461502008-10-24 04:54:22 +0000738
739 case ImplicitConversionSequence::BadConversion:
740 return true;
741 }
742
743 // Everything went well.
744 return false;
745}
746
747/// PerformImplicitConversion - Perform an implicit conversion of the
748/// expression From to the type ToType by following the standard
749/// conversion sequence SCS. Returns true if there was an error, false
750/// otherwise. The expression From is replaced with the converted
Douglas Gregor6fd35572008-12-19 17:40:08 +0000751/// expression. Flavor is the context in which we're performing this
752/// conversion, for use in error messages.
Douglas Gregorbb461502008-10-24 04:54:22 +0000753bool
754Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Douglas Gregor6fd35572008-12-19 17:40:08 +0000755 const StandardConversionSequence& SCS,
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000756 const char *Flavor) {
Douglas Gregorbb461502008-10-24 04:54:22 +0000757 // Overall FIXME: we are recomputing too many types here and doing
758 // far too much extra work. What this means is that we need to keep
759 // track of more information that is computed when we try the
760 // implicit conversion initially, so that we don't need to recompute
761 // anything here.
762 QualType FromType = From->getType();
763
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000764 if (SCS.CopyConstructor) {
765 // FIXME: Create a temporary object by calling the copy
766 // constructor.
Douglas Gregor5ac8ffa2009-01-16 19:38:23 +0000767 ImpCastExprToType(From, ToType.getNonReferenceType(),
768 ToType->isReferenceType());
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000769 return false;
770 }
771
Douglas Gregorbb461502008-10-24 04:54:22 +0000772 // Perform the first implicit conversion.
773 switch (SCS.First) {
774 case ICK_Identity:
775 case ICK_Lvalue_To_Rvalue:
776 // Nothing to do.
777 break;
778
779 case ICK_Array_To_Pointer:
Douglas Gregor45014fd2008-11-10 20:40:00 +0000780 if (FromType->isOverloadType()) {
781 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true);
782 if (!Fn)
783 return true;
784
785 FixOverloadedFunctionReference(From, Fn);
786 FromType = From->getType();
787 } else {
788 FromType = Context.getArrayDecayedType(FromType);
789 }
Douglas Gregorbb461502008-10-24 04:54:22 +0000790 ImpCastExprToType(From, FromType);
791 break;
792
793 case ICK_Function_To_Pointer:
794 FromType = Context.getPointerType(FromType);
795 ImpCastExprToType(From, FromType);
796 break;
797
798 default:
799 assert(false && "Improper first standard conversion");
800 break;
801 }
802
803 // Perform the second implicit conversion
804 switch (SCS.Second) {
805 case ICK_Identity:
806 // Nothing to do.
807 break;
808
809 case ICK_Integral_Promotion:
810 case ICK_Floating_Promotion:
811 case ICK_Integral_Conversion:
812 case ICK_Floating_Conversion:
813 case ICK_Floating_Integral:
814 FromType = ToType.getUnqualifiedType();
815 ImpCastExprToType(From, FromType);
816 break;
817
818 case ICK_Pointer_Conversion:
Douglas Gregor6fd35572008-12-19 17:40:08 +0000819 if (SCS.IncompatibleObjC) {
820 // Diagnose incompatible Objective-C conversions
821 Diag(From->getSourceRange().getBegin(),
822 diag::ext_typecheck_convert_incompatible_pointer)
823 << From->getType() << ToType << Flavor
824 << From->getSourceRange();
825 }
826
Douglas Gregorbb461502008-10-24 04:54:22 +0000827 if (CheckPointerConversion(From, ToType))
828 return true;
829 ImpCastExprToType(From, ToType);
830 break;
831
832 case ICK_Pointer_Member:
Sebastian Redlba387562009-01-25 19:43:20 +0000833 if (CheckMemberPointerConversion(From, ToType))
834 return true;
835 ImpCastExprToType(From, ToType);
Douglas Gregorbb461502008-10-24 04:54:22 +0000836 break;
837
838 case ICK_Boolean_Conversion:
839 FromType = Context.BoolTy;
840 ImpCastExprToType(From, FromType);
841 break;
842
843 default:
844 assert(false && "Improper second standard conversion");
845 break;
846 }
847
848 switch (SCS.Third) {
849 case ICK_Identity:
850 // Nothing to do.
851 break;
852
853 case ICK_Qualification:
Douglas Gregor5ac8ffa2009-01-16 19:38:23 +0000854 ImpCastExprToType(From, ToType.getNonReferenceType(),
855 ToType->isReferenceType());
Douglas Gregorbb461502008-10-24 04:54:22 +0000856 break;
857
858 default:
859 assert(false && "Improper second standard conversion");
860 break;
861 }
862
863 return false;
864}
865
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000866Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT,
867 SourceLocation KWLoc,
868 SourceLocation LParen,
869 TypeTy *Ty,
870 SourceLocation RParen) {
871 // FIXME: Some of the type traits have requirements. Interestingly, only the
872 // __is_base_of requirement is explicitly stated to be diagnosed. Indeed,
873 // G++ accepts __is_pod(Incomplete) without complaints, and claims that the
874 // type is indeed a POD.
875
876 // There is no point in eagerly computing the value. The traits are designed
877 // to be used from type trait templates, so Ty will be a template parameter
878 // 99% of the time.
879 return Owned(new UnaryTypeTraitExpr(KWLoc, OTT,
880 QualType::getFromOpaquePtr(Ty),
881 RParen, Context.BoolTy));
882}