blob: e26f5c1f3d54dc3ab81ee3fa2cdd610f537c8840 [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 Lattnerb87b1b32007-08-10 20:18:51 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026using namespace clang;
27
Steve Naroffdf7855b2007-02-21 23:46:25 +000028/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000029/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
30/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
31/// multiple tokens. However, the common case is that StringToks points to one
32/// string.
33///
34Action::ExprResult
Chris Lattner146762e2007-07-20 16:59:19 +000035Sema::ParseStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000036 assert(NumStringToks && "Must have at least one string!");
37
Steve Naroff4f88b312007-03-13 22:37:02 +000038 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
39 if (Literal.hadError)
40 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000041
Chris Lattner23b7eb62007-06-15 23:05:46 +000042 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000043 for (unsigned i = 0; i != NumStringToks; ++i)
44 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000045
46 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000047 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000048
Chris Lattner5b183d82006-11-10 05:03:26 +000049 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000050 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000051 Literal.AnyWide, t, StringToks[0].getLocation(),
52 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000053}
54
Chris Lattnere168f762006-11-10 05:29:30 +000055
Chris Lattnerac18be92006-11-20 06:49:47 +000056/// ParseIdentifierExpr - The parser read an identifier in expression context,
57/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
58/// identifier is used in an function call context.
59Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
60 IdentifierInfo &II,
61 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000062 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000063 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000064 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000065 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000066 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000067 if (HasTrailingLParen &&
68 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000069 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000070 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000071 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000072 // If this name wasn't predeclared and if this is not a function call,
73 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000074 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000075 }
Chris Lattner17ed4872006-11-20 04:58:19 +000076 }
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.
Chris Lattner7b939cf2007-08-23 21:58:08 +0000165 if (!Literal.isLong && !Literal.isLongLong) {
166 // Are int/unsigned possibilities?
Chris Lattner4481b422007-07-14 01:29:45 +0000167 unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000168 // Does it fit in a unsigned int?
169 if (ResultVal.isIntN(IntSize)) {
170 // Does it fit in a signed int?
171 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
172 t = Context.IntTy;
173 else if (AllowUnsigned)
174 t = Context.UnsignedIntTy;
175 }
176
177 if (!t.isNull())
178 ResultVal.trunc(IntSize);
179 }
180
181 // Are long/unsigned long possibilities?
182 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner4481b422007-07-14 01:29:45 +0000183 unsigned LongSize = Context.getTypeSize(Context.LongTy,
184 Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000185
186 // Does it fit in a unsigned long?
187 if (ResultVal.isIntN(LongSize)) {
188 // Does it fit in a signed long?
189 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
190 t = Context.LongTy;
191 else if (AllowUnsigned)
192 t = Context.UnsignedLongTy;
193 }
194 if (!t.isNull())
195 ResultVal.trunc(LongSize);
196 }
197
198 // Finally, check long long if needed.
199 if (t.isNull()) {
200 unsigned LongLongSize =
Chris Lattner4481b422007-07-14 01:29:45 +0000201 Context.getTypeSize(Context.LongLongTy, Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000202
203 // Does it fit in a unsigned long long?
204 if (ResultVal.isIntN(LongLongSize)) {
205 // Does it fit in a signed long long?
206 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
207 t = Context.LongLongTy;
208 else if (AllowUnsigned)
209 t = Context.UnsignedLongLongTy;
210 }
211 }
212
213 // If we still couldn't decide a type, we probably have something that
214 // does not fit in a signed long long, but has no U suffix.
215 if (t.isNull()) {
216 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
217 t = Context.UnsignedLongLongTy;
218 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000219 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000220
221 return new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000222 } else if (Literal.isFloatingLiteral()) {
Steve Naroff97b9e912007-07-09 23:53:58 +0000223 // FIXME: handle float values > 32 (including compute the real type...).
224 return new FloatingLiteral(Literal.GetFloatValue(), Context.FloatTy,
225 Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000226 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000227 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000228}
229
230Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
231 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000232 Expr *e = (Expr *)Val;
233 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000234 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000235}
236
Steve Naroff71b59a92007-06-04 22:22:31 +0000237/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000238/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000239QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
240 SourceLocation OpLoc, bool isSizeof) {
241 // C99 6.5.3.4p1:
242 if (isa<FunctionType>(exprType) && isSizeof)
243 // alignof(function) is allowed.
244 Diag(OpLoc, diag::ext_sizeof_function_type);
245 else if (exprType->isVoidType())
246 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
247 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000248 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000249 diag::err_alignof_incomplete_type,
250 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000251 return QualType(); // error
252 }
253 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
254 return Context.getSizeType();
255}
256
Chris Lattnere168f762006-11-10 05:29:30 +0000257Action::ExprResult Sema::
258ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000259 SourceLocation LPLoc, TypeTy *Ty,
260 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000261 // If error parsing type, ignore.
262 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000263
264 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000265 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000266
Steve Naroff043d45d2007-05-15 02:32:35 +0000267 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
268
269 if (resultType.isNull())
270 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000271 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000272}
273
Chris Lattner74ed76b2007-08-24 21:41:10 +0000274QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000275 DefaultFunctionArrayConversion(V);
276
277 if (const ComplexType *CT = V->getType()->getAsComplexType())
278 return CT->getElementType();
279 return V->getType();
280}
281
282
Chris Lattnere168f762006-11-10 05:29:30 +0000283
284Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
285 tok::TokenKind Kind,
286 ExprTy *Input) {
287 UnaryOperator::Opcode Opc;
288 switch (Kind) {
289 default: assert(0 && "Unknown unary op!");
290 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
291 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
292 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000293 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000294 if (result.isNull())
295 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000296 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000297}
298
299Action::ExprResult Sema::
300ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
301 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000302 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000303
304 // Perform default conversions.
305 DefaultFunctionArrayConversion(LHSExp);
306 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000307
Chris Lattner36d572b2007-07-16 00:14:47 +0000308 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000309
Steve Naroffc1aadb12007-03-28 21:49:40 +0000310 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
311 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000312 // in the subscript position. As a result, we need to derive the array base
313 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000314 Expr *BaseExpr, *IndexExpr;
315 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000316 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000317 BaseExpr = LHSExp;
318 IndexExpr = RHSExp;
319 // FIXME: need to deal with const...
320 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000321 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000322 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000323 BaseExpr = RHSExp;
324 IndexExpr = LHSExp;
325 // FIXME: need to deal with const...
326 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000327 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
328 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000329 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000330
331 // Component access limited to variables (reject vec4.rg[1]).
332 if (!isa<DeclRefExpr>(BaseExpr))
333 return Diag(LLoc, diag::err_ocuvector_component_access,
334 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000335 // FIXME: need to deal with const...
336 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000337 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000338 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
339 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000340 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000341 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000342 if (!IndexExpr->getType()->isIntegerType())
343 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
344 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000345
Chris Lattner36d572b2007-07-16 00:14:47 +0000346 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
347 // the following check catches trying to index a pointer to a function (e.g.
348 // void (*)(int)). Functions are not objects in C99.
349 if (!ResultType->isObjectType())
350 return Diag(BaseExpr->getLocStart(),
351 diag::err_typecheck_subscript_not_object,
352 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
353
354 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000355}
356
Steve Narofff8fd09e2007-07-27 22:15:19 +0000357QualType Sema::
358CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
359 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000360 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000361
362 // The vector accessor can't exceed the number of elements.
363 const char *compStr = CompName.getName();
364 if (strlen(compStr) > vecType->getNumElements()) {
365 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
366 baseType.getAsString(), SourceRange(CompLoc));
367 return QualType();
368 }
369 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000370 if (vecType->getPointAccessorIdx(*compStr) != -1) {
371 do
372 compStr++;
373 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
374 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
375 do
376 compStr++;
377 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
378 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
379 do
380 compStr++;
381 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
382 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000383
384 if (*compStr) {
385 // We didn't get to the end of the string. This means the component names
386 // didn't come from the same set *or* we encountered an illegal name.
387 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
388 std::string(compStr,compStr+1), SourceRange(CompLoc));
389 return QualType();
390 }
391 // Each component accessor can't exceed the vector type.
392 compStr = CompName.getName();
393 while (*compStr) {
394 if (vecType->isAccessorWithinNumElements(*compStr))
395 compStr++;
396 else
397 break;
398 }
399 if (*compStr) {
400 // We didn't get to the end of the string. This means a component accessor
401 // exceeds the number of elements in the vector.
402 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
403 baseType.getAsString(), SourceRange(CompLoc));
404 return QualType();
405 }
406 // The component accessor looks fine - now we need to compute the actual type.
407 // The vector type is implied by the component accessor. For example,
408 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
409 unsigned CompSize = strlen(CompName.getName());
410 if (CompSize == 1)
411 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000412
413 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
414 // Now look up the TypeDefDecl from the vector type. Without this,
415 // diagostics look bad. We want OCU vector types to appear built-in.
416 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
417 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
418 return Context.getTypedefType(OCUVectorDecls[i]);
419 }
420 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000421}
422
Chris Lattnere168f762006-11-10 05:29:30 +0000423Action::ExprResult Sema::
424ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
425 tok::TokenKind OpKind, SourceLocation MemberLoc,
426 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000427 Expr *BaseExpr = static_cast<Expr *>(Base);
428 assert(BaseExpr && "no record expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000429
Steve Naroff185616f2007-07-26 03:11:44 +0000430 QualType BaseType = BaseExpr->getType();
431 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000432
Steve Narofff1e53692007-03-23 22:27:02 +0000433 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000434 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000435 BaseType = PT->getPointeeType();
436 else
437 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
438 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000439 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000440 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000441 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000442 RecordDecl *RDecl = RTy->getDecl();
443 if (RTy->isIncompleteType())
444 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
445 BaseExpr->getSourceRange());
446 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000447 FieldDecl *MemberDecl = RDecl->getMember(&Member);
448 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000449 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
450 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000451 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
452 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000453 // Component access limited to variables (reject vec4.rg.g).
454 if (!isa<DeclRefExpr>(BaseExpr))
455 return Diag(OpLoc, diag::err_ocuvector_component_access,
456 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000457 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
458 if (ret.isNull())
459 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000460 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Steve Naroff185616f2007-07-26 03:11:44 +0000461 } else
462 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
463 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000464}
465
466/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
467/// This provides the location of the left/right parens and a list of comma
468/// locations.
469Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000470ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
471 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000472 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000473 Expr *Fn = static_cast<Expr *>(fn);
474 Expr **Args = reinterpret_cast<Expr**>(args);
475 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000476
Chris Lattner38dbdb22007-07-21 03:03:59 +0000477 UsualUnaryConversions(Fn);
478 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000479
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000480 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
481 // type pointer to function".
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000482 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000483 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000484 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
485 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000486
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000487 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000488 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000489 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
490 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000491
Steve Naroff17f76e02007-05-03 21:03:48 +0000492 // If a prototype isn't declared, the parser implicitly defines a func decl
493 QualType resultType = funcT->getResultType();
494
495 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
496 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
497 // assignment, to the types of the corresponding parameter, ...
498
499 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000500 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000501
Steve Naroffb8c289d2007-05-08 22:18:00 +0000502 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000503 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000504 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000505 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000506 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000507 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000508 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000509 SourceRange(Args[NumArgsInProto]->getLocStart(),
510 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000511 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000512 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000513 }
514 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000515 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000516 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000517 assert(argExpr && "ParseCallExpr(): missing argument expression");
518
Steve Naroff17f76e02007-05-03 21:03:48 +0000519 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000520 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000521
Steve Naroffb8af1c22007-07-25 20:45:33 +0000522 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner41977962007-07-31 19:29:30 +0000523 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000524 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000525 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000526 lhsType = Context.getPointerType(lhsType);
527
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000528 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
529 argExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000530 if (Args[i] != argExpr) // The expression was converted.
531 Args[i] = argExpr; // Make sure we store the converted expression.
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000532 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000533
534 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000535 switch (result) {
536 case Compatible:
537 break;
538 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000539 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000540 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000541 Diag(l, diag::ext_typecheck_passing_pointer_int,
542 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000543 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000544 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000545 break;
546 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000547 Diag(l, diag::ext_typecheck_passing_pointer_int,
548 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000549 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000550 break;
551 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000552 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000553 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000554 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000555 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000556 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000557 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
558 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000559 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000560 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000561 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000562 return Diag(l, diag::err_typecheck_passing_incompatible,
563 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000564 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000565 }
566 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000567 // Even if the types checked, bail if we had the wrong number of arguments.
Chris Lattner38dbdb22007-07-21 03:03:59 +0000568 if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
Steve Naroffb8c289d2007-05-08 22:18:00 +0000569 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000570 }
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000571
572 // Do special checking on direct calls to functions.
573 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
574 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
575 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Anders Carlssona3a9c432007-08-17 15:44:17 +0000576 if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args, NumArgsInCall))
Anders Carlsson98f07902007-08-17 05:31:46 +0000577 return true;
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000578
Chris Lattner38dbdb22007-07-21 03:03:59 +0000579 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000580}
581
582Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000583ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000584 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000585 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
586 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000587 // FIXME: put back this assert when initializers are worked out.
588 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
589 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000590
591 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000592 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000593}
594
595Action::ExprResult Sema::
596ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
597 SourceLocation RParenLoc) {
598 // FIXME: add semantic analysis (C99 6.7.8). This involves
599 // knowledge of the object being intialized. As a result, the code for
600 // doing the semantic analysis will likely be located elsewhere (i.e. in
601 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
602 return false; // FIXME instantiate an InitListExpr.
603}
604
605Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000606ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
607 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000608 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
609
610 Expr *castExpr = static_cast<Expr*>(Op);
611 QualType castType = QualType::getFromOpaquePtr(Ty);
612
Chris Lattnerbd270732007-07-18 16:00:06 +0000613 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
614 // type needs to be scalar.
615 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000616 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
617 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
618 }
619 if (!castExpr->getType()->isScalarType()) {
620 return Diag(castExpr->getLocStart(),
621 diag::err_typecheck_expect_scalar_operand,
622 castExpr->getType().getAsString(), castExpr->getSourceRange());
623 }
624 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000625}
626
Steve Narofff8a28c52007-05-15 20:29:32 +0000627inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000628 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000629 UsualUnaryConversions(cond);
630 UsualUnaryConversions(lex);
631 UsualUnaryConversions(rex);
632 QualType condT = cond->getType();
633 QualType lexT = lex->getType();
634 QualType rexT = rex->getType();
635
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000636 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000637 if (!condT->isScalarType()) { // C99 6.5.15p2
638 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
639 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000640 return QualType();
641 }
642 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000643 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
644 UsualArithmeticConversions(lex, rex);
645 return lex->getType();
646 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000647 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
648 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
649
650 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
651 return lexT;
652
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000653 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000654 lexT.getAsString(), rexT.getAsString(),
655 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000656 return QualType();
657 }
658 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000659 // C99 6.5.15p3
660 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000661 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000662 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000663 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000664
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000665 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
666 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
667 // get the "pointed to" types
668 QualType lhptee = LHSPT->getPointeeType();
669 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000670
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000671 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
672 if (lhptee->isVoidType() &&
673 (rhptee->isObjectType() || rhptee->isIncompleteType()))
674 return lexT;
675 if (rhptee->isVoidType() &&
676 (lhptee->isObjectType() || lhptee->isIncompleteType()))
677 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000678
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000679 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
680 rhptee.getUnqualifiedType())) {
681 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
682 lexT.getAsString(), rexT.getAsString(),
683 lex->getSourceRange(), rex->getSourceRange());
684 return lexT; // FIXME: this is an _ext - is this return o.k?
685 }
686 // The pointer types are compatible.
687 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
688 // differently qualified versions of compatible types, the result type is a
689 // pointer to an appropriately qualified version of the *composite* type.
690 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000691 }
692 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000693
Steve Naroff7a5af782007-07-13 16:58:59 +0000694 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
695 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000696
697 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000698 lexT.getAsString(), rexT.getAsString(),
699 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000700 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000701}
702
Chris Lattnere168f762006-11-10 05:29:30 +0000703/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
704/// in the case of a the GNU conditional expr extension.
705Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
706 SourceLocation ColonLoc,
707 ExprTy *Cond, ExprTy *LHS,
708 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000709 Expr *CondExpr = (Expr *) Cond;
710 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
711 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
712 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000713 if (result.isNull())
714 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000715 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000716}
717
Steve Naroff81569d22007-07-15 02:02:06 +0000718// promoteExprToType - a helper function to ensure we create exactly one
719// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000720static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000721 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
722 impCast->setType(type);
723 else
724 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000725 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000726}
727
728/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000729void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000730 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000731 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000732
Chris Lattnercd1d0862007-07-31 16:56:34 +0000733 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000734 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
735 t = e->getType();
736 }
Steve Naroff81569d22007-07-15 02:02:06 +0000737 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000738 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000739 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000740 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000741}
742
Steve Naroff71b59a92007-06-04 22:22:31 +0000743/// UsualUnaryConversion - Performs various conversions that are common to most
744/// operators (C99 6.3). The conversions of array and function types are
745/// sometimes surpressed. For example, the array->pointer conversion doesn't
746/// apply if the array is an argument to the sizeof or address (&) operators.
747/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000748void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000749 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000750 assert(!t.isNull() && "UsualUnaryConversions - missing type");
751
Chris Lattnercd1d0862007-07-31 16:56:34 +0000752 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000753 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
754 t = expr->getType();
755 }
Steve Naroff81569d22007-07-15 02:02:06 +0000756 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000757 promoteExprToType(expr, Context.IntTy);
758 else
759 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000760}
761
Steve Naroff1926c832007-04-24 00:23:05 +0000762/// UsualArithmeticConversions - Performs various conversions that are common to
763/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
764/// routine returns the first non-arithmetic type found. The client is
765/// responsible for emitting appropriate error diagnostics.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000766QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
767 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +0000768 if (!isCompAssign) {
769 UsualUnaryConversions(lhsExpr);
770 UsualUnaryConversions(rhsExpr);
771 }
Steve Naroff94a5aca2007-07-16 22:23:01 +0000772 QualType lhs = lhsExpr->getType();
773 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000774
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000775 // If both types are identical, no conversion is needed.
776 if (lhs == rhs)
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000777 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000778
779 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
780 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000781 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000782 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000783
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000784 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000785
786 // Handle complex types first (C99 6.3.1.8p1).
787 if (lhs->isComplexType() || rhs->isComplexType()) {
788 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000789 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000790 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
791 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000792 }
793 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000794 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
795 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000796 }
Steve Naroff81569d22007-07-15 02:02:06 +0000797 // Two complex types. Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000798 if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000799 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
800 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000801 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000802 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
803 return rhs;
Steve Naroffbf223ba2007-04-24 20:56:26 +0000804 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000805 // Now handle "real" floating types (i.e. float, double, long double).
806 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
807 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000808 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000809 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
810 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000811 }
812 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000813 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
814 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000815 }
Steve Naroff81569d22007-07-15 02:02:06 +0000816 // We have two real floating types, float/complex combos were handled above.
817 // Convert the smaller operand to the bigger result.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000818 if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000819 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
820 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000821 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000822 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
823 return rhs;
Steve Naroffb01bbe32007-04-25 01:22:31 +0000824 }
Steve Naroff81569d22007-07-15 02:02:06 +0000825 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000826 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000827 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
828 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000829 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000830 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
831 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +0000832}
833
Steve Naroff3f597292007-05-11 22:18:03 +0000834// CheckPointerTypesForAssignment - This is a very tricky routine (despite
835// being closely modeled after the C99 spec:-). The odd characteristic of this
836// routine is it effectively iqnores the qualifiers on the top level pointee.
837// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
838// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000839Sema::AssignmentCheckResult
840Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000841 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000842
843 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000844 lhptee = lhsType->getAsPointerType()->getPointeeType();
845 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000846
847 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000848 lhptee = lhptee.getCanonicalType();
849 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000850
Steve Naroff98cf3e92007-06-06 18:38:38 +0000851 AssignmentCheckResult r = Compatible;
852
Steve Naroff3f597292007-05-11 22:18:03 +0000853 // C99 6.5.16.1p1: This following citation is common to constraints
854 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
855 // qualifiers of the type *pointed to* by the right;
856 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
857 rhptee.getQualifiers())
858 r = CompatiblePointerDiscardsQualifiers;
859
860 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
861 // incomplete type and the other is a pointer to a qualified or unqualified
862 // version of void...
863 if (lhptee.getUnqualifiedType()->isVoidType() &&
864 (rhptee->isObjectType() || rhptee->isIncompleteType()))
865 ;
866 else if (rhptee.getUnqualifiedType()->isVoidType() &&
867 (lhptee->isObjectType() || lhptee->isIncompleteType()))
868 ;
869 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
870 // unqualified versions of compatible types, ...
871 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
872 rhptee.getUnqualifiedType()))
873 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000874 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000875}
876
Steve Naroff98cf3e92007-06-06 18:38:38 +0000877/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000878/// has code to accommodate several GCC extensions when type checking
879/// pointers. Here are some objectionable examples that GCC considers warnings:
880///
881/// int a, *pint;
882/// short *pshort;
883/// struct foo *pfoo;
884///
885/// pint = pshort; // warning: assignment from incompatible pointer type
886/// a = pint; // warning: assignment makes integer from pointer without a cast
887/// pint = a; // warning: assignment makes pointer from integer without a cast
888/// pint = pfoo; // warning: assignment from incompatible pointer type
889///
890/// As a result, the code for dealing with pointers is more complex than the
891/// C99 spec dictates.
892/// Note: the warning above turn into errors when -pedantic-errors is enabled.
893///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000894Sema::AssignmentCheckResult
895Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000896 if (lhsType == rhsType) // common case, fast path...
897 return Compatible;
898
Steve Naroff84ff4b42007-07-09 21:31:10 +0000899 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000900 if (lhsType->isVectorType() || rhsType->isVectorType()) {
901 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
902 return Incompatible;
903 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000904 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000905 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000906 if (rhsType->isIntegerType())
907 return PointerFromInt;
908
Steve Naroff3f597292007-05-11 22:18:03 +0000909 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000910 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000911 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000912 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
913 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
914 return IntFromPointer;
915
Steve Naroff3f597292007-05-11 22:18:03 +0000916 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000917 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000918 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
919 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000920 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000921 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
922 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000923 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000924 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000925 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000926}
927
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000928Sema::AssignmentCheckResult
929Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
930 // This check seems unnatural, however it is necessary to insure the proper
931 // conversion of functions/arrays. If the conversion were done for all
932 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
933 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +0000934 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000935
936 Sema::AssignmentCheckResult result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000937
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000938 result = CheckAssignmentConstraints(lhsType, rExpr->getType());
939
940 // C99 6.5.16.1p2: The value of the right operand is converted to the
941 // type of the assignment expression.
942 if (rExpr->getType() != lhsType)
943 promoteExprToType(rExpr, lhsType);
944 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000945}
946
947Sema::AssignmentCheckResult
948Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
949 return CheckAssignmentConstraints(lhsType, rhsType);
950}
951
Steve Naroff7a5af782007-07-13 16:58:59 +0000952inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000953 Diag(loc, diag::err_typecheck_invalid_operands,
954 lex->getType().getAsString(), rex->getType().getAsString(),
955 lex->getSourceRange(), rex->getSourceRange());
956}
957
Steve Naroff7a5af782007-07-13 16:58:59 +0000958inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
959 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000960 QualType lhsType = lex->getType(), rhsType = rex->getType();
961
962 // make sure the vector types are identical.
963 if (lhsType == rhsType)
964 return lhsType;
965 // You cannot convert between vector values of different size.
966 Diag(loc, diag::err_typecheck_vector_not_convertable,
967 lex->getType().getAsString(), rex->getType().getAsString(),
968 lex->getSourceRange(), rex->getSourceRange());
969 return QualType();
970}
971
Steve Naroff218bc2b2007-05-04 21:54:46 +0000972inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000973 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000974{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000975 QualType lhsType = lex->getType(), rhsType = rex->getType();
976
977 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000978 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +0000979
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000980 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000981
Steve Naroffdbd9e892007-07-17 00:58:39 +0000982 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000983 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000984 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000985 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000986}
987
Steve Naroff218bc2b2007-05-04 21:54:46 +0000988inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000989 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000990{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000991 QualType lhsType = lex->getType(), rhsType = rex->getType();
992
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000993 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000994
Steve Naroffdbd9e892007-07-17 00:58:39 +0000995 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000996 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000997 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000998 return QualType();
999}
1000
1001inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001002 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001003{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001004 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001005 return CheckVectorOperands(loc, lex, rex);
1006
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001007 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff94a5aca2007-07-16 22:23:01 +00001008
Steve Naroffe4718892007-04-27 18:30:00 +00001009 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001010 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001011 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001012
Steve Naroffdbd9e892007-07-17 00:58:39 +00001013 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1014 return lex->getType();
1015 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1016 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001017 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001018 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001019}
1020
Steve Naroff218bc2b2007-05-04 21:54:46 +00001021inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001022 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001023{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001024 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001025 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001026
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001027 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001028
1029 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001030 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001031 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001032
1033 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001034 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001035 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00001036 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001037 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001038 return QualType();
1039}
1040
1041inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001042 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001043{
Chris Lattner1d411a82007-06-05 20:52:21 +00001044 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1045 // for int << longlong -> the result type should be int, not long long.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001046 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001047
Steve Naroffdbd9e892007-07-17 00:58:39 +00001048 // handle the common case first (both operands are arithmetic).
1049 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001050 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001051 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001052 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001053}
1054
Steve Naroff218bc2b2007-05-04 21:54:46 +00001055inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
Steve Naroff7a5af782007-07-13 16:58:59 +00001056 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001057{
Steve Naroff47fea352007-08-10 18:26:40 +00001058 // C99 6.5.8p3
1059 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1060 UsualArithmeticConversions(lex, rex);
1061 else {
1062 UsualUnaryConversions(lex);
1063 UsualUnaryConversions(rex);
1064 }
Steve Naroff31090012007-07-16 21:54:35 +00001065 QualType lType = lex->getType();
1066 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001067
1068 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001069 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +00001070
Chris Lattner1895e582007-08-26 01:10:14 +00001071 bool LHSIsNull = lex->isNullPointerConstant(Context);
1072 bool RHSIsNull = rex->isNullPointerConstant(Context);
1073
Steve Naroffcdee44c2007-08-16 21:48:38 +00001074 // All of the following pointer related warnings are GCC extensions. One
1075 // day, we can consider making them errors (when -pedantic-errors is enabled).
1076 if (lType->isPointerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001077 if (!LHSIsNull && !RHSIsNull &&
1078 !Type::pointerTypesAreCompatible(lType, rType)) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001079 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1080 lType.getAsString(), rType.getAsString(),
1081 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001082 }
Chris Lattner1895e582007-08-26 01:10:14 +00001083 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001084 return Context.IntTy;
1085 }
1086 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001087 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001088 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1089 lType.getAsString(), rType.getAsString(),
1090 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001091 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001092 return Context.IntTy;
1093 }
1094 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001095 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001096 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1097 lType.getAsString(), rType.getAsString(),
1098 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001099 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001100 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001101 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001102 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001103 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001104}
1105
Steve Naroff218bc2b2007-05-04 21:54:46 +00001106inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
Steve Naroff7a5af782007-07-13 16:58:59 +00001107 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001108{
Steve Naroff47fea352007-08-10 18:26:40 +00001109 // C99 6.5.9p4
1110 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1111 UsualArithmeticConversions(lex, rex);
1112 else {
1113 UsualUnaryConversions(lex);
1114 UsualUnaryConversions(rex);
1115 }
Steve Naroff31090012007-07-16 21:54:35 +00001116 QualType lType = lex->getType();
1117 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001118
1119 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001120 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001121
Chris Lattner1895e582007-08-26 01:10:14 +00001122 bool LHSIsNull = lex->isNullPointerConstant(Context);
1123 bool RHSIsNull = rex->isNullPointerConstant(Context);
1124
Steve Naroffcdee44c2007-08-16 21:48:38 +00001125 // All of the following pointer related warnings are GCC extensions. One
1126 // day, we can consider making them errors (when -pedantic-errors is enabled).
1127 if (lType->isPointerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001128 if (!LHSIsNull && !RHSIsNull &&
1129 !Type::pointerTypesAreCompatible(lType, rType)) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001130 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1131 lType.getAsString(), rType.getAsString(),
1132 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001133 }
Chris Lattner1895e582007-08-26 01:10:14 +00001134 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001135 return Context.IntTy;
1136 }
1137 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001138 if (!RHSIsNull) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001139 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1140 lType.getAsString(), rType.getAsString(),
1141 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001142 }
Chris Lattner1895e582007-08-26 01:10:14 +00001143 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001144 return Context.IntTy;
1145 }
1146 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001147 if (!LHSIsNull) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001148 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1149 lType.getAsString(), rType.getAsString(),
1150 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffcdee44c2007-08-16 21:48:38 +00001151 }
Chris Lattner1895e582007-08-26 01:10:14 +00001152 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001153 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001154 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001155 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001156 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001157}
1158
Steve Naroff218bc2b2007-05-04 21:54:46 +00001159inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001160 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001161{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001162 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001163 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001164
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001165 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001166
Steve Naroffdbd9e892007-07-17 00:58:39 +00001167 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001168 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001169 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001170 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001171}
1172
Steve Naroff218bc2b2007-05-04 21:54:46 +00001173inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001174 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001175{
Steve Naroff31090012007-07-16 21:54:35 +00001176 UsualUnaryConversions(lex);
1177 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001178
Steve Naroffdbd9e892007-07-17 00:58:39 +00001179 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001180 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001181 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001182 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001183}
1184
Steve Naroff35d85152007-05-07 00:24:15 +00001185inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001186 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001187{
Steve Naroff0af91202007-04-27 21:51:21 +00001188 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001189 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001190 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001191 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1192
1193 switch (mlval) { // C99 6.5.16p2
1194 case Expr::MLV_Valid:
1195 break;
1196 case Expr::MLV_ConstQualified:
1197 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1198 hadError = true;
1199 break;
1200 case Expr::MLV_ArrayType:
1201 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1202 lhsType.getAsString(), lex->getSourceRange());
1203 return QualType();
1204 case Expr::MLV_NotObjectType:
1205 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1206 lhsType.getAsString(), lex->getSourceRange());
1207 return QualType();
1208 case Expr::MLV_InvalidExpression:
1209 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1210 lex->getSourceRange());
1211 return QualType();
1212 case Expr::MLV_IncompleteType:
1213 case Expr::MLV_IncompleteVoidType:
1214 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1215 lhsType.getAsString(), lex->getSourceRange());
1216 return QualType();
Steve Naroff0d595ca2007-07-30 03:29:09 +00001217 case Expr::MLV_DuplicateVectorComponents:
1218 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1219 lex->getSourceRange());
1220 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001221 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001222 AssignmentCheckResult result;
1223
1224 if (compoundType.isNull())
1225 result = CheckSingleAssignmentConstraints(lhsType, rex);
1226 else
1227 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffad373bd2007-07-31 12:34:36 +00001228
Steve Naroff218bc2b2007-05-04 21:54:46 +00001229 // decode the result (notice that extensions still return a type).
1230 switch (result) {
1231 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001232 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001233 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001234 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001235 lhsType.getAsString(), rhsType.getAsString(),
1236 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001237 hadError = true;
1238 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001239 case PointerFromInt:
1240 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001241 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001242 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1243 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001244 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001245 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001246 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001247 case IntFromPointer:
1248 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1249 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001250 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001251 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001252 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001253 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1254 lhsType.getAsString(), rhsType.getAsString(),
1255 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001256 break;
1257 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001258 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1259 lhsType.getAsString(), rhsType.getAsString(),
1260 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001261 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001262 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001263 // C99 6.5.16p3: The type of an assignment expression is the type of the
1264 // left operand unless the left operand has qualified type, in which case
1265 // it is the unqualified version of the type of the left operand.
1266 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1267 // is converted to the type of the assignment expression (above).
1268 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1269 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001270}
1271
Steve Naroff218bc2b2007-05-04 21:54:46 +00001272inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001273 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001274 UsualUnaryConversions(rex);
1275 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001276}
1277
Steve Naroff7a5af782007-07-13 16:58:59 +00001278/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1279/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001280QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001281 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001282 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001283
Steve Naroff9d139172007-08-24 17:20:07 +00001284 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff35d85152007-05-07 00:24:15 +00001285 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1286 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001287 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1288 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001289 return QualType();
1290 }
Steve Naroff9d139172007-08-24 17:20:07 +00001291 } else if (!resType->isRealType()) {
1292 if (resType->isComplexType())
1293 // C99 does not support ++/-- on complex types.
1294 Diag(OpLoc, diag::ext_integer_increment_complex,
1295 resType.getAsString(), op->getSourceRange());
1296 else {
1297 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1298 resType.getAsString(), op->getSourceRange());
1299 return QualType();
1300 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001301 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001302 // At this point, we know we have a real, complex or pointer type.
1303 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001304 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1305 if (mlval != Expr::MLV_Valid) {
1306 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001307 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001308 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001309 return QualType();
1310 }
1311 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001312}
1313
Steve Naroff1926c832007-04-24 00:23:05 +00001314/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001315/// This routine allows us to typecheck complex/recursive expressions
1316/// where the declaration is needed for type checking. Here are some
1317/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001318static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001319 switch (e->getStmtClass()) {
1320 case Stmt::DeclRefExprClass:
1321 return cast<DeclRefExpr>(e)->getDecl();
1322 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001323 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001324 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001325 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001326 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001327 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001328 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001329 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001330 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001331 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001332 default:
1333 return 0;
1334 }
1335}
1336
1337/// CheckAddressOfOperand - The operand of & must be either a function
1338/// designator or an lvalue designating an object. If it is an lvalue, the
1339/// object cannot be declared with storage class register or be a bit field.
1340/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001341/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001342QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001343 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001344 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001345
Steve Naroff9358c712007-05-27 23:58:33 +00001346 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001347 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1348 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001349 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001350 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1351 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001352 return QualType();
1353 }
Steve Naroff47500512007-04-19 23:00:49 +00001354 } else if (dcl) {
1355 // We have an lvalue with a decl. Make sure the decl is not declared
1356 // with the register storage-class specifier.
1357 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001358 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001359 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001360 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001361 return QualType();
1362 }
Steve Narofff633d092007-04-25 19:01:39 +00001363 } else
1364 assert(0 && "Unknown/unexpected decl type");
1365
Steve Naroff47500512007-04-19 23:00:49 +00001366 // FIXME: add check for bitfields!
1367 }
1368 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001369 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001370}
1371
Steve Naroff35d85152007-05-07 00:24:15 +00001372QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001373 UsualUnaryConversions(op);
1374 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001375
Chris Lattnerc996b172007-07-31 16:53:04 +00001376 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001377 QualType ptype = PT->getPointeeType();
1378 // C99 6.5.3.2p4. "if it points to an object,...".
1379 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1380 // GCC compat: special case 'void *' (treat as warning).
1381 if (ptype->isVoidType()) {
1382 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001383 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001384 } else {
1385 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001386 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001387 return QualType();
1388 }
1389 }
1390 return ptype;
1391 }
1392 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001393 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001394 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001395}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001396
1397static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1398 tok::TokenKind Kind) {
1399 BinaryOperator::Opcode Opc;
1400 switch (Kind) {
1401 default: assert(0 && "Unknown binop!");
1402 case tok::star: Opc = BinaryOperator::Mul; break;
1403 case tok::slash: Opc = BinaryOperator::Div; break;
1404 case tok::percent: Opc = BinaryOperator::Rem; break;
1405 case tok::plus: Opc = BinaryOperator::Add; break;
1406 case tok::minus: Opc = BinaryOperator::Sub; break;
1407 case tok::lessless: Opc = BinaryOperator::Shl; break;
1408 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1409 case tok::lessequal: Opc = BinaryOperator::LE; break;
1410 case tok::less: Opc = BinaryOperator::LT; break;
1411 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1412 case tok::greater: Opc = BinaryOperator::GT; break;
1413 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1414 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1415 case tok::amp: Opc = BinaryOperator::And; break;
1416 case tok::caret: Opc = BinaryOperator::Xor; break;
1417 case tok::pipe: Opc = BinaryOperator::Or; break;
1418 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1419 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1420 case tok::equal: Opc = BinaryOperator::Assign; break;
1421 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1422 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1423 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1424 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1425 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1426 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1427 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1428 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1429 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1430 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1431 case tok::comma: Opc = BinaryOperator::Comma; break;
1432 }
1433 return Opc;
1434}
1435
Steve Naroff35d85152007-05-07 00:24:15 +00001436static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1437 tok::TokenKind Kind) {
1438 UnaryOperator::Opcode Opc;
1439 switch (Kind) {
1440 default: assert(0 && "Unknown unary op!");
1441 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1442 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1443 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1444 case tok::star: Opc = UnaryOperator::Deref; break;
1445 case tok::plus: Opc = UnaryOperator::Plus; break;
1446 case tok::minus: Opc = UnaryOperator::Minus; break;
1447 case tok::tilde: Opc = UnaryOperator::Not; break;
1448 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1449 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1450 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1451 case tok::kw___real: Opc = UnaryOperator::Real; break;
1452 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001453 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001454 }
1455 return Opc;
1456}
1457
Steve Naroff218bc2b2007-05-04 21:54:46 +00001458// Binary Operators. 'Tok' is the token for the operator.
1459Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1460 ExprTy *LHS, ExprTy *RHS) {
1461 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1462 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1463
1464 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1465 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1466
Chris Lattner256c21b2007-06-28 03:53:10 +00001467 QualType ResultTy; // Result type of the binary operator.
1468 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001469
1470 switch (Opc) {
1471 default:
1472 assert(0 && "Unknown binary expr!");
1473 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001474 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001475 break;
1476 case BinaryOperator::Mul:
1477 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001478 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001479 break;
1480 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001481 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001482 break;
1483 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001484 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001485 break;
1486 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001487 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001488 break;
1489 case BinaryOperator::Shl:
1490 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001491 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001492 break;
1493 case BinaryOperator::LE:
1494 case BinaryOperator::LT:
1495 case BinaryOperator::GE:
1496 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001497 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001498 break;
1499 case BinaryOperator::EQ:
1500 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001501 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001502 break;
1503 case BinaryOperator::And:
1504 case BinaryOperator::Xor:
1505 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001506 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001507 break;
1508 case BinaryOperator::LAnd:
1509 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001510 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001511 break;
1512 case BinaryOperator::MulAssign:
1513 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001514 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001515 if (!CompTy.isNull())
1516 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001517 break;
1518 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001519 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001520 if (!CompTy.isNull())
1521 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001522 break;
1523 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001524 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001525 if (!CompTy.isNull())
1526 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001527 break;
1528 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001529 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001530 if (!CompTy.isNull())
1531 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001532 break;
1533 case BinaryOperator::ShlAssign:
1534 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001535 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001536 if (!CompTy.isNull())
1537 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001538 break;
1539 case BinaryOperator::AndAssign:
1540 case BinaryOperator::XorAssign:
1541 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001542 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001543 if (!CompTy.isNull())
1544 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001545 break;
1546 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001547 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001548 break;
1549 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001550 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001551 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001552 if (CompTy.isNull())
1553 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1554 else
Chris Lattner9369a562007-06-29 16:31:29 +00001555 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001556}
1557
Steve Naroff35d85152007-05-07 00:24:15 +00001558// Unary Operators. 'Tok' is the token for the operator.
1559Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001560 ExprTy *input) {
1561 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001562 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1563 QualType resultType;
1564 switch (Opc) {
1565 default:
1566 assert(0 && "Unimplemented unary expr!");
1567 case UnaryOperator::PreInc:
1568 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001569 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001570 break;
1571 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001572 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001573 break;
1574 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001575 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001576 break;
1577 case UnaryOperator::Plus:
1578 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001579 UsualUnaryConversions(Input);
1580 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001581 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001582 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1583 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001584 break;
1585 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001586 UsualUnaryConversions(Input);
1587 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00001588 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1589 if (!resultType->isIntegerType()) {
1590 if (resultType->isComplexType())
1591 // C99 does not support '~' for complex conjugation.
1592 Diag(OpLoc, diag::ext_integer_complement_complex,
1593 resultType.getAsString());
1594 else
1595 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1596 resultType.getAsString());
1597 }
Steve Naroff35d85152007-05-07 00:24:15 +00001598 break;
1599 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001600 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001601 DefaultFunctionArrayConversion(Input);
1602 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001603 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001604 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1605 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001606 // LNot always has type int. C99 6.5.3.3p5.
1607 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001608 break;
1609 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001610 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001611 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001612 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001613 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1614 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00001615 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00001616 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00001617 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00001618 break;
Chris Lattner86554282007-06-08 22:32:33 +00001619 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00001620 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001621 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001622 }
1623 if (resultType.isNull())
1624 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001625 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001626}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001627
1628/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1629Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1630 SourceLocation LabLoc,
1631 IdentifierInfo *LabelII) {
1632 // Look up the record for this label identifier.
1633 LabelStmt *&LabelDecl = LabelMap[LabelII];
1634
1635 // If we haven't seen this label yet, create a forward reference.
1636 if (LabelDecl == 0)
1637 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1638
1639 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001640 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1641 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001642}
1643
Chris Lattner366727f2007-07-24 16:58:17 +00001644Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
1645 SourceLocation RPLoc) { // "({..})"
1646 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1647 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1648 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1649
1650 // FIXME: there are a variety of strange constraints to enforce here, for
1651 // example, it is not possible to goto into a stmt expression apparently.
1652 // More semantic analysis is needed.
1653
1654 // FIXME: the last statement in the compount stmt has its value used. We
1655 // should not warn about it being unused.
1656
1657 // If there are sub stmts in the compound stmt, take the type of the last one
1658 // as the type of the stmtexpr.
1659 QualType Ty = Context.VoidTy;
1660
1661 if (!Compound->body_empty())
1662 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1663 Ty = LastExpr->getType();
1664
1665 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1666}
Steve Naroff78864672007-08-01 22:05:33 +00001667
Steve Naroff788d8642007-08-01 23:45:51 +00001668Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00001669 TypeTy *arg1, TypeTy *arg2,
1670 SourceLocation RPLoc) {
1671 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1672 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1673
1674 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1675
Steve Naroff788d8642007-08-01 23:45:51 +00001676 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2, RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00001677}
1678
Steve Naroff9efdabc2007-08-03 21:21:27 +00001679Sema::ExprResult Sema::ParseChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
1680 ExprTy *expr1, ExprTy *expr2,
1681 SourceLocation RPLoc) {
1682 Expr *CondExpr = static_cast<Expr*>(cond);
1683 Expr *LHSExpr = static_cast<Expr*>(expr1);
1684 Expr *RHSExpr = static_cast<Expr*>(expr2);
1685
1686 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
1687
1688 // The conditional expression is required to be a constant expression.
1689 llvm::APSInt condEval(32);
1690 SourceLocation ExpLoc;
1691 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
1692 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
1693 CondExpr->getSourceRange());
1694
1695 // If the condition is > zero, then the AST type is the same as the LSHExpr.
1696 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
1697 RHSExpr->getType();
1698 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
1699}
1700
Anders Carlsson76f4a902007-08-21 17:43:55 +00001701// TODO: Move this to SemaObjC.cpp
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001702Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001703 StringLiteral* S = static_cast<StringLiteral *>(string);
1704
1705 if (CheckBuiltinCFStringArgument(S))
1706 return true;
1707
1708 QualType t = Context.getCFConstantStringType();
1709 t = t.getQualifiedType(QualType::Const);
1710 t = Context.getPointerType(t);
1711
1712 return new ObjCStringLiteral(S, t);
1713}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001714
1715Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
1716 SourceLocation LParenLoc,
1717 TypeTy *Ty,
1718 SourceLocation RParenLoc) {
1719 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
1720
1721 QualType t = Context.getPointerType(Context.CharTy);
1722 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
1723}