blob: 27e4027e68ac91b1f6977518f42ce4d0e7a70b9e [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,
Chris Lattnerf814d882008-07-25 21:45:37 +0000590 SourceLocation OpLoc,
591 const SourceRange &ExprRange,
592 bool isSizeof) {
Chris Lattner4b009652007-07-25 00:24:17 +0000593 // C99 6.5.3.4p1:
594 if (isa<FunctionType>(exprType) && isSizeof)
595 // alignof(function) is allowed.
Chris Lattnerf814d882008-07-25 21:45:37 +0000596 Diag(OpLoc, diag::ext_sizeof_function_type, ExprRange);
Chris Lattner4b009652007-07-25 00:24:17 +0000597 else if (exprType->isVoidType())
Chris Lattnerf814d882008-07-25 21:45:37 +0000598 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof",
599 ExprRange);
Chris Lattner4b009652007-07-25 00:24:17 +0000600 else if (exprType->isIncompleteType()) {
601 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
602 diag::err_alignof_incomplete_type,
Chris Lattnerf814d882008-07-25 21:45:37 +0000603 exprType.getAsString(), ExprRange);
Chris Lattner4b009652007-07-25 00:24:17 +0000604 return QualType(); // error
605 }
606 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
607 return Context.getSizeType();
608}
609
610Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000611ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000612 SourceLocation LPLoc, TypeTy *Ty,
613 SourceLocation RPLoc) {
614 // If error parsing type, ignore.
615 if (Ty == 0) return true;
616
617 // Verify that this is a valid expression.
618 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
619
Chris Lattnerf814d882008-07-25 21:45:37 +0000620 QualType resultType =
621 CheckSizeOfAlignOfOperand(ArgTy, OpLoc, SourceRange(LPLoc, RPLoc),isSizeof);
Chris Lattner4b009652007-07-25 00:24:17 +0000622
623 if (resultType.isNull())
624 return true;
625 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
626}
627
Chris Lattner5110ad52007-08-24 21:41:10 +0000628QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000629 DefaultFunctionArrayConversion(V);
630
Chris Lattnera16e42d2007-08-26 05:39:26 +0000631 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000632 if (const ComplexType *CT = V->getType()->getAsComplexType())
633 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000634
635 // Otherwise they pass through real integer and floating point types here.
636 if (V->getType()->isArithmeticType())
637 return V->getType();
638
639 // Reject anything else.
640 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
641 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000642}
643
644
Chris Lattner4b009652007-07-25 00:24:17 +0000645
Steve Naroff87d58b42007-09-16 03:34:24 +0000646Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000647 tok::TokenKind Kind,
648 ExprTy *Input) {
649 UnaryOperator::Opcode Opc;
650 switch (Kind) {
651 default: assert(0 && "Unknown unary op!");
652 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
653 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
654 }
655 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
656 if (result.isNull())
657 return true;
658 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
659}
660
661Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000662ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000663 ExprTy *Idx, SourceLocation RLoc) {
664 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
665
666 // Perform default conversions.
667 DefaultFunctionArrayConversion(LHSExp);
668 DefaultFunctionArrayConversion(RHSExp);
669
670 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
671
672 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000673 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000674 // in the subscript position. As a result, we need to derive the array base
675 // and index from the expression types.
676 Expr *BaseExpr, *IndexExpr;
677 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000678 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000679 BaseExpr = LHSExp;
680 IndexExpr = RHSExp;
681 // FIXME: need to deal with const...
682 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000683 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000684 // Handle the uncommon case of "123[Ptr]".
685 BaseExpr = RHSExp;
686 IndexExpr = LHSExp;
687 // FIXME: need to deal with const...
688 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000689 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
690 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000691 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000692
693 // Component access limited to variables (reject vec4.rg[1]).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000694 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
695 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000696 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000697 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000698 // FIXME: need to deal with const...
699 ResultType = VTy->getElementType();
700 } else {
701 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
702 RHSExp->getSourceRange());
703 }
704 // C99 6.5.2.1p1
705 if (!IndexExpr->getType()->isIntegerType())
706 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
707 IndexExpr->getSourceRange());
708
709 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
710 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattner9db553e2008-04-02 06:59:01 +0000711 // void (*)(int)) and pointers to incomplete types. Functions are not
712 // objects in C99.
Chris Lattner4b009652007-07-25 00:24:17 +0000713 if (!ResultType->isObjectType())
714 return Diag(BaseExpr->getLocStart(),
715 diag::err_typecheck_subscript_not_object,
716 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
717
718 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
719}
720
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000721QualType Sema::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000722CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000723 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000724 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begemanc8e51f82008-05-09 06:41:27 +0000725
726 // This flag determines whether or not the component is to be treated as a
727 // special name, or a regular GLSL-style component access.
728 bool SpecialComponent = false;
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000729
730 // The vector accessor can't exceed the number of elements.
731 const char *compStr = CompName.getName();
732 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000733 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000734 baseType.getAsString(), SourceRange(CompLoc));
735 return QualType();
736 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000737
738 // Check that we've found one of the special components, or that the component
739 // names must come from the same set.
740 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
741 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
742 SpecialComponent = true;
743 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner9096b792007-08-02 22:33:49 +0000744 do
745 compStr++;
746 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
747 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
748 do
749 compStr++;
750 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
751 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
752 do
753 compStr++;
754 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
755 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000756
Nate Begemanc8e51f82008-05-09 06:41:27 +0000757 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000758 // We didn't get to the end of the string. This means the component names
759 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000760 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000761 std::string(compStr,compStr+1), SourceRange(CompLoc));
762 return QualType();
763 }
764 // Each component accessor can't exceed the vector type.
765 compStr = CompName.getName();
766 while (*compStr) {
767 if (vecType->isAccessorWithinNumElements(*compStr))
768 compStr++;
769 else
770 break;
771 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000772 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000773 // We didn't get to the end of the string. This means a component accessor
774 // exceeds the number of elements in the vector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000775 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000776 baseType.getAsString(), SourceRange(CompLoc));
777 return QualType();
778 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000779
780 // If we have a special component name, verify that the current vector length
781 // is an even number, since all special component names return exactly half
782 // the elements.
783 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
784 return QualType();
785 }
786
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000787 // The component accessor looks fine - now we need to compute the actual type.
788 // The vector type is implied by the component accessor. For example,
789 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begemanc8e51f82008-05-09 06:41:27 +0000790 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
791 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
792 : strlen(CompName.getName());
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000793 if (CompSize == 1)
794 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000795
Nate Begemanaf6ed502008-04-18 23:10:10 +0000796 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroff82113e32007-07-29 16:33:31 +0000797 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemanaf6ed502008-04-18 23:10:10 +0000798 // diagostics look bad. We want extended vector types to appear built-in.
799 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
800 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
801 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroff82113e32007-07-29 16:33:31 +0000802 }
803 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000804}
805
Chris Lattner4b009652007-07-25 00:24:17 +0000806Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000807ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000808 tok::TokenKind OpKind, SourceLocation MemberLoc,
809 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000810 Expr *BaseExpr = static_cast<Expr *>(Base);
811 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000812
813 // Perform default conversions.
814 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000815
Steve Naroff2cb66382007-07-26 03:11:44 +0000816 QualType BaseType = BaseExpr->getType();
817 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000818
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000819 // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr
820 // must have pointer type, and the accessed type is the pointee.
Chris Lattner4b009652007-07-25 00:24:17 +0000821 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000822 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000823 BaseType = PT->getPointeeType();
824 else
Chris Lattner7d5a8762008-07-21 05:35:34 +0000825 return Diag(MemberLoc, diag::err_typecheck_member_reference_arrow,
826 BaseType.getAsString(), BaseExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000827 }
Chris Lattnera57cf472008-07-21 04:28:12 +0000828
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000829 // Handle field access to simple records. This also handles access to fields
830 // of the ObjC 'id' struct.
Chris Lattnere35a1042007-07-31 19:29:30 +0000831 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000832 RecordDecl *RDecl = RTy->getDecl();
833 if (RTy->isIncompleteType())
834 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
835 BaseExpr->getSourceRange());
836 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000837 FieldDecl *MemberDecl = RDecl->getMember(&Member);
838 if (!MemberDecl)
Chris Lattner7d5a8762008-07-21 05:35:34 +0000839 return Diag(MemberLoc, diag::err_typecheck_no_member, Member.getName(),
840 BaseExpr->getSourceRange());
Eli Friedman76b49832008-02-06 22:48:16 +0000841
842 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000843 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000844 QualType MemberType = MemberDecl->getType();
845 unsigned combinedQualifiers =
Chris Lattner35fef522008-02-20 20:55:12 +0000846 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman76b49832008-02-06 22:48:16 +0000847 MemberType = MemberType.getQualifiedType(combinedQualifiers);
848
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000849 return new MemberExpr(BaseExpr, OpKind == tok::arrow, MemberDecl,
Eli Friedman76b49832008-02-06 22:48:16 +0000850 MemberLoc, MemberType);
Chris Lattnera57cf472008-07-21 04:28:12 +0000851 }
852
Chris Lattnere9d71612008-07-21 04:59:05 +0000853 // Handle access to Objective-C instance variables, such as "Obj->ivar" and
854 // (*Obj).ivar.
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000855 if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) {
856 if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000857 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
Chris Lattnera57cf472008-07-21 04:28:12 +0000858 OpKind == tok::arrow);
Chris Lattner7d5a8762008-07-21 05:35:34 +0000859 return Diag(MemberLoc, diag::err_typecheck_member_reference_ivar,
Chris Lattner52292be2008-07-21 04:42:08 +0000860 IFTy->getDecl()->getName(), Member.getName(),
Chris Lattner7d5a8762008-07-21 05:35:34 +0000861 BaseExpr->getSourceRange());
Chris Lattnera57cf472008-07-21 04:28:12 +0000862 }
863
Chris Lattnere9d71612008-07-21 04:59:05 +0000864 // Handle Objective-C property access, which is "Obj.property" where Obj is a
865 // pointer to a (potentially qualified) interface type.
866 const PointerType *PTy;
867 const ObjCInterfaceType *IFTy;
868 if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) &&
869 (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) {
870 ObjCInterfaceDecl *IFace = IFTy->getDecl();
871
Chris Lattner55a24332008-07-21 06:44:27 +0000872 // FIXME: The logic for looking up nullary and unary selectors should be
873 // shared with the code in ActOnInstanceMessage.
874
Chris Lattnere9d71612008-07-21 04:59:05 +0000875 // Before we look for explicit property declarations, we check for
876 // nullary methods (which allow '.' notation).
877 Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
Chris Lattnere9d71612008-07-21 04:59:05 +0000878 if (ObjCMethodDecl *MD = IFace->lookupInstanceMethod(Sel))
879 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
880 MemberLoc, BaseExpr);
881
Chris Lattner55a24332008-07-21 06:44:27 +0000882 // If this reference is in an @implementation, check for 'private' methods.
883 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
884 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
885 if (ObjCImplementationDecl *ImpDecl =
886 ObjCImplementations[ClassDecl->getIdentifier()])
887 if (ObjCMethodDecl *MD = ImpDecl->getInstanceMethod(Sel))
888 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
889 MemberLoc, BaseExpr);
890 }
891
Chris Lattnere9d71612008-07-21 04:59:05 +0000892 // FIXME: Need to deal with setter methods that take 1 argument. E.g.:
893 // @interface NSBundle : NSObject {}
894 // - (NSString *)bundlePath;
895 // - (void)setBundlePath:(NSString *)x;
896 // @end
897 // void someMethod() { frameworkBundle.bundlePath = 0; }
898 //
899 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member))
900 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
901
902 // Lastly, check protocols on qualified interfaces.
Chris Lattnerd5f81792008-07-21 05:20:01 +0000903 for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(),
904 E = IFTy->qual_end(); I != E; ++I)
905 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member))
906 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000907 }
Chris Lattnera57cf472008-07-21 04:28:12 +0000908
909 // Handle 'field access' to vectors, such as 'V.xx'.
910 if (BaseType->isExtVectorType() && OpKind == tok::period) {
911 // Component access limited to variables (reject vec4.rg.g).
912 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
913 !isa<ExtVectorElementExpr>(BaseExpr))
Chris Lattner7d5a8762008-07-21 05:35:34 +0000914 return Diag(MemberLoc, diag::err_ext_vector_component_access,
915 BaseExpr->getSourceRange());
Chris Lattnera57cf472008-07-21 04:28:12 +0000916 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
917 if (ret.isNull())
918 return true;
919 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
920 }
921
Chris Lattner7d5a8762008-07-21 05:35:34 +0000922 return Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union,
923 BaseType.getAsString(), BaseExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000924}
925
Steve Naroff87d58b42007-09-16 03:34:24 +0000926/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000927/// This provides the location of the left/right parens and a list of comma
928/// locations.
929Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000930ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000931 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +0000932 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
933 Expr *Fn = static_cast<Expr *>(fn);
934 Expr **Args = reinterpret_cast<Expr**>(args);
935 assert(Fn && "no function call expression");
Chris Lattner3e254fb2008-04-08 04:40:51 +0000936 FunctionDecl *FDecl = NULL;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000937
938 // Promote the function operand.
939 UsualUnaryConversions(Fn);
940
941 // If we're directly calling a function, get the declaration for
942 // that function.
943 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
944 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
945 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
946
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000947 // Make the call expr early, before semantic checks. This guarantees cleanup
948 // of arguments and function on error.
Chris Lattner97316c02008-04-10 02:22:51 +0000949 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000950 Context.BoolTy, RParenLoc));
951
Chris Lattner4b009652007-07-25 00:24:17 +0000952 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
953 // type pointer to function".
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000954 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000955 if (PT == 0)
956 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
957 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000958 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
959 if (FuncT == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000960 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
961 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000962
963 // We know the result type of the call, set it.
964 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +0000965
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000966 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000967 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
968 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000969 unsigned NumArgsInProto = Proto->getNumArgs();
970 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +0000971
Chris Lattner3e254fb2008-04-08 04:40:51 +0000972 // If too few arguments are available (and we don't have default
973 // arguments for the remaining parameters), don't make the call.
974 if (NumArgs < NumArgsInProto) {
Chris Lattner97316c02008-04-10 02:22:51 +0000975 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000976 // Use default arguments for missing arguments
977 NumArgsToCheck = NumArgsInProto;
Chris Lattner97316c02008-04-10 02:22:51 +0000978 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000979 } else
980 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
981 Fn->getSourceRange());
982 }
983
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000984 // If too many are passed and not variadic, error on the extras and drop
985 // them.
986 if (NumArgs > NumArgsInProto) {
987 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000988 Diag(Args[NumArgsInProto]->getLocStart(),
989 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
990 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000991 Args[NumArgs-1]->getLocEnd()));
992 // This deletes the extra arguments.
993 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +0000994 }
995 NumArgsToCheck = NumArgsInProto;
996 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000997
Chris Lattner4b009652007-07-25 00:24:17 +0000998 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000999 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner005ed752008-01-04 18:04:52 +00001000 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001001
1002 Expr *Arg;
1003 if (i < NumArgs)
1004 Arg = Args[i];
1005 else
1006 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner005ed752008-01-04 18:04:52 +00001007 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001008
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001009 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +00001010 AssignConvertType ConvTy =
1011 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001012 TheCall->setArg(i, Arg);
1013
Chris Lattner005ed752008-01-04 18:04:52 +00001014 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
1015 ArgType, Arg, "passing"))
1016 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001017 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001018
1019 // If this is a variadic call, handle args passed through "...".
1020 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +00001021 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001022 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
1023 Expr *Arg = Args[i];
1024 DefaultArgumentPromotion(Arg);
1025 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +00001026 }
Steve Naroffdb65e052007-08-28 23:30:39 +00001027 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001028 } else {
1029 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
1030
Steve Naroffdb65e052007-08-28 23:30:39 +00001031 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001032 for (unsigned i = 0; i != NumArgs; i++) {
1033 Expr *Arg = Args[i];
1034 DefaultArgumentPromotion(Arg);
1035 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +00001036 }
Chris Lattner4b009652007-07-25 00:24:17 +00001037 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001038
Chris Lattner2e64c072007-08-10 20:18:51 +00001039 // Do special checking on direct calls to functions.
Eli Friedmand0e9d092008-05-14 19:38:39 +00001040 if (FDecl)
1041 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattner2e64c072007-08-10 20:18:51 +00001042
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001043 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +00001044}
1045
1046Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001047ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +00001048 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +00001049 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001050 QualType literalType = QualType::getFromOpaquePtr(Ty);
1051 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +00001052 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +00001053 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +00001054
Eli Friedman8c2173d2008-05-20 05:22:08 +00001055 if (literalType->isArrayType()) {
1056 if (literalType->getAsVariableArrayType())
1057 return Diag(LParenLoc,
1058 diag::err_variable_object_no_init,
1059 SourceRange(LParenLoc,
1060 literalExpr->getSourceRange().getEnd()));
1061 } else if (literalType->isIncompleteType()) {
1062 return Diag(LParenLoc,
1063 diag::err_typecheck_decl_incomplete_type,
1064 literalType.getAsString(),
1065 SourceRange(LParenLoc,
1066 literalExpr->getSourceRange().getEnd()));
1067 }
1068
Steve Narofff0b23542008-01-10 22:15:12 +00001069 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +00001070 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +00001071
Argiris Kirtzidis95256e62008-06-28 06:07:14 +00001072 bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl();
Steve Naroffbe37fc02008-01-14 18:19:28 +00001073 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +00001074 if (CheckForConstantInitializer(literalExpr, literalType))
1075 return true;
1076 }
Steve Naroffbe37fc02008-01-14 18:19:28 +00001077 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001078}
1079
1080Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001081ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +00001082 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +00001083 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +00001084
Steve Naroff0acc9c92007-09-15 18:49:24 +00001085 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +00001086 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +00001087
Chris Lattner48d7f382008-04-02 04:24:33 +00001088 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
1089 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
1090 return E;
Chris Lattner4b009652007-07-25 00:24:17 +00001091}
1092
Chris Lattnerd1f26b32007-12-20 00:44:32 +00001093bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001094 assert(VectorTy->isVectorType() && "Not a vector type!");
1095
1096 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001097 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001098 return Diag(R.getBegin(),
1099 Ty->isVectorType() ?
1100 diag::err_invalid_conversion_between_vectors :
1101 diag::err_invalid_conversion_between_vector_and_integer,
1102 VectorTy.getAsString().c_str(),
1103 Ty.getAsString().c_str(), R);
1104 } else
1105 return Diag(R.getBegin(),
1106 diag::err_invalid_conversion_between_vector_and_scalar,
1107 VectorTy.getAsString().c_str(),
1108 Ty.getAsString().c_str(), R);
1109
1110 return false;
1111}
1112
Chris Lattner4b009652007-07-25 00:24:17 +00001113Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001114ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +00001115 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +00001116 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +00001117
1118 Expr *castExpr = static_cast<Expr*>(Op);
1119 QualType castType = QualType::getFromOpaquePtr(Ty);
1120
Steve Naroff68adb482007-08-31 00:32:44 +00001121 UsualUnaryConversions(castExpr);
1122
Chris Lattner4b009652007-07-25 00:24:17 +00001123 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
1124 // type needs to be scalar.
Chris Lattnerdb526732007-10-29 04:26:44 +00001125 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff5ad85292008-06-03 12:56:35 +00001126 if (!castType->isScalarType() && !castType->isVectorType()) {
1127 // GCC struct/union extension.
1128 if (castType == castExpr->getType() &&
Steve Naroff7f1c5b52008-06-03 13:21:30 +00001129 castType->isStructureType() || castType->isUnionType()) {
1130 Diag(LParenLoc, diag::ext_typecheck_cast_nonscalar,
1131 SourceRange(LParenLoc, RParenLoc));
1132 return new CastExpr(castType, castExpr, LParenLoc);
1133 } else
Steve Naroff5ad85292008-06-03 12:56:35 +00001134 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
1135 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
1136 }
Steve Narofff459ee52008-01-24 22:55:05 +00001137 if (!castExpr->getType()->isScalarType() &&
1138 !castExpr->getType()->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +00001139 return Diag(castExpr->getLocStart(),
1140 diag::err_typecheck_expect_scalar_operand,
1141 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001142
1143 if (castExpr->getType()->isVectorType()) {
1144 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
1145 castExpr->getType(), castType))
1146 return true;
1147 } else if (castType->isVectorType()) {
1148 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
1149 castType, castExpr->getType()))
1150 return true;
Chris Lattnerdb526732007-10-29 04:26:44 +00001151 }
Chris Lattner4b009652007-07-25 00:24:17 +00001152 }
1153 return new CastExpr(castType, castExpr, LParenLoc);
1154}
1155
Chris Lattner98a425c2007-11-26 01:40:58 +00001156/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
1157/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +00001158inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
1159 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
1160 UsualUnaryConversions(cond);
1161 UsualUnaryConversions(lex);
1162 UsualUnaryConversions(rex);
1163 QualType condT = cond->getType();
1164 QualType lexT = lex->getType();
1165 QualType rexT = rex->getType();
1166
1167 // first, check the condition.
1168 if (!condT->isScalarType()) { // C99 6.5.15p2
1169 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
1170 condT.getAsString());
1171 return QualType();
1172 }
Chris Lattner992ae932008-01-06 22:42:25 +00001173
1174 // Now check the two expressions.
1175
1176 // If both operands have arithmetic type, do the usual arithmetic conversions
1177 // to find a common type: C99 6.5.15p3,5.
1178 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001179 UsualArithmeticConversions(lex, rex);
1180 return lex->getType();
1181 }
Chris Lattner992ae932008-01-06 22:42:25 +00001182
1183 // If both operands are the same structure or union type, the result is that
1184 // type.
Chris Lattner71225142007-07-31 21:27:01 +00001185 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +00001186 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +00001187 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +00001188 // "If both the operands have structure or union type, the result has
1189 // that type." This implies that CV qualifiers are dropped.
1190 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001191 }
Chris Lattner992ae932008-01-06 22:42:25 +00001192
1193 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroff95cb3892008-05-12 21:44:38 +00001194 // The following || allows only one side to be void (a GCC-ism).
1195 if (lexT->isVoidType() || rexT->isVoidType()) {
Eli Friedmanf025aac2008-06-04 19:47:51 +00001196 if (!lexT->isVoidType())
Steve Naroff95cb3892008-05-12 21:44:38 +00001197 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
1198 rex->getSourceRange());
1199 if (!rexT->isVoidType())
1200 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
Nuno Lopes4ba41fd2008-06-04 19:14:12 +00001201 lex->getSourceRange());
Eli Friedmanf025aac2008-06-04 19:47:51 +00001202 ImpCastExprToType(lex, Context.VoidTy);
1203 ImpCastExprToType(rex, Context.VoidTy);
1204 return Context.VoidTy;
Steve Naroff95cb3892008-05-12 21:44:38 +00001205 }
Steve Naroff12ebf272008-01-08 01:11:38 +00001206 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
1207 // the type of the other operand."
1208 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001209 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +00001210 return lexT;
1211 }
1212 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001213 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +00001214 return rexT;
1215 }
Chris Lattner0ac51632008-01-06 22:50:31 +00001216 // Handle the case where both operands are pointers before we handle null
1217 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +00001218 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
1219 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
1220 // get the "pointed to" types
1221 QualType lhptee = LHSPT->getPointeeType();
1222 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001223
Chris Lattner71225142007-07-31 21:27:01 +00001224 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
1225 if (lhptee->isVoidType() &&
Chris Lattner9db553e2008-04-02 06:59:01 +00001226 rhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +00001227 // Figure out necessary qualifiers (C99 6.5.15p6)
1228 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +00001229 QualType destType = Context.getPointerType(destPointee);
1230 ImpCastExprToType(lex, destType); // add qualifiers if necessary
1231 ImpCastExprToType(rex, destType); // promote to void*
1232 return destType;
1233 }
Chris Lattner9db553e2008-04-02 06:59:01 +00001234 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +00001235 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +00001236 QualType destType = Context.getPointerType(destPointee);
1237 ImpCastExprToType(lex, destType); // add qualifiers if necessary
1238 ImpCastExprToType(rex, destType); // promote to void*
1239 return destType;
1240 }
Chris Lattner4b009652007-07-25 00:24:17 +00001241
Steve Naroff85f0dc52007-10-15 20:41:53 +00001242 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1243 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +00001244 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +00001245 lexT.getAsString(), rexT.getAsString(),
1246 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman33284862008-01-30 17:02:03 +00001247 // In this situation, we assume void* type. No especially good
1248 // reason, but this is what gcc does, and we do have to pick
1249 // to get a consistent AST.
1250 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
1251 ImpCastExprToType(lex, voidPtrTy);
1252 ImpCastExprToType(rex, voidPtrTy);
1253 return voidPtrTy;
Chris Lattner71225142007-07-31 21:27:01 +00001254 }
1255 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001256 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
1257 // differently qualified versions of compatible types, the result type is
1258 // a pointer to an appropriately qualified version of the *composite*
1259 // type.
Eli Friedmane38150e2008-05-16 20:37:07 +00001260 // FIXME: Need to calculate the composite type.
Eli Friedmanca07c902008-02-10 22:59:36 +00001261 // FIXME: Need to add qualifiers
Eli Friedmane38150e2008-05-16 20:37:07 +00001262 QualType compositeType = lexT;
1263 ImpCastExprToType(lex, compositeType);
1264 ImpCastExprToType(rex, compositeType);
1265 return compositeType;
Chris Lattner4b009652007-07-25 00:24:17 +00001266 }
Chris Lattner4b009652007-07-25 00:24:17 +00001267 }
Steve Naroff605896f2008-05-31 22:33:45 +00001268 // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type
1269 // evaluates to "struct objc_object *" (and is handled above when comparing
1270 // id with statically typed objects). FIXME: Do we need an ImpCastExprToType?
1271 if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) {
1272 if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true))
1273 return Context.getObjCIdType();
1274 }
Chris Lattner992ae932008-01-06 22:42:25 +00001275 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +00001276 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
1277 lexT.getAsString(), rexT.getAsString(),
1278 lex->getSourceRange(), rex->getSourceRange());
1279 return QualType();
1280}
1281
Steve Naroff87d58b42007-09-16 03:34:24 +00001282/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +00001283/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +00001284Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001285 SourceLocation ColonLoc,
1286 ExprTy *Cond, ExprTy *LHS,
1287 ExprTy *RHS) {
1288 Expr *CondExpr = (Expr *) Cond;
1289 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +00001290
1291 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
1292 // was the condition.
1293 bool isLHSNull = LHSExpr == 0;
1294 if (isLHSNull)
1295 LHSExpr = CondExpr;
1296
Chris Lattner4b009652007-07-25 00:24:17 +00001297 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
1298 RHSExpr, QuestionLoc);
1299 if (result.isNull())
1300 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +00001301 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
1302 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +00001303}
1304
Chris Lattner4b009652007-07-25 00:24:17 +00001305
1306// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1307// being closely modeled after the C99 spec:-). The odd characteristic of this
1308// routine is it effectively iqnores the qualifiers on the top level pointee.
1309// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1310// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001311Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001312Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1313 QualType lhptee, rhptee;
1314
1315 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001316 lhptee = lhsType->getAsPointerType()->getPointeeType();
1317 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001318
1319 // make sure we operate on the canonical type
1320 lhptee = lhptee.getCanonicalType();
1321 rhptee = rhptee.getCanonicalType();
1322
Chris Lattner005ed752008-01-04 18:04:52 +00001323 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001324
1325 // C99 6.5.16.1p1: This following citation is common to constraints
1326 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1327 // qualifiers of the type *pointed to* by the right;
Chris Lattner35fef522008-02-20 20:55:12 +00001328 // FIXME: Handle ASQualType
1329 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1330 rhptee.getCVRQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001331 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001332
1333 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1334 // incomplete type and the other is a pointer to a qualified or unqualified
1335 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001336 if (lhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001337 if (rhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001338 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001339
1340 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001341 assert(rhptee->isFunctionType());
1342 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001343 }
1344
1345 if (rhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001346 if (lhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001347 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001348
1349 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001350 assert(lhptee->isFunctionType());
1351 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001352 }
1353
Chris Lattner4b009652007-07-25 00:24:17 +00001354 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1355 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001356 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1357 rhptee.getUnqualifiedType()))
1358 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001359 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001360}
1361
1362/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1363/// has code to accommodate several GCC extensions when type checking
1364/// pointers. Here are some objectionable examples that GCC considers warnings:
1365///
1366/// int a, *pint;
1367/// short *pshort;
1368/// struct foo *pfoo;
1369///
1370/// pint = pshort; // warning: assignment from incompatible pointer type
1371/// a = pint; // warning: assignment makes integer from pointer without a cast
1372/// pint = a; // warning: assignment makes pointer from integer without a cast
1373/// pint = pfoo; // warning: assignment from incompatible pointer type
1374///
1375/// As a result, the code for dealing with pointers is more complex than the
1376/// C99 spec dictates.
Chris Lattner4b009652007-07-25 00:24:17 +00001377///
Chris Lattner005ed752008-01-04 18:04:52 +00001378Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001379Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001380 // Get canonical types. We're not formatting these types, just comparing
1381 // them.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001382 lhsType = lhsType.getCanonicalType().getUnqualifiedType();
1383 rhsType = rhsType.getCanonicalType().getUnqualifiedType();
1384
1385 if (lhsType == rhsType)
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001386 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001387
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001388 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattnere1577e22008-04-07 06:52:53 +00001389 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001390 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001391 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001392 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001393
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001394 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1395 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001396 return Compatible;
Steve Naroff936c4362008-06-03 14:04:54 +00001397 // Relax integer conversions like we do for pointers below.
1398 if (rhsType->isIntegerType())
1399 return IntToPointer;
1400 if (lhsType->isIntegerType())
1401 return PointerToInt;
Chris Lattner1853da22008-01-04 23:18:45 +00001402 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001403 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001404
Nate Begemanc5f0f652008-07-14 18:02:46 +00001405 if (lhsType->isVectorType() || rhsType->isVectorType()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001406 // For ExtVector, allow vector splats; float -> <n x float>
Nate Begemanc5f0f652008-07-14 18:02:46 +00001407 if (const ExtVectorType *LV = lhsType->getAsExtVectorType())
1408 if (LV->getElementType() == rhsType)
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001409 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001410
Nate Begemanc5f0f652008-07-14 18:02:46 +00001411 // If we are allowing lax vector conversions, and LHS and RHS are both
1412 // vectors, the total size only needs to be the same. This is a bitcast;
1413 // no bits are changed but the result type is different.
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001414 if (getLangOptions().LaxVectorConversions &&
1415 lhsType->isVectorType() && rhsType->isVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001416 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
1417 return Compatible;
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001418 }
1419 return Incompatible;
1420 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001421
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001422 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001423 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001424
Chris Lattner390564e2008-04-07 06:49:41 +00001425 if (isa<PointerType>(lhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001426 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001427 return IntToPointer;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001428
Chris Lattner390564e2008-04-07 06:49:41 +00001429 if (isa<PointerType>(rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001430 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001431 return Incompatible;
1432 }
1433
Chris Lattner390564e2008-04-07 06:49:41 +00001434 if (isa<PointerType>(rhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001435 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001436 if (lhsType == Context.BoolTy)
1437 return Compatible;
1438
1439 if (lhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001440 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001441
Chris Lattner390564e2008-04-07 06:49:41 +00001442 if (isa<PointerType>(lhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001443 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001444 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001445 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001446
Chris Lattner1853da22008-01-04 23:18:45 +00001447 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner390564e2008-04-07 06:49:41 +00001448 if (Context.typesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001449 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001450 }
1451 return Incompatible;
1452}
1453
Chris Lattner005ed752008-01-04 18:04:52 +00001454Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001455Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001456 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1457 // a null pointer constant.
Ted Kremenek42730c52008-01-07 19:49:32 +00001458 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001459 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001460 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001461 return Compatible;
1462 }
Chris Lattner5f505bf2007-10-16 02:55:40 +00001463 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001464 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001465 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001466 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001467 //
1468 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1469 // are better understood.
1470 if (!lhsType->isReferenceType())
1471 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001472
Chris Lattner005ed752008-01-04 18:04:52 +00001473 Sema::AssignConvertType result =
1474 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001475
1476 // C99 6.5.16.1p2: The value of the right operand is converted to the
1477 // type of the assignment expression.
1478 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001479 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001480 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001481}
1482
Chris Lattner005ed752008-01-04 18:04:52 +00001483Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001484Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1485 return CheckAssignmentConstraints(lhsType, rhsType);
1486}
1487
Chris Lattner2c8bff72007-12-12 05:47:28 +00001488QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001489 Diag(loc, diag::err_typecheck_invalid_operands,
1490 lex->getType().getAsString(), rex->getType().getAsString(),
1491 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001492 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001493}
1494
1495inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1496 Expr *&rex) {
Nate Begeman03105572008-04-04 01:30:25 +00001497 // For conversion purposes, we ignore any qualifiers.
1498 // For example, "const float" and "float" are equivalent.
1499 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1500 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001501
Nate Begemanc5f0f652008-07-14 18:02:46 +00001502 // If the vector types are identical, return.
Nate Begeman03105572008-04-04 01:30:25 +00001503 if (lhsType == rhsType)
Chris Lattner4b009652007-07-25 00:24:17 +00001504 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001505
Nate Begemanc5f0f652008-07-14 18:02:46 +00001506 // Handle the case of a vector & extvector type of the same size and element
1507 // type. It would be nice if we only had one vector type someday.
1508 if (getLangOptions().LaxVectorConversions)
1509 if (const VectorType *LV = lhsType->getAsVectorType())
1510 if (const VectorType *RV = rhsType->getAsVectorType())
1511 if (LV->getElementType() == RV->getElementType() &&
1512 LV->getNumElements() == RV->getNumElements())
1513 return lhsType->isExtVectorType() ? lhsType : rhsType;
1514
1515 // If the lhs is an extended vector and the rhs is a scalar of the same type
1516 // or a literal, promote the rhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001517 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001518 QualType eltType = V->getElementType();
1519
1520 if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) ||
1521 (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) ||
1522 (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001523 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001524 return lhsType;
1525 }
1526 }
1527
Nate Begemanc5f0f652008-07-14 18:02:46 +00001528 // If the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001529 // promote the lhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001530 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001531 QualType eltType = V->getElementType();
1532
1533 if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) ||
1534 (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) ||
1535 (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001536 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001537 return rhsType;
1538 }
1539 }
1540
Chris Lattner4b009652007-07-25 00:24:17 +00001541 // You cannot convert between vector values of different size.
1542 Diag(loc, diag::err_typecheck_vector_not_convertable,
1543 lex->getType().getAsString(), rex->getType().getAsString(),
1544 lex->getSourceRange(), rex->getSourceRange());
1545 return QualType();
1546}
1547
1548inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001549 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001550{
1551 QualType lhsType = lex->getType(), rhsType = rex->getType();
1552
1553 if (lhsType->isVectorType() || rhsType->isVectorType())
1554 return CheckVectorOperands(loc, lex, rex);
1555
Steve Naroff8f708362007-08-24 19:07:16 +00001556 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001557
Chris Lattner4b009652007-07-25 00:24:17 +00001558 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001559 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001560 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001561}
1562
1563inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001564 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001565{
1566 QualType lhsType = lex->getType(), rhsType = rex->getType();
1567
Steve Naroff8f708362007-08-24 19:07:16 +00001568 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001569
Chris Lattner4b009652007-07-25 00:24:17 +00001570 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001571 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001572 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001573}
1574
1575inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001576 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001577{
1578 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1579 return CheckVectorOperands(loc, lex, rex);
1580
Steve Naroff8f708362007-08-24 19:07:16 +00001581 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001582
Chris Lattner4b009652007-07-25 00:24:17 +00001583 // handle the common case first (both operands are arithmetic).
1584 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001585 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001586
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001587 // Put any potential pointer into PExp
1588 Expr* PExp = lex, *IExp = rex;
1589 if (IExp->getType()->isPointerType())
1590 std::swap(PExp, IExp);
1591
1592 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1593 if (IExp->getType()->isIntegerType()) {
1594 // Check for arithmetic on pointers to incomplete types
1595 if (!PTy->getPointeeType()->isObjectType()) {
1596 if (PTy->getPointeeType()->isVoidType()) {
1597 Diag(loc, diag::ext_gnu_void_ptr,
1598 lex->getSourceRange(), rex->getSourceRange());
1599 } else {
1600 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1601 lex->getType().getAsString(), lex->getSourceRange());
1602 return QualType();
1603 }
1604 }
1605 return PExp->getType();
1606 }
1607 }
1608
Chris Lattner2c8bff72007-12-12 05:47:28 +00001609 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001610}
1611
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001612// C99 6.5.6
1613QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1614 SourceLocation loc, bool isCompAssign) {
Chris Lattner4b009652007-07-25 00:24:17 +00001615 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1616 return CheckVectorOperands(loc, lex, rex);
1617
Steve Naroff8f708362007-08-24 19:07:16 +00001618 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001619
Chris Lattnerf6da2912007-12-09 21:53:25 +00001620 // Enforce type constraints: C99 6.5.6p3.
1621
1622 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001623 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001624 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001625
1626 // Either ptr - int or ptr - ptr.
1627 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001628 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001629
Chris Lattnerf6da2912007-12-09 21:53:25 +00001630 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001631 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001632 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001633 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001634 Diag(loc, diag::ext_gnu_void_ptr,
1635 lex->getSourceRange(), rex->getSourceRange());
1636 } else {
1637 Diag(loc, diag::err_typecheck_sub_ptr_object,
1638 lex->getType().getAsString(), lex->getSourceRange());
1639 return QualType();
1640 }
1641 }
1642
1643 // The result type of a pointer-int computation is the pointer type.
1644 if (rex->getType()->isIntegerType())
1645 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001646
Chris Lattnerf6da2912007-12-09 21:53:25 +00001647 // Handle pointer-pointer subtractions.
1648 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001649 QualType rpointee = RHSPTy->getPointeeType();
1650
Chris Lattnerf6da2912007-12-09 21:53:25 +00001651 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001652 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001653 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001654 if (rpointee->isVoidType()) {
1655 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-12-09 21:53:25 +00001656 Diag(loc, diag::ext_gnu_void_ptr,
1657 lex->getSourceRange(), rex->getSourceRange());
1658 } else {
1659 Diag(loc, diag::err_typecheck_sub_ptr_object,
1660 rex->getType().getAsString(), rex->getSourceRange());
1661 return QualType();
1662 }
1663 }
1664
1665 // Pointee types must be compatible.
Steve Naroff577f9722008-01-29 18:58:14 +00001666 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1667 rpointee.getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001668 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1669 lex->getType().getAsString(), rex->getType().getAsString(),
1670 lex->getSourceRange(), rex->getSourceRange());
1671 return QualType();
1672 }
1673
1674 return Context.getPointerDiffType();
1675 }
1676 }
1677
Chris Lattner2c8bff72007-12-12 05:47:28 +00001678 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001679}
1680
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001681// C99 6.5.7
1682QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1683 bool isCompAssign) {
Chris Lattner2c8bff72007-12-12 05:47:28 +00001684 // C99 6.5.7p2: Each of the operands shall have integer type.
1685 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1686 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001687
Chris Lattner2c8bff72007-12-12 05:47:28 +00001688 // Shifts don't perform usual arithmetic conversions, they just do integer
1689 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001690 if (!isCompAssign)
1691 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001692 UsualUnaryConversions(rex);
1693
1694 // "The type of the result is that of the promoted left operand."
1695 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001696}
1697
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001698// C99 6.5.8
1699QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1700 bool isRelational) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001701 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1702 return CheckVectorCompareOperands(lex, rex, loc, isRelational);
1703
Chris Lattner254f3bc2007-08-26 01:18:55 +00001704 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001705 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1706 UsualArithmeticConversions(lex, rex);
1707 else {
1708 UsualUnaryConversions(lex);
1709 UsualUnaryConversions(rex);
1710 }
Chris Lattner4b009652007-07-25 00:24:17 +00001711 QualType lType = lex->getType();
1712 QualType rType = rex->getType();
1713
Ted Kremenek486509e2007-10-29 17:13:39 +00001714 // For non-floating point types, check for self-comparisons of the form
1715 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1716 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001717 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001718 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1719 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001720 if (DRL->getDecl() == DRR->getDecl())
1721 Diag(loc, diag::warn_selfcomparison);
1722 }
1723
Chris Lattner254f3bc2007-08-26 01:18:55 +00001724 if (isRelational) {
1725 if (lType->isRealType() && rType->isRealType())
1726 return Context.IntTy;
1727 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001728 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001729 if (lType->isFloatingType()) {
1730 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001731 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001732 }
1733
Chris Lattner254f3bc2007-08-26 01:18:55 +00001734 if (lType->isArithmeticType() && rType->isArithmeticType())
1735 return Context.IntTy;
1736 }
Chris Lattner4b009652007-07-25 00:24:17 +00001737
Chris Lattner22be8422007-08-26 01:10:14 +00001738 bool LHSIsNull = lex->isNullPointerConstant(Context);
1739 bool RHSIsNull = rex->isNullPointerConstant(Context);
1740
Chris Lattner254f3bc2007-08-26 01:18:55 +00001741 // All of the following pointer related warnings are GCC extensions, except
1742 // when handling null pointer constants. One day, we can consider making them
1743 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001744 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001745 QualType LCanPointeeTy =
1746 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1747 QualType RCanPointeeTy =
1748 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman50727042008-02-08 01:19:44 +00001749
Steve Naroff3b435622007-11-13 14:57:38 +00001750 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001751 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1752 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1753 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001754 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1755 lType.getAsString(), rType.getAsString(),
1756 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001757 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001758 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001759 return Context.IntTy;
1760 }
Steve Naroff936c4362008-06-03 14:04:54 +00001761 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) {
1762 if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
1763 ImpCastExprToType(rex, lType);
1764 return Context.IntTy;
1765 }
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001766 }
Steve Naroff936c4362008-06-03 14:04:54 +00001767 if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) &&
1768 rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001769 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001770 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1771 lType.getAsString(), rType.getAsString(),
1772 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001773 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001774 return Context.IntTy;
1775 }
Steve Naroff936c4362008-06-03 14:04:54 +00001776 if (lType->isIntegerType() &&
1777 (rType->isPointerType() || rType->isObjCQualifiedIdType())) {
Chris Lattner22be8422007-08-26 01:10:14 +00001778 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001779 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1780 lType.getAsString(), rType.getAsString(),
1781 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001782 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001783 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001784 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00001785 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001786}
1787
Nate Begemanc5f0f652008-07-14 18:02:46 +00001788/// CheckVectorCompareOperands - vector comparisons are a clang extension that
1789/// operates on extended vector types. Instead of producing an IntTy result,
1790/// like a scalar comparison, a vector comparison produces a vector of integer
1791/// types.
1792QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex,
1793 SourceLocation loc,
1794 bool isRelational) {
1795 // Check to make sure we're operating on vectors of the same type and width,
1796 // Allowing one side to be a scalar of element type.
1797 QualType vType = CheckVectorOperands(loc, lex, rex);
1798 if (vType.isNull())
1799 return vType;
1800
1801 QualType lType = lex->getType();
1802 QualType rType = rex->getType();
1803
1804 // For non-floating point types, check for self-comparisons of the form
1805 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1806 // often indicate logic errors in the program.
1807 if (!lType->isFloatingType()) {
1808 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1809 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
1810 if (DRL->getDecl() == DRR->getDecl())
1811 Diag(loc, diag::warn_selfcomparison);
1812 }
1813
1814 // Check for comparisons of floating point operands using != and ==.
1815 if (!isRelational && lType->isFloatingType()) {
1816 assert (rType->isFloatingType());
1817 CheckFloatComparison(loc,lex,rex);
1818 }
1819
1820 // Return the type for the comparison, which is the same as vector type for
1821 // integer vectors, or an integer type of identical size and number of
1822 // elements for floating point vectors.
1823 if (lType->isIntegerType())
1824 return lType;
1825
1826 const VectorType *VTy = lType->getAsVectorType();
1827
1828 // FIXME: need to deal with non-32b int / non-64b long long
1829 unsigned TypeSize = Context.getTypeSize(VTy->getElementType());
1830 if (TypeSize == 32) {
1831 return Context.getExtVectorType(Context.IntTy, VTy->getNumElements());
1832 }
1833 assert(TypeSize == 64 && "Unhandled vector element size in vector compare");
1834 return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements());
1835}
1836
Chris Lattner4b009652007-07-25 00:24:17 +00001837inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001838 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001839{
1840 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1841 return CheckVectorOperands(loc, lex, rex);
1842
Steve Naroff8f708362007-08-24 19:07:16 +00001843 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001844
1845 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001846 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001847 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001848}
1849
1850inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1851 Expr *&lex, Expr *&rex, SourceLocation loc)
1852{
1853 UsualUnaryConversions(lex);
1854 UsualUnaryConversions(rex);
1855
Eli Friedmanbea3f842008-05-13 20:16:47 +00001856 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Chris Lattner4b009652007-07-25 00:24:17 +00001857 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001858 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001859}
1860
1861inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001862 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001863{
1864 QualType lhsType = lex->getType();
1865 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner4b009652007-07-25 00:24:17 +00001866 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1867
1868 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00001869 case Expr::MLV_Valid:
1870 break;
1871 case Expr::MLV_ConstQualified:
1872 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1873 return QualType();
1874 case Expr::MLV_ArrayType:
1875 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1876 lhsType.getAsString(), lex->getSourceRange());
1877 return QualType();
1878 case Expr::MLV_NotObjectType:
1879 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1880 lhsType.getAsString(), lex->getSourceRange());
1881 return QualType();
1882 case Expr::MLV_InvalidExpression:
1883 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1884 lex->getSourceRange());
1885 return QualType();
1886 case Expr::MLV_IncompleteType:
1887 case Expr::MLV_IncompleteVoidType:
1888 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1889 lhsType.getAsString(), lex->getSourceRange());
1890 return QualType();
1891 case Expr::MLV_DuplicateVectorComponents:
1892 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1893 lex->getSourceRange());
1894 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001895 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001896
Chris Lattner005ed752008-01-04 18:04:52 +00001897 AssignConvertType ConvTy;
1898 if (compoundType.isNull())
1899 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1900 else
1901 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1902
1903 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1904 rex, "assigning"))
1905 return QualType();
1906
Chris Lattner4b009652007-07-25 00:24:17 +00001907 // C99 6.5.16p3: The type of an assignment expression is the type of the
1908 // left operand unless the left operand has qualified type, in which case
1909 // it is the unqualified version of the type of the left operand.
1910 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1911 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001912 // C++ 5.17p1: the type of the assignment expression is that of its left
1913 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00001914 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001915}
1916
1917inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1918 Expr *&lex, Expr *&rex, SourceLocation loc) {
Chris Lattner03c430f2008-07-25 20:54:07 +00001919
1920 // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions.
1921 DefaultFunctionArrayConversion(rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001922 return rex->getType();
1923}
1924
1925/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1926/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1927QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1928 QualType resType = op->getType();
1929 assert(!resType.isNull() && "no type for increment/decrement expression");
1930
Steve Naroffd30e1932007-08-24 17:20:07 +00001931 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00001932 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001933 if (pt->getPointeeType()->isVoidType()) {
1934 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
1935 } else if (!pt->getPointeeType()->isObjectType()) {
1936 // C99 6.5.2.4p2, 6.5.6p2
Chris Lattner4b009652007-07-25 00:24:17 +00001937 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1938 resType.getAsString(), op->getSourceRange());
1939 return QualType();
1940 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001941 } else if (!resType->isRealType()) {
1942 if (resType->isComplexType())
1943 // C99 does not support ++/-- on complex types.
1944 Diag(OpLoc, diag::ext_integer_increment_complex,
1945 resType.getAsString(), op->getSourceRange());
1946 else {
1947 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1948 resType.getAsString(), op->getSourceRange());
1949 return QualType();
1950 }
Chris Lattner4b009652007-07-25 00:24:17 +00001951 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001952 // At this point, we know we have a real, complex or pointer type.
1953 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001954 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1955 if (mlval != Expr::MLV_Valid) {
1956 // FIXME: emit a more precise diagnostic...
1957 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1958 op->getSourceRange());
1959 return QualType();
1960 }
1961 return resType;
1962}
1963
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001964/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00001965/// This routine allows us to typecheck complex/recursive expressions
1966/// where the declaration is needed for type checking. Here are some
1967/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattner48d7f382008-04-02 04:24:33 +00001968static ValueDecl *getPrimaryDecl(Expr *E) {
1969 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001970 case Stmt::DeclRefExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001971 return cast<DeclRefExpr>(E)->getDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001972 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00001973 // Fields cannot be declared with a 'register' storage class.
1974 // &X->f is always ok, even if X is declared register.
Chris Lattner48d7f382008-04-02 04:24:33 +00001975 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnera3249072007-11-16 17:46:48 +00001976 return 0;
Chris Lattner48d7f382008-04-02 04:24:33 +00001977 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001978 case Stmt::ArraySubscriptExprClass: {
1979 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1980
Chris Lattner48d7f382008-04-02 04:24:33 +00001981 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00001982 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001983 return 0;
1984 else
1985 return VD;
1986 }
Chris Lattner4b009652007-07-25 00:24:17 +00001987 case Stmt::UnaryOperatorClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001988 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001989 case Stmt::ParenExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001990 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00001991 case Stmt::ImplicitCastExprClass:
1992 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner48d7f382008-04-02 04:24:33 +00001993 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001994 default:
1995 return 0;
1996 }
1997}
1998
1999/// CheckAddressOfOperand - The operand of & must be either a function
2000/// designator or an lvalue designating an object. If it is an lvalue, the
2001/// object cannot be declared with storage class register or be a bit field.
2002/// Note: The usual conversions are *not* applied to the operand of the &
2003/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
2004QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00002005 if (getLangOptions().C99) {
2006 // Implement C99-only parts of addressof rules.
2007 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
2008 if (uOp->getOpcode() == UnaryOperator::Deref)
2009 // Per C99 6.5.3.2, the address of a deref always returns a valid result
2010 // (assuming the deref expression is valid).
2011 return uOp->getSubExpr()->getType();
2012 }
2013 // Technically, there should be a check for array subscript
2014 // expressions here, but the result of one is always an lvalue anyway.
2015 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00002016 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner4b009652007-07-25 00:24:17 +00002017 Expr::isLvalueResult lval = op->isLvalue();
2018
2019 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00002020 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
2021 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00002022 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
2023 op->getSourceRange());
2024 return QualType();
2025 }
Steve Naroff73cf87e2008-02-29 23:30:25 +00002026 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
2027 if (MemExpr->getMemberDecl()->isBitField()) {
2028 Diag(OpLoc, diag::err_typecheck_address_of,
2029 std::string("bit-field"), op->getSourceRange());
2030 return QualType();
2031 }
2032 // Check for Apple extension for accessing vector components.
2033 } else if (isa<ArraySubscriptExpr>(op) &&
2034 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
2035 Diag(OpLoc, diag::err_typecheck_address_of,
2036 std::string("vector"), op->getSourceRange());
2037 return QualType();
2038 } else if (dcl) { // C99 6.5.3.2p1
Chris Lattner4b009652007-07-25 00:24:17 +00002039 // We have an lvalue with a decl. Make sure the decl is not declared
2040 // with the register storage-class specifier.
2041 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
2042 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff73cf87e2008-02-29 23:30:25 +00002043 Diag(OpLoc, diag::err_typecheck_address_of,
2044 std::string("register variable"), op->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00002045 return QualType();
2046 }
2047 } else
2048 assert(0 && "Unknown/unexpected decl type");
Chris Lattner4b009652007-07-25 00:24:17 +00002049 }
2050 // If the operand has type "type", the result has type "pointer to type".
2051 return Context.getPointerType(op->getType());
2052}
2053
2054QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
2055 UsualUnaryConversions(op);
2056 QualType qType = op->getType();
2057
Chris Lattner7931f4a2007-07-31 16:53:04 +00002058 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00002059 // Note that per both C89 and C99, this is always legal, even
2060 // if ptype is an incomplete type or void.
2061 // It would be possible to warn about dereferencing a
2062 // void pointer, but it's completely well-defined,
2063 // and such a warning is unlikely to catch any mistakes.
2064 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00002065 }
2066 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
2067 qType.getAsString(), op->getSourceRange());
2068 return QualType();
2069}
2070
2071static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
2072 tok::TokenKind Kind) {
2073 BinaryOperator::Opcode Opc;
2074 switch (Kind) {
2075 default: assert(0 && "Unknown binop!");
2076 case tok::star: Opc = BinaryOperator::Mul; break;
2077 case tok::slash: Opc = BinaryOperator::Div; break;
2078 case tok::percent: Opc = BinaryOperator::Rem; break;
2079 case tok::plus: Opc = BinaryOperator::Add; break;
2080 case tok::minus: Opc = BinaryOperator::Sub; break;
2081 case tok::lessless: Opc = BinaryOperator::Shl; break;
2082 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
2083 case tok::lessequal: Opc = BinaryOperator::LE; break;
2084 case tok::less: Opc = BinaryOperator::LT; break;
2085 case tok::greaterequal: Opc = BinaryOperator::GE; break;
2086 case tok::greater: Opc = BinaryOperator::GT; break;
2087 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
2088 case tok::equalequal: Opc = BinaryOperator::EQ; break;
2089 case tok::amp: Opc = BinaryOperator::And; break;
2090 case tok::caret: Opc = BinaryOperator::Xor; break;
2091 case tok::pipe: Opc = BinaryOperator::Or; break;
2092 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
2093 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
2094 case tok::equal: Opc = BinaryOperator::Assign; break;
2095 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
2096 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
2097 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
2098 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
2099 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
2100 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
2101 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
2102 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
2103 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
2104 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
2105 case tok::comma: Opc = BinaryOperator::Comma; break;
2106 }
2107 return Opc;
2108}
2109
2110static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
2111 tok::TokenKind Kind) {
2112 UnaryOperator::Opcode Opc;
2113 switch (Kind) {
2114 default: assert(0 && "Unknown unary op!");
2115 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
2116 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
2117 case tok::amp: Opc = UnaryOperator::AddrOf; break;
2118 case tok::star: Opc = UnaryOperator::Deref; break;
2119 case tok::plus: Opc = UnaryOperator::Plus; break;
2120 case tok::minus: Opc = UnaryOperator::Minus; break;
2121 case tok::tilde: Opc = UnaryOperator::Not; break;
2122 case tok::exclaim: Opc = UnaryOperator::LNot; break;
2123 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
2124 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
2125 case tok::kw___real: Opc = UnaryOperator::Real; break;
2126 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
2127 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
2128 }
2129 return Opc;
2130}
2131
2132// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002133Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00002134 ExprTy *LHS, ExprTy *RHS) {
2135 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
2136 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
2137
Steve Naroff87d58b42007-09-16 03:34:24 +00002138 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
2139 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00002140
2141 QualType ResultTy; // Result type of the binary operator.
2142 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
2143
2144 switch (Opc) {
2145 default:
2146 assert(0 && "Unknown binary expr!");
2147 case BinaryOperator::Assign:
2148 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
2149 break;
2150 case BinaryOperator::Mul:
2151 case BinaryOperator::Div:
2152 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
2153 break;
2154 case BinaryOperator::Rem:
2155 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
2156 break;
2157 case BinaryOperator::Add:
2158 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
2159 break;
2160 case BinaryOperator::Sub:
2161 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
2162 break;
2163 case BinaryOperator::Shl:
2164 case BinaryOperator::Shr:
2165 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
2166 break;
2167 case BinaryOperator::LE:
2168 case BinaryOperator::LT:
2169 case BinaryOperator::GE:
2170 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002171 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002172 break;
2173 case BinaryOperator::EQ:
2174 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002175 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00002176 break;
2177 case BinaryOperator::And:
2178 case BinaryOperator::Xor:
2179 case BinaryOperator::Or:
2180 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
2181 break;
2182 case BinaryOperator::LAnd:
2183 case BinaryOperator::LOr:
2184 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
2185 break;
2186 case BinaryOperator::MulAssign:
2187 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002188 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002189 if (!CompTy.isNull())
2190 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2191 break;
2192 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002193 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002194 if (!CompTy.isNull())
2195 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2196 break;
2197 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002198 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002199 if (!CompTy.isNull())
2200 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2201 break;
2202 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002203 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002204 if (!CompTy.isNull())
2205 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2206 break;
2207 case BinaryOperator::ShlAssign:
2208 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002209 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002210 if (!CompTy.isNull())
2211 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2212 break;
2213 case BinaryOperator::AndAssign:
2214 case BinaryOperator::XorAssign:
2215 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002216 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002217 if (!CompTy.isNull())
2218 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2219 break;
2220 case BinaryOperator::Comma:
2221 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
2222 break;
2223 }
2224 if (ResultTy.isNull())
2225 return true;
2226 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00002227 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002228 else
Chris Lattnerf420df12007-08-28 18:36:55 +00002229 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002230}
2231
2232// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002233Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00002234 ExprTy *input) {
2235 Expr *Input = (Expr*)input;
2236 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2237 QualType resultType;
2238 switch (Opc) {
2239 default:
2240 assert(0 && "Unimplemented unary expr!");
2241 case UnaryOperator::PreInc:
2242 case UnaryOperator::PreDec:
2243 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
2244 break;
2245 case UnaryOperator::AddrOf:
2246 resultType = CheckAddressOfOperand(Input, OpLoc);
2247 break;
2248 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00002249 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00002250 resultType = CheckIndirectionOperand(Input, OpLoc);
2251 break;
2252 case UnaryOperator::Plus:
2253 case UnaryOperator::Minus:
2254 UsualUnaryConversions(Input);
2255 resultType = Input->getType();
2256 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
2257 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2258 resultType.getAsString());
2259 break;
2260 case UnaryOperator::Not: // bitwise complement
2261 UsualUnaryConversions(Input);
2262 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00002263 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2264 if (!resultType->isIntegerType()) {
2265 if (resultType->isComplexType())
2266 // C99 does not support '~' for complex conjugation.
2267 Diag(OpLoc, diag::ext_integer_complement_complex,
2268 resultType.getAsString());
2269 else
2270 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2271 resultType.getAsString());
2272 }
Chris Lattner4b009652007-07-25 00:24:17 +00002273 break;
2274 case UnaryOperator::LNot: // logical negation
2275 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
2276 DefaultFunctionArrayConversion(Input);
2277 resultType = Input->getType();
2278 if (!resultType->isScalarType()) // C99 6.5.3.3p1
2279 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2280 resultType.getAsString());
2281 // LNot always has type int. C99 6.5.3.3p5.
2282 resultType = Context.IntTy;
2283 break;
2284 case UnaryOperator::SizeOf:
Chris Lattnerf814d882008-07-25 21:45:37 +00002285 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc,
2286 Input->getSourceRange(), true);
Chris Lattner4b009652007-07-25 00:24:17 +00002287 break;
2288 case UnaryOperator::AlignOf:
Chris Lattnerf814d882008-07-25 21:45:37 +00002289 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc,
2290 Input->getSourceRange(), false);
Chris Lattner4b009652007-07-25 00:24:17 +00002291 break;
Chris Lattner03931a72007-08-24 21:16:53 +00002292 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00002293 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00002294 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00002295 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002296 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00002297 resultType = Input->getType();
2298 break;
2299 }
2300 if (resultType.isNull())
2301 return true;
2302 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2303}
2304
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002305/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2306Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00002307 SourceLocation LabLoc,
2308 IdentifierInfo *LabelII) {
2309 // Look up the record for this label identifier.
2310 LabelStmt *&LabelDecl = LabelMap[LabelII];
2311
2312 // If we haven't seen this label yet, create a forward reference.
2313 if (LabelDecl == 0)
2314 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2315
2316 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00002317 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2318 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00002319}
2320
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002321Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00002322 SourceLocation RPLoc) { // "({..})"
2323 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2324 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2325 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2326
2327 // FIXME: there are a variety of strange constraints to enforce here, for
2328 // example, it is not possible to goto into a stmt expression apparently.
2329 // More semantic analysis is needed.
2330
2331 // FIXME: the last statement in the compount stmt has its value used. We
2332 // should not warn about it being unused.
2333
2334 // If there are sub stmts in the compound stmt, take the type of the last one
2335 // as the type of the stmtexpr.
2336 QualType Ty = Context.VoidTy;
2337
2338 if (!Compound->body_empty())
2339 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2340 Ty = LastExpr->getType();
2341
2342 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2343}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002344
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002345Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002346 SourceLocation TypeLoc,
2347 TypeTy *argty,
2348 OffsetOfComponent *CompPtr,
2349 unsigned NumComponents,
2350 SourceLocation RPLoc) {
2351 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2352 assert(!ArgTy.isNull() && "Missing type argument!");
2353
2354 // We must have at least one component that refers to the type, and the first
2355 // one is known to be a field designator. Verify that the ArgTy represents
2356 // a struct/union/class.
2357 if (!ArgTy->isRecordType())
2358 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2359
2360 // Otherwise, create a compound literal expression as the base, and
2361 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002362 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002363
Chris Lattnerb37522e2007-08-31 21:49:13 +00002364 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2365 // GCC extension, diagnose them.
2366 if (NumComponents != 1)
2367 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2368 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2369
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002370 for (unsigned i = 0; i != NumComponents; ++i) {
2371 const OffsetOfComponent &OC = CompPtr[i];
2372 if (OC.isBrackets) {
2373 // Offset of an array sub-field. TODO: Should we allow vector elements?
2374 const ArrayType *AT = Res->getType()->getAsArrayType();
2375 if (!AT) {
2376 delete Res;
2377 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2378 Res->getType().getAsString());
2379 }
2380
Chris Lattner2af6a802007-08-30 17:59:59 +00002381 // FIXME: C++: Verify that operator[] isn't overloaded.
2382
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002383 // C99 6.5.2.1p1
2384 Expr *Idx = static_cast<Expr*>(OC.U.E);
2385 if (!Idx->getType()->isIntegerType())
2386 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2387 Idx->getSourceRange());
2388
2389 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2390 continue;
2391 }
2392
2393 const RecordType *RC = Res->getType()->getAsRecordType();
2394 if (!RC) {
2395 delete Res;
2396 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2397 Res->getType().getAsString());
2398 }
2399
2400 // Get the decl corresponding to this.
2401 RecordDecl *RD = RC->getDecl();
2402 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2403 if (!MemberDecl)
2404 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2405 OC.U.IdentInfo->getName(),
2406 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002407
2408 // FIXME: C++: Verify that MemberDecl isn't a static field.
2409 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002410 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2411 // matter here.
2412 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002413 }
2414
2415 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2416 BuiltinLoc);
2417}
2418
2419
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002420Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002421 TypeTy *arg1, TypeTy *arg2,
2422 SourceLocation RPLoc) {
2423 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2424 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2425
2426 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2427
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002428 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002429}
2430
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002431Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002432 ExprTy *expr1, ExprTy *expr2,
2433 SourceLocation RPLoc) {
2434 Expr *CondExpr = static_cast<Expr*>(cond);
2435 Expr *LHSExpr = static_cast<Expr*>(expr1);
2436 Expr *RHSExpr = static_cast<Expr*>(expr2);
2437
2438 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2439
2440 // The conditional expression is required to be a constant expression.
2441 llvm::APSInt condEval(32);
2442 SourceLocation ExpLoc;
2443 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2444 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2445 CondExpr->getSourceRange());
2446
2447 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2448 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2449 RHSExpr->getType();
2450 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2451}
2452
Nate Begemanbd881ef2008-01-30 20:50:20 +00002453/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002454/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002455/// The number of arguments has already been validated to match the number of
2456/// arguments in FnType.
2457static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002458 unsigned NumParams = FnType->getNumArgs();
Nate Begeman778fd3b2008-04-18 23:35:14 +00002459 for (unsigned i = 0; i != NumParams; ++i) {
2460 QualType ExprTy = Args[i]->getType().getCanonicalType();
2461 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2462
2463 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002464 return false;
Nate Begeman778fd3b2008-04-18 23:35:14 +00002465 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002466 return true;
2467}
2468
2469Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2470 SourceLocation *CommaLocs,
2471 SourceLocation BuiltinLoc,
2472 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002473 // __builtin_overload requires at least 2 arguments
2474 if (NumArgs < 2)
2475 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2476 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002477
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002478 // The first argument is required to be a constant expression. It tells us
2479 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002480 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002481 Expr *NParamsExpr = Args[0];
2482 llvm::APSInt constEval(32);
2483 SourceLocation ExpLoc;
2484 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2485 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2486 NParamsExpr->getSourceRange());
2487
2488 // Verify that the number of parameters is > 0
2489 unsigned NumParams = constEval.getZExtValue();
2490 if (NumParams == 0)
2491 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2492 NParamsExpr->getSourceRange());
2493 // Verify that we have at least 1 + NumParams arguments to the builtin.
2494 if ((NumParams + 1) > NumArgs)
2495 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2496 SourceRange(BuiltinLoc, RParenLoc));
2497
2498 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002499 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002500 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002501 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2502 // UsualUnaryConversions will convert the function DeclRefExpr into a
2503 // pointer to function.
2504 Expr *Fn = UsualUnaryConversions(Args[i]);
2505 FunctionTypeProto *FnType = 0;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002506 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2507 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2508 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2509 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002510
2511 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2512 // parameters, and the number of parameters must match the value passed to
2513 // the builtin.
2514 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002515 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2516 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002517
2518 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002519 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002520 // If they match, return a new OverloadExpr.
Nate Begemanc6078c92008-01-31 05:38:29 +00002521 if (ExprsMatchFnType(Args+1, FnType)) {
2522 if (OE)
2523 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2524 OE->getFn()->getSourceRange());
2525 // Remember our match, and continue processing the remaining arguments
2526 // to catch any errors.
2527 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2528 BuiltinLoc, RParenLoc);
2529 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002530 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002531 // Return the newly created OverloadExpr node, if we succeded in matching
2532 // exactly one of the candidate functions.
2533 if (OE)
2534 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002535
2536 // If we didn't find a matching function Expr in the __builtin_overload list
2537 // the return an error.
2538 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002539 for (unsigned i = 0; i != NumParams; ++i) {
2540 if (i != 0) typeNames += ", ";
2541 typeNames += Args[i+1]->getType().getAsString();
2542 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002543
2544 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2545 SourceRange(BuiltinLoc, RParenLoc));
2546}
2547
Anders Carlsson36760332007-10-15 20:28:48 +00002548Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2549 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002550 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002551 Expr *E = static_cast<Expr*>(expr);
2552 QualType T = QualType::getFromOpaquePtr(type);
2553
2554 InitBuiltinVaListType();
2555
Chris Lattner005ed752008-01-04 18:04:52 +00002556 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2557 != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002558 return Diag(E->getLocStart(),
2559 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2560 E->getType().getAsString(),
2561 E->getSourceRange());
2562
2563 // FIXME: Warn if a non-POD type is passed in.
2564
2565 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2566}
2567
Chris Lattner005ed752008-01-04 18:04:52 +00002568bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2569 SourceLocation Loc,
2570 QualType DstType, QualType SrcType,
2571 Expr *SrcExpr, const char *Flavor) {
2572 // Decode the result (notice that AST's are still created for extensions).
2573 bool isInvalid = false;
2574 unsigned DiagKind;
2575 switch (ConvTy) {
2576 default: assert(0 && "Unknown conversion type");
2577 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002578 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00002579 DiagKind = diag::ext_typecheck_convert_pointer_int;
2580 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002581 case IntToPointer:
2582 DiagKind = diag::ext_typecheck_convert_int_pointer;
2583 break;
Chris Lattner005ed752008-01-04 18:04:52 +00002584 case IncompatiblePointer:
2585 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2586 break;
2587 case FunctionVoidPointer:
2588 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2589 break;
2590 case CompatiblePointerDiscardsQualifiers:
2591 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2592 break;
2593 case Incompatible:
2594 DiagKind = diag::err_typecheck_convert_incompatible;
2595 isInvalid = true;
2596 break;
2597 }
2598
2599 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2600 SrcExpr->getSourceRange());
2601 return isInvalid;
2602}