blob: 0a42de06cad4d5b4f7eb1cad5d4b3efd985ef165 [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"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000019#include "clang/Basic/Diagnostic.h"
Sebastian Redlb5ee8742008-12-03 20:26:15 +000020#include "clang/Basic/TargetInfo.h"
Douglas Gregorddfd9d52008-12-23 00:26:44 +000021#include "llvm/ADT/STLExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022using namespace clang;
23
Douglas Gregor24094772008-11-19 19:09:45 +000024/// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function
Douglas Gregorb0212bd2008-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.
Douglas Gregor24094772008-11-19 19:09:45 +000029Sema::ExprResult
30Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc,
31 TypeTy *Ty, bool HasTrailingLParen,
32 const CXXScopeSpec &SS) {
Douglas Gregorb0212bd2008-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);
Douglas Gregoraee3bf82008-11-18 15:03:34 +000037 return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen,
Douglas Gregor24094772008-11-19 19:09:45 +000038 &SS);
Douglas Gregorb0212bd2008-11-17 20:34:05 +000039}
Sebastian Redlb93b49c2008-11-11 11:37:55 +000040
Douglas Gregor24094772008-11-19 19:09:45 +000041/// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator
Douglas Gregor96a32dd2008-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.
Douglas Gregor24094772008-11-19 19:09:45 +000046Sema::ExprResult
47Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc,
48 OverloadedOperatorKind Op,
49 bool HasTrailingLParen,
50 const CXXScopeSpec &SS) {
Douglas Gregor96a32dd2008-11-18 14:39:36 +000051 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op);
Douglas Gregor24094772008-11-19 19:09:45 +000052 return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS);
Douglas Gregor96a32dd2008-11-18 14:39:36 +000053}
54
Sebastian Redlb93b49c2008-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 Lattnerc2a5f512008-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 Gregor78d70132009-01-14 22:20:51 +000064 Decl *TypeInfoDecl = LookupDecl(TypeInfoII, Decl::IDNS_Tag,
Sebastian Redlb93b49c2008-11-11 11:37:55 +000065 0, StdNs, /*createBuiltins=*/false);
66 RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl);
Chris Lattnerc2a5f512008-11-20 05:51:55 +000067 if (!TypeInfoRecordDecl)
68 return Diag(OpLoc, diag::err_need_header_before_typeid);
Sebastian Redlb93b49c2008-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 Naroff5cbb02f2007-09-16 14:56:35 +000076/// ActOnCXXBoolLiteral - Parse {true,false} literals.
Chris Lattner4b009652007-07-25 00:24:17 +000077Action::ExprResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +000078Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
Douglas Gregorf8e92702008-10-24 15:36:09 +000079 assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
Chris Lattner4b009652007-07-25 00:24:17 +000080 "Unknown C++ Boolean value!");
Steve Naroffac5d4f12007-08-25 14:02:58 +000081 return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000082}
Chris Lattnera7447ba2008-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}
Argiris Kirtzidis38f16712008-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 Gregora5b022a2008-11-04 14:32:21 +0000102 return new CXXThisExpr(ThisLoc, MD->getThisType(Context));
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000103
104 return Diag(ThisLoc, diag::err_invalid_this_use);
105}
Argiris Kirtzidis7a1e7412008-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
123 if (const RecordType *RT = Ty->getAsRecordType()) {
124 // C++ 5.2.3p1:
125 // If the simple-type-specifier specifies a class type, the class type shall
126 // be complete.
127 //
128 if (!RT->getDecl()->isDefinition())
Chris Lattner77d52da2008-11-20 06:06:08 +0000129 return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000130 << Ty << FullRange;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000131
Argiris Kirtzidiscf4e8f82008-10-06 23:16:35 +0000132 unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error,
133 "class constructors are not supported yet");
134 return Diag(TyBeginLoc, DiagID);
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000135 }
136
137 // C++ 5.2.3p1:
138 // If the expression list is a single expression, the type conversion
139 // expression is equivalent (in definedness, and if defined in meaning) to the
140 // corresponding cast expression.
141 //
142 if (NumExprs == 1) {
143 if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
144 return true;
Douglas Gregor21a04f32008-10-27 19:41:14 +0000145 return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc,
146 Exprs[0], RParenLoc);
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000147 }
148
149 // C++ 5.2.3p1:
150 // If the expression list specifies more than a single value, the type shall
151 // be a class with a suitably declared constructor.
152 //
153 if (NumExprs > 1)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000154 return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg)
155 << FullRange;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000156
157 assert(NumExprs == 0 && "Expected 0 expressions");
158
159 // C++ 5.2.3p2:
160 // The expression T(), where T is a simple-type-specifier for a non-array
161 // complete object type or the (possibly cv-qualified) void type, creates an
162 // rvalue of the specified type, which is value-initialized.
163 //
164 if (Ty->isArrayType())
Chris Lattner9d2cf082008-11-19 05:27:50 +0000165 return Diag(TyBeginLoc, diag::err_value_init_for_array_type) << FullRange;
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000166 if (!Ty->isDependentType() && Ty->isIncompleteType() && !Ty->isVoidType())
Chris Lattner9d2cf082008-11-19 05:27:50 +0000167 return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000168 << Ty << FullRange;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000169
170 return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc);
171}
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000172
173
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000174/// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.:
175/// @code new (memory) int[size][4] @endcode
176/// or
177/// @code ::new Foo(23, "hello") @endcode
178/// For the interpretation of this heap of arguments, consult the base version.
179Action::ExprResult
180Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
181 SourceLocation PlacementLParen,
182 ExprTy **PlacementArgs, unsigned NumPlaceArgs,
183 SourceLocation PlacementRParen, bool ParenTypeId,
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000184 Declarator &D, SourceLocation ConstructorLParen,
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000185 ExprTy **ConstructorArgs, unsigned NumConsArgs,
186 SourceLocation ConstructorRParen)
187{
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000188 // FIXME: Throughout this function, we have rather bad location information.
189 // Implementing Declarator::getSourceRange() would go a long way toward
190 // fixing that.
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000191
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000192 Expr *ArraySize = 0;
193 unsigned Skip = 0;
194 // If the specified type is an array, unwrap it and save the expression.
195 if (D.getNumTypeObjects() > 0 &&
196 D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
197 DeclaratorChunk &Chunk = D.getTypeObject(0);
198 if (Chunk.Arr.hasStatic)
199 return Diag(Chunk.Loc, diag::err_static_illegal_in_new);
200 if (!Chunk.Arr.NumElts)
201 return Diag(Chunk.Loc, diag::err_array_new_needs_size);
202 ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts);
203 Skip = 1;
204 }
205
206 QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, Skip);
207 if (D.getInvalidType())
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000208 return true;
209
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000210 if (CheckAllocatedType(AllocType, D))
211 return true;
212
213 QualType ResultType = Context.getPointerType(AllocType);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000214
215 // That every array dimension except the first is constant was already
216 // checked by the type check above.
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000217
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000218 // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral
219 // or enumeration type with a non-negative value."
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000220 if (ArraySize) {
221 QualType SizeType = ArraySize->getType();
222 if (!SizeType->isIntegralType() && !SizeType->isEnumeralType())
223 return Diag(ArraySize->getSourceRange().getBegin(),
224 diag::err_array_size_not_integral)
225 << SizeType << ArraySize->getSourceRange();
226 // Let's see if this is a constant < 0. If so, we reject it out of hand.
227 // We don't care about special rules, so we tell the machinery it's not
228 // evaluated - it gives us a result in more cases.
229 llvm::APSInt Value;
230 if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) {
231 if (Value < llvm::APSInt(
232 llvm::APInt::getNullValue(Value.getBitWidth()), false))
233 return Diag(ArraySize->getSourceRange().getBegin(),
234 diag::err_typecheck_negative_array_size)
235 << ArraySize->getSourceRange();
236 }
237 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000238
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000239 FunctionDecl *OperatorNew = 0;
240 FunctionDecl *OperatorDelete = 0;
241 Expr **PlaceArgs = (Expr**)PlacementArgs;
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000242 if (FindAllocationFunctions(StartLoc, UseGlobal, AllocType, ArraySize,
243 PlaceArgs, NumPlaceArgs, OperatorNew,
244 OperatorDelete))
245 return true;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000246
247 bool Init = ConstructorLParen.isValid();
248 // --- Choosing a constructor ---
249 // C++ 5.3.4p15
250 // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid)
251 // the object is not initialized. If the object, or any part of it, is
252 // const-qualified, it's an error.
253 // 2) If T is a POD and there's an empty initializer, the object is value-
254 // initialized.
255 // 3) If T is a POD and there's one initializer argument, the object is copy-
256 // constructed.
257 // 4) If T is a POD and there's more initializer arguments, it's an error.
258 // 5) If T is not a POD, the initializer arguments are used as constructor
259 // arguments.
260 //
261 // Or by the C++0x formulation:
262 // 1) If there's no initializer, the object is default-initialized according
263 // to C++0x rules.
264 // 2) Otherwise, the object is direct-initialized.
265 CXXConstructorDecl *Constructor = 0;
266 Expr **ConsArgs = (Expr**)ConstructorArgs;
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000267 if (const RecordType *RT = AllocType->getAsRecordType()) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000268 // FIXME: This is incorrect for when there is an empty initializer and
269 // no user-defined constructor. Must zero-initialize, not default-construct.
270 Constructor = PerformInitializationByConstructor(
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000271 AllocType, ConsArgs, NumConsArgs,
272 D.getDeclSpec().getSourceRange().getBegin(),
273 SourceRange(D.getDeclSpec().getSourceRange().getBegin(),
274 ConstructorRParen),
Chris Lattner271d4c22008-11-24 05:29:24 +0000275 RT->getDecl()->getDeclName(),
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000276 NumConsArgs != 0 ? IK_Direct : IK_Default);
277 if (!Constructor)
278 return true;
279 } else {
280 if (!Init) {
281 // FIXME: Check that no subpart is const.
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000282 if (AllocType.isConstQualified()) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000283 Diag(StartLoc, diag::err_new_uninitialized_const)
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000284 << D.getSourceRange();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000285 return true;
286 }
287 } else if (NumConsArgs == 0) {
288 // Object is value-initialized. Do nothing.
289 } else if (NumConsArgs == 1) {
290 // Object is direct-initialized.
Chris Lattner271d4c22008-11-24 05:29:24 +0000291 // FIXME: WHAT DeclarationName do we pass in here?
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000292 if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc,
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000293 DeclarationName() /*AllocType.getAsString()*/,
294 /*DirectInit=*/true))
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000295 return true;
296 } else {
297 Diag(StartLoc, diag::err_builtin_direct_init_more_than_one_arg)
298 << SourceRange(ConstructorLParen, ConstructorRParen);
299 }
300 }
301
302 // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16)
303
304 return new CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, NumPlaceArgs,
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000305 ParenTypeId, ArraySize, Constructor, Init,
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000306 ConsArgs, NumConsArgs, OperatorDelete, ResultType,
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000307 StartLoc, Init ? ConstructorRParen : SourceLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000308}
309
310/// CheckAllocatedType - Checks that a type is suitable as the allocated type
311/// in a new-expression.
312/// dimension off and stores the size expression in ArraySize.
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000313bool Sema::CheckAllocatedType(QualType AllocType, const Declarator &D)
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000314{
315 // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
316 // abstract class type or array thereof.
317 // FIXME: We don't have abstract types yet.
318 // FIXME: Under C++ semantics, an incomplete object type is still an object
319 // type. This code assumes the C semantics, where it's not.
320 if (!AllocType->isObjectType()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000321 unsigned type; // For the select in the message.
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000322 if (AllocType->isFunctionType()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000323 type = 0;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000324 } else if(AllocType->isIncompleteType()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000325 type = 1;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000326 } else {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000327 assert(AllocType->isReferenceType() && "What else could it be?");
328 type = 2;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000329 }
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000330 SourceRange TyR = D.getDeclSpec().getSourceRange();
331 // FIXME: This is very much a guess and won't work for, e.g., pointers.
332 if (D.getNumTypeObjects() > 0)
333 TyR.setEnd(D.getTypeObject(0).Loc);
334 Diag(TyR.getBegin(), diag::err_bad_new_type)
335 << AllocType.getAsString() << type << TyR;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000336 return true;
337 }
338
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000339 // Every dimension shall be of constant size.
340 unsigned i = 1;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000341 while (const ArrayType *Array = Context.getAsArrayType(AllocType)) {
342 if (!Array->isConstantArrayType()) {
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000343 Diag(D.getTypeObject(i).Loc, diag::err_new_array_nonconst)
344 << static_cast<Expr*>(D.getTypeObject(i).Arr.NumElts)->getSourceRange();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000345 return true;
346 }
347 AllocType = Array->getElementType();
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000348 ++i;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000349 }
350
351 return false;
352}
353
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000354/// FindAllocationFunctions - Finds the overloads of operator new and delete
355/// that are appropriate for the allocation.
356bool Sema::FindAllocationFunctions(SourceLocation StartLoc, bool UseGlobal,
357 QualType AllocType, bool IsArray,
358 Expr **PlaceArgs, unsigned NumPlaceArgs,
359 FunctionDecl *&OperatorNew,
360 FunctionDecl *&OperatorDelete)
361{
362 // --- Choosing an allocation function ---
363 // C++ 5.3.4p8 - 14 & 18
364 // 1) If UseGlobal is true, only look in the global scope. Else, also look
365 // in the scope of the allocated class.
366 // 2) If an array size is given, look for operator new[], else look for
367 // operator new.
368 // 3) The first argument is always size_t. Append the arguments from the
369 // placement form.
370 // FIXME: Also find the appropriate delete operator.
371
372 llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs);
373 // We don't care about the actual value of this argument.
374 // FIXME: Should the Sema create the expression and embed it in the syntax
375 // tree? Or should the consumer just recalculate the value?
376 AllocArgs[0] = new IntegerLiteral(llvm::APInt::getNullValue(
377 Context.Target.getPointerWidth(0)),
378 Context.getSizeType(),
379 SourceLocation());
380 std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1);
381
382 DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName(
383 IsArray ? OO_Array_New : OO_New);
384 if (AllocType->isRecordType() && !UseGlobal) {
Sebastian Redlec5f3262008-12-04 22:20:51 +0000385 CXXRecordDecl *Record = cast<CXXRecordType>(AllocType->getAsRecordType())
386 ->getDecl();
387 // FIXME: We fail to find inherited overloads.
388 if (FindAllocationOverload(StartLoc, NewName, &AllocArgs[0],
389 AllocArgs.size(), Record, /*AllowMissing=*/true,
390 OperatorNew))
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000391 return true;
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000392 }
393 if (!OperatorNew) {
394 // Didn't find a member overload. Look for a global one.
395 DeclareGlobalNewDelete();
Sebastian Redlec5f3262008-12-04 22:20:51 +0000396 DeclContext *TUDecl = Context.getTranslationUnitDecl();
397 if (FindAllocationOverload(StartLoc, NewName, &AllocArgs[0],
398 AllocArgs.size(), TUDecl, /*AllowMissing=*/false,
399 OperatorNew))
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000400 return true;
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000401 }
402
Sebastian Redlec5f3262008-12-04 22:20:51 +0000403 // FIXME: This is leaked on error. But so much is currently in Sema that it's
404 // easier to clean it in one go.
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000405 AllocArgs[0]->Destroy(Context);
406 return false;
407}
408
Sebastian Redlec5f3262008-12-04 22:20:51 +0000409/// FindAllocationOverload - Find an fitting overload for the allocation
410/// function in the specified scope.
411bool Sema::FindAllocationOverload(SourceLocation StartLoc, DeclarationName Name,
412 Expr** Args, unsigned NumArgs,
413 DeclContext *Ctx, bool AllowMissing,
414 FunctionDecl *&Operator)
415{
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000416 DeclContext::lookup_iterator Alloc, AllocEnd;
Steve Naroffab63fd62009-01-08 17:28:14 +0000417 llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Name);
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000418 if (Alloc == AllocEnd) {
Sebastian Redlec5f3262008-12-04 22:20:51 +0000419 if (AllowMissing)
420 return false;
421 // FIXME: Bad location information.
422 return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
423 << Name << 0;
424 }
425
426 OverloadCandidateSet Candidates;
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000427 for (; Alloc != AllocEnd; ++Alloc) {
428 // Even member operator new/delete are implicitly treated as
429 // static, so don't use AddMemberCandidate.
430 if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc))
431 AddOverloadCandidate(Fn, Args, NumArgs, Candidates,
432 /*SuppressUserConversions=*/false);
Sebastian Redlec5f3262008-12-04 22:20:51 +0000433 }
434
435 // Do the resolution.
436 OverloadCandidateSet::iterator Best;
437 switch(BestViableFunction(Candidates, Best)) {
438 case OR_Success: {
439 // Got one!
440 FunctionDecl *FnDecl = Best->Function;
441 // The first argument is size_t, and the first parameter must be size_t,
442 // too. This is checked on declaration and can be assumed. (It can't be
443 // asserted on, though, since invalid decls are left in there.)
444 for (unsigned i = 1; i < NumArgs; ++i) {
445 // FIXME: Passing word to diagnostic.
446 if (PerformCopyInitialization(Args[i-1],
447 FnDecl->getParamDecl(i)->getType(),
448 "passing"))
449 return true;
450 }
451 Operator = FnDecl;
452 return false;
453 }
454
455 case OR_No_Viable_Function:
456 if (AllowMissing)
457 return false;
458 // FIXME: Bad location information.
459 Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
460 << Name << (unsigned)Candidates.size();
461 PrintOverloadCandidates(Candidates, /*OnlyViable=*/false);
462 return true;
463
464 case OR_Ambiguous:
465 // FIXME: Bad location information.
466 Diag(StartLoc, diag::err_ovl_ambiguous_call)
467 << Name;
468 PrintOverloadCandidates(Candidates, /*OnlyViable=*/true);
469 return true;
470 }
471 assert(false && "Unreachable, bad result from BestViableFunction");
472 return true;
473}
474
475
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000476/// DeclareGlobalNewDelete - Declare the global forms of operator new and
477/// delete. These are:
478/// @code
479/// void* operator new(std::size_t) throw(std::bad_alloc);
480/// void* operator new[](std::size_t) throw(std::bad_alloc);
481/// void operator delete(void *) throw();
482/// void operator delete[](void *) throw();
483/// @endcode
484/// Note that the placement and nothrow forms of new are *not* implicitly
485/// declared. Their use requires including \<new\>.
486void Sema::DeclareGlobalNewDelete()
487{
488 if (GlobalNewDeleteDeclared)
489 return;
490 GlobalNewDeleteDeclared = true;
491
492 QualType VoidPtr = Context.getPointerType(Context.VoidTy);
493 QualType SizeT = Context.getSizeType();
494
495 // FIXME: Exception specifications are not added.
496 DeclareGlobalAllocationFunction(
497 Context.DeclarationNames.getCXXOperatorName(OO_New),
498 VoidPtr, SizeT);
499 DeclareGlobalAllocationFunction(
500 Context.DeclarationNames.getCXXOperatorName(OO_Array_New),
501 VoidPtr, SizeT);
502 DeclareGlobalAllocationFunction(
503 Context.DeclarationNames.getCXXOperatorName(OO_Delete),
504 Context.VoidTy, VoidPtr);
505 DeclareGlobalAllocationFunction(
506 Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
507 Context.VoidTy, VoidPtr);
508}
509
510/// DeclareGlobalAllocationFunction - Declares a single implicit global
511/// allocation function if it doesn't already exist.
512void Sema::DeclareGlobalAllocationFunction(DeclarationName Name,
513 QualType Return, QualType Argument)
514{
515 DeclContext *GlobalCtx = Context.getTranslationUnitDecl();
516
517 // Check if this function is already declared.
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000518 {
Douglas Gregor7c865852008-12-23 22:05:29 +0000519 DeclContext::lookup_iterator Alloc, AllocEnd;
Steve Naroffab63fd62009-01-08 17:28:14 +0000520 for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name);
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000521 Alloc != AllocEnd; ++Alloc) {
522 // FIXME: Do we need to check for default arguments here?
523 FunctionDecl *Func = cast<FunctionDecl>(*Alloc);
524 if (Func->getNumParams() == 1 &&
525 Context.getCanonicalType(Func->getParamDecl(0)->getType()) == Argument)
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000526 return;
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000527 }
528 }
529
530 QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0);
531 FunctionDecl *Alloc =
532 FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name,
533 FnType, FunctionDecl::None, false, 0,
534 SourceLocation());
535 Alloc->setImplicit();
536 ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
537 0, Argument, VarDecl::None, 0, 0);
Ted Kremenek8494c962009-01-14 00:42:25 +0000538 Alloc->setParams(Context, &Param, 1);
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000539
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000540 // FIXME: Also add this declaration to the IdentifierResolver, but
541 // make sure it is at the end of the chain to coincide with the
542 // global scope.
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000543 ((DeclContext *)TUScope->getEntity())->addDecl(Alloc);
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000544}
545
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000546/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
547/// @code ::delete ptr; @endcode
548/// or
549/// @code delete [] ptr; @endcode
550Action::ExprResult
551Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
552 bool ArrayForm, ExprTy *Operand)
553{
554 // C++ 5.3.5p1: "The operand shall have a pointer type, or a class type
555 // having a single conversion function to a pointer type. The result has
556 // type void."
557 // DR599 amends "pointer type" to "pointer to object type" in both cases.
558
559 Expr *Ex = (Expr *)Operand;
560 QualType Type = Ex->getType();
561
562 if (Type->isRecordType()) {
563 // FIXME: Find that one conversion function and amend the type.
564 }
565
566 if (!Type->isPointerType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000567 Diag(StartLoc, diag::err_delete_operand) << Type << Ex->getSourceRange();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000568 return true;
569 }
570
571 QualType Pointee = Type->getAsPointerType()->getPointeeType();
572 if (Pointee->isIncompleteType() && !Pointee->isVoidType())
573 Diag(StartLoc, diag::warn_delete_incomplete)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000574 << Pointee << Ex->getSourceRange();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000575 else if (!Pointee->isObjectType()) {
576 Diag(StartLoc, diag::err_delete_operand)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000577 << Type << Ex->getSourceRange();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000578 return true;
579 }
580
581 // FIXME: Look up the correct operator delete overload and pass a pointer
582 // along.
583 // FIXME: Check access and ambiguity of operator delete and destructor.
584
585 return new CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 0, Ex,
586 StartLoc);
587}
588
589
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000590/// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a
591/// C++ if/switch/while/for statement.
592/// e.g: "if (int x = f()) {...}"
593Action::ExprResult
594Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
595 Declarator &D,
596 SourceLocation EqualLoc,
597 ExprTy *AssignExprVal) {
598 assert(AssignExprVal && "Null assignment expression");
599
600 // C++ 6.4p2:
601 // The declarator shall not specify a function or an array.
602 // The type-specifier-seq shall not contain typedef and shall not declare a
603 // new class or enumeration.
604
605 assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
606 "Parser allowed 'typedef' as storage class of condition decl.");
607
608 QualType Ty = GetTypeForDeclarator(D, S);
609
610 if (Ty->isFunctionType()) { // The declarator shall not specify a function...
611 // We exit without creating a CXXConditionDeclExpr because a FunctionDecl
612 // would be created and CXXConditionDeclExpr wants a VarDecl.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000613 return Diag(StartLoc, diag::err_invalid_use_of_function_type)
614 << SourceRange(StartLoc, EqualLoc);
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000615 } else if (Ty->isArrayType()) { // ...or an array.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000616 Diag(StartLoc, diag::err_invalid_use_of_array_type)
617 << SourceRange(StartLoc, EqualLoc);
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000618 } else if (const RecordType *RT = Ty->getAsRecordType()) {
619 RecordDecl *RD = RT->getDecl();
620 // The type-specifier-seq shall not declare a new class...
621 if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD)))
622 Diag(RD->getLocation(), diag::err_type_defined_in_condition);
623 } else if (const EnumType *ET = Ty->getAsEnumType()) {
624 EnumDecl *ED = ET->getDecl();
625 // ...or enumeration.
626 if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED)))
627 Diag(ED->getLocation(), diag::err_type_defined_in_condition);
628 }
629
630 DeclTy *Dcl = ActOnDeclarator(S, D, 0);
631 if (!Dcl)
632 return true;
Sebastian Redl91f9b0a2008-12-13 16:23:55 +0000633 AddInitializerToDecl(Dcl, ExprArg(*this, AssignExprVal));
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000634
Douglas Gregor48840c72008-12-10 23:01:14 +0000635 // Mark this variable as one that is declared within a conditional.
636 if (VarDecl *VD = dyn_cast<VarDecl>((Decl *)Dcl))
637 VD->setDeclaredInCondition(true);
638
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000639 return new CXXConditionDeclExpr(StartLoc, EqualLoc,
640 cast<VarDecl>(static_cast<Decl *>(Dcl)));
641}
642
643/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
644bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) {
645 // C++ 6.4p4:
646 // The value of a condition that is an initialized declaration in a statement
647 // other than a switch statement is the value of the declared variable
648 // implicitly converted to type bool. If that conversion is ill-formed, the
649 // program is ill-formed.
650 // The value of a condition that is an expression is the value of the
651 // expression, implicitly converted to bool.
652 //
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000653 return PerformContextuallyConvertToBool(CondExpr);
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000654}
Douglas Gregor1815b3b2008-09-12 00:47:35 +0000655
656/// Helper function to determine whether this is the (deprecated) C++
657/// conversion from a string literal to a pointer to non-const char or
658/// non-const wchar_t (for narrow and wide string literals,
659/// respectively).
660bool
661Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
662 // Look inside the implicit cast, if it exists.
663 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
664 From = Cast->getSubExpr();
665
666 // A string literal (2.13.4) that is not a wide string literal can
667 // be converted to an rvalue of type "pointer to char"; a wide
668 // string literal can be converted to an rvalue of type "pointer
669 // to wchar_t" (C++ 4.2p2).
670 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From))
671 if (const PointerType *ToPtrType = ToType->getAsPointerType())
672 if (const BuiltinType *ToPointeeType
673 = ToPtrType->getPointeeType()->getAsBuiltinType()) {
674 // This conversion is considered only when there is an
675 // explicit appropriate pointer target type (C++ 4.2p2).
676 if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 &&
677 ((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
678 (!StrLit->isWide() &&
679 (ToPointeeType->getKind() == BuiltinType::Char_U ||
680 ToPointeeType->getKind() == BuiltinType::Char_S))))
681 return true;
682 }
683
684 return false;
685}
Douglas Gregorbb461502008-10-24 04:54:22 +0000686
687/// PerformImplicitConversion - Perform an implicit conversion of the
688/// expression From to the type ToType. Returns true if there was an
689/// error, false otherwise. The expression From is replaced with the
Douglas Gregor6fd35572008-12-19 17:40:08 +0000690/// converted expression. Flavor is the kind of conversion we're
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000691/// performing, used in the error message. If @p AllowExplicit,
692/// explicit user-defined conversions are permitted.
Douglas Gregorbb461502008-10-24 04:54:22 +0000693bool
Douglas Gregor6fd35572008-12-19 17:40:08 +0000694Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000695 const char *Flavor, bool AllowExplicit)
Douglas Gregorbb461502008-10-24 04:54:22 +0000696{
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000697 ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType, false,
698 AllowExplicit);
699 return PerformImplicitConversion(From, ToType, ICS, Flavor);
700}
701
702/// PerformImplicitConversion - Perform an implicit conversion of the
703/// expression From to the type ToType using the pre-computed implicit
704/// conversion sequence ICS. Returns true if there was an error, false
705/// otherwise. The expression From is replaced with the converted
706/// expression. Flavor is the kind of conversion we're performing,
707/// used in the error message.
708bool
709Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
710 const ImplicitConversionSequence &ICS,
711 const char* Flavor) {
Douglas Gregorbb461502008-10-24 04:54:22 +0000712 switch (ICS.ConversionKind) {
713 case ImplicitConversionSequence::StandardConversion:
Douglas Gregor6fd35572008-12-19 17:40:08 +0000714 if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor))
Douglas Gregorbb461502008-10-24 04:54:22 +0000715 return true;
716 break;
717
718 case ImplicitConversionSequence::UserDefinedConversion:
719 // FIXME: This is, of course, wrong. We'll need to actually call
720 // the constructor or conversion operator, and then cope with the
721 // standard conversions.
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000722 ImpCastExprToType(From, ToType.getNonReferenceType(),
723 ToType->isReferenceType());
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000724 return false;
Douglas Gregorbb461502008-10-24 04:54:22 +0000725
726 case ImplicitConversionSequence::EllipsisConversion:
727 assert(false && "Cannot perform an ellipsis conversion");
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000728 return false;
Douglas Gregorbb461502008-10-24 04:54:22 +0000729
730 case ImplicitConversionSequence::BadConversion:
731 return true;
732 }
733
734 // Everything went well.
735 return false;
736}
737
738/// PerformImplicitConversion - Perform an implicit conversion of the
739/// expression From to the type ToType by following the standard
740/// conversion sequence SCS. Returns true if there was an error, false
741/// otherwise. The expression From is replaced with the converted
Douglas Gregor6fd35572008-12-19 17:40:08 +0000742/// expression. Flavor is the context in which we're performing this
743/// conversion, for use in error messages.
Douglas Gregorbb461502008-10-24 04:54:22 +0000744bool
745Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
Douglas Gregor6fd35572008-12-19 17:40:08 +0000746 const StandardConversionSequence& SCS,
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000747 const char *Flavor) {
Douglas Gregorbb461502008-10-24 04:54:22 +0000748 // Overall FIXME: we are recomputing too many types here and doing
749 // far too much extra work. What this means is that we need to keep
750 // track of more information that is computed when we try the
751 // implicit conversion initially, so that we don't need to recompute
752 // anything here.
753 QualType FromType = From->getType();
754
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000755 if (SCS.CopyConstructor) {
756 // FIXME: Create a temporary object by calling the copy
757 // constructor.
758 ImpCastExprToType(From, ToType);
759 return false;
760 }
761
Douglas Gregorbb461502008-10-24 04:54:22 +0000762 // Perform the first implicit conversion.
763 switch (SCS.First) {
764 case ICK_Identity:
765 case ICK_Lvalue_To_Rvalue:
766 // Nothing to do.
767 break;
768
769 case ICK_Array_To_Pointer:
Douglas Gregor45014fd2008-11-10 20:40:00 +0000770 if (FromType->isOverloadType()) {
771 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true);
772 if (!Fn)
773 return true;
774
775 FixOverloadedFunctionReference(From, Fn);
776 FromType = From->getType();
777 } else {
778 FromType = Context.getArrayDecayedType(FromType);
779 }
Douglas Gregorbb461502008-10-24 04:54:22 +0000780 ImpCastExprToType(From, FromType);
781 break;
782
783 case ICK_Function_To_Pointer:
784 FromType = Context.getPointerType(FromType);
785 ImpCastExprToType(From, FromType);
786 break;
787
788 default:
789 assert(false && "Improper first standard conversion");
790 break;
791 }
792
793 // Perform the second implicit conversion
794 switch (SCS.Second) {
795 case ICK_Identity:
796 // Nothing to do.
797 break;
798
799 case ICK_Integral_Promotion:
800 case ICK_Floating_Promotion:
801 case ICK_Integral_Conversion:
802 case ICK_Floating_Conversion:
803 case ICK_Floating_Integral:
804 FromType = ToType.getUnqualifiedType();
805 ImpCastExprToType(From, FromType);
806 break;
807
808 case ICK_Pointer_Conversion:
Douglas Gregor6fd35572008-12-19 17:40:08 +0000809 if (SCS.IncompatibleObjC) {
810 // Diagnose incompatible Objective-C conversions
811 Diag(From->getSourceRange().getBegin(),
812 diag::ext_typecheck_convert_incompatible_pointer)
813 << From->getType() << ToType << Flavor
814 << From->getSourceRange();
815 }
816
Douglas Gregorbb461502008-10-24 04:54:22 +0000817 if (CheckPointerConversion(From, ToType))
818 return true;
819 ImpCastExprToType(From, ToType);
820 break;
821
822 case ICK_Pointer_Member:
823 // FIXME: Implement pointer-to-member conversions.
824 assert(false && "Pointer-to-member conversions are unsupported");
825 break;
826
827 case ICK_Boolean_Conversion:
828 FromType = Context.BoolTy;
829 ImpCastExprToType(From, FromType);
830 break;
831
832 default:
833 assert(false && "Improper second standard conversion");
834 break;
835 }
836
837 switch (SCS.Third) {
838 case ICK_Identity:
839 // Nothing to do.
840 break;
841
842 case ICK_Qualification:
843 ImpCastExprToType(From, ToType);
844 break;
845
846 default:
847 assert(false && "Improper second standard conversion");
848 break;
849 }
850
851 return false;
852}
853
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000854Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT,
855 SourceLocation KWLoc,
856 SourceLocation LParen,
857 TypeTy *Ty,
858 SourceLocation RParen) {
859 // FIXME: Some of the type traits have requirements. Interestingly, only the
860 // __is_base_of requirement is explicitly stated to be diagnosed. Indeed,
861 // G++ accepts __is_pod(Incomplete) without complaints, and claims that the
862 // type is indeed a POD.
863
864 // There is no point in eagerly computing the value. The traits are designed
865 // to be used from type trait templates, so Ty will be a template parameter
866 // 99% of the time.
867 return Owned(new UnaryTypeTraitExpr(KWLoc, OTT,
868 QualType::getFromOpaquePtr(Ty),
869 RParen, Context.BoolTy));
870}