blob: 2f072f12ab0b4c2f676d545598bc9d5a2f672da6 [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 Lattnerc996b172007-07-31 16:53:04 +0000306 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
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 Lattnerc996b172007-07-31 16:53:04 +0000311 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000312 // 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 Lattner41977962007-07-31 19:29:30 +0000317 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
318 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000319 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
Steve Narofff8fd09e2007-07-27 22:15:19 +0000342QualType Sema::
343CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
344 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000345 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000346
347 // The vector accessor can't exceed the number of elements.
348 const char *compStr = CompName.getName();
349 if (strlen(compStr) > vecType->getNumElements()) {
350 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
351 baseType.getAsString(), SourceRange(CompLoc));
352 return QualType();
353 }
354 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000355 if (vecType->getPointAccessorIdx(*compStr) != -1) {
356 do
357 compStr++;
358 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
359 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
360 do
361 compStr++;
362 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
363 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
364 do
365 compStr++;
366 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
367 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000368
369 if (*compStr) {
370 // We didn't get to the end of the string. This means the component names
371 // didn't come from the same set *or* we encountered an illegal name.
372 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
373 std::string(compStr,compStr+1), SourceRange(CompLoc));
374 return QualType();
375 }
376 // Each component accessor can't exceed the vector type.
377 compStr = CompName.getName();
378 while (*compStr) {
379 if (vecType->isAccessorWithinNumElements(*compStr))
380 compStr++;
381 else
382 break;
383 }
384 if (*compStr) {
385 // We didn't get to the end of the string. This means a component accessor
386 // exceeds the number of elements in the vector.
387 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
388 baseType.getAsString(), SourceRange(CompLoc));
389 return QualType();
390 }
391 // The component accessor looks fine - now we need to compute the actual type.
392 // The vector type is implied by the component accessor. For example,
393 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
394 unsigned CompSize = strlen(CompName.getName());
395 if (CompSize == 1)
396 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000397
398 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
399 // Now look up the TypeDefDecl from the vector type. Without this,
400 // diagostics look bad. We want OCU vector types to appear built-in.
401 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
402 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
403 return Context.getTypedefType(OCUVectorDecls[i]);
404 }
405 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000406}
407
Chris Lattnere168f762006-11-10 05:29:30 +0000408Action::ExprResult Sema::
409ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
410 tok::TokenKind OpKind, SourceLocation MemberLoc,
411 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000412 Expr *BaseExpr = static_cast<Expr *>(Base);
413 assert(BaseExpr && "no record expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000414
Steve Naroff185616f2007-07-26 03:11:44 +0000415 QualType BaseType = BaseExpr->getType();
416 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000417
Steve Narofff1e53692007-03-23 22:27:02 +0000418 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000419 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000420 BaseType = PT->getPointeeType();
421 else
422 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
423 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000424 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000425 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000426 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000427 RecordDecl *RDecl = RTy->getDecl();
428 if (RTy->isIncompleteType())
429 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
430 BaseExpr->getSourceRange());
431 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000432 FieldDecl *MemberDecl = RDecl->getMember(&Member);
433 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000434 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
435 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000436 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
437 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
438 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
439 if (ret.isNull())
440 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000441 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Steve Naroff185616f2007-07-26 03:11:44 +0000442 } else
443 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
444 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000445}
446
447/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
448/// This provides the location of the left/right parens and a list of comma
449/// locations.
450Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000451ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
452 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000453 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000454 Expr *Fn = static_cast<Expr *>(fn);
455 Expr **Args = reinterpret_cast<Expr**>(args);
456 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000457
Chris Lattner38dbdb22007-07-21 03:03:59 +0000458 UsualUnaryConversions(Fn);
459 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000460
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000461 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
462 // type pointer to function".
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000463 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000464 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000465 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
466 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000467
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000468 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000469 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000470 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
471 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000472
Steve Naroff17f76e02007-05-03 21:03:48 +0000473 // If a prototype isn't declared, the parser implicitly defines a func decl
474 QualType resultType = funcT->getResultType();
475
476 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
477 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
478 // assignment, to the types of the corresponding parameter, ...
479
480 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000481 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000482
Steve Naroffb8c289d2007-05-08 22:18:00 +0000483 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000484 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000485 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000486 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000487 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000488 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000489 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000490 SourceRange(Args[NumArgsInProto]->getLocStart(),
491 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000492 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000493 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000494 }
495 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000496 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000497 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000498 assert(argExpr && "ParseCallExpr(): missing argument expression");
499
Steve Naroff17f76e02007-05-03 21:03:48 +0000500 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000501 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000502
Steve Naroffb8af1c22007-07-25 20:45:33 +0000503 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner41977962007-07-31 19:29:30 +0000504 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000505 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000506 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000507 lhsType = Context.getPointerType(lhsType);
508
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000509 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
510 argExpr);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000511 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000512
513 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000514 switch (result) {
515 case Compatible:
516 break;
517 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000518 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000519 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000520 Diag(l, diag::ext_typecheck_passing_pointer_int,
521 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000522 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000523 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000524 break;
525 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000526 Diag(l, diag::ext_typecheck_passing_pointer_int,
527 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000528 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000529 break;
530 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000531 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000532 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000533 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000534 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000535 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000536 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
537 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000538 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000539 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000540 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000541 return Diag(l, diag::err_typecheck_passing_incompatible,
542 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000543 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000544 }
545 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000546 // Even if the types checked, bail if we had the wrong number of arguments.
Chris Lattner38dbdb22007-07-21 03:03:59 +0000547 if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
Steve Naroffb8c289d2007-05-08 22:18:00 +0000548 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000549 }
Chris Lattner38dbdb22007-07-21 03:03:59 +0000550 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000551}
552
553Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000554ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000555 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000556 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
557 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000558 // FIXME: put back this assert when initializers are worked out.
559 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
560 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000561
562 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000563 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000564}
565
566Action::ExprResult Sema::
567ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
568 SourceLocation RParenLoc) {
569 // FIXME: add semantic analysis (C99 6.7.8). This involves
570 // knowledge of the object being intialized. As a result, the code for
571 // doing the semantic analysis will likely be located elsewhere (i.e. in
572 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
573 return false; // FIXME instantiate an InitListExpr.
574}
575
576Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000577ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
578 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000579 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
580
581 Expr *castExpr = static_cast<Expr*>(Op);
582 QualType castType = QualType::getFromOpaquePtr(Ty);
583
Chris Lattnerbd270732007-07-18 16:00:06 +0000584 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
585 // type needs to be scalar.
586 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000587 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
588 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
589 }
590 if (!castExpr->getType()->isScalarType()) {
591 return Diag(castExpr->getLocStart(),
592 diag::err_typecheck_expect_scalar_operand,
593 castExpr->getType().getAsString(), castExpr->getSourceRange());
594 }
595 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000596}
597
Steve Narofff8a28c52007-05-15 20:29:32 +0000598inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000599 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000600 UsualUnaryConversions(cond);
601 UsualUnaryConversions(lex);
602 UsualUnaryConversions(rex);
603 QualType condT = cond->getType();
604 QualType lexT = lex->getType();
605 QualType rexT = rex->getType();
606
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000607 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000608 if (!condT->isScalarType()) { // C99 6.5.15p2
609 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
610 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000611 return QualType();
612 }
613 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000614 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
615 UsualArithmeticConversions(lex, rex);
616 return lex->getType();
617 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000618 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
619 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
620
621 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
622 return lexT;
623
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000624 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000625 lexT.getAsString(), rexT.getAsString(),
626 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000627 return QualType();
628 }
629 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000630 // C99 6.5.15p3
631 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000632 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000633 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000634 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000635
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000636 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
637 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
638 // get the "pointed to" types
639 QualType lhptee = LHSPT->getPointeeType();
640 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000641
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000642 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
643 if (lhptee->isVoidType() &&
644 (rhptee->isObjectType() || rhptee->isIncompleteType()))
645 return lexT;
646 if (rhptee->isVoidType() &&
647 (lhptee->isObjectType() || lhptee->isIncompleteType()))
648 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000649
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000650 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
651 rhptee.getUnqualifiedType())) {
652 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
653 lexT.getAsString(), rexT.getAsString(),
654 lex->getSourceRange(), rex->getSourceRange());
655 return lexT; // FIXME: this is an _ext - is this return o.k?
656 }
657 // The pointer types are compatible.
658 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
659 // differently qualified versions of compatible types, the result type is a
660 // pointer to an appropriately qualified version of the *composite* type.
661 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000662 }
663 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000664
Steve Naroff7a5af782007-07-13 16:58:59 +0000665 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
666 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000667
668 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000669 lexT.getAsString(), rexT.getAsString(),
670 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000671 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000672}
673
Chris Lattnere168f762006-11-10 05:29:30 +0000674/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
675/// in the case of a the GNU conditional expr extension.
676Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
677 SourceLocation ColonLoc,
678 ExprTy *Cond, ExprTy *LHS,
679 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000680 Expr *CondExpr = (Expr *) Cond;
681 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
682 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
683 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000684 if (result.isNull())
685 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000686 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000687}
688
Steve Naroff81569d22007-07-15 02:02:06 +0000689// promoteExprToType - a helper function to ensure we create exactly one
690// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000691static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000692 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
693 impCast->setType(type);
694 else
695 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000696 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000697}
698
699/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000700void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000701 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000702 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000703
Chris Lattnercd1d0862007-07-31 16:56:34 +0000704 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000705 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
706 t = e->getType();
707 }
Steve Naroff81569d22007-07-15 02:02:06 +0000708 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000709 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000710 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000711 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000712}
713
Steve Naroff71b59a92007-06-04 22:22:31 +0000714/// UsualUnaryConversion - Performs various conversions that are common to most
715/// operators (C99 6.3). The conversions of array and function types are
716/// sometimes surpressed. For example, the array->pointer conversion doesn't
717/// apply if the array is an argument to the sizeof or address (&) operators.
718/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000719void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000720 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000721 assert(!t.isNull() && "UsualUnaryConversions - missing type");
722
Chris Lattnercd1d0862007-07-31 16:56:34 +0000723 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000724 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
725 t = expr->getType();
726 }
Steve Naroff81569d22007-07-15 02:02:06 +0000727 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000728 promoteExprToType(expr, Context.IntTy);
729 else
730 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000731}
732
Steve Naroff1926c832007-04-24 00:23:05 +0000733/// UsualArithmeticConversions - Performs various conversions that are common to
734/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
735/// routine returns the first non-arithmetic type found. The client is
736/// responsible for emitting appropriate error diagnostics.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000737void Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr) {
Steve Naroff31090012007-07-16 21:54:35 +0000738 UsualUnaryConversions(lhsExpr);
739 UsualUnaryConversions(rhsExpr);
740
Steve Naroff94a5aca2007-07-16 22:23:01 +0000741 QualType lhs = lhsExpr->getType();
742 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000743
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000744 // If both types are identical, no conversion is needed.
745 if (lhs == rhs)
Steve Naroffdbd9e892007-07-17 00:58:39 +0000746 return;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000747
748 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
749 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000750 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
751 return;
Steve Naroff1926c832007-04-24 00:23:05 +0000752
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000753 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000754
755 // Handle complex types first (C99 6.3.1.8p1).
756 if (lhs->isComplexType() || rhs->isComplexType()) {
757 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000758 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
759 promoteExprToType(rhsExpr, lhs);
760 return;
761 }
762 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
763 promoteExprToType(lhsExpr, rhs);
764 return;
765 }
Steve Naroff81569d22007-07-15 02:02:06 +0000766 // Two complex types. Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000767 if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs
768 promoteExprToType(rhsExpr, lhs);
769 return;
770 }
771 promoteExprToType(lhsExpr, rhs); // convert the lhs
772 return;
Steve Naroffbf223ba2007-04-24 20:56:26 +0000773 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000774 // Now handle "real" floating types (i.e. float, double, long double).
775 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
776 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000777 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
778 promoteExprToType(rhsExpr, lhs);
779 return;
780 }
781 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
782 promoteExprToType(lhsExpr, rhs);
783 return;
784 }
Steve Naroff81569d22007-07-15 02:02:06 +0000785 // We have two real floating types, float/complex combos were handled above.
786 // Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000787 if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs
788 promoteExprToType(rhsExpr, lhs);
789 return;
790 }
791 promoteExprToType(lhsExpr, rhs); // convert the lhs
792 return;
Steve Naroffb01bbe32007-04-25 01:22:31 +0000793 }
Steve Naroff81569d22007-07-15 02:02:06 +0000794 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000795 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
796 promoteExprToType(rhsExpr, lhs);
797 return;
798 }
799 promoteExprToType(lhsExpr, rhs); // convert the lhs
800 return;
Steve Narofff1e53692007-03-23 22:27:02 +0000801}
802
Steve Naroff3f597292007-05-11 22:18:03 +0000803// CheckPointerTypesForAssignment - This is a very tricky routine (despite
804// being closely modeled after the C99 spec:-). The odd characteristic of this
805// routine is it effectively iqnores the qualifiers on the top level pointee.
806// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
807// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000808Sema::AssignmentCheckResult
809Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000810 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000811
812 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000813 lhptee = lhsType->getAsPointerType()->getPointeeType();
814 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000815
816 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000817 lhptee = lhptee.getCanonicalType();
818 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000819
Steve Naroff98cf3e92007-06-06 18:38:38 +0000820 AssignmentCheckResult r = Compatible;
821
Steve Naroff3f597292007-05-11 22:18:03 +0000822 // C99 6.5.16.1p1: This following citation is common to constraints
823 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
824 // qualifiers of the type *pointed to* by the right;
825 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
826 rhptee.getQualifiers())
827 r = CompatiblePointerDiscardsQualifiers;
828
829 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
830 // incomplete type and the other is a pointer to a qualified or unqualified
831 // version of void...
832 if (lhptee.getUnqualifiedType()->isVoidType() &&
833 (rhptee->isObjectType() || rhptee->isIncompleteType()))
834 ;
835 else if (rhptee.getUnqualifiedType()->isVoidType() &&
836 (lhptee->isObjectType() || lhptee->isIncompleteType()))
837 ;
838 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
839 // unqualified versions of compatible types, ...
840 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
841 rhptee.getUnqualifiedType()))
842 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000843 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000844}
845
Steve Naroff98cf3e92007-06-06 18:38:38 +0000846/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000847/// has code to accommodate several GCC extensions when type checking
848/// pointers. Here are some objectionable examples that GCC considers warnings:
849///
850/// int a, *pint;
851/// short *pshort;
852/// struct foo *pfoo;
853///
854/// pint = pshort; // warning: assignment from incompatible pointer type
855/// a = pint; // warning: assignment makes integer from pointer without a cast
856/// pint = a; // warning: assignment makes pointer from integer without a cast
857/// pint = pfoo; // warning: assignment from incompatible pointer type
858///
859/// As a result, the code for dealing with pointers is more complex than the
860/// C99 spec dictates.
861/// Note: the warning above turn into errors when -pedantic-errors is enabled.
862///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000863Sema::AssignmentCheckResult
864Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000865 if (lhsType == rhsType) // common case, fast path...
866 return Compatible;
867
Steve Naroff84ff4b42007-07-09 21:31:10 +0000868 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000869 if (lhsType->isVectorType() || rhsType->isVectorType()) {
870 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
871 return Incompatible;
872 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000873 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000874 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000875 if (rhsType->isIntegerType())
876 return PointerFromInt;
877
Steve Naroff3f597292007-05-11 22:18:03 +0000878 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000879 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000880 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000881 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
882 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
883 return IntFromPointer;
884
Steve Naroff3f597292007-05-11 22:18:03 +0000885 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000886 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000887 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
888 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000889 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000890 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
891 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000892 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000893 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000894 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000895}
896
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000897Sema::AssignmentCheckResult
898Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
899 // This check seems unnatural, however it is necessary to insure the proper
900 // conversion of functions/arrays. If the conversion were done for all
901 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
902 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +0000903 DefaultFunctionArrayConversion(rExpr);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000904
Steve Naroff31090012007-07-16 21:54:35 +0000905 return CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000906}
907
908Sema::AssignmentCheckResult
909Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
910 return CheckAssignmentConstraints(lhsType, rhsType);
911}
912
Steve Naroff7a5af782007-07-13 16:58:59 +0000913inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000914 Diag(loc, diag::err_typecheck_invalid_operands,
915 lex->getType().getAsString(), rex->getType().getAsString(),
916 lex->getSourceRange(), rex->getSourceRange());
917}
918
Steve Naroff7a5af782007-07-13 16:58:59 +0000919inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
920 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000921 QualType lhsType = lex->getType(), rhsType = rex->getType();
922
923 // make sure the vector types are identical.
924 if (lhsType == rhsType)
925 return lhsType;
926 // You cannot convert between vector values of different size.
927 Diag(loc, diag::err_typecheck_vector_not_convertable,
928 lex->getType().getAsString(), rex->getType().getAsString(),
929 lex->getSourceRange(), rex->getSourceRange());
930 return QualType();
931}
932
Steve Naroff218bc2b2007-05-04 21:54:46 +0000933inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000934 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000935{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000936 QualType lhsType = lex->getType(), rhsType = rex->getType();
937
938 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000939 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +0000940
Steve Naroffdbd9e892007-07-17 00:58:39 +0000941 UsualArithmeticConversions(lex, rex);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000942
Steve Naroffdbd9e892007-07-17 00:58:39 +0000943 // handle the common case first (both operands are arithmetic).
944 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
945 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000946 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000947 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000948}
949
Steve Naroff218bc2b2007-05-04 21:54:46 +0000950inline QualType Sema::CheckRemainderOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000951 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000952{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000953 QualType lhsType = lex->getType(), rhsType = rex->getType();
954
Steve Naroffdbd9e892007-07-17 00:58:39 +0000955 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000956
Steve Naroffdbd9e892007-07-17 00:58:39 +0000957 // handle the common case first (both operands are arithmetic).
958 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
959 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000960 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000961 return QualType();
962}
963
964inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000965 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000966{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000967 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +0000968 return CheckVectorOperands(loc, lex, rex);
969
Steve Naroffdbd9e892007-07-17 00:58:39 +0000970 UsualArithmeticConversions(lex, rex);
Steve Naroff94a5aca2007-07-16 22:23:01 +0000971
Steve Naroffe4718892007-04-27 18:30:00 +0000972 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000973 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
974 return lex->getType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000975
Steve Naroffdbd9e892007-07-17 00:58:39 +0000976 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
977 return lex->getType();
978 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
979 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000980 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000981 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000982}
983
Steve Naroff218bc2b2007-05-04 21:54:46 +0000984inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000985 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000986{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000987 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000988 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000989
Steve Naroffdbd9e892007-07-17 00:58:39 +0000990 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000991
992 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000993 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
994 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000995
996 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffdbd9e892007-07-17 00:58:39 +0000997 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000998 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000999 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001000 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001001 return QualType();
1002}
1003
1004inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff7a5af782007-07-13 16:58:59 +00001005 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001006{
Chris Lattner1d411a82007-06-05 20:52:21 +00001007 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1008 // for int << longlong -> the result type should be int, not long long.
Steve Naroffdbd9e892007-07-17 00:58:39 +00001009 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001010
Steve Naroffdbd9e892007-07-17 00:58:39 +00001011 // handle the common case first (both operands are arithmetic).
1012 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
1013 return lex->getType();
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::CheckRelationalOperands( // C99 6.5.8
Steve Naroff7a5af782007-07-13 16:58:59 +00001019 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001020{
Steve Naroff31090012007-07-16 21:54:35 +00001021 UsualUnaryConversions(lex);
1022 UsualUnaryConversions(rex);
1023 QualType lType = lex->getType();
1024 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001025
1026 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001027 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +00001028
Steve Naroff75c17232007-06-13 21:41:08 +00001029 if (lType->isPointerType()) {
1030 if (rType->isPointerType())
1031 return Context.IntTy;
1032 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001033 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001034 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1035 lex->getSourceRange(), rex->getSourceRange());
1036 return Context.IntTy; // the previous diagnostic is a GCC extension.
1037 }
1038 } else if (rType->isPointerType()) {
1039 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001040 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001041 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1042 lex->getSourceRange(), rex->getSourceRange());
1043 return Context.IntTy; // the previous diagnostic is a GCC extension.
1044 }
Steve Naroff043d45d2007-05-15 02:32:35 +00001045 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001046 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001047 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001048}
1049
Steve Naroff218bc2b2007-05-04 21:54:46 +00001050inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
Steve Naroff7a5af782007-07-13 16:58:59 +00001051 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001052{
Steve Naroff31090012007-07-16 21:54:35 +00001053 UsualUnaryConversions(lex);
1054 UsualUnaryConversions(rex);
1055 QualType lType = lex->getType();
1056 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001057
1058 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001059 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001060
Steve Naroff75c17232007-06-13 21:41:08 +00001061 if (lType->isPointerType()) {
1062 if (rType->isPointerType())
1063 return Context.IntTy;
1064 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001065 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001066 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1067 lex->getSourceRange(), rex->getSourceRange());
1068 return Context.IntTy; // the previous diagnostic is a GCC extension.
1069 }
1070 } else if (rType->isPointerType()) {
1071 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001072 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001073 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1074 lex->getSourceRange(), rex->getSourceRange());
1075 return Context.IntTy; // the previous diagnostic is a GCC extension.
1076 }
Steve Naroff043d45d2007-05-15 02:32:35 +00001077 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001078 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001079 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001080}
1081
Steve Naroff218bc2b2007-05-04 21:54:46 +00001082inline QualType Sema::CheckBitwiseOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +00001083 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001084{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001085 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001086 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001087
Steve Naroffdbd9e892007-07-17 00:58:39 +00001088 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001089
Steve Naroffdbd9e892007-07-17 00:58:39 +00001090 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
1091 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001092 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001093 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001094}
1095
Steve Naroff218bc2b2007-05-04 21:54:46 +00001096inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001097 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001098{
Steve Naroff31090012007-07-16 21:54:35 +00001099 UsualUnaryConversions(lex);
1100 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001101
Steve Naroffdbd9e892007-07-17 00:58:39 +00001102 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001103 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001104 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001105 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001106}
1107
Steve Naroff35d85152007-05-07 00:24:15 +00001108inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
1109 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001110{
Steve Naroff0af91202007-04-27 21:51:21 +00001111 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001112 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001113 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001114 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1115
1116 switch (mlval) { // C99 6.5.16p2
1117 case Expr::MLV_Valid:
1118 break;
1119 case Expr::MLV_ConstQualified:
1120 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1121 hadError = true;
1122 break;
1123 case Expr::MLV_ArrayType:
1124 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1125 lhsType.getAsString(), lex->getSourceRange());
1126 return QualType();
1127 case Expr::MLV_NotObjectType:
1128 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1129 lhsType.getAsString(), lex->getSourceRange());
1130 return QualType();
1131 case Expr::MLV_InvalidExpression:
1132 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1133 lex->getSourceRange());
1134 return QualType();
1135 case Expr::MLV_IncompleteType:
1136 case Expr::MLV_IncompleteVoidType:
1137 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1138 lhsType.getAsString(), lex->getSourceRange());
1139 return QualType();
Steve Naroff0d595ca2007-07-30 03:29:09 +00001140 case Expr::MLV_DuplicateVectorComponents:
1141 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1142 lex->getSourceRange());
1143 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001144 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001145 AssignmentCheckResult result;
1146
1147 if (compoundType.isNull())
1148 result = CheckSingleAssignmentConstraints(lhsType, rex);
1149 else
1150 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffad373bd2007-07-31 12:34:36 +00001151
Steve Naroff218bc2b2007-05-04 21:54:46 +00001152 // decode the result (notice that extensions still return a type).
1153 switch (result) {
1154 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001155 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001156 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001157 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001158 lhsType.getAsString(), rhsType.getAsString(),
1159 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001160 hadError = true;
1161 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001162 case PointerFromInt:
1163 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001164 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001165 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1166 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001167 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001168 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001169 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001170 case IntFromPointer:
1171 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1172 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001173 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001174 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001175 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001176 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1177 lhsType.getAsString(), rhsType.getAsString(),
1178 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001179 break;
1180 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001181 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1182 lhsType.getAsString(), rhsType.getAsString(),
1183 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001184 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001185 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001186 // C99 6.5.16p3: The type of an assignment expression is the type of the
1187 // left operand unless the left operand has qualified type, in which case
1188 // it is the unqualified version of the type of the left operand.
1189 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1190 // is converted to the type of the assignment expression (above).
1191 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1192 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001193}
1194
Steve Naroff218bc2b2007-05-04 21:54:46 +00001195inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001196 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001197 UsualUnaryConversions(rex);
1198 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001199}
1200
Steve Naroff7a5af782007-07-13 16:58:59 +00001201/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1202/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001203QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001204 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001205 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001206
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001207 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +00001208 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1209 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001210 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1211 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001212 return QualType();
1213 }
1214 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +00001215 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001216 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1217 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001218 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001219 }
Steve Naroff094046f2007-05-13 03:21:25 +00001220 // At this point, we know we have a real or pointer type. Now make sure
1221 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001222 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1223 if (mlval != Expr::MLV_Valid) {
1224 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001225 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001226 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001227 return QualType();
1228 }
1229 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001230}
1231
Steve Naroff1926c832007-04-24 00:23:05 +00001232/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001233/// This routine allows us to typecheck complex/recursive expressions
1234/// where the declaration is needed for type checking. Here are some
1235/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001236static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001237 switch (e->getStmtClass()) {
1238 case Stmt::DeclRefExprClass:
1239 return cast<DeclRefExpr>(e)->getDecl();
1240 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001241 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001242 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001243 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001244 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001245 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001246 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001247 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001248 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001249 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001250 default:
1251 return 0;
1252 }
1253}
1254
1255/// CheckAddressOfOperand - The operand of & must be either a function
1256/// designator or an lvalue designating an object. If it is an lvalue, the
1257/// object cannot be declared with storage class register or be a bit field.
1258/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001259/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001260QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001261 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001262 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001263
Steve Naroff9358c712007-05-27 23:58:33 +00001264 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001265 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1266 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001267 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001268 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1269 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001270 return QualType();
1271 }
Steve Naroff47500512007-04-19 23:00:49 +00001272 } else if (dcl) {
1273 // We have an lvalue with a decl. Make sure the decl is not declared
1274 // with the register storage-class specifier.
1275 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001276 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001277 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001278 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001279 return QualType();
1280 }
Steve Narofff633d092007-04-25 19:01:39 +00001281 } else
1282 assert(0 && "Unknown/unexpected decl type");
1283
Steve Naroff47500512007-04-19 23:00:49 +00001284 // FIXME: add check for bitfields!
1285 }
1286 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001287 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001288}
1289
Steve Naroff35d85152007-05-07 00:24:15 +00001290QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001291 UsualUnaryConversions(op);
1292 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001293
Chris Lattnerc996b172007-07-31 16:53:04 +00001294 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001295 QualType ptype = PT->getPointeeType();
1296 // C99 6.5.3.2p4. "if it points to an object,...".
1297 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1298 // GCC compat: special case 'void *' (treat as warning).
1299 if (ptype->isVoidType()) {
1300 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001301 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001302 } else {
1303 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001304 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001305 return QualType();
1306 }
1307 }
1308 return ptype;
1309 }
1310 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001311 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001312 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001313}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001314
1315static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1316 tok::TokenKind Kind) {
1317 BinaryOperator::Opcode Opc;
1318 switch (Kind) {
1319 default: assert(0 && "Unknown binop!");
1320 case tok::star: Opc = BinaryOperator::Mul; break;
1321 case tok::slash: Opc = BinaryOperator::Div; break;
1322 case tok::percent: Opc = BinaryOperator::Rem; break;
1323 case tok::plus: Opc = BinaryOperator::Add; break;
1324 case tok::minus: Opc = BinaryOperator::Sub; break;
1325 case tok::lessless: Opc = BinaryOperator::Shl; break;
1326 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1327 case tok::lessequal: Opc = BinaryOperator::LE; break;
1328 case tok::less: Opc = BinaryOperator::LT; break;
1329 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1330 case tok::greater: Opc = BinaryOperator::GT; break;
1331 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1332 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1333 case tok::amp: Opc = BinaryOperator::And; break;
1334 case tok::caret: Opc = BinaryOperator::Xor; break;
1335 case tok::pipe: Opc = BinaryOperator::Or; break;
1336 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1337 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1338 case tok::equal: Opc = BinaryOperator::Assign; break;
1339 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1340 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1341 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1342 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1343 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1344 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1345 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1346 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1347 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1348 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1349 case tok::comma: Opc = BinaryOperator::Comma; break;
1350 }
1351 return Opc;
1352}
1353
Steve Naroff35d85152007-05-07 00:24:15 +00001354static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1355 tok::TokenKind Kind) {
1356 UnaryOperator::Opcode Opc;
1357 switch (Kind) {
1358 default: assert(0 && "Unknown unary op!");
1359 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1360 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1361 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1362 case tok::star: Opc = UnaryOperator::Deref; break;
1363 case tok::plus: Opc = UnaryOperator::Plus; break;
1364 case tok::minus: Opc = UnaryOperator::Minus; break;
1365 case tok::tilde: Opc = UnaryOperator::Not; break;
1366 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1367 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1368 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1369 case tok::kw___real: Opc = UnaryOperator::Real; break;
1370 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001371 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001372 }
1373 return Opc;
1374}
1375
Steve Naroff218bc2b2007-05-04 21:54:46 +00001376// Binary Operators. 'Tok' is the token for the operator.
1377Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1378 ExprTy *LHS, ExprTy *RHS) {
1379 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1380 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1381
1382 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1383 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1384
Chris Lattner256c21b2007-06-28 03:53:10 +00001385 QualType ResultTy; // Result type of the binary operator.
1386 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001387
1388 switch (Opc) {
1389 default:
1390 assert(0 && "Unknown binary expr!");
1391 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001392 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001393 break;
1394 case BinaryOperator::Mul:
1395 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001396 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001397 break;
1398 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001399 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001400 break;
1401 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001402 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001403 break;
1404 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001405 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001406 break;
1407 case BinaryOperator::Shl:
1408 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001409 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001410 break;
1411 case BinaryOperator::LE:
1412 case BinaryOperator::LT:
1413 case BinaryOperator::GE:
1414 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001415 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001416 break;
1417 case BinaryOperator::EQ:
1418 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001419 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001420 break;
1421 case BinaryOperator::And:
1422 case BinaryOperator::Xor:
1423 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001424 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001425 break;
1426 case BinaryOperator::LAnd:
1427 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001428 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001429 break;
1430 case BinaryOperator::MulAssign:
1431 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001432 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1433 if (!CompTy.isNull())
1434 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001435 break;
1436 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001437 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1438 if (!CompTy.isNull())
1439 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001440 break;
1441 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001442 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1443 if (!CompTy.isNull())
1444 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001445 break;
1446 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001447 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1448 if (!CompTy.isNull())
1449 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001450 break;
1451 case BinaryOperator::ShlAssign:
1452 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001453 CompTy = CheckShiftOperands(lhs, rhs, TokLoc);
1454 if (!CompTy.isNull())
1455 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001456 break;
1457 case BinaryOperator::AndAssign:
1458 case BinaryOperator::XorAssign:
1459 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001460 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1461 if (!CompTy.isNull())
1462 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001463 break;
1464 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001465 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001466 break;
1467 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001468 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001469 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001470 if (CompTy.isNull())
1471 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1472 else
Chris Lattner9369a562007-06-29 16:31:29 +00001473 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001474}
1475
Steve Naroff35d85152007-05-07 00:24:15 +00001476// Unary Operators. 'Tok' is the token for the operator.
1477Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001478 ExprTy *input) {
1479 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001480 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1481 QualType resultType;
1482 switch (Opc) {
1483 default:
1484 assert(0 && "Unimplemented unary expr!");
1485 case UnaryOperator::PreInc:
1486 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001487 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001488 break;
1489 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001490 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001491 break;
1492 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001493 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001494 break;
1495 case UnaryOperator::Plus:
1496 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001497 UsualUnaryConversions(Input);
1498 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001499 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001500 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1501 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001502 break;
1503 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001504 UsualUnaryConversions(Input);
1505 resultType = Input->getType();
Steve Naroff1c8b7d32007-07-12 21:46:55 +00001506 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1507 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1508 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001509 break;
1510 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001511 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001512 DefaultFunctionArrayConversion(Input);
1513 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001514 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001515 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1516 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001517 // LNot always has type int. C99 6.5.3.3p5.
1518 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001519 break;
1520 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001521 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001522 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001523 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001524 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1525 break;
1526 case UnaryOperator::Extension:
1527 // FIXME: does __extension__ cause any promotions? I would think not.
1528 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001529 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001530 }
1531 if (resultType.isNull())
1532 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001533 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001534}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001535
1536/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1537Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1538 SourceLocation LabLoc,
1539 IdentifierInfo *LabelII) {
1540 // Look up the record for this label identifier.
1541 LabelStmt *&LabelDecl = LabelMap[LabelII];
1542
1543 // If we haven't seen this label yet, create a forward reference.
1544 if (LabelDecl == 0)
1545 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1546
1547 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001548 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1549 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001550}
1551
Chris Lattner366727f2007-07-24 16:58:17 +00001552Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
1553 SourceLocation RPLoc) { // "({..})"
1554 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1555 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1556 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1557
1558 // FIXME: there are a variety of strange constraints to enforce here, for
1559 // example, it is not possible to goto into a stmt expression apparently.
1560 // More semantic analysis is needed.
1561
1562 // FIXME: the last statement in the compount stmt has its value used. We
1563 // should not warn about it being unused.
1564
1565 // If there are sub stmts in the compound stmt, take the type of the last one
1566 // as the type of the stmtexpr.
1567 QualType Ty = Context.VoidTy;
1568
1569 if (!Compound->body_empty())
1570 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1571 Ty = LastExpr->getType();
1572
1573 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1574}
Steve Naroff78864672007-08-01 22:05:33 +00001575
Steve Naroff788d8642007-08-01 23:45:51 +00001576Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00001577 TypeTy *arg1, TypeTy *arg2,
1578 SourceLocation RPLoc) {
1579 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1580 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1581
1582 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1583
Steve Naroff788d8642007-08-01 23:45:51 +00001584 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2, RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00001585}
1586