blob: 09db18ea541e137c08cc68b4ab89f60198a38ee6 [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"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Basic/Diagnostic.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");
Douglas Gregoreb11cd02009-01-14 22:20:51 +000064 Decl *TypeInfoDecl = LookupDecl(TypeInfoII, Decl::IDNS_Tag,
Sebastian Redlc42e1182008-11-11 11:37:55 +000065 0, StdNs, /*createBuiltins=*/false);
66 RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl);
Chris Lattner572af492008-11-20 05:51:55 +000067 if (!TypeInfoRecordDecl)
68 return Diag(OpLoc, diag::err_need_header_before_typeid);
Sebastian Redlc42e1182008-11-11 11:37:55 +000069
70 QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl);
71
72 return new CXXTypeidExpr(isType, TyOrExpr, TypeInfoType.withConst(),
73 SourceRange(OpLoc, RParenLoc));
74}
75
Steve Naroff1b273c42007-09-16 14:56:35 +000076/// ActOnCXXBoolLiteral - Parse {true,false} literals.
Reid Spencer5f016e22007-07-11 17:01:13 +000077Action::ExprResult
Steve Naroff1b273c42007-09-16 14:56:35 +000078Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
Douglas Gregor2f639b92008-10-24 15:36:09 +000079 assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
Reid Spencer5f016e22007-07-11 17:01:13 +000080 "Unknown C++ Boolean value!");
Steve Naroff210679c2007-08-25 14:02:58 +000081 return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000082}
Chris Lattner50dd2892008-02-26 00:51:44 +000083
84/// ActOnCXXThrow - Parse throw expressions.
85Action::ExprResult
86Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) {
87 return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc);
88}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000089
90Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) {
91 /// C++ 9.3.2: In the body of a non-static member function, the keyword this
92 /// is a non-lvalue expression whose value is the address of the object for
93 /// which the function is called.
94
95 if (!isa<FunctionDecl>(CurContext)) {
96 Diag(ThisLoc, diag::err_invalid_this_use);
97 return ExprResult(true);
98 }
99
100 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
101 if (MD->isInstance())
Douglas Gregor796da182008-11-04 14:32:21 +0000102 return new CXXThisExpr(ThisLoc, MD->getThisType(Context));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000103
104 return Diag(ThisLoc, diag::err_invalid_this_use);
105}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000106
107/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
108/// Can be interpreted either as function-style casting ("int(x)")
109/// or class type construction ("ClassType(x,y,z)")
110/// or creation of a value-initialized type ("int()").
111Action::ExprResult
112Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
113 SourceLocation LParenLoc,
114 ExprTy **ExprTys, unsigned NumExprs,
115 SourceLocation *CommaLocs,
116 SourceLocation RParenLoc) {
117 assert(TypeRep && "Missing type!");
118 QualType Ty = QualType::getFromOpaquePtr(TypeRep);
119 Expr **Exprs = (Expr**)ExprTys;
120 SourceLocation TyBeginLoc = TypeRange.getBegin();
121 SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
122
Douglas Gregor506ae412009-01-16 18:33:17 +0000123 // C++ [expr.type.conv]p1:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000124 // If the expression list is a single expression, the type conversion
125 // expression is equivalent (in definedness, and if defined in meaning) to the
126 // corresponding cast expression.
127 //
128 if (NumExprs == 1) {
129 if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
130 return true;
Douglas Gregor49badde2008-10-27 19:41:14 +0000131 return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc,
132 Exprs[0], RParenLoc);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000133 }
134
Douglas Gregor506ae412009-01-16 18:33:17 +0000135 if (const RecordType *RT = Ty->getAsRecordType()) {
136 CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
137
138 if (NumExprs > 1 || Record->hasUserDeclaredConstructor()) {
139 CXXConstructorDecl *Constructor
140 = PerformInitializationByConstructor(Ty, Exprs, NumExprs,
141 TypeRange.getBegin(),
142 SourceRange(TypeRange.getBegin(),
143 RParenLoc),
144 DeclarationName(),
145 IK_Direct);
146
147 if (!Constructor)
148 return true;
149
150 return new CXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc,
151 Exprs, NumExprs, RParenLoc);
152 }
153
154 // Fall through to value-initialize an object of class type that
155 // doesn't have a user-declared default constructor.
156 }
157
158 // C++ [expr.type.conv]p1:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000159 // If the expression list specifies more than a single value, the type shall
160 // be a class with a suitably declared constructor.
161 //
162 if (NumExprs > 1)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000163 return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg)
164 << FullRange;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000165
166 assert(NumExprs == 0 && "Expected 0 expressions");
167
Douglas Gregor506ae412009-01-16 18:33:17 +0000168 // C++ [expr.type.conv]p2:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000169 // The expression T(), where T is a simple-type-specifier for a non-array
170 // complete object type or the (possibly cv-qualified) void type, creates an
171 // rvalue of the specified type, which is value-initialized.
172 //
173 if (Ty->isArrayType())
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000174 return Diag(TyBeginLoc, diag::err_value_init_for_array_type) << FullRange;
Douglas Gregor898574e2008-12-05 23:32:09 +0000175 if (!Ty->isDependentType() && Ty->isIncompleteType() && !Ty->isVoidType())
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000176 return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use)
Douglas Gregor898574e2008-12-05 23:32:09 +0000177 << Ty << FullRange;
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,
542 FnType, FunctionDecl::None, false, 0,
543 SourceLocation());
544 Alloc->setImplicit();
545 ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
546 0, Argument, VarDecl::None, 0, 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();
581 if (Pointee->isIncompleteType() && !Pointee->isVoidType())
582 Diag(StartLoc, diag::warn_delete_incomplete)
Chris Lattnerd1625842008-11-24 06:25:27 +0000583 << Pointee << Ex->getSourceRange();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000584 else if (!Pointee->isObjectType()) {
585 Diag(StartLoc, diag::err_delete_operand)
Chris Lattnerd1625842008-11-24 06:25:27 +0000586 << Type << Ex->getSourceRange();
Sebastian Redl4c5d3202008-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
Argyrios Kyrtzidis59210932008-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 Lattnerdcd5ef12008-11-19 05:27:50 +0000622 return Diag(StartLoc, diag::err_invalid_use_of_function_type)
623 << SourceRange(StartLoc, EqualLoc);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000624 } else if (Ty->isArrayType()) { // ...or an array.
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000625 Diag(StartLoc, diag::err_invalid_use_of_array_type)
626 << SourceRange(StartLoc, EqualLoc);
Argyrios Kyrtzidis59210932008-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 Redl798d1192008-12-13 16:23:55 +0000642 AddInitializerToDecl(Dcl, ExprArg(*this, AssignExprVal));
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000643
Douglas Gregorcaaf29a2008-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
Argyrios Kyrtzidis59210932008-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 Gregor09f41cf2009-01-14 15:45:31 +0000662 return PerformContextuallyConvertToBool(CondExpr);
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000663}
Douglas Gregor77a52232008-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 Gregor94b1dd22008-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 Gregor45920e82008-12-19 17:40:08 +0000699/// converted expression. Flavor is the kind of conversion we're
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000700/// performing, used in the error message. If @p AllowExplicit,
701/// explicit user-defined conversions are permitted.
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000702bool
Douglas Gregor45920e82008-12-19 17:40:08 +0000703Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000704 const char *Flavor, bool AllowExplicit)
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000705{
Douglas Gregor09f41cf2009-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 Gregor94b1dd22008-10-24 04:54:22 +0000721 switch (ICS.ConversionKind) {
722 case ImplicitConversionSequence::StandardConversion:
Douglas Gregor45920e82008-12-19 17:40:08 +0000723 if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor))
Douglas Gregor94b1dd22008-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 Gregor09f41cf2009-01-14 15:45:31 +0000731 ImpCastExprToType(From, ToType.getNonReferenceType(),
732 ToType->isReferenceType());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000733 return false;
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000734
735 case ImplicitConversionSequence::EllipsisConversion:
736 assert(false && "Cannot perform an ellipsis conversion");
Douglas Gregor60d62c22008-10-31 16:23:19 +0000737 return false;
Douglas Gregor94b1dd22008-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 Gregor45920e82008-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 Gregor94b1dd22008-10-24 04:54:22 +0000753bool
754Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Douglas Gregor45920e82008-12-19 17:40:08 +0000755 const StandardConversionSequence& SCS,
Douglas Gregor09f41cf2009-01-14 15:45:31 +0000756 const char *Flavor) {
Douglas Gregor94b1dd22008-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 Gregor225c41e2008-11-03 19:09:14 +0000764 if (SCS.CopyConstructor) {
765 // FIXME: Create a temporary object by calling the copy
766 // constructor.
Douglas Gregor66b947f2009-01-16 19:38:23 +0000767 ImpCastExprToType(From, ToType.getNonReferenceType(),
768 ToType->isReferenceType());
Douglas Gregor225c41e2008-11-03 19:09:14 +0000769 return false;
770 }
771
Douglas Gregor94b1dd22008-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 Gregor904eed32008-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 Gregor94b1dd22008-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 Gregor45920e82008-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 Gregor94b1dd22008-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:
833 // FIXME: Implement pointer-to-member conversions.
834 assert(false && "Pointer-to-member conversions are unsupported");
835 break;
836
837 case ICK_Boolean_Conversion:
838 FromType = Context.BoolTy;
839 ImpCastExprToType(From, FromType);
840 break;
841
842 default:
843 assert(false && "Improper second standard conversion");
844 break;
845 }
846
847 switch (SCS.Third) {
848 case ICK_Identity:
849 // Nothing to do.
850 break;
851
852 case ICK_Qualification:
Douglas Gregor66b947f2009-01-16 19:38:23 +0000853 ImpCastExprToType(From, ToType.getNonReferenceType(),
854 ToType->isReferenceType());
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000855 break;
856
857 default:
858 assert(false && "Improper second standard conversion");
859 break;
860 }
861
862 return false;
863}
864
Sebastian Redl64b45f72009-01-05 20:52:13 +0000865Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT,
866 SourceLocation KWLoc,
867 SourceLocation LParen,
868 TypeTy *Ty,
869 SourceLocation RParen) {
870 // FIXME: Some of the type traits have requirements. Interestingly, only the
871 // __is_base_of requirement is explicitly stated to be diagnosed. Indeed,
872 // G++ accepts __is_pod(Incomplete) without complaints, and claims that the
873 // type is indeed a POD.
874
875 // There is no point in eagerly computing the value. The traits are designed
876 // to be used from type trait templates, so Ty will be a template parameter
877 // 99% of the time.
878 return Owned(new UnaryTypeTraitExpr(KWLoc, OTT,
879 QualType::getFromOpaquePtr(Ty),
880 RParen, Context.BoolTy));
881}