blob: 86d6d772ddb7d32f4fac498bf08fac3aef9d0ed4 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek588e5eb2007-11-25 00:58:00 +000015#include "SemaUtil.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/Expr.h"
Steve Naroff563477d2007-09-18 23:55:05 +000018#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Lex/Preprocessor.h"
20#include "clang/Lex/LiteralSupport.h"
21#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner925e60d2007-12-28 05:29:59 +000023#include "llvm/ADT/OwningPtr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner59907c42007-08-10 20:18:51 +000025#include "llvm/ADT/StringExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Steve Narofff69936d2007-09-16 03:34:24 +000028/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Reid Spencer5f016e22007-07-11 17:01:13 +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 Narofff69936d2007-09-16 03:34:24 +000035Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Reid Spencer5f016e22007-07-11 17:01:13 +000036 assert(NumStringToks && "Must have at least one string!");
37
38 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
39 if (Literal.hadError)
40 return ExprResult(true);
41
42 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
43 for (unsigned i = 0; i != NumStringToks; ++i)
44 StringTokLocs.push_back(StringToks[i].getLocation());
45
46 // FIXME: handle wchar_t
Anders Carlssonee98ac52007-10-15 02:50:23 +000047 QualType t;
48
49 if (Literal.Pascal)
50 t = Context.getPointerType(Context.UnsignedCharTy);
51 else
52 t = Context.getPointerType(Context.CharTy);
53
54 if (Literal.Pascal && Literal.GetStringLength() > 256)
55 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
56 SourceRange(StringToks[0].getLocation(),
57 StringToks[NumStringToks-1].getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +000058
59 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
60 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Anders Carlssonee98ac52007-10-15 02:50:23 +000061 Literal.AnyWide, t,
62 StringToks[0].getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +000063 StringToks[NumStringToks-1].getLocation());
64}
65
66
Steve Naroff08d92e42007-09-15 18:49:24 +000067/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Reid Spencer5f016e22007-07-11 17:01:13 +000068/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
69/// identifier is used in an function call context.
Steve Naroff08d92e42007-09-15 18:49:24 +000070Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +000071 IdentifierInfo &II,
72 bool HasTrailingLParen) {
73 // Could be enum-constant or decl.
Steve Naroff8c9f13e2007-09-16 16:16:00 +000074 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Reid Spencer5f016e22007-07-11 17:01:13 +000075 if (D == 0) {
76 // Otherwise, this could be an implicitly declared function reference (legal
77 // in C90, extension in C99).
78 if (HasTrailingLParen &&
79 // Not in C++.
80 !getLangOptions().CPlusPlus)
81 D = ImplicitlyDefineFunction(Loc, II, S);
82 else {
Steve Naroff7779db42007-11-12 14:29:37 +000083 if (CurMethodDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000084 ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface();
85 ObjCInterfaceDecl *clsDeclared;
86 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, clsDeclared)) {
Steve Naroff7e3411b2007-11-15 02:58:25 +000087 IdentifierInfo &II = Context.Idents.get("self");
88 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
89 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
90 static_cast<Expr*>(SelfExpr.Val), true, true);
91 }
Steve Naroff7779db42007-11-12 14:29:37 +000092 }
Reid Spencer5f016e22007-07-11 17:01:13 +000093 // If this name wasn't predeclared and if this is not a function call,
94 // diagnose the problem.
95 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
96 }
97 }
Steve Naroffe1223f72007-08-28 03:03:08 +000098 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroff53a32342007-08-28 18:45:29 +000099 // Only create DeclRefExpr's for valid Decl's.
Steve Naroff5912a352007-08-28 20:14:24 +0000100 if (VD->isInvalidDecl())
Steve Naroffe1223f72007-08-28 03:03:08 +0000101 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroffe1223f72007-08-28 03:03:08 +0000103 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 if (isa<TypedefDecl>(D))
105 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian5ef404f2007-12-05 18:16:33 +0000107 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Reid Spencer5f016e22007-07-11 17:01:13 +0000108
109 assert(0 && "Invalid decl");
Chris Lattnereddbe032007-07-21 04:57:45 +0000110 abort();
Reid Spencer5f016e22007-07-11 17:01:13 +0000111}
112
Steve Narofff69936d2007-09-16 03:34:24 +0000113Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson22742662007-07-21 05:21:51 +0000114 tok::TokenKind Kind) {
115 PreDefinedExpr::IdentType IT;
116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 switch (Kind) {
Chris Lattner1423ea42008-01-12 18:39:25 +0000118 default: assert(0 && "Unknown simple primary expr!");
119 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
120 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
121 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
Chris Lattner1423ea42008-01-12 18:39:25 +0000123
124 // Verify that this is in a function context.
Chris Lattner8f978d52008-01-12 19:32:28 +0000125 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattner1423ea42008-01-12 18:39:25 +0000126 return Diag(Loc, diag::err_predef_outside_function);
Anders Carlsson22742662007-07-21 05:21:51 +0000127
Chris Lattnerfa28b302008-01-12 08:14:25 +0000128 // Pre-defined identifiers are of type char[x], where x is the length of the
129 // string.
Chris Lattner8f978d52008-01-12 19:32:28 +0000130 unsigned Length;
131 if (CurFunctionDecl)
132 Length = CurFunctionDecl->getIdentifier()->getLength();
133 else
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000134 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattner1423ea42008-01-12 18:39:25 +0000135
Chris Lattner8f978d52008-01-12 19:32:28 +0000136 llvm::APInt LengthI(32, Length + 1);
Chris Lattner1423ea42008-01-12 18:39:25 +0000137 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattner8f978d52008-01-12 19:32:28 +0000138 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattnerfa28b302008-01-12 08:14:25 +0000139 return new PreDefinedExpr(Loc, ResTy, IT);
Reid Spencer5f016e22007-07-11 17:01:13 +0000140}
141
Steve Narofff69936d2007-09-16 03:34:24 +0000142Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 llvm::SmallString<16> CharBuffer;
144 CharBuffer.resize(Tok.getLength());
145 const char *ThisTokBegin = &CharBuffer[0];
146 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
147
148 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
149 Tok.getLocation(), PP);
150 if (Literal.hadError())
151 return ExprResult(true);
152 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
153 Tok.getLocation());
154}
155
Steve Narofff69936d2007-09-16 03:34:24 +0000156Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // fast path for a single digit (which is quite common). A single digit
158 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
159 if (Tok.getLength() == 1) {
160 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
161
Chris Lattner701e5eb2007-09-04 02:45:27 +0000162 unsigned IntSize = static_cast<unsigned>(
163 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
165 Context.IntTy,
166 Tok.getLocation()));
167 }
168 llvm::SmallString<512> IntegerBuffer;
169 IntegerBuffer.resize(Tok.getLength());
170 const char *ThisTokBegin = &IntegerBuffer[0];
171
172 // Get the spelling of the token, which eliminates trigraphs, etc.
173 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
174 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
175 Tok.getLocation(), PP);
176 if (Literal.hadError)
177 return ExprResult(true);
178
Chris Lattner5d661452007-08-26 03:42:43 +0000179 Expr *Res;
180
181 if (Literal.isFloatingLiteral()) {
Chris Lattner525a0502007-09-22 18:29:59 +0000182 QualType Ty;
183 const llvm::fltSemantics *Format;
184 uint64_t Size; unsigned Align;
185
186 if (Literal.isFloat) {
187 Ty = Context.FloatTy;
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000188 Context.Target.getFloatInfo(Size, Align, Format,
189 Context.getFullLoc(Tok.getLocation()));
190
Chris Lattner525a0502007-09-22 18:29:59 +0000191 } else if (Literal.isLong) {
192 Ty = Context.LongDoubleTy;
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000193 Context.Target.getLongDoubleInfo(Size, Align, Format,
194 Context.getFullLoc(Tok.getLocation()));
Chris Lattner525a0502007-09-22 18:29:59 +0000195 } else {
196 Ty = Context.DoubleTy;
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000197 Context.Target.getDoubleInfo(Size, Align, Format,
198 Context.getFullLoc(Tok.getLocation()));
Chris Lattner525a0502007-09-22 18:29:59 +0000199 }
200
Ted Kremenek720c4ec2007-11-29 00:56:49 +0000201 // isExact will be set by GetFloatValue().
202 bool isExact = false;
203
204 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
205 Ty, Tok.getLocation());
206
Chris Lattner5d661452007-08-26 03:42:43 +0000207 } else if (!Literal.isIntegerLiteral()) {
208 return ExprResult(true);
209 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 QualType t;
211
Neil Boothb9449512007-08-29 22:00:19 +0000212 // long long is a C99 feature.
213 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth79859c32007-08-29 22:13:52 +0000214 Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000215 Diag(Tok.getLocation(), diag::ext_longlong);
216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 // Get the value in the widest-possible width.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000218 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(
219 Context.getFullLoc(Tok.getLocation())), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000220
221 if (Literal.GetIntegerValue(ResultVal)) {
222 // If this value didn't fit into uintmax_t, warn and force to ull.
223 Diag(Tok.getLocation(), diag::warn_integer_too_large);
224 t = Context.UnsignedLongLongTy;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000225 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 ResultVal.getBitWidth() && "long long is not intmax_t?");
227 } else {
228 // If this value fits into a ULL, try to figure out what else it fits into
229 // according to the rules of C99 6.4.4.1p5.
230
231 // Octal, Hexadecimal, and integers with a U suffix are allowed to
232 // be an unsigned int.
233 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
234
235 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner97c51562007-08-23 21:58:08 +0000236 if (!Literal.isLong && !Literal.isLongLong) {
237 // Are int/unsigned possibilities?
Chris Lattner701e5eb2007-09-04 02:45:27 +0000238 unsigned IntSize = static_cast<unsigned>(
239 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // Does it fit in a unsigned int?
241 if (ResultVal.isIntN(IntSize)) {
242 // Does it fit in a signed int?
243 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
244 t = Context.IntTy;
245 else if (AllowUnsigned)
246 t = Context.UnsignedIntTy;
247 }
248
249 if (!t.isNull())
250 ResultVal.trunc(IntSize);
251 }
252
253 // Are long/unsigned long possibilities?
254 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner701e5eb2007-09-04 02:45:27 +0000255 unsigned LongSize = static_cast<unsigned>(
256 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000257
258 // Does it fit in a unsigned long?
259 if (ResultVal.isIntN(LongSize)) {
260 // Does it fit in a signed long?
261 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
262 t = Context.LongTy;
263 else if (AllowUnsigned)
264 t = Context.UnsignedLongTy;
265 }
266 if (!t.isNull())
267 ResultVal.trunc(LongSize);
268 }
269
270 // Finally, check long long if needed.
271 if (t.isNull()) {
Chris Lattner701e5eb2007-09-04 02:45:27 +0000272 unsigned LongLongSize = static_cast<unsigned>(
273 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000274
275 // Does it fit in a unsigned long long?
276 if (ResultVal.isIntN(LongLongSize)) {
277 // Does it fit in a signed long long?
278 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
279 t = Context.LongLongTy;
280 else if (AllowUnsigned)
281 t = Context.UnsignedLongLongTy;
282 }
283 }
284
285 // If we still couldn't decide a type, we probably have something that
286 // does not fit in a signed long long, but has no U suffix.
287 if (t.isNull()) {
288 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
289 t = Context.UnsignedLongLongTy;
290 }
291 }
292
Chris Lattner5d661452007-08-26 03:42:43 +0000293 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 }
Chris Lattner5d661452007-08-26 03:42:43 +0000295
296 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
297 if (Literal.isImaginary)
298 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
299
300 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000301}
302
Steve Narofff69936d2007-09-16 03:34:24 +0000303Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 ExprTy *Val) {
305 Expr *e = (Expr *)Val;
Steve Narofff69936d2007-09-16 03:34:24 +0000306 assert((e != 0) && "ActOnParenExpr() missing expr");
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 return new ParenExpr(L, R, e);
308}
309
310/// The UsualUnaryConversions() function is *not* called by this routine.
311/// See C99 6.3.2.1p[2-4] for more details.
312QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
313 SourceLocation OpLoc, bool isSizeof) {
314 // C99 6.5.3.4p1:
315 if (isa<FunctionType>(exprType) && isSizeof)
316 // alignof(function) is allowed.
317 Diag(OpLoc, diag::ext_sizeof_function_type);
318 else if (exprType->isVoidType())
319 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
320 else if (exprType->isIncompleteType()) {
321 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
322 diag::err_alignof_incomplete_type,
323 exprType.getAsString());
324 return QualType(); // error
325 }
326 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
327 return Context.getSizeType();
328}
329
330Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000331ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 SourceLocation LPLoc, TypeTy *Ty,
333 SourceLocation RPLoc) {
334 // If error parsing type, ignore.
335 if (Ty == 0) return true;
336
337 // Verify that this is a valid expression.
338 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
339
340 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
341
342 if (resultType.isNull())
343 return true;
344 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
345}
346
Chris Lattner5d794252007-08-24 21:41:10 +0000347QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattnerdbb36972007-08-24 21:16:53 +0000348 DefaultFunctionArrayConversion(V);
349
Chris Lattnercc26ed72007-08-26 05:39:26 +0000350 // These operators return the element type of a complex type.
Chris Lattnerdbb36972007-08-24 21:16:53 +0000351 if (const ComplexType *CT = V->getType()->getAsComplexType())
352 return CT->getElementType();
Chris Lattnercc26ed72007-08-26 05:39:26 +0000353
354 // Otherwise they pass through real integer and floating point types here.
355 if (V->getType()->isArithmeticType())
356 return V->getType();
357
358 // Reject anything else.
359 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
360 return QualType();
Chris Lattnerdbb36972007-08-24 21:16:53 +0000361}
362
363
Reid Spencer5f016e22007-07-11 17:01:13 +0000364
Steve Narofff69936d2007-09-16 03:34:24 +0000365Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 tok::TokenKind Kind,
367 ExprTy *Input) {
368 UnaryOperator::Opcode Opc;
369 switch (Kind) {
370 default: assert(0 && "Unknown unary op!");
371 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
372 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
373 }
374 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
375 if (result.isNull())
376 return true;
377 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
378}
379
380Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000381ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner727a80d2007-07-15 23:59:53 +0000383 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner12d9ff62007-07-16 00:14:47 +0000384
385 // Perform default conversions.
386 DefaultFunctionArrayConversion(LHSExp);
387 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner727a80d2007-07-15 23:59:53 +0000388
Chris Lattner12d9ff62007-07-16 00:14:47 +0000389 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000392 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 // in the subscript position. As a result, we need to derive the array base
394 // and index from the expression types.
Chris Lattner12d9ff62007-07-16 00:14:47 +0000395 Expr *BaseExpr, *IndexExpr;
396 QualType ResultType;
Chris Lattnerbefee482007-07-31 16:53:04 +0000397 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner12d9ff62007-07-16 00:14:47 +0000398 BaseExpr = LHSExp;
399 IndexExpr = RHSExp;
400 // FIXME: need to deal with const...
401 ResultType = PTy->getPointeeType();
Chris Lattnerbefee482007-07-31 16:53:04 +0000402 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000403 // Handle the uncommon case of "123[Ptr]".
Chris Lattner12d9ff62007-07-16 00:14:47 +0000404 BaseExpr = RHSExp;
405 IndexExpr = LHSExp;
406 // FIXME: need to deal with const...
407 ResultType = PTy->getPointeeType();
Chris Lattnerc8629632007-07-31 19:29:30 +0000408 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
409 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner12d9ff62007-07-16 00:14:47 +0000410 IndexExpr = RHSExp;
Steve Naroff608e0ee2007-08-03 22:40:33 +0000411
412 // Component access limited to variables (reject vec4.rg[1]).
413 if (!isa<DeclRefExpr>(BaseExpr))
414 return Diag(LLoc, diag::err_ocuvector_component_access,
415 SourceRange(LLoc, RLoc));
Chris Lattner12d9ff62007-07-16 00:14:47 +0000416 // FIXME: need to deal with const...
417 ResultType = VTy->getElementType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 } else {
Chris Lattner727a80d2007-07-15 23:59:53 +0000419 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
420 RHSExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 }
422 // C99 6.5.2.1p1
Chris Lattner12d9ff62007-07-16 00:14:47 +0000423 if (!IndexExpr->getType()->isIntegerType())
424 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
425 IndexExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000426
Chris Lattner12d9ff62007-07-16 00:14:47 +0000427 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
428 // the following check catches trying to index a pointer to a function (e.g.
429 // void (*)(int)). Functions are not objects in C99.
430 if (!ResultType->isObjectType())
431 return Diag(BaseExpr->getLocStart(),
432 diag::err_typecheck_subscript_not_object,
433 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
434
435 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000436}
437
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000438QualType Sema::
439CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
440 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattnerc8629632007-07-31 19:29:30 +0000441 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000442
443 // The vector accessor can't exceed the number of elements.
444 const char *compStr = CompName.getName();
445 if (strlen(compStr) > vecType->getNumElements()) {
446 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
447 baseType.getAsString(), SourceRange(CompLoc));
448 return QualType();
449 }
450 // The component names must come from the same set.
Chris Lattner88dca042007-08-02 22:33:49 +0000451 if (vecType->getPointAccessorIdx(*compStr) != -1) {
452 do
453 compStr++;
454 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
455 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
456 do
457 compStr++;
458 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
459 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
460 do
461 compStr++;
462 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
463 }
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000464
465 if (*compStr) {
466 // We didn't get to the end of the string. This means the component names
467 // didn't come from the same set *or* we encountered an illegal name.
468 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
469 std::string(compStr,compStr+1), SourceRange(CompLoc));
470 return QualType();
471 }
472 // Each component accessor can't exceed the vector type.
473 compStr = CompName.getName();
474 while (*compStr) {
475 if (vecType->isAccessorWithinNumElements(*compStr))
476 compStr++;
477 else
478 break;
479 }
480 if (*compStr) {
481 // We didn't get to the end of the string. This means a component accessor
482 // exceeds the number of elements in the vector.
483 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
484 baseType.getAsString(), SourceRange(CompLoc));
485 return QualType();
486 }
487 // The component accessor looks fine - now we need to compute the actual type.
488 // The vector type is implied by the component accessor. For example,
489 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
490 unsigned CompSize = strlen(CompName.getName());
491 if (CompSize == 1)
492 return vecType->getElementType();
Steve Naroffbea0b342007-07-29 16:33:31 +0000493
494 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
495 // Now look up the TypeDefDecl from the vector type. Without this,
496 // diagostics look bad. We want OCU vector types to appear built-in.
497 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
498 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
499 return Context.getTypedefType(OCUVectorDecls[i]);
500 }
501 return VT; // should never get here (a typedef type should always be found).
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000502}
503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000505ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 tok::TokenKind OpKind, SourceLocation MemberLoc,
507 IdentifierInfo &Member) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000508 Expr *BaseExpr = static_cast<Expr *>(Base);
509 assert(BaseExpr && "no record expression");
Steve Naroff3cc4af82007-12-16 21:42:28 +0000510
511 // Perform default conversions.
512 DefaultFunctionArrayConversion(BaseExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000513
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000514 QualType BaseType = BaseExpr->getType();
515 assert(!BaseType.isNull() && "no type for member expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000516
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 if (OpKind == tok::arrow) {
Chris Lattnerbefee482007-07-31 16:53:04 +0000518 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000519 BaseType = PT->getPointeeType();
520 else
521 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
522 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 }
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000524 // The base type is either a record or an OCUVectorType.
Chris Lattnerc8629632007-07-31 19:29:30 +0000525 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000526 RecordDecl *RDecl = RTy->getDecl();
527 if (RTy->isIncompleteType())
528 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
529 BaseExpr->getSourceRange());
530 // The record definition is complete, now make sure the member is valid.
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000531 FieldDecl *MemberDecl = RDecl->getMember(&Member);
532 if (!MemberDecl)
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000533 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
534 SourceRange(MemberLoc));
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000535 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
536 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff608e0ee2007-08-03 22:40:33 +0000537 // Component access limited to variables (reject vec4.rg.g).
538 if (!isa<DeclRefExpr>(BaseExpr))
539 return Diag(OpLoc, diag::err_ocuvector_component_access,
540 SourceRange(MemberLoc));
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000541 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
542 if (ret.isNull())
543 return true;
Chris Lattner6481a572007-08-03 17:31:20 +0000544 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000545 } else if (BaseType->isObjCInterfaceType()) {
546 ObjCInterfaceDecl *IFace;
547 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
548 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000549 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000550 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
551 ObjCInterfaceDecl *clsDeclared;
552 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000553 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
554 OpKind==tok::arrow);
555 }
556 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
557 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000558}
559
Steve Narofff69936d2007-09-16 03:34:24 +0000560/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +0000561/// This provides the location of the left/right parens and a list of comma
562/// locations.
563Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000564ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner925e60d2007-12-28 05:29:59 +0000565 ExprTy **args, unsigned NumArgs,
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner74c469f2007-07-21 03:03:59 +0000567 Expr *Fn = static_cast<Expr *>(fn);
568 Expr **Args = reinterpret_cast<Expr**>(args);
569 assert(Fn && "no function call expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000570
Chris Lattner925e60d2007-12-28 05:29:59 +0000571 // Make the call expr early, before semantic checks. This guarantees cleanup
572 // of arguments and function on error.
573 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
574 Context.BoolTy, RParenLoc));
575
576 // Promote the function operand.
577 TheCall->setCallee(UsualUnaryConversions(Fn));
578
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
580 // type pointer to function".
Chris Lattner925e60d2007-12-28 05:29:59 +0000581 const PointerType *PT = Fn->getType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 if (PT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000583 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
584 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner925e60d2007-12-28 05:29:59 +0000585 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
586 if (FuncT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000587 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
588 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner925e60d2007-12-28 05:29:59 +0000589
590 // We know the result type of the call, set it.
591 TheCall->setType(FuncT->getResultType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000592
Chris Lattner925e60d2007-12-28 05:29:59 +0000593 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
595 // assignment, to the types of the corresponding parameter, ...
Chris Lattner925e60d2007-12-28 05:29:59 +0000596 unsigned NumArgsInProto = Proto->getNumArgs();
597 unsigned NumArgsToCheck = NumArgs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000598
Chris Lattner925e60d2007-12-28 05:29:59 +0000599 // If too few arguments are available, don't make the call.
600 if (NumArgs < NumArgsInProto)
601 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
602 Fn->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000603
Chris Lattner925e60d2007-12-28 05:29:59 +0000604 // If too many are passed and not variadic, error on the extras and drop
605 // them.
606 if (NumArgs > NumArgsInProto) {
607 if (!Proto->isVariadic()) {
Chris Lattnerd472b312007-07-21 03:09:58 +0000608 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000609 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnerd472b312007-07-21 03:09:58 +0000610 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner925e60d2007-12-28 05:29:59 +0000611 Args[NumArgs-1]->getLocEnd()));
612 // This deletes the extra arguments.
613 TheCall->setNumArgs(NumArgsInProto);
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 }
615 NumArgsToCheck = NumArgsInProto;
616 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000617
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 // Continue to check argument types (even if we have too few/many args).
Chris Lattner925e60d2007-12-28 05:29:59 +0000619 for (unsigned i = 0; i != NumArgsToCheck; i++) {
620 Expr *Arg = Args[i];
Chris Lattner5cf216b2008-01-04 18:04:52 +0000621 QualType ProtoArgType = Proto->getArgType(i);
622 QualType ArgType = Arg->getType();
Steve Naroff700204c2007-07-24 21:46:40 +0000623
Chris Lattner925e60d2007-12-28 05:29:59 +0000624 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000625 AssignConvertType ConvTy =
626 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner925e60d2007-12-28 05:29:59 +0000627 TheCall->setArg(i, Arg);
628
Chris Lattner5cf216b2008-01-04 18:04:52 +0000629 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
630 ArgType, Arg, "passing"))
631 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000633
634 // If this is a variadic call, handle args passed through "...".
635 if (Proto->isVariadic()) {
Steve Naroffb291ab62007-08-28 23:30:39 +0000636 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner925e60d2007-12-28 05:29:59 +0000637 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
638 Expr *Arg = Args[i];
639 DefaultArgumentPromotion(Arg);
640 TheCall->setArg(i, Arg);
Steve Naroffb291ab62007-08-28 23:30:39 +0000641 }
Steve Naroffb291ab62007-08-28 23:30:39 +0000642 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000643 } else {
644 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
645
Steve Naroffb291ab62007-08-28 23:30:39 +0000646 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner925e60d2007-12-28 05:29:59 +0000647 for (unsigned i = 0; i != NumArgs; i++) {
648 Expr *Arg = Args[i];
649 DefaultArgumentPromotion(Arg);
650 TheCall->setArg(i, Arg);
Steve Naroffb291ab62007-08-28 23:30:39 +0000651 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000653
Chris Lattner59907c42007-08-10 20:18:51 +0000654 // Do special checking on direct calls to functions.
655 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
656 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
657 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattner925e60d2007-12-28 05:29:59 +0000658 if (CheckFunctionCall(FDecl, TheCall.get()))
Anders Carlsson71993dd2007-08-17 05:31:46 +0000659 return true;
Chris Lattner59907c42007-08-10 20:18:51 +0000660
Chris Lattner925e60d2007-12-28 05:29:59 +0000661 return TheCall.take();
Reid Spencer5f016e22007-07-11 17:01:13 +0000662}
663
664Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000665ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroffaff1edd2007-07-19 21:32:11 +0000666 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofff69936d2007-09-16 03:34:24 +0000667 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Naroff4aa88f82007-07-19 01:06:55 +0000668 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroffaff1edd2007-07-19 21:32:11 +0000669 // FIXME: put back this assert when initializers are worked out.
Steve Narofff69936d2007-09-16 03:34:24 +0000670 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroffaff1edd2007-07-19 21:32:11 +0000671 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlssond35c8322007-12-05 07:24:19 +0000672
Steve Naroff2fdc3742007-12-10 22:44:33 +0000673 // FIXME: add more semantic analysis (C99 6.5.2.5).
Steve Naroffd0091aa2008-01-10 22:15:12 +0000674 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff58d18212008-01-09 20:58:06 +0000675 return true;
Steve Naroffe9b12192008-01-14 18:19:28 +0000676
677 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
678 if (isFileScope) { // 6.5.2.5p3
Steve Naroffd0091aa2008-01-10 22:15:12 +0000679 if (CheckForConstantInitializer(literalExpr, literalType))
680 return true;
681 }
Steve Naroffe9b12192008-01-14 18:19:28 +0000682 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000683}
684
685Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000686ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000687 SourceLocation RBraceLoc) {
Steve Narofff0090632007-09-02 02:04:30 +0000688 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000689
Steve Naroff08d92e42007-09-15 18:49:24 +0000690 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroffd35005e2007-09-03 01:24:23 +0000691 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000692
Steve Naroff38374b02007-09-02 20:30:18 +0000693 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
694 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
695 return e;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000696}
697
Chris Lattnerfe23e212007-12-20 00:44:32 +0000698bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssona64db8f2007-11-27 05:51:55 +0000699 assert(VectorTy->isVectorType() && "Not a vector type!");
700
701 if (Ty->isVectorType() || Ty->isIntegerType()) {
702 if (Context.getTypeSize(VectorTy, SourceLocation()) !=
703 Context.getTypeSize(Ty, SourceLocation()))
704 return Diag(R.getBegin(),
705 Ty->isVectorType() ?
706 diag::err_invalid_conversion_between_vectors :
707 diag::err_invalid_conversion_between_vector_and_integer,
708 VectorTy.getAsString().c_str(),
709 Ty.getAsString().c_str(), R);
710 } else
711 return Diag(R.getBegin(),
712 diag::err_invalid_conversion_between_vector_and_scalar,
713 VectorTy.getAsString().c_str(),
714 Ty.getAsString().c_str(), R);
715
716 return false;
717}
718
Steve Naroff4aa88f82007-07-19 01:06:55 +0000719Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000720ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 SourceLocation RParenLoc, ExprTy *Op) {
Steve Narofff69936d2007-09-16 03:34:24 +0000722 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff16beff82007-07-16 23:25:18 +0000723
724 Expr *castExpr = static_cast<Expr*>(Op);
725 QualType castType = QualType::getFromOpaquePtr(Ty);
726
Steve Naroff711602b2007-08-31 00:32:44 +0000727 UsualUnaryConversions(castExpr);
728
Chris Lattner75af4802007-07-18 16:00:06 +0000729 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
730 // type needs to be scalar.
Chris Lattner3da2db42007-10-29 04:26:44 +0000731 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff9412d682008-01-24 22:55:05 +0000732 if (!castType->isScalarType() && !castType->isVectorType())
Chris Lattner3da2db42007-10-29 04:26:44 +0000733 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
734 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Steve Naroff9412d682008-01-24 22:55:05 +0000735 if (!castExpr->getType()->isScalarType() &&
736 !castExpr->getType()->isVectorType())
Chris Lattner3da2db42007-10-29 04:26:44 +0000737 return Diag(castExpr->getLocStart(),
738 diag::err_typecheck_expect_scalar_operand,
739 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssona64db8f2007-11-27 05:51:55 +0000740
741 if (castExpr->getType()->isVectorType()) {
742 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
743 castExpr->getType(), castType))
744 return true;
745 } else if (castType->isVectorType()) {
746 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
747 castType, castExpr->getType()))
748 return true;
Chris Lattner3da2db42007-10-29 04:26:44 +0000749 }
Steve Naroff16beff82007-07-16 23:25:18 +0000750 }
751 return new CastExpr(castType, castExpr, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000752}
753
Chris Lattnera21ddb32007-11-26 01:40:58 +0000754/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
755/// In that case, lex = cond.
Reid Spencer5f016e22007-07-11 17:01:13 +0000756inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff49b45262007-07-13 16:58:59 +0000757 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000758 UsualUnaryConversions(cond);
759 UsualUnaryConversions(lex);
760 UsualUnaryConversions(rex);
761 QualType condT = cond->getType();
762 QualType lexT = lex->getType();
763 QualType rexT = rex->getType();
764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 // first, check the condition.
Steve Naroff49b45262007-07-13 16:58:59 +0000766 if (!condT->isScalarType()) { // C99 6.5.15p2
767 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
768 condT.getAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 return QualType();
770 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000771
772 // Now check the two expressions.
773
774 // If both operands have arithmetic type, do the usual arithmetic conversions
775 // to find a common type: C99 6.5.15p3,5.
776 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Steve Naroffa4332e22007-07-17 00:58:39 +0000777 UsualArithmeticConversions(lex, rex);
778 return lex->getType();
779 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000780
781 // If both operands are the same structure or union type, the result is that
782 // type.
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000783 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner70d67a92008-01-06 22:42:25 +0000784 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattnera21ddb32007-11-26 01:40:58 +0000785 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner70d67a92008-01-06 22:42:25 +0000786 // "If both the operands have structure or union type, the result has
787 // that type." This implies that CV qualifiers are dropped.
788 return lexT.getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000790
791 // C99 6.5.15p5: "If both operands have void type, the result has void type."
792 if (lexT->isVoidType() && rexT->isVoidType())
793 return lexT.getUnqualifiedType();
Steve Naroffb6d54e52008-01-08 01:11:38 +0000794
795 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
796 // the type of the other operand."
797 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000798 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroffb6d54e52008-01-08 01:11:38 +0000799 return lexT;
800 }
801 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000802 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroffb6d54e52008-01-08 01:11:38 +0000803 return rexT;
804 }
Chris Lattnerbd57d362008-01-06 22:50:31 +0000805 // Handle the case where both operands are pointers before we handle null
806 // pointer constants in case both operands are null pointer constants.
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000807 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
808 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
809 // get the "pointed to" types
810 QualType lhptee = LHSPT->getPointeeType();
811 QualType rhptee = RHSPT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000812
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000813 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
814 if (lhptee->isVoidType() &&
815 (rhptee->isObjectType() || rhptee->isIncompleteType()))
816 return lexT;
817 if (rhptee->isVoidType() &&
818 (lhptee->isObjectType() || lhptee->isIncompleteType()))
819 return rexT;
Reid Spencer5f016e22007-07-11 17:01:13 +0000820
Steve Naroffec0550f2007-10-15 20:41:53 +0000821 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
822 rhptee.getUnqualifiedType())) {
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000823 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
824 lexT.getAsString(), rexT.getAsString(),
825 lex->getSourceRange(), rex->getSourceRange());
Eli Friedmanb1284ac2008-01-30 17:02:03 +0000826 // In this situation, we assume void* type. No especially good
827 // reason, but this is what gcc does, and we do have to pick
828 // to get a consistent AST.
829 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
830 ImpCastExprToType(lex, voidPtrTy);
831 ImpCastExprToType(rex, voidPtrTy);
832 return voidPtrTy;
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000833 }
834 // The pointer types are compatible.
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000835 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
836 // differently qualified versions of compatible types, the result type is
837 // a pointer to an appropriately qualified version of the *composite*
838 // type.
Chris Lattnerbd57d362008-01-06 22:50:31 +0000839 // FIXME: Need to return the composite type.
840 return lexT;
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 }
842 }
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000843
Chris Lattner70d67a92008-01-06 22:42:25 +0000844 // Otherwise, the operands are not compatible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff49b45262007-07-13 16:58:59 +0000846 lexT.getAsString(), rexT.getAsString(),
847 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 return QualType();
849}
850
Steve Narofff69936d2007-09-16 03:34:24 +0000851/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Reid Spencer5f016e22007-07-11 17:01:13 +0000852/// in the case of a the GNU conditional expr extension.
Steve Narofff69936d2007-09-16 03:34:24 +0000853Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 SourceLocation ColonLoc,
855 ExprTy *Cond, ExprTy *LHS,
856 ExprTy *RHS) {
Chris Lattner26824902007-07-16 21:39:03 +0000857 Expr *CondExpr = (Expr *) Cond;
858 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattnera21ddb32007-11-26 01:40:58 +0000859
860 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
861 // was the condition.
862 bool isLHSNull = LHSExpr == 0;
863 if (isLHSNull)
864 LHSExpr = CondExpr;
865
Chris Lattner26824902007-07-16 21:39:03 +0000866 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
867 RHSExpr, QuestionLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 if (result.isNull())
869 return true;
Chris Lattnera21ddb32007-11-26 01:40:58 +0000870 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
871 RHSExpr, result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000872}
873
Steve Naroffb291ab62007-08-28 23:30:39 +0000874/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffb3c2b882008-01-29 02:42:22 +0000875/// do not have a prototype. Arguments that have type float are promoted to
876/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner925e60d2007-12-28 05:29:59 +0000877void Sema::DefaultArgumentPromotion(Expr *&Expr) {
878 QualType Ty = Expr->getType();
879 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffb291ab62007-08-28 23:30:39 +0000880
Chris Lattner925e60d2007-12-28 05:29:59 +0000881 if (Ty == Context.FloatTy)
Chris Lattner1e0a3902008-01-16 19:17:22 +0000882 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroffb3c2b882008-01-29 02:42:22 +0000883 else
884 UsualUnaryConversions(Expr);
Steve Naroffb291ab62007-08-28 23:30:39 +0000885}
886
Steve Narofffa2eaab2007-07-15 02:02:06 +0000887/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000888void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Narofffa2eaab2007-07-15 02:02:06 +0000889 QualType t = e->getType();
Steve Naroff90045e82007-07-13 23:32:42 +0000890 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendling08ad47c2007-07-17 03:52:31 +0000891
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000892 if (const ReferenceType *ref = t->getAsReferenceType()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000893 ImpCastExprToType(e, ref->getReferenceeType()); // C++ [expr]
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000894 t = e->getType();
895 }
Steve Narofffa2eaab2007-07-15 02:02:06 +0000896 if (t->isFunctionType())
Chris Lattner1e0a3902008-01-16 19:17:22 +0000897 ImpCastExprToType(e, Context.getPointerType(t));
Chris Lattnerc8629632007-07-31 19:29:30 +0000898 else if (const ArrayType *ary = t->getAsArrayType())
Chris Lattner1e0a3902008-01-16 19:17:22 +0000899 ImpCastExprToType(e, Context.getPointerType(ary->getElementType()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000900}
901
Nate Begemane2ce1d92008-01-17 17:46:27 +0000902/// UsualUnaryConversions - Performs various conversions that are common to most
Reid Spencer5f016e22007-07-11 17:01:13 +0000903/// operators (C99 6.3). The conversions of array and function types are
904/// sometimes surpressed. For example, the array->pointer conversion doesn't
905/// apply if the array is an argument to the sizeof or address (&) operators.
906/// In these instances, this routine should *not* be called.
Chris Lattner925e60d2007-12-28 05:29:59 +0000907Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
908 QualType Ty = Expr->getType();
909 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000910
Chris Lattner925e60d2007-12-28 05:29:59 +0000911 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000912 ImpCastExprToType(Expr, Ref->getReferenceeType()); // C++ [expr]
Chris Lattner925e60d2007-12-28 05:29:59 +0000913 Ty = Expr->getType();
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000914 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000915 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattner1e0a3902008-01-16 19:17:22 +0000916 ImpCastExprToType(Expr, Context.IntTy);
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000917 else
Chris Lattner925e60d2007-12-28 05:29:59 +0000918 DefaultFunctionArrayConversion(Expr);
919
920 return Expr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000921}
922
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000923/// UsualArithmeticConversions - Performs various conversions that are common to
Reid Spencer5f016e22007-07-11 17:01:13 +0000924/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
925/// routine returns the first non-arithmetic type found. The client is
926/// responsible for emitting appropriate error diagnostics.
Steve Naroffdfb9bbb2008-01-15 22:21:49 +0000927/// FIXME: verify the conversion rules for "complex int" are consistent with GCC.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000928QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
929 bool isCompAssign) {
Steve Naroff8702a0f2007-08-25 19:54:59 +0000930 if (!isCompAssign) {
931 UsualUnaryConversions(lhsExpr);
932 UsualUnaryConversions(rhsExpr);
933 }
Steve Naroff3187e202007-10-18 18:55:53 +0000934 // For conversion purposes, we ignore any qualifiers.
935 // For example, "const float" and "float" are equivalent.
Steve Narofff68a63f2007-11-10 19:45:54 +0000936 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
937 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000938
939 // If both types are identical, no conversion is needed.
Steve Naroff3187e202007-10-18 18:55:53 +0000940 if (lhs == rhs)
941 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000942
943 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
944 // The caller can deal with this (e.g. pointer + int).
Steve Naroffa4332e22007-07-17 00:58:39 +0000945 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000946 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000947
948 // At this point, we have two different arithmetic types.
949
950 // Handle complex types first (C99 6.3.1.8p1).
951 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff02f62a92008-01-15 19:36:10 +0000952 // if we have an integer operand, the result is the complex type.
Steve Naroffdfb9bbb2008-01-15 22:21:49 +0000953 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
954 // convert the rhs to the lhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000955 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000956 return lhs;
Steve Naroff02f62a92008-01-15 19:36:10 +0000957 }
Steve Naroffdfb9bbb2008-01-15 22:21:49 +0000958 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
959 // convert the lhs to the rhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000960 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000961 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +0000962 }
Steve Narofff1448a02007-08-27 01:27:54 +0000963 // This handles complex/complex, complex/float, or float/complex.
964 // When both operands are complex, the shorter operand is converted to the
965 // type of the longer, and that is the type of the result. This corresponds
966 // to what is done when combining two real floating-point operands.
967 // The fun begins when size promotion occur across type domains.
968 // From H&S 6.3.4: When one operand is complex and the other is a real
969 // floating-point type, the less precise type is converted, within it's
970 // real or complex domain, to the precision of the other type. For example,
971 // when combining a "long double" with a "double _Complex", the
972 // "double _Complex" is promoted to "long double _Complex".
Steve Narofffb0d4962007-08-27 15:30:22 +0000973 int result = Context.compareFloatingType(lhs, rhs);
974
975 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff55fe4552007-08-27 21:32:55 +0000976 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
977 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +0000978 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff55fe4552007-08-27 21:32:55 +0000979 } else if (result < 0) { // The right side is bigger, convert lhs.
980 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
981 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +0000982 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff55fe4552007-08-27 21:32:55 +0000983 }
984 // At this point, lhs and rhs have the same rank/size. Now, make sure the
985 // domains match. This is a requirement for our implementation, C99
986 // does not require this promotion.
987 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
988 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff29960362007-08-27 21:43:43 +0000989 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +0000990 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff29960362007-08-27 21:43:43 +0000991 return rhs;
Steve Naroff55fe4552007-08-27 21:32:55 +0000992 } else { // handle "_Complex double, double".
Steve Naroff29960362007-08-27 21:43:43 +0000993 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +0000994 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff29960362007-08-27 21:43:43 +0000995 return lhs;
Steve Naroff55fe4552007-08-27 21:32:55 +0000996 }
Steve Naroffa4332e22007-07-17 00:58:39 +0000997 }
Steve Naroff29960362007-08-27 21:43:43 +0000998 return lhs; // The domain/size match exactly.
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 // Now handle "real" floating types (i.e. float, double, long double).
1001 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1002 // if we have an integer operand, the result is the real floating type.
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001003 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
1004 // convert rhs to the lhs floating point type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001005 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001006 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001007 }
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001008 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
1009 // convert lhs to the rhs floating point type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001010 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001011 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001012 }
Steve Narofffa2eaab2007-07-15 02:02:06 +00001013 // We have two real floating types, float/complex combos were handled above.
1014 // Convert the smaller operand to the bigger result.
Steve Narofffb0d4962007-08-27 15:30:22 +00001015 int result = Context.compareFloatingType(lhs, rhs);
1016
1017 if (result > 0) { // convert the rhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001018 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001019 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001020 }
Steve Narofffb0d4962007-08-27 15:30:22 +00001021 if (result < 0) { // convert the lhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001022 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Narofffb0d4962007-08-27 15:30:22 +00001023 return rhs;
1024 }
1025 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 }
Steve Naroff02f62a92008-01-15 19:36:10 +00001027 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1028 // Handle GCC complex int extension.
Steve Naroff02f62a92008-01-15 19:36:10 +00001029 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
1030 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
1031
1032 if (lhsComplexInt && rhsComplexInt) {
1033 if (Context.maxIntegerType(lhsComplexInt->getElementType(),
Chris Lattner1e0a3902008-01-16 19:17:22 +00001034 rhsComplexInt->getElementType()) == lhs) {
1035 // convert the rhs
1036 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1037 return lhs;
Steve Naroff02f62a92008-01-15 19:36:10 +00001038 }
Chris Lattner1e0a3902008-01-16 19:17:22 +00001039 if (!isCompAssign)
1040 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff02f62a92008-01-15 19:36:10 +00001041 return rhs;
1042 } else if (lhsComplexInt && rhs->isIntegerType()) {
1043 // convert the rhs to the lhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001044 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff02f62a92008-01-15 19:36:10 +00001045 return lhs;
1046 } else if (rhsComplexInt && lhs->isIntegerType()) {
1047 // convert the lhs to the rhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001048 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff02f62a92008-01-15 19:36:10 +00001049 return rhs;
1050 }
1051 }
Steve Narofffa2eaab2007-07-15 02:02:06 +00001052 // Finally, we have two differing integer types.
Steve Naroffa4332e22007-07-17 00:58:39 +00001053 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001054 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001055 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001056 }
Chris Lattner1e0a3902008-01-16 19:17:22 +00001057 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001058 return rhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001059}
1060
1061// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1062// being closely modeled after the C99 spec:-). The odd characteristic of this
1063// routine is it effectively iqnores the qualifiers on the top level pointee.
1064// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1065// FIXME: add a couple examples in this comment.
Chris Lattner5cf216b2008-01-04 18:04:52 +00001066Sema::AssignConvertType
Reid Spencer5f016e22007-07-11 17:01:13 +00001067Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1068 QualType lhptee, rhptee;
1069
1070 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner2dcb6bb2007-07-31 21:27:01 +00001071 lhptee = lhsType->getAsPointerType()->getPointeeType();
1072 rhptee = rhsType->getAsPointerType()->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001073
1074 // make sure we operate on the canonical type
1075 lhptee = lhptee.getCanonicalType();
1076 rhptee = rhptee.getCanonicalType();
1077
Chris Lattner5cf216b2008-01-04 18:04:52 +00001078 AssignConvertType ConvTy = Compatible;
Reid Spencer5f016e22007-07-11 17:01:13 +00001079
1080 // C99 6.5.16.1p1: This following citation is common to constraints
1081 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1082 // qualifiers of the type *pointed to* by the right;
1083 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
1084 rhptee.getQualifiers())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001085 ConvTy = CompatiblePointerDiscardsQualifiers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001086
1087 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1088 // incomplete type and the other is a pointer to a qualified or unqualified
1089 // version of void...
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001090 if (lhptee->isVoidType()) {
1091 if (rhptee->isObjectType() || rhptee->isIncompleteType())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001092 return ConvTy;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001093
1094 // As an extension, we allow cast to/from void* to function pointer.
1095 if (rhptee->isFunctionType())
1096 return FunctionVoidPointer;
1097 }
1098
1099 if (rhptee->isVoidType()) {
1100 if (lhptee->isObjectType() || lhptee->isIncompleteType())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001101 return ConvTy;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001102
1103 // As an extension, we allow cast to/from void* to function pointer.
1104 if (lhptee->isFunctionType())
1105 return FunctionVoidPointer;
1106 }
1107
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1109 // unqualified versions of compatible types, ...
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001110 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1111 rhptee.getUnqualifiedType()))
1112 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner5cf216b2008-01-04 18:04:52 +00001113 return ConvTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001114}
1115
1116/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1117/// has code to accommodate several GCC extensions when type checking
1118/// pointers. Here are some objectionable examples that GCC considers warnings:
1119///
1120/// int a, *pint;
1121/// short *pshort;
1122/// struct foo *pfoo;
1123///
1124/// pint = pshort; // warning: assignment from incompatible pointer type
1125/// a = pint; // warning: assignment makes integer from pointer without a cast
1126/// pint = a; // warning: assignment makes pointer from integer without a cast
1127/// pint = pfoo; // warning: assignment from incompatible pointer type
1128///
1129/// As a result, the code for dealing with pointers is more complex than the
1130/// C99 spec dictates.
1131/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1132///
Chris Lattner5cf216b2008-01-04 18:04:52 +00001133Sema::AssignConvertType
Reid Spencer5f016e22007-07-11 17:01:13 +00001134Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattnerfc144e22008-01-04 23:18:45 +00001135 // Get canonical types. We're not formatting these types, just comparing
1136 // them.
1137 lhsType = lhsType.getCanonicalType();
1138 rhsType = rhsType.getCanonicalType();
1139
1140 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerd2656dd2008-01-07 17:51:46 +00001141 return Compatible; // Common case: fast path an exact match.
Steve Naroff700204c2007-07-24 21:46:40 +00001142
Anders Carlsson793680e2007-10-12 23:56:29 +00001143 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Steve Naroffec0550f2007-10-15 20:41:53 +00001144 if (Context.referenceTypesAreCompatible(lhsType, rhsType))
Anders Carlsson793680e2007-10-12 23:56:29 +00001145 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001146 return Incompatible;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001147 }
Chris Lattnerfc144e22008-01-04 23:18:45 +00001148
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001149 if (lhsType->isObjCQualifiedIdType()
1150 || rhsType->isObjCQualifiedIdType()) {
1151 if (Context.ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType))
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001152 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001153 return Incompatible;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001154 }
Chris Lattnere8b3e962008-01-04 23:32:24 +00001155
1156 if (lhsType->isVectorType() || rhsType->isVectorType()) {
1157 // For OCUVector, allow vector splats; float -> <n x float>
1158 if (const OCUVectorType *LV = lhsType->getAsOCUVectorType()) {
1159 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1160 return Compatible;
1161 }
1162
1163 // If LHS and RHS are both vectors of integer or both vectors of floating
1164 // point types, and the total vector length is the same, allow the
1165 // conversion. This is a bitcast; no bits are changed but the result type
1166 // is different.
1167 if (getLangOptions().LaxVectorConversions &&
1168 lhsType->isVectorType() && rhsType->isVectorType()) {
1169 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1170 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
1171 if (Context.getTypeSize(lhsType, SourceLocation()) ==
1172 Context.getTypeSize(rhsType, SourceLocation()))
Nate Begeman4119d1a2007-12-30 02:59:45 +00001173 return Compatible;
1174 }
Chris Lattnere8b3e962008-01-04 23:32:24 +00001175 }
1176 return Incompatible;
1177 }
1178
1179 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001181
1182 if (lhsType->isPointerType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 if (rhsType->isIntegerType())
Chris Lattnerb7b61152008-01-04 18:22:42 +00001184 return IntToPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +00001185
1186 if (rhsType->isPointerType())
1187 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnerfc144e22008-01-04 23:18:45 +00001188 return Incompatible;
1189 }
1190
1191 if (rhsType->isPointerType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1193 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
Chris Lattnerb7b61152008-01-04 18:22:42 +00001194 return PointerToInt;
Reid Spencer5f016e22007-07-11 17:01:13 +00001195
1196 if (lhsType->isPointerType())
1197 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnerfc144e22008-01-04 23:18:45 +00001198 return Incompatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001199 }
1200
1201 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Steve Naroffec0550f2007-10-15 20:41:53 +00001202 if (Context.tagTypesAreCompatible(lhsType, rhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 return Compatible;
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 }
1205 return Incompatible;
1206}
1207
Chris Lattner5cf216b2008-01-04 18:04:52 +00001208Sema::AssignConvertType
Steve Naroff90045e82007-07-13 23:32:42 +00001209Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff529a4ad2007-11-27 17:58:44 +00001210 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1211 // a null pointer constant.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001212 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahanian9d3185e2008-01-03 18:46:52 +00001213 && rExpr->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001214 ImpCastExprToType(rExpr, lhsType);
Steve Naroff529a4ad2007-11-27 17:58:44 +00001215 return Compatible;
1216 }
Chris Lattner943140e2007-10-16 02:55:40 +00001217 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroff90045e82007-07-13 23:32:42 +00001218 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff08d92e42007-09-15 18:49:24 +00001219 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroff90045e82007-07-13 23:32:42 +00001220 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner943140e2007-10-16 02:55:40 +00001221 //
1222 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1223 // are better understood.
1224 if (!lhsType->isReferenceType())
1225 DefaultFunctionArrayConversion(rExpr);
Steve Narofff1120de2007-08-24 22:33:52 +00001226
Chris Lattner5cf216b2008-01-04 18:04:52 +00001227 Sema::AssignConvertType result =
1228 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Narofff1120de2007-08-24 22:33:52 +00001229
1230 // C99 6.5.16.1p2: The value of the right operand is converted to the
1231 // type of the assignment expression.
1232 if (rExpr->getType() != lhsType)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001233 ImpCastExprToType(rExpr, lhsType);
Steve Narofff1120de2007-08-24 22:33:52 +00001234 return result;
Steve Naroff90045e82007-07-13 23:32:42 +00001235}
1236
Chris Lattner5cf216b2008-01-04 18:04:52 +00001237Sema::AssignConvertType
Steve Naroff90045e82007-07-13 23:32:42 +00001238Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1239 return CheckAssignmentConstraints(lhsType, rhsType);
1240}
1241
Chris Lattnerca5eede2007-12-12 05:47:28 +00001242QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 Diag(loc, diag::err_typecheck_invalid_operands,
1244 lex->getType().getAsString(), rex->getType().getAsString(),
1245 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnerca5eede2007-12-12 05:47:28 +00001246 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001247}
1248
Steve Naroff49b45262007-07-13 16:58:59 +00001249inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1250 Expr *&rex) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 QualType lhsType = lex->getType(), rhsType = rex->getType();
1252
1253 // make sure the vector types are identical.
1254 if (lhsType == rhsType)
1255 return lhsType;
Nate Begeman4119d1a2007-12-30 02:59:45 +00001256
1257 // if the lhs is an ocu vector and the rhs is a scalar of the same type,
1258 // promote the rhs to the vector type.
1259 if (const OCUVectorType *V = lhsType->getAsOCUVectorType()) {
1260 if (V->getElementType().getCanonicalType().getTypePtr()
1261 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001262 ImpCastExprToType(rex, lhsType);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001263 return lhsType;
1264 }
1265 }
1266
1267 // if the rhs is an ocu vector and the lhs is a scalar of the same type,
1268 // promote the lhs to the vector type.
1269 if (const OCUVectorType *V = rhsType->getAsOCUVectorType()) {
1270 if (V->getElementType().getCanonicalType().getTypePtr()
1271 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001272 ImpCastExprToType(lex, rhsType);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001273 return rhsType;
1274 }
1275 }
1276
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 // You cannot convert between vector values of different size.
1278 Diag(loc, diag::err_typecheck_vector_not_convertable,
1279 lex->getType().getAsString(), rex->getType().getAsString(),
1280 lex->getSourceRange(), rex->getSourceRange());
1281 return QualType();
1282}
1283
1284inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001285 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001286{
Steve Naroff90045e82007-07-13 23:32:42 +00001287 QualType lhsType = lex->getType(), rhsType = rex->getType();
1288
1289 if (lhsType->isVectorType() || rhsType->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 return CheckVectorOperands(loc, lex, rex);
Steve Naroff49b45262007-07-13 16:58:59 +00001291
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001292 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001293
Steve Naroffa4332e22007-07-17 00:58:39 +00001294 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001295 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001296 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001297}
1298
1299inline QualType Sema::CheckRemainderOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001300 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001301{
Steve Naroff90045e82007-07-13 23:32:42 +00001302 QualType lhsType = lex->getType(), rhsType = rex->getType();
1303
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001304 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001305
Steve Naroffa4332e22007-07-17 00:58:39 +00001306 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001307 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001308 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001309}
1310
1311inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001312 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001313{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001314 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff49b45262007-07-13 16:58:59 +00001315 return CheckVectorOperands(loc, lex, rex);
1316
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001317 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff3e5e5562007-07-16 22:23:01 +00001318
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 // handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001320 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001321 return compType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001322
Steve Naroffa4332e22007-07-17 00:58:39 +00001323 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1324 return lex->getType();
1325 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1326 return rex->getType();
Chris Lattnerca5eede2007-12-12 05:47:28 +00001327 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001328}
1329
1330inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001331 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001332{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001333 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001335
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001336 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001337
Chris Lattner6e4ab612007-12-09 21:53:25 +00001338 // Enforce type constraints: C99 6.5.6p3.
1339
1340 // Handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001341 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001342 return compType;
Chris Lattner6e4ab612007-12-09 21:53:25 +00001343
1344 // Either ptr - int or ptr - ptr.
1345 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff2565eef2008-01-29 18:58:14 +00001346 QualType lpointee = LHSPTy->getPointeeType();
1347
Chris Lattner6e4ab612007-12-09 21:53:25 +00001348 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff2565eef2008-01-29 18:58:14 +00001349 if (!lpointee->isObjectType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001350 // Handle the GNU void* extension.
Steve Naroff2565eef2008-01-29 18:58:14 +00001351 if (lpointee->isVoidType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001352 Diag(loc, diag::ext_gnu_void_ptr,
1353 lex->getSourceRange(), rex->getSourceRange());
1354 } else {
1355 Diag(loc, diag::err_typecheck_sub_ptr_object,
1356 lex->getType().getAsString(), lex->getSourceRange());
1357 return QualType();
1358 }
1359 }
1360
1361 // The result type of a pointer-int computation is the pointer type.
1362 if (rex->getType()->isIntegerType())
1363 return lex->getType();
Steve Naroff3e5e5562007-07-16 22:23:01 +00001364
Chris Lattner6e4ab612007-12-09 21:53:25 +00001365 // Handle pointer-pointer subtractions.
1366 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Steve Naroff2565eef2008-01-29 18:58:14 +00001367 QualType rpointee = RHSPTy->getPointeeType();
1368
Chris Lattner6e4ab612007-12-09 21:53:25 +00001369 // RHS must be an object type, unless void (GNU).
Steve Naroff2565eef2008-01-29 18:58:14 +00001370 if (!rpointee->isObjectType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001371 // Handle the GNU void* extension.
Steve Naroff2565eef2008-01-29 18:58:14 +00001372 if (rpointee->isVoidType()) {
1373 if (!lpointee->isVoidType())
Chris Lattner6e4ab612007-12-09 21:53:25 +00001374 Diag(loc, diag::ext_gnu_void_ptr,
1375 lex->getSourceRange(), rex->getSourceRange());
1376 } else {
1377 Diag(loc, diag::err_typecheck_sub_ptr_object,
1378 rex->getType().getAsString(), rex->getSourceRange());
1379 return QualType();
1380 }
1381 }
1382
1383 // Pointee types must be compatible.
Steve Naroff2565eef2008-01-29 18:58:14 +00001384 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1385 rpointee.getUnqualifiedType())) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001386 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1387 lex->getType().getAsString(), rex->getType().getAsString(),
1388 lex->getSourceRange(), rex->getSourceRange());
1389 return QualType();
1390 }
1391
1392 return Context.getPointerDiffType();
1393 }
1394 }
1395
Chris Lattnerca5eede2007-12-12 05:47:28 +00001396 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001397}
1398
1399inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Chris Lattnerca5eede2007-12-12 05:47:28 +00001400 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) {
1401 // C99 6.5.7p2: Each of the operands shall have integer type.
1402 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1403 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001404
Chris Lattnerca5eede2007-12-12 05:47:28 +00001405 // Shifts don't perform usual arithmetic conversions, they just do integer
1406 // promotions on each operand. C99 6.5.7p3
Chris Lattner1dcf2c82007-12-13 07:28:16 +00001407 if (!isCompAssign)
1408 UsualUnaryConversions(lex);
Chris Lattnerca5eede2007-12-12 05:47:28 +00001409 UsualUnaryConversions(rex);
1410
1411 // "The type of the result is that of the promoted left operand."
1412 return lex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001413}
1414
Chris Lattnera5937dd2007-08-26 01:18:55 +00001415inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1416 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Reid Spencer5f016e22007-07-11 17:01:13 +00001417{
Chris Lattnera5937dd2007-08-26 01:18:55 +00001418 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff30bf7712007-08-10 18:26:40 +00001419 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1420 UsualArithmeticConversions(lex, rex);
1421 else {
1422 UsualUnaryConversions(lex);
1423 UsualUnaryConversions(rex);
1424 }
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001425 QualType lType = lex->getType();
1426 QualType rType = rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001427
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001428 // For non-floating point types, check for self-comparisons of the form
1429 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1430 // often indicate logic errors in the program.
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00001431 if (!lType->isFloatingType()) {
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001432 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1433 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00001434 if (DRL->getDecl() == DRR->getDecl())
1435 Diag(loc, diag::warn_selfcomparison);
1436 }
1437
Chris Lattnera5937dd2007-08-26 01:18:55 +00001438 if (isRelational) {
1439 if (lType->isRealType() && rType->isRealType())
1440 return Context.IntTy;
1441 } else {
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001442 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001443 if (lType->isFloatingType()) {
1444 assert (rType->isFloatingType());
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001445 CheckFloatComparison(loc,lex,rex);
Ted Kremenek6a261552007-10-29 16:40:01 +00001446 }
1447
Chris Lattnera5937dd2007-08-26 01:18:55 +00001448 if (lType->isArithmeticType() && rType->isArithmeticType())
1449 return Context.IntTy;
1450 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001451
Chris Lattnerd28f8152007-08-26 01:10:14 +00001452 bool LHSIsNull = lex->isNullPointerConstant(Context);
1453 bool RHSIsNull = rex->isNullPointerConstant(Context);
1454
Chris Lattnera5937dd2007-08-26 01:18:55 +00001455 // All of the following pointer related warnings are GCC extensions, except
1456 // when handling null pointer constants. One day, we can consider making them
1457 // errors (when -pedantic-errors is enabled).
Steve Naroff77878cc2007-08-27 04:08:11 +00001458 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Steve Naroff2565eef2008-01-29 18:58:14 +00001459 QualType lpointee = lType->getAsPointerType()->getPointeeType();
1460 QualType rpointee = rType->getAsPointerType()->getPointeeType();
1461
Steve Naroff66296cb2007-11-13 14:57:38 +00001462 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Steve Naroff2565eef2008-01-29 18:58:14 +00001463 !lpointee->isVoidType() && !lpointee->isVoidType() &&
1464 !Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1465 rpointee.getUnqualifiedType())) {
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001466 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1467 lType.getAsString(), rType.getAsString(),
1468 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +00001469 }
Chris Lattner1e0a3902008-01-16 19:17:22 +00001470 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001471 return Context.IntTy;
1472 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001473 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
1474 && Context.ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001475 ImpCastExprToType(rex, lType);
Fariborz Jahanian7359f042007-12-20 01:06:58 +00001476 return Context.IntTy;
1477 }
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001478 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001479 if (!RHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001480 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1481 lType.getAsString(), rType.getAsString(),
1482 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1e0a3902008-01-16 19:17:22 +00001483 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001484 return Context.IntTy;
1485 }
1486 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001487 if (!LHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001488 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1489 lType.getAsString(), rType.getAsString(),
1490 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1e0a3902008-01-16 19:17:22 +00001491 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001492 return Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001493 }
Chris Lattnerca5eede2007-12-12 05:47:28 +00001494 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001495}
1496
Reid Spencer5f016e22007-07-11 17:01:13 +00001497inline QualType Sema::CheckBitwiseOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001498 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001499{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001500 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001501 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001502
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001503 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001504
Steve Naroffa4332e22007-07-17 00:58:39 +00001505 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001506 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001507 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001508}
1509
1510inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff49b45262007-07-13 16:58:59 +00001511 Expr *&lex, Expr *&rex, SourceLocation loc)
Reid Spencer5f016e22007-07-11 17:01:13 +00001512{
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001513 UsualUnaryConversions(lex);
1514 UsualUnaryConversions(rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001515
Steve Naroffa4332e22007-07-17 00:58:39 +00001516 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 return Context.IntTy;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001518 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001519}
1520
1521inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Narofff1120de2007-08-24 22:33:52 +00001522 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Reid Spencer5f016e22007-07-11 17:01:13 +00001523{
1524 QualType lhsType = lex->getType();
1525 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1527
1528 switch (mlval) { // C99 6.5.16p2
Chris Lattner5cf216b2008-01-04 18:04:52 +00001529 case Expr::MLV_Valid:
1530 break;
1531 case Expr::MLV_ConstQualified:
1532 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1533 return QualType();
1534 case Expr::MLV_ArrayType:
1535 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1536 lhsType.getAsString(), lex->getSourceRange());
1537 return QualType();
1538 case Expr::MLV_NotObjectType:
1539 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1540 lhsType.getAsString(), lex->getSourceRange());
1541 return QualType();
1542 case Expr::MLV_InvalidExpression:
1543 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1544 lex->getSourceRange());
1545 return QualType();
1546 case Expr::MLV_IncompleteType:
1547 case Expr::MLV_IncompleteVoidType:
1548 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1549 lhsType.getAsString(), lex->getSourceRange());
1550 return QualType();
1551 case Expr::MLV_DuplicateVectorComponents:
1552 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1553 lex->getSourceRange());
1554 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001555 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001556
Chris Lattner5cf216b2008-01-04 18:04:52 +00001557 AssignConvertType ConvTy;
1558 if (compoundType.isNull())
1559 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1560 else
1561 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1562
1563 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1564 rex, "assigning"))
1565 return QualType();
1566
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 // C99 6.5.16p3: The type of an assignment expression is the type of the
1568 // left operand unless the left operand has qualified type, in which case
1569 // it is the unqualified version of the type of the left operand.
1570 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1571 // is converted to the type of the assignment expression (above).
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001572 // C++ 5.17p1: the type of the assignment expression is that of its left
1573 // oprdu.
Chris Lattner5cf216b2008-01-04 18:04:52 +00001574 return lhsType.getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001575}
1576
1577inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff49b45262007-07-13 16:58:59 +00001578 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001579 UsualUnaryConversions(rex);
1580 return rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001581}
1582
Steve Naroff49b45262007-07-13 16:58:59 +00001583/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1584/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Reid Spencer5f016e22007-07-11 17:01:13 +00001585QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff49b45262007-07-13 16:58:59 +00001586 QualType resType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001587 assert(!resType.isNull() && "no type for increment/decrement expression");
1588
Steve Naroff084f9ed2007-08-24 17:20:07 +00001589 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffd848a382007-11-11 14:15:57 +00001590 if (const PointerType *pt = resType->getAsPointerType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
1592 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1593 resType.getAsString(), op->getSourceRange());
1594 return QualType();
1595 }
Steve Naroff084f9ed2007-08-24 17:20:07 +00001596 } else if (!resType->isRealType()) {
1597 if (resType->isComplexType())
1598 // C99 does not support ++/-- on complex types.
1599 Diag(OpLoc, diag::ext_integer_increment_complex,
1600 resType.getAsString(), op->getSourceRange());
1601 else {
1602 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1603 resType.getAsString(), op->getSourceRange());
1604 return QualType();
1605 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 }
Steve Naroffdd10e022007-08-23 21:37:33 +00001607 // At this point, we know we have a real, complex or pointer type.
1608 // Now make sure the operand is a modifiable lvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +00001609 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1610 if (mlval != Expr::MLV_Valid) {
1611 // FIXME: emit a more precise diagnostic...
1612 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1613 op->getSourceRange());
1614 return QualType();
1615 }
1616 return resType;
1617}
1618
1619/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
1620/// This routine allows us to typecheck complex/recursive expressions
1621/// where the declaration is needed for type checking. Here are some
1622/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
1623static Decl *getPrimaryDeclaration(Expr *e) {
1624 switch (e->getStmtClass()) {
1625 case Stmt::DeclRefExprClass:
1626 return cast<DeclRefExpr>(e)->getDecl();
1627 case Stmt::MemberExprClass:
Chris Lattnerf82228f2007-11-16 17:46:48 +00001628 // Fields cannot be declared with a 'register' storage class.
1629 // &X->f is always ok, even if X is declared register.
1630 if (cast<MemberExpr>(e)->isArrow())
1631 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001632 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
1633 case Stmt::ArraySubscriptExprClass:
Chris Lattnerf82228f2007-11-16 17:46:48 +00001634 // &X[4] and &4[X] is invalid if X is invalid.
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Reid Spencer5f016e22007-07-11 17:01:13 +00001636 case Stmt::UnaryOperatorClass:
1637 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
1638 case Stmt::ParenExprClass:
1639 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Chris Lattnerf82228f2007-11-16 17:46:48 +00001640 case Stmt::ImplicitCastExprClass:
1641 // &X[4] when X is an array, has an implicit cast from array to pointer.
1642 return getPrimaryDeclaration(cast<ImplicitCastExpr>(e)->getSubExpr());
Reid Spencer5f016e22007-07-11 17:01:13 +00001643 default:
1644 return 0;
1645 }
1646}
1647
1648/// CheckAddressOfOperand - The operand of & must be either a function
1649/// designator or an lvalue designating an object. If it is an lvalue, the
1650/// object cannot be declared with storage class register or be a bit field.
1651/// Note: The usual conversions are *not* applied to the operand of the &
1652/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1653QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff08f19672008-01-13 17:10:08 +00001654 if (getLangOptions().C99) {
1655 // Implement C99-only parts of addressof rules.
1656 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1657 if (uOp->getOpcode() == UnaryOperator::Deref)
1658 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1659 // (assuming the deref expression is valid).
1660 return uOp->getSubExpr()->getType();
1661 }
1662 // Technically, there should be a check for array subscript
1663 // expressions here, but the result of one is always an lvalue anyway.
1664 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 Decl *dcl = getPrimaryDeclaration(op);
1666 Expr::isLvalueResult lval = op->isLvalue();
1667
1668 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnerf82228f2007-11-16 17:46:48 +00001669 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1670 // FIXME: emit more specific diag...
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1672 op->getSourceRange());
1673 return QualType();
1674 }
1675 } else if (dcl) {
1676 // We have an lvalue with a decl. Make sure the decl is not declared
1677 // with the register storage-class specifier.
1678 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1679 if (vd->getStorageClass() == VarDecl::Register) {
1680 Diag(OpLoc, diag::err_typecheck_address_of_register,
1681 op->getSourceRange());
1682 return QualType();
1683 }
1684 } else
1685 assert(0 && "Unknown/unexpected decl type");
1686
1687 // FIXME: add check for bitfields!
1688 }
1689 // If the operand has type "type", the result has type "pointer to type".
1690 return Context.getPointerType(op->getType());
1691}
1692
1693QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001694 UsualUnaryConversions(op);
1695 QualType qType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001696
Chris Lattnerbefee482007-07-31 16:53:04 +00001697 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff08f19672008-01-13 17:10:08 +00001698 // Note that per both C89 and C99, this is always legal, even
1699 // if ptype is an incomplete type or void.
1700 // It would be possible to warn about dereferencing a
1701 // void pointer, but it's completely well-defined,
1702 // and such a warning is unlikely to catch any mistakes.
1703 return PT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 }
1705 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1706 qType.getAsString(), op->getSourceRange());
1707 return QualType();
1708}
1709
1710static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1711 tok::TokenKind Kind) {
1712 BinaryOperator::Opcode Opc;
1713 switch (Kind) {
1714 default: assert(0 && "Unknown binop!");
1715 case tok::star: Opc = BinaryOperator::Mul; break;
1716 case tok::slash: Opc = BinaryOperator::Div; break;
1717 case tok::percent: Opc = BinaryOperator::Rem; break;
1718 case tok::plus: Opc = BinaryOperator::Add; break;
1719 case tok::minus: Opc = BinaryOperator::Sub; break;
1720 case tok::lessless: Opc = BinaryOperator::Shl; break;
1721 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1722 case tok::lessequal: Opc = BinaryOperator::LE; break;
1723 case tok::less: Opc = BinaryOperator::LT; break;
1724 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1725 case tok::greater: Opc = BinaryOperator::GT; break;
1726 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1727 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1728 case tok::amp: Opc = BinaryOperator::And; break;
1729 case tok::caret: Opc = BinaryOperator::Xor; break;
1730 case tok::pipe: Opc = BinaryOperator::Or; break;
1731 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1732 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1733 case tok::equal: Opc = BinaryOperator::Assign; break;
1734 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1735 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1736 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1737 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1738 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1739 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1740 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1741 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1742 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1743 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1744 case tok::comma: Opc = BinaryOperator::Comma; break;
1745 }
1746 return Opc;
1747}
1748
1749static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1750 tok::TokenKind Kind) {
1751 UnaryOperator::Opcode Opc;
1752 switch (Kind) {
1753 default: assert(0 && "Unknown unary op!");
1754 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1755 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1756 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1757 case tok::star: Opc = UnaryOperator::Deref; break;
1758 case tok::plus: Opc = UnaryOperator::Plus; break;
1759 case tok::minus: Opc = UnaryOperator::Minus; break;
1760 case tok::tilde: Opc = UnaryOperator::Not; break;
1761 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1762 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1763 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1764 case tok::kw___real: Opc = UnaryOperator::Real; break;
1765 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1766 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1767 }
1768 return Opc;
1769}
1770
1771// Binary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00001772Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Reid Spencer5f016e22007-07-11 17:01:13 +00001773 ExprTy *LHS, ExprTy *RHS) {
1774 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1775 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1776
Steve Narofff69936d2007-09-16 03:34:24 +00001777 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1778 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Reid Spencer5f016e22007-07-11 17:01:13 +00001779
1780 QualType ResultTy; // Result type of the binary operator.
1781 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
1782
1783 switch (Opc) {
1784 default:
1785 assert(0 && "Unknown binary expr!");
1786 case BinaryOperator::Assign:
1787 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
1788 break;
1789 case BinaryOperator::Mul:
1790 case BinaryOperator::Div:
1791 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1792 break;
1793 case BinaryOperator::Rem:
1794 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1795 break;
1796 case BinaryOperator::Add:
1797 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1798 break;
1799 case BinaryOperator::Sub:
1800 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1801 break;
1802 case BinaryOperator::Shl:
1803 case BinaryOperator::Shr:
1804 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
1805 break;
1806 case BinaryOperator::LE:
1807 case BinaryOperator::LT:
1808 case BinaryOperator::GE:
1809 case BinaryOperator::GT:
Chris Lattnera5937dd2007-08-26 01:18:55 +00001810 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 break;
1812 case BinaryOperator::EQ:
1813 case BinaryOperator::NE:
Chris Lattnera5937dd2007-08-26 01:18:55 +00001814 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 break;
1816 case BinaryOperator::And:
1817 case BinaryOperator::Xor:
1818 case BinaryOperator::Or:
1819 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1820 break;
1821 case BinaryOperator::LAnd:
1822 case BinaryOperator::LOr:
1823 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
1824 break;
1825 case BinaryOperator::MulAssign:
1826 case BinaryOperator::DivAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001827 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001828 if (!CompTy.isNull())
1829 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1830 break;
1831 case BinaryOperator::RemAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001832 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 if (!CompTy.isNull())
1834 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1835 break;
1836 case BinaryOperator::AddAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001837 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 if (!CompTy.isNull())
1839 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1840 break;
1841 case BinaryOperator::SubAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001842 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001843 if (!CompTy.isNull())
1844 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1845 break;
1846 case BinaryOperator::ShlAssign:
1847 case BinaryOperator::ShrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001848 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001849 if (!CompTy.isNull())
1850 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1851 break;
1852 case BinaryOperator::AndAssign:
1853 case BinaryOperator::XorAssign:
1854 case BinaryOperator::OrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001855 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 if (!CompTy.isNull())
1857 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1858 break;
1859 case BinaryOperator::Comma:
1860 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
1861 break;
1862 }
1863 if (ResultTy.isNull())
1864 return true;
1865 if (CompTy.isNull())
Chris Lattner17d1b2a2007-08-28 18:36:55 +00001866 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001867 else
Chris Lattner17d1b2a2007-08-28 18:36:55 +00001868 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001869}
1870
1871// Unary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00001872Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Reid Spencer5f016e22007-07-11 17:01:13 +00001873 ExprTy *input) {
1874 Expr *Input = (Expr*)input;
1875 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1876 QualType resultType;
1877 switch (Opc) {
1878 default:
1879 assert(0 && "Unimplemented unary expr!");
1880 case UnaryOperator::PreInc:
1881 case UnaryOperator::PreDec:
1882 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
1883 break;
1884 case UnaryOperator::AddrOf:
1885 resultType = CheckAddressOfOperand(Input, OpLoc);
1886 break;
1887 case UnaryOperator::Deref:
Steve Naroff1ca9b112007-12-18 04:06:57 +00001888 DefaultFunctionArrayConversion(Input);
Reid Spencer5f016e22007-07-11 17:01:13 +00001889 resultType = CheckIndirectionOperand(Input, OpLoc);
1890 break;
1891 case UnaryOperator::Plus:
1892 case UnaryOperator::Minus:
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001893 UsualUnaryConversions(Input);
1894 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001895 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1896 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1897 resultType.getAsString());
1898 break;
1899 case UnaryOperator::Not: // bitwise complement
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001900 UsualUnaryConversions(Input);
1901 resultType = Input->getType();
Steve Naroff084f9ed2007-08-24 17:20:07 +00001902 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1903 if (!resultType->isIntegerType()) {
1904 if (resultType->isComplexType())
1905 // C99 does not support '~' for complex conjugation.
1906 Diag(OpLoc, diag::ext_integer_complement_complex,
1907 resultType.getAsString());
1908 else
1909 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1910 resultType.getAsString());
1911 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 break;
1913 case UnaryOperator::LNot: // logical negation
1914 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001915 DefaultFunctionArrayConversion(Input);
1916 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 if (!resultType->isScalarType()) // C99 6.5.3.3p1
1918 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1919 resultType.getAsString());
1920 // LNot always has type int. C99 6.5.3.3p5.
1921 resultType = Context.IntTy;
1922 break;
1923 case UnaryOperator::SizeOf:
1924 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
1925 break;
1926 case UnaryOperator::AlignOf:
1927 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1928 break;
Chris Lattnerdbb36972007-08-24 21:16:53 +00001929 case UnaryOperator::Real:
Chris Lattnerdbb36972007-08-24 21:16:53 +00001930 case UnaryOperator::Imag:
Chris Lattner5d794252007-08-24 21:41:10 +00001931 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattnerdbb36972007-08-24 21:16:53 +00001932 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 case UnaryOperator::Extension:
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 resultType = Input->getType();
1935 break;
1936 }
1937 if (resultType.isNull())
1938 return true;
1939 return new UnaryOperator(Input, Opc, resultType, OpLoc);
1940}
1941
Steve Naroff1b273c42007-09-16 14:56:35 +00001942/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1943Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001944 SourceLocation LabLoc,
1945 IdentifierInfo *LabelII) {
1946 // Look up the record for this label identifier.
1947 LabelStmt *&LabelDecl = LabelMap[LabelII];
1948
1949 // If we haven't seen this label yet, create a forward reference.
1950 if (LabelDecl == 0)
1951 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1952
1953 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattner6481a572007-08-03 17:31:20 +00001954 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1955 Context.getPointerType(Context.VoidTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001956}
1957
Steve Naroff1b273c42007-09-16 14:56:35 +00001958Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001959 SourceLocation RPLoc) { // "({..})"
1960 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1961 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1962 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1963
1964 // FIXME: there are a variety of strange constraints to enforce here, for
1965 // example, it is not possible to goto into a stmt expression apparently.
1966 // More semantic analysis is needed.
1967
1968 // FIXME: the last statement in the compount stmt has its value used. We
1969 // should not warn about it being unused.
1970
1971 // If there are sub stmts in the compound stmt, take the type of the last one
1972 // as the type of the stmtexpr.
1973 QualType Ty = Context.VoidTy;
1974
1975 if (!Compound->body_empty())
1976 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1977 Ty = LastExpr->getType();
1978
1979 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1980}
Steve Naroffd34e9152007-08-01 22:05:33 +00001981
Steve Naroff1b273c42007-09-16 14:56:35 +00001982Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001983 SourceLocation TypeLoc,
1984 TypeTy *argty,
1985 OffsetOfComponent *CompPtr,
1986 unsigned NumComponents,
1987 SourceLocation RPLoc) {
1988 QualType ArgTy = QualType::getFromOpaquePtr(argty);
1989 assert(!ArgTy.isNull() && "Missing type argument!");
1990
1991 // We must have at least one component that refers to the type, and the first
1992 // one is known to be a field designator. Verify that the ArgTy represents
1993 // a struct/union/class.
1994 if (!ArgTy->isRecordType())
1995 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
1996
1997 // Otherwise, create a compound literal expression as the base, and
1998 // iteratively process the offsetof designators.
Steve Naroffe9b12192008-01-14 18:19:28 +00001999 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002000
Chris Lattner9e2b75c2007-08-31 21:49:13 +00002001 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2002 // GCC extension, diagnose them.
2003 if (NumComponents != 1)
2004 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2005 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2006
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002007 for (unsigned i = 0; i != NumComponents; ++i) {
2008 const OffsetOfComponent &OC = CompPtr[i];
2009 if (OC.isBrackets) {
2010 // Offset of an array sub-field. TODO: Should we allow vector elements?
2011 const ArrayType *AT = Res->getType()->getAsArrayType();
2012 if (!AT) {
2013 delete Res;
2014 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2015 Res->getType().getAsString());
2016 }
2017
Chris Lattner704fe352007-08-30 17:59:59 +00002018 // FIXME: C++: Verify that operator[] isn't overloaded.
2019
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002020 // C99 6.5.2.1p1
2021 Expr *Idx = static_cast<Expr*>(OC.U.E);
2022 if (!Idx->getType()->isIntegerType())
2023 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2024 Idx->getSourceRange());
2025
2026 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2027 continue;
2028 }
2029
2030 const RecordType *RC = Res->getType()->getAsRecordType();
2031 if (!RC) {
2032 delete Res;
2033 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2034 Res->getType().getAsString());
2035 }
2036
2037 // Get the decl corresponding to this.
2038 RecordDecl *RD = RC->getDecl();
2039 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2040 if (!MemberDecl)
2041 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2042 OC.U.IdentInfo->getName(),
2043 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner704fe352007-08-30 17:59:59 +00002044
2045 // FIXME: C++: Verify that MemberDecl isn't a static field.
2046 // FIXME: Verify that MemberDecl isn't a bitfield.
2047
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002048 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
2049 }
2050
2051 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2052 BuiltinLoc);
2053}
2054
2055
Steve Naroff1b273c42007-09-16 14:56:35 +00002056Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroffd34e9152007-08-01 22:05:33 +00002057 TypeTy *arg1, TypeTy *arg2,
2058 SourceLocation RPLoc) {
2059 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2060 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2061
2062 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2063
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002064 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroffd34e9152007-08-01 22:05:33 +00002065}
2066
Steve Naroff1b273c42007-09-16 14:56:35 +00002067Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroffd04fdd52007-08-03 21:21:27 +00002068 ExprTy *expr1, ExprTy *expr2,
2069 SourceLocation RPLoc) {
2070 Expr *CondExpr = static_cast<Expr*>(cond);
2071 Expr *LHSExpr = static_cast<Expr*>(expr1);
2072 Expr *RHSExpr = static_cast<Expr*>(expr2);
2073
2074 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2075
2076 // The conditional expression is required to be a constant expression.
2077 llvm::APSInt condEval(32);
2078 SourceLocation ExpLoc;
2079 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2080 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2081 CondExpr->getSourceRange());
2082
2083 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2084 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2085 RHSExpr->getType();
2086 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2087}
2088
Nate Begeman67295d02008-01-30 20:50:20 +00002089/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begemane2ce1d92008-01-17 17:46:27 +00002090/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begeman67295d02008-01-30 20:50:20 +00002091/// The number of arguments has already been validated to match the number of
2092/// arguments in FnType.
2093static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begemane2ce1d92008-01-17 17:46:27 +00002094 unsigned NumParams = FnType->getNumArgs();
2095 for (unsigned i = 0; i != NumParams; ++i)
Nate Begeman67295d02008-01-30 20:50:20 +00002096 if (Args[i]->getType().getCanonicalType() !=
2097 FnType->getArgType(i).getCanonicalType())
Nate Begemane2ce1d92008-01-17 17:46:27 +00002098 return false;
2099 return true;
2100}
2101
2102Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2103 SourceLocation *CommaLocs,
2104 SourceLocation BuiltinLoc,
2105 SourceLocation RParenLoc) {
2106 assert((NumArgs > 1) && "Too few arguments for OverloadExpr!");
2107
2108 Expr **Args = reinterpret_cast<Expr**>(args);
2109 // The first argument is required to be a constant expression. It tells us
2110 // the number of arguments to pass to each of the functions to be overloaded.
2111 Expr *NParamsExpr = Args[0];
2112 llvm::APSInt constEval(32);
2113 SourceLocation ExpLoc;
2114 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2115 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2116 NParamsExpr->getSourceRange());
2117
2118 // Verify that the number of parameters is > 0
2119 unsigned NumParams = constEval.getZExtValue();
2120 if (NumParams == 0)
2121 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2122 NParamsExpr->getSourceRange());
2123 // Verify that we have at least 1 + NumParams arguments to the builtin.
2124 if ((NumParams + 1) > NumArgs)
2125 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2126 SourceRange(BuiltinLoc, RParenLoc));
2127
2128 // Figure out the return type, by matching the args to one of the functions
Nate Begeman67295d02008-01-30 20:50:20 +00002129 // listed after the parameters.
Nate Begemane2ce1d92008-01-17 17:46:27 +00002130 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2131 // UsualUnaryConversions will convert the function DeclRefExpr into a
2132 // pointer to function.
2133 Expr *Fn = UsualUnaryConversions(Args[i]);
2134 FunctionTypeProto *FnType = 0;
Nate Begeman67295d02008-01-30 20:50:20 +00002135 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2136 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2137 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2138 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002139
2140 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2141 // parameters, and the number of parameters must match the value passed to
2142 // the builtin.
2143 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begeman67295d02008-01-30 20:50:20 +00002144 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2145 Fn->getSourceRange());
Nate Begemane2ce1d92008-01-17 17:46:27 +00002146
2147 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begeman67295d02008-01-30 20:50:20 +00002148 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begemane2ce1d92008-01-17 17:46:27 +00002149 // If they match, return a new OverloadExpr.
Nate Begeman67295d02008-01-30 20:50:20 +00002150 if (ExprsMatchFnType(Args+1, FnType))
Nate Begemane2ce1d92008-01-17 17:46:27 +00002151 return new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2152 BuiltinLoc, RParenLoc);
2153 }
2154
2155 // If we didn't find a matching function Expr in the __builtin_overload list
2156 // the return an error.
2157 std::string typeNames;
Nate Begeman67295d02008-01-30 20:50:20 +00002158 for (unsigned i = 0; i != NumParams; ++i) {
2159 if (i != 0) typeNames += ", ";
2160 typeNames += Args[i+1]->getType().getAsString();
2161 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002162
2163 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2164 SourceRange(BuiltinLoc, RParenLoc));
2165}
2166
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002167Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2168 ExprTy *expr, TypeTy *type,
Chris Lattner5cf216b2008-01-04 18:04:52 +00002169 SourceLocation RPLoc) {
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002170 Expr *E = static_cast<Expr*>(expr);
2171 QualType T = QualType::getFromOpaquePtr(type);
2172
2173 InitBuiltinVaListType();
2174
Chris Lattner5cf216b2008-01-04 18:04:52 +00002175 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2176 != Compatible)
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002177 return Diag(E->getLocStart(),
2178 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2179 E->getType().getAsString(),
2180 E->getSourceRange());
2181
2182 // FIXME: Warn if a non-POD type is passed in.
2183
2184 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2185}
2186
Chris Lattner5cf216b2008-01-04 18:04:52 +00002187bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2188 SourceLocation Loc,
2189 QualType DstType, QualType SrcType,
2190 Expr *SrcExpr, const char *Flavor) {
2191 // Decode the result (notice that AST's are still created for extensions).
2192 bool isInvalid = false;
2193 unsigned DiagKind;
2194 switch (ConvTy) {
2195 default: assert(0 && "Unknown conversion type");
2196 case Compatible: return false;
Chris Lattnerb7b61152008-01-04 18:22:42 +00002197 case PointerToInt:
Chris Lattner5cf216b2008-01-04 18:04:52 +00002198 DiagKind = diag::ext_typecheck_convert_pointer_int;
2199 break;
Chris Lattnerb7b61152008-01-04 18:22:42 +00002200 case IntToPointer:
2201 DiagKind = diag::ext_typecheck_convert_int_pointer;
2202 break;
Chris Lattner5cf216b2008-01-04 18:04:52 +00002203 case IncompatiblePointer:
2204 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2205 break;
2206 case FunctionVoidPointer:
2207 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2208 break;
2209 case CompatiblePointerDiscardsQualifiers:
2210 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2211 break;
2212 case Incompatible:
2213 DiagKind = diag::err_typecheck_convert_incompatible;
2214 isInvalid = true;
2215 break;
2216 }
2217
2218 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2219 SrcExpr->getSourceRange());
2220 return isInvalid;
2221}
2222