blob: 955ac683e38a8abf53e458b2e8160da7dab096f6 [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 Lattner5b183d82006-11-10 05:03:26 +000025using namespace clang;
26
Steve Naroffdf7855b2007-02-21 23:46:25 +000027/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000028/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
29/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
30/// multiple tokens. However, the common case is that StringToks points to one
31/// string.
32///
33Action::ExprResult
Chris Lattner146762e2007-07-20 16:59:19 +000034Sema::ParseStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000035 assert(NumStringToks && "Must have at least one string!");
36
Steve Naroff4f88b312007-03-13 22:37:02 +000037 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
38 if (Literal.hadError)
39 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000040
Chris Lattner23b7eb62007-06-15 23:05:46 +000041 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000042 for (unsigned i = 0; i != NumStringToks; ++i)
43 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000044
45 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000046 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000047
Chris Lattner5b183d82006-11-10 05:03:26 +000048 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000049 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000050 Literal.AnyWide, t, StringToks[0].getLocation(),
51 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000052}
53
Chris Lattnere168f762006-11-10 05:29:30 +000054
Chris Lattnerac18be92006-11-20 06:49:47 +000055/// ParseIdentifierExpr - The parser read an identifier in expression context,
56/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
57/// identifier is used in an function call context.
58Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
59 IdentifierInfo &II,
60 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000061 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000062 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000063 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000064 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000065 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000066 if (HasTrailingLParen &&
67 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000068 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000069 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000070 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000071 // If this name wasn't predeclared and if this is not a function call,
72 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000073 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000074 }
Chris Lattner17ed4872006-11-20 04:58:19 +000075 }
76
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
Steve Naroff09ef4742007-03-09 23:16:33 +0000144 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000145 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000146
147 // Get the value in the widest-possible width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000148 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000149
150 if (Literal.GetIntegerValue(ResultVal)) {
151 // If this value didn't fit into uintmax_t, warn and force to ull.
152 Diag(Tok.getLocation(), diag::warn_integer_too_large);
153 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000154 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000155 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000156 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000157 // If this value fits into a ULL, try to figure out what else it fits into
158 // according to the rules of C99 6.4.4.1p5.
159
160 // Octal, Hexadecimal, and integers with a U suffix are allowed to
161 // be an unsigned int.
162 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
163
164 // Check from smallest to largest, picking the smallest type we can.
165 if (!Literal.isLong) { // Are int/unsigned possibilities?
Chris Lattner4481b422007-07-14 01:29:45 +0000166 unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000167 // Does it fit in a unsigned int?
168 if (ResultVal.isIntN(IntSize)) {
169 // Does it fit in a signed int?
170 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
171 t = Context.IntTy;
172 else if (AllowUnsigned)
173 t = Context.UnsignedIntTy;
174 }
175
176 if (!t.isNull())
177 ResultVal.trunc(IntSize);
178 }
179
180 // Are long/unsigned long possibilities?
181 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner4481b422007-07-14 01:29:45 +0000182 unsigned LongSize = Context.getTypeSize(Context.LongTy,
183 Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000184
185 // Does it fit in a unsigned long?
186 if (ResultVal.isIntN(LongSize)) {
187 // Does it fit in a signed long?
188 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
189 t = Context.LongTy;
190 else if (AllowUnsigned)
191 t = Context.UnsignedLongTy;
192 }
193 if (!t.isNull())
194 ResultVal.trunc(LongSize);
195 }
196
197 // Finally, check long long if needed.
198 if (t.isNull()) {
199 unsigned LongLongSize =
Chris Lattner4481b422007-07-14 01:29:45 +0000200 Context.getTypeSize(Context.LongLongTy, Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000201
202 // Does it fit in a unsigned long long?
203 if (ResultVal.isIntN(LongLongSize)) {
204 // Does it fit in a signed long long?
205 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
206 t = Context.LongLongTy;
207 else if (AllowUnsigned)
208 t = Context.UnsignedLongLongTy;
209 }
210 }
211
212 // If we still couldn't decide a type, we probably have something that
213 // does not fit in a signed long long, but has no U suffix.
214 if (t.isNull()) {
215 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
216 t = Context.UnsignedLongLongTy;
217 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000218 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000219
220 return new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000221 } else if (Literal.isFloatingLiteral()) {
Steve Naroff97b9e912007-07-09 23:53:58 +0000222 // FIXME: handle float values > 32 (including compute the real type...).
223 return new FloatingLiteral(Literal.GetFloatValue(), Context.FloatTy,
224 Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000225 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000226 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000227}
228
229Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
230 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000231 Expr *e = (Expr *)Val;
232 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000233 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000234}
235
Steve Naroff71b59a92007-06-04 22:22:31 +0000236/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000237/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000238QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
239 SourceLocation OpLoc, bool isSizeof) {
240 // C99 6.5.3.4p1:
241 if (isa<FunctionType>(exprType) && isSizeof)
242 // alignof(function) is allowed.
243 Diag(OpLoc, diag::ext_sizeof_function_type);
244 else if (exprType->isVoidType())
245 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
246 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000247 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000248 diag::err_alignof_incomplete_type,
249 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000250 return QualType(); // error
251 }
252 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
253 return Context.getSizeType();
254}
255
Chris Lattnere168f762006-11-10 05:29:30 +0000256Action::ExprResult Sema::
257ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000258 SourceLocation LPLoc, TypeTy *Ty,
259 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000260 // If error parsing type, ignore.
261 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000262
263 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000264 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000265
Steve Naroff043d45d2007-05-15 02:32:35 +0000266 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
267
268 if (resultType.isNull())
269 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000270 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000271}
272
273
274Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
275 tok::TokenKind Kind,
276 ExprTy *Input) {
277 UnaryOperator::Opcode Opc;
278 switch (Kind) {
279 default: assert(0 && "Unknown unary op!");
280 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
281 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
282 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000283 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000284 if (result.isNull())
285 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000286 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000287}
288
289Action::ExprResult Sema::
290ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
291 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000292 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000293
294 // Perform default conversions.
295 DefaultFunctionArrayConversion(LHSExp);
296 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000297
Chris Lattner36d572b2007-07-16 00:14:47 +0000298 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000299
Steve Naroffc1aadb12007-03-28 21:49:40 +0000300 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
301 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000302 // in the subscript position. As a result, we need to derive the array base
303 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000304 Expr *BaseExpr, *IndexExpr;
305 QualType ResultType;
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000306 if (const PointerType *PTy = LHSTy->isPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000307 BaseExpr = LHSExp;
308 IndexExpr = RHSExp;
309 // FIXME: need to deal with const...
310 ResultType = PTy->getPointeeType();
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000311 } else if (const PointerType *PTy = RHSTy->isPointerType()) {
312 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000313 BaseExpr = RHSExp;
314 IndexExpr = LHSExp;
315 // FIXME: need to deal with const...
316 ResultType = PTy->getPointeeType();
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000317 } else if (const VectorType *VTy = LHSTy->isVectorType()) { // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000318 BaseExpr = LHSExp;
319 IndexExpr = RHSExp;
320 // FIXME: need to deal with const...
321 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000322 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000323 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
324 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000325 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000326 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000327 if (!IndexExpr->getType()->isIntegerType())
328 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
329 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000330
Chris Lattner36d572b2007-07-16 00:14:47 +0000331 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
332 // the following check catches trying to index a pointer to a function (e.g.
333 // void (*)(int)). Functions are not objects in C99.
334 if (!ResultType->isObjectType())
335 return Diag(BaseExpr->getLocStart(),
336 diag::err_typecheck_subscript_not_object,
337 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
338
339 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000340}
341
342Action::ExprResult Sema::
343ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
344 tok::TokenKind OpKind, SourceLocation MemberLoc,
345 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000346 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000347
348 assert(!qualifiedType.isNull() && "no type for member expression");
349
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000350 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000351
352 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000353 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
354 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000355 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000356 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000357 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
358 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000359 if (!isa<RecordType>(canonType))
360 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
361
362 // get the struct/union definition from the type.
363 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000364
Steve Naroff92e30f82007-04-02 22:35:25 +0000365 if (canonType->isIncompleteType())
366 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000367
Steve Naroff92e30f82007-04-02 22:35:25 +0000368 FieldDecl *MemberDecl = RD->getMember(&Member);
369 if (!MemberDecl)
370 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
371
Steve Naroff9358c712007-05-27 23:58:33 +0000372 return new MemberExpr((Expr*)Base, OpKind == tok::arrow,
373 MemberDecl, MemberLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000374}
375
376/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
377/// This provides the location of the left/right parens and a list of comma
378/// locations.
379Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000380ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
381 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000382 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000383 Expr *Fn = static_cast<Expr *>(fn);
384 Expr **Args = reinterpret_cast<Expr**>(args);
385 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000386
Chris Lattner38dbdb22007-07-21 03:03:59 +0000387 UsualUnaryConversions(Fn);
388 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000389
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000390 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
391 // type pointer to function".
Steve Naroff31090012007-07-16 21:54:35 +0000392 const PointerType *PT = dyn_cast<PointerType>(funcType);
393 if (PT == 0) PT = dyn_cast<PointerType>(funcType.getCanonicalType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000394
395 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000396 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
397 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000398
399 const FunctionType *funcT = dyn_cast<FunctionType>(PT->getPointeeType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000400 if (funcT == 0)
401 funcT = dyn_cast<FunctionType>(PT->getPointeeType().getCanonicalType());
402
403 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000404 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
405 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000406
Steve Naroff17f76e02007-05-03 21:03:48 +0000407 // If a prototype isn't declared, the parser implicitly defines a func decl
408 QualType resultType = funcT->getResultType();
409
410 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
411 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
412 // assignment, to the types of the corresponding parameter, ...
413
414 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000415 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000416
Steve Naroffb8c289d2007-05-08 22:18:00 +0000417 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000418 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000419 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000420 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000421 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000422 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000423 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000424 SourceRange(Args[NumArgsInProto]->getLocStart(),
425 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000426 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000427 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000428 }
429 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000430 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000431 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000432 assert(argExpr && "ParseCallExpr(): missing argument expression");
433
Steve Naroff17f76e02007-05-03 21:03:48 +0000434 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000435 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000436
Steve Naroffb8af1c22007-07-25 20:45:33 +0000437 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000438 if (const ArrayType *ary = lhsType->isArrayType())
439 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000440 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000441 lhsType = Context.getPointerType(lhsType);
442
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000443 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
444 argExpr);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000445 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000446
447 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000448 switch (result) {
449 case Compatible:
450 break;
451 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000452 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000453 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000454 Diag(l, diag::ext_typecheck_passing_pointer_int,
455 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000456 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000457 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000458 break;
459 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000460 Diag(l, diag::ext_typecheck_passing_pointer_int,
461 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000462 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000463 break;
464 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000465 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000466 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000467 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000468 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000469 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000470 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
471 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000472 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000473 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000474 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000475 return Diag(l, diag::err_typecheck_passing_incompatible,
476 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000477 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000478 }
479 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000480 // Even if the types checked, bail if we had the wrong number of arguments.
Chris Lattner38dbdb22007-07-21 03:03:59 +0000481 if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
Steve Naroffb8c289d2007-05-08 22:18:00 +0000482 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000483 }
Chris Lattner38dbdb22007-07-21 03:03:59 +0000484 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000485}
486
487Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000488ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000489 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000490 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
491 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000492 // FIXME: put back this assert when initializers are worked out.
493 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
494 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000495
496 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000497 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000498}
499
500Action::ExprResult Sema::
501ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
502 SourceLocation RParenLoc) {
503 // FIXME: add semantic analysis (C99 6.7.8). This involves
504 // knowledge of the object being intialized. As a result, the code for
505 // doing the semantic analysis will likely be located elsewhere (i.e. in
506 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
507 return false; // FIXME instantiate an InitListExpr.
508}
509
510Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000511ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
512 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000513 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
514
515 Expr *castExpr = static_cast<Expr*>(Op);
516 QualType castType = QualType::getFromOpaquePtr(Ty);
517
Chris Lattnerbd270732007-07-18 16:00:06 +0000518 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
519 // type needs to be scalar.
520 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000521 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
522 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
523 }
524 if (!castExpr->getType()->isScalarType()) {
525 return Diag(castExpr->getLocStart(),
526 diag::err_typecheck_expect_scalar_operand,
527 castExpr->getType().getAsString(), castExpr->getSourceRange());
528 }
529 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000530}
531
Steve Narofff8a28c52007-05-15 20:29:32 +0000532inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000533 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000534 UsualUnaryConversions(cond);
535 UsualUnaryConversions(lex);
536 UsualUnaryConversions(rex);
537 QualType condT = cond->getType();
538 QualType lexT = lex->getType();
539 QualType rexT = rex->getType();
540
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000541 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000542 if (!condT->isScalarType()) { // C99 6.5.15p2
543 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
544 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000545 return QualType();
546 }
547 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000548 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
549 UsualArithmeticConversions(lex, rex);
550 return lex->getType();
551 }
Steve Naroff7a5af782007-07-13 16:58:59 +0000552 if ((lexT->isStructureType() && rexT->isStructureType()) || // C99 6.5.15p3
553 (lexT->isUnionType() && rexT->isUnionType())) {
554 TagType *lTag = cast<TagType>(lexT.getCanonicalType());
555 TagType *rTag = cast<TagType>(rexT.getCanonicalType());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000556
557 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
Steve Naroff7a5af782007-07-13 16:58:59 +0000558 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000559 else {
560 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000561 lexT.getAsString(), rexT.getAsString(),
562 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000563 return QualType();
564 }
565 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000566 // C99 6.5.15p3
567 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000568 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000569 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000570 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000571
Steve Naroff7a5af782007-07-13 16:58:59 +0000572 if (lexT->isPointerType() && rexT->isPointerType()) { // C99 6.5.15p3,6
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000573 QualType lhptee, rhptee;
574
575 // get the "pointed to" type
Steve Naroff7a5af782007-07-13 16:58:59 +0000576 lhptee = cast<PointerType>(lexT.getCanonicalType())->getPointeeType();
577 rhptee = cast<PointerType>(rexT.getCanonicalType())->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000578
579 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
580 if (lhptee.getUnqualifiedType()->isVoidType() &&
581 (rhptee->isObjectType() || rhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000582 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000583 if (rhptee.getUnqualifiedType()->isVoidType() &&
584 (lhptee->isObjectType() || lhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000585 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000586
587 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
588 // *or* to differently qualified versions of compatible types, the result
589 // type is a pointer to an appropriately qualified version of the
590 // *composite* type.
591 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
592 rhptee.getUnqualifiedType())) {
593 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
Steve Naroff7a5af782007-07-13 16:58:59 +0000594 lexT.getAsString(), rexT.getAsString(),
595 lex->getSourceRange(), rex->getSourceRange());
596 return lexT; // FIXME: this is an _ext - is this return o.k?
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000597 }
598 }
Steve Naroff7a5af782007-07-13 16:58:59 +0000599 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
600 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000601
602 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000603 lexT.getAsString(), rexT.getAsString(),
604 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000605 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000606}
607
Chris Lattnere168f762006-11-10 05:29:30 +0000608/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
609/// in the case of a the GNU conditional expr extension.
610Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
611 SourceLocation ColonLoc,
612 ExprTy *Cond, ExprTy *LHS,
613 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000614 Expr *CondExpr = (Expr *) Cond;
615 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
616 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
617 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000618 if (result.isNull())
619 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000620 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000621}
622
Steve Naroff81569d22007-07-15 02:02:06 +0000623// promoteExprToType - a helper function to ensure we create exactly one
624// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000625static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000626 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
627 impCast->setType(type);
628 else
629 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000630 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000631}
632
633/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000634void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000635 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000636 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000637
Bill Wendling89ba70e2007-07-17 05:09:22 +0000638 if (const ReferenceType *ref = t->isReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000639 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
640 t = e->getType();
641 }
Steve Naroff81569d22007-07-15 02:02:06 +0000642 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000643 promoteExprToType(e, Context.getPointerType(t));
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000644 else if (const ArrayType *ary = t->isArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000645 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000646}
647
Steve Naroff71b59a92007-06-04 22:22:31 +0000648/// UsualUnaryConversion - Performs various conversions that are common to most
649/// operators (C99 6.3). The conversions of array and function types are
650/// sometimes surpressed. For example, the array->pointer conversion doesn't
651/// apply if the array is an argument to the sizeof or address (&) operators.
652/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000653void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000654 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000655 assert(!t.isNull() && "UsualUnaryConversions - missing type");
656
Bill Wendling89ba70e2007-07-17 05:09:22 +0000657 if (const ReferenceType *ref = t->isReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000658 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
659 t = expr->getType();
660 }
Steve Naroff81569d22007-07-15 02:02:06 +0000661 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000662 promoteExprToType(expr, Context.IntTy);
663 else
664 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000665}
666
Steve Naroff1926c832007-04-24 00:23:05 +0000667/// UsualArithmeticConversions - Performs various conversions that are common to
668/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
669/// routine returns the first non-arithmetic type found. The client is
670/// responsible for emitting appropriate error diagnostics.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000671void Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr) {
Steve Naroff31090012007-07-16 21:54:35 +0000672 UsualUnaryConversions(lhsExpr);
673 UsualUnaryConversions(rhsExpr);
674
Steve Naroff94a5aca2007-07-16 22:23:01 +0000675 QualType lhs = lhsExpr->getType();
676 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000677
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000678 // If both types are identical, no conversion is needed.
679 if (lhs == rhs)
Steve Naroffdbd9e892007-07-17 00:58:39 +0000680 return;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000681
682 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
683 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000684 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
685 return;
Steve Naroff1926c832007-04-24 00:23:05 +0000686
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000687 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000688
689 // Handle complex types first (C99 6.3.1.8p1).
690 if (lhs->isComplexType() || rhs->isComplexType()) {
691 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000692 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
693 promoteExprToType(rhsExpr, lhs);
694 return;
695 }
696 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
697 promoteExprToType(lhsExpr, rhs);
698 return;
699 }
Steve Naroff81569d22007-07-15 02:02:06 +0000700 // Two complex types. Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000701 if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs
702 promoteExprToType(rhsExpr, lhs);
703 return;
704 }
705 promoteExprToType(lhsExpr, rhs); // convert the lhs
706 return;
Steve Naroffbf223ba2007-04-24 20:56:26 +0000707 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000708 // Now handle "real" floating types (i.e. float, double, long double).
709 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
710 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000711 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
712 promoteExprToType(rhsExpr, lhs);
713 return;
714 }
715 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
716 promoteExprToType(lhsExpr, rhs);
717 return;
718 }
Steve Naroff81569d22007-07-15 02:02:06 +0000719 // We have two real floating types, float/complex combos were handled above.
720 // Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000721 if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs
722 promoteExprToType(rhsExpr, lhs);
723 return;
724 }
725 promoteExprToType(lhsExpr, rhs); // convert the lhs
726 return;
Steve Naroffb01bbe32007-04-25 01:22:31 +0000727 }
Steve Naroff81569d22007-07-15 02:02:06 +0000728 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000729 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
730 promoteExprToType(rhsExpr, lhs);
731 return;
732 }
733 promoteExprToType(lhsExpr, rhs); // convert the lhs
734 return;
Steve Narofff1e53692007-03-23 22:27:02 +0000735}
736
Steve Naroff3f597292007-05-11 22:18:03 +0000737// CheckPointerTypesForAssignment - This is a very tricky routine (despite
738// being closely modeled after the C99 spec:-). The odd characteristic of this
739// routine is it effectively iqnores the qualifiers on the top level pointee.
740// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
741// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000742Sema::AssignmentCheckResult
743Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000744 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000745
746 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000747 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
748 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000749
750 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000751 lhptee = lhptee.getCanonicalType();
752 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000753
Steve Naroff98cf3e92007-06-06 18:38:38 +0000754 AssignmentCheckResult r = Compatible;
755
Steve Naroff3f597292007-05-11 22:18:03 +0000756 // C99 6.5.16.1p1: This following citation is common to constraints
757 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
758 // qualifiers of the type *pointed to* by the right;
759 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
760 rhptee.getQualifiers())
761 r = CompatiblePointerDiscardsQualifiers;
762
763 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
764 // incomplete type and the other is a pointer to a qualified or unqualified
765 // version of void...
766 if (lhptee.getUnqualifiedType()->isVoidType() &&
767 (rhptee->isObjectType() || rhptee->isIncompleteType()))
768 ;
769 else if (rhptee.getUnqualifiedType()->isVoidType() &&
770 (lhptee->isObjectType() || lhptee->isIncompleteType()))
771 ;
772 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
773 // unqualified versions of compatible types, ...
774 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
775 rhptee.getUnqualifiedType()))
776 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000777 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000778}
779
Steve Naroff98cf3e92007-06-06 18:38:38 +0000780/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000781/// has code to accommodate several GCC extensions when type checking
782/// pointers. Here are some objectionable examples that GCC considers warnings:
783///
784/// int a, *pint;
785/// short *pshort;
786/// struct foo *pfoo;
787///
788/// pint = pshort; // warning: assignment from incompatible pointer type
789/// a = pint; // warning: assignment makes integer from pointer without a cast
790/// pint = a; // warning: assignment makes pointer from integer without a cast
791/// pint = pfoo; // warning: assignment from incompatible pointer type
792///
793/// As a result, the code for dealing with pointers is more complex than the
794/// C99 spec dictates.
795/// Note: the warning above turn into errors when -pedantic-errors is enabled.
796///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000797Sema::AssignmentCheckResult
798Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000799 if (lhsType == rhsType) // common case, fast path...
800 return Compatible;
801
Steve Naroff84ff4b42007-07-09 21:31:10 +0000802 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000803 if (lhsType->isVectorType() || rhsType->isVectorType()) {
804 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
805 return Incompatible;
806 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000807 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000808 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000809 if (rhsType->isIntegerType())
810 return PointerFromInt;
811
Steve Naroff3f597292007-05-11 22:18:03 +0000812 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000813 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000814 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000815 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
816 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
817 return IntFromPointer;
818
Steve Naroff3f597292007-05-11 22:18:03 +0000819 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000820 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000821 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
822 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000823 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000824 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
825 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000826 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000827 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000828 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000829}
830
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000831Sema::AssignmentCheckResult
832Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
833 // This check seems unnatural, however it is necessary to insure the proper
834 // conversion of functions/arrays. If the conversion were done for all
835 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
836 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +0000837 DefaultFunctionArrayConversion(rExpr);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000838
Steve Naroff31090012007-07-16 21:54:35 +0000839 return CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000840}
841
842Sema::AssignmentCheckResult
843Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
844 return CheckAssignmentConstraints(lhsType, rhsType);
845}
846
Steve Naroff7a5af782007-07-13 16:58:59 +0000847inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000848 Diag(loc, diag::err_typecheck_invalid_operands,
849 lex->getType().getAsString(), rex->getType().getAsString(),
850 lex->getSourceRange(), rex->getSourceRange());
851}
852
Steve Naroff7a5af782007-07-13 16:58:59 +0000853inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
854 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000855 QualType lhsType = lex->getType(), rhsType = rex->getType();
856
857 // make sure the vector types are identical.
858 if (lhsType == rhsType)
859 return lhsType;
860 // You cannot convert between vector values of different size.
861 Diag(loc, diag::err_typecheck_vector_not_convertable,
862 lex->getType().getAsString(), rex->getType().getAsString(),
863 lex->getSourceRange(), rex->getSourceRange());
864 return QualType();
865}
866
Steve Naroff218bc2b2007-05-04 21:54:46 +0000867inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000868 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000869{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000870 QualType lhsType = lex->getType(), rhsType = rex->getType();
871
872 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000873 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +0000874
Steve Naroffdbd9e892007-07-17 00:58:39 +0000875 UsualArithmeticConversions(lex, rex);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000876
Steve Naroffdbd9e892007-07-17 00:58:39 +0000877 // handle the common case first (both operands are arithmetic).
878 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
879 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000880 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000881 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000882}
883
Steve Naroff218bc2b2007-05-04 21:54:46 +0000884inline QualType Sema::CheckRemainderOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000885 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000886{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000887 QualType lhsType = lex->getType(), rhsType = rex->getType();
888
Steve Naroffdbd9e892007-07-17 00:58:39 +0000889 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000890
Steve Naroffdbd9e892007-07-17 00:58:39 +0000891 // handle the common case first (both operands are arithmetic).
892 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
893 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000894 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000895 return QualType();
896}
897
898inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000899 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000900{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000901 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +0000902 return CheckVectorOperands(loc, lex, rex);
903
Steve Naroffdbd9e892007-07-17 00:58:39 +0000904 UsualArithmeticConversions(lex, rex);
Steve Naroff94a5aca2007-07-16 22:23:01 +0000905
Steve Naroffe4718892007-04-27 18:30:00 +0000906 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000907 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
908 return lex->getType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000909
Steve Naroffdbd9e892007-07-17 00:58:39 +0000910 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
911 return lex->getType();
912 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
913 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000914 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000915 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000916}
917
Steve Naroff218bc2b2007-05-04 21:54:46 +0000918inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000919 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000920{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000921 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000922 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000923
Steve Naroffdbd9e892007-07-17 00:58:39 +0000924 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000925
926 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000927 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
928 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000929
930 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffdbd9e892007-07-17 00:58:39 +0000931 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000932 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000933 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000934 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000935 return QualType();
936}
937
938inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff7a5af782007-07-13 16:58:59 +0000939 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000940{
Chris Lattner1d411a82007-06-05 20:52:21 +0000941 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
942 // for int << longlong -> the result type should be int, not long long.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000943 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +0000944
Steve Naroffdbd9e892007-07-17 00:58:39 +0000945 // handle the common case first (both operands are arithmetic).
946 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
947 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000948 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000949 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000950}
951
Steve Naroff218bc2b2007-05-04 21:54:46 +0000952inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
Steve Naroff7a5af782007-07-13 16:58:59 +0000953 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000954{
Steve Naroff31090012007-07-16 21:54:35 +0000955 UsualUnaryConversions(lex);
956 UsualUnaryConversions(rex);
957 QualType lType = lex->getType();
958 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000959
960 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000961 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000962
Steve Naroff75c17232007-06-13 21:41:08 +0000963 if (lType->isPointerType()) {
964 if (rType->isPointerType())
965 return Context.IntTy;
966 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000967 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000968 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
969 lex->getSourceRange(), rex->getSourceRange());
970 return Context.IntTy; // the previous diagnostic is a GCC extension.
971 }
972 } else if (rType->isPointerType()) {
973 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000974 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000975 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
976 lex->getSourceRange(), rex->getSourceRange());
977 return Context.IntTy; // the previous diagnostic is a GCC extension.
978 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000979 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000980 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000981 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000982}
983
Steve Naroff218bc2b2007-05-04 21:54:46 +0000984inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
Steve Naroff7a5af782007-07-13 16:58:59 +0000985 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000986{
Steve Naroff31090012007-07-16 21:54:35 +0000987 UsualUnaryConversions(lex);
988 UsualUnaryConversions(rex);
989 QualType lType = lex->getType();
990 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000991
992 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000993 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000994
Steve Naroff75c17232007-06-13 21:41:08 +0000995 if (lType->isPointerType()) {
996 if (rType->isPointerType())
997 return Context.IntTy;
998 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000999 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001000 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1001 lex->getSourceRange(), rex->getSourceRange());
1002 return Context.IntTy; // the previous diagnostic is a GCC extension.
1003 }
1004 } else if (rType->isPointerType()) {
1005 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001006 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001007 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1008 lex->getSourceRange(), rex->getSourceRange());
1009 return Context.IntTy; // the previous diagnostic is a GCC extension.
1010 }
Steve Naroff043d45d2007-05-15 02:32:35 +00001011 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001012 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001013 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001014}
1015
Steve Naroff218bc2b2007-05-04 21:54:46 +00001016inline QualType Sema::CheckBitwiseOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +00001017 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001018{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001019 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001020 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001021
Steve Naroffdbd9e892007-07-17 00:58:39 +00001022 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001023
Steve Naroffdbd9e892007-07-17 00:58:39 +00001024 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
1025 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001026 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001027 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001028}
1029
Steve Naroff218bc2b2007-05-04 21:54:46 +00001030inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001031 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001032{
Steve Naroff31090012007-07-16 21:54:35 +00001033 UsualUnaryConversions(lex);
1034 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001035
Steve Naroffdbd9e892007-07-17 00:58:39 +00001036 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001037 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001038 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001039 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001040}
1041
Steve Naroff35d85152007-05-07 00:24:15 +00001042inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
1043 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001044{
Steve Naroff0af91202007-04-27 21:51:21 +00001045 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001046 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001047 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001048 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1049
1050 switch (mlval) { // C99 6.5.16p2
1051 case Expr::MLV_Valid:
1052 break;
1053 case Expr::MLV_ConstQualified:
1054 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1055 hadError = true;
1056 break;
1057 case Expr::MLV_ArrayType:
1058 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1059 lhsType.getAsString(), lex->getSourceRange());
1060 return QualType();
1061 case Expr::MLV_NotObjectType:
1062 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1063 lhsType.getAsString(), lex->getSourceRange());
1064 return QualType();
1065 case Expr::MLV_InvalidExpression:
1066 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1067 lex->getSourceRange());
1068 return QualType();
1069 case Expr::MLV_IncompleteType:
1070 case Expr::MLV_IncompleteVoidType:
1071 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1072 lhsType.getAsString(), lex->getSourceRange());
1073 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001074 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001075 AssignmentCheckResult result;
1076
1077 if (compoundType.isNull())
1078 result = CheckSingleAssignmentConstraints(lhsType, rex);
1079 else
1080 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1081
Steve Naroff218bc2b2007-05-04 21:54:46 +00001082 // decode the result (notice that extensions still return a type).
1083 switch (result) {
1084 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001085 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001086 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001087 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001088 lhsType.getAsString(), rhsType.getAsString(),
1089 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001090 hadError = true;
1091 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001092 case PointerFromInt:
1093 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001094 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001095 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1096 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001097 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001098 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001099 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001100 case IntFromPointer:
1101 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1102 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001103 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001104 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001105 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001106 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1107 lhsType.getAsString(), rhsType.getAsString(),
1108 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001109 break;
1110 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001111 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1112 lhsType.getAsString(), rhsType.getAsString(),
1113 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001114 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001115 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001116 // C99 6.5.16p3: The type of an assignment expression is the type of the
1117 // left operand unless the left operand has qualified type, in which case
1118 // it is the unqualified version of the type of the left operand.
1119 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1120 // is converted to the type of the assignment expression (above).
1121 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1122 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001123}
1124
Steve Naroff218bc2b2007-05-04 21:54:46 +00001125inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001126 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001127 UsualUnaryConversions(rex);
1128 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001129}
1130
Steve Naroff7a5af782007-07-13 16:58:59 +00001131/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1132/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001133QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001134 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001135 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001136
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001137 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +00001138 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1139 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001140 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1141 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001142 return QualType();
1143 }
1144 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +00001145 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001146 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1147 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001148 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001149 }
Steve Naroff094046f2007-05-13 03:21:25 +00001150 // At this point, we know we have a real or pointer type. Now make sure
1151 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001152 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1153 if (mlval != Expr::MLV_Valid) {
1154 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001155 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001156 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001157 return QualType();
1158 }
1159 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001160}
1161
Steve Naroff1926c832007-04-24 00:23:05 +00001162/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001163/// This routine allows us to typecheck complex/recursive expressions
1164/// where the declaration is needed for type checking. Here are some
1165/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001166static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001167 switch (e->getStmtClass()) {
1168 case Stmt::DeclRefExprClass:
1169 return cast<DeclRefExpr>(e)->getDecl();
1170 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001171 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001172 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001173 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001174 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001175 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001176 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001177 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001178 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001179 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001180 default:
1181 return 0;
1182 }
1183}
1184
1185/// CheckAddressOfOperand - The operand of & must be either a function
1186/// designator or an lvalue designating an object. If it is an lvalue, the
1187/// object cannot be declared with storage class register or be a bit field.
1188/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001189/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001190QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001191 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001192 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001193
Steve Naroff9358c712007-05-27 23:58:33 +00001194 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001195 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1196 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001197 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001198 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1199 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001200 return QualType();
1201 }
Steve Naroff47500512007-04-19 23:00:49 +00001202 } else if (dcl) {
1203 // We have an lvalue with a decl. Make sure the decl is not declared
1204 // with the register storage-class specifier.
1205 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001206 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001207 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001208 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001209 return QualType();
1210 }
Steve Narofff633d092007-04-25 19:01:39 +00001211 } else
1212 assert(0 && "Unknown/unexpected decl type");
1213
Steve Naroff47500512007-04-19 23:00:49 +00001214 // FIXME: add check for bitfields!
1215 }
1216 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001217 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001218}
1219
Steve Naroff35d85152007-05-07 00:24:15 +00001220QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001221 UsualUnaryConversions(op);
1222 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001223
Steve Naroff758ada12007-05-28 16:15:57 +00001224 if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) {
1225 QualType ptype = PT->getPointeeType();
1226 // C99 6.5.3.2p4. "if it points to an object,...".
1227 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1228 // GCC compat: special case 'void *' (treat as warning).
1229 if (ptype->isVoidType()) {
1230 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001231 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001232 } else {
1233 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001234 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001235 return QualType();
1236 }
1237 }
1238 return ptype;
1239 }
1240 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001241 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001242 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001243}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001244
1245static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1246 tok::TokenKind Kind) {
1247 BinaryOperator::Opcode Opc;
1248 switch (Kind) {
1249 default: assert(0 && "Unknown binop!");
1250 case tok::star: Opc = BinaryOperator::Mul; break;
1251 case tok::slash: Opc = BinaryOperator::Div; break;
1252 case tok::percent: Opc = BinaryOperator::Rem; break;
1253 case tok::plus: Opc = BinaryOperator::Add; break;
1254 case tok::minus: Opc = BinaryOperator::Sub; break;
1255 case tok::lessless: Opc = BinaryOperator::Shl; break;
1256 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1257 case tok::lessequal: Opc = BinaryOperator::LE; break;
1258 case tok::less: Opc = BinaryOperator::LT; break;
1259 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1260 case tok::greater: Opc = BinaryOperator::GT; break;
1261 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1262 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1263 case tok::amp: Opc = BinaryOperator::And; break;
1264 case tok::caret: Opc = BinaryOperator::Xor; break;
1265 case tok::pipe: Opc = BinaryOperator::Or; break;
1266 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1267 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1268 case tok::equal: Opc = BinaryOperator::Assign; break;
1269 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1270 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1271 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1272 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1273 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1274 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1275 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1276 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1277 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1278 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1279 case tok::comma: Opc = BinaryOperator::Comma; break;
1280 }
1281 return Opc;
1282}
1283
Steve Naroff35d85152007-05-07 00:24:15 +00001284static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1285 tok::TokenKind Kind) {
1286 UnaryOperator::Opcode Opc;
1287 switch (Kind) {
1288 default: assert(0 && "Unknown unary op!");
1289 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1290 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1291 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1292 case tok::star: Opc = UnaryOperator::Deref; break;
1293 case tok::plus: Opc = UnaryOperator::Plus; break;
1294 case tok::minus: Opc = UnaryOperator::Minus; break;
1295 case tok::tilde: Opc = UnaryOperator::Not; break;
1296 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1297 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1298 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1299 case tok::kw___real: Opc = UnaryOperator::Real; break;
1300 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001301 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001302 }
1303 return Opc;
1304}
1305
Steve Naroff218bc2b2007-05-04 21:54:46 +00001306// Binary Operators. 'Tok' is the token for the operator.
1307Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1308 ExprTy *LHS, ExprTy *RHS) {
1309 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1310 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1311
1312 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1313 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1314
Chris Lattner256c21b2007-06-28 03:53:10 +00001315 QualType ResultTy; // Result type of the binary operator.
1316 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001317
1318 switch (Opc) {
1319 default:
1320 assert(0 && "Unknown binary expr!");
1321 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001322 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001323 break;
1324 case BinaryOperator::Mul:
1325 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001326 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001327 break;
1328 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001329 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001330 break;
1331 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001332 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001333 break;
1334 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001335 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001336 break;
1337 case BinaryOperator::Shl:
1338 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001339 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001340 break;
1341 case BinaryOperator::LE:
1342 case BinaryOperator::LT:
1343 case BinaryOperator::GE:
1344 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001345 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001346 break;
1347 case BinaryOperator::EQ:
1348 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001349 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001350 break;
1351 case BinaryOperator::And:
1352 case BinaryOperator::Xor:
1353 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001354 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001355 break;
1356 case BinaryOperator::LAnd:
1357 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001358 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001359 break;
1360 case BinaryOperator::MulAssign:
1361 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001362 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1363 if (!CompTy.isNull())
1364 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001365 break;
1366 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001367 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1368 if (!CompTy.isNull())
1369 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001370 break;
1371 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001372 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1373 if (!CompTy.isNull())
1374 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001375 break;
1376 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001377 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1378 if (!CompTy.isNull())
1379 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001380 break;
1381 case BinaryOperator::ShlAssign:
1382 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001383 CompTy = CheckShiftOperands(lhs, rhs, TokLoc);
1384 if (!CompTy.isNull())
1385 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001386 break;
1387 case BinaryOperator::AndAssign:
1388 case BinaryOperator::XorAssign:
1389 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001390 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1391 if (!CompTy.isNull())
1392 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001393 break;
1394 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001395 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001396 break;
1397 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001398 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001399 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001400 if (CompTy.isNull())
1401 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1402 else
Chris Lattner9369a562007-06-29 16:31:29 +00001403 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001404}
1405
Steve Naroff35d85152007-05-07 00:24:15 +00001406// Unary Operators. 'Tok' is the token for the operator.
1407Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001408 ExprTy *input) {
1409 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001410 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1411 QualType resultType;
1412 switch (Opc) {
1413 default:
1414 assert(0 && "Unimplemented unary expr!");
1415 case UnaryOperator::PreInc:
1416 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001417 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001418 break;
1419 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001420 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001421 break;
1422 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001423 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001424 break;
1425 case UnaryOperator::Plus:
1426 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001427 UsualUnaryConversions(Input);
1428 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001429 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001430 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1431 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001432 break;
1433 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001434 UsualUnaryConversions(Input);
1435 resultType = Input->getType();
Steve Naroff1c8b7d32007-07-12 21:46:55 +00001436 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1437 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1438 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001439 break;
1440 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001441 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001442 DefaultFunctionArrayConversion(Input);
1443 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001444 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001445 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1446 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001447 // LNot always has type int. C99 6.5.3.3p5.
1448 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001449 break;
1450 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001451 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001452 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001453 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001454 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1455 break;
1456 case UnaryOperator::Extension:
1457 // FIXME: does __extension__ cause any promotions? I would think not.
1458 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001459 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001460 }
1461 if (resultType.isNull())
1462 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001463 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001464}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001465
1466/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1467Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1468 SourceLocation LabLoc,
1469 IdentifierInfo *LabelII) {
1470 // Look up the record for this label identifier.
1471 LabelStmt *&LabelDecl = LabelMap[LabelII];
1472
1473 // If we haven't seen this label yet, create a forward reference.
1474 if (LabelDecl == 0)
1475 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1476
1477 // Create the AST node. The address of a label always has type 'void*'.
1478 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1479 Context.getPointerType(Context.VoidTy));
1480}
1481
Chris Lattner366727f2007-07-24 16:58:17 +00001482Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
1483 SourceLocation RPLoc) { // "({..})"
1484 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1485 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1486 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1487
1488 // FIXME: there are a variety of strange constraints to enforce here, for
1489 // example, it is not possible to goto into a stmt expression apparently.
1490 // More semantic analysis is needed.
1491
1492 // FIXME: the last statement in the compount stmt has its value used. We
1493 // should not warn about it being unused.
1494
1495 // If there are sub stmts in the compound stmt, take the type of the last one
1496 // as the type of the stmtexpr.
1497 QualType Ty = Context.VoidTy;
1498
1499 if (!Compound->body_empty())
1500 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1501 Ty = LastExpr->getType();
1502
1503 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1504}