blob: daddb50c7179ac3291eb7f611c1a62cec59036ef [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 Naroff7e6f7c22007-08-28 03:03:08 +000077 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroffcf871f52007-08-28 18:45:29 +000078 // Only create DeclRefExpr's for valid Decl's.
Steve Narofff93b6722007-08-28 20:14:24 +000079 if (VD->isInvalidDecl())
Steve Naroff7e6f7c22007-08-28 03:03:08 +000080 return true;
Steve Naroff509fe022007-05-17 01:16:00 +000081 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff7e6f7c22007-08-28 03:03:08 +000082 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +000083 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000084 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
85
86 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +000087 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +000088}
Chris Lattnere168f762006-11-10 05:29:30 +000089
Anders Carlsson625bfc82007-07-21 05:21:51 +000090Sema::ExprResult Sema::ParsePreDefinedExpr(SourceLocation Loc,
91 tok::TokenKind Kind) {
92 PreDefinedExpr::IdentType IT;
93
Chris Lattnere168f762006-11-10 05:29:30 +000094 switch (Kind) {
95 default:
96 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +000097 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
Anders Carlsson625bfc82007-07-21 05:21:51 +000098 IT = PreDefinedExpr::Func;
99 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000100 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000101 IT = PreDefinedExpr::Function;
102 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000103 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000104 IT = PreDefinedExpr::PrettyFunction;
105 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000106 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000107
108 // Pre-defined identifiers are always of type char *.
109 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000110}
111
Chris Lattner146762e2007-07-20 16:59:19 +0000112Sema::ExprResult Sema::ParseCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000113 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000114 CharBuffer.resize(Tok.getLength());
115 const char *ThisTokBegin = &CharBuffer[0];
116 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
117
118 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
119 Tok.getLocation(), PP);
120 if (Literal.hadError())
121 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000122 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
123 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000124}
125
Chris Lattner146762e2007-07-20 16:59:19 +0000126Action::ExprResult Sema::ParseNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000127 // fast path for a single digit (which is quite common). A single digit
128 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
129 if (Tok.getLength() == 1) {
130 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000131
Chris Lattner4481b422007-07-14 01:29:45 +0000132 unsigned IntSize = Context.getTypeSize(Context.IntTy, Tok.getLocation());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000133 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
134 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000135 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000136 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000137 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000138 IntegerBuffer.resize(Tok.getLength());
139 const char *ThisTokBegin = &IntegerBuffer[0];
140
Chris Lattner67ca9252007-05-21 01:08:44 +0000141 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000142 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000143 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000144 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000145 if (Literal.hadError)
146 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000147
Chris Lattner1c20a172007-08-26 03:42:43 +0000148 Expr *Res;
149
150 if (Literal.isFloatingLiteral()) {
151 // FIXME: handle float values > 32 (including compute the real type...).
152 QualType Ty = Literal.isFloat ? Context.FloatTy : Context.DoubleTy;
153 Res = new FloatingLiteral(Literal.GetFloatValue(), Ty, Tok.getLocation());
154 } else if (!Literal.isIntegerLiteral()) {
155 return ExprResult(true);
156 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000157 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000158
Neil Boothac582c52007-08-29 22:00:19 +0000159 // long long is a C99 feature.
160 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth4a1ee052007-08-29 22:13:52 +0000161 Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000162 Diag(Tok.getLocation(), diag::ext_longlong);
163
Chris Lattner67ca9252007-05-21 01:08:44 +0000164 // Get the value in the widest-possible width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000165 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000166
167 if (Literal.GetIntegerValue(ResultVal)) {
168 // If this value didn't fit into uintmax_t, warn and force to ull.
169 Diag(Tok.getLocation(), diag::warn_integer_too_large);
170 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000171 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000172 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000173 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000174 // If this value fits into a ULL, try to figure out what else it fits into
175 // according to the rules of C99 6.4.4.1p5.
176
177 // Octal, Hexadecimal, and integers with a U suffix are allowed to
178 // be an unsigned int.
179 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
180
181 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner7b939cf2007-08-23 21:58:08 +0000182 if (!Literal.isLong && !Literal.isLongLong) {
183 // Are int/unsigned possibilities?
Chris Lattner4481b422007-07-14 01:29:45 +0000184 unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000185 // Does it fit in a unsigned int?
186 if (ResultVal.isIntN(IntSize)) {
187 // Does it fit in a signed int?
188 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
189 t = Context.IntTy;
190 else if (AllowUnsigned)
191 t = Context.UnsignedIntTy;
192 }
193
194 if (!t.isNull())
195 ResultVal.trunc(IntSize);
196 }
197
198 // Are long/unsigned long possibilities?
199 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner4481b422007-07-14 01:29:45 +0000200 unsigned LongSize = Context.getTypeSize(Context.LongTy,
201 Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000202
203 // Does it fit in a unsigned long?
204 if (ResultVal.isIntN(LongSize)) {
205 // Does it fit in a signed long?
206 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
207 t = Context.LongTy;
208 else if (AllowUnsigned)
209 t = Context.UnsignedLongTy;
210 }
211 if (!t.isNull())
212 ResultVal.trunc(LongSize);
213 }
214
215 // Finally, check long long if needed.
216 if (t.isNull()) {
217 unsigned LongLongSize =
Chris Lattner4481b422007-07-14 01:29:45 +0000218 Context.getTypeSize(Context.LongLongTy, Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000219
220 // Does it fit in a unsigned long long?
221 if (ResultVal.isIntN(LongLongSize)) {
222 // Does it fit in a signed long long?
223 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
224 t = Context.LongLongTy;
225 else if (AllowUnsigned)
226 t = Context.UnsignedLongLongTy;
227 }
228 }
229
230 // If we still couldn't decide a type, we probably have something that
231 // does not fit in a signed long long, but has no U suffix.
232 if (t.isNull()) {
233 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
234 t = Context.UnsignedLongLongTy;
235 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000236 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000237
Chris Lattner1c20a172007-08-26 03:42:43 +0000238 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000239 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000240
241 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
242 if (Literal.isImaginary)
243 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
244
245 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000246}
247
248Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
249 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000250 Expr *e = (Expr *)Val;
251 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000252 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000253}
254
Steve Naroff71b59a92007-06-04 22:22:31 +0000255/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000256/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000257QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
258 SourceLocation OpLoc, bool isSizeof) {
259 // C99 6.5.3.4p1:
260 if (isa<FunctionType>(exprType) && isSizeof)
261 // alignof(function) is allowed.
262 Diag(OpLoc, diag::ext_sizeof_function_type);
263 else if (exprType->isVoidType())
264 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
265 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000266 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000267 diag::err_alignof_incomplete_type,
268 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000269 return QualType(); // error
270 }
271 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
272 return Context.getSizeType();
273}
274
Chris Lattnere168f762006-11-10 05:29:30 +0000275Action::ExprResult Sema::
276ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000277 SourceLocation LPLoc, TypeTy *Ty,
278 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000279 // If error parsing type, ignore.
280 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000281
282 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000283 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000284
Steve Naroff043d45d2007-05-15 02:32:35 +0000285 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
286
287 if (resultType.isNull())
288 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000289 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000290}
291
Chris Lattner74ed76b2007-08-24 21:41:10 +0000292QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000293 DefaultFunctionArrayConversion(V);
294
Chris Lattnere267f5d2007-08-26 05:39:26 +0000295 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000296 if (const ComplexType *CT = V->getType()->getAsComplexType())
297 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000298
299 // Otherwise they pass through real integer and floating point types here.
300 if (V->getType()->isArithmeticType())
301 return V->getType();
302
303 // Reject anything else.
304 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
305 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000306}
307
308
Chris Lattnere168f762006-11-10 05:29:30 +0000309
310Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
311 tok::TokenKind Kind,
312 ExprTy *Input) {
313 UnaryOperator::Opcode Opc;
314 switch (Kind) {
315 default: assert(0 && "Unknown unary op!");
316 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
317 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
318 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000319 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000320 if (result.isNull())
321 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000322 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000323}
324
325Action::ExprResult Sema::
326ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
327 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000328 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000329
330 // Perform default conversions.
331 DefaultFunctionArrayConversion(LHSExp);
332 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000333
Chris Lattner36d572b2007-07-16 00:14:47 +0000334 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000335
Steve Naroffc1aadb12007-03-28 21:49:40 +0000336 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattnerf17bd422007-08-30 17:45:32 +0000337 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000338 // in the subscript position. As a result, we need to derive the array base
339 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000340 Expr *BaseExpr, *IndexExpr;
341 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000342 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000343 BaseExpr = LHSExp;
344 IndexExpr = RHSExp;
345 // FIXME: need to deal with const...
346 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000347 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000348 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000349 BaseExpr = RHSExp;
350 IndexExpr = LHSExp;
351 // FIXME: need to deal with const...
352 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000353 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
354 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000355 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000356
357 // Component access limited to variables (reject vec4.rg[1]).
358 if (!isa<DeclRefExpr>(BaseExpr))
359 return Diag(LLoc, diag::err_ocuvector_component_access,
360 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000361 // FIXME: need to deal with const...
362 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000363 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000364 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
365 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000366 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000367 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000368 if (!IndexExpr->getType()->isIntegerType())
369 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
370 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000371
Chris Lattner36d572b2007-07-16 00:14:47 +0000372 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
373 // the following check catches trying to index a pointer to a function (e.g.
374 // void (*)(int)). Functions are not objects in C99.
375 if (!ResultType->isObjectType())
376 return Diag(BaseExpr->getLocStart(),
377 diag::err_typecheck_subscript_not_object,
378 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
379
380 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000381}
382
Steve Narofff8fd09e2007-07-27 22:15:19 +0000383QualType Sema::
384CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
385 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000386 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000387
388 // The vector accessor can't exceed the number of elements.
389 const char *compStr = CompName.getName();
390 if (strlen(compStr) > vecType->getNumElements()) {
391 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
392 baseType.getAsString(), SourceRange(CompLoc));
393 return QualType();
394 }
395 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000396 if (vecType->getPointAccessorIdx(*compStr) != -1) {
397 do
398 compStr++;
399 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
400 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
401 do
402 compStr++;
403 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
404 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
405 do
406 compStr++;
407 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
408 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000409
410 if (*compStr) {
411 // We didn't get to the end of the string. This means the component names
412 // didn't come from the same set *or* we encountered an illegal name.
413 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
414 std::string(compStr,compStr+1), SourceRange(CompLoc));
415 return QualType();
416 }
417 // Each component accessor can't exceed the vector type.
418 compStr = CompName.getName();
419 while (*compStr) {
420 if (vecType->isAccessorWithinNumElements(*compStr))
421 compStr++;
422 else
423 break;
424 }
425 if (*compStr) {
426 // We didn't get to the end of the string. This means a component accessor
427 // exceeds the number of elements in the vector.
428 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
429 baseType.getAsString(), SourceRange(CompLoc));
430 return QualType();
431 }
432 // The component accessor looks fine - now we need to compute the actual type.
433 // The vector type is implied by the component accessor. For example,
434 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
435 unsigned CompSize = strlen(CompName.getName());
436 if (CompSize == 1)
437 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000438
439 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
440 // Now look up the TypeDefDecl from the vector type. Without this,
441 // diagostics look bad. We want OCU vector types to appear built-in.
442 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
443 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
444 return Context.getTypedefType(OCUVectorDecls[i]);
445 }
446 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000447}
448
Chris Lattnere168f762006-11-10 05:29:30 +0000449Action::ExprResult Sema::
450ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
451 tok::TokenKind OpKind, SourceLocation MemberLoc,
452 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000453 Expr *BaseExpr = static_cast<Expr *>(Base);
454 assert(BaseExpr && "no record expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000455
Steve Naroff185616f2007-07-26 03:11:44 +0000456 QualType BaseType = BaseExpr->getType();
457 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000458
Steve Narofff1e53692007-03-23 22:27:02 +0000459 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000460 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000461 BaseType = PT->getPointeeType();
462 else
463 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
464 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000465 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000466 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000467 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000468 RecordDecl *RDecl = RTy->getDecl();
469 if (RTy->isIncompleteType())
470 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
471 BaseExpr->getSourceRange());
472 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000473 FieldDecl *MemberDecl = RDecl->getMember(&Member);
474 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000475 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
476 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000477 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
478 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000479 // Component access limited to variables (reject vec4.rg.g).
480 if (!isa<DeclRefExpr>(BaseExpr))
481 return Diag(OpLoc, diag::err_ocuvector_component_access,
482 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000483 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
484 if (ret.isNull())
485 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000486 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Steve Naroff185616f2007-07-26 03:11:44 +0000487 } else
488 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
489 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000490}
491
492/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
493/// This provides the location of the left/right parens and a list of comma
494/// locations.
495Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000496ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
497 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000498 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000499 Expr *Fn = static_cast<Expr *>(fn);
500 Expr **Args = reinterpret_cast<Expr**>(args);
501 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000502
Chris Lattner38dbdb22007-07-21 03:03:59 +0000503 UsualUnaryConversions(Fn);
504 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000505
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000506 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
507 // type pointer to function".
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000508 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000509 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000510 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
511 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000512
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000513 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000514 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000515 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
516 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000517
Steve Naroff17f76e02007-05-03 21:03:48 +0000518 // If a prototype isn't declared, the parser implicitly defines a func decl
519 QualType resultType = funcT->getResultType();
520
521 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
522 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
523 // assignment, to the types of the corresponding parameter, ...
524
525 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000526 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000527
Steve Naroffb8c289d2007-05-08 22:18:00 +0000528 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000529 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000530 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000531 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000532 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000533 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000534 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000535 SourceRange(Args[NumArgsInProto]->getLocStart(),
536 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000537 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000538 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000539 }
540 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000541 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000542 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000543 assert(argExpr && "ParseCallExpr(): missing argument expression");
544
Steve Naroff17f76e02007-05-03 21:03:48 +0000545 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000546 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000547
Steve Naroffb8af1c22007-07-25 20:45:33 +0000548 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner41977962007-07-31 19:29:30 +0000549 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000550 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000551 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000552 lhsType = Context.getPointerType(lhsType);
553
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000554 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
555 argExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000556 if (Args[i] != argExpr) // The expression was converted.
557 Args[i] = argExpr; // Make sure we store the converted expression.
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000558 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000559
560 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000561 switch (result) {
562 case Compatible:
563 break;
564 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000565 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000566 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000567 Diag(l, diag::ext_typecheck_passing_pointer_int,
568 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000569 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000570 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000571 break;
572 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000573 Diag(l, diag::ext_typecheck_passing_pointer_int,
574 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000575 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000576 break;
577 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000578 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000579 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000580 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000581 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000582 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000583 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
584 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000585 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000586 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000587 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000588 return Diag(l, diag::err_typecheck_passing_incompatible,
589 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000590 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000591 }
592 }
Steve Naroff0b661582007-08-28 23:30:39 +0000593 if (NumArgsInCall > NumArgsInProto && proto->isVariadic()) {
594 // Promote the arguments (C99 6.5.2.2p7).
595 for (unsigned i = NumArgsInProto; i < NumArgsInCall; i++) {
596 Expr *argExpr = Args[i];
597 assert(argExpr && "ParseCallExpr(): missing argument expression");
598
599 DefaultArgumentPromotion(argExpr);
600 if (Args[i] != argExpr) // The expression was converted.
601 Args[i] = argExpr; // Make sure we store the converted expression.
602 }
603 } else if (NumArgsInCall != NumArgsInProto && !proto->isVariadic()) {
604 // Even if the types checked, bail if the number of arguments don't match.
Steve Naroffb8c289d2007-05-08 22:18:00 +0000605 return true;
Steve Naroff0b661582007-08-28 23:30:39 +0000606 }
607 } else if (isa<FunctionTypeNoProto>(funcT)) {
608 // Promote the arguments (C99 6.5.2.2p6).
609 for (unsigned i = 0; i < NumArgsInCall; i++) {
610 Expr *argExpr = Args[i];
611 assert(argExpr && "ParseCallExpr(): missing argument expression");
612
613 DefaultArgumentPromotion(argExpr);
614 if (Args[i] != argExpr) // The expression was converted.
615 Args[i] = argExpr; // Make sure we store the converted expression.
616 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000617 }
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000618 // Do special checking on direct calls to functions.
619 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
620 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
621 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattnerf17bd422007-08-30 17:45:32 +0000622 if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args,
623 NumArgsInCall))
Anders Carlsson98f07902007-08-17 05:31:46 +0000624 return true;
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000625
Chris Lattner38dbdb22007-07-21 03:03:59 +0000626 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000627}
628
629Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000630ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000631 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000632 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
633 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000634 // FIXME: put back this assert when initializers are worked out.
635 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
636 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000637
638 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000639 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000640}
641
642Action::ExprResult Sema::
Anders Carlsson4692db02007-08-31 04:56:16 +0000643ParseInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
644 SourceLocation RBraceLoc) {
645// Expr **InitList = reinterpret_cast<Expr**>(initlist);
646
Steve Narofffbd09832007-07-19 01:06:55 +0000647 // FIXME: add semantic analysis (C99 6.7.8). This involves
648 // knowledge of the object being intialized. As a result, the code for
649 // doing the semantic analysis will likely be located elsewhere (i.e. in
650 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
Anders Carlsson4692db02007-08-31 04:56:16 +0000651
652 //return new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
Steve Narofffbd09832007-07-19 01:06:55 +0000653 return false; // FIXME instantiate an InitListExpr.
654}
655
656Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000657ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
658 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000659 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
660
661 Expr *castExpr = static_cast<Expr*>(Op);
662 QualType castType = QualType::getFromOpaquePtr(Ty);
663
Steve Naroff43b8f7f2007-08-31 00:32:44 +0000664 UsualUnaryConversions(castExpr);
665
Chris Lattnerbd270732007-07-18 16:00:06 +0000666 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
667 // type needs to be scalar.
668 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000669 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
670 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
671 }
672 if (!castExpr->getType()->isScalarType()) {
673 return Diag(castExpr->getLocStart(),
674 diag::err_typecheck_expect_scalar_operand,
675 castExpr->getType().getAsString(), castExpr->getSourceRange());
676 }
677 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000678}
679
Steve Narofff8a28c52007-05-15 20:29:32 +0000680inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000681 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000682 UsualUnaryConversions(cond);
683 UsualUnaryConversions(lex);
684 UsualUnaryConversions(rex);
685 QualType condT = cond->getType();
686 QualType lexT = lex->getType();
687 QualType rexT = rex->getType();
688
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000689 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000690 if (!condT->isScalarType()) { // C99 6.5.15p2
691 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
692 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000693 return QualType();
694 }
695 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000696 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
697 UsualArithmeticConversions(lex, rex);
698 return lex->getType();
699 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000700 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
701 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
702
Chris Lattnerf17bd422007-08-30 17:45:32 +0000703 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000704 return lexT;
705
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000706 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000707 lexT.getAsString(), rexT.getAsString(),
708 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000709 return QualType();
710 }
711 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000712 // C99 6.5.15p3
713 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000714 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000715 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000716 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000717
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000718 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
719 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
720 // get the "pointed to" types
721 QualType lhptee = LHSPT->getPointeeType();
722 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000723
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000724 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
725 if (lhptee->isVoidType() &&
726 (rhptee->isObjectType() || rhptee->isIncompleteType()))
727 return lexT;
728 if (rhptee->isVoidType() &&
729 (lhptee->isObjectType() || lhptee->isIncompleteType()))
730 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000731
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000732 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
733 rhptee.getUnqualifiedType())) {
734 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
735 lexT.getAsString(), rexT.getAsString(),
736 lex->getSourceRange(), rex->getSourceRange());
737 return lexT; // FIXME: this is an _ext - is this return o.k?
738 }
739 // The pointer types are compatible.
Chris Lattnerf17bd422007-08-30 17:45:32 +0000740 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
741 // differently qualified versions of compatible types, the result type is
742 // a pointer to an appropriately qualified version of the *composite*
743 // type.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000744 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000745 }
746 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000747
Steve Naroff7a5af782007-07-13 16:58:59 +0000748 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
749 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000750
751 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000752 lexT.getAsString(), rexT.getAsString(),
753 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000754 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000755}
756
Chris Lattnere168f762006-11-10 05:29:30 +0000757/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
758/// in the case of a the GNU conditional expr extension.
759Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
760 SourceLocation ColonLoc,
761 ExprTy *Cond, ExprTy *LHS,
762 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000763 Expr *CondExpr = (Expr *) Cond;
764 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
765 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
766 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000767 if (result.isNull())
768 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000769 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000770}
771
Steve Naroff81569d22007-07-15 02:02:06 +0000772// promoteExprToType - a helper function to ensure we create exactly one
773// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000774static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000775 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
776 impCast->setType(type);
777 else
778 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000779 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000780}
781
Steve Naroff0b661582007-08-28 23:30:39 +0000782/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
783/// do not have a prototype. Integer promotions are performed on each
784/// argument, and arguments that have type float are promoted to double.
785void Sema::DefaultArgumentPromotion(Expr *&expr) {
786 QualType t = expr->getType();
787 assert(!t.isNull() && "DefaultArgumentPromotion - missing type");
788
789 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
790 promoteExprToType(expr, Context.IntTy);
791 if (t == Context.FloatTy)
792 promoteExprToType(expr, Context.DoubleTy);
793}
794
Steve Naroff81569d22007-07-15 02:02:06 +0000795/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000796void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000797 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000798 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000799
Chris Lattnercd1d0862007-07-31 16:56:34 +0000800 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000801 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
802 t = e->getType();
803 }
Steve Naroff81569d22007-07-15 02:02:06 +0000804 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000805 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000806 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000807 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000808}
809
Steve Naroff71b59a92007-06-04 22:22:31 +0000810/// UsualUnaryConversion - Performs various conversions that are common to most
811/// operators (C99 6.3). The conversions of array and function types are
812/// sometimes surpressed. For example, the array->pointer conversion doesn't
813/// apply if the array is an argument to the sizeof or address (&) operators.
814/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000815void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000816 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000817 assert(!t.isNull() && "UsualUnaryConversions - missing type");
818
Chris Lattnercd1d0862007-07-31 16:56:34 +0000819 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000820 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
821 t = expr->getType();
822 }
Steve Naroff81569d22007-07-15 02:02:06 +0000823 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000824 promoteExprToType(expr, Context.IntTy);
825 else
826 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000827}
828
Chris Lattnerf17bd422007-08-30 17:45:32 +0000829/// UsualArithmeticConversions - Performs various conversions that are common to
Steve Naroff1926c832007-04-24 00:23:05 +0000830/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
831/// routine returns the first non-arithmetic type found. The client is
832/// responsible for emitting appropriate error diagnostics.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000833QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
834 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +0000835 if (!isCompAssign) {
836 UsualUnaryConversions(lhsExpr);
837 UsualUnaryConversions(rhsExpr);
838 }
Steve Naroff94a5aca2007-07-16 22:23:01 +0000839 QualType lhs = lhsExpr->getType();
840 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000841
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000842 // If both types are identical, no conversion is needed.
843 if (lhs == rhs)
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000844 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000845
846 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
847 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000848 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000849 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000850
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000851 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000852
853 // Handle complex types first (C99 6.3.1.8p1).
854 if (lhs->isComplexType() || rhs->isComplexType()) {
855 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000856 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000857 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
858 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000859 }
860 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000861 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
862 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000863 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000864 // This handles complex/complex, complex/float, or float/complex.
865 // When both operands are complex, the shorter operand is converted to the
866 // type of the longer, and that is the type of the result. This corresponds
867 // to what is done when combining two real floating-point operands.
868 // The fun begins when size promotion occur across type domains.
869 // From H&S 6.3.4: When one operand is complex and the other is a real
870 // floating-point type, the less precise type is converted, within it's
871 // real or complex domain, to the precision of the other type. For example,
872 // when combining a "long double" with a "double _Complex", the
873 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff7af82d42007-08-27 15:30:22 +0000874 int result = Context.compareFloatingType(lhs, rhs);
875
876 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroffe31313d2007-08-27 21:32:55 +0000877 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
878 if (!isCompAssign)
879 promoteExprToType(rhsExpr, rhs);
880 } else if (result < 0) { // The right side is bigger, convert lhs.
881 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
882 if (!isCompAssign)
883 promoteExprToType(lhsExpr, lhs);
884 }
885 // At this point, lhs and rhs have the same rank/size. Now, make sure the
886 // domains match. This is a requirement for our implementation, C99
887 // does not require this promotion.
888 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
889 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroffa042db22007-08-27 21:43:43 +0000890 if (!isCompAssign)
891 promoteExprToType(lhsExpr, rhs);
892 return rhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000893 } else { // handle "_Complex double, double".
Steve Naroffa042db22007-08-27 21:43:43 +0000894 if (!isCompAssign)
895 promoteExprToType(rhsExpr, lhs);
896 return lhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000897 }
Steve Naroffdbd9e892007-07-17 00:58:39 +0000898 }
Steve Naroffa042db22007-08-27 21:43:43 +0000899 return lhs; // The domain/size match exactly.
Steve Naroffbf223ba2007-04-24 20:56:26 +0000900 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000901 // Now handle "real" floating types (i.e. float, double, long double).
902 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
903 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000904 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000905 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
906 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000907 }
908 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000909 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
910 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000911 }
Steve Naroff81569d22007-07-15 02:02:06 +0000912 // We have two real floating types, float/complex combos were handled above.
913 // Convert the smaller operand to the bigger result.
Steve Naroff7af82d42007-08-27 15:30:22 +0000914 int result = Context.compareFloatingType(lhs, rhs);
915
916 if (result > 0) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000917 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
918 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000919 }
Steve Naroff7af82d42007-08-27 15:30:22 +0000920 if (result < 0) { // convert the lhs
921 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
922 return rhs;
923 }
924 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +0000925 }
Steve Naroff81569d22007-07-15 02:02:06 +0000926 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000927 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000928 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
929 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000930 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000931 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
932 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +0000933}
934
Steve Naroff3f597292007-05-11 22:18:03 +0000935// CheckPointerTypesForAssignment - This is a very tricky routine (despite
936// being closely modeled after the C99 spec:-). The odd characteristic of this
937// routine is it effectively iqnores the qualifiers on the top level pointee.
938// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
939// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000940Sema::AssignmentCheckResult
941Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000942 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000943
944 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000945 lhptee = lhsType->getAsPointerType()->getPointeeType();
946 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000947
948 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000949 lhptee = lhptee.getCanonicalType();
950 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000951
Steve Naroff98cf3e92007-06-06 18:38:38 +0000952 AssignmentCheckResult r = Compatible;
953
Steve Naroff3f597292007-05-11 22:18:03 +0000954 // C99 6.5.16.1p1: This following citation is common to constraints
955 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
956 // qualifiers of the type *pointed to* by the right;
957 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
958 rhptee.getQualifiers())
959 r = CompatiblePointerDiscardsQualifiers;
960
961 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
962 // incomplete type and the other is a pointer to a qualified or unqualified
963 // version of void...
964 if (lhptee.getUnqualifiedType()->isVoidType() &&
965 (rhptee->isObjectType() || rhptee->isIncompleteType()))
966 ;
967 else if (rhptee.getUnqualifiedType()->isVoidType() &&
968 (lhptee->isObjectType() || lhptee->isIncompleteType()))
969 ;
970 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
971 // unqualified versions of compatible types, ...
972 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
973 rhptee.getUnqualifiedType()))
974 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000975 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000976}
977
Steve Naroff98cf3e92007-06-06 18:38:38 +0000978/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000979/// has code to accommodate several GCC extensions when type checking
980/// pointers. Here are some objectionable examples that GCC considers warnings:
981///
982/// int a, *pint;
983/// short *pshort;
984/// struct foo *pfoo;
985///
986/// pint = pshort; // warning: assignment from incompatible pointer type
987/// a = pint; // warning: assignment makes integer from pointer without a cast
988/// pint = a; // warning: assignment makes pointer from integer without a cast
989/// pint = pfoo; // warning: assignment from incompatible pointer type
990///
991/// As a result, the code for dealing with pointers is more complex than the
992/// C99 spec dictates.
993/// Note: the warning above turn into errors when -pedantic-errors is enabled.
994///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000995Sema::AssignmentCheckResult
996Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000997 if (lhsType == rhsType) // common case, fast path...
998 return Compatible;
999
Steve Naroff84ff4b42007-07-09 21:31:10 +00001000 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +00001001 if (lhsType->isVectorType() || rhsType->isVectorType()) {
1002 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
1003 return Incompatible;
1004 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001005 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +00001006 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001007 if (rhsType->isIntegerType())
1008 return PointerFromInt;
1009
Steve Naroff3f597292007-05-11 22:18:03 +00001010 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001011 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +00001012 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001013 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1014 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
1015 return IntFromPointer;
1016
Steve Naroff3f597292007-05-11 22:18:03 +00001017 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001018 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +00001019 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
1020 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001021 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +00001022 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
1023 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001024 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +00001025 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001026 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +00001027}
1028
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001029Sema::AssignmentCheckResult
1030Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
1031 // This check seems unnatural, however it is necessary to insure the proper
1032 // conversion of functions/arrays. If the conversion were done for all
1033 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
1034 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +00001035 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001036
1037 Sema::AssignmentCheckResult result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001038
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001039 result = CheckAssignmentConstraints(lhsType, rExpr->getType());
1040
1041 // C99 6.5.16.1p2: The value of the right operand is converted to the
1042 // type of the assignment expression.
1043 if (rExpr->getType() != lhsType)
1044 promoteExprToType(rExpr, lhsType);
1045 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001046}
1047
1048Sema::AssignmentCheckResult
1049Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1050 return CheckAssignmentConstraints(lhsType, rhsType);
1051}
1052
Steve Naroff7a5af782007-07-13 16:58:59 +00001053inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001054 Diag(loc, diag::err_typecheck_invalid_operands,
1055 lex->getType().getAsString(), rex->getType().getAsString(),
1056 lex->getSourceRange(), rex->getSourceRange());
1057}
1058
Steve Naroff7a5af782007-07-13 16:58:59 +00001059inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1060 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +00001061 QualType lhsType = lex->getType(), rhsType = rex->getType();
1062
1063 // make sure the vector types are identical.
1064 if (lhsType == rhsType)
1065 return lhsType;
1066 // You cannot convert between vector values of different size.
1067 Diag(loc, diag::err_typecheck_vector_not_convertable,
1068 lex->getType().getAsString(), rex->getType().getAsString(),
1069 lex->getSourceRange(), rex->getSourceRange());
1070 return QualType();
1071}
1072
Steve Naroff218bc2b2007-05-04 21:54:46 +00001073inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001074 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001075{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001076 QualType lhsType = lex->getType(), rhsType = rex->getType();
1077
1078 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001079 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001080
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001081 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001082
Steve Naroffdbd9e892007-07-17 00:58:39 +00001083 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001084 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001085 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001086 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001087}
1088
Steve Naroff218bc2b2007-05-04 21:54:46 +00001089inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001090 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001091{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001092 QualType lhsType = lex->getType(), rhsType = rex->getType();
1093
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001094 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001095
Steve Naroffdbd9e892007-07-17 00:58:39 +00001096 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001097 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001098 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001099 return QualType();
1100}
1101
1102inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001103 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001104{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001105 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001106 return CheckVectorOperands(loc, lex, rex);
1107
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001108 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff94a5aca2007-07-16 22:23:01 +00001109
Steve Naroffe4718892007-04-27 18:30:00 +00001110 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001111 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001112 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001113
Steve Naroffdbd9e892007-07-17 00:58:39 +00001114 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1115 return lex->getType();
1116 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1117 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001118 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001119 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001120}
1121
Steve Naroff218bc2b2007-05-04 21:54:46 +00001122inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001123 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001124{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001125 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001126 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001127
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001128 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001129
1130 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001131 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001132 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001133
1134 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001135 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001136 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00001137 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001138 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001139 return QualType();
1140}
1141
1142inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001143 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001144{
Chris Lattner1d411a82007-06-05 20:52:21 +00001145 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1146 // for int << longlong -> the result type should be int, not long long.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001147 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001148
Steve Naroffdbd9e892007-07-17 00:58:39 +00001149 // handle the common case first (both operands are arithmetic).
1150 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001151 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001152 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001153 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001154}
1155
Chris Lattnerb620c342007-08-26 01:18:55 +00001156inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1157 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Steve Naroff1926c832007-04-24 00:23:05 +00001158{
Chris Lattnerb620c342007-08-26 01:18:55 +00001159 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001160 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1161 UsualArithmeticConversions(lex, rex);
1162 else {
1163 UsualUnaryConversions(lex);
1164 UsualUnaryConversions(rex);
1165 }
Steve Naroff31090012007-07-16 21:54:35 +00001166 QualType lType = lex->getType();
1167 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001168
Chris Lattnerb620c342007-08-26 01:18:55 +00001169 if (isRelational) {
1170 if (lType->isRealType() && rType->isRealType())
1171 return Context.IntTy;
1172 } else {
Chris Lattner27022852007-08-30 06:10:41 +00001173 if (lType->isFloatingType() && rType->isFloatingType())
Ted Kremenek5ccf0d82007-08-29 18:06:12 +00001174 Diag(loc, diag::warn_floatingpoint_eq);
1175
Chris Lattnerb620c342007-08-26 01:18:55 +00001176 if (lType->isArithmeticType() && rType->isArithmeticType())
1177 return Context.IntTy;
1178 }
Steve Naroffe4718892007-04-27 18:30:00 +00001179
Chris Lattner1895e582007-08-26 01:10:14 +00001180 bool LHSIsNull = lex->isNullPointerConstant(Context);
1181 bool RHSIsNull = rex->isNullPointerConstant(Context);
1182
Chris Lattnerb620c342007-08-26 01:18:55 +00001183 // All of the following pointer related warnings are GCC extensions, except
1184 // when handling null pointer constants. One day, we can consider making them
1185 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001186 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner1895e582007-08-26 01:10:14 +00001187 if (!LHSIsNull && !RHSIsNull &&
Steve Naroff808eb8f2007-08-27 04:08:11 +00001188 !Type::pointerTypesAreCompatible(lType.getUnqualifiedType(),
1189 rType.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001190 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1191 lType.getAsString(), rType.getAsString(),
1192 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001193 }
Chris Lattner1895e582007-08-26 01:10:14 +00001194 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001195 return Context.IntTy;
1196 }
1197 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001198 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001199 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1200 lType.getAsString(), rType.getAsString(),
1201 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001202 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001203 return Context.IntTy;
1204 }
1205 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001206 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001207 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1208 lType.getAsString(), rType.getAsString(),
1209 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001210 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001211 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001212 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001213 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001214 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001215}
1216
Steve Naroff218bc2b2007-05-04 21:54:46 +00001217inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001218 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001219{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001220 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001221 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001222
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001223 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001224
Steve Naroffdbd9e892007-07-17 00:58:39 +00001225 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001226 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001227 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001228 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001229}
1230
Steve Naroff218bc2b2007-05-04 21:54:46 +00001231inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001232 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001233{
Steve Naroff31090012007-07-16 21:54:35 +00001234 UsualUnaryConversions(lex);
1235 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001236
Steve Naroffdbd9e892007-07-17 00:58:39 +00001237 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001238 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001239 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001240 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001241}
1242
Steve Naroff35d85152007-05-07 00:24:15 +00001243inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001244 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001245{
Steve Naroff0af91202007-04-27 21:51:21 +00001246 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001247 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001248 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001249 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1250
1251 switch (mlval) { // C99 6.5.16p2
1252 case Expr::MLV_Valid:
1253 break;
1254 case Expr::MLV_ConstQualified:
1255 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1256 hadError = true;
1257 break;
1258 case Expr::MLV_ArrayType:
1259 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1260 lhsType.getAsString(), lex->getSourceRange());
1261 return QualType();
1262 case Expr::MLV_NotObjectType:
1263 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1264 lhsType.getAsString(), lex->getSourceRange());
1265 return QualType();
1266 case Expr::MLV_InvalidExpression:
1267 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1268 lex->getSourceRange());
1269 return QualType();
1270 case Expr::MLV_IncompleteType:
1271 case Expr::MLV_IncompleteVoidType:
1272 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1273 lhsType.getAsString(), lex->getSourceRange());
1274 return QualType();
Steve Naroff0d595ca2007-07-30 03:29:09 +00001275 case Expr::MLV_DuplicateVectorComponents:
1276 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1277 lex->getSourceRange());
1278 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001279 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001280 AssignmentCheckResult result;
1281
1282 if (compoundType.isNull())
1283 result = CheckSingleAssignmentConstraints(lhsType, rex);
1284 else
1285 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffad373bd2007-07-31 12:34:36 +00001286
Steve Naroff218bc2b2007-05-04 21:54:46 +00001287 // decode the result (notice that extensions still return a type).
1288 switch (result) {
1289 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001290 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001291 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001292 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001293 lhsType.getAsString(), rhsType.getAsString(),
1294 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001295 hadError = true;
1296 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001297 case PointerFromInt:
1298 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001299 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001300 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1301 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001302 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001303 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001304 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001305 case IntFromPointer:
1306 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1307 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001308 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001309 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001310 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001311 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1312 lhsType.getAsString(), rhsType.getAsString(),
1313 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001314 break;
1315 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001316 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1317 lhsType.getAsString(), rhsType.getAsString(),
1318 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001319 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001320 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001321 // C99 6.5.16p3: The type of an assignment expression is the type of the
1322 // left operand unless the left operand has qualified type, in which case
1323 // it is the unqualified version of the type of the left operand.
1324 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1325 // is converted to the type of the assignment expression (above).
Chris Lattnerf17bd422007-08-30 17:45:32 +00001326 // C++ 5.17p1: the type of the assignment expression is that of its left
1327 // oprdu.
Steve Naroff98cf3e92007-06-06 18:38:38 +00001328 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001329}
1330
Steve Naroff218bc2b2007-05-04 21:54:46 +00001331inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001332 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001333 UsualUnaryConversions(rex);
1334 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001335}
1336
Steve Naroff7a5af782007-07-13 16:58:59 +00001337/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1338/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001339QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001340 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001341 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001342
Steve Naroff9d139172007-08-24 17:20:07 +00001343 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff35d85152007-05-07 00:24:15 +00001344 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1345 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001346 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1347 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001348 return QualType();
1349 }
Steve Naroff9d139172007-08-24 17:20:07 +00001350 } else if (!resType->isRealType()) {
1351 if (resType->isComplexType())
1352 // C99 does not support ++/-- on complex types.
1353 Diag(OpLoc, diag::ext_integer_increment_complex,
1354 resType.getAsString(), op->getSourceRange());
1355 else {
1356 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1357 resType.getAsString(), op->getSourceRange());
1358 return QualType();
1359 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001360 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001361 // At this point, we know we have a real, complex or pointer type.
1362 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001363 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1364 if (mlval != Expr::MLV_Valid) {
1365 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001366 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001367 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001368 return QualType();
1369 }
1370 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001371}
1372
Steve Naroff1926c832007-04-24 00:23:05 +00001373/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001374/// This routine allows us to typecheck complex/recursive expressions
1375/// where the declaration is needed for type checking. Here are some
1376/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001377static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001378 switch (e->getStmtClass()) {
1379 case Stmt::DeclRefExprClass:
1380 return cast<DeclRefExpr>(e)->getDecl();
1381 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001382 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001383 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001384 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001385 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001386 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001387 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001388 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001389 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001390 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001391 default:
1392 return 0;
1393 }
1394}
1395
1396/// CheckAddressOfOperand - The operand of & must be either a function
1397/// designator or an lvalue designating an object. If it is an lvalue, the
1398/// object cannot be declared with storage class register or be a bit field.
1399/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001400/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001401QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001402 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001403 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001404
Steve Naroff9358c712007-05-27 23:58:33 +00001405 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001406 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1407 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001408 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001409 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1410 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001411 return QualType();
1412 }
Steve Naroff47500512007-04-19 23:00:49 +00001413 } else if (dcl) {
1414 // We have an lvalue with a decl. Make sure the decl is not declared
1415 // with the register storage-class specifier.
1416 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001417 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001418 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001419 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001420 return QualType();
1421 }
Steve Narofff633d092007-04-25 19:01:39 +00001422 } else
1423 assert(0 && "Unknown/unexpected decl type");
1424
Steve Naroff47500512007-04-19 23:00:49 +00001425 // FIXME: add check for bitfields!
1426 }
1427 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001428 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001429}
1430
Steve Naroff35d85152007-05-07 00:24:15 +00001431QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001432 UsualUnaryConversions(op);
1433 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001434
Chris Lattnerc996b172007-07-31 16:53:04 +00001435 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001436 QualType ptype = PT->getPointeeType();
1437 // C99 6.5.3.2p4. "if it points to an object,...".
1438 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1439 // GCC compat: special case 'void *' (treat as warning).
1440 if (ptype->isVoidType()) {
1441 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001442 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001443 } else {
1444 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001445 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001446 return QualType();
1447 }
1448 }
1449 return ptype;
1450 }
1451 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001452 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001453 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001454}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001455
1456static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1457 tok::TokenKind Kind) {
1458 BinaryOperator::Opcode Opc;
1459 switch (Kind) {
1460 default: assert(0 && "Unknown binop!");
1461 case tok::star: Opc = BinaryOperator::Mul; break;
1462 case tok::slash: Opc = BinaryOperator::Div; break;
1463 case tok::percent: Opc = BinaryOperator::Rem; break;
1464 case tok::plus: Opc = BinaryOperator::Add; break;
1465 case tok::minus: Opc = BinaryOperator::Sub; break;
1466 case tok::lessless: Opc = BinaryOperator::Shl; break;
1467 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1468 case tok::lessequal: Opc = BinaryOperator::LE; break;
1469 case tok::less: Opc = BinaryOperator::LT; break;
1470 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1471 case tok::greater: Opc = BinaryOperator::GT; break;
1472 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1473 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1474 case tok::amp: Opc = BinaryOperator::And; break;
1475 case tok::caret: Opc = BinaryOperator::Xor; break;
1476 case tok::pipe: Opc = BinaryOperator::Or; break;
1477 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1478 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1479 case tok::equal: Opc = BinaryOperator::Assign; break;
1480 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1481 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1482 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1483 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1484 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1485 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1486 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1487 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1488 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1489 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1490 case tok::comma: Opc = BinaryOperator::Comma; break;
1491 }
1492 return Opc;
1493}
1494
Steve Naroff35d85152007-05-07 00:24:15 +00001495static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1496 tok::TokenKind Kind) {
1497 UnaryOperator::Opcode Opc;
1498 switch (Kind) {
1499 default: assert(0 && "Unknown unary op!");
1500 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1501 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1502 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1503 case tok::star: Opc = UnaryOperator::Deref; break;
1504 case tok::plus: Opc = UnaryOperator::Plus; break;
1505 case tok::minus: Opc = UnaryOperator::Minus; break;
1506 case tok::tilde: Opc = UnaryOperator::Not; break;
1507 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1508 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1509 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1510 case tok::kw___real: Opc = UnaryOperator::Real; break;
1511 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001512 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001513 }
1514 return Opc;
1515}
1516
Steve Naroff218bc2b2007-05-04 21:54:46 +00001517// Binary Operators. 'Tok' is the token for the operator.
1518Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1519 ExprTy *LHS, ExprTy *RHS) {
1520 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1521 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1522
1523 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1524 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1525
Chris Lattner256c21b2007-06-28 03:53:10 +00001526 QualType ResultTy; // Result type of the binary operator.
1527 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001528
1529 switch (Opc) {
1530 default:
1531 assert(0 && "Unknown binary expr!");
1532 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001533 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001534 break;
1535 case BinaryOperator::Mul:
1536 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001537 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001538 break;
1539 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001540 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001541 break;
1542 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001543 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001544 break;
1545 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001546 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001547 break;
1548 case BinaryOperator::Shl:
1549 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001550 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001551 break;
1552 case BinaryOperator::LE:
1553 case BinaryOperator::LT:
1554 case BinaryOperator::GE:
1555 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001556 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001557 break;
1558 case BinaryOperator::EQ:
1559 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001560 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001561 break;
1562 case BinaryOperator::And:
1563 case BinaryOperator::Xor:
1564 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001565 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001566 break;
1567 case BinaryOperator::LAnd:
1568 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001569 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001570 break;
1571 case BinaryOperator::MulAssign:
1572 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001573 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001574 if (!CompTy.isNull())
1575 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001576 break;
1577 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001578 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001579 if (!CompTy.isNull())
1580 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001581 break;
1582 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001583 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001584 if (!CompTy.isNull())
1585 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001586 break;
1587 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001588 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001589 if (!CompTy.isNull())
1590 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001591 break;
1592 case BinaryOperator::ShlAssign:
1593 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001594 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001595 if (!CompTy.isNull())
1596 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001597 break;
1598 case BinaryOperator::AndAssign:
1599 case BinaryOperator::XorAssign:
1600 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001601 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001602 if (!CompTy.isNull())
1603 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001604 break;
1605 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001606 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001607 break;
1608 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001609 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001610 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001611 if (CompTy.isNull())
Chris Lattnerc11005f2007-08-28 18:36:55 +00001612 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner256c21b2007-06-28 03:53:10 +00001613 else
Chris Lattnerc11005f2007-08-28 18:36:55 +00001614 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001615}
1616
Steve Naroff35d85152007-05-07 00:24:15 +00001617// Unary Operators. 'Tok' is the token for the operator.
1618Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001619 ExprTy *input) {
1620 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001621 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1622 QualType resultType;
1623 switch (Opc) {
1624 default:
1625 assert(0 && "Unimplemented unary expr!");
1626 case UnaryOperator::PreInc:
1627 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001628 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001629 break;
1630 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001631 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001632 break;
1633 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001634 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001635 break;
1636 case UnaryOperator::Plus:
1637 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001638 UsualUnaryConversions(Input);
1639 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001640 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001641 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1642 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001643 break;
1644 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001645 UsualUnaryConversions(Input);
1646 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00001647 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1648 if (!resultType->isIntegerType()) {
1649 if (resultType->isComplexType())
1650 // C99 does not support '~' for complex conjugation.
1651 Diag(OpLoc, diag::ext_integer_complement_complex,
1652 resultType.getAsString());
1653 else
1654 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1655 resultType.getAsString());
1656 }
Steve Naroff35d85152007-05-07 00:24:15 +00001657 break;
1658 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001659 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001660 DefaultFunctionArrayConversion(Input);
1661 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001662 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001663 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1664 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001665 // LNot always has type int. C99 6.5.3.3p5.
1666 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001667 break;
1668 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001669 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001670 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001671 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001672 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1673 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00001674 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00001675 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00001676 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00001677 break;
Chris Lattner86554282007-06-08 22:32:33 +00001678 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00001679 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001680 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001681 }
1682 if (resultType.isNull())
1683 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001684 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001685}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001686
1687/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1688Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1689 SourceLocation LabLoc,
1690 IdentifierInfo *LabelII) {
1691 // Look up the record for this label identifier.
1692 LabelStmt *&LabelDecl = LabelMap[LabelII];
1693
1694 // If we haven't seen this label yet, create a forward reference.
1695 if (LabelDecl == 0)
1696 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1697
1698 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001699 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1700 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001701}
1702
Chris Lattner366727f2007-07-24 16:58:17 +00001703Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
1704 SourceLocation RPLoc) { // "({..})"
1705 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1706 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1707 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1708
1709 // FIXME: there are a variety of strange constraints to enforce here, for
1710 // example, it is not possible to goto into a stmt expression apparently.
1711 // More semantic analysis is needed.
1712
1713 // FIXME: the last statement in the compount stmt has its value used. We
1714 // should not warn about it being unused.
1715
1716 // If there are sub stmts in the compound stmt, take the type of the last one
1717 // as the type of the stmtexpr.
1718 QualType Ty = Context.VoidTy;
1719
1720 if (!Compound->body_empty())
1721 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1722 Ty = LastExpr->getType();
1723
1724 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1725}
Steve Naroff78864672007-08-01 22:05:33 +00001726
Chris Lattnerf17bd422007-08-30 17:45:32 +00001727Sema::ExprResult Sema::ParseBuiltinOffsetOf(SourceLocation BuiltinLoc,
1728 SourceLocation TypeLoc,
1729 TypeTy *argty,
1730 OffsetOfComponent *CompPtr,
1731 unsigned NumComponents,
1732 SourceLocation RPLoc) {
1733 QualType ArgTy = QualType::getFromOpaquePtr(argty);
1734 assert(!ArgTy.isNull() && "Missing type argument!");
1735
1736 // We must have at least one component that refers to the type, and the first
1737 // one is known to be a field designator. Verify that the ArgTy represents
1738 // a struct/union/class.
1739 if (!ArgTy->isRecordType())
1740 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
1741
1742 // Otherwise, create a compound literal expression as the base, and
1743 // iteratively process the offsetof designators.
1744 Expr *Res = new CompoundLiteralExpr(ArgTy, 0);
1745
Chris Lattner78502cf2007-08-31 21:49:13 +00001746 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
1747 // GCC extension, diagnose them.
1748 if (NumComponents != 1)
1749 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
1750 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
1751
Chris Lattnerf17bd422007-08-30 17:45:32 +00001752 for (unsigned i = 0; i != NumComponents; ++i) {
1753 const OffsetOfComponent &OC = CompPtr[i];
1754 if (OC.isBrackets) {
1755 // Offset of an array sub-field. TODO: Should we allow vector elements?
1756 const ArrayType *AT = Res->getType()->getAsArrayType();
1757 if (!AT) {
1758 delete Res;
1759 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
1760 Res->getType().getAsString());
1761 }
1762
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001763 // FIXME: C++: Verify that operator[] isn't overloaded.
1764
Chris Lattnerf17bd422007-08-30 17:45:32 +00001765 // C99 6.5.2.1p1
1766 Expr *Idx = static_cast<Expr*>(OC.U.E);
1767 if (!Idx->getType()->isIntegerType())
1768 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
1769 Idx->getSourceRange());
1770
1771 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
1772 continue;
1773 }
1774
1775 const RecordType *RC = Res->getType()->getAsRecordType();
1776 if (!RC) {
1777 delete Res;
1778 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
1779 Res->getType().getAsString());
1780 }
1781
1782 // Get the decl corresponding to this.
1783 RecordDecl *RD = RC->getDecl();
1784 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
1785 if (!MemberDecl)
1786 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
1787 OC.U.IdentInfo->getName(),
1788 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001789
1790 // FIXME: C++: Verify that MemberDecl isn't a static field.
1791 // FIXME: Verify that MemberDecl isn't a bitfield.
1792
Chris Lattnerf17bd422007-08-30 17:45:32 +00001793 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
1794 }
1795
1796 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
1797 BuiltinLoc);
1798}
1799
1800
Steve Naroff788d8642007-08-01 23:45:51 +00001801Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00001802 TypeTy *arg1, TypeTy *arg2,
1803 SourceLocation RPLoc) {
1804 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1805 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1806
1807 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1808
Chris Lattnerf17bd422007-08-30 17:45:32 +00001809 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00001810}
1811
Steve Naroff9efdabc2007-08-03 21:21:27 +00001812Sema::ExprResult Sema::ParseChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
1813 ExprTy *expr1, ExprTy *expr2,
1814 SourceLocation RPLoc) {
1815 Expr *CondExpr = static_cast<Expr*>(cond);
1816 Expr *LHSExpr = static_cast<Expr*>(expr1);
1817 Expr *RHSExpr = static_cast<Expr*>(expr2);
1818
1819 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
1820
1821 // The conditional expression is required to be a constant expression.
1822 llvm::APSInt condEval(32);
1823 SourceLocation ExpLoc;
1824 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
1825 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
1826 CondExpr->getSourceRange());
1827
1828 // If the condition is > zero, then the AST type is the same as the LSHExpr.
1829 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
1830 RHSExpr->getType();
1831 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
1832}
1833
Anders Carlsson76f4a902007-08-21 17:43:55 +00001834// TODO: Move this to SemaObjC.cpp
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001835Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001836 StringLiteral* S = static_cast<StringLiteral *>(string);
1837
1838 if (CheckBuiltinCFStringArgument(S))
1839 return true;
1840
1841 QualType t = Context.getCFConstantStringType();
1842 t = t.getQualifiedType(QualType::Const);
1843 t = Context.getPointerType(t);
1844
1845 return new ObjCStringLiteral(S, t);
1846}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001847
1848Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
1849 SourceLocation LParenLoc,
1850 TypeTy *Ty,
1851 SourceLocation RParenLoc) {
1852 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
1853
1854 QualType t = Context.getPointerType(Context.CharTy);
1855 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
1856}