blob: 00838f79c1b9cc73c1694c9cb67380c78dfd882e [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 Naroff185616f2007-07-26 03:11:44 +0000346 Expr *BaseExpr = static_cast<Expr *>(Base);
347 assert(BaseExpr && "no record expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000348
Steve Naroff185616f2007-07-26 03:11:44 +0000349 QualType BaseType = BaseExpr->getType();
350 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000351
Steve Narofff1e53692007-03-23 22:27:02 +0000352 if (OpKind == tok::arrow) {
Steve Naroff185616f2007-07-26 03:11:44 +0000353 if (const PointerType *PT = BaseType->isPointerType())
354 BaseType = PT->getPointeeType();
355 else
356 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
357 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000358 }
Steve Naroff185616f2007-07-26 03:11:44 +0000359 // Get the member decl from the struct/union definition.
360 FieldDecl *MemberDecl;
361 if (const RecordType *RTy = BaseType->isRecordType()) {
362 RecordDecl *RDecl = RTy->getDecl();
363 if (RTy->isIncompleteType())
364 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
365 BaseExpr->getSourceRange());
366 // The record definition is complete, now make sure the member is valid.
367 if (!(MemberDecl = RDecl->getMember(&Member)))
368 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
369 SourceRange(MemberLoc));
370 } else
371 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
372 SourceRange(MemberLoc));
Steve Naroff92e30f82007-04-02 22:35:25 +0000373
Steve Naroff185616f2007-07-26 03:11:44 +0000374 return new MemberExpr(BaseExpr, OpKind == tok::arrow, MemberDecl, MemberLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000375}
376
377/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
378/// This provides the location of the left/right parens and a list of comma
379/// locations.
380Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000381ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
382 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000383 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000384 Expr *Fn = static_cast<Expr *>(fn);
385 Expr **Args = reinterpret_cast<Expr**>(args);
386 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000387
Chris Lattner38dbdb22007-07-21 03:03:59 +0000388 UsualUnaryConversions(Fn);
389 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000390
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000391 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
392 // type pointer to function".
Steve Naroff31090012007-07-16 21:54:35 +0000393 const PointerType *PT = dyn_cast<PointerType>(funcType);
394 if (PT == 0) PT = dyn_cast<PointerType>(funcType.getCanonicalType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000395
396 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000397 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
398 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000399
400 const FunctionType *funcT = dyn_cast<FunctionType>(PT->getPointeeType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000401 if (funcT == 0)
402 funcT = dyn_cast<FunctionType>(PT->getPointeeType().getCanonicalType());
403
404 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000405 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
406 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000407
Steve Naroff17f76e02007-05-03 21:03:48 +0000408 // If a prototype isn't declared, the parser implicitly defines a func decl
409 QualType resultType = funcT->getResultType();
410
411 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
412 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
413 // assignment, to the types of the corresponding parameter, ...
414
415 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000416 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000417
Steve Naroffb8c289d2007-05-08 22:18:00 +0000418 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000419 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000420 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000421 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000422 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000423 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000424 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000425 SourceRange(Args[NumArgsInProto]->getLocStart(),
426 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000427 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000428 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000429 }
430 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000431 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000432 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000433 assert(argExpr && "ParseCallExpr(): missing argument expression");
434
Steve Naroff17f76e02007-05-03 21:03:48 +0000435 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000436 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000437
Steve Naroffb8af1c22007-07-25 20:45:33 +0000438 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000439 if (const ArrayType *ary = lhsType->isArrayType())
440 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000441 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000442 lhsType = Context.getPointerType(lhsType);
443
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000444 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
445 argExpr);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000446 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000447
448 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000449 switch (result) {
450 case Compatible:
451 break;
452 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000453 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000454 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000455 Diag(l, diag::ext_typecheck_passing_pointer_int,
456 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000457 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000458 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000459 break;
460 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000461 Diag(l, diag::ext_typecheck_passing_pointer_int,
462 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000463 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000464 break;
465 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000466 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000467 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000468 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000469 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000470 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000471 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
472 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000473 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000474 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000475 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000476 return Diag(l, diag::err_typecheck_passing_incompatible,
477 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000478 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000479 }
480 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000481 // Even if the types checked, bail if we had the wrong number of arguments.
Chris Lattner38dbdb22007-07-21 03:03:59 +0000482 if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
Steve Naroffb8c289d2007-05-08 22:18:00 +0000483 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000484 }
Chris Lattner38dbdb22007-07-21 03:03:59 +0000485 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000486}
487
488Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000489ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000490 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000491 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
492 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000493 // FIXME: put back this assert when initializers are worked out.
494 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
495 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000496
497 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000498 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000499}
500
501Action::ExprResult Sema::
502ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
503 SourceLocation RParenLoc) {
504 // FIXME: add semantic analysis (C99 6.7.8). This involves
505 // knowledge of the object being intialized. As a result, the code for
506 // doing the semantic analysis will likely be located elsewhere (i.e. in
507 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
508 return false; // FIXME instantiate an InitListExpr.
509}
510
511Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000512ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
513 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000514 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
515
516 Expr *castExpr = static_cast<Expr*>(Op);
517 QualType castType = QualType::getFromOpaquePtr(Ty);
518
Chris Lattnerbd270732007-07-18 16:00:06 +0000519 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
520 // type needs to be scalar.
521 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000522 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
523 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
524 }
525 if (!castExpr->getType()->isScalarType()) {
526 return Diag(castExpr->getLocStart(),
527 diag::err_typecheck_expect_scalar_operand,
528 castExpr->getType().getAsString(), castExpr->getSourceRange());
529 }
530 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000531}
532
Steve Narofff8a28c52007-05-15 20:29:32 +0000533inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000534 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000535 UsualUnaryConversions(cond);
536 UsualUnaryConversions(lex);
537 UsualUnaryConversions(rex);
538 QualType condT = cond->getType();
539 QualType lexT = lex->getType();
540 QualType rexT = rex->getType();
541
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000542 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000543 if (!condT->isScalarType()) { // C99 6.5.15p2
544 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
545 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000546 return QualType();
547 }
548 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000549 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
550 UsualArithmeticConversions(lex, rex);
551 return lex->getType();
552 }
Steve Naroff7a5af782007-07-13 16:58:59 +0000553 if ((lexT->isStructureType() && rexT->isStructureType()) || // C99 6.5.15p3
554 (lexT->isUnionType() && rexT->isUnionType())) {
555 TagType *lTag = cast<TagType>(lexT.getCanonicalType());
556 TagType *rTag = cast<TagType>(rexT.getCanonicalType());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000557
558 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
Steve Naroff7a5af782007-07-13 16:58:59 +0000559 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000560 else {
561 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000562 lexT.getAsString(), rexT.getAsString(),
563 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000564 return QualType();
565 }
566 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000567 // C99 6.5.15p3
568 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000569 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000570 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000571 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000572
Steve Naroff7a5af782007-07-13 16:58:59 +0000573 if (lexT->isPointerType() && rexT->isPointerType()) { // C99 6.5.15p3,6
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000574 QualType lhptee, rhptee;
575
576 // get the "pointed to" type
Steve Naroff7a5af782007-07-13 16:58:59 +0000577 lhptee = cast<PointerType>(lexT.getCanonicalType())->getPointeeType();
578 rhptee = cast<PointerType>(rexT.getCanonicalType())->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000579
580 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
581 if (lhptee.getUnqualifiedType()->isVoidType() &&
582 (rhptee->isObjectType() || rhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000583 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000584 if (rhptee.getUnqualifiedType()->isVoidType() &&
585 (lhptee->isObjectType() || lhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000586 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000587
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000588 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
589 rhptee.getUnqualifiedType())) {
590 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
Steve Naroff7a5af782007-07-13 16:58:59 +0000591 lexT.getAsString(), rexT.getAsString(),
592 lex->getSourceRange(), rex->getSourceRange());
593 return lexT; // FIXME: this is an _ext - is this return o.k?
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000594 }
Steve Naroff49ab9772007-07-26 14:35:56 +0000595 // The pointer types are compatible.
596 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
597 // differently qualified versions of compatible types, the result type is a
598 // pointer to an appropriately qualified version of the *composite* type.
599 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000600 }
Steve Naroff7a5af782007-07-13 16:58:59 +0000601 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
602 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000603
604 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000605 lexT.getAsString(), rexT.getAsString(),
606 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000607 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000608}
609
Chris Lattnere168f762006-11-10 05:29:30 +0000610/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
611/// in the case of a the GNU conditional expr extension.
612Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
613 SourceLocation ColonLoc,
614 ExprTy *Cond, ExprTy *LHS,
615 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000616 Expr *CondExpr = (Expr *) Cond;
617 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
618 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
619 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000620 if (result.isNull())
621 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000622 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000623}
624
Steve Naroff81569d22007-07-15 02:02:06 +0000625// promoteExprToType - a helper function to ensure we create exactly one
626// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000627static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000628 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
629 impCast->setType(type);
630 else
631 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000632 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000633}
634
635/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000636void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000637 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000638 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000639
Bill Wendling89ba70e2007-07-17 05:09:22 +0000640 if (const ReferenceType *ref = t->isReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000641 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
642 t = e->getType();
643 }
Steve Naroff81569d22007-07-15 02:02:06 +0000644 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000645 promoteExprToType(e, Context.getPointerType(t));
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000646 else if (const ArrayType *ary = t->isArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000647 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000648}
649
Steve Naroff71b59a92007-06-04 22:22:31 +0000650/// UsualUnaryConversion - Performs various conversions that are common to most
651/// operators (C99 6.3). The conversions of array and function types are
652/// sometimes surpressed. For example, the array->pointer conversion doesn't
653/// apply if the array is an argument to the sizeof or address (&) operators.
654/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000655void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000656 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000657 assert(!t.isNull() && "UsualUnaryConversions - missing type");
658
Bill Wendling89ba70e2007-07-17 05:09:22 +0000659 if (const ReferenceType *ref = t->isReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000660 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
661 t = expr->getType();
662 }
Steve Naroff81569d22007-07-15 02:02:06 +0000663 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000664 promoteExprToType(expr, Context.IntTy);
665 else
666 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000667}
668
Steve Naroff1926c832007-04-24 00:23:05 +0000669/// UsualArithmeticConversions - Performs various conversions that are common to
670/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
671/// routine returns the first non-arithmetic type found. The client is
672/// responsible for emitting appropriate error diagnostics.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000673void Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr) {
Steve Naroff31090012007-07-16 21:54:35 +0000674 UsualUnaryConversions(lhsExpr);
675 UsualUnaryConversions(rhsExpr);
676
Steve Naroff94a5aca2007-07-16 22:23:01 +0000677 QualType lhs = lhsExpr->getType();
678 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000679
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000680 // If both types are identical, no conversion is needed.
681 if (lhs == rhs)
Steve Naroffdbd9e892007-07-17 00:58:39 +0000682 return;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000683
684 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
685 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000686 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
687 return;
Steve Naroff1926c832007-04-24 00:23:05 +0000688
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000689 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000690
691 // Handle complex types first (C99 6.3.1.8p1).
692 if (lhs->isComplexType() || rhs->isComplexType()) {
693 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000694 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
695 promoteExprToType(rhsExpr, lhs);
696 return;
697 }
698 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
699 promoteExprToType(lhsExpr, rhs);
700 return;
701 }
Steve Naroff81569d22007-07-15 02:02:06 +0000702 // Two complex types. Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000703 if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs
704 promoteExprToType(rhsExpr, lhs);
705 return;
706 }
707 promoteExprToType(lhsExpr, rhs); // convert the lhs
708 return;
Steve Naroffbf223ba2007-04-24 20:56:26 +0000709 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000710 // Now handle "real" floating types (i.e. float, double, long double).
711 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
712 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000713 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
714 promoteExprToType(rhsExpr, lhs);
715 return;
716 }
717 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
718 promoteExprToType(lhsExpr, rhs);
719 return;
720 }
Steve Naroff81569d22007-07-15 02:02:06 +0000721 // We have two real floating types, float/complex combos were handled above.
722 // Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000723 if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs
724 promoteExprToType(rhsExpr, lhs);
725 return;
726 }
727 promoteExprToType(lhsExpr, rhs); // convert the lhs
728 return;
Steve Naroffb01bbe32007-04-25 01:22:31 +0000729 }
Steve Naroff81569d22007-07-15 02:02:06 +0000730 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000731 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
732 promoteExprToType(rhsExpr, lhs);
733 return;
734 }
735 promoteExprToType(lhsExpr, rhs); // convert the lhs
736 return;
Steve Narofff1e53692007-03-23 22:27:02 +0000737}
738
Steve Naroff3f597292007-05-11 22:18:03 +0000739// CheckPointerTypesForAssignment - This is a very tricky routine (despite
740// being closely modeled after the C99 spec:-). The odd characteristic of this
741// routine is it effectively iqnores the qualifiers on the top level pointee.
742// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
743// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000744Sema::AssignmentCheckResult
745Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000746 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000747
748 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000749 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
750 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000751
752 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000753 lhptee = lhptee.getCanonicalType();
754 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000755
Steve Naroff98cf3e92007-06-06 18:38:38 +0000756 AssignmentCheckResult r = Compatible;
757
Steve Naroff3f597292007-05-11 22:18:03 +0000758 // C99 6.5.16.1p1: This following citation is common to constraints
759 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
760 // qualifiers of the type *pointed to* by the right;
761 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
762 rhptee.getQualifiers())
763 r = CompatiblePointerDiscardsQualifiers;
764
765 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
766 // incomplete type and the other is a pointer to a qualified or unqualified
767 // version of void...
768 if (lhptee.getUnqualifiedType()->isVoidType() &&
769 (rhptee->isObjectType() || rhptee->isIncompleteType()))
770 ;
771 else if (rhptee.getUnqualifiedType()->isVoidType() &&
772 (lhptee->isObjectType() || lhptee->isIncompleteType()))
773 ;
774 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
775 // unqualified versions of compatible types, ...
776 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
777 rhptee.getUnqualifiedType()))
778 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000779 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000780}
781
Steve Naroff98cf3e92007-06-06 18:38:38 +0000782/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000783/// has code to accommodate several GCC extensions when type checking
784/// pointers. Here are some objectionable examples that GCC considers warnings:
785///
786/// int a, *pint;
787/// short *pshort;
788/// struct foo *pfoo;
789///
790/// pint = pshort; // warning: assignment from incompatible pointer type
791/// a = pint; // warning: assignment makes integer from pointer without a cast
792/// pint = a; // warning: assignment makes pointer from integer without a cast
793/// pint = pfoo; // warning: assignment from incompatible pointer type
794///
795/// As a result, the code for dealing with pointers is more complex than the
796/// C99 spec dictates.
797/// Note: the warning above turn into errors when -pedantic-errors is enabled.
798///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000799Sema::AssignmentCheckResult
800Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000801 if (lhsType == rhsType) // common case, fast path...
802 return Compatible;
803
Steve Naroff84ff4b42007-07-09 21:31:10 +0000804 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000805 if (lhsType->isVectorType() || rhsType->isVectorType()) {
806 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
807 return Incompatible;
808 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000809 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000810 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000811 if (rhsType->isIntegerType())
812 return PointerFromInt;
813
Steve Naroff3f597292007-05-11 22:18:03 +0000814 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000815 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000816 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000817 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
818 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
819 return IntFromPointer;
820
Steve Naroff3f597292007-05-11 22:18:03 +0000821 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000822 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000823 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
824 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000825 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000826 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
827 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000828 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000829 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000830 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000831}
832
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000833Sema::AssignmentCheckResult
834Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
835 // This check seems unnatural, however it is necessary to insure the proper
836 // conversion of functions/arrays. If the conversion were done for all
837 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
838 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +0000839 DefaultFunctionArrayConversion(rExpr);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000840
Steve Naroff31090012007-07-16 21:54:35 +0000841 return CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000842}
843
844Sema::AssignmentCheckResult
845Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
846 return CheckAssignmentConstraints(lhsType, rhsType);
847}
848
Steve Naroff7a5af782007-07-13 16:58:59 +0000849inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000850 Diag(loc, diag::err_typecheck_invalid_operands,
851 lex->getType().getAsString(), rex->getType().getAsString(),
852 lex->getSourceRange(), rex->getSourceRange());
853}
854
Steve Naroff7a5af782007-07-13 16:58:59 +0000855inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
856 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000857 QualType lhsType = lex->getType(), rhsType = rex->getType();
858
859 // make sure the vector types are identical.
860 if (lhsType == rhsType)
861 return lhsType;
862 // You cannot convert between vector values of different size.
863 Diag(loc, diag::err_typecheck_vector_not_convertable,
864 lex->getType().getAsString(), rex->getType().getAsString(),
865 lex->getSourceRange(), rex->getSourceRange());
866 return QualType();
867}
868
Steve Naroff218bc2b2007-05-04 21:54:46 +0000869inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000870 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000871{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000872 QualType lhsType = lex->getType(), rhsType = rex->getType();
873
874 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000875 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +0000876
Steve Naroffdbd9e892007-07-17 00:58:39 +0000877 UsualArithmeticConversions(lex, rex);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000878
Steve Naroffdbd9e892007-07-17 00:58:39 +0000879 // handle the common case first (both operands are arithmetic).
880 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
881 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000882 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000883 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000884}
885
Steve Naroff218bc2b2007-05-04 21:54:46 +0000886inline QualType Sema::CheckRemainderOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000887 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000888{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000889 QualType lhsType = lex->getType(), rhsType = rex->getType();
890
Steve Naroffdbd9e892007-07-17 00:58:39 +0000891 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000892
Steve Naroffdbd9e892007-07-17 00:58:39 +0000893 // handle the common case first (both operands are arithmetic).
894 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
895 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000896 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000897 return QualType();
898}
899
900inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000901 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000902{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000903 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +0000904 return CheckVectorOperands(loc, lex, rex);
905
Steve Naroffdbd9e892007-07-17 00:58:39 +0000906 UsualArithmeticConversions(lex, rex);
Steve Naroff94a5aca2007-07-16 22:23:01 +0000907
Steve Naroffe4718892007-04-27 18:30:00 +0000908 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000909 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
910 return lex->getType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000911
Steve Naroffdbd9e892007-07-17 00:58:39 +0000912 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
913 return lex->getType();
914 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
915 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000916 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000917 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000918}
919
Steve Naroff218bc2b2007-05-04 21:54:46 +0000920inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000921 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000922{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000923 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000924 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000925
Steve Naroffdbd9e892007-07-17 00:58:39 +0000926 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000927
928 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000929 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
930 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000931
932 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffdbd9e892007-07-17 00:58:39 +0000933 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000934 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000935 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000936 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000937 return QualType();
938}
939
940inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff7a5af782007-07-13 16:58:59 +0000941 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000942{
Chris Lattner1d411a82007-06-05 20:52:21 +0000943 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
944 // for int << longlong -> the result type should be int, not long long.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000945 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +0000946
Steve Naroffdbd9e892007-07-17 00:58:39 +0000947 // handle the common case first (both operands are arithmetic).
948 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
949 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000950 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000951 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000952}
953
Steve Naroff218bc2b2007-05-04 21:54:46 +0000954inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
Steve Naroff7a5af782007-07-13 16:58:59 +0000955 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000956{
Steve Naroff31090012007-07-16 21:54:35 +0000957 UsualUnaryConversions(lex);
958 UsualUnaryConversions(rex);
959 QualType lType = lex->getType();
960 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000961
962 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000963 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000964
Steve Naroff75c17232007-06-13 21:41:08 +0000965 if (lType->isPointerType()) {
966 if (rType->isPointerType())
967 return Context.IntTy;
968 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000969 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000970 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
971 lex->getSourceRange(), rex->getSourceRange());
972 return Context.IntTy; // the previous diagnostic is a GCC extension.
973 }
974 } else if (rType->isPointerType()) {
975 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000976 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000977 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
978 lex->getSourceRange(), rex->getSourceRange());
979 return Context.IntTy; // the previous diagnostic is a GCC extension.
980 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000981 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000982 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000983 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000984}
985
Steve Naroff218bc2b2007-05-04 21:54:46 +0000986inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
Steve Naroff7a5af782007-07-13 16:58:59 +0000987 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000988{
Steve Naroff31090012007-07-16 21:54:35 +0000989 UsualUnaryConversions(lex);
990 UsualUnaryConversions(rex);
991 QualType lType = lex->getType();
992 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000993
994 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000995 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000996
Steve Naroff75c17232007-06-13 21:41:08 +0000997 if (lType->isPointerType()) {
998 if (rType->isPointerType())
999 return Context.IntTy;
1000 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001001 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001002 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1003 lex->getSourceRange(), rex->getSourceRange());
1004 return Context.IntTy; // the previous diagnostic is a GCC extension.
1005 }
1006 } else if (rType->isPointerType()) {
1007 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001008 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001009 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1010 lex->getSourceRange(), rex->getSourceRange());
1011 return Context.IntTy; // the previous diagnostic is a GCC extension.
1012 }
Steve Naroff043d45d2007-05-15 02:32:35 +00001013 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001014 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001015 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001016}
1017
Steve Naroff218bc2b2007-05-04 21:54:46 +00001018inline QualType Sema::CheckBitwiseOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +00001019 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001020{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001021 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001022 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001023
Steve Naroffdbd9e892007-07-17 00:58:39 +00001024 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001025
Steve Naroffdbd9e892007-07-17 00:58:39 +00001026 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
1027 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001028 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001029 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001030}
1031
Steve Naroff218bc2b2007-05-04 21:54:46 +00001032inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001033 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001034{
Steve Naroff31090012007-07-16 21:54:35 +00001035 UsualUnaryConversions(lex);
1036 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001037
Steve Naroffdbd9e892007-07-17 00:58:39 +00001038 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001039 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001040 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001041 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001042}
1043
Steve Naroff35d85152007-05-07 00:24:15 +00001044inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
1045 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001046{
Steve Naroff0af91202007-04-27 21:51:21 +00001047 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001048 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001049 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001050 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1051
1052 switch (mlval) { // C99 6.5.16p2
1053 case Expr::MLV_Valid:
1054 break;
1055 case Expr::MLV_ConstQualified:
1056 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1057 hadError = true;
1058 break;
1059 case Expr::MLV_ArrayType:
1060 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1061 lhsType.getAsString(), lex->getSourceRange());
1062 return QualType();
1063 case Expr::MLV_NotObjectType:
1064 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1065 lhsType.getAsString(), lex->getSourceRange());
1066 return QualType();
1067 case Expr::MLV_InvalidExpression:
1068 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1069 lex->getSourceRange());
1070 return QualType();
1071 case Expr::MLV_IncompleteType:
1072 case Expr::MLV_IncompleteVoidType:
1073 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1074 lhsType.getAsString(), lex->getSourceRange());
1075 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001076 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001077 AssignmentCheckResult result;
1078
1079 if (compoundType.isNull())
1080 result = CheckSingleAssignmentConstraints(lhsType, rex);
1081 else
1082 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1083
Steve Naroff218bc2b2007-05-04 21:54:46 +00001084 // decode the result (notice that extensions still return a type).
1085 switch (result) {
1086 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001087 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001088 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001089 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001090 lhsType.getAsString(), rhsType.getAsString(),
1091 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001092 hadError = true;
1093 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001094 case PointerFromInt:
1095 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001096 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001097 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1098 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001099 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001100 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001101 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001102 case IntFromPointer:
1103 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1104 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001105 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001106 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001107 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001108 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1109 lhsType.getAsString(), rhsType.getAsString(),
1110 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001111 break;
1112 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001113 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1114 lhsType.getAsString(), rhsType.getAsString(),
1115 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001116 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001117 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001118 // C99 6.5.16p3: The type of an assignment expression is the type of the
1119 // left operand unless the left operand has qualified type, in which case
1120 // it is the unqualified version of the type of the left operand.
1121 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1122 // is converted to the type of the assignment expression (above).
1123 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1124 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001125}
1126
Steve Naroff218bc2b2007-05-04 21:54:46 +00001127inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001128 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001129 UsualUnaryConversions(rex);
1130 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001131}
1132
Steve Naroff7a5af782007-07-13 16:58:59 +00001133/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1134/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001135QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001136 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001137 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001138
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001139 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +00001140 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1141 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001142 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1143 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001144 return QualType();
1145 }
1146 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +00001147 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001148 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1149 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001150 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001151 }
Steve Naroff094046f2007-05-13 03:21:25 +00001152 // At this point, we know we have a real or pointer type. Now make sure
1153 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001154 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1155 if (mlval != Expr::MLV_Valid) {
1156 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001157 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001158 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001159 return QualType();
1160 }
1161 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001162}
1163
Steve Naroff1926c832007-04-24 00:23:05 +00001164/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001165/// This routine allows us to typecheck complex/recursive expressions
1166/// where the declaration is needed for type checking. Here are some
1167/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001168static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001169 switch (e->getStmtClass()) {
1170 case Stmt::DeclRefExprClass:
1171 return cast<DeclRefExpr>(e)->getDecl();
1172 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001173 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001174 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001175 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001176 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001177 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001178 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001179 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001180 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001181 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001182 default:
1183 return 0;
1184 }
1185}
1186
1187/// CheckAddressOfOperand - The operand of & must be either a function
1188/// designator or an lvalue designating an object. If it is an lvalue, the
1189/// object cannot be declared with storage class register or be a bit field.
1190/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001191/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001192QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001193 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001194 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001195
Steve Naroff9358c712007-05-27 23:58:33 +00001196 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001197 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1198 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001199 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001200 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1201 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001202 return QualType();
1203 }
Steve Naroff47500512007-04-19 23:00:49 +00001204 } else if (dcl) {
1205 // We have an lvalue with a decl. Make sure the decl is not declared
1206 // with the register storage-class specifier.
1207 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001208 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001209 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001210 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001211 return QualType();
1212 }
Steve Narofff633d092007-04-25 19:01:39 +00001213 } else
1214 assert(0 && "Unknown/unexpected decl type");
1215
Steve Naroff47500512007-04-19 23:00:49 +00001216 // FIXME: add check for bitfields!
1217 }
1218 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001219 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001220}
1221
Steve Naroff35d85152007-05-07 00:24:15 +00001222QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001223 UsualUnaryConversions(op);
1224 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001225
Steve Naroff758ada12007-05-28 16:15:57 +00001226 if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) {
1227 QualType ptype = PT->getPointeeType();
1228 // C99 6.5.3.2p4. "if it points to an object,...".
1229 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1230 // GCC compat: special case 'void *' (treat as warning).
1231 if (ptype->isVoidType()) {
1232 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001233 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001234 } else {
1235 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001236 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001237 return QualType();
1238 }
1239 }
1240 return ptype;
1241 }
1242 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001243 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001244 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001245}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001246
1247static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1248 tok::TokenKind Kind) {
1249 BinaryOperator::Opcode Opc;
1250 switch (Kind) {
1251 default: assert(0 && "Unknown binop!");
1252 case tok::star: Opc = BinaryOperator::Mul; break;
1253 case tok::slash: Opc = BinaryOperator::Div; break;
1254 case tok::percent: Opc = BinaryOperator::Rem; break;
1255 case tok::plus: Opc = BinaryOperator::Add; break;
1256 case tok::minus: Opc = BinaryOperator::Sub; break;
1257 case tok::lessless: Opc = BinaryOperator::Shl; break;
1258 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1259 case tok::lessequal: Opc = BinaryOperator::LE; break;
1260 case tok::less: Opc = BinaryOperator::LT; break;
1261 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1262 case tok::greater: Opc = BinaryOperator::GT; break;
1263 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1264 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1265 case tok::amp: Opc = BinaryOperator::And; break;
1266 case tok::caret: Opc = BinaryOperator::Xor; break;
1267 case tok::pipe: Opc = BinaryOperator::Or; break;
1268 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1269 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1270 case tok::equal: Opc = BinaryOperator::Assign; break;
1271 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1272 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1273 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1274 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1275 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1276 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1277 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1278 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1279 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1280 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1281 case tok::comma: Opc = BinaryOperator::Comma; break;
1282 }
1283 return Opc;
1284}
1285
Steve Naroff35d85152007-05-07 00:24:15 +00001286static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1287 tok::TokenKind Kind) {
1288 UnaryOperator::Opcode Opc;
1289 switch (Kind) {
1290 default: assert(0 && "Unknown unary op!");
1291 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1292 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1293 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1294 case tok::star: Opc = UnaryOperator::Deref; break;
1295 case tok::plus: Opc = UnaryOperator::Plus; break;
1296 case tok::minus: Opc = UnaryOperator::Minus; break;
1297 case tok::tilde: Opc = UnaryOperator::Not; break;
1298 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1299 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1300 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1301 case tok::kw___real: Opc = UnaryOperator::Real; break;
1302 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001303 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001304 }
1305 return Opc;
1306}
1307
Steve Naroff218bc2b2007-05-04 21:54:46 +00001308// Binary Operators. 'Tok' is the token for the operator.
1309Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1310 ExprTy *LHS, ExprTy *RHS) {
1311 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1312 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1313
1314 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1315 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1316
Chris Lattner256c21b2007-06-28 03:53:10 +00001317 QualType ResultTy; // Result type of the binary operator.
1318 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001319
1320 switch (Opc) {
1321 default:
1322 assert(0 && "Unknown binary expr!");
1323 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001324 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001325 break;
1326 case BinaryOperator::Mul:
1327 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001328 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001329 break;
1330 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001331 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001332 break;
1333 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001334 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001335 break;
1336 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001337 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001338 break;
1339 case BinaryOperator::Shl:
1340 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001341 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001342 break;
1343 case BinaryOperator::LE:
1344 case BinaryOperator::LT:
1345 case BinaryOperator::GE:
1346 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001347 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001348 break;
1349 case BinaryOperator::EQ:
1350 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001351 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001352 break;
1353 case BinaryOperator::And:
1354 case BinaryOperator::Xor:
1355 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001356 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001357 break;
1358 case BinaryOperator::LAnd:
1359 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001360 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001361 break;
1362 case BinaryOperator::MulAssign:
1363 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001364 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1365 if (!CompTy.isNull())
1366 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001367 break;
1368 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001369 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1370 if (!CompTy.isNull())
1371 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001372 break;
1373 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001374 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1375 if (!CompTy.isNull())
1376 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001377 break;
1378 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001379 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1380 if (!CompTy.isNull())
1381 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001382 break;
1383 case BinaryOperator::ShlAssign:
1384 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001385 CompTy = CheckShiftOperands(lhs, rhs, TokLoc);
1386 if (!CompTy.isNull())
1387 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001388 break;
1389 case BinaryOperator::AndAssign:
1390 case BinaryOperator::XorAssign:
1391 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001392 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1393 if (!CompTy.isNull())
1394 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001395 break;
1396 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001397 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001398 break;
1399 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001400 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001401 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001402 if (CompTy.isNull())
1403 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1404 else
Chris Lattner9369a562007-06-29 16:31:29 +00001405 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001406}
1407
Steve Naroff35d85152007-05-07 00:24:15 +00001408// Unary Operators. 'Tok' is the token for the operator.
1409Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001410 ExprTy *input) {
1411 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001412 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1413 QualType resultType;
1414 switch (Opc) {
1415 default:
1416 assert(0 && "Unimplemented unary expr!");
1417 case UnaryOperator::PreInc:
1418 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001419 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001420 break;
1421 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001422 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001423 break;
1424 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001425 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001426 break;
1427 case UnaryOperator::Plus:
1428 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001429 UsualUnaryConversions(Input);
1430 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001431 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001432 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1433 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001434 break;
1435 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001436 UsualUnaryConversions(Input);
1437 resultType = Input->getType();
Steve Naroff1c8b7d32007-07-12 21:46:55 +00001438 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1439 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1440 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001441 break;
1442 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001443 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001444 DefaultFunctionArrayConversion(Input);
1445 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001446 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001447 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1448 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001449 // LNot always has type int. C99 6.5.3.3p5.
1450 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001451 break;
1452 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001453 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001454 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001455 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001456 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1457 break;
1458 case UnaryOperator::Extension:
1459 // FIXME: does __extension__ cause any promotions? I would think not.
1460 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001461 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001462 }
1463 if (resultType.isNull())
1464 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001465 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001466}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001467
1468/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1469Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1470 SourceLocation LabLoc,
1471 IdentifierInfo *LabelII) {
1472 // Look up the record for this label identifier.
1473 LabelStmt *&LabelDecl = LabelMap[LabelII];
1474
1475 // If we haven't seen this label yet, create a forward reference.
1476 if (LabelDecl == 0)
1477 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1478
1479 // Create the AST node. The address of a label always has type 'void*'.
1480 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1481 Context.getPointerType(Context.VoidTy));
1482}
1483
Chris Lattner366727f2007-07-24 16:58:17 +00001484Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
1485 SourceLocation RPLoc) { // "({..})"
1486 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1487 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1488 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1489
1490 // FIXME: there are a variety of strange constraints to enforce here, for
1491 // example, it is not possible to goto into a stmt expression apparently.
1492 // More semantic analysis is needed.
1493
1494 // FIXME: the last statement in the compount stmt has its value used. We
1495 // should not warn about it being unused.
1496
1497 // If there are sub stmts in the compound stmt, take the type of the last one
1498 // as the type of the stmtexpr.
1499 QualType Ty = Context.VoidTy;
1500
1501 if (!Compound->body_empty())
1502 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1503 Ty = LastExpr->getType();
1504
1505 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1506}