blob: 80b33f208794ceea97b17f997baa552a00f2ce77 [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"
15#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner3e254fb2008-04-08 04:40:51 +000017#include "clang/AST/ExprCXX.h"
Steve Naroff9ed3e772008-05-29 21:12:08 +000018#include "clang/AST/ExprObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Lex/Preprocessor.h"
20#include "clang/Lex/LiteralSupport.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000021#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023#include "clang/Basic/TargetInfo.h"
Steve Naroff52a81c02008-09-03 18:15:37 +000024#include "clang/Parse/DeclSpec.h"
25#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
Chris Lattner299b8842008-07-25 21:10:04 +000028//===----------------------------------------------------------------------===//
29// Standard Promotions and Conversions
30//===----------------------------------------------------------------------===//
31
Chris Lattner299b8842008-07-25 21:10:04 +000032/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
33void Sema::DefaultFunctionArrayConversion(Expr *&E) {
34 QualType Ty = E->getType();
35 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
36
37 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
38 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
39 Ty = E->getType();
40 }
41 if (Ty->isFunctionType())
42 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattner2aa68822008-07-25 21:33:13 +000043 else if (Ty->isArrayType()) {
44 // In C90 mode, arrays only promote to pointers if the array expression is
45 // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has
46 // type 'array of type' is converted to an expression that has type 'pointer
47 // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression
48 // that has type 'array of type' ...". The relevant change is "an lvalue"
49 // (C90) to "an expression" (C99).
Chris Lattner25168a52008-07-26 21:30:36 +000050 if (getLangOptions().C99 || E->isLvalue(Context) == Expr::LV_Valid)
Chris Lattner2aa68822008-07-25 21:33:13 +000051 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
52 }
Chris Lattner299b8842008-07-25 21:10:04 +000053}
54
55/// UsualUnaryConversions - Performs various conversions that are common to most
56/// operators (C99 6.3). The conversions of array and function types are
57/// sometimes surpressed. For example, the array->pointer conversion doesn't
58/// apply if the array is an argument to the sizeof or address (&) operators.
59/// In these instances, this routine should *not* be called.
60Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
61 QualType Ty = Expr->getType();
62 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
63
64 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
65 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
66 Ty = Expr->getType();
67 }
68 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
69 ImpCastExprToType(Expr, Context.IntTy);
70 else
71 DefaultFunctionArrayConversion(Expr);
72
73 return Expr;
74}
75
Chris Lattner9305c3d2008-07-25 22:25:12 +000076/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
77/// do not have a prototype. Arguments that have type float are promoted to
78/// double. All other argument types are converted by UsualUnaryConversions().
79void Sema::DefaultArgumentPromotion(Expr *&Expr) {
80 QualType Ty = Expr->getType();
81 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
82
83 // If this is a 'float' (CVR qualified or typedef) promote to double.
84 if (const BuiltinType *BT = Ty->getAsBuiltinType())
85 if (BT->getKind() == BuiltinType::Float)
86 return ImpCastExprToType(Expr, Context.DoubleTy);
87
88 UsualUnaryConversions(Expr);
89}
90
Chris Lattner299b8842008-07-25 21:10:04 +000091/// UsualArithmeticConversions - Performs various conversions that are common to
92/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
93/// routine returns the first non-arithmetic type found. The client is
94/// responsible for emitting appropriate error diagnostics.
95/// FIXME: verify the conversion rules for "complex int" are consistent with
96/// GCC.
97QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
98 bool isCompAssign) {
99 if (!isCompAssign) {
100 UsualUnaryConversions(lhsExpr);
101 UsualUnaryConversions(rhsExpr);
102 }
103 // For conversion purposes, we ignore any qualifiers.
104 // For example, "const float" and "float" are equivalent.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000105 QualType lhs =
106 Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType();
107 QualType rhs =
108 Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType();
Chris Lattner299b8842008-07-25 21:10:04 +0000109
110 // If both types are identical, no conversion is needed.
111 if (lhs == rhs)
112 return lhs;
113
114 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
115 // The caller can deal with this (e.g. pointer + int).
116 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
117 return lhs;
118
119 // At this point, we have two different arithmetic types.
120
121 // Handle complex types first (C99 6.3.1.8p1).
122 if (lhs->isComplexType() || rhs->isComplexType()) {
123 // if we have an integer operand, the result is the complex type.
124 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
125 // convert the rhs to the lhs complex type.
126 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
127 return lhs;
128 }
129 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
130 // convert the lhs to the rhs complex type.
131 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
132 return rhs;
133 }
134 // This handles complex/complex, complex/float, or float/complex.
135 // When both operands are complex, the shorter operand is converted to the
136 // type of the longer, and that is the type of the result. This corresponds
137 // to what is done when combining two real floating-point operands.
138 // The fun begins when size promotion occur across type domains.
139 // From H&S 6.3.4: When one operand is complex and the other is a real
140 // floating-point type, the less precise type is converted, within it's
141 // real or complex domain, to the precision of the other type. For example,
142 // when combining a "long double" with a "double _Complex", the
143 // "double _Complex" is promoted to "long double _Complex".
144 int result = Context.getFloatingTypeOrder(lhs, rhs);
145
146 if (result > 0) { // The left side is bigger, convert rhs.
147 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
148 if (!isCompAssign)
149 ImpCastExprToType(rhsExpr, rhs);
150 } else if (result < 0) { // The right side is bigger, convert lhs.
151 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
152 if (!isCompAssign)
153 ImpCastExprToType(lhsExpr, lhs);
154 }
155 // At this point, lhs and rhs have the same rank/size. Now, make sure the
156 // domains match. This is a requirement for our implementation, C99
157 // does not require this promotion.
158 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
159 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
160 if (!isCompAssign)
161 ImpCastExprToType(lhsExpr, rhs);
162 return rhs;
163 } else { // handle "_Complex double, double".
164 if (!isCompAssign)
165 ImpCastExprToType(rhsExpr, lhs);
166 return lhs;
167 }
168 }
169 return lhs; // The domain/size match exactly.
170 }
171 // Now handle "real" floating types (i.e. float, double, long double).
172 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
173 // if we have an integer operand, the result is the real floating type.
174 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
175 // convert rhs to the lhs floating point type.
176 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
177 return lhs;
178 }
179 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
180 // convert lhs to the rhs floating point type.
181 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
182 return rhs;
183 }
184 // We have two real floating types, float/complex combos were handled above.
185 // Convert the smaller operand to the bigger result.
186 int result = Context.getFloatingTypeOrder(lhs, rhs);
187
188 if (result > 0) { // convert the rhs
189 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
190 return lhs;
191 }
192 if (result < 0) { // convert the lhs
193 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
194 return rhs;
195 }
196 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
197 }
198 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
199 // Handle GCC complex int extension.
200 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
201 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
202
203 if (lhsComplexInt && rhsComplexInt) {
204 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
205 rhsComplexInt->getElementType()) >= 0) {
206 // convert the rhs
207 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
208 return lhs;
209 }
210 if (!isCompAssign)
211 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
212 return rhs;
213 } else if (lhsComplexInt && rhs->isIntegerType()) {
214 // convert the rhs to the lhs complex type.
215 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
216 return lhs;
217 } else if (rhsComplexInt && lhs->isIntegerType()) {
218 // convert the lhs to the rhs complex type.
219 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
220 return rhs;
221 }
222 }
223 // Finally, we have two differing integer types.
224 // The rules for this case are in C99 6.3.1.8
225 int compare = Context.getIntegerTypeOrder(lhs, rhs);
226 bool lhsSigned = lhs->isSignedIntegerType(),
227 rhsSigned = rhs->isSignedIntegerType();
228 QualType destType;
229 if (lhsSigned == rhsSigned) {
230 // Same signedness; use the higher-ranked type
231 destType = compare >= 0 ? lhs : rhs;
232 } else if (compare != (lhsSigned ? 1 : -1)) {
233 // The unsigned type has greater than or equal rank to the
234 // signed type, so use the unsigned type
235 destType = lhsSigned ? rhs : lhs;
236 } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) {
237 // The two types are different widths; if we are here, that
238 // means the signed type is larger than the unsigned type, so
239 // use the signed type.
240 destType = lhsSigned ? lhs : rhs;
241 } else {
242 // The signed type is higher-ranked than the unsigned type,
243 // but isn't actually any bigger (like unsigned int and long
244 // on most 32-bit systems). Use the unsigned type corresponding
245 // to the signed type.
246 destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
247 }
248 if (!isCompAssign) {
249 ImpCastExprToType(lhsExpr, destType);
250 ImpCastExprToType(rhsExpr, destType);
251 }
252 return destType;
253}
254
255//===----------------------------------------------------------------------===//
256// Semantic Analysis for various Expression Types
257//===----------------------------------------------------------------------===//
258
259
Steve Naroff87d58b42007-09-16 03:34:24 +0000260/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +0000261/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
262/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
263/// multiple tokens. However, the common case is that StringToks points to one
264/// string.
265///
266Action::ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +0000267Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner4b009652007-07-25 00:24:17 +0000268 assert(NumStringToks && "Must have at least one string!");
269
270 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
271 if (Literal.hadError)
272 return ExprResult(true);
273
274 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
275 for (unsigned i = 0; i != NumStringToks; ++i)
276 StringTokLocs.push_back(StringToks[i].getLocation());
Chris Lattnera6dcce32008-02-11 00:02:17 +0000277
278 // Verify that pascal strings aren't too large.
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000279 if (Literal.Pascal && Literal.GetStringLength() > 256)
280 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
281 SourceRange(StringToks[0].getLocation(),
282 StringToks[NumStringToks-1].getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000283
Chris Lattnera6dcce32008-02-11 00:02:17 +0000284 QualType StrTy = Context.CharTy;
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +0000285 if (Literal.AnyWide) StrTy = Context.getWCharType();
Chris Lattnera6dcce32008-02-11 00:02:17 +0000286 if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
287
288 // Get an array type for the string, according to C99 6.4.5. This includes
289 // the nul terminator character as well as the string length for pascal
290 // strings.
291 StrTy = Context.getConstantArrayType(StrTy,
292 llvm::APInt(32, Literal.GetStringLength()+1),
293 ArrayType::Normal, 0);
294
Chris Lattner4b009652007-07-25 00:24:17 +0000295 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
296 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera6dcce32008-02-11 00:02:17 +0000297 Literal.AnyWide, StrTy,
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000298 StringToks[0].getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000299 StringToks[NumStringToks-1].getLocation());
300}
301
Steve Naroffd6163f32008-09-05 22:11:13 +0000302/// DeclDefinedWithinScope - Return true if the specified decl is defined at or
303/// within the 'Within' scope. The current Scope is CurScope.
304///
305/// NOTE: This method is extremely inefficient (linear scan), this should not be
306/// used in common cases.
307///
308static bool DeclDefinedWithinScope(ScopedDecl *D, Scope *Within,
309 Scope *CurScope) {
310 while (1) {
311 assert(CurScope && "CurScope not nested within 'Within'?");
312
313 // Check this scope for the decl.
314 if (CurScope->isDeclScope(D)) return true;
315
316 if (CurScope == Within) return false;
317 CurScope = CurScope->getParent();
318 }
319}
Chris Lattner4b009652007-07-25 00:24:17 +0000320
Steve Naroff0acc9c92007-09-15 18:49:24 +0000321/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattner4b009652007-07-25 00:24:17 +0000322/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroffe50e14c2008-03-19 23:46:26 +0000323/// identifier is used in a function call context.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000324Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000325 IdentifierInfo &II,
326 bool HasTrailingLParen) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000327 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroff6384a012008-04-02 14:35:35 +0000328 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000329
330 // If this reference is in an Objective-C method, then ivar lookup happens as
331 // well.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000332 if (getCurMethodDecl()) {
Steve Naroffe57c21a2008-04-01 23:04:06 +0000333 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000334 // There are two cases to handle here. 1) scoped lookup could have failed,
335 // in which case we should look for an ivar. 2) scoped lookup could have
336 // found a decl, but that decl is outside the current method (i.e. a global
337 // variable). In these two cases, we do a lookup for an ivar with this
338 // name, if the lookup suceeds, we replace it our current decl.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000339 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000340 ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface();
Chris Lattnered94f762008-07-21 04:44:44 +0000341 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II)) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000342 // FIXME: This should use a new expr for a direct reference, don't turn
343 // this into Self->ivar, just return a BareIVarExpr or something.
344 IdentifierInfo &II = Context.Idents.get("self");
345 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
346 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
347 static_cast<Expr*>(SelfExpr.Val), true, true);
348 }
349 }
Steve Naroff0ccfaa42008-08-10 19:10:41 +0000350 // Needed to implement property "super.method" notation.
Daniel Dunbar4837ae72008-08-14 22:04:54 +0000351 if (SD == 0 && &II == SuperID) {
Steve Naroff6f786252008-06-02 23:03:37 +0000352 QualType T = Context.getPointerType(Context.getObjCInterfaceType(
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000353 getCurMethodDecl()->getClassInterface()));
Steve Naroff0ccfaa42008-08-10 19:10:41 +0000354 return new PredefinedExpr(Loc, T, PredefinedExpr::ObjCSuper);
Steve Naroff6f786252008-06-02 23:03:37 +0000355 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000356 }
Steve Naroff4d1b93d2008-09-10 18:33:00 +0000357 // If we are parsing a block, check the block parameter list.
358 if (CurBlock) {
359 for (unsigned i = 0, e = CurBlock->Params.size(); i != e; ++i)
360 if (CurBlock->Params[i]->getIdentifier() == &II)
361 D = CurBlock->Params[i];
362 }
Chris Lattner4b009652007-07-25 00:24:17 +0000363 if (D == 0) {
364 // Otherwise, this could be an implicitly declared function reference (legal
365 // in C90, extension in C99).
366 if (HasTrailingLParen &&
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000367 !getLangOptions().CPlusPlus) // Not in C++.
Chris Lattner4b009652007-07-25 00:24:17 +0000368 D = ImplicitlyDefineFunction(Loc, II, S);
369 else {
370 // If this name wasn't predeclared and if this is not a function call,
371 // diagnose the problem.
372 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
373 }
374 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000375
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000376 if (CXXFieldDecl *FD = dyn_cast<CXXFieldDecl>(D)) {
377 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) {
378 if (MD->isStatic())
379 // "invalid use of member 'x' in static member function"
380 return Diag(Loc, diag::err_invalid_member_use_in_static_method,
381 FD->getName());
382 if (cast<CXXRecordDecl>(MD->getParent()) != FD->getParent())
383 // "invalid use of nonstatic data member 'x'"
384 return Diag(Loc, diag::err_invalid_non_static_member_use,
385 FD->getName());
386
387 if (FD->isInvalidDecl())
388 return true;
389
390 // FIXME: Use DeclRefExpr or a new Expr for a direct CXXField reference.
391 ExprResult ThisExpr = ActOnCXXThis(SourceLocation());
392 return new MemberExpr(static_cast<Expr*>(ThisExpr.Val),
393 true, FD, Loc, FD->getType());
394 }
395
396 return Diag(Loc, diag::err_invalid_non_static_member_use, FD->getName());
397 }
Chris Lattner4b009652007-07-25 00:24:17 +0000398 if (isa<TypedefDecl>(D))
399 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek42730c52008-01-07 19:49:32 +0000400 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian3102df92007-12-05 18:16:33 +0000401 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000402 if (isa<NamespaceDecl>(D))
403 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000404
Steve Naroffd6163f32008-09-05 22:11:13 +0000405 // Make the DeclRefExpr or BlockDeclRefExpr for the decl.
406 ValueDecl *VD = cast<ValueDecl>(D);
407
408 // check if referencing an identifier with __attribute__((deprecated)).
409 if (VD->getAttr<DeprecatedAttr>())
410 Diag(Loc, diag::warn_deprecated, VD->getName());
411
412 // Only create DeclRefExpr's for valid Decl's.
413 if (VD->isInvalidDecl())
414 return true;
415
416 // If this reference is not in a block or if the referenced variable is
417 // within the block, create a normal DeclRefExpr.
418 //
419 // FIXME: This will create BlockDeclRefExprs for global variables,
420 // function references, enums constants, etc which is suboptimal :) and breaks
421 // things like "integer constant expression" tests.
422 //
423 if (!CurBlock || DeclDefinedWithinScope(VD, CurBlock->TheScope, S))
424 return new DeclRefExpr(VD, VD->getType(), Loc);
425
426 // If we are in a block and the variable is outside the current block,
427 // bind the variable reference with a BlockDeclRefExpr.
428
429 // If the variable is in the byref set, bind it directly, otherwise it will be
430 // bound by-copy, thus we make it const within the closure.
431 if (!CurBlock->ByRefVars.count(VD))
432 VD->getType().addConst();
433
434 return new BlockDeclRefExpr(VD, VD->getType(), Loc, false);
Chris Lattner4b009652007-07-25 00:24:17 +0000435}
436
Chris Lattner69909292008-08-10 01:53:14 +0000437Sema::ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000438 tok::TokenKind Kind) {
Chris Lattner69909292008-08-10 01:53:14 +0000439 PredefinedExpr::IdentType IT;
Chris Lattner4b009652007-07-25 00:24:17 +0000440
441 switch (Kind) {
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000442 default: assert(0 && "Unknown simple primary expr!");
Chris Lattner69909292008-08-10 01:53:14 +0000443 case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2]
444 case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break;
445 case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000446 }
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000447
448 // Verify that this is in a function context.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000449 if (getCurFunctionDecl() == 0 && getCurMethodDecl() == 0)
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000450 return Diag(Loc, diag::err_predef_outside_function);
Chris Lattner4b009652007-07-25 00:24:17 +0000451
Chris Lattner7e637512008-01-12 08:14:25 +0000452 // Pre-defined identifiers are of type char[x], where x is the length of the
453 // string.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000454 unsigned Length;
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000455 if (getCurFunctionDecl())
456 Length = getCurFunctionDecl()->getIdentifier()->getLength();
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000457 else
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000458 Length = getCurMethodDecl()->getSynthesizedMethodSize();
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000459
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000460 llvm::APInt LengthI(32, Length + 1);
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000461 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000462 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattner69909292008-08-10 01:53:14 +0000463 return new PredefinedExpr(Loc, ResTy, IT);
Chris Lattner4b009652007-07-25 00:24:17 +0000464}
465
Steve Naroff87d58b42007-09-16 03:34:24 +0000466Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000467 llvm::SmallString<16> CharBuffer;
468 CharBuffer.resize(Tok.getLength());
469 const char *ThisTokBegin = &CharBuffer[0];
470 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
471
472 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
473 Tok.getLocation(), PP);
474 if (Literal.hadError())
475 return ExprResult(true);
Chris Lattner6b22fb72008-03-01 08:32:21 +0000476
477 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
478
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000479 return new CharacterLiteral(Literal.getValue(), Literal.isWide(), type,
480 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000481}
482
Steve Naroff87d58b42007-09-16 03:34:24 +0000483Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000484 // fast path for a single digit (which is quite common). A single digit
485 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
486 if (Tok.getLength() == 1) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000487 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000488
Chris Lattner8cd0e932008-03-05 18:54:05 +0000489 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner48d7f382008-04-02 04:24:33 +0000490 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Chris Lattner4b009652007-07-25 00:24:17 +0000491 Context.IntTy,
492 Tok.getLocation()));
493 }
494 llvm::SmallString<512> IntegerBuffer;
495 IntegerBuffer.resize(Tok.getLength());
496 const char *ThisTokBegin = &IntegerBuffer[0];
497
498 // Get the spelling of the token, which eliminates trigraphs, etc.
499 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
500 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
501 Tok.getLocation(), PP);
502 if (Literal.hadError)
503 return ExprResult(true);
504
Chris Lattner1de66eb2007-08-26 03:42:43 +0000505 Expr *Res;
506
507 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000508 QualType Ty;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000509 if (Literal.isFloat)
Chris Lattner858eece2007-09-22 18:29:59 +0000510 Ty = Context.FloatTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000511 else if (!Literal.isLong)
Chris Lattner858eece2007-09-22 18:29:59 +0000512 Ty = Context.DoubleTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000513 else
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000514 Ty = Context.LongDoubleTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000515
516 const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty);
517
Ted Kremenekddedbe22007-11-29 00:56:49 +0000518 // isExact will be set by GetFloatValue().
519 bool isExact = false;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000520 Res = new FloatingLiteral(Literal.GetFloatValue(Format, &isExact), &isExact,
Ted Kremenekddedbe22007-11-29 00:56:49 +0000521 Ty, Tok.getLocation());
522
Chris Lattner1de66eb2007-08-26 03:42:43 +0000523 } else if (!Literal.isIntegerLiteral()) {
524 return ExprResult(true);
525 } else {
Chris Lattner48d7f382008-04-02 04:24:33 +0000526 QualType Ty;
Chris Lattner4b009652007-07-25 00:24:17 +0000527
Neil Booth7421e9c2007-08-29 22:00:19 +0000528 // long long is a C99 feature.
529 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000530 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000531 Diag(Tok.getLocation(), diag::ext_longlong);
532
Chris Lattner4b009652007-07-25 00:24:17 +0000533 // Get the value in the widest-possible width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000534 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000535
536 if (Literal.GetIntegerValue(ResultVal)) {
537 // If this value didn't fit into uintmax_t, warn and force to ull.
538 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattner48d7f382008-04-02 04:24:33 +0000539 Ty = Context.UnsignedLongLongTy;
540 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner8cd0e932008-03-05 18:54:05 +0000541 "long long is not intmax_t?");
Chris Lattner4b009652007-07-25 00:24:17 +0000542 } else {
543 // If this value fits into a ULL, try to figure out what else it fits into
544 // according to the rules of C99 6.4.4.1p5.
545
546 // Octal, Hexadecimal, and integers with a U suffix are allowed to
547 // be an unsigned int.
548 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
549
550 // Check from smallest to largest, picking the smallest type we can.
Chris Lattnere4068872008-05-09 05:59:00 +0000551 unsigned Width = 0;
Chris Lattner98540b62007-08-23 21:58:08 +0000552 if (!Literal.isLong && !Literal.isLongLong) {
553 // Are int/unsigned possibilities?
Chris Lattnere4068872008-05-09 05:59:00 +0000554 unsigned IntSize = Context.Target.getIntWidth();
555
Chris Lattner4b009652007-07-25 00:24:17 +0000556 // Does it fit in a unsigned int?
557 if (ResultVal.isIntN(IntSize)) {
558 // Does it fit in a signed int?
559 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000560 Ty = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000561 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000562 Ty = Context.UnsignedIntTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000563 Width = IntSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000564 }
Chris Lattner4b009652007-07-25 00:24:17 +0000565 }
566
567 // Are long/unsigned long possibilities?
Chris Lattner48d7f382008-04-02 04:24:33 +0000568 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattnere4068872008-05-09 05:59:00 +0000569 unsigned LongSize = Context.Target.getLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000570
571 // Does it fit in a unsigned long?
572 if (ResultVal.isIntN(LongSize)) {
573 // Does it fit in a signed long?
574 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000575 Ty = Context.LongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000576 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000577 Ty = Context.UnsignedLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000578 Width = LongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000579 }
Chris Lattner4b009652007-07-25 00:24:17 +0000580 }
581
582 // Finally, check long long if needed.
Chris Lattner48d7f382008-04-02 04:24:33 +0000583 if (Ty.isNull()) {
Chris Lattnere4068872008-05-09 05:59:00 +0000584 unsigned LongLongSize = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000585
586 // Does it fit in a unsigned long long?
587 if (ResultVal.isIntN(LongLongSize)) {
588 // Does it fit in a signed long long?
589 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000590 Ty = Context.LongLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000591 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000592 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000593 Width = LongLongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000594 }
595 }
596
597 // If we still couldn't decide a type, we probably have something that
598 // does not fit in a signed long long, but has no U suffix.
Chris Lattner48d7f382008-04-02 04:24:33 +0000599 if (Ty.isNull()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000600 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattner48d7f382008-04-02 04:24:33 +0000601 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000602 Width = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000603 }
Chris Lattnere4068872008-05-09 05:59:00 +0000604
605 if (ResultVal.getBitWidth() != Width)
606 ResultVal.trunc(Width);
Chris Lattner4b009652007-07-25 00:24:17 +0000607 }
608
Chris Lattner48d7f382008-04-02 04:24:33 +0000609 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000610 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000611
612 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
613 if (Literal.isImaginary)
614 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
615
616 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000617}
618
Steve Naroff87d58b42007-09-16 03:34:24 +0000619Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000620 ExprTy *Val) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000621 Expr *E = (Expr *)Val;
622 assert((E != 0) && "ActOnParenExpr() missing expr");
623 return new ParenExpr(L, R, E);
Chris Lattner4b009652007-07-25 00:24:17 +0000624}
625
626/// The UsualUnaryConversions() function is *not* called by this routine.
627/// See C99 6.3.2.1p[2-4] for more details.
628QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
Chris Lattnerf814d882008-07-25 21:45:37 +0000629 SourceLocation OpLoc,
630 const SourceRange &ExprRange,
631 bool isSizeof) {
Chris Lattner4b009652007-07-25 00:24:17 +0000632 // C99 6.5.3.4p1:
633 if (isa<FunctionType>(exprType) && isSizeof)
634 // alignof(function) is allowed.
Chris Lattnerf814d882008-07-25 21:45:37 +0000635 Diag(OpLoc, diag::ext_sizeof_function_type, ExprRange);
Chris Lattner4b009652007-07-25 00:24:17 +0000636 else if (exprType->isVoidType())
Chris Lattnerf814d882008-07-25 21:45:37 +0000637 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof",
638 ExprRange);
Chris Lattner4b009652007-07-25 00:24:17 +0000639 else if (exprType->isIncompleteType()) {
640 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
641 diag::err_alignof_incomplete_type,
Chris Lattnerf814d882008-07-25 21:45:37 +0000642 exprType.getAsString(), ExprRange);
Chris Lattner4b009652007-07-25 00:24:17 +0000643 return QualType(); // error
644 }
645 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
646 return Context.getSizeType();
647}
648
649Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000650ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000651 SourceLocation LPLoc, TypeTy *Ty,
652 SourceLocation RPLoc) {
653 // If error parsing type, ignore.
654 if (Ty == 0) return true;
655
656 // Verify that this is a valid expression.
657 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
658
Chris Lattnerf814d882008-07-25 21:45:37 +0000659 QualType resultType =
660 CheckSizeOfAlignOfOperand(ArgTy, OpLoc, SourceRange(LPLoc, RPLoc),isSizeof);
Chris Lattner4b009652007-07-25 00:24:17 +0000661
662 if (resultType.isNull())
663 return true;
664 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
665}
666
Chris Lattner5110ad52007-08-24 21:41:10 +0000667QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000668 DefaultFunctionArrayConversion(V);
669
Chris Lattnera16e42d2007-08-26 05:39:26 +0000670 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000671 if (const ComplexType *CT = V->getType()->getAsComplexType())
672 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000673
674 // Otherwise they pass through real integer and floating point types here.
675 if (V->getType()->isArithmeticType())
676 return V->getType();
677
678 // Reject anything else.
679 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
680 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000681}
682
683
Chris Lattner4b009652007-07-25 00:24:17 +0000684
Steve Naroff87d58b42007-09-16 03:34:24 +0000685Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000686 tok::TokenKind Kind,
687 ExprTy *Input) {
688 UnaryOperator::Opcode Opc;
689 switch (Kind) {
690 default: assert(0 && "Unknown unary op!");
691 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
692 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
693 }
694 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
695 if (result.isNull())
696 return true;
697 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
698}
699
700Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000701ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000702 ExprTy *Idx, SourceLocation RLoc) {
703 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
704
705 // Perform default conversions.
706 DefaultFunctionArrayConversion(LHSExp);
707 DefaultFunctionArrayConversion(RHSExp);
708
709 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
710
711 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000712 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000713 // in the subscript position. As a result, we need to derive the array base
714 // and index from the expression types.
715 Expr *BaseExpr, *IndexExpr;
716 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000717 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000718 BaseExpr = LHSExp;
719 IndexExpr = RHSExp;
720 // FIXME: need to deal with const...
721 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000722 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000723 // Handle the uncommon case of "123[Ptr]".
724 BaseExpr = RHSExp;
725 IndexExpr = LHSExp;
726 // FIXME: need to deal with const...
727 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000728 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
729 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000730 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000731
732 // Component access limited to variables (reject vec4.rg[1]).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000733 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
734 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000735 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000736 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000737 // FIXME: need to deal with const...
738 ResultType = VTy->getElementType();
739 } else {
740 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
741 RHSExp->getSourceRange());
742 }
743 // C99 6.5.2.1p1
744 if (!IndexExpr->getType()->isIntegerType())
745 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
746 IndexExpr->getSourceRange());
747
748 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
749 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattner9db553e2008-04-02 06:59:01 +0000750 // void (*)(int)) and pointers to incomplete types. Functions are not
751 // objects in C99.
Chris Lattner4b009652007-07-25 00:24:17 +0000752 if (!ResultType->isObjectType())
753 return Diag(BaseExpr->getLocStart(),
754 diag::err_typecheck_subscript_not_object,
755 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
756
757 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
758}
759
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000760QualType Sema::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000761CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000762 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000763 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begemanc8e51f82008-05-09 06:41:27 +0000764
765 // This flag determines whether or not the component is to be treated as a
766 // special name, or a regular GLSL-style component access.
767 bool SpecialComponent = false;
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000768
769 // The vector accessor can't exceed the number of elements.
770 const char *compStr = CompName.getName();
771 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000772 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000773 baseType.getAsString(), SourceRange(CompLoc));
774 return QualType();
775 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000776
777 // Check that we've found one of the special components, or that the component
778 // names must come from the same set.
779 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
780 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
781 SpecialComponent = true;
782 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner9096b792007-08-02 22:33:49 +0000783 do
784 compStr++;
785 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
786 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
787 do
788 compStr++;
789 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
790 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
791 do
792 compStr++;
793 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
794 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000795
Nate Begemanc8e51f82008-05-09 06:41:27 +0000796 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000797 // We didn't get to the end of the string. This means the component names
798 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000799 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000800 std::string(compStr,compStr+1), SourceRange(CompLoc));
801 return QualType();
802 }
803 // Each component accessor can't exceed the vector type.
804 compStr = CompName.getName();
805 while (*compStr) {
806 if (vecType->isAccessorWithinNumElements(*compStr))
807 compStr++;
808 else
809 break;
810 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000811 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000812 // We didn't get to the end of the string. This means a component accessor
813 // exceeds the number of elements in the vector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000814 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000815 baseType.getAsString(), SourceRange(CompLoc));
816 return QualType();
817 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000818
819 // If we have a special component name, verify that the current vector length
820 // is an even number, since all special component names return exactly half
821 // the elements.
822 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
823 return QualType();
824 }
825
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000826 // The component accessor looks fine - now we need to compute the actual type.
827 // The vector type is implied by the component accessor. For example,
828 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begemanc8e51f82008-05-09 06:41:27 +0000829 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
830 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
831 : strlen(CompName.getName());
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000832 if (CompSize == 1)
833 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000834
Nate Begemanaf6ed502008-04-18 23:10:10 +0000835 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroff82113e32007-07-29 16:33:31 +0000836 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemanaf6ed502008-04-18 23:10:10 +0000837 // diagostics look bad. We want extended vector types to appear built-in.
838 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
839 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
840 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroff82113e32007-07-29 16:33:31 +0000841 }
842 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000843}
844
Daniel Dunbar60e8b162008-09-03 01:05:41 +0000845/// constructSetterName - Return the setter name for the given
846/// identifier, i.e. "set" + Name where the initial character of Name
847/// has been capitalized.
848// FIXME: Merge with same routine in Parser. But where should this
849// live?
850static IdentifierInfo *constructSetterName(IdentifierTable &Idents,
851 const IdentifierInfo *Name) {
852 unsigned N = Name->getLength();
853 char *SelectorName = new char[3 + N];
854 memcpy(SelectorName, "set", 3);
855 memcpy(&SelectorName[3], Name->getName(), N);
856 SelectorName[3] = toupper(SelectorName[3]);
857
858 IdentifierInfo *Setter =
859 &Idents.get(SelectorName, &SelectorName[3 + N]);
860 delete[] SelectorName;
861 return Setter;
862}
863
Chris Lattner4b009652007-07-25 00:24:17 +0000864Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000865ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000866 tok::TokenKind OpKind, SourceLocation MemberLoc,
867 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000868 Expr *BaseExpr = static_cast<Expr *>(Base);
869 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000870
871 // Perform default conversions.
872 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000873
Steve Naroff2cb66382007-07-26 03:11:44 +0000874 QualType BaseType = BaseExpr->getType();
875 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000876
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000877 // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr
878 // must have pointer type, and the accessed type is the pointee.
Chris Lattner4b009652007-07-25 00:24:17 +0000879 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000880 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000881 BaseType = PT->getPointeeType();
882 else
Chris Lattner7d5a8762008-07-21 05:35:34 +0000883 return Diag(MemberLoc, diag::err_typecheck_member_reference_arrow,
884 BaseType.getAsString(), BaseExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000885 }
Chris Lattnera57cf472008-07-21 04:28:12 +0000886
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000887 // Handle field access to simple records. This also handles access to fields
888 // of the ObjC 'id' struct.
Chris Lattnere35a1042007-07-31 19:29:30 +0000889 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000890 RecordDecl *RDecl = RTy->getDecl();
891 if (RTy->isIncompleteType())
892 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
893 BaseExpr->getSourceRange());
894 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000895 FieldDecl *MemberDecl = RDecl->getMember(&Member);
896 if (!MemberDecl)
Chris Lattner7d5a8762008-07-21 05:35:34 +0000897 return Diag(MemberLoc, diag::err_typecheck_no_member, Member.getName(),
898 BaseExpr->getSourceRange());
Eli Friedman76b49832008-02-06 22:48:16 +0000899
900 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000901 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000902 QualType MemberType = MemberDecl->getType();
903 unsigned combinedQualifiers =
Chris Lattner35fef522008-02-20 20:55:12 +0000904 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman76b49832008-02-06 22:48:16 +0000905 MemberType = MemberType.getQualifiedType(combinedQualifiers);
906
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000907 return new MemberExpr(BaseExpr, OpKind == tok::arrow, MemberDecl,
Eli Friedman76b49832008-02-06 22:48:16 +0000908 MemberLoc, MemberType);
Chris Lattnera57cf472008-07-21 04:28:12 +0000909 }
910
Chris Lattnere9d71612008-07-21 04:59:05 +0000911 // Handle access to Objective-C instance variables, such as "Obj->ivar" and
912 // (*Obj).ivar.
Chris Lattnerb2b9da72008-07-21 04:36:39 +0000913 if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) {
914 if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000915 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
Chris Lattnera57cf472008-07-21 04:28:12 +0000916 OpKind == tok::arrow);
Chris Lattner7d5a8762008-07-21 05:35:34 +0000917 return Diag(MemberLoc, diag::err_typecheck_member_reference_ivar,
Chris Lattner52292be2008-07-21 04:42:08 +0000918 IFTy->getDecl()->getName(), Member.getName(),
Chris Lattner7d5a8762008-07-21 05:35:34 +0000919 BaseExpr->getSourceRange());
Chris Lattnera57cf472008-07-21 04:28:12 +0000920 }
921
Chris Lattnere9d71612008-07-21 04:59:05 +0000922 // Handle Objective-C property access, which is "Obj.property" where Obj is a
923 // pointer to a (potentially qualified) interface type.
924 const PointerType *PTy;
925 const ObjCInterfaceType *IFTy;
926 if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) &&
927 (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) {
928 ObjCInterfaceDecl *IFace = IFTy->getDecl();
Daniel Dunbardd851282008-08-30 05:35:15 +0000929
Daniel Dunbar60e8b162008-09-03 01:05:41 +0000930 // Search for a declared property first.
Chris Lattnere9d71612008-07-21 04:59:05 +0000931 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member))
932 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
933
Daniel Dunbar60e8b162008-09-03 01:05:41 +0000934 // Check protocols on qualified interfaces.
Chris Lattnerd5f81792008-07-21 05:20:01 +0000935 for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(),
936 E = IFTy->qual_end(); I != E; ++I)
937 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member))
938 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
Daniel Dunbar60e8b162008-09-03 01:05:41 +0000939
940 // If that failed, look for an "implicit" property by seeing if the nullary
941 // selector is implemented.
942
943 // FIXME: The logic for looking up nullary and unary selectors should be
944 // shared with the code in ActOnInstanceMessage.
945
946 Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
947 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
948
949 // If this reference is in an @implementation, check for 'private' methods.
950 if (!Getter)
951 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
952 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
953 if (ObjCImplementationDecl *ImpDecl =
954 ObjCImplementations[ClassDecl->getIdentifier()])
955 Getter = ImpDecl->getInstanceMethod(Sel);
956
957 if (Getter) {
958 // If we found a getter then this may be a valid dot-reference, we
959 // need to also look for the matching setter.
960 IdentifierInfo *SetterName = constructSetterName(PP.getIdentifierTable(),
961 &Member);
962 Selector SetterSel = PP.getSelectorTable().getUnarySelector(SetterName);
963 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
964
965 if (!Setter) {
966 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
967 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
968 if (ObjCImplementationDecl *ImpDecl =
969 ObjCImplementations[ClassDecl->getIdentifier()])
970 Setter = ImpDecl->getInstanceMethod(SetterSel);
971 }
972
973 // FIXME: There are some issues here. First, we are not
974 // diagnosing accesses to read-only properties because we do not
975 // know if this is a getter or setter yet. Second, we are
976 // checking that the type of the setter matches the type we
977 // expect.
978 return new ObjCPropertyRefExpr(Getter, Setter, Getter->getResultType(),
979 MemberLoc, BaseExpr);
980 }
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000981 }
Chris Lattnera57cf472008-07-21 04:28:12 +0000982
983 // Handle 'field access' to vectors, such as 'V.xx'.
984 if (BaseType->isExtVectorType() && OpKind == tok::period) {
985 // Component access limited to variables (reject vec4.rg.g).
986 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
987 !isa<ExtVectorElementExpr>(BaseExpr))
Chris Lattner7d5a8762008-07-21 05:35:34 +0000988 return Diag(MemberLoc, diag::err_ext_vector_component_access,
989 BaseExpr->getSourceRange());
Chris Lattnera57cf472008-07-21 04:28:12 +0000990 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
991 if (ret.isNull())
992 return true;
993 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
994 }
995
Chris Lattner7d5a8762008-07-21 05:35:34 +0000996 return Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union,
997 BaseType.getAsString(), BaseExpr->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000998}
999
Steve Naroff87d58b42007-09-16 03:34:24 +00001000/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001001/// This provides the location of the left/right parens and a list of comma
1002/// locations.
1003Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001004ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001005 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +00001006 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
1007 Expr *Fn = static_cast<Expr *>(fn);
1008 Expr **Args = reinterpret_cast<Expr**>(args);
1009 assert(Fn && "no function call expression");
Chris Lattner3e254fb2008-04-08 04:40:51 +00001010 FunctionDecl *FDecl = NULL;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001011
1012 // Promote the function operand.
1013 UsualUnaryConversions(Fn);
1014
1015 // If we're directly calling a function, get the declaration for
1016 // that function.
1017 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
1018 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
1019 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
1020
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001021 // Make the call expr early, before semantic checks. This guarantees cleanup
1022 // of arguments and function on error.
Chris Lattner97316c02008-04-10 02:22:51 +00001023 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001024 Context.BoolTy, RParenLoc));
Steve Naroffd6163f32008-09-05 22:11:13 +00001025 const FunctionType *FuncT;
1026 if (!Fn->getType()->isBlockPointerType()) {
1027 // C99 6.5.2.2p1 - "The expression that denotes the called function shall
1028 // have type pointer to function".
1029 const PointerType *PT = Fn->getType()->getAsPointerType();
1030 if (PT == 0)
1031 return Diag(LParenLoc, diag::err_typecheck_call_not_function,
1032 Fn->getSourceRange());
1033 FuncT = PT->getPointeeType()->getAsFunctionType();
1034 } else { // This is a block call.
1035 FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()->
1036 getAsFunctionType();
1037 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001038 if (FuncT == 0)
Chris Lattner61000b12008-08-14 04:33:24 +00001039 return Diag(LParenLoc, diag::err_typecheck_call_not_function,
1040 Fn->getSourceRange());
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001041
1042 // We know the result type of the call, set it.
1043 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +00001044
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001045 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001046 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
1047 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001048 unsigned NumArgsInProto = Proto->getNumArgs();
1049 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +00001050
Chris Lattner3e254fb2008-04-08 04:40:51 +00001051 // If too few arguments are available (and we don't have default
1052 // arguments for the remaining parameters), don't make the call.
1053 if (NumArgs < NumArgsInProto) {
Chris Lattner97316c02008-04-10 02:22:51 +00001054 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001055 // Use default arguments for missing arguments
1056 NumArgsToCheck = NumArgsInProto;
Chris Lattner97316c02008-04-10 02:22:51 +00001057 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001058 } else
Steve Naroffd6163f32008-09-05 22:11:13 +00001059 return Diag(RParenLoc,
1060 !Fn->getType()->isBlockPointerType()
1061 ? diag::err_typecheck_call_too_few_args
1062 : diag::err_typecheck_block_too_few_args,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001063 Fn->getSourceRange());
1064 }
1065
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001066 // If too many are passed and not variadic, error on the extras and drop
1067 // them.
1068 if (NumArgs > NumArgsInProto) {
1069 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001070 Diag(Args[NumArgsInProto]->getLocStart(),
Steve Naroffd6163f32008-09-05 22:11:13 +00001071 !Fn->getType()->isBlockPointerType()
1072 ? diag::err_typecheck_call_too_many_args
1073 : diag::err_typecheck_block_too_many_args,
1074 Fn->getSourceRange(),
Chris Lattner4b009652007-07-25 00:24:17 +00001075 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001076 Args[NumArgs-1]->getLocEnd()));
1077 // This deletes the extra arguments.
1078 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +00001079 }
1080 NumArgsToCheck = NumArgsInProto;
1081 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001082
Chris Lattner4b009652007-07-25 00:24:17 +00001083 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001084 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner005ed752008-01-04 18:04:52 +00001085 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001086
1087 Expr *Arg;
1088 if (i < NumArgs)
1089 Arg = Args[i];
1090 else
1091 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner005ed752008-01-04 18:04:52 +00001092 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001093
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001094 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +00001095 AssignConvertType ConvTy =
1096 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001097 TheCall->setArg(i, Arg);
Eli Friedman583c31e2008-09-02 05:09:35 +00001098
Chris Lattner005ed752008-01-04 18:04:52 +00001099 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
1100 ArgType, Arg, "passing"))
1101 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001102 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001103
1104 // If this is a variadic call, handle args passed through "...".
1105 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +00001106 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001107 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
1108 Expr *Arg = Args[i];
1109 DefaultArgumentPromotion(Arg);
1110 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +00001111 }
Steve Naroffdb65e052007-08-28 23:30:39 +00001112 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001113 } else {
1114 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
1115
Steve Naroffdb65e052007-08-28 23:30:39 +00001116 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001117 for (unsigned i = 0; i != NumArgs; i++) {
1118 Expr *Arg = Args[i];
1119 DefaultArgumentPromotion(Arg);
1120 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +00001121 }
Chris Lattner4b009652007-07-25 00:24:17 +00001122 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001123
Chris Lattner2e64c072007-08-10 20:18:51 +00001124 // Do special checking on direct calls to functions.
Eli Friedmand0e9d092008-05-14 19:38:39 +00001125 if (FDecl)
1126 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattner2e64c072007-08-10 20:18:51 +00001127
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001128 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +00001129}
1130
1131Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001132ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +00001133 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +00001134 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001135 QualType literalType = QualType::getFromOpaquePtr(Ty);
1136 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +00001137 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +00001138 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +00001139
Eli Friedman8c2173d2008-05-20 05:22:08 +00001140 if (literalType->isArrayType()) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001141 if (literalType->isVariableArrayType())
Eli Friedman8c2173d2008-05-20 05:22:08 +00001142 return Diag(LParenLoc,
1143 diag::err_variable_object_no_init,
1144 SourceRange(LParenLoc,
1145 literalExpr->getSourceRange().getEnd()));
1146 } else if (literalType->isIncompleteType()) {
1147 return Diag(LParenLoc,
1148 diag::err_typecheck_decl_incomplete_type,
1149 literalType.getAsString(),
1150 SourceRange(LParenLoc,
1151 literalExpr->getSourceRange().getEnd()));
1152 }
1153
Steve Narofff0b23542008-01-10 22:15:12 +00001154 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +00001155 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +00001156
Argiris Kirtzidis95256e62008-06-28 06:07:14 +00001157 bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl();
Steve Naroffbe37fc02008-01-14 18:19:28 +00001158 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +00001159 if (CheckForConstantInitializer(literalExpr, literalType))
1160 return true;
1161 }
Steve Naroffbe37fc02008-01-14 18:19:28 +00001162 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001163}
1164
1165Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001166ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +00001167 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +00001168 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +00001169
Steve Naroff0acc9c92007-09-15 18:49:24 +00001170 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +00001171 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +00001172
Chris Lattner48d7f382008-04-02 04:24:33 +00001173 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
1174 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
1175 return E;
Chris Lattner4b009652007-07-25 00:24:17 +00001176}
1177
Argiris Kirtzidis95de23a2008-08-16 20:27:34 +00001178/// CheckCastTypes - Check type constraints for casting between types.
Daniel Dunbar5ad49de2008-08-20 03:55:42 +00001179bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) {
Argiris Kirtzidis95de23a2008-08-16 20:27:34 +00001180 UsualUnaryConversions(castExpr);
1181
1182 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
1183 // type needs to be scalar.
1184 if (castType->isVoidType()) {
1185 // Cast to void allows any expr type.
1186 } else if (!castType->isScalarType() && !castType->isVectorType()) {
1187 // GCC struct/union extension: allow cast to self.
1188 if (Context.getCanonicalType(castType) !=
1189 Context.getCanonicalType(castExpr->getType()) ||
1190 (!castType->isStructureType() && !castType->isUnionType())) {
1191 // Reject any other conversions to non-scalar types.
1192 return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar,
1193 castType.getAsString(), castExpr->getSourceRange());
1194 }
1195
1196 // accept this, but emit an ext-warn.
1197 Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar,
1198 castType.getAsString(), castExpr->getSourceRange());
1199 } else if (!castExpr->getType()->isScalarType() &&
1200 !castExpr->getType()->isVectorType()) {
1201 return Diag(castExpr->getLocStart(),
1202 diag::err_typecheck_expect_scalar_operand,
1203 castExpr->getType().getAsString(),castExpr->getSourceRange());
1204 } else if (castExpr->getType()->isVectorType()) {
1205 if (CheckVectorCast(TyR, castExpr->getType(), castType))
1206 return true;
1207 } else if (castType->isVectorType()) {
1208 if (CheckVectorCast(TyR, castType, castExpr->getType()))
1209 return true;
1210 }
1211 return false;
1212}
1213
Chris Lattnerd1f26b32007-12-20 00:44:32 +00001214bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001215 assert(VectorTy->isVectorType() && "Not a vector type!");
1216
1217 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001218 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonf257b4c2007-11-27 05:51:55 +00001219 return Diag(R.getBegin(),
1220 Ty->isVectorType() ?
1221 diag::err_invalid_conversion_between_vectors :
1222 diag::err_invalid_conversion_between_vector_and_integer,
1223 VectorTy.getAsString().c_str(),
1224 Ty.getAsString().c_str(), R);
1225 } else
1226 return Diag(R.getBegin(),
1227 diag::err_invalid_conversion_between_vector_and_scalar,
1228 VectorTy.getAsString().c_str(),
1229 Ty.getAsString().c_str(), R);
1230
1231 return false;
1232}
1233
Chris Lattner4b009652007-07-25 00:24:17 +00001234Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +00001235ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +00001236 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +00001237 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +00001238
1239 Expr *castExpr = static_cast<Expr*>(Op);
1240 QualType castType = QualType::getFromOpaquePtr(Ty);
1241
Argiris Kirtzidis95de23a2008-08-16 20:27:34 +00001242 if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr))
1243 return true;
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001244 return new ExplicitCastExpr(castType, castExpr, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001245}
1246
Chris Lattner98a425c2007-11-26 01:40:58 +00001247/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
1248/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +00001249inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
1250 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
1251 UsualUnaryConversions(cond);
1252 UsualUnaryConversions(lex);
1253 UsualUnaryConversions(rex);
1254 QualType condT = cond->getType();
1255 QualType lexT = lex->getType();
1256 QualType rexT = rex->getType();
1257
1258 // first, check the condition.
1259 if (!condT->isScalarType()) { // C99 6.5.15p2
1260 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
1261 condT.getAsString());
1262 return QualType();
1263 }
Chris Lattner992ae932008-01-06 22:42:25 +00001264
1265 // Now check the two expressions.
1266
1267 // If both operands have arithmetic type, do the usual arithmetic conversions
1268 // to find a common type: C99 6.5.15p3,5.
1269 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001270 UsualArithmeticConversions(lex, rex);
1271 return lex->getType();
1272 }
Chris Lattner992ae932008-01-06 22:42:25 +00001273
1274 // If both operands are the same structure or union type, the result is that
1275 // type.
Chris Lattner71225142007-07-31 21:27:01 +00001276 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +00001277 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +00001278 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +00001279 // "If both the operands have structure or union type, the result has
1280 // that type." This implies that CV qualifiers are dropped.
1281 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001282 }
Chris Lattner992ae932008-01-06 22:42:25 +00001283
1284 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroff95cb3892008-05-12 21:44:38 +00001285 // The following || allows only one side to be void (a GCC-ism).
1286 if (lexT->isVoidType() || rexT->isVoidType()) {
Eli Friedmanf025aac2008-06-04 19:47:51 +00001287 if (!lexT->isVoidType())
Steve Naroff95cb3892008-05-12 21:44:38 +00001288 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
1289 rex->getSourceRange());
1290 if (!rexT->isVoidType())
1291 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
Nuno Lopes4ba41fd2008-06-04 19:14:12 +00001292 lex->getSourceRange());
Eli Friedmanf025aac2008-06-04 19:47:51 +00001293 ImpCastExprToType(lex, Context.VoidTy);
1294 ImpCastExprToType(rex, Context.VoidTy);
1295 return Context.VoidTy;
Steve Naroff95cb3892008-05-12 21:44:38 +00001296 }
Steve Naroff12ebf272008-01-08 01:11:38 +00001297 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
1298 // the type of the other operand."
1299 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001300 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +00001301 return lexT;
1302 }
1303 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001304 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +00001305 return rexT;
1306 }
Daniel Dunbarcc785c82008-09-03 17:53:25 +00001307 // Allow any Objective-C types to devolve to id type.
1308 // FIXME: This seems to match gcc behavior, although that is very
1309 // arguably incorrect. For example, (xxx ? (id<P>) : (id<P>)) has
1310 // type id, which seems broken.
1311 if (Context.isObjCObjectPointerType(lexT) &&
1312 Context.isObjCObjectPointerType(rexT)) {
1313 // FIXME: This is not the correct composite type. This only
1314 // happens to work because id can more or less be used anywhere,
1315 // however this may change the type of method sends.
1316 // FIXME: gcc adds some type-checking of the arguments and emits
1317 // (confusing) incompatible comparison warnings in some
1318 // cases. Investigate.
1319 QualType compositeType = Context.getObjCIdType();
1320 ImpCastExprToType(lex, compositeType);
1321 ImpCastExprToType(rex, compositeType);
1322 return compositeType;
1323 }
Chris Lattner0ac51632008-01-06 22:50:31 +00001324 // Handle the case where both operands are pointers before we handle null
1325 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +00001326 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
1327 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
1328 // get the "pointed to" types
1329 QualType lhptee = LHSPT->getPointeeType();
1330 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001331
Chris Lattner71225142007-07-31 21:27:01 +00001332 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
1333 if (lhptee->isVoidType() &&
Chris Lattner9db553e2008-04-02 06:59:01 +00001334 rhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +00001335 // Figure out necessary qualifiers (C99 6.5.15p6)
1336 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +00001337 QualType destType = Context.getPointerType(destPointee);
1338 ImpCastExprToType(lex, destType); // add qualifiers if necessary
1339 ImpCastExprToType(rex, destType); // promote to void*
1340 return destType;
1341 }
Chris Lattner9db553e2008-04-02 06:59:01 +00001342 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +00001343 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +00001344 QualType destType = Context.getPointerType(destPointee);
1345 ImpCastExprToType(lex, destType); // add qualifiers if necessary
1346 ImpCastExprToType(rex, destType); // promote to void*
1347 return destType;
1348 }
Chris Lattner4b009652007-07-25 00:24:17 +00001349
Steve Naroff85f0dc52007-10-15 20:41:53 +00001350 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1351 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +00001352 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +00001353 lexT.getAsString(), rexT.getAsString(),
1354 lex->getSourceRange(), rex->getSourceRange());
Daniel Dunbarcd23bb22008-08-26 00:41:39 +00001355 // In this situation, assume a conservative type; in general
1356 // we assume void* type. No especially good reason, but this
1357 // is what gcc does, and we do have to pick to get a
1358 // consistent AST. However, if either type is an Objective-C
1359 // object type then use id.
1360 QualType incompatTy;
1361 if (Context.isObjCObjectPointerType(lexT) ||
1362 Context.isObjCObjectPointerType(rexT)) {
1363 incompatTy = Context.getObjCIdType();
1364 } else {
1365 incompatTy = Context.getPointerType(Context.VoidTy);
1366 }
1367 ImpCastExprToType(lex, incompatTy);
1368 ImpCastExprToType(rex, incompatTy);
1369 return incompatTy;
Chris Lattner71225142007-07-31 21:27:01 +00001370 }
1371 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001372 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
1373 // differently qualified versions of compatible types, the result type is
1374 // a pointer to an appropriately qualified version of the *composite*
1375 // type.
Eli Friedmane38150e2008-05-16 20:37:07 +00001376 // FIXME: Need to calculate the composite type.
Eli Friedmanca07c902008-02-10 22:59:36 +00001377 // FIXME: Need to add qualifiers
Eli Friedmane38150e2008-05-16 20:37:07 +00001378 QualType compositeType = lexT;
1379 ImpCastExprToType(lex, compositeType);
1380 ImpCastExprToType(rex, compositeType);
1381 return compositeType;
Chris Lattner4b009652007-07-25 00:24:17 +00001382 }
Chris Lattner4b009652007-07-25 00:24:17 +00001383 }
Chris Lattner992ae932008-01-06 22:42:25 +00001384 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +00001385 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
1386 lexT.getAsString(), rexT.getAsString(),
1387 lex->getSourceRange(), rex->getSourceRange());
1388 return QualType();
1389}
1390
Steve Naroff87d58b42007-09-16 03:34:24 +00001391/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +00001392/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +00001393Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001394 SourceLocation ColonLoc,
1395 ExprTy *Cond, ExprTy *LHS,
1396 ExprTy *RHS) {
1397 Expr *CondExpr = (Expr *) Cond;
1398 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +00001399
1400 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
1401 // was the condition.
1402 bool isLHSNull = LHSExpr == 0;
1403 if (isLHSNull)
1404 LHSExpr = CondExpr;
1405
Chris Lattner4b009652007-07-25 00:24:17 +00001406 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
1407 RHSExpr, QuestionLoc);
1408 if (result.isNull())
1409 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +00001410 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
1411 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +00001412}
1413
Chris Lattner4b009652007-07-25 00:24:17 +00001414
1415// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1416// being closely modeled after the C99 spec:-). The odd characteristic of this
1417// routine is it effectively iqnores the qualifiers on the top level pointee.
1418// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1419// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001420Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001421Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1422 QualType lhptee, rhptee;
1423
1424 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001425 lhptee = lhsType->getAsPointerType()->getPointeeType();
1426 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001427
1428 // make sure we operate on the canonical type
Chris Lattnerd5a56aa2008-07-26 22:17:49 +00001429 lhptee = Context.getCanonicalType(lhptee);
1430 rhptee = Context.getCanonicalType(rhptee);
Chris Lattner4b009652007-07-25 00:24:17 +00001431
Chris Lattner005ed752008-01-04 18:04:52 +00001432 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001433
1434 // C99 6.5.16.1p1: This following citation is common to constraints
1435 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1436 // qualifiers of the type *pointed to* by the right;
Chris Lattner35fef522008-02-20 20:55:12 +00001437 // FIXME: Handle ASQualType
1438 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1439 rhptee.getCVRQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001440 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001441
1442 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1443 // incomplete type and the other is a pointer to a qualified or unqualified
1444 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001445 if (lhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001446 if (rhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001447 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001448
1449 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001450 assert(rhptee->isFunctionType());
1451 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001452 }
1453
1454 if (rhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001455 if (lhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001456 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001457
1458 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001459 assert(lhptee->isFunctionType());
1460 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001461 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00001462
1463 // Check for ObjC interfaces
1464 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
1465 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
1466 if (LHSIface && RHSIface &&
1467 Context.canAssignObjCInterfaces(LHSIface, RHSIface))
1468 return ConvTy;
1469
1470 // ID acts sort of like void* for ObjC interfaces
1471 if (LHSIface && Context.isObjCIdType(rhptee))
1472 return ConvTy;
1473 if (RHSIface && Context.isObjCIdType(lhptee))
1474 return ConvTy;
1475
Chris Lattner4b009652007-07-25 00:24:17 +00001476 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1477 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001478 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1479 rhptee.getUnqualifiedType()))
1480 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001481 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001482}
1483
Steve Naroff3454b6c2008-09-04 15:10:53 +00001484/// CheckBlockPointerTypesForAssignment - This routine determines whether two
1485/// block pointer types are compatible or whether a block and normal pointer
1486/// are compatible. It is more restrict than comparing two function pointer
1487// types.
1488Sema::AssignConvertType
1489Sema::CheckBlockPointerTypesForAssignment(QualType lhsType,
1490 QualType rhsType) {
1491 QualType lhptee, rhptee;
1492
1493 // get the "pointed to" type (ignoring qualifiers at the top level)
1494 lhptee = lhsType->getAsBlockPointerType()->getPointeeType();
1495 rhptee = rhsType->getAsBlockPointerType()->getPointeeType();
1496
1497 // make sure we operate on the canonical type
1498 lhptee = Context.getCanonicalType(lhptee);
1499 rhptee = Context.getCanonicalType(rhptee);
1500
1501 AssignConvertType ConvTy = Compatible;
1502
1503 // For blocks we enforce that qualifiers are identical.
1504 if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers())
1505 ConvTy = CompatiblePointerDiscardsQualifiers;
1506
1507 if (!Context.typesAreBlockCompatible(lhptee, rhptee))
1508 return IncompatibleBlockPointer;
1509 return ConvTy;
1510}
1511
Chris Lattner4b009652007-07-25 00:24:17 +00001512/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1513/// has code to accommodate several GCC extensions when type checking
1514/// pointers. Here are some objectionable examples that GCC considers warnings:
1515///
1516/// int a, *pint;
1517/// short *pshort;
1518/// struct foo *pfoo;
1519///
1520/// pint = pshort; // warning: assignment from incompatible pointer type
1521/// a = pint; // warning: assignment makes integer from pointer without a cast
1522/// pint = a; // warning: assignment makes pointer from integer without a cast
1523/// pint = pfoo; // warning: assignment from incompatible pointer type
1524///
1525/// As a result, the code for dealing with pointers is more complex than the
1526/// C99 spec dictates.
Chris Lattner4b009652007-07-25 00:24:17 +00001527///
Chris Lattner005ed752008-01-04 18:04:52 +00001528Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001529Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001530 // Get canonical types. We're not formatting these types, just comparing
1531 // them.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +00001532 lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType();
1533 rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType();
Eli Friedman48d0bb02008-05-30 18:07:22 +00001534
1535 if (lhsType == rhsType)
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001536 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001537
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001538 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattnere1577e22008-04-07 06:52:53 +00001539 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001540 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001541 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001542 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001543
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001544 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1545 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001546 return Compatible;
Steve Naroff936c4362008-06-03 14:04:54 +00001547 // Relax integer conversions like we do for pointers below.
1548 if (rhsType->isIntegerType())
1549 return IntToPointer;
1550 if (lhsType->isIntegerType())
1551 return PointerToInt;
Chris Lattner1853da22008-01-04 23:18:45 +00001552 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001553 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001554
Nate Begemanc5f0f652008-07-14 18:02:46 +00001555 if (lhsType->isVectorType() || rhsType->isVectorType()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001556 // For ExtVector, allow vector splats; float -> <n x float>
Nate Begemanc5f0f652008-07-14 18:02:46 +00001557 if (const ExtVectorType *LV = lhsType->getAsExtVectorType())
1558 if (LV->getElementType() == rhsType)
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001559 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001560
Nate Begemanc5f0f652008-07-14 18:02:46 +00001561 // If we are allowing lax vector conversions, and LHS and RHS are both
1562 // vectors, the total size only needs to be the same. This is a bitcast;
1563 // no bits are changed but the result type is different.
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001564 if (getLangOptions().LaxVectorConversions &&
1565 lhsType->isVectorType() && rhsType->isVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001566 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
1567 return Compatible;
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001568 }
1569 return Incompatible;
1570 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001571
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001572 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001573 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001574
Chris Lattner390564e2008-04-07 06:49:41 +00001575 if (isa<PointerType>(lhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001576 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001577 return IntToPointer;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001578
Chris Lattner390564e2008-04-07 06:49:41 +00001579 if (isa<PointerType>(rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001580 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff3454b6c2008-09-04 15:10:53 +00001581
Steve Naroffd6163f32008-09-05 22:11:13 +00001582 if (rhsType->getAsBlockPointerType())
1583 if (lhsType->getAsPointerType()->getPointeeType()->isVoidType())
Steve Naroff3454b6c2008-09-04 15:10:53 +00001584 return BlockVoidPointer;
1585
1586 return Incompatible;
1587 }
1588
1589 if (isa<BlockPointerType>(lhsType)) {
1590 if (rhsType->isIntegerType())
1591 return IntToPointer;
1592
1593 if (rhsType->isBlockPointerType())
1594 return CheckBlockPointerTypesForAssignment(lhsType, rhsType);
1595
1596 if (const PointerType *RHSPT = rhsType->getAsPointerType()) {
1597 if (RHSPT->getPointeeType()->isVoidType())
1598 return BlockVoidPointer;
1599 }
Chris Lattner1853da22008-01-04 23:18:45 +00001600 return Incompatible;
1601 }
1602
Chris Lattner390564e2008-04-07 06:49:41 +00001603 if (isa<PointerType>(rhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001604 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001605 if (lhsType == Context.BoolTy)
1606 return Compatible;
1607
1608 if (lhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001609 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001610
Chris Lattner390564e2008-04-07 06:49:41 +00001611 if (isa<PointerType>(lhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001612 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff3454b6c2008-09-04 15:10:53 +00001613
1614 if (isa<BlockPointerType>(lhsType) &&
1615 rhsType->getAsPointerType()->getPointeeType()->isVoidType())
1616 return BlockVoidPointer;
Chris Lattner1853da22008-01-04 23:18:45 +00001617 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001618 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001619
Chris Lattner1853da22008-01-04 23:18:45 +00001620 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner390564e2008-04-07 06:49:41 +00001621 if (Context.typesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001622 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001623 }
1624 return Incompatible;
1625}
1626
Chris Lattner005ed752008-01-04 18:04:52 +00001627Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001628Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001629 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1630 // a null pointer constant.
Steve Naroff4fea7b62008-09-04 16:56:14 +00001631 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType() ||
1632 lhsType->isBlockPointerType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001633 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001634 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001635 return Compatible;
1636 }
Steve Naroff3454b6c2008-09-04 15:10:53 +00001637
1638 // We don't allow conversion of non-null-pointer constants to integers.
1639 if (lhsType->isBlockPointerType() && rExpr->getType()->isIntegerType())
1640 return IntToBlockPointer;
1641
Chris Lattner5f505bf2007-10-16 02:55:40 +00001642 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001643 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001644 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001645 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001646 //
1647 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1648 // are better understood.
1649 if (!lhsType->isReferenceType())
1650 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001651
Chris Lattner005ed752008-01-04 18:04:52 +00001652 Sema::AssignConvertType result =
1653 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001654
1655 // C99 6.5.16.1p2: The value of the right operand is converted to the
1656 // type of the assignment expression.
1657 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001658 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001659 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001660}
1661
Chris Lattner005ed752008-01-04 18:04:52 +00001662Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001663Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1664 return CheckAssignmentConstraints(lhsType, rhsType);
1665}
1666
Chris Lattner2c8bff72007-12-12 05:47:28 +00001667QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001668 Diag(loc, diag::err_typecheck_invalid_operands,
1669 lex->getType().getAsString(), rex->getType().getAsString(),
1670 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001671 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001672}
1673
1674inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1675 Expr *&rex) {
Nate Begeman03105572008-04-04 01:30:25 +00001676 // For conversion purposes, we ignore any qualifiers.
1677 // For example, "const float" and "float" are equivalent.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +00001678 QualType lhsType =
1679 Context.getCanonicalType(lex->getType()).getUnqualifiedType();
1680 QualType rhsType =
1681 Context.getCanonicalType(rex->getType()).getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001682
Nate Begemanc5f0f652008-07-14 18:02:46 +00001683 // If the vector types are identical, return.
Nate Begeman03105572008-04-04 01:30:25 +00001684 if (lhsType == rhsType)
Chris Lattner4b009652007-07-25 00:24:17 +00001685 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001686
Nate Begemanc5f0f652008-07-14 18:02:46 +00001687 // Handle the case of a vector & extvector type of the same size and element
1688 // type. It would be nice if we only had one vector type someday.
1689 if (getLangOptions().LaxVectorConversions)
1690 if (const VectorType *LV = lhsType->getAsVectorType())
1691 if (const VectorType *RV = rhsType->getAsVectorType())
1692 if (LV->getElementType() == RV->getElementType() &&
1693 LV->getNumElements() == RV->getNumElements())
1694 return lhsType->isExtVectorType() ? lhsType : rhsType;
1695
1696 // If the lhs is an extended vector and the rhs is a scalar of the same type
1697 // or a literal, promote the rhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001698 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001699 QualType eltType = V->getElementType();
1700
1701 if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) ||
1702 (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) ||
1703 (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001704 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001705 return lhsType;
1706 }
1707 }
1708
Nate Begemanc5f0f652008-07-14 18:02:46 +00001709 // If the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001710 // promote the lhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001711 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001712 QualType eltType = V->getElementType();
1713
1714 if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) ||
1715 (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) ||
1716 (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001717 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001718 return rhsType;
1719 }
1720 }
1721
Chris Lattner4b009652007-07-25 00:24:17 +00001722 // You cannot convert between vector values of different size.
1723 Diag(loc, diag::err_typecheck_vector_not_convertable,
1724 lex->getType().getAsString(), rex->getType().getAsString(),
1725 lex->getSourceRange(), rex->getSourceRange());
1726 return QualType();
1727}
1728
1729inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001730 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001731{
1732 QualType lhsType = lex->getType(), rhsType = rex->getType();
1733
1734 if (lhsType->isVectorType() || rhsType->isVectorType())
1735 return CheckVectorOperands(loc, lex, rex);
1736
Steve Naroff8f708362007-08-24 19:07:16 +00001737 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001738
Chris Lattner4b009652007-07-25 00:24:17 +00001739 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001740 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001741 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001742}
1743
1744inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001745 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001746{
1747 QualType lhsType = lex->getType(), rhsType = rex->getType();
1748
Steve Naroff8f708362007-08-24 19:07:16 +00001749 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001750
Chris Lattner4b009652007-07-25 00:24:17 +00001751 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001752 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001753 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001754}
1755
1756inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001757 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001758{
1759 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1760 return CheckVectorOperands(loc, lex, rex);
1761
Steve Naroff8f708362007-08-24 19:07:16 +00001762 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001763
Chris Lattner4b009652007-07-25 00:24:17 +00001764 // handle the common case first (both operands are arithmetic).
1765 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001766 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001767
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001768 // Put any potential pointer into PExp
1769 Expr* PExp = lex, *IExp = rex;
1770 if (IExp->getType()->isPointerType())
1771 std::swap(PExp, IExp);
1772
1773 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1774 if (IExp->getType()->isIntegerType()) {
1775 // Check for arithmetic on pointers to incomplete types
1776 if (!PTy->getPointeeType()->isObjectType()) {
1777 if (PTy->getPointeeType()->isVoidType()) {
1778 Diag(loc, diag::ext_gnu_void_ptr,
1779 lex->getSourceRange(), rex->getSourceRange());
1780 } else {
1781 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1782 lex->getType().getAsString(), lex->getSourceRange());
1783 return QualType();
1784 }
1785 }
1786 return PExp->getType();
1787 }
1788 }
1789
Chris Lattner2c8bff72007-12-12 05:47:28 +00001790 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001791}
1792
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001793// C99 6.5.6
1794QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1795 SourceLocation loc, bool isCompAssign) {
Chris Lattner4b009652007-07-25 00:24:17 +00001796 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1797 return CheckVectorOperands(loc, lex, rex);
1798
Steve Naroff8f708362007-08-24 19:07:16 +00001799 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001800
Chris Lattnerf6da2912007-12-09 21:53:25 +00001801 // Enforce type constraints: C99 6.5.6p3.
1802
1803 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001804 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001805 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001806
1807 // Either ptr - int or ptr - ptr.
1808 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001809 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001810
Chris Lattnerf6da2912007-12-09 21:53:25 +00001811 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001812 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001813 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001814 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001815 Diag(loc, diag::ext_gnu_void_ptr,
1816 lex->getSourceRange(), rex->getSourceRange());
1817 } else {
1818 Diag(loc, diag::err_typecheck_sub_ptr_object,
1819 lex->getType().getAsString(), lex->getSourceRange());
1820 return QualType();
1821 }
1822 }
1823
1824 // The result type of a pointer-int computation is the pointer type.
1825 if (rex->getType()->isIntegerType())
1826 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001827
Chris Lattnerf6da2912007-12-09 21:53:25 +00001828 // Handle pointer-pointer subtractions.
1829 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001830 QualType rpointee = RHSPTy->getPointeeType();
1831
Chris Lattnerf6da2912007-12-09 21:53:25 +00001832 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001833 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001834 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001835 if (rpointee->isVoidType()) {
1836 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-12-09 21:53:25 +00001837 Diag(loc, diag::ext_gnu_void_ptr,
1838 lex->getSourceRange(), rex->getSourceRange());
1839 } else {
1840 Diag(loc, diag::err_typecheck_sub_ptr_object,
1841 rex->getType().getAsString(), rex->getSourceRange());
1842 return QualType();
1843 }
1844 }
1845
1846 // Pointee types must be compatible.
Eli Friedman583c31e2008-09-02 05:09:35 +00001847 if (!Context.typesAreCompatible(
1848 Context.getCanonicalType(lpointee).getUnqualifiedType(),
1849 Context.getCanonicalType(rpointee).getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001850 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1851 lex->getType().getAsString(), rex->getType().getAsString(),
1852 lex->getSourceRange(), rex->getSourceRange());
1853 return QualType();
1854 }
1855
1856 return Context.getPointerDiffType();
1857 }
1858 }
1859
Chris Lattner2c8bff72007-12-12 05:47:28 +00001860 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001861}
1862
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001863// C99 6.5.7
1864QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1865 bool isCompAssign) {
Chris Lattner2c8bff72007-12-12 05:47:28 +00001866 // C99 6.5.7p2: Each of the operands shall have integer type.
1867 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1868 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001869
Chris Lattner2c8bff72007-12-12 05:47:28 +00001870 // Shifts don't perform usual arithmetic conversions, they just do integer
1871 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001872 if (!isCompAssign)
1873 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001874 UsualUnaryConversions(rex);
1875
1876 // "The type of the result is that of the promoted left operand."
1877 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001878}
1879
Eli Friedman0d9549b2008-08-22 00:56:42 +00001880static bool areComparableObjCInterfaces(QualType LHS, QualType RHS,
1881 ASTContext& Context) {
1882 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
1883 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
1884 // ID acts sort of like void* for ObjC interfaces
1885 if (LHSIface && Context.isObjCIdType(RHS))
1886 return true;
1887 if (RHSIface && Context.isObjCIdType(LHS))
1888 return true;
1889 if (!LHSIface || !RHSIface)
1890 return false;
1891 return Context.canAssignObjCInterfaces(LHSIface, RHSIface) ||
1892 Context.canAssignObjCInterfaces(RHSIface, LHSIface);
1893}
1894
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001895// C99 6.5.8
1896QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1897 bool isRelational) {
Nate Begemanc5f0f652008-07-14 18:02:46 +00001898 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1899 return CheckVectorCompareOperands(lex, rex, loc, isRelational);
1900
Chris Lattner254f3bc2007-08-26 01:18:55 +00001901 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001902 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1903 UsualArithmeticConversions(lex, rex);
1904 else {
1905 UsualUnaryConversions(lex);
1906 UsualUnaryConversions(rex);
1907 }
Chris Lattner4b009652007-07-25 00:24:17 +00001908 QualType lType = lex->getType();
1909 QualType rType = rex->getType();
1910
Ted Kremenek486509e2007-10-29 17:13:39 +00001911 // For non-floating point types, check for self-comparisons of the form
1912 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1913 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001914 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001915 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1916 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001917 if (DRL->getDecl() == DRR->getDecl())
1918 Diag(loc, diag::warn_selfcomparison);
1919 }
1920
Chris Lattner254f3bc2007-08-26 01:18:55 +00001921 if (isRelational) {
1922 if (lType->isRealType() && rType->isRealType())
1923 return Context.IntTy;
1924 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001925 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001926 if (lType->isFloatingType()) {
1927 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001928 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001929 }
1930
Chris Lattner254f3bc2007-08-26 01:18:55 +00001931 if (lType->isArithmeticType() && rType->isArithmeticType())
1932 return Context.IntTy;
1933 }
Chris Lattner4b009652007-07-25 00:24:17 +00001934
Chris Lattner22be8422007-08-26 01:10:14 +00001935 bool LHSIsNull = lex->isNullPointerConstant(Context);
1936 bool RHSIsNull = rex->isNullPointerConstant(Context);
1937
Chris Lattner254f3bc2007-08-26 01:18:55 +00001938 // All of the following pointer related warnings are GCC extensions, except
1939 // when handling null pointer constants. One day, we can consider making them
1940 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001941 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001942 QualType LCanPointeeTy =
Chris Lattnerd5a56aa2008-07-26 22:17:49 +00001943 Context.getCanonicalType(lType->getAsPointerType()->getPointeeType());
Chris Lattner56a5cd62008-04-03 05:07:25 +00001944 QualType RCanPointeeTy =
Chris Lattnerd5a56aa2008-07-26 22:17:49 +00001945 Context.getCanonicalType(rType->getAsPointerType()->getPointeeType());
Eli Friedman50727042008-02-08 01:19:44 +00001946
Steve Naroff3b435622007-11-13 14:57:38 +00001947 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001948 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1949 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
Eli Friedman0d9549b2008-08-22 00:56:42 +00001950 RCanPointeeTy.getUnqualifiedType()) &&
1951 !areComparableObjCInterfaces(LCanPointeeTy, RCanPointeeTy, Context)) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001952 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1953 lType.getAsString(), rType.getAsString(),
1954 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001955 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001956 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001957 return Context.IntTy;
1958 }
Steve Naroff3454b6c2008-09-04 15:10:53 +00001959 // Handle block pointer types.
1960 if (lType->isBlockPointerType() && rType->isBlockPointerType()) {
1961 QualType lpointee = lType->getAsBlockPointerType()->getPointeeType();
1962 QualType rpointee = rType->getAsBlockPointerType()->getPointeeType();
1963
1964 if (!LHSIsNull && !RHSIsNull &&
1965 !Context.typesAreBlockCompatible(lpointee, rpointee)) {
1966 Diag(loc, diag::err_typecheck_comparison_of_distinct_blocks,
1967 lType.getAsString(), rType.getAsString(),
1968 lex->getSourceRange(), rex->getSourceRange());
1969 }
1970 ImpCastExprToType(rex, lType); // promote the pointer to pointer
1971 return Context.IntTy;
1972 }
1973
Steve Naroff936c4362008-06-03 14:04:54 +00001974 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) {
1975 if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
1976 ImpCastExprToType(rex, lType);
1977 return Context.IntTy;
1978 }
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001979 }
Steve Naroff936c4362008-06-03 14:04:54 +00001980 if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) &&
1981 rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001982 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001983 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1984 lType.getAsString(), rType.getAsString(),
1985 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001986 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001987 return Context.IntTy;
1988 }
Steve Naroff936c4362008-06-03 14:04:54 +00001989 if (lType->isIntegerType() &&
1990 (rType->isPointerType() || rType->isObjCQualifiedIdType())) {
Chris Lattner22be8422007-08-26 01:10:14 +00001991 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001992 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1993 lType.getAsString(), rType.getAsString(),
1994 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001995 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001996 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001997 }
Steve Naroff4fea7b62008-09-04 16:56:14 +00001998 // Handle block pointers.
1999 if (lType->isBlockPointerType() && rType->isIntegerType()) {
2000 if (!RHSIsNull)
2001 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
2002 lType.getAsString(), rType.getAsString(),
2003 lex->getSourceRange(), rex->getSourceRange());
2004 ImpCastExprToType(rex, lType); // promote the integer to pointer
2005 return Context.IntTy;
2006 }
2007 if (lType->isIntegerType() && rType->isBlockPointerType()) {
2008 if (!LHSIsNull)
2009 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
2010 lType.getAsString(), rType.getAsString(),
2011 lex->getSourceRange(), rex->getSourceRange());
2012 ImpCastExprToType(lex, rType); // promote the integer to pointer
2013 return Context.IntTy;
2014 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00002015 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00002016}
2017
Nate Begemanc5f0f652008-07-14 18:02:46 +00002018/// CheckVectorCompareOperands - vector comparisons are a clang extension that
2019/// operates on extended vector types. Instead of producing an IntTy result,
2020/// like a scalar comparison, a vector comparison produces a vector of integer
2021/// types.
2022QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex,
2023 SourceLocation loc,
2024 bool isRelational) {
2025 // Check to make sure we're operating on vectors of the same type and width,
2026 // Allowing one side to be a scalar of element type.
2027 QualType vType = CheckVectorOperands(loc, lex, rex);
2028 if (vType.isNull())
2029 return vType;
2030
2031 QualType lType = lex->getType();
2032 QualType rType = rex->getType();
2033
2034 // For non-floating point types, check for self-comparisons of the form
2035 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
2036 // often indicate logic errors in the program.
2037 if (!lType->isFloatingType()) {
2038 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
2039 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
2040 if (DRL->getDecl() == DRR->getDecl())
2041 Diag(loc, diag::warn_selfcomparison);
2042 }
2043
2044 // Check for comparisons of floating point operands using != and ==.
2045 if (!isRelational && lType->isFloatingType()) {
2046 assert (rType->isFloatingType());
2047 CheckFloatComparison(loc,lex,rex);
2048 }
2049
2050 // Return the type for the comparison, which is the same as vector type for
2051 // integer vectors, or an integer type of identical size and number of
2052 // elements for floating point vectors.
2053 if (lType->isIntegerType())
2054 return lType;
2055
2056 const VectorType *VTy = lType->getAsVectorType();
2057
2058 // FIXME: need to deal with non-32b int / non-64b long long
2059 unsigned TypeSize = Context.getTypeSize(VTy->getElementType());
2060 if (TypeSize == 32) {
2061 return Context.getExtVectorType(Context.IntTy, VTy->getNumElements());
2062 }
2063 assert(TypeSize == 64 && "Unhandled vector element size in vector compare");
2064 return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements());
2065}
2066
Chris Lattner4b009652007-07-25 00:24:17 +00002067inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00002068 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00002069{
2070 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
2071 return CheckVectorOperands(loc, lex, rex);
2072
Steve Naroff8f708362007-08-24 19:07:16 +00002073 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00002074
2075 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00002076 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00002077 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00002078}
2079
2080inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
2081 Expr *&lex, Expr *&rex, SourceLocation loc)
2082{
2083 UsualUnaryConversions(lex);
2084 UsualUnaryConversions(rex);
2085
Eli Friedmanbea3f842008-05-13 20:16:47 +00002086 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Chris Lattner4b009652007-07-25 00:24:17 +00002087 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00002088 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00002089}
2090
2091inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00002092 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00002093{
2094 QualType lhsType = lex->getType();
2095 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner25168a52008-07-26 21:30:36 +00002096 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue(Context);
Chris Lattner4b009652007-07-25 00:24:17 +00002097
2098 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00002099 case Expr::MLV_Valid:
2100 break;
2101 case Expr::MLV_ConstQualified:
2102 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
2103 return QualType();
2104 case Expr::MLV_ArrayType:
2105 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
2106 lhsType.getAsString(), lex->getSourceRange());
2107 return QualType();
2108 case Expr::MLV_NotObjectType:
2109 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
2110 lhsType.getAsString(), lex->getSourceRange());
2111 return QualType();
2112 case Expr::MLV_InvalidExpression:
2113 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
2114 lex->getSourceRange());
2115 return QualType();
2116 case Expr::MLV_IncompleteType:
2117 case Expr::MLV_IncompleteVoidType:
2118 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
2119 lhsType.getAsString(), lex->getSourceRange());
2120 return QualType();
2121 case Expr::MLV_DuplicateVectorComponents:
2122 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
2123 lex->getSourceRange());
2124 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00002125 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002126
Chris Lattner005ed752008-01-04 18:04:52 +00002127 AssignConvertType ConvTy;
Chris Lattner34c85082008-08-21 18:04:13 +00002128 if (compoundType.isNull()) {
2129 // Simple assignment "x = y".
Chris Lattner005ed752008-01-04 18:04:52 +00002130 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
Chris Lattner34c85082008-08-21 18:04:13 +00002131
2132 // If the RHS is a unary plus or minus, check to see if they = and + are
2133 // right next to each other. If so, the user may have typo'd "x =+ 4"
2134 // instead of "x += 4".
2135 Expr *RHSCheck = rex;
2136 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck))
2137 RHSCheck = ICE->getSubExpr();
2138 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) {
2139 if ((UO->getOpcode() == UnaryOperator::Plus ||
2140 UO->getOpcode() == UnaryOperator::Minus) &&
2141 loc.isFileID() && UO->getOperatorLoc().isFileID() &&
2142 // Only if the two operators are exactly adjacent.
2143 loc.getFileLocWithOffset(1) == UO->getOperatorLoc())
2144 Diag(loc, diag::warn_not_compound_assign,
2145 UO->getOpcode() == UnaryOperator::Plus ? "+" : "-",
2146 SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()));
2147 }
2148 } else {
2149 // Compound assignment "x += y"
Chris Lattner005ed752008-01-04 18:04:52 +00002150 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Chris Lattner34c85082008-08-21 18:04:13 +00002151 }
Chris Lattner005ed752008-01-04 18:04:52 +00002152
2153 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
2154 rex, "assigning"))
2155 return QualType();
2156
Chris Lattner4b009652007-07-25 00:24:17 +00002157 // C99 6.5.16p3: The type of an assignment expression is the type of the
2158 // left operand unless the left operand has qualified type, in which case
2159 // it is the unqualified version of the type of the left operand.
2160 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
2161 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002162 // C++ 5.17p1: the type of the assignment expression is that of its left
2163 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00002164 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00002165}
2166
2167inline QualType Sema::CheckCommaOperands( // C99 6.5.17
2168 Expr *&lex, Expr *&rex, SourceLocation loc) {
Chris Lattner03c430f2008-07-25 20:54:07 +00002169
2170 // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions.
2171 DefaultFunctionArrayConversion(rex);
Chris Lattner4b009652007-07-25 00:24:17 +00002172 return rex->getType();
2173}
2174
2175/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
2176/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
2177QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
2178 QualType resType = op->getType();
2179 assert(!resType.isNull() && "no type for increment/decrement expression");
2180
Steve Naroffd30e1932007-08-24 17:20:07 +00002181 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00002182 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedmand9b1fec2008-05-18 18:08:51 +00002183 if (pt->getPointeeType()->isVoidType()) {
2184 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
2185 } else if (!pt->getPointeeType()->isObjectType()) {
2186 // C99 6.5.2.4p2, 6.5.6p2
Chris Lattner4b009652007-07-25 00:24:17 +00002187 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
2188 resType.getAsString(), op->getSourceRange());
2189 return QualType();
2190 }
Steve Naroffd30e1932007-08-24 17:20:07 +00002191 } else if (!resType->isRealType()) {
2192 if (resType->isComplexType())
2193 // C99 does not support ++/-- on complex types.
2194 Diag(OpLoc, diag::ext_integer_increment_complex,
2195 resType.getAsString(), op->getSourceRange());
2196 else {
2197 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
2198 resType.getAsString(), op->getSourceRange());
2199 return QualType();
2200 }
Chris Lattner4b009652007-07-25 00:24:17 +00002201 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00002202 // At this point, we know we have a real, complex or pointer type.
2203 // Now make sure the operand is a modifiable lvalue.
Chris Lattner25168a52008-07-26 21:30:36 +00002204 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue(Context);
Chris Lattner4b009652007-07-25 00:24:17 +00002205 if (mlval != Expr::MLV_Valid) {
2206 // FIXME: emit a more precise diagnostic...
2207 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
2208 op->getSourceRange());
2209 return QualType();
2210 }
2211 return resType;
2212}
2213
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00002214/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00002215/// This routine allows us to typecheck complex/recursive expressions
Daniel Dunbarb45f75c2008-08-04 20:02:37 +00002216/// where the declaration is needed for type checking. We only need to
2217/// handle cases when the expression references a function designator
2218/// or is an lvalue. Here are some examples:
2219/// - &(x) => x
2220/// - &*****f => f for f a function designator.
2221/// - &s.xx => s
2222/// - &s.zz[1].yy -> s, if zz is an array
2223/// - *(x + 1) -> x, if x is an array
2224/// - &"123"[2] -> 0
2225/// - & __real__ x -> x
Chris Lattner48d7f382008-04-02 04:24:33 +00002226static ValueDecl *getPrimaryDecl(Expr *E) {
2227 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002228 case Stmt::DeclRefExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00002229 return cast<DeclRefExpr>(E)->getDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00002230 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00002231 // Fields cannot be declared with a 'register' storage class.
2232 // &X->f is always ok, even if X is declared register.
Chris Lattner48d7f382008-04-02 04:24:33 +00002233 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnera3249072007-11-16 17:46:48 +00002234 return 0;
Chris Lattner48d7f382008-04-02 04:24:33 +00002235 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00002236 case Stmt::ArraySubscriptExprClass: {
Daniel Dunbarb45f75c2008-08-04 20:02:37 +00002237 // &X[4] and &4[X] refers to X if X is not a pointer.
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00002238
Chris Lattner48d7f382008-04-02 04:24:33 +00002239 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00002240 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00002241 return 0;
2242 else
2243 return VD;
2244 }
Daniel Dunbarb45f75c2008-08-04 20:02:37 +00002245 case Stmt::UnaryOperatorClass: {
2246 UnaryOperator *UO = cast<UnaryOperator>(E);
2247
2248 switch(UO->getOpcode()) {
2249 case UnaryOperator::Deref: {
2250 // *(X + 1) refers to X if X is not a pointer.
2251 ValueDecl *VD = getPrimaryDecl(UO->getSubExpr());
2252 if (!VD || VD->getType()->isPointerType())
2253 return 0;
2254 return VD;
2255 }
2256 case UnaryOperator::Real:
2257 case UnaryOperator::Imag:
2258 case UnaryOperator::Extension:
2259 return getPrimaryDecl(UO->getSubExpr());
2260 default:
2261 return 0;
2262 }
2263 }
2264 case Stmt::BinaryOperatorClass: {
2265 BinaryOperator *BO = cast<BinaryOperator>(E);
2266
2267 // Handle cases involving pointer arithmetic. The result of an
2268 // Assign or AddAssign is not an lvalue so they can be ignored.
2269
2270 // (x + n) or (n + x) => x
2271 if (BO->getOpcode() == BinaryOperator::Add) {
2272 if (BO->getLHS()->getType()->isPointerType()) {
2273 return getPrimaryDecl(BO->getLHS());
2274 } else if (BO->getRHS()->getType()->isPointerType()) {
2275 return getPrimaryDecl(BO->getRHS());
2276 }
2277 }
2278
2279 return 0;
2280 }
Chris Lattner4b009652007-07-25 00:24:17 +00002281 case Stmt::ParenExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00002282 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00002283 case Stmt::ImplicitCastExprClass:
2284 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner48d7f382008-04-02 04:24:33 +00002285 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00002286 default:
2287 return 0;
2288 }
2289}
2290
2291/// CheckAddressOfOperand - The operand of & must be either a function
2292/// designator or an lvalue designating an object. If it is an lvalue, the
2293/// object cannot be declared with storage class register or be a bit field.
2294/// Note: The usual conversions are *not* applied to the operand of the &
2295/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
2296QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00002297 if (getLangOptions().C99) {
2298 // Implement C99-only parts of addressof rules.
2299 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
2300 if (uOp->getOpcode() == UnaryOperator::Deref)
2301 // Per C99 6.5.3.2, the address of a deref always returns a valid result
2302 // (assuming the deref expression is valid).
2303 return uOp->getSubExpr()->getType();
2304 }
2305 // Technically, there should be a check for array subscript
2306 // expressions here, but the result of one is always an lvalue anyway.
2307 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00002308 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner25168a52008-07-26 21:30:36 +00002309 Expr::isLvalueResult lval = op->isLvalue(Context);
Chris Lattner4b009652007-07-25 00:24:17 +00002310
2311 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00002312 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
2313 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00002314 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
2315 op->getSourceRange());
2316 return QualType();
2317 }
Steve Naroff73cf87e2008-02-29 23:30:25 +00002318 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
2319 if (MemExpr->getMemberDecl()->isBitField()) {
2320 Diag(OpLoc, diag::err_typecheck_address_of,
2321 std::string("bit-field"), op->getSourceRange());
2322 return QualType();
2323 }
2324 // Check for Apple extension for accessing vector components.
2325 } else if (isa<ArraySubscriptExpr>(op) &&
2326 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
2327 Diag(OpLoc, diag::err_typecheck_address_of,
2328 std::string("vector"), op->getSourceRange());
2329 return QualType();
2330 } else if (dcl) { // C99 6.5.3.2p1
Chris Lattner4b009652007-07-25 00:24:17 +00002331 // We have an lvalue with a decl. Make sure the decl is not declared
2332 // with the register storage-class specifier.
2333 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
2334 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff73cf87e2008-02-29 23:30:25 +00002335 Diag(OpLoc, diag::err_typecheck_address_of,
2336 std::string("register variable"), op->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00002337 return QualType();
2338 }
2339 } else
2340 assert(0 && "Unknown/unexpected decl type");
Chris Lattner4b009652007-07-25 00:24:17 +00002341 }
Chris Lattnera55e3212008-07-27 00:48:22 +00002342
Chris Lattner4b009652007-07-25 00:24:17 +00002343 // If the operand has type "type", the result has type "pointer to type".
2344 return Context.getPointerType(op->getType());
2345}
2346
2347QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
2348 UsualUnaryConversions(op);
2349 QualType qType = op->getType();
2350
Chris Lattner7931f4a2007-07-31 16:53:04 +00002351 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00002352 // Note that per both C89 and C99, this is always legal, even
2353 // if ptype is an incomplete type or void.
2354 // It would be possible to warn about dereferencing a
2355 // void pointer, but it's completely well-defined,
2356 // and such a warning is unlikely to catch any mistakes.
2357 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00002358 }
2359 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
2360 qType.getAsString(), op->getSourceRange());
2361 return QualType();
2362}
2363
2364static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
2365 tok::TokenKind Kind) {
2366 BinaryOperator::Opcode Opc;
2367 switch (Kind) {
2368 default: assert(0 && "Unknown binop!");
2369 case tok::star: Opc = BinaryOperator::Mul; break;
2370 case tok::slash: Opc = BinaryOperator::Div; break;
2371 case tok::percent: Opc = BinaryOperator::Rem; break;
2372 case tok::plus: Opc = BinaryOperator::Add; break;
2373 case tok::minus: Opc = BinaryOperator::Sub; break;
2374 case tok::lessless: Opc = BinaryOperator::Shl; break;
2375 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
2376 case tok::lessequal: Opc = BinaryOperator::LE; break;
2377 case tok::less: Opc = BinaryOperator::LT; break;
2378 case tok::greaterequal: Opc = BinaryOperator::GE; break;
2379 case tok::greater: Opc = BinaryOperator::GT; break;
2380 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
2381 case tok::equalequal: Opc = BinaryOperator::EQ; break;
2382 case tok::amp: Opc = BinaryOperator::And; break;
2383 case tok::caret: Opc = BinaryOperator::Xor; break;
2384 case tok::pipe: Opc = BinaryOperator::Or; break;
2385 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
2386 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
2387 case tok::equal: Opc = BinaryOperator::Assign; break;
2388 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
2389 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
2390 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
2391 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
2392 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
2393 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
2394 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
2395 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
2396 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
2397 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
2398 case tok::comma: Opc = BinaryOperator::Comma; break;
2399 }
2400 return Opc;
2401}
2402
2403static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
2404 tok::TokenKind Kind) {
2405 UnaryOperator::Opcode Opc;
2406 switch (Kind) {
2407 default: assert(0 && "Unknown unary op!");
2408 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
2409 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
2410 case tok::amp: Opc = UnaryOperator::AddrOf; break;
2411 case tok::star: Opc = UnaryOperator::Deref; break;
2412 case tok::plus: Opc = UnaryOperator::Plus; break;
2413 case tok::minus: Opc = UnaryOperator::Minus; break;
2414 case tok::tilde: Opc = UnaryOperator::Not; break;
2415 case tok::exclaim: Opc = UnaryOperator::LNot; break;
2416 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
2417 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
2418 case tok::kw___real: Opc = UnaryOperator::Real; break;
2419 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
2420 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
2421 }
2422 return Opc;
2423}
2424
2425// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002426Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00002427 ExprTy *LHS, ExprTy *RHS) {
2428 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
2429 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
2430
Steve Naroff87d58b42007-09-16 03:34:24 +00002431 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
2432 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00002433
2434 QualType ResultTy; // Result type of the binary operator.
2435 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
2436
2437 switch (Opc) {
2438 default:
2439 assert(0 && "Unknown binary expr!");
2440 case BinaryOperator::Assign:
2441 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
2442 break;
2443 case BinaryOperator::Mul:
2444 case BinaryOperator::Div:
2445 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
2446 break;
2447 case BinaryOperator::Rem:
2448 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
2449 break;
2450 case BinaryOperator::Add:
2451 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
2452 break;
2453 case BinaryOperator::Sub:
2454 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
2455 break;
2456 case BinaryOperator::Shl:
2457 case BinaryOperator::Shr:
2458 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
2459 break;
2460 case BinaryOperator::LE:
2461 case BinaryOperator::LT:
2462 case BinaryOperator::GE:
2463 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002464 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002465 break;
2466 case BinaryOperator::EQ:
2467 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002468 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00002469 break;
2470 case BinaryOperator::And:
2471 case BinaryOperator::Xor:
2472 case BinaryOperator::Or:
2473 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
2474 break;
2475 case BinaryOperator::LAnd:
2476 case BinaryOperator::LOr:
2477 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
2478 break;
2479 case BinaryOperator::MulAssign:
2480 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002481 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002482 if (!CompTy.isNull())
2483 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2484 break;
2485 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002486 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002487 if (!CompTy.isNull())
2488 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2489 break;
2490 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002491 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002492 if (!CompTy.isNull())
2493 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2494 break;
2495 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002496 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002497 if (!CompTy.isNull())
2498 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2499 break;
2500 case BinaryOperator::ShlAssign:
2501 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002502 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002503 if (!CompTy.isNull())
2504 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2505 break;
2506 case BinaryOperator::AndAssign:
2507 case BinaryOperator::XorAssign:
2508 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002509 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002510 if (!CompTy.isNull())
2511 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2512 break;
2513 case BinaryOperator::Comma:
2514 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
2515 break;
2516 }
2517 if (ResultTy.isNull())
2518 return true;
2519 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00002520 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002521 else
Chris Lattnerf420df12007-08-28 18:36:55 +00002522 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002523}
2524
2525// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002526Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00002527 ExprTy *input) {
2528 Expr *Input = (Expr*)input;
2529 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2530 QualType resultType;
2531 switch (Opc) {
2532 default:
2533 assert(0 && "Unimplemented unary expr!");
2534 case UnaryOperator::PreInc:
2535 case UnaryOperator::PreDec:
2536 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
2537 break;
2538 case UnaryOperator::AddrOf:
2539 resultType = CheckAddressOfOperand(Input, OpLoc);
2540 break;
2541 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00002542 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00002543 resultType = CheckIndirectionOperand(Input, OpLoc);
2544 break;
2545 case UnaryOperator::Plus:
2546 case UnaryOperator::Minus:
2547 UsualUnaryConversions(Input);
2548 resultType = Input->getType();
2549 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
2550 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2551 resultType.getAsString());
2552 break;
2553 case UnaryOperator::Not: // bitwise complement
2554 UsualUnaryConversions(Input);
2555 resultType = Input->getType();
Chris Lattnerbd695022008-07-25 23:52:49 +00002556 // C99 6.5.3.3p1. We allow complex int and float as a GCC extension.
2557 if (resultType->isComplexType() || resultType->isComplexIntegerType())
2558 // C99 does not support '~' for complex conjugation.
2559 Diag(OpLoc, diag::ext_integer_complement_complex,
2560 resultType.getAsString(), Input->getSourceRange());
2561 else if (!resultType->isIntegerType())
2562 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2563 resultType.getAsString(), Input->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00002564 break;
2565 case UnaryOperator::LNot: // logical negation
2566 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
2567 DefaultFunctionArrayConversion(Input);
2568 resultType = Input->getType();
2569 if (!resultType->isScalarType()) // C99 6.5.3.3p1
2570 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2571 resultType.getAsString());
2572 // LNot always has type int. C99 6.5.3.3p5.
2573 resultType = Context.IntTy;
2574 break;
2575 case UnaryOperator::SizeOf:
Chris Lattnerf814d882008-07-25 21:45:37 +00002576 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc,
2577 Input->getSourceRange(), true);
Chris Lattner4b009652007-07-25 00:24:17 +00002578 break;
2579 case UnaryOperator::AlignOf:
Chris Lattnerf814d882008-07-25 21:45:37 +00002580 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc,
2581 Input->getSourceRange(), false);
Chris Lattner4b009652007-07-25 00:24:17 +00002582 break;
Chris Lattner03931a72007-08-24 21:16:53 +00002583 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00002584 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00002585 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00002586 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002587 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00002588 resultType = Input->getType();
2589 break;
2590 }
2591 if (resultType.isNull())
2592 return true;
2593 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2594}
2595
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002596/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2597Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00002598 SourceLocation LabLoc,
2599 IdentifierInfo *LabelII) {
2600 // Look up the record for this label identifier.
2601 LabelStmt *&LabelDecl = LabelMap[LabelII];
2602
Daniel Dunbar879788d2008-08-04 16:51:22 +00002603 // If we haven't seen this label yet, create a forward reference. It
2604 // will be validated and/or cleaned up in ActOnFinishFunctionBody.
Chris Lattner4b009652007-07-25 00:24:17 +00002605 if (LabelDecl == 0)
2606 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2607
2608 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00002609 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2610 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00002611}
2612
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002613Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00002614 SourceLocation RPLoc) { // "({..})"
2615 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2616 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2617 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2618
2619 // FIXME: there are a variety of strange constraints to enforce here, for
2620 // example, it is not possible to goto into a stmt expression apparently.
2621 // More semantic analysis is needed.
2622
2623 // FIXME: the last statement in the compount stmt has its value used. We
2624 // should not warn about it being unused.
2625
2626 // If there are sub stmts in the compound stmt, take the type of the last one
2627 // as the type of the stmtexpr.
2628 QualType Ty = Context.VoidTy;
2629
Chris Lattner200964f2008-07-26 19:51:01 +00002630 if (!Compound->body_empty()) {
2631 Stmt *LastStmt = Compound->body_back();
2632 // If LastStmt is a label, skip down through into the body.
2633 while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt))
2634 LastStmt = Label->getSubStmt();
2635
2636 if (Expr *LastExpr = dyn_cast<Expr>(LastStmt))
Chris Lattner4b009652007-07-25 00:24:17 +00002637 Ty = LastExpr->getType();
Chris Lattner200964f2008-07-26 19:51:01 +00002638 }
Chris Lattner4b009652007-07-25 00:24:17 +00002639
2640 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2641}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002642
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002643Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002644 SourceLocation TypeLoc,
2645 TypeTy *argty,
2646 OffsetOfComponent *CompPtr,
2647 unsigned NumComponents,
2648 SourceLocation RPLoc) {
2649 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2650 assert(!ArgTy.isNull() && "Missing type argument!");
2651
2652 // We must have at least one component that refers to the type, and the first
2653 // one is known to be a field designator. Verify that the ArgTy represents
2654 // a struct/union/class.
2655 if (!ArgTy->isRecordType())
2656 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2657
2658 // Otherwise, create a compound literal expression as the base, and
2659 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002660 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002661
Chris Lattnerb37522e2007-08-31 21:49:13 +00002662 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2663 // GCC extension, diagnose them.
2664 if (NumComponents != 1)
2665 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2666 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2667
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002668 for (unsigned i = 0; i != NumComponents; ++i) {
2669 const OffsetOfComponent &OC = CompPtr[i];
2670 if (OC.isBrackets) {
2671 // Offset of an array sub-field. TODO: Should we allow vector elements?
Chris Lattnera1923f62008-08-04 07:31:14 +00002672 const ArrayType *AT = Context.getAsArrayType(Res->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002673 if (!AT) {
2674 delete Res;
2675 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2676 Res->getType().getAsString());
2677 }
2678
Chris Lattner2af6a802007-08-30 17:59:59 +00002679 // FIXME: C++: Verify that operator[] isn't overloaded.
2680
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002681 // C99 6.5.2.1p1
2682 Expr *Idx = static_cast<Expr*>(OC.U.E);
2683 if (!Idx->getType()->isIntegerType())
2684 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2685 Idx->getSourceRange());
2686
2687 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2688 continue;
2689 }
2690
2691 const RecordType *RC = Res->getType()->getAsRecordType();
2692 if (!RC) {
2693 delete Res;
2694 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2695 Res->getType().getAsString());
2696 }
2697
2698 // Get the decl corresponding to this.
2699 RecordDecl *RD = RC->getDecl();
2700 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2701 if (!MemberDecl)
2702 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2703 OC.U.IdentInfo->getName(),
2704 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002705
2706 // FIXME: C++: Verify that MemberDecl isn't a static field.
2707 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002708 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2709 // matter here.
2710 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002711 }
2712
2713 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2714 BuiltinLoc);
2715}
2716
2717
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002718Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002719 TypeTy *arg1, TypeTy *arg2,
2720 SourceLocation RPLoc) {
2721 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2722 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2723
2724 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2725
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002726 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002727}
2728
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002729Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002730 ExprTy *expr1, ExprTy *expr2,
2731 SourceLocation RPLoc) {
2732 Expr *CondExpr = static_cast<Expr*>(cond);
2733 Expr *LHSExpr = static_cast<Expr*>(expr1);
2734 Expr *RHSExpr = static_cast<Expr*>(expr2);
2735
2736 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2737
2738 // The conditional expression is required to be a constant expression.
2739 llvm::APSInt condEval(32);
2740 SourceLocation ExpLoc;
2741 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2742 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2743 CondExpr->getSourceRange());
2744
2745 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2746 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2747 RHSExpr->getType();
2748 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2749}
2750
Steve Naroff52a81c02008-09-03 18:15:37 +00002751//===----------------------------------------------------------------------===//
2752// Clang Extensions.
2753//===----------------------------------------------------------------------===//
2754
2755/// ActOnBlockStart - This callback is invoked when a block literal is started.
2756void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope,
2757 Declarator &ParamInfo) {
2758 // Analyze block parameters.
2759 BlockSemaInfo *BSI = new BlockSemaInfo();
2760
2761 // Add BSI to CurBlock.
2762 BSI->PrevBlockInfo = CurBlock;
2763 CurBlock = BSI;
2764
2765 BSI->ReturnType = 0;
2766 BSI->TheScope = BlockScope;
2767
2768 // Analyze arguments to block.
2769 assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function &&
2770 "Not a function declarator!");
2771 DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun;
2772
2773 BSI->hasPrototype = FTI.hasPrototype;
2774 BSI->isVariadic = true;
2775
2776 // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes
2777 // no arguments, not a function that takes a single void argument.
2778 if (FTI.hasPrototype &&
2779 FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 &&
2780 (!((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType().getCVRQualifiers() &&
2781 ((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType()->isVoidType())) {
2782 // empty arg list, don't push any params.
2783 BSI->isVariadic = false;
2784 } else if (FTI.hasPrototype) {
2785 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
2786 BSI->Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param);
2787 BSI->isVariadic = FTI.isVariadic;
2788 }
2789}
2790
2791/// ActOnBlockError - If there is an error parsing a block, this callback
2792/// is invoked to pop the information about the block from the action impl.
2793void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) {
2794 // Ensure that CurBlock is deleted.
2795 llvm::OwningPtr<BlockSemaInfo> CC(CurBlock);
2796
2797 // Pop off CurBlock, handle nested blocks.
2798 CurBlock = CurBlock->PrevBlockInfo;
2799
2800 // FIXME: Delete the ParmVarDecl objects as well???
2801
2802}
2803
2804/// ActOnBlockStmtExpr - This is called when the body of a block statement
2805/// literal was successfully completed. ^(int x){...}
2806Sema::ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *body,
2807 Scope *CurScope) {
2808 // Ensure that CurBlock is deleted.
2809 llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock);
2810 llvm::OwningPtr<CompoundStmt> Body(static_cast<CompoundStmt*>(body));
2811
2812 // Pop off CurBlock, handle nested blocks.
2813 CurBlock = CurBlock->PrevBlockInfo;
2814
2815 QualType RetTy = Context.VoidTy;
2816 if (BSI->ReturnType)
2817 RetTy = QualType(BSI->ReturnType, 0);
2818
2819 llvm::SmallVector<QualType, 8> ArgTypes;
2820 for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i)
2821 ArgTypes.push_back(BSI->Params[i]->getType());
2822
2823 QualType BlockTy;
2824 if (!BSI->hasPrototype)
2825 BlockTy = Context.getFunctionTypeNoProto(RetTy);
2826 else
2827 BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(),
2828 BSI->isVariadic);
2829
2830 BlockTy = Context.getBlockPointerType(BlockTy);
2831 return new BlockStmtExpr(CaretLoc, BlockTy,
2832 &BSI->Params[0], BSI->Params.size(), Body.take());
2833}
2834
2835/// ActOnBlockExprExpr - This is called when the body of a block
2836/// expression literal was successfully completed. ^(int x)[foo bar: x]
2837Sema::ExprResult Sema::ActOnBlockExprExpr(SourceLocation CaretLoc, ExprTy *body,
2838 Scope *CurScope) {
2839 // Ensure that CurBlock is deleted.
2840 llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock);
2841 llvm::OwningPtr<Expr> Body(static_cast<Expr*>(body));
2842
2843 // Pop off CurBlock, handle nested blocks.
2844 CurBlock = CurBlock->PrevBlockInfo;
2845
2846 if (BSI->ReturnType) {
2847 Diag(CaretLoc, diag::err_return_in_block_expression);
2848 return true;
2849 }
2850
2851 QualType RetTy = Body->getType();
2852
2853 llvm::SmallVector<QualType, 8> ArgTypes;
2854 for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i)
2855 ArgTypes.push_back(BSI->Params[i]->getType());
2856
2857 QualType BlockTy;
2858 if (!BSI->hasPrototype)
2859 BlockTy = Context.getFunctionTypeNoProto(RetTy);
2860 else
2861 BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(),
2862 BSI->isVariadic);
2863
2864 BlockTy = Context.getBlockPointerType(BlockTy);
2865 return new BlockExprExpr(CaretLoc, BlockTy,
2866 &BSI->Params[0], BSI->Params.size(), Body.take());
2867}
2868
Nate Begemanbd881ef2008-01-30 20:50:20 +00002869/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002870/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002871/// The number of arguments has already been validated to match the number of
2872/// arguments in FnType.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +00002873static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType,
2874 ASTContext &Context) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002875 unsigned NumParams = FnType->getNumArgs();
Nate Begeman778fd3b2008-04-18 23:35:14 +00002876 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +00002877 QualType ExprTy = Context.getCanonicalType(Args[i]->getType());
2878 QualType ParmTy = Context.getCanonicalType(FnType->getArgType(i));
Nate Begeman778fd3b2008-04-18 23:35:14 +00002879
2880 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002881 return false;
Nate Begeman778fd3b2008-04-18 23:35:14 +00002882 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002883 return true;
2884}
2885
2886Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2887 SourceLocation *CommaLocs,
2888 SourceLocation BuiltinLoc,
2889 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002890 // __builtin_overload requires at least 2 arguments
2891 if (NumArgs < 2)
2892 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2893 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002894
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002895 // The first argument is required to be a constant expression. It tells us
2896 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002897 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002898 Expr *NParamsExpr = Args[0];
2899 llvm::APSInt constEval(32);
2900 SourceLocation ExpLoc;
2901 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2902 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2903 NParamsExpr->getSourceRange());
2904
2905 // Verify that the number of parameters is > 0
2906 unsigned NumParams = constEval.getZExtValue();
2907 if (NumParams == 0)
2908 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2909 NParamsExpr->getSourceRange());
2910 // Verify that we have at least 1 + NumParams arguments to the builtin.
2911 if ((NumParams + 1) > NumArgs)
2912 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2913 SourceRange(BuiltinLoc, RParenLoc));
2914
2915 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002916 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002917 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002918 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2919 // UsualUnaryConversions will convert the function DeclRefExpr into a
2920 // pointer to function.
2921 Expr *Fn = UsualUnaryConversions(Args[i]);
Chris Lattnerd5a56aa2008-07-26 22:17:49 +00002922 const FunctionTypeProto *FnType = 0;
2923 if (const PointerType *PT = Fn->getType()->getAsPointerType())
2924 FnType = PT->getPointeeType()->getAsFunctionTypeProto();
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002925
2926 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2927 // parameters, and the number of parameters must match the value passed to
2928 // the builtin.
2929 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002930 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2931 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002932
2933 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002934 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002935 // If they match, return a new OverloadExpr.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +00002936 if (ExprsMatchFnType(Args+1, FnType, Context)) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002937 if (OE)
2938 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2939 OE->getFn()->getSourceRange());
2940 // Remember our match, and continue processing the remaining arguments
2941 // to catch any errors.
2942 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2943 BuiltinLoc, RParenLoc);
2944 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002945 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002946 // Return the newly created OverloadExpr node, if we succeded in matching
2947 // exactly one of the candidate functions.
2948 if (OE)
2949 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002950
2951 // If we didn't find a matching function Expr in the __builtin_overload list
2952 // the return an error.
2953 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002954 for (unsigned i = 0; i != NumParams; ++i) {
2955 if (i != 0) typeNames += ", ";
2956 typeNames += Args[i+1]->getType().getAsString();
2957 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002958
2959 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2960 SourceRange(BuiltinLoc, RParenLoc));
2961}
2962
Anders Carlsson36760332007-10-15 20:28:48 +00002963Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2964 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002965 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002966 Expr *E = static_cast<Expr*>(expr);
2967 QualType T = QualType::getFromOpaquePtr(type);
2968
2969 InitBuiltinVaListType();
Eli Friedmandd2b9af2008-08-09 23:32:40 +00002970
2971 // Get the va_list type
2972 QualType VaListType = Context.getBuiltinVaListType();
2973 // Deal with implicit array decay; for example, on x86-64,
2974 // va_list is an array, but it's supposed to decay to
2975 // a pointer for va_arg.
2976 if (VaListType->isArrayType())
2977 VaListType = Context.getArrayDecayedType(VaListType);
Eli Friedman8754e5b2008-08-20 22:17:17 +00002978 // Make sure the input expression also decays appropriately.
2979 UsualUnaryConversions(E);
Eli Friedmandd2b9af2008-08-09 23:32:40 +00002980
2981 if (CheckAssignmentConstraints(VaListType, E->getType()) != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002982 return Diag(E->getLocStart(),
2983 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2984 E->getType().getAsString(),
2985 E->getSourceRange());
2986
2987 // FIXME: Warn if a non-POD type is passed in.
2988
2989 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2990}
2991
Chris Lattner005ed752008-01-04 18:04:52 +00002992bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2993 SourceLocation Loc,
2994 QualType DstType, QualType SrcType,
2995 Expr *SrcExpr, const char *Flavor) {
2996 // Decode the result (notice that AST's are still created for extensions).
2997 bool isInvalid = false;
2998 unsigned DiagKind;
2999 switch (ConvTy) {
3000 default: assert(0 && "Unknown conversion type");
3001 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00003002 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00003003 DiagKind = diag::ext_typecheck_convert_pointer_int;
3004 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00003005 case IntToPointer:
3006 DiagKind = diag::ext_typecheck_convert_int_pointer;
3007 break;
Chris Lattner005ed752008-01-04 18:04:52 +00003008 case IncompatiblePointer:
3009 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
3010 break;
3011 case FunctionVoidPointer:
3012 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
3013 break;
3014 case CompatiblePointerDiscardsQualifiers:
3015 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
3016 break;
Steve Naroff3454b6c2008-09-04 15:10:53 +00003017 case IntToBlockPointer:
3018 DiagKind = diag::err_int_to_block_pointer;
3019 break;
3020 case IncompatibleBlockPointer:
3021 DiagKind = diag::err_typecheck_convert_incompatible_block_pointer;
3022 break;
3023 case BlockVoidPointer:
3024 DiagKind = diag::ext_typecheck_convert_pointer_void_block;
3025 break;
Chris Lattner005ed752008-01-04 18:04:52 +00003026 case Incompatible:
3027 DiagKind = diag::err_typecheck_convert_incompatible;
3028 isInvalid = true;
3029 break;
3030 }
3031
3032 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
3033 SrcExpr->getSourceRange());
3034 return isInvalid;
3035}