blob: 9dfcd7d5eab08f760f71402a6c25b0368e9b1fb0 [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;
Steve Naroff01047312007-08-03 22:40:33 +0000320
321 // Component access limited to variables (reject vec4.rg[1]).
322 if (!isa<DeclRefExpr>(BaseExpr))
323 return Diag(LLoc, diag::err_ocuvector_component_access,
324 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000325 // FIXME: need to deal with const...
326 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000327 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000328 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
329 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000330 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000331 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000332 if (!IndexExpr->getType()->isIntegerType())
333 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
334 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000335
Chris Lattner36d572b2007-07-16 00:14:47 +0000336 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
337 // the following check catches trying to index a pointer to a function (e.g.
338 // void (*)(int)). Functions are not objects in C99.
339 if (!ResultType->isObjectType())
340 return Diag(BaseExpr->getLocStart(),
341 diag::err_typecheck_subscript_not_object,
342 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
343
344 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000345}
346
Steve Narofff8fd09e2007-07-27 22:15:19 +0000347QualType Sema::
348CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
349 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000350 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000351
352 // The vector accessor can't exceed the number of elements.
353 const char *compStr = CompName.getName();
354 if (strlen(compStr) > vecType->getNumElements()) {
355 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
356 baseType.getAsString(), SourceRange(CompLoc));
357 return QualType();
358 }
359 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000360 if (vecType->getPointAccessorIdx(*compStr) != -1) {
361 do
362 compStr++;
363 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
364 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
365 do
366 compStr++;
367 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
368 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
369 do
370 compStr++;
371 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
372 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000373
374 if (*compStr) {
375 // We didn't get to the end of the string. This means the component names
376 // didn't come from the same set *or* we encountered an illegal name.
377 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
378 std::string(compStr,compStr+1), SourceRange(CompLoc));
379 return QualType();
380 }
381 // Each component accessor can't exceed the vector type.
382 compStr = CompName.getName();
383 while (*compStr) {
384 if (vecType->isAccessorWithinNumElements(*compStr))
385 compStr++;
386 else
387 break;
388 }
389 if (*compStr) {
390 // We didn't get to the end of the string. This means a component accessor
391 // exceeds the number of elements in the vector.
392 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
393 baseType.getAsString(), SourceRange(CompLoc));
394 return QualType();
395 }
396 // The component accessor looks fine - now we need to compute the actual type.
397 // The vector type is implied by the component accessor. For example,
398 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
399 unsigned CompSize = strlen(CompName.getName());
400 if (CompSize == 1)
401 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000402
403 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
404 // Now look up the TypeDefDecl from the vector type. Without this,
405 // diagostics look bad. We want OCU vector types to appear built-in.
406 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
407 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
408 return Context.getTypedefType(OCUVectorDecls[i]);
409 }
410 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000411}
412
Chris Lattnere168f762006-11-10 05:29:30 +0000413Action::ExprResult Sema::
414ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
415 tok::TokenKind OpKind, SourceLocation MemberLoc,
416 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000417 Expr *BaseExpr = static_cast<Expr *>(Base);
418 assert(BaseExpr && "no record expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000419
Steve Naroff185616f2007-07-26 03:11:44 +0000420 QualType BaseType = BaseExpr->getType();
421 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000422
Steve Narofff1e53692007-03-23 22:27:02 +0000423 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000424 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000425 BaseType = PT->getPointeeType();
426 else
427 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
428 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000429 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000430 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000431 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000432 RecordDecl *RDecl = RTy->getDecl();
433 if (RTy->isIncompleteType())
434 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
435 BaseExpr->getSourceRange());
436 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000437 FieldDecl *MemberDecl = RDecl->getMember(&Member);
438 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000439 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
440 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000441 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
442 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000443 // Component access limited to variables (reject vec4.rg.g).
444 if (!isa<DeclRefExpr>(BaseExpr))
445 return Diag(OpLoc, diag::err_ocuvector_component_access,
446 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000447 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
448 if (ret.isNull())
449 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000450 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Steve Naroff185616f2007-07-26 03:11:44 +0000451 } else
452 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
453 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000454}
455
456/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
457/// This provides the location of the left/right parens and a list of comma
458/// locations.
459Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000460ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
461 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000462 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000463 Expr *Fn = static_cast<Expr *>(fn);
464 Expr **Args = reinterpret_cast<Expr**>(args);
465 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000466
Chris Lattner38dbdb22007-07-21 03:03:59 +0000467 UsualUnaryConversions(Fn);
468 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000469
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000470 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
471 // type pointer to function".
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000472 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000473 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000474 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
475 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000476
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000477 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000478 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000479 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
480 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000481
Steve Naroff17f76e02007-05-03 21:03:48 +0000482 // If a prototype isn't declared, the parser implicitly defines a func decl
483 QualType resultType = funcT->getResultType();
484
485 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
486 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
487 // assignment, to the types of the corresponding parameter, ...
488
489 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000490 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000491
Steve Naroffb8c289d2007-05-08 22:18:00 +0000492 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000493 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000494 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000495 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000496 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000497 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000498 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000499 SourceRange(Args[NumArgsInProto]->getLocStart(),
500 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000501 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000502 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000503 }
504 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000505 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000506 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000507 assert(argExpr && "ParseCallExpr(): missing argument expression");
508
Steve Naroff17f76e02007-05-03 21:03:48 +0000509 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000510 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000511
Steve Naroffb8af1c22007-07-25 20:45:33 +0000512 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner41977962007-07-31 19:29:30 +0000513 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000514 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000515 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000516 lhsType = Context.getPointerType(lhsType);
517
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000518 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
519 argExpr);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000520 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000521
522 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000523 switch (result) {
524 case Compatible:
525 break;
526 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000527 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000528 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000529 Diag(l, diag::ext_typecheck_passing_pointer_int,
530 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000531 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000532 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000533 break;
534 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000535 Diag(l, diag::ext_typecheck_passing_pointer_int,
536 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000537 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000538 break;
539 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000540 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000541 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000542 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000543 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000544 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000545 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
546 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000547 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000548 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000549 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000550 return Diag(l, diag::err_typecheck_passing_incompatible,
551 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000552 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000553 }
554 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000555 // Even if the types checked, bail if we had the wrong number of arguments.
Chris Lattner38dbdb22007-07-21 03:03:59 +0000556 if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
Steve Naroffb8c289d2007-05-08 22:18:00 +0000557 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000558 }
Chris Lattner38dbdb22007-07-21 03:03:59 +0000559 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000560}
561
562Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000563ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000564 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000565 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
566 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000567 // FIXME: put back this assert when initializers are worked out.
568 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
569 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000570
571 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000572 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000573}
574
575Action::ExprResult Sema::
576ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
577 SourceLocation RParenLoc) {
578 // FIXME: add semantic analysis (C99 6.7.8). This involves
579 // knowledge of the object being intialized. As a result, the code for
580 // doing the semantic analysis will likely be located elsewhere (i.e. in
581 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
582 return false; // FIXME instantiate an InitListExpr.
583}
584
585Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000586ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
587 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000588 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
589
590 Expr *castExpr = static_cast<Expr*>(Op);
591 QualType castType = QualType::getFromOpaquePtr(Ty);
592
Chris Lattnerbd270732007-07-18 16:00:06 +0000593 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
594 // type needs to be scalar.
595 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000596 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
597 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
598 }
599 if (!castExpr->getType()->isScalarType()) {
600 return Diag(castExpr->getLocStart(),
601 diag::err_typecheck_expect_scalar_operand,
602 castExpr->getType().getAsString(), castExpr->getSourceRange());
603 }
604 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000605}
606
Steve Narofff8a28c52007-05-15 20:29:32 +0000607inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000608 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000609 UsualUnaryConversions(cond);
610 UsualUnaryConversions(lex);
611 UsualUnaryConversions(rex);
612 QualType condT = cond->getType();
613 QualType lexT = lex->getType();
614 QualType rexT = rex->getType();
615
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000616 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000617 if (!condT->isScalarType()) { // C99 6.5.15p2
618 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
619 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000620 return QualType();
621 }
622 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000623 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
624 UsualArithmeticConversions(lex, rex);
625 return lex->getType();
626 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000627 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
628 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
629
630 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
631 return lexT;
632
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000633 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000634 lexT.getAsString(), rexT.getAsString(),
635 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000636 return QualType();
637 }
638 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000639 // C99 6.5.15p3
640 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000641 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000642 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000643 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000644
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000645 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
646 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
647 // get the "pointed to" types
648 QualType lhptee = LHSPT->getPointeeType();
649 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000650
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000651 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
652 if (lhptee->isVoidType() &&
653 (rhptee->isObjectType() || rhptee->isIncompleteType()))
654 return lexT;
655 if (rhptee->isVoidType() &&
656 (lhptee->isObjectType() || lhptee->isIncompleteType()))
657 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000658
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000659 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
660 rhptee.getUnqualifiedType())) {
661 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
662 lexT.getAsString(), rexT.getAsString(),
663 lex->getSourceRange(), rex->getSourceRange());
664 return lexT; // FIXME: this is an _ext - is this return o.k?
665 }
666 // The pointer types are compatible.
667 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
668 // differently qualified versions of compatible types, the result type is a
669 // pointer to an appropriately qualified version of the *composite* type.
670 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000671 }
672 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000673
Steve Naroff7a5af782007-07-13 16:58:59 +0000674 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
675 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000676
677 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000678 lexT.getAsString(), rexT.getAsString(),
679 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000680 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000681}
682
Chris Lattnere168f762006-11-10 05:29:30 +0000683/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
684/// in the case of a the GNU conditional expr extension.
685Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
686 SourceLocation ColonLoc,
687 ExprTy *Cond, ExprTy *LHS,
688 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000689 Expr *CondExpr = (Expr *) Cond;
690 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
691 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
692 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000693 if (result.isNull())
694 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000695 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000696}
697
Steve Naroff81569d22007-07-15 02:02:06 +0000698// promoteExprToType - a helper function to ensure we create exactly one
699// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000700static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000701 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
702 impCast->setType(type);
703 else
704 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000705 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000706}
707
708/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000709void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000710 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000711 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000712
Chris Lattnercd1d0862007-07-31 16:56:34 +0000713 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000714 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
715 t = e->getType();
716 }
Steve Naroff81569d22007-07-15 02:02:06 +0000717 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000718 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000719 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000720 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000721}
722
Steve Naroff71b59a92007-06-04 22:22:31 +0000723/// UsualUnaryConversion - Performs various conversions that are common to most
724/// operators (C99 6.3). The conversions of array and function types are
725/// sometimes surpressed. For example, the array->pointer conversion doesn't
726/// apply if the array is an argument to the sizeof or address (&) operators.
727/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000728void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000729 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000730 assert(!t.isNull() && "UsualUnaryConversions - missing type");
731
Chris Lattnercd1d0862007-07-31 16:56:34 +0000732 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000733 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
734 t = expr->getType();
735 }
Steve Naroff81569d22007-07-15 02:02:06 +0000736 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000737 promoteExprToType(expr, Context.IntTy);
738 else
739 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000740}
741
Steve Naroff1926c832007-04-24 00:23:05 +0000742/// UsualArithmeticConversions - Performs various conversions that are common to
743/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
744/// routine returns the first non-arithmetic type found. The client is
745/// responsible for emitting appropriate error diagnostics.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000746void Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr) {
Steve Naroff31090012007-07-16 21:54:35 +0000747 UsualUnaryConversions(lhsExpr);
748 UsualUnaryConversions(rhsExpr);
749
Steve Naroff94a5aca2007-07-16 22:23:01 +0000750 QualType lhs = lhsExpr->getType();
751 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000752
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000753 // If both types are identical, no conversion is needed.
754 if (lhs == rhs)
Steve Naroffdbd9e892007-07-17 00:58:39 +0000755 return;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000756
757 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
758 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000759 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
760 return;
Steve Naroff1926c832007-04-24 00:23:05 +0000761
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000762 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000763
764 // Handle complex types first (C99 6.3.1.8p1).
765 if (lhs->isComplexType() || rhs->isComplexType()) {
766 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000767 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
768 promoteExprToType(rhsExpr, lhs);
769 return;
770 }
771 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
772 promoteExprToType(lhsExpr, rhs);
773 return;
774 }
Steve Naroff81569d22007-07-15 02:02:06 +0000775 // Two complex types. Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000776 if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs
777 promoteExprToType(rhsExpr, lhs);
778 return;
779 }
780 promoteExprToType(lhsExpr, rhs); // convert the lhs
781 return;
Steve Naroffbf223ba2007-04-24 20:56:26 +0000782 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000783 // Now handle "real" floating types (i.e. float, double, long double).
784 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
785 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000786 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
787 promoteExprToType(rhsExpr, lhs);
788 return;
789 }
790 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
791 promoteExprToType(lhsExpr, rhs);
792 return;
793 }
Steve Naroff81569d22007-07-15 02:02:06 +0000794 // We have two real floating types, float/complex combos were handled above.
795 // Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000796 if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs
797 promoteExprToType(rhsExpr, lhs);
798 return;
799 }
800 promoteExprToType(lhsExpr, rhs); // convert the lhs
801 return;
Steve Naroffb01bbe32007-04-25 01:22:31 +0000802 }
Steve Naroff81569d22007-07-15 02:02:06 +0000803 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000804 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
805 promoteExprToType(rhsExpr, lhs);
806 return;
807 }
808 promoteExprToType(lhsExpr, rhs); // convert the lhs
809 return;
Steve Narofff1e53692007-03-23 22:27:02 +0000810}
811
Steve Naroff3f597292007-05-11 22:18:03 +0000812// CheckPointerTypesForAssignment - This is a very tricky routine (despite
813// being closely modeled after the C99 spec:-). The odd characteristic of this
814// routine is it effectively iqnores the qualifiers on the top level pointee.
815// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
816// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000817Sema::AssignmentCheckResult
818Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000819 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000820
821 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000822 lhptee = lhsType->getAsPointerType()->getPointeeType();
823 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000824
825 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000826 lhptee = lhptee.getCanonicalType();
827 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000828
Steve Naroff98cf3e92007-06-06 18:38:38 +0000829 AssignmentCheckResult r = Compatible;
830
Steve Naroff3f597292007-05-11 22:18:03 +0000831 // C99 6.5.16.1p1: This following citation is common to constraints
832 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
833 // qualifiers of the type *pointed to* by the right;
834 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
835 rhptee.getQualifiers())
836 r = CompatiblePointerDiscardsQualifiers;
837
838 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
839 // incomplete type and the other is a pointer to a qualified or unqualified
840 // version of void...
841 if (lhptee.getUnqualifiedType()->isVoidType() &&
842 (rhptee->isObjectType() || rhptee->isIncompleteType()))
843 ;
844 else if (rhptee.getUnqualifiedType()->isVoidType() &&
845 (lhptee->isObjectType() || lhptee->isIncompleteType()))
846 ;
847 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
848 // unqualified versions of compatible types, ...
849 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
850 rhptee.getUnqualifiedType()))
851 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000852 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000853}
854
Steve Naroff98cf3e92007-06-06 18:38:38 +0000855/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000856/// has code to accommodate several GCC extensions when type checking
857/// pointers. Here are some objectionable examples that GCC considers warnings:
858///
859/// int a, *pint;
860/// short *pshort;
861/// struct foo *pfoo;
862///
863/// pint = pshort; // warning: assignment from incompatible pointer type
864/// a = pint; // warning: assignment makes integer from pointer without a cast
865/// pint = a; // warning: assignment makes pointer from integer without a cast
866/// pint = pfoo; // warning: assignment from incompatible pointer type
867///
868/// As a result, the code for dealing with pointers is more complex than the
869/// C99 spec dictates.
870/// Note: the warning above turn into errors when -pedantic-errors is enabled.
871///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000872Sema::AssignmentCheckResult
873Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000874 if (lhsType == rhsType) // common case, fast path...
875 return Compatible;
876
Steve Naroff84ff4b42007-07-09 21:31:10 +0000877 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000878 if (lhsType->isVectorType() || rhsType->isVectorType()) {
879 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
880 return Incompatible;
881 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000882 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000883 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000884 if (rhsType->isIntegerType())
885 return PointerFromInt;
886
Steve Naroff3f597292007-05-11 22:18:03 +0000887 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000888 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000889 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000890 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
891 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
892 return IntFromPointer;
893
Steve Naroff3f597292007-05-11 22:18:03 +0000894 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000895 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000896 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
897 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000898 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000899 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
900 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000901 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000902 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000903 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000904}
905
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000906Sema::AssignmentCheckResult
907Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
908 // This check seems unnatural, however it is necessary to insure the proper
909 // conversion of functions/arrays. If the conversion were done for all
910 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
911 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +0000912 DefaultFunctionArrayConversion(rExpr);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000913
Steve Naroff31090012007-07-16 21:54:35 +0000914 return CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000915}
916
917Sema::AssignmentCheckResult
918Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
919 return CheckAssignmentConstraints(lhsType, rhsType);
920}
921
Steve Naroff7a5af782007-07-13 16:58:59 +0000922inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000923 Diag(loc, diag::err_typecheck_invalid_operands,
924 lex->getType().getAsString(), rex->getType().getAsString(),
925 lex->getSourceRange(), rex->getSourceRange());
926}
927
Steve Naroff7a5af782007-07-13 16:58:59 +0000928inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
929 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000930 QualType lhsType = lex->getType(), rhsType = rex->getType();
931
932 // make sure the vector types are identical.
933 if (lhsType == rhsType)
934 return lhsType;
935 // You cannot convert between vector values of different size.
936 Diag(loc, diag::err_typecheck_vector_not_convertable,
937 lex->getType().getAsString(), rex->getType().getAsString(),
938 lex->getSourceRange(), rex->getSourceRange());
939 return QualType();
940}
941
Steve Naroff218bc2b2007-05-04 21:54:46 +0000942inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000943 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000944{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000945 QualType lhsType = lex->getType(), rhsType = rex->getType();
946
947 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000948 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +0000949
Steve Naroffdbd9e892007-07-17 00:58:39 +0000950 UsualArithmeticConversions(lex, rex);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000951
Steve Naroffdbd9e892007-07-17 00:58:39 +0000952 // handle the common case first (both operands are arithmetic).
953 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
954 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000955 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000956 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000957}
958
Steve Naroff218bc2b2007-05-04 21:54:46 +0000959inline QualType Sema::CheckRemainderOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000960 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000961{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000962 QualType lhsType = lex->getType(), rhsType = rex->getType();
963
Steve Naroffdbd9e892007-07-17 00:58:39 +0000964 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000965
Steve Naroffdbd9e892007-07-17 00:58:39 +0000966 // handle the common case first (both operands are arithmetic).
967 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
968 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000969 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000970 return QualType();
971}
972
973inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000974 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000975{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000976 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +0000977 return CheckVectorOperands(loc, lex, rex);
978
Steve Naroffdbd9e892007-07-17 00:58:39 +0000979 UsualArithmeticConversions(lex, rex);
Steve Naroff94a5aca2007-07-16 22:23:01 +0000980
Steve Naroffe4718892007-04-27 18:30:00 +0000981 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000982 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
983 return lex->getType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000984
Steve Naroffdbd9e892007-07-17 00:58:39 +0000985 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
986 return lex->getType();
987 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
988 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000989 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000990 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000991}
992
Steve Naroff218bc2b2007-05-04 21:54:46 +0000993inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000994 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000995{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000996 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000997 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000998
Steve Naroffdbd9e892007-07-17 00:58:39 +0000999 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001000
1001 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001002 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1003 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +00001004
1005 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffdbd9e892007-07-17 00:58:39 +00001006 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +00001007 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00001008 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001009 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001010 return QualType();
1011}
1012
1013inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff7a5af782007-07-13 16:58:59 +00001014 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001015{
Chris Lattner1d411a82007-06-05 20:52:21 +00001016 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1017 // for int << longlong -> the result type should be int, not long long.
Steve Naroffdbd9e892007-07-17 00:58:39 +00001018 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001019
Steve Naroffdbd9e892007-07-17 00:58:39 +00001020 // handle the common case first (both operands are arithmetic).
1021 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
1022 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001023 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001024 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001025}
1026
Steve Naroff218bc2b2007-05-04 21:54:46 +00001027inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
Steve Naroff7a5af782007-07-13 16:58:59 +00001028 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001029{
Steve Naroff31090012007-07-16 21:54:35 +00001030 UsualUnaryConversions(lex);
1031 UsualUnaryConversions(rex);
1032 QualType lType = lex->getType();
1033 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001034
1035 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001036 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +00001037
Steve Naroff75c17232007-06-13 21:41:08 +00001038 if (lType->isPointerType()) {
1039 if (rType->isPointerType())
1040 return Context.IntTy;
1041 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001042 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001043 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1044 lex->getSourceRange(), rex->getSourceRange());
1045 return Context.IntTy; // the previous diagnostic is a GCC extension.
1046 }
1047 } else if (rType->isPointerType()) {
1048 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001049 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001050 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1051 lex->getSourceRange(), rex->getSourceRange());
1052 return Context.IntTy; // the previous diagnostic is a GCC extension.
1053 }
Steve Naroff043d45d2007-05-15 02:32:35 +00001054 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001055 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001056 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001057}
1058
Steve Naroff218bc2b2007-05-04 21:54:46 +00001059inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
Steve Naroff7a5af782007-07-13 16:58:59 +00001060 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001061{
Steve Naroff31090012007-07-16 21:54:35 +00001062 UsualUnaryConversions(lex);
1063 UsualUnaryConversions(rex);
1064 QualType lType = lex->getType();
1065 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001066
1067 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001068 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001069
Steve Naroff75c17232007-06-13 21:41:08 +00001070 if (lType->isPointerType()) {
1071 if (rType->isPointerType())
1072 return Context.IntTy;
1073 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001074 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001075 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1076 lex->getSourceRange(), rex->getSourceRange());
1077 return Context.IntTy; // the previous diagnostic is a GCC extension.
1078 }
1079 } else if (rType->isPointerType()) {
1080 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001081 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001082 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1083 lex->getSourceRange(), rex->getSourceRange());
1084 return Context.IntTy; // the previous diagnostic is a GCC extension.
1085 }
Steve Naroff043d45d2007-05-15 02:32:35 +00001086 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001087 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001088 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001089}
1090
Steve Naroff218bc2b2007-05-04 21:54:46 +00001091inline QualType Sema::CheckBitwiseOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +00001092 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001093{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001094 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001095 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001096
Steve Naroffdbd9e892007-07-17 00:58:39 +00001097 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001098
Steve Naroffdbd9e892007-07-17 00:58:39 +00001099 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
1100 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001101 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001102 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001103}
1104
Steve Naroff218bc2b2007-05-04 21:54:46 +00001105inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001106 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001107{
Steve Naroff31090012007-07-16 21:54:35 +00001108 UsualUnaryConversions(lex);
1109 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001110
Steve Naroffdbd9e892007-07-17 00:58:39 +00001111 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001112 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001113 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001114 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001115}
1116
Steve Naroff35d85152007-05-07 00:24:15 +00001117inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
1118 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001119{
Steve Naroff0af91202007-04-27 21:51:21 +00001120 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001121 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001122 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001123 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1124
1125 switch (mlval) { // C99 6.5.16p2
1126 case Expr::MLV_Valid:
1127 break;
1128 case Expr::MLV_ConstQualified:
1129 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1130 hadError = true;
1131 break;
1132 case Expr::MLV_ArrayType:
1133 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1134 lhsType.getAsString(), lex->getSourceRange());
1135 return QualType();
1136 case Expr::MLV_NotObjectType:
1137 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1138 lhsType.getAsString(), lex->getSourceRange());
1139 return QualType();
1140 case Expr::MLV_InvalidExpression:
1141 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1142 lex->getSourceRange());
1143 return QualType();
1144 case Expr::MLV_IncompleteType:
1145 case Expr::MLV_IncompleteVoidType:
1146 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1147 lhsType.getAsString(), lex->getSourceRange());
1148 return QualType();
Steve Naroff0d595ca2007-07-30 03:29:09 +00001149 case Expr::MLV_DuplicateVectorComponents:
1150 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1151 lex->getSourceRange());
1152 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001153 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001154 AssignmentCheckResult result;
1155
1156 if (compoundType.isNull())
1157 result = CheckSingleAssignmentConstraints(lhsType, rex);
1158 else
1159 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffad373bd2007-07-31 12:34:36 +00001160
Steve Naroff218bc2b2007-05-04 21:54:46 +00001161 // decode the result (notice that extensions still return a type).
1162 switch (result) {
1163 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001164 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001165 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001166 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001167 lhsType.getAsString(), rhsType.getAsString(),
1168 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001169 hadError = true;
1170 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001171 case PointerFromInt:
1172 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001173 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001174 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1175 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001176 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001177 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001178 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001179 case IntFromPointer:
1180 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1181 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001182 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001183 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001184 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001185 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1186 lhsType.getAsString(), rhsType.getAsString(),
1187 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001188 break;
1189 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001190 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1191 lhsType.getAsString(), rhsType.getAsString(),
1192 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001193 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001194 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001195 // C99 6.5.16p3: The type of an assignment expression is the type of the
1196 // left operand unless the left operand has qualified type, in which case
1197 // it is the unqualified version of the type of the left operand.
1198 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1199 // is converted to the type of the assignment expression (above).
1200 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1201 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001202}
1203
Steve Naroff218bc2b2007-05-04 21:54:46 +00001204inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001205 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001206 UsualUnaryConversions(rex);
1207 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001208}
1209
Steve Naroff7a5af782007-07-13 16:58:59 +00001210/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1211/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001212QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001213 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001214 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001215
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001216 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +00001217 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1218 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001219 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1220 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001221 return QualType();
1222 }
1223 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +00001224 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001225 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1226 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001227 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001228 }
Steve Naroff094046f2007-05-13 03:21:25 +00001229 // At this point, we know we have a real or pointer type. Now make sure
1230 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001231 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1232 if (mlval != Expr::MLV_Valid) {
1233 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001234 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001235 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001236 return QualType();
1237 }
1238 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001239}
1240
Steve Naroff1926c832007-04-24 00:23:05 +00001241/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001242/// This routine allows us to typecheck complex/recursive expressions
1243/// where the declaration is needed for type checking. Here are some
1244/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001245static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001246 switch (e->getStmtClass()) {
1247 case Stmt::DeclRefExprClass:
1248 return cast<DeclRefExpr>(e)->getDecl();
1249 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001250 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001251 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001252 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001253 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001254 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001255 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001256 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001257 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001258 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001259 default:
1260 return 0;
1261 }
1262}
1263
1264/// CheckAddressOfOperand - The operand of & must be either a function
1265/// designator or an lvalue designating an object. If it is an lvalue, the
1266/// object cannot be declared with storage class register or be a bit field.
1267/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001268/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001269QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001270 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001271 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001272
Steve Naroff9358c712007-05-27 23:58:33 +00001273 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001274 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1275 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001276 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001277 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1278 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001279 return QualType();
1280 }
Steve Naroff47500512007-04-19 23:00:49 +00001281 } else if (dcl) {
1282 // We have an lvalue with a decl. Make sure the decl is not declared
1283 // with the register storage-class specifier.
1284 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001285 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001286 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001287 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001288 return QualType();
1289 }
Steve Narofff633d092007-04-25 19:01:39 +00001290 } else
1291 assert(0 && "Unknown/unexpected decl type");
1292
Steve Naroff47500512007-04-19 23:00:49 +00001293 // FIXME: add check for bitfields!
1294 }
1295 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001296 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001297}
1298
Steve Naroff35d85152007-05-07 00:24:15 +00001299QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001300 UsualUnaryConversions(op);
1301 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001302
Chris Lattnerc996b172007-07-31 16:53:04 +00001303 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001304 QualType ptype = PT->getPointeeType();
1305 // C99 6.5.3.2p4. "if it points to an object,...".
1306 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1307 // GCC compat: special case 'void *' (treat as warning).
1308 if (ptype->isVoidType()) {
1309 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001310 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001311 } else {
1312 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001313 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001314 return QualType();
1315 }
1316 }
1317 return ptype;
1318 }
1319 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001320 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001321 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001322}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001323
1324static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1325 tok::TokenKind Kind) {
1326 BinaryOperator::Opcode Opc;
1327 switch (Kind) {
1328 default: assert(0 && "Unknown binop!");
1329 case tok::star: Opc = BinaryOperator::Mul; break;
1330 case tok::slash: Opc = BinaryOperator::Div; break;
1331 case tok::percent: Opc = BinaryOperator::Rem; break;
1332 case tok::plus: Opc = BinaryOperator::Add; break;
1333 case tok::minus: Opc = BinaryOperator::Sub; break;
1334 case tok::lessless: Opc = BinaryOperator::Shl; break;
1335 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1336 case tok::lessequal: Opc = BinaryOperator::LE; break;
1337 case tok::less: Opc = BinaryOperator::LT; break;
1338 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1339 case tok::greater: Opc = BinaryOperator::GT; break;
1340 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1341 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1342 case tok::amp: Opc = BinaryOperator::And; break;
1343 case tok::caret: Opc = BinaryOperator::Xor; break;
1344 case tok::pipe: Opc = BinaryOperator::Or; break;
1345 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1346 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1347 case tok::equal: Opc = BinaryOperator::Assign; break;
1348 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1349 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1350 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1351 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1352 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1353 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1354 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1355 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1356 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1357 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1358 case tok::comma: Opc = BinaryOperator::Comma; break;
1359 }
1360 return Opc;
1361}
1362
Steve Naroff35d85152007-05-07 00:24:15 +00001363static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1364 tok::TokenKind Kind) {
1365 UnaryOperator::Opcode Opc;
1366 switch (Kind) {
1367 default: assert(0 && "Unknown unary op!");
1368 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1369 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1370 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1371 case tok::star: Opc = UnaryOperator::Deref; break;
1372 case tok::plus: Opc = UnaryOperator::Plus; break;
1373 case tok::minus: Opc = UnaryOperator::Minus; break;
1374 case tok::tilde: Opc = UnaryOperator::Not; break;
1375 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1376 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1377 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1378 case tok::kw___real: Opc = UnaryOperator::Real; break;
1379 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001380 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001381 }
1382 return Opc;
1383}
1384
Steve Naroff218bc2b2007-05-04 21:54:46 +00001385// Binary Operators. 'Tok' is the token for the operator.
1386Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1387 ExprTy *LHS, ExprTy *RHS) {
1388 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1389 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1390
1391 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1392 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1393
Chris Lattner256c21b2007-06-28 03:53:10 +00001394 QualType ResultTy; // Result type of the binary operator.
1395 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001396
1397 switch (Opc) {
1398 default:
1399 assert(0 && "Unknown binary expr!");
1400 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001401 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001402 break;
1403 case BinaryOperator::Mul:
1404 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001405 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001406 break;
1407 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001408 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001409 break;
1410 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001411 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001412 break;
1413 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001414 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001415 break;
1416 case BinaryOperator::Shl:
1417 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001418 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001419 break;
1420 case BinaryOperator::LE:
1421 case BinaryOperator::LT:
1422 case BinaryOperator::GE:
1423 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001424 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001425 break;
1426 case BinaryOperator::EQ:
1427 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001428 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001429 break;
1430 case BinaryOperator::And:
1431 case BinaryOperator::Xor:
1432 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001433 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001434 break;
1435 case BinaryOperator::LAnd:
1436 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001437 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001438 break;
1439 case BinaryOperator::MulAssign:
1440 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001441 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1442 if (!CompTy.isNull())
1443 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001444 break;
1445 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001446 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1447 if (!CompTy.isNull())
1448 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001449 break;
1450 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001451 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1452 if (!CompTy.isNull())
1453 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001454 break;
1455 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001456 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1457 if (!CompTy.isNull())
1458 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001459 break;
1460 case BinaryOperator::ShlAssign:
1461 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001462 CompTy = CheckShiftOperands(lhs, rhs, TokLoc);
1463 if (!CompTy.isNull())
1464 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001465 break;
1466 case BinaryOperator::AndAssign:
1467 case BinaryOperator::XorAssign:
1468 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001469 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1470 if (!CompTy.isNull())
1471 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001472 break;
1473 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001474 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001475 break;
1476 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001477 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001478 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001479 if (CompTy.isNull())
1480 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1481 else
Chris Lattner9369a562007-06-29 16:31:29 +00001482 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001483}
1484
Steve Naroff35d85152007-05-07 00:24:15 +00001485// Unary Operators. 'Tok' is the token for the operator.
1486Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001487 ExprTy *input) {
1488 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001489 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1490 QualType resultType;
1491 switch (Opc) {
1492 default:
1493 assert(0 && "Unimplemented unary expr!");
1494 case UnaryOperator::PreInc:
1495 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001496 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001497 break;
1498 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001499 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001500 break;
1501 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001502 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001503 break;
1504 case UnaryOperator::Plus:
1505 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001506 UsualUnaryConversions(Input);
1507 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001508 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001509 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1510 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001511 break;
1512 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001513 UsualUnaryConversions(Input);
1514 resultType = Input->getType();
Steve Naroff1c8b7d32007-07-12 21:46:55 +00001515 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1516 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1517 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001518 break;
1519 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001520 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001521 DefaultFunctionArrayConversion(Input);
1522 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001523 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001524 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1525 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001526 // LNot always has type int. C99 6.5.3.3p5.
1527 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001528 break;
1529 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001530 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001531 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001532 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001533 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1534 break;
1535 case UnaryOperator::Extension:
1536 // FIXME: does __extension__ cause any promotions? I would think not.
1537 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001538 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001539 }
1540 if (resultType.isNull())
1541 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001542 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001543}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001544
1545/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1546Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1547 SourceLocation LabLoc,
1548 IdentifierInfo *LabelII) {
1549 // Look up the record for this label identifier.
1550 LabelStmt *&LabelDecl = LabelMap[LabelII];
1551
1552 // If we haven't seen this label yet, create a forward reference.
1553 if (LabelDecl == 0)
1554 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1555
1556 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001557 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1558 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001559}
1560
Chris Lattner366727f2007-07-24 16:58:17 +00001561Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
1562 SourceLocation RPLoc) { // "({..})"
1563 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1564 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1565 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1566
1567 // FIXME: there are a variety of strange constraints to enforce here, for
1568 // example, it is not possible to goto into a stmt expression apparently.
1569 // More semantic analysis is needed.
1570
1571 // FIXME: the last statement in the compount stmt has its value used. We
1572 // should not warn about it being unused.
1573
1574 // If there are sub stmts in the compound stmt, take the type of the last one
1575 // as the type of the stmtexpr.
1576 QualType Ty = Context.VoidTy;
1577
1578 if (!Compound->body_empty())
1579 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1580 Ty = LastExpr->getType();
1581
1582 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1583}
Steve Naroff78864672007-08-01 22:05:33 +00001584
Steve Naroff788d8642007-08-01 23:45:51 +00001585Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00001586 TypeTy *arg1, TypeTy *arg2,
1587 SourceLocation RPLoc) {
1588 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1589 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1590
1591 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1592
Steve Naroff788d8642007-08-01 23:45:51 +00001593 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2, RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00001594}
1595
Steve Naroff9efdabc2007-08-03 21:21:27 +00001596Sema::ExprResult Sema::ParseChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
1597 ExprTy *expr1, ExprTy *expr2,
1598 SourceLocation RPLoc) {
1599 Expr *CondExpr = static_cast<Expr*>(cond);
1600 Expr *LHSExpr = static_cast<Expr*>(expr1);
1601 Expr *RHSExpr = static_cast<Expr*>(expr2);
1602
1603 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
1604
1605 // The conditional expression is required to be a constant expression.
1606 llvm::APSInt condEval(32);
1607 SourceLocation ExpLoc;
1608 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
1609 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
1610 CondExpr->getSourceRange());
1611
1612 // If the condition is > zero, then the AST type is the same as the LSHExpr.
1613 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
1614 RHSExpr->getType();
1615 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
1616}
1617