blob: 8a4752ab1e647b566af683986cbef05c6950e922 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaExpr.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 expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek30c66752007-11-25 00:58:00 +000015#include "SemaUtil.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Argiris Kirtzidis38f16712008-07-01 10:37:29 +000017#include "clang/AST/DeclCXX.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/AST/Expr.h"
Chris Lattner3e254fb2008-04-08 04:40:51 +000019#include "clang/AST/ExprCXX.h"
Steve Naroff9ed3e772008-05-29 21:12:08 +000020#include "clang/AST/ExprObjC.h"
Steve Naroffc39ca262007-09-18 23:55:05 +000021#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include "clang/Lex/Preprocessor.h"
23#include "clang/Lex/LiteralSupport.h"
24#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000025#include "clang/Basic/TargetInfo.h"
Chris Lattner83bd5eb2007-12-28 05:29:59 +000026#include "llvm/ADT/OwningPtr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027#include "llvm/ADT/SmallString.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000028#include "llvm/ADT/StringExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000029using namespace clang;
30
Chris Lattner299b8842008-07-25 21:10:04 +000031//===----------------------------------------------------------------------===//
32// Standard Promotions and Conversions
33//===----------------------------------------------------------------------===//
34
35/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
36/// do not have a prototype. Arguments that have type float are promoted to
37/// double. All other argument types are converted by UsualUnaryConversions().
38void Sema::DefaultArgumentPromotion(Expr *&Expr) {
39 QualType Ty = Expr->getType();
40 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
41
42 // If this is a 'float' (CVR qualified or typedef) promote to double.
43 if (const BuiltinType *BT = Ty->getAsBuiltinType())
44 if (BT->getKind() == BuiltinType::Float)
45 return ImpCastExprToType(Expr, Context.DoubleTy);
46
47 UsualUnaryConversions(Expr);
48}
49
50/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
51void Sema::DefaultFunctionArrayConversion(Expr *&E) {
52 QualType Ty = E->getType();
53 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
54
55 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
56 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
57 Ty = E->getType();
58 }
59 if (Ty->isFunctionType())
60 ImpCastExprToType(E, Context.getPointerType(Ty));
61 else if (Ty->isArrayType())
62 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
63}
64
65/// UsualUnaryConversions - Performs various conversions that are common to most
66/// operators (C99 6.3). The conversions of array and function types are
67/// sometimes surpressed. For example, the array->pointer conversion doesn't
68/// apply if the array is an argument to the sizeof or address (&) operators.
69/// In these instances, this routine should *not* be called.
70Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
71 QualType Ty = Expr->getType();
72 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
73
74 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
75 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
76 Ty = Expr->getType();
77 }
78 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
79 ImpCastExprToType(Expr, Context.IntTy);
80 else
81 DefaultFunctionArrayConversion(Expr);
82
83 return Expr;
84}
85
86/// UsualArithmeticConversions - Performs various conversions that are common to
87/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
88/// routine returns the first non-arithmetic type found. The client is
89/// responsible for emitting appropriate error diagnostics.
90/// FIXME: verify the conversion rules for "complex int" are consistent with
91/// GCC.
92QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
93 bool isCompAssign) {
94 if (!isCompAssign) {
95 UsualUnaryConversions(lhsExpr);
96 UsualUnaryConversions(rhsExpr);
97 }
98 // For conversion purposes, we ignore any qualifiers.
99 // For example, "const float" and "float" are equivalent.
100 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
101 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
102
103 // If both types are identical, no conversion is needed.
104 if (lhs == rhs)
105 return lhs;
106
107 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
108 // The caller can deal with this (e.g. pointer + int).
109 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
110 return lhs;
111
112 // At this point, we have two different arithmetic types.
113
114 // Handle complex types first (C99 6.3.1.8p1).
115 if (lhs->isComplexType() || rhs->isComplexType()) {
116 // if we have an integer operand, the result is the complex type.
117 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
118 // convert the rhs to the lhs complex type.
119 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
120 return lhs;
121 }
122 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
123 // convert the lhs to the rhs complex type.
124 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
125 return rhs;
126 }
127 // This handles complex/complex, complex/float, or float/complex.
128 // When both operands are complex, the shorter operand is converted to the
129 // type of the longer, and that is the type of the result. This corresponds
130 // to what is done when combining two real floating-point operands.
131 // The fun begins when size promotion occur across type domains.
132 // From H&S 6.3.4: When one operand is complex and the other is a real
133 // floating-point type, the less precise type is converted, within it's
134 // real or complex domain, to the precision of the other type. For example,
135 // when combining a "long double" with a "double _Complex", the
136 // "double _Complex" is promoted to "long double _Complex".
137 int result = Context.getFloatingTypeOrder(lhs, rhs);
138
139 if (result > 0) { // The left side is bigger, convert rhs.
140 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
141 if (!isCompAssign)
142 ImpCastExprToType(rhsExpr, rhs);
143 } else if (result < 0) { // The right side is bigger, convert lhs.
144 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
145 if (!isCompAssign)
146 ImpCastExprToType(lhsExpr, lhs);
147 }
148 // At this point, lhs and rhs have the same rank/size. Now, make sure the
149 // domains match. This is a requirement for our implementation, C99
150 // does not require this promotion.
151 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
152 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
153 if (!isCompAssign)
154 ImpCastExprToType(lhsExpr, rhs);
155 return rhs;
156 } else { // handle "_Complex double, double".
157 if (!isCompAssign)
158 ImpCastExprToType(rhsExpr, lhs);
159 return lhs;
160 }
161 }
162 return lhs; // The domain/size match exactly.
163 }
164 // Now handle "real" floating types (i.e. float, double, long double).
165 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
166 // if we have an integer operand, the result is the real floating type.
167 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
168 // convert rhs to the lhs floating point type.
169 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
170 return lhs;
171 }
172 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
173 // convert lhs to the rhs floating point type.
174 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
175 return rhs;
176 }
177 // We have two real floating types, float/complex combos were handled above.
178 // Convert the smaller operand to the bigger result.
179 int result = Context.getFloatingTypeOrder(lhs, rhs);
180
181 if (result > 0) { // convert the rhs
182 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
183 return lhs;
184 }
185 if (result < 0) { // convert the lhs
186 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
187 return rhs;
188 }
189 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
190 }
191 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
192 // Handle GCC complex int extension.
193 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
194 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
195
196 if (lhsComplexInt && rhsComplexInt) {
197 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
198 rhsComplexInt->getElementType()) >= 0) {
199 // convert the rhs
200 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
201 return lhs;
202 }
203 if (!isCompAssign)
204 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
205 return rhs;
206 } else if (lhsComplexInt && rhs->isIntegerType()) {
207 // convert the rhs to the lhs complex type.
208 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
209 return lhs;
210 } else if (rhsComplexInt && lhs->isIntegerType()) {
211 // convert the lhs to the rhs complex type.
212 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
213 return rhs;
214 }
215 }
216 // Finally, we have two differing integer types.
217 // The rules for this case are in C99 6.3.1.8
218 int compare = Context.getIntegerTypeOrder(lhs, rhs);
219 bool lhsSigned = lhs->isSignedIntegerType(),
220 rhsSigned = rhs->isSignedIntegerType();
221 QualType destType;
222 if (lhsSigned == rhsSigned) {
223 // Same signedness; use the higher-ranked type
224 destType = compare >= 0 ? lhs : rhs;
225 } else if (compare != (lhsSigned ? 1 : -1)) {
226 // The unsigned type has greater than or equal rank to the
227 // signed type, so use the unsigned type
228 destType = lhsSigned ? rhs : lhs;
229 } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) {
230 // The two types are different widths; if we are here, that
231 // means the signed type is larger than the unsigned type, so
232 // use the signed type.
233 destType = lhsSigned ? lhs : rhs;
234 } else {
235 // The signed type is higher-ranked than the unsigned type,
236 // but isn't actually any bigger (like unsigned int and long
237 // on most 32-bit systems). Use the unsigned type corresponding
238 // to the signed type.
239 destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
240 }
241 if (!isCompAssign) {
242 ImpCastExprToType(lhsExpr, destType);
243 ImpCastExprToType(rhsExpr, destType);
244 }
245 return destType;
246}
247
248//===----------------------------------------------------------------------===//
249// Semantic Analysis for various Expression Types
250//===----------------------------------------------------------------------===//
251
252
Steve Naroff87d58b42007-09-16 03:34:24 +0000253/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +0000254/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
255/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
256/// multiple tokens. However, the common case is that StringToks points to one
257/// string.
258///
259Action::ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +0000260Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner4b009652007-07-25 00:24:17 +0000261 assert(NumStringToks && "Must have at least one string!");
262
263 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
264 if (Literal.hadError)
265 return ExprResult(true);
266
267 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
268 for (unsigned i = 0; i != NumStringToks; ++i)
269 StringTokLocs.push_back(StringToks[i].getLocation());
Chris Lattnera6dcce32008-02-11 00:02:17 +0000270
271 // Verify that pascal strings aren't too large.
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000272 if (Literal.Pascal && Literal.GetStringLength() > 256)
273 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
274 SourceRange(StringToks[0].getLocation(),
275 StringToks[NumStringToks-1].getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000276
Chris Lattnera6dcce32008-02-11 00:02:17 +0000277 QualType StrTy = Context.CharTy;
Eli Friedman256b7d72008-05-27 07:57:14 +0000278 if (Literal.AnyWide) StrTy = Context.getWcharType();
Chris Lattnera6dcce32008-02-11 00:02:17 +0000279 if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
280
281 // Get an array type for the string, according to C99 6.4.5. This includes
282 // the nul terminator character as well as the string length for pascal
283 // strings.
284 StrTy = Context.getConstantArrayType(StrTy,
285 llvm::APInt(32, Literal.GetStringLength()+1),
286 ArrayType::Normal, 0);
287
Chris Lattner4b009652007-07-25 00:24:17 +0000288 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
289 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera6dcce32008-02-11 00:02:17 +0000290 Literal.AnyWide, StrTy,
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000291 StringToks[0].getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000292 StringToks[NumStringToks-1].getLocation());
293}
294
295
Steve Naroff0acc9c92007-09-15 18:49:24 +0000296/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattner4b009652007-07-25 00:24:17 +0000297/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroffe50e14c2008-03-19 23:46:26 +0000298/// identifier is used in a function call context.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000299Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000300 IdentifierInfo &II,
301 bool HasTrailingLParen) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000302 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroff6384a012008-04-02 14:35:35 +0000303 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000304
305 // If this reference is in an Objective-C method, then ivar lookup happens as
306 // well.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000307 if (getCurMethodDecl()) {
Steve Naroffe57c21a2008-04-01 23:04:06 +0000308 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000309 // There are two cases to handle here. 1) scoped lookup could have failed,
310 // in which case we should look for an ivar. 2) scoped lookup could have
311 // found a decl, but that decl is outside the current method (i.e. a global
312 // variable). In these two cases, we do a lookup for an ivar with this
313 // name, if the lookup suceeds, we replace it our current decl.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000314 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000315 ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface();
Chris Lattnered94f762008-07-21 04:44:44 +0000316 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II)) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000317 // FIXME: This should use a new expr for a direct reference, don't turn
318 // this into Self->ivar, just return a BareIVarExpr or something.
319 IdentifierInfo &II = Context.Idents.get("self");
320 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
321 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
322 static_cast<Expr*>(SelfExpr.Val), true, true);
323 }
324 }
Steve Naroffe90d4cc2008-06-05 18:14:25 +0000325 if (SD == 0 && !strcmp(II.getName(), "super")) {
Steve Naroff6f786252008-06-02 23:03:37 +0000326 QualType T = Context.getPointerType(Context.getObjCInterfaceType(
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000327 getCurMethodDecl()->getClassInterface()));
Steve Naroff6f786252008-06-02 23:03:37 +0000328 return new ObjCSuperRefExpr(T, Loc);
329 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000330 }
331
Chris Lattner4b009652007-07-25 00:24:17 +0000332 if (D == 0) {
333 // Otherwise, this could be an implicitly declared function reference (legal
334 // in C90, extension in C99).
335 if (HasTrailingLParen &&
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000336 !getLangOptions().CPlusPlus) // Not in C++.
Chris Lattner4b009652007-07-25 00:24:17 +0000337 D = ImplicitlyDefineFunction(Loc, II, S);
338 else {
339 // If this name wasn't predeclared and if this is not a function call,
340 // diagnose the problem.
341 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
342 }
343 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000344
Steve Naroff91b03f72007-08-28 03:03:08 +0000345 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneree4c3bf2008-02-29 16:48:43 +0000346 // check if referencing an identifier with __attribute__((deprecated)).
347 if (VD->getAttr<DeprecatedAttr>())
348 Diag(Loc, diag::warn_deprecated, VD->getName());
349
Steve Naroffcae537d2007-08-28 18:45:29 +0000350 // Only create DeclRefExpr's for valid Decl's.
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000351 if (VD->isInvalidDecl())
Steve Naroff91b03f72007-08-28 03:03:08 +0000352 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000353 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff91b03f72007-08-28 03:03:08 +0000354 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000355
356 if (CXXFieldDecl *FD = dyn_cast<CXXFieldDecl>(D)) {
357 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) {
358 if (MD->isStatic())
359 // "invalid use of member 'x' in static member function"
360 return Diag(Loc, diag::err_invalid_member_use_in_static_method,
361 FD->getName());
362 if (cast<CXXRecordDecl>(MD->getParent()) != FD->getParent())
363 // "invalid use of nonstatic data member 'x'"
364 return Diag(Loc, diag::err_invalid_non_static_member_use,
365 FD->getName());
366
367 if (FD->isInvalidDecl())
368 return true;
369
370 // FIXME: Use DeclRefExpr or a new Expr for a direct CXXField reference.
371 ExprResult ThisExpr = ActOnCXXThis(SourceLocation());
372 return new MemberExpr(static_cast<Expr*>(ThisExpr.Val),
373 true, FD, Loc, FD->getType());
374 }
375
376 return Diag(Loc, diag::err_invalid_non_static_member_use, FD->getName());
377 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000378
Chris Lattner4b009652007-07-25 00:24:17 +0000379 if (isa<TypedefDecl>(D))
380 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek42730c52008-01-07 19:49:32 +0000381 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian3102df92007-12-05 18:16:33 +0000382 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000383 if (isa<NamespaceDecl>(D))
384 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000385
386 assert(0 && "Invalid decl");
387 abort();
388}
389
Steve Naroff87d58b42007-09-16 03:34:24 +0000390Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000391 tok::TokenKind Kind) {
392 PreDefinedExpr::IdentType IT;
393
394 switch (Kind) {
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000395 default: assert(0 && "Unknown simple primary expr!");
396 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
397 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
398 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000399 }
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000400
401 // Verify that this is in a function context.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000402 if (getCurFunctionDecl() == 0 && getCurMethodDecl() == 0)
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000403 return Diag(Loc, diag::err_predef_outside_function);
Chris Lattner4b009652007-07-25 00:24:17 +0000404
Chris Lattner7e637512008-01-12 08:14:25 +0000405 // Pre-defined identifiers are of type char[x], where x is the length of the
406 // string.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000407 unsigned Length;
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000408 if (getCurFunctionDecl())
409 Length = getCurFunctionDecl()->getIdentifier()->getLength();
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000410 else
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000411 Length = getCurMethodDecl()->getSynthesizedMethodSize();
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000412
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000413 llvm::APInt LengthI(32, Length + 1);
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000414 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000415 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattner7e637512008-01-12 08:14:25 +0000416 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattner4b009652007-07-25 00:24:17 +0000417}
418
Steve Naroff87d58b42007-09-16 03:34:24 +0000419Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000420 llvm::SmallString<16> CharBuffer;
421 CharBuffer.resize(Tok.getLength());
422 const char *ThisTokBegin = &CharBuffer[0];
423 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
424
425 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
426 Tok.getLocation(), PP);
427 if (Literal.hadError())
428 return ExprResult(true);
Chris Lattner6b22fb72008-03-01 08:32:21 +0000429
430 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
431
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000432 return new CharacterLiteral(Literal.getValue(), Literal.isWide(), type,
433 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000434}
435
Steve Naroff87d58b42007-09-16 03:34:24 +0000436Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000437 // fast path for a single digit (which is quite common). A single digit
438 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
439 if (Tok.getLength() == 1) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000440 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000441
Chris Lattner8cd0e932008-03-05 18:54:05 +0000442 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner48d7f382008-04-02 04:24:33 +0000443 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Chris Lattner4b009652007-07-25 00:24:17 +0000444 Context.IntTy,
445 Tok.getLocation()));
446 }
447 llvm::SmallString<512> IntegerBuffer;
448 IntegerBuffer.resize(Tok.getLength());
449 const char *ThisTokBegin = &IntegerBuffer[0];
450
451 // Get the spelling of the token, which eliminates trigraphs, etc.
452 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
453 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
454 Tok.getLocation(), PP);
455 if (Literal.hadError)
456 return ExprResult(true);
457
Chris Lattner1de66eb2007-08-26 03:42:43 +0000458 Expr *Res;
459
460 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000461 QualType Ty;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000462 if (Literal.isFloat)
Chris Lattner858eece2007-09-22 18:29:59 +0000463 Ty = Context.FloatTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000464 else if (!Literal.isLong)
Chris Lattner858eece2007-09-22 18:29:59 +0000465 Ty = Context.DoubleTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000466 else
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000467 Ty = Context.LongDoubleTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000468
469 const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty);
470
Ted Kremenekddedbe22007-11-29 00:56:49 +0000471 // isExact will be set by GetFloatValue().
472 bool isExact = false;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000473 Res = new FloatingLiteral(Literal.GetFloatValue(Format, &isExact), &isExact,
Ted Kremenekddedbe22007-11-29 00:56:49 +0000474 Ty, Tok.getLocation());
475
Chris Lattner1de66eb2007-08-26 03:42:43 +0000476 } else if (!Literal.isIntegerLiteral()) {
477 return ExprResult(true);
478 } else {
Chris Lattner48d7f382008-04-02 04:24:33 +0000479 QualType Ty;
Chris Lattner4b009652007-07-25 00:24:17 +0000480
Neil Booth7421e9c2007-08-29 22:00:19 +0000481 // long long is a C99 feature.
482 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000483 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000484 Diag(Tok.getLocation(), diag::ext_longlong);
485
Chris Lattner4b009652007-07-25 00:24:17 +0000486 // Get the value in the widest-possible width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000487 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000488
489 if (Literal.GetIntegerValue(ResultVal)) {
490 // If this value didn't fit into uintmax_t, warn and force to ull.
491 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattner48d7f382008-04-02 04:24:33 +0000492 Ty = Context.UnsignedLongLongTy;
493 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner8cd0e932008-03-05 18:54:05 +0000494 "long long is not intmax_t?");
Chris Lattner4b009652007-07-25 00:24:17 +0000495 } else {
496 // If this value fits into a ULL, try to figure out what else it fits into
497 // according to the rules of C99 6.4.4.1p5.
498
499 // Octal, Hexadecimal, and integers with a U suffix are allowed to
500 // be an unsigned int.
501 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
502
503 // Check from smallest to largest, picking the smallest type we can.
Chris Lattnere4068872008-05-09 05:59:00 +0000504 unsigned Width = 0;
Chris Lattner98540b62007-08-23 21:58:08 +0000505 if (!Literal.isLong && !Literal.isLongLong) {
506 // Are int/unsigned possibilities?
Chris Lattnere4068872008-05-09 05:59:00 +0000507 unsigned IntSize = Context.Target.getIntWidth();
508
Chris Lattner4b009652007-07-25 00:24:17 +0000509 // Does it fit in a unsigned int?
510 if (ResultVal.isIntN(IntSize)) {
511 // Does it fit in a signed int?
512 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000513 Ty = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000514 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000515 Ty = Context.UnsignedIntTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000516 Width = IntSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000517 }
Chris Lattner4b009652007-07-25 00:24:17 +0000518 }
519
520 // Are long/unsigned long possibilities?
Chris Lattner48d7f382008-04-02 04:24:33 +0000521 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattnere4068872008-05-09 05:59:00 +0000522 unsigned LongSize = Context.Target.getLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000523
524 // Does it fit in a unsigned long?
525 if (ResultVal.isIntN(LongSize)) {
526 // Does it fit in a signed long?
527 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000528 Ty = Context.LongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000529 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000530 Ty = Context.UnsignedLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000531 Width = LongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000532 }
Chris Lattner4b009652007-07-25 00:24:17 +0000533 }
534
535 // Finally, check long long if needed.
Chris Lattner48d7f382008-04-02 04:24:33 +0000536 if (Ty.isNull()) {
Chris Lattnere4068872008-05-09 05:59:00 +0000537 unsigned LongLongSize = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000538
539 // Does it fit in a unsigned long long?
540 if (ResultVal.isIntN(LongLongSize)) {
541 // Does it fit in a signed long long?
542 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000543 Ty = Context.LongLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000544 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000545 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000546 Width = LongLongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000547 }
548 }
549
550 // If we still couldn't decide a type, we probably have something that
551 // does not fit in a signed long long, but has no U suffix.
Chris Lattner48d7f382008-04-02 04:24:33 +0000552 if (Ty.isNull()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000553 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattner48d7f382008-04-02 04:24:33 +0000554 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000555 Width = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000556 }
Chris Lattnere4068872008-05-09 05:59:00 +0000557
558 if (ResultVal.getBitWidth() != Width)
559 ResultVal.trunc(Width);
Chris Lattner4b009652007-07-25 00:24:17 +0000560 }
561
Chris Lattner48d7f382008-04-02 04:24:33 +0000562 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000563 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000564
565 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
566 if (Literal.isImaginary)
567 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
568
569 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000570}
571
Steve Naroff87d58b42007-09-16 03:34:24 +0000572Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000573 ExprTy *Val) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000574 Expr *E = (Expr *)Val;
575 assert((E != 0) && "ActOnParenExpr() missing expr");
576 return new ParenExpr(L, R, E);
Chris Lattner4b009652007-07-25 00:24:17 +0000577}
578
579/// The UsualUnaryConversions() function is *not* called by this routine.
580/// See C99 6.3.2.1p[2-4] for more details.
581QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
582 SourceLocation OpLoc, bool isSizeof) {
583 // C99 6.5.3.4p1:
584 if (isa<FunctionType>(exprType) && isSizeof)
585 // alignof(function) is allowed.
586 Diag(OpLoc, diag::ext_sizeof_function_type);
587 else if (exprType->isVoidType())
588 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
589 else if (exprType->isIncompleteType()) {
590 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
591 diag::err_alignof_incomplete_type,
592 exprType.getAsString());
593 return QualType(); // error
594 }
595 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
596 return Context.getSizeType();
597}
598
599Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000600ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000601 SourceLocation LPLoc, TypeTy *Ty,
602 SourceLocation RPLoc) {
603 // If error parsing type, ignore.
604 if (Ty == 0) return true;
605
606 // Verify that this is a valid expression.
607 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
608
609 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
610
611 if (resultType.isNull())
612 return true;
613 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
614}
615
Chris Lattner5110ad52007-08-24 21:41:10 +0000616QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000617 DefaultFunctionArrayConversion(V);
618
Chris Lattnera16e42d2007-08-26 05:39:26 +0000619 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000620 if (const ComplexType *CT = V->getType()->getAsComplexType())
621 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000622
623 // Otherwise they pass through real integer and floating point types here.
624 if (V->getType()->isArithmeticType())
625 return V->getType();
626
627 // Reject anything else.
628 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
629 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000630}
631
632
Chris Lattner4b009652007-07-25 00:24:17 +0000633
Steve Naroff87d58b42007-09-16 03:34:24 +0000634Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000635 tok::TokenKind Kind,
636 ExprTy *Input) {
637 UnaryOperator::Opcode Opc;
638 switch (Kind) {
639 default: assert(0 && "Unknown unary op!");
640 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
641 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
642 }
643 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
644 if (result.isNull())
645 return true;
646 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
647}
648
649Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000650ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000651 ExprTy *Idx, SourceLocation RLoc) {
652 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
653
654 // Perform default conversions.
655 DefaultFunctionArrayConversion(LHSExp);
656 DefaultFunctionArrayConversion(RHSExp);
657
658 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
659
660 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000661 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000662 // in the subscript position. As a result, we need to derive the array base
663 // and index from the expression types.
664 Expr *BaseExpr, *IndexExpr;
665 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000666 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000667 BaseExpr = LHSExp;
668 IndexExpr = RHSExp;
669 // FIXME: need to deal with const...
670 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000671 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000672 // Handle the uncommon case of "123[Ptr]".
673 BaseExpr = RHSExp;
674 IndexExpr = LHSExp;
675 // FIXME: need to deal with const...
676 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000677 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
678 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000679 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000680
681 // Component access limited to variables (reject vec4.rg[1]).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000682 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
683 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000684 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000685 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000686 // FIXME: need to deal with const...
687 ResultType = VTy->getElementType();
688 } else {
689 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
690 RHSExp->getSourceRange());
691 }
692 // C99 6.5.2.1p1
693 if (!IndexExpr->getType()->isIntegerType())
694 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
695 IndexExpr->getSourceRange());
696
697 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
698 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattner9db553e2008-04-02 06:59:01 +0000699 // void (*)(int)) and pointers to incomplete types. Functions are not
700 // objects in C99.
Chris Lattner4b009652007-07-25 00:24:17 +0000701 if (!ResultType->isObjectType())
702 return Diag(BaseExpr->getLocStart(),
703 diag::err_typecheck_subscript_not_object,
704 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
705
706 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
707}
708
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000709QualType Sema::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000710CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000711 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000712 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begemanc8e51f82008-05-09 06:41:27 +0000713
714 // This flag determines whether or not the component is to be treated as a
715 // special name, or a regular GLSL-style component access.
716 bool SpecialComponent = false;
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000717
718 // The vector accessor can't exceed the number of elements.
719 const char *compStr = CompName.getName();
720 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000721 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000722 baseType.getAsString(), SourceRange(CompLoc));
723 return QualType();
724 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000725
726 // Check that we've found one of the special components, or that the component
727 // names must come from the same set.
728 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
729 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
730 SpecialComponent = true;
731 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner9096b792007-08-02 22:33:49 +0000732 do
733 compStr++;
734 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
735 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
736 do
737 compStr++;
738 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
739 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
740 do
741 compStr++;
742 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
743 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000744
Nate Begemanc8e51f82008-05-09 06:41:27 +0000745 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000746 // We didn't get to the end of the string. This means the component names
747 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000748 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000749 std::string(compStr,compStr+1), SourceRange(CompLoc));
750 return QualType();
751 }
752 // Each component accessor can't exceed the vector type.
753 compStr = CompName.getName();
754 while (*compStr) {
755 if (vecType->isAccessorWithinNumElements(*compStr))
756 compStr++;
757 else
758 break;
759 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000760 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000761 // We didn't get to the end of the string. This means a component accessor
762 // exceeds the number of elements in the vector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000763 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000764 baseType.getAsString(), SourceRange(CompLoc));
765 return QualType();
766 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000767
768 // If we have a special component name, verify that the current vector length
769 // is an even number, since all special component names return exactly half
770 // the elements.
771 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
772 return QualType();
773 }
774
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000775 // The component accessor looks fine - now we need to compute the actual type.
776 // The vector type is implied by the component accessor. For example,
777 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begemanc8e51f82008-05-09 06:41:27 +0000778 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
779 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
780 : strlen(CompName.getName());
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000781 if (CompSize == 1)
782 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000783
Nate Begemanaf6ed502008-04-18 23:10:10 +0000784 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroff82113e32007-07-29 16:33:31 +0000785 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemanaf6ed502008-04-18 23:10:10 +0000786 // diagostics look bad. We want extended vector types to appear built-in.
787 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
788 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
789 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroff82113e32007-07-29 16:33:31 +0000790 }
791 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000792}
793
Chris Lattner4b009652007-07-25 00:24:17 +0000794Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000795ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000796 tok::TokenKind OpKind, SourceLocation MemberLoc,
797 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000798 Expr *BaseExpr = static_cast<Expr *>(Base);
799 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000800
801 // Perform default conversions.
802 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000803
Steve Naroff2cb66382007-07-26 03:11:44 +0000804 QualType BaseType = BaseExpr->getType();
805 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000806
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000807 // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr
808 // must have pointer type, and the accessed type is the pointee.
Chris Lattner4b009652007-07-25 00:24:17 +0000809 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000810 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000811 BaseType = PT->getPointeeType();
812 else
Chris Lattner7d5a8762008-07-21 05:35:34 +0000813 return Diag(MemberLoc, diag::err_typecheck_member_reference_arrow,
814 BaseType.getAsString(), BaseExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000815 }
Chris Lattnera57cf472008-07-21 04:28:12 +0000816
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000817 // Handle field access to simple records. This also handles access to fields
818 // of the ObjC 'id' struct.
Chris Lattnere35a1042007-07-31 19:29:30 +0000819 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000820 RecordDecl *RDecl = RTy->getDecl();
821 if (RTy->isIncompleteType())
822 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
823 BaseExpr->getSourceRange());
824 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000825 FieldDecl *MemberDecl = RDecl->getMember(&Member);
826 if (!MemberDecl)
Chris Lattner7d5a8762008-07-21 05:35:34 +0000827 return Diag(MemberLoc, diag::err_typecheck_no_member, Member.getName(),
828 BaseExpr->getSourceRange());
Eli Friedman76b49832008-02-06 22:48:16 +0000829
830 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000831 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000832 QualType MemberType = MemberDecl->getType();
833 unsigned combinedQualifiers =
Chris Lattner35fef522008-02-20 20:55:12 +0000834 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman76b49832008-02-06 22:48:16 +0000835 MemberType = MemberType.getQualifiedType(combinedQualifiers);
836
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000837 return new MemberExpr(BaseExpr, OpKind == tok::arrow, MemberDecl,
Eli Friedman76b49832008-02-06 22:48:16 +0000838 MemberLoc, MemberType);
Chris Lattnera57cf472008-07-21 04:28:12 +0000839 }
840
Chris Lattnere9d71612008-07-21 04:59:05 +0000841 // Handle access to Objective-C instance variables, such as "Obj->ivar" and
842 // (*Obj).ivar.
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000843 if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) {
844 if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000845 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
Chris Lattnera57cf472008-07-21 04:28:12 +0000846 OpKind == tok::arrow);
Chris Lattner7d5a8762008-07-21 05:35:34 +0000847 return Diag(MemberLoc, diag::err_typecheck_member_reference_ivar,
Chris Lattner52292be2008-07-21 04:42:08 +0000848 IFTy->getDecl()->getName(), Member.getName(),
Chris Lattner7d5a8762008-07-21 05:35:34 +0000849 BaseExpr->getSourceRange());
Chris Lattnera57cf472008-07-21 04:28:12 +0000850 }
851
Chris Lattnere9d71612008-07-21 04:59:05 +0000852 // Handle Objective-C property access, which is "Obj.property" where Obj is a
853 // pointer to a (potentially qualified) interface type.
854 const PointerType *PTy;
855 const ObjCInterfaceType *IFTy;
856 if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) &&
857 (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) {
858 ObjCInterfaceDecl *IFace = IFTy->getDecl();
859
Chris Lattner55a24332008-07-21 06:44:27 +0000860 // FIXME: The logic for looking up nullary and unary selectors should be
861 // shared with the code in ActOnInstanceMessage.
862
Chris Lattnere9d71612008-07-21 04:59:05 +0000863 // Before we look for explicit property declarations, we check for
864 // nullary methods (which allow '.' notation).
865 Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
Chris Lattnere9d71612008-07-21 04:59:05 +0000866 if (ObjCMethodDecl *MD = IFace->lookupInstanceMethod(Sel))
867 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
868 MemberLoc, BaseExpr);
869
Chris Lattner55a24332008-07-21 06:44:27 +0000870 // If this reference is in an @implementation, check for 'private' methods.
871 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
872 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
873 if (ObjCImplementationDecl *ImpDecl =
874 ObjCImplementations[ClassDecl->getIdentifier()])
875 if (ObjCMethodDecl *MD = ImpDecl->getInstanceMethod(Sel))
876 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
877 MemberLoc, BaseExpr);
878 }
879
Chris Lattnere9d71612008-07-21 04:59:05 +0000880 // FIXME: Need to deal with setter methods that take 1 argument. E.g.:
881 // @interface NSBundle : NSObject {}
882 // - (NSString *)bundlePath;
883 // - (void)setBundlePath:(NSString *)x;
884 // @end
885 // void someMethod() { frameworkBundle.bundlePath = 0; }
886 //
887 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member))
888 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
889
890 // Lastly, check protocols on qualified interfaces.
Chris Lattnerd5f81792008-07-21 05:20:01 +0000891 for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(),
892 E = IFTy->qual_end(); I != E; ++I)
893 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member))
894 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000895 }
Chris Lattnera57cf472008-07-21 04:28:12 +0000896
897 // Handle 'field access' to vectors, such as 'V.xx'.
898 if (BaseType->isExtVectorType() && OpKind == tok::period) {
899 // Component access limited to variables (reject vec4.rg.g).
900 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
901 !isa<ExtVectorElementExpr>(BaseExpr))
Chris Lattner7d5a8762008-07-21 05:35:34 +0000902 return Diag(MemberLoc, diag::err_ext_vector_component_access,
903 BaseExpr->getSourceRange());
Chris Lattnera57cf472008-07-21 04:28:12 +0000904 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
905 if (ret.isNull())
906 return true;
907 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
908 }
909
Chris Lattner7d5a8762008-07-21 05:35:34 +0000910 return Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union,
911 BaseType.getAsString(), BaseExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000912}
913
Steve Naroff87d58b42007-09-16 03:34:24 +0000914/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000915/// This provides the location of the left/right parens and a list of comma
916/// locations.
917Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000918ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000919 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +0000920 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
921 Expr *Fn = static_cast<Expr *>(fn);
922 Expr **Args = reinterpret_cast<Expr**>(args);
923 assert(Fn && "no function call expression");
Chris Lattner3e254fb2008-04-08 04:40:51 +0000924 FunctionDecl *FDecl = NULL;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000925
926 // Promote the function operand.
927 UsualUnaryConversions(Fn);
928
929 // If we're directly calling a function, get the declaration for
930 // that function.
931 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
932 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
933 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
934
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000935 // Make the call expr early, before semantic checks. This guarantees cleanup
936 // of arguments and function on error.
Chris Lattner97316c02008-04-10 02:22:51 +0000937 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000938 Context.BoolTy, RParenLoc));
939
Chris Lattner4b009652007-07-25 00:24:17 +0000940 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
941 // type pointer to function".
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000942 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000943 if (PT == 0)
944 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
945 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000946 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
947 if (FuncT == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000948 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
949 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000950
951 // We know the result type of the call, set it.
952 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +0000953
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000954 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000955 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
956 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000957 unsigned NumArgsInProto = Proto->getNumArgs();
958 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +0000959
Chris Lattner3e254fb2008-04-08 04:40:51 +0000960 // If too few arguments are available (and we don't have default
961 // arguments for the remaining parameters), don't make the call.
962 if (NumArgs < NumArgsInProto) {
Chris Lattner97316c02008-04-10 02:22:51 +0000963 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000964 // Use default arguments for missing arguments
965 NumArgsToCheck = NumArgsInProto;
Chris Lattner97316c02008-04-10 02:22:51 +0000966 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000967 } else
968 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
969 Fn->getSourceRange());
970 }
971
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000972 // If too many are passed and not variadic, error on the extras and drop
973 // them.
974 if (NumArgs > NumArgsInProto) {
975 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000976 Diag(Args[NumArgsInProto]->getLocStart(),
977 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
978 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000979 Args[NumArgs-1]->getLocEnd()));
980 // This deletes the extra arguments.
981 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +0000982 }
983 NumArgsToCheck = NumArgsInProto;
984 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000985
Chris Lattner4b009652007-07-25 00:24:17 +0000986 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000987 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner005ed752008-01-04 18:04:52 +0000988 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000989
990 Expr *Arg;
991 if (i < NumArgs)
992 Arg = Args[i];
993 else
994 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner005ed752008-01-04 18:04:52 +0000995 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000996
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000997 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +0000998 AssignConvertType ConvTy =
999 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001000 TheCall->setArg(i, Arg);
1001
Chris Lattner005ed752008-01-04 18:04:52 +00001002 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
1003 ArgType, Arg, "passing"))
1004 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001005 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001006
1007 // If this is a variadic call, handle args passed through "...".
1008 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +00001009 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001010 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
1011 Expr *Arg = Args[i];
1012 DefaultArgumentPromotion(Arg);
1013 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +00001014 }
Steve Naroffdb65e052007-08-28 23:30:39 +00001015 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001016 } else {
1017 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
1018
Steve Naroffdb65e052007-08-28 23:30:39 +00001019 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001020 for (unsigned i = 0; i != NumArgs; i++) {
1021 Expr *Arg = Args[i];
1022 DefaultArgumentPromotion(Arg);
1023 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +00001024 }
Chris Lattner4b009652007-07-25 00:24:17 +00001025 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001026
Chris Lattner2e64c072007-08-10 20:18:51 +00001027 // Do special checking on direct calls to functions.
Eli Friedmand0e9d092008-05-14 19:38:39 +00001028 if (FDecl)
1029 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattner2e64c072007-08-10 20:18:51 +00001030
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001031 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +00001032}
1033
1034Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001035ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +00001036 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +00001037 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001038 QualType literalType = QualType::getFromOpaquePtr(Ty);
1039 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +00001040 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +00001041 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +00001042
Eli Friedman8c2173d2008-05-20 05:22:08 +00001043 if (literalType->isArrayType()) {
1044 if (literalType->getAsVariableArrayType())
1045 return Diag(LParenLoc,
1046 diag::err_variable_object_no_init,
1047 SourceRange(LParenLoc,
1048 literalExpr->getSourceRange().getEnd()));
1049 } else if (literalType->isIncompleteType()) {
1050 return Diag(LParenLoc,
1051 diag::err_typecheck_decl_incomplete_type,
1052 literalType.getAsString(),
1053 SourceRange(LParenLoc,
1054 literalExpr->getSourceRange().getEnd()));
1055 }
1056
Steve Narofff0b23542008-01-10 22:15:12 +00001057 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +00001058 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +00001059
Argiris Kirtzidis95256e62008-06-28 06:07:14 +00001060 bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl();
Steve Naroffbe37fc02008-01-14 18:19:28 +00001061 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +00001062 if (CheckForConstantInitializer(literalExpr, literalType))
1063 return true;
1064 }
Steve Naroffbe37fc02008-01-14 18:19:28 +00001065 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001066}
1067
1068Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001069ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +00001070 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +00001071 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +00001072
Steve Naroff0acc9c92007-09-15 18:49:24 +00001073 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +00001074 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +00001075
Chris Lattner48d7f382008-04-02 04:24:33 +00001076 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
1077 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
1078 return E;
Chris Lattner4b009652007-07-25 00:24:17 +00001079}
1080
Chris Lattnerd1f26b32007-12-20 00:44:32 +00001081bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001082 assert(VectorTy->isVectorType() && "Not a vector type!");
1083
1084 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001085 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001086 return Diag(R.getBegin(),
1087 Ty->isVectorType() ?
1088 diag::err_invalid_conversion_between_vectors :
1089 diag::err_invalid_conversion_between_vector_and_integer,
1090 VectorTy.getAsString().c_str(),
1091 Ty.getAsString().c_str(), R);
1092 } else
1093 return Diag(R.getBegin(),
1094 diag::err_invalid_conversion_between_vector_and_scalar,
1095 VectorTy.getAsString().c_str(),
1096 Ty.getAsString().c_str(), R);
1097
1098 return false;
1099}
1100
Chris Lattner4b009652007-07-25 00:24:17 +00001101Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001102ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +00001103 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +00001104 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +00001105
1106 Expr *castExpr = static_cast<Expr*>(Op);
1107 QualType castType = QualType::getFromOpaquePtr(Ty);
1108
Steve Naroff68adb482007-08-31 00:32:44 +00001109 UsualUnaryConversions(castExpr);
1110
Chris Lattner4b009652007-07-25 00:24:17 +00001111 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
1112 // type needs to be scalar.
Chris Lattnerdb526732007-10-29 04:26:44 +00001113 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff5ad85292008-06-03 12:56:35 +00001114 if (!castType->isScalarType() && !castType->isVectorType()) {
1115 // GCC struct/union extension.
1116 if (castType == castExpr->getType() &&
Steve Naroff7f1c5b52008-06-03 13:21:30 +00001117 castType->isStructureType() || castType->isUnionType()) {
1118 Diag(LParenLoc, diag::ext_typecheck_cast_nonscalar,
1119 SourceRange(LParenLoc, RParenLoc));
1120 return new CastExpr(castType, castExpr, LParenLoc);
1121 } else
Steve Naroff5ad85292008-06-03 12:56:35 +00001122 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
1123 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
1124 }
Steve Narofff459ee52008-01-24 22:55:05 +00001125 if (!castExpr->getType()->isScalarType() &&
1126 !castExpr->getType()->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +00001127 return Diag(castExpr->getLocStart(),
1128 diag::err_typecheck_expect_scalar_operand,
1129 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001130
1131 if (castExpr->getType()->isVectorType()) {
1132 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
1133 castExpr->getType(), castType))
1134 return true;
1135 } else if (castType->isVectorType()) {
1136 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
1137 castType, castExpr->getType()))
1138 return true;
Chris Lattnerdb526732007-10-29 04:26:44 +00001139 }
Chris Lattner4b009652007-07-25 00:24:17 +00001140 }
1141 return new CastExpr(castType, castExpr, LParenLoc);
1142}
1143
Chris Lattner98a425c2007-11-26 01:40:58 +00001144/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
1145/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +00001146inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
1147 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
1148 UsualUnaryConversions(cond);
1149 UsualUnaryConversions(lex);
1150 UsualUnaryConversions(rex);
1151 QualType condT = cond->getType();
1152 QualType lexT = lex->getType();
1153 QualType rexT = rex->getType();
1154
1155 // first, check the condition.
1156 if (!condT->isScalarType()) { // C99 6.5.15p2
1157 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
1158 condT.getAsString());
1159 return QualType();
1160 }
Chris Lattner992ae932008-01-06 22:42:25 +00001161
1162 // Now check the two expressions.
1163
1164 // If both operands have arithmetic type, do the usual arithmetic conversions
1165 // to find a common type: C99 6.5.15p3,5.
1166 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001167 UsualArithmeticConversions(lex, rex);
1168 return lex->getType();
1169 }
Chris Lattner992ae932008-01-06 22:42:25 +00001170
1171 // If both operands are the same structure or union type, the result is that
1172 // type.
Chris Lattner71225142007-07-31 21:27:01 +00001173 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +00001174 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +00001175 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +00001176 // "If both the operands have structure or union type, the result has
1177 // that type." This implies that CV qualifiers are dropped.
1178 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001179 }
Chris Lattner992ae932008-01-06 22:42:25 +00001180
1181 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroff95cb3892008-05-12 21:44:38 +00001182 // The following || allows only one side to be void (a GCC-ism).
1183 if (lexT->isVoidType() || rexT->isVoidType()) {
Eli Friedmanf025aac2008-06-04 19:47:51 +00001184 if (!lexT->isVoidType())
Steve Naroff95cb3892008-05-12 21:44:38 +00001185 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
1186 rex->getSourceRange());
1187 if (!rexT->isVoidType())
1188 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
Nuno Lopes4ba41fd2008-06-04 19:14:12 +00001189 lex->getSourceRange());
Eli Friedmanf025aac2008-06-04 19:47:51 +00001190 ImpCastExprToType(lex, Context.VoidTy);
1191 ImpCastExprToType(rex, Context.VoidTy);
1192 return Context.VoidTy;
Steve Naroff95cb3892008-05-12 21:44:38 +00001193 }
Steve Naroff12ebf272008-01-08 01:11:38 +00001194 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
1195 // the type of the other operand."
1196 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001197 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +00001198 return lexT;
1199 }
1200 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001201 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +00001202 return rexT;
1203 }
Chris Lattner0ac51632008-01-06 22:50:31 +00001204 // Handle the case where both operands are pointers before we handle null
1205 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +00001206 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
1207 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
1208 // get the "pointed to" types
1209 QualType lhptee = LHSPT->getPointeeType();
1210 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001211
Chris Lattner71225142007-07-31 21:27:01 +00001212 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
1213 if (lhptee->isVoidType() &&
Chris Lattner9db553e2008-04-02 06:59:01 +00001214 rhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +00001215 // Figure out necessary qualifiers (C99 6.5.15p6)
1216 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +00001217 QualType destType = Context.getPointerType(destPointee);
1218 ImpCastExprToType(lex, destType); // add qualifiers if necessary
1219 ImpCastExprToType(rex, destType); // promote to void*
1220 return destType;
1221 }
Chris Lattner9db553e2008-04-02 06:59:01 +00001222 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +00001223 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +00001224 QualType destType = Context.getPointerType(destPointee);
1225 ImpCastExprToType(lex, destType); // add qualifiers if necessary
1226 ImpCastExprToType(rex, destType); // promote to void*
1227 return destType;
1228 }
Chris Lattner4b009652007-07-25 00:24:17 +00001229
Steve Naroff85f0dc52007-10-15 20:41:53 +00001230 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1231 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +00001232 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +00001233 lexT.getAsString(), rexT.getAsString(),
1234 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman33284862008-01-30 17:02:03 +00001235 // In this situation, we assume void* type. No especially good
1236 // reason, but this is what gcc does, and we do have to pick
1237 // to get a consistent AST.
1238 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
1239 ImpCastExprToType(lex, voidPtrTy);
1240 ImpCastExprToType(rex, voidPtrTy);
1241 return voidPtrTy;
Chris Lattner71225142007-07-31 21:27:01 +00001242 }
1243 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001244 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
1245 // differently qualified versions of compatible types, the result type is
1246 // a pointer to an appropriately qualified version of the *composite*
1247 // type.
Eli Friedmane38150e2008-05-16 20:37:07 +00001248 // FIXME: Need to calculate the composite type.
Eli Friedmanca07c902008-02-10 22:59:36 +00001249 // FIXME: Need to add qualifiers
Eli Friedmane38150e2008-05-16 20:37:07 +00001250 QualType compositeType = lexT;
1251 ImpCastExprToType(lex, compositeType);
1252 ImpCastExprToType(rex, compositeType);
1253 return compositeType;
Chris Lattner4b009652007-07-25 00:24:17 +00001254 }
Chris Lattner4b009652007-07-25 00:24:17 +00001255 }
Steve Naroff605896f2008-05-31 22:33:45 +00001256 // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type
1257 // evaluates to "struct objc_object *" (and is handled above when comparing
1258 // id with statically typed objects). FIXME: Do we need an ImpCastExprToType?
1259 if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) {
1260 if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true))
1261 return Context.getObjCIdType();
1262 }
Chris Lattner992ae932008-01-06 22:42:25 +00001263 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +00001264 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
1265 lexT.getAsString(), rexT.getAsString(),
1266 lex->getSourceRange(), rex->getSourceRange());
1267 return QualType();
1268}
1269
Steve Naroff87d58b42007-09-16 03:34:24 +00001270/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +00001271/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +00001272Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001273 SourceLocation ColonLoc,
1274 ExprTy *Cond, ExprTy *LHS,
1275 ExprTy *RHS) {
1276 Expr *CondExpr = (Expr *) Cond;
1277 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +00001278
1279 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
1280 // was the condition.
1281 bool isLHSNull = LHSExpr == 0;
1282 if (isLHSNull)
1283 LHSExpr = CondExpr;
1284
Chris Lattner4b009652007-07-25 00:24:17 +00001285 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
1286 RHSExpr, QuestionLoc);
1287 if (result.isNull())
1288 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +00001289 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
1290 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +00001291}
1292
Chris Lattner4b009652007-07-25 00:24:17 +00001293
1294// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1295// being closely modeled after the C99 spec:-). The odd characteristic of this
1296// routine is it effectively iqnores the qualifiers on the top level pointee.
1297// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1298// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001299Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001300Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1301 QualType lhptee, rhptee;
1302
1303 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001304 lhptee = lhsType->getAsPointerType()->getPointeeType();
1305 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001306
1307 // make sure we operate on the canonical type
1308 lhptee = lhptee.getCanonicalType();
1309 rhptee = rhptee.getCanonicalType();
1310
Chris Lattner005ed752008-01-04 18:04:52 +00001311 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001312
1313 // C99 6.5.16.1p1: This following citation is common to constraints
1314 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1315 // qualifiers of the type *pointed to* by the right;
Chris Lattner35fef522008-02-20 20:55:12 +00001316 // FIXME: Handle ASQualType
1317 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1318 rhptee.getCVRQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001319 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001320
1321 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1322 // incomplete type and the other is a pointer to a qualified or unqualified
1323 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001324 if (lhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001325 if (rhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001326 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001327
1328 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001329 assert(rhptee->isFunctionType());
1330 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001331 }
1332
1333 if (rhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001334 if (lhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001335 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001336
1337 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001338 assert(lhptee->isFunctionType());
1339 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001340 }
1341
Chris Lattner4b009652007-07-25 00:24:17 +00001342 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1343 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001344 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1345 rhptee.getUnqualifiedType()))
1346 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001347 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001348}
1349
1350/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1351/// has code to accommodate several GCC extensions when type checking
1352/// pointers. Here are some objectionable examples that GCC considers warnings:
1353///
1354/// int a, *pint;
1355/// short *pshort;
1356/// struct foo *pfoo;
1357///
1358/// pint = pshort; // warning: assignment from incompatible pointer type
1359/// a = pint; // warning: assignment makes integer from pointer without a cast
1360/// pint = a; // warning: assignment makes pointer from integer without a cast
1361/// pint = pfoo; // warning: assignment from incompatible pointer type
1362///
1363/// As a result, the code for dealing with pointers is more complex than the
1364/// C99 spec dictates.
Chris Lattner4b009652007-07-25 00:24:17 +00001365///
Chris Lattner005ed752008-01-04 18:04:52 +00001366Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001367Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001368 // Get canonical types. We're not formatting these types, just comparing
1369 // them.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001370 lhsType = lhsType.getCanonicalType().getUnqualifiedType();
1371 rhsType = rhsType.getCanonicalType().getUnqualifiedType();
1372
1373 if (lhsType == rhsType)
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001374 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001375
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001376 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattnere1577e22008-04-07 06:52:53 +00001377 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001378 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001379 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001380 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001381
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001382 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1383 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001384 return Compatible;
Steve Naroff936c4362008-06-03 14:04:54 +00001385 // Relax integer conversions like we do for pointers below.
1386 if (rhsType->isIntegerType())
1387 return IntToPointer;
1388 if (lhsType->isIntegerType())
1389 return PointerToInt;
Chris Lattner1853da22008-01-04 23:18:45 +00001390 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001391 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001392
Nate Begemanc5f0f652008-07-14 18:02:46 +00001393 if (lhsType->isVectorType() || rhsType->isVectorType()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001394 // For ExtVector, allow vector splats; float -> <n x float>
Nate Begemanc5f0f652008-07-14 18:02:46 +00001395 if (const ExtVectorType *LV = lhsType->getAsExtVectorType())
1396 if (LV->getElementType() == rhsType)
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001397 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001398
Nate Begemanc5f0f652008-07-14 18:02:46 +00001399 // If we are allowing lax vector conversions, and LHS and RHS are both
1400 // vectors, the total size only needs to be the same. This is a bitcast;
1401 // no bits are changed but the result type is different.
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001402 if (getLangOptions().LaxVectorConversions &&
1403 lhsType->isVectorType() && rhsType->isVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001404 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
1405 return Compatible;
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001406 }
1407 return Incompatible;
1408 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001409
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001410 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001411 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001412
Chris Lattner390564e2008-04-07 06:49:41 +00001413 if (isa<PointerType>(lhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001414 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001415 return IntToPointer;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001416
Chris Lattner390564e2008-04-07 06:49:41 +00001417 if (isa<PointerType>(rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001418 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001419 return Incompatible;
1420 }
1421
Chris Lattner390564e2008-04-07 06:49:41 +00001422 if (isa<PointerType>(rhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001423 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001424 if (lhsType == Context.BoolTy)
1425 return Compatible;
1426
1427 if (lhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001428 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001429
Chris Lattner390564e2008-04-07 06:49:41 +00001430 if (isa<PointerType>(lhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001431 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001432 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001433 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001434
Chris Lattner1853da22008-01-04 23:18:45 +00001435 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner390564e2008-04-07 06:49:41 +00001436 if (Context.typesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001437 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001438 }
1439 return Incompatible;
1440}
1441
Chris Lattner005ed752008-01-04 18:04:52 +00001442Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001443Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001444 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1445 // a null pointer constant.
Ted Kremenek42730c52008-01-07 19:49:32 +00001446 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001447 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001448 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001449 return Compatible;
1450 }
Chris Lattner5f505bf2007-10-16 02:55:40 +00001451 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001452 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001453 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001454 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001455 //
1456 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1457 // are better understood.
1458 if (!lhsType->isReferenceType())
1459 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001460
Chris Lattner005ed752008-01-04 18:04:52 +00001461 Sema::AssignConvertType result =
1462 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001463
1464 // C99 6.5.16.1p2: The value of the right operand is converted to the
1465 // type of the assignment expression.
1466 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001467 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001468 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001469}
1470
Chris Lattner005ed752008-01-04 18:04:52 +00001471Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001472Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1473 return CheckAssignmentConstraints(lhsType, rhsType);
1474}
1475
Chris Lattner2c8bff72007-12-12 05:47:28 +00001476QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001477 Diag(loc, diag::err_typecheck_invalid_operands,
1478 lex->getType().getAsString(), rex->getType().getAsString(),
1479 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001480 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001481}
1482
1483inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1484 Expr *&rex) {
Nate Begeman03105572008-04-04 01:30:25 +00001485 // For conversion purposes, we ignore any qualifiers.
1486 // For example, "const float" and "float" are equivalent.
1487 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1488 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001489
Nate Begemanc5f0f652008-07-14 18:02:46 +00001490 // If the vector types are identical, return.
Nate Begeman03105572008-04-04 01:30:25 +00001491 if (lhsType == rhsType)
Chris Lattner4b009652007-07-25 00:24:17 +00001492 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001493
Nate Begemanc5f0f652008-07-14 18:02:46 +00001494 // Handle the case of a vector & extvector type of the same size and element
1495 // type. It would be nice if we only had one vector type someday.
1496 if (getLangOptions().LaxVectorConversions)
1497 if (const VectorType *LV = lhsType->getAsVectorType())
1498 if (const VectorType *RV = rhsType->getAsVectorType())
1499 if (LV->getElementType() == RV->getElementType() &&
1500 LV->getNumElements() == RV->getNumElements())
1501 return lhsType->isExtVectorType() ? lhsType : rhsType;
1502
1503 // If the lhs is an extended vector and the rhs is a scalar of the same type
1504 // or a literal, promote the rhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001505 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001506 QualType eltType = V->getElementType();
1507
1508 if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) ||
1509 (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) ||
1510 (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001511 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001512 return lhsType;
1513 }
1514 }
1515
Nate Begemanc5f0f652008-07-14 18:02:46 +00001516 // If the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001517 // promote the lhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001518 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001519 QualType eltType = V->getElementType();
1520
1521 if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) ||
1522 (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) ||
1523 (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001524 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001525 return rhsType;
1526 }
1527 }
1528
Chris Lattner4b009652007-07-25 00:24:17 +00001529 // You cannot convert between vector values of different size.
1530 Diag(loc, diag::err_typecheck_vector_not_convertable,
1531 lex->getType().getAsString(), rex->getType().getAsString(),
1532 lex->getSourceRange(), rex->getSourceRange());
1533 return QualType();
1534}
1535
1536inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001537 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001538{
1539 QualType lhsType = lex->getType(), rhsType = rex->getType();
1540
1541 if (lhsType->isVectorType() || rhsType->isVectorType())
1542 return CheckVectorOperands(loc, lex, rex);
1543
Steve Naroff8f708362007-08-24 19:07:16 +00001544 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001545
Chris Lattner4b009652007-07-25 00:24:17 +00001546 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001547 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001548 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001549}
1550
1551inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001552 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001553{
1554 QualType lhsType = lex->getType(), rhsType = rex->getType();
1555
Steve Naroff8f708362007-08-24 19:07:16 +00001556 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001557
Chris Lattner4b009652007-07-25 00:24:17 +00001558 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001559 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001560 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001561}
1562
1563inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001564 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001565{
1566 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1567 return CheckVectorOperands(loc, lex, rex);
1568
Steve Naroff8f708362007-08-24 19:07:16 +00001569 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001570
Chris Lattner4b009652007-07-25 00:24:17 +00001571 // handle the common case first (both operands are arithmetic).
1572 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001573 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001574
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001575 // Put any potential pointer into PExp
1576 Expr* PExp = lex, *IExp = rex;
1577 if (IExp->getType()->isPointerType())
1578 std::swap(PExp, IExp);
1579
1580 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1581 if (IExp->getType()->isIntegerType()) {
1582 // Check for arithmetic on pointers to incomplete types
1583 if (!PTy->getPointeeType()->isObjectType()) {
1584 if (PTy->getPointeeType()->isVoidType()) {
1585 Diag(loc, diag::ext_gnu_void_ptr,
1586 lex->getSourceRange(), rex->getSourceRange());
1587 } else {
1588 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1589 lex->getType().getAsString(), lex->getSourceRange());
1590 return QualType();
1591 }
1592 }
1593 return PExp->getType();
1594 }
1595 }
1596
Chris Lattner2c8bff72007-12-12 05:47:28 +00001597 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001598}
1599
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001600// C99 6.5.6
1601QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1602 SourceLocation loc, bool isCompAssign) {
Chris Lattner4b009652007-07-25 00:24:17 +00001603 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1604 return CheckVectorOperands(loc, lex, rex);
1605
Steve Naroff8f708362007-08-24 19:07:16 +00001606 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001607
Chris Lattnerf6da2912007-12-09 21:53:25 +00001608 // Enforce type constraints: C99 6.5.6p3.
1609
1610 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001611 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001612 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001613
1614 // Either ptr - int or ptr - ptr.
1615 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001616 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001617
Chris Lattnerf6da2912007-12-09 21:53:25 +00001618 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001619 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001620 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001621 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001622 Diag(loc, diag::ext_gnu_void_ptr,
1623 lex->getSourceRange(), rex->getSourceRange());
1624 } else {
1625 Diag(loc, diag::err_typecheck_sub_ptr_object,
1626 lex->getType().getAsString(), lex->getSourceRange());
1627 return QualType();
1628 }
1629 }
1630
1631 // The result type of a pointer-int computation is the pointer type.
1632 if (rex->getType()->isIntegerType())
1633 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001634
Chris Lattnerf6da2912007-12-09 21:53:25 +00001635 // Handle pointer-pointer subtractions.
1636 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001637 QualType rpointee = RHSPTy->getPointeeType();
1638
Chris Lattnerf6da2912007-12-09 21:53:25 +00001639 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001640 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001641 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001642 if (rpointee->isVoidType()) {
1643 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-12-09 21:53:25 +00001644 Diag(loc, diag::ext_gnu_void_ptr,
1645 lex->getSourceRange(), rex->getSourceRange());
1646 } else {
1647 Diag(loc, diag::err_typecheck_sub_ptr_object,
1648 rex->getType().getAsString(), rex->getSourceRange());
1649 return QualType();
1650 }
1651 }
1652
1653 // Pointee types must be compatible.
Steve Naroff577f9722008-01-29 18:58:14 +00001654 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1655 rpointee.getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001656 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1657 lex->getType().getAsString(), rex->getType().getAsString(),
1658 lex->getSourceRange(), rex->getSourceRange());
1659 return QualType();
1660 }
1661
1662 return Context.getPointerDiffType();
1663 }
1664 }
1665
Chris Lattner2c8bff72007-12-12 05:47:28 +00001666 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001667}
1668
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001669// C99 6.5.7
1670QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1671 bool isCompAssign) {
Chris Lattner2c8bff72007-12-12 05:47:28 +00001672 // C99 6.5.7p2: Each of the operands shall have integer type.
1673 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1674 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001675
Chris Lattner2c8bff72007-12-12 05:47:28 +00001676 // Shifts don't perform usual arithmetic conversions, they just do integer
1677 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001678 if (!isCompAssign)
1679 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001680 UsualUnaryConversions(rex);
1681
1682 // "The type of the result is that of the promoted left operand."
1683 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001684}
1685
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001686// C99 6.5.8
1687QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1688 bool isRelational) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001689 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1690 return CheckVectorCompareOperands(lex, rex, loc, isRelational);
1691
Chris Lattner254f3bc2007-08-26 01:18:55 +00001692 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001693 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1694 UsualArithmeticConversions(lex, rex);
1695 else {
1696 UsualUnaryConversions(lex);
1697 UsualUnaryConversions(rex);
1698 }
Chris Lattner4b009652007-07-25 00:24:17 +00001699 QualType lType = lex->getType();
1700 QualType rType = rex->getType();
1701
Ted Kremenek486509e2007-10-29 17:13:39 +00001702 // For non-floating point types, check for self-comparisons of the form
1703 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1704 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001705 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001706 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1707 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001708 if (DRL->getDecl() == DRR->getDecl())
1709 Diag(loc, diag::warn_selfcomparison);
1710 }
1711
Chris Lattner254f3bc2007-08-26 01:18:55 +00001712 if (isRelational) {
1713 if (lType->isRealType() && rType->isRealType())
1714 return Context.IntTy;
1715 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001716 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001717 if (lType->isFloatingType()) {
1718 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001719 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001720 }
1721
Chris Lattner254f3bc2007-08-26 01:18:55 +00001722 if (lType->isArithmeticType() && rType->isArithmeticType())
1723 return Context.IntTy;
1724 }
Chris Lattner4b009652007-07-25 00:24:17 +00001725
Chris Lattner22be8422007-08-26 01:10:14 +00001726 bool LHSIsNull = lex->isNullPointerConstant(Context);
1727 bool RHSIsNull = rex->isNullPointerConstant(Context);
1728
Chris Lattner254f3bc2007-08-26 01:18:55 +00001729 // All of the following pointer related warnings are GCC extensions, except
1730 // when handling null pointer constants. One day, we can consider making them
1731 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001732 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001733 QualType LCanPointeeTy =
1734 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1735 QualType RCanPointeeTy =
1736 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman50727042008-02-08 01:19:44 +00001737
Steve Naroff3b435622007-11-13 14:57:38 +00001738 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001739 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1740 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1741 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001742 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1743 lType.getAsString(), rType.getAsString(),
1744 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001745 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001746 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001747 return Context.IntTy;
1748 }
Steve Naroff936c4362008-06-03 14:04:54 +00001749 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) {
1750 if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
1751 ImpCastExprToType(rex, lType);
1752 return Context.IntTy;
1753 }
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001754 }
Steve Naroff936c4362008-06-03 14:04:54 +00001755 if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) &&
1756 rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001757 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001758 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1759 lType.getAsString(), rType.getAsString(),
1760 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001761 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001762 return Context.IntTy;
1763 }
Steve Naroff936c4362008-06-03 14:04:54 +00001764 if (lType->isIntegerType() &&
1765 (rType->isPointerType() || rType->isObjCQualifiedIdType())) {
Chris Lattner22be8422007-08-26 01:10:14 +00001766 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001767 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1768 lType.getAsString(), rType.getAsString(),
1769 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001770 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001771 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001772 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00001773 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001774}
1775
Nate Begemanc5f0f652008-07-14 18:02:46 +00001776/// CheckVectorCompareOperands - vector comparisons are a clang extension that
1777/// operates on extended vector types. Instead of producing an IntTy result,
1778/// like a scalar comparison, a vector comparison produces a vector of integer
1779/// types.
1780QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex,
1781 SourceLocation loc,
1782 bool isRelational) {
1783 // Check to make sure we're operating on vectors of the same type and width,
1784 // Allowing one side to be a scalar of element type.
1785 QualType vType = CheckVectorOperands(loc, lex, rex);
1786 if (vType.isNull())
1787 return vType;
1788
1789 QualType lType = lex->getType();
1790 QualType rType = rex->getType();
1791
1792 // For non-floating point types, check for self-comparisons of the form
1793 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1794 // often indicate logic errors in the program.
1795 if (!lType->isFloatingType()) {
1796 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1797 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
1798 if (DRL->getDecl() == DRR->getDecl())
1799 Diag(loc, diag::warn_selfcomparison);
1800 }
1801
1802 // Check for comparisons of floating point operands using != and ==.
1803 if (!isRelational && lType->isFloatingType()) {
1804 assert (rType->isFloatingType());
1805 CheckFloatComparison(loc,lex,rex);
1806 }
1807
1808 // Return the type for the comparison, which is the same as vector type for
1809 // integer vectors, or an integer type of identical size and number of
1810 // elements for floating point vectors.
1811 if (lType->isIntegerType())
1812 return lType;
1813
1814 const VectorType *VTy = lType->getAsVectorType();
1815
1816 // FIXME: need to deal with non-32b int / non-64b long long
1817 unsigned TypeSize = Context.getTypeSize(VTy->getElementType());
1818 if (TypeSize == 32) {
1819 return Context.getExtVectorType(Context.IntTy, VTy->getNumElements());
1820 }
1821 assert(TypeSize == 64 && "Unhandled vector element size in vector compare");
1822 return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements());
1823}
1824
Chris Lattner4b009652007-07-25 00:24:17 +00001825inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001826 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001827{
1828 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1829 return CheckVectorOperands(loc, lex, rex);
1830
Steve Naroff8f708362007-08-24 19:07:16 +00001831 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001832
1833 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001834 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001835 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001836}
1837
1838inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1839 Expr *&lex, Expr *&rex, SourceLocation loc)
1840{
1841 UsualUnaryConversions(lex);
1842 UsualUnaryConversions(rex);
1843
Eli Friedmanbea3f842008-05-13 20:16:47 +00001844 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Chris Lattner4b009652007-07-25 00:24:17 +00001845 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001846 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001847}
1848
1849inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001850 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001851{
1852 QualType lhsType = lex->getType();
1853 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner4b009652007-07-25 00:24:17 +00001854 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1855
1856 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00001857 case Expr::MLV_Valid:
1858 break;
1859 case Expr::MLV_ConstQualified:
1860 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1861 return QualType();
1862 case Expr::MLV_ArrayType:
1863 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1864 lhsType.getAsString(), lex->getSourceRange());
1865 return QualType();
1866 case Expr::MLV_NotObjectType:
1867 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1868 lhsType.getAsString(), lex->getSourceRange());
1869 return QualType();
1870 case Expr::MLV_InvalidExpression:
1871 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1872 lex->getSourceRange());
1873 return QualType();
1874 case Expr::MLV_IncompleteType:
1875 case Expr::MLV_IncompleteVoidType:
1876 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1877 lhsType.getAsString(), lex->getSourceRange());
1878 return QualType();
1879 case Expr::MLV_DuplicateVectorComponents:
1880 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1881 lex->getSourceRange());
1882 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001883 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001884
Chris Lattner005ed752008-01-04 18:04:52 +00001885 AssignConvertType ConvTy;
1886 if (compoundType.isNull())
1887 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1888 else
1889 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1890
1891 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1892 rex, "assigning"))
1893 return QualType();
1894
Chris Lattner4b009652007-07-25 00:24:17 +00001895 // C99 6.5.16p3: The type of an assignment expression is the type of the
1896 // left operand unless the left operand has qualified type, in which case
1897 // it is the unqualified version of the type of the left operand.
1898 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1899 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001900 // C++ 5.17p1: the type of the assignment expression is that of its left
1901 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00001902 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001903}
1904
1905inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1906 Expr *&lex, Expr *&rex, SourceLocation loc) {
Chris Lattner03c430f2008-07-25 20:54:07 +00001907
1908 // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions.
1909 DefaultFunctionArrayConversion(rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001910 return rex->getType();
1911}
1912
1913/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1914/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1915QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1916 QualType resType = op->getType();
1917 assert(!resType.isNull() && "no type for increment/decrement expression");
1918
Steve Naroffd30e1932007-08-24 17:20:07 +00001919 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00001920 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001921 if (pt->getPointeeType()->isVoidType()) {
1922 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
1923 } else if (!pt->getPointeeType()->isObjectType()) {
1924 // C99 6.5.2.4p2, 6.5.6p2
Chris Lattner4b009652007-07-25 00:24:17 +00001925 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1926 resType.getAsString(), op->getSourceRange());
1927 return QualType();
1928 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001929 } else if (!resType->isRealType()) {
1930 if (resType->isComplexType())
1931 // C99 does not support ++/-- on complex types.
1932 Diag(OpLoc, diag::ext_integer_increment_complex,
1933 resType.getAsString(), op->getSourceRange());
1934 else {
1935 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1936 resType.getAsString(), op->getSourceRange());
1937 return QualType();
1938 }
Chris Lattner4b009652007-07-25 00:24:17 +00001939 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001940 // At this point, we know we have a real, complex or pointer type.
1941 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001942 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1943 if (mlval != Expr::MLV_Valid) {
1944 // FIXME: emit a more precise diagnostic...
1945 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1946 op->getSourceRange());
1947 return QualType();
1948 }
1949 return resType;
1950}
1951
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001952/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00001953/// This routine allows us to typecheck complex/recursive expressions
1954/// where the declaration is needed for type checking. Here are some
1955/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattner48d7f382008-04-02 04:24:33 +00001956static ValueDecl *getPrimaryDecl(Expr *E) {
1957 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001958 case Stmt::DeclRefExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001959 return cast<DeclRefExpr>(E)->getDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001960 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00001961 // Fields cannot be declared with a 'register' storage class.
1962 // &X->f is always ok, even if X is declared register.
Chris Lattner48d7f382008-04-02 04:24:33 +00001963 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnera3249072007-11-16 17:46:48 +00001964 return 0;
Chris Lattner48d7f382008-04-02 04:24:33 +00001965 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001966 case Stmt::ArraySubscriptExprClass: {
1967 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1968
Chris Lattner48d7f382008-04-02 04:24:33 +00001969 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00001970 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001971 return 0;
1972 else
1973 return VD;
1974 }
Chris Lattner4b009652007-07-25 00:24:17 +00001975 case Stmt::UnaryOperatorClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001976 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001977 case Stmt::ParenExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001978 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00001979 case Stmt::ImplicitCastExprClass:
1980 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner48d7f382008-04-02 04:24:33 +00001981 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001982 default:
1983 return 0;
1984 }
1985}
1986
1987/// CheckAddressOfOperand - The operand of & must be either a function
1988/// designator or an lvalue designating an object. If it is an lvalue, the
1989/// object cannot be declared with storage class register or be a bit field.
1990/// Note: The usual conversions are *not* applied to the operand of the &
1991/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1992QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001993 if (getLangOptions().C99) {
1994 // Implement C99-only parts of addressof rules.
1995 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1996 if (uOp->getOpcode() == UnaryOperator::Deref)
1997 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1998 // (assuming the deref expression is valid).
1999 return uOp->getSubExpr()->getType();
2000 }
2001 // Technically, there should be a check for array subscript
2002 // expressions here, but the result of one is always an lvalue anyway.
2003 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00002004 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner4b009652007-07-25 00:24:17 +00002005 Expr::isLvalueResult lval = op->isLvalue();
2006
2007 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00002008 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
2009 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00002010 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
2011 op->getSourceRange());
2012 return QualType();
2013 }
Steve Naroff73cf87e2008-02-29 23:30:25 +00002014 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
2015 if (MemExpr->getMemberDecl()->isBitField()) {
2016 Diag(OpLoc, diag::err_typecheck_address_of,
2017 std::string("bit-field"), op->getSourceRange());
2018 return QualType();
2019 }
2020 // Check for Apple extension for accessing vector components.
2021 } else if (isa<ArraySubscriptExpr>(op) &&
2022 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
2023 Diag(OpLoc, diag::err_typecheck_address_of,
2024 std::string("vector"), op->getSourceRange());
2025 return QualType();
2026 } else if (dcl) { // C99 6.5.3.2p1
Chris Lattner4b009652007-07-25 00:24:17 +00002027 // We have an lvalue with a decl. Make sure the decl is not declared
2028 // with the register storage-class specifier.
2029 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
2030 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff73cf87e2008-02-29 23:30:25 +00002031 Diag(OpLoc, diag::err_typecheck_address_of,
2032 std::string("register variable"), op->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00002033 return QualType();
2034 }
2035 } else
2036 assert(0 && "Unknown/unexpected decl type");
Chris Lattner4b009652007-07-25 00:24:17 +00002037 }
2038 // If the operand has type "type", the result has type "pointer to type".
2039 return Context.getPointerType(op->getType());
2040}
2041
2042QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
2043 UsualUnaryConversions(op);
2044 QualType qType = op->getType();
2045
Chris Lattner7931f4a2007-07-31 16:53:04 +00002046 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00002047 // Note that per both C89 and C99, this is always legal, even
2048 // if ptype is an incomplete type or void.
2049 // It would be possible to warn about dereferencing a
2050 // void pointer, but it's completely well-defined,
2051 // and such a warning is unlikely to catch any mistakes.
2052 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00002053 }
2054 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
2055 qType.getAsString(), op->getSourceRange());
2056 return QualType();
2057}
2058
2059static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
2060 tok::TokenKind Kind) {
2061 BinaryOperator::Opcode Opc;
2062 switch (Kind) {
2063 default: assert(0 && "Unknown binop!");
2064 case tok::star: Opc = BinaryOperator::Mul; break;
2065 case tok::slash: Opc = BinaryOperator::Div; break;
2066 case tok::percent: Opc = BinaryOperator::Rem; break;
2067 case tok::plus: Opc = BinaryOperator::Add; break;
2068 case tok::minus: Opc = BinaryOperator::Sub; break;
2069 case tok::lessless: Opc = BinaryOperator::Shl; break;
2070 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
2071 case tok::lessequal: Opc = BinaryOperator::LE; break;
2072 case tok::less: Opc = BinaryOperator::LT; break;
2073 case tok::greaterequal: Opc = BinaryOperator::GE; break;
2074 case tok::greater: Opc = BinaryOperator::GT; break;
2075 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
2076 case tok::equalequal: Opc = BinaryOperator::EQ; break;
2077 case tok::amp: Opc = BinaryOperator::And; break;
2078 case tok::caret: Opc = BinaryOperator::Xor; break;
2079 case tok::pipe: Opc = BinaryOperator::Or; break;
2080 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
2081 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
2082 case tok::equal: Opc = BinaryOperator::Assign; break;
2083 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
2084 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
2085 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
2086 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
2087 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
2088 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
2089 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
2090 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
2091 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
2092 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
2093 case tok::comma: Opc = BinaryOperator::Comma; break;
2094 }
2095 return Opc;
2096}
2097
2098static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
2099 tok::TokenKind Kind) {
2100 UnaryOperator::Opcode Opc;
2101 switch (Kind) {
2102 default: assert(0 && "Unknown unary op!");
2103 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
2104 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
2105 case tok::amp: Opc = UnaryOperator::AddrOf; break;
2106 case tok::star: Opc = UnaryOperator::Deref; break;
2107 case tok::plus: Opc = UnaryOperator::Plus; break;
2108 case tok::minus: Opc = UnaryOperator::Minus; break;
2109 case tok::tilde: Opc = UnaryOperator::Not; break;
2110 case tok::exclaim: Opc = UnaryOperator::LNot; break;
2111 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
2112 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
2113 case tok::kw___real: Opc = UnaryOperator::Real; break;
2114 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
2115 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
2116 }
2117 return Opc;
2118}
2119
2120// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002121Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00002122 ExprTy *LHS, ExprTy *RHS) {
2123 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
2124 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
2125
Steve Naroff87d58b42007-09-16 03:34:24 +00002126 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
2127 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00002128
2129 QualType ResultTy; // Result type of the binary operator.
2130 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
2131
2132 switch (Opc) {
2133 default:
2134 assert(0 && "Unknown binary expr!");
2135 case BinaryOperator::Assign:
2136 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
2137 break;
2138 case BinaryOperator::Mul:
2139 case BinaryOperator::Div:
2140 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
2141 break;
2142 case BinaryOperator::Rem:
2143 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
2144 break;
2145 case BinaryOperator::Add:
2146 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
2147 break;
2148 case BinaryOperator::Sub:
2149 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
2150 break;
2151 case BinaryOperator::Shl:
2152 case BinaryOperator::Shr:
2153 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
2154 break;
2155 case BinaryOperator::LE:
2156 case BinaryOperator::LT:
2157 case BinaryOperator::GE:
2158 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002159 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002160 break;
2161 case BinaryOperator::EQ:
2162 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002163 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00002164 break;
2165 case BinaryOperator::And:
2166 case BinaryOperator::Xor:
2167 case BinaryOperator::Or:
2168 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
2169 break;
2170 case BinaryOperator::LAnd:
2171 case BinaryOperator::LOr:
2172 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
2173 break;
2174 case BinaryOperator::MulAssign:
2175 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002176 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002177 if (!CompTy.isNull())
2178 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2179 break;
2180 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002181 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002182 if (!CompTy.isNull())
2183 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2184 break;
2185 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002186 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002187 if (!CompTy.isNull())
2188 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2189 break;
2190 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002191 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002192 if (!CompTy.isNull())
2193 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2194 break;
2195 case BinaryOperator::ShlAssign:
2196 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002197 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002198 if (!CompTy.isNull())
2199 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2200 break;
2201 case BinaryOperator::AndAssign:
2202 case BinaryOperator::XorAssign:
2203 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002204 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002205 if (!CompTy.isNull())
2206 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2207 break;
2208 case BinaryOperator::Comma:
2209 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
2210 break;
2211 }
2212 if (ResultTy.isNull())
2213 return true;
2214 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00002215 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002216 else
Chris Lattnerf420df12007-08-28 18:36:55 +00002217 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002218}
2219
2220// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002221Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00002222 ExprTy *input) {
2223 Expr *Input = (Expr*)input;
2224 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2225 QualType resultType;
2226 switch (Opc) {
2227 default:
2228 assert(0 && "Unimplemented unary expr!");
2229 case UnaryOperator::PreInc:
2230 case UnaryOperator::PreDec:
2231 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
2232 break;
2233 case UnaryOperator::AddrOf:
2234 resultType = CheckAddressOfOperand(Input, OpLoc);
2235 break;
2236 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00002237 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00002238 resultType = CheckIndirectionOperand(Input, OpLoc);
2239 break;
2240 case UnaryOperator::Plus:
2241 case UnaryOperator::Minus:
2242 UsualUnaryConversions(Input);
2243 resultType = Input->getType();
2244 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
2245 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2246 resultType.getAsString());
2247 break;
2248 case UnaryOperator::Not: // bitwise complement
2249 UsualUnaryConversions(Input);
2250 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00002251 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2252 if (!resultType->isIntegerType()) {
2253 if (resultType->isComplexType())
2254 // C99 does not support '~' for complex conjugation.
2255 Diag(OpLoc, diag::ext_integer_complement_complex,
2256 resultType.getAsString());
2257 else
2258 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2259 resultType.getAsString());
2260 }
Chris Lattner4b009652007-07-25 00:24:17 +00002261 break;
2262 case UnaryOperator::LNot: // logical negation
2263 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
2264 DefaultFunctionArrayConversion(Input);
2265 resultType = Input->getType();
2266 if (!resultType->isScalarType()) // C99 6.5.3.3p1
2267 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2268 resultType.getAsString());
2269 // LNot always has type int. C99 6.5.3.3p5.
2270 resultType = Context.IntTy;
2271 break;
2272 case UnaryOperator::SizeOf:
2273 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
2274 break;
2275 case UnaryOperator::AlignOf:
2276 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2277 break;
Chris Lattner03931a72007-08-24 21:16:53 +00002278 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00002279 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00002280 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00002281 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002282 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00002283 resultType = Input->getType();
2284 break;
2285 }
2286 if (resultType.isNull())
2287 return true;
2288 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2289}
2290
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002291/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2292Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00002293 SourceLocation LabLoc,
2294 IdentifierInfo *LabelII) {
2295 // Look up the record for this label identifier.
2296 LabelStmt *&LabelDecl = LabelMap[LabelII];
2297
2298 // If we haven't seen this label yet, create a forward reference.
2299 if (LabelDecl == 0)
2300 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2301
2302 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00002303 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2304 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00002305}
2306
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002307Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00002308 SourceLocation RPLoc) { // "({..})"
2309 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2310 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2311 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2312
2313 // FIXME: there are a variety of strange constraints to enforce here, for
2314 // example, it is not possible to goto into a stmt expression apparently.
2315 // More semantic analysis is needed.
2316
2317 // FIXME: the last statement in the compount stmt has its value used. We
2318 // should not warn about it being unused.
2319
2320 // If there are sub stmts in the compound stmt, take the type of the last one
2321 // as the type of the stmtexpr.
2322 QualType Ty = Context.VoidTy;
2323
2324 if (!Compound->body_empty())
2325 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2326 Ty = LastExpr->getType();
2327
2328 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2329}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002330
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002331Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002332 SourceLocation TypeLoc,
2333 TypeTy *argty,
2334 OffsetOfComponent *CompPtr,
2335 unsigned NumComponents,
2336 SourceLocation RPLoc) {
2337 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2338 assert(!ArgTy.isNull() && "Missing type argument!");
2339
2340 // We must have at least one component that refers to the type, and the first
2341 // one is known to be a field designator. Verify that the ArgTy represents
2342 // a struct/union/class.
2343 if (!ArgTy->isRecordType())
2344 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2345
2346 // Otherwise, create a compound literal expression as the base, and
2347 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002348 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002349
Chris Lattnerb37522e2007-08-31 21:49:13 +00002350 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2351 // GCC extension, diagnose them.
2352 if (NumComponents != 1)
2353 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2354 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2355
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002356 for (unsigned i = 0; i != NumComponents; ++i) {
2357 const OffsetOfComponent &OC = CompPtr[i];
2358 if (OC.isBrackets) {
2359 // Offset of an array sub-field. TODO: Should we allow vector elements?
2360 const ArrayType *AT = Res->getType()->getAsArrayType();
2361 if (!AT) {
2362 delete Res;
2363 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2364 Res->getType().getAsString());
2365 }
2366
Chris Lattner2af6a802007-08-30 17:59:59 +00002367 // FIXME: C++: Verify that operator[] isn't overloaded.
2368
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002369 // C99 6.5.2.1p1
2370 Expr *Idx = static_cast<Expr*>(OC.U.E);
2371 if (!Idx->getType()->isIntegerType())
2372 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2373 Idx->getSourceRange());
2374
2375 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2376 continue;
2377 }
2378
2379 const RecordType *RC = Res->getType()->getAsRecordType();
2380 if (!RC) {
2381 delete Res;
2382 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2383 Res->getType().getAsString());
2384 }
2385
2386 // Get the decl corresponding to this.
2387 RecordDecl *RD = RC->getDecl();
2388 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2389 if (!MemberDecl)
2390 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2391 OC.U.IdentInfo->getName(),
2392 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002393
2394 // FIXME: C++: Verify that MemberDecl isn't a static field.
2395 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002396 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2397 // matter here.
2398 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002399 }
2400
2401 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2402 BuiltinLoc);
2403}
2404
2405
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002406Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002407 TypeTy *arg1, TypeTy *arg2,
2408 SourceLocation RPLoc) {
2409 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2410 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2411
2412 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2413
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002414 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002415}
2416
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002417Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002418 ExprTy *expr1, ExprTy *expr2,
2419 SourceLocation RPLoc) {
2420 Expr *CondExpr = static_cast<Expr*>(cond);
2421 Expr *LHSExpr = static_cast<Expr*>(expr1);
2422 Expr *RHSExpr = static_cast<Expr*>(expr2);
2423
2424 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2425
2426 // The conditional expression is required to be a constant expression.
2427 llvm::APSInt condEval(32);
2428 SourceLocation ExpLoc;
2429 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2430 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2431 CondExpr->getSourceRange());
2432
2433 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2434 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2435 RHSExpr->getType();
2436 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2437}
2438
Nate Begemanbd881ef2008-01-30 20:50:20 +00002439/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002440/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002441/// The number of arguments has already been validated to match the number of
2442/// arguments in FnType.
2443static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002444 unsigned NumParams = FnType->getNumArgs();
Nate Begeman778fd3b2008-04-18 23:35:14 +00002445 for (unsigned i = 0; i != NumParams; ++i) {
2446 QualType ExprTy = Args[i]->getType().getCanonicalType();
2447 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2448
2449 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002450 return false;
Nate Begeman778fd3b2008-04-18 23:35:14 +00002451 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002452 return true;
2453}
2454
2455Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2456 SourceLocation *CommaLocs,
2457 SourceLocation BuiltinLoc,
2458 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002459 // __builtin_overload requires at least 2 arguments
2460 if (NumArgs < 2)
2461 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2462 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002463
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002464 // The first argument is required to be a constant expression. It tells us
2465 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002466 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002467 Expr *NParamsExpr = Args[0];
2468 llvm::APSInt constEval(32);
2469 SourceLocation ExpLoc;
2470 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2471 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2472 NParamsExpr->getSourceRange());
2473
2474 // Verify that the number of parameters is > 0
2475 unsigned NumParams = constEval.getZExtValue();
2476 if (NumParams == 0)
2477 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2478 NParamsExpr->getSourceRange());
2479 // Verify that we have at least 1 + NumParams arguments to the builtin.
2480 if ((NumParams + 1) > NumArgs)
2481 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2482 SourceRange(BuiltinLoc, RParenLoc));
2483
2484 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002485 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002486 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002487 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2488 // UsualUnaryConversions will convert the function DeclRefExpr into a
2489 // pointer to function.
2490 Expr *Fn = UsualUnaryConversions(Args[i]);
2491 FunctionTypeProto *FnType = 0;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002492 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2493 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2494 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2495 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002496
2497 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2498 // parameters, and the number of parameters must match the value passed to
2499 // the builtin.
2500 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002501 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2502 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002503
2504 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002505 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002506 // If they match, return a new OverloadExpr.
Nate Begemanc6078c92008-01-31 05:38:29 +00002507 if (ExprsMatchFnType(Args+1, FnType)) {
2508 if (OE)
2509 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2510 OE->getFn()->getSourceRange());
2511 // Remember our match, and continue processing the remaining arguments
2512 // to catch any errors.
2513 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2514 BuiltinLoc, RParenLoc);
2515 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002516 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002517 // Return the newly created OverloadExpr node, if we succeded in matching
2518 // exactly one of the candidate functions.
2519 if (OE)
2520 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002521
2522 // If we didn't find a matching function Expr in the __builtin_overload list
2523 // the return an error.
2524 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002525 for (unsigned i = 0; i != NumParams; ++i) {
2526 if (i != 0) typeNames += ", ";
2527 typeNames += Args[i+1]->getType().getAsString();
2528 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002529
2530 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2531 SourceRange(BuiltinLoc, RParenLoc));
2532}
2533
Anders Carlsson36760332007-10-15 20:28:48 +00002534Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2535 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002536 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002537 Expr *E = static_cast<Expr*>(expr);
2538 QualType T = QualType::getFromOpaquePtr(type);
2539
2540 InitBuiltinVaListType();
2541
Chris Lattner005ed752008-01-04 18:04:52 +00002542 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2543 != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002544 return Diag(E->getLocStart(),
2545 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2546 E->getType().getAsString(),
2547 E->getSourceRange());
2548
2549 // FIXME: Warn if a non-POD type is passed in.
2550
2551 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2552}
2553
Chris Lattner005ed752008-01-04 18:04:52 +00002554bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2555 SourceLocation Loc,
2556 QualType DstType, QualType SrcType,
2557 Expr *SrcExpr, const char *Flavor) {
2558 // Decode the result (notice that AST's are still created for extensions).
2559 bool isInvalid = false;
2560 unsigned DiagKind;
2561 switch (ConvTy) {
2562 default: assert(0 && "Unknown conversion type");
2563 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002564 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00002565 DiagKind = diag::ext_typecheck_convert_pointer_int;
2566 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002567 case IntToPointer:
2568 DiagKind = diag::ext_typecheck_convert_int_pointer;
2569 break;
Chris Lattner005ed752008-01-04 18:04:52 +00002570 case IncompatiblePointer:
2571 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2572 break;
2573 case FunctionVoidPointer:
2574 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2575 break;
2576 case CompatiblePointerDiscardsQualifiers:
2577 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2578 break;
2579 case Incompatible:
2580 DiagKind = diag::err_typecheck_convert_incompatible;
2581 isInvalid = true;
2582 break;
2583 }
2584
2585 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2586 SrcExpr->getSourceRange());
2587 return isInvalid;
2588}