blob: 9ef2e679901dc1be543ef3e67439c848f786479d [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000015#include "clang/AST/ASTContext.h"
Chris Lattner17ed4872006-11-20 04:58:19 +000016#include "clang/AST/Decl.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000017#include "clang/AST/Expr.h"
18#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000019#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000021#include "clang/Basic/Diagnostic.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000022#include "clang/Basic/LangOptions.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000023#include "clang/Basic/TargetInfo.h"
24#include "llvm/ADT/SmallString.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000025using namespace clang;
26
Steve Naroffdf7855b2007-02-21 23:46:25 +000027/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000028/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
29/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
30/// multiple tokens. However, the common case is that StringToks points to one
31/// string.
32///
33Action::ExprResult
Chris Lattner146762e2007-07-20 16:59:19 +000034Sema::ParseStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000035 assert(NumStringToks && "Must have at least one string!");
36
Steve Naroff4f88b312007-03-13 22:37:02 +000037 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
38 if (Literal.hadError)
39 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000040
Chris Lattner23b7eb62007-06-15 23:05:46 +000041 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000042 for (unsigned i = 0; i != NumStringToks; ++i)
43 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000044
45 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000046 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000047
Chris Lattner5b183d82006-11-10 05:03:26 +000048 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000049 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000050 Literal.AnyWide, t, StringToks[0].getLocation(),
51 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000052}
53
Chris Lattnere168f762006-11-10 05:29:30 +000054
Chris Lattnerac18be92006-11-20 06:49:47 +000055/// ParseIdentifierExpr - The parser read an identifier in expression context,
56/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
57/// identifier is used in an function call context.
58Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
59 IdentifierInfo &II,
60 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000061 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000062 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000063 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000064 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000065 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000066 if (HasTrailingLParen &&
67 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000068 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000069 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000070 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000071 // If this name wasn't predeclared and if this is not a function call,
72 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000073 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000074 }
Chris Lattner17ed4872006-11-20 04:58:19 +000075 }
76
Steve Naroff46ba1eb2007-04-03 23:13:13 +000077 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
Steve Naroff509fe022007-05-17 01:16:00 +000078 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff46ba1eb2007-04-03 23:13:13 +000079 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000080 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
81
82 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +000083 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +000084}
Chris Lattnere168f762006-11-10 05:29:30 +000085
Anders Carlsson625bfc82007-07-21 05:21:51 +000086Sema::ExprResult Sema::ParsePreDefinedExpr(SourceLocation Loc,
87 tok::TokenKind Kind) {
88 PreDefinedExpr::IdentType IT;
89
Chris Lattnere168f762006-11-10 05:29:30 +000090 switch (Kind) {
91 default:
92 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +000093 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
Anders Carlsson625bfc82007-07-21 05:21:51 +000094 IT = PreDefinedExpr::Func;
95 break;
Chris Lattnere168f762006-11-10 05:29:30 +000096 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +000097 IT = PreDefinedExpr::Function;
98 break;
Chris Lattnere168f762006-11-10 05:29:30 +000099 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000100 IT = PreDefinedExpr::PrettyFunction;
101 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000102 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000103
104 // Pre-defined identifiers are always of type char *.
105 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000106}
107
Chris Lattner146762e2007-07-20 16:59:19 +0000108Sema::ExprResult Sema::ParseCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000109 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000110 CharBuffer.resize(Tok.getLength());
111 const char *ThisTokBegin = &CharBuffer[0];
112 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
113
114 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
115 Tok.getLocation(), PP);
116 if (Literal.hadError())
117 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000118 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
119 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000120}
121
Chris Lattner146762e2007-07-20 16:59:19 +0000122Action::ExprResult Sema::ParseNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000123 // fast path for a single digit (which is quite common). A single digit
124 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
125 if (Tok.getLength() == 1) {
126 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000127
Chris Lattner4481b422007-07-14 01:29:45 +0000128 unsigned IntSize = Context.getTypeSize(Context.IntTy, Tok.getLocation());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000129 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
130 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000131 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000132 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000133 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000134 IntegerBuffer.resize(Tok.getLength());
135 const char *ThisTokBegin = &IntegerBuffer[0];
136
Chris Lattner67ca9252007-05-21 01:08:44 +0000137 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000138 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000139 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000140 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000141 if (Literal.hadError)
142 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000143
Steve Naroff09ef4742007-03-09 23:16:33 +0000144 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000145 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000146
147 // Get the value in the widest-possible width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000148 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000149
150 if (Literal.GetIntegerValue(ResultVal)) {
151 // If this value didn't fit into uintmax_t, warn and force to ull.
152 Diag(Tok.getLocation(), diag::warn_integer_too_large);
153 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000154 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000155 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000156 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000157 // If this value fits into a ULL, try to figure out what else it fits into
158 // according to the rules of C99 6.4.4.1p5.
159
160 // Octal, Hexadecimal, and integers with a U suffix are allowed to
161 // be an unsigned int.
162 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
163
164 // Check from smallest to largest, picking the smallest type we can.
165 if (!Literal.isLong) { // Are int/unsigned possibilities?
Chris Lattner4481b422007-07-14 01:29:45 +0000166 unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000167 // Does it fit in a unsigned int?
168 if (ResultVal.isIntN(IntSize)) {
169 // Does it fit in a signed int?
170 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
171 t = Context.IntTy;
172 else if (AllowUnsigned)
173 t = Context.UnsignedIntTy;
174 }
175
176 if (!t.isNull())
177 ResultVal.trunc(IntSize);
178 }
179
180 // Are long/unsigned long possibilities?
181 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner4481b422007-07-14 01:29:45 +0000182 unsigned LongSize = Context.getTypeSize(Context.LongTy,
183 Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000184
185 // Does it fit in a unsigned long?
186 if (ResultVal.isIntN(LongSize)) {
187 // Does it fit in a signed long?
188 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
189 t = Context.LongTy;
190 else if (AllowUnsigned)
191 t = Context.UnsignedLongTy;
192 }
193 if (!t.isNull())
194 ResultVal.trunc(LongSize);
195 }
196
197 // Finally, check long long if needed.
198 if (t.isNull()) {
199 unsigned LongLongSize =
Chris Lattner4481b422007-07-14 01:29:45 +0000200 Context.getTypeSize(Context.LongLongTy, Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000201
202 // Does it fit in a unsigned long long?
203 if (ResultVal.isIntN(LongLongSize)) {
204 // Does it fit in a signed long long?
205 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
206 t = Context.LongLongTy;
207 else if (AllowUnsigned)
208 t = Context.UnsignedLongLongTy;
209 }
210 }
211
212 // If we still couldn't decide a type, we probably have something that
213 // does not fit in a signed long long, but has no U suffix.
214 if (t.isNull()) {
215 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
216 t = Context.UnsignedLongLongTy;
217 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000218 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000219
220 return new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000221 } else if (Literal.isFloatingLiteral()) {
Steve Naroff97b9e912007-07-09 23:53:58 +0000222 // FIXME: handle float values > 32 (including compute the real type...).
223 return new FloatingLiteral(Literal.GetFloatValue(), Context.FloatTy,
224 Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000225 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000226 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000227}
228
229Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
230 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000231 Expr *e = (Expr *)Val;
232 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000233 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000234}
235
Steve Naroff71b59a92007-06-04 22:22:31 +0000236/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000237/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000238QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
239 SourceLocation OpLoc, bool isSizeof) {
240 // C99 6.5.3.4p1:
241 if (isa<FunctionType>(exprType) && isSizeof)
242 // alignof(function) is allowed.
243 Diag(OpLoc, diag::ext_sizeof_function_type);
244 else if (exprType->isVoidType())
245 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
246 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000247 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000248 diag::err_alignof_incomplete_type,
249 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000250 return QualType(); // error
251 }
252 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
253 return Context.getSizeType();
254}
255
Chris Lattnere168f762006-11-10 05:29:30 +0000256Action::ExprResult Sema::
257ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000258 SourceLocation LPLoc, TypeTy *Ty,
259 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000260 // If error parsing type, ignore.
261 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000262
263 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000264 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000265
Steve Naroff043d45d2007-05-15 02:32:35 +0000266 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
267
268 if (resultType.isNull())
269 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000270 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000271}
272
273
274Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
275 tok::TokenKind Kind,
276 ExprTy *Input) {
277 UnaryOperator::Opcode Opc;
278 switch (Kind) {
279 default: assert(0 && "Unknown unary op!");
280 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
281 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
282 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000283 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000284 if (result.isNull())
285 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000286 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000287}
288
289Action::ExprResult Sema::
290ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
291 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000292 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000293
294 // Perform default conversions.
295 DefaultFunctionArrayConversion(LHSExp);
296 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000297
Chris Lattner36d572b2007-07-16 00:14:47 +0000298 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000299
Steve Naroffc1aadb12007-03-28 21:49:40 +0000300 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
301 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000302 // in the subscript position. As a result, we need to derive the array base
303 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000304 Expr *BaseExpr, *IndexExpr;
305 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000306 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000307 BaseExpr = LHSExp;
308 IndexExpr = RHSExp;
309 // FIXME: need to deal with const...
310 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000311 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000312 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000313 BaseExpr = RHSExp;
314 IndexExpr = LHSExp;
315 // FIXME: need to deal with const...
316 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000317 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
318 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000319 IndexExpr = RHSExp;
320 // FIXME: need to deal with const...
321 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000322 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000323 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
324 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000325 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000326 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000327 if (!IndexExpr->getType()->isIntegerType())
328 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
329 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000330
Chris Lattner36d572b2007-07-16 00:14:47 +0000331 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
332 // the following check catches trying to index a pointer to a function (e.g.
333 // void (*)(int)). Functions are not objects in C99.
334 if (!ResultType->isObjectType())
335 return Diag(BaseExpr->getLocStart(),
336 diag::err_typecheck_subscript_not_object,
337 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
338
339 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000340}
341
Steve Narofff8fd09e2007-07-27 22:15:19 +0000342QualType Sema::
343CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
344 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000345 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000346
347 // The vector accessor can't exceed the number of elements.
348 const char *compStr = CompName.getName();
349 if (strlen(compStr) > vecType->getNumElements()) {
350 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
351 baseType.getAsString(), SourceRange(CompLoc));
352 return QualType();
353 }
354 // The component names must come from the same set.
355 if (vecType->isPointAccessor(*compStr))
356 do { compStr++; } while (*compStr && vecType->isPointAccessor(*compStr));
357 else if (vecType->isColorAccessor(*compStr))
358 do { compStr++; } while (*compStr && vecType->isColorAccessor(*compStr));
359 else if (vecType->isTextureAccessor(*compStr))
360 do { compStr++; } while (*compStr && vecType->isTextureAccessor(*compStr));
361
362 if (*compStr) {
363 // We didn't get to the end of the string. This means the component names
364 // didn't come from the same set *or* we encountered an illegal name.
365 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
366 std::string(compStr,compStr+1), SourceRange(CompLoc));
367 return QualType();
368 }
369 // Each component accessor can't exceed the vector type.
370 compStr = CompName.getName();
371 while (*compStr) {
372 if (vecType->isAccessorWithinNumElements(*compStr))
373 compStr++;
374 else
375 break;
376 }
377 if (*compStr) {
378 // We didn't get to the end of the string. This means a component accessor
379 // exceeds the number of elements in the vector.
380 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
381 baseType.getAsString(), SourceRange(CompLoc));
382 return QualType();
383 }
384 // The component accessor looks fine - now we need to compute the actual type.
385 // The vector type is implied by the component accessor. For example,
386 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
387 unsigned CompSize = strlen(CompName.getName());
388 if (CompSize == 1)
389 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000390
391 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
392 // Now look up the TypeDefDecl from the vector type. Without this,
393 // diagostics look bad. We want OCU vector types to appear built-in.
394 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
395 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
396 return Context.getTypedefType(OCUVectorDecls[i]);
397 }
398 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000399}
400
Chris Lattnere168f762006-11-10 05:29:30 +0000401Action::ExprResult Sema::
402ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
403 tok::TokenKind OpKind, SourceLocation MemberLoc,
404 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000405 Expr *BaseExpr = static_cast<Expr *>(Base);
406 assert(BaseExpr && "no record expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000407
Steve Naroff185616f2007-07-26 03:11:44 +0000408 QualType BaseType = BaseExpr->getType();
409 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000410
Steve Narofff1e53692007-03-23 22:27:02 +0000411 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000412 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000413 BaseType = PT->getPointeeType();
414 else
415 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
416 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000417 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000418 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000419 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000420 RecordDecl *RDecl = RTy->getDecl();
421 if (RTy->isIncompleteType())
422 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
423 BaseExpr->getSourceRange());
424 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000425 FieldDecl *MemberDecl = RDecl->getMember(&Member);
426 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000427 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
428 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000429 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
430 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
431 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
432 if (ret.isNull())
433 return true;
Steve Narofff7a5da12007-07-28 23:10:27 +0000434 return new OCUVectorComponent(ret, BaseExpr, Member, MemberLoc);
Steve Naroff185616f2007-07-26 03:11:44 +0000435 } else
436 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
437 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000438}
439
440/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
441/// This provides the location of the left/right parens and a list of comma
442/// locations.
443Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000444ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
445 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000446 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000447 Expr *Fn = static_cast<Expr *>(fn);
448 Expr **Args = reinterpret_cast<Expr**>(args);
449 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000450
Chris Lattner38dbdb22007-07-21 03:03:59 +0000451 UsualUnaryConversions(Fn);
452 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000453
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000454 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
455 // type pointer to function".
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000456 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000457 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000458 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
459 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000460
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000461 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000462 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000463 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
464 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000465
Steve Naroff17f76e02007-05-03 21:03:48 +0000466 // If a prototype isn't declared, the parser implicitly defines a func decl
467 QualType resultType = funcT->getResultType();
468
469 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
470 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
471 // assignment, to the types of the corresponding parameter, ...
472
473 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000474 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000475
Steve Naroffb8c289d2007-05-08 22:18:00 +0000476 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000477 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000478 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000479 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000480 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000481 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000482 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000483 SourceRange(Args[NumArgsInProto]->getLocStart(),
484 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000485 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000486 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000487 }
488 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000489 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000490 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000491 assert(argExpr && "ParseCallExpr(): missing argument expression");
492
Steve Naroff17f76e02007-05-03 21:03:48 +0000493 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000494 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000495
Steve Naroffb8af1c22007-07-25 20:45:33 +0000496 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner41977962007-07-31 19:29:30 +0000497 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000498 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000499 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000500 lhsType = Context.getPointerType(lhsType);
501
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000502 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
503 argExpr);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000504 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000505
506 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000507 switch (result) {
508 case Compatible:
509 break;
510 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000511 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000512 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000513 Diag(l, diag::ext_typecheck_passing_pointer_int,
514 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000515 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000516 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000517 break;
518 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000519 Diag(l, diag::ext_typecheck_passing_pointer_int,
520 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000521 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000522 break;
523 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000524 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000525 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000526 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000527 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000528 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000529 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
530 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000531 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000532 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000533 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000534 return Diag(l, diag::err_typecheck_passing_incompatible,
535 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000536 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000537 }
538 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000539 // Even if the types checked, bail if we had the wrong number of arguments.
Chris Lattner38dbdb22007-07-21 03:03:59 +0000540 if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
Steve Naroffb8c289d2007-05-08 22:18:00 +0000541 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000542 }
Chris Lattner38dbdb22007-07-21 03:03:59 +0000543 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000544}
545
546Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000547ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000548 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000549 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
550 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000551 // FIXME: put back this assert when initializers are worked out.
552 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
553 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000554
555 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000556 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000557}
558
559Action::ExprResult Sema::
560ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
561 SourceLocation RParenLoc) {
562 // FIXME: add semantic analysis (C99 6.7.8). This involves
563 // knowledge of the object being intialized. As a result, the code for
564 // doing the semantic analysis will likely be located elsewhere (i.e. in
565 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
566 return false; // FIXME instantiate an InitListExpr.
567}
568
569Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000570ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
571 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000572 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
573
574 Expr *castExpr = static_cast<Expr*>(Op);
575 QualType castType = QualType::getFromOpaquePtr(Ty);
576
Chris Lattnerbd270732007-07-18 16:00:06 +0000577 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
578 // type needs to be scalar.
579 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000580 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
581 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
582 }
583 if (!castExpr->getType()->isScalarType()) {
584 return Diag(castExpr->getLocStart(),
585 diag::err_typecheck_expect_scalar_operand,
586 castExpr->getType().getAsString(), castExpr->getSourceRange());
587 }
588 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000589}
590
Steve Narofff8a28c52007-05-15 20:29:32 +0000591inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000592 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000593 UsualUnaryConversions(cond);
594 UsualUnaryConversions(lex);
595 UsualUnaryConversions(rex);
596 QualType condT = cond->getType();
597 QualType lexT = lex->getType();
598 QualType rexT = rex->getType();
599
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000600 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000601 if (!condT->isScalarType()) { // C99 6.5.15p2
602 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
603 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000604 return QualType();
605 }
606 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000607 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
608 UsualArithmeticConversions(lex, rex);
609 return lex->getType();
610 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000611 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
612 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
613
614 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
615 return lexT;
616
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000617 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000618 lexT.getAsString(), rexT.getAsString(),
619 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000620 return QualType();
621 }
622 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000623 // C99 6.5.15p3
624 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000625 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000626 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000627 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000628
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000629 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
630 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
631 // get the "pointed to" types
632 QualType lhptee = LHSPT->getPointeeType();
633 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000634
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000635 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
636 if (lhptee->isVoidType() &&
637 (rhptee->isObjectType() || rhptee->isIncompleteType()))
638 return lexT;
639 if (rhptee->isVoidType() &&
640 (lhptee->isObjectType() || lhptee->isIncompleteType()))
641 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000642
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000643 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
644 rhptee.getUnqualifiedType())) {
645 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
646 lexT.getAsString(), rexT.getAsString(),
647 lex->getSourceRange(), rex->getSourceRange());
648 return lexT; // FIXME: this is an _ext - is this return o.k?
649 }
650 // The pointer types are compatible.
651 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
652 // differently qualified versions of compatible types, the result type is a
653 // pointer to an appropriately qualified version of the *composite* type.
654 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000655 }
656 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000657
Steve Naroff7a5af782007-07-13 16:58:59 +0000658 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
659 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000660
661 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000662 lexT.getAsString(), rexT.getAsString(),
663 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000664 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000665}
666
Chris Lattnere168f762006-11-10 05:29:30 +0000667/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
668/// in the case of a the GNU conditional expr extension.
669Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
670 SourceLocation ColonLoc,
671 ExprTy *Cond, ExprTy *LHS,
672 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000673 Expr *CondExpr = (Expr *) Cond;
674 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
675 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
676 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000677 if (result.isNull())
678 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000679 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000680}
681
Steve Naroff81569d22007-07-15 02:02:06 +0000682// promoteExprToType - a helper function to ensure we create exactly one
683// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000684static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000685 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
686 impCast->setType(type);
687 else
688 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000689 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000690}
691
692/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000693void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000694 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000695 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000696
Chris Lattnercd1d0862007-07-31 16:56:34 +0000697 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000698 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
699 t = e->getType();
700 }
Steve Naroff81569d22007-07-15 02:02:06 +0000701 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000702 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000703 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000704 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000705}
706
Steve Naroff71b59a92007-06-04 22:22:31 +0000707/// UsualUnaryConversion - Performs various conversions that are common to most
708/// operators (C99 6.3). The conversions of array and function types are
709/// sometimes surpressed. For example, the array->pointer conversion doesn't
710/// apply if the array is an argument to the sizeof or address (&) operators.
711/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000712void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000713 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000714 assert(!t.isNull() && "UsualUnaryConversions - missing type");
715
Chris Lattnercd1d0862007-07-31 16:56:34 +0000716 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000717 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
718 t = expr->getType();
719 }
Steve Naroff81569d22007-07-15 02:02:06 +0000720 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000721 promoteExprToType(expr, Context.IntTy);
722 else
723 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000724}
725
Steve Naroff1926c832007-04-24 00:23:05 +0000726/// UsualArithmeticConversions - Performs various conversions that are common to
727/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
728/// routine returns the first non-arithmetic type found. The client is
729/// responsible for emitting appropriate error diagnostics.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000730void Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr) {
Steve Naroff31090012007-07-16 21:54:35 +0000731 UsualUnaryConversions(lhsExpr);
732 UsualUnaryConversions(rhsExpr);
733
Steve Naroff94a5aca2007-07-16 22:23:01 +0000734 QualType lhs = lhsExpr->getType();
735 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000736
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000737 // If both types are identical, no conversion is needed.
738 if (lhs == rhs)
Steve Naroffdbd9e892007-07-17 00:58:39 +0000739 return;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000740
741 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
742 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000743 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
744 return;
Steve Naroff1926c832007-04-24 00:23:05 +0000745
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000746 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000747
748 // Handle complex types first (C99 6.3.1.8p1).
749 if (lhs->isComplexType() || rhs->isComplexType()) {
750 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000751 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
752 promoteExprToType(rhsExpr, lhs);
753 return;
754 }
755 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
756 promoteExprToType(lhsExpr, rhs);
757 return;
758 }
Steve Naroff81569d22007-07-15 02:02:06 +0000759 // Two complex types. Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000760 if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs
761 promoteExprToType(rhsExpr, lhs);
762 return;
763 }
764 promoteExprToType(lhsExpr, rhs); // convert the lhs
765 return;
Steve Naroffbf223ba2007-04-24 20:56:26 +0000766 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000767 // Now handle "real" floating types (i.e. float, double, long double).
768 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
769 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000770 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
771 promoteExprToType(rhsExpr, lhs);
772 return;
773 }
774 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
775 promoteExprToType(lhsExpr, rhs);
776 return;
777 }
Steve Naroff81569d22007-07-15 02:02:06 +0000778 // We have two real floating types, float/complex combos were handled above.
779 // Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000780 if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs
781 promoteExprToType(rhsExpr, lhs);
782 return;
783 }
784 promoteExprToType(lhsExpr, rhs); // convert the lhs
785 return;
Steve Naroffb01bbe32007-04-25 01:22:31 +0000786 }
Steve Naroff81569d22007-07-15 02:02:06 +0000787 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000788 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
789 promoteExprToType(rhsExpr, lhs);
790 return;
791 }
792 promoteExprToType(lhsExpr, rhs); // convert the lhs
793 return;
Steve Narofff1e53692007-03-23 22:27:02 +0000794}
795
Steve Naroff3f597292007-05-11 22:18:03 +0000796// CheckPointerTypesForAssignment - This is a very tricky routine (despite
797// being closely modeled after the C99 spec:-). The odd characteristic of this
798// routine is it effectively iqnores the qualifiers on the top level pointee.
799// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
800// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000801Sema::AssignmentCheckResult
802Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000803 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000804
805 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000806 lhptee = lhsType->getAsPointerType()->getPointeeType();
807 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000808
809 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000810 lhptee = lhptee.getCanonicalType();
811 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000812
Steve Naroff98cf3e92007-06-06 18:38:38 +0000813 AssignmentCheckResult r = Compatible;
814
Steve Naroff3f597292007-05-11 22:18:03 +0000815 // C99 6.5.16.1p1: This following citation is common to constraints
816 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
817 // qualifiers of the type *pointed to* by the right;
818 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
819 rhptee.getQualifiers())
820 r = CompatiblePointerDiscardsQualifiers;
821
822 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
823 // incomplete type and the other is a pointer to a qualified or unqualified
824 // version of void...
825 if (lhptee.getUnqualifiedType()->isVoidType() &&
826 (rhptee->isObjectType() || rhptee->isIncompleteType()))
827 ;
828 else if (rhptee.getUnqualifiedType()->isVoidType() &&
829 (lhptee->isObjectType() || lhptee->isIncompleteType()))
830 ;
831 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
832 // unqualified versions of compatible types, ...
833 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
834 rhptee.getUnqualifiedType()))
835 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000836 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000837}
838
Steve Naroff98cf3e92007-06-06 18:38:38 +0000839/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000840/// has code to accommodate several GCC extensions when type checking
841/// pointers. Here are some objectionable examples that GCC considers warnings:
842///
843/// int a, *pint;
844/// short *pshort;
845/// struct foo *pfoo;
846///
847/// pint = pshort; // warning: assignment from incompatible pointer type
848/// a = pint; // warning: assignment makes integer from pointer without a cast
849/// pint = a; // warning: assignment makes pointer from integer without a cast
850/// pint = pfoo; // warning: assignment from incompatible pointer type
851///
852/// As a result, the code for dealing with pointers is more complex than the
853/// C99 spec dictates.
854/// Note: the warning above turn into errors when -pedantic-errors is enabled.
855///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000856Sema::AssignmentCheckResult
857Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000858 if (lhsType == rhsType) // common case, fast path...
859 return Compatible;
860
Steve Naroff84ff4b42007-07-09 21:31:10 +0000861 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000862 if (lhsType->isVectorType() || rhsType->isVectorType()) {
863 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
864 return Incompatible;
865 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000866 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000867 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000868 if (rhsType->isIntegerType())
869 return PointerFromInt;
870
Steve Naroff3f597292007-05-11 22:18:03 +0000871 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000872 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000873 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000874 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
875 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
876 return IntFromPointer;
877
Steve Naroff3f597292007-05-11 22:18:03 +0000878 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000879 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000880 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
881 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000882 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000883 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
884 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000885 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000886 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000887 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000888}
889
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000890Sema::AssignmentCheckResult
891Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
892 // This check seems unnatural, however it is necessary to insure the proper
893 // conversion of functions/arrays. If the conversion were done for all
894 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
895 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +0000896 DefaultFunctionArrayConversion(rExpr);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000897
Steve Naroff31090012007-07-16 21:54:35 +0000898 return CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000899}
900
901Sema::AssignmentCheckResult
902Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
903 return CheckAssignmentConstraints(lhsType, rhsType);
904}
905
Steve Naroff7a5af782007-07-13 16:58:59 +0000906inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000907 Diag(loc, diag::err_typecheck_invalid_operands,
908 lex->getType().getAsString(), rex->getType().getAsString(),
909 lex->getSourceRange(), rex->getSourceRange());
910}
911
Steve Naroff7a5af782007-07-13 16:58:59 +0000912inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
913 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000914 QualType lhsType = lex->getType(), rhsType = rex->getType();
915
916 // make sure the vector types are identical.
917 if (lhsType == rhsType)
918 return lhsType;
919 // You cannot convert between vector values of different size.
920 Diag(loc, diag::err_typecheck_vector_not_convertable,
921 lex->getType().getAsString(), rex->getType().getAsString(),
922 lex->getSourceRange(), rex->getSourceRange());
923 return QualType();
924}
925
Steve Naroff218bc2b2007-05-04 21:54:46 +0000926inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000927 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000928{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000929 QualType lhsType = lex->getType(), rhsType = rex->getType();
930
931 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000932 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +0000933
Steve Naroffdbd9e892007-07-17 00:58:39 +0000934 UsualArithmeticConversions(lex, rex);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000935
Steve Naroffdbd9e892007-07-17 00:58:39 +0000936 // handle the common case first (both operands are arithmetic).
937 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
938 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000939 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000940 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000941}
942
Steve Naroff218bc2b2007-05-04 21:54:46 +0000943inline QualType Sema::CheckRemainderOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000944 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000945{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000946 QualType lhsType = lex->getType(), rhsType = rex->getType();
947
Steve Naroffdbd9e892007-07-17 00:58:39 +0000948 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000949
Steve Naroffdbd9e892007-07-17 00:58:39 +0000950 // handle the common case first (both operands are arithmetic).
951 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
952 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000953 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000954 return QualType();
955}
956
957inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000958 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000959{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000960 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +0000961 return CheckVectorOperands(loc, lex, rex);
962
Steve Naroffdbd9e892007-07-17 00:58:39 +0000963 UsualArithmeticConversions(lex, rex);
Steve Naroff94a5aca2007-07-16 22:23:01 +0000964
Steve Naroffe4718892007-04-27 18:30:00 +0000965 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000966 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
967 return lex->getType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000968
Steve Naroffdbd9e892007-07-17 00:58:39 +0000969 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
970 return lex->getType();
971 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
972 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000973 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000974 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000975}
976
Steve Naroff218bc2b2007-05-04 21:54:46 +0000977inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000978 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000979{
Steve Naroff94a5aca2007-07-16 22:23:01 +0000980 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000981 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000982
Steve Naroffdbd9e892007-07-17 00:58:39 +0000983 UsualArithmeticConversions(lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000984
985 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000986 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
987 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000988
989 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffdbd9e892007-07-17 00:58:39 +0000990 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +0000991 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000992 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000993 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000994 return QualType();
995}
996
997inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff7a5af782007-07-13 16:58:59 +0000998 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000999{
Chris Lattner1d411a82007-06-05 20:52:21 +00001000 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1001 // for int << longlong -> the result type should be int, not long long.
Steve Naroffdbd9e892007-07-17 00:58:39 +00001002 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001003
Steve Naroffdbd9e892007-07-17 00:58:39 +00001004 // handle the common case first (both operands are arithmetic).
1005 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
1006 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001007 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001008 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001009}
1010
Steve Naroff218bc2b2007-05-04 21:54:46 +00001011inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
Steve Naroff7a5af782007-07-13 16:58:59 +00001012 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001013{
Steve Naroff31090012007-07-16 21:54:35 +00001014 UsualUnaryConversions(lex);
1015 UsualUnaryConversions(rex);
1016 QualType lType = lex->getType();
1017 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001018
1019 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001020 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +00001021
Steve Naroff75c17232007-06-13 21:41:08 +00001022 if (lType->isPointerType()) {
1023 if (rType->isPointerType())
1024 return Context.IntTy;
1025 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001026 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001027 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1028 lex->getSourceRange(), rex->getSourceRange());
1029 return Context.IntTy; // the previous diagnostic is a GCC extension.
1030 }
1031 } else if (rType->isPointerType()) {
1032 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001033 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001034 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1035 lex->getSourceRange(), rex->getSourceRange());
1036 return Context.IntTy; // the previous diagnostic is a GCC extension.
1037 }
Steve Naroff043d45d2007-05-15 02:32:35 +00001038 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001039 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001040 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001041}
1042
Steve Naroff218bc2b2007-05-04 21:54:46 +00001043inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
Steve Naroff7a5af782007-07-13 16:58:59 +00001044 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001045{
Steve Naroff31090012007-07-16 21:54:35 +00001046 UsualUnaryConversions(lex);
1047 UsualUnaryConversions(rex);
1048 QualType lType = lex->getType();
1049 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001050
1051 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001052 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001053
Steve Naroff75c17232007-06-13 21:41:08 +00001054 if (lType->isPointerType()) {
1055 if (rType->isPointerType())
1056 return Context.IntTy;
1057 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001058 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001059 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1060 lex->getSourceRange(), rex->getSourceRange());
1061 return Context.IntTy; // the previous diagnostic is a GCC extension.
1062 }
1063 } else if (rType->isPointerType()) {
1064 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +00001065 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +00001066 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1067 lex->getSourceRange(), rex->getSourceRange());
1068 return Context.IntTy; // the previous diagnostic is a GCC extension.
1069 }
Steve Naroff043d45d2007-05-15 02:32:35 +00001070 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001071 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001072 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001073}
1074
Steve Naroff218bc2b2007-05-04 21:54:46 +00001075inline QualType Sema::CheckBitwiseOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +00001076 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001077{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001078 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001079 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001080
Steve Naroffdbd9e892007-07-17 00:58:39 +00001081 UsualArithmeticConversions(lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001082
Steve Naroffdbd9e892007-07-17 00:58:39 +00001083 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
1084 return lex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001085 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001086 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001087}
1088
Steve Naroff218bc2b2007-05-04 21:54:46 +00001089inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001090 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001091{
Steve Naroff31090012007-07-16 21:54:35 +00001092 UsualUnaryConversions(lex);
1093 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001094
Steve Naroffdbd9e892007-07-17 00:58:39 +00001095 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001096 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001097 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001098 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001099}
1100
Steve Naroff35d85152007-05-07 00:24:15 +00001101inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
1102 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001103{
Steve Naroff0af91202007-04-27 21:51:21 +00001104 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001105 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001106 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001107 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1108
1109 switch (mlval) { // C99 6.5.16p2
1110 case Expr::MLV_Valid:
1111 break;
1112 case Expr::MLV_ConstQualified:
1113 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1114 hadError = true;
1115 break;
1116 case Expr::MLV_ArrayType:
1117 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1118 lhsType.getAsString(), lex->getSourceRange());
1119 return QualType();
1120 case Expr::MLV_NotObjectType:
1121 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1122 lhsType.getAsString(), lex->getSourceRange());
1123 return QualType();
1124 case Expr::MLV_InvalidExpression:
1125 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1126 lex->getSourceRange());
1127 return QualType();
1128 case Expr::MLV_IncompleteType:
1129 case Expr::MLV_IncompleteVoidType:
1130 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1131 lhsType.getAsString(), lex->getSourceRange());
1132 return QualType();
Steve Naroff0d595ca2007-07-30 03:29:09 +00001133 case Expr::MLV_DuplicateVectorComponents:
1134 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1135 lex->getSourceRange());
1136 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001137 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001138 AssignmentCheckResult result;
1139
1140 if (compoundType.isNull())
1141 result = CheckSingleAssignmentConstraints(lhsType, rex);
1142 else
1143 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffad373bd2007-07-31 12:34:36 +00001144
Steve Naroff218bc2b2007-05-04 21:54:46 +00001145 // decode the result (notice that extensions still return a type).
1146 switch (result) {
1147 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001148 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001149 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001150 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001151 lhsType.getAsString(), rhsType.getAsString(),
1152 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001153 hadError = true;
1154 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001155 case PointerFromInt:
1156 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001157 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001158 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1159 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001160 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001161 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001162 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001163 case IntFromPointer:
1164 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1165 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001166 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001167 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001168 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001169 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1170 lhsType.getAsString(), rhsType.getAsString(),
1171 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001172 break;
1173 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001174 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1175 lhsType.getAsString(), rhsType.getAsString(),
1176 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001177 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001178 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001179 // C99 6.5.16p3: The type of an assignment expression is the type of the
1180 // left operand unless the left operand has qualified type, in which case
1181 // it is the unqualified version of the type of the left operand.
1182 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1183 // is converted to the type of the assignment expression (above).
1184 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1185 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001186}
1187
Steve Naroff218bc2b2007-05-04 21:54:46 +00001188inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001189 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001190 UsualUnaryConversions(rex);
1191 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001192}
1193
Steve Naroff7a5af782007-07-13 16:58:59 +00001194/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1195/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001196QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001197 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001198 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001199
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001200 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +00001201 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1202 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001203 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1204 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001205 return QualType();
1206 }
1207 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +00001208 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001209 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1210 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001211 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001212 }
Steve Naroff094046f2007-05-13 03:21:25 +00001213 // At this point, we know we have a real or pointer type. Now make sure
1214 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001215 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1216 if (mlval != Expr::MLV_Valid) {
1217 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001218 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001219 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001220 return QualType();
1221 }
1222 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001223}
1224
Steve Naroff1926c832007-04-24 00:23:05 +00001225/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001226/// This routine allows us to typecheck complex/recursive expressions
1227/// where the declaration is needed for type checking. Here are some
1228/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001229static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001230 switch (e->getStmtClass()) {
1231 case Stmt::DeclRefExprClass:
1232 return cast<DeclRefExpr>(e)->getDecl();
1233 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001234 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001235 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001236 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001237 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001238 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001239 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001240 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001241 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001242 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001243 default:
1244 return 0;
1245 }
1246}
1247
1248/// CheckAddressOfOperand - The operand of & must be either a function
1249/// designator or an lvalue designating an object. If it is an lvalue, the
1250/// object cannot be declared with storage class register or be a bit field.
1251/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001252/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001253QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001254 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001255 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001256
Steve Naroff9358c712007-05-27 23:58:33 +00001257 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001258 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1259 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001260 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001261 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1262 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001263 return QualType();
1264 }
Steve Naroff47500512007-04-19 23:00:49 +00001265 } else if (dcl) {
1266 // We have an lvalue with a decl. Make sure the decl is not declared
1267 // with the register storage-class specifier.
1268 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001269 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001270 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001271 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001272 return QualType();
1273 }
Steve Narofff633d092007-04-25 19:01:39 +00001274 } else
1275 assert(0 && "Unknown/unexpected decl type");
1276
Steve Naroff47500512007-04-19 23:00:49 +00001277 // FIXME: add check for bitfields!
1278 }
1279 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001280 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001281}
1282
Steve Naroff35d85152007-05-07 00:24:15 +00001283QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001284 UsualUnaryConversions(op);
1285 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001286
Chris Lattnerc996b172007-07-31 16:53:04 +00001287 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001288 QualType ptype = PT->getPointeeType();
1289 // C99 6.5.3.2p4. "if it points to an object,...".
1290 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1291 // GCC compat: special case 'void *' (treat as warning).
1292 if (ptype->isVoidType()) {
1293 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001294 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001295 } else {
1296 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001297 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001298 return QualType();
1299 }
1300 }
1301 return ptype;
1302 }
1303 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001304 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001305 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001306}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001307
1308static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1309 tok::TokenKind Kind) {
1310 BinaryOperator::Opcode Opc;
1311 switch (Kind) {
1312 default: assert(0 && "Unknown binop!");
1313 case tok::star: Opc = BinaryOperator::Mul; break;
1314 case tok::slash: Opc = BinaryOperator::Div; break;
1315 case tok::percent: Opc = BinaryOperator::Rem; break;
1316 case tok::plus: Opc = BinaryOperator::Add; break;
1317 case tok::minus: Opc = BinaryOperator::Sub; break;
1318 case tok::lessless: Opc = BinaryOperator::Shl; break;
1319 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1320 case tok::lessequal: Opc = BinaryOperator::LE; break;
1321 case tok::less: Opc = BinaryOperator::LT; break;
1322 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1323 case tok::greater: Opc = BinaryOperator::GT; break;
1324 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1325 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1326 case tok::amp: Opc = BinaryOperator::And; break;
1327 case tok::caret: Opc = BinaryOperator::Xor; break;
1328 case tok::pipe: Opc = BinaryOperator::Or; break;
1329 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1330 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1331 case tok::equal: Opc = BinaryOperator::Assign; break;
1332 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1333 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1334 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1335 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1336 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1337 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1338 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1339 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1340 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1341 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1342 case tok::comma: Opc = BinaryOperator::Comma; break;
1343 }
1344 return Opc;
1345}
1346
Steve Naroff35d85152007-05-07 00:24:15 +00001347static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1348 tok::TokenKind Kind) {
1349 UnaryOperator::Opcode Opc;
1350 switch (Kind) {
1351 default: assert(0 && "Unknown unary op!");
1352 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1353 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1354 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1355 case tok::star: Opc = UnaryOperator::Deref; break;
1356 case tok::plus: Opc = UnaryOperator::Plus; break;
1357 case tok::minus: Opc = UnaryOperator::Minus; break;
1358 case tok::tilde: Opc = UnaryOperator::Not; break;
1359 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1360 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1361 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1362 case tok::kw___real: Opc = UnaryOperator::Real; break;
1363 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001364 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001365 }
1366 return Opc;
1367}
1368
Steve Naroff218bc2b2007-05-04 21:54:46 +00001369// Binary Operators. 'Tok' is the token for the operator.
1370Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1371 ExprTy *LHS, ExprTy *RHS) {
1372 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1373 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1374
1375 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1376 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1377
Chris Lattner256c21b2007-06-28 03:53:10 +00001378 QualType ResultTy; // Result type of the binary operator.
1379 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001380
1381 switch (Opc) {
1382 default:
1383 assert(0 && "Unknown binary expr!");
1384 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001385 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001386 break;
1387 case BinaryOperator::Mul:
1388 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001389 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001390 break;
1391 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001392 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001393 break;
1394 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001395 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001396 break;
1397 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001398 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001399 break;
1400 case BinaryOperator::Shl:
1401 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001402 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001403 break;
1404 case BinaryOperator::LE:
1405 case BinaryOperator::LT:
1406 case BinaryOperator::GE:
1407 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001408 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001409 break;
1410 case BinaryOperator::EQ:
1411 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001412 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001413 break;
1414 case BinaryOperator::And:
1415 case BinaryOperator::Xor:
1416 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001417 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001418 break;
1419 case BinaryOperator::LAnd:
1420 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001421 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001422 break;
1423 case BinaryOperator::MulAssign:
1424 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001425 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1426 if (!CompTy.isNull())
1427 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001428 break;
1429 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001430 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1431 if (!CompTy.isNull())
1432 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001433 break;
1434 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001435 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1436 if (!CompTy.isNull())
1437 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001438 break;
1439 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001440 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1441 if (!CompTy.isNull())
1442 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001443 break;
1444 case BinaryOperator::ShlAssign:
1445 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001446 CompTy = CheckShiftOperands(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::AndAssign:
1451 case BinaryOperator::XorAssign:
1452 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001453 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1454 if (!CompTy.isNull())
1455 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001456 break;
1457 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001458 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001459 break;
1460 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001461 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001462 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001463 if (CompTy.isNull())
1464 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1465 else
Chris Lattner9369a562007-06-29 16:31:29 +00001466 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001467}
1468
Steve Naroff35d85152007-05-07 00:24:15 +00001469// Unary Operators. 'Tok' is the token for the operator.
1470Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001471 ExprTy *input) {
1472 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001473 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1474 QualType resultType;
1475 switch (Opc) {
1476 default:
1477 assert(0 && "Unimplemented unary expr!");
1478 case UnaryOperator::PreInc:
1479 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001480 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001481 break;
1482 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001483 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001484 break;
1485 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001486 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001487 break;
1488 case UnaryOperator::Plus:
1489 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001490 UsualUnaryConversions(Input);
1491 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001492 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001493 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1494 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001495 break;
1496 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001497 UsualUnaryConversions(Input);
1498 resultType = Input->getType();
Steve Naroff1c8b7d32007-07-12 21:46:55 +00001499 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1500 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1501 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001502 break;
1503 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001504 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001505 DefaultFunctionArrayConversion(Input);
1506 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001507 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001508 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1509 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001510 // LNot always has type int. C99 6.5.3.3p5.
1511 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001512 break;
1513 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001514 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001515 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001516 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001517 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1518 break;
1519 case UnaryOperator::Extension:
1520 // FIXME: does __extension__ cause any promotions? I would think not.
1521 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001522 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001523 }
1524 if (resultType.isNull())
1525 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001526 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001527}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001528
1529/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1530Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1531 SourceLocation LabLoc,
1532 IdentifierInfo *LabelII) {
1533 // Look up the record for this label identifier.
1534 LabelStmt *&LabelDecl = LabelMap[LabelII];
1535
1536 // If we haven't seen this label yet, create a forward reference.
1537 if (LabelDecl == 0)
1538 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1539
1540 // Create the AST node. The address of a label always has type 'void*'.
1541 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1542 Context.getPointerType(Context.VoidTy));
1543}
1544
Chris Lattner366727f2007-07-24 16:58:17 +00001545Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
1546 SourceLocation RPLoc) { // "({..})"
1547 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1548 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1549 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1550
1551 // FIXME: there are a variety of strange constraints to enforce here, for
1552 // example, it is not possible to goto into a stmt expression apparently.
1553 // More semantic analysis is needed.
1554
1555 // FIXME: the last statement in the compount stmt has its value used. We
1556 // should not warn about it being unused.
1557
1558 // If there are sub stmts in the compound stmt, take the type of the last one
1559 // as the type of the stmtexpr.
1560 QualType Ty = Context.VoidTy;
1561
1562 if (!Compound->body_empty())
1563 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1564 Ty = LastExpr->getType();
1565
1566 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1567}
Steve Naroff78864672007-08-01 22:05:33 +00001568
1569Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation LPLoc,
1570 TypeTy *arg1, TypeTy *arg2,
1571 SourceLocation RPLoc) {
1572 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1573 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1574
1575 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1576
1577 return new TypesCompatibleExpr(Context.IntTy, LPLoc, argT1, argT2, RPLoc);
1578}
1579