blob: c206e1a9cddd7e606adf1a2ce91a616ea5f8757d [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Steve Naroff1b273c42007-09-16 14:56:35 +000022/// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
Reid Spencer5f016e22007-07-11 17:01:13 +000023Action::ExprResult
Steve Naroff1b273c42007-09-16 14:56:35 +000024Sema::ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
Reid Spencer5f016e22007-07-11 17:01:13 +000025 SourceLocation LAngleBracketLoc, TypeTy *Ty,
26 SourceLocation RAngleBracketLoc,
27 SourceLocation LParenLoc, ExprTy *E,
28 SourceLocation RParenLoc) {
29 CXXCastExpr::Opcode Op;
30
31 switch (Kind) {
32 default: assert(0 && "Unknown C++ cast!");
33 case tok::kw_const_cast: Op = CXXCastExpr::ConstCast; break;
34 case tok::kw_dynamic_cast: Op = CXXCastExpr::DynamicCast; break;
35 case tok::kw_reinterpret_cast: Op = CXXCastExpr::ReinterpretCast; break;
36 case tok::kw_static_cast: Op = CXXCastExpr::StaticCast; break;
37 }
38
39 return new CXXCastExpr(Op, QualType::getFromOpaquePtr(Ty), (Expr*)E, OpLoc);
40}
41
Steve Naroff1b273c42007-09-16 14:56:35 +000042/// ActOnCXXBoolLiteral - Parse {true,false} literals.
Reid Spencer5f016e22007-07-11 17:01:13 +000043Action::ExprResult
Steve Naroff1b273c42007-09-16 14:56:35 +000044Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
Reid Spencer5f016e22007-07-11 17:01:13 +000045 assert((Kind != tok::kw_true || Kind != tok::kw_false) &&
46 "Unknown C++ Boolean value!");
Steve Naroff210679c2007-08-25 14:02:58 +000047 return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
Chris Lattner50dd2892008-02-26 00:51:44 +000049
50/// ActOnCXXThrow - Parse throw expressions.
51Action::ExprResult
52Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) {
53 return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc);
54}
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000055
56Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) {
57 /// C++ 9.3.2: In the body of a non-static member function, the keyword this
58 /// is a non-lvalue expression whose value is the address of the object for
59 /// which the function is called.
60
61 if (!isa<FunctionDecl>(CurContext)) {
62 Diag(ThisLoc, diag::err_invalid_this_use);
63 return ExprResult(true);
64 }
65
66 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
67 if (MD->isInstance())
Chris Lattnerd9f69102008-08-10 01:53:14 +000068 return new PredefinedExpr(ThisLoc, MD->getThisType(Context),
69 PredefinedExpr::CXXThis);
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +000070
71 return Diag(ThisLoc, diag::err_invalid_this_use);
72}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000073
74/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
75/// Can be interpreted either as function-style casting ("int(x)")
76/// or class type construction ("ClassType(x,y,z)")
77/// or creation of a value-initialized type ("int()").
78Action::ExprResult
79Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
80 SourceLocation LParenLoc,
81 ExprTy **ExprTys, unsigned NumExprs,
82 SourceLocation *CommaLocs,
83 SourceLocation RParenLoc) {
84 assert(TypeRep && "Missing type!");
85 QualType Ty = QualType::getFromOpaquePtr(TypeRep);
86 Expr **Exprs = (Expr**)ExprTys;
87 SourceLocation TyBeginLoc = TypeRange.getBegin();
88 SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
89
90 if (const RecordType *RT = Ty->getAsRecordType()) {
91 // C++ 5.2.3p1:
92 // If the simple-type-specifier specifies a class type, the class type shall
93 // be complete.
94 //
95 if (!RT->getDecl()->isDefinition())
96 return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use,
97 Ty.getAsString(), FullRange);
98
Argyrios Kyrtzidis4021a842008-10-06 23:16:35 +000099 unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error,
100 "class constructors are not supported yet");
101 return Diag(TyBeginLoc, DiagID);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000102 }
103
104 // C++ 5.2.3p1:
105 // If the expression list is a single expression, the type conversion
106 // expression is equivalent (in definedness, and if defined in meaning) to the
107 // corresponding cast expression.
108 //
109 if (NumExprs == 1) {
110 if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
111 return true;
112 return new CXXFunctionalCastExpr(Ty, TyBeginLoc, Exprs[0], RParenLoc);
113 }
114
115 // C++ 5.2.3p1:
116 // If the expression list specifies more than a single value, the type shall
117 // be a class with a suitably declared constructor.
118 //
119 if (NumExprs > 1)
120 return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg,
121 FullRange);
122
123 assert(NumExprs == 0 && "Expected 0 expressions");
124
125 // C++ 5.2.3p2:
126 // The expression T(), where T is a simple-type-specifier for a non-array
127 // complete object type or the (possibly cv-qualified) void type, creates an
128 // rvalue of the specified type, which is value-initialized.
129 //
130 if (Ty->isArrayType())
131 return Diag(TyBeginLoc, diag::err_value_init_for_array_type, FullRange);
132 if (Ty->isIncompleteType() && !Ty->isVoidType())
133 return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use,
134 Ty.getAsString(), FullRange);
135
136 return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc);
137}
Argyrios Kyrtzidis59210932008-09-10 02:17:11 +0000138
139
140/// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a
141/// C++ if/switch/while/for statement.
142/// e.g: "if (int x = f()) {...}"
143Action::ExprResult
144Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
145 Declarator &D,
146 SourceLocation EqualLoc,
147 ExprTy *AssignExprVal) {
148 assert(AssignExprVal && "Null assignment expression");
149
150 // C++ 6.4p2:
151 // The declarator shall not specify a function or an array.
152 // The type-specifier-seq shall not contain typedef and shall not declare a
153 // new class or enumeration.
154
155 assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
156 "Parser allowed 'typedef' as storage class of condition decl.");
157
158 QualType Ty = GetTypeForDeclarator(D, S);
159
160 if (Ty->isFunctionType()) { // The declarator shall not specify a function...
161 // We exit without creating a CXXConditionDeclExpr because a FunctionDecl
162 // would be created and CXXConditionDeclExpr wants a VarDecl.
163 return Diag(StartLoc, diag::err_invalid_use_of_function_type,
164 SourceRange(StartLoc, EqualLoc));
165 } else if (Ty->isArrayType()) { // ...or an array.
166 Diag(StartLoc, diag::err_invalid_use_of_array_type,
167 SourceRange(StartLoc, EqualLoc));
168 } else if (const RecordType *RT = Ty->getAsRecordType()) {
169 RecordDecl *RD = RT->getDecl();
170 // The type-specifier-seq shall not declare a new class...
171 if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD)))
172 Diag(RD->getLocation(), diag::err_type_defined_in_condition);
173 } else if (const EnumType *ET = Ty->getAsEnumType()) {
174 EnumDecl *ED = ET->getDecl();
175 // ...or enumeration.
176 if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED)))
177 Diag(ED->getLocation(), diag::err_type_defined_in_condition);
178 }
179
180 DeclTy *Dcl = ActOnDeclarator(S, D, 0);
181 if (!Dcl)
182 return true;
183 AddInitializerToDecl(Dcl, AssignExprVal);
184
185 return new CXXConditionDeclExpr(StartLoc, EqualLoc,
186 cast<VarDecl>(static_cast<Decl *>(Dcl)));
187}
188
189/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
190bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) {
191 // C++ 6.4p4:
192 // The value of a condition that is an initialized declaration in a statement
193 // other than a switch statement is the value of the declared variable
194 // implicitly converted to type bool. If that conversion is ill-formed, the
195 // program is ill-formed.
196 // The value of a condition that is an expression is the value of the
197 // expression, implicitly converted to bool.
198 //
199 QualType Ty = CondExpr->getType(); // Save the type.
200 AssignConvertType
201 ConvTy = CheckSingleAssignmentConstraints(Context.BoolTy, CondExpr);
202 if (ConvTy == Incompatible)
203 return Diag(CondExpr->getLocStart(), diag::err_typecheck_bool_condition,
204 Ty.getAsString(), CondExpr->getSourceRange());
205 return false;
206}
Douglas Gregor77a52232008-09-12 00:47:35 +0000207
208/// Helper function to determine whether this is the (deprecated) C++
209/// conversion from a string literal to a pointer to non-const char or
210/// non-const wchar_t (for narrow and wide string literals,
211/// respectively).
212bool
213Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
214 // Look inside the implicit cast, if it exists.
215 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
216 From = Cast->getSubExpr();
217
218 // A string literal (2.13.4) that is not a wide string literal can
219 // be converted to an rvalue of type "pointer to char"; a wide
220 // string literal can be converted to an rvalue of type "pointer
221 // to wchar_t" (C++ 4.2p2).
222 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From))
223 if (const PointerType *ToPtrType = ToType->getAsPointerType())
224 if (const BuiltinType *ToPointeeType
225 = ToPtrType->getPointeeType()->getAsBuiltinType()) {
226 // This conversion is considered only when there is an
227 // explicit appropriate pointer target type (C++ 4.2p2).
228 if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 &&
229 ((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
230 (!StrLit->isWide() &&
231 (ToPointeeType->getKind() == BuiltinType::Char_U ||
232 ToPointeeType->getKind() == BuiltinType::Char_S))))
233 return true;
234 }
235
236 return false;
237}
Douglas Gregor94b1dd22008-10-24 04:54:22 +0000238
239/// PerformImplicitConversion - Perform an implicit conversion of the
240/// expression From to the type ToType. Returns true if there was an
241/// error, false otherwise. The expression From is replaced with the
242/// converted expression.
243bool
244Sema::PerformImplicitConversion(Expr *&From, QualType ToType)
245{
246 ImplicitConversionSequence ICS = TryCopyInitialization(From, ToType);
247 switch (ICS.ConversionKind) {
248 case ImplicitConversionSequence::StandardConversion:
249 if (PerformImplicitConversion(From, ToType, ICS.Standard))
250 return true;
251 break;
252
253 case ImplicitConversionSequence::UserDefinedConversion:
254 // FIXME: This is, of course, wrong. We'll need to actually call
255 // the constructor or conversion operator, and then cope with the
256 // standard conversions.
257 ImpCastExprToType(From, ToType);
258 break;
259
260 case ImplicitConversionSequence::EllipsisConversion:
261 assert(false && "Cannot perform an ellipsis conversion");
262 break;
263
264 case ImplicitConversionSequence::BadConversion:
265 return true;
266 }
267
268 // Everything went well.
269 return false;
270}
271
272/// PerformImplicitConversion - Perform an implicit conversion of the
273/// expression From to the type ToType by following the standard
274/// conversion sequence SCS. Returns true if there was an error, false
275/// otherwise. The expression From is replaced with the converted
276/// expression.
277bool
278Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
279 const StandardConversionSequence& SCS)
280{
281 // Overall FIXME: we are recomputing too many types here and doing
282 // far too much extra work. What this means is that we need to keep
283 // track of more information that is computed when we try the
284 // implicit conversion initially, so that we don't need to recompute
285 // anything here.
286 QualType FromType = From->getType();
287
288 // Perform the first implicit conversion.
289 switch (SCS.First) {
290 case ICK_Identity:
291 case ICK_Lvalue_To_Rvalue:
292 // Nothing to do.
293 break;
294
295 case ICK_Array_To_Pointer:
296 FromType = Context.getArrayDecayedType(FromType);
297 ImpCastExprToType(From, FromType);
298 break;
299
300 case ICK_Function_To_Pointer:
301 FromType = Context.getPointerType(FromType);
302 ImpCastExprToType(From, FromType);
303 break;
304
305 default:
306 assert(false && "Improper first standard conversion");
307 break;
308 }
309
310 // Perform the second implicit conversion
311 switch (SCS.Second) {
312 case ICK_Identity:
313 // Nothing to do.
314 break;
315
316 case ICK_Integral_Promotion:
317 case ICK_Floating_Promotion:
318 case ICK_Integral_Conversion:
319 case ICK_Floating_Conversion:
320 case ICK_Floating_Integral:
321 FromType = ToType.getUnqualifiedType();
322 ImpCastExprToType(From, FromType);
323 break;
324
325 case ICK_Pointer_Conversion:
326 if (CheckPointerConversion(From, ToType))
327 return true;
328 ImpCastExprToType(From, ToType);
329 break;
330
331 case ICK_Pointer_Member:
332 // FIXME: Implement pointer-to-member conversions.
333 assert(false && "Pointer-to-member conversions are unsupported");
334 break;
335
336 case ICK_Boolean_Conversion:
337 FromType = Context.BoolTy;
338 ImpCastExprToType(From, FromType);
339 break;
340
341 default:
342 assert(false && "Improper second standard conversion");
343 break;
344 }
345
346 switch (SCS.Third) {
347 case ICK_Identity:
348 // Nothing to do.
349 break;
350
351 case ICK_Qualification:
352 ImpCastExprToType(From, ToType);
353 break;
354
355 default:
356 assert(false && "Improper second standard conversion");
357 break;
358 }
359
360 return false;
361}
362