blob: 6cabbda8e2e28609d3e0e1490bdc4463ac05bf8e [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000015#include "clang/AST/ASTContext.h"
Chris Lattner17ed4872006-11-20 04:58:19 +000016#include "clang/AST/Decl.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000017#include "clang/AST/Expr.h"
18#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000019#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000021#include "clang/Basic/Diagnostic.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000022#include "clang/Basic/LangOptions.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000023#include "clang/Basic/TargetInfo.h"
24#include "llvm/ADT/SmallString.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026using namespace clang;
27
Steve Naroffdf7855b2007-02-21 23:46:25 +000028/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000029/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
30/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
31/// multiple tokens. However, the common case is that StringToks points to one
32/// string.
33///
34Action::ExprResult
Chris Lattner146762e2007-07-20 16:59:19 +000035Sema::ParseStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000036 assert(NumStringToks && "Must have at least one string!");
37
Steve Naroff4f88b312007-03-13 22:37:02 +000038 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
39 if (Literal.hadError)
40 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000041
Chris Lattner23b7eb62007-06-15 23:05:46 +000042 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000043 for (unsigned i = 0; i != NumStringToks; ++i)
44 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000045
46 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000047 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000048
Chris Lattner5b183d82006-11-10 05:03:26 +000049 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000050 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000051 Literal.AnyWide, t, StringToks[0].getLocation(),
52 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000053}
54
Chris Lattnere168f762006-11-10 05:29:30 +000055
Chris Lattnerac18be92006-11-20 06:49:47 +000056/// ParseIdentifierExpr - The parser read an identifier in expression context,
57/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
58/// identifier is used in an function call context.
59Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
60 IdentifierInfo &II,
61 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000062 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000063 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000064 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000065 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000066 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000067 if (HasTrailingLParen &&
68 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000069 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000070 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000071 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000072 // If this name wasn't predeclared and if this is not a function call,
73 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000074 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000075 }
Chris Lattner17ed4872006-11-20 04:58:19 +000076 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +000077 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
Steve Naroff509fe022007-05-17 01:16:00 +000078 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff46ba1eb2007-04-03 23:13:13 +000079 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000080 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
81
82 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +000083 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +000084}
Chris Lattnere168f762006-11-10 05:29:30 +000085
Anders Carlsson625bfc82007-07-21 05:21:51 +000086Sema::ExprResult Sema::ParsePreDefinedExpr(SourceLocation Loc,
87 tok::TokenKind Kind) {
88 PreDefinedExpr::IdentType IT;
89
Chris Lattnere168f762006-11-10 05:29:30 +000090 switch (Kind) {
91 default:
92 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +000093 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
Anders Carlsson625bfc82007-07-21 05:21:51 +000094 IT = PreDefinedExpr::Func;
95 break;
Chris Lattnere168f762006-11-10 05:29:30 +000096 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +000097 IT = PreDefinedExpr::Function;
98 break;
Chris Lattnere168f762006-11-10 05:29:30 +000099 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000100 IT = PreDefinedExpr::PrettyFunction;
101 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000102 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000103
104 // Pre-defined identifiers are always of type char *.
105 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000106}
107
Chris Lattner146762e2007-07-20 16:59:19 +0000108Sema::ExprResult Sema::ParseCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000109 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000110 CharBuffer.resize(Tok.getLength());
111 const char *ThisTokBegin = &CharBuffer[0];
112 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
113
114 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
115 Tok.getLocation(), PP);
116 if (Literal.hadError())
117 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000118 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
119 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000120}
121
Chris Lattner146762e2007-07-20 16:59:19 +0000122Action::ExprResult Sema::ParseNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000123 // fast path for a single digit (which is quite common). A single digit
124 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
125 if (Tok.getLength() == 1) {
126 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000127
Chris Lattner4481b422007-07-14 01:29:45 +0000128 unsigned IntSize = Context.getTypeSize(Context.IntTy, Tok.getLocation());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000129 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
130 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000131 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000132 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000133 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000134 IntegerBuffer.resize(Tok.getLength());
135 const char *ThisTokBegin = &IntegerBuffer[0];
136
Chris Lattner67ca9252007-05-21 01:08:44 +0000137 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000138 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000139 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000140 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000141 if (Literal.hadError)
142 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000143
Chris Lattner1c20a172007-08-26 03:42:43 +0000144 Expr *Res;
145
146 if (Literal.isFloatingLiteral()) {
147 // FIXME: handle float values > 32 (including compute the real type...).
148 QualType Ty = Literal.isFloat ? Context.FloatTy : Context.DoubleTy;
149 Res = new FloatingLiteral(Literal.GetFloatValue(), Ty, Tok.getLocation());
150 } else if (!Literal.isIntegerLiteral()) {
151 return ExprResult(true);
152 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000153 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000154
155 // Get the value in the widest-possible width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000156 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000157
158 if (Literal.GetIntegerValue(ResultVal)) {
159 // If this value didn't fit into uintmax_t, warn and force to ull.
160 Diag(Tok.getLocation(), diag::warn_integer_too_large);
161 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000162 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000163 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000164 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000165 // If this value fits into a ULL, try to figure out what else it fits into
166 // according to the rules of C99 6.4.4.1p5.
167
168 // Octal, Hexadecimal, and integers with a U suffix are allowed to
169 // be an unsigned int.
170 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
171
172 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner7b939cf2007-08-23 21:58:08 +0000173 if (!Literal.isLong && !Literal.isLongLong) {
174 // Are int/unsigned possibilities?
Chris Lattner4481b422007-07-14 01:29:45 +0000175 unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000176 // Does it fit in a unsigned int?
177 if (ResultVal.isIntN(IntSize)) {
178 // Does it fit in a signed int?
179 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
180 t = Context.IntTy;
181 else if (AllowUnsigned)
182 t = Context.UnsignedIntTy;
183 }
184
185 if (!t.isNull())
186 ResultVal.trunc(IntSize);
187 }
188
189 // Are long/unsigned long possibilities?
190 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner4481b422007-07-14 01:29:45 +0000191 unsigned LongSize = Context.getTypeSize(Context.LongTy,
192 Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000193
194 // Does it fit in a unsigned long?
195 if (ResultVal.isIntN(LongSize)) {
196 // Does it fit in a signed long?
197 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
198 t = Context.LongTy;
199 else if (AllowUnsigned)
200 t = Context.UnsignedLongTy;
201 }
202 if (!t.isNull())
203 ResultVal.trunc(LongSize);
204 }
205
206 // Finally, check long long if needed.
207 if (t.isNull()) {
208 unsigned LongLongSize =
Chris Lattner4481b422007-07-14 01:29:45 +0000209 Context.getTypeSize(Context.LongLongTy, Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000210
211 // Does it fit in a unsigned long long?
212 if (ResultVal.isIntN(LongLongSize)) {
213 // Does it fit in a signed long long?
214 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
215 t = Context.LongLongTy;
216 else if (AllowUnsigned)
217 t = Context.UnsignedLongLongTy;
218 }
219 }
220
221 // If we still couldn't decide a type, we probably have something that
222 // does not fit in a signed long long, but has no U suffix.
223 if (t.isNull()) {
224 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
225 t = Context.UnsignedLongLongTy;
226 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000227 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000228
Chris Lattner1c20a172007-08-26 03:42:43 +0000229 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000230 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000231
232 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
233 if (Literal.isImaginary)
234 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
235
236 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000237}
238
239Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
240 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000241 Expr *e = (Expr *)Val;
242 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000243 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000244}
245
Steve Naroff71b59a92007-06-04 22:22:31 +0000246/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000247/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000248QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
249 SourceLocation OpLoc, bool isSizeof) {
250 // C99 6.5.3.4p1:
251 if (isa<FunctionType>(exprType) && isSizeof)
252 // alignof(function) is allowed.
253 Diag(OpLoc, diag::ext_sizeof_function_type);
254 else if (exprType->isVoidType())
255 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
256 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000257 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000258 diag::err_alignof_incomplete_type,
259 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000260 return QualType(); // error
261 }
262 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
263 return Context.getSizeType();
264}
265
Chris Lattnere168f762006-11-10 05:29:30 +0000266Action::ExprResult Sema::
267ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000268 SourceLocation LPLoc, TypeTy *Ty,
269 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000270 // If error parsing type, ignore.
271 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000272
273 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000274 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000275
Steve Naroff043d45d2007-05-15 02:32:35 +0000276 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
277
278 if (resultType.isNull())
279 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000280 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000281}
282
Chris Lattner74ed76b2007-08-24 21:41:10 +0000283QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000284 DefaultFunctionArrayConversion(V);
285
Chris Lattnere267f5d2007-08-26 05:39:26 +0000286 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000287 if (const ComplexType *CT = V->getType()->getAsComplexType())
288 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000289
290 // Otherwise they pass through real integer and floating point types here.
291 if (V->getType()->isArithmeticType())
292 return V->getType();
293
294 // Reject anything else.
295 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
296 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000297}
298
299
Chris Lattnere168f762006-11-10 05:29:30 +0000300
301Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
302 tok::TokenKind Kind,
303 ExprTy *Input) {
304 UnaryOperator::Opcode Opc;
305 switch (Kind) {
306 default: assert(0 && "Unknown unary op!");
307 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
308 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
309 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000310 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000311 if (result.isNull())
312 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000313 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000314}
315
316Action::ExprResult Sema::
317ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
318 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000319 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000320
321 // Perform default conversions.
322 DefaultFunctionArrayConversion(LHSExp);
323 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000324
Chris Lattner36d572b2007-07-16 00:14:47 +0000325 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000326
Steve Naroffc1aadb12007-03-28 21:49:40 +0000327 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
328 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000329 // in the subscript position. As a result, we need to derive the array base
330 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000331 Expr *BaseExpr, *IndexExpr;
332 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000333 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000334 BaseExpr = LHSExp;
335 IndexExpr = RHSExp;
336 // FIXME: need to deal with const...
337 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000338 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000339 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000340 BaseExpr = RHSExp;
341 IndexExpr = LHSExp;
342 // FIXME: need to deal with const...
343 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000344 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
345 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000346 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000347
348 // Component access limited to variables (reject vec4.rg[1]).
349 if (!isa<DeclRefExpr>(BaseExpr))
350 return Diag(LLoc, diag::err_ocuvector_component_access,
351 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000352 // FIXME: need to deal with const...
353 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000354 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000355 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
356 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000357 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000358 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000359 if (!IndexExpr->getType()->isIntegerType())
360 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
361 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000362
Chris Lattner36d572b2007-07-16 00:14:47 +0000363 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
364 // the following check catches trying to index a pointer to a function (e.g.
365 // void (*)(int)). Functions are not objects in C99.
366 if (!ResultType->isObjectType())
367 return Diag(BaseExpr->getLocStart(),
368 diag::err_typecheck_subscript_not_object,
369 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
370
371 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000372}
373
Steve Narofff8fd09e2007-07-27 22:15:19 +0000374QualType Sema::
375CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
376 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000377 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000378
379 // The vector accessor can't exceed the number of elements.
380 const char *compStr = CompName.getName();
381 if (strlen(compStr) > vecType->getNumElements()) {
382 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
383 baseType.getAsString(), SourceRange(CompLoc));
384 return QualType();
385 }
386 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000387 if (vecType->getPointAccessorIdx(*compStr) != -1) {
388 do
389 compStr++;
390 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
391 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
392 do
393 compStr++;
394 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
395 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
396 do
397 compStr++;
398 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
399 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000400
401 if (*compStr) {
402 // We didn't get to the end of the string. This means the component names
403 // didn't come from the same set *or* we encountered an illegal name.
404 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
405 std::string(compStr,compStr+1), SourceRange(CompLoc));
406 return QualType();
407 }
408 // Each component accessor can't exceed the vector type.
409 compStr = CompName.getName();
410 while (*compStr) {
411 if (vecType->isAccessorWithinNumElements(*compStr))
412 compStr++;
413 else
414 break;
415 }
416 if (*compStr) {
417 // We didn't get to the end of the string. This means a component accessor
418 // exceeds the number of elements in the vector.
419 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
420 baseType.getAsString(), SourceRange(CompLoc));
421 return QualType();
422 }
423 // The component accessor looks fine - now we need to compute the actual type.
424 // The vector type is implied by the component accessor. For example,
425 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
426 unsigned CompSize = strlen(CompName.getName());
427 if (CompSize == 1)
428 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000429
430 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
431 // Now look up the TypeDefDecl from the vector type. Without this,
432 // diagostics look bad. We want OCU vector types to appear built-in.
433 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
434 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
435 return Context.getTypedefType(OCUVectorDecls[i]);
436 }
437 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000438}
439
Chris Lattnere168f762006-11-10 05:29:30 +0000440Action::ExprResult Sema::
441ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
442 tok::TokenKind OpKind, SourceLocation MemberLoc,
443 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000444 Expr *BaseExpr = static_cast<Expr *>(Base);
445 assert(BaseExpr && "no record expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000446
Steve Naroff185616f2007-07-26 03:11:44 +0000447 QualType BaseType = BaseExpr->getType();
448 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000449
Steve Narofff1e53692007-03-23 22:27:02 +0000450 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000451 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000452 BaseType = PT->getPointeeType();
453 else
454 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
455 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000456 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000457 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000458 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000459 RecordDecl *RDecl = RTy->getDecl();
460 if (RTy->isIncompleteType())
461 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
462 BaseExpr->getSourceRange());
463 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000464 FieldDecl *MemberDecl = RDecl->getMember(&Member);
465 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000466 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
467 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000468 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
469 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000470 // Component access limited to variables (reject vec4.rg.g).
471 if (!isa<DeclRefExpr>(BaseExpr))
472 return Diag(OpLoc, diag::err_ocuvector_component_access,
473 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000474 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
475 if (ret.isNull())
476 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000477 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Steve Naroff185616f2007-07-26 03:11:44 +0000478 } else
479 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
480 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000481}
482
483/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
484/// This provides the location of the left/right parens and a list of comma
485/// locations.
486Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000487ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
488 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000489 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000490 Expr *Fn = static_cast<Expr *>(fn);
491 Expr **Args = reinterpret_cast<Expr**>(args);
492 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000493
Chris Lattner38dbdb22007-07-21 03:03:59 +0000494 UsualUnaryConversions(Fn);
495 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000496
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000497 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
498 // type pointer to function".
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000499 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000500 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000501 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
502 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000503
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000504 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000505 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000506 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
507 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000508
Steve Naroff17f76e02007-05-03 21:03:48 +0000509 // If a prototype isn't declared, the parser implicitly defines a func decl
510 QualType resultType = funcT->getResultType();
511
512 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
513 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
514 // assignment, to the types of the corresponding parameter, ...
515
516 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000517 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000518
Steve Naroffb8c289d2007-05-08 22:18:00 +0000519 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000520 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000521 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000522 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000523 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000524 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000525 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000526 SourceRange(Args[NumArgsInProto]->getLocStart(),
527 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000528 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000529 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000530 }
531 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000532 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000533 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000534 assert(argExpr && "ParseCallExpr(): missing argument expression");
535
Steve Naroff17f76e02007-05-03 21:03:48 +0000536 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000537 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000538
Steve Naroffb8af1c22007-07-25 20:45:33 +0000539 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner41977962007-07-31 19:29:30 +0000540 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000541 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000542 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000543 lhsType = Context.getPointerType(lhsType);
544
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000545 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
546 argExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000547 if (Args[i] != argExpr) // The expression was converted.
548 Args[i] = argExpr; // Make sure we store the converted expression.
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000549 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000550
551 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000552 switch (result) {
553 case Compatible:
554 break;
555 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000556 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000557 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000558 Diag(l, diag::ext_typecheck_passing_pointer_int,
559 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000560 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000561 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000562 break;
563 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000564 Diag(l, diag::ext_typecheck_passing_pointer_int,
565 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000566 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000567 break;
568 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000569 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000570 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000571 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000572 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000573 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000574 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
575 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000576 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000577 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000578 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000579 return Diag(l, diag::err_typecheck_passing_incompatible,
580 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000581 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000582 }
583 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000584 // Even if the types checked, bail if we had the wrong number of arguments.
Chris Lattner38dbdb22007-07-21 03:03:59 +0000585 if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
Steve Naroffb8c289d2007-05-08 22:18:00 +0000586 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000587 }
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000588
589 // Do special checking on direct calls to functions.
590 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
591 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
592 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Anders Carlssona3a9c432007-08-17 15:44:17 +0000593 if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args, NumArgsInCall))
Anders Carlsson98f07902007-08-17 05:31:46 +0000594 return true;
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000595
Chris Lattner38dbdb22007-07-21 03:03:59 +0000596 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000597}
598
599Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000600ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000601 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000602 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
603 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000604 // FIXME: put back this assert when initializers are worked out.
605 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
606 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000607
608 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000609 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000610}
611
612Action::ExprResult Sema::
613ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
614 SourceLocation RParenLoc) {
615 // FIXME: add semantic analysis (C99 6.7.8). This involves
616 // knowledge of the object being intialized. As a result, the code for
617 // doing the semantic analysis will likely be located elsewhere (i.e. in
618 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
619 return false; // FIXME instantiate an InitListExpr.
620}
621
622Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000623ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
624 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000625 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
626
627 Expr *castExpr = static_cast<Expr*>(Op);
628 QualType castType = QualType::getFromOpaquePtr(Ty);
629
Chris Lattnerbd270732007-07-18 16:00:06 +0000630 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
631 // type needs to be scalar.
632 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000633 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
634 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
635 }
636 if (!castExpr->getType()->isScalarType()) {
637 return Diag(castExpr->getLocStart(),
638 diag::err_typecheck_expect_scalar_operand,
639 castExpr->getType().getAsString(), castExpr->getSourceRange());
640 }
641 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000642}
643
Steve Narofff8a28c52007-05-15 20:29:32 +0000644inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000645 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000646 UsualUnaryConversions(cond);
647 UsualUnaryConversions(lex);
648 UsualUnaryConversions(rex);
649 QualType condT = cond->getType();
650 QualType lexT = lex->getType();
651 QualType rexT = rex->getType();
652
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000653 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000654 if (!condT->isScalarType()) { // C99 6.5.15p2
655 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
656 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000657 return QualType();
658 }
659 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000660 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
661 UsualArithmeticConversions(lex, rex);
662 return lex->getType();
663 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000664 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
665 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
666
667 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
668 return lexT;
669
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000670 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000671 lexT.getAsString(), rexT.getAsString(),
672 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000673 return QualType();
674 }
675 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000676 // C99 6.5.15p3
677 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000678 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000679 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000680 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000681
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000682 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
683 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
684 // get the "pointed to" types
685 QualType lhptee = LHSPT->getPointeeType();
686 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000687
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000688 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
689 if (lhptee->isVoidType() &&
690 (rhptee->isObjectType() || rhptee->isIncompleteType()))
691 return lexT;
692 if (rhptee->isVoidType() &&
693 (lhptee->isObjectType() || lhptee->isIncompleteType()))
694 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000695
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000696 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
697 rhptee.getUnqualifiedType())) {
698 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
699 lexT.getAsString(), rexT.getAsString(),
700 lex->getSourceRange(), rex->getSourceRange());
701 return lexT; // FIXME: this is an _ext - is this return o.k?
702 }
703 // The pointer types are compatible.
704 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
705 // differently qualified versions of compatible types, the result type is a
706 // pointer to an appropriately qualified version of the *composite* type.
707 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000708 }
709 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000710
Steve Naroff7a5af782007-07-13 16:58:59 +0000711 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
712 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000713
714 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000715 lexT.getAsString(), rexT.getAsString(),
716 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000717 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000718}
719
Chris Lattnere168f762006-11-10 05:29:30 +0000720/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
721/// in the case of a the GNU conditional expr extension.
722Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
723 SourceLocation ColonLoc,
724 ExprTy *Cond, ExprTy *LHS,
725 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000726 Expr *CondExpr = (Expr *) Cond;
727 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
728 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
729 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000730 if (result.isNull())
731 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000732 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000733}
734
Steve Naroff81569d22007-07-15 02:02:06 +0000735// promoteExprToType - a helper function to ensure we create exactly one
736// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000737static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000738 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
739 impCast->setType(type);
740 else
741 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000742 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000743}
744
745/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000746void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000747 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000748 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000749
Chris Lattnercd1d0862007-07-31 16:56:34 +0000750 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000751 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
752 t = e->getType();
753 }
Steve Naroff81569d22007-07-15 02:02:06 +0000754 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000755 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000756 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000757 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000758}
759
Steve Naroff71b59a92007-06-04 22:22:31 +0000760/// UsualUnaryConversion - Performs various conversions that are common to most
761/// operators (C99 6.3). The conversions of array and function types are
762/// sometimes surpressed. For example, the array->pointer conversion doesn't
763/// apply if the array is an argument to the sizeof or address (&) operators.
764/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000765void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000766 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000767 assert(!t.isNull() && "UsualUnaryConversions - missing type");
768
Chris Lattnercd1d0862007-07-31 16:56:34 +0000769 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000770 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
771 t = expr->getType();
772 }
Steve Naroff81569d22007-07-15 02:02:06 +0000773 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000774 promoteExprToType(expr, Context.IntTy);
775 else
776 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000777}
778
Steve Naroff1926c832007-04-24 00:23:05 +0000779/// UsualArithmeticConversions - Performs various conversions that are common to
780/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
781/// routine returns the first non-arithmetic type found. The client is
782/// responsible for emitting appropriate error diagnostics.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000783QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
784 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +0000785 if (!isCompAssign) {
786 UsualUnaryConversions(lhsExpr);
787 UsualUnaryConversions(rhsExpr);
788 }
Steve Naroff94a5aca2007-07-16 22:23:01 +0000789 QualType lhs = lhsExpr->getType();
790 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000791
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000792 // If both types are identical, no conversion is needed.
793 if (lhs == rhs)
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000794 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000795
796 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
797 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000798 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000799 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000800
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000801 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000802
803 // Handle complex types first (C99 6.3.1.8p1).
804 if (lhs->isComplexType() || rhs->isComplexType()) {
805 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000806 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000807 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
808 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000809 }
810 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000811 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
812 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000813 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000814 // This handles complex/complex, complex/float, or float/complex.
815 // When both operands are complex, the shorter operand is converted to the
816 // type of the longer, and that is the type of the result. This corresponds
817 // to what is done when combining two real floating-point operands.
818 // The fun begins when size promotion occur across type domains.
819 // From H&S 6.3.4: When one operand is complex and the other is a real
820 // floating-point type, the less precise type is converted, within it's
821 // real or complex domain, to the precision of the other type. For example,
822 // when combining a "long double" with a "double _Complex", the
823 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff7af82d42007-08-27 15:30:22 +0000824 int result = Context.compareFloatingType(lhs, rhs);
825
826 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff9091ef72007-08-27 01:27:54 +0000827 QualType tMax = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
828 if (!isCompAssign) promoteExprToType(rhsExpr, tMax);
829 return tMax;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000830 }
Steve Naroff7af82d42007-08-27 15:30:22 +0000831 if (result < 0) { // The right side is bigger, convert lhs.
832 QualType tMax = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
833 if (!isCompAssign) promoteExprToType(lhsExpr, tMax);
834 return tMax;
835 }
836 // The floating point types were ranked equally.
837 if (lhs->isComplexType()) { // handle "_Complex double, double".
838 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
839 return lhs;
840 }
841 // The right side is complex, handle "double, _Complex double".
842 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
843 return rhs;
Steve Naroffbf223ba2007-04-24 20:56:26 +0000844 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000845 // Now handle "real" floating types (i.e. float, double, long double).
846 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
847 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000848 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000849 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
850 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000851 }
852 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000853 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
854 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000855 }
Steve Naroff81569d22007-07-15 02:02:06 +0000856 // We have two real floating types, float/complex combos were handled above.
857 // Convert the smaller operand to the bigger result.
Steve Naroff7af82d42007-08-27 15:30:22 +0000858 int result = Context.compareFloatingType(lhs, rhs);
859
860 if (result > 0) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000861 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
862 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000863 }
Steve Naroff7af82d42007-08-27 15:30:22 +0000864 if (result < 0) { // convert the lhs
865 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
866 return rhs;
867 }
868 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +0000869 }
Steve Naroff81569d22007-07-15 02:02:06 +0000870 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000871 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000872 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
873 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000874 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000875 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
876 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +0000877}
878
Steve Naroff3f597292007-05-11 22:18:03 +0000879// CheckPointerTypesForAssignment - This is a very tricky routine (despite
880// being closely modeled after the C99 spec:-). The odd characteristic of this
881// routine is it effectively iqnores the qualifiers on the top level pointee.
882// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
883// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000884Sema::AssignmentCheckResult
885Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000886 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000887
888 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000889 lhptee = lhsType->getAsPointerType()->getPointeeType();
890 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000891
892 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000893 lhptee = lhptee.getCanonicalType();
894 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000895
Steve Naroff98cf3e92007-06-06 18:38:38 +0000896 AssignmentCheckResult r = Compatible;
897
Steve Naroff3f597292007-05-11 22:18:03 +0000898 // C99 6.5.16.1p1: This following citation is common to constraints
899 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
900 // qualifiers of the type *pointed to* by the right;
901 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
902 rhptee.getQualifiers())
903 r = CompatiblePointerDiscardsQualifiers;
904
905 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
906 // incomplete type and the other is a pointer to a qualified or unqualified
907 // version of void...
908 if (lhptee.getUnqualifiedType()->isVoidType() &&
909 (rhptee->isObjectType() || rhptee->isIncompleteType()))
910 ;
911 else if (rhptee.getUnqualifiedType()->isVoidType() &&
912 (lhptee->isObjectType() || lhptee->isIncompleteType()))
913 ;
914 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
915 // unqualified versions of compatible types, ...
916 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
917 rhptee.getUnqualifiedType()))
918 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000919 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000920}
921
Steve Naroff98cf3e92007-06-06 18:38:38 +0000922/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000923/// has code to accommodate several GCC extensions when type checking
924/// pointers. Here are some objectionable examples that GCC considers warnings:
925///
926/// int a, *pint;
927/// short *pshort;
928/// struct foo *pfoo;
929///
930/// pint = pshort; // warning: assignment from incompatible pointer type
931/// a = pint; // warning: assignment makes integer from pointer without a cast
932/// pint = a; // warning: assignment makes pointer from integer without a cast
933/// pint = pfoo; // warning: assignment from incompatible pointer type
934///
935/// As a result, the code for dealing with pointers is more complex than the
936/// C99 spec dictates.
937/// Note: the warning above turn into errors when -pedantic-errors is enabled.
938///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000939Sema::AssignmentCheckResult
940Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000941 if (lhsType == rhsType) // common case, fast path...
942 return Compatible;
943
Steve Naroff84ff4b42007-07-09 21:31:10 +0000944 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000945 if (lhsType->isVectorType() || rhsType->isVectorType()) {
946 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
947 return Incompatible;
948 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000949 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000950 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000951 if (rhsType->isIntegerType())
952 return PointerFromInt;
953
Steve Naroff3f597292007-05-11 22:18:03 +0000954 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000955 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000956 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000957 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
958 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
959 return IntFromPointer;
960
Steve Naroff3f597292007-05-11 22:18:03 +0000961 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000962 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000963 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
964 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000965 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000966 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
967 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000968 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000969 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000970 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000971}
972
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000973Sema::AssignmentCheckResult
974Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
975 // This check seems unnatural, however it is necessary to insure the proper
976 // conversion of functions/arrays. If the conversion were done for all
977 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
978 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +0000979 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000980
981 Sema::AssignmentCheckResult result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000982
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000983 result = CheckAssignmentConstraints(lhsType, rExpr->getType());
984
985 // C99 6.5.16.1p2: The value of the right operand is converted to the
986 // type of the assignment expression.
987 if (rExpr->getType() != lhsType)
988 promoteExprToType(rExpr, lhsType);
989 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000990}
991
992Sema::AssignmentCheckResult
993Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
994 return CheckAssignmentConstraints(lhsType, rhsType);
995}
996
Steve Naroff7a5af782007-07-13 16:58:59 +0000997inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000998 Diag(loc, diag::err_typecheck_invalid_operands,
999 lex->getType().getAsString(), rex->getType().getAsString(),
1000 lex->getSourceRange(), rex->getSourceRange());
1001}
1002
Steve Naroff7a5af782007-07-13 16:58:59 +00001003inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1004 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +00001005 QualType lhsType = lex->getType(), rhsType = rex->getType();
1006
1007 // make sure the vector types are identical.
1008 if (lhsType == rhsType)
1009 return lhsType;
1010 // You cannot convert between vector values of different size.
1011 Diag(loc, diag::err_typecheck_vector_not_convertable,
1012 lex->getType().getAsString(), rex->getType().getAsString(),
1013 lex->getSourceRange(), rex->getSourceRange());
1014 return QualType();
1015}
1016
Steve Naroff218bc2b2007-05-04 21:54:46 +00001017inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001018 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001019{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001020 QualType lhsType = lex->getType(), rhsType = rex->getType();
1021
1022 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001023 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001024
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001025 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001026
Steve Naroffdbd9e892007-07-17 00:58:39 +00001027 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001028 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001029 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001030 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001031}
1032
Steve Naroff218bc2b2007-05-04 21:54:46 +00001033inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001034 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001035{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001036 QualType lhsType = lex->getType(), rhsType = rex->getType();
1037
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001038 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001039
Steve Naroffdbd9e892007-07-17 00:58:39 +00001040 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001041 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001042 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001043 return QualType();
1044}
1045
1046inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001047 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001048{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001049 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001050 return CheckVectorOperands(loc, lex, rex);
1051
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001052 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff94a5aca2007-07-16 22:23:01 +00001053
Steve Naroffe4718892007-04-27 18:30:00 +00001054 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001055 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001056 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001057
Steve Naroffdbd9e892007-07-17 00:58:39 +00001058 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1059 return lex->getType();
1060 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1061 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001062 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001063 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001064}
1065
Steve Naroff218bc2b2007-05-04 21:54:46 +00001066inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001067 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001068{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001069 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001070 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001071
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001072 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001073
1074 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001075 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001076 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001077
1078 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001079 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001080 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00001081 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001082 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001083 return QualType();
1084}
1085
1086inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001087 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001088{
Chris Lattner1d411a82007-06-05 20:52:21 +00001089 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1090 // for int << longlong -> the result type should be int, not long long.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001091 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001092
Steve Naroffdbd9e892007-07-17 00:58:39 +00001093 // handle the common case first (both operands are arithmetic).
1094 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001095 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001096 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001097 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001098}
1099
Chris Lattnerb620c342007-08-26 01:18:55 +00001100inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1101 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Steve Naroff1926c832007-04-24 00:23:05 +00001102{
Chris Lattnerb620c342007-08-26 01:18:55 +00001103 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001104 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1105 UsualArithmeticConversions(lex, rex);
1106 else {
1107 UsualUnaryConversions(lex);
1108 UsualUnaryConversions(rex);
1109 }
Steve Naroff31090012007-07-16 21:54:35 +00001110 QualType lType = lex->getType();
1111 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001112
Chris Lattnerb620c342007-08-26 01:18:55 +00001113 if (isRelational) {
1114 if (lType->isRealType() && rType->isRealType())
1115 return Context.IntTy;
1116 } else {
1117 if (lType->isArithmeticType() && rType->isArithmeticType())
1118 return Context.IntTy;
1119 }
Steve Naroffe4718892007-04-27 18:30:00 +00001120
Chris Lattner1895e582007-08-26 01:10:14 +00001121 bool LHSIsNull = lex->isNullPointerConstant(Context);
1122 bool RHSIsNull = rex->isNullPointerConstant(Context);
1123
Chris Lattnerb620c342007-08-26 01:18:55 +00001124 // All of the following pointer related warnings are GCC extensions, except
1125 // when handling null pointer constants. One day, we can consider making them
1126 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001127 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner1895e582007-08-26 01:10:14 +00001128 if (!LHSIsNull && !RHSIsNull &&
Steve Naroff808eb8f2007-08-27 04:08:11 +00001129 !Type::pointerTypesAreCompatible(lType.getUnqualifiedType(),
1130 rType.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001131 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1132 lType.getAsString(), rType.getAsString(),
1133 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001134 }
Chris Lattner1895e582007-08-26 01:10:14 +00001135 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001136 return Context.IntTy;
1137 }
1138 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001139 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001140 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1141 lType.getAsString(), rType.getAsString(),
1142 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001143 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001144 return Context.IntTy;
1145 }
1146 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001147 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001148 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1149 lType.getAsString(), rType.getAsString(),
1150 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001151 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001152 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001153 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001154 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001155 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001156}
1157
Steve Naroff218bc2b2007-05-04 21:54:46 +00001158inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001159 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001160{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001161 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001162 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001163
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001164 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001165
Steve Naroffdbd9e892007-07-17 00:58:39 +00001166 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001167 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001168 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001169 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001170}
1171
Steve Naroff218bc2b2007-05-04 21:54:46 +00001172inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001173 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001174{
Steve Naroff31090012007-07-16 21:54:35 +00001175 UsualUnaryConversions(lex);
1176 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001177
Steve Naroffdbd9e892007-07-17 00:58:39 +00001178 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001179 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001180 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001181 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001182}
1183
Steve Naroff35d85152007-05-07 00:24:15 +00001184inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001185 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001186{
Steve Naroff0af91202007-04-27 21:51:21 +00001187 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001188 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001189 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001190 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1191
1192 switch (mlval) { // C99 6.5.16p2
1193 case Expr::MLV_Valid:
1194 break;
1195 case Expr::MLV_ConstQualified:
1196 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1197 hadError = true;
1198 break;
1199 case Expr::MLV_ArrayType:
1200 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1201 lhsType.getAsString(), lex->getSourceRange());
1202 return QualType();
1203 case Expr::MLV_NotObjectType:
1204 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1205 lhsType.getAsString(), lex->getSourceRange());
1206 return QualType();
1207 case Expr::MLV_InvalidExpression:
1208 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1209 lex->getSourceRange());
1210 return QualType();
1211 case Expr::MLV_IncompleteType:
1212 case Expr::MLV_IncompleteVoidType:
1213 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1214 lhsType.getAsString(), lex->getSourceRange());
1215 return QualType();
Steve Naroff0d595ca2007-07-30 03:29:09 +00001216 case Expr::MLV_DuplicateVectorComponents:
1217 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1218 lex->getSourceRange());
1219 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001220 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001221 AssignmentCheckResult result;
1222
1223 if (compoundType.isNull())
1224 result = CheckSingleAssignmentConstraints(lhsType, rex);
1225 else
1226 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffad373bd2007-07-31 12:34:36 +00001227
Steve Naroff218bc2b2007-05-04 21:54:46 +00001228 // decode the result (notice that extensions still return a type).
1229 switch (result) {
1230 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001231 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001232 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001233 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001234 lhsType.getAsString(), rhsType.getAsString(),
1235 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001236 hadError = true;
1237 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001238 case PointerFromInt:
1239 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001240 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001241 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1242 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001243 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001244 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001245 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001246 case IntFromPointer:
1247 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1248 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001249 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001250 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001251 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001252 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1253 lhsType.getAsString(), rhsType.getAsString(),
1254 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001255 break;
1256 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001257 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1258 lhsType.getAsString(), rhsType.getAsString(),
1259 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001260 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001261 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001262 // C99 6.5.16p3: The type of an assignment expression is the type of the
1263 // left operand unless the left operand has qualified type, in which case
1264 // it is the unqualified version of the type of the left operand.
1265 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1266 // is converted to the type of the assignment expression (above).
1267 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1268 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001269}
1270
Steve Naroff218bc2b2007-05-04 21:54:46 +00001271inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001272 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001273 UsualUnaryConversions(rex);
1274 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001275}
1276
Steve Naroff7a5af782007-07-13 16:58:59 +00001277/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1278/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001279QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001280 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001281 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001282
Steve Naroff9d139172007-08-24 17:20:07 +00001283 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff35d85152007-05-07 00:24:15 +00001284 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1285 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001286 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1287 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001288 return QualType();
1289 }
Steve Naroff9d139172007-08-24 17:20:07 +00001290 } else if (!resType->isRealType()) {
1291 if (resType->isComplexType())
1292 // C99 does not support ++/-- on complex types.
1293 Diag(OpLoc, diag::ext_integer_increment_complex,
1294 resType.getAsString(), op->getSourceRange());
1295 else {
1296 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1297 resType.getAsString(), op->getSourceRange());
1298 return QualType();
1299 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001300 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001301 // At this point, we know we have a real, complex or pointer type.
1302 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001303 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1304 if (mlval != Expr::MLV_Valid) {
1305 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001306 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001307 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001308 return QualType();
1309 }
1310 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001311}
1312
Steve Naroff1926c832007-04-24 00:23:05 +00001313/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001314/// This routine allows us to typecheck complex/recursive expressions
1315/// where the declaration is needed for type checking. Here are some
1316/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001317static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001318 switch (e->getStmtClass()) {
1319 case Stmt::DeclRefExprClass:
1320 return cast<DeclRefExpr>(e)->getDecl();
1321 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001322 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001323 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001324 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001325 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001326 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001327 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001328 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001329 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001330 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001331 default:
1332 return 0;
1333 }
1334}
1335
1336/// CheckAddressOfOperand - The operand of & must be either a function
1337/// designator or an lvalue designating an object. If it is an lvalue, the
1338/// object cannot be declared with storage class register or be a bit field.
1339/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001340/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001341QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001342 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001343 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001344
Steve Naroff9358c712007-05-27 23:58:33 +00001345 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001346 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1347 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001348 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001349 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1350 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001351 return QualType();
1352 }
Steve Naroff47500512007-04-19 23:00:49 +00001353 } else if (dcl) {
1354 // We have an lvalue with a decl. Make sure the decl is not declared
1355 // with the register storage-class specifier.
1356 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001357 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001358 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001359 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001360 return QualType();
1361 }
Steve Narofff633d092007-04-25 19:01:39 +00001362 } else
1363 assert(0 && "Unknown/unexpected decl type");
1364
Steve Naroff47500512007-04-19 23:00:49 +00001365 // FIXME: add check for bitfields!
1366 }
1367 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001368 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001369}
1370
Steve Naroff35d85152007-05-07 00:24:15 +00001371QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001372 UsualUnaryConversions(op);
1373 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001374
Chris Lattnerc996b172007-07-31 16:53:04 +00001375 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001376 QualType ptype = PT->getPointeeType();
1377 // C99 6.5.3.2p4. "if it points to an object,...".
1378 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1379 // GCC compat: special case 'void *' (treat as warning).
1380 if (ptype->isVoidType()) {
1381 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001382 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001383 } else {
1384 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001385 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001386 return QualType();
1387 }
1388 }
1389 return ptype;
1390 }
1391 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001392 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001393 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001394}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001395
1396static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1397 tok::TokenKind Kind) {
1398 BinaryOperator::Opcode Opc;
1399 switch (Kind) {
1400 default: assert(0 && "Unknown binop!");
1401 case tok::star: Opc = BinaryOperator::Mul; break;
1402 case tok::slash: Opc = BinaryOperator::Div; break;
1403 case tok::percent: Opc = BinaryOperator::Rem; break;
1404 case tok::plus: Opc = BinaryOperator::Add; break;
1405 case tok::minus: Opc = BinaryOperator::Sub; break;
1406 case tok::lessless: Opc = BinaryOperator::Shl; break;
1407 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1408 case tok::lessequal: Opc = BinaryOperator::LE; break;
1409 case tok::less: Opc = BinaryOperator::LT; break;
1410 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1411 case tok::greater: Opc = BinaryOperator::GT; break;
1412 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1413 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1414 case tok::amp: Opc = BinaryOperator::And; break;
1415 case tok::caret: Opc = BinaryOperator::Xor; break;
1416 case tok::pipe: Opc = BinaryOperator::Or; break;
1417 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1418 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1419 case tok::equal: Opc = BinaryOperator::Assign; break;
1420 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1421 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1422 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1423 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1424 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1425 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1426 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1427 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1428 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1429 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1430 case tok::comma: Opc = BinaryOperator::Comma; break;
1431 }
1432 return Opc;
1433}
1434
Steve Naroff35d85152007-05-07 00:24:15 +00001435static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1436 tok::TokenKind Kind) {
1437 UnaryOperator::Opcode Opc;
1438 switch (Kind) {
1439 default: assert(0 && "Unknown unary op!");
1440 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1441 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1442 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1443 case tok::star: Opc = UnaryOperator::Deref; break;
1444 case tok::plus: Opc = UnaryOperator::Plus; break;
1445 case tok::minus: Opc = UnaryOperator::Minus; break;
1446 case tok::tilde: Opc = UnaryOperator::Not; break;
1447 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1448 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1449 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1450 case tok::kw___real: Opc = UnaryOperator::Real; break;
1451 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001452 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001453 }
1454 return Opc;
1455}
1456
Steve Naroff218bc2b2007-05-04 21:54:46 +00001457// Binary Operators. 'Tok' is the token for the operator.
1458Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1459 ExprTy *LHS, ExprTy *RHS) {
1460 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1461 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1462
1463 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1464 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1465
Chris Lattner256c21b2007-06-28 03:53:10 +00001466 QualType ResultTy; // Result type of the binary operator.
1467 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001468
1469 switch (Opc) {
1470 default:
1471 assert(0 && "Unknown binary expr!");
1472 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001473 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001474 break;
1475 case BinaryOperator::Mul:
1476 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001477 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001478 break;
1479 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001480 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001481 break;
1482 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001483 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001484 break;
1485 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001486 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001487 break;
1488 case BinaryOperator::Shl:
1489 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001490 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001491 break;
1492 case BinaryOperator::LE:
1493 case BinaryOperator::LT:
1494 case BinaryOperator::GE:
1495 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001496 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001497 break;
1498 case BinaryOperator::EQ:
1499 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001500 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001501 break;
1502 case BinaryOperator::And:
1503 case BinaryOperator::Xor:
1504 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001505 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001506 break;
1507 case BinaryOperator::LAnd:
1508 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001509 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001510 break;
1511 case BinaryOperator::MulAssign:
1512 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001513 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001514 if (!CompTy.isNull())
1515 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001516 break;
1517 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001518 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001519 if (!CompTy.isNull())
1520 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001521 break;
1522 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001523 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001524 if (!CompTy.isNull())
1525 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001526 break;
1527 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001528 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001529 if (!CompTy.isNull())
1530 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001531 break;
1532 case BinaryOperator::ShlAssign:
1533 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001534 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001535 if (!CompTy.isNull())
1536 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001537 break;
1538 case BinaryOperator::AndAssign:
1539 case BinaryOperator::XorAssign:
1540 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001541 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001542 if (!CompTy.isNull())
1543 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001544 break;
1545 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001546 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001547 break;
1548 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001549 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001550 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001551 if (CompTy.isNull())
1552 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1553 else
Chris Lattner9369a562007-06-29 16:31:29 +00001554 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001555}
1556
Steve Naroff35d85152007-05-07 00:24:15 +00001557// Unary Operators. 'Tok' is the token for the operator.
1558Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001559 ExprTy *input) {
1560 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001561 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1562 QualType resultType;
1563 switch (Opc) {
1564 default:
1565 assert(0 && "Unimplemented unary expr!");
1566 case UnaryOperator::PreInc:
1567 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001568 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001569 break;
1570 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001571 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001572 break;
1573 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001574 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001575 break;
1576 case UnaryOperator::Plus:
1577 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001578 UsualUnaryConversions(Input);
1579 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001580 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001581 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1582 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001583 break;
1584 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001585 UsualUnaryConversions(Input);
1586 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00001587 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1588 if (!resultType->isIntegerType()) {
1589 if (resultType->isComplexType())
1590 // C99 does not support '~' for complex conjugation.
1591 Diag(OpLoc, diag::ext_integer_complement_complex,
1592 resultType.getAsString());
1593 else
1594 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1595 resultType.getAsString());
1596 }
Steve Naroff35d85152007-05-07 00:24:15 +00001597 break;
1598 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001599 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001600 DefaultFunctionArrayConversion(Input);
1601 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001602 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001603 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1604 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001605 // LNot always has type int. C99 6.5.3.3p5.
1606 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001607 break;
1608 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001609 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001610 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001611 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001612 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1613 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00001614 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00001615 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00001616 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00001617 break;
Chris Lattner86554282007-06-08 22:32:33 +00001618 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00001619 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001620 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001621 }
1622 if (resultType.isNull())
1623 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001624 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001625}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001626
1627/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1628Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1629 SourceLocation LabLoc,
1630 IdentifierInfo *LabelII) {
1631 // Look up the record for this label identifier.
1632 LabelStmt *&LabelDecl = LabelMap[LabelII];
1633
1634 // If we haven't seen this label yet, create a forward reference.
1635 if (LabelDecl == 0)
1636 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1637
1638 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001639 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1640 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001641}
1642
Chris Lattner366727f2007-07-24 16:58:17 +00001643Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
1644 SourceLocation RPLoc) { // "({..})"
1645 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1646 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1647 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1648
1649 // FIXME: there are a variety of strange constraints to enforce here, for
1650 // example, it is not possible to goto into a stmt expression apparently.
1651 // More semantic analysis is needed.
1652
1653 // FIXME: the last statement in the compount stmt has its value used. We
1654 // should not warn about it being unused.
1655
1656 // If there are sub stmts in the compound stmt, take the type of the last one
1657 // as the type of the stmtexpr.
1658 QualType Ty = Context.VoidTy;
1659
1660 if (!Compound->body_empty())
1661 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1662 Ty = LastExpr->getType();
1663
1664 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1665}
Steve Naroff78864672007-08-01 22:05:33 +00001666
Steve Naroff788d8642007-08-01 23:45:51 +00001667Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00001668 TypeTy *arg1, TypeTy *arg2,
1669 SourceLocation RPLoc) {
1670 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1671 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1672
1673 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1674
Steve Naroff788d8642007-08-01 23:45:51 +00001675 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2, RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00001676}
1677
Steve Naroff9efdabc2007-08-03 21:21:27 +00001678Sema::ExprResult Sema::ParseChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
1679 ExprTy *expr1, ExprTy *expr2,
1680 SourceLocation RPLoc) {
1681 Expr *CondExpr = static_cast<Expr*>(cond);
1682 Expr *LHSExpr = static_cast<Expr*>(expr1);
1683 Expr *RHSExpr = static_cast<Expr*>(expr2);
1684
1685 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
1686
1687 // The conditional expression is required to be a constant expression.
1688 llvm::APSInt condEval(32);
1689 SourceLocation ExpLoc;
1690 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
1691 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
1692 CondExpr->getSourceRange());
1693
1694 // If the condition is > zero, then the AST type is the same as the LSHExpr.
1695 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
1696 RHSExpr->getType();
1697 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
1698}
1699
Anders Carlsson76f4a902007-08-21 17:43:55 +00001700// TODO: Move this to SemaObjC.cpp
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001701Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001702 StringLiteral* S = static_cast<StringLiteral *>(string);
1703
1704 if (CheckBuiltinCFStringArgument(S))
1705 return true;
1706
1707 QualType t = Context.getCFConstantStringType();
1708 t = t.getQualifiedType(QualType::Const);
1709 t = Context.getPointerType(t);
1710
1711 return new ObjCStringLiteral(S, t);
1712}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001713
1714Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
1715 SourceLocation LParenLoc,
1716 TypeTy *Ty,
1717 SourceLocation RParenLoc) {
1718 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
1719
1720 QualType t = Context.getPointerType(Context.CharTy);
1721 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
1722}