blob: 7d224c5e802c84889c674321dab19808b79dc717 [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner5b183d82006-11-10 05:03:26 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek43fb8b02007-11-25 00:58:00 +000015#include "SemaUtil.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000016#include "clang/AST/ASTContext.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000017#include "clang/AST/Expr.h"
Steve Naroffd54978b2007-09-18 23:55:05 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000019#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000020#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner08464942007-12-28 05:29:59 +000023#include "llvm/ADT/OwningPtr.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000024#include "llvm/ADT/SmallString.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026using namespace clang;
27
Steve Naroff83895f72007-09-16 03:34:24 +000028/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +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 Naroff83895f72007-09-16 03:34:24 +000035Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000036 assert(NumStringToks && "Must have at least one string!");
37
Steve Naroff4f88b312007-03-13 22:37:02 +000038 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
39 if (Literal.hadError)
40 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000041
Chris Lattner23b7eb62007-06-15 23:05:46 +000042 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000043 for (unsigned i = 0; i != NumStringToks; ++i)
44 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000045
46 // FIXME: handle wchar_t
Anders Carlssoncbfc4b82007-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()));
Steve Narofff1e53692007-03-23 22:27:02 +000058
Chris Lattner5b183d82006-11-10 05:03:26 +000059 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000060 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000061 Literal.AnyWide, t,
62 StringToks[0].getLocation(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000063 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000064}
65
Chris Lattnere168f762006-11-10 05:29:30 +000066
Steve Naroff30d242c2007-09-15 18:49:24 +000067/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattnerac18be92006-11-20 06:49:47 +000068/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
69/// identifier is used in an function call context.
Steve Naroff30d242c2007-09-15 18:49:24 +000070Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattnerac18be92006-11-20 06:49:47 +000071 IdentifierInfo &II,
72 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000073 // Could be enum-constant or decl.
Steve Naroff2f742082007-09-16 16:16:00 +000074 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000075 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000076 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000077 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000078 if (HasTrailingLParen &&
79 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000080 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000081 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000082 else {
Steve Naroffe46504b2007-11-12 14:29:37 +000083 if (CurMethodDecl) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +000084 ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface();
85 ObjCInterfaceDecl *clsDeclared;
86 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, clsDeclared)) {
Steve Narofff60782b2007-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 Naroffe46504b2007-11-12 14:29:37 +000092 }
Chris Lattnerac18be92006-11-20 06:49:47 +000093 // If this name wasn't predeclared and if this is not a function call,
94 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000095 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000096 }
Chris Lattner17ed4872006-11-20 04:58:19 +000097 }
Steve Naroff7e6f7c22007-08-28 03:03:08 +000098 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroffcf871f52007-08-28 18:45:29 +000099 // Only create DeclRefExpr's for valid Decl's.
Steve Narofff93b6722007-08-28 20:14:24 +0000100 if (VD->isInvalidDecl())
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000101 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000102 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000103 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000104 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +0000105 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000106 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanianb31a2f22007-12-05 18:16:33 +0000107 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Steve Narofff1e53692007-03-23 22:27:02 +0000108
109 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +0000110 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +0000111}
Chris Lattnere168f762006-11-10 05:29:30 +0000112
Steve Naroff83895f72007-09-16 03:34:24 +0000113Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson625bfc82007-07-21 05:21:51 +0000114 tok::TokenKind Kind) {
115 PreDefinedExpr::IdentType IT;
116
Chris Lattnere168f762006-11-10 05:29:30 +0000117 switch (Kind) {
118 default:
119 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +0000120 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000121 IT = PreDefinedExpr::Func;
122 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000123 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000124 IT = PreDefinedExpr::Function;
125 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000126 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000127 IT = PreDefinedExpr::PrettyFunction;
128 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000129 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000130
131 // Pre-defined identifiers are always of type char *.
132 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000133}
134
Steve Naroff83895f72007-09-16 03:34:24 +0000135Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000136 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000137 CharBuffer.resize(Tok.getLength());
138 const char *ThisTokBegin = &CharBuffer[0];
139 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
140
141 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
142 Tok.getLocation(), PP);
143 if (Literal.hadError())
144 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000145 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
146 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000147}
148
Steve Naroff83895f72007-09-16 03:34:24 +0000149Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000150 // fast path for a single digit (which is quite common). A single digit
151 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
152 if (Tok.getLength() == 1) {
153 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000154
Chris Lattner9cf21c52007-09-04 02:45:27 +0000155 unsigned IntSize = static_cast<unsigned>(
156 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Chris Lattner23b7eb62007-06-15 23:05:46 +0000157 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
158 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000159 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000160 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000161 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000162 IntegerBuffer.resize(Tok.getLength());
163 const char *ThisTokBegin = &IntegerBuffer[0];
164
Chris Lattner67ca9252007-05-21 01:08:44 +0000165 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000166 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000167 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000168 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000169 if (Literal.hadError)
170 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000171
Chris Lattner1c20a172007-08-26 03:42:43 +0000172 Expr *Res;
173
174 if (Literal.isFloatingLiteral()) {
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000175 QualType Ty;
176 const llvm::fltSemantics *Format;
177 uint64_t Size; unsigned Align;
178
179 if (Literal.isFloat) {
180 Ty = Context.FloatTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000181 Context.Target.getFloatInfo(Size, Align, Format,
182 Context.getFullLoc(Tok.getLocation()));
183
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000184 } else if (Literal.isLong) {
185 Ty = Context.LongDoubleTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000186 Context.Target.getLongDoubleInfo(Size, Align, Format,
187 Context.getFullLoc(Tok.getLocation()));
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000188 } else {
189 Ty = Context.DoubleTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000190 Context.Target.getDoubleInfo(Size, Align, Format,
191 Context.getFullLoc(Tok.getLocation()));
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000192 }
193
Ted Kremenek3a2c9502007-11-29 00:56:49 +0000194 // isExact will be set by GetFloatValue().
195 bool isExact = false;
196
197 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
198 Ty, Tok.getLocation());
199
Chris Lattner1c20a172007-08-26 03:42:43 +0000200 } else if (!Literal.isIntegerLiteral()) {
201 return ExprResult(true);
202 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000203 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000204
Neil Boothac582c52007-08-29 22:00:19 +0000205 // long long is a C99 feature.
206 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth4a1ee052007-08-29 22:13:52 +0000207 Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000208 Diag(Tok.getLocation(), diag::ext_longlong);
209
Chris Lattner67ca9252007-05-21 01:08:44 +0000210 // Get the value in the widest-possible width.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000211 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(
212 Context.getFullLoc(Tok.getLocation())), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000213
214 if (Literal.GetIntegerValue(ResultVal)) {
215 // If this value didn't fit into uintmax_t, warn and force to ull.
216 Diag(Tok.getLocation(), diag::warn_integer_too_large);
217 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000218 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000219 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000220 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000221 // If this value fits into a ULL, try to figure out what else it fits into
222 // according to the rules of C99 6.4.4.1p5.
223
224 // Octal, Hexadecimal, and integers with a U suffix are allowed to
225 // be an unsigned int.
226 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
227
228 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner7b939cf2007-08-23 21:58:08 +0000229 if (!Literal.isLong && !Literal.isLongLong) {
230 // Are int/unsigned possibilities?
Chris Lattner9cf21c52007-09-04 02:45:27 +0000231 unsigned IntSize = static_cast<unsigned>(
232 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000233 // Does it fit in a unsigned int?
234 if (ResultVal.isIntN(IntSize)) {
235 // Does it fit in a signed int?
236 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
237 t = Context.IntTy;
238 else if (AllowUnsigned)
239 t = Context.UnsignedIntTy;
240 }
241
242 if (!t.isNull())
243 ResultVal.trunc(IntSize);
244 }
245
246 // Are long/unsigned long possibilities?
247 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000248 unsigned LongSize = static_cast<unsigned>(
249 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000250
251 // Does it fit in a unsigned long?
252 if (ResultVal.isIntN(LongSize)) {
253 // Does it fit in a signed long?
254 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
255 t = Context.LongTy;
256 else if (AllowUnsigned)
257 t = Context.UnsignedLongTy;
258 }
259 if (!t.isNull())
260 ResultVal.trunc(LongSize);
261 }
262
263 // Finally, check long long if needed.
264 if (t.isNull()) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000265 unsigned LongLongSize = static_cast<unsigned>(
266 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000267
268 // Does it fit in a unsigned long long?
269 if (ResultVal.isIntN(LongLongSize)) {
270 // Does it fit in a signed long long?
271 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
272 t = Context.LongLongTy;
273 else if (AllowUnsigned)
274 t = Context.UnsignedLongLongTy;
275 }
276 }
277
278 // If we still couldn't decide a type, we probably have something that
279 // does not fit in a signed long long, but has no U suffix.
280 if (t.isNull()) {
281 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
282 t = Context.UnsignedLongLongTy;
283 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000284 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000285
Chris Lattner1c20a172007-08-26 03:42:43 +0000286 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000287 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000288
289 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
290 if (Literal.isImaginary)
291 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
292
293 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000294}
295
Steve Naroff83895f72007-09-16 03:34:24 +0000296Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattnere168f762006-11-10 05:29:30 +0000297 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000298 Expr *e = (Expr *)Val;
Steve Naroff83895f72007-09-16 03:34:24 +0000299 assert((e != 0) && "ActOnParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000300 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000301}
302
Steve Naroff71b59a92007-06-04 22:22:31 +0000303/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000304/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000305QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
306 SourceLocation OpLoc, bool isSizeof) {
307 // C99 6.5.3.4p1:
308 if (isa<FunctionType>(exprType) && isSizeof)
309 // alignof(function) is allowed.
310 Diag(OpLoc, diag::ext_sizeof_function_type);
311 else if (exprType->isVoidType())
312 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
313 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000314 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000315 diag::err_alignof_incomplete_type,
316 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000317 return QualType(); // error
318 }
319 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
320 return Context.getSizeType();
321}
322
Chris Lattnere168f762006-11-10 05:29:30 +0000323Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000324ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000325 SourceLocation LPLoc, TypeTy *Ty,
326 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000327 // If error parsing type, ignore.
328 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000329
330 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000331 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000332
Steve Naroff043d45d2007-05-15 02:32:35 +0000333 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
334
335 if (resultType.isNull())
336 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000337 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000338}
339
Chris Lattner74ed76b2007-08-24 21:41:10 +0000340QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000341 DefaultFunctionArrayConversion(V);
342
Chris Lattnere267f5d2007-08-26 05:39:26 +0000343 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000344 if (const ComplexType *CT = V->getType()->getAsComplexType())
345 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000346
347 // Otherwise they pass through real integer and floating point types here.
348 if (V->getType()->isArithmeticType())
349 return V->getType();
350
351 // Reject anything else.
352 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
353 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000354}
355
356
Chris Lattnere168f762006-11-10 05:29:30 +0000357
Steve Naroff83895f72007-09-16 03:34:24 +0000358Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000359 tok::TokenKind Kind,
360 ExprTy *Input) {
361 UnaryOperator::Opcode Opc;
362 switch (Kind) {
363 default: assert(0 && "Unknown unary op!");
364 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
365 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
366 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000367 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000368 if (result.isNull())
369 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000370 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000371}
372
373Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000374ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000375 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000376 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000377
378 // Perform default conversions.
379 DefaultFunctionArrayConversion(LHSExp);
380 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000381
Chris Lattner36d572b2007-07-16 00:14:47 +0000382 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000383
Steve Naroffc1aadb12007-03-28 21:49:40 +0000384 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattnerf17bd422007-08-30 17:45:32 +0000385 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000386 // in the subscript position. As a result, we need to derive the array base
387 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000388 Expr *BaseExpr, *IndexExpr;
389 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000390 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000391 BaseExpr = LHSExp;
392 IndexExpr = RHSExp;
393 // FIXME: need to deal with const...
394 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000395 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000396 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000397 BaseExpr = RHSExp;
398 IndexExpr = LHSExp;
399 // FIXME: need to deal with const...
400 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000401 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
402 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000403 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000404
405 // Component access limited to variables (reject vec4.rg[1]).
406 if (!isa<DeclRefExpr>(BaseExpr))
407 return Diag(LLoc, diag::err_ocuvector_component_access,
408 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000409 // FIXME: need to deal with const...
410 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000411 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000412 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
413 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000414 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000415 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000416 if (!IndexExpr->getType()->isIntegerType())
417 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
418 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000419
Chris Lattner36d572b2007-07-16 00:14:47 +0000420 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
421 // the following check catches trying to index a pointer to a function (e.g.
422 // void (*)(int)). Functions are not objects in C99.
423 if (!ResultType->isObjectType())
424 return Diag(BaseExpr->getLocStart(),
425 diag::err_typecheck_subscript_not_object,
426 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
427
428 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000429}
430
Steve Narofff8fd09e2007-07-27 22:15:19 +0000431QualType Sema::
432CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
433 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000434 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000435
436 // The vector accessor can't exceed the number of elements.
437 const char *compStr = CompName.getName();
438 if (strlen(compStr) > vecType->getNumElements()) {
439 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
440 baseType.getAsString(), SourceRange(CompLoc));
441 return QualType();
442 }
443 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000444 if (vecType->getPointAccessorIdx(*compStr) != -1) {
445 do
446 compStr++;
447 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
448 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
449 do
450 compStr++;
451 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
452 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
453 do
454 compStr++;
455 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
456 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000457
458 if (*compStr) {
459 // We didn't get to the end of the string. This means the component names
460 // didn't come from the same set *or* we encountered an illegal name.
461 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
462 std::string(compStr,compStr+1), SourceRange(CompLoc));
463 return QualType();
464 }
465 // Each component accessor can't exceed the vector type.
466 compStr = CompName.getName();
467 while (*compStr) {
468 if (vecType->isAccessorWithinNumElements(*compStr))
469 compStr++;
470 else
471 break;
472 }
473 if (*compStr) {
474 // We didn't get to the end of the string. This means a component accessor
475 // exceeds the number of elements in the vector.
476 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
477 baseType.getAsString(), SourceRange(CompLoc));
478 return QualType();
479 }
480 // The component accessor looks fine - now we need to compute the actual type.
481 // The vector type is implied by the component accessor. For example,
482 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
483 unsigned CompSize = strlen(CompName.getName());
484 if (CompSize == 1)
485 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000486
487 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
488 // Now look up the TypeDefDecl from the vector type. Without this,
489 // diagostics look bad. We want OCU vector types to appear built-in.
490 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
491 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
492 return Context.getTypedefType(OCUVectorDecls[i]);
493 }
494 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000495}
496
Chris Lattnere168f762006-11-10 05:29:30 +0000497Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000498ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000499 tok::TokenKind OpKind, SourceLocation MemberLoc,
500 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000501 Expr *BaseExpr = static_cast<Expr *>(Base);
502 assert(BaseExpr && "no record expression");
Steve Naroffeaaae462007-12-16 21:42:28 +0000503
504 // Perform default conversions.
505 DefaultFunctionArrayConversion(BaseExpr);
Steve Naroffca8f7122007-04-01 01:41:35 +0000506
Steve Naroff185616f2007-07-26 03:11:44 +0000507 QualType BaseType = BaseExpr->getType();
508 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000509
Steve Narofff1e53692007-03-23 22:27:02 +0000510 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000511 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000512 BaseType = PT->getPointeeType();
513 else
514 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
515 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000516 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000517 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000518 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000519 RecordDecl *RDecl = RTy->getDecl();
520 if (RTy->isIncompleteType())
521 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
522 BaseExpr->getSourceRange());
523 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000524 FieldDecl *MemberDecl = RDecl->getMember(&Member);
525 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000526 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
527 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000528 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
529 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000530 // Component access limited to variables (reject vec4.rg.g).
531 if (!isa<DeclRefExpr>(BaseExpr))
532 return Diag(OpLoc, diag::err_ocuvector_component_access,
533 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000534 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
535 if (ret.isNull())
536 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000537 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000538 } else if (BaseType->isObjCInterfaceType()) {
539 ObjCInterfaceDecl *IFace;
540 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
541 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000542 else
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000543 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
544 ObjCInterfaceDecl *clsDeclared;
545 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000546 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
547 OpKind==tok::arrow);
548 }
549 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
550 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000551}
552
Steve Naroff83895f72007-09-16 03:34:24 +0000553/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattnere168f762006-11-10 05:29:30 +0000554/// This provides the location of the left/right parens and a list of comma
555/// locations.
556Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000557ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner08464942007-12-28 05:29:59 +0000558 ExprTy **args, unsigned NumArgs,
Chris Lattnere168f762006-11-10 05:29:30 +0000559 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000560 Expr *Fn = static_cast<Expr *>(fn);
561 Expr **Args = reinterpret_cast<Expr**>(args);
562 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000563
Chris Lattner08464942007-12-28 05:29:59 +0000564 // Make the call expr early, before semantic checks. This guarantees cleanup
565 // of arguments and function on error.
566 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
567 Context.BoolTy, RParenLoc));
568
569 // Promote the function operand.
570 TheCall->setCallee(UsualUnaryConversions(Fn));
571
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000572 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
573 // type pointer to function".
Chris Lattner08464942007-12-28 05:29:59 +0000574 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000575 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000576 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
577 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner08464942007-12-28 05:29:59 +0000578 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
579 if (FuncT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000580 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
581 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner08464942007-12-28 05:29:59 +0000582
583 // We know the result type of the call, set it.
584 TheCall->setType(FuncT->getResultType());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000585
Chris Lattner08464942007-12-28 05:29:59 +0000586 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000587 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
588 // assignment, to the types of the corresponding parameter, ...
Chris Lattner08464942007-12-28 05:29:59 +0000589 unsigned NumArgsInProto = Proto->getNumArgs();
590 unsigned NumArgsToCheck = NumArgs;
Steve Naroff17f76e02007-05-03 21:03:48 +0000591
Chris Lattner08464942007-12-28 05:29:59 +0000592 // If too few arguments are available, don't make the call.
593 if (NumArgs < NumArgsInProto)
594 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
595 Fn->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000596
Chris Lattner08464942007-12-28 05:29:59 +0000597 // If too many are passed and not variadic, error on the extras and drop
598 // them.
599 if (NumArgs > NumArgsInProto) {
600 if (!Proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000601 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000602 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000603 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner08464942007-12-28 05:29:59 +0000604 Args[NumArgs-1]->getLocEnd()));
605 // This deletes the extra arguments.
606 TheCall->setNumArgs(NumArgsInProto);
Steve Naroff8563f652007-05-28 19:25:56 +0000607 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000608 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000609 }
Chris Lattner08464942007-12-28 05:29:59 +0000610
Steve Naroff17f76e02007-05-03 21:03:48 +0000611 // Continue to check argument types (even if we have too few/many args).
Chris Lattner08464942007-12-28 05:29:59 +0000612 for (unsigned i = 0; i != NumArgsToCheck; i++) {
613 Expr *Arg = Args[i];
Chris Lattner9bad62c2008-01-04 18:04:52 +0000614 QualType ProtoArgType = Proto->getArgType(i);
615 QualType ArgType = Arg->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000616
Chris Lattner08464942007-12-28 05:29:59 +0000617 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner9bad62c2008-01-04 18:04:52 +0000618 AssignConvertType ConvTy =
619 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner08464942007-12-28 05:29:59 +0000620 TheCall->setArg(i, Arg);
621
Chris Lattner9bad62c2008-01-04 18:04:52 +0000622 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
623 ArgType, Arg, "passing"))
624 return true;
Steve Naroff17f76e02007-05-03 21:03:48 +0000625 }
Chris Lattner08464942007-12-28 05:29:59 +0000626
627 // If this is a variadic call, handle args passed through "...".
628 if (Proto->isVariadic()) {
Steve Naroff0b661582007-08-28 23:30:39 +0000629 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner08464942007-12-28 05:29:59 +0000630 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
631 Expr *Arg = Args[i];
632 DefaultArgumentPromotion(Arg);
633 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000634 }
Steve Naroff0b661582007-08-28 23:30:39 +0000635 }
Chris Lattner08464942007-12-28 05:29:59 +0000636 } else {
637 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
638
Steve Naroff0b661582007-08-28 23:30:39 +0000639 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner08464942007-12-28 05:29:59 +0000640 for (unsigned i = 0; i != NumArgs; i++) {
641 Expr *Arg = Args[i];
642 DefaultArgumentPromotion(Arg);
643 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000644 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000645 }
Chris Lattner08464942007-12-28 05:29:59 +0000646
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000647 // Do special checking on direct calls to functions.
648 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
649 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
650 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattner08464942007-12-28 05:29:59 +0000651 if (CheckFunctionCall(FDecl, TheCall.get()))
Anders Carlsson98f07902007-08-17 05:31:46 +0000652 return true;
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000653
Chris Lattner08464942007-12-28 05:29:59 +0000654 return TheCall.take();
Chris Lattnere168f762006-11-10 05:29:30 +0000655}
656
657Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000658ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000659 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff83895f72007-09-16 03:34:24 +0000660 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Narofffbd09832007-07-19 01:06:55 +0000661 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000662 // FIXME: put back this assert when initializers are worked out.
Steve Naroff83895f72007-09-16 03:34:24 +0000663 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroff57eb2c52007-07-19 21:32:11 +0000664 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson2c1ec6d2007-12-05 07:24:19 +0000665
Steve Naroff91f78082007-12-10 22:44:33 +0000666 // FIXME: add more semantic analysis (C99 6.5.2.5).
667 if (CheckInitializer(literalExpr, literalType, false))
668 return 0;
Anders Carlsson2c1ec6d2007-12-05 07:24:19 +0000669
Chris Lattner266a2ff2008-01-02 21:46:24 +0000670 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000671}
672
673Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000674ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson4692db02007-08-31 04:56:16 +0000675 SourceLocation RBraceLoc) {
Steve Naroff2fea1392007-09-02 02:04:30 +0000676 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson4692db02007-08-31 04:56:16 +0000677
Steve Naroff30d242c2007-09-15 18:49:24 +0000678 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff7d2c5ed2007-09-03 01:24:23 +0000679 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson4692db02007-08-31 04:56:16 +0000680
Steve Naroffb03f5942007-09-02 20:30:18 +0000681 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
682 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
683 return e;
Steve Narofffbd09832007-07-19 01:06:55 +0000684}
685
Chris Lattner6c9ffe92007-12-20 00:44:32 +0000686bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonde71adf2007-11-27 05:51:55 +0000687 assert(VectorTy->isVectorType() && "Not a vector type!");
688
689 if (Ty->isVectorType() || Ty->isIntegerType()) {
690 if (Context.getTypeSize(VectorTy, SourceLocation()) !=
691 Context.getTypeSize(Ty, SourceLocation()))
692 return Diag(R.getBegin(),
693 Ty->isVectorType() ?
694 diag::err_invalid_conversion_between_vectors :
695 diag::err_invalid_conversion_between_vector_and_integer,
696 VectorTy.getAsString().c_str(),
697 Ty.getAsString().c_str(), R);
698 } else
699 return Diag(R.getBegin(),
700 diag::err_invalid_conversion_between_vector_and_scalar,
701 VectorTy.getAsString().c_str(),
702 Ty.getAsString().c_str(), R);
703
704 return false;
705}
706
Steve Narofffbd09832007-07-19 01:06:55 +0000707Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000708ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattnere168f762006-11-10 05:29:30 +0000709 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff83895f72007-09-16 03:34:24 +0000710 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000711
712 Expr *castExpr = static_cast<Expr*>(Op);
713 QualType castType = QualType::getFromOpaquePtr(Ty);
714
Steve Naroff43b8f7f2007-08-31 00:32:44 +0000715 UsualUnaryConversions(castExpr);
716
Chris Lattnerbd270732007-07-18 16:00:06 +0000717 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
718 // type needs to be scalar.
Chris Lattner3bc4d202007-10-29 04:26:44 +0000719 if (!castType->isVoidType()) { // Cast to void allows any expr type.
720 if (!castType->isScalarType())
721 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
722 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Anders Carlssonde71adf2007-11-27 05:51:55 +0000723 if (!castExpr->getType()->isScalarType())
Chris Lattner3bc4d202007-10-29 04:26:44 +0000724 return Diag(castExpr->getLocStart(),
725 diag::err_typecheck_expect_scalar_operand,
726 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonde71adf2007-11-27 05:51:55 +0000727
728 if (castExpr->getType()->isVectorType()) {
729 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
730 castExpr->getType(), castType))
731 return true;
732 } else if (castType->isVectorType()) {
733 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
734 castType, castExpr->getType()))
735 return true;
Chris Lattner3bc4d202007-10-29 04:26:44 +0000736 }
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000737 }
738 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000739}
740
Steve Naroffa78c4642007-10-18 05:13:08 +0000741// promoteExprToType - a helper function to ensure we create exactly one
742// ImplicitCastExpr.
743static void promoteExprToType(Expr *&expr, QualType type) {
744 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
745 impCast->setType(type);
746 else
747 expr = new ImplicitCastExpr(type, expr);
748 return;
749}
750
Chris Lattner2ab40a62007-11-26 01:40:58 +0000751/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
752/// In that case, lex = cond.
Steve Narofff8a28c52007-05-15 20:29:32 +0000753inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000754 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000755 UsualUnaryConversions(cond);
756 UsualUnaryConversions(lex);
757 UsualUnaryConversions(rex);
758 QualType condT = cond->getType();
759 QualType lexT = lex->getType();
760 QualType rexT = rex->getType();
761
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000762 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000763 if (!condT->isScalarType()) { // C99 6.5.15p2
764 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
765 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000766 return QualType();
767 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000768
769 // Now check the two expressions.
770
771 // If both operands have arithmetic type, do the usual arithmetic conversions
772 // to find a common type: C99 6.5.15p3,5.
773 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Steve Naroffdbd9e892007-07-17 00:58:39 +0000774 UsualArithmeticConversions(lex, rex);
775 return lex->getType();
776 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000777
778 // If both operands are the same structure or union type, the result is that
779 // type.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000780 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattnere2949f42008-01-06 22:42:25 +0000781 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner2ab40a62007-11-26 01:40:58 +0000782 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattnere2949f42008-01-06 22:42:25 +0000783 // "If both the operands have structure or union type, the result has
784 // that type." This implies that CV qualifiers are dropped.
785 return lexT.getUnqualifiedType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000786 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000787
788 // C99 6.5.15p5: "If both operands have void type, the result has void type."
789 if (lexT->isVoidType() && rexT->isVoidType())
790 return lexT.getUnqualifiedType();
Steve Naroff039ad3c2008-01-08 01:11:38 +0000791
792 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
793 // the type of the other operand."
794 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
795 promoteExprToType(rex, lexT); // promote the null to a pointer.
796 return lexT;
797 }
798 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
799 promoteExprToType(lex, rexT); // promote the null to a pointer.
800 return rexT;
801 }
Chris Lattner4d3b0f52008-01-06 22:50:31 +0000802 // Handle the case where both operands are pointers before we handle null
803 // pointer constants in case both operands are null pointer constants.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000804 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
805 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
806 // get the "pointed to" types
807 QualType lhptee = LHSPT->getPointeeType();
808 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000809
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000810 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
811 if (lhptee->isVoidType() &&
812 (rhptee->isObjectType() || rhptee->isIncompleteType()))
813 return lexT;
814 if (rhptee->isVoidType() &&
815 (lhptee->isObjectType() || lhptee->isIncompleteType()))
816 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000817
Steve Naroff32e44c02007-10-15 20:41:53 +0000818 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
819 rhptee.getUnqualifiedType())) {
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000820 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
821 lexT.getAsString(), rexT.getAsString(),
822 lex->getSourceRange(), rex->getSourceRange());
823 return lexT; // FIXME: this is an _ext - is this return o.k?
824 }
825 // The pointer types are compatible.
Chris Lattnerf17bd422007-08-30 17:45:32 +0000826 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
827 // differently qualified versions of compatible types, the result type is
828 // a pointer to an appropriately qualified version of the *composite*
829 // type.
Chris Lattner4d3b0f52008-01-06 22:50:31 +0000830 // FIXME: Need to return the composite type.
831 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000832 }
833 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000834
Chris Lattnere2949f42008-01-06 22:42:25 +0000835 // Otherwise, the operands are not compatible.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000836 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000837 lexT.getAsString(), rexT.getAsString(),
838 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000839 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000840}
841
Steve Naroff83895f72007-09-16 03:34:24 +0000842/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattnere168f762006-11-10 05:29:30 +0000843/// in the case of a the GNU conditional expr extension.
Steve Naroff83895f72007-09-16 03:34:24 +0000844Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000845 SourceLocation ColonLoc,
846 ExprTy *Cond, ExprTy *LHS,
847 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000848 Expr *CondExpr = (Expr *) Cond;
849 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000850
851 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
852 // was the condition.
853 bool isLHSNull = LHSExpr == 0;
854 if (isLHSNull)
855 LHSExpr = CondExpr;
856
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000857 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
858 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000859 if (result.isNull())
860 return true;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000861 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
862 RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000863}
864
Steve Naroff0b661582007-08-28 23:30:39 +0000865/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
866/// do not have a prototype. Integer promotions are performed on each
867/// argument, and arguments that have type float are promoted to double.
Chris Lattner08464942007-12-28 05:29:59 +0000868void Sema::DefaultArgumentPromotion(Expr *&Expr) {
869 QualType Ty = Expr->getType();
870 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroff0b661582007-08-28 23:30:39 +0000871
Chris Lattner08464942007-12-28 05:29:59 +0000872 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
873 promoteExprToType(Expr, Context.IntTy);
874 if (Ty == Context.FloatTy)
875 promoteExprToType(Expr, Context.DoubleTy);
Steve Naroff0b661582007-08-28 23:30:39 +0000876}
877
Steve Naroff81569d22007-07-15 02:02:06 +0000878/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000879void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000880 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000881 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000882
Chris Lattnercd1d0862007-07-31 16:56:34 +0000883 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000884 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
885 t = e->getType();
886 }
Steve Naroff81569d22007-07-15 02:02:06 +0000887 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000888 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000889 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000890 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000891}
892
Steve Naroff71b59a92007-06-04 22:22:31 +0000893/// UsualUnaryConversion - Performs various conversions that are common to most
894/// operators (C99 6.3). The conversions of array and function types are
895/// sometimes surpressed. For example, the array->pointer conversion doesn't
896/// apply if the array is an argument to the sizeof or address (&) operators.
897/// In these instances, this routine should *not* be called.
Chris Lattner08464942007-12-28 05:29:59 +0000898Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
899 QualType Ty = Expr->getType();
900 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Steve Naroff71b59a92007-06-04 22:22:31 +0000901
Chris Lattner08464942007-12-28 05:29:59 +0000902 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
903 promoteExprToType(Expr, Ref->getReferenceeType()); // C++ [expr]
904 Ty = Expr->getType();
Bill Wendling354fb262007-07-17 04:16:47 +0000905 }
Chris Lattner08464942007-12-28 05:29:59 +0000906 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
907 promoteExprToType(Expr, Context.IntTy);
Steve Naroff31090012007-07-16 21:54:35 +0000908 else
Chris Lattner08464942007-12-28 05:29:59 +0000909 DefaultFunctionArrayConversion(Expr);
910
911 return Expr;
Steve Naroff71b59a92007-06-04 22:22:31 +0000912}
913
Chris Lattnerf17bd422007-08-30 17:45:32 +0000914/// UsualArithmeticConversions - Performs various conversions that are common to
Steve Naroff1926c832007-04-24 00:23:05 +0000915/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
916/// routine returns the first non-arithmetic type found. The client is
917/// responsible for emitting appropriate error diagnostics.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000918QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
919 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +0000920 if (!isCompAssign) {
921 UsualUnaryConversions(lhsExpr);
922 UsualUnaryConversions(rhsExpr);
923 }
Steve Naroff1bb21df2007-10-18 18:55:53 +0000924 // For conversion purposes, we ignore any qualifiers.
925 // For example, "const float" and "float" are equivalent.
Steve Naroff257b4a22007-11-10 19:45:54 +0000926 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
927 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Steve Naroff1926c832007-04-24 00:23:05 +0000928
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000929 // If both types are identical, no conversion is needed.
Steve Naroff1bb21df2007-10-18 18:55:53 +0000930 if (lhs == rhs)
931 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000932
933 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
934 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000935 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000936 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000937
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000938 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000939
940 // Handle complex types first (C99 6.3.1.8p1).
941 if (lhs->isComplexType() || rhs->isComplexType()) {
942 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000943 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000944 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
945 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000946 }
947 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000948 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
949 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000950 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000951 // This handles complex/complex, complex/float, or float/complex.
952 // When both operands are complex, the shorter operand is converted to the
953 // type of the longer, and that is the type of the result. This corresponds
954 // to what is done when combining two real floating-point operands.
955 // The fun begins when size promotion occur across type domains.
956 // From H&S 6.3.4: When one operand is complex and the other is a real
957 // floating-point type, the less precise type is converted, within it's
958 // real or complex domain, to the precision of the other type. For example,
959 // when combining a "long double" with a "double _Complex", the
960 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff7af82d42007-08-27 15:30:22 +0000961 int result = Context.compareFloatingType(lhs, rhs);
962
963 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroffe31313d2007-08-27 21:32:55 +0000964 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
965 if (!isCompAssign)
966 promoteExprToType(rhsExpr, rhs);
967 } else if (result < 0) { // The right side is bigger, convert lhs.
968 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
969 if (!isCompAssign)
970 promoteExprToType(lhsExpr, lhs);
971 }
972 // At this point, lhs and rhs have the same rank/size. Now, make sure the
973 // domains match. This is a requirement for our implementation, C99
974 // does not require this promotion.
975 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
976 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroffa042db22007-08-27 21:43:43 +0000977 if (!isCompAssign)
978 promoteExprToType(lhsExpr, rhs);
979 return rhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000980 } else { // handle "_Complex double, double".
Steve Naroffa042db22007-08-27 21:43:43 +0000981 if (!isCompAssign)
982 promoteExprToType(rhsExpr, lhs);
983 return lhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000984 }
Steve Naroffdbd9e892007-07-17 00:58:39 +0000985 }
Steve Naroffa042db22007-08-27 21:43:43 +0000986 return lhs; // The domain/size match exactly.
Steve Naroffbf223ba2007-04-24 20:56:26 +0000987 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000988 // Now handle "real" floating types (i.e. float, double, long double).
989 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
990 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000991 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000992 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
993 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000994 }
995 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000996 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
997 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000998 }
Steve Naroff81569d22007-07-15 02:02:06 +0000999 // We have two real floating types, float/complex combos were handled above.
1000 // Convert the smaller operand to the bigger result.
Steve Naroff7af82d42007-08-27 15:30:22 +00001001 int result = Context.compareFloatingType(lhs, rhs);
1002
1003 if (result > 0) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001004 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1005 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001006 }
Steve Naroff7af82d42007-08-27 15:30:22 +00001007 if (result < 0) { // convert the lhs
1008 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
1009 return rhs;
1010 }
1011 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +00001012 }
Steve Naroff81569d22007-07-15 02:02:06 +00001013 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +00001014 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001015 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1016 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001017 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001018 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
1019 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +00001020}
1021
Steve Naroff3f597292007-05-11 22:18:03 +00001022// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1023// being closely modeled after the C99 spec:-). The odd characteristic of this
1024// routine is it effectively iqnores the qualifiers on the top level pointee.
1025// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1026// FIXME: add a couple examples in this comment.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001027Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001028Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +00001029 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001030
1031 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +00001032 lhptee = lhsType->getAsPointerType()->getPointeeType();
1033 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001034
1035 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +00001036 lhptee = lhptee.getCanonicalType();
1037 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001038
Chris Lattner9bad62c2008-01-04 18:04:52 +00001039 AssignConvertType ConvTy = Compatible;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001040
Steve Naroff3f597292007-05-11 22:18:03 +00001041 // C99 6.5.16.1p1: This following citation is common to constraints
1042 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1043 // qualifiers of the type *pointed to* by the right;
1044 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
1045 rhptee.getQualifiers())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001046 ConvTy = CompatiblePointerDiscardsQualifiers;
Steve Naroff3f597292007-05-11 22:18:03 +00001047
1048 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1049 // incomplete type and the other is a pointer to a qualified or unqualified
1050 // version of void...
Chris Lattner0a788432008-01-03 22:56:36 +00001051 if (lhptee->isVoidType()) {
1052 if (rhptee->isObjectType() || rhptee->isIncompleteType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001053 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001054
1055 // As an extension, we allow cast to/from void* to function pointer.
1056 if (rhptee->isFunctionType())
1057 return FunctionVoidPointer;
1058 }
1059
1060 if (rhptee->isVoidType()) {
1061 if (lhptee->isObjectType() || lhptee->isIncompleteType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001062 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001063
1064 // As an extension, we allow cast to/from void* to function pointer.
1065 if (lhptee->isFunctionType())
1066 return FunctionVoidPointer;
1067 }
1068
Steve Naroff3f597292007-05-11 22:18:03 +00001069 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1070 // unqualified versions of compatible types, ...
Chris Lattner0a788432008-01-03 22:56:36 +00001071 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1072 rhptee.getUnqualifiedType()))
1073 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner9bad62c2008-01-04 18:04:52 +00001074 return ConvTy;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001075}
1076
Steve Naroff98cf3e92007-06-06 18:38:38 +00001077/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +00001078/// has code to accommodate several GCC extensions when type checking
1079/// pointers. Here are some objectionable examples that GCC considers warnings:
1080///
1081/// int a, *pint;
1082/// short *pshort;
1083/// struct foo *pfoo;
1084///
1085/// pint = pshort; // warning: assignment from incompatible pointer type
1086/// a = pint; // warning: assignment makes integer from pointer without a cast
1087/// pint = a; // warning: assignment makes pointer from integer without a cast
1088/// pint = pfoo; // warning: assignment from incompatible pointer type
1089///
1090/// As a result, the code for dealing with pointers is more complex than the
1091/// C99 spec dictates.
1092/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1093///
Chris Lattner9bad62c2008-01-04 18:04:52 +00001094Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001095Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattnera52c2f22008-01-04 23:18:45 +00001096 // Get canonical types. We're not formatting these types, just comparing
1097 // them.
1098 lhsType = lhsType.getCanonicalType();
1099 rhsType = rhsType.getCanonicalType();
1100
1101 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerf5c973d2008-01-07 17:51:46 +00001102 return Compatible; // Common case: fast path an exact match.
Steve Naroff44fd8ff2007-07-24 21:46:40 +00001103
Anders Carlsson24ebce62007-10-12 23:56:29 +00001104 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001105 if (Context.referenceTypesAreCompatible(lhsType, rhsType))
Anders Carlsson24ebce62007-10-12 23:56:29 +00001106 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001107 return Incompatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001108 }
Chris Lattnera52c2f22008-01-04 23:18:45 +00001109
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001110 if (lhsType->isObjCQualifiedIdType()
1111 || rhsType->isObjCQualifiedIdType()) {
1112 if (Context.ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType))
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001113 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001114 return Incompatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001115 }
Chris Lattner881a2122008-01-04 23:32:24 +00001116
1117 if (lhsType->isVectorType() || rhsType->isVectorType()) {
1118 // For OCUVector, allow vector splats; float -> <n x float>
1119 if (const OCUVectorType *LV = lhsType->getAsOCUVectorType()) {
1120 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1121 return Compatible;
1122 }
1123
1124 // If LHS and RHS are both vectors of integer or both vectors of floating
1125 // point types, and the total vector length is the same, allow the
1126 // conversion. This is a bitcast; no bits are changed but the result type
1127 // is different.
1128 if (getLangOptions().LaxVectorConversions &&
1129 lhsType->isVectorType() && rhsType->isVectorType()) {
1130 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1131 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
1132 if (Context.getTypeSize(lhsType, SourceLocation()) ==
1133 Context.getTypeSize(rhsType, SourceLocation()))
Nate Begeman330aaa72007-12-30 02:59:45 +00001134 return Compatible;
1135 }
Chris Lattner881a2122008-01-04 23:32:24 +00001136 }
1137 return Incompatible;
1138 }
1139
1140 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001141 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001142
1143 if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001144 if (rhsType->isIntegerType())
Chris Lattner940cfeb2008-01-04 18:22:42 +00001145 return IntToPointer;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001146
Steve Naroff3f597292007-05-11 22:18:03 +00001147 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001148 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnera52c2f22008-01-04 23:18:45 +00001149 return Incompatible;
1150 }
1151
1152 if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001153 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1154 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
Chris Lattner940cfeb2008-01-04 18:22:42 +00001155 return PointerToInt;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001156
Steve Naroff3f597292007-05-11 22:18:03 +00001157 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001158 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnera52c2f22008-01-04 23:18:45 +00001159 return Incompatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001160 }
1161
1162 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001163 if (Context.tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001164 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +00001165 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001166 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +00001167}
1168
Chris Lattner9bad62c2008-01-04 18:04:52 +00001169Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001170Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001171 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1172 // a null pointer constant.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001173 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahanian5cc21a72008-01-03 18:46:52 +00001174 && rExpr->isNullPointerConstant(Context)) {
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001175 promoteExprToType(rExpr, lhsType);
1176 return Compatible;
1177 }
Chris Lattnere6dcd502007-10-16 02:55:40 +00001178 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001179 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff30d242c2007-09-15 18:49:24 +00001180 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001181 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattnere6dcd502007-10-16 02:55:40 +00001182 //
1183 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1184 // are better understood.
1185 if (!lhsType->isReferenceType())
1186 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001187
Chris Lattner9bad62c2008-01-04 18:04:52 +00001188 Sema::AssignConvertType result =
1189 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001190
1191 // C99 6.5.16.1p2: The value of the right operand is converted to the
1192 // type of the assignment expression.
1193 if (rExpr->getType() != lhsType)
1194 promoteExprToType(rExpr, lhsType);
1195 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001196}
1197
Chris Lattner9bad62c2008-01-04 18:04:52 +00001198Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001199Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1200 return CheckAssignmentConstraints(lhsType, rhsType);
1201}
1202
Chris Lattner5c11c412007-12-12 05:47:28 +00001203QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001204 Diag(loc, diag::err_typecheck_invalid_operands,
1205 lex->getType().getAsString(), rex->getType().getAsString(),
1206 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner5c11c412007-12-12 05:47:28 +00001207 return QualType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001208}
1209
Steve Naroff7a5af782007-07-13 16:58:59 +00001210inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1211 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +00001212 QualType lhsType = lex->getType(), rhsType = rex->getType();
1213
1214 // make sure the vector types are identical.
1215 if (lhsType == rhsType)
1216 return lhsType;
Nate Begeman330aaa72007-12-30 02:59:45 +00001217
1218 // if the lhs is an ocu vector and the rhs is a scalar of the same type,
1219 // promote the rhs to the vector type.
1220 if (const OCUVectorType *V = lhsType->getAsOCUVectorType()) {
1221 if (V->getElementType().getCanonicalType().getTypePtr()
1222 == rhsType.getCanonicalType().getTypePtr()) {
1223 promoteExprToType(rex, lhsType);
1224 return lhsType;
1225 }
1226 }
1227
1228 // if the rhs is an ocu vector and the lhs is a scalar of the same type,
1229 // promote the lhs to the vector type.
1230 if (const OCUVectorType *V = rhsType->getAsOCUVectorType()) {
1231 if (V->getElementType().getCanonicalType().getTypePtr()
1232 == lhsType.getCanonicalType().getTypePtr()) {
1233 promoteExprToType(lex, rhsType);
1234 return rhsType;
1235 }
1236 }
1237
Steve Naroff84ff4b42007-07-09 21:31:10 +00001238 // You cannot convert between vector values of different size.
1239 Diag(loc, diag::err_typecheck_vector_not_convertable,
1240 lex->getType().getAsString(), rex->getType().getAsString(),
1241 lex->getSourceRange(), rex->getSourceRange());
1242 return QualType();
1243}
1244
Steve Naroff218bc2b2007-05-04 21:54:46 +00001245inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001246 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001247{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001248 QualType lhsType = lex->getType(), rhsType = rex->getType();
1249
1250 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001251 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001252
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001253 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001254
Steve Naroffdbd9e892007-07-17 00:58:39 +00001255 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001256 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001257 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001258}
1259
Steve Naroff218bc2b2007-05-04 21:54:46 +00001260inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001261 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001262{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001263 QualType lhsType = lex->getType(), rhsType = rex->getType();
1264
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001265 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001266
Steve Naroffdbd9e892007-07-17 00:58:39 +00001267 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001268 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001269 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001270}
1271
1272inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001273 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001274{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001275 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001276 return CheckVectorOperands(loc, lex, rex);
1277
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001278 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff94a5aca2007-07-16 22:23:01 +00001279
Steve Naroffe4718892007-04-27 18:30:00 +00001280 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001281 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001282 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001283
Steve Naroffdbd9e892007-07-17 00:58:39 +00001284 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1285 return lex->getType();
1286 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1287 return rex->getType();
Chris Lattner5c11c412007-12-12 05:47:28 +00001288 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001289}
1290
Steve Naroff218bc2b2007-05-04 21:54:46 +00001291inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001292 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001293{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001294 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001295 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001296
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001297 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001298
Chris Lattner4d62f422007-12-09 21:53:25 +00001299 // Enforce type constraints: C99 6.5.6p3.
1300
1301 // Handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001302 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001303 return compType;
Chris Lattner4d62f422007-12-09 21:53:25 +00001304
1305 // Either ptr - int or ptr - ptr.
1306 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
1307 // The LHS must be an object type, not incomplete, function, etc.
1308 if (!LHSPTy->getPointeeType()->isObjectType()) {
1309 // Handle the GNU void* extension.
1310 if (LHSPTy->getPointeeType()->isVoidType()) {
1311 Diag(loc, diag::ext_gnu_void_ptr,
1312 lex->getSourceRange(), rex->getSourceRange());
1313 } else {
1314 Diag(loc, diag::err_typecheck_sub_ptr_object,
1315 lex->getType().getAsString(), lex->getSourceRange());
1316 return QualType();
1317 }
1318 }
1319
1320 // The result type of a pointer-int computation is the pointer type.
1321 if (rex->getType()->isIntegerType())
1322 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +00001323
Chris Lattner4d62f422007-12-09 21:53:25 +00001324 // Handle pointer-pointer subtractions.
1325 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
1326 // RHS must be an object type, unless void (GNU).
1327 if (!RHSPTy->getPointeeType()->isObjectType()) {
1328 // Handle the GNU void* extension.
1329 if (RHSPTy->getPointeeType()->isVoidType()) {
1330 if (!LHSPTy->getPointeeType()->isVoidType())
1331 Diag(loc, diag::ext_gnu_void_ptr,
1332 lex->getSourceRange(), rex->getSourceRange());
1333 } else {
1334 Diag(loc, diag::err_typecheck_sub_ptr_object,
1335 rex->getType().getAsString(), rex->getSourceRange());
1336 return QualType();
1337 }
1338 }
1339
1340 // Pointee types must be compatible.
1341 if (!Context.typesAreCompatible(LHSPTy->getPointeeType(),
1342 RHSPTy->getPointeeType())) {
1343 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1344 lex->getType().getAsString(), rex->getType().getAsString(),
1345 lex->getSourceRange(), rex->getSourceRange());
1346 return QualType();
1347 }
1348
1349 return Context.getPointerDiffType();
1350 }
1351 }
1352
Chris Lattner5c11c412007-12-12 05:47:28 +00001353 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001354}
1355
1356inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Chris Lattner5c11c412007-12-12 05:47:28 +00001357 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) {
1358 // C99 6.5.7p2: Each of the operands shall have integer type.
1359 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1360 return InvalidOperands(loc, lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001361
Chris Lattner5c11c412007-12-12 05:47:28 +00001362 // Shifts don't perform usual arithmetic conversions, they just do integer
1363 // promotions on each operand. C99 6.5.7p3
Chris Lattner3c133402007-12-13 07:28:16 +00001364 if (!isCompAssign)
1365 UsualUnaryConversions(lex);
Chris Lattner5c11c412007-12-12 05:47:28 +00001366 UsualUnaryConversions(rex);
1367
1368 // "The type of the result is that of the promoted left operand."
1369 return lex->getType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001370}
1371
Chris Lattnerb620c342007-08-26 01:18:55 +00001372inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1373 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Steve Naroff1926c832007-04-24 00:23:05 +00001374{
Chris Lattnerb620c342007-08-26 01:18:55 +00001375 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001376 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1377 UsualArithmeticConversions(lex, rex);
1378 else {
1379 UsualUnaryConversions(lex);
1380 UsualUnaryConversions(rex);
1381 }
Steve Naroff31090012007-07-16 21:54:35 +00001382 QualType lType = lex->getType();
1383 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001384
Ted Kremeneke2763b02007-10-29 17:13:39 +00001385 // For non-floating point types, check for self-comparisons of the form
1386 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1387 // often indicate logic errors in the program.
Ted Kremeneke451eae2007-10-29 16:58:49 +00001388 if (!lType->isFloatingType()) {
1389 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(IgnoreParen(lex)))
1390 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(IgnoreParen(rex)))
1391 if (DRL->getDecl() == DRR->getDecl())
1392 Diag(loc, diag::warn_selfcomparison);
1393 }
1394
Chris Lattnerb620c342007-08-26 01:18:55 +00001395 if (isRelational) {
1396 if (lType->isRealType() && rType->isRealType())
1397 return Context.IntTy;
1398 } else {
Ted Kremeneke2763b02007-10-29 17:13:39 +00001399 // Check for comparisons of floating point operands using != and ==.
Ted Kremeneke2763b02007-10-29 17:13:39 +00001400 if (lType->isFloatingType()) {
1401 assert (rType->isFloatingType());
Ted Kremenek43fb8b02007-11-25 00:58:00 +00001402 CheckFloatComparison(loc,lex,rex);
Ted Kremenekd4ecc6d2007-10-29 16:40:01 +00001403 }
1404
Chris Lattnerb620c342007-08-26 01:18:55 +00001405 if (lType->isArithmeticType() && rType->isArithmeticType())
1406 return Context.IntTy;
1407 }
Steve Naroffe4718892007-04-27 18:30:00 +00001408
Chris Lattner1895e582007-08-26 01:10:14 +00001409 bool LHSIsNull = lex->isNullPointerConstant(Context);
1410 bool RHSIsNull = rex->isNullPointerConstant(Context);
1411
Chris Lattnerb620c342007-08-26 01:18:55 +00001412 // All of the following pointer related warnings are GCC extensions, except
1413 // when handling null pointer constants. One day, we can consider making them
1414 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001415 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Steve Naroffb6666252007-11-13 14:57:38 +00001416
1417 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
1418 !lType->getAsPointerType()->getPointeeType()->isVoidType() &&
1419 !rType->getAsPointerType()->getPointeeType()->isVoidType() &&
Steve Naroff32e44c02007-10-15 20:41:53 +00001420 !Context.pointerTypesAreCompatible(lType.getUnqualifiedType(),
1421 rType.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001422 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1423 lType.getAsString(), rType.getAsString(),
1424 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001425 }
Chris Lattner1895e582007-08-26 01:10:14 +00001426 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001427 return Context.IntTy;
1428 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001429 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
1430 && Context.ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Fariborz Jahanian134cbef2007-12-20 01:06:58 +00001431 promoteExprToType(rex, lType);
1432 return Context.IntTy;
1433 }
Steve Naroffcdee44c2007-08-16 21:48:38 +00001434 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001435 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001436 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1437 lType.getAsString(), rType.getAsString(),
1438 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001439 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001440 return Context.IntTy;
1441 }
1442 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001443 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001444 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1445 lType.getAsString(), rType.getAsString(),
1446 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001447 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001448 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001449 }
Chris Lattner5c11c412007-12-12 05:47:28 +00001450 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001451}
1452
Steve Naroff218bc2b2007-05-04 21:54:46 +00001453inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001454 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001455{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001456 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001457 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001458
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001459 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001460
Steve Naroffdbd9e892007-07-17 00:58:39 +00001461 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001462 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001463 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001464}
1465
Steve Naroff218bc2b2007-05-04 21:54:46 +00001466inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001467 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001468{
Steve Naroff31090012007-07-16 21:54:35 +00001469 UsualUnaryConversions(lex);
1470 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001471
Steve Naroffdbd9e892007-07-17 00:58:39 +00001472 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001473 return Context.IntTy;
Chris Lattner5c11c412007-12-12 05:47:28 +00001474 return InvalidOperands(loc, lex, rex);
Steve Naroffae4143e2007-04-26 20:39:23 +00001475}
1476
Steve Naroff35d85152007-05-07 00:24:15 +00001477inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001478 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001479{
Steve Naroff0af91202007-04-27 21:51:21 +00001480 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001481 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff9358c712007-05-27 23:58:33 +00001482 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1483
1484 switch (mlval) { // C99 6.5.16p2
Chris Lattner9bad62c2008-01-04 18:04:52 +00001485 case Expr::MLV_Valid:
1486 break;
1487 case Expr::MLV_ConstQualified:
1488 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1489 return QualType();
1490 case Expr::MLV_ArrayType:
1491 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1492 lhsType.getAsString(), lex->getSourceRange());
1493 return QualType();
1494 case Expr::MLV_NotObjectType:
1495 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1496 lhsType.getAsString(), lex->getSourceRange());
1497 return QualType();
1498 case Expr::MLV_InvalidExpression:
1499 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1500 lex->getSourceRange());
1501 return QualType();
1502 case Expr::MLV_IncompleteType:
1503 case Expr::MLV_IncompleteVoidType:
1504 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1505 lhsType.getAsString(), lex->getSourceRange());
1506 return QualType();
1507 case Expr::MLV_DuplicateVectorComponents:
1508 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1509 lex->getSourceRange());
1510 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001511 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001512
Chris Lattner9bad62c2008-01-04 18:04:52 +00001513 AssignConvertType ConvTy;
1514 if (compoundType.isNull())
1515 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1516 else
1517 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1518
1519 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1520 rex, "assigning"))
1521 return QualType();
1522
Steve Naroff98cf3e92007-06-06 18:38:38 +00001523 // C99 6.5.16p3: The type of an assignment expression is the type of the
1524 // left operand unless the left operand has qualified type, in which case
1525 // it is the unqualified version of the type of the left operand.
1526 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1527 // is converted to the type of the assignment expression (above).
Chris Lattnerf17bd422007-08-30 17:45:32 +00001528 // C++ 5.17p1: the type of the assignment expression is that of its left
1529 // oprdu.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001530 return lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001531}
1532
Steve Naroff218bc2b2007-05-04 21:54:46 +00001533inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001534 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001535 UsualUnaryConversions(rex);
1536 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001537}
1538
Steve Naroff7a5af782007-07-13 16:58:59 +00001539/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1540/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001541QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001542 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001543 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001544
Steve Naroff9d139172007-08-24 17:20:07 +00001545 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff5f9ae642007-11-11 14:15:57 +00001546 if (const PointerType *pt = resType->getAsPointerType()) {
Steve Naroff35d85152007-05-07 00:24:15 +00001547 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001548 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1549 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001550 return QualType();
1551 }
Steve Naroff9d139172007-08-24 17:20:07 +00001552 } else if (!resType->isRealType()) {
1553 if (resType->isComplexType())
1554 // C99 does not support ++/-- on complex types.
1555 Diag(OpLoc, diag::ext_integer_increment_complex,
1556 resType.getAsString(), op->getSourceRange());
1557 else {
1558 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1559 resType.getAsString(), op->getSourceRange());
1560 return QualType();
1561 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001562 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001563 // At this point, we know we have a real, complex or pointer type.
1564 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001565 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1566 if (mlval != Expr::MLV_Valid) {
1567 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001568 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001569 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001570 return QualType();
1571 }
1572 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001573}
1574
Steve Naroff1926c832007-04-24 00:23:05 +00001575/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001576/// This routine allows us to typecheck complex/recursive expressions
1577/// where the declaration is needed for type checking. Here are some
1578/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001579static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001580 switch (e->getStmtClass()) {
1581 case Stmt::DeclRefExprClass:
1582 return cast<DeclRefExpr>(e)->getDecl();
1583 case Stmt::MemberExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001584 // Fields cannot be declared with a 'register' storage class.
1585 // &X->f is always ok, even if X is declared register.
1586 if (cast<MemberExpr>(e)->isArrow())
1587 return 0;
Steve Naroff1926c832007-04-24 00:23:05 +00001588 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001589 case Stmt::ArraySubscriptExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001590 // &X[4] and &4[X] is invalid if X is invalid.
Steve Naroff1926c832007-04-24 00:23:05 +00001591 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001592 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001593 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001594 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001595 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Chris Lattner48d52842007-11-16 17:46:48 +00001596 case Stmt::ImplicitCastExprClass:
1597 // &X[4] when X is an array, has an implicit cast from array to pointer.
1598 return getPrimaryDeclaration(cast<ImplicitCastExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001599 default:
1600 return 0;
1601 }
1602}
1603
1604/// CheckAddressOfOperand - The operand of & must be either a function
1605/// designator or an lvalue designating an object. If it is an lvalue, the
1606/// object cannot be declared with storage class register or be a bit field.
1607/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001608/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001609QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001610 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001611 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001612
Steve Naroff9358c712007-05-27 23:58:33 +00001613 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattner48d52842007-11-16 17:46:48 +00001614 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1615 // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001616 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1617 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001618 return QualType();
1619 }
Steve Naroff47500512007-04-19 23:00:49 +00001620 } else if (dcl) {
1621 // We have an lvalue with a decl. Make sure the decl is not declared
1622 // with the register storage-class specifier.
1623 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001624 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001625 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001626 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001627 return QualType();
1628 }
Steve Narofff633d092007-04-25 19:01:39 +00001629 } else
1630 assert(0 && "Unknown/unexpected decl type");
1631
Steve Naroff47500512007-04-19 23:00:49 +00001632 // FIXME: add check for bitfields!
1633 }
1634 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001635 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001636}
1637
Steve Naroff35d85152007-05-07 00:24:15 +00001638QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001639 UsualUnaryConversions(op);
1640 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001641
Chris Lattnerc996b172007-07-31 16:53:04 +00001642 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001643 QualType ptype = PT->getPointeeType();
1644 // C99 6.5.3.2p4. "if it points to an object,...".
1645 if (ptype->isIncompleteType()) { // An incomplete type is not an object
Chris Lattnercfb6f432008-01-06 22:21:46 +00001646 // GCC compat: special case 'void *' (treat as extension, not error).
Steve Naroff758ada12007-05-28 16:15:57 +00001647 if (ptype->isVoidType()) {
Chris Lattnercfb6f432008-01-06 22:21:46 +00001648 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001649 } else {
1650 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001651 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001652 return QualType();
1653 }
1654 }
1655 return ptype;
1656 }
1657 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001658 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001659 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001660}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001661
1662static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1663 tok::TokenKind Kind) {
1664 BinaryOperator::Opcode Opc;
1665 switch (Kind) {
1666 default: assert(0 && "Unknown binop!");
1667 case tok::star: Opc = BinaryOperator::Mul; break;
1668 case tok::slash: Opc = BinaryOperator::Div; break;
1669 case tok::percent: Opc = BinaryOperator::Rem; break;
1670 case tok::plus: Opc = BinaryOperator::Add; break;
1671 case tok::minus: Opc = BinaryOperator::Sub; break;
1672 case tok::lessless: Opc = BinaryOperator::Shl; break;
1673 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1674 case tok::lessequal: Opc = BinaryOperator::LE; break;
1675 case tok::less: Opc = BinaryOperator::LT; break;
1676 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1677 case tok::greater: Opc = BinaryOperator::GT; break;
1678 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1679 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1680 case tok::amp: Opc = BinaryOperator::And; break;
1681 case tok::caret: Opc = BinaryOperator::Xor; break;
1682 case tok::pipe: Opc = BinaryOperator::Or; break;
1683 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1684 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1685 case tok::equal: Opc = BinaryOperator::Assign; break;
1686 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1687 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1688 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1689 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1690 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1691 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1692 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1693 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1694 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1695 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1696 case tok::comma: Opc = BinaryOperator::Comma; break;
1697 }
1698 return Opc;
1699}
1700
Steve Naroff35d85152007-05-07 00:24:15 +00001701static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1702 tok::TokenKind Kind) {
1703 UnaryOperator::Opcode Opc;
1704 switch (Kind) {
1705 default: assert(0 && "Unknown unary op!");
1706 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1707 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1708 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1709 case tok::star: Opc = UnaryOperator::Deref; break;
1710 case tok::plus: Opc = UnaryOperator::Plus; break;
1711 case tok::minus: Opc = UnaryOperator::Minus; break;
1712 case tok::tilde: Opc = UnaryOperator::Not; break;
1713 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1714 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1715 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1716 case tok::kw___real: Opc = UnaryOperator::Real; break;
1717 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001718 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001719 }
1720 return Opc;
1721}
1722
Steve Naroff218bc2b2007-05-04 21:54:46 +00001723// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001724Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Steve Naroff218bc2b2007-05-04 21:54:46 +00001725 ExprTy *LHS, ExprTy *RHS) {
1726 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1727 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1728
Steve Naroff83895f72007-09-16 03:34:24 +00001729 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1730 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Steve Naroff218bc2b2007-05-04 21:54:46 +00001731
Chris Lattner256c21b2007-06-28 03:53:10 +00001732 QualType ResultTy; // Result type of the binary operator.
1733 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001734
1735 switch (Opc) {
1736 default:
1737 assert(0 && "Unknown binary expr!");
1738 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001739 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001740 break;
1741 case BinaryOperator::Mul:
1742 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001743 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001744 break;
1745 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001746 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001747 break;
1748 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001749 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001750 break;
1751 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001752 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001753 break;
1754 case BinaryOperator::Shl:
1755 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001756 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001757 break;
1758 case BinaryOperator::LE:
1759 case BinaryOperator::LT:
1760 case BinaryOperator::GE:
1761 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001762 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001763 break;
1764 case BinaryOperator::EQ:
1765 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001766 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001767 break;
1768 case BinaryOperator::And:
1769 case BinaryOperator::Xor:
1770 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001771 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001772 break;
1773 case BinaryOperator::LAnd:
1774 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001775 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001776 break;
1777 case BinaryOperator::MulAssign:
1778 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001779 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001780 if (!CompTy.isNull())
1781 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001782 break;
1783 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001784 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001785 if (!CompTy.isNull())
1786 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001787 break;
1788 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001789 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001790 if (!CompTy.isNull())
1791 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001792 break;
1793 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001794 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001795 if (!CompTy.isNull())
1796 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001797 break;
1798 case BinaryOperator::ShlAssign:
1799 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001800 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001801 if (!CompTy.isNull())
1802 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001803 break;
1804 case BinaryOperator::AndAssign:
1805 case BinaryOperator::XorAssign:
1806 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001807 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001808 if (!CompTy.isNull())
1809 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001810 break;
1811 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001812 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001813 break;
1814 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001815 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001816 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001817 if (CompTy.isNull())
Chris Lattnerc11005f2007-08-28 18:36:55 +00001818 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner256c21b2007-06-28 03:53:10 +00001819 else
Chris Lattnerc11005f2007-08-28 18:36:55 +00001820 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001821}
1822
Steve Naroff35d85152007-05-07 00:24:15 +00001823// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001824Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001825 ExprTy *input) {
1826 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001827 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1828 QualType resultType;
1829 switch (Opc) {
1830 default:
1831 assert(0 && "Unimplemented unary expr!");
1832 case UnaryOperator::PreInc:
1833 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001834 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001835 break;
1836 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001837 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001838 break;
1839 case UnaryOperator::Deref:
Steve Naroffb7235642007-12-18 04:06:57 +00001840 DefaultFunctionArrayConversion(Input);
Chris Lattner86554282007-06-08 22:32:33 +00001841 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001842 break;
1843 case UnaryOperator::Plus:
1844 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001845 UsualUnaryConversions(Input);
1846 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001847 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001848 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1849 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001850 break;
1851 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001852 UsualUnaryConversions(Input);
1853 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00001854 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1855 if (!resultType->isIntegerType()) {
1856 if (resultType->isComplexType())
1857 // C99 does not support '~' for complex conjugation.
1858 Diag(OpLoc, diag::ext_integer_complement_complex,
1859 resultType.getAsString());
1860 else
1861 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1862 resultType.getAsString());
1863 }
Steve Naroff35d85152007-05-07 00:24:15 +00001864 break;
1865 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001866 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001867 DefaultFunctionArrayConversion(Input);
1868 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001869 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001870 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1871 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001872 // LNot always has type int. C99 6.5.3.3p5.
1873 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001874 break;
1875 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001876 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001877 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001878 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001879 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1880 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00001881 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00001882 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00001883 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00001884 break;
Chris Lattner86554282007-06-08 22:32:33 +00001885 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00001886 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001887 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001888 }
1889 if (resultType.isNull())
1890 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001891 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001892}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001893
Steve Naroff66356bd2007-09-16 14:56:35 +00001894/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1895Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattnereefa10e2007-05-28 06:56:27 +00001896 SourceLocation LabLoc,
1897 IdentifierInfo *LabelII) {
1898 // Look up the record for this label identifier.
1899 LabelStmt *&LabelDecl = LabelMap[LabelII];
1900
1901 // If we haven't seen this label yet, create a forward reference.
1902 if (LabelDecl == 0)
1903 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1904
1905 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001906 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1907 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001908}
1909
Steve Naroff66356bd2007-09-16 14:56:35 +00001910Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner366727f2007-07-24 16:58:17 +00001911 SourceLocation RPLoc) { // "({..})"
1912 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1913 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1914 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1915
1916 // FIXME: there are a variety of strange constraints to enforce here, for
1917 // example, it is not possible to goto into a stmt expression apparently.
1918 // More semantic analysis is needed.
1919
1920 // FIXME: the last statement in the compount stmt has its value used. We
1921 // should not warn about it being unused.
1922
1923 // If there are sub stmts in the compound stmt, take the type of the last one
1924 // as the type of the stmtexpr.
1925 QualType Ty = Context.VoidTy;
1926
1927 if (!Compound->body_empty())
1928 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1929 Ty = LastExpr->getType();
1930
1931 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1932}
Steve Naroff78864672007-08-01 22:05:33 +00001933
Steve Naroff66356bd2007-09-16 14:56:35 +00001934Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattnerf17bd422007-08-30 17:45:32 +00001935 SourceLocation TypeLoc,
1936 TypeTy *argty,
1937 OffsetOfComponent *CompPtr,
1938 unsigned NumComponents,
1939 SourceLocation RPLoc) {
1940 QualType ArgTy = QualType::getFromOpaquePtr(argty);
1941 assert(!ArgTy.isNull() && "Missing type argument!");
1942
1943 // We must have at least one component that refers to the type, and the first
1944 // one is known to be a field designator. Verify that the ArgTy represents
1945 // a struct/union/class.
1946 if (!ArgTy->isRecordType())
1947 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
1948
1949 // Otherwise, create a compound literal expression as the base, and
1950 // iteratively process the offsetof designators.
Chris Lattner266a2ff2008-01-02 21:46:24 +00001951 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0);
Chris Lattnerf17bd422007-08-30 17:45:32 +00001952
Chris Lattner78502cf2007-08-31 21:49:13 +00001953 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
1954 // GCC extension, diagnose them.
1955 if (NumComponents != 1)
1956 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
1957 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
1958
Chris Lattnerf17bd422007-08-30 17:45:32 +00001959 for (unsigned i = 0; i != NumComponents; ++i) {
1960 const OffsetOfComponent &OC = CompPtr[i];
1961 if (OC.isBrackets) {
1962 // Offset of an array sub-field. TODO: Should we allow vector elements?
1963 const ArrayType *AT = Res->getType()->getAsArrayType();
1964 if (!AT) {
1965 delete Res;
1966 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
1967 Res->getType().getAsString());
1968 }
1969
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001970 // FIXME: C++: Verify that operator[] isn't overloaded.
1971
Chris Lattnerf17bd422007-08-30 17:45:32 +00001972 // C99 6.5.2.1p1
1973 Expr *Idx = static_cast<Expr*>(OC.U.E);
1974 if (!Idx->getType()->isIntegerType())
1975 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
1976 Idx->getSourceRange());
1977
1978 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
1979 continue;
1980 }
1981
1982 const RecordType *RC = Res->getType()->getAsRecordType();
1983 if (!RC) {
1984 delete Res;
1985 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
1986 Res->getType().getAsString());
1987 }
1988
1989 // Get the decl corresponding to this.
1990 RecordDecl *RD = RC->getDecl();
1991 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
1992 if (!MemberDecl)
1993 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
1994 OC.U.IdentInfo->getName(),
1995 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001996
1997 // FIXME: C++: Verify that MemberDecl isn't a static field.
1998 // FIXME: Verify that MemberDecl isn't a bitfield.
1999
Chris Lattnerf17bd422007-08-30 17:45:32 +00002000 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
2001 }
2002
2003 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2004 BuiltinLoc);
2005}
2006
2007
Steve Naroff66356bd2007-09-16 14:56:35 +00002008Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00002009 TypeTy *arg1, TypeTy *arg2,
2010 SourceLocation RPLoc) {
2011 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2012 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2013
2014 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2015
Chris Lattnerf17bd422007-08-30 17:45:32 +00002016 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00002017}
2018
Steve Naroff66356bd2007-09-16 14:56:35 +00002019Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff9efdabc2007-08-03 21:21:27 +00002020 ExprTy *expr1, ExprTy *expr2,
2021 SourceLocation RPLoc) {
2022 Expr *CondExpr = static_cast<Expr*>(cond);
2023 Expr *LHSExpr = static_cast<Expr*>(expr1);
2024 Expr *RHSExpr = static_cast<Expr*>(expr2);
2025
2026 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2027
2028 // The conditional expression is required to be a constant expression.
2029 llvm::APSInt condEval(32);
2030 SourceLocation ExpLoc;
2031 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2032 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2033 CondExpr->getSourceRange());
2034
2035 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2036 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2037 RHSExpr->getType();
2038 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2039}
2040
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002041Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2042 ExprTy *expr, TypeTy *type,
Chris Lattner9bad62c2008-01-04 18:04:52 +00002043 SourceLocation RPLoc) {
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002044 Expr *E = static_cast<Expr*>(expr);
2045 QualType T = QualType::getFromOpaquePtr(type);
2046
2047 InitBuiltinVaListType();
2048
Chris Lattner9bad62c2008-01-04 18:04:52 +00002049 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2050 != Compatible)
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002051 return Diag(E->getLocStart(),
2052 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2053 E->getType().getAsString(),
2054 E->getSourceRange());
2055
2056 // FIXME: Warn if a non-POD type is passed in.
2057
2058 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2059}
2060
Chris Lattner9bad62c2008-01-04 18:04:52 +00002061bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2062 SourceLocation Loc,
2063 QualType DstType, QualType SrcType,
2064 Expr *SrcExpr, const char *Flavor) {
2065 // Decode the result (notice that AST's are still created for extensions).
2066 bool isInvalid = false;
2067 unsigned DiagKind;
2068 switch (ConvTy) {
2069 default: assert(0 && "Unknown conversion type");
2070 case Compatible: return false;
Chris Lattner940cfeb2008-01-04 18:22:42 +00002071 case PointerToInt:
Chris Lattner9bad62c2008-01-04 18:04:52 +00002072 DiagKind = diag::ext_typecheck_convert_pointer_int;
2073 break;
Chris Lattner940cfeb2008-01-04 18:22:42 +00002074 case IntToPointer:
2075 DiagKind = diag::ext_typecheck_convert_int_pointer;
2076 break;
Chris Lattner9bad62c2008-01-04 18:04:52 +00002077 case IncompatiblePointer:
2078 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2079 break;
2080 case FunctionVoidPointer:
2081 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2082 break;
2083 case CompatiblePointerDiscardsQualifiers:
2084 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2085 break;
2086 case Incompatible:
2087 DiagKind = diag::err_typecheck_convert_incompatible;
2088 isInvalid = true;
2089 break;
2090 }
2091
2092 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2093 SrcExpr->getSourceRange());
2094 return isInvalid;
2095}
2096