blob: 0d48ad7aa520d99274a0033c476e0a231f741045 [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());
Chris Lattnera6dcce32008-02-11 00:02:17 +000045
46 // Verify that pascal strings aren't too large.
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000047 if (Literal.Pascal && Literal.GetStringLength() > 256)
48 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
49 SourceRange(StringToks[0].getLocation(),
50 StringToks[NumStringToks-1].getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +000051
Chris Lattnera6dcce32008-02-11 00:02:17 +000052 QualType StrTy = Context.CharTy;
53 // FIXME: handle wchar_t
54 if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
55
56 // Get an array type for the string, according to C99 6.4.5. This includes
57 // the nul terminator character as well as the string length for pascal
58 // strings.
59 StrTy = Context.getConstantArrayType(StrTy,
60 llvm::APInt(32, Literal.GetStringLength()+1),
61 ArrayType::Normal, 0);
62
Chris Lattner4b009652007-07-25 00:24:17 +000063 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
64 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera6dcce32008-02-11 00:02:17 +000065 Literal.AnyWide, StrTy,
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000066 StringToks[0].getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +000067 StringToks[NumStringToks-1].getLocation());
68}
69
70
Steve Naroff0acc9c92007-09-15 18:49:24 +000071/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattner4b009652007-07-25 00:24:17 +000072/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
73/// identifier is used in an function call context.
Steve Naroff0acc9c92007-09-15 18:49:24 +000074Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +000075 IdentifierInfo &II,
76 bool HasTrailingLParen) {
77 // Could be enum-constant or decl.
Steve Narofff0c31dd2007-09-16 16:16:00 +000078 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner4b009652007-07-25 00:24:17 +000079 if (D == 0) {
80 // Otherwise, this could be an implicitly declared function reference (legal
81 // in C90, extension in C99).
82 if (HasTrailingLParen &&
83 // Not in C++.
84 !getLangOptions().CPlusPlus)
85 D = ImplicitlyDefineFunction(Loc, II, S);
86 else {
Steve Naroff5eb2a4a2007-11-12 14:29:37 +000087 if (CurMethodDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +000088 ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface();
89 ObjCInterfaceDecl *clsDeclared;
90 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, clsDeclared)) {
Steve Naroff6b759ce2007-11-15 02:58:25 +000091 IdentifierInfo &II = Context.Idents.get("self");
92 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
93 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
94 static_cast<Expr*>(SelfExpr.Val), true, true);
95 }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +000096 }
Chris Lattner4b009652007-07-25 00:24:17 +000097 // If this name wasn't predeclared and if this is not a function call,
98 // diagnose the problem.
99 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
100 }
101 }
Steve Naroff91b03f72007-08-28 03:03:08 +0000102 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroffcae537d2007-08-28 18:45:29 +0000103 // Only create DeclRefExpr's for valid Decl's.
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000104 if (VD->isInvalidDecl())
Steve Naroff91b03f72007-08-28 03:03:08 +0000105 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000106 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff91b03f72007-08-28 03:03:08 +0000107 }
Chris Lattner4b009652007-07-25 00:24:17 +0000108 if (isa<TypedefDecl>(D))
109 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian3102df92007-12-05 18:16:33 +0000111 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000112
113 assert(0 && "Invalid decl");
114 abort();
115}
116
Steve Naroff87d58b42007-09-16 03:34:24 +0000117Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000118 tok::TokenKind Kind) {
119 PreDefinedExpr::IdentType IT;
120
121 switch (Kind) {
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000122 default: assert(0 && "Unknown simple primary expr!");
123 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
124 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
125 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000126 }
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000127
128 // Verify that this is in a function context.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000129 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000130 return Diag(Loc, diag::err_predef_outside_function);
Chris Lattner4b009652007-07-25 00:24:17 +0000131
Chris Lattner7e637512008-01-12 08:14:25 +0000132 // Pre-defined identifiers are of type char[x], where x is the length of the
133 // string.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000134 unsigned Length;
135 if (CurFunctionDecl)
136 Length = CurFunctionDecl->getIdentifier()->getLength();
137 else
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000138 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000139
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000140 llvm::APInt LengthI(32, Length + 1);
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000141 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000142 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattner7e637512008-01-12 08:14:25 +0000143 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattner4b009652007-07-25 00:24:17 +0000144}
145
Steve Naroff87d58b42007-09-16 03:34:24 +0000146Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000147 llvm::SmallString<16> CharBuffer;
148 CharBuffer.resize(Tok.getLength());
149 const char *ThisTokBegin = &CharBuffer[0];
150 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
151
152 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
153 Tok.getLocation(), PP);
154 if (Literal.hadError())
155 return ExprResult(true);
156 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
157 Tok.getLocation());
158}
159
Steve Naroff87d58b42007-09-16 03:34:24 +0000160Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000161 // fast path for a single digit (which is quite common). A single digit
162 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
163 if (Tok.getLength() == 1) {
164 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
165
Chris Lattner3496d522007-09-04 02:45:27 +0000166 unsigned IntSize = static_cast<unsigned>(
167 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000168 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
169 Context.IntTy,
170 Tok.getLocation()));
171 }
172 llvm::SmallString<512> IntegerBuffer;
173 IntegerBuffer.resize(Tok.getLength());
174 const char *ThisTokBegin = &IntegerBuffer[0];
175
176 // Get the spelling of the token, which eliminates trigraphs, etc.
177 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
178 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
179 Tok.getLocation(), PP);
180 if (Literal.hadError)
181 return ExprResult(true);
182
Chris Lattner1de66eb2007-08-26 03:42:43 +0000183 Expr *Res;
184
185 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000186 QualType Ty;
187 const llvm::fltSemantics *Format;
188 uint64_t Size; unsigned Align;
189
190 if (Literal.isFloat) {
191 Ty = Context.FloatTy;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000192 Context.Target.getFloatInfo(Size, Align, Format,
193 Context.getFullLoc(Tok.getLocation()));
194
Chris Lattner858eece2007-09-22 18:29:59 +0000195 } else if (Literal.isLong) {
196 Ty = Context.LongDoubleTy;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000197 Context.Target.getLongDoubleInfo(Size, Align, Format,
198 Context.getFullLoc(Tok.getLocation()));
Chris Lattner858eece2007-09-22 18:29:59 +0000199 } else {
200 Ty = Context.DoubleTy;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000201 Context.Target.getDoubleInfo(Size, Align, Format,
202 Context.getFullLoc(Tok.getLocation()));
Chris Lattner858eece2007-09-22 18:29:59 +0000203 }
204
Ted Kremenekddedbe22007-11-29 00:56:49 +0000205 // isExact will be set by GetFloatValue().
206 bool isExact = false;
207
208 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
209 Ty, Tok.getLocation());
210
Chris Lattner1de66eb2007-08-26 03:42:43 +0000211 } else if (!Literal.isIntegerLiteral()) {
212 return ExprResult(true);
213 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000214 QualType t;
215
Neil Booth7421e9c2007-08-29 22:00:19 +0000216 // long long is a C99 feature.
217 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000218 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000219 Diag(Tok.getLocation(), diag::ext_longlong);
220
Chris Lattner4b009652007-07-25 00:24:17 +0000221 // Get the value in the widest-possible width.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000222 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(
223 Context.getFullLoc(Tok.getLocation())), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000224
225 if (Literal.GetIntegerValue(ResultVal)) {
226 // If this value didn't fit into uintmax_t, warn and force to ull.
227 Diag(Tok.getLocation(), diag::warn_integer_too_large);
228 t = Context.UnsignedLongLongTy;
229 assert(Context.getTypeSize(t, Tok.getLocation()) ==
230 ResultVal.getBitWidth() && "long long is not intmax_t?");
231 } else {
232 // If this value fits into a ULL, try to figure out what else it fits into
233 // according to the rules of C99 6.4.4.1p5.
234
235 // Octal, Hexadecimal, and integers with a U suffix are allowed to
236 // be an unsigned int.
237 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
238
239 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner98540b62007-08-23 21:58:08 +0000240 if (!Literal.isLong && !Literal.isLongLong) {
241 // Are int/unsigned possibilities?
Chris Lattner3496d522007-09-04 02:45:27 +0000242 unsigned IntSize = static_cast<unsigned>(
243 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000244 // Does it fit in a unsigned int?
245 if (ResultVal.isIntN(IntSize)) {
246 // Does it fit in a signed int?
247 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
248 t = Context.IntTy;
249 else if (AllowUnsigned)
250 t = Context.UnsignedIntTy;
251 }
252
253 if (!t.isNull())
254 ResultVal.trunc(IntSize);
255 }
256
257 // Are long/unsigned long possibilities?
258 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner3496d522007-09-04 02:45:27 +0000259 unsigned LongSize = static_cast<unsigned>(
260 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000261
262 // Does it fit in a unsigned long?
263 if (ResultVal.isIntN(LongSize)) {
264 // Does it fit in a signed long?
265 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
266 t = Context.LongTy;
267 else if (AllowUnsigned)
268 t = Context.UnsignedLongTy;
269 }
270 if (!t.isNull())
271 ResultVal.trunc(LongSize);
272 }
273
274 // Finally, check long long if needed.
275 if (t.isNull()) {
Chris Lattner3496d522007-09-04 02:45:27 +0000276 unsigned LongLongSize = static_cast<unsigned>(
277 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000278
279 // Does it fit in a unsigned long long?
280 if (ResultVal.isIntN(LongLongSize)) {
281 // Does it fit in a signed long long?
282 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
283 t = Context.LongLongTy;
284 else if (AllowUnsigned)
285 t = Context.UnsignedLongLongTy;
286 }
287 }
288
289 // If we still couldn't decide a type, we probably have something that
290 // does not fit in a signed long long, but has no U suffix.
291 if (t.isNull()) {
292 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
293 t = Context.UnsignedLongLongTy;
294 }
295 }
296
Chris Lattner1de66eb2007-08-26 03:42:43 +0000297 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000298 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000299
300 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
301 if (Literal.isImaginary)
302 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
303
304 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000305}
306
Steve Naroff87d58b42007-09-16 03:34:24 +0000307Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000308 ExprTy *Val) {
309 Expr *e = (Expr *)Val;
Steve Naroff87d58b42007-09-16 03:34:24 +0000310 assert((e != 0) && "ActOnParenExpr() missing expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000311 return new ParenExpr(L, R, e);
312}
313
314/// The UsualUnaryConversions() function is *not* called by this routine.
315/// See C99 6.3.2.1p[2-4] for more details.
316QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
317 SourceLocation OpLoc, bool isSizeof) {
318 // C99 6.5.3.4p1:
319 if (isa<FunctionType>(exprType) && isSizeof)
320 // alignof(function) is allowed.
321 Diag(OpLoc, diag::ext_sizeof_function_type);
322 else if (exprType->isVoidType())
323 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
324 else if (exprType->isIncompleteType()) {
325 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
326 diag::err_alignof_incomplete_type,
327 exprType.getAsString());
328 return QualType(); // error
329 }
330 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
331 return Context.getSizeType();
332}
333
334Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000335ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000336 SourceLocation LPLoc, TypeTy *Ty,
337 SourceLocation RPLoc) {
338 // If error parsing type, ignore.
339 if (Ty == 0) return true;
340
341 // Verify that this is a valid expression.
342 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
343
344 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
345
346 if (resultType.isNull())
347 return true;
348 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
349}
350
Chris Lattner5110ad52007-08-24 21:41:10 +0000351QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000352 DefaultFunctionArrayConversion(V);
353
Chris Lattnera16e42d2007-08-26 05:39:26 +0000354 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000355 if (const ComplexType *CT = V->getType()->getAsComplexType())
356 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000357
358 // Otherwise they pass through real integer and floating point types here.
359 if (V->getType()->isArithmeticType())
360 return V->getType();
361
362 // Reject anything else.
363 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
364 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000365}
366
367
Chris Lattner4b009652007-07-25 00:24:17 +0000368
Steve Naroff87d58b42007-09-16 03:34:24 +0000369Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000370 tok::TokenKind Kind,
371 ExprTy *Input) {
372 UnaryOperator::Opcode Opc;
373 switch (Kind) {
374 default: assert(0 && "Unknown unary op!");
375 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
376 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
377 }
378 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
379 if (result.isNull())
380 return true;
381 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
382}
383
384Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000385ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000386 ExprTy *Idx, SourceLocation RLoc) {
387 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
388
389 // Perform default conversions.
390 DefaultFunctionArrayConversion(LHSExp);
391 DefaultFunctionArrayConversion(RHSExp);
392
393 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
394
395 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000396 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000397 // in the subscript position. As a result, we need to derive the array base
398 // and index from the expression types.
399 Expr *BaseExpr, *IndexExpr;
400 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000401 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000402 BaseExpr = LHSExp;
403 IndexExpr = RHSExp;
404 // FIXME: need to deal with const...
405 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000406 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000407 // Handle the uncommon case of "123[Ptr]".
408 BaseExpr = RHSExp;
409 IndexExpr = LHSExp;
410 // FIXME: need to deal with const...
411 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000412 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
413 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000414 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000415
416 // Component access limited to variables (reject vec4.rg[1]).
417 if (!isa<DeclRefExpr>(BaseExpr))
418 return Diag(LLoc, diag::err_ocuvector_component_access,
419 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000420 // FIXME: need to deal with const...
421 ResultType = VTy->getElementType();
422 } else {
423 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
424 RHSExp->getSourceRange());
425 }
426 // C99 6.5.2.1p1
427 if (!IndexExpr->getType()->isIntegerType())
428 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
429 IndexExpr->getSourceRange());
430
431 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
432 // the following check catches trying to index a pointer to a function (e.g.
433 // void (*)(int)). Functions are not objects in C99.
434 if (!ResultType->isObjectType())
435 return Diag(BaseExpr->getLocStart(),
436 diag::err_typecheck_subscript_not_object,
437 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
438
439 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
440}
441
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000442QualType Sema::
443CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
444 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattnere35a1042007-07-31 19:29:30 +0000445 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000446
447 // The vector accessor can't exceed the number of elements.
448 const char *compStr = CompName.getName();
449 if (strlen(compStr) > vecType->getNumElements()) {
450 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
451 baseType.getAsString(), SourceRange(CompLoc));
452 return QualType();
453 }
454 // The component names must come from the same set.
Chris Lattner9096b792007-08-02 22:33:49 +0000455 if (vecType->getPointAccessorIdx(*compStr) != -1) {
456 do
457 compStr++;
458 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
459 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
460 do
461 compStr++;
462 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
463 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
464 do
465 compStr++;
466 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
467 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000468
469 if (*compStr) {
470 // We didn't get to the end of the string. This means the component names
471 // didn't come from the same set *or* we encountered an illegal name.
472 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
473 std::string(compStr,compStr+1), SourceRange(CompLoc));
474 return QualType();
475 }
476 // Each component accessor can't exceed the vector type.
477 compStr = CompName.getName();
478 while (*compStr) {
479 if (vecType->isAccessorWithinNumElements(*compStr))
480 compStr++;
481 else
482 break;
483 }
484 if (*compStr) {
485 // We didn't get to the end of the string. This means a component accessor
486 // exceeds the number of elements in the vector.
487 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
488 baseType.getAsString(), SourceRange(CompLoc));
489 return QualType();
490 }
491 // The component accessor looks fine - now we need to compute the actual type.
492 // The vector type is implied by the component accessor. For example,
493 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
494 unsigned CompSize = strlen(CompName.getName());
495 if (CompSize == 1)
496 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000497
498 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
499 // Now look up the TypeDefDecl from the vector type. Without this,
500 // diagostics look bad. We want OCU vector types to appear built-in.
501 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
502 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
503 return Context.getTypedefType(OCUVectorDecls[i]);
504 }
505 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000506}
507
Chris Lattner4b009652007-07-25 00:24:17 +0000508Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000509ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000510 tok::TokenKind OpKind, SourceLocation MemberLoc,
511 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000512 Expr *BaseExpr = static_cast<Expr *>(Base);
513 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000514
515 // Perform default conversions.
516 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000517
Steve Naroff2cb66382007-07-26 03:11:44 +0000518 QualType BaseType = BaseExpr->getType();
519 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000520
Chris Lattner4b009652007-07-25 00:24:17 +0000521 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000522 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000523 BaseType = PT->getPointeeType();
524 else
525 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
526 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000527 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000528 // The base type is either a record or an OCUVectorType.
Chris Lattnere35a1042007-07-31 19:29:30 +0000529 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000530 RecordDecl *RDecl = RTy->getDecl();
531 if (RTy->isIncompleteType())
532 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
533 BaseExpr->getSourceRange());
534 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000535 FieldDecl *MemberDecl = RDecl->getMember(&Member);
536 if (!MemberDecl)
Steve Naroff2cb66382007-07-26 03:11:44 +0000537 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
538 SourceRange(MemberLoc));
Eli Friedman76b49832008-02-06 22:48:16 +0000539
540 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000541 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000542 QualType MemberType = MemberDecl->getType();
543 unsigned combinedQualifiers =
544 MemberType.getQualifiers() | BaseType.getQualifiers();
545 MemberType = MemberType.getQualifiedType(combinedQualifiers);
546
547 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
548 MemberLoc, MemberType);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000549 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff89345522007-08-03 22:40:33 +0000550 // Component access limited to variables (reject vec4.rg.g).
551 if (!isa<DeclRefExpr>(BaseExpr))
552 return Diag(OpLoc, diag::err_ocuvector_component_access,
553 SourceRange(MemberLoc));
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000554 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
555 if (ret.isNull())
556 return true;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000557 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000558 } else if (BaseType->isObjCInterfaceType()) {
559 ObjCInterfaceDecl *IFace;
560 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
561 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000562 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000563 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
564 ObjCInterfaceDecl *clsDeclared;
565 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000566 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
567 OpKind==tok::arrow);
568 }
569 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
570 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000571}
572
Steve Naroff87d58b42007-09-16 03:34:24 +0000573/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000574/// This provides the location of the left/right parens and a list of comma
575/// locations.
576Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000577ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000578 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +0000579 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
580 Expr *Fn = static_cast<Expr *>(fn);
581 Expr **Args = reinterpret_cast<Expr**>(args);
582 assert(Fn && "no function call expression");
583
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000584 // Make the call expr early, before semantic checks. This guarantees cleanup
585 // of arguments and function on error.
586 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
587 Context.BoolTy, RParenLoc));
588
589 // Promote the function operand.
590 TheCall->setCallee(UsualUnaryConversions(Fn));
591
Chris Lattner4b009652007-07-25 00:24:17 +0000592 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
593 // type pointer to function".
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000594 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000595 if (PT == 0)
596 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
597 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000598 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
599 if (FuncT == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000600 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
601 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000602
603 // We know the result type of the call, set it.
604 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +0000605
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000606 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000607 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
608 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000609 unsigned NumArgsInProto = Proto->getNumArgs();
610 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +0000611
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000612 // If too few arguments are available, don't make the call.
613 if (NumArgs < NumArgsInProto)
614 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
615 Fn->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +0000616
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000617 // If too many are passed and not variadic, error on the extras and drop
618 // them.
619 if (NumArgs > NumArgsInProto) {
620 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000621 Diag(Args[NumArgsInProto]->getLocStart(),
622 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
623 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000624 Args[NumArgs-1]->getLocEnd()));
625 // This deletes the extra arguments.
626 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +0000627 }
628 NumArgsToCheck = NumArgsInProto;
629 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000630
Chris Lattner4b009652007-07-25 00:24:17 +0000631 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000632 for (unsigned i = 0; i != NumArgsToCheck; i++) {
633 Expr *Arg = Args[i];
Chris Lattner005ed752008-01-04 18:04:52 +0000634 QualType ProtoArgType = Proto->getArgType(i);
635 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000636
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000637 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +0000638 AssignConvertType ConvTy =
639 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000640 TheCall->setArg(i, Arg);
641
Chris Lattner005ed752008-01-04 18:04:52 +0000642 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
643 ArgType, Arg, "passing"))
644 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000645 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000646
647 // If this is a variadic call, handle args passed through "...".
648 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +0000649 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000650 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
651 Expr *Arg = Args[i];
652 DefaultArgumentPromotion(Arg);
653 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000654 }
Steve Naroffdb65e052007-08-28 23:30:39 +0000655 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000656 } else {
657 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
658
Steve Naroffdb65e052007-08-28 23:30:39 +0000659 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000660 for (unsigned i = 0; i != NumArgs; i++) {
661 Expr *Arg = Args[i];
662 DefaultArgumentPromotion(Arg);
663 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000664 }
Chris Lattner4b009652007-07-25 00:24:17 +0000665 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000666
Chris Lattner2e64c072007-08-10 20:18:51 +0000667 // Do special checking on direct calls to functions.
668 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
669 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
670 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000671 if (CheckFunctionCall(FDecl, TheCall.get()))
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000672 return true;
Chris Lattner2e64c072007-08-10 20:18:51 +0000673
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000674 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +0000675}
676
677Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000678ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000679 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000680 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000681 QualType literalType = QualType::getFromOpaquePtr(Ty);
682 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +0000683 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000684 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +0000685
Steve Naroffcb69fb72007-12-10 22:44:33 +0000686 // FIXME: add more semantic analysis (C99 6.5.2.5).
Steve Narofff0b23542008-01-10 22:15:12 +0000687 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +0000688 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +0000689
690 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
691 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +0000692 if (CheckForConstantInitializer(literalExpr, literalType))
693 return true;
694 }
Steve Naroffbe37fc02008-01-14 18:19:28 +0000695 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000696}
697
698Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000699ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +0000700 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +0000701 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000702
Steve Naroff0acc9c92007-09-15 18:49:24 +0000703 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +0000704 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +0000705
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000706 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
707 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
708 return e;
Chris Lattner4b009652007-07-25 00:24:17 +0000709}
710
Chris Lattnerd1f26b32007-12-20 00:44:32 +0000711bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000712 assert(VectorTy->isVectorType() && "Not a vector type!");
713
714 if (Ty->isVectorType() || Ty->isIntegerType()) {
715 if (Context.getTypeSize(VectorTy, SourceLocation()) !=
716 Context.getTypeSize(Ty, SourceLocation()))
717 return Diag(R.getBegin(),
718 Ty->isVectorType() ?
719 diag::err_invalid_conversion_between_vectors :
720 diag::err_invalid_conversion_between_vector_and_integer,
721 VectorTy.getAsString().c_str(),
722 Ty.getAsString().c_str(), R);
723 } else
724 return Diag(R.getBegin(),
725 diag::err_invalid_conversion_between_vector_and_scalar,
726 VectorTy.getAsString().c_str(),
727 Ty.getAsString().c_str(), R);
728
729 return false;
730}
731
Chris Lattner4b009652007-07-25 00:24:17 +0000732Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000733ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000734 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000735 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000736
737 Expr *castExpr = static_cast<Expr*>(Op);
738 QualType castType = QualType::getFromOpaquePtr(Ty);
739
Steve Naroff68adb482007-08-31 00:32:44 +0000740 UsualUnaryConversions(castExpr);
741
Chris Lattner4b009652007-07-25 00:24:17 +0000742 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
743 // type needs to be scalar.
Chris Lattnerdb526732007-10-29 04:26:44 +0000744 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Narofff459ee52008-01-24 22:55:05 +0000745 if (!castType->isScalarType() && !castType->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +0000746 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
747 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Steve Narofff459ee52008-01-24 22:55:05 +0000748 if (!castExpr->getType()->isScalarType() &&
749 !castExpr->getType()->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +0000750 return Diag(castExpr->getLocStart(),
751 diag::err_typecheck_expect_scalar_operand,
752 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000753
754 if (castExpr->getType()->isVectorType()) {
755 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
756 castExpr->getType(), castType))
757 return true;
758 } else if (castType->isVectorType()) {
759 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
760 castType, castExpr->getType()))
761 return true;
Chris Lattnerdb526732007-10-29 04:26:44 +0000762 }
Chris Lattner4b009652007-07-25 00:24:17 +0000763 }
764 return new CastExpr(castType, castExpr, LParenLoc);
765}
766
Chris Lattner98a425c2007-11-26 01:40:58 +0000767/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
768/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +0000769inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
770 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
771 UsualUnaryConversions(cond);
772 UsualUnaryConversions(lex);
773 UsualUnaryConversions(rex);
774 QualType condT = cond->getType();
775 QualType lexT = lex->getType();
776 QualType rexT = rex->getType();
777
778 // first, check the condition.
779 if (!condT->isScalarType()) { // C99 6.5.15p2
780 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
781 condT.getAsString());
782 return QualType();
783 }
Chris Lattner992ae932008-01-06 22:42:25 +0000784
785 // Now check the two expressions.
786
787 // If both operands have arithmetic type, do the usual arithmetic conversions
788 // to find a common type: C99 6.5.15p3,5.
789 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000790 UsualArithmeticConversions(lex, rex);
791 return lex->getType();
792 }
Chris Lattner992ae932008-01-06 22:42:25 +0000793
794 // If both operands are the same structure or union type, the result is that
795 // type.
Chris Lattner71225142007-07-31 21:27:01 +0000796 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +0000797 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +0000798 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +0000799 // "If both the operands have structure or union type, the result has
800 // that type." This implies that CV qualifiers are dropped.
801 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +0000802 }
Chris Lattner992ae932008-01-06 22:42:25 +0000803
804 // C99 6.5.15p5: "If both operands have void type, the result has void type."
805 if (lexT->isVoidType() && rexT->isVoidType())
806 return lexT.getUnqualifiedType();
Steve Naroff12ebf272008-01-08 01:11:38 +0000807
808 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
809 // the type of the other operand."
810 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000811 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000812 return lexT;
813 }
814 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000815 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000816 return rexT;
817 }
Chris Lattner0ac51632008-01-06 22:50:31 +0000818 // Handle the case where both operands are pointers before we handle null
819 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +0000820 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
821 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
822 // get the "pointed to" types
823 QualType lhptee = LHSPT->getPointeeType();
824 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000825
Chris Lattner71225142007-07-31 21:27:01 +0000826 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
827 if (lhptee->isVoidType() &&
Eli Friedmanca07c902008-02-10 22:59:36 +0000828 (rhptee->isObjectType() || rhptee->isIncompleteType())) {
829 // figure out necessary qualifiers (C99 6.5.15p6)
830 QualType destPointee = lhptee.getQualifiedType(rhptee.getQualifiers());
831 QualType destType = Context.getPointerType(destPointee);
832 ImpCastExprToType(lex, destType); // add qualifiers if necessary
833 ImpCastExprToType(rex, destType); // promote to void*
834 return destType;
835 }
Chris Lattner71225142007-07-31 21:27:01 +0000836 if (rhptee->isVoidType() &&
Eli Friedmanca07c902008-02-10 22:59:36 +0000837 (lhptee->isObjectType() || lhptee->isIncompleteType())) {
838 QualType destPointee = rhptee.getQualifiedType(lhptee.getQualifiers());
839 QualType destType = Context.getPointerType(destPointee);
840 ImpCastExprToType(lex, destType); // add qualifiers if necessary
841 ImpCastExprToType(rex, destType); // promote to void*
842 return destType;
843 }
Chris Lattner4b009652007-07-25 00:24:17 +0000844
Steve Naroff85f0dc52007-10-15 20:41:53 +0000845 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
846 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +0000847 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +0000848 lexT.getAsString(), rexT.getAsString(),
849 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman33284862008-01-30 17:02:03 +0000850 // In this situation, we assume void* type. No especially good
851 // reason, but this is what gcc does, and we do have to pick
852 // to get a consistent AST.
853 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
854 ImpCastExprToType(lex, voidPtrTy);
855 ImpCastExprToType(rex, voidPtrTy);
856 return voidPtrTy;
Chris Lattner71225142007-07-31 21:27:01 +0000857 }
858 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000859 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
860 // differently qualified versions of compatible types, the result type is
861 // a pointer to an appropriately qualified version of the *composite*
862 // type.
Chris Lattner0ac51632008-01-06 22:50:31 +0000863 // FIXME: Need to return the composite type.
Eli Friedmanca07c902008-02-10 22:59:36 +0000864 // FIXME: Need to add qualifiers
Chris Lattner0ac51632008-01-06 22:50:31 +0000865 return lexT;
Chris Lattner4b009652007-07-25 00:24:17 +0000866 }
Chris Lattner4b009652007-07-25 00:24:17 +0000867 }
Chris Lattner71225142007-07-31 21:27:01 +0000868
Chris Lattner992ae932008-01-06 22:42:25 +0000869 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +0000870 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
871 lexT.getAsString(), rexT.getAsString(),
872 lex->getSourceRange(), rex->getSourceRange());
873 return QualType();
874}
875
Steve Naroff87d58b42007-09-16 03:34:24 +0000876/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +0000877/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +0000878Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000879 SourceLocation ColonLoc,
880 ExprTy *Cond, ExprTy *LHS,
881 ExprTy *RHS) {
882 Expr *CondExpr = (Expr *) Cond;
883 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +0000884
885 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
886 // was the condition.
887 bool isLHSNull = LHSExpr == 0;
888 if (isLHSNull)
889 LHSExpr = CondExpr;
890
Chris Lattner4b009652007-07-25 00:24:17 +0000891 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
892 RHSExpr, QuestionLoc);
893 if (result.isNull())
894 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +0000895 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
896 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +0000897}
898
Steve Naroffdb65e052007-08-28 23:30:39 +0000899/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffbbaed752008-01-29 02:42:22 +0000900/// do not have a prototype. Arguments that have type float are promoted to
901/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000902void Sema::DefaultArgumentPromotion(Expr *&Expr) {
903 QualType Ty = Expr->getType();
904 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffdb65e052007-08-28 23:30:39 +0000905
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000906 if (Ty == Context.FloatTy)
Chris Lattnere992d6c2008-01-16 19:17:22 +0000907 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroffbbaed752008-01-29 02:42:22 +0000908 else
909 UsualUnaryConversions(Expr);
Steve Naroffdb65e052007-08-28 23:30:39 +0000910}
911
Chris Lattner4b009652007-07-25 00:24:17 +0000912/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
913void Sema::DefaultFunctionArrayConversion(Expr *&e) {
914 QualType t = e->getType();
915 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
916
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000917 if (const ReferenceType *ref = t->getAsReferenceType()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000918 ImpCastExprToType(e, ref->getReferenceeType()); // C++ [expr]
Chris Lattner4b009652007-07-25 00:24:17 +0000919 t = e->getType();
920 }
921 if (t->isFunctionType())
Chris Lattnere992d6c2008-01-16 19:17:22 +0000922 ImpCastExprToType(e, Context.getPointerType(t));
Steve Naroffac26e9a2008-02-09 16:59:44 +0000923 else if (const ArrayType *ary = t->getAsArrayType()) {
Steve Naroff9ffeda12008-02-09 17:25:18 +0000924 // Make sure we don't lose qualifiers when dealing with typedefs. Example:
Steve Naroffac26e9a2008-02-09 16:59:44 +0000925 // typedef int arr[10];
926 // void test2() {
927 // const arr b;
928 // b[4] = 1;
929 // }
930 QualType ELT = ary->getElementType();
931 ELT = ELT.getQualifiedType(t.getQualifiers()|ELT.getQualifiers());
932 ImpCastExprToType(e, Context.getPointerType(ELT));
933 }
Chris Lattner4b009652007-07-25 00:24:17 +0000934}
935
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000936/// UsualUnaryConversions - Performs various conversions that are common to most
Chris Lattner4b009652007-07-25 00:24:17 +0000937/// operators (C99 6.3). The conversions of array and function types are
938/// sometimes surpressed. For example, the array->pointer conversion doesn't
939/// apply if the array is an argument to the sizeof or address (&) operators.
940/// In these instances, this routine should *not* be called.
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000941Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
942 QualType Ty = Expr->getType();
943 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000944
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000945 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000946 ImpCastExprToType(Expr, Ref->getReferenceeType()); // C++ [expr]
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000947 Ty = Expr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000948 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000949 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattnere992d6c2008-01-16 19:17:22 +0000950 ImpCastExprToType(Expr, Context.IntTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000951 else
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000952 DefaultFunctionArrayConversion(Expr);
953
954 return Expr;
Chris Lattner4b009652007-07-25 00:24:17 +0000955}
956
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000957/// UsualArithmeticConversions - Performs various conversions that are common to
Chris Lattner4b009652007-07-25 00:24:17 +0000958/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
959/// routine returns the first non-arithmetic type found. The client is
960/// responsible for emitting appropriate error diagnostics.
Steve Naroffe8419ca2008-01-15 22:21:49 +0000961/// FIXME: verify the conversion rules for "complex int" are consistent with GCC.
Steve Naroff8f708362007-08-24 19:07:16 +0000962QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
963 bool isCompAssign) {
Steve Naroffb2f9f552007-08-25 19:54:59 +0000964 if (!isCompAssign) {
965 UsualUnaryConversions(lhsExpr);
966 UsualUnaryConversions(rhsExpr);
967 }
Steve Naroff7438fdf2007-10-18 18:55:53 +0000968 // For conversion purposes, we ignore any qualifiers.
969 // For example, "const float" and "float" are equivalent.
Steve Naroff1ddb6f52007-11-10 19:45:54 +0000970 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
971 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +0000972
973 // If both types are identical, no conversion is needed.
Steve Naroff7438fdf2007-10-18 18:55:53 +0000974 if (lhs == rhs)
975 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000976
977 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
978 // The caller can deal with this (e.g. pointer + int).
979 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +0000980 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000981
982 // At this point, we have two different arithmetic types.
983
984 // Handle complex types first (C99 6.3.1.8p1).
985 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff43001212008-01-15 19:36:10 +0000986 // if we have an integer operand, the result is the complex type.
Steve Naroffe8419ca2008-01-15 22:21:49 +0000987 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +0000988 // convert the rhs to the lhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +0000989 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +0000990 return lhs;
Steve Naroff43001212008-01-15 19:36:10 +0000991 }
Steve Naroffe8419ca2008-01-15 22:21:49 +0000992 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +0000993 // convert the lhs to the rhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +0000994 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +0000995 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000996 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000997 // This handles complex/complex, complex/float, or float/complex.
998 // When both operands are complex, the shorter operand is converted to the
999 // type of the longer, and that is the type of the result. This corresponds
1000 // to what is done when combining two real floating-point operands.
1001 // The fun begins when size promotion occur across type domains.
1002 // From H&S 6.3.4: When one operand is complex and the other is a real
1003 // floating-point type, the less precise type is converted, within it's
1004 // real or complex domain, to the precision of the other type. For example,
1005 // when combining a "long double" with a "double _Complex", the
1006 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff45fc9822007-08-27 15:30:22 +00001007 int result = Context.compareFloatingType(lhs, rhs);
1008
1009 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff3b565d62007-08-27 21:32:55 +00001010 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1011 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001012 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001013 } else if (result < 0) { // The right side is bigger, convert lhs.
1014 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1015 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001016 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001017 }
1018 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1019 // domains match. This is a requirement for our implementation, C99
1020 // does not require this promotion.
1021 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1022 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001023 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001024 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001025 return rhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001026 } else { // handle "_Complex double, double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001027 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001028 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001029 return lhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001030 }
Chris Lattner4b009652007-07-25 00:24:17 +00001031 }
Steve Naroff3b6157f2007-08-27 21:43:43 +00001032 return lhs; // The domain/size match exactly.
Chris Lattner4b009652007-07-25 00:24:17 +00001033 }
1034 // Now handle "real" floating types (i.e. float, double, long double).
1035 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1036 // if we have an integer operand, the result is the real floating type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001037 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001038 // convert rhs to the lhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001039 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001040 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001041 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001042 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001043 // convert lhs to the rhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001044 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001045 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001046 }
1047 // We have two real floating types, float/complex combos were handled above.
1048 // Convert the smaller operand to the bigger result.
Steve Naroff45fc9822007-08-27 15:30:22 +00001049 int result = Context.compareFloatingType(lhs, rhs);
1050
1051 if (result > 0) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001052 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001053 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001054 }
Steve Naroff45fc9822007-08-27 15:30:22 +00001055 if (result < 0) { // convert the lhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001056 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff45fc9822007-08-27 15:30:22 +00001057 return rhs;
1058 }
1059 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Chris Lattner4b009652007-07-25 00:24:17 +00001060 }
Steve Naroff43001212008-01-15 19:36:10 +00001061 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1062 // Handle GCC complex int extension.
Steve Naroff43001212008-01-15 19:36:10 +00001063 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman50727042008-02-08 01:19:44 +00001064 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff43001212008-01-15 19:36:10 +00001065
Eli Friedman50727042008-02-08 01:19:44 +00001066 if (lhsComplexInt && rhsComplexInt) {
1067 if (Context.maxIntegerType(lhsComplexInt->getElementType(),
Eli Friedman94075c02008-02-08 01:24:30 +00001068 rhsComplexInt->getElementType()) == lhs) {
1069 // convert the rhs
1070 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1071 return lhs;
Eli Friedman50727042008-02-08 01:19:44 +00001072 }
1073 if (!isCompAssign)
Eli Friedman94075c02008-02-08 01:24:30 +00001074 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman50727042008-02-08 01:19:44 +00001075 return rhs;
1076 } else if (lhsComplexInt && rhs->isIntegerType()) {
1077 // convert the rhs to the lhs complex type.
1078 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1079 return lhs;
1080 } else if (rhsComplexInt && lhs->isIntegerType()) {
1081 // convert the lhs to the rhs complex type.
1082 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1083 return rhs;
1084 }
Steve Naroff43001212008-01-15 19:36:10 +00001085 }
Chris Lattner4b009652007-07-25 00:24:17 +00001086 // Finally, we have two differing integer types.
1087 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001088 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001089 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001090 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001091 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff8f708362007-08-24 19:07:16 +00001092 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001093}
1094
1095// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1096// being closely modeled after the C99 spec:-). The odd characteristic of this
1097// routine is it effectively iqnores the qualifiers on the top level pointee.
1098// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1099// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001100Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001101Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1102 QualType lhptee, rhptee;
1103
1104 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001105 lhptee = lhsType->getAsPointerType()->getPointeeType();
1106 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001107
1108 // make sure we operate on the canonical type
1109 lhptee = lhptee.getCanonicalType();
1110 rhptee = rhptee.getCanonicalType();
1111
Chris Lattner005ed752008-01-04 18:04:52 +00001112 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001113
1114 // C99 6.5.16.1p1: This following citation is common to constraints
1115 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1116 // qualifiers of the type *pointed to* by the right;
1117 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
1118 rhptee.getQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001119 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001120
1121 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1122 // incomplete type and the other is a pointer to a qualified or unqualified
1123 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001124 if (lhptee->isVoidType()) {
1125 if (rhptee->isObjectType() || rhptee->isIncompleteType())
Chris Lattner005ed752008-01-04 18:04:52 +00001126 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001127
1128 // As an extension, we allow cast to/from void* to function pointer.
1129 if (rhptee->isFunctionType())
1130 return FunctionVoidPointer;
1131 }
1132
1133 if (rhptee->isVoidType()) {
1134 if (lhptee->isObjectType() || lhptee->isIncompleteType())
Chris Lattner005ed752008-01-04 18:04:52 +00001135 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001136
1137 // As an extension, we allow cast to/from void* to function pointer.
1138 if (lhptee->isFunctionType())
1139 return FunctionVoidPointer;
1140 }
1141
Chris Lattner4b009652007-07-25 00:24:17 +00001142 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1143 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001144 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1145 rhptee.getUnqualifiedType()))
1146 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001147 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001148}
1149
1150/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1151/// has code to accommodate several GCC extensions when type checking
1152/// pointers. Here are some objectionable examples that GCC considers warnings:
1153///
1154/// int a, *pint;
1155/// short *pshort;
1156/// struct foo *pfoo;
1157///
1158/// pint = pshort; // warning: assignment from incompatible pointer type
1159/// a = pint; // warning: assignment makes integer from pointer without a cast
1160/// pint = a; // warning: assignment makes pointer from integer without a cast
1161/// pint = pfoo; // warning: assignment from incompatible pointer type
1162///
1163/// As a result, the code for dealing with pointers is more complex than the
1164/// C99 spec dictates.
1165/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1166///
Chris Lattner005ed752008-01-04 18:04:52 +00001167Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001168Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001169 // Get canonical types. We're not formatting these types, just comparing
1170 // them.
1171 lhsType = lhsType.getCanonicalType();
1172 rhsType = rhsType.getCanonicalType();
1173
1174 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001175 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001176
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001177 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001178 if (Context.referenceTypesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001179 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001180 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001181 }
Chris Lattner1853da22008-01-04 23:18:45 +00001182
Ted Kremenek42730c52008-01-07 19:49:32 +00001183 if (lhsType->isObjCQualifiedIdType()
1184 || rhsType->isObjCQualifiedIdType()) {
1185 if (Context.ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001186 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001187 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001188 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001189
1190 if (lhsType->isVectorType() || rhsType->isVectorType()) {
1191 // For OCUVector, allow vector splats; float -> <n x float>
1192 if (const OCUVectorType *LV = lhsType->getAsOCUVectorType()) {
1193 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1194 return Compatible;
1195 }
1196
1197 // If LHS and RHS are both vectors of integer or both vectors of floating
1198 // point types, and the total vector length is the same, allow the
1199 // conversion. This is a bitcast; no bits are changed but the result type
1200 // is different.
1201 if (getLangOptions().LaxVectorConversions &&
1202 lhsType->isVectorType() && rhsType->isVectorType()) {
1203 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1204 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
1205 if (Context.getTypeSize(lhsType, SourceLocation()) ==
1206 Context.getTypeSize(rhsType, SourceLocation()))
Nate Begemanec2d1062007-12-30 02:59:45 +00001207 return Compatible;
1208 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001209 }
1210 return Incompatible;
1211 }
1212
1213 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001214 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001215
1216 if (lhsType->isPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001217 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001218 return IntToPointer;
Chris Lattner4b009652007-07-25 00:24:17 +00001219
1220 if (rhsType->isPointerType())
1221 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001222 return Incompatible;
1223 }
1224
1225 if (rhsType->isPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001226 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1227 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001228 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001229
1230 if (lhsType->isPointerType())
1231 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001232 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001233 }
1234
1235 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001236 if (Context.tagTypesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001237 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001238 }
1239 return Incompatible;
1240}
1241
Chris Lattner005ed752008-01-04 18:04:52 +00001242Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001243Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001244 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1245 // a null pointer constant.
Ted Kremenek42730c52008-01-07 19:49:32 +00001246 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001247 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001248 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001249 return Compatible;
1250 }
Chris Lattner5f505bf2007-10-16 02:55:40 +00001251 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001252 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001253 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001254 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001255 //
1256 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1257 // are better understood.
1258 if (!lhsType->isReferenceType())
1259 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001260
Chris Lattner005ed752008-01-04 18:04:52 +00001261 Sema::AssignConvertType result =
1262 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001263
1264 // C99 6.5.16.1p2: The value of the right operand is converted to the
1265 // type of the assignment expression.
1266 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001267 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001268 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001269}
1270
Chris Lattner005ed752008-01-04 18:04:52 +00001271Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001272Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1273 return CheckAssignmentConstraints(lhsType, rhsType);
1274}
1275
Chris Lattner2c8bff72007-12-12 05:47:28 +00001276QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001277 Diag(loc, diag::err_typecheck_invalid_operands,
1278 lex->getType().getAsString(), rex->getType().getAsString(),
1279 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001280 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001281}
1282
1283inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1284 Expr *&rex) {
1285 QualType lhsType = lex->getType(), rhsType = rex->getType();
1286
1287 // make sure the vector types are identical.
1288 if (lhsType == rhsType)
1289 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001290
1291 // if the lhs is an ocu vector and the rhs is a scalar of the same type,
1292 // promote the rhs to the vector type.
1293 if (const OCUVectorType *V = lhsType->getAsOCUVectorType()) {
1294 if (V->getElementType().getCanonicalType().getTypePtr()
1295 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001296 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001297 return lhsType;
1298 }
1299 }
1300
1301 // if the rhs is an ocu vector and the lhs is a scalar of the same type,
1302 // promote the lhs to the vector type.
1303 if (const OCUVectorType *V = rhsType->getAsOCUVectorType()) {
1304 if (V->getElementType().getCanonicalType().getTypePtr()
1305 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001306 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001307 return rhsType;
1308 }
1309 }
1310
Chris Lattner4b009652007-07-25 00:24:17 +00001311 // You cannot convert between vector values of different size.
1312 Diag(loc, diag::err_typecheck_vector_not_convertable,
1313 lex->getType().getAsString(), rex->getType().getAsString(),
1314 lex->getSourceRange(), rex->getSourceRange());
1315 return QualType();
1316}
1317
1318inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001319 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001320{
1321 QualType lhsType = lex->getType(), rhsType = rex->getType();
1322
1323 if (lhsType->isVectorType() || rhsType->isVectorType())
1324 return CheckVectorOperands(loc, lex, rex);
1325
Steve Naroff8f708362007-08-24 19:07:16 +00001326 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001327
Chris Lattner4b009652007-07-25 00:24:17 +00001328 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001329 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001330 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001331}
1332
1333inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001334 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001335{
1336 QualType lhsType = lex->getType(), rhsType = rex->getType();
1337
Steve Naroff8f708362007-08-24 19:07:16 +00001338 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001339
Chris Lattner4b009652007-07-25 00:24:17 +00001340 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001341 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001342 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001343}
1344
1345inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001346 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001347{
1348 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1349 return CheckVectorOperands(loc, lex, rex);
1350
Steve Naroff8f708362007-08-24 19:07:16 +00001351 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001352
1353 // handle the common case first (both operands are arithmetic).
1354 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001355 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001356
1357 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1358 return lex->getType();
1359 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1360 return rex->getType();
Chris Lattner2c8bff72007-12-12 05:47:28 +00001361 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001362}
1363
1364inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001365 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001366{
1367 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1368 return CheckVectorOperands(loc, lex, rex);
1369
Steve Naroff8f708362007-08-24 19:07:16 +00001370 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001371
Chris Lattnerf6da2912007-12-09 21:53:25 +00001372 // Enforce type constraints: C99 6.5.6p3.
1373
1374 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001375 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001376 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001377
1378 // Either ptr - int or ptr - ptr.
1379 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001380 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001381
Chris Lattnerf6da2912007-12-09 21:53:25 +00001382 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001383 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001384 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001385 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001386 Diag(loc, diag::ext_gnu_void_ptr,
1387 lex->getSourceRange(), rex->getSourceRange());
1388 } else {
1389 Diag(loc, diag::err_typecheck_sub_ptr_object,
1390 lex->getType().getAsString(), lex->getSourceRange());
1391 return QualType();
1392 }
1393 }
1394
1395 // The result type of a pointer-int computation is the pointer type.
1396 if (rex->getType()->isIntegerType())
1397 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001398
Chris Lattnerf6da2912007-12-09 21:53:25 +00001399 // Handle pointer-pointer subtractions.
1400 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001401 QualType rpointee = RHSPTy->getPointeeType();
1402
Chris Lattnerf6da2912007-12-09 21:53:25 +00001403 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001404 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001405 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001406 if (rpointee->isVoidType()) {
1407 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-12-09 21:53:25 +00001408 Diag(loc, diag::ext_gnu_void_ptr,
1409 lex->getSourceRange(), rex->getSourceRange());
1410 } else {
1411 Diag(loc, diag::err_typecheck_sub_ptr_object,
1412 rex->getType().getAsString(), rex->getSourceRange());
1413 return QualType();
1414 }
1415 }
1416
1417 // Pointee types must be compatible.
Steve Naroff577f9722008-01-29 18:58:14 +00001418 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1419 rpointee.getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001420 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1421 lex->getType().getAsString(), rex->getType().getAsString(),
1422 lex->getSourceRange(), rex->getSourceRange());
1423 return QualType();
1424 }
1425
1426 return Context.getPointerDiffType();
1427 }
1428 }
1429
Chris Lattner2c8bff72007-12-12 05:47:28 +00001430 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001431}
1432
1433inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Chris Lattner2c8bff72007-12-12 05:47:28 +00001434 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) {
1435 // C99 6.5.7p2: Each of the operands shall have integer type.
1436 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1437 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001438
Chris Lattner2c8bff72007-12-12 05:47:28 +00001439 // Shifts don't perform usual arithmetic conversions, they just do integer
1440 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001441 if (!isCompAssign)
1442 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001443 UsualUnaryConversions(rex);
1444
1445 // "The type of the result is that of the promoted left operand."
1446 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001447}
1448
Chris Lattner254f3bc2007-08-26 01:18:55 +00001449inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1450 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Chris Lattner4b009652007-07-25 00:24:17 +00001451{
Chris Lattner254f3bc2007-08-26 01:18:55 +00001452 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001453 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1454 UsualArithmeticConversions(lex, rex);
1455 else {
1456 UsualUnaryConversions(lex);
1457 UsualUnaryConversions(rex);
1458 }
Chris Lattner4b009652007-07-25 00:24:17 +00001459 QualType lType = lex->getType();
1460 QualType rType = rex->getType();
1461
Ted Kremenek486509e2007-10-29 17:13:39 +00001462 // For non-floating point types, check for self-comparisons of the form
1463 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1464 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001465 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001466 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1467 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001468 if (DRL->getDecl() == DRR->getDecl())
1469 Diag(loc, diag::warn_selfcomparison);
1470 }
1471
Chris Lattner254f3bc2007-08-26 01:18:55 +00001472 if (isRelational) {
1473 if (lType->isRealType() && rType->isRealType())
1474 return Context.IntTy;
1475 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001476 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001477 if (lType->isFloatingType()) {
1478 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001479 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001480 }
1481
Chris Lattner254f3bc2007-08-26 01:18:55 +00001482 if (lType->isArithmeticType() && rType->isArithmeticType())
1483 return Context.IntTy;
1484 }
Chris Lattner4b009652007-07-25 00:24:17 +00001485
Chris Lattner22be8422007-08-26 01:10:14 +00001486 bool LHSIsNull = lex->isNullPointerConstant(Context);
1487 bool RHSIsNull = rex->isNullPointerConstant(Context);
1488
Chris Lattner254f3bc2007-08-26 01:18:55 +00001489 // All of the following pointer related warnings are GCC extensions, except
1490 // when handling null pointer constants. One day, we can consider making them
1491 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001492 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Eli Friedman50727042008-02-08 01:19:44 +00001493 QualType lpointee = lType->getAsPointerType()->getPointeeType();
1494 QualType rpointee = rType->getAsPointerType()->getPointeeType();
1495
Steve Naroff3b435622007-11-13 14:57:38 +00001496 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Steve Naroff577f9722008-01-29 18:58:14 +00001497 !lpointee->isVoidType() && !lpointee->isVoidType() &&
1498 !Context.typesAreCompatible(lpointee.getUnqualifiedType(),
Eli Friedman50727042008-02-08 01:19:44 +00001499 rpointee.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001500 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1501 lType.getAsString(), rType.getAsString(),
1502 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001503 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001504 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001505 return Context.IntTy;
1506 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001507 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
1508 && Context.ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001509 ImpCastExprToType(rex, lType);
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001510 return Context.IntTy;
1511 }
Steve Naroff4462cb02007-08-16 21:48:38 +00001512 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001513 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001514 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1515 lType.getAsString(), rType.getAsString(),
1516 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001517 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001518 return Context.IntTy;
1519 }
1520 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001521 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001522 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1523 lType.getAsString(), rType.getAsString(),
1524 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001525 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001526 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001527 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00001528 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001529}
1530
Chris Lattner4b009652007-07-25 00:24:17 +00001531inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001532 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001533{
1534 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1535 return CheckVectorOperands(loc, lex, rex);
1536
Steve Naroff8f708362007-08-24 19:07:16 +00001537 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001538
1539 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001540 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001541 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001542}
1543
1544inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1545 Expr *&lex, Expr *&rex, SourceLocation loc)
1546{
1547 UsualUnaryConversions(lex);
1548 UsualUnaryConversions(rex);
1549
1550 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
1551 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001552 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001553}
1554
1555inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001556 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001557{
1558 QualType lhsType = lex->getType();
1559 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner4b009652007-07-25 00:24:17 +00001560 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1561
1562 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00001563 case Expr::MLV_Valid:
1564 break;
1565 case Expr::MLV_ConstQualified:
1566 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1567 return QualType();
1568 case Expr::MLV_ArrayType:
1569 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1570 lhsType.getAsString(), lex->getSourceRange());
1571 return QualType();
1572 case Expr::MLV_NotObjectType:
1573 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1574 lhsType.getAsString(), lex->getSourceRange());
1575 return QualType();
1576 case Expr::MLV_InvalidExpression:
1577 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1578 lex->getSourceRange());
1579 return QualType();
1580 case Expr::MLV_IncompleteType:
1581 case Expr::MLV_IncompleteVoidType:
1582 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1583 lhsType.getAsString(), lex->getSourceRange());
1584 return QualType();
1585 case Expr::MLV_DuplicateVectorComponents:
1586 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1587 lex->getSourceRange());
1588 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001589 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001590
Chris Lattner005ed752008-01-04 18:04:52 +00001591 AssignConvertType ConvTy;
1592 if (compoundType.isNull())
1593 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1594 else
1595 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1596
1597 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1598 rex, "assigning"))
1599 return QualType();
1600
Chris Lattner4b009652007-07-25 00:24:17 +00001601 // C99 6.5.16p3: The type of an assignment expression is the type of the
1602 // left operand unless the left operand has qualified type, in which case
1603 // it is the unqualified version of the type of the left operand.
1604 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1605 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001606 // C++ 5.17p1: the type of the assignment expression is that of its left
1607 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00001608 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001609}
1610
1611inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1612 Expr *&lex, Expr *&rex, SourceLocation loc) {
1613 UsualUnaryConversions(rex);
1614 return rex->getType();
1615}
1616
1617/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1618/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1619QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1620 QualType resType = op->getType();
1621 assert(!resType.isNull() && "no type for increment/decrement expression");
1622
Steve Naroffd30e1932007-08-24 17:20:07 +00001623 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00001624 if (const PointerType *pt = resType->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001625 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
1626 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1627 resType.getAsString(), op->getSourceRange());
1628 return QualType();
1629 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001630 } else if (!resType->isRealType()) {
1631 if (resType->isComplexType())
1632 // C99 does not support ++/-- on complex types.
1633 Diag(OpLoc, diag::ext_integer_increment_complex,
1634 resType.getAsString(), op->getSourceRange());
1635 else {
1636 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1637 resType.getAsString(), op->getSourceRange());
1638 return QualType();
1639 }
Chris Lattner4b009652007-07-25 00:24:17 +00001640 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001641 // At this point, we know we have a real, complex or pointer type.
1642 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001643 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1644 if (mlval != Expr::MLV_Valid) {
1645 // FIXME: emit a more precise diagnostic...
1646 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1647 op->getSourceRange());
1648 return QualType();
1649 }
1650 return resType;
1651}
1652
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001653/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00001654/// This routine allows us to typecheck complex/recursive expressions
1655/// where the declaration is needed for type checking. Here are some
1656/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001657static ValueDecl *getPrimaryDecl(Expr *e) {
Chris Lattner4b009652007-07-25 00:24:17 +00001658 switch (e->getStmtClass()) {
1659 case Stmt::DeclRefExprClass:
1660 return cast<DeclRefExpr>(e)->getDecl();
1661 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00001662 // Fields cannot be declared with a 'register' storage class.
1663 // &X->f is always ok, even if X is declared register.
1664 if (cast<MemberExpr>(e)->isArrow())
1665 return 0;
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001666 return getPrimaryDecl(cast<MemberExpr>(e)->getBase());
1667 case Stmt::ArraySubscriptExprClass: {
1668 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1669
1670 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(e)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00001671 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001672 return 0;
1673 else
1674 return VD;
1675 }
Chris Lattner4b009652007-07-25 00:24:17 +00001676 case Stmt::UnaryOperatorClass:
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001677 return getPrimaryDecl(cast<UnaryOperator>(e)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001678 case Stmt::ParenExprClass:
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001679 return getPrimaryDecl(cast<ParenExpr>(e)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00001680 case Stmt::ImplicitCastExprClass:
1681 // &X[4] when X is an array, has an implicit cast from array to pointer.
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001682 return getPrimaryDecl(cast<ImplicitCastExpr>(e)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001683 default:
1684 return 0;
1685 }
1686}
1687
1688/// CheckAddressOfOperand - The operand of & must be either a function
1689/// designator or an lvalue designating an object. If it is an lvalue, the
1690/// object cannot be declared with storage class register or be a bit field.
1691/// Note: The usual conversions are *not* applied to the operand of the &
1692/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1693QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001694 if (getLangOptions().C99) {
1695 // Implement C99-only parts of addressof rules.
1696 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1697 if (uOp->getOpcode() == UnaryOperator::Deref)
1698 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1699 // (assuming the deref expression is valid).
1700 return uOp->getSubExpr()->getType();
1701 }
1702 // Technically, there should be a check for array subscript
1703 // expressions here, but the result of one is always an lvalue anyway.
1704 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001705 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner4b009652007-07-25 00:24:17 +00001706 Expr::isLvalueResult lval = op->isLvalue();
1707
1708 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00001709 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1710 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00001711 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1712 op->getSourceRange());
1713 return QualType();
1714 }
1715 } else if (dcl) {
1716 // We have an lvalue with a decl. Make sure the decl is not declared
1717 // with the register storage-class specifier.
1718 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1719 if (vd->getStorageClass() == VarDecl::Register) {
1720 Diag(OpLoc, diag::err_typecheck_address_of_register,
1721 op->getSourceRange());
1722 return QualType();
1723 }
1724 } else
1725 assert(0 && "Unknown/unexpected decl type");
1726
1727 // FIXME: add check for bitfields!
1728 }
1729 // If the operand has type "type", the result has type "pointer to type".
1730 return Context.getPointerType(op->getType());
1731}
1732
1733QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
1734 UsualUnaryConversions(op);
1735 QualType qType = op->getType();
1736
Chris Lattner7931f4a2007-07-31 16:53:04 +00001737 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001738 // Note that per both C89 and C99, this is always legal, even
1739 // if ptype is an incomplete type or void.
1740 // It would be possible to warn about dereferencing a
1741 // void pointer, but it's completely well-defined,
1742 // and such a warning is unlikely to catch any mistakes.
1743 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001744 }
1745 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1746 qType.getAsString(), op->getSourceRange());
1747 return QualType();
1748}
1749
1750static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1751 tok::TokenKind Kind) {
1752 BinaryOperator::Opcode Opc;
1753 switch (Kind) {
1754 default: assert(0 && "Unknown binop!");
1755 case tok::star: Opc = BinaryOperator::Mul; break;
1756 case tok::slash: Opc = BinaryOperator::Div; break;
1757 case tok::percent: Opc = BinaryOperator::Rem; break;
1758 case tok::plus: Opc = BinaryOperator::Add; break;
1759 case tok::minus: Opc = BinaryOperator::Sub; break;
1760 case tok::lessless: Opc = BinaryOperator::Shl; break;
1761 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1762 case tok::lessequal: Opc = BinaryOperator::LE; break;
1763 case tok::less: Opc = BinaryOperator::LT; break;
1764 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1765 case tok::greater: Opc = BinaryOperator::GT; break;
1766 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1767 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1768 case tok::amp: Opc = BinaryOperator::And; break;
1769 case tok::caret: Opc = BinaryOperator::Xor; break;
1770 case tok::pipe: Opc = BinaryOperator::Or; break;
1771 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1772 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1773 case tok::equal: Opc = BinaryOperator::Assign; break;
1774 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1775 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1776 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1777 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1778 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1779 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1780 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1781 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1782 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1783 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1784 case tok::comma: Opc = BinaryOperator::Comma; break;
1785 }
1786 return Opc;
1787}
1788
1789static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1790 tok::TokenKind Kind) {
1791 UnaryOperator::Opcode Opc;
1792 switch (Kind) {
1793 default: assert(0 && "Unknown unary op!");
1794 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1795 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1796 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1797 case tok::star: Opc = UnaryOperator::Deref; break;
1798 case tok::plus: Opc = UnaryOperator::Plus; break;
1799 case tok::minus: Opc = UnaryOperator::Minus; break;
1800 case tok::tilde: Opc = UnaryOperator::Not; break;
1801 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1802 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1803 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1804 case tok::kw___real: Opc = UnaryOperator::Real; break;
1805 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1806 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1807 }
1808 return Opc;
1809}
1810
1811// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00001812Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00001813 ExprTy *LHS, ExprTy *RHS) {
1814 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1815 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1816
Steve Naroff87d58b42007-09-16 03:34:24 +00001817 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1818 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00001819
1820 QualType ResultTy; // Result type of the binary operator.
1821 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
1822
1823 switch (Opc) {
1824 default:
1825 assert(0 && "Unknown binary expr!");
1826 case BinaryOperator::Assign:
1827 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
1828 break;
1829 case BinaryOperator::Mul:
1830 case BinaryOperator::Div:
1831 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1832 break;
1833 case BinaryOperator::Rem:
1834 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1835 break;
1836 case BinaryOperator::Add:
1837 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1838 break;
1839 case BinaryOperator::Sub:
1840 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1841 break;
1842 case BinaryOperator::Shl:
1843 case BinaryOperator::Shr:
1844 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
1845 break;
1846 case BinaryOperator::LE:
1847 case BinaryOperator::LT:
1848 case BinaryOperator::GE:
1849 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00001850 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001851 break;
1852 case BinaryOperator::EQ:
1853 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00001854 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001855 break;
1856 case BinaryOperator::And:
1857 case BinaryOperator::Xor:
1858 case BinaryOperator::Or:
1859 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1860 break;
1861 case BinaryOperator::LAnd:
1862 case BinaryOperator::LOr:
1863 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
1864 break;
1865 case BinaryOperator::MulAssign:
1866 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001867 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001868 if (!CompTy.isNull())
1869 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1870 break;
1871 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001872 CompTy = CheckRemainderOperands(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::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001877 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001878 if (!CompTy.isNull())
1879 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1880 break;
1881 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001882 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001883 if (!CompTy.isNull())
1884 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1885 break;
1886 case BinaryOperator::ShlAssign:
1887 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001888 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001889 if (!CompTy.isNull())
1890 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1891 break;
1892 case BinaryOperator::AndAssign:
1893 case BinaryOperator::XorAssign:
1894 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001895 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001896 if (!CompTy.isNull())
1897 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1898 break;
1899 case BinaryOperator::Comma:
1900 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
1901 break;
1902 }
1903 if (ResultTy.isNull())
1904 return true;
1905 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00001906 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001907 else
Chris Lattnerf420df12007-08-28 18:36:55 +00001908 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001909}
1910
1911// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00001912Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00001913 ExprTy *input) {
1914 Expr *Input = (Expr*)input;
1915 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1916 QualType resultType;
1917 switch (Opc) {
1918 default:
1919 assert(0 && "Unimplemented unary expr!");
1920 case UnaryOperator::PreInc:
1921 case UnaryOperator::PreDec:
1922 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
1923 break;
1924 case UnaryOperator::AddrOf:
1925 resultType = CheckAddressOfOperand(Input, OpLoc);
1926 break;
1927 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00001928 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00001929 resultType = CheckIndirectionOperand(Input, OpLoc);
1930 break;
1931 case UnaryOperator::Plus:
1932 case UnaryOperator::Minus:
1933 UsualUnaryConversions(Input);
1934 resultType = Input->getType();
1935 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1936 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1937 resultType.getAsString());
1938 break;
1939 case UnaryOperator::Not: // bitwise complement
1940 UsualUnaryConversions(Input);
1941 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00001942 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1943 if (!resultType->isIntegerType()) {
1944 if (resultType->isComplexType())
1945 // C99 does not support '~' for complex conjugation.
1946 Diag(OpLoc, diag::ext_integer_complement_complex,
1947 resultType.getAsString());
1948 else
1949 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1950 resultType.getAsString());
1951 }
Chris Lattner4b009652007-07-25 00:24:17 +00001952 break;
1953 case UnaryOperator::LNot: // logical negation
1954 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
1955 DefaultFunctionArrayConversion(Input);
1956 resultType = Input->getType();
1957 if (!resultType->isScalarType()) // C99 6.5.3.3p1
1958 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1959 resultType.getAsString());
1960 // LNot always has type int. C99 6.5.3.3p5.
1961 resultType = Context.IntTy;
1962 break;
1963 case UnaryOperator::SizeOf:
1964 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
1965 break;
1966 case UnaryOperator::AlignOf:
1967 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1968 break;
Chris Lattner03931a72007-08-24 21:16:53 +00001969 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00001970 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00001971 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00001972 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001973 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00001974 resultType = Input->getType();
1975 break;
1976 }
1977 if (resultType.isNull())
1978 return true;
1979 return new UnaryOperator(Input, Opc, resultType, OpLoc);
1980}
1981
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001982/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1983Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001984 SourceLocation LabLoc,
1985 IdentifierInfo *LabelII) {
1986 // Look up the record for this label identifier.
1987 LabelStmt *&LabelDecl = LabelMap[LabelII];
1988
1989 // If we haven't seen this label yet, create a forward reference.
1990 if (LabelDecl == 0)
1991 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1992
1993 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001994 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1995 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001996}
1997
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001998Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00001999 SourceLocation RPLoc) { // "({..})"
2000 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2001 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2002 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2003
2004 // FIXME: there are a variety of strange constraints to enforce here, for
2005 // example, it is not possible to goto into a stmt expression apparently.
2006 // More semantic analysis is needed.
2007
2008 // FIXME: the last statement in the compount stmt has its value used. We
2009 // should not warn about it being unused.
2010
2011 // If there are sub stmts in the compound stmt, take the type of the last one
2012 // as the type of the stmtexpr.
2013 QualType Ty = Context.VoidTy;
2014
2015 if (!Compound->body_empty())
2016 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2017 Ty = LastExpr->getType();
2018
2019 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2020}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002021
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002022Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002023 SourceLocation TypeLoc,
2024 TypeTy *argty,
2025 OffsetOfComponent *CompPtr,
2026 unsigned NumComponents,
2027 SourceLocation RPLoc) {
2028 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2029 assert(!ArgTy.isNull() && "Missing type argument!");
2030
2031 // We must have at least one component that refers to the type, and the first
2032 // one is known to be a field designator. Verify that the ArgTy represents
2033 // a struct/union/class.
2034 if (!ArgTy->isRecordType())
2035 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2036
2037 // Otherwise, create a compound literal expression as the base, and
2038 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002039 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002040
Chris Lattnerb37522e2007-08-31 21:49:13 +00002041 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2042 // GCC extension, diagnose them.
2043 if (NumComponents != 1)
2044 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2045 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2046
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002047 for (unsigned i = 0; i != NumComponents; ++i) {
2048 const OffsetOfComponent &OC = CompPtr[i];
2049 if (OC.isBrackets) {
2050 // Offset of an array sub-field. TODO: Should we allow vector elements?
2051 const ArrayType *AT = Res->getType()->getAsArrayType();
2052 if (!AT) {
2053 delete Res;
2054 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2055 Res->getType().getAsString());
2056 }
2057
Chris Lattner2af6a802007-08-30 17:59:59 +00002058 // FIXME: C++: Verify that operator[] isn't overloaded.
2059
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002060 // C99 6.5.2.1p1
2061 Expr *Idx = static_cast<Expr*>(OC.U.E);
2062 if (!Idx->getType()->isIntegerType())
2063 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2064 Idx->getSourceRange());
2065
2066 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2067 continue;
2068 }
2069
2070 const RecordType *RC = Res->getType()->getAsRecordType();
2071 if (!RC) {
2072 delete Res;
2073 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2074 Res->getType().getAsString());
2075 }
2076
2077 // Get the decl corresponding to this.
2078 RecordDecl *RD = RC->getDecl();
2079 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2080 if (!MemberDecl)
2081 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2082 OC.U.IdentInfo->getName(),
2083 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002084
2085 // FIXME: C++: Verify that MemberDecl isn't a static field.
2086 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002087 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2088 // matter here.
2089 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002090 }
2091
2092 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2093 BuiltinLoc);
2094}
2095
2096
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002097Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002098 TypeTy *arg1, TypeTy *arg2,
2099 SourceLocation RPLoc) {
2100 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2101 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2102
2103 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2104
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002105 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002106}
2107
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002108Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002109 ExprTy *expr1, ExprTy *expr2,
2110 SourceLocation RPLoc) {
2111 Expr *CondExpr = static_cast<Expr*>(cond);
2112 Expr *LHSExpr = static_cast<Expr*>(expr1);
2113 Expr *RHSExpr = static_cast<Expr*>(expr2);
2114
2115 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2116
2117 // The conditional expression is required to be a constant expression.
2118 llvm::APSInt condEval(32);
2119 SourceLocation ExpLoc;
2120 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2121 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2122 CondExpr->getSourceRange());
2123
2124 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2125 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2126 RHSExpr->getType();
2127 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2128}
2129
Nate Begemanbd881ef2008-01-30 20:50:20 +00002130/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002131/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002132/// The number of arguments has already been validated to match the number of
2133/// arguments in FnType.
2134static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002135 unsigned NumParams = FnType->getNumArgs();
2136 for (unsigned i = 0; i != NumParams; ++i)
Nate Begemanbd881ef2008-01-30 20:50:20 +00002137 if (Args[i]->getType().getCanonicalType() !=
2138 FnType->getArgType(i).getCanonicalType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002139 return false;
2140 return true;
2141}
2142
2143Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2144 SourceLocation *CommaLocs,
2145 SourceLocation BuiltinLoc,
2146 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002147 // __builtin_overload requires at least 2 arguments
2148 if (NumArgs < 2)
2149 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2150 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002151
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002152 // The first argument is required to be a constant expression. It tells us
2153 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002154 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002155 Expr *NParamsExpr = Args[0];
2156 llvm::APSInt constEval(32);
2157 SourceLocation ExpLoc;
2158 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2159 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2160 NParamsExpr->getSourceRange());
2161
2162 // Verify that the number of parameters is > 0
2163 unsigned NumParams = constEval.getZExtValue();
2164 if (NumParams == 0)
2165 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2166 NParamsExpr->getSourceRange());
2167 // Verify that we have at least 1 + NumParams arguments to the builtin.
2168 if ((NumParams + 1) > NumArgs)
2169 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2170 SourceRange(BuiltinLoc, RParenLoc));
2171
2172 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002173 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002174 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002175 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2176 // UsualUnaryConversions will convert the function DeclRefExpr into a
2177 // pointer to function.
2178 Expr *Fn = UsualUnaryConversions(Args[i]);
2179 FunctionTypeProto *FnType = 0;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002180 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2181 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2182 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2183 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002184
2185 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2186 // parameters, and the number of parameters must match the value passed to
2187 // the builtin.
2188 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002189 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2190 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002191
2192 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002193 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002194 // If they match, return a new OverloadExpr.
Nate Begemanc6078c92008-01-31 05:38:29 +00002195 if (ExprsMatchFnType(Args+1, FnType)) {
2196 if (OE)
2197 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2198 OE->getFn()->getSourceRange());
2199 // Remember our match, and continue processing the remaining arguments
2200 // to catch any errors.
2201 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2202 BuiltinLoc, RParenLoc);
2203 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002204 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002205 // Return the newly created OverloadExpr node, if we succeded in matching
2206 // exactly one of the candidate functions.
2207 if (OE)
2208 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002209
2210 // If we didn't find a matching function Expr in the __builtin_overload list
2211 // the return an error.
2212 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002213 for (unsigned i = 0; i != NumParams; ++i) {
2214 if (i != 0) typeNames += ", ";
2215 typeNames += Args[i+1]->getType().getAsString();
2216 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002217
2218 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2219 SourceRange(BuiltinLoc, RParenLoc));
2220}
2221
Anders Carlsson36760332007-10-15 20:28:48 +00002222Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2223 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002224 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002225 Expr *E = static_cast<Expr*>(expr);
2226 QualType T = QualType::getFromOpaquePtr(type);
2227
2228 InitBuiltinVaListType();
2229
Chris Lattner005ed752008-01-04 18:04:52 +00002230 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2231 != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002232 return Diag(E->getLocStart(),
2233 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2234 E->getType().getAsString(),
2235 E->getSourceRange());
2236
2237 // FIXME: Warn if a non-POD type is passed in.
2238
2239 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2240}
2241
Chris Lattner005ed752008-01-04 18:04:52 +00002242bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2243 SourceLocation Loc,
2244 QualType DstType, QualType SrcType,
2245 Expr *SrcExpr, const char *Flavor) {
2246 // Decode the result (notice that AST's are still created for extensions).
2247 bool isInvalid = false;
2248 unsigned DiagKind;
2249 switch (ConvTy) {
2250 default: assert(0 && "Unknown conversion type");
2251 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002252 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00002253 DiagKind = diag::ext_typecheck_convert_pointer_int;
2254 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002255 case IntToPointer:
2256 DiagKind = diag::ext_typecheck_convert_int_pointer;
2257 break;
Chris Lattner005ed752008-01-04 18:04:52 +00002258 case IncompatiblePointer:
2259 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2260 break;
2261 case FunctionVoidPointer:
2262 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2263 break;
2264 case CompatiblePointerDiscardsQualifiers:
2265 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2266 break;
2267 case Incompatible:
2268 DiagKind = diag::err_typecheck_convert_incompatible;
2269 isInvalid = true;
2270 break;
2271 }
2272
2273 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2274 SrcExpr->getSourceRange());
2275 return isInvalid;
2276}
2277