blob: 2b63c10d98e0eba118e834b8205f9467520fa258 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek30c66752007-11-25 00:58:00 +000015#include "SemaUtil.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/AST/Expr.h"
Steve Naroffc39ca262007-09-18 23:55:05 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Lex/Preprocessor.h"
20#include "clang/Lex/LiteralSupport.h"
21#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner83bd5eb2007-12-28 05:29:59 +000023#include "llvm/ADT/OwningPtr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
Steve Naroff87d58b42007-09-16 03:34:24 +000028/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +000029/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
30/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
31/// multiple tokens. However, the common case is that StringToks points to one
32/// string.
33///
34Action::ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +000035Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner4b009652007-07-25 00:24:17 +000036 assert(NumStringToks && "Must have at least one string!");
37
38 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
39 if (Literal.hadError)
40 return ExprResult(true);
41
42 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
43 for (unsigned i = 0; i != NumStringToks; ++i)
44 StringTokLocs.push_back(StringToks[i].getLocation());
45
46 // FIXME: handle wchar_t
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000047 QualType t;
48
49 if (Literal.Pascal)
50 t = Context.getPointerType(Context.UnsignedCharTy);
51 else
52 t = Context.getPointerType(Context.CharTy);
53
54 if (Literal.Pascal && Literal.GetStringLength() > 256)
55 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
56 SourceRange(StringToks[0].getLocation(),
57 StringToks[NumStringToks-1].getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +000058
59 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
60 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000061 Literal.AnyWide, t,
62 StringToks[0].getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +000063 StringToks[NumStringToks-1].getLocation());
64}
65
66
Steve Naroff0acc9c92007-09-15 18:49:24 +000067/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattner4b009652007-07-25 00:24:17 +000068/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
69/// identifier is used in an function call context.
Steve Naroff0acc9c92007-09-15 18:49:24 +000070Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +000071 IdentifierInfo &II,
72 bool HasTrailingLParen) {
73 // Could be enum-constant or decl.
Steve Narofff0c31dd2007-09-16 16:16:00 +000074 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner4b009652007-07-25 00:24:17 +000075 if (D == 0) {
76 // Otherwise, this could be an implicitly declared function reference (legal
77 // in C90, extension in C99).
78 if (HasTrailingLParen &&
79 // Not in C++.
80 !getLangOptions().CPlusPlus)
81 D = ImplicitlyDefineFunction(Loc, II, S);
82 else {
Steve Naroff5eb2a4a2007-11-12 14:29:37 +000083 if (CurMethodDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +000084 ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface();
85 ObjCInterfaceDecl *clsDeclared;
86 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, clsDeclared)) {
Steve Naroff6b759ce2007-11-15 02:58:25 +000087 IdentifierInfo &II = Context.Idents.get("self");
88 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
89 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
90 static_cast<Expr*>(SelfExpr.Val), true, true);
91 }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +000092 }
Chris Lattner4b009652007-07-25 00:24:17 +000093 // If this name wasn't predeclared and if this is not a function call,
94 // diagnose the problem.
95 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
96 }
97 }
Steve Naroff91b03f72007-08-28 03:03:08 +000098 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroffcae537d2007-08-28 18:45:29 +000099 // Only create DeclRefExpr's for valid Decl's.
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000100 if (VD->isInvalidDecl())
Steve Naroff91b03f72007-08-28 03:03:08 +0000101 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000102 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff91b03f72007-08-28 03:03:08 +0000103 }
Chris Lattner4b009652007-07-25 00:24:17 +0000104 if (isa<TypedefDecl>(D))
105 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek42730c52008-01-07 19:49:32 +0000106 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian3102df92007-12-05 18:16:33 +0000107 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000108
109 assert(0 && "Invalid decl");
110 abort();
111}
112
Steve Naroff87d58b42007-09-16 03:34:24 +0000113Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000114 tok::TokenKind Kind) {
115 PreDefinedExpr::IdentType IT;
116
117 switch (Kind) {
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000118 default: assert(0 && "Unknown simple primary expr!");
119 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
120 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
121 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000122 }
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000123
124 // Verify that this is in a function context.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000125 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000126 return Diag(Loc, diag::err_predef_outside_function);
Chris Lattner4b009652007-07-25 00:24:17 +0000127
Chris Lattner7e637512008-01-12 08:14:25 +0000128 // Pre-defined identifiers are of type char[x], where x is the length of the
129 // string.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000130 unsigned Length;
131 if (CurFunctionDecl)
132 Length = CurFunctionDecl->getIdentifier()->getLength();
133 else
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000134 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000135
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000136 llvm::APInt LengthI(32, Length + 1);
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000137 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000138 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattner7e637512008-01-12 08:14:25 +0000139 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattner4b009652007-07-25 00:24:17 +0000140}
141
Steve Naroff87d58b42007-09-16 03:34:24 +0000142Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000143 llvm::SmallString<16> CharBuffer;
144 CharBuffer.resize(Tok.getLength());
145 const char *ThisTokBegin = &CharBuffer[0];
146 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
147
148 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
149 Tok.getLocation(), PP);
150 if (Literal.hadError())
151 return ExprResult(true);
152 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
153 Tok.getLocation());
154}
155
Steve Naroff87d58b42007-09-16 03:34:24 +0000156Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000157 // fast path for a single digit (which is quite common). A single digit
158 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
159 if (Tok.getLength() == 1) {
160 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
161
Chris Lattner3496d522007-09-04 02:45:27 +0000162 unsigned IntSize = static_cast<unsigned>(
163 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000164 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
165 Context.IntTy,
166 Tok.getLocation()));
167 }
168 llvm::SmallString<512> IntegerBuffer;
169 IntegerBuffer.resize(Tok.getLength());
170 const char *ThisTokBegin = &IntegerBuffer[0];
171
172 // Get the spelling of the token, which eliminates trigraphs, etc.
173 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
174 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
175 Tok.getLocation(), PP);
176 if (Literal.hadError)
177 return ExprResult(true);
178
Chris Lattner1de66eb2007-08-26 03:42:43 +0000179 Expr *Res;
180
181 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000182 QualType Ty;
183 const llvm::fltSemantics *Format;
184 uint64_t Size; unsigned Align;
185
186 if (Literal.isFloat) {
187 Ty = Context.FloatTy;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000188 Context.Target.getFloatInfo(Size, Align, Format,
189 Context.getFullLoc(Tok.getLocation()));
190
Chris Lattner858eece2007-09-22 18:29:59 +0000191 } else if (Literal.isLong) {
192 Ty = Context.LongDoubleTy;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000193 Context.Target.getLongDoubleInfo(Size, Align, Format,
194 Context.getFullLoc(Tok.getLocation()));
Chris Lattner858eece2007-09-22 18:29:59 +0000195 } else {
196 Ty = Context.DoubleTy;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000197 Context.Target.getDoubleInfo(Size, Align, Format,
198 Context.getFullLoc(Tok.getLocation()));
Chris Lattner858eece2007-09-22 18:29:59 +0000199 }
200
Ted Kremenekddedbe22007-11-29 00:56:49 +0000201 // isExact will be set by GetFloatValue().
202 bool isExact = false;
203
204 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
205 Ty, Tok.getLocation());
206
Chris Lattner1de66eb2007-08-26 03:42:43 +0000207 } else if (!Literal.isIntegerLiteral()) {
208 return ExprResult(true);
209 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000210 QualType t;
211
Neil Booth7421e9c2007-08-29 22:00:19 +0000212 // long long is a C99 feature.
213 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000214 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000215 Diag(Tok.getLocation(), diag::ext_longlong);
216
Chris Lattner4b009652007-07-25 00:24:17 +0000217 // Get the value in the widest-possible width.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000218 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(
219 Context.getFullLoc(Tok.getLocation())), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000220
221 if (Literal.GetIntegerValue(ResultVal)) {
222 // If this value didn't fit into uintmax_t, warn and force to ull.
223 Diag(Tok.getLocation(), diag::warn_integer_too_large);
224 t = Context.UnsignedLongLongTy;
225 assert(Context.getTypeSize(t, Tok.getLocation()) ==
226 ResultVal.getBitWidth() && "long long is not intmax_t?");
227 } else {
228 // If this value fits into a ULL, try to figure out what else it fits into
229 // according to the rules of C99 6.4.4.1p5.
230
231 // Octal, Hexadecimal, and integers with a U suffix are allowed to
232 // be an unsigned int.
233 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
234
235 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner98540b62007-08-23 21:58:08 +0000236 if (!Literal.isLong && !Literal.isLongLong) {
237 // Are int/unsigned possibilities?
Chris Lattner3496d522007-09-04 02:45:27 +0000238 unsigned IntSize = static_cast<unsigned>(
239 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000240 // Does it fit in a unsigned int?
241 if (ResultVal.isIntN(IntSize)) {
242 // Does it fit in a signed int?
243 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
244 t = Context.IntTy;
245 else if (AllowUnsigned)
246 t = Context.UnsignedIntTy;
247 }
248
249 if (!t.isNull())
250 ResultVal.trunc(IntSize);
251 }
252
253 // Are long/unsigned long possibilities?
254 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner3496d522007-09-04 02:45:27 +0000255 unsigned LongSize = static_cast<unsigned>(
256 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000257
258 // Does it fit in a unsigned long?
259 if (ResultVal.isIntN(LongSize)) {
260 // Does it fit in a signed long?
261 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
262 t = Context.LongTy;
263 else if (AllowUnsigned)
264 t = Context.UnsignedLongTy;
265 }
266 if (!t.isNull())
267 ResultVal.trunc(LongSize);
268 }
269
270 // Finally, check long long if needed.
271 if (t.isNull()) {
Chris Lattner3496d522007-09-04 02:45:27 +0000272 unsigned LongLongSize = static_cast<unsigned>(
273 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000274
275 // Does it fit in a unsigned long long?
276 if (ResultVal.isIntN(LongLongSize)) {
277 // Does it fit in a signed long long?
278 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
279 t = Context.LongLongTy;
280 else if (AllowUnsigned)
281 t = Context.UnsignedLongLongTy;
282 }
283 }
284
285 // If we still couldn't decide a type, we probably have something that
286 // does not fit in a signed long long, but has no U suffix.
287 if (t.isNull()) {
288 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
289 t = Context.UnsignedLongLongTy;
290 }
291 }
292
Chris Lattner1de66eb2007-08-26 03:42:43 +0000293 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000294 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000295
296 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
297 if (Literal.isImaginary)
298 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
299
300 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000301}
302
Steve Naroff87d58b42007-09-16 03:34:24 +0000303Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000304 ExprTy *Val) {
305 Expr *e = (Expr *)Val;
Steve Naroff87d58b42007-09-16 03:34:24 +0000306 assert((e != 0) && "ActOnParenExpr() missing expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000307 return new ParenExpr(L, R, e);
308}
309
310/// The UsualUnaryConversions() function is *not* called by this routine.
311/// See C99 6.3.2.1p[2-4] for more details.
312QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
313 SourceLocation OpLoc, bool isSizeof) {
314 // C99 6.5.3.4p1:
315 if (isa<FunctionType>(exprType) && isSizeof)
316 // alignof(function) is allowed.
317 Diag(OpLoc, diag::ext_sizeof_function_type);
318 else if (exprType->isVoidType())
319 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
320 else if (exprType->isIncompleteType()) {
321 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
322 diag::err_alignof_incomplete_type,
323 exprType.getAsString());
324 return QualType(); // error
325 }
326 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
327 return Context.getSizeType();
328}
329
330Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000331ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000332 SourceLocation LPLoc, TypeTy *Ty,
333 SourceLocation RPLoc) {
334 // If error parsing type, ignore.
335 if (Ty == 0) return true;
336
337 // Verify that this is a valid expression.
338 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
339
340 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
341
342 if (resultType.isNull())
343 return true;
344 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
345}
346
Chris Lattner5110ad52007-08-24 21:41:10 +0000347QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000348 DefaultFunctionArrayConversion(V);
349
Chris Lattnera16e42d2007-08-26 05:39:26 +0000350 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000351 if (const ComplexType *CT = V->getType()->getAsComplexType())
352 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000353
354 // Otherwise they pass through real integer and floating point types here.
355 if (V->getType()->isArithmeticType())
356 return V->getType();
357
358 // Reject anything else.
359 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
360 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000361}
362
363
Chris Lattner4b009652007-07-25 00:24:17 +0000364
Steve Naroff87d58b42007-09-16 03:34:24 +0000365Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000366 tok::TokenKind Kind,
367 ExprTy *Input) {
368 UnaryOperator::Opcode Opc;
369 switch (Kind) {
370 default: assert(0 && "Unknown unary op!");
371 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
372 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
373 }
374 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
375 if (result.isNull())
376 return true;
377 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
378}
379
380Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000381ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000382 ExprTy *Idx, SourceLocation RLoc) {
383 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
384
385 // Perform default conversions.
386 DefaultFunctionArrayConversion(LHSExp);
387 DefaultFunctionArrayConversion(RHSExp);
388
389 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
390
391 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000392 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000393 // in the subscript position. As a result, we need to derive the array base
394 // and index from the expression types.
395 Expr *BaseExpr, *IndexExpr;
396 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000397 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000398 BaseExpr = LHSExp;
399 IndexExpr = RHSExp;
400 // FIXME: need to deal with const...
401 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000402 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000403 // Handle the uncommon case of "123[Ptr]".
404 BaseExpr = RHSExp;
405 IndexExpr = LHSExp;
406 // FIXME: need to deal with const...
407 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000408 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
409 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000410 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000411
412 // Component access limited to variables (reject vec4.rg[1]).
413 if (!isa<DeclRefExpr>(BaseExpr))
414 return Diag(LLoc, diag::err_ocuvector_component_access,
415 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000416 // FIXME: need to deal with const...
417 ResultType = VTy->getElementType();
418 } else {
419 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
420 RHSExp->getSourceRange());
421 }
422 // C99 6.5.2.1p1
423 if (!IndexExpr->getType()->isIntegerType())
424 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
425 IndexExpr->getSourceRange());
426
427 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
428 // the following check catches trying to index a pointer to a function (e.g.
429 // void (*)(int)). Functions are not objects in C99.
430 if (!ResultType->isObjectType())
431 return Diag(BaseExpr->getLocStart(),
432 diag::err_typecheck_subscript_not_object,
433 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
434
435 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
436}
437
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000438QualType Sema::
439CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
440 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattnere35a1042007-07-31 19:29:30 +0000441 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000442
443 // The vector accessor can't exceed the number of elements.
444 const char *compStr = CompName.getName();
445 if (strlen(compStr) > vecType->getNumElements()) {
446 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
447 baseType.getAsString(), SourceRange(CompLoc));
448 return QualType();
449 }
450 // The component names must come from the same set.
Chris Lattner9096b792007-08-02 22:33:49 +0000451 if (vecType->getPointAccessorIdx(*compStr) != -1) {
452 do
453 compStr++;
454 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
455 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
456 do
457 compStr++;
458 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
459 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
460 do
461 compStr++;
462 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
463 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000464
465 if (*compStr) {
466 // We didn't get to the end of the string. This means the component names
467 // didn't come from the same set *or* we encountered an illegal name.
468 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
469 std::string(compStr,compStr+1), SourceRange(CompLoc));
470 return QualType();
471 }
472 // Each component accessor can't exceed the vector type.
473 compStr = CompName.getName();
474 while (*compStr) {
475 if (vecType->isAccessorWithinNumElements(*compStr))
476 compStr++;
477 else
478 break;
479 }
480 if (*compStr) {
481 // We didn't get to the end of the string. This means a component accessor
482 // exceeds the number of elements in the vector.
483 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
484 baseType.getAsString(), SourceRange(CompLoc));
485 return QualType();
486 }
487 // The component accessor looks fine - now we need to compute the actual type.
488 // The vector type is implied by the component accessor. For example,
489 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
490 unsigned CompSize = strlen(CompName.getName());
491 if (CompSize == 1)
492 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000493
494 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
495 // Now look up the TypeDefDecl from the vector type. Without this,
496 // diagostics look bad. We want OCU vector types to appear built-in.
497 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
498 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
499 return Context.getTypedefType(OCUVectorDecls[i]);
500 }
501 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000502}
503
Chris Lattner4b009652007-07-25 00:24:17 +0000504Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000505ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000506 tok::TokenKind OpKind, SourceLocation MemberLoc,
507 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000508 Expr *BaseExpr = static_cast<Expr *>(Base);
509 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000510
511 // Perform default conversions.
512 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000513
Steve Naroff2cb66382007-07-26 03:11:44 +0000514 QualType BaseType = BaseExpr->getType();
515 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000516
Chris Lattner4b009652007-07-25 00:24:17 +0000517 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000518 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000519 BaseType = PT->getPointeeType();
520 else
521 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
522 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000523 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000524 // The base type is either a record or an OCUVectorType.
Chris Lattnere35a1042007-07-31 19:29:30 +0000525 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000526 RecordDecl *RDecl = RTy->getDecl();
527 if (RTy->isIncompleteType())
528 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
529 BaseExpr->getSourceRange());
530 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000531 FieldDecl *MemberDecl = RDecl->getMember(&Member);
532 if (!MemberDecl)
Steve Naroff2cb66382007-07-26 03:11:44 +0000533 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
534 SourceRange(MemberLoc));
Eli Friedman76b49832008-02-06 22:48:16 +0000535
536 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000537 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000538 QualType MemberType = MemberDecl->getType();
539 unsigned combinedQualifiers =
540 MemberType.getQualifiers() | BaseType.getQualifiers();
541 MemberType = MemberType.getQualifiedType(combinedQualifiers);
542
543 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
544 MemberLoc, MemberType);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000545 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff89345522007-08-03 22:40:33 +0000546 // Component access limited to variables (reject vec4.rg.g).
547 if (!isa<DeclRefExpr>(BaseExpr))
548 return Diag(OpLoc, diag::err_ocuvector_component_access,
549 SourceRange(MemberLoc));
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000550 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
551 if (ret.isNull())
552 return true;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000553 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000554 } else if (BaseType->isObjCInterfaceType()) {
555 ObjCInterfaceDecl *IFace;
556 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
557 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000558 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000559 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
560 ObjCInterfaceDecl *clsDeclared;
561 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000562 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
563 OpKind==tok::arrow);
564 }
565 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
566 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000567}
568
Steve Naroff87d58b42007-09-16 03:34:24 +0000569/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000570/// This provides the location of the left/right parens and a list of comma
571/// locations.
572Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000573ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000574 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +0000575 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
576 Expr *Fn = static_cast<Expr *>(fn);
577 Expr **Args = reinterpret_cast<Expr**>(args);
578 assert(Fn && "no function call expression");
579
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000580 // Make the call expr early, before semantic checks. This guarantees cleanup
581 // of arguments and function on error.
582 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
583 Context.BoolTy, RParenLoc));
584
585 // Promote the function operand.
586 TheCall->setCallee(UsualUnaryConversions(Fn));
587
Chris Lattner4b009652007-07-25 00:24:17 +0000588 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
589 // type pointer to function".
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000590 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000591 if (PT == 0)
592 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
593 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000594 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
595 if (FuncT == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000596 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
597 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000598
599 // We know the result type of the call, set it.
600 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +0000601
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000602 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000603 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
604 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000605 unsigned NumArgsInProto = Proto->getNumArgs();
606 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +0000607
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000608 // If too few arguments are available, don't make the call.
609 if (NumArgs < NumArgsInProto)
610 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
611 Fn->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000612
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000613 // If too many are passed and not variadic, error on the extras and drop
614 // them.
615 if (NumArgs > NumArgsInProto) {
616 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000617 Diag(Args[NumArgsInProto]->getLocStart(),
618 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
619 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000620 Args[NumArgs-1]->getLocEnd()));
621 // This deletes the extra arguments.
622 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +0000623 }
624 NumArgsToCheck = NumArgsInProto;
625 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000626
Chris Lattner4b009652007-07-25 00:24:17 +0000627 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000628 for (unsigned i = 0; i != NumArgsToCheck; i++) {
629 Expr *Arg = Args[i];
Chris Lattner005ed752008-01-04 18:04:52 +0000630 QualType ProtoArgType = Proto->getArgType(i);
631 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000632
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000633 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +0000634 AssignConvertType ConvTy =
635 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000636 TheCall->setArg(i, Arg);
637
Chris Lattner005ed752008-01-04 18:04:52 +0000638 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
639 ArgType, Arg, "passing"))
640 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000641 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000642
643 // If this is a variadic call, handle args passed through "...".
644 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +0000645 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000646 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
647 Expr *Arg = Args[i];
648 DefaultArgumentPromotion(Arg);
649 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000650 }
Steve Naroffdb65e052007-08-28 23:30:39 +0000651 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000652 } else {
653 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
654
Steve Naroffdb65e052007-08-28 23:30:39 +0000655 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000656 for (unsigned i = 0; i != NumArgs; i++) {
657 Expr *Arg = Args[i];
658 DefaultArgumentPromotion(Arg);
659 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000660 }
Chris Lattner4b009652007-07-25 00:24:17 +0000661 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000662
Chris Lattner2e64c072007-08-10 20:18:51 +0000663 // Do special checking on direct calls to functions.
664 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
665 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
666 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000667 if (CheckFunctionCall(FDecl, TheCall.get()))
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000668 return true;
Chris Lattner2e64c072007-08-10 20:18:51 +0000669
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000670 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +0000671}
672
673Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000674ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000675 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000676 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000677 QualType literalType = QualType::getFromOpaquePtr(Ty);
678 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +0000679 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000680 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +0000681
Steve Naroffcb69fb72007-12-10 22:44:33 +0000682 // FIXME: add more semantic analysis (C99 6.5.2.5).
Steve Narofff0b23542008-01-10 22:15:12 +0000683 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +0000684 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +0000685
686 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
687 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +0000688 if (CheckForConstantInitializer(literalExpr, literalType))
689 return true;
690 }
Steve Naroffbe37fc02008-01-14 18:19:28 +0000691 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000692}
693
694Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000695ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +0000696 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +0000697 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000698
Steve Naroff0acc9c92007-09-15 18:49:24 +0000699 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +0000700 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +0000701
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000702 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
703 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
704 return e;
Chris Lattner4b009652007-07-25 00:24:17 +0000705}
706
Chris Lattnerd1f26b32007-12-20 00:44:32 +0000707bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000708 assert(VectorTy->isVectorType() && "Not a vector type!");
709
710 if (Ty->isVectorType() || Ty->isIntegerType()) {
711 if (Context.getTypeSize(VectorTy, SourceLocation()) !=
712 Context.getTypeSize(Ty, SourceLocation()))
713 return Diag(R.getBegin(),
714 Ty->isVectorType() ?
715 diag::err_invalid_conversion_between_vectors :
716 diag::err_invalid_conversion_between_vector_and_integer,
717 VectorTy.getAsString().c_str(),
718 Ty.getAsString().c_str(), R);
719 } else
720 return Diag(R.getBegin(),
721 diag::err_invalid_conversion_between_vector_and_scalar,
722 VectorTy.getAsString().c_str(),
723 Ty.getAsString().c_str(), R);
724
725 return false;
726}
727
Chris Lattner4b009652007-07-25 00:24:17 +0000728Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000729ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000730 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000731 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000732
733 Expr *castExpr = static_cast<Expr*>(Op);
734 QualType castType = QualType::getFromOpaquePtr(Ty);
735
Steve Naroff68adb482007-08-31 00:32:44 +0000736 UsualUnaryConversions(castExpr);
737
Chris Lattner4b009652007-07-25 00:24:17 +0000738 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
739 // type needs to be scalar.
Chris Lattnerdb526732007-10-29 04:26:44 +0000740 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Narofff459ee52008-01-24 22:55:05 +0000741 if (!castType->isScalarType() && !castType->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +0000742 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
743 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Steve Narofff459ee52008-01-24 22:55:05 +0000744 if (!castExpr->getType()->isScalarType() &&
745 !castExpr->getType()->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +0000746 return Diag(castExpr->getLocStart(),
747 diag::err_typecheck_expect_scalar_operand,
748 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000749
750 if (castExpr->getType()->isVectorType()) {
751 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
752 castExpr->getType(), castType))
753 return true;
754 } else if (castType->isVectorType()) {
755 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
756 castType, castExpr->getType()))
757 return true;
Chris Lattnerdb526732007-10-29 04:26:44 +0000758 }
Chris Lattner4b009652007-07-25 00:24:17 +0000759 }
760 return new CastExpr(castType, castExpr, LParenLoc);
761}
762
Chris Lattner98a425c2007-11-26 01:40:58 +0000763/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
764/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +0000765inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
766 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
767 UsualUnaryConversions(cond);
768 UsualUnaryConversions(lex);
769 UsualUnaryConversions(rex);
770 QualType condT = cond->getType();
771 QualType lexT = lex->getType();
772 QualType rexT = rex->getType();
773
774 // first, check the condition.
775 if (!condT->isScalarType()) { // C99 6.5.15p2
776 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
777 condT.getAsString());
778 return QualType();
779 }
Chris Lattner992ae932008-01-06 22:42:25 +0000780
781 // Now check the two expressions.
782
783 // If both operands have arithmetic type, do the usual arithmetic conversions
784 // to find a common type: C99 6.5.15p3,5.
785 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000786 UsualArithmeticConversions(lex, rex);
787 return lex->getType();
788 }
Chris Lattner992ae932008-01-06 22:42:25 +0000789
790 // If both operands are the same structure or union type, the result is that
791 // type.
Chris Lattner71225142007-07-31 21:27:01 +0000792 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +0000793 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +0000794 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +0000795 // "If both the operands have structure or union type, the result has
796 // that type." This implies that CV qualifiers are dropped.
797 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +0000798 }
Chris Lattner992ae932008-01-06 22:42:25 +0000799
800 // C99 6.5.15p5: "If both operands have void type, the result has void type."
801 if (lexT->isVoidType() && rexT->isVoidType())
802 return lexT.getUnqualifiedType();
Steve Naroff12ebf272008-01-08 01:11:38 +0000803
804 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
805 // the type of the other operand."
806 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000807 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000808 return lexT;
809 }
810 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000811 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000812 return rexT;
813 }
Chris Lattner0ac51632008-01-06 22:50:31 +0000814 // Handle the case where both operands are pointers before we handle null
815 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +0000816 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
817 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
818 // get the "pointed to" types
819 QualType lhptee = LHSPT->getPointeeType();
820 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000821
Chris Lattner71225142007-07-31 21:27:01 +0000822 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
823 if (lhptee->isVoidType() &&
824 (rhptee->isObjectType() || rhptee->isIncompleteType()))
825 return lexT;
826 if (rhptee->isVoidType() &&
827 (lhptee->isObjectType() || lhptee->isIncompleteType()))
828 return rexT;
Chris Lattner4b009652007-07-25 00:24:17 +0000829
Steve Naroff85f0dc52007-10-15 20:41:53 +0000830 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
831 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +0000832 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +0000833 lexT.getAsString(), rexT.getAsString(),
834 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman33284862008-01-30 17:02:03 +0000835 // In this situation, we assume void* type. No especially good
836 // reason, but this is what gcc does, and we do have to pick
837 // to get a consistent AST.
838 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
839 ImpCastExprToType(lex, voidPtrTy);
840 ImpCastExprToType(rex, voidPtrTy);
841 return voidPtrTy;
Chris Lattner71225142007-07-31 21:27:01 +0000842 }
843 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000844 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
845 // differently qualified versions of compatible types, the result type is
846 // a pointer to an appropriately qualified version of the *composite*
847 // type.
Chris Lattner0ac51632008-01-06 22:50:31 +0000848 // FIXME: Need to return the composite type.
849 return lexT;
Chris Lattner4b009652007-07-25 00:24:17 +0000850 }
Chris Lattner4b009652007-07-25 00:24:17 +0000851 }
Chris Lattner71225142007-07-31 21:27:01 +0000852
Chris Lattner992ae932008-01-06 22:42:25 +0000853 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +0000854 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
855 lexT.getAsString(), rexT.getAsString(),
856 lex->getSourceRange(), rex->getSourceRange());
857 return QualType();
858}
859
Steve Naroff87d58b42007-09-16 03:34:24 +0000860/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +0000861/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +0000862Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000863 SourceLocation ColonLoc,
864 ExprTy *Cond, ExprTy *LHS,
865 ExprTy *RHS) {
866 Expr *CondExpr = (Expr *) Cond;
867 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +0000868
869 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
870 // was the condition.
871 bool isLHSNull = LHSExpr == 0;
872 if (isLHSNull)
873 LHSExpr = CondExpr;
874
Chris Lattner4b009652007-07-25 00:24:17 +0000875 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
876 RHSExpr, QuestionLoc);
877 if (result.isNull())
878 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +0000879 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
880 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +0000881}
882
Steve Naroffdb65e052007-08-28 23:30:39 +0000883/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffbbaed752008-01-29 02:42:22 +0000884/// do not have a prototype. Arguments that have type float are promoted to
885/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000886void Sema::DefaultArgumentPromotion(Expr *&Expr) {
887 QualType Ty = Expr->getType();
888 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffdb65e052007-08-28 23:30:39 +0000889
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000890 if (Ty == Context.FloatTy)
Chris Lattnere992d6c2008-01-16 19:17:22 +0000891 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroffbbaed752008-01-29 02:42:22 +0000892 else
893 UsualUnaryConversions(Expr);
Steve Naroffdb65e052007-08-28 23:30:39 +0000894}
895
Chris Lattner4b009652007-07-25 00:24:17 +0000896/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
897void Sema::DefaultFunctionArrayConversion(Expr *&e) {
898 QualType t = e->getType();
899 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
900
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000901 if (const ReferenceType *ref = t->getAsReferenceType()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000902 ImpCastExprToType(e, ref->getReferenceeType()); // C++ [expr]
Chris Lattner4b009652007-07-25 00:24:17 +0000903 t = e->getType();
904 }
905 if (t->isFunctionType())
Chris Lattnere992d6c2008-01-16 19:17:22 +0000906 ImpCastExprToType(e, Context.getPointerType(t));
Steve Naroffac26e9a2008-02-09 16:59:44 +0000907 else if (const ArrayType *ary = t->getAsArrayType()) {
Steve Naroff9ffeda12008-02-09 17:25:18 +0000908 // Make sure we don't lose qualifiers when dealing with typedefs. Example:
Steve Naroffac26e9a2008-02-09 16:59:44 +0000909 // typedef int arr[10];
910 // void test2() {
911 // const arr b;
912 // b[4] = 1;
913 // }
914 QualType ELT = ary->getElementType();
915 ELT = ELT.getQualifiedType(t.getQualifiers()|ELT.getQualifiers());
916 ImpCastExprToType(e, Context.getPointerType(ELT));
917 }
Chris Lattner4b009652007-07-25 00:24:17 +0000918}
919
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000920/// UsualUnaryConversions - Performs various conversions that are common to most
Chris Lattner4b009652007-07-25 00:24:17 +0000921/// operators (C99 6.3). The conversions of array and function types are
922/// sometimes surpressed. For example, the array->pointer conversion doesn't
923/// apply if the array is an argument to the sizeof or address (&) operators.
924/// In these instances, this routine should *not* be called.
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000925Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
926 QualType Ty = Expr->getType();
927 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000928
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000929 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000930 ImpCastExprToType(Expr, Ref->getReferenceeType()); // C++ [expr]
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000931 Ty = Expr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000932 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000933 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattnere992d6c2008-01-16 19:17:22 +0000934 ImpCastExprToType(Expr, Context.IntTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000935 else
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000936 DefaultFunctionArrayConversion(Expr);
937
938 return Expr;
Chris Lattner4b009652007-07-25 00:24:17 +0000939}
940
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000941/// UsualArithmeticConversions - Performs various conversions that are common to
Chris Lattner4b009652007-07-25 00:24:17 +0000942/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
943/// routine returns the first non-arithmetic type found. The client is
944/// responsible for emitting appropriate error diagnostics.
Steve Naroffe8419ca2008-01-15 22:21:49 +0000945/// FIXME: verify the conversion rules for "complex int" are consistent with GCC.
Steve Naroff8f708362007-08-24 19:07:16 +0000946QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
947 bool isCompAssign) {
Steve Naroffb2f9f552007-08-25 19:54:59 +0000948 if (!isCompAssign) {
949 UsualUnaryConversions(lhsExpr);
950 UsualUnaryConversions(rhsExpr);
951 }
Steve Naroff7438fdf2007-10-18 18:55:53 +0000952 // For conversion purposes, we ignore any qualifiers.
953 // For example, "const float" and "float" are equivalent.
Steve Naroff1ddb6f52007-11-10 19:45:54 +0000954 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
955 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +0000956
957 // If both types are identical, no conversion is needed.
Steve Naroff7438fdf2007-10-18 18:55:53 +0000958 if (lhs == rhs)
959 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000960
961 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
962 // The caller can deal with this (e.g. pointer + int).
963 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +0000964 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000965
966 // At this point, we have two different arithmetic types.
967
968 // Handle complex types first (C99 6.3.1.8p1).
969 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff43001212008-01-15 19:36:10 +0000970 // if we have an integer operand, the result is the complex type.
Steve Naroffe8419ca2008-01-15 22:21:49 +0000971 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +0000972 // convert the rhs to the lhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +0000973 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +0000974 return lhs;
Steve Naroff43001212008-01-15 19:36:10 +0000975 }
Steve Naroffe8419ca2008-01-15 22:21:49 +0000976 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +0000977 // convert the lhs to the rhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +0000978 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +0000979 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000980 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000981 // This handles complex/complex, complex/float, or float/complex.
982 // When both operands are complex, the shorter operand is converted to the
983 // type of the longer, and that is the type of the result. This corresponds
984 // to what is done when combining two real floating-point operands.
985 // The fun begins when size promotion occur across type domains.
986 // From H&S 6.3.4: When one operand is complex and the other is a real
987 // floating-point type, the less precise type is converted, within it's
988 // real or complex domain, to the precision of the other type. For example,
989 // when combining a "long double" with a "double _Complex", the
990 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff45fc9822007-08-27 15:30:22 +0000991 int result = Context.compareFloatingType(lhs, rhs);
992
993 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff3b565d62007-08-27 21:32:55 +0000994 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
995 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +0000996 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff3b565d62007-08-27 21:32:55 +0000997 } else if (result < 0) { // The right side is bigger, convert lhs.
998 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
999 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001000 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001001 }
1002 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1003 // domains match. This is a requirement for our implementation, C99
1004 // does not require this promotion.
1005 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1006 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001007 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001008 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001009 return rhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001010 } else { // handle "_Complex double, double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001011 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001012 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001013 return lhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001014 }
Chris Lattner4b009652007-07-25 00:24:17 +00001015 }
Steve Naroff3b6157f2007-08-27 21:43:43 +00001016 return lhs; // The domain/size match exactly.
Chris Lattner4b009652007-07-25 00:24:17 +00001017 }
1018 // Now handle "real" floating types (i.e. float, double, long double).
1019 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1020 // if we have an integer operand, the result is the real floating type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001021 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001022 // convert rhs to the lhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001023 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001024 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001025 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001026 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001027 // convert lhs to the rhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001028 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001029 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001030 }
1031 // We have two real floating types, float/complex combos were handled above.
1032 // Convert the smaller operand to the bigger result.
Steve Naroff45fc9822007-08-27 15:30:22 +00001033 int result = Context.compareFloatingType(lhs, rhs);
1034
1035 if (result > 0) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001036 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001037 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001038 }
Steve Naroff45fc9822007-08-27 15:30:22 +00001039 if (result < 0) { // convert the lhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001040 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff45fc9822007-08-27 15:30:22 +00001041 return rhs;
1042 }
1043 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Chris Lattner4b009652007-07-25 00:24:17 +00001044 }
Steve Naroff43001212008-01-15 19:36:10 +00001045 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1046 // Handle GCC complex int extension.
Steve Naroff43001212008-01-15 19:36:10 +00001047 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman50727042008-02-08 01:19:44 +00001048 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff43001212008-01-15 19:36:10 +00001049
Eli Friedman50727042008-02-08 01:19:44 +00001050 if (lhsComplexInt && rhsComplexInt) {
1051 if (Context.maxIntegerType(lhsComplexInt->getElementType(),
Eli Friedman94075c02008-02-08 01:24:30 +00001052 rhsComplexInt->getElementType()) == lhs) {
1053 // convert the rhs
1054 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1055 return lhs;
Eli Friedman50727042008-02-08 01:19:44 +00001056 }
1057 if (!isCompAssign)
Eli Friedman94075c02008-02-08 01:24:30 +00001058 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman50727042008-02-08 01:19:44 +00001059 return rhs;
1060 } else if (lhsComplexInt && rhs->isIntegerType()) {
1061 // convert the rhs to the lhs complex type.
1062 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1063 return lhs;
1064 } else if (rhsComplexInt && lhs->isIntegerType()) {
1065 // convert the lhs to the rhs complex type.
1066 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1067 return rhs;
1068 }
Steve Naroff43001212008-01-15 19:36:10 +00001069 }
Chris Lattner4b009652007-07-25 00:24:17 +00001070 // Finally, we have two differing integer types.
1071 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001072 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001073 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001074 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001075 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff8f708362007-08-24 19:07:16 +00001076 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001077}
1078
1079// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1080// being closely modeled after the C99 spec:-). The odd characteristic of this
1081// routine is it effectively iqnores the qualifiers on the top level pointee.
1082// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1083// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001084Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001085Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1086 QualType lhptee, rhptee;
1087
1088 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001089 lhptee = lhsType->getAsPointerType()->getPointeeType();
1090 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001091
1092 // make sure we operate on the canonical type
1093 lhptee = lhptee.getCanonicalType();
1094 rhptee = rhptee.getCanonicalType();
1095
Chris Lattner005ed752008-01-04 18:04:52 +00001096 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001097
1098 // C99 6.5.16.1p1: This following citation is common to constraints
1099 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1100 // qualifiers of the type *pointed to* by the right;
1101 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
1102 rhptee.getQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001103 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001104
1105 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1106 // incomplete type and the other is a pointer to a qualified or unqualified
1107 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001108 if (lhptee->isVoidType()) {
1109 if (rhptee->isObjectType() || rhptee->isIncompleteType())
Chris Lattner005ed752008-01-04 18:04:52 +00001110 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001111
1112 // As an extension, we allow cast to/from void* to function pointer.
1113 if (rhptee->isFunctionType())
1114 return FunctionVoidPointer;
1115 }
1116
1117 if (rhptee->isVoidType()) {
1118 if (lhptee->isObjectType() || lhptee->isIncompleteType())
Chris Lattner005ed752008-01-04 18:04:52 +00001119 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001120
1121 // As an extension, we allow cast to/from void* to function pointer.
1122 if (lhptee->isFunctionType())
1123 return FunctionVoidPointer;
1124 }
1125
Chris Lattner4b009652007-07-25 00:24:17 +00001126 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1127 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001128 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1129 rhptee.getUnqualifiedType()))
1130 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001131 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001132}
1133
1134/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1135/// has code to accommodate several GCC extensions when type checking
1136/// pointers. Here are some objectionable examples that GCC considers warnings:
1137///
1138/// int a, *pint;
1139/// short *pshort;
1140/// struct foo *pfoo;
1141///
1142/// pint = pshort; // warning: assignment from incompatible pointer type
1143/// a = pint; // warning: assignment makes integer from pointer without a cast
1144/// pint = a; // warning: assignment makes pointer from integer without a cast
1145/// pint = pfoo; // warning: assignment from incompatible pointer type
1146///
1147/// As a result, the code for dealing with pointers is more complex than the
1148/// C99 spec dictates.
1149/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1150///
Chris Lattner005ed752008-01-04 18:04:52 +00001151Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001152Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001153 // Get canonical types. We're not formatting these types, just comparing
1154 // them.
1155 lhsType = lhsType.getCanonicalType();
1156 rhsType = rhsType.getCanonicalType();
1157
1158 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001159 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001160
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001161 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001162 if (Context.referenceTypesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001163 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001164 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001165 }
Chris Lattner1853da22008-01-04 23:18:45 +00001166
Ted Kremenek42730c52008-01-07 19:49:32 +00001167 if (lhsType->isObjCQualifiedIdType()
1168 || rhsType->isObjCQualifiedIdType()) {
1169 if (Context.ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001170 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001171 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001172 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001173
1174 if (lhsType->isVectorType() || rhsType->isVectorType()) {
1175 // For OCUVector, allow vector splats; float -> <n x float>
1176 if (const OCUVectorType *LV = lhsType->getAsOCUVectorType()) {
1177 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1178 return Compatible;
1179 }
1180
1181 // If LHS and RHS are both vectors of integer or both vectors of floating
1182 // point types, and the total vector length is the same, allow the
1183 // conversion. This is a bitcast; no bits are changed but the result type
1184 // is different.
1185 if (getLangOptions().LaxVectorConversions &&
1186 lhsType->isVectorType() && rhsType->isVectorType()) {
1187 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1188 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
1189 if (Context.getTypeSize(lhsType, SourceLocation()) ==
1190 Context.getTypeSize(rhsType, SourceLocation()))
Nate Begemanec2d1062007-12-30 02:59:45 +00001191 return Compatible;
1192 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001193 }
1194 return Incompatible;
1195 }
1196
1197 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001198 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001199
1200 if (lhsType->isPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001201 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001202 return IntToPointer;
Chris Lattner4b009652007-07-25 00:24:17 +00001203
1204 if (rhsType->isPointerType())
1205 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001206 return Incompatible;
1207 }
1208
1209 if (rhsType->isPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001210 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1211 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001212 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001213
1214 if (lhsType->isPointerType())
1215 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001216 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001217 }
1218
1219 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001220 if (Context.tagTypesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001221 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001222 }
1223 return Incompatible;
1224}
1225
Chris Lattner005ed752008-01-04 18:04:52 +00001226Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001227Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001228 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1229 // a null pointer constant.
Ted Kremenek42730c52008-01-07 19:49:32 +00001230 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001231 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001232 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001233 return Compatible;
1234 }
Chris Lattner5f505bf2007-10-16 02:55:40 +00001235 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001236 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001237 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001238 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001239 //
1240 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1241 // are better understood.
1242 if (!lhsType->isReferenceType())
1243 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001244
Chris Lattner005ed752008-01-04 18:04:52 +00001245 Sema::AssignConvertType result =
1246 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001247
1248 // C99 6.5.16.1p2: The value of the right operand is converted to the
1249 // type of the assignment expression.
1250 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001251 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001252 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001253}
1254
Chris Lattner005ed752008-01-04 18:04:52 +00001255Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001256Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1257 return CheckAssignmentConstraints(lhsType, rhsType);
1258}
1259
Chris Lattner2c8bff72007-12-12 05:47:28 +00001260QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001261 Diag(loc, diag::err_typecheck_invalid_operands,
1262 lex->getType().getAsString(), rex->getType().getAsString(),
1263 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001264 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001265}
1266
1267inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1268 Expr *&rex) {
1269 QualType lhsType = lex->getType(), rhsType = rex->getType();
1270
1271 // make sure the vector types are identical.
1272 if (lhsType == rhsType)
1273 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001274
1275 // if the lhs is an ocu vector and the rhs is a scalar of the same type,
1276 // promote the rhs to the vector type.
1277 if (const OCUVectorType *V = lhsType->getAsOCUVectorType()) {
1278 if (V->getElementType().getCanonicalType().getTypePtr()
1279 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001280 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001281 return lhsType;
1282 }
1283 }
1284
1285 // if the rhs is an ocu vector and the lhs is a scalar of the same type,
1286 // promote the lhs to the vector type.
1287 if (const OCUVectorType *V = rhsType->getAsOCUVectorType()) {
1288 if (V->getElementType().getCanonicalType().getTypePtr()
1289 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001290 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001291 return rhsType;
1292 }
1293 }
1294
Chris Lattner4b009652007-07-25 00:24:17 +00001295 // You cannot convert between vector values of different size.
1296 Diag(loc, diag::err_typecheck_vector_not_convertable,
1297 lex->getType().getAsString(), rex->getType().getAsString(),
1298 lex->getSourceRange(), rex->getSourceRange());
1299 return QualType();
1300}
1301
1302inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001303 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001304{
1305 QualType lhsType = lex->getType(), rhsType = rex->getType();
1306
1307 if (lhsType->isVectorType() || rhsType->isVectorType())
1308 return CheckVectorOperands(loc, lex, rex);
1309
Steve Naroff8f708362007-08-24 19:07:16 +00001310 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001311
Chris Lattner4b009652007-07-25 00:24:17 +00001312 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001313 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001314 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001315}
1316
1317inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001318 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001319{
1320 QualType lhsType = lex->getType(), rhsType = rex->getType();
1321
Steve Naroff8f708362007-08-24 19:07:16 +00001322 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001323
Chris Lattner4b009652007-07-25 00:24:17 +00001324 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001325 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001326 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001327}
1328
1329inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001330 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001331{
1332 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1333 return CheckVectorOperands(loc, lex, rex);
1334
Steve Naroff8f708362007-08-24 19:07:16 +00001335 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001336
1337 // handle the common case first (both operands are arithmetic).
1338 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001339 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001340
1341 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1342 return lex->getType();
1343 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1344 return rex->getType();
Chris Lattner2c8bff72007-12-12 05:47:28 +00001345 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001346}
1347
1348inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001349 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001350{
1351 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1352 return CheckVectorOperands(loc, lex, rex);
1353
Steve Naroff8f708362007-08-24 19:07:16 +00001354 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001355
Chris Lattnerf6da2912007-12-09 21:53:25 +00001356 // Enforce type constraints: C99 6.5.6p3.
1357
1358 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001359 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001360 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001361
1362 // Either ptr - int or ptr - ptr.
1363 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001364 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001365
Chris Lattnerf6da2912007-12-09 21:53:25 +00001366 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001367 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001368 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001369 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001370 Diag(loc, diag::ext_gnu_void_ptr,
1371 lex->getSourceRange(), rex->getSourceRange());
1372 } else {
1373 Diag(loc, diag::err_typecheck_sub_ptr_object,
1374 lex->getType().getAsString(), lex->getSourceRange());
1375 return QualType();
1376 }
1377 }
1378
1379 // The result type of a pointer-int computation is the pointer type.
1380 if (rex->getType()->isIntegerType())
1381 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001382
Chris Lattnerf6da2912007-12-09 21:53:25 +00001383 // Handle pointer-pointer subtractions.
1384 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001385 QualType rpointee = RHSPTy->getPointeeType();
1386
Chris Lattnerf6da2912007-12-09 21:53:25 +00001387 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001388 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001389 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001390 if (rpointee->isVoidType()) {
1391 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-12-09 21:53:25 +00001392 Diag(loc, diag::ext_gnu_void_ptr,
1393 lex->getSourceRange(), rex->getSourceRange());
1394 } else {
1395 Diag(loc, diag::err_typecheck_sub_ptr_object,
1396 rex->getType().getAsString(), rex->getSourceRange());
1397 return QualType();
1398 }
1399 }
1400
1401 // Pointee types must be compatible.
Steve Naroff577f9722008-01-29 18:58:14 +00001402 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1403 rpointee.getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001404 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1405 lex->getType().getAsString(), rex->getType().getAsString(),
1406 lex->getSourceRange(), rex->getSourceRange());
1407 return QualType();
1408 }
1409
1410 return Context.getPointerDiffType();
1411 }
1412 }
1413
Chris Lattner2c8bff72007-12-12 05:47:28 +00001414 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001415}
1416
1417inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Chris Lattner2c8bff72007-12-12 05:47:28 +00001418 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) {
1419 // C99 6.5.7p2: Each of the operands shall have integer type.
1420 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1421 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001422
Chris Lattner2c8bff72007-12-12 05:47:28 +00001423 // Shifts don't perform usual arithmetic conversions, they just do integer
1424 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001425 if (!isCompAssign)
1426 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001427 UsualUnaryConversions(rex);
1428
1429 // "The type of the result is that of the promoted left operand."
1430 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001431}
1432
Chris Lattner254f3bc2007-08-26 01:18:55 +00001433inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1434 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Chris Lattner4b009652007-07-25 00:24:17 +00001435{
Chris Lattner254f3bc2007-08-26 01:18:55 +00001436 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001437 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1438 UsualArithmeticConversions(lex, rex);
1439 else {
1440 UsualUnaryConversions(lex);
1441 UsualUnaryConversions(rex);
1442 }
Chris Lattner4b009652007-07-25 00:24:17 +00001443 QualType lType = lex->getType();
1444 QualType rType = rex->getType();
1445
Ted Kremenek486509e2007-10-29 17:13:39 +00001446 // For non-floating point types, check for self-comparisons of the form
1447 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1448 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001449 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001450 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1451 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001452 if (DRL->getDecl() == DRR->getDecl())
1453 Diag(loc, diag::warn_selfcomparison);
1454 }
1455
Chris Lattner254f3bc2007-08-26 01:18:55 +00001456 if (isRelational) {
1457 if (lType->isRealType() && rType->isRealType())
1458 return Context.IntTy;
1459 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001460 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001461 if (lType->isFloatingType()) {
1462 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001463 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001464 }
1465
Chris Lattner254f3bc2007-08-26 01:18:55 +00001466 if (lType->isArithmeticType() && rType->isArithmeticType())
1467 return Context.IntTy;
1468 }
Chris Lattner4b009652007-07-25 00:24:17 +00001469
Chris Lattner22be8422007-08-26 01:10:14 +00001470 bool LHSIsNull = lex->isNullPointerConstant(Context);
1471 bool RHSIsNull = rex->isNullPointerConstant(Context);
1472
Chris Lattner254f3bc2007-08-26 01:18:55 +00001473 // All of the following pointer related warnings are GCC extensions, except
1474 // when handling null pointer constants. One day, we can consider making them
1475 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001476 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Eli Friedman50727042008-02-08 01:19:44 +00001477 QualType lpointee = lType->getAsPointerType()->getPointeeType();
1478 QualType rpointee = rType->getAsPointerType()->getPointeeType();
1479
Steve Naroff3b435622007-11-13 14:57:38 +00001480 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Steve Naroff577f9722008-01-29 18:58:14 +00001481 !lpointee->isVoidType() && !lpointee->isVoidType() &&
1482 !Context.typesAreCompatible(lpointee.getUnqualifiedType(),
Eli Friedman50727042008-02-08 01:19:44 +00001483 rpointee.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001484 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1485 lType.getAsString(), rType.getAsString(),
1486 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001487 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001488 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001489 return Context.IntTy;
1490 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001491 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
1492 && Context.ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001493 ImpCastExprToType(rex, lType);
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001494 return Context.IntTy;
1495 }
Steve Naroff4462cb02007-08-16 21:48:38 +00001496 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001497 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001498 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1499 lType.getAsString(), rType.getAsString(),
1500 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001501 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001502 return Context.IntTy;
1503 }
1504 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001505 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001506 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1507 lType.getAsString(), rType.getAsString(),
1508 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001509 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001510 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001511 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00001512 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001513}
1514
Chris Lattner4b009652007-07-25 00:24:17 +00001515inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001516 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001517{
1518 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1519 return CheckVectorOperands(loc, lex, rex);
1520
Steve Naroff8f708362007-08-24 19:07:16 +00001521 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001522
1523 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001524 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001525 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001526}
1527
1528inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1529 Expr *&lex, Expr *&rex, SourceLocation loc)
1530{
1531 UsualUnaryConversions(lex);
1532 UsualUnaryConversions(rex);
1533
1534 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
1535 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001536 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001537}
1538
1539inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001540 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001541{
1542 QualType lhsType = lex->getType();
1543 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner4b009652007-07-25 00:24:17 +00001544 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1545
1546 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00001547 case Expr::MLV_Valid:
1548 break;
1549 case Expr::MLV_ConstQualified:
1550 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1551 return QualType();
1552 case Expr::MLV_ArrayType:
1553 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1554 lhsType.getAsString(), lex->getSourceRange());
1555 return QualType();
1556 case Expr::MLV_NotObjectType:
1557 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1558 lhsType.getAsString(), lex->getSourceRange());
1559 return QualType();
1560 case Expr::MLV_InvalidExpression:
1561 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1562 lex->getSourceRange());
1563 return QualType();
1564 case Expr::MLV_IncompleteType:
1565 case Expr::MLV_IncompleteVoidType:
1566 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1567 lhsType.getAsString(), lex->getSourceRange());
1568 return QualType();
1569 case Expr::MLV_DuplicateVectorComponents:
1570 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1571 lex->getSourceRange());
1572 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001573 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001574
Chris Lattner005ed752008-01-04 18:04:52 +00001575 AssignConvertType ConvTy;
1576 if (compoundType.isNull())
1577 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1578 else
1579 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1580
1581 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1582 rex, "assigning"))
1583 return QualType();
1584
Chris Lattner4b009652007-07-25 00:24:17 +00001585 // C99 6.5.16p3: The type of an assignment expression is the type of the
1586 // left operand unless the left operand has qualified type, in which case
1587 // it is the unqualified version of the type of the left operand.
1588 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1589 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001590 // C++ 5.17p1: the type of the assignment expression is that of its left
1591 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00001592 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001593}
1594
1595inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1596 Expr *&lex, Expr *&rex, SourceLocation loc) {
1597 UsualUnaryConversions(rex);
1598 return rex->getType();
1599}
1600
1601/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1602/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1603QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1604 QualType resType = op->getType();
1605 assert(!resType.isNull() && "no type for increment/decrement expression");
1606
Steve Naroffd30e1932007-08-24 17:20:07 +00001607 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00001608 if (const PointerType *pt = resType->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001609 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
1610 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1611 resType.getAsString(), op->getSourceRange());
1612 return QualType();
1613 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001614 } else if (!resType->isRealType()) {
1615 if (resType->isComplexType())
1616 // C99 does not support ++/-- on complex types.
1617 Diag(OpLoc, diag::ext_integer_increment_complex,
1618 resType.getAsString(), op->getSourceRange());
1619 else {
1620 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1621 resType.getAsString(), op->getSourceRange());
1622 return QualType();
1623 }
Chris Lattner4b009652007-07-25 00:24:17 +00001624 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001625 // At this point, we know we have a real, complex or pointer type.
1626 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001627 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1628 if (mlval != Expr::MLV_Valid) {
1629 // FIXME: emit a more precise diagnostic...
1630 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1631 op->getSourceRange());
1632 return QualType();
1633 }
1634 return resType;
1635}
1636
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001637/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00001638/// This routine allows us to typecheck complex/recursive expressions
1639/// where the declaration is needed for type checking. Here are some
1640/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001641static ValueDecl *getPrimaryDecl(Expr *e) {
Chris Lattner4b009652007-07-25 00:24:17 +00001642 switch (e->getStmtClass()) {
1643 case Stmt::DeclRefExprClass:
1644 return cast<DeclRefExpr>(e)->getDecl();
1645 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00001646 // Fields cannot be declared with a 'register' storage class.
1647 // &X->f is always ok, even if X is declared register.
1648 if (cast<MemberExpr>(e)->isArrow())
1649 return 0;
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001650 return getPrimaryDecl(cast<MemberExpr>(e)->getBase());
1651 case Stmt::ArraySubscriptExprClass: {
1652 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1653
1654 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(e)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00001655 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001656 return 0;
1657 else
1658 return VD;
1659 }
Chris Lattner4b009652007-07-25 00:24:17 +00001660 case Stmt::UnaryOperatorClass:
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001661 return getPrimaryDecl(cast<UnaryOperator>(e)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001662 case Stmt::ParenExprClass:
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001663 return getPrimaryDecl(cast<ParenExpr>(e)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00001664 case Stmt::ImplicitCastExprClass:
1665 // &X[4] when X is an array, has an implicit cast from array to pointer.
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001666 return getPrimaryDecl(cast<ImplicitCastExpr>(e)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001667 default:
1668 return 0;
1669 }
1670}
1671
1672/// CheckAddressOfOperand - The operand of & must be either a function
1673/// designator or an lvalue designating an object. If it is an lvalue, the
1674/// object cannot be declared with storage class register or be a bit field.
1675/// Note: The usual conversions are *not* applied to the operand of the &
1676/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1677QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001678 if (getLangOptions().C99) {
1679 // Implement C99-only parts of addressof rules.
1680 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1681 if (uOp->getOpcode() == UnaryOperator::Deref)
1682 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1683 // (assuming the deref expression is valid).
1684 return uOp->getSubExpr()->getType();
1685 }
1686 // Technically, there should be a check for array subscript
1687 // expressions here, but the result of one is always an lvalue anyway.
1688 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001689 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner4b009652007-07-25 00:24:17 +00001690 Expr::isLvalueResult lval = op->isLvalue();
1691
1692 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00001693 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1694 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00001695 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1696 op->getSourceRange());
1697 return QualType();
1698 }
1699 } else if (dcl) {
1700 // We have an lvalue with a decl. Make sure the decl is not declared
1701 // with the register storage-class specifier.
1702 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1703 if (vd->getStorageClass() == VarDecl::Register) {
1704 Diag(OpLoc, diag::err_typecheck_address_of_register,
1705 op->getSourceRange());
1706 return QualType();
1707 }
1708 } else
1709 assert(0 && "Unknown/unexpected decl type");
1710
1711 // FIXME: add check for bitfields!
1712 }
1713 // If the operand has type "type", the result has type "pointer to type".
1714 return Context.getPointerType(op->getType());
1715}
1716
1717QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
1718 UsualUnaryConversions(op);
1719 QualType qType = op->getType();
1720
Chris Lattner7931f4a2007-07-31 16:53:04 +00001721 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001722 // Note that per both C89 and C99, this is always legal, even
1723 // if ptype is an incomplete type or void.
1724 // It would be possible to warn about dereferencing a
1725 // void pointer, but it's completely well-defined,
1726 // and such a warning is unlikely to catch any mistakes.
1727 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001728 }
1729 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1730 qType.getAsString(), op->getSourceRange());
1731 return QualType();
1732}
1733
1734static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1735 tok::TokenKind Kind) {
1736 BinaryOperator::Opcode Opc;
1737 switch (Kind) {
1738 default: assert(0 && "Unknown binop!");
1739 case tok::star: Opc = BinaryOperator::Mul; break;
1740 case tok::slash: Opc = BinaryOperator::Div; break;
1741 case tok::percent: Opc = BinaryOperator::Rem; break;
1742 case tok::plus: Opc = BinaryOperator::Add; break;
1743 case tok::minus: Opc = BinaryOperator::Sub; break;
1744 case tok::lessless: Opc = BinaryOperator::Shl; break;
1745 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1746 case tok::lessequal: Opc = BinaryOperator::LE; break;
1747 case tok::less: Opc = BinaryOperator::LT; break;
1748 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1749 case tok::greater: Opc = BinaryOperator::GT; break;
1750 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1751 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1752 case tok::amp: Opc = BinaryOperator::And; break;
1753 case tok::caret: Opc = BinaryOperator::Xor; break;
1754 case tok::pipe: Opc = BinaryOperator::Or; break;
1755 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1756 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1757 case tok::equal: Opc = BinaryOperator::Assign; break;
1758 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1759 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1760 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1761 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1762 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1763 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1764 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1765 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1766 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1767 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1768 case tok::comma: Opc = BinaryOperator::Comma; break;
1769 }
1770 return Opc;
1771}
1772
1773static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1774 tok::TokenKind Kind) {
1775 UnaryOperator::Opcode Opc;
1776 switch (Kind) {
1777 default: assert(0 && "Unknown unary op!");
1778 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1779 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1780 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1781 case tok::star: Opc = UnaryOperator::Deref; break;
1782 case tok::plus: Opc = UnaryOperator::Plus; break;
1783 case tok::minus: Opc = UnaryOperator::Minus; break;
1784 case tok::tilde: Opc = UnaryOperator::Not; break;
1785 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1786 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1787 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1788 case tok::kw___real: Opc = UnaryOperator::Real; break;
1789 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1790 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1791 }
1792 return Opc;
1793}
1794
1795// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00001796Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00001797 ExprTy *LHS, ExprTy *RHS) {
1798 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1799 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1800
Steve Naroff87d58b42007-09-16 03:34:24 +00001801 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1802 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00001803
1804 QualType ResultTy; // Result type of the binary operator.
1805 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
1806
1807 switch (Opc) {
1808 default:
1809 assert(0 && "Unknown binary expr!");
1810 case BinaryOperator::Assign:
1811 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
1812 break;
1813 case BinaryOperator::Mul:
1814 case BinaryOperator::Div:
1815 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1816 break;
1817 case BinaryOperator::Rem:
1818 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1819 break;
1820 case BinaryOperator::Add:
1821 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1822 break;
1823 case BinaryOperator::Sub:
1824 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1825 break;
1826 case BinaryOperator::Shl:
1827 case BinaryOperator::Shr:
1828 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
1829 break;
1830 case BinaryOperator::LE:
1831 case BinaryOperator::LT:
1832 case BinaryOperator::GE:
1833 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00001834 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001835 break;
1836 case BinaryOperator::EQ:
1837 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00001838 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001839 break;
1840 case BinaryOperator::And:
1841 case BinaryOperator::Xor:
1842 case BinaryOperator::Or:
1843 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1844 break;
1845 case BinaryOperator::LAnd:
1846 case BinaryOperator::LOr:
1847 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
1848 break;
1849 case BinaryOperator::MulAssign:
1850 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001851 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001852 if (!CompTy.isNull())
1853 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1854 break;
1855 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001856 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001857 if (!CompTy.isNull())
1858 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1859 break;
1860 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001861 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001862 if (!CompTy.isNull())
1863 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1864 break;
1865 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001866 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001867 if (!CompTy.isNull())
1868 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1869 break;
1870 case BinaryOperator::ShlAssign:
1871 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001872 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001873 if (!CompTy.isNull())
1874 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1875 break;
1876 case BinaryOperator::AndAssign:
1877 case BinaryOperator::XorAssign:
1878 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001879 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001880 if (!CompTy.isNull())
1881 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1882 break;
1883 case BinaryOperator::Comma:
1884 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
1885 break;
1886 }
1887 if (ResultTy.isNull())
1888 return true;
1889 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00001890 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001891 else
Chris Lattnerf420df12007-08-28 18:36:55 +00001892 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001893}
1894
1895// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00001896Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00001897 ExprTy *input) {
1898 Expr *Input = (Expr*)input;
1899 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1900 QualType resultType;
1901 switch (Opc) {
1902 default:
1903 assert(0 && "Unimplemented unary expr!");
1904 case UnaryOperator::PreInc:
1905 case UnaryOperator::PreDec:
1906 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
1907 break;
1908 case UnaryOperator::AddrOf:
1909 resultType = CheckAddressOfOperand(Input, OpLoc);
1910 break;
1911 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00001912 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00001913 resultType = CheckIndirectionOperand(Input, OpLoc);
1914 break;
1915 case UnaryOperator::Plus:
1916 case UnaryOperator::Minus:
1917 UsualUnaryConversions(Input);
1918 resultType = Input->getType();
1919 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1920 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1921 resultType.getAsString());
1922 break;
1923 case UnaryOperator::Not: // bitwise complement
1924 UsualUnaryConversions(Input);
1925 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00001926 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1927 if (!resultType->isIntegerType()) {
1928 if (resultType->isComplexType())
1929 // C99 does not support '~' for complex conjugation.
1930 Diag(OpLoc, diag::ext_integer_complement_complex,
1931 resultType.getAsString());
1932 else
1933 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1934 resultType.getAsString());
1935 }
Chris Lattner4b009652007-07-25 00:24:17 +00001936 break;
1937 case UnaryOperator::LNot: // logical negation
1938 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
1939 DefaultFunctionArrayConversion(Input);
1940 resultType = Input->getType();
1941 if (!resultType->isScalarType()) // C99 6.5.3.3p1
1942 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1943 resultType.getAsString());
1944 // LNot always has type int. C99 6.5.3.3p5.
1945 resultType = Context.IntTy;
1946 break;
1947 case UnaryOperator::SizeOf:
1948 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
1949 break;
1950 case UnaryOperator::AlignOf:
1951 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1952 break;
Chris Lattner03931a72007-08-24 21:16:53 +00001953 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00001954 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00001955 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00001956 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001957 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00001958 resultType = Input->getType();
1959 break;
1960 }
1961 if (resultType.isNull())
1962 return true;
1963 return new UnaryOperator(Input, Opc, resultType, OpLoc);
1964}
1965
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001966/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1967Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001968 SourceLocation LabLoc,
1969 IdentifierInfo *LabelII) {
1970 // Look up the record for this label identifier.
1971 LabelStmt *&LabelDecl = LabelMap[LabelII];
1972
1973 // If we haven't seen this label yet, create a forward reference.
1974 if (LabelDecl == 0)
1975 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1976
1977 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001978 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1979 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001980}
1981
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001982Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00001983 SourceLocation RPLoc) { // "({..})"
1984 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1985 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1986 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1987
1988 // FIXME: there are a variety of strange constraints to enforce here, for
1989 // example, it is not possible to goto into a stmt expression apparently.
1990 // More semantic analysis is needed.
1991
1992 // FIXME: the last statement in the compount stmt has its value used. We
1993 // should not warn about it being unused.
1994
1995 // If there are sub stmts in the compound stmt, take the type of the last one
1996 // as the type of the stmtexpr.
1997 QualType Ty = Context.VoidTy;
1998
1999 if (!Compound->body_empty())
2000 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2001 Ty = LastExpr->getType();
2002
2003 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2004}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002005
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002006Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002007 SourceLocation TypeLoc,
2008 TypeTy *argty,
2009 OffsetOfComponent *CompPtr,
2010 unsigned NumComponents,
2011 SourceLocation RPLoc) {
2012 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2013 assert(!ArgTy.isNull() && "Missing type argument!");
2014
2015 // We must have at least one component that refers to the type, and the first
2016 // one is known to be a field designator. Verify that the ArgTy represents
2017 // a struct/union/class.
2018 if (!ArgTy->isRecordType())
2019 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2020
2021 // Otherwise, create a compound literal expression as the base, and
2022 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002023 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002024
Chris Lattnerb37522e2007-08-31 21:49:13 +00002025 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2026 // GCC extension, diagnose them.
2027 if (NumComponents != 1)
2028 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2029 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2030
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002031 for (unsigned i = 0; i != NumComponents; ++i) {
2032 const OffsetOfComponent &OC = CompPtr[i];
2033 if (OC.isBrackets) {
2034 // Offset of an array sub-field. TODO: Should we allow vector elements?
2035 const ArrayType *AT = Res->getType()->getAsArrayType();
2036 if (!AT) {
2037 delete Res;
2038 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2039 Res->getType().getAsString());
2040 }
2041
Chris Lattner2af6a802007-08-30 17:59:59 +00002042 // FIXME: C++: Verify that operator[] isn't overloaded.
2043
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002044 // C99 6.5.2.1p1
2045 Expr *Idx = static_cast<Expr*>(OC.U.E);
2046 if (!Idx->getType()->isIntegerType())
2047 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2048 Idx->getSourceRange());
2049
2050 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2051 continue;
2052 }
2053
2054 const RecordType *RC = Res->getType()->getAsRecordType();
2055 if (!RC) {
2056 delete Res;
2057 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2058 Res->getType().getAsString());
2059 }
2060
2061 // Get the decl corresponding to this.
2062 RecordDecl *RD = RC->getDecl();
2063 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2064 if (!MemberDecl)
2065 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2066 OC.U.IdentInfo->getName(),
2067 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002068
2069 // FIXME: C++: Verify that MemberDecl isn't a static field.
2070 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002071 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2072 // matter here.
2073 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002074 }
2075
2076 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2077 BuiltinLoc);
2078}
2079
2080
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002081Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002082 TypeTy *arg1, TypeTy *arg2,
2083 SourceLocation RPLoc) {
2084 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2085 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2086
2087 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2088
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002089 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002090}
2091
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002092Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002093 ExprTy *expr1, ExprTy *expr2,
2094 SourceLocation RPLoc) {
2095 Expr *CondExpr = static_cast<Expr*>(cond);
2096 Expr *LHSExpr = static_cast<Expr*>(expr1);
2097 Expr *RHSExpr = static_cast<Expr*>(expr2);
2098
2099 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2100
2101 // The conditional expression is required to be a constant expression.
2102 llvm::APSInt condEval(32);
2103 SourceLocation ExpLoc;
2104 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2105 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2106 CondExpr->getSourceRange());
2107
2108 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2109 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2110 RHSExpr->getType();
2111 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2112}
2113
Nate Begemanbd881ef2008-01-30 20:50:20 +00002114/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002115/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002116/// The number of arguments has already been validated to match the number of
2117/// arguments in FnType.
2118static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002119 unsigned NumParams = FnType->getNumArgs();
2120 for (unsigned i = 0; i != NumParams; ++i)
Nate Begemanbd881ef2008-01-30 20:50:20 +00002121 if (Args[i]->getType().getCanonicalType() !=
2122 FnType->getArgType(i).getCanonicalType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002123 return false;
2124 return true;
2125}
2126
2127Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2128 SourceLocation *CommaLocs,
2129 SourceLocation BuiltinLoc,
2130 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002131 // __builtin_overload requires at least 2 arguments
2132 if (NumArgs < 2)
2133 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2134 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002135
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002136 // The first argument is required to be a constant expression. It tells us
2137 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002138 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002139 Expr *NParamsExpr = Args[0];
2140 llvm::APSInt constEval(32);
2141 SourceLocation ExpLoc;
2142 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2143 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2144 NParamsExpr->getSourceRange());
2145
2146 // Verify that the number of parameters is > 0
2147 unsigned NumParams = constEval.getZExtValue();
2148 if (NumParams == 0)
2149 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2150 NParamsExpr->getSourceRange());
2151 // Verify that we have at least 1 + NumParams arguments to the builtin.
2152 if ((NumParams + 1) > NumArgs)
2153 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2154 SourceRange(BuiltinLoc, RParenLoc));
2155
2156 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002157 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002158 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002159 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2160 // UsualUnaryConversions will convert the function DeclRefExpr into a
2161 // pointer to function.
2162 Expr *Fn = UsualUnaryConversions(Args[i]);
2163 FunctionTypeProto *FnType = 0;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002164 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2165 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2166 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2167 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002168
2169 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2170 // parameters, and the number of parameters must match the value passed to
2171 // the builtin.
2172 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002173 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2174 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002175
2176 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002177 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002178 // If they match, return a new OverloadExpr.
Nate Begemanc6078c92008-01-31 05:38:29 +00002179 if (ExprsMatchFnType(Args+1, FnType)) {
2180 if (OE)
2181 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2182 OE->getFn()->getSourceRange());
2183 // Remember our match, and continue processing the remaining arguments
2184 // to catch any errors.
2185 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2186 BuiltinLoc, RParenLoc);
2187 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002188 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002189 // Return the newly created OverloadExpr node, if we succeded in matching
2190 // exactly one of the candidate functions.
2191 if (OE)
2192 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002193
2194 // If we didn't find a matching function Expr in the __builtin_overload list
2195 // the return an error.
2196 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002197 for (unsigned i = 0; i != NumParams; ++i) {
2198 if (i != 0) typeNames += ", ";
2199 typeNames += Args[i+1]->getType().getAsString();
2200 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002201
2202 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2203 SourceRange(BuiltinLoc, RParenLoc));
2204}
2205
Anders Carlsson36760332007-10-15 20:28:48 +00002206Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2207 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002208 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002209 Expr *E = static_cast<Expr*>(expr);
2210 QualType T = QualType::getFromOpaquePtr(type);
2211
2212 InitBuiltinVaListType();
2213
Chris Lattner005ed752008-01-04 18:04:52 +00002214 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2215 != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002216 return Diag(E->getLocStart(),
2217 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2218 E->getType().getAsString(),
2219 E->getSourceRange());
2220
2221 // FIXME: Warn if a non-POD type is passed in.
2222
2223 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2224}
2225
Chris Lattner005ed752008-01-04 18:04:52 +00002226bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2227 SourceLocation Loc,
2228 QualType DstType, QualType SrcType,
2229 Expr *SrcExpr, const char *Flavor) {
2230 // Decode the result (notice that AST's are still created for extensions).
2231 bool isInvalid = false;
2232 unsigned DiagKind;
2233 switch (ConvTy) {
2234 default: assert(0 && "Unknown conversion type");
2235 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002236 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00002237 DiagKind = diag::ext_typecheck_convert_pointer_int;
2238 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002239 case IntToPointer:
2240 DiagKind = diag::ext_typecheck_convert_int_pointer;
2241 break;
Chris Lattner005ed752008-01-04 18:04:52 +00002242 case IncompatiblePointer:
2243 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2244 break;
2245 case FunctionVoidPointer:
2246 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2247 break;
2248 case CompatiblePointerDiscardsQualifiers:
2249 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2250 break;
2251 case Incompatible:
2252 DiagKind = diag::err_typecheck_convert_incompatible;
2253 isInvalid = true;
2254 break;
2255 }
2256
2257 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2258 SrcExpr->getSourceRange());
2259 return isInvalid;
2260}
2261