blob: fc4bc865cc443d5e946360d45ada535f3977770f [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 Lattner17ed4872006-11-20 04:58:19 +000083}
Chris Lattnere168f762006-11-10 05:29:30 +000084
Chris Lattner17ed4872006-11-20 04:58:19 +000085Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
86 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000087 switch (Kind) {
88 default:
89 assert(0 && "Unknown simple primary expr!");
Steve Naroffe4718892007-04-27 18:30:00 +000090 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000091 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
92 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
93 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +000094 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +000095 }
96}
97
Chris Lattner146762e2007-07-20 16:59:19 +000098Sema::ExprResult Sema::ParseCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +000099 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000100 CharBuffer.resize(Tok.getLength());
101 const char *ThisTokBegin = &CharBuffer[0];
102 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
103
104 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
105 Tok.getLocation(), PP);
106 if (Literal.hadError())
107 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000108 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
109 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000110}
111
Chris Lattner146762e2007-07-20 16:59:19 +0000112Action::ExprResult Sema::ParseNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000113 // fast path for a single digit (which is quite common). A single digit
114 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
115 if (Tok.getLength() == 1) {
116 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000117
Chris Lattner4481b422007-07-14 01:29:45 +0000118 unsigned IntSize = Context.getTypeSize(Context.IntTy, Tok.getLocation());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000119 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
120 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000121 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000122 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000123 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000124 IntegerBuffer.resize(Tok.getLength());
125 const char *ThisTokBegin = &IntegerBuffer[0];
126
Chris Lattner67ca9252007-05-21 01:08:44 +0000127 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000128 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000129 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000130 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000131 if (Literal.hadError)
132 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000133
Steve Naroff09ef4742007-03-09 23:16:33 +0000134 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000135 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000136
137 // Get the value in the widest-possible width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000138 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000139
140 if (Literal.GetIntegerValue(ResultVal)) {
141 // If this value didn't fit into uintmax_t, warn and force to ull.
142 Diag(Tok.getLocation(), diag::warn_integer_too_large);
143 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000144 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000145 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000146 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000147 // If this value fits into a ULL, try to figure out what else it fits into
148 // according to the rules of C99 6.4.4.1p5.
149
150 // Octal, Hexadecimal, and integers with a U suffix are allowed to
151 // be an unsigned int.
152 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
153
154 // Check from smallest to largest, picking the smallest type we can.
155 if (!Literal.isLong) { // Are int/unsigned possibilities?
Chris Lattner4481b422007-07-14 01:29:45 +0000156 unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000157 // Does it fit in a unsigned int?
158 if (ResultVal.isIntN(IntSize)) {
159 // Does it fit in a signed int?
160 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
161 t = Context.IntTy;
162 else if (AllowUnsigned)
163 t = Context.UnsignedIntTy;
164 }
165
166 if (!t.isNull())
167 ResultVal.trunc(IntSize);
168 }
169
170 // Are long/unsigned long possibilities?
171 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner4481b422007-07-14 01:29:45 +0000172 unsigned LongSize = Context.getTypeSize(Context.LongTy,
173 Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000174
175 // Does it fit in a unsigned long?
176 if (ResultVal.isIntN(LongSize)) {
177 // Does it fit in a signed long?
178 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
179 t = Context.LongTy;
180 else if (AllowUnsigned)
181 t = Context.UnsignedLongTy;
182 }
183 if (!t.isNull())
184 ResultVal.trunc(LongSize);
185 }
186
187 // Finally, check long long if needed.
188 if (t.isNull()) {
189 unsigned LongLongSize =
Chris Lattner4481b422007-07-14 01:29:45 +0000190 Context.getTypeSize(Context.LongLongTy, Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000191
192 // Does it fit in a unsigned long long?
193 if (ResultVal.isIntN(LongLongSize)) {
194 // Does it fit in a signed long long?
195 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
196 t = Context.LongLongTy;
197 else if (AllowUnsigned)
198 t = Context.UnsignedLongLongTy;
199 }
200 }
201
202 // If we still couldn't decide a type, we probably have something that
203 // does not fit in a signed long long, but has no U suffix.
204 if (t.isNull()) {
205 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
206 t = Context.UnsignedLongLongTy;
207 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000208 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000209
210 return new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000211 } else if (Literal.isFloatingLiteral()) {
Steve Naroff97b9e912007-07-09 23:53:58 +0000212 // FIXME: handle float values > 32 (including compute the real type...).
213 return new FloatingLiteral(Literal.GetFloatValue(), Context.FloatTy,
214 Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000215 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000216 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000217}
218
219Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
220 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000221 Expr *e = (Expr *)Val;
222 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000223 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000224}
225
Steve Naroff71b59a92007-06-04 22:22:31 +0000226/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000227/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000228QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
229 SourceLocation OpLoc, bool isSizeof) {
230 // C99 6.5.3.4p1:
231 if (isa<FunctionType>(exprType) && isSizeof)
232 // alignof(function) is allowed.
233 Diag(OpLoc, diag::ext_sizeof_function_type);
234 else if (exprType->isVoidType())
235 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
236 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000237 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000238 diag::err_alignof_incomplete_type,
239 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000240 return QualType(); // error
241 }
242 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
243 return Context.getSizeType();
244}
245
Chris Lattnere168f762006-11-10 05:29:30 +0000246Action::ExprResult Sema::
247ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000248 SourceLocation LPLoc, TypeTy *Ty,
249 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000250 // If error parsing type, ignore.
251 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000252
253 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000254 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000255
Steve Naroff043d45d2007-05-15 02:32:35 +0000256 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
257
258 if (resultType.isNull())
259 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000260 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000261}
262
263
264Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
265 tok::TokenKind Kind,
266 ExprTy *Input) {
267 UnaryOperator::Opcode Opc;
268 switch (Kind) {
269 default: assert(0 && "Unknown unary op!");
270 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
271 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
272 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000273 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000274 if (result.isNull())
275 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000276 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000277}
278
279Action::ExprResult Sema::
280ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
281 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000282 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000283
284 // Perform default conversions.
285 DefaultFunctionArrayConversion(LHSExp);
286 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000287
Chris Lattner36d572b2007-07-16 00:14:47 +0000288 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000289
Steve Naroffc1aadb12007-03-28 21:49:40 +0000290 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
291 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000292 // in the subscript position. As a result, we need to derive the array base
293 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000294 Expr *BaseExpr, *IndexExpr;
295 QualType ResultType;
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000296 if (const PointerType *PTy = LHSTy->isPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000297 BaseExpr = LHSExp;
298 IndexExpr = RHSExp;
299 // FIXME: need to deal with const...
300 ResultType = PTy->getPointeeType();
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000301 } else if (const PointerType *PTy = RHSTy->isPointerType()) {
302 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000303 BaseExpr = RHSExp;
304 IndexExpr = LHSExp;
305 // FIXME: need to deal with const...
306 ResultType = PTy->getPointeeType();
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000307 } else if (const VectorType *VTy = LHSTy->isVectorType()) { // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000308 BaseExpr = LHSExp;
309 IndexExpr = RHSExp;
310 // FIXME: need to deal with const...
311 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000312 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000313 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
314 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000315 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000316 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000317 if (!IndexExpr->getType()->isIntegerType())
318 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
319 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000320
Chris Lattner36d572b2007-07-16 00:14:47 +0000321 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
322 // the following check catches trying to index a pointer to a function (e.g.
323 // void (*)(int)). Functions are not objects in C99.
324 if (!ResultType->isObjectType())
325 return Diag(BaseExpr->getLocStart(),
326 diag::err_typecheck_subscript_not_object,
327 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
328
329 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000330}
331
332Action::ExprResult Sema::
333ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
334 tok::TokenKind OpKind, SourceLocation MemberLoc,
335 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000336 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000337
338 assert(!qualifiedType.isNull() && "no type for member expression");
339
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000340 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000341
342 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000343 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
344 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000345 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000346 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000347 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
348 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000349 if (!isa<RecordType>(canonType))
350 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
351
352 // get the struct/union definition from the type.
353 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000354
Steve Naroff92e30f82007-04-02 22:35:25 +0000355 if (canonType->isIncompleteType())
356 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000357
Steve Naroff92e30f82007-04-02 22:35:25 +0000358 FieldDecl *MemberDecl = RD->getMember(&Member);
359 if (!MemberDecl)
360 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
361
Steve Naroff9358c712007-05-27 23:58:33 +0000362 return new MemberExpr((Expr*)Base, OpKind == tok::arrow,
363 MemberDecl, MemberLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000364}
365
366/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
367/// This provides the location of the left/right parens and a list of comma
368/// locations.
369Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000370ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
371 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000372 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000373 Expr *Fn = static_cast<Expr *>(fn);
374 Expr **Args = reinterpret_cast<Expr**>(args);
375 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000376
Chris Lattner38dbdb22007-07-21 03:03:59 +0000377 UsualUnaryConversions(Fn);
378 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000379
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000380 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
381 // type pointer to function".
Steve Naroff31090012007-07-16 21:54:35 +0000382 const PointerType *PT = dyn_cast<PointerType>(funcType);
383 if (PT == 0) PT = dyn_cast<PointerType>(funcType.getCanonicalType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000384
385 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000386 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
387 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000388
389 const FunctionType *funcT = dyn_cast<FunctionType>(PT->getPointeeType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000390 if (funcT == 0)
391 funcT = dyn_cast<FunctionType>(PT->getPointeeType().getCanonicalType());
392
393 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000394 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
395 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000396
Steve Naroff17f76e02007-05-03 21:03:48 +0000397 // If a prototype isn't declared, the parser implicitly defines a func decl
398 QualType resultType = funcT->getResultType();
399
400 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
401 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
402 // assignment, to the types of the corresponding parameter, ...
403
404 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000405 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000406
Steve Naroffb8c289d2007-05-08 22:18:00 +0000407 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000408 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000409 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000410 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000411 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000412 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000413 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000414 SourceRange(Args[NumArgsInProto]->getLocStart(),
415 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000416 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000417 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000418 }
419 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000420 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000421 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000422 assert(argExpr && "ParseCallExpr(): missing argument expression");
423
Steve Naroff17f76e02007-05-03 21:03:48 +0000424 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000425 QualType rhsType = argExpr->getType();
Steve Naroff17f76e02007-05-03 21:03:48 +0000426
427 if (lhsType == rhsType) // common case, fast path...
428 continue;
Steve Naroff98cf3e92007-06-06 18:38:38 +0000429
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000430 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
431 argExpr);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000432 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000433
434 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000435 switch (result) {
436 case Compatible:
437 break;
438 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000439 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000440 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000441 Diag(l, diag::ext_typecheck_passing_pointer_int,
442 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000443 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000444 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000445 break;
446 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000447 Diag(l, diag::ext_typecheck_passing_pointer_int,
448 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000449 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000450 break;
451 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000452 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000453 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000454 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000455 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000456 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000457 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
458 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000459 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000460 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000461 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000462 return Diag(l, diag::err_typecheck_passing_incompatible,
463 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000464 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000465 }
466 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000467 // Even if the types checked, bail if we had the wrong number of arguments.
Chris Lattner38dbdb22007-07-21 03:03:59 +0000468 if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
Steve Naroffb8c289d2007-05-08 22:18:00 +0000469 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000470 }
Chris Lattner38dbdb22007-07-21 03:03:59 +0000471 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000472}
473
474Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000475ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000476 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000477 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
478 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000479 // FIXME: put back this assert when initializers are worked out.
480 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
481 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000482
483 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000484 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000485}
486
487Action::ExprResult Sema::
488ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
489 SourceLocation RParenLoc) {
490 // FIXME: add semantic analysis (C99 6.7.8). This involves
491 // knowledge of the object being intialized. As a result, the code for
492 // doing the semantic analysis will likely be located elsewhere (i.e. in
493 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
494 return false; // FIXME instantiate an InitListExpr.
495}
496
497Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000498ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
499 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000500 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
501
502 Expr *castExpr = static_cast<Expr*>(Op);
503 QualType castType = QualType::getFromOpaquePtr(Ty);
504
Chris Lattnerbd270732007-07-18 16:00:06 +0000505 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
506 // type needs to be scalar.
507 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000508 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
509 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
510 }
511 if (!castExpr->getType()->isScalarType()) {
512 return Diag(castExpr->getLocStart(),
513 diag::err_typecheck_expect_scalar_operand,
514 castExpr->getType().getAsString(), castExpr->getSourceRange());
515 }
516 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000517}
518
Steve Narofff8a28c52007-05-15 20:29:32 +0000519inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000520 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000521 UsualUnaryConversions(cond);
522 UsualUnaryConversions(lex);
523 UsualUnaryConversions(rex);
524 QualType condT = cond->getType();
525 QualType lexT = lex->getType();
526 QualType rexT = rex->getType();
527
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000528 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000529 if (!condT->isScalarType()) { // C99 6.5.15p2
530 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
531 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000532 return QualType();
533 }
534 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000535 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
536 UsualArithmeticConversions(lex, rex);
537 return lex->getType();
538 }
Steve Naroff7a5af782007-07-13 16:58:59 +0000539 if ((lexT->isStructureType() && rexT->isStructureType()) || // C99 6.5.15p3
540 (lexT->isUnionType() && rexT->isUnionType())) {
541 TagType *lTag = cast<TagType>(lexT.getCanonicalType());
542 TagType *rTag = cast<TagType>(rexT.getCanonicalType());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000543
544 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
Steve Naroff7a5af782007-07-13 16:58:59 +0000545 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000546 else {
547 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000548 lexT.getAsString(), rexT.getAsString(),
549 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000550 return QualType();
551 }
552 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000553 // C99 6.5.15p3
554 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000555 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000556 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000557 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000558
Steve Naroff7a5af782007-07-13 16:58:59 +0000559 if (lexT->isPointerType() && rexT->isPointerType()) { // C99 6.5.15p3,6
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000560 QualType lhptee, rhptee;
561
562 // get the "pointed to" type
Steve Naroff7a5af782007-07-13 16:58:59 +0000563 lhptee = cast<PointerType>(lexT.getCanonicalType())->getPointeeType();
564 rhptee = cast<PointerType>(rexT.getCanonicalType())->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000565
566 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
567 if (lhptee.getUnqualifiedType()->isVoidType() &&
568 (rhptee->isObjectType() || rhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000569 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000570 if (rhptee.getUnqualifiedType()->isVoidType() &&
571 (lhptee->isObjectType() || lhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000572 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000573
574 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
575 // *or* to differently qualified versions of compatible types, the result
576 // type is a pointer to an appropriately qualified version of the
577 // *composite* type.
578 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
579 rhptee.getUnqualifiedType())) {
580 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
Steve Naroff7a5af782007-07-13 16:58:59 +0000581 lexT.getAsString(), rexT.getAsString(),
582 lex->getSourceRange(), rex->getSourceRange());
583 return lexT; // FIXME: this is an _ext - is this return o.k?
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000584 }
585 }
Steve Naroff7a5af782007-07-13 16:58:59 +0000586 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
587 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000588
589 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000590 lexT.getAsString(), rexT.getAsString(),
591 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000592 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000593}
594
Chris Lattnere168f762006-11-10 05:29:30 +0000595/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
596/// in the case of a the GNU conditional expr extension.
597Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
598 SourceLocation ColonLoc,
599 ExprTy *Cond, ExprTy *LHS,
600 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000601 Expr *CondExpr = (Expr *) Cond;
602 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
603 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
604 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000605 if (result.isNull())
606 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000607 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000608}
609
Steve Naroff81569d22007-07-15 02:02:06 +0000610// promoteExprToType - a helper function to ensure we create exactly one
611// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000612static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000613 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
614 impCast->setType(type);
615 else
616 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000617 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000618}
619
620/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000621void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000622 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000623 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000624
Bill Wendling89ba70e2007-07-17 05:09:22 +0000625 if (const ReferenceType *ref = t->isReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000626 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
627 t = e->getType();
628 }
Steve Naroff81569d22007-07-15 02:02:06 +0000629 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000630 promoteExprToType(e, Context.getPointerType(t));
631 else if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
632 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000633}
634
Steve Naroff71b59a92007-06-04 22:22:31 +0000635/// UsualUnaryConversion - Performs various conversions that are common to most
636/// operators (C99 6.3). The conversions of array and function types are
637/// sometimes surpressed. For example, the array->pointer conversion doesn't
638/// apply if the array is an argument to the sizeof or address (&) operators.
639/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000640void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000641 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000642 assert(!t.isNull() && "UsualUnaryConversions - missing type");
643
Bill Wendling89ba70e2007-07-17 05:09:22 +0000644 if (const ReferenceType *ref = t->isReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000645 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
646 t = expr->getType();
647 }
Steve Naroff81569d22007-07-15 02:02:06 +0000648 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000649 promoteExprToType(expr, Context.IntTy);
650 else
651 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000652}
653
Steve Naroff1926c832007-04-24 00:23:05 +0000654/// UsualArithmeticConversions - Performs various conversions that are common to
655/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
656/// routine returns the first non-arithmetic type found. The client is
657/// responsible for emitting appropriate error diagnostics.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000658void Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr) {
Steve Naroff31090012007-07-16 21:54:35 +0000659 UsualUnaryConversions(lhsExpr);
660 UsualUnaryConversions(rhsExpr);
661
Steve Naroff94a5aca2007-07-16 22:23:01 +0000662 QualType lhs = lhsExpr->getType();
663 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000664
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000665 // If both types are identical, no conversion is needed.
666 if (lhs == rhs)
Steve Naroffdbd9e892007-07-17 00:58:39 +0000667 return;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000668
669 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
670 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000671 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
672 return;
Steve Naroff1926c832007-04-24 00:23:05 +0000673
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000674 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000675
676 // Handle complex types first (C99 6.3.1.8p1).
677 if (lhs->isComplexType() || rhs->isComplexType()) {
678 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000679 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
680 promoteExprToType(rhsExpr, lhs);
681 return;
682 }
683 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
684 promoteExprToType(lhsExpr, rhs);
685 return;
686 }
Steve Naroff81569d22007-07-15 02:02:06 +0000687 // Two complex types. Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000688 if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs
689 promoteExprToType(rhsExpr, lhs);
690 return;
691 }
692 promoteExprToType(lhsExpr, rhs); // convert the lhs
693 return;
Steve Naroffbf223ba2007-04-24 20:56:26 +0000694 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000695 // Now handle "real" floating types (i.e. float, double, long double).
696 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
697 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000698 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
699 promoteExprToType(rhsExpr, lhs);
700 return;
701 }
702 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
703 promoteExprToType(lhsExpr, rhs);
704 return;
705 }
Steve Naroff81569d22007-07-15 02:02:06 +0000706 // We have two real floating types, float/complex combos were handled above.
707 // Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000708 if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs
709 promoteExprToType(rhsExpr, lhs);
710 return;
711 }
712 promoteExprToType(lhsExpr, rhs); // convert the lhs
713 return;
Steve Naroffb01bbe32007-04-25 01:22:31 +0000714 }
Steve Naroff81569d22007-07-15 02:02:06 +0000715 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000716 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
717 promoteExprToType(rhsExpr, lhs);
718 return;
719 }
720 promoteExprToType(lhsExpr, rhs); // convert the lhs
721 return;
Steve Narofff1e53692007-03-23 22:27:02 +0000722}
723
Steve Naroff3f597292007-05-11 22:18:03 +0000724// CheckPointerTypesForAssignment - This is a very tricky routine (despite
725// being closely modeled after the C99 spec:-). The odd characteristic of this
726// routine is it effectively iqnores the qualifiers on the top level pointee.
727// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
728// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000729Sema::AssignmentCheckResult
730Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000731 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000732
733 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000734 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
735 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000736
737 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000738 lhptee = lhptee.getCanonicalType();
739 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000740
Steve Naroff98cf3e92007-06-06 18:38:38 +0000741 AssignmentCheckResult r = Compatible;
742
Steve Naroff3f597292007-05-11 22:18:03 +0000743 // C99 6.5.16.1p1: This following citation is common to constraints
744 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
745 // qualifiers of the type *pointed to* by the right;
746 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
747 rhptee.getQualifiers())
748 r = CompatiblePointerDiscardsQualifiers;
749
750 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
751 // incomplete type and the other is a pointer to a qualified or unqualified
752 // version of void...
753 if (lhptee.getUnqualifiedType()->isVoidType() &&
754 (rhptee->isObjectType() || rhptee->isIncompleteType()))
755 ;
756 else if (rhptee.getUnqualifiedType()->isVoidType() &&
757 (lhptee->isObjectType() || lhptee->isIncompleteType()))
758 ;
759 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
760 // unqualified versions of compatible types, ...
761 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
762 rhptee.getUnqualifiedType()))
763 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000764 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000765}
766
Steve Naroff98cf3e92007-06-06 18:38:38 +0000767/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000768/// has code to accommodate several GCC extensions when type checking
769/// pointers. Here are some objectionable examples that GCC considers warnings:
770///
771/// int a, *pint;
772/// short *pshort;
773/// struct foo *pfoo;
774///
775/// pint = pshort; // warning: assignment from incompatible pointer type
776/// a = pint; // warning: assignment makes integer from pointer without a cast
777/// pint = a; // warning: assignment makes pointer from integer without a cast
778/// pint = pfoo; // warning: assignment from incompatible pointer type
779///
780/// As a result, the code for dealing with pointers is more complex than the
781/// C99 spec dictates.
782/// Note: the warning above turn into errors when -pedantic-errors is enabled.
783///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000784Sema::AssignmentCheckResult
785Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000786 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000787 if (lhsType->isVectorType() || rhsType->isVectorType()) {
788 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
789 return Incompatible;
790 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000791 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000792 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000793 if (rhsType->isIntegerType())
794 return PointerFromInt;
795
Steve Naroff3f597292007-05-11 22:18:03 +0000796 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000797 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000798 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000799 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
800 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
801 return IntFromPointer;
802
Steve Naroff3f597292007-05-11 22:18:03 +0000803 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000804 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000805 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
806 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000807 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000808 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
809 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000810 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000811 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000812 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000813}
814
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000815Sema::AssignmentCheckResult
816Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
817 // This check seems unnatural, however it is necessary to insure the proper
818 // conversion of functions/arrays. If the conversion were done for all
819 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
820 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +0000821 DefaultFunctionArrayConversion(rExpr);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000822
Steve Naroff31090012007-07-16 21:54:35 +0000823 return CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000824}
825
826Sema::AssignmentCheckResult
827Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
828 return CheckAssignmentConstraints(lhsType, rhsType);
829}
830
Steve Naroff7a5af782007-07-13 16:58:59 +0000831inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000832 Diag(loc, diag::err_typecheck_invalid_operands,
833 lex->getType().getAsString(), rex->getType().getAsString(),
834 lex->getSourceRange(), rex->getSourceRange());
835}
836
Steve Naroff7a5af782007-07-13 16:58:59 +0000837inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
838 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000839 QualType lhsType = lex->getType(), rhsType = rex->getType();
840
841 // make sure the vector types are identical.
842 if (lhsType == rhsType)
843 return lhsType;
844 // You cannot convert between vector values of different size.
845 Diag(loc, diag::err_typecheck_vector_not_convertable,
846 lex->getType().getAsString(), rex->getType().getAsString(),
847 lex->getSourceRange(), rex->getSourceRange());
848 return QualType();
849}
850
Steve Naroff218bc2b2007-05-04 21:54:46 +0000851inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000852 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000853{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000854 QualType lhsType = lex->getType(), rhsType = rex->getType();
855
856 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000857 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +0000858
Steve Naroffdbd9e892007-07-17 00:58:39 +0000859 UsualArithmeticConversions(lex, rex);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000860
Steve Naroffdbd9e892007-07-17 00:58:39 +0000861 // handle the common case first (both operands are arithmetic).
862 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
863 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000864 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000865 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000866}
867
Steve Naroff218bc2b2007-05-04 21:54:46 +0000868inline QualType Sema::CheckRemainderOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000869 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000870{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000871 QualType lhsType = lex->getType(), rhsType = rex->getType();
872
Steve Naroffdbd9e892007-07-17 00:58:39 +0000873 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000874
Steve Naroffdbd9e892007-07-17 00:58:39 +0000875 // handle the common case first (both operands are arithmetic).
876 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
877 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000878 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000879 return QualType();
880}
881
882inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000883 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000884{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000885 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +0000886 return CheckVectorOperands(loc, lex, rex);
887
Steve Naroffdbd9e892007-07-17 00:58:39 +0000888 UsualArithmeticConversions(lex, rex);
Steve Naroff94a5aca2007-07-16 22:23:01 +0000889
Steve Naroffe4718892007-04-27 18:30:00 +0000890 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000891 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
892 return lex->getType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000893
Steve Naroffdbd9e892007-07-17 00:58:39 +0000894 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
895 return lex->getType();
896 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
897 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000898 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000899 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000900}
901
Steve Naroff218bc2b2007-05-04 21:54:46 +0000902inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000903 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000904{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000905 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000906 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000907
Steve Naroffdbd9e892007-07-17 00:58:39 +0000908 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000909
910 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000911 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
912 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000913
914 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffdbd9e892007-07-17 00:58:39 +0000915 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000916 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000917 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000918 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000919 return QualType();
920}
921
922inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff7a5af782007-07-13 16:58:59 +0000923 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000924{
Chris Lattner1d411a82007-06-05 20:52:21 +0000925 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
926 // for int << longlong -> the result type should be int, not long long.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000927 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +0000928
Steve Naroffdbd9e892007-07-17 00:58:39 +0000929 // handle the common case first (both operands are arithmetic).
930 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
931 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000932 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000933 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000934}
935
Steve Naroff218bc2b2007-05-04 21:54:46 +0000936inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
Steve Naroff7a5af782007-07-13 16:58:59 +0000937 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000938{
Steve Naroff31090012007-07-16 21:54:35 +0000939 UsualUnaryConversions(lex);
940 UsualUnaryConversions(rex);
941 QualType lType = lex->getType();
942 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000943
944 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000945 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000946
Steve Naroff75c17232007-06-13 21:41:08 +0000947 if (lType->isPointerType()) {
948 if (rType->isPointerType())
949 return Context.IntTy;
950 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000951 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000952 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
953 lex->getSourceRange(), rex->getSourceRange());
954 return Context.IntTy; // the previous diagnostic is a GCC extension.
955 }
956 } else if (rType->isPointerType()) {
957 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000958 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000959 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
960 lex->getSourceRange(), rex->getSourceRange());
961 return Context.IntTy; // the previous diagnostic is a GCC extension.
962 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000963 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000964 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000965 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000966}
967
Steve Naroff218bc2b2007-05-04 21:54:46 +0000968inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
Steve Naroff7a5af782007-07-13 16:58:59 +0000969 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000970{
Steve Naroff31090012007-07-16 21:54:35 +0000971 UsualUnaryConversions(lex);
972 UsualUnaryConversions(rex);
973 QualType lType = lex->getType();
974 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000975
976 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000977 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000978
Steve Naroff75c17232007-06-13 21:41:08 +0000979 if (lType->isPointerType()) {
980 if (rType->isPointerType())
981 return Context.IntTy;
982 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000983 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000984 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
985 lex->getSourceRange(), rex->getSourceRange());
986 return Context.IntTy; // the previous diagnostic is a GCC extension.
987 }
988 } else if (rType->isPointerType()) {
989 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000990 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000991 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
992 lex->getSourceRange(), rex->getSourceRange());
993 return Context.IntTy; // the previous diagnostic is a GCC extension.
994 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000995 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000996 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000997 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000998}
999
Steve Naroff218bc2b2007-05-04 21:54:46 +00001000inline QualType Sema::CheckBitwiseOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +00001001 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001002{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001003 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001004 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001005
Steve Naroffdbd9e892007-07-17 00:58:39 +00001006 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001007
Steve Naroffdbd9e892007-07-17 00:58:39 +00001008 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
1009 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001010 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001011 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001012}
1013
Steve Naroff218bc2b2007-05-04 21:54:46 +00001014inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001015 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001016{
Steve Naroff31090012007-07-16 21:54:35 +00001017 UsualUnaryConversions(lex);
1018 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001019
Steve Naroffdbd9e892007-07-17 00:58:39 +00001020 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001021 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001022 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001023 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001024}
1025
Steve Naroff35d85152007-05-07 00:24:15 +00001026inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
1027 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001028{
Steve Naroff0af91202007-04-27 21:51:21 +00001029 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001030 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001031 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001032 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1033
1034 switch (mlval) { // C99 6.5.16p2
1035 case Expr::MLV_Valid:
1036 break;
1037 case Expr::MLV_ConstQualified:
1038 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1039 hadError = true;
1040 break;
1041 case Expr::MLV_ArrayType:
1042 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1043 lhsType.getAsString(), lex->getSourceRange());
1044 return QualType();
1045 case Expr::MLV_NotObjectType:
1046 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1047 lhsType.getAsString(), lex->getSourceRange());
1048 return QualType();
1049 case Expr::MLV_InvalidExpression:
1050 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1051 lex->getSourceRange());
1052 return QualType();
1053 case Expr::MLV_IncompleteType:
1054 case Expr::MLV_IncompleteVoidType:
1055 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1056 lhsType.getAsString(), lex->getSourceRange());
1057 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001058 }
1059 if (lhsType == rhsType) // common case, fast path...
1060 return lhsType;
1061
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001062 AssignmentCheckResult result;
1063
1064 if (compoundType.isNull())
1065 result = CheckSingleAssignmentConstraints(lhsType, rex);
1066 else
1067 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1068
Steve Naroff218bc2b2007-05-04 21:54:46 +00001069 // decode the result (notice that extensions still return a type).
1070 switch (result) {
1071 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001072 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001073 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001074 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001075 lhsType.getAsString(), rhsType.getAsString(),
1076 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001077 hadError = true;
1078 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001079 case PointerFromInt:
1080 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001081 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001082 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1083 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001084 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001085 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001086 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001087 case IntFromPointer:
1088 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1089 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001090 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001091 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001092 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001093 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1094 lhsType.getAsString(), rhsType.getAsString(),
1095 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001096 break;
1097 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001098 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1099 lhsType.getAsString(), rhsType.getAsString(),
1100 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001101 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001102 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001103 // C99 6.5.16p3: The type of an assignment expression is the type of the
1104 // left operand unless the left operand has qualified type, in which case
1105 // it is the unqualified version of the type of the left operand.
1106 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1107 // is converted to the type of the assignment expression (above).
1108 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1109 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001110}
1111
Steve Naroff218bc2b2007-05-04 21:54:46 +00001112inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001113 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001114 UsualUnaryConversions(rex);
1115 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001116}
1117
Steve Naroff7a5af782007-07-13 16:58:59 +00001118/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1119/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001120QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001121 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001122 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001123
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001124 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +00001125 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1126 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001127 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1128 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001129 return QualType();
1130 }
1131 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +00001132 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001133 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1134 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001135 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001136 }
Steve Naroff094046f2007-05-13 03:21:25 +00001137 // At this point, we know we have a real or pointer type. Now make sure
1138 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001139 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1140 if (mlval != Expr::MLV_Valid) {
1141 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001142 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001143 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001144 return QualType();
1145 }
1146 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001147}
1148
Steve Naroff1926c832007-04-24 00:23:05 +00001149/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001150/// This routine allows us to typecheck complex/recursive expressions
1151/// where the declaration is needed for type checking. Here are some
1152/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001153static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001154 switch (e->getStmtClass()) {
1155 case Stmt::DeclRefExprClass:
1156 return cast<DeclRefExpr>(e)->getDecl();
1157 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001158 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001159 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001160 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001161 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001162 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001163 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001164 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001165 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001166 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001167 default:
1168 return 0;
1169 }
1170}
1171
1172/// CheckAddressOfOperand - The operand of & must be either a function
1173/// designator or an lvalue designating an object. If it is an lvalue, the
1174/// object cannot be declared with storage class register or be a bit field.
1175/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001176/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001177QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001178 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001179 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001180
Steve Naroff9358c712007-05-27 23:58:33 +00001181 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001182 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1183 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001184 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001185 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1186 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001187 return QualType();
1188 }
Steve Naroff47500512007-04-19 23:00:49 +00001189 } else if (dcl) {
1190 // We have an lvalue with a decl. Make sure the decl is not declared
1191 // with the register storage-class specifier.
1192 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001193 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001194 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001195 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001196 return QualType();
1197 }
Steve Narofff633d092007-04-25 19:01:39 +00001198 } else
1199 assert(0 && "Unknown/unexpected decl type");
1200
Steve Naroff47500512007-04-19 23:00:49 +00001201 // FIXME: add check for bitfields!
1202 }
1203 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001204 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001205}
1206
Steve Naroff35d85152007-05-07 00:24:15 +00001207QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001208 UsualUnaryConversions(op);
1209 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001210
Steve Naroff758ada12007-05-28 16:15:57 +00001211 if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) {
1212 QualType ptype = PT->getPointeeType();
1213 // C99 6.5.3.2p4. "if it points to an object,...".
1214 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1215 // GCC compat: special case 'void *' (treat as warning).
1216 if (ptype->isVoidType()) {
1217 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001218 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001219 } else {
1220 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001221 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001222 return QualType();
1223 }
1224 }
1225 return ptype;
1226 }
1227 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001228 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001229 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001230}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001231
1232static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1233 tok::TokenKind Kind) {
1234 BinaryOperator::Opcode Opc;
1235 switch (Kind) {
1236 default: assert(0 && "Unknown binop!");
1237 case tok::star: Opc = BinaryOperator::Mul; break;
1238 case tok::slash: Opc = BinaryOperator::Div; break;
1239 case tok::percent: Opc = BinaryOperator::Rem; break;
1240 case tok::plus: Opc = BinaryOperator::Add; break;
1241 case tok::minus: Opc = BinaryOperator::Sub; break;
1242 case tok::lessless: Opc = BinaryOperator::Shl; break;
1243 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1244 case tok::lessequal: Opc = BinaryOperator::LE; break;
1245 case tok::less: Opc = BinaryOperator::LT; break;
1246 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1247 case tok::greater: Opc = BinaryOperator::GT; break;
1248 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1249 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1250 case tok::amp: Opc = BinaryOperator::And; break;
1251 case tok::caret: Opc = BinaryOperator::Xor; break;
1252 case tok::pipe: Opc = BinaryOperator::Or; break;
1253 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1254 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1255 case tok::equal: Opc = BinaryOperator::Assign; break;
1256 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1257 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1258 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1259 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1260 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1261 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1262 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1263 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1264 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1265 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1266 case tok::comma: Opc = BinaryOperator::Comma; break;
1267 }
1268 return Opc;
1269}
1270
Steve Naroff35d85152007-05-07 00:24:15 +00001271static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1272 tok::TokenKind Kind) {
1273 UnaryOperator::Opcode Opc;
1274 switch (Kind) {
1275 default: assert(0 && "Unknown unary op!");
1276 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1277 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1278 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1279 case tok::star: Opc = UnaryOperator::Deref; break;
1280 case tok::plus: Opc = UnaryOperator::Plus; break;
1281 case tok::minus: Opc = UnaryOperator::Minus; break;
1282 case tok::tilde: Opc = UnaryOperator::Not; break;
1283 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1284 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1285 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1286 case tok::kw___real: Opc = UnaryOperator::Real; break;
1287 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001288 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001289 }
1290 return Opc;
1291}
1292
Steve Naroff218bc2b2007-05-04 21:54:46 +00001293// Binary Operators. 'Tok' is the token for the operator.
1294Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1295 ExprTy *LHS, ExprTy *RHS) {
1296 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1297 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1298
1299 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1300 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1301
Chris Lattner256c21b2007-06-28 03:53:10 +00001302 QualType ResultTy; // Result type of the binary operator.
1303 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001304
1305 switch (Opc) {
1306 default:
1307 assert(0 && "Unknown binary expr!");
1308 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001309 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001310 break;
1311 case BinaryOperator::Mul:
1312 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001313 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001314 break;
1315 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001316 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001317 break;
1318 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001319 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001320 break;
1321 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001322 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001323 break;
1324 case BinaryOperator::Shl:
1325 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001326 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001327 break;
1328 case BinaryOperator::LE:
1329 case BinaryOperator::LT:
1330 case BinaryOperator::GE:
1331 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001332 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001333 break;
1334 case BinaryOperator::EQ:
1335 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001336 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001337 break;
1338 case BinaryOperator::And:
1339 case BinaryOperator::Xor:
1340 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001341 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001342 break;
1343 case BinaryOperator::LAnd:
1344 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001345 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001346 break;
1347 case BinaryOperator::MulAssign:
1348 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001349 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1350 if (!CompTy.isNull())
1351 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001352 break;
1353 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001354 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1355 if (!CompTy.isNull())
1356 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001357 break;
1358 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001359 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1360 if (!CompTy.isNull())
1361 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001362 break;
1363 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001364 CompTy = CheckSubtractionOperands(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::ShlAssign:
1369 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001370 CompTy = CheckShiftOperands(lhs, rhs, TokLoc);
1371 if (!CompTy.isNull())
1372 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001373 break;
1374 case BinaryOperator::AndAssign:
1375 case BinaryOperator::XorAssign:
1376 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001377 CompTy = CheckBitwiseOperands(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::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001382 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001383 break;
1384 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001385 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001386 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001387 if (CompTy.isNull())
1388 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1389 else
Chris Lattner9369a562007-06-29 16:31:29 +00001390 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001391}
1392
Steve Naroff35d85152007-05-07 00:24:15 +00001393// Unary Operators. 'Tok' is the token for the operator.
1394Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001395 ExprTy *input) {
1396 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001397 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1398 QualType resultType;
1399 switch (Opc) {
1400 default:
1401 assert(0 && "Unimplemented unary expr!");
1402 case UnaryOperator::PreInc:
1403 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001404 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001405 break;
1406 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001407 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001408 break;
1409 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001410 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001411 break;
1412 case UnaryOperator::Plus:
1413 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001414 UsualUnaryConversions(Input);
1415 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001416 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001417 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1418 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001419 break;
1420 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001421 UsualUnaryConversions(Input);
1422 resultType = Input->getType();
Steve Naroff1c8b7d32007-07-12 21:46:55 +00001423 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1424 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1425 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001426 break;
1427 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001428 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001429 DefaultFunctionArrayConversion(Input);
1430 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001431 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001432 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1433 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001434 // LNot always has type int. C99 6.5.3.3p5.
1435 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001436 break;
1437 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001438 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001439 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001440 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001441 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1442 break;
1443 case UnaryOperator::Extension:
1444 // FIXME: does __extension__ cause any promotions? I would think not.
1445 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001446 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001447 }
1448 if (resultType.isNull())
1449 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001450 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001451}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001452
1453/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1454Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1455 SourceLocation LabLoc,
1456 IdentifierInfo *LabelII) {
1457 // Look up the record for this label identifier.
1458 LabelStmt *&LabelDecl = LabelMap[LabelII];
1459
1460 // If we haven't seen this label yet, create a forward reference.
1461 if (LabelDecl == 0)
1462 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1463
1464 // Create the AST node. The address of a label always has type 'void*'.
1465 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1466 Context.getPointerType(Context.VoidTy));
1467}
1468