blob: 7ed0bf102a6da37f4e60a57ddd8bdfed88fbaacf [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 Lattner17ed4872006-11-20 04:58:19 +000017#include "clang/AST/Decl.h"
Steve Naroffc6814ea2007-10-02 20:01:56 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000019#include "clang/AST/Expr.h"
Steve Naroffd54978b2007-09-18 23:55:05 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000021#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000022#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000023#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000024#include "clang/Basic/Diagnostic.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000025#include "clang/Basic/LangOptions.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026#include "clang/Basic/TargetInfo.h"
Chris Lattner08464942007-12-28 05:29:59 +000027#include "llvm/ADT/OwningPtr.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000028#include "llvm/ADT/SmallString.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000029#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000030using namespace clang;
31
Steve Naroff83895f72007-09-16 03:34:24 +000032/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000033/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
34/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
35/// multiple tokens. However, the common case is that StringToks points to one
36/// string.
37///
38Action::ExprResult
Steve Naroff83895f72007-09-16 03:34:24 +000039Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000040 assert(NumStringToks && "Must have at least one string!");
41
Steve Naroff4f88b312007-03-13 22:37:02 +000042 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
43 if (Literal.hadError)
44 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000045
Chris Lattner23b7eb62007-06-15 23:05:46 +000046 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000047 for (unsigned i = 0; i != NumStringToks; ++i)
48 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000049
50 // FIXME: handle wchar_t
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000051 QualType t;
52
53 if (Literal.Pascal)
54 t = Context.getPointerType(Context.UnsignedCharTy);
55 else
56 t = Context.getPointerType(Context.CharTy);
57
58 if (Literal.Pascal && Literal.GetStringLength() > 256)
59 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
60 SourceRange(StringToks[0].getLocation(),
61 StringToks[NumStringToks-1].getLocation()));
Steve Narofff1e53692007-03-23 22:27:02 +000062
Chris Lattner5b183d82006-11-10 05:03:26 +000063 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000064 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000065 Literal.AnyWide, t,
66 StringToks[0].getLocation(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000067 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000068}
69
Chris Lattnere168f762006-11-10 05:29:30 +000070
Steve Naroff30d242c2007-09-15 18:49:24 +000071/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattnerac18be92006-11-20 06:49:47 +000072/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
73/// identifier is used in an function call context.
Steve Naroff30d242c2007-09-15 18:49:24 +000074Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattnerac18be92006-11-20 06:49:47 +000075 IdentifierInfo &II,
76 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000077 // Could be enum-constant or decl.
Steve Naroff2f742082007-09-16 16:16:00 +000078 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000079 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000080 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000081 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000082 if (HasTrailingLParen &&
83 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000084 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000085 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000086 else {
Steve Naroffe46504b2007-11-12 14:29:37 +000087 if (CurMethodDecl) {
88 ObjcInterfaceDecl *IFace = CurMethodDecl->getClassInterface();
89 ObjcInterfaceDecl *clsDeclared;
Steve Narofff60782b2007-11-15 02:58:25 +000090 if (ObjcIvarDecl *IV = IFace->lookupInstanceVariable(&II, clsDeclared)) {
91 IdentifierInfo &II = Context.Idents.get("self");
92 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
93 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
94 static_cast<Expr*>(SelfExpr.Val), true, true);
95 }
Steve Naroffe46504b2007-11-12 14:29:37 +000096 }
Chris Lattnerac18be92006-11-20 06:49:47 +000097 // If this name wasn't predeclared and if this is not a function call,
98 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000099 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +0000100 }
Chris Lattner17ed4872006-11-20 04:58:19 +0000101 }
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000102 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroffcf871f52007-08-28 18:45:29 +0000103 // Only create DeclRefExpr's for valid Decl's.
Steve Narofff93b6722007-08-28 20:14:24 +0000104 if (VD->isInvalidDecl())
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000105 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000106 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000107 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000108 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +0000109 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Fariborz Jahanianb31a2f22007-12-05 18:16:33 +0000110 if (isa<ObjcInterfaceDecl>(D))
111 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Steve Narofff1e53692007-03-23 22:27:02 +0000112
113 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +0000114 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +0000115}
Chris Lattnere168f762006-11-10 05:29:30 +0000116
Steve Naroff83895f72007-09-16 03:34:24 +0000117Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson625bfc82007-07-21 05:21:51 +0000118 tok::TokenKind Kind) {
119 PreDefinedExpr::IdentType IT;
120
Chris Lattnere168f762006-11-10 05:29:30 +0000121 switch (Kind) {
122 default:
123 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +0000124 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000125 IT = PreDefinedExpr::Func;
126 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000127 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000128 IT = PreDefinedExpr::Function;
129 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000130 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000131 IT = PreDefinedExpr::PrettyFunction;
132 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000133 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000134
135 // Pre-defined identifiers are always of type char *.
136 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000137}
138
Steve Naroff83895f72007-09-16 03:34:24 +0000139Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000140 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000141 CharBuffer.resize(Tok.getLength());
142 const char *ThisTokBegin = &CharBuffer[0];
143 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
144
145 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
146 Tok.getLocation(), PP);
147 if (Literal.hadError())
148 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000149 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
150 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000151}
152
Steve Naroff83895f72007-09-16 03:34:24 +0000153Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000154 // fast path for a single digit (which is quite common). A single digit
155 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
156 if (Tok.getLength() == 1) {
157 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000158
Chris Lattner9cf21c52007-09-04 02:45:27 +0000159 unsigned IntSize = static_cast<unsigned>(
160 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Chris Lattner23b7eb62007-06-15 23:05:46 +0000161 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
162 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000163 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000164 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000165 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000166 IntegerBuffer.resize(Tok.getLength());
167 const char *ThisTokBegin = &IntegerBuffer[0];
168
Chris Lattner67ca9252007-05-21 01:08:44 +0000169 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000170 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000171 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000172 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000173 if (Literal.hadError)
174 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000175
Chris Lattner1c20a172007-08-26 03:42:43 +0000176 Expr *Res;
177
178 if (Literal.isFloatingLiteral()) {
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000179 QualType Ty;
180 const llvm::fltSemantics *Format;
181 uint64_t Size; unsigned Align;
182
183 if (Literal.isFloat) {
184 Ty = Context.FloatTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000185 Context.Target.getFloatInfo(Size, Align, Format,
186 Context.getFullLoc(Tok.getLocation()));
187
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000188 } else if (Literal.isLong) {
189 Ty = Context.LongDoubleTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000190 Context.Target.getLongDoubleInfo(Size, Align, Format,
191 Context.getFullLoc(Tok.getLocation()));
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000192 } else {
193 Ty = Context.DoubleTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000194 Context.Target.getDoubleInfo(Size, Align, Format,
195 Context.getFullLoc(Tok.getLocation()));
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000196 }
197
Ted Kremenek3a2c9502007-11-29 00:56:49 +0000198 // isExact will be set by GetFloatValue().
199 bool isExact = false;
200
201 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
202 Ty, Tok.getLocation());
203
Chris Lattner1c20a172007-08-26 03:42:43 +0000204 } else if (!Literal.isIntegerLiteral()) {
205 return ExprResult(true);
206 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000207 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000208
Neil Boothac582c52007-08-29 22:00:19 +0000209 // long long is a C99 feature.
210 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth4a1ee052007-08-29 22:13:52 +0000211 Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000212 Diag(Tok.getLocation(), diag::ext_longlong);
213
Chris Lattner67ca9252007-05-21 01:08:44 +0000214 // Get the value in the widest-possible width.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000215 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(
216 Context.getFullLoc(Tok.getLocation())), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000217
218 if (Literal.GetIntegerValue(ResultVal)) {
219 // If this value didn't fit into uintmax_t, warn and force to ull.
220 Diag(Tok.getLocation(), diag::warn_integer_too_large);
221 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000222 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000223 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000224 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000225 // If this value fits into a ULL, try to figure out what else it fits into
226 // according to the rules of C99 6.4.4.1p5.
227
228 // Octal, Hexadecimal, and integers with a U suffix are allowed to
229 // be an unsigned int.
230 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
231
232 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner7b939cf2007-08-23 21:58:08 +0000233 if (!Literal.isLong && !Literal.isLongLong) {
234 // Are int/unsigned possibilities?
Chris Lattner9cf21c52007-09-04 02:45:27 +0000235 unsigned IntSize = static_cast<unsigned>(
236 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000237 // Does it fit in a unsigned int?
238 if (ResultVal.isIntN(IntSize)) {
239 // Does it fit in a signed int?
240 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
241 t = Context.IntTy;
242 else if (AllowUnsigned)
243 t = Context.UnsignedIntTy;
244 }
245
246 if (!t.isNull())
247 ResultVal.trunc(IntSize);
248 }
249
250 // Are long/unsigned long possibilities?
251 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000252 unsigned LongSize = static_cast<unsigned>(
253 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000254
255 // Does it fit in a unsigned long?
256 if (ResultVal.isIntN(LongSize)) {
257 // Does it fit in a signed long?
258 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
259 t = Context.LongTy;
260 else if (AllowUnsigned)
261 t = Context.UnsignedLongTy;
262 }
263 if (!t.isNull())
264 ResultVal.trunc(LongSize);
265 }
266
267 // Finally, check long long if needed.
268 if (t.isNull()) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000269 unsigned LongLongSize = static_cast<unsigned>(
270 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000271
272 // Does it fit in a unsigned long long?
273 if (ResultVal.isIntN(LongLongSize)) {
274 // Does it fit in a signed long long?
275 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
276 t = Context.LongLongTy;
277 else if (AllowUnsigned)
278 t = Context.UnsignedLongLongTy;
279 }
280 }
281
282 // If we still couldn't decide a type, we probably have something that
283 // does not fit in a signed long long, but has no U suffix.
284 if (t.isNull()) {
285 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
286 t = Context.UnsignedLongLongTy;
287 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000288 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000289
Chris Lattner1c20a172007-08-26 03:42:43 +0000290 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000291 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000292
293 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
294 if (Literal.isImaginary)
295 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
296
297 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000298}
299
Steve Naroff83895f72007-09-16 03:34:24 +0000300Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattnere168f762006-11-10 05:29:30 +0000301 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000302 Expr *e = (Expr *)Val;
Steve Naroff83895f72007-09-16 03:34:24 +0000303 assert((e != 0) && "ActOnParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000304 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000305}
306
Steve Naroff71b59a92007-06-04 22:22:31 +0000307/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000308/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000309QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
310 SourceLocation OpLoc, bool isSizeof) {
311 // C99 6.5.3.4p1:
312 if (isa<FunctionType>(exprType) && isSizeof)
313 // alignof(function) is allowed.
314 Diag(OpLoc, diag::ext_sizeof_function_type);
315 else if (exprType->isVoidType())
316 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
317 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000318 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000319 diag::err_alignof_incomplete_type,
320 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000321 return QualType(); // error
322 }
323 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
324 return Context.getSizeType();
325}
326
Chris Lattnere168f762006-11-10 05:29:30 +0000327Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000328ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000329 SourceLocation LPLoc, TypeTy *Ty,
330 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000331 // If error parsing type, ignore.
332 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000333
334 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000335 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000336
Steve Naroff043d45d2007-05-15 02:32:35 +0000337 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
338
339 if (resultType.isNull())
340 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000341 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000342}
343
Chris Lattner74ed76b2007-08-24 21:41:10 +0000344QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000345 DefaultFunctionArrayConversion(V);
346
Chris Lattnere267f5d2007-08-26 05:39:26 +0000347 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000348 if (const ComplexType *CT = V->getType()->getAsComplexType())
349 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000350
351 // Otherwise they pass through real integer and floating point types here.
352 if (V->getType()->isArithmeticType())
353 return V->getType();
354
355 // Reject anything else.
356 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
357 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000358}
359
360
Chris Lattnere168f762006-11-10 05:29:30 +0000361
Steve Naroff83895f72007-09-16 03:34:24 +0000362Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000363 tok::TokenKind Kind,
364 ExprTy *Input) {
365 UnaryOperator::Opcode Opc;
366 switch (Kind) {
367 default: assert(0 && "Unknown unary op!");
368 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
369 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
370 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000371 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000372 if (result.isNull())
373 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000374 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000375}
376
377Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000378ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000379 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000380 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000381
382 // Perform default conversions.
383 DefaultFunctionArrayConversion(LHSExp);
384 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000385
Chris Lattner36d572b2007-07-16 00:14:47 +0000386 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000387
Steve Naroffc1aadb12007-03-28 21:49:40 +0000388 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattnerf17bd422007-08-30 17:45:32 +0000389 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000390 // in the subscript position. As a result, we need to derive the array base
391 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000392 Expr *BaseExpr, *IndexExpr;
393 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000394 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000395 BaseExpr = LHSExp;
396 IndexExpr = RHSExp;
397 // FIXME: need to deal with const...
398 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000399 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000400 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000401 BaseExpr = RHSExp;
402 IndexExpr = LHSExp;
403 // FIXME: need to deal with const...
404 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000405 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
406 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000407 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000408
409 // Component access limited to variables (reject vec4.rg[1]).
410 if (!isa<DeclRefExpr>(BaseExpr))
411 return Diag(LLoc, diag::err_ocuvector_component_access,
412 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000413 // FIXME: need to deal with const...
414 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000415 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000416 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
417 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000418 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000419 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000420 if (!IndexExpr->getType()->isIntegerType())
421 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
422 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000423
Chris Lattner36d572b2007-07-16 00:14:47 +0000424 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
425 // the following check catches trying to index a pointer to a function (e.g.
426 // void (*)(int)). Functions are not objects in C99.
427 if (!ResultType->isObjectType())
428 return Diag(BaseExpr->getLocStart(),
429 diag::err_typecheck_subscript_not_object,
430 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
431
432 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000433}
434
Steve Narofff8fd09e2007-07-27 22:15:19 +0000435QualType Sema::
436CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
437 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000438 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000439
440 // The vector accessor can't exceed the number of elements.
441 const char *compStr = CompName.getName();
442 if (strlen(compStr) > vecType->getNumElements()) {
443 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
444 baseType.getAsString(), SourceRange(CompLoc));
445 return QualType();
446 }
447 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000448 if (vecType->getPointAccessorIdx(*compStr) != -1) {
449 do
450 compStr++;
451 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
452 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
453 do
454 compStr++;
455 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
456 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
457 do
458 compStr++;
459 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
460 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000461
462 if (*compStr) {
463 // We didn't get to the end of the string. This means the component names
464 // didn't come from the same set *or* we encountered an illegal name.
465 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
466 std::string(compStr,compStr+1), SourceRange(CompLoc));
467 return QualType();
468 }
469 // Each component accessor can't exceed the vector type.
470 compStr = CompName.getName();
471 while (*compStr) {
472 if (vecType->isAccessorWithinNumElements(*compStr))
473 compStr++;
474 else
475 break;
476 }
477 if (*compStr) {
478 // We didn't get to the end of the string. This means a component accessor
479 // exceeds the number of elements in the vector.
480 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
481 baseType.getAsString(), SourceRange(CompLoc));
482 return QualType();
483 }
484 // The component accessor looks fine - now we need to compute the actual type.
485 // The vector type is implied by the component accessor. For example,
486 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
487 unsigned CompSize = strlen(CompName.getName());
488 if (CompSize == 1)
489 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000490
491 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
492 // Now look up the TypeDefDecl from the vector type. Without this,
493 // diagostics look bad. We want OCU vector types to appear built-in.
494 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
495 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
496 return Context.getTypedefType(OCUVectorDecls[i]);
497 }
498 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000499}
500
Chris Lattnere168f762006-11-10 05:29:30 +0000501Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000502ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000503 tok::TokenKind OpKind, SourceLocation MemberLoc,
504 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000505 Expr *BaseExpr = static_cast<Expr *>(Base);
506 assert(BaseExpr && "no record expression");
Steve Naroffeaaae462007-12-16 21:42:28 +0000507
508 // Perform default conversions.
509 DefaultFunctionArrayConversion(BaseExpr);
Steve Naroffca8f7122007-04-01 01:41:35 +0000510
Steve Naroff185616f2007-07-26 03:11:44 +0000511 QualType BaseType = BaseExpr->getType();
512 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000513
Steve Narofff1e53692007-03-23 22:27:02 +0000514 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000515 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000516 BaseType = PT->getPointeeType();
517 else
518 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
519 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000520 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000521 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000522 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000523 RecordDecl *RDecl = RTy->getDecl();
524 if (RTy->isIncompleteType())
525 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
526 BaseExpr->getSourceRange());
527 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000528 FieldDecl *MemberDecl = RDecl->getMember(&Member);
529 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000530 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
531 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000532 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
533 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000534 // Component access limited to variables (reject vec4.rg.g).
535 if (!isa<DeclRefExpr>(BaseExpr))
536 return Diag(OpLoc, diag::err_ocuvector_component_access,
537 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000538 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
539 if (ret.isNull())
540 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000541 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000542 } else if (BaseType->isObjcInterfaceType()) {
543 ObjcInterfaceDecl *IFace;
544 if (isa<ObjcInterfaceType>(BaseType.getCanonicalType()))
545 IFace = dyn_cast<ObjcInterfaceType>(BaseType)->getDecl();
546 else
Fariborz Jahanianc47dc4f2007-12-13 20:47:42 +0000547 IFace = dyn_cast<ObjcQualifiedInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000548 ObjcInterfaceDecl *clsDeclared;
549 if (ObjcIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
550 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
551 OpKind==tok::arrow);
552 }
553 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
554 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000555}
556
Steve Naroff83895f72007-09-16 03:34:24 +0000557/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattnere168f762006-11-10 05:29:30 +0000558/// This provides the location of the left/right parens and a list of comma
559/// locations.
560Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000561ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner08464942007-12-28 05:29:59 +0000562 ExprTy **args, unsigned NumArgs,
Chris Lattnere168f762006-11-10 05:29:30 +0000563 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000564 Expr *Fn = static_cast<Expr *>(fn);
565 Expr **Args = reinterpret_cast<Expr**>(args);
566 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000567
Chris Lattner08464942007-12-28 05:29:59 +0000568 // Make the call expr early, before semantic checks. This guarantees cleanup
569 // of arguments and function on error.
570 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
571 Context.BoolTy, RParenLoc));
572
573 // Promote the function operand.
574 TheCall->setCallee(UsualUnaryConversions(Fn));
575
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000576 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
577 // type pointer to function".
Chris Lattner08464942007-12-28 05:29:59 +0000578 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000579 if (PT == 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 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
583 if (FuncT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000584 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
585 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner08464942007-12-28 05:29:59 +0000586
587 // We know the result type of the call, set it.
588 TheCall->setType(FuncT->getResultType());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000589
Chris Lattner08464942007-12-28 05:29:59 +0000590 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000591 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
592 // assignment, to the types of the corresponding parameter, ...
Chris Lattner08464942007-12-28 05:29:59 +0000593 unsigned NumArgsInProto = Proto->getNumArgs();
594 unsigned NumArgsToCheck = NumArgs;
Steve Naroff17f76e02007-05-03 21:03:48 +0000595
Chris Lattner08464942007-12-28 05:29:59 +0000596 // If too few arguments are available, don't make the call.
597 if (NumArgs < NumArgsInProto)
598 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
599 Fn->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000600
Chris Lattner08464942007-12-28 05:29:59 +0000601 // If too many are passed and not variadic, error on the extras and drop
602 // them.
603 if (NumArgs > NumArgsInProto) {
604 if (!Proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000605 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000606 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000607 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner08464942007-12-28 05:29:59 +0000608 Args[NumArgs-1]->getLocEnd()));
609 // This deletes the extra arguments.
610 TheCall->setNumArgs(NumArgsInProto);
Steve Naroff8563f652007-05-28 19:25:56 +0000611 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000612 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000613 }
Chris Lattner08464942007-12-28 05:29:59 +0000614
Steve Naroff17f76e02007-05-03 21:03:48 +0000615 // Continue to check argument types (even if we have too few/many args).
Chris Lattner08464942007-12-28 05:29:59 +0000616 for (unsigned i = 0; i != NumArgsToCheck; i++) {
617 Expr *Arg = Args[i];
Chris Lattner9bad62c2008-01-04 18:04:52 +0000618 QualType ProtoArgType = Proto->getArgType(i);
619 QualType ArgType = Arg->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000620
Chris Lattner08464942007-12-28 05:29:59 +0000621 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner9bad62c2008-01-04 18:04:52 +0000622 AssignConvertType ConvTy =
623 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner08464942007-12-28 05:29:59 +0000624 TheCall->setArg(i, Arg);
625
Chris Lattner9bad62c2008-01-04 18:04:52 +0000626 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
627 ArgType, Arg, "passing"))
628 return true;
Steve Naroff17f76e02007-05-03 21:03:48 +0000629 }
Chris Lattner08464942007-12-28 05:29:59 +0000630
631 // If this is a variadic call, handle args passed through "...".
632 if (Proto->isVariadic()) {
Steve Naroff0b661582007-08-28 23:30:39 +0000633 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner08464942007-12-28 05:29:59 +0000634 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
635 Expr *Arg = Args[i];
636 DefaultArgumentPromotion(Arg);
637 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000638 }
Steve Naroff0b661582007-08-28 23:30:39 +0000639 }
Chris Lattner08464942007-12-28 05:29:59 +0000640 } else {
641 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
642
Steve Naroff0b661582007-08-28 23:30:39 +0000643 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner08464942007-12-28 05:29:59 +0000644 for (unsigned i = 0; i != NumArgs; i++) {
645 Expr *Arg = Args[i];
646 DefaultArgumentPromotion(Arg);
647 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000648 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000649 }
Chris Lattner08464942007-12-28 05:29:59 +0000650
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000651 // Do special checking on direct calls to functions.
652 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
653 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
654 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattner08464942007-12-28 05:29:59 +0000655 if (CheckFunctionCall(FDecl, TheCall.get()))
Anders Carlsson98f07902007-08-17 05:31:46 +0000656 return true;
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000657
Chris Lattner08464942007-12-28 05:29:59 +0000658 return TheCall.take();
Chris Lattnere168f762006-11-10 05:29:30 +0000659}
660
661Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000662ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000663 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff83895f72007-09-16 03:34:24 +0000664 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Narofffbd09832007-07-19 01:06:55 +0000665 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000666 // FIXME: put back this assert when initializers are worked out.
Steve Naroff83895f72007-09-16 03:34:24 +0000667 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroff57eb2c52007-07-19 21:32:11 +0000668 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson2c1ec6d2007-12-05 07:24:19 +0000669
Steve Naroff91f78082007-12-10 22:44:33 +0000670 // FIXME: add more semantic analysis (C99 6.5.2.5).
671 if (CheckInitializer(literalExpr, literalType, false))
672 return 0;
Anders Carlsson2c1ec6d2007-12-05 07:24:19 +0000673
Chris Lattner266a2ff2008-01-02 21:46:24 +0000674 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000675}
676
677Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000678ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson4692db02007-08-31 04:56:16 +0000679 SourceLocation RBraceLoc) {
Steve Naroff2fea1392007-09-02 02:04:30 +0000680 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson4692db02007-08-31 04:56:16 +0000681
Steve Naroff30d242c2007-09-15 18:49:24 +0000682 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff7d2c5ed2007-09-03 01:24:23 +0000683 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson4692db02007-08-31 04:56:16 +0000684
Steve Naroffb03f5942007-09-02 20:30:18 +0000685 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
686 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
687 return e;
Steve Narofffbd09832007-07-19 01:06:55 +0000688}
689
Chris Lattner6c9ffe92007-12-20 00:44:32 +0000690bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonde71adf2007-11-27 05:51:55 +0000691 assert(VectorTy->isVectorType() && "Not a vector type!");
692
693 if (Ty->isVectorType() || Ty->isIntegerType()) {
694 if (Context.getTypeSize(VectorTy, SourceLocation()) !=
695 Context.getTypeSize(Ty, SourceLocation()))
696 return Diag(R.getBegin(),
697 Ty->isVectorType() ?
698 diag::err_invalid_conversion_between_vectors :
699 diag::err_invalid_conversion_between_vector_and_integer,
700 VectorTy.getAsString().c_str(),
701 Ty.getAsString().c_str(), R);
702 } else
703 return Diag(R.getBegin(),
704 diag::err_invalid_conversion_between_vector_and_scalar,
705 VectorTy.getAsString().c_str(),
706 Ty.getAsString().c_str(), R);
707
708 return false;
709}
710
Steve Narofffbd09832007-07-19 01:06:55 +0000711Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000712ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattnere168f762006-11-10 05:29:30 +0000713 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff83895f72007-09-16 03:34:24 +0000714 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000715
716 Expr *castExpr = static_cast<Expr*>(Op);
717 QualType castType = QualType::getFromOpaquePtr(Ty);
718
Steve Naroff43b8f7f2007-08-31 00:32:44 +0000719 UsualUnaryConversions(castExpr);
720
Chris Lattnerbd270732007-07-18 16:00:06 +0000721 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
722 // type needs to be scalar.
Chris Lattner3bc4d202007-10-29 04:26:44 +0000723 if (!castType->isVoidType()) { // Cast to void allows any expr type.
724 if (!castType->isScalarType())
725 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
726 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Anders Carlssonde71adf2007-11-27 05:51:55 +0000727 if (!castExpr->getType()->isScalarType())
Chris Lattner3bc4d202007-10-29 04:26:44 +0000728 return Diag(castExpr->getLocStart(),
729 diag::err_typecheck_expect_scalar_operand,
730 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonde71adf2007-11-27 05:51:55 +0000731
732 if (castExpr->getType()->isVectorType()) {
733 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
734 castExpr->getType(), castType))
735 return true;
736 } else if (castType->isVectorType()) {
737 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
738 castType, castExpr->getType()))
739 return true;
Chris Lattner3bc4d202007-10-29 04:26:44 +0000740 }
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000741 }
742 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000743}
744
Steve Naroffa78c4642007-10-18 05:13:08 +0000745// promoteExprToType - a helper function to ensure we create exactly one
746// ImplicitCastExpr.
747static void promoteExprToType(Expr *&expr, QualType type) {
748 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
749 impCast->setType(type);
750 else
751 expr = new ImplicitCastExpr(type, expr);
752 return;
753}
754
Chris Lattner2ab40a62007-11-26 01:40:58 +0000755/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
756/// In that case, lex = cond.
Steve Narofff8a28c52007-05-15 20:29:32 +0000757inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000758 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000759 UsualUnaryConversions(cond);
760 UsualUnaryConversions(lex);
761 UsualUnaryConversions(rex);
762 QualType condT = cond->getType();
763 QualType lexT = lex->getType();
764 QualType rexT = rex->getType();
765
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000766 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000767 if (!condT->isScalarType()) { // C99 6.5.15p2
768 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
769 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000770 return QualType();
771 }
772 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000773 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
774 UsualArithmeticConversions(lex, rex);
775 return lex->getType();
776 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000777 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
778 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
Chris Lattner2ab40a62007-11-26 01:40:58 +0000779 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000780 return lexT;
781
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000782 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000783 lexT.getAsString(), rexT.getAsString(),
784 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000785 return QualType();
786 }
787 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000788 // C99 6.5.15p3
Steve Naroffa78c4642007-10-18 05:13:08 +0000789 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
790 promoteExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff7a5af782007-07-13 16:58:59 +0000791 return lexT;
Steve Naroffa78c4642007-10-18 05:13:08 +0000792 }
793 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
794 promoteExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff7a5af782007-07-13 16:58:59 +0000795 return rexT;
Steve Naroffa78c4642007-10-18 05:13:08 +0000796 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000797 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
798 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
799 // get the "pointed to" types
800 QualType lhptee = LHSPT->getPointeeType();
801 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000802
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000803 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
804 if (lhptee->isVoidType() &&
805 (rhptee->isObjectType() || rhptee->isIncompleteType()))
806 return lexT;
807 if (rhptee->isVoidType() &&
808 (lhptee->isObjectType() || lhptee->isIncompleteType()))
809 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000810
Steve Naroff32e44c02007-10-15 20:41:53 +0000811 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
812 rhptee.getUnqualifiedType())) {
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000813 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
814 lexT.getAsString(), rexT.getAsString(),
815 lex->getSourceRange(), rex->getSourceRange());
816 return lexT; // FIXME: this is an _ext - is this return o.k?
817 }
818 // The pointer types are compatible.
Chris Lattnerf17bd422007-08-30 17:45:32 +0000819 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
820 // differently qualified versions of compatible types, the result type is
821 // a pointer to an appropriately qualified version of the *composite*
822 // type.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000823 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000824 }
825 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000826
Steve Naroff7a5af782007-07-13 16:58:59 +0000827 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
828 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000829
830 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000831 lexT.getAsString(), rexT.getAsString(),
832 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000833 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000834}
835
Steve Naroff83895f72007-09-16 03:34:24 +0000836/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattnere168f762006-11-10 05:29:30 +0000837/// in the case of a the GNU conditional expr extension.
Steve Naroff83895f72007-09-16 03:34:24 +0000838Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000839 SourceLocation ColonLoc,
840 ExprTy *Cond, ExprTy *LHS,
841 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000842 Expr *CondExpr = (Expr *) Cond;
843 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000844
845 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
846 // was the condition.
847 bool isLHSNull = LHSExpr == 0;
848 if (isLHSNull)
849 LHSExpr = CondExpr;
850
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000851 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
852 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000853 if (result.isNull())
854 return true;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000855 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
856 RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000857}
858
Steve Naroff0b661582007-08-28 23:30:39 +0000859/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
860/// do not have a prototype. Integer promotions are performed on each
861/// argument, and arguments that have type float are promoted to double.
Chris Lattner08464942007-12-28 05:29:59 +0000862void Sema::DefaultArgumentPromotion(Expr *&Expr) {
863 QualType Ty = Expr->getType();
864 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroff0b661582007-08-28 23:30:39 +0000865
Chris Lattner08464942007-12-28 05:29:59 +0000866 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
867 promoteExprToType(Expr, Context.IntTy);
868 if (Ty == Context.FloatTy)
869 promoteExprToType(Expr, Context.DoubleTy);
Steve Naroff0b661582007-08-28 23:30:39 +0000870}
871
Steve Naroff81569d22007-07-15 02:02:06 +0000872/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000873void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000874 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000875 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000876
Chris Lattnercd1d0862007-07-31 16:56:34 +0000877 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000878 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
879 t = e->getType();
880 }
Steve Naroff81569d22007-07-15 02:02:06 +0000881 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000882 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000883 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000884 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000885}
886
Steve Naroff71b59a92007-06-04 22:22:31 +0000887/// UsualUnaryConversion - Performs various conversions that are common to most
888/// operators (C99 6.3). The conversions of array and function types are
889/// sometimes surpressed. For example, the array->pointer conversion doesn't
890/// apply if the array is an argument to the sizeof or address (&) operators.
891/// In these instances, this routine should *not* be called.
Chris Lattner08464942007-12-28 05:29:59 +0000892Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
893 QualType Ty = Expr->getType();
894 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Steve Naroff71b59a92007-06-04 22:22:31 +0000895
Chris Lattner08464942007-12-28 05:29:59 +0000896 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
897 promoteExprToType(Expr, Ref->getReferenceeType()); // C++ [expr]
898 Ty = Expr->getType();
Bill Wendling354fb262007-07-17 04:16:47 +0000899 }
Chris Lattner08464942007-12-28 05:29:59 +0000900 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
901 promoteExprToType(Expr, Context.IntTy);
Steve Naroff31090012007-07-16 21:54:35 +0000902 else
Chris Lattner08464942007-12-28 05:29:59 +0000903 DefaultFunctionArrayConversion(Expr);
904
905 return Expr;
Steve Naroff71b59a92007-06-04 22:22:31 +0000906}
907
Chris Lattnerf17bd422007-08-30 17:45:32 +0000908/// UsualArithmeticConversions - Performs various conversions that are common to
Steve Naroff1926c832007-04-24 00:23:05 +0000909/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
910/// routine returns the first non-arithmetic type found. The client is
911/// responsible for emitting appropriate error diagnostics.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000912QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
913 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +0000914 if (!isCompAssign) {
915 UsualUnaryConversions(lhsExpr);
916 UsualUnaryConversions(rhsExpr);
917 }
Steve Naroff1bb21df2007-10-18 18:55:53 +0000918 // For conversion purposes, we ignore any qualifiers.
919 // For example, "const float" and "float" are equivalent.
Steve Naroff257b4a22007-11-10 19:45:54 +0000920 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
921 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Steve Naroff1926c832007-04-24 00:23:05 +0000922
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000923 // If both types are identical, no conversion is needed.
Steve Naroff1bb21df2007-10-18 18:55:53 +0000924 if (lhs == rhs)
925 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000926
927 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
928 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000929 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000930 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000931
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000932 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000933
934 // Handle complex types first (C99 6.3.1.8p1).
935 if (lhs->isComplexType() || rhs->isComplexType()) {
936 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000937 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000938 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
939 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000940 }
941 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000942 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
943 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000944 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000945 // This handles complex/complex, complex/float, or float/complex.
946 // When both operands are complex, the shorter operand is converted to the
947 // type of the longer, and that is the type of the result. This corresponds
948 // to what is done when combining two real floating-point operands.
949 // The fun begins when size promotion occur across type domains.
950 // From H&S 6.3.4: When one operand is complex and the other is a real
951 // floating-point type, the less precise type is converted, within it's
952 // real or complex domain, to the precision of the other type. For example,
953 // when combining a "long double" with a "double _Complex", the
954 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff7af82d42007-08-27 15:30:22 +0000955 int result = Context.compareFloatingType(lhs, rhs);
956
957 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroffe31313d2007-08-27 21:32:55 +0000958 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
959 if (!isCompAssign)
960 promoteExprToType(rhsExpr, rhs);
961 } else if (result < 0) { // The right side is bigger, convert lhs.
962 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
963 if (!isCompAssign)
964 promoteExprToType(lhsExpr, lhs);
965 }
966 // At this point, lhs and rhs have the same rank/size. Now, make sure the
967 // domains match. This is a requirement for our implementation, C99
968 // does not require this promotion.
969 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
970 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroffa042db22007-08-27 21:43:43 +0000971 if (!isCompAssign)
972 promoteExprToType(lhsExpr, rhs);
973 return rhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000974 } else { // handle "_Complex double, double".
Steve Naroffa042db22007-08-27 21:43:43 +0000975 if (!isCompAssign)
976 promoteExprToType(rhsExpr, lhs);
977 return lhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000978 }
Steve Naroffdbd9e892007-07-17 00:58:39 +0000979 }
Steve Naroffa042db22007-08-27 21:43:43 +0000980 return lhs; // The domain/size match exactly.
Steve Naroffbf223ba2007-04-24 20:56:26 +0000981 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000982 // Now handle "real" floating types (i.e. float, double, long double).
983 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
984 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000985 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000986 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
987 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000988 }
989 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000990 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
991 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000992 }
Steve Naroff81569d22007-07-15 02:02:06 +0000993 // We have two real floating types, float/complex combos were handled above.
994 // Convert the smaller operand to the bigger result.
Steve Naroff7af82d42007-08-27 15:30:22 +0000995 int result = Context.compareFloatingType(lhs, rhs);
996
997 if (result > 0) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000998 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
999 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001000 }
Steve Naroff7af82d42007-08-27 15:30:22 +00001001 if (result < 0) { // convert the lhs
1002 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
1003 return rhs;
1004 }
1005 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +00001006 }
Steve Naroff81569d22007-07-15 02:02:06 +00001007 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +00001008 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001009 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1010 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001011 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001012 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
1013 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +00001014}
1015
Steve Naroff3f597292007-05-11 22:18:03 +00001016// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1017// being closely modeled after the C99 spec:-). The odd characteristic of this
1018// routine is it effectively iqnores the qualifiers on the top level pointee.
1019// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1020// FIXME: add a couple examples in this comment.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001021Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001022Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +00001023 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001024
1025 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +00001026 lhptee = lhsType->getAsPointerType()->getPointeeType();
1027 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001028
1029 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +00001030 lhptee = lhptee.getCanonicalType();
1031 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001032
Chris Lattner9bad62c2008-01-04 18:04:52 +00001033 AssignConvertType ConvTy = Compatible;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001034
Steve Naroff3f597292007-05-11 22:18:03 +00001035 // C99 6.5.16.1p1: This following citation is common to constraints
1036 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1037 // qualifiers of the type *pointed to* by the right;
1038 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
1039 rhptee.getQualifiers())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001040 ConvTy = CompatiblePointerDiscardsQualifiers;
Steve Naroff3f597292007-05-11 22:18:03 +00001041
1042 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1043 // incomplete type and the other is a pointer to a qualified or unqualified
1044 // version of void...
Chris Lattner0a788432008-01-03 22:56:36 +00001045 if (lhptee->isVoidType()) {
1046 if (rhptee->isObjectType() || rhptee->isIncompleteType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001047 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001048
1049 // As an extension, we allow cast to/from void* to function pointer.
1050 if (rhptee->isFunctionType())
1051 return FunctionVoidPointer;
1052 }
1053
1054 if (rhptee->isVoidType()) {
1055 if (lhptee->isObjectType() || lhptee->isIncompleteType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001056 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001057
1058 // As an extension, we allow cast to/from void* to function pointer.
1059 if (lhptee->isFunctionType())
1060 return FunctionVoidPointer;
1061 }
1062
Steve Naroff3f597292007-05-11 22:18:03 +00001063 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1064 // unqualified versions of compatible types, ...
Chris Lattner0a788432008-01-03 22:56:36 +00001065 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1066 rhptee.getUnqualifiedType()))
1067 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner9bad62c2008-01-04 18:04:52 +00001068 return ConvTy;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001069}
1070
Steve Naroff98cf3e92007-06-06 18:38:38 +00001071/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +00001072/// has code to accommodate several GCC extensions when type checking
1073/// pointers. Here are some objectionable examples that GCC considers warnings:
1074///
1075/// int a, *pint;
1076/// short *pshort;
1077/// struct foo *pfoo;
1078///
1079/// pint = pshort; // warning: assignment from incompatible pointer type
1080/// a = pint; // warning: assignment makes integer from pointer without a cast
1081/// pint = a; // warning: assignment makes pointer from integer without a cast
1082/// pint = pfoo; // warning: assignment from incompatible pointer type
1083///
1084/// As a result, the code for dealing with pointers is more complex than the
1085/// C99 spec dictates.
1086/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1087///
Chris Lattner9bad62c2008-01-04 18:04:52 +00001088Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001089Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001090
1091
Steve Naroff4a158f32007-11-13 00:31:42 +00001092 if (lhsType.getCanonicalType().getUnqualifiedType() ==
1093 rhsType.getCanonicalType().getUnqualifiedType())
Chris Lattnerb104d312007-10-29 05:15:40 +00001094 return Compatible; // common case, fast path...
Steve Naroff44fd8ff2007-07-24 21:46:40 +00001095
Anders Carlsson24ebce62007-10-12 23:56:29 +00001096 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001097 if (Context.referenceTypesAreCompatible(lhsType, rhsType))
Anders Carlsson24ebce62007-10-12 23:56:29 +00001098 return Compatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001099 }
1100 else if (lhsType->isObjcQualifiedIdType()
1101 || rhsType->isObjcQualifiedIdType()) {
1102 if (Context.ObjcQualifiedIdTypesAreCompatible(lhsType, rhsType))
1103 return Compatible;
1104 }
1105 else if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +00001106 if (lhsType->isVectorType() || rhsType->isVectorType()) {
Nate Begeman330aaa72007-12-30 02:59:45 +00001107 // For OCUVector, allow vector splats; float -> <n x float>
1108 if (const OCUVectorType *LV = lhsType->getAsOCUVectorType()) {
1109 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1110 return Compatible;
1111 }
Anders Carlssone1af1d22007-11-30 04:21:22 +00001112 if (!getLangOptions().LaxVectorConversions) {
1113 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
1114 return Incompatible;
1115 } else {
1116 if (lhsType->isVectorType() && rhsType->isVectorType()) {
Nate Begeman627daba2007-12-30 01:45:55 +00001117 // If LHS and RHS are both integer or both floating point types, and
1118 // the total vector length is the same, allow the conversion. This is
1119 // a bitcast; no bits are changed but the result type is different.
Anders Carlssone1af1d22007-11-30 04:21:22 +00001120 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1121 (lhsType->isRealFloatingType() &&
1122 rhsType->isRealFloatingType())) {
1123 if (Context.getTypeSize(lhsType, SourceLocation()) ==
1124 Context.getTypeSize(rhsType, SourceLocation()))
1125 return Compatible;
1126 }
1127 }
Steve Naroffe728ba32007-07-10 22:20:04 +00001128 return Incompatible;
Anders Carlssone1af1d22007-11-30 04:21:22 +00001129 }
1130 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001131 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +00001132 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001133 if (rhsType->isIntegerType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001134 return PointerInt;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001135
Steve Naroff3f597292007-05-11 22:18:03 +00001136 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001137 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +00001138 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001139 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1140 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
Chris Lattner9bad62c2008-01-04 18:04:52 +00001141 return PointerInt;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001142
Steve Naroff3f597292007-05-11 22:18:03 +00001143 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001144 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +00001145 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001146 if (Context.tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001147 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +00001148 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001149 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +00001150}
1151
Chris Lattner9bad62c2008-01-04 18:04:52 +00001152Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001153Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001154 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1155 // a null pointer constant.
Fariborz Jahanian5cc21a72008-01-03 18:46:52 +00001156 if ((lhsType->isPointerType() || lhsType->isObjcQualifiedIdType())
1157 && rExpr->isNullPointerConstant(Context)) {
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001158 promoteExprToType(rExpr, lhsType);
1159 return Compatible;
1160 }
Chris Lattnere6dcd502007-10-16 02:55:40 +00001161 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001162 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff30d242c2007-09-15 18:49:24 +00001163 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001164 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattnere6dcd502007-10-16 02:55:40 +00001165 //
1166 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1167 // are better understood.
1168 if (!lhsType->isReferenceType())
1169 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001170
Chris Lattner9bad62c2008-01-04 18:04:52 +00001171 Sema::AssignConvertType result =
1172 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001173
1174 // C99 6.5.16.1p2: The value of the right operand is converted to the
1175 // type of the assignment expression.
1176 if (rExpr->getType() != lhsType)
1177 promoteExprToType(rExpr, lhsType);
1178 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001179}
1180
Chris Lattner9bad62c2008-01-04 18:04:52 +00001181Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001182Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1183 return CheckAssignmentConstraints(lhsType, rhsType);
1184}
1185
Chris Lattner5c11c412007-12-12 05:47:28 +00001186QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001187 Diag(loc, diag::err_typecheck_invalid_operands,
1188 lex->getType().getAsString(), rex->getType().getAsString(),
1189 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner5c11c412007-12-12 05:47:28 +00001190 return QualType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001191}
1192
Steve Naroff7a5af782007-07-13 16:58:59 +00001193inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1194 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +00001195 QualType lhsType = lex->getType(), rhsType = rex->getType();
1196
1197 // make sure the vector types are identical.
1198 if (lhsType == rhsType)
1199 return lhsType;
Nate Begeman330aaa72007-12-30 02:59:45 +00001200
1201 // if the lhs is an ocu vector and the rhs is a scalar of the same type,
1202 // promote the rhs to the vector type.
1203 if (const OCUVectorType *V = lhsType->getAsOCUVectorType()) {
1204 if (V->getElementType().getCanonicalType().getTypePtr()
1205 == rhsType.getCanonicalType().getTypePtr()) {
1206 promoteExprToType(rex, lhsType);
1207 return lhsType;
1208 }
1209 }
1210
1211 // if the rhs is an ocu vector and the lhs is a scalar of the same type,
1212 // promote the lhs to the vector type.
1213 if (const OCUVectorType *V = rhsType->getAsOCUVectorType()) {
1214 if (V->getElementType().getCanonicalType().getTypePtr()
1215 == lhsType.getCanonicalType().getTypePtr()) {
1216 promoteExprToType(lex, rhsType);
1217 return rhsType;
1218 }
1219 }
1220
Steve Naroff84ff4b42007-07-09 21:31:10 +00001221 // You cannot convert between vector values of different size.
1222 Diag(loc, diag::err_typecheck_vector_not_convertable,
1223 lex->getType().getAsString(), rex->getType().getAsString(),
1224 lex->getSourceRange(), rex->getSourceRange());
1225 return QualType();
1226}
1227
Steve Naroff218bc2b2007-05-04 21:54:46 +00001228inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001229 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001230{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001231 QualType lhsType = lex->getType(), rhsType = rex->getType();
1232
1233 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001234 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001235
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001236 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001237
Steve Naroffdbd9e892007-07-17 00:58:39 +00001238 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001239 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001240 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001241}
1242
Steve Naroff218bc2b2007-05-04 21:54:46 +00001243inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001244 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001245{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001246 QualType lhsType = lex->getType(), rhsType = rex->getType();
1247
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001248 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001249
Steve Naroffdbd9e892007-07-17 00:58:39 +00001250 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001251 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001252 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001253}
1254
1255inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001256 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001257{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001258 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001259 return CheckVectorOperands(loc, lex, rex);
1260
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001261 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff94a5aca2007-07-16 22:23:01 +00001262
Steve Naroffe4718892007-04-27 18:30:00 +00001263 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001264 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001265 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001266
Steve Naroffdbd9e892007-07-17 00:58:39 +00001267 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1268 return lex->getType();
1269 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1270 return rex->getType();
Chris Lattner5c11c412007-12-12 05:47:28 +00001271 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001272}
1273
Steve Naroff218bc2b2007-05-04 21:54:46 +00001274inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001275 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001276{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001277 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001278 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001279
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001280 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001281
Chris Lattner4d62f422007-12-09 21:53:25 +00001282 // Enforce type constraints: C99 6.5.6p3.
1283
1284 // Handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001285 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001286 return compType;
Chris Lattner4d62f422007-12-09 21:53:25 +00001287
1288 // Either ptr - int or ptr - ptr.
1289 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
1290 // The LHS must be an object type, not incomplete, function, etc.
1291 if (!LHSPTy->getPointeeType()->isObjectType()) {
1292 // Handle the GNU void* extension.
1293 if (LHSPTy->getPointeeType()->isVoidType()) {
1294 Diag(loc, diag::ext_gnu_void_ptr,
1295 lex->getSourceRange(), rex->getSourceRange());
1296 } else {
1297 Diag(loc, diag::err_typecheck_sub_ptr_object,
1298 lex->getType().getAsString(), lex->getSourceRange());
1299 return QualType();
1300 }
1301 }
1302
1303 // The result type of a pointer-int computation is the pointer type.
1304 if (rex->getType()->isIntegerType())
1305 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +00001306
Chris Lattner4d62f422007-12-09 21:53:25 +00001307 // Handle pointer-pointer subtractions.
1308 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
1309 // RHS must be an object type, unless void (GNU).
1310 if (!RHSPTy->getPointeeType()->isObjectType()) {
1311 // Handle the GNU void* extension.
1312 if (RHSPTy->getPointeeType()->isVoidType()) {
1313 if (!LHSPTy->getPointeeType()->isVoidType())
1314 Diag(loc, diag::ext_gnu_void_ptr,
1315 lex->getSourceRange(), rex->getSourceRange());
1316 } else {
1317 Diag(loc, diag::err_typecheck_sub_ptr_object,
1318 rex->getType().getAsString(), rex->getSourceRange());
1319 return QualType();
1320 }
1321 }
1322
1323 // Pointee types must be compatible.
1324 if (!Context.typesAreCompatible(LHSPTy->getPointeeType(),
1325 RHSPTy->getPointeeType())) {
1326 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1327 lex->getType().getAsString(), rex->getType().getAsString(),
1328 lex->getSourceRange(), rex->getSourceRange());
1329 return QualType();
1330 }
1331
1332 return Context.getPointerDiffType();
1333 }
1334 }
1335
Chris Lattner5c11c412007-12-12 05:47:28 +00001336 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001337}
1338
1339inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Chris Lattner5c11c412007-12-12 05:47:28 +00001340 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) {
1341 // C99 6.5.7p2: Each of the operands shall have integer type.
1342 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1343 return InvalidOperands(loc, lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001344
Chris Lattner5c11c412007-12-12 05:47:28 +00001345 // Shifts don't perform usual arithmetic conversions, they just do integer
1346 // promotions on each operand. C99 6.5.7p3
Chris Lattner3c133402007-12-13 07:28:16 +00001347 if (!isCompAssign)
1348 UsualUnaryConversions(lex);
Chris Lattner5c11c412007-12-12 05:47:28 +00001349 UsualUnaryConversions(rex);
1350
1351 // "The type of the result is that of the promoted left operand."
1352 return lex->getType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001353}
1354
Chris Lattnerb620c342007-08-26 01:18:55 +00001355inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1356 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Steve Naroff1926c832007-04-24 00:23:05 +00001357{
Chris Lattnerb620c342007-08-26 01:18:55 +00001358 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001359 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1360 UsualArithmeticConversions(lex, rex);
1361 else {
1362 UsualUnaryConversions(lex);
1363 UsualUnaryConversions(rex);
1364 }
Steve Naroff31090012007-07-16 21:54:35 +00001365 QualType lType = lex->getType();
1366 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001367
Ted Kremeneke2763b02007-10-29 17:13:39 +00001368 // For non-floating point types, check for self-comparisons of the form
1369 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1370 // often indicate logic errors in the program.
Ted Kremeneke451eae2007-10-29 16:58:49 +00001371 if (!lType->isFloatingType()) {
1372 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(IgnoreParen(lex)))
1373 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(IgnoreParen(rex)))
1374 if (DRL->getDecl() == DRR->getDecl())
1375 Diag(loc, diag::warn_selfcomparison);
1376 }
1377
Chris Lattnerb620c342007-08-26 01:18:55 +00001378 if (isRelational) {
1379 if (lType->isRealType() && rType->isRealType())
1380 return Context.IntTy;
1381 } else {
Ted Kremeneke2763b02007-10-29 17:13:39 +00001382 // Check for comparisons of floating point operands using != and ==.
Ted Kremeneke2763b02007-10-29 17:13:39 +00001383 if (lType->isFloatingType()) {
1384 assert (rType->isFloatingType());
Ted Kremenek43fb8b02007-11-25 00:58:00 +00001385 CheckFloatComparison(loc,lex,rex);
Ted Kremenekd4ecc6d2007-10-29 16:40:01 +00001386 }
1387
Chris Lattnerb620c342007-08-26 01:18:55 +00001388 if (lType->isArithmeticType() && rType->isArithmeticType())
1389 return Context.IntTy;
1390 }
Steve Naroffe4718892007-04-27 18:30:00 +00001391
Chris Lattner1895e582007-08-26 01:10:14 +00001392 bool LHSIsNull = lex->isNullPointerConstant(Context);
1393 bool RHSIsNull = rex->isNullPointerConstant(Context);
1394
Chris Lattnerb620c342007-08-26 01:18:55 +00001395 // All of the following pointer related warnings are GCC extensions, except
1396 // when handling null pointer constants. One day, we can consider making them
1397 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001398 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Steve Naroffb6666252007-11-13 14:57:38 +00001399
1400 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
1401 !lType->getAsPointerType()->getPointeeType()->isVoidType() &&
1402 !rType->getAsPointerType()->getPointeeType()->isVoidType() &&
Steve Naroff32e44c02007-10-15 20:41:53 +00001403 !Context.pointerTypesAreCompatible(lType.getUnqualifiedType(),
1404 rType.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001405 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1406 lType.getAsString(), rType.getAsString(),
1407 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001408 }
Chris Lattner1895e582007-08-26 01:10:14 +00001409 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001410 return Context.IntTy;
1411 }
Fariborz Jahanian134cbef2007-12-20 01:06:58 +00001412 if ((lType->isObjcQualifiedIdType() || rType->isObjcQualifiedIdType())
Fariborz Jahanianff7d2bf2007-12-21 00:33:59 +00001413 && Context.ObjcQualifiedIdTypesAreCompatible(lType, rType, true)) {
Fariborz Jahanian134cbef2007-12-20 01:06:58 +00001414 promoteExprToType(rex, lType);
1415 return Context.IntTy;
1416 }
Steve Naroffcdee44c2007-08-16 21:48:38 +00001417 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001418 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001419 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1420 lType.getAsString(), rType.getAsString(),
1421 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001422 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001423 return Context.IntTy;
1424 }
1425 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001426 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001427 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1428 lType.getAsString(), rType.getAsString(),
1429 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001430 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001431 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001432 }
Chris Lattner5c11c412007-12-12 05:47:28 +00001433 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001434}
1435
Steve Naroff218bc2b2007-05-04 21:54:46 +00001436inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001437 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001438{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001439 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001440 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001441
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001442 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001443
Steve Naroffdbd9e892007-07-17 00:58:39 +00001444 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001445 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001446 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001447}
1448
Steve Naroff218bc2b2007-05-04 21:54:46 +00001449inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001450 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001451{
Steve Naroff31090012007-07-16 21:54:35 +00001452 UsualUnaryConversions(lex);
1453 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001454
Steve Naroffdbd9e892007-07-17 00:58:39 +00001455 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001456 return Context.IntTy;
Chris Lattner5c11c412007-12-12 05:47:28 +00001457 return InvalidOperands(loc, lex, rex);
Steve Naroffae4143e2007-04-26 20:39:23 +00001458}
1459
Steve Naroff35d85152007-05-07 00:24:15 +00001460inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001461 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001462{
Steve Naroff0af91202007-04-27 21:51:21 +00001463 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001464 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff9358c712007-05-27 23:58:33 +00001465 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1466
1467 switch (mlval) { // C99 6.5.16p2
Chris Lattner9bad62c2008-01-04 18:04:52 +00001468 case Expr::MLV_Valid:
1469 break;
1470 case Expr::MLV_ConstQualified:
1471 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1472 return QualType();
1473 case Expr::MLV_ArrayType:
1474 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1475 lhsType.getAsString(), lex->getSourceRange());
1476 return QualType();
1477 case Expr::MLV_NotObjectType:
1478 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1479 lhsType.getAsString(), lex->getSourceRange());
1480 return QualType();
1481 case Expr::MLV_InvalidExpression:
1482 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1483 lex->getSourceRange());
1484 return QualType();
1485 case Expr::MLV_IncompleteType:
1486 case Expr::MLV_IncompleteVoidType:
1487 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1488 lhsType.getAsString(), lex->getSourceRange());
1489 return QualType();
1490 case Expr::MLV_DuplicateVectorComponents:
1491 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1492 lex->getSourceRange());
1493 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001494 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001495
Chris Lattner9bad62c2008-01-04 18:04:52 +00001496 AssignConvertType ConvTy;
1497 if (compoundType.isNull())
1498 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1499 else
1500 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1501
1502 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1503 rex, "assigning"))
1504 return QualType();
1505
Steve Naroff98cf3e92007-06-06 18:38:38 +00001506 // C99 6.5.16p3: The type of an assignment expression is the type of the
1507 // left operand unless the left operand has qualified type, in which case
1508 // it is the unqualified version of the type of the left operand.
1509 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1510 // is converted to the type of the assignment expression (above).
Chris Lattnerf17bd422007-08-30 17:45:32 +00001511 // C++ 5.17p1: the type of the assignment expression is that of its left
1512 // oprdu.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001513 return lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001514}
1515
Steve Naroff218bc2b2007-05-04 21:54:46 +00001516inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001517 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001518 UsualUnaryConversions(rex);
1519 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001520}
1521
Steve Naroff7a5af782007-07-13 16:58:59 +00001522/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1523/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001524QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001525 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001526 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001527
Steve Naroff9d139172007-08-24 17:20:07 +00001528 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff5f9ae642007-11-11 14:15:57 +00001529 if (const PointerType *pt = resType->getAsPointerType()) {
Steve Naroff35d85152007-05-07 00:24:15 +00001530 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001531 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1532 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001533 return QualType();
1534 }
Steve Naroff9d139172007-08-24 17:20:07 +00001535 } else if (!resType->isRealType()) {
1536 if (resType->isComplexType())
1537 // C99 does not support ++/-- on complex types.
1538 Diag(OpLoc, diag::ext_integer_increment_complex,
1539 resType.getAsString(), op->getSourceRange());
1540 else {
1541 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1542 resType.getAsString(), op->getSourceRange());
1543 return QualType();
1544 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001545 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001546 // At this point, we know we have a real, complex or pointer type.
1547 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001548 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1549 if (mlval != Expr::MLV_Valid) {
1550 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001551 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001552 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001553 return QualType();
1554 }
1555 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001556}
1557
Steve Naroff1926c832007-04-24 00:23:05 +00001558/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001559/// This routine allows us to typecheck complex/recursive expressions
1560/// where the declaration is needed for type checking. Here are some
1561/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001562static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001563 switch (e->getStmtClass()) {
1564 case Stmt::DeclRefExprClass:
1565 return cast<DeclRefExpr>(e)->getDecl();
1566 case Stmt::MemberExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001567 // Fields cannot be declared with a 'register' storage class.
1568 // &X->f is always ok, even if X is declared register.
1569 if (cast<MemberExpr>(e)->isArrow())
1570 return 0;
Steve Naroff1926c832007-04-24 00:23:05 +00001571 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001572 case Stmt::ArraySubscriptExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001573 // &X[4] and &4[X] is invalid if X is invalid.
Steve Naroff1926c832007-04-24 00:23:05 +00001574 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001575 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001576 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001577 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001578 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Chris Lattner48d52842007-11-16 17:46:48 +00001579 case Stmt::ImplicitCastExprClass:
1580 // &X[4] when X is an array, has an implicit cast from array to pointer.
1581 return getPrimaryDeclaration(cast<ImplicitCastExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001582 default:
1583 return 0;
1584 }
1585}
1586
1587/// CheckAddressOfOperand - The operand of & must be either a function
1588/// designator or an lvalue designating an object. If it is an lvalue, the
1589/// object cannot be declared with storage class register or be a bit field.
1590/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001591/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001592QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001593 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001594 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001595
Steve Naroff9358c712007-05-27 23:58:33 +00001596 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattner48d52842007-11-16 17:46:48 +00001597 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1598 // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001599 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1600 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001601 return QualType();
1602 }
Steve Naroff47500512007-04-19 23:00:49 +00001603 } else if (dcl) {
1604 // We have an lvalue with a decl. Make sure the decl is not declared
1605 // with the register storage-class specifier.
1606 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001607 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001608 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001609 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001610 return QualType();
1611 }
Steve Narofff633d092007-04-25 19:01:39 +00001612 } else
1613 assert(0 && "Unknown/unexpected decl type");
1614
Steve Naroff47500512007-04-19 23:00:49 +00001615 // FIXME: add check for bitfields!
1616 }
1617 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001618 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001619}
1620
Steve Naroff35d85152007-05-07 00:24:15 +00001621QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001622 UsualUnaryConversions(op);
1623 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001624
Chris Lattnerc996b172007-07-31 16:53:04 +00001625 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001626 QualType ptype = PT->getPointeeType();
1627 // C99 6.5.3.2p4. "if it points to an object,...".
1628 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1629 // GCC compat: special case 'void *' (treat as warning).
1630 if (ptype->isVoidType()) {
1631 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001632 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001633 } else {
1634 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001635 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001636 return QualType();
1637 }
1638 }
1639 return ptype;
1640 }
1641 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001642 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001643 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001644}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001645
1646static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1647 tok::TokenKind Kind) {
1648 BinaryOperator::Opcode Opc;
1649 switch (Kind) {
1650 default: assert(0 && "Unknown binop!");
1651 case tok::star: Opc = BinaryOperator::Mul; break;
1652 case tok::slash: Opc = BinaryOperator::Div; break;
1653 case tok::percent: Opc = BinaryOperator::Rem; break;
1654 case tok::plus: Opc = BinaryOperator::Add; break;
1655 case tok::minus: Opc = BinaryOperator::Sub; break;
1656 case tok::lessless: Opc = BinaryOperator::Shl; break;
1657 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1658 case tok::lessequal: Opc = BinaryOperator::LE; break;
1659 case tok::less: Opc = BinaryOperator::LT; break;
1660 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1661 case tok::greater: Opc = BinaryOperator::GT; break;
1662 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1663 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1664 case tok::amp: Opc = BinaryOperator::And; break;
1665 case tok::caret: Opc = BinaryOperator::Xor; break;
1666 case tok::pipe: Opc = BinaryOperator::Or; break;
1667 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1668 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1669 case tok::equal: Opc = BinaryOperator::Assign; break;
1670 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1671 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1672 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1673 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1674 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1675 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1676 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1677 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1678 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1679 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1680 case tok::comma: Opc = BinaryOperator::Comma; break;
1681 }
1682 return Opc;
1683}
1684
Steve Naroff35d85152007-05-07 00:24:15 +00001685static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1686 tok::TokenKind Kind) {
1687 UnaryOperator::Opcode Opc;
1688 switch (Kind) {
1689 default: assert(0 && "Unknown unary op!");
1690 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1691 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1692 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1693 case tok::star: Opc = UnaryOperator::Deref; break;
1694 case tok::plus: Opc = UnaryOperator::Plus; break;
1695 case tok::minus: Opc = UnaryOperator::Minus; break;
1696 case tok::tilde: Opc = UnaryOperator::Not; break;
1697 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1698 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1699 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1700 case tok::kw___real: Opc = UnaryOperator::Real; break;
1701 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001702 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001703 }
1704 return Opc;
1705}
1706
Steve Naroff218bc2b2007-05-04 21:54:46 +00001707// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001708Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Steve Naroff218bc2b2007-05-04 21:54:46 +00001709 ExprTy *LHS, ExprTy *RHS) {
1710 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1711 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1712
Steve Naroff83895f72007-09-16 03:34:24 +00001713 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1714 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Steve Naroff218bc2b2007-05-04 21:54:46 +00001715
Chris Lattner256c21b2007-06-28 03:53:10 +00001716 QualType ResultTy; // Result type of the binary operator.
1717 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001718
1719 switch (Opc) {
1720 default:
1721 assert(0 && "Unknown binary expr!");
1722 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001723 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001724 break;
1725 case BinaryOperator::Mul:
1726 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001727 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001728 break;
1729 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001730 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001731 break;
1732 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001733 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001734 break;
1735 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001736 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001737 break;
1738 case BinaryOperator::Shl:
1739 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001740 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001741 break;
1742 case BinaryOperator::LE:
1743 case BinaryOperator::LT:
1744 case BinaryOperator::GE:
1745 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001746 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001747 break;
1748 case BinaryOperator::EQ:
1749 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001750 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001751 break;
1752 case BinaryOperator::And:
1753 case BinaryOperator::Xor:
1754 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001755 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001756 break;
1757 case BinaryOperator::LAnd:
1758 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001759 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001760 break;
1761 case BinaryOperator::MulAssign:
1762 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001763 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001764 if (!CompTy.isNull())
1765 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001766 break;
1767 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001768 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001769 if (!CompTy.isNull())
1770 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001771 break;
1772 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001773 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001774 if (!CompTy.isNull())
1775 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001776 break;
1777 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001778 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001779 if (!CompTy.isNull())
1780 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001781 break;
1782 case BinaryOperator::ShlAssign:
1783 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001784 CompTy = CheckShiftOperands(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::AndAssign:
1789 case BinaryOperator::XorAssign:
1790 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001791 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001792 if (!CompTy.isNull())
1793 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001794 break;
1795 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001796 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001797 break;
1798 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001799 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001800 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001801 if (CompTy.isNull())
Chris Lattnerc11005f2007-08-28 18:36:55 +00001802 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner256c21b2007-06-28 03:53:10 +00001803 else
Chris Lattnerc11005f2007-08-28 18:36:55 +00001804 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001805}
1806
Steve Naroff35d85152007-05-07 00:24:15 +00001807// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001808Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001809 ExprTy *input) {
1810 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001811 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1812 QualType resultType;
1813 switch (Opc) {
1814 default:
1815 assert(0 && "Unimplemented unary expr!");
1816 case UnaryOperator::PreInc:
1817 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001818 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001819 break;
1820 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001821 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001822 break;
1823 case UnaryOperator::Deref:
Steve Naroffb7235642007-12-18 04:06:57 +00001824 DefaultFunctionArrayConversion(Input);
Chris Lattner86554282007-06-08 22:32:33 +00001825 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001826 break;
1827 case UnaryOperator::Plus:
1828 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001829 UsualUnaryConversions(Input);
1830 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001831 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001832 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1833 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001834 break;
1835 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001836 UsualUnaryConversions(Input);
1837 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00001838 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1839 if (!resultType->isIntegerType()) {
1840 if (resultType->isComplexType())
1841 // C99 does not support '~' for complex conjugation.
1842 Diag(OpLoc, diag::ext_integer_complement_complex,
1843 resultType.getAsString());
1844 else
1845 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1846 resultType.getAsString());
1847 }
Steve Naroff35d85152007-05-07 00:24:15 +00001848 break;
1849 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001850 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001851 DefaultFunctionArrayConversion(Input);
1852 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001853 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001854 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1855 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001856 // LNot always has type int. C99 6.5.3.3p5.
1857 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001858 break;
1859 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001860 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001861 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001862 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001863 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1864 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00001865 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00001866 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00001867 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00001868 break;
Chris Lattner86554282007-06-08 22:32:33 +00001869 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00001870 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001871 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001872 }
1873 if (resultType.isNull())
1874 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001875 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001876}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001877
Steve Naroff66356bd2007-09-16 14:56:35 +00001878/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1879Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattnereefa10e2007-05-28 06:56:27 +00001880 SourceLocation LabLoc,
1881 IdentifierInfo *LabelII) {
1882 // Look up the record for this label identifier.
1883 LabelStmt *&LabelDecl = LabelMap[LabelII];
1884
1885 // If we haven't seen this label yet, create a forward reference.
1886 if (LabelDecl == 0)
1887 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1888
1889 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001890 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1891 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001892}
1893
Steve Naroff66356bd2007-09-16 14:56:35 +00001894Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner366727f2007-07-24 16:58:17 +00001895 SourceLocation RPLoc) { // "({..})"
1896 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1897 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1898 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1899
1900 // FIXME: there are a variety of strange constraints to enforce here, for
1901 // example, it is not possible to goto into a stmt expression apparently.
1902 // More semantic analysis is needed.
1903
1904 // FIXME: the last statement in the compount stmt has its value used. We
1905 // should not warn about it being unused.
1906
1907 // If there are sub stmts in the compound stmt, take the type of the last one
1908 // as the type of the stmtexpr.
1909 QualType Ty = Context.VoidTy;
1910
1911 if (!Compound->body_empty())
1912 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1913 Ty = LastExpr->getType();
1914
1915 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1916}
Steve Naroff78864672007-08-01 22:05:33 +00001917
Steve Naroff66356bd2007-09-16 14:56:35 +00001918Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattnerf17bd422007-08-30 17:45:32 +00001919 SourceLocation TypeLoc,
1920 TypeTy *argty,
1921 OffsetOfComponent *CompPtr,
1922 unsigned NumComponents,
1923 SourceLocation RPLoc) {
1924 QualType ArgTy = QualType::getFromOpaquePtr(argty);
1925 assert(!ArgTy.isNull() && "Missing type argument!");
1926
1927 // We must have at least one component that refers to the type, and the first
1928 // one is known to be a field designator. Verify that the ArgTy represents
1929 // a struct/union/class.
1930 if (!ArgTy->isRecordType())
1931 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
1932
1933 // Otherwise, create a compound literal expression as the base, and
1934 // iteratively process the offsetof designators.
Chris Lattner266a2ff2008-01-02 21:46:24 +00001935 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0);
Chris Lattnerf17bd422007-08-30 17:45:32 +00001936
Chris Lattner78502cf2007-08-31 21:49:13 +00001937 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
1938 // GCC extension, diagnose them.
1939 if (NumComponents != 1)
1940 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
1941 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
1942
Chris Lattnerf17bd422007-08-30 17:45:32 +00001943 for (unsigned i = 0; i != NumComponents; ++i) {
1944 const OffsetOfComponent &OC = CompPtr[i];
1945 if (OC.isBrackets) {
1946 // Offset of an array sub-field. TODO: Should we allow vector elements?
1947 const ArrayType *AT = Res->getType()->getAsArrayType();
1948 if (!AT) {
1949 delete Res;
1950 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
1951 Res->getType().getAsString());
1952 }
1953
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001954 // FIXME: C++: Verify that operator[] isn't overloaded.
1955
Chris Lattnerf17bd422007-08-30 17:45:32 +00001956 // C99 6.5.2.1p1
1957 Expr *Idx = static_cast<Expr*>(OC.U.E);
1958 if (!Idx->getType()->isIntegerType())
1959 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
1960 Idx->getSourceRange());
1961
1962 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
1963 continue;
1964 }
1965
1966 const RecordType *RC = Res->getType()->getAsRecordType();
1967 if (!RC) {
1968 delete Res;
1969 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
1970 Res->getType().getAsString());
1971 }
1972
1973 // Get the decl corresponding to this.
1974 RecordDecl *RD = RC->getDecl();
1975 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
1976 if (!MemberDecl)
1977 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
1978 OC.U.IdentInfo->getName(),
1979 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001980
1981 // FIXME: C++: Verify that MemberDecl isn't a static field.
1982 // FIXME: Verify that MemberDecl isn't a bitfield.
1983
Chris Lattnerf17bd422007-08-30 17:45:32 +00001984 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
1985 }
1986
1987 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
1988 BuiltinLoc);
1989}
1990
1991
Steve Naroff66356bd2007-09-16 14:56:35 +00001992Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00001993 TypeTy *arg1, TypeTy *arg2,
1994 SourceLocation RPLoc) {
1995 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1996 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1997
1998 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1999
Chris Lattnerf17bd422007-08-30 17:45:32 +00002000 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00002001}
2002
Steve Naroff66356bd2007-09-16 14:56:35 +00002003Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff9efdabc2007-08-03 21:21:27 +00002004 ExprTy *expr1, ExprTy *expr2,
2005 SourceLocation RPLoc) {
2006 Expr *CondExpr = static_cast<Expr*>(cond);
2007 Expr *LHSExpr = static_cast<Expr*>(expr1);
2008 Expr *RHSExpr = static_cast<Expr*>(expr2);
2009
2010 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2011
2012 // The conditional expression is required to be a constant expression.
2013 llvm::APSInt condEval(32);
2014 SourceLocation ExpLoc;
2015 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2016 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2017 CondExpr->getSourceRange());
2018
2019 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2020 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2021 RHSExpr->getType();
2022 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2023}
2024
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002025Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2026 ExprTy *expr, TypeTy *type,
Chris Lattner9bad62c2008-01-04 18:04:52 +00002027 SourceLocation RPLoc) {
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002028 Expr *E = static_cast<Expr*>(expr);
2029 QualType T = QualType::getFromOpaquePtr(type);
2030
2031 InitBuiltinVaListType();
2032
Chris Lattner9bad62c2008-01-04 18:04:52 +00002033 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2034 != Compatible)
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002035 return Diag(E->getLocStart(),
2036 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2037 E->getType().getAsString(),
2038 E->getSourceRange());
2039
2040 // FIXME: Warn if a non-POD type is passed in.
2041
2042 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2043}
2044
Anders Carlsson76f4a902007-08-21 17:43:55 +00002045// TODO: Move this to SemaObjC.cpp
Chris Lattnere002fbe2007-12-12 01:04:12 +00002046Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
2047 ExprTy **Strings,
2048 unsigned NumStrings) {
Chris Lattnere002fbe2007-12-12 01:04:12 +00002049 SourceLocation AtLoc = AtLocs[0];
2050 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
Fariborz Jahanian4d1288a2007-12-12 23:55:49 +00002051 if (NumStrings > 1) {
2052 // Concatenate objc strings.
2053 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
2054 SourceLocation EndLoc = ES->getSourceRange().getEnd();
2055 unsigned Length = 0;
2056 for (unsigned i = 0; i < NumStrings; i++)
2057 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
2058 char *strBuf = new char [Length];
2059 char *p = strBuf;
2060 bool isWide = false;
2061 for (unsigned i = 0; i < NumStrings; i++) {
2062 S = static_cast<StringLiteral *>(Strings[i]);
2063 if (S->isWide())
2064 isWide = true;
2065 memcpy(p, S->getStrData(), S->getByteLength());
2066 p += S->getByteLength();
2067 delete S;
2068 }
2069 S = new StringLiteral(strBuf, Length,
2070 isWide, Context.getPointerType(Context.CharTy),
2071 AtLoc, EndLoc);
2072 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002073
2074 if (CheckBuiltinCFStringArgument(S))
2075 return true;
2076
Steve Narofff73b7842007-10-15 23:35:17 +00002077 if (Context.getObjcConstantStringInterface().isNull()) {
2078 // Initialize the constant string interface lazily. This assumes
2079 // the NSConstantString interface is seen in this translation unit.
2080 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
2081 ScopedDecl *IFace = LookupScopedDecl(NSIdent, Decl::IDNS_Ordinary,
2082 SourceLocation(), TUScope);
Steve Naroff69849552007-10-16 00:00:18 +00002083 ObjcInterfaceDecl *strIFace = dyn_cast_or_null<ObjcInterfaceDecl>(IFace);
Steve Naroff684dc412007-10-18 23:53:51 +00002084 if (!strIFace)
2085 return Diag(S->getLocStart(), diag::err_undef_interface,
2086 NSIdent->getName());
Steve Naroff69849552007-10-16 00:00:18 +00002087 Context.setObjcConstantStringInterface(strIFace);
Steve Narofff73b7842007-10-15 23:35:17 +00002088 }
2089 QualType t = Context.getObjcConstantStringInterface();
Anders Carlsson76f4a902007-08-21 17:43:55 +00002090 t = Context.getPointerType(t);
Steve Naroffa397efd2007-11-03 11:27:19 +00002091 return new ObjCStringLiteral(S, t, AtLoc);
Anders Carlsson76f4a902007-08-21 17:43:55 +00002092}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002093
2094Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
Chris Lattner37390be2007-10-16 22:51:17 +00002095 SourceLocation EncodeLoc,
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002096 SourceLocation LParenLoc,
2097 TypeTy *Ty,
2098 SourceLocation RParenLoc) {
2099 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
2100
2101 QualType t = Context.getPointerType(Context.CharTy);
2102 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
2103}
Steve Narofff0f2afc2007-09-17 21:01:15 +00002104
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002105Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
2106 SourceLocation AtLoc,
Fariborz Jahanian6bd1d612007-10-16 23:21:02 +00002107 SourceLocation SelLoc,
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002108 SourceLocation LParenLoc,
2109 SourceLocation RParenLoc) {
Steve Naroff6d40db02007-10-31 18:42:27 +00002110 QualType t = Context.getObjcSelType();
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002111 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
2112}
2113
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002114Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
2115 SourceLocation AtLoc,
2116 SourceLocation ProtoLoc,
2117 SourceLocation LParenLoc,
2118 SourceLocation RParenLoc) {
2119 ObjcProtocolDecl* PDecl = ObjcProtocols[ProtocolId];
2120 if (!PDecl) {
2121 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
2122 return true;
2123 }
2124
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +00002125 QualType t = Context.getObjcProtoType();
Fariborz Jahaniane183a822007-10-18 22:59:23 +00002126 if (t.isNull())
2127 return true;
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +00002128 t = Context.getPointerType(t);
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002129 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
2130}
Steve Naroff077c83b2007-10-16 23:12:48 +00002131
Chris Lattner9bad62c2008-01-04 18:04:52 +00002132bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2133 SourceLocation Loc,
2134 QualType DstType, QualType SrcType,
2135 Expr *SrcExpr, const char *Flavor) {
2136 // Decode the result (notice that AST's are still created for extensions).
2137 bool isInvalid = false;
2138 unsigned DiagKind;
2139 switch (ConvTy) {
2140 default: assert(0 && "Unknown conversion type");
2141 case Compatible: return false;
2142 case PointerInt:
2143 DiagKind = diag::ext_typecheck_convert_pointer_int;
2144 break;
2145 case IncompatiblePointer:
2146 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2147 break;
2148 case FunctionVoidPointer:
2149 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2150 break;
2151 case CompatiblePointerDiscardsQualifiers:
2152 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2153 break;
2154 case Incompatible:
2155 DiagKind = diag::err_typecheck_convert_incompatible;
2156 isInvalid = true;
2157 break;
2158 }
2159
2160 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2161 SrcExpr->getSourceRange());
2162 return isInvalid;
2163}
2164
Steve Naroff077c83b2007-10-16 23:12:48 +00002165bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
2166 ObjcMethodDecl *Method) {
2167 bool anyIncompatibleArgs = false;
2168
2169 for (unsigned i = 0; i < NumArgs; i++) {
2170 Expr *argExpr = Args[i];
2171 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
2172
2173 QualType lhsType = Method->getParamDecl(i)->getType();
2174 QualType rhsType = argExpr->getType();
2175
2176 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
2177 if (const ArrayType *ary = lhsType->getAsArrayType())
2178 lhsType = Context.getPointerType(ary->getElementType());
2179 else if (lhsType->isFunctionType())
2180 lhsType = Context.getPointerType(lhsType);
2181
Chris Lattner9bad62c2008-01-04 18:04:52 +00002182 AssignConvertType Result = CheckSingleAssignmentConstraints(lhsType,
2183 argExpr);
Steve Naroff077c83b2007-10-16 23:12:48 +00002184 if (Args[i] != argExpr) // The expression was converted.
2185 Args[i] = argExpr; // Make sure we store the converted expression.
Chris Lattner9bad62c2008-01-04 18:04:52 +00002186
2187 anyIncompatibleArgs |=
2188 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
2189 argExpr, "sending");
Steve Naroff077c83b2007-10-16 23:12:48 +00002190 }
2191 return anyIncompatibleArgs;
2192}
2193
Steve Narofff73590d2007-09-27 14:38:14 +00002194// ActOnClassMessage - used for both unary and keyword messages.
2195// ArgExprs is optional - if it is present, the number of expressions
2196// is obtained from Sel.getNumArgs().
2197Sema::ExprResult Sema::ActOnClassMessage(
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002198 Scope *S,
Steve Naroffc6814ea2007-10-02 20:01:56 +00002199 IdentifierInfo *receiverName, Selector Sel,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002200 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
Steve Narofff0f2afc2007-09-17 21:01:15 +00002201{
Steve Naroffc6814ea2007-10-02 20:01:56 +00002202 assert(receiverName && "missing receiver class name");
Steve Naroffd54978b2007-09-18 23:55:05 +00002203
Steve Naroff077c83b2007-10-16 23:12:48 +00002204 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002205 ObjcInterfaceDecl* ClassDecl = 0;
2206 if (!strcmp(receiverName->getName(), "super") && CurMethodDecl) {
2207 ClassDecl = CurMethodDecl->getClassInterface()->getSuperClass();
Fariborz Jahanian2d0d3ba2007-11-12 20:20:37 +00002208 if (ClassDecl && CurMethodDecl->isInstance()) {
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002209 // Synthesize a cast to the super class. This hack allows us to loosely
2210 // represent super without creating a special expression node.
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002211 IdentifierInfo &II = Context.Idents.get("self");
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002212 ExprResult ReceiverExpr = ActOnIdentifierExpr(S, lbrac, II, false);
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002213 QualType superTy = Context.getObjcInterfaceType(ClassDecl);
2214 superTy = Context.getPointerType(superTy);
2215 ReceiverExpr = ActOnCastExpr(SourceLocation(), superTy.getAsOpaquePtr(),
2216 SourceLocation(), ReceiverExpr.Val);
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002217 // We are really in an instance method, redirect.
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002218 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002219 Args, NumArgs);
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002220 }
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002221 // We are sending a message to 'super' within a class method. Do nothing,
2222 // the receiver will pass through as 'super' (how convenient:-).
2223 } else
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002224 ClassDecl = getObjCInterfaceDecl(receiverName);
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002225
2226 // FIXME: can ClassDecl ever be null?
Steve Naroffc6814ea2007-10-02 20:01:56 +00002227 ObjcMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
Steve Naroff55f52da2007-10-16 20:39:36 +00002228 QualType returnType;
Steve Naroff96d20612007-11-05 15:27:52 +00002229
2230 // Before we give up, check if the selector is an instance method.
2231 if (!Method)
2232 Method = ClassDecl->lookupInstanceMethod(Sel);
Steve Naroff55f52da2007-10-16 20:39:36 +00002233 if (!Method) {
2234 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
2235 SourceRange(lbrac, rbrac));
Steve Naroff6d40db02007-10-31 18:42:27 +00002236 returnType = Context.getObjcIdType();
Steve Naroff55f52da2007-10-16 20:39:36 +00002237 } else {
Steve Naroffd2754262007-10-16 21:36:54 +00002238 returnType = Method->getResultType();
Steve Naroff077c83b2007-10-16 23:12:48 +00002239 if (Sel.getNumArgs()) {
2240 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2241 return true;
2242 }
Steve Naroff55f52da2007-10-16 20:39:36 +00002243 }
Steve Naroff66697aa2007-11-03 16:37:59 +00002244 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002245 lbrac, rbrac, ArgExprs, NumArgs);
Steve Narofff0f2afc2007-09-17 21:01:15 +00002246}
2247
Steve Narofff73590d2007-09-27 14:38:14 +00002248// ActOnInstanceMessage - used for both unary and keyword messages.
2249// ArgExprs is optional - if it is present, the number of expressions
2250// is obtained from Sel.getNumArgs().
2251Sema::ExprResult Sema::ActOnInstanceMessage(
Steve Naroff80175062007-09-28 22:22:11 +00002252 ExprTy *receiver, Selector Sel,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002253 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
Steve Narofff73590d2007-09-27 14:38:14 +00002254{
Steve Naroffd54978b2007-09-18 23:55:05 +00002255 assert(receiver && "missing receiver expression");
2256
Steve Naroff077c83b2007-10-16 23:12:48 +00002257 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Steve Naroffd54978b2007-09-18 23:55:05 +00002258 Expr *RExpr = static_cast<Expr *>(receiver);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002259 QualType receiverType = RExpr->getType();
Steve Naroff7f549f12007-10-10 21:53:07 +00002260 QualType returnType;
Fariborz Jahanian243c6112008-01-03 20:01:35 +00002261 ObjcMethodDecl *Method = 0;
Steve Naroff7f549f12007-10-10 21:53:07 +00002262
Steve Naroff49effde2007-11-11 17:52:25 +00002263 if (receiverType == Context.getObjcIdType() ||
2264 receiverType == Context.getObjcClassType()) {
Steve Naroff66697aa2007-11-03 16:37:59 +00002265 Method = InstanceMethodPool[Sel].Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00002266 if (!Method)
2267 Method = FactoryMethodPool[Sel].Method;
Steve Naroff55f52da2007-10-16 20:39:36 +00002268 if (!Method) {
2269 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
2270 SourceRange(lbrac, rbrac));
Steve Naroff6d40db02007-10-31 18:42:27 +00002271 returnType = Context.getObjcIdType();
Steve Naroff55f52da2007-10-16 20:39:36 +00002272 } else {
Steve Naroffd2754262007-10-16 21:36:54 +00002273 returnType = Method->getResultType();
Steve Naroff077c83b2007-10-16 23:12:48 +00002274 if (Sel.getNumArgs())
2275 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2276 return true;
Steve Naroff55f52da2007-10-16 20:39:36 +00002277 }
Steve Naroff7f549f12007-10-10 21:53:07 +00002278 } else {
Fariborz Jahanian775d5d02008-01-04 00:27:46 +00002279 bool receiverIsQualId = dyn_cast<ObjcQualifiedIdType>(receiverType) != 0;
Chris Lattner9c7a0362007-10-10 23:42:28 +00002280 // FIXME (snaroff): checking in this code from Patrick. Needs to be
2281 // revisited. how do we get the ClassDecl from the receiver expression?
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00002282 if (!receiverIsQualId)
2283 while (receiverType->isPointerType()) {
2284 PointerType *pointerType =
2285 static_cast<PointerType*>(receiverType.getTypePtr());
2286 receiverType = pointerType->getPointeeType();
2287 }
Fariborz Jahanian243c6112008-01-03 20:01:35 +00002288 ObjcInterfaceDecl* ClassDecl = 0;
Fariborz Jahanianff6a4552007-12-07 21:21:21 +00002289 if (ObjcQualifiedInterfaceType *QIT =
2290 dyn_cast<ObjcQualifiedInterfaceType>(receiverType)) {
Fariborz Jahanianc47dc4f2007-12-13 20:47:42 +00002291 ClassDecl = QIT->getDecl();
Fariborz Jahanianff6a4552007-12-07 21:21:21 +00002292 Method = ClassDecl->lookupInstanceMethod(Sel);
2293 if (!Method) {
2294 // search protocols
2295 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
2296 ObjcProtocolDecl *PDecl = QIT->getProtocols(i);
2297 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
2298 break;
2299 }
2300 }
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00002301 if (!Method)
2302 Diag(lbrac, diag::warn_method_not_found_in_protocol,
2303 std::string("-"), Sel.getName(),
2304 SourceRange(lbrac, rbrac));
2305 }
2306 else if (ObjcQualifiedIdType *QIT =
2307 dyn_cast<ObjcQualifiedIdType>(receiverType)) {
2308 // search protocols
2309 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
2310 ObjcProtocolDecl *PDecl = QIT->getProtocols(i);
2311 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
2312 break;
2313 }
2314 if (!Method)
2315 Diag(lbrac, diag::warn_method_not_found_in_protocol,
2316 std::string("-"), Sel.getName(),
2317 SourceRange(lbrac, rbrac));
Fariborz Jahanianff6a4552007-12-07 21:21:21 +00002318 }
2319 else {
2320 assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) &&
2321 "bad receiver type");
2322 ClassDecl = static_cast<ObjcInterfaceType*>(
2323 receiverType.getTypePtr())->getDecl();
2324 // FIXME: consider using InstanceMethodPool, since it will be faster
2325 // than the following method (which can do *many* linear searches). The
2326 // idea is to add class info to InstanceMethodPool...
2327 Method = ClassDecl->lookupInstanceMethod(Sel);
2328 }
Steve Naroff55f52da2007-10-16 20:39:36 +00002329 if (!Method) {
Steve Naroff22e078e2007-11-11 00:10:47 +00002330 // If we have an implementation in scope, check "private" methods.
Fariborz Jahanian243c6112008-01-03 20:01:35 +00002331 if (ClassDecl)
2332 if (ObjcImplementationDecl *ImpDecl =
Steve Naroff22e078e2007-11-11 00:10:47 +00002333 ObjcImplementations[ClassDecl->getIdentifier()])
Fariborz Jahanian243c6112008-01-03 20:01:35 +00002334 Method = ImpDecl->getInstanceMethod(Sel);
Steve Naroff5cace622007-12-11 03:38:03 +00002335 // If we still haven't found a method, look in the global pool. This
2336 // behavior isn't very desirable, however we need it for GCC compatibility.
Steve Naroff888f33c2007-12-07 20:41:14 +00002337 if (!Method)
2338 Method = InstanceMethodPool[Sel].Method;
Steve Naroff22e078e2007-11-11 00:10:47 +00002339 }
2340 if (!Method) {
Steve Naroff55f52da2007-10-16 20:39:36 +00002341 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
2342 SourceRange(lbrac, rbrac));
Steve Naroff6d40db02007-10-31 18:42:27 +00002343 returnType = Context.getObjcIdType();
Steve Naroff55f52da2007-10-16 20:39:36 +00002344 } else {
Steve Naroffd2754262007-10-16 21:36:54 +00002345 returnType = Method->getResultType();
Steve Naroff077c83b2007-10-16 23:12:48 +00002346 if (Sel.getNumArgs())
2347 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2348 return true;
Steve Naroff55f52da2007-10-16 20:39:36 +00002349 }
Steve Naroffc6814ea2007-10-02 20:01:56 +00002350 }
Steve Naroff66697aa2007-11-03 16:37:59 +00002351 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002352 ArgExprs, NumArgs);
Steve Narofff0f2afc2007-09-17 21:01:15 +00002353}