blob: 9ffca0870c82cdd8a92b85d08e5164f48eab13df [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));
Chris Lattner2aa68822008-07-25 21:33:13 +000061 else if (Ty->isArrayType()) {
62 // In C90 mode, arrays only promote to pointers if the array expression is
63 // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has
64 // type 'array of type' is converted to an expression that has type 'pointer
65 // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression
66 // that has type 'array of type' ...". The relevant change is "an lvalue"
67 // (C90) to "an expression" (C99).
68 if (getLangOptions().C99 || E->isLvalue() == Expr::LV_Valid)
69 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
70 }
Chris Lattner299b8842008-07-25 21:10:04 +000071}
72
73/// UsualUnaryConversions - Performs various conversions that are common to most
74/// operators (C99 6.3). The conversions of array and function types are
75/// sometimes surpressed. For example, the array->pointer conversion doesn't
76/// apply if the array is an argument to the sizeof or address (&) operators.
77/// In these instances, this routine should *not* be called.
78Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
79 QualType Ty = Expr->getType();
80 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
81
82 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
83 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
84 Ty = Expr->getType();
85 }
86 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
87 ImpCastExprToType(Expr, Context.IntTy);
88 else
89 DefaultFunctionArrayConversion(Expr);
90
91 return Expr;
92}
93
94/// UsualArithmeticConversions - Performs various conversions that are common to
95/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
96/// routine returns the first non-arithmetic type found. The client is
97/// responsible for emitting appropriate error diagnostics.
98/// FIXME: verify the conversion rules for "complex int" are consistent with
99/// GCC.
100QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
101 bool isCompAssign) {
102 if (!isCompAssign) {
103 UsualUnaryConversions(lhsExpr);
104 UsualUnaryConversions(rhsExpr);
105 }
106 // For conversion purposes, we ignore any qualifiers.
107 // For example, "const float" and "float" are equivalent.
108 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
109 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
110
111 // If both types are identical, no conversion is needed.
112 if (lhs == rhs)
113 return lhs;
114
115 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
116 // The caller can deal with this (e.g. pointer + int).
117 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
118 return lhs;
119
120 // At this point, we have two different arithmetic types.
121
122 // Handle complex types first (C99 6.3.1.8p1).
123 if (lhs->isComplexType() || rhs->isComplexType()) {
124 // if we have an integer operand, the result is the complex type.
125 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
126 // convert the rhs to the lhs complex type.
127 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
128 return lhs;
129 }
130 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
131 // convert the lhs to the rhs complex type.
132 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
133 return rhs;
134 }
135 // This handles complex/complex, complex/float, or float/complex.
136 // When both operands are complex, the shorter operand is converted to the
137 // type of the longer, and that is the type of the result. This corresponds
138 // to what is done when combining two real floating-point operands.
139 // The fun begins when size promotion occur across type domains.
140 // From H&S 6.3.4: When one operand is complex and the other is a real
141 // floating-point type, the less precise type is converted, within it's
142 // real or complex domain, to the precision of the other type. For example,
143 // when combining a "long double" with a "double _Complex", the
144 // "double _Complex" is promoted to "long double _Complex".
145 int result = Context.getFloatingTypeOrder(lhs, rhs);
146
147 if (result > 0) { // The left side is bigger, convert rhs.
148 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
149 if (!isCompAssign)
150 ImpCastExprToType(rhsExpr, rhs);
151 } else if (result < 0) { // The right side is bigger, convert lhs.
152 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
153 if (!isCompAssign)
154 ImpCastExprToType(lhsExpr, lhs);
155 }
156 // At this point, lhs and rhs have the same rank/size. Now, make sure the
157 // domains match. This is a requirement for our implementation, C99
158 // does not require this promotion.
159 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
160 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
161 if (!isCompAssign)
162 ImpCastExprToType(lhsExpr, rhs);
163 return rhs;
164 } else { // handle "_Complex double, double".
165 if (!isCompAssign)
166 ImpCastExprToType(rhsExpr, lhs);
167 return lhs;
168 }
169 }
170 return lhs; // The domain/size match exactly.
171 }
172 // Now handle "real" floating types (i.e. float, double, long double).
173 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
174 // if we have an integer operand, the result is the real floating type.
175 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
176 // convert rhs to the lhs floating point type.
177 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
178 return lhs;
179 }
180 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
181 // convert lhs to the rhs floating point type.
182 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
183 return rhs;
184 }
185 // We have two real floating types, float/complex combos were handled above.
186 // Convert the smaller operand to the bigger result.
187 int result = Context.getFloatingTypeOrder(lhs, rhs);
188
189 if (result > 0) { // convert the rhs
190 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
191 return lhs;
192 }
193 if (result < 0) { // convert the lhs
194 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
195 return rhs;
196 }
197 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
198 }
199 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
200 // Handle GCC complex int extension.
201 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
202 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
203
204 if (lhsComplexInt && rhsComplexInt) {
205 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
206 rhsComplexInt->getElementType()) >= 0) {
207 // convert the rhs
208 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
209 return lhs;
210 }
211 if (!isCompAssign)
212 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
213 return rhs;
214 } else if (lhsComplexInt && rhs->isIntegerType()) {
215 // convert the rhs to the lhs complex type.
216 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
217 return lhs;
218 } else if (rhsComplexInt && lhs->isIntegerType()) {
219 // convert the lhs to the rhs complex type.
220 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
221 return rhs;
222 }
223 }
224 // Finally, we have two differing integer types.
225 // The rules for this case are in C99 6.3.1.8
226 int compare = Context.getIntegerTypeOrder(lhs, rhs);
227 bool lhsSigned = lhs->isSignedIntegerType(),
228 rhsSigned = rhs->isSignedIntegerType();
229 QualType destType;
230 if (lhsSigned == rhsSigned) {
231 // Same signedness; use the higher-ranked type
232 destType = compare >= 0 ? lhs : rhs;
233 } else if (compare != (lhsSigned ? 1 : -1)) {
234 // The unsigned type has greater than or equal rank to the
235 // signed type, so use the unsigned type
236 destType = lhsSigned ? rhs : lhs;
237 } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) {
238 // The two types are different widths; if we are here, that
239 // means the signed type is larger than the unsigned type, so
240 // use the signed type.
241 destType = lhsSigned ? lhs : rhs;
242 } else {
243 // The signed type is higher-ranked than the unsigned type,
244 // but isn't actually any bigger (like unsigned int and long
245 // on most 32-bit systems). Use the unsigned type corresponding
246 // to the signed type.
247 destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
248 }
249 if (!isCompAssign) {
250 ImpCastExprToType(lhsExpr, destType);
251 ImpCastExprToType(rhsExpr, destType);
252 }
253 return destType;
254}
255
256//===----------------------------------------------------------------------===//
257// Semantic Analysis for various Expression Types
258//===----------------------------------------------------------------------===//
259
260
Steve Naroff87d58b42007-09-16 03:34:24 +0000261/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +0000262/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
263/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
264/// multiple tokens. However, the common case is that StringToks points to one
265/// string.
266///
267Action::ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +0000268Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner4b009652007-07-25 00:24:17 +0000269 assert(NumStringToks && "Must have at least one string!");
270
271 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
272 if (Literal.hadError)
273 return ExprResult(true);
274
275 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
276 for (unsigned i = 0; i != NumStringToks; ++i)
277 StringTokLocs.push_back(StringToks[i].getLocation());
Chris Lattnera6dcce32008-02-11 00:02:17 +0000278
279 // Verify that pascal strings aren't too large.
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000280 if (Literal.Pascal && Literal.GetStringLength() > 256)
281 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
282 SourceRange(StringToks[0].getLocation(),
283 StringToks[NumStringToks-1].getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000284
Chris Lattnera6dcce32008-02-11 00:02:17 +0000285 QualType StrTy = Context.CharTy;
Eli Friedman256b7d72008-05-27 07:57:14 +0000286 if (Literal.AnyWide) StrTy = Context.getWcharType();
Chris Lattnera6dcce32008-02-11 00:02:17 +0000287 if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
288
289 // Get an array type for the string, according to C99 6.4.5. This includes
290 // the nul terminator character as well as the string length for pascal
291 // strings.
292 StrTy = Context.getConstantArrayType(StrTy,
293 llvm::APInt(32, Literal.GetStringLength()+1),
294 ArrayType::Normal, 0);
295
Chris Lattner4b009652007-07-25 00:24:17 +0000296 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
297 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera6dcce32008-02-11 00:02:17 +0000298 Literal.AnyWide, StrTy,
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000299 StringToks[0].getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000300 StringToks[NumStringToks-1].getLocation());
301}
302
303
Steve Naroff0acc9c92007-09-15 18:49:24 +0000304/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattner4b009652007-07-25 00:24:17 +0000305/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroffe50e14c2008-03-19 23:46:26 +0000306/// identifier is used in a function call context.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000307Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000308 IdentifierInfo &II,
309 bool HasTrailingLParen) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000310 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroff6384a012008-04-02 14:35:35 +0000311 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000312
313 // If this reference is in an Objective-C method, then ivar lookup happens as
314 // well.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000315 if (getCurMethodDecl()) {
Steve Naroffe57c21a2008-04-01 23:04:06 +0000316 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000317 // There are two cases to handle here. 1) scoped lookup could have failed,
318 // in which case we should look for an ivar. 2) scoped lookup could have
319 // found a decl, but that decl is outside the current method (i.e. a global
320 // variable). In these two cases, we do a lookup for an ivar with this
321 // name, if the lookup suceeds, we replace it our current decl.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000322 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000323 ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface();
Chris Lattnered94f762008-07-21 04:44:44 +0000324 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II)) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000325 // FIXME: This should use a new expr for a direct reference, don't turn
326 // this into Self->ivar, just return a BareIVarExpr or something.
327 IdentifierInfo &II = Context.Idents.get("self");
328 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
329 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
330 static_cast<Expr*>(SelfExpr.Val), true, true);
331 }
332 }
Steve Naroffe90d4cc2008-06-05 18:14:25 +0000333 if (SD == 0 && !strcmp(II.getName(), "super")) {
Steve Naroff6f786252008-06-02 23:03:37 +0000334 QualType T = Context.getPointerType(Context.getObjCInterfaceType(
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000335 getCurMethodDecl()->getClassInterface()));
Steve Naroff6f786252008-06-02 23:03:37 +0000336 return new ObjCSuperRefExpr(T, Loc);
337 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000338 }
339
Chris Lattner4b009652007-07-25 00:24:17 +0000340 if (D == 0) {
341 // Otherwise, this could be an implicitly declared function reference (legal
342 // in C90, extension in C99).
343 if (HasTrailingLParen &&
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000344 !getLangOptions().CPlusPlus) // Not in C++.
Chris Lattner4b009652007-07-25 00:24:17 +0000345 D = ImplicitlyDefineFunction(Loc, II, S);
346 else {
347 // If this name wasn't predeclared and if this is not a function call,
348 // diagnose the problem.
349 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
350 }
351 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000352
Steve Naroff91b03f72007-08-28 03:03:08 +0000353 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneree4c3bf2008-02-29 16:48:43 +0000354 // check if referencing an identifier with __attribute__((deprecated)).
355 if (VD->getAttr<DeprecatedAttr>())
356 Diag(Loc, diag::warn_deprecated, VD->getName());
357
Steve Naroffcae537d2007-08-28 18:45:29 +0000358 // Only create DeclRefExpr's for valid Decl's.
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000359 if (VD->isInvalidDecl())
Steve Naroff91b03f72007-08-28 03:03:08 +0000360 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000361 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff91b03f72007-08-28 03:03:08 +0000362 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000363
364 if (CXXFieldDecl *FD = dyn_cast<CXXFieldDecl>(D)) {
365 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) {
366 if (MD->isStatic())
367 // "invalid use of member 'x' in static member function"
368 return Diag(Loc, diag::err_invalid_member_use_in_static_method,
369 FD->getName());
370 if (cast<CXXRecordDecl>(MD->getParent()) != FD->getParent())
371 // "invalid use of nonstatic data member 'x'"
372 return Diag(Loc, diag::err_invalid_non_static_member_use,
373 FD->getName());
374
375 if (FD->isInvalidDecl())
376 return true;
377
378 // FIXME: Use DeclRefExpr or a new Expr for a direct CXXField reference.
379 ExprResult ThisExpr = ActOnCXXThis(SourceLocation());
380 return new MemberExpr(static_cast<Expr*>(ThisExpr.Val),
381 true, FD, Loc, FD->getType());
382 }
383
384 return Diag(Loc, diag::err_invalid_non_static_member_use, FD->getName());
385 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000386
Chris Lattner4b009652007-07-25 00:24:17 +0000387 if (isa<TypedefDecl>(D))
388 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek42730c52008-01-07 19:49:32 +0000389 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian3102df92007-12-05 18:16:33 +0000390 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000391 if (isa<NamespaceDecl>(D))
392 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000393
394 assert(0 && "Invalid decl");
395 abort();
396}
397
Steve Naroff87d58b42007-09-16 03:34:24 +0000398Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000399 tok::TokenKind Kind) {
400 PreDefinedExpr::IdentType IT;
401
402 switch (Kind) {
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000403 default: assert(0 && "Unknown simple primary expr!");
404 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
405 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
406 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000407 }
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000408
409 // Verify that this is in a function context.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000410 if (getCurFunctionDecl() == 0 && getCurMethodDecl() == 0)
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000411 return Diag(Loc, diag::err_predef_outside_function);
Chris Lattner4b009652007-07-25 00:24:17 +0000412
Chris Lattner7e637512008-01-12 08:14:25 +0000413 // Pre-defined identifiers are of type char[x], where x is the length of the
414 // string.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000415 unsigned Length;
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000416 if (getCurFunctionDecl())
417 Length = getCurFunctionDecl()->getIdentifier()->getLength();
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000418 else
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000419 Length = getCurMethodDecl()->getSynthesizedMethodSize();
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000420
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000421 llvm::APInt LengthI(32, Length + 1);
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000422 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000423 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattner7e637512008-01-12 08:14:25 +0000424 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattner4b009652007-07-25 00:24:17 +0000425}
426
Steve Naroff87d58b42007-09-16 03:34:24 +0000427Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000428 llvm::SmallString<16> CharBuffer;
429 CharBuffer.resize(Tok.getLength());
430 const char *ThisTokBegin = &CharBuffer[0];
431 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
432
433 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
434 Tok.getLocation(), PP);
435 if (Literal.hadError())
436 return ExprResult(true);
Chris Lattner6b22fb72008-03-01 08:32:21 +0000437
438 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
439
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000440 return new CharacterLiteral(Literal.getValue(), Literal.isWide(), type,
441 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000442}
443
Steve Naroff87d58b42007-09-16 03:34:24 +0000444Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000445 // fast path for a single digit (which is quite common). A single digit
446 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
447 if (Tok.getLength() == 1) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000448 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000449
Chris Lattner8cd0e932008-03-05 18:54:05 +0000450 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner48d7f382008-04-02 04:24:33 +0000451 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Chris Lattner4b009652007-07-25 00:24:17 +0000452 Context.IntTy,
453 Tok.getLocation()));
454 }
455 llvm::SmallString<512> IntegerBuffer;
456 IntegerBuffer.resize(Tok.getLength());
457 const char *ThisTokBegin = &IntegerBuffer[0];
458
459 // Get the spelling of the token, which eliminates trigraphs, etc.
460 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
461 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
462 Tok.getLocation(), PP);
463 if (Literal.hadError)
464 return ExprResult(true);
465
Chris Lattner1de66eb2007-08-26 03:42:43 +0000466 Expr *Res;
467
468 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000469 QualType Ty;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000470 if (Literal.isFloat)
Chris Lattner858eece2007-09-22 18:29:59 +0000471 Ty = Context.FloatTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000472 else if (!Literal.isLong)
Chris Lattner858eece2007-09-22 18:29:59 +0000473 Ty = Context.DoubleTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000474 else
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000475 Ty = Context.LongDoubleTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000476
477 const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty);
478
Ted Kremenekddedbe22007-11-29 00:56:49 +0000479 // isExact will be set by GetFloatValue().
480 bool isExact = false;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000481 Res = new FloatingLiteral(Literal.GetFloatValue(Format, &isExact), &isExact,
Ted Kremenekddedbe22007-11-29 00:56:49 +0000482 Ty, Tok.getLocation());
483
Chris Lattner1de66eb2007-08-26 03:42:43 +0000484 } else if (!Literal.isIntegerLiteral()) {
485 return ExprResult(true);
486 } else {
Chris Lattner48d7f382008-04-02 04:24:33 +0000487 QualType Ty;
Chris Lattner4b009652007-07-25 00:24:17 +0000488
Neil Booth7421e9c2007-08-29 22:00:19 +0000489 // long long is a C99 feature.
490 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000491 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000492 Diag(Tok.getLocation(), diag::ext_longlong);
493
Chris Lattner4b009652007-07-25 00:24:17 +0000494 // Get the value in the widest-possible width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000495 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000496
497 if (Literal.GetIntegerValue(ResultVal)) {
498 // If this value didn't fit into uintmax_t, warn and force to ull.
499 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattner48d7f382008-04-02 04:24:33 +0000500 Ty = Context.UnsignedLongLongTy;
501 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner8cd0e932008-03-05 18:54:05 +0000502 "long long is not intmax_t?");
Chris Lattner4b009652007-07-25 00:24:17 +0000503 } else {
504 // If this value fits into a ULL, try to figure out what else it fits into
505 // according to the rules of C99 6.4.4.1p5.
506
507 // Octal, Hexadecimal, and integers with a U suffix are allowed to
508 // be an unsigned int.
509 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
510
511 // Check from smallest to largest, picking the smallest type we can.
Chris Lattnere4068872008-05-09 05:59:00 +0000512 unsigned Width = 0;
Chris Lattner98540b62007-08-23 21:58:08 +0000513 if (!Literal.isLong && !Literal.isLongLong) {
514 // Are int/unsigned possibilities?
Chris Lattnere4068872008-05-09 05:59:00 +0000515 unsigned IntSize = Context.Target.getIntWidth();
516
Chris Lattner4b009652007-07-25 00:24:17 +0000517 // Does it fit in a unsigned int?
518 if (ResultVal.isIntN(IntSize)) {
519 // Does it fit in a signed int?
520 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000521 Ty = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000522 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000523 Ty = Context.UnsignedIntTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000524 Width = IntSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000525 }
Chris Lattner4b009652007-07-25 00:24:17 +0000526 }
527
528 // Are long/unsigned long possibilities?
Chris Lattner48d7f382008-04-02 04:24:33 +0000529 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattnere4068872008-05-09 05:59:00 +0000530 unsigned LongSize = Context.Target.getLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000531
532 // Does it fit in a unsigned long?
533 if (ResultVal.isIntN(LongSize)) {
534 // Does it fit in a signed long?
535 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000536 Ty = Context.LongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000537 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000538 Ty = Context.UnsignedLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000539 Width = LongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000540 }
Chris Lattner4b009652007-07-25 00:24:17 +0000541 }
542
543 // Finally, check long long if needed.
Chris Lattner48d7f382008-04-02 04:24:33 +0000544 if (Ty.isNull()) {
Chris Lattnere4068872008-05-09 05:59:00 +0000545 unsigned LongLongSize = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000546
547 // Does it fit in a unsigned long long?
548 if (ResultVal.isIntN(LongLongSize)) {
549 // Does it fit in a signed long long?
550 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000551 Ty = Context.LongLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000552 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000553 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000554 Width = LongLongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000555 }
556 }
557
558 // If we still couldn't decide a type, we probably have something that
559 // does not fit in a signed long long, but has no U suffix.
Chris Lattner48d7f382008-04-02 04:24:33 +0000560 if (Ty.isNull()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000561 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattner48d7f382008-04-02 04:24:33 +0000562 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000563 Width = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000564 }
Chris Lattnere4068872008-05-09 05:59:00 +0000565
566 if (ResultVal.getBitWidth() != Width)
567 ResultVal.trunc(Width);
Chris Lattner4b009652007-07-25 00:24:17 +0000568 }
569
Chris Lattner48d7f382008-04-02 04:24:33 +0000570 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000571 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000572
573 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
574 if (Literal.isImaginary)
575 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
576
577 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000578}
579
Steve Naroff87d58b42007-09-16 03:34:24 +0000580Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000581 ExprTy *Val) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000582 Expr *E = (Expr *)Val;
583 assert((E != 0) && "ActOnParenExpr() missing expr");
584 return new ParenExpr(L, R, E);
Chris Lattner4b009652007-07-25 00:24:17 +0000585}
586
587/// The UsualUnaryConversions() function is *not* called by this routine.
588/// See C99 6.3.2.1p[2-4] for more details.
589QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
590 SourceLocation OpLoc, bool isSizeof) {
591 // C99 6.5.3.4p1:
592 if (isa<FunctionType>(exprType) && isSizeof)
593 // alignof(function) is allowed.
594 Diag(OpLoc, diag::ext_sizeof_function_type);
595 else if (exprType->isVoidType())
596 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
597 else if (exprType->isIncompleteType()) {
598 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
599 diag::err_alignof_incomplete_type,
600 exprType.getAsString());
601 return QualType(); // error
602 }
603 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
604 return Context.getSizeType();
605}
606
607Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000608ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000609 SourceLocation LPLoc, TypeTy *Ty,
610 SourceLocation RPLoc) {
611 // If error parsing type, ignore.
612 if (Ty == 0) return true;
613
614 // Verify that this is a valid expression.
615 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
616
617 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
618
619 if (resultType.isNull())
620 return true;
621 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
622}
623
Chris Lattner5110ad52007-08-24 21:41:10 +0000624QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000625 DefaultFunctionArrayConversion(V);
626
Chris Lattnera16e42d2007-08-26 05:39:26 +0000627 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000628 if (const ComplexType *CT = V->getType()->getAsComplexType())
629 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000630
631 // Otherwise they pass through real integer and floating point types here.
632 if (V->getType()->isArithmeticType())
633 return V->getType();
634
635 // Reject anything else.
636 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
637 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000638}
639
640
Chris Lattner4b009652007-07-25 00:24:17 +0000641
Steve Naroff87d58b42007-09-16 03:34:24 +0000642Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000643 tok::TokenKind Kind,
644 ExprTy *Input) {
645 UnaryOperator::Opcode Opc;
646 switch (Kind) {
647 default: assert(0 && "Unknown unary op!");
648 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
649 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
650 }
651 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
652 if (result.isNull())
653 return true;
654 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
655}
656
657Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000658ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000659 ExprTy *Idx, SourceLocation RLoc) {
660 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
661
662 // Perform default conversions.
663 DefaultFunctionArrayConversion(LHSExp);
664 DefaultFunctionArrayConversion(RHSExp);
665
666 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
667
668 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000669 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000670 // in the subscript position. As a result, we need to derive the array base
671 // and index from the expression types.
672 Expr *BaseExpr, *IndexExpr;
673 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000674 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000675 BaseExpr = LHSExp;
676 IndexExpr = RHSExp;
677 // FIXME: need to deal with const...
678 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000679 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000680 // Handle the uncommon case of "123[Ptr]".
681 BaseExpr = RHSExp;
682 IndexExpr = LHSExp;
683 // FIXME: need to deal with const...
684 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000685 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
686 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000687 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000688
689 // Component access limited to variables (reject vec4.rg[1]).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000690 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
691 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000692 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000693 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000694 // FIXME: need to deal with const...
695 ResultType = VTy->getElementType();
696 } else {
697 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
698 RHSExp->getSourceRange());
699 }
700 // C99 6.5.2.1p1
701 if (!IndexExpr->getType()->isIntegerType())
702 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
703 IndexExpr->getSourceRange());
704
705 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
706 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattner9db553e2008-04-02 06:59:01 +0000707 // void (*)(int)) and pointers to incomplete types. Functions are not
708 // objects in C99.
Chris Lattner4b009652007-07-25 00:24:17 +0000709 if (!ResultType->isObjectType())
710 return Diag(BaseExpr->getLocStart(),
711 diag::err_typecheck_subscript_not_object,
712 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
713
714 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
715}
716
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000717QualType Sema::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000718CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000719 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000720 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begemanc8e51f82008-05-09 06:41:27 +0000721
722 // This flag determines whether or not the component is to be treated as a
723 // special name, or a regular GLSL-style component access.
724 bool SpecialComponent = false;
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000725
726 // The vector accessor can't exceed the number of elements.
727 const char *compStr = CompName.getName();
728 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000729 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000730 baseType.getAsString(), SourceRange(CompLoc));
731 return QualType();
732 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000733
734 // Check that we've found one of the special components, or that the component
735 // names must come from the same set.
736 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
737 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
738 SpecialComponent = true;
739 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner9096b792007-08-02 22:33:49 +0000740 do
741 compStr++;
742 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
743 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
744 do
745 compStr++;
746 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
747 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
748 do
749 compStr++;
750 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
751 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000752
Nate Begemanc8e51f82008-05-09 06:41:27 +0000753 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000754 // We didn't get to the end of the string. This means the component names
755 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000756 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000757 std::string(compStr,compStr+1), SourceRange(CompLoc));
758 return QualType();
759 }
760 // Each component accessor can't exceed the vector type.
761 compStr = CompName.getName();
762 while (*compStr) {
763 if (vecType->isAccessorWithinNumElements(*compStr))
764 compStr++;
765 else
766 break;
767 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000768 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000769 // We didn't get to the end of the string. This means a component accessor
770 // exceeds the number of elements in the vector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000771 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000772 baseType.getAsString(), SourceRange(CompLoc));
773 return QualType();
774 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000775
776 // If we have a special component name, verify that the current vector length
777 // is an even number, since all special component names return exactly half
778 // the elements.
779 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
780 return QualType();
781 }
782
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000783 // The component accessor looks fine - now we need to compute the actual type.
784 // The vector type is implied by the component accessor. For example,
785 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begemanc8e51f82008-05-09 06:41:27 +0000786 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
787 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
788 : strlen(CompName.getName());
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000789 if (CompSize == 1)
790 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000791
Nate Begemanaf6ed502008-04-18 23:10:10 +0000792 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroff82113e32007-07-29 16:33:31 +0000793 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemanaf6ed502008-04-18 23:10:10 +0000794 // diagostics look bad. We want extended vector types to appear built-in.
795 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
796 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
797 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroff82113e32007-07-29 16:33:31 +0000798 }
799 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000800}
801
Chris Lattner4b009652007-07-25 00:24:17 +0000802Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000803ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000804 tok::TokenKind OpKind, SourceLocation MemberLoc,
805 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000806 Expr *BaseExpr = static_cast<Expr *>(Base);
807 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000808
809 // Perform default conversions.
810 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000811
Steve Naroff2cb66382007-07-26 03:11:44 +0000812 QualType BaseType = BaseExpr->getType();
813 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000814
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000815 // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr
816 // must have pointer type, and the accessed type is the pointee.
Chris Lattner4b009652007-07-25 00:24:17 +0000817 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000818 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000819 BaseType = PT->getPointeeType();
820 else
Chris Lattner7d5a8762008-07-21 05:35:34 +0000821 return Diag(MemberLoc, diag::err_typecheck_member_reference_arrow,
822 BaseType.getAsString(), BaseExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000823 }
Chris Lattnera57cf472008-07-21 04:28:12 +0000824
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000825 // Handle field access to simple records. This also handles access to fields
826 // of the ObjC 'id' struct.
Chris Lattnere35a1042007-07-31 19:29:30 +0000827 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000828 RecordDecl *RDecl = RTy->getDecl();
829 if (RTy->isIncompleteType())
830 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
831 BaseExpr->getSourceRange());
832 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000833 FieldDecl *MemberDecl = RDecl->getMember(&Member);
834 if (!MemberDecl)
Chris Lattner7d5a8762008-07-21 05:35:34 +0000835 return Diag(MemberLoc, diag::err_typecheck_no_member, Member.getName(),
836 BaseExpr->getSourceRange());
Eli Friedman76b49832008-02-06 22:48:16 +0000837
838 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000839 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000840 QualType MemberType = MemberDecl->getType();
841 unsigned combinedQualifiers =
Chris Lattner35fef522008-02-20 20:55:12 +0000842 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman76b49832008-02-06 22:48:16 +0000843 MemberType = MemberType.getQualifiedType(combinedQualifiers);
844
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000845 return new MemberExpr(BaseExpr, OpKind == tok::arrow, MemberDecl,
Eli Friedman76b49832008-02-06 22:48:16 +0000846 MemberLoc, MemberType);
Chris Lattnera57cf472008-07-21 04:28:12 +0000847 }
848
Chris Lattnere9d71612008-07-21 04:59:05 +0000849 // Handle access to Objective-C instance variables, such as "Obj->ivar" and
850 // (*Obj).ivar.
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000851 if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) {
852 if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000853 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
Chris Lattnera57cf472008-07-21 04:28:12 +0000854 OpKind == tok::arrow);
Chris Lattner7d5a8762008-07-21 05:35:34 +0000855 return Diag(MemberLoc, diag::err_typecheck_member_reference_ivar,
Chris Lattner52292be2008-07-21 04:42:08 +0000856 IFTy->getDecl()->getName(), Member.getName(),
Chris Lattner7d5a8762008-07-21 05:35:34 +0000857 BaseExpr->getSourceRange());
Chris Lattnera57cf472008-07-21 04:28:12 +0000858 }
859
Chris Lattnere9d71612008-07-21 04:59:05 +0000860 // Handle Objective-C property access, which is "Obj.property" where Obj is a
861 // pointer to a (potentially qualified) interface type.
862 const PointerType *PTy;
863 const ObjCInterfaceType *IFTy;
864 if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) &&
865 (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) {
866 ObjCInterfaceDecl *IFace = IFTy->getDecl();
867
Chris Lattner55a24332008-07-21 06:44:27 +0000868 // FIXME: The logic for looking up nullary and unary selectors should be
869 // shared with the code in ActOnInstanceMessage.
870
Chris Lattnere9d71612008-07-21 04:59:05 +0000871 // Before we look for explicit property declarations, we check for
872 // nullary methods (which allow '.' notation).
873 Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
Chris Lattnere9d71612008-07-21 04:59:05 +0000874 if (ObjCMethodDecl *MD = IFace->lookupInstanceMethod(Sel))
875 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
876 MemberLoc, BaseExpr);
877
Chris Lattner55a24332008-07-21 06:44:27 +0000878 // If this reference is in an @implementation, check for 'private' methods.
879 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
880 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
881 if (ObjCImplementationDecl *ImpDecl =
882 ObjCImplementations[ClassDecl->getIdentifier()])
883 if (ObjCMethodDecl *MD = ImpDecl->getInstanceMethod(Sel))
884 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
885 MemberLoc, BaseExpr);
886 }
887
Chris Lattnere9d71612008-07-21 04:59:05 +0000888 // FIXME: Need to deal with setter methods that take 1 argument. E.g.:
889 // @interface NSBundle : NSObject {}
890 // - (NSString *)bundlePath;
891 // - (void)setBundlePath:(NSString *)x;
892 // @end
893 // void someMethod() { frameworkBundle.bundlePath = 0; }
894 //
895 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member))
896 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
897
898 // Lastly, check protocols on qualified interfaces.
Chris Lattnerd5f81792008-07-21 05:20:01 +0000899 for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(),
900 E = IFTy->qual_end(); I != E; ++I)
901 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member))
902 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000903 }
Chris Lattnera57cf472008-07-21 04:28:12 +0000904
905 // Handle 'field access' to vectors, such as 'V.xx'.
906 if (BaseType->isExtVectorType() && OpKind == tok::period) {
907 // Component access limited to variables (reject vec4.rg.g).
908 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
909 !isa<ExtVectorElementExpr>(BaseExpr))
Chris Lattner7d5a8762008-07-21 05:35:34 +0000910 return Diag(MemberLoc, diag::err_ext_vector_component_access,
911 BaseExpr->getSourceRange());
Chris Lattnera57cf472008-07-21 04:28:12 +0000912 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
913 if (ret.isNull())
914 return true;
915 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
916 }
917
Chris Lattner7d5a8762008-07-21 05:35:34 +0000918 return Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union,
919 BaseType.getAsString(), BaseExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000920}
921
Steve Naroff87d58b42007-09-16 03:34:24 +0000922/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000923/// This provides the location of the left/right parens and a list of comma
924/// locations.
925Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000926ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000927 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +0000928 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
929 Expr *Fn = static_cast<Expr *>(fn);
930 Expr **Args = reinterpret_cast<Expr**>(args);
931 assert(Fn && "no function call expression");
Chris Lattner3e254fb2008-04-08 04:40:51 +0000932 FunctionDecl *FDecl = NULL;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000933
934 // Promote the function operand.
935 UsualUnaryConversions(Fn);
936
937 // If we're directly calling a function, get the declaration for
938 // that function.
939 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
940 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
941 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
942
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000943 // Make the call expr early, before semantic checks. This guarantees cleanup
944 // of arguments and function on error.
Chris Lattner97316c02008-04-10 02:22:51 +0000945 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000946 Context.BoolTy, RParenLoc));
947
Chris Lattner4b009652007-07-25 00:24:17 +0000948 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
949 // type pointer to function".
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000950 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000951 if (PT == 0)
952 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
953 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000954 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
955 if (FuncT == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000956 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
957 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000958
959 // We know the result type of the call, set it.
960 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +0000961
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000962 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000963 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
964 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000965 unsigned NumArgsInProto = Proto->getNumArgs();
966 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +0000967
Chris Lattner3e254fb2008-04-08 04:40:51 +0000968 // If too few arguments are available (and we don't have default
969 // arguments for the remaining parameters), don't make the call.
970 if (NumArgs < NumArgsInProto) {
Chris Lattner97316c02008-04-10 02:22:51 +0000971 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000972 // Use default arguments for missing arguments
973 NumArgsToCheck = NumArgsInProto;
Chris Lattner97316c02008-04-10 02:22:51 +0000974 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000975 } else
976 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
977 Fn->getSourceRange());
978 }
979
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000980 // If too many are passed and not variadic, error on the extras and drop
981 // them.
982 if (NumArgs > NumArgsInProto) {
983 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000984 Diag(Args[NumArgsInProto]->getLocStart(),
985 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
986 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000987 Args[NumArgs-1]->getLocEnd()));
988 // This deletes the extra arguments.
989 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +0000990 }
991 NumArgsToCheck = NumArgsInProto;
992 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000993
Chris Lattner4b009652007-07-25 00:24:17 +0000994 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000995 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner005ed752008-01-04 18:04:52 +0000996 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000997
998 Expr *Arg;
999 if (i < NumArgs)
1000 Arg = Args[i];
1001 else
1002 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner005ed752008-01-04 18:04:52 +00001003 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001004
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001005 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +00001006 AssignConvertType ConvTy =
1007 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001008 TheCall->setArg(i, Arg);
1009
Chris Lattner005ed752008-01-04 18:04:52 +00001010 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
1011 ArgType, Arg, "passing"))
1012 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001013 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001014
1015 // If this is a variadic call, handle args passed through "...".
1016 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +00001017 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001018 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
1019 Expr *Arg = Args[i];
1020 DefaultArgumentPromotion(Arg);
1021 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +00001022 }
Steve Naroffdb65e052007-08-28 23:30:39 +00001023 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001024 } else {
1025 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
1026
Steve Naroffdb65e052007-08-28 23:30:39 +00001027 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001028 for (unsigned i = 0; i != NumArgs; i++) {
1029 Expr *Arg = Args[i];
1030 DefaultArgumentPromotion(Arg);
1031 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +00001032 }
Chris Lattner4b009652007-07-25 00:24:17 +00001033 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001034
Chris Lattner2e64c072007-08-10 20:18:51 +00001035 // Do special checking on direct calls to functions.
Eli Friedmand0e9d092008-05-14 19:38:39 +00001036 if (FDecl)
1037 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattner2e64c072007-08-10 20:18:51 +00001038
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001039 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +00001040}
1041
1042Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001043ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +00001044 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +00001045 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001046 QualType literalType = QualType::getFromOpaquePtr(Ty);
1047 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +00001048 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +00001049 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +00001050
Eli Friedman8c2173d2008-05-20 05:22:08 +00001051 if (literalType->isArrayType()) {
1052 if (literalType->getAsVariableArrayType())
1053 return Diag(LParenLoc,
1054 diag::err_variable_object_no_init,
1055 SourceRange(LParenLoc,
1056 literalExpr->getSourceRange().getEnd()));
1057 } else if (literalType->isIncompleteType()) {
1058 return Diag(LParenLoc,
1059 diag::err_typecheck_decl_incomplete_type,
1060 literalType.getAsString(),
1061 SourceRange(LParenLoc,
1062 literalExpr->getSourceRange().getEnd()));
1063 }
1064
Steve Narofff0b23542008-01-10 22:15:12 +00001065 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +00001066 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +00001067
Argiris Kirtzidis95256e62008-06-28 06:07:14 +00001068 bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl();
Steve Naroffbe37fc02008-01-14 18:19:28 +00001069 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +00001070 if (CheckForConstantInitializer(literalExpr, literalType))
1071 return true;
1072 }
Steve Naroffbe37fc02008-01-14 18:19:28 +00001073 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001074}
1075
1076Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001077ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +00001078 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +00001079 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +00001080
Steve Naroff0acc9c92007-09-15 18:49:24 +00001081 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +00001082 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +00001083
Chris Lattner48d7f382008-04-02 04:24:33 +00001084 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
1085 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
1086 return E;
Chris Lattner4b009652007-07-25 00:24:17 +00001087}
1088
Chris Lattnerd1f26b32007-12-20 00:44:32 +00001089bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001090 assert(VectorTy->isVectorType() && "Not a vector type!");
1091
1092 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001093 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001094 return Diag(R.getBegin(),
1095 Ty->isVectorType() ?
1096 diag::err_invalid_conversion_between_vectors :
1097 diag::err_invalid_conversion_between_vector_and_integer,
1098 VectorTy.getAsString().c_str(),
1099 Ty.getAsString().c_str(), R);
1100 } else
1101 return Diag(R.getBegin(),
1102 diag::err_invalid_conversion_between_vector_and_scalar,
1103 VectorTy.getAsString().c_str(),
1104 Ty.getAsString().c_str(), R);
1105
1106 return false;
1107}
1108
Chris Lattner4b009652007-07-25 00:24:17 +00001109Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001110ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +00001111 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +00001112 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +00001113
1114 Expr *castExpr = static_cast<Expr*>(Op);
1115 QualType castType = QualType::getFromOpaquePtr(Ty);
1116
Steve Naroff68adb482007-08-31 00:32:44 +00001117 UsualUnaryConversions(castExpr);
1118
Chris Lattner4b009652007-07-25 00:24:17 +00001119 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
1120 // type needs to be scalar.
Chris Lattnerdb526732007-10-29 04:26:44 +00001121 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff5ad85292008-06-03 12:56:35 +00001122 if (!castType->isScalarType() && !castType->isVectorType()) {
1123 // GCC struct/union extension.
1124 if (castType == castExpr->getType() &&
Steve Naroff7f1c5b52008-06-03 13:21:30 +00001125 castType->isStructureType() || castType->isUnionType()) {
1126 Diag(LParenLoc, diag::ext_typecheck_cast_nonscalar,
1127 SourceRange(LParenLoc, RParenLoc));
1128 return new CastExpr(castType, castExpr, LParenLoc);
1129 } else
Steve Naroff5ad85292008-06-03 12:56:35 +00001130 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
1131 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
1132 }
Steve Narofff459ee52008-01-24 22:55:05 +00001133 if (!castExpr->getType()->isScalarType() &&
1134 !castExpr->getType()->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +00001135 return Diag(castExpr->getLocStart(),
1136 diag::err_typecheck_expect_scalar_operand,
1137 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001138
1139 if (castExpr->getType()->isVectorType()) {
1140 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
1141 castExpr->getType(), castType))
1142 return true;
1143 } else if (castType->isVectorType()) {
1144 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
1145 castType, castExpr->getType()))
1146 return true;
Chris Lattnerdb526732007-10-29 04:26:44 +00001147 }
Chris Lattner4b009652007-07-25 00:24:17 +00001148 }
1149 return new CastExpr(castType, castExpr, LParenLoc);
1150}
1151
Chris Lattner98a425c2007-11-26 01:40:58 +00001152/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
1153/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +00001154inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
1155 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
1156 UsualUnaryConversions(cond);
1157 UsualUnaryConversions(lex);
1158 UsualUnaryConversions(rex);
1159 QualType condT = cond->getType();
1160 QualType lexT = lex->getType();
1161 QualType rexT = rex->getType();
1162
1163 // first, check the condition.
1164 if (!condT->isScalarType()) { // C99 6.5.15p2
1165 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
1166 condT.getAsString());
1167 return QualType();
1168 }
Chris Lattner992ae932008-01-06 22:42:25 +00001169
1170 // Now check the two expressions.
1171
1172 // If both operands have arithmetic type, do the usual arithmetic conversions
1173 // to find a common type: C99 6.5.15p3,5.
1174 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001175 UsualArithmeticConversions(lex, rex);
1176 return lex->getType();
1177 }
Chris Lattner992ae932008-01-06 22:42:25 +00001178
1179 // If both operands are the same structure or union type, the result is that
1180 // type.
Chris Lattner71225142007-07-31 21:27:01 +00001181 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +00001182 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +00001183 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +00001184 // "If both the operands have structure or union type, the result has
1185 // that type." This implies that CV qualifiers are dropped.
1186 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001187 }
Chris Lattner992ae932008-01-06 22:42:25 +00001188
1189 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroff95cb3892008-05-12 21:44:38 +00001190 // The following || allows only one side to be void (a GCC-ism).
1191 if (lexT->isVoidType() || rexT->isVoidType()) {
Eli Friedmanf025aac2008-06-04 19:47:51 +00001192 if (!lexT->isVoidType())
Steve Naroff95cb3892008-05-12 21:44:38 +00001193 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
1194 rex->getSourceRange());
1195 if (!rexT->isVoidType())
1196 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
Nuno Lopes4ba41fd2008-06-04 19:14:12 +00001197 lex->getSourceRange());
Eli Friedmanf025aac2008-06-04 19:47:51 +00001198 ImpCastExprToType(lex, Context.VoidTy);
1199 ImpCastExprToType(rex, Context.VoidTy);
1200 return Context.VoidTy;
Steve Naroff95cb3892008-05-12 21:44:38 +00001201 }
Steve Naroff12ebf272008-01-08 01:11:38 +00001202 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
1203 // the type of the other operand."
1204 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001205 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +00001206 return lexT;
1207 }
1208 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001209 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +00001210 return rexT;
1211 }
Chris Lattner0ac51632008-01-06 22:50:31 +00001212 // Handle the case where both operands are pointers before we handle null
1213 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +00001214 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
1215 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
1216 // get the "pointed to" types
1217 QualType lhptee = LHSPT->getPointeeType();
1218 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001219
Chris Lattner71225142007-07-31 21:27:01 +00001220 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
1221 if (lhptee->isVoidType() &&
Chris Lattner9db553e2008-04-02 06:59:01 +00001222 rhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +00001223 // Figure out necessary qualifiers (C99 6.5.15p6)
1224 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +00001225 QualType destType = Context.getPointerType(destPointee);
1226 ImpCastExprToType(lex, destType); // add qualifiers if necessary
1227 ImpCastExprToType(rex, destType); // promote to void*
1228 return destType;
1229 }
Chris Lattner9db553e2008-04-02 06:59:01 +00001230 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +00001231 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +00001232 QualType destType = Context.getPointerType(destPointee);
1233 ImpCastExprToType(lex, destType); // add qualifiers if necessary
1234 ImpCastExprToType(rex, destType); // promote to void*
1235 return destType;
1236 }
Chris Lattner4b009652007-07-25 00:24:17 +00001237
Steve Naroff85f0dc52007-10-15 20:41:53 +00001238 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1239 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +00001240 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +00001241 lexT.getAsString(), rexT.getAsString(),
1242 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman33284862008-01-30 17:02:03 +00001243 // In this situation, we assume void* type. No especially good
1244 // reason, but this is what gcc does, and we do have to pick
1245 // to get a consistent AST.
1246 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
1247 ImpCastExprToType(lex, voidPtrTy);
1248 ImpCastExprToType(rex, voidPtrTy);
1249 return voidPtrTy;
Chris Lattner71225142007-07-31 21:27:01 +00001250 }
1251 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001252 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
1253 // differently qualified versions of compatible types, the result type is
1254 // a pointer to an appropriately qualified version of the *composite*
1255 // type.
Eli Friedmane38150e2008-05-16 20:37:07 +00001256 // FIXME: Need to calculate the composite type.
Eli Friedmanca07c902008-02-10 22:59:36 +00001257 // FIXME: Need to add qualifiers
Eli Friedmane38150e2008-05-16 20:37:07 +00001258 QualType compositeType = lexT;
1259 ImpCastExprToType(lex, compositeType);
1260 ImpCastExprToType(rex, compositeType);
1261 return compositeType;
Chris Lattner4b009652007-07-25 00:24:17 +00001262 }
Chris Lattner4b009652007-07-25 00:24:17 +00001263 }
Steve Naroff605896f2008-05-31 22:33:45 +00001264 // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type
1265 // evaluates to "struct objc_object *" (and is handled above when comparing
1266 // id with statically typed objects). FIXME: Do we need an ImpCastExprToType?
1267 if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) {
1268 if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true))
1269 return Context.getObjCIdType();
1270 }
Chris Lattner992ae932008-01-06 22:42:25 +00001271 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +00001272 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
1273 lexT.getAsString(), rexT.getAsString(),
1274 lex->getSourceRange(), rex->getSourceRange());
1275 return QualType();
1276}
1277
Steve Naroff87d58b42007-09-16 03:34:24 +00001278/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +00001279/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +00001280Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001281 SourceLocation ColonLoc,
1282 ExprTy *Cond, ExprTy *LHS,
1283 ExprTy *RHS) {
1284 Expr *CondExpr = (Expr *) Cond;
1285 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +00001286
1287 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
1288 // was the condition.
1289 bool isLHSNull = LHSExpr == 0;
1290 if (isLHSNull)
1291 LHSExpr = CondExpr;
1292
Chris Lattner4b009652007-07-25 00:24:17 +00001293 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
1294 RHSExpr, QuestionLoc);
1295 if (result.isNull())
1296 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +00001297 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
1298 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +00001299}
1300
Chris Lattner4b009652007-07-25 00:24:17 +00001301
1302// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1303// being closely modeled after the C99 spec:-). The odd characteristic of this
1304// routine is it effectively iqnores the qualifiers on the top level pointee.
1305// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1306// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001307Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001308Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1309 QualType lhptee, rhptee;
1310
1311 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001312 lhptee = lhsType->getAsPointerType()->getPointeeType();
1313 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001314
1315 // make sure we operate on the canonical type
1316 lhptee = lhptee.getCanonicalType();
1317 rhptee = rhptee.getCanonicalType();
1318
Chris Lattner005ed752008-01-04 18:04:52 +00001319 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001320
1321 // C99 6.5.16.1p1: This following citation is common to constraints
1322 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1323 // qualifiers of the type *pointed to* by the right;
Chris Lattner35fef522008-02-20 20:55:12 +00001324 // FIXME: Handle ASQualType
1325 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1326 rhptee.getCVRQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001327 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001328
1329 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1330 // incomplete type and the other is a pointer to a qualified or unqualified
1331 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001332 if (lhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001333 if (rhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001334 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001335
1336 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001337 assert(rhptee->isFunctionType());
1338 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001339 }
1340
1341 if (rhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001342 if (lhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001343 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001344
1345 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001346 assert(lhptee->isFunctionType());
1347 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001348 }
1349
Chris Lattner4b009652007-07-25 00:24:17 +00001350 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1351 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001352 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1353 rhptee.getUnqualifiedType()))
1354 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001355 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001356}
1357
1358/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1359/// has code to accommodate several GCC extensions when type checking
1360/// pointers. Here are some objectionable examples that GCC considers warnings:
1361///
1362/// int a, *pint;
1363/// short *pshort;
1364/// struct foo *pfoo;
1365///
1366/// pint = pshort; // warning: assignment from incompatible pointer type
1367/// a = pint; // warning: assignment makes integer from pointer without a cast
1368/// pint = a; // warning: assignment makes pointer from integer without a cast
1369/// pint = pfoo; // warning: assignment from incompatible pointer type
1370///
1371/// As a result, the code for dealing with pointers is more complex than the
1372/// C99 spec dictates.
Chris Lattner4b009652007-07-25 00:24:17 +00001373///
Chris Lattner005ed752008-01-04 18:04:52 +00001374Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001375Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001376 // Get canonical types. We're not formatting these types, just comparing
1377 // them.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001378 lhsType = lhsType.getCanonicalType().getUnqualifiedType();
1379 rhsType = rhsType.getCanonicalType().getUnqualifiedType();
1380
1381 if (lhsType == rhsType)
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001382 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001383
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001384 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattnere1577e22008-04-07 06:52:53 +00001385 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001386 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001387 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001388 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001389
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001390 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1391 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001392 return Compatible;
Steve Naroff936c4362008-06-03 14:04:54 +00001393 // Relax integer conversions like we do for pointers below.
1394 if (rhsType->isIntegerType())
1395 return IntToPointer;
1396 if (lhsType->isIntegerType())
1397 return PointerToInt;
Chris Lattner1853da22008-01-04 23:18:45 +00001398 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001399 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001400
Nate Begemanc5f0f652008-07-14 18:02:46 +00001401 if (lhsType->isVectorType() || rhsType->isVectorType()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001402 // For ExtVector, allow vector splats; float -> <n x float>
Nate Begemanc5f0f652008-07-14 18:02:46 +00001403 if (const ExtVectorType *LV = lhsType->getAsExtVectorType())
1404 if (LV->getElementType() == rhsType)
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001405 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001406
Nate Begemanc5f0f652008-07-14 18:02:46 +00001407 // If we are allowing lax vector conversions, and LHS and RHS are both
1408 // vectors, the total size only needs to be the same. This is a bitcast;
1409 // no bits are changed but the result type is different.
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001410 if (getLangOptions().LaxVectorConversions &&
1411 lhsType->isVectorType() && rhsType->isVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001412 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
1413 return Compatible;
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001414 }
1415 return Incompatible;
1416 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001417
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001418 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001419 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001420
Chris Lattner390564e2008-04-07 06:49:41 +00001421 if (isa<PointerType>(lhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001422 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001423 return IntToPointer;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001424
Chris Lattner390564e2008-04-07 06:49:41 +00001425 if (isa<PointerType>(rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001426 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001427 return Incompatible;
1428 }
1429
Chris Lattner390564e2008-04-07 06:49:41 +00001430 if (isa<PointerType>(rhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001431 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001432 if (lhsType == Context.BoolTy)
1433 return Compatible;
1434
1435 if (lhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001436 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001437
Chris Lattner390564e2008-04-07 06:49:41 +00001438 if (isa<PointerType>(lhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001439 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001440 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001441 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001442
Chris Lattner1853da22008-01-04 23:18:45 +00001443 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner390564e2008-04-07 06:49:41 +00001444 if (Context.typesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001445 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001446 }
1447 return Incompatible;
1448}
1449
Chris Lattner005ed752008-01-04 18:04:52 +00001450Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001451Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001452 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1453 // a null pointer constant.
Ted Kremenek42730c52008-01-07 19:49:32 +00001454 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001455 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001456 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001457 return Compatible;
1458 }
Chris Lattner5f505bf2007-10-16 02:55:40 +00001459 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001460 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001461 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001462 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001463 //
1464 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1465 // are better understood.
1466 if (!lhsType->isReferenceType())
1467 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001468
Chris Lattner005ed752008-01-04 18:04:52 +00001469 Sema::AssignConvertType result =
1470 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001471
1472 // C99 6.5.16.1p2: The value of the right operand is converted to the
1473 // type of the assignment expression.
1474 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001475 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001476 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001477}
1478
Chris Lattner005ed752008-01-04 18:04:52 +00001479Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001480Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1481 return CheckAssignmentConstraints(lhsType, rhsType);
1482}
1483
Chris Lattner2c8bff72007-12-12 05:47:28 +00001484QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001485 Diag(loc, diag::err_typecheck_invalid_operands,
1486 lex->getType().getAsString(), rex->getType().getAsString(),
1487 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001488 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001489}
1490
1491inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1492 Expr *&rex) {
Nate Begeman03105572008-04-04 01:30:25 +00001493 // For conversion purposes, we ignore any qualifiers.
1494 // For example, "const float" and "float" are equivalent.
1495 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1496 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001497
Nate Begemanc5f0f652008-07-14 18:02:46 +00001498 // If the vector types are identical, return.
Nate Begeman03105572008-04-04 01:30:25 +00001499 if (lhsType == rhsType)
Chris Lattner4b009652007-07-25 00:24:17 +00001500 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001501
Nate Begemanc5f0f652008-07-14 18:02:46 +00001502 // Handle the case of a vector & extvector type of the same size and element
1503 // type. It would be nice if we only had one vector type someday.
1504 if (getLangOptions().LaxVectorConversions)
1505 if (const VectorType *LV = lhsType->getAsVectorType())
1506 if (const VectorType *RV = rhsType->getAsVectorType())
1507 if (LV->getElementType() == RV->getElementType() &&
1508 LV->getNumElements() == RV->getNumElements())
1509 return lhsType->isExtVectorType() ? lhsType : rhsType;
1510
1511 // If the lhs is an extended vector and the rhs is a scalar of the same type
1512 // or a literal, promote the rhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001513 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001514 QualType eltType = V->getElementType();
1515
1516 if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) ||
1517 (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) ||
1518 (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001519 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001520 return lhsType;
1521 }
1522 }
1523
Nate Begemanc5f0f652008-07-14 18:02:46 +00001524 // If the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001525 // promote the lhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001526 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001527 QualType eltType = V->getElementType();
1528
1529 if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) ||
1530 (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) ||
1531 (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001532 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001533 return rhsType;
1534 }
1535 }
1536
Chris Lattner4b009652007-07-25 00:24:17 +00001537 // You cannot convert between vector values of different size.
1538 Diag(loc, diag::err_typecheck_vector_not_convertable,
1539 lex->getType().getAsString(), rex->getType().getAsString(),
1540 lex->getSourceRange(), rex->getSourceRange());
1541 return QualType();
1542}
1543
1544inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001545 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001546{
1547 QualType lhsType = lex->getType(), rhsType = rex->getType();
1548
1549 if (lhsType->isVectorType() || rhsType->isVectorType())
1550 return CheckVectorOperands(loc, lex, rex);
1551
Steve Naroff8f708362007-08-24 19:07:16 +00001552 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001553
Chris Lattner4b009652007-07-25 00:24:17 +00001554 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001555 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001556 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001557}
1558
1559inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001560 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001561{
1562 QualType lhsType = lex->getType(), rhsType = rex->getType();
1563
Steve Naroff8f708362007-08-24 19:07:16 +00001564 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001565
Chris Lattner4b009652007-07-25 00:24:17 +00001566 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001567 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001568 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001569}
1570
1571inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001572 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001573{
1574 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1575 return CheckVectorOperands(loc, lex, rex);
1576
Steve Naroff8f708362007-08-24 19:07:16 +00001577 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001578
Chris Lattner4b009652007-07-25 00:24:17 +00001579 // handle the common case first (both operands are arithmetic).
1580 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001581 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001582
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001583 // Put any potential pointer into PExp
1584 Expr* PExp = lex, *IExp = rex;
1585 if (IExp->getType()->isPointerType())
1586 std::swap(PExp, IExp);
1587
1588 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1589 if (IExp->getType()->isIntegerType()) {
1590 // Check for arithmetic on pointers to incomplete types
1591 if (!PTy->getPointeeType()->isObjectType()) {
1592 if (PTy->getPointeeType()->isVoidType()) {
1593 Diag(loc, diag::ext_gnu_void_ptr,
1594 lex->getSourceRange(), rex->getSourceRange());
1595 } else {
1596 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1597 lex->getType().getAsString(), lex->getSourceRange());
1598 return QualType();
1599 }
1600 }
1601 return PExp->getType();
1602 }
1603 }
1604
Chris Lattner2c8bff72007-12-12 05:47:28 +00001605 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001606}
1607
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001608// C99 6.5.6
1609QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1610 SourceLocation loc, bool isCompAssign) {
Chris Lattner4b009652007-07-25 00:24:17 +00001611 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1612 return CheckVectorOperands(loc, lex, rex);
1613
Steve Naroff8f708362007-08-24 19:07:16 +00001614 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001615
Chris Lattnerf6da2912007-12-09 21:53:25 +00001616 // Enforce type constraints: C99 6.5.6p3.
1617
1618 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001619 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001620 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001621
1622 // Either ptr - int or ptr - ptr.
1623 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001624 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001625
Chris Lattnerf6da2912007-12-09 21:53:25 +00001626 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001627 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001628 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001629 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001630 Diag(loc, diag::ext_gnu_void_ptr,
1631 lex->getSourceRange(), rex->getSourceRange());
1632 } else {
1633 Diag(loc, diag::err_typecheck_sub_ptr_object,
1634 lex->getType().getAsString(), lex->getSourceRange());
1635 return QualType();
1636 }
1637 }
1638
1639 // The result type of a pointer-int computation is the pointer type.
1640 if (rex->getType()->isIntegerType())
1641 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001642
Chris Lattnerf6da2912007-12-09 21:53:25 +00001643 // Handle pointer-pointer subtractions.
1644 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001645 QualType rpointee = RHSPTy->getPointeeType();
1646
Chris Lattnerf6da2912007-12-09 21:53:25 +00001647 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001648 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001649 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001650 if (rpointee->isVoidType()) {
1651 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-12-09 21:53:25 +00001652 Diag(loc, diag::ext_gnu_void_ptr,
1653 lex->getSourceRange(), rex->getSourceRange());
1654 } else {
1655 Diag(loc, diag::err_typecheck_sub_ptr_object,
1656 rex->getType().getAsString(), rex->getSourceRange());
1657 return QualType();
1658 }
1659 }
1660
1661 // Pointee types must be compatible.
Steve Naroff577f9722008-01-29 18:58:14 +00001662 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1663 rpointee.getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001664 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1665 lex->getType().getAsString(), rex->getType().getAsString(),
1666 lex->getSourceRange(), rex->getSourceRange());
1667 return QualType();
1668 }
1669
1670 return Context.getPointerDiffType();
1671 }
1672 }
1673
Chris Lattner2c8bff72007-12-12 05:47:28 +00001674 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001675}
1676
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001677// C99 6.5.7
1678QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1679 bool isCompAssign) {
Chris Lattner2c8bff72007-12-12 05:47:28 +00001680 // C99 6.5.7p2: Each of the operands shall have integer type.
1681 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1682 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001683
Chris Lattner2c8bff72007-12-12 05:47:28 +00001684 // Shifts don't perform usual arithmetic conversions, they just do integer
1685 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001686 if (!isCompAssign)
1687 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001688 UsualUnaryConversions(rex);
1689
1690 // "The type of the result is that of the promoted left operand."
1691 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001692}
1693
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001694// C99 6.5.8
1695QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1696 bool isRelational) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001697 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1698 return CheckVectorCompareOperands(lex, rex, loc, isRelational);
1699
Chris Lattner254f3bc2007-08-26 01:18:55 +00001700 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001701 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1702 UsualArithmeticConversions(lex, rex);
1703 else {
1704 UsualUnaryConversions(lex);
1705 UsualUnaryConversions(rex);
1706 }
Chris Lattner4b009652007-07-25 00:24:17 +00001707 QualType lType = lex->getType();
1708 QualType rType = rex->getType();
1709
Ted Kremenek486509e2007-10-29 17:13:39 +00001710 // For non-floating point types, check for self-comparisons of the form
1711 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1712 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001713 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001714 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1715 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001716 if (DRL->getDecl() == DRR->getDecl())
1717 Diag(loc, diag::warn_selfcomparison);
1718 }
1719
Chris Lattner254f3bc2007-08-26 01:18:55 +00001720 if (isRelational) {
1721 if (lType->isRealType() && rType->isRealType())
1722 return Context.IntTy;
1723 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001724 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001725 if (lType->isFloatingType()) {
1726 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001727 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001728 }
1729
Chris Lattner254f3bc2007-08-26 01:18:55 +00001730 if (lType->isArithmeticType() && rType->isArithmeticType())
1731 return Context.IntTy;
1732 }
Chris Lattner4b009652007-07-25 00:24:17 +00001733
Chris Lattner22be8422007-08-26 01:10:14 +00001734 bool LHSIsNull = lex->isNullPointerConstant(Context);
1735 bool RHSIsNull = rex->isNullPointerConstant(Context);
1736
Chris Lattner254f3bc2007-08-26 01:18:55 +00001737 // All of the following pointer related warnings are GCC extensions, except
1738 // when handling null pointer constants. One day, we can consider making them
1739 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001740 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001741 QualType LCanPointeeTy =
1742 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1743 QualType RCanPointeeTy =
1744 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman50727042008-02-08 01:19:44 +00001745
Steve Naroff3b435622007-11-13 14:57:38 +00001746 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001747 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1748 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1749 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001750 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1751 lType.getAsString(), rType.getAsString(),
1752 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001753 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001754 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001755 return Context.IntTy;
1756 }
Steve Naroff936c4362008-06-03 14:04:54 +00001757 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) {
1758 if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
1759 ImpCastExprToType(rex, lType);
1760 return Context.IntTy;
1761 }
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001762 }
Steve Naroff936c4362008-06-03 14:04:54 +00001763 if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) &&
1764 rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001765 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001766 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1767 lType.getAsString(), rType.getAsString(),
1768 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001769 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001770 return Context.IntTy;
1771 }
Steve Naroff936c4362008-06-03 14:04:54 +00001772 if (lType->isIntegerType() &&
1773 (rType->isPointerType() || rType->isObjCQualifiedIdType())) {
Chris Lattner22be8422007-08-26 01:10:14 +00001774 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001775 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1776 lType.getAsString(), rType.getAsString(),
1777 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001778 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001779 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001780 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00001781 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001782}
1783
Nate Begemanc5f0f652008-07-14 18:02:46 +00001784/// CheckVectorCompareOperands - vector comparisons are a clang extension that
1785/// operates on extended vector types. Instead of producing an IntTy result,
1786/// like a scalar comparison, a vector comparison produces a vector of integer
1787/// types.
1788QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex,
1789 SourceLocation loc,
1790 bool isRelational) {
1791 // Check to make sure we're operating on vectors of the same type and width,
1792 // Allowing one side to be a scalar of element type.
1793 QualType vType = CheckVectorOperands(loc, lex, rex);
1794 if (vType.isNull())
1795 return vType;
1796
1797 QualType lType = lex->getType();
1798 QualType rType = rex->getType();
1799
1800 // For non-floating point types, check for self-comparisons of the form
1801 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1802 // often indicate logic errors in the program.
1803 if (!lType->isFloatingType()) {
1804 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1805 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
1806 if (DRL->getDecl() == DRR->getDecl())
1807 Diag(loc, diag::warn_selfcomparison);
1808 }
1809
1810 // Check for comparisons of floating point operands using != and ==.
1811 if (!isRelational && lType->isFloatingType()) {
1812 assert (rType->isFloatingType());
1813 CheckFloatComparison(loc,lex,rex);
1814 }
1815
1816 // Return the type for the comparison, which is the same as vector type for
1817 // integer vectors, or an integer type of identical size and number of
1818 // elements for floating point vectors.
1819 if (lType->isIntegerType())
1820 return lType;
1821
1822 const VectorType *VTy = lType->getAsVectorType();
1823
1824 // FIXME: need to deal with non-32b int / non-64b long long
1825 unsigned TypeSize = Context.getTypeSize(VTy->getElementType());
1826 if (TypeSize == 32) {
1827 return Context.getExtVectorType(Context.IntTy, VTy->getNumElements());
1828 }
1829 assert(TypeSize == 64 && "Unhandled vector element size in vector compare");
1830 return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements());
1831}
1832
Chris Lattner4b009652007-07-25 00:24:17 +00001833inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001834 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001835{
1836 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1837 return CheckVectorOperands(loc, lex, rex);
1838
Steve Naroff8f708362007-08-24 19:07:16 +00001839 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001840
1841 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001842 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001843 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001844}
1845
1846inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1847 Expr *&lex, Expr *&rex, SourceLocation loc)
1848{
1849 UsualUnaryConversions(lex);
1850 UsualUnaryConversions(rex);
1851
Eli Friedmanbea3f842008-05-13 20:16:47 +00001852 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Chris Lattner4b009652007-07-25 00:24:17 +00001853 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001854 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001855}
1856
1857inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001858 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001859{
1860 QualType lhsType = lex->getType();
1861 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner4b009652007-07-25 00:24:17 +00001862 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1863
1864 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00001865 case Expr::MLV_Valid:
1866 break;
1867 case Expr::MLV_ConstQualified:
1868 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1869 return QualType();
1870 case Expr::MLV_ArrayType:
1871 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1872 lhsType.getAsString(), lex->getSourceRange());
1873 return QualType();
1874 case Expr::MLV_NotObjectType:
1875 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1876 lhsType.getAsString(), lex->getSourceRange());
1877 return QualType();
1878 case Expr::MLV_InvalidExpression:
1879 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1880 lex->getSourceRange());
1881 return QualType();
1882 case Expr::MLV_IncompleteType:
1883 case Expr::MLV_IncompleteVoidType:
1884 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1885 lhsType.getAsString(), lex->getSourceRange());
1886 return QualType();
1887 case Expr::MLV_DuplicateVectorComponents:
1888 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1889 lex->getSourceRange());
1890 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001891 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001892
Chris Lattner005ed752008-01-04 18:04:52 +00001893 AssignConvertType ConvTy;
1894 if (compoundType.isNull())
1895 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1896 else
1897 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1898
1899 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1900 rex, "assigning"))
1901 return QualType();
1902
Chris Lattner4b009652007-07-25 00:24:17 +00001903 // C99 6.5.16p3: The type of an assignment expression is the type of the
1904 // left operand unless the left operand has qualified type, in which case
1905 // it is the unqualified version of the type of the left operand.
1906 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1907 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001908 // C++ 5.17p1: the type of the assignment expression is that of its left
1909 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00001910 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001911}
1912
1913inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1914 Expr *&lex, Expr *&rex, SourceLocation loc) {
Chris Lattner03c430f2008-07-25 20:54:07 +00001915
1916 // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions.
1917 DefaultFunctionArrayConversion(rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001918 return rex->getType();
1919}
1920
1921/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1922/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1923QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1924 QualType resType = op->getType();
1925 assert(!resType.isNull() && "no type for increment/decrement expression");
1926
Steve Naroffd30e1932007-08-24 17:20:07 +00001927 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00001928 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001929 if (pt->getPointeeType()->isVoidType()) {
1930 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
1931 } else if (!pt->getPointeeType()->isObjectType()) {
1932 // C99 6.5.2.4p2, 6.5.6p2
Chris Lattner4b009652007-07-25 00:24:17 +00001933 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1934 resType.getAsString(), op->getSourceRange());
1935 return QualType();
1936 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001937 } else if (!resType->isRealType()) {
1938 if (resType->isComplexType())
1939 // C99 does not support ++/-- on complex types.
1940 Diag(OpLoc, diag::ext_integer_increment_complex,
1941 resType.getAsString(), op->getSourceRange());
1942 else {
1943 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1944 resType.getAsString(), op->getSourceRange());
1945 return QualType();
1946 }
Chris Lattner4b009652007-07-25 00:24:17 +00001947 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001948 // At this point, we know we have a real, complex or pointer type.
1949 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001950 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1951 if (mlval != Expr::MLV_Valid) {
1952 // FIXME: emit a more precise diagnostic...
1953 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1954 op->getSourceRange());
1955 return QualType();
1956 }
1957 return resType;
1958}
1959
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001960/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00001961/// This routine allows us to typecheck complex/recursive expressions
1962/// where the declaration is needed for type checking. Here are some
1963/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattner48d7f382008-04-02 04:24:33 +00001964static ValueDecl *getPrimaryDecl(Expr *E) {
1965 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001966 case Stmt::DeclRefExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001967 return cast<DeclRefExpr>(E)->getDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001968 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00001969 // Fields cannot be declared with a 'register' storage class.
1970 // &X->f is always ok, even if X is declared register.
Chris Lattner48d7f382008-04-02 04:24:33 +00001971 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnera3249072007-11-16 17:46:48 +00001972 return 0;
Chris Lattner48d7f382008-04-02 04:24:33 +00001973 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001974 case Stmt::ArraySubscriptExprClass: {
1975 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1976
Chris Lattner48d7f382008-04-02 04:24:33 +00001977 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00001978 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001979 return 0;
1980 else
1981 return VD;
1982 }
Chris Lattner4b009652007-07-25 00:24:17 +00001983 case Stmt::UnaryOperatorClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001984 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001985 case Stmt::ParenExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001986 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00001987 case Stmt::ImplicitCastExprClass:
1988 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner48d7f382008-04-02 04:24:33 +00001989 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001990 default:
1991 return 0;
1992 }
1993}
1994
1995/// CheckAddressOfOperand - The operand of & must be either a function
1996/// designator or an lvalue designating an object. If it is an lvalue, the
1997/// object cannot be declared with storage class register or be a bit field.
1998/// Note: The usual conversions are *not* applied to the operand of the &
1999/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
2000QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00002001 if (getLangOptions().C99) {
2002 // Implement C99-only parts of addressof rules.
2003 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
2004 if (uOp->getOpcode() == UnaryOperator::Deref)
2005 // Per C99 6.5.3.2, the address of a deref always returns a valid result
2006 // (assuming the deref expression is valid).
2007 return uOp->getSubExpr()->getType();
2008 }
2009 // Technically, there should be a check for array subscript
2010 // expressions here, but the result of one is always an lvalue anyway.
2011 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00002012 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner4b009652007-07-25 00:24:17 +00002013 Expr::isLvalueResult lval = op->isLvalue();
2014
2015 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00002016 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
2017 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00002018 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
2019 op->getSourceRange());
2020 return QualType();
2021 }
Steve Naroff73cf87e2008-02-29 23:30:25 +00002022 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
2023 if (MemExpr->getMemberDecl()->isBitField()) {
2024 Diag(OpLoc, diag::err_typecheck_address_of,
2025 std::string("bit-field"), op->getSourceRange());
2026 return QualType();
2027 }
2028 // Check for Apple extension for accessing vector components.
2029 } else if (isa<ArraySubscriptExpr>(op) &&
2030 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
2031 Diag(OpLoc, diag::err_typecheck_address_of,
2032 std::string("vector"), op->getSourceRange());
2033 return QualType();
2034 } else if (dcl) { // C99 6.5.3.2p1
Chris Lattner4b009652007-07-25 00:24:17 +00002035 // We have an lvalue with a decl. Make sure the decl is not declared
2036 // with the register storage-class specifier.
2037 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
2038 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff73cf87e2008-02-29 23:30:25 +00002039 Diag(OpLoc, diag::err_typecheck_address_of,
2040 std::string("register variable"), op->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00002041 return QualType();
2042 }
2043 } else
2044 assert(0 && "Unknown/unexpected decl type");
Chris Lattner4b009652007-07-25 00:24:17 +00002045 }
2046 // If the operand has type "type", the result has type "pointer to type".
2047 return Context.getPointerType(op->getType());
2048}
2049
2050QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
2051 UsualUnaryConversions(op);
2052 QualType qType = op->getType();
2053
Chris Lattner7931f4a2007-07-31 16:53:04 +00002054 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00002055 // Note that per both C89 and C99, this is always legal, even
2056 // if ptype is an incomplete type or void.
2057 // It would be possible to warn about dereferencing a
2058 // void pointer, but it's completely well-defined,
2059 // and such a warning is unlikely to catch any mistakes.
2060 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00002061 }
2062 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
2063 qType.getAsString(), op->getSourceRange());
2064 return QualType();
2065}
2066
2067static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
2068 tok::TokenKind Kind) {
2069 BinaryOperator::Opcode Opc;
2070 switch (Kind) {
2071 default: assert(0 && "Unknown binop!");
2072 case tok::star: Opc = BinaryOperator::Mul; break;
2073 case tok::slash: Opc = BinaryOperator::Div; break;
2074 case tok::percent: Opc = BinaryOperator::Rem; break;
2075 case tok::plus: Opc = BinaryOperator::Add; break;
2076 case tok::minus: Opc = BinaryOperator::Sub; break;
2077 case tok::lessless: Opc = BinaryOperator::Shl; break;
2078 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
2079 case tok::lessequal: Opc = BinaryOperator::LE; break;
2080 case tok::less: Opc = BinaryOperator::LT; break;
2081 case tok::greaterequal: Opc = BinaryOperator::GE; break;
2082 case tok::greater: Opc = BinaryOperator::GT; break;
2083 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
2084 case tok::equalequal: Opc = BinaryOperator::EQ; break;
2085 case tok::amp: Opc = BinaryOperator::And; break;
2086 case tok::caret: Opc = BinaryOperator::Xor; break;
2087 case tok::pipe: Opc = BinaryOperator::Or; break;
2088 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
2089 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
2090 case tok::equal: Opc = BinaryOperator::Assign; break;
2091 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
2092 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
2093 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
2094 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
2095 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
2096 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
2097 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
2098 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
2099 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
2100 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
2101 case tok::comma: Opc = BinaryOperator::Comma; break;
2102 }
2103 return Opc;
2104}
2105
2106static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
2107 tok::TokenKind Kind) {
2108 UnaryOperator::Opcode Opc;
2109 switch (Kind) {
2110 default: assert(0 && "Unknown unary op!");
2111 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
2112 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
2113 case tok::amp: Opc = UnaryOperator::AddrOf; break;
2114 case tok::star: Opc = UnaryOperator::Deref; break;
2115 case tok::plus: Opc = UnaryOperator::Plus; break;
2116 case tok::minus: Opc = UnaryOperator::Minus; break;
2117 case tok::tilde: Opc = UnaryOperator::Not; break;
2118 case tok::exclaim: Opc = UnaryOperator::LNot; break;
2119 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
2120 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
2121 case tok::kw___real: Opc = UnaryOperator::Real; break;
2122 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
2123 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
2124 }
2125 return Opc;
2126}
2127
2128// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002129Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00002130 ExprTy *LHS, ExprTy *RHS) {
2131 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
2132 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
2133
Steve Naroff87d58b42007-09-16 03:34:24 +00002134 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
2135 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00002136
2137 QualType ResultTy; // Result type of the binary operator.
2138 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
2139
2140 switch (Opc) {
2141 default:
2142 assert(0 && "Unknown binary expr!");
2143 case BinaryOperator::Assign:
2144 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
2145 break;
2146 case BinaryOperator::Mul:
2147 case BinaryOperator::Div:
2148 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
2149 break;
2150 case BinaryOperator::Rem:
2151 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
2152 break;
2153 case BinaryOperator::Add:
2154 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
2155 break;
2156 case BinaryOperator::Sub:
2157 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
2158 break;
2159 case BinaryOperator::Shl:
2160 case BinaryOperator::Shr:
2161 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
2162 break;
2163 case BinaryOperator::LE:
2164 case BinaryOperator::LT:
2165 case BinaryOperator::GE:
2166 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002167 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002168 break;
2169 case BinaryOperator::EQ:
2170 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002171 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00002172 break;
2173 case BinaryOperator::And:
2174 case BinaryOperator::Xor:
2175 case BinaryOperator::Or:
2176 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
2177 break;
2178 case BinaryOperator::LAnd:
2179 case BinaryOperator::LOr:
2180 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
2181 break;
2182 case BinaryOperator::MulAssign:
2183 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002184 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002185 if (!CompTy.isNull())
2186 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2187 break;
2188 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002189 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002190 if (!CompTy.isNull())
2191 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2192 break;
2193 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002194 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002195 if (!CompTy.isNull())
2196 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2197 break;
2198 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002199 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002200 if (!CompTy.isNull())
2201 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2202 break;
2203 case BinaryOperator::ShlAssign:
2204 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002205 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002206 if (!CompTy.isNull())
2207 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2208 break;
2209 case BinaryOperator::AndAssign:
2210 case BinaryOperator::XorAssign:
2211 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002212 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002213 if (!CompTy.isNull())
2214 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2215 break;
2216 case BinaryOperator::Comma:
2217 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
2218 break;
2219 }
2220 if (ResultTy.isNull())
2221 return true;
2222 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00002223 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002224 else
Chris Lattnerf420df12007-08-28 18:36:55 +00002225 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002226}
2227
2228// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002229Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00002230 ExprTy *input) {
2231 Expr *Input = (Expr*)input;
2232 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2233 QualType resultType;
2234 switch (Opc) {
2235 default:
2236 assert(0 && "Unimplemented unary expr!");
2237 case UnaryOperator::PreInc:
2238 case UnaryOperator::PreDec:
2239 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
2240 break;
2241 case UnaryOperator::AddrOf:
2242 resultType = CheckAddressOfOperand(Input, OpLoc);
2243 break;
2244 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00002245 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00002246 resultType = CheckIndirectionOperand(Input, OpLoc);
2247 break;
2248 case UnaryOperator::Plus:
2249 case UnaryOperator::Minus:
2250 UsualUnaryConversions(Input);
2251 resultType = Input->getType();
2252 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
2253 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2254 resultType.getAsString());
2255 break;
2256 case UnaryOperator::Not: // bitwise complement
2257 UsualUnaryConversions(Input);
2258 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00002259 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2260 if (!resultType->isIntegerType()) {
2261 if (resultType->isComplexType())
2262 // C99 does not support '~' for complex conjugation.
2263 Diag(OpLoc, diag::ext_integer_complement_complex,
2264 resultType.getAsString());
2265 else
2266 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2267 resultType.getAsString());
2268 }
Chris Lattner4b009652007-07-25 00:24:17 +00002269 break;
2270 case UnaryOperator::LNot: // logical negation
2271 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
2272 DefaultFunctionArrayConversion(Input);
2273 resultType = Input->getType();
2274 if (!resultType->isScalarType()) // C99 6.5.3.3p1
2275 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2276 resultType.getAsString());
2277 // LNot always has type int. C99 6.5.3.3p5.
2278 resultType = Context.IntTy;
2279 break;
2280 case UnaryOperator::SizeOf:
2281 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
2282 break;
2283 case UnaryOperator::AlignOf:
2284 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2285 break;
Chris Lattner03931a72007-08-24 21:16:53 +00002286 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00002287 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00002288 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00002289 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002290 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00002291 resultType = Input->getType();
2292 break;
2293 }
2294 if (resultType.isNull())
2295 return true;
2296 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2297}
2298
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002299/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2300Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00002301 SourceLocation LabLoc,
2302 IdentifierInfo *LabelII) {
2303 // Look up the record for this label identifier.
2304 LabelStmt *&LabelDecl = LabelMap[LabelII];
2305
2306 // If we haven't seen this label yet, create a forward reference.
2307 if (LabelDecl == 0)
2308 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2309
2310 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00002311 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2312 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00002313}
2314
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002315Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00002316 SourceLocation RPLoc) { // "({..})"
2317 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2318 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2319 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2320
2321 // FIXME: there are a variety of strange constraints to enforce here, for
2322 // example, it is not possible to goto into a stmt expression apparently.
2323 // More semantic analysis is needed.
2324
2325 // FIXME: the last statement in the compount stmt has its value used. We
2326 // should not warn about it being unused.
2327
2328 // If there are sub stmts in the compound stmt, take the type of the last one
2329 // as the type of the stmtexpr.
2330 QualType Ty = Context.VoidTy;
2331
2332 if (!Compound->body_empty())
2333 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2334 Ty = LastExpr->getType();
2335
2336 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2337}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002338
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002339Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002340 SourceLocation TypeLoc,
2341 TypeTy *argty,
2342 OffsetOfComponent *CompPtr,
2343 unsigned NumComponents,
2344 SourceLocation RPLoc) {
2345 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2346 assert(!ArgTy.isNull() && "Missing type argument!");
2347
2348 // We must have at least one component that refers to the type, and the first
2349 // one is known to be a field designator. Verify that the ArgTy represents
2350 // a struct/union/class.
2351 if (!ArgTy->isRecordType())
2352 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2353
2354 // Otherwise, create a compound literal expression as the base, and
2355 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002356 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002357
Chris Lattnerb37522e2007-08-31 21:49:13 +00002358 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2359 // GCC extension, diagnose them.
2360 if (NumComponents != 1)
2361 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2362 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2363
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002364 for (unsigned i = 0; i != NumComponents; ++i) {
2365 const OffsetOfComponent &OC = CompPtr[i];
2366 if (OC.isBrackets) {
2367 // Offset of an array sub-field. TODO: Should we allow vector elements?
2368 const ArrayType *AT = Res->getType()->getAsArrayType();
2369 if (!AT) {
2370 delete Res;
2371 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2372 Res->getType().getAsString());
2373 }
2374
Chris Lattner2af6a802007-08-30 17:59:59 +00002375 // FIXME: C++: Verify that operator[] isn't overloaded.
2376
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002377 // C99 6.5.2.1p1
2378 Expr *Idx = static_cast<Expr*>(OC.U.E);
2379 if (!Idx->getType()->isIntegerType())
2380 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2381 Idx->getSourceRange());
2382
2383 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2384 continue;
2385 }
2386
2387 const RecordType *RC = Res->getType()->getAsRecordType();
2388 if (!RC) {
2389 delete Res;
2390 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2391 Res->getType().getAsString());
2392 }
2393
2394 // Get the decl corresponding to this.
2395 RecordDecl *RD = RC->getDecl();
2396 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2397 if (!MemberDecl)
2398 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2399 OC.U.IdentInfo->getName(),
2400 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002401
2402 // FIXME: C++: Verify that MemberDecl isn't a static field.
2403 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002404 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2405 // matter here.
2406 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002407 }
2408
2409 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2410 BuiltinLoc);
2411}
2412
2413
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002414Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002415 TypeTy *arg1, TypeTy *arg2,
2416 SourceLocation RPLoc) {
2417 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2418 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2419
2420 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2421
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002422 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002423}
2424
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002425Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002426 ExprTy *expr1, ExprTy *expr2,
2427 SourceLocation RPLoc) {
2428 Expr *CondExpr = static_cast<Expr*>(cond);
2429 Expr *LHSExpr = static_cast<Expr*>(expr1);
2430 Expr *RHSExpr = static_cast<Expr*>(expr2);
2431
2432 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2433
2434 // The conditional expression is required to be a constant expression.
2435 llvm::APSInt condEval(32);
2436 SourceLocation ExpLoc;
2437 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2438 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2439 CondExpr->getSourceRange());
2440
2441 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2442 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2443 RHSExpr->getType();
2444 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2445}
2446
Nate Begemanbd881ef2008-01-30 20:50:20 +00002447/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002448/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002449/// The number of arguments has already been validated to match the number of
2450/// arguments in FnType.
2451static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002452 unsigned NumParams = FnType->getNumArgs();
Nate Begeman778fd3b2008-04-18 23:35:14 +00002453 for (unsigned i = 0; i != NumParams; ++i) {
2454 QualType ExprTy = Args[i]->getType().getCanonicalType();
2455 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2456
2457 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002458 return false;
Nate Begeman778fd3b2008-04-18 23:35:14 +00002459 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002460 return true;
2461}
2462
2463Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2464 SourceLocation *CommaLocs,
2465 SourceLocation BuiltinLoc,
2466 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002467 // __builtin_overload requires at least 2 arguments
2468 if (NumArgs < 2)
2469 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2470 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002471
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002472 // The first argument is required to be a constant expression. It tells us
2473 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002474 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002475 Expr *NParamsExpr = Args[0];
2476 llvm::APSInt constEval(32);
2477 SourceLocation ExpLoc;
2478 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2479 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2480 NParamsExpr->getSourceRange());
2481
2482 // Verify that the number of parameters is > 0
2483 unsigned NumParams = constEval.getZExtValue();
2484 if (NumParams == 0)
2485 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2486 NParamsExpr->getSourceRange());
2487 // Verify that we have at least 1 + NumParams arguments to the builtin.
2488 if ((NumParams + 1) > NumArgs)
2489 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2490 SourceRange(BuiltinLoc, RParenLoc));
2491
2492 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002493 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002494 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002495 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2496 // UsualUnaryConversions will convert the function DeclRefExpr into a
2497 // pointer to function.
2498 Expr *Fn = UsualUnaryConversions(Args[i]);
2499 FunctionTypeProto *FnType = 0;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002500 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2501 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2502 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2503 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002504
2505 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2506 // parameters, and the number of parameters must match the value passed to
2507 // the builtin.
2508 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002509 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2510 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002511
2512 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002513 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002514 // If they match, return a new OverloadExpr.
Nate Begemanc6078c92008-01-31 05:38:29 +00002515 if (ExprsMatchFnType(Args+1, FnType)) {
2516 if (OE)
2517 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2518 OE->getFn()->getSourceRange());
2519 // Remember our match, and continue processing the remaining arguments
2520 // to catch any errors.
2521 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2522 BuiltinLoc, RParenLoc);
2523 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002524 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002525 // Return the newly created OverloadExpr node, if we succeded in matching
2526 // exactly one of the candidate functions.
2527 if (OE)
2528 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002529
2530 // If we didn't find a matching function Expr in the __builtin_overload list
2531 // the return an error.
2532 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002533 for (unsigned i = 0; i != NumParams; ++i) {
2534 if (i != 0) typeNames += ", ";
2535 typeNames += Args[i+1]->getType().getAsString();
2536 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002537
2538 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2539 SourceRange(BuiltinLoc, RParenLoc));
2540}
2541
Anders Carlsson36760332007-10-15 20:28:48 +00002542Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2543 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002544 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002545 Expr *E = static_cast<Expr*>(expr);
2546 QualType T = QualType::getFromOpaquePtr(type);
2547
2548 InitBuiltinVaListType();
2549
Chris Lattner005ed752008-01-04 18:04:52 +00002550 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2551 != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002552 return Diag(E->getLocStart(),
2553 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2554 E->getType().getAsString(),
2555 E->getSourceRange());
2556
2557 // FIXME: Warn if a non-POD type is passed in.
2558
2559 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2560}
2561
Chris Lattner005ed752008-01-04 18:04:52 +00002562bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2563 SourceLocation Loc,
2564 QualType DstType, QualType SrcType,
2565 Expr *SrcExpr, const char *Flavor) {
2566 // Decode the result (notice that AST's are still created for extensions).
2567 bool isInvalid = false;
2568 unsigned DiagKind;
2569 switch (ConvTy) {
2570 default: assert(0 && "Unknown conversion type");
2571 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002572 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00002573 DiagKind = diag::ext_typecheck_convert_pointer_int;
2574 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002575 case IntToPointer:
2576 DiagKind = diag::ext_typecheck_convert_int_pointer;
2577 break;
Chris Lattner005ed752008-01-04 18:04:52 +00002578 case IncompatiblePointer:
2579 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2580 break;
2581 case FunctionVoidPointer:
2582 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2583 break;
2584 case CompatiblePointerDiscardsQualifiers:
2585 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2586 break;
2587 case Incompatible:
2588 DiagKind = diag::err_typecheck_convert_incompatible;
2589 isInvalid = true;
2590 break;
2591 }
2592
2593 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2594 SrcExpr->getSourceRange());
2595 return isInvalid;
2596}